asan_mapping_sparc64.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //===-- asan_mapping_sparc64.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. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // SPARC64-specific definitions for ASan memory mapping.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef ASAN_MAPPING_SPARC64_H
  14. #define ASAN_MAPPING_SPARC64_H
  15. // This is tailored to the 52-bit VM layout on SPARC-T4 and later.
  16. // The VM space is split into two 51-bit halves at both ends: the low part
  17. // has all the bits above the 51st cleared, while the high part has them set.
  18. // 0xfff8000000000000 - 0xffffffffffffffff
  19. // 0x0000000000000000 - 0x0007ffffffffffff
  20. #define VMA_BITS 52
  21. #define HIGH_BITS (64 - VMA_BITS)
  22. // The idea is to chop the high bits before doing the scaling, so the two
  23. // parts become contiguous again and the usual scheme can be applied.
  24. #define MEM_TO_SHADOW(mem) \
  25. ((((mem) << HIGH_BITS) >> (HIGH_BITS + (ASAN_SHADOW_SCALE))) + \
  26. (ASAN_SHADOW_OFFSET))
  27. #define SHADOW_TO_MEM(ptr) (__asan::ShadowToMemSparc64(ptr))
  28. #define kLowMemBeg 0
  29. #define kLowMemEnd (ASAN_SHADOW_OFFSET - 1)
  30. #define kLowShadowBeg ASAN_SHADOW_OFFSET
  31. #define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd)
  32. // But of course there is the huge hole between the high shadow memory,
  33. // which is in the low part, and the beginning of the high part.
  34. #define kHighMemBeg (-(1ULL << (VMA_BITS - 1)))
  35. #define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
  36. #define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd)
  37. #define kMidShadowBeg 0
  38. #define kMidShadowEnd 0
  39. // With the zero shadow base we can not actually map pages starting from 0.
  40. // This constant is somewhat arbitrary.
  41. #define kZeroBaseShadowStart 0
  42. #define kZeroBaseMaxShadowStart (1 << 18)
  43. #define kShadowGapBeg (kLowShadowEnd + 1)
  44. #define kShadowGapEnd (kHighShadowBeg - 1)
  45. #define kShadowGap2Beg 0
  46. #define kShadowGap2End 0
  47. #define kShadowGap3Beg 0
  48. #define kShadowGap3End 0
  49. namespace __asan {
  50. static inline bool AddrIsInLowMem(uptr a) {
  51. PROFILE_ASAN_MAPPING();
  52. return a <= kLowMemEnd;
  53. }
  54. static inline bool AddrIsInLowShadow(uptr a) {
  55. PROFILE_ASAN_MAPPING();
  56. return a >= kLowShadowBeg && a <= kLowShadowEnd;
  57. }
  58. static inline bool AddrIsInMidMem(uptr a) {
  59. PROFILE_ASAN_MAPPING();
  60. return false;
  61. }
  62. static inline bool AddrIsInMidShadow(uptr a) {
  63. PROFILE_ASAN_MAPPING();
  64. return false;
  65. }
  66. static inline bool AddrIsInHighMem(uptr a) {
  67. PROFILE_ASAN_MAPPING();
  68. return kHighMemBeg && a >= kHighMemBeg && a <= kHighMemEnd;
  69. }
  70. static inline bool AddrIsInHighShadow(uptr a) {
  71. PROFILE_ASAN_MAPPING();
  72. return kHighMemBeg && a >= kHighShadowBeg && a <= kHighShadowEnd;
  73. }
  74. static inline bool AddrIsInShadowGap(uptr a) {
  75. PROFILE_ASAN_MAPPING();
  76. return a >= kShadowGapBeg && a <= kShadowGapEnd;
  77. }
  78. static inline constexpr uptr ShadowToMemSparc64(uptr p) {
  79. PROFILE_ASAN_MAPPING();
  80. p -= ASAN_SHADOW_OFFSET;
  81. p <<= ASAN_SHADOW_SCALE;
  82. if (p >= 0x8000000000000) {
  83. p |= (~0ULL) << VMA_BITS;
  84. }
  85. return p;
  86. }
  87. static_assert(ShadowToMemSparc64(MEM_TO_SHADOW(0x0000000000000000)) ==
  88. 0x0000000000000000);
  89. static_assert(ShadowToMemSparc64(MEM_TO_SHADOW(0xfff8000000000000)) ==
  90. 0xfff8000000000000);
  91. // Gets aligned down.
  92. static_assert(ShadowToMemSparc64(MEM_TO_SHADOW(0x0007ffffffffffff)) ==
  93. 0x0007fffffffffff8);
  94. } // namespace __asan
  95. #endif // ASAN_MAPPING_SPARC64_H