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