Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen: handle dead code with unsafe_store of FCA pointers #50164

Merged
merged 2 commits into from
Jun 22, 2023
Merged

Conversation

vtjnash
Copy link
Member

@vtjnash vtjnash commented Jun 14, 2023

Fix #50125

@vchuravy
Copy link
Member

Can you add the tests from #50244

@vtjnash
Copy link
Member Author

vtjnash commented Jun 21, 2023

Those tests are either not legal or already pass on master

@NHDaly
Copy link
Member

NHDaly commented Jun 22, 2023

Cool, glad to see a fix coming up! :)
Ah, funny timing that you were doing this at the same time as me.

@NHDaly
Copy link
Member

NHDaly commented Jun 22, 2023

Can you explain what's wrong with the tests in #50244?

This first one seems legal from what I understand:

# issue #50243
struct ImmutableNonIsBits
    v::Vector{Int}
end
const ref50243 = Ref(ImmutableNonIsBits([1,2,3]))
load50243() = unsafe_load(Ptr{ImmutableNonIsBits}(pointer_from_objref(ref50243)))
sum50243() = sum(load50243().v)  # (Do something with v to prevent the compiler from eliding)
@test load50243() === ref50243[]
@test sum50243() === 6
@test @allocated(sum50243()) === 0

Is this fundamentally different than doing the same trick with an isbits type, like an NTuple?

And i think this second one is possibly more illegal, but i'm not 100% clear why.

const ref50243 = Ref(ImmutableNonIsBits([1,2,3]))
store50243!(v2) = unsafe_store!(Ptr{ImmutableNonIsBits}(pointer_from_objref(ref50243)), v2)
new50243 = ImmutableNonIsBits([1])
store50243!(new50243)
@test ref50243[] === new50243
@test @allocated(store50243!(ref50243[])) === 0

... But neither of them pass on master - what makes you say that?

@vtjnash
Copy link
Member Author

vtjnash commented Jun 22, 2023

The first tests pass on master for me, except for the allocated bytes test which is not semantically guaranteed. The unsafe_store! has the wrong memory provenance for that, so it is UB. The first test also might have the wrong memory provenance, but it is only a read, so it is potentially unclear right now if that is legal or not.

@vtjnash vtjnash merged commit ef6d900 into master Jun 22, 2023
@vtjnash vtjnash deleted the jn/50125 branch June 22, 2023 14:17
@NHDaly
Copy link
Member

NHDaly commented Jun 22, 2023

Thanks, makes sense.

The first tests pass on master for me, except for the allocated bytes test which is not semantically guaranteed.

^ Yeah, this is specifically what I was trying to test. Even though it's not semantically guaranteed, I assume that it's something we want to preserve in our implementation of julia, which is why I thought it might be good to test.

But maybe the goal with the test suite is that it should pass under any implementation of julia that follows the spec? Which does make sense 👍

@vtjnash
Copy link
Member Author

vtjnash commented Jun 22, 2023

It would likely fail currently under the interpreter and/or runtime implementations of this, so it is not an ideal test.

@NHDaly
Copy link
Member

NHDaly commented Jun 26, 2023

Thanks all.


One more question: should this be backported to 1.9? Since it's a bugfix? (It's also a perf fix for unsafe_load(::Ptr{<immutable>}), and I think those aren't usually backported.) But it's also a bug fix for the case that crashes?

@vtjnash vtjnash added the backport 1.9 Change should be backported to release-1.9 label Jun 26, 2023
@KristofferC KristofferC mentioned this pull request Jul 11, 2023
35 tasks
KristofferC pushed a commit that referenced this pull request Aug 10, 2023
KristofferC added a commit that referenced this pull request Aug 18, 2023
Backported PRs:
- [x] #47782 <!-- Generalize Bool parse method to AbstractString -->
- [x] #48634 <!-- Remove unused "deps" mechanism in internal sorting
keywords [NFC] -->
- [x] #49931 <!-- Lock finalizers' lists at exit -->
- [x] #50064 <!-- Fix numbered prompt with input only with comment -->
- [x] #50474 <!-- docs: Fix a `!!! note` which was miscapitalized -->
- [x] #50516 <!-- Fix visibility of assert on GCC12/13 -->
- [x] #50635 <!-- `versioninfo()`: include build info and unofficial
warning -->
- [x] #49915 <!-- Revert "Remove number / vector (#44358)" -->
- [x] #50781 <!-- fix `bit_map!` with aliasing -->
- [x] #50845 <!-- fix #50438, use default pool for at-threads -->
- [x] #49031 <!-- Update inference.md -->
- [x] #50289 <!-- Initialize prev_nold and nold in gc_reset_page -->
- [x] #50559 <!-- Expand kwcall lowering positional default check to
vararg -->
- [x] #49582 <!-- Update HISTORY.md for `DelimitedFiles` -->
- [x] #50341 <!-- invokelatest docs should say not exported before 1.9
-->
- [x] #50525 <!-- only check that values are finite in `generic_lufact`
when `check=true` -->
- [x] #50444 <!-- Optimize getfield lowering to avoid boxing in some
cases -->
- [x] #50523 <!-- Avoid generic call in most cases for getproperty -->
- [x] #50860 <!-- Add `Base.get_extension` to docs/API -->
- [x] #50164 <!-- codegen: handle dead code with unsafe_store of FCA
pointers -->
- [x] #50568 <!-- `Array(::AbstractRange)` should return an `Array` -->
- [x] #50871 <!-- macOS: Don't inspect dead threadtls during exception
handling. -->

Need manual backport:
- [ ] #48542 <!-- Add docs on task-specific buffering using
multithreading -->
- [ ] #50591 <!-- build: fix various makefile bugs -->


Non-merged PRs with backport label:
- [ ] #50842 <!-- Avoid race conditions with recursive rm -->
- [ ] #50823 <!-- Make ranges more robust with unsigned indexes. -->
- [ ] #50663 <!-- Fix Expr(:loopinfo) codegen -->
- [ ] #49716 <!-- Update varinfo() docstring signature -->
- [ ] #49713 <!-- prevent REPL from erroring in numbered mode in some
situations -->
- [ ] #49573 <!-- Implement jl_cpu_pause on PPC64 -->
- [ ] #48726 <!-- fix macro expansion of property destructuring -->
- [ ] #48642 <!-- Use gc alloc instead of alloc typed in lowering -->
- [ ] #48183 <!-- Don't use pkgimage for package if any includes fall in
tracked path for coverage or alloc tracking -->
- [ ] #48050 <!-- improve `--heap-size-hint` arg handling -->
- [ ] #47615 <!-- Allow threadsafe access to buffer of type inference
profiling trees -->
@KristofferC KristofferC removed the backport 1.9 Change should be backported to release-1.9 label Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unsafe pointers with non-isbits may crash
4 participants