poison.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #ifndef ABSL_BASE_INTERNAL_POISON_H_
  15. #define ABSL_BASE_INTERNAL_POISON_H_
  16. #include <cstdint>
  17. #include "absl/base/config.h"
  18. namespace absl {
  19. ABSL_NAMESPACE_BEGIN
  20. namespace base_internal {
  21. inline void* GetBadPointerInternal() {
  22. // A likely bad pointer. Pointers are required to have high bits that are all
  23. // zero or all one for certain 64-bit CPUs. This pointer value will hopefully
  24. // cause a crash on dereference and also be clearly recognizable as invalid.
  25. constexpr uint64_t kBadPtr = 0xBAD0BAD0BAD0BAD0;
  26. auto ret = reinterpret_cast<void*>(static_cast<uintptr_t>(kBadPtr));
  27. #ifndef _MSC_VER // MSVC doesn't support inline asm with `volatile`.
  28. // Try to prevent the compiler from optimizing out the undefined behavior.
  29. asm volatile("" : : "r"(ret) :); // NOLINT
  30. #endif
  31. return ret;
  32. }
  33. void* InitializePoisonedPointerInternal();
  34. inline void* get_poisoned_pointer() {
  35. #if defined(NDEBUG) && !defined(ABSL_HAVE_ADDRESS_SANITIZER) && \
  36. !defined(ABSL_HAVE_MEMORY_SANITIZER)
  37. // In optimized non-sanitized builds, avoid the function-local static because
  38. // of the codegen and runtime cost.
  39. return GetBadPointerInternal();
  40. #else
  41. // Non-optimized builds may use more robust implementation. Note that we can't
  42. // use a static global because Chromium doesn't allow non-constinit globals.
  43. static void* ptr = InitializePoisonedPointerInternal();
  44. return ptr;
  45. #endif
  46. }
  47. } // namespace base_internal
  48. ABSL_NAMESPACE_END
  49. } // namespace absl
  50. #endif // ABSL_BASE_INTERNAL_POISON_H_