Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/tools/miri/tests/pass/c-variadic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ fn various_types() {
}
}

fn equal_up_to_free_lifetime() {
// Types are considered equal up to free lifetimes: `*const &'static str`
// is the same as `*const &'a str`.
// Bound lifetimes (using e.g. `for<'_>`) are different.
#[expect(improper_ctypes_definitions)]
pub unsafe extern "C" fn foo(mut args: ...) -> &'static str {
unsafe { *args.next_arg::<*const &'static str>() }
}

let data = String::from("abc");
let x: &str = data.as_str();
assert_eq!(unsafe { foo(&raw const x) }, "abc");
}

fn clone() {
if cfg!(force_intrinsic_fallback) {
// Skip this test when we use the fallback bodies. The fallback body does
Expand Down Expand Up @@ -170,6 +184,7 @@ fn main() {
forward_by_ref();
nested();
various_types();
equal_up_to_free_lifetime();
clone();
clone_and_advance();
}
10 changes: 10 additions & 0 deletions tests/ui/consts/const-eval/c-variadic-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ unsafe fn read_cast_pointer() {
//~^ ERROR requested `*const u8` is incompatible with next argument of type `usize`
}

unsafe fn read_cast_lifetime() {
// The types are equal up to free lifetimes.
const { read_as::<*const &'static i32>(std::ptr::dangling::<&i32>()) };

// Bound lifetimes do matter.
const { read_as::<*const fn(&'static ())>(std::ptr::dangling::<for<'a> fn(&'a ())>()) };
//~^ ERROR va_arg type mismatch: requested `*const fn(&())` is incompatible with next argument of type `*const for<'a> fn(&'a ())`
}

fn use_after_free() {
const unsafe extern "C" fn helper(ap: ...) -> [u8; size_of::<VaList>()] {
unsafe { std::mem::transmute(ap) }
Expand Down Expand Up @@ -196,6 +205,7 @@ fn main() {
read_too_many();
read_cast_numeric();
read_cast_pointer();
read_cast_lifetime();
manual_copy_read();
manual_copy_drop();
manual_copy_forget();
Expand Down
66 changes: 47 additions & 19 deletions tests/ui/consts/const-eval/c-variadic-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,36 @@ LL | const { read_as::<*const u8>(1usize) };
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0080]: va_arg type mismatch: requested `*const fn(&())` is incompatible with next argument of type `*const for<'a> fn(&'a ())`
--> $DIR/c-variadic-fail.rs:127:13
|
LL | const { read_as::<*const fn(&'static ())>(std::ptr::dangling::<for<'a> fn(&'a ())>()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `read_cast_lifetime::{constant#1}` failed inside this call
|
note: inside `read_as::<*const fn(&())>`
--> $DIR/c-variadic-fail.rs:37:5
|
LL | ap.next_arg::<T>()
| ^^^^^^^^^^^^^^^^^^
note: inside `VaList::<'_>::next_arg::<*const fn(&())>`
--> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:127:5
|
LL | const { read_as::<*const fn(&'static ())>(std::ptr::dangling::<for<'a> fn(&'a ())>()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:127:5
|
LL | const { read_as::<*const fn(&'static ())>(std::ptr::dangling::<for<'a> fn(&'a ())>()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0080]: memory access failed: ALLOC0 has been freed, so this pointer is dangling
--> $DIR/c-variadic-fail.rs:131:13
--> $DIR/c-variadic-fail.rs:140:13
|
LL | ap.next_arg::<i32>();
| ^^^^^^^^^^^^^^^^^^^^ evaluation of `use_after_free::{constant#0}` failed inside this call
Expand All @@ -400,7 +428,7 @@ note: inside `VaList::<'_>::next_arg::<i32>`
--> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:127:5
--> $DIR/c-variadic-fail.rs:136:5
|
LL | / const {
LL | | unsafe {
Expand All @@ -411,7 +439,7 @@ LL | | };
| |_____^

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:127:5
--> $DIR/c-variadic-fail.rs:136:5
|
LL | / const {
LL | | unsafe {
Expand All @@ -424,13 +452,13 @@ LL | | };
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0080]: using ALLOC1 as variable argument list pointer but it does not point to a variable argument list
--> $DIR/c-variadic-fail.rs:153:22
--> $DIR/c-variadic-fail.rs:162:22
|
LL | const { unsafe { helper(1, 2, 3) } };
| ^^^^^^^^^^^^^^^ evaluation of `manual_copy_drop::{constant#0}` failed inside this call
|
note: inside `manual_copy_drop::helper`
--> $DIR/c-variadic-fail.rs:150:9
--> $DIR/c-variadic-fail.rs:159:9
|
LL | drop(ap);
| ^^^^^^^^
Expand All @@ -442,27 +470,27 @@ note: inside `<VaList<'_> as Drop>::drop`
--> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:153:5
--> $DIR/c-variadic-fail.rs:162:5
|
LL | const { unsafe { helper(1, 2, 3) } };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:153:5
--> $DIR/c-variadic-fail.rs:162:5
|
LL | const { unsafe { helper(1, 2, 3) } };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0080]: using ALLOC2 as variable argument list pointer but it does not point to a variable argument list
--> $DIR/c-variadic-fail.rs:169:22
--> $DIR/c-variadic-fail.rs:178:22
|
LL | const { unsafe { helper(1, 2, 3) } };
| ^^^^^^^^^^^^^^^ evaluation of `manual_copy_forget::{constant#0}` failed inside this call
|
note: inside `manual_copy_forget::helper`
--> $DIR/c-variadic-fail.rs:166:9
--> $DIR/c-variadic-fail.rs:175:9
|
LL | drop(ap);
| ^^^^^^^^
Expand All @@ -474,49 +502,49 @@ note: inside `<VaList<'_> as Drop>::drop`
--> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:169:5
--> $DIR/c-variadic-fail.rs:178:5
|
LL | const { unsafe { helper(1, 2, 3) } };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:169:5
--> $DIR/c-variadic-fail.rs:178:5
|
LL | const { unsafe { helper(1, 2, 3) } };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0080]: using ALLOC3 as variable argument list pointer but it does not point to a variable argument list
--> $DIR/c-variadic-fail.rs:182:22
--> $DIR/c-variadic-fail.rs:191:22
|
LL | const { unsafe { helper(1, 2, 3) } };
| ^^^^^^^^^^^^^^^ evaluation of `manual_copy_read::{constant#0}` failed inside this call
|
note: inside `manual_copy_read::helper`
--> $DIR/c-variadic-fail.rs:179:17
--> $DIR/c-variadic-fail.rs:188:17
|
LL | let _ = ap.next_arg::<i32>();
| ^^^^^^^^^^^^^^^^^^^^
note: inside `VaList::<'_>::next_arg::<i32>`
--> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:182:5
--> $DIR/c-variadic-fail.rs:191:5
|
LL | const { unsafe { helper(1, 2, 3) } };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:182:5
--> $DIR/c-variadic-fail.rs:191:5
|
LL | const { unsafe { helper(1, 2, 3) } };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0080]: pointer not dereferenceable: pointer must point to some allocation, but got null pointer
--> $DIR/c-variadic-fail.rs:190:5
--> $DIR/c-variadic-fail.rs:199:5
|
LL | }
| ^ evaluation of `drop_of_invalid::{constant#0}` failed inside this call
Expand All @@ -527,7 +555,7 @@ note: inside `<VaList<'_> as Drop>::drop`
--> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:187:5
--> $DIR/c-variadic-fail.rs:196:5
|
LL | / const {
LL | | let mut invalid: MaybeUninit<VaList> = MaybeUninit::zeroed();
Expand All @@ -536,7 +564,7 @@ LL | | }
| |_____^

note: erroneous constant encountered
--> $DIR/c-variadic-fail.rs:187:5
--> $DIR/c-variadic-fail.rs:196:5
|
LL | / const {
LL | | let mut invalid: MaybeUninit<VaList> = MaybeUninit::zeroed();
Expand All @@ -546,6 +574,6 @@ LL | | }
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 19 previous errors
error: aborting due to 20 previous errors

For more information about this error, try `rustc --explain E0080`.
Loading