1 //
2 // Copyright 2017 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #ifndef ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
18 #define ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
19
20 #include <string.h>
21
22 #include <cstdint>
23
24 #include "absl/base/attributes.h"
25 #include "absl/base/config.h"
26
27 // unaligned APIs
28
29 // Portable handling of unaligned loads, stores, and copies.
30
31 // The unaligned API is C++ only. The declarations use C++ features
32 // (namespaces, inline) which are absent or incompatible in C.
33 #if defined(__cplusplus)
34
35 #if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
36 defined(ABSL_HAVE_THREAD_SANITIZER) || defined(ABSL_HAVE_MEMORY_SANITIZER)
37 // Consider we have an unaligned load/store of 4 bytes from address 0x...05.
38 // AddressSanitizer will treat it as a 3-byte access to the range 05:07 and
39 // will miss a bug if 08 is the first unaddressable byte.
40 // ThreadSanitizer will also treat this as a 3-byte access to 05:07 and will
41 // miss a race between this access and some other accesses to 08.
42 // MemorySanitizer will correctly propagate the shadow on unaligned stores
43 // and correctly report bugs on unaligned loads, but it may not properly
44 // update and report the origin of the uninitialized memory.
45 // For all three tools, replacing an unaligned access with a tool-specific
46 // callback solves the problem.
47
48 // Make sure uint16_t/uint32_t/uint64_t are defined.
49 #include <stdint.h>
50
51 extern "C" {
52 uint16_t __sanitizer_unaligned_load16(const void *p);
53 uint32_t __sanitizer_unaligned_load32(const void *p);
54 uint64_t __sanitizer_unaligned_load64(const void *p);
55 void __sanitizer_unaligned_store16(void *p, uint16_t v);
56 void __sanitizer_unaligned_store32(void *p, uint32_t v);
57 void __sanitizer_unaligned_store64(void *p, uint64_t v);
58 } // extern "C"
59
60 namespace absl {
61 ABSL_NAMESPACE_BEGIN
62 namespace base_internal {
63
UnalignedLoad16(const void * p)64 inline uint16_t UnalignedLoad16(const void *p) {
65 return __sanitizer_unaligned_load16(p);
66 }
67
UnalignedLoad32(const void * p)68 inline uint32_t UnalignedLoad32(const void *p) {
69 return __sanitizer_unaligned_load32(p);
70 }
71
UnalignedLoad64(const void * p)72 inline uint64_t UnalignedLoad64(const void *p) {
73 return __sanitizer_unaligned_load64(p);
74 }
75
UnalignedStore16(void * p,uint16_t v)76 inline void UnalignedStore16(void *p, uint16_t v) {
77 __sanitizer_unaligned_store16(p, v);
78 }
79
UnalignedStore32(void * p,uint32_t v)80 inline void UnalignedStore32(void *p, uint32_t v) {
81 __sanitizer_unaligned_store32(p, v);
82 }
83
UnalignedStore64(void * p,uint64_t v)84 inline void UnalignedStore64(void *p, uint64_t v) {
85 __sanitizer_unaligned_store64(p, v);
86 }
87
88 } // namespace base_internal
89 ABSL_NAMESPACE_END
90 } // namespace absl
91
92 #define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
93 (absl::base_internal::UnalignedLoad16(_p))
94 #define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
95 (absl::base_internal::UnalignedLoad32(_p))
96 #define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
97 (absl::base_internal::UnalignedLoad64(_p))
98
99 #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
100 (absl::base_internal::UnalignedStore16(_p, _val))
101 #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
102 (absl::base_internal::UnalignedStore32(_p, _val))
103 #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
104 (absl::base_internal::UnalignedStore64(_p, _val))
105
106 #else
107
108 namespace absl {
109 ABSL_NAMESPACE_BEGIN
110 namespace base_internal {
111
UnalignedLoad16(const void * p)112 inline uint16_t UnalignedLoad16(const void *p) {
113 uint16_t t;
114 memcpy(&t, p, sizeof t);
115 return t;
116 }
117
UnalignedLoad32(const void * p)118 inline uint32_t UnalignedLoad32(const void *p) {
119 uint32_t t;
120 memcpy(&t, p, sizeof t);
121 return t;
122 }
123
UnalignedLoad64(const void * p)124 inline uint64_t UnalignedLoad64(const void *p) {
125 uint64_t t;
126 memcpy(&t, p, sizeof t);
127 return t;
128 }
129
UnalignedStore16(void * p,uint16_t v)130 inline void UnalignedStore16(void *p, uint16_t v) { memcpy(p, &v, sizeof v); }
131
UnalignedStore32(void * p,uint32_t v)132 inline void UnalignedStore32(void *p, uint32_t v) { memcpy(p, &v, sizeof v); }
133
UnalignedStore64(void * p,uint64_t v)134 inline void UnalignedStore64(void *p, uint64_t v) { memcpy(p, &v, sizeof v); }
135
136 } // namespace base_internal
137 ABSL_NAMESPACE_END
138 } // namespace absl
139
140 #define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
141 (absl::base_internal::UnalignedLoad16(_p))
142 #define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
143 (absl::base_internal::UnalignedLoad32(_p))
144 #define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
145 (absl::base_internal::UnalignedLoad64(_p))
146
147 #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
148 (absl::base_internal::UnalignedStore16(_p, _val))
149 #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
150 (absl::base_internal::UnalignedStore32(_p, _val))
151 #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
152 (absl::base_internal::UnalignedStore64(_p, _val))
153
154 #endif
155
156 #endif // defined(__cplusplus), end of unaligned API
157
158 #endif // ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
159