Skip to content

Commit 65d11b5

Browse files
extern-fn-explicit-align test: add wrapped and lower requested alignment, improve assertions
1 parent f704396 commit 65d11b5

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

tests/run-make/extern-fn-explicit-align/test.c

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#include <stdint.h>
44
#include <string.h>
55

6+
struct BoolAndU32
7+
{
8+
bool a;
9+
uint32_t b;
10+
};
11+
612
#ifdef _MSC_VER
713
__declspec(align(16))
814
struct TwoU64s
@@ -18,12 +24,28 @@ struct __attribute__((aligned(16))) TwoU64s
1824
};
1925
#endif
2026

21-
struct BoolAndU32
27+
struct WrappedU64s
2228
{
23-
bool a;
24-
uint32_t b;
29+
struct TwoU64s a;
2530
};
2631

32+
#ifdef _MSC_VER
33+
__declspec(align(1))
34+
struct LowerAlign
35+
{
36+
uint64_t a;
37+
uint64_t b;
38+
};
39+
#else
40+
struct __attribute__((aligned(1))) LowerAlign
41+
{
42+
uint64_t a;
43+
uint64_t b;
44+
};
45+
#endif
46+
47+
48+
2749
int32_t many_args(
2850
void *a,
2951
void *b,
@@ -34,11 +56,27 @@ int32_t many_args(
3456
void *g,
3557
struct TwoU64s h,
3658
void *i,
37-
void *j,
59+
struct WrappedU64s j,
3860
void *k,
39-
void *l,
61+
struct LowerAlign l,
4062
const char *m)
4163
{
64+
assert(!a);
65+
assert(!b);
66+
assert(!c);
67+
assert(d == 42);
68+
assert(e);
69+
assert(f.a);
70+
assert(f.b == 1337);
71+
assert(!g);
72+
assert(h.a == 1);
73+
assert(h.b == 2);
74+
assert(!i);
75+
assert(j.a.a == 3);
76+
assert(j.a.b == 4);
77+
assert(!k);
78+
assert(l.a == 5);
79+
assert(l.b == 6);
4280
assert(strcmp(m, "Hello world") == 0);
4381
return 0;
4482
}

tests/run-make/extern-fn-explicit-align/test.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
use std::ffi::{CStr, c_char};
44
use std::ptr::null_mut;
55

6+
#[derive(Copy, Clone)]
7+
#[repr(C)]
8+
pub struct BoolAndU32 {
9+
pub a: bool,
10+
pub b: u32,
11+
}
12+
613
#[derive(Copy, Clone)]
714
#[repr(C)]
815
#[repr(align(16))]
@@ -11,11 +18,20 @@ pub struct TwoU64s {
1118
pub b: u64,
1219
}
1320

21+
#[derive(Copy, Clone)]
1422
#[repr(C)]
23+
pub struct WrappedU64s {
24+
pub a: TwoU64s
25+
}
26+
1527
#[derive(Copy, Clone)]
16-
pub struct BoolAndU32 {
17-
pub a: bool,
18-
pub b: u32,
28+
#[repr(C)]
29+
// Even though requesting align 1 can never change the alignment, it still affects the ABI
30+
// on some platforms like i686-windows.
31+
#[repr(align(1))]
32+
pub struct LowerAlign {
33+
pub a: u64,
34+
pub b: u64,
1935
}
2036

2137
#[link(name = "test", kind = "static")]
@@ -30,33 +46,35 @@ extern "C" {
3046
g: *mut (),
3147
h: TwoU64s,
3248
i: *mut (),
33-
j: *mut (),
49+
j: WrappedU64s,
3450
k: *mut (),
35-
l: *mut (),
51+
l: LowerAlign,
3652
m: *const c_char,
3753
) -> i32;
3854
}
3955

4056
const STRING: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"Hello world\0") };
4157

4258
fn main() {
59+
let bool_and_u32 = BoolAndU32 { a: true, b: 1337 };
4360
let two_u64s = TwoU64s { a: 1, b: 2 };
44-
let bool_and_u32 = BoolAndU32 { a: true, b: 3 };
61+
let wrapped = WrappedU64s { a: TwoU64s { a: 3, b: 4 } };
62+
let lower = LowerAlign { a: 5, b: 6 };
4563
let string = STRING;
4664
unsafe {
4765
many_args(
4866
null_mut(),
4967
null_mut(),
5068
null_mut(),
51-
4,
69+
42,
5270
true,
5371
bool_and_u32,
5472
null_mut(),
5573
two_u64s,
5674
null_mut(),
75+
wrapped,
5776
null_mut(),
58-
null_mut(),
59-
null_mut(),
77+
lower,
6078
string.as_ptr(),
6179
);
6280
}

0 commit comments

Comments
 (0)