poison.cc (2436B)
1 // Copyright 2024 The Abseil Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "absl/base/internal/poison.h" 16 17 #include <cstdlib> 18 19 #include "absl/base/config.h" 20 #include "absl/base/internal/direct_mmap.h" 21 22 #ifndef _WIN32 23 #include <unistd.h> 24 #endif 25 26 #if defined(ABSL_HAVE_ADDRESS_SANITIZER) 27 #include <sanitizer/asan_interface.h> 28 #elif defined(ABSL_HAVE_MEMORY_SANITIZER) 29 #include <sanitizer/msan_interface.h> 30 #elif defined(ABSL_HAVE_MMAP) 31 #include <sys/mman.h> 32 #endif 33 34 #if defined(_WIN32) 35 #include <windows.h> 36 #endif 37 38 namespace absl { 39 ABSL_NAMESPACE_BEGIN 40 namespace base_internal { 41 42 namespace { 43 44 size_t GetPageSize() { 45 #ifdef _WIN32 46 SYSTEM_INFO system_info; 47 GetSystemInfo(&system_info); 48 return system_info.dwPageSize; 49 #elif defined(__wasm__) || defined(__asmjs__) || defined(__hexagon__) 50 return getpagesize(); 51 #else 52 return static_cast<size_t>(sysconf(_SC_PAGESIZE)); 53 #endif 54 } 55 56 } // namespace 57 58 void* InitializePoisonedPointerInternal() { 59 const size_t block_size = GetPageSize(); 60 #if defined(ABSL_HAVE_ADDRESS_SANITIZER) 61 void* data = malloc(block_size); 62 ASAN_POISON_MEMORY_REGION(data, block_size); 63 #elif defined(ABSL_HAVE_MEMORY_SANITIZER) 64 void* data = malloc(block_size); 65 __msan_poison(data, block_size); 66 #elif defined(ABSL_HAVE_MMAP) 67 void* data = DirectMmap(nullptr, block_size, PROT_NONE, 68 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 69 if (data == MAP_FAILED) return GetBadPointerInternal(); 70 #elif defined(_WIN32) 71 void* data = VirtualAlloc(nullptr, block_size, MEM_RESERVE | MEM_COMMIT, 72 PAGE_NOACCESS); 73 if (data == nullptr) return GetBadPointerInternal(); 74 #else 75 return GetBadPointerInternal(); 76 #endif 77 // Return the middle of the block so that dereferences before and after the 78 // pointer will both crash. 79 return static_cast<char*>(data) + block_size / 2; 80 } 81 82 } // namespace base_internal 83 ABSL_NAMESPACE_END 84 } // namespace absl