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