-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
support c-variadic functions in rustc_const_eval
#150601
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
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
d30044f to
5f98625
Compare
This comment has been minimized.
This comment has been minimized.
5f98625 to
3368321
Compare
This comment has been minimized.
This comment has been minimized.
3368321 to
41a34bc
Compare
This comment has been minimized.
This comment has been minimized.
41a34bc to
8608ba7
Compare
This comment has been minimized.
This comment has been minimized.
8608ba7 to
207b032
Compare
|
☔ The latest upstream changes made this pull request unmergeable. Please resolve the merge conflicts. |
207b032 to
1794556
Compare
This comment has been minimized.
This comment has been minimized.
b297adc to
c081ba3
Compare
This comment has been minimized.
This comment has been minimized.
c081ba3 to
293ba18
Compare
|
This PR changes rustc_public cc @oli-obk, @celinval, @ouz-a, @makai410 Some changes occurred to the CTFE machinery Some changes occurred in compiler/rustc_codegen_cranelift cc @bjorn3 Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr The Miri subtree was changed cc @rust-lang/miri Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr Some changes occurred in compiler/rustc_codegen_gcc |
|
☔ The latest upstream changes (presumably #146923) made this pull request unmergeable. Please resolve the merge conflicts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This'll need another rebase once the va_copy/va_end PR merges. I'm mostly wondering
- whether things are in roughly the right place now
- if the tests cover what you'd expect
| self.float_muladd_intrinsic::<Quad>(args, dest, MulAddType::Nondeterministic)? | ||
| } | ||
|
|
||
| sym::va_copy => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code looks kind of rough/unpolished, I'm probably just missing some convenient helpers.
| const unsafe extern "C" fn helper(ap: ...) { | ||
| // A copy created using Clone is valid, and can be used to read arguments. | ||
| let mut copy = ap.clone(); | ||
| assert!(copy.arg::<i32>() == 1i32); | ||
|
|
||
| let mut u = core::mem::MaybeUninit::uninit(); | ||
| unsafe { core::ptr::copy_nonoverlapping(&ap, u.as_mut_ptr(), 1) }; | ||
|
|
||
| // Manually creating the copy is fine. | ||
| let mut copy = unsafe { u.assume_init() }; | ||
|
|
||
| // Using the copy is actually fine. | ||
| let _ = copy.arg::<i32>(); | ||
| drop(copy); | ||
|
|
||
| // But then using the original is UB. | ||
| drop(ap); | ||
| } | ||
|
|
||
| const { unsafe { helper(1, 2, 3) } }; | ||
| //~^ ERROR va_end on unknown va_list allocation ALLOC0 [E0080] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is what we discussed? A copy actually is fine, but using the copy and then the original (or vice versa) is UB.
|
|
||
| // Using the copy is actually fine. | ||
| let _ = copy.arg::<i32>(); | ||
| drop(copy); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| drop(copy); | |
| mem::forget(copy); |
Does it still catch the UB if you avoid the double-drop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does, the copy.arg<i32>() deallocates the original allocation, so when the original gets dropped it's referencing a non-existent allocation and that gets reported. I'll add an additional test for it.
532c69e to
57c69f3
Compare
| drop(copy); | ||
|
|
||
| // But then using the original is UB. | ||
| drop(ap); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test that I expected would call va_arg on op again here, before dropping it. The va_arg should be UB because the iterator was already advanced via the copy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've also added a test now that first reads from the copy, and then from the original.
This comment has been minimized.
This comment has been minimized.
however `Drop` for `VaList` is not yet available in const fn
this does not detect UB yet when a `VaList` is copied manually
2763963 to
9e57c73
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
| /// Map for "extra" function pointers. | ||
| extra_fn_ptr_map: FxIndexMap<AllocId, M::ExtraFnVal>, | ||
|
|
||
| pub(crate) va_list_map: FxIndexMap<AllocId, Vec<MPlaceTy<'tcx, M::Provenance>>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, you have a separate map here to give "meaning" to a VaList allocation... that's the part I missed earlier.
If you have that... why do you even need the new variant in GlobalAlloc? I know I told you to add that, but I also didn't expect a va_list_map field. I like the idea with the field, but it's not clear to me that we need both the field and the new kind of GlobalAlloc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An allocation is still needed to make the pointer. None of the other variants fit nicely I think, but maybe I'm just missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A GlobalAlloc is only needed for global allocations, as the name suggests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, how should I get an AllocId then? something like this?
let (alloc_id, _, _) = {
let f = self.layout_of(self.tcx.types.unit).unwrap();
let mplace = MPlaceTy::fake_alloc_zst(f);
let ptr = mplace.ptr();
self.ptr_get_alloc_id(ptr, 0)?
};this specifically causes
pointer not dereferenceable: pointer must point to some allocation, but got 0x1[noalloc] which is a dangling pointer (it has no provenance)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other approaches frustratingly create a Pointer instead of a Pointer:<M::Provemance>...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your new va_list_map works basically like the existing extra_fn_ptr_map, so you can copy what we do for that:
rust/compiler/rustc_const_eval/src/interpret/memory.rs
Lines 221 to 222 in 7ec34de
| let id = self.tcx.reserve_alloc_id(); | |
| let old = self.memory.extra_fn_ptr_map.insert(id, extra); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please keep va_list_map private to this file and have methods similar to the existing special allocation kinds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the above, you still get a Pointer<CmseProvenance>. Other code uses global_root_pointer to turn that into a Pointer<M::Provenance>, but that fails when the pointer is not actually a global allocation. Function pointers would still use
pub enum GlobalAlloc<'tcx> {
/// The alloc ID is used as a function pointer.
Function { instance: Instance<'tcx> },
// ...
}I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, I found the assert that needs to change, nvm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will have to add a new variant to this enum and take that into account in Miri.
a3a75ad to
19347f2
Compare
|
@rustbot author (I haven't yet had a chance to look at the code in detail, but given the larger structural issues it's probably better tog et those right before I do the detailed review.) |
|
Reminder, once the PR becomes ready for a review, use |
This comment has been minimized.
This comment has been minimized.
19347f2 to
83db12a
Compare
tracking issue: #44930
The new
GlobalAlloc::VaListis used to create anAllocIdthat represents the variable argument list of a frame. The allocation itself does not store any data, all we need is the unique identifier.The actual variable argument list is stored in
Memory, and keyed by theAllocId. TheFramealso stores thisAllocId, so that when a frame is popped, it can deallocate the variable arguments.At "runtime" a
VaListvalue stores a pointer to the global allocation in its first bytes. The provenance on this pointer can be used to retrieve itsAllocId, and the offset of the pointer is used to store the index of the next argument to read from the variable argument list.Miri does not yet support
va_arg, but I think that can be done separetely?r? @RalfJung
cc @workingjubilee