diff options
Diffstat (limited to 'tests/codegen/cffi')
-rw-r--r-- | tests/codegen/cffi/c-variadic-copy.rs | 16 | ||||
-rw-r--r-- | tests/codegen/cffi/c-variadic-opt.rs | 30 | ||||
-rw-r--r-- | tests/codegen/cffi/c-variadic.rs | 72 | ||||
-rw-r--r-- | tests/codegen/cffi/ffi-const.rs | 13 | ||||
-rw-r--r-- | tests/codegen/cffi/ffi-out-of-bounds-loads.rs | 25 | ||||
-rw-r--r-- | tests/codegen/cffi/ffi-pure.rs | 13 | ||||
-rw-r--r-- | tests/codegen/cffi/ffi-returns-twice.rs | 11 |
7 files changed, 180 insertions, 0 deletions
diff --git a/tests/codegen/cffi/c-variadic-copy.rs b/tests/codegen/cffi/c-variadic-copy.rs new file mode 100644 index 000000000..4c61c4fcf --- /dev/null +++ b/tests/codegen/cffi/c-variadic-copy.rs @@ -0,0 +1,16 @@ +// Tests that `VaListImpl::clone` gets inlined into a call to `llvm.va_copy` + +#![crate_type = "lib"] +#![feature(c_variadic)] +#![no_std] +use core::ffi::VaList; + +extern "C" { + fn foreign_c_variadic_1(_: VaList, ...); +} + +pub unsafe extern "C" fn clone_variadic(ap: VaList) { + let mut ap2 = ap.clone(); + // CHECK: call void @llvm.va_copy + foreign_c_variadic_1(ap2.as_va_list(), 42i32); +} diff --git a/tests/codegen/cffi/c-variadic-opt.rs b/tests/codegen/cffi/c-variadic-opt.rs new file mode 100644 index 000000000..969dce80f --- /dev/null +++ b/tests/codegen/cffi/c-variadic-opt.rs @@ -0,0 +1,30 @@ +// compile-flags: -C opt-level=3 + +#![crate_type = "lib"] +#![feature(c_variadic)] +#![no_std] +use core::ffi::VaList; + +extern "C" { + fn vprintf(fmt: *const i8, ap: VaList) -> i32; +} + +// Ensure that `va_start` and `va_end` are properly injected even +// when the "spoofed" `VaListImpl` is not used. +#[no_mangle] +pub unsafe extern "C" fn c_variadic_no_use(fmt: *const i8, mut ap: ...) -> i32 { + // CHECK: call void @llvm.va_start + vprintf(fmt, ap.as_va_list()) + // CHECK: call void @llvm.va_end +} + +// Check that `VaListImpl::clone` gets inlined into a direct call to `llvm.va_copy` +#[no_mangle] +pub unsafe extern "C" fn c_variadic_clone(fmt: *const i8, mut ap: ...) -> i32 { + // CHECK: call void @llvm.va_start + let mut ap2 = ap.clone(); + // CHECK: call void @llvm.va_copy + let res = vprintf(fmt, ap2.as_va_list()); + res + // CHECK: call void @llvm.va_end +} diff --git a/tests/codegen/cffi/c-variadic.rs b/tests/codegen/cffi/c-variadic.rs new file mode 100644 index 000000000..cab326522 --- /dev/null +++ b/tests/codegen/cffi/c-variadic.rs @@ -0,0 +1,72 @@ +// ignore-wasm32-bare compiled with panic=abort by default +// compile-flags: -C no-prepopulate-passes -Copt-level=0 +// + +#![crate_type = "lib"] +#![feature(c_variadic)] +#![feature(c_unwind)] +#![no_std] +use core::ffi::VaList; + +extern "C" { + fn foreign_c_variadic_0(_: i32, ...); + fn foreign_c_variadic_1(_: VaList, ...); +} + +pub unsafe extern "C" fn use_foreign_c_variadic_0() { + // Ensure that we correctly call foreign C-variadic functions. + // CHECK: call void (i32, ...) @foreign_c_variadic_0([[PARAM:i32( signext)?]] 0) + foreign_c_variadic_0(0); + // CHECK: call void (i32, ...) @foreign_c_variadic_0([[PARAM]] 0, [[PARAM]] 42) + foreign_c_variadic_0(0, 42i32); + // CHECK: call void (i32, ...) @foreign_c_variadic_0([[PARAM]] 0, [[PARAM]] 42, [[PARAM]] 1024) + foreign_c_variadic_0(0, 42i32, 1024i32); + // CHECK: call void (i32, ...) @foreign_c_variadic_0([[PARAM]] 0, [[PARAM]] 42, [[PARAM]] 1024, [[PARAM]] 0) + foreign_c_variadic_0(0, 42i32, 1024i32, 0i32); +} + +// Ensure that we do not remove the `va_list` passed to the foreign function when +// removing the "spoofed" `VaListImpl` that is used by Rust defined C-variadics. +pub unsafe extern "C" fn use_foreign_c_variadic_1_0(ap: VaList) { + // CHECK: call void ({{.*}}, ...) @foreign_c_variadic_1({{.*}} %ap) + foreign_c_variadic_1(ap); +} + +pub unsafe extern "C" fn use_foreign_c_variadic_1_1(ap: VaList) { + // CHECK: call void ({{.*}}, ...) @foreign_c_variadic_1({{.*}} %ap, [[PARAM]] 42) + foreign_c_variadic_1(ap, 42i32); +} +pub unsafe extern "C" fn use_foreign_c_variadic_1_2(ap: VaList) { + // CHECK: call void ({{.*}}, ...) @foreign_c_variadic_1({{.*}} %ap, [[PARAM]] 2, [[PARAM]] 42) + foreign_c_variadic_1(ap, 2i32, 42i32); +} + +pub unsafe extern "C" fn use_foreign_c_variadic_1_3(ap: VaList) { + // CHECK: call void ({{.*}}, ...) @foreign_c_variadic_1({{.*}} %ap, [[PARAM]] 2, [[PARAM]] 42, [[PARAM]] 0) + foreign_c_variadic_1(ap, 2i32, 42i32, 0i32); +} + +// Ensure that `va_start` and `va_end` are properly injected. +#[no_mangle] +pub unsafe extern "C" fn c_variadic(n: i32, mut ap: ...) -> i32 { + // CHECK: call void @llvm.va_start + let mut sum = 0; + for _ in 0..n { + sum += ap.arg::<i32>(); + } + sum + // CHECK: call void @llvm.va_end +} + +// Ensure that we generate the correct `call` signature when calling a Rust +// defined C-variadic. +pub unsafe fn test_c_variadic_call() { + // CHECK: call [[RET:(signext )?i32]] (i32, ...) @c_variadic([[PARAM]] 0) + c_variadic(0); + // CHECK: call [[RET]] (i32, ...) @c_variadic([[PARAM]] 0, [[PARAM]] 42) + c_variadic(0, 42i32); + // CHECK: call [[RET]] (i32, ...) @c_variadic([[PARAM]] 0, [[PARAM]] 42, [[PARAM]] 1024) + c_variadic(0, 42i32, 1024i32); + // CHECK: call [[RET]] (i32, ...) @c_variadic([[PARAM]] 0, [[PARAM]] 42, [[PARAM]] 1024, [[PARAM]] 0) + c_variadic(0, 42i32, 1024i32, 0i32); +} diff --git a/tests/codegen/cffi/ffi-const.rs b/tests/codegen/cffi/ffi-const.rs new file mode 100644 index 000000000..937205034 --- /dev/null +++ b/tests/codegen/cffi/ffi-const.rs @@ -0,0 +1,13 @@ +// compile-flags: -C no-prepopulate-passes +#![crate_type = "lib"] +#![feature(ffi_const)] + +pub fn bar() { unsafe { foo() } } + +extern "C" { + // CHECK-LABEL: declare{{.*}}void @foo() + // CHECK-SAME: [[ATTRS:#[0-9]+]] + // The attribute changed from `readnone` to `memory(none)` with LLVM 16.0. + // CHECK-DAG: attributes [[ATTRS]] = { {{.*}}{{readnone|memory\(none\)}}{{.*}} } + #[ffi_const] pub fn foo(); +} diff --git a/tests/codegen/cffi/ffi-out-of-bounds-loads.rs b/tests/codegen/cffi/ffi-out-of-bounds-loads.rs new file mode 100644 index 000000000..099726b2f --- /dev/null +++ b/tests/codegen/cffi/ffi-out-of-bounds-loads.rs @@ -0,0 +1,25 @@ +// Regression test for #29988 + +// compile-flags: -C no-prepopulate-passes +// only-x86_64 +// ignore-windows + +#[repr(C)] +struct S { + f1: i32, + f2: i32, + f3: i32, +} + +extern "C" { + fn foo(s: S); +} + +fn main() { + let s = S { f1: 1, f2: 2, f3: 3 }; + unsafe { + // CHECK: load { i64, i32 }, {{.*}}, align 4 + // CHECK: call void @foo({ i64, i32 } {{.*}}) + foo(s); + } +} diff --git a/tests/codegen/cffi/ffi-pure.rs b/tests/codegen/cffi/ffi-pure.rs new file mode 100644 index 000000000..2ed735813 --- /dev/null +++ b/tests/codegen/cffi/ffi-pure.rs @@ -0,0 +1,13 @@ +// compile-flags: -C no-prepopulate-passes +#![crate_type = "lib"] +#![feature(ffi_pure)] + +pub fn bar() { unsafe { foo() } } + +extern "C" { + // CHECK-LABEL: declare{{.*}}void @foo() + // CHECK-SAME: [[ATTRS:#[0-9]+]] + // The attribute changed from `readonly` to `memory(read)` with LLVM 16.0. + // CHECK-DAG: attributes [[ATTRS]] = { {{.*}}{{readonly|memory\(read\)}}{{.*}} } + #[ffi_pure] pub fn foo(); +} diff --git a/tests/codegen/cffi/ffi-returns-twice.rs b/tests/codegen/cffi/ffi-returns-twice.rs new file mode 100644 index 000000000..0fbe03f0b --- /dev/null +++ b/tests/codegen/cffi/ffi-returns-twice.rs @@ -0,0 +1,11 @@ +// compile-flags: -C no-prepopulate-passes +#![crate_type = "lib"] +#![feature(ffi_returns_twice)] + +pub fn bar() { unsafe { foo() } } + +extern "C" { + // CHECK: declare{{( dso_local)?}} void @foo(){{.*}}[[ATTRS:#[0-9]+]] + // CHECK: attributes [[ATTRS]] = { {{.*}}returns_twice{{.*}} } + #[ffi_returns_twice] pub fn foo(); +} |