unaligned_access.h
Go to the documentation of this file.
00001 //
00002 // Copyright 2017 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 //
00016 
00017 #ifndef ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
00018 #define ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
00019 
00020 #include <string.h>
00021 #include <cstdint>
00022 
00023 #include "absl/base/attributes.h"
00024 
00025 // unaligned APIs
00026 
00027 // Portable handling of unaligned loads, stores, and copies.
00028 
00029 // The unaligned API is C++ only.  The declarations use C++ features
00030 // (namespaces, inline) which are absent or incompatible in C.
00031 #if defined(__cplusplus)
00032 
00033 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\
00034     defined(MEMORY_SANITIZER)
00035 // Consider we have an unaligned load/store of 4 bytes from address 0x...05.
00036 // AddressSanitizer will treat it as a 3-byte access to the range 05:07 and
00037 // will miss a bug if 08 is the first unaddressable byte.
00038 // ThreadSanitizer will also treat this as a 3-byte access to 05:07 and will
00039 // miss a race between this access and some other accesses to 08.
00040 // MemorySanitizer will correctly propagate the shadow on unaligned stores
00041 // and correctly report bugs on unaligned loads, but it may not properly
00042 // update and report the origin of the uninitialized memory.
00043 // For all three tools, replacing an unaligned access with a tool-specific
00044 // callback solves the problem.
00045 
00046 // Make sure uint16_t/uint32_t/uint64_t are defined.
00047 #include <stdint.h>
00048 
00049 extern "C" {
00050 uint16_t __sanitizer_unaligned_load16(const void *p);
00051 uint32_t __sanitizer_unaligned_load32(const void *p);
00052 uint64_t __sanitizer_unaligned_load64(const void *p);
00053 void __sanitizer_unaligned_store16(void *p, uint16_t v);
00054 void __sanitizer_unaligned_store32(void *p, uint32_t v);
00055 void __sanitizer_unaligned_store64(void *p, uint64_t v);
00056 }  // extern "C"
00057 
00058 namespace absl {
00059 namespace base_internal {
00060 
00061 inline uint16_t UnalignedLoad16(const void *p) {
00062   return __sanitizer_unaligned_load16(p);
00063 }
00064 
00065 inline uint32_t UnalignedLoad32(const void *p) {
00066   return __sanitizer_unaligned_load32(p);
00067 }
00068 
00069 inline uint64_t UnalignedLoad64(const void *p) {
00070   return __sanitizer_unaligned_load64(p);
00071 }
00072 
00073 inline void UnalignedStore16(void *p, uint16_t v) {
00074   __sanitizer_unaligned_store16(p, v);
00075 }
00076 
00077 inline void UnalignedStore32(void *p, uint32_t v) {
00078   __sanitizer_unaligned_store32(p, v);
00079 }
00080 
00081 inline void UnalignedStore64(void *p, uint64_t v) {
00082   __sanitizer_unaligned_store64(p, v);
00083 }
00084 
00085 }  // namespace base_internal
00086 }  // namespace absl
00087 
00088 #define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
00089   (absl::base_internal::UnalignedLoad16(_p))
00090 #define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
00091   (absl::base_internal::UnalignedLoad32(_p))
00092 #define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
00093   (absl::base_internal::UnalignedLoad64(_p))
00094 
00095 #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
00096   (absl::base_internal::UnalignedStore16(_p, _val))
00097 #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
00098   (absl::base_internal::UnalignedStore32(_p, _val))
00099 #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
00100   (absl::base_internal::UnalignedStore64(_p, _val))
00101 
00102 #else
00103 
00104 namespace absl {
00105 namespace base_internal {
00106 
00107 inline uint16_t UnalignedLoad16(const void *p) {
00108   uint16_t t;
00109   memcpy(&t, p, sizeof t);
00110   return t;
00111 }
00112 
00113 inline uint32_t UnalignedLoad32(const void *p) {
00114   uint32_t t;
00115   memcpy(&t, p, sizeof t);
00116   return t;
00117 }
00118 
00119 inline uint64_t UnalignedLoad64(const void *p) {
00120   uint64_t t;
00121   memcpy(&t, p, sizeof t);
00122   return t;
00123 }
00124 
00125 inline void UnalignedStore16(void *p, uint16_t v) { memcpy(p, &v, sizeof v); }
00126 
00127 inline void UnalignedStore32(void *p, uint32_t v) { memcpy(p, &v, sizeof v); }
00128 
00129 inline void UnalignedStore64(void *p, uint64_t v) { memcpy(p, &v, sizeof v); }
00130 
00131 }  // namespace base_internal
00132 }  // namespace absl
00133 
00134 #define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
00135   (absl::base_internal::UnalignedLoad16(_p))
00136 #define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
00137   (absl::base_internal::UnalignedLoad32(_p))
00138 #define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
00139   (absl::base_internal::UnalignedLoad64(_p))
00140 
00141 #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
00142   (absl::base_internal::UnalignedStore16(_p, _val))
00143 #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
00144   (absl::base_internal::UnalignedStore32(_p, _val))
00145 #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
00146   (absl::base_internal::UnalignedStore64(_p, _val))
00147 
00148 #endif
00149 
00150 #endif  // defined(__cplusplus), end of unaligned API
00151 
00152 #endif  // ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:16