hwasan_mapping.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //===-- hwasan_mapping.h ----------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file is a part of HWAddressSanitizer and defines memory mapping.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef HWASAN_MAPPING_H
  14. #define HWASAN_MAPPING_H
  15. #include "sanitizer_common/sanitizer_internal_defs.h"
  16. #include "hwasan_interface_internal.h"
  17. // Typical mapping on Linux/x86_64:
  18. // with dynamic shadow mapped at [0x770d59f40000, 0x7f0d59f40000]:
  19. // || [0x7f0d59f40000, 0x7fffffffffff] || HighMem ||
  20. // || [0x7efe2f934000, 0x7f0d59f3ffff] || HighShadow ||
  21. // || [0x7e7e2f934000, 0x7efe2f933fff] || ShadowGap ||
  22. // || [0x770d59f40000, 0x7e7e2f933fff] || LowShadow ||
  23. // || [0x000000000000, 0x770d59f3ffff] || LowMem ||
  24. // Typical mapping on Android/AArch64
  25. // with dynamic shadow mapped: [0x007477480000, 0x007c77480000]:
  26. // || [0x007c77480000, 0x007fffffffff] || HighMem ||
  27. // || [0x007c3ebc8000, 0x007c7747ffff] || HighShadow ||
  28. // || [0x007bbebc8000, 0x007c3ebc7fff] || ShadowGap ||
  29. // || [0x007477480000, 0x007bbebc7fff] || LowShadow ||
  30. // || [0x000000000000, 0x00747747ffff] || LowMem ||
  31. // Reasonable values are 4 (for 1/16th shadow) and 6 (for 1/64th).
  32. constexpr uptr kShadowScale = 4;
  33. constexpr uptr kShadowAlignment = 1ULL << kShadowScale;
  34. namespace __hwasan {
  35. extern uptr kLowMemStart;
  36. extern uptr kLowMemEnd;
  37. extern uptr kLowShadowEnd;
  38. extern uptr kLowShadowStart;
  39. extern uptr kHighShadowStart;
  40. extern uptr kHighShadowEnd;
  41. extern uptr kHighMemStart;
  42. extern uptr kHighMemEnd;
  43. inline uptr GetShadowOffset() {
  44. return SANITIZER_FUCHSIA ? 0 : __hwasan_shadow_memory_dynamic_address;
  45. }
  46. inline uptr MemToShadow(uptr untagged_addr) {
  47. return (untagged_addr >> kShadowScale) + GetShadowOffset();
  48. }
  49. inline uptr ShadowToMem(uptr shadow_addr) {
  50. return (shadow_addr - GetShadowOffset()) << kShadowScale;
  51. }
  52. inline uptr MemToShadowSize(uptr size) {
  53. return size >> kShadowScale;
  54. }
  55. bool MemIsApp(uptr p);
  56. inline bool MemIsShadow(uptr p) {
  57. return (kLowShadowStart <= p && p <= kLowShadowEnd) ||
  58. (kHighShadowStart <= p && p <= kHighShadowEnd);
  59. }
  60. uptr GetAliasRegionStart();
  61. } // namespace __hwasan
  62. #endif // HWASAN_MAPPING_H