safestack_platform.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===-- safestack_platform.h ----------------------------------------------===//
  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 implements platform specific parts of SafeStack runtime.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SAFESTACK_PLATFORM_H
  13. #define SAFESTACK_PLATFORM_H
  14. #include "safestack_util.h"
  15. #include "sanitizer_common/sanitizer_platform.h"
  16. #include <dlfcn.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sys/mman.h>
  21. #include <sys/syscall.h>
  22. #include <sys/types.h>
  23. #include <unistd.h>
  24. #if !(SANITIZER_NETBSD || SANITIZER_FREEBSD || SANITIZER_LINUX)
  25. #error "Support for your platform has not been implemented"
  26. #endif
  27. #if SANITIZER_NETBSD
  28. #include <lwp.h>
  29. extern "C" void *__mmap(void *, size_t, int, int, int, int, off_t);
  30. #endif
  31. #if SANITIZER_FREEBSD
  32. #include <sys/thr.h>
  33. #endif
  34. namespace safestack {
  35. #if SANITIZER_NETBSD
  36. static void *GetRealLibcAddress(const char *symbol) {
  37. void *real = dlsym(RTLD_NEXT, symbol);
  38. if (!real)
  39. real = dlsym(RTLD_DEFAULT, symbol);
  40. if (!real) {
  41. fprintf(stderr, "safestack GetRealLibcAddress failed for symbol=%s",
  42. symbol);
  43. abort();
  44. }
  45. return real;
  46. }
  47. #define _REAL(func, ...) real##_##func(__VA_ARGS__)
  48. #define DEFINE__REAL(ret_type, func, ...) \
  49. static ret_type (*real_##func)(__VA_ARGS__) = NULL; \
  50. if (!real_##func) { \
  51. real_##func = (ret_type(*)(__VA_ARGS__))GetRealLibcAddress(#func); \
  52. } \
  53. SFS_CHECK(real_##func);
  54. #endif
  55. using ThreadId = uint64_t;
  56. inline ThreadId GetTid() {
  57. #if SANITIZER_NETBSD
  58. DEFINE__REAL(int, _lwp_self);
  59. return _REAL(_lwp_self);
  60. #elif SANITIZER_FREEBSD
  61. long Tid;
  62. thr_self(&Tid);
  63. return Tid;
  64. #else
  65. return syscall(SYS_gettid);
  66. #endif
  67. }
  68. inline int TgKill(pid_t pid, ThreadId tid, int sig) {
  69. #if SANITIZER_NETBSD
  70. DEFINE__REAL(int, _lwp_kill, int a, int b);
  71. (void)pid;
  72. return _REAL(_lwp_kill, tid, sig);
  73. #elif SANITIZER_FREEBSD
  74. return syscall(SYS_thr_kill2, pid, tid, sig);
  75. #else
  76. return syscall(SYS_tgkill, pid, tid, sig);
  77. #endif
  78. }
  79. inline void *Mmap(void *addr, size_t length, int prot, int flags, int fd,
  80. off_t offset) {
  81. #if SANITIZER_NETBSD
  82. return __mmap(addr, length, prot, flags, fd, 0, offset);
  83. #elif defined(__x86_64__) && (SANITIZER_FREEBSD)
  84. return (void *)__syscall(SYS_mmap, addr, length, prot, flags, fd, offset);
  85. #else
  86. return (void *)syscall(SYS_mmap, addr, length, prot, flags, fd, offset);
  87. #endif
  88. }
  89. inline int Munmap(void *addr, size_t length) {
  90. #if SANITIZER_NETBSD
  91. DEFINE__REAL(int, munmap, void *a, size_t b);
  92. return _REAL(munmap, addr, length);
  93. #else
  94. return syscall(SYS_munmap, addr, length);
  95. #endif
  96. }
  97. inline int Mprotect(void *addr, size_t length, int prot) {
  98. #if SANITIZER_NETBSD
  99. DEFINE__REAL(int, mprotect, void *a, size_t b, int c);
  100. return _REAL(mprotect, addr, length, prot);
  101. #else
  102. return syscall(SYS_mprotect, addr, length, prot);
  103. #endif
  104. }
  105. } // namespace safestack
  106. #endif // SAFESTACK_PLATFORM_H