xref: /aosp_15_r20/external/abseil-cpp/absl/base/internal/poison.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2024 The Abseil Authors
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_BASE_INTERNAL_POISON_H_
16*9356374aSAndroid Build Coastguard Worker #define ABSL_BASE_INTERNAL_POISON_H_
17*9356374aSAndroid Build Coastguard Worker 
18*9356374aSAndroid Build Coastguard Worker #include <cstdint>
19*9356374aSAndroid Build Coastguard Worker 
20*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
21*9356374aSAndroid Build Coastguard Worker 
22*9356374aSAndroid Build Coastguard Worker namespace absl {
23*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
24*9356374aSAndroid Build Coastguard Worker namespace base_internal {
25*9356374aSAndroid Build Coastguard Worker 
GetBadPointerInternal()26*9356374aSAndroid Build Coastguard Worker inline void* GetBadPointerInternal() {
27*9356374aSAndroid Build Coastguard Worker   // A likely bad pointer. Pointers are required to have high bits that are all
28*9356374aSAndroid Build Coastguard Worker   // zero or all one for certain 64-bit CPUs. This pointer value will hopefully
29*9356374aSAndroid Build Coastguard Worker   // cause a crash on dereference and also be clearly recognizable as invalid.
30*9356374aSAndroid Build Coastguard Worker   constexpr uint64_t kBadPtr = 0xBAD0BAD0BAD0BAD0;
31*9356374aSAndroid Build Coastguard Worker   auto ret = reinterpret_cast<void*>(static_cast<uintptr_t>(kBadPtr));
32*9356374aSAndroid Build Coastguard Worker #ifndef _MSC_VER  // MSVC doesn't support inline asm with `volatile`.
33*9356374aSAndroid Build Coastguard Worker   // Try to prevent the compiler from optimizing out the undefined behavior.
34*9356374aSAndroid Build Coastguard Worker   asm volatile("" : : "r"(ret) :);  // NOLINT
35*9356374aSAndroid Build Coastguard Worker #endif
36*9356374aSAndroid Build Coastguard Worker   return ret;
37*9356374aSAndroid Build Coastguard Worker }
38*9356374aSAndroid Build Coastguard Worker 
39*9356374aSAndroid Build Coastguard Worker void* InitializePoisonedPointerInternal();
40*9356374aSAndroid Build Coastguard Worker 
get_poisoned_pointer()41*9356374aSAndroid Build Coastguard Worker inline void* get_poisoned_pointer() {
42*9356374aSAndroid Build Coastguard Worker #if defined(NDEBUG) && !defined(ABSL_HAVE_ADDRESS_SANITIZER) && \
43*9356374aSAndroid Build Coastguard Worker     !defined(ABSL_HAVE_MEMORY_SANITIZER)
44*9356374aSAndroid Build Coastguard Worker   // In optimized non-sanitized builds, avoid the function-local static because
45*9356374aSAndroid Build Coastguard Worker   // of the codegen and runtime cost.
46*9356374aSAndroid Build Coastguard Worker   return GetBadPointerInternal();
47*9356374aSAndroid Build Coastguard Worker #else
48*9356374aSAndroid Build Coastguard Worker   // Non-optimized builds may use more robust implementation. Note that we can't
49*9356374aSAndroid Build Coastguard Worker   // use a static global because Chromium doesn't allow non-constinit globals.
50*9356374aSAndroid Build Coastguard Worker   static void* ptr = InitializePoisonedPointerInternal();
51*9356374aSAndroid Build Coastguard Worker   return ptr;
52*9356374aSAndroid Build Coastguard Worker #endif
53*9356374aSAndroid Build Coastguard Worker }
54*9356374aSAndroid Build Coastguard Worker 
55*9356374aSAndroid Build Coastguard Worker }  // namespace base_internal
56*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
57*9356374aSAndroid Build Coastguard Worker }  // namespace absl
58*9356374aSAndroid Build Coastguard Worker 
59*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_BASE_INTERNAL_POISON_H_
60