Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:08

0001 // Copyright 2024 The Abseil Authors
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //     https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_BASE_INTERNAL_POISON_H_
0016 #define ABSL_BASE_INTERNAL_POISON_H_
0017 
0018 #include <cstdint>
0019 
0020 #include "absl/base/config.h"
0021 
0022 namespace absl {
0023 ABSL_NAMESPACE_BEGIN
0024 namespace base_internal {
0025 
0026 inline void* GetBadPointerInternal() {
0027   // A likely bad pointer. Pointers are required to have high bits that are all
0028   // zero or all one for certain 64-bit CPUs. This pointer value will hopefully
0029   // cause a crash on dereference and also be clearly recognizable as invalid.
0030   constexpr uint64_t kBadPtr = 0xBAD0BAD0BAD0BAD0;
0031   auto ret = reinterpret_cast<void*>(static_cast<uintptr_t>(kBadPtr));
0032 #ifndef _MSC_VER  // MSVC doesn't support inline asm with `volatile`.
0033   // Try to prevent the compiler from optimizing out the undefined behavior.
0034   asm volatile("" : : "r"(ret) :);  // NOLINT
0035 #endif
0036   return ret;
0037 }
0038 
0039 void* InitializePoisonedPointerInternal();
0040 
0041 inline void* get_poisoned_pointer() {
0042 #if defined(NDEBUG) && !defined(ABSL_HAVE_ADDRESS_SANITIZER) && \
0043     !defined(ABSL_HAVE_MEMORY_SANITIZER)
0044   // In optimized non-sanitized builds, avoid the function-local static because
0045   // of the codegen and runtime cost.
0046   return GetBadPointerInternal();
0047 #else
0048   // Non-optimized builds may use more robust implementation. Note that we can't
0049   // use a static global because Chromium doesn't allow non-constinit globals.
0050   static void* ptr = InitializePoisonedPointerInternal();
0051   return ptr;
0052 #endif
0053 }
0054 
0055 }  // namespace base_internal
0056 ABSL_NAMESPACE_END
0057 }  // namespace absl
0058 
0059 #endif  // ABSL_BASE_INTERNAL_POISON_H_