dfsan_platform.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // All of the following configurations are supported.
  29. // ASLR disabled: main executable and DSOs at 0x555550000000
  30. // PIE and ASLR: main executable and DSOs at 0x7f0000000000
  31. // non-PIE: main executable below 0x100000000, DSOs at 0x7f0000000000
  32. // Heap at 0x700000000000.
  33. const MappingDesc kMemoryLayout[] = {
  34. {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},
  35. {0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},
  36. {0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},
  37. {0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},
  38. {0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},
  39. {0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},
  40. {0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},
  41. {0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},
  42. {0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},
  43. {0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},
  44. {0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
  45. {0x700000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};
  46. # define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)
  47. # define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x100000000000ULL)
  48. #else
  49. # error "Unsupported platform"
  50. #endif
  51. const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
  52. #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
  53. #ifndef __clang__
  54. __attribute__((optimize("unroll-loops")))
  55. #endif
  56. inline bool
  57. addr_is_type(uptr addr, MappingDesc::Type mapping_type) {
  58. // It is critical for performance that this loop is unrolled (because then it is
  59. // simplified into just a few constant comparisons).
  60. #ifdef __clang__
  61. # pragma unroll
  62. #endif
  63. for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
  64. if (kMemoryLayout[i].type == mapping_type &&
  65. addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
  66. return true;
  67. return false;
  68. }
  69. #define MEM_IS_APP(mem) addr_is_type((uptr)(mem), MappingDesc::APP)
  70. #define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)
  71. #define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)
  72. } // namespace __dfsan
  73. #endif