xref: /xnu-8796.141.3/tests/os_unaligned.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <stdint.h>
2*1b191cb5SApple OSS Distributions #include <darwintest.h>
3*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
4*1b191cb5SApple OSS Distributions 
5*1b191cb5SApple OSS Distributions #include "../libkern/os/ptrtools.h"
6*1b191cb5SApple OSS Distributions 
7*1b191cb5SApple OSS Distributions #define CHECK_ALIGNMENT(T) \
8*1b191cb5SApple OSS Distributions { \
9*1b191cb5SApple OSS Distributions 	T *__p; \
10*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ_ULONG(__alignof__(*__p), sizeof(*__p), #T " native alignment"); \
11*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ_ULONG(__alignof__(os_unaligned_deref(__p)), 1UL, #T " alignment"); \
12*1b191cb5SApple OSS Distributions }
13*1b191cb5SApple OSS Distributions 
14*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
15*1b191cb5SApple OSS Distributions 
16*1b191cb5SApple OSS Distributions struct A {
17*1b191cb5SApple OSS Distributions 	int a;
18*1b191cb5SApple OSS Distributions };
19*1b191cb5SApple OSS Distributions 
20*1b191cb5SApple OSS Distributions T_DECL(os_unaligned, "Unaligned pointer access")
21*1b191cb5SApple OSS Distributions {
22*1b191cb5SApple OSS Distributions 	int x = 0x914842;
23*1b191cb5SApple OSS Distributions 	int *p = &x;
24*1b191cb5SApple OSS Distributions 
25*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ_INT(os_unaligned_deref(p), x, "load");
26*1b191cb5SApple OSS Distributions 	os_unaligned_deref(&x) = INT_MIN;
27*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ_INT(x, INT_MIN, "store");
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions 	CHECK_ALIGNMENT(unsigned);
30*1b191cb5SApple OSS Distributions 	CHECK_ALIGNMENT(long long);
31*1b191cb5SApple OSS Distributions 	CHECK_ALIGNMENT(uintptr_t);
32*1b191cb5SApple OSS Distributions 	CHECK_ALIGNMENT(int16_t);
33*1b191cb5SApple OSS Distributions 	CHECK_ALIGNMENT(uint64_t);
34*1b191cb5SApple OSS Distributions 	CHECK_ALIGNMENT(struct A);
35*1b191cb5SApple OSS Distributions 	CHECK_ALIGNMENT(void *);
36*1b191cb5SApple OSS Distributions }
37