|
| 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 | +#elif defined(_WIN32) |
| 33 | +#include <windows.h> |
| 34 | +#endif |
| 35 | + |
| 36 | +namespace absl { |
| 37 | +ABSL_NAMESPACE_BEGIN |
| 38 | +namespace base_internal { |
| 39 | + |
| 40 | +namespace { |
| 41 | + |
| 42 | +size_t GetPageSize() { |
| 43 | +#ifdef _WIN32 |
| 44 | + SYSTEM_INFO system_info; |
| 45 | + GetSystemInfo(&system_info); |
| 46 | + return system_info.dwPageSize; |
| 47 | +#elif defined(__wasm__) || defined(__asmjs__) || defined(__hexagon__) |
| 48 | + return getpagesize(); |
| 49 | +#else |
| 50 | + return static_cast<size_t>(sysconf(_SC_PAGESIZE)); |
| 51 | +#endif |
| 52 | +} |
| 53 | + |
| 54 | +} // namespace |
| 55 | + |
| 56 | +void* InitializePoisonedPointerInternal() { |
| 57 | + const size_t block_size = GetPageSize(); |
| 58 | +#if defined(ABSL_HAVE_ADDRESS_SANITIZER) |
| 59 | + void* data = malloc(block_size); |
| 60 | + ASAN_POISON_MEMORY_REGION(data, block_size); |
| 61 | +#elif defined(ABSL_HAVE_MEMORY_SANITIZER) |
| 62 | + void* data = malloc(block_size); |
| 63 | + __msan_poison(data, block_size); |
| 64 | +#elif defined(ABSL_HAVE_MMAP) |
| 65 | + void* data = DirectMmap(nullptr, block_size, PROT_NONE, |
| 66 | + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 67 | + if (data == MAP_FAILED) return GetBadPointerInternal(); |
| 68 | +#elif defined(_WIN32) |
| 69 | + void* data = VirtualAlloc(nullptr, block_size, MEM_RESERVE | MEM_COMMIT, |
| 70 | + PAGE_NOACCESS); |
| 71 | + if (data == nullptr) return GetBadPointerInternal(); |
| 72 | +#else |
| 73 | + return GetBadPointerInternal(); |
| 74 | +#endif |
| 75 | + // Return the middle of the block so that dereferences before and after the |
| 76 | + // pointer will both crash. |
| 77 | + return static_cast<char*>(data) + block_size / 2; |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace base_internal |
| 81 | +ABSL_NAMESPACE_END |
| 82 | +} // namespace absl |
0 commit comments