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