From 65d11b5c6562c57d61bf0db88736da06b8b77423 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Sun, 11 Jun 2023 13:41:46 -0400 Subject: [PATCH] extern-fn-explicit-align test: add wrapped and lower requested alignment, improve assertions --- .../run-make/extern-fn-explicit-align/test.c | 48 +++++++++++++++++-- .../run-make/extern-fn-explicit-align/test.rs | 38 +++++++++++---- 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/tests/run-make/extern-fn-explicit-align/test.c b/tests/run-make/extern-fn-explicit-align/test.c index 154b63ff45b3..292e7a822b59 100644 --- a/tests/run-make/extern-fn-explicit-align/test.c +++ b/tests/run-make/extern-fn-explicit-align/test.c @@ -3,6 +3,12 @@ #include #include +struct BoolAndU32 +{ + bool a; + uint32_t b; +}; + #ifdef _MSC_VER __declspec(align(16)) struct TwoU64s @@ -18,12 +24,28 @@ struct __attribute__((aligned(16))) TwoU64s }; #endif -struct BoolAndU32 +struct WrappedU64s { - bool a; - uint32_t b; + struct TwoU64s a; }; +#ifdef _MSC_VER +__declspec(align(1)) +struct LowerAlign +{ + uint64_t a; + uint64_t b; +}; +#else +struct __attribute__((aligned(1))) LowerAlign +{ + uint64_t a; + uint64_t b; +}; +#endif + + + int32_t many_args( void *a, void *b, @@ -34,11 +56,27 @@ int32_t many_args( void *g, struct TwoU64s h, void *i, - void *j, + struct WrappedU64s j, void *k, - void *l, + struct LowerAlign l, const char *m) { + assert(!a); + assert(!b); + assert(!c); + assert(d == 42); + assert(e); + assert(f.a); + assert(f.b == 1337); + assert(!g); + assert(h.a == 1); + assert(h.b == 2); + assert(!i); + assert(j.a.a == 3); + assert(j.a.b == 4); + assert(!k); + assert(l.a == 5); + assert(l.b == 6); assert(strcmp(m, "Hello world") == 0); return 0; } diff --git a/tests/run-make/extern-fn-explicit-align/test.rs b/tests/run-make/extern-fn-explicit-align/test.rs index 4621c36c4942..c5b3e24048ee 100644 --- a/tests/run-make/extern-fn-explicit-align/test.rs +++ b/tests/run-make/extern-fn-explicit-align/test.rs @@ -3,6 +3,13 @@ use std::ffi::{CStr, c_char}; use std::ptr::null_mut; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct BoolAndU32 { + pub a: bool, + pub b: u32, +} + #[derive(Copy, Clone)] #[repr(C)] #[repr(align(16))] @@ -11,11 +18,20 @@ pub struct TwoU64s { pub b: u64, } -#[repr(C)] #[derive(Copy, Clone)] -pub struct BoolAndU32 { - pub a: bool, - pub b: u32, +#[repr(C)] +pub struct WrappedU64s { + pub a: TwoU64s +} + +#[derive(Copy, Clone)] +#[repr(C)] +// Even though requesting align 1 can never change the alignment, it still affects the ABI +// on some platforms like i686-windows. +#[repr(align(1))] +pub struct LowerAlign { + pub a: u64, + pub b: u64, } #[link(name = "test", kind = "static")] @@ -30,9 +46,9 @@ extern "C" { g: *mut (), h: TwoU64s, i: *mut (), - j: *mut (), + j: WrappedU64s, k: *mut (), - l: *mut (), + l: LowerAlign, m: *const c_char, ) -> i32; } @@ -40,23 +56,25 @@ extern "C" { const STRING: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"Hello world\0") }; fn main() { + let bool_and_u32 = BoolAndU32 { a: true, b: 1337 }; let two_u64s = TwoU64s { a: 1, b: 2 }; - let bool_and_u32 = BoolAndU32 { a: true, b: 3 }; + let wrapped = WrappedU64s { a: TwoU64s { a: 3, b: 4 } }; + let lower = LowerAlign { a: 5, b: 6 }; let string = STRING; unsafe { many_args( null_mut(), null_mut(), null_mut(), - 4, + 42, true, bool_and_u32, null_mut(), two_u64s, null_mut(), + wrapped, null_mut(), - null_mut(), - null_mut(), + lower, string.as_ptr(), ); }