dfsan_platform.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===-- dfsan_platform.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 DataFlowSanitizer.
  10. //
  11. // Platform specific information for DFSan.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef DFSAN_PLATFORM_H
  14. #define DFSAN_PLATFORM_H
  15. #include "sanitizer_common/sanitizer_common.h"
  16. #include "sanitizer_common/sanitizer_platform.h"
  17. namespace __dfsan {
  18. using __sanitizer::uptr;
  19. // TODO: The memory mapping code to setup a 1:1 shadow is based on msan.
  20. // Consider refactoring these into a shared implementation.
  21. struct MappingDesc {
  22. uptr start;
  23. uptr end;
  24. enum Type { INVALID, APP, SHADOW, ORIGIN } type;
  25. const char *name;
  26. };
  27. #if SANITIZER_LINUX && SANITIZER_WORDSIZE == 64
  28. # if defined(__aarch64__)
  29. // The mapping assumes 48-bit VMA. AArch64 maps:
  30. // - 0x0000000000000-0x0100000000000: 39/42/48-bits program own segments
  31. // - 0x0a00000000000-0x0b00000000000: 48-bits PIE program segments
  32. // Ideally, this would extend to 0x0c00000000000 (2^45 bytes - the
  33. // maximum ASLR region for 48-bit VMA) but it is too hard to fit in
  34. // the larger app/shadow/origin regions.
  35. // - 0x0e00000000000-0x1000000000000: 48-bits libraries segments
  36. const MappingDesc kMemoryLayout[] = {
  37. {0X0000000000000, 0X0100000000000, MappingDesc::APP, "app-10-13"},
  38. {0X0100000000000, 0X0200000000000, MappingDesc::SHADOW, "shadow-14"},
  39. {0X0200000000000, 0X0300000000000, MappingDesc::INVALID, "invalid"},
  40. {0X0300000000000, 0X0400000000000, MappingDesc::ORIGIN, "origin-14"},
  41. {0X0400000000000, 0X0600000000000, MappingDesc::SHADOW, "shadow-15"},
  42. {0X0600000000000, 0X0800000000000, MappingDesc::ORIGIN, "origin-15"},
  43. {0X0800000000000, 0X0A00000000000, MappingDesc::INVALID, "invalid"},
  44. {0X0A00000000000, 0X0B00000000000, MappingDesc::APP, "app-14"},
  45. {0X0B00000000000, 0X0C00000000000, MappingDesc::SHADOW, "shadow-10-13"},
  46. {0X0C00000000000, 0X0D00000000000, MappingDesc::INVALID, "invalid"},
  47. {0X0D00000000000, 0X0E00000000000, MappingDesc::ORIGIN, "origin-10-13"},
  48. {0X0E00000000000, 0X1000000000000, MappingDesc::APP, "app-15"},
  49. };
  50. # define MEM_TO_SHADOW(mem) ((uptr)mem ^ 0xB00000000000ULL)
  51. # define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x200000000000ULL)
  52. # else
  53. // All of the following configurations are supported.
  54. // ASLR disabled: main executable and DSOs at 0x555550000000
  55. // PIE and ASLR: main executable and DSOs at 0x7f0000000000
  56. // non-PIE: main executable below 0x100000000, DSOs at 0x7f0000000000
  57. // Heap at 0x700000000000.
  58. const MappingDesc kMemoryLayout[] = {
  59. {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},
  60. {0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},
  61. {0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},
  62. {0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},
  63. {0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},
  64. {0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},
  65. {0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},
  66. {0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},
  67. {0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},
  68. {0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},
  69. {0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
  70. {0x700000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};
  71. # define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)
  72. # define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x100000000000ULL)
  73. # endif
  74. #else
  75. # error "Unsupported platform"
  76. #endif
  77. const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
  78. #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
  79. #ifndef __clang__
  80. __attribute__((optimize("unroll-loops")))
  81. #endif
  82. inline bool
  83. addr_is_type(uptr addr, MappingDesc::Type mapping_type) {
  84. // It is critical for performance that this loop is unrolled (because then it is
  85. // simplified into just a few constant comparisons).
  86. #ifdef __clang__
  87. # pragma unroll
  88. #endif
  89. for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
  90. if (kMemoryLayout[i].type == mapping_type &&
  91. addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
  92. return true;
  93. return false;
  94. }
  95. #define MEM_IS_APP(mem) addr_is_type((uptr)(mem), MappingDesc::APP)
  96. #define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)
  97. #define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)
  98. } // namespace __dfsan
  99. #endif