tsan_interface.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===-- tsan_interface.cpp ------------------------------------------------===//
  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 ThreadSanitizer (TSan), a race detector.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "tsan_interface.h"
  13. #include "tsan_interface_ann.h"
  14. #include "tsan_rtl.h"
  15. #include "sanitizer_common/sanitizer_internal_defs.h"
  16. #include "sanitizer_common/sanitizer_ptrauth.h"
  17. #define CALLERPC ((uptr)__builtin_return_address(0))
  18. using namespace __tsan;
  19. void __tsan_init() { Initialize(cur_thread_init()); }
  20. void __tsan_flush_memory() {
  21. FlushShadowMemory();
  22. }
  23. void __tsan_read16_pc(void *addr, void *pc) {
  24. uptr pc_no_pac = STRIP_PAC_PC(pc);
  25. ThreadState *thr = cur_thread();
  26. MemoryAccess(thr, pc_no_pac, (uptr)addr, 8, kAccessRead);
  27. MemoryAccess(thr, pc_no_pac, (uptr)addr + 8, 8, kAccessRead);
  28. }
  29. void __tsan_write16_pc(void *addr, void *pc) {
  30. uptr pc_no_pac = STRIP_PAC_PC(pc);
  31. ThreadState *thr = cur_thread();
  32. MemoryAccess(thr, pc_no_pac, (uptr)addr, 8, kAccessWrite);
  33. MemoryAccess(thr, pc_no_pac, (uptr)addr + 8, 8, kAccessWrite);
  34. }
  35. // __tsan_unaligned_read/write calls are emitted by compiler.
  36. void __tsan_unaligned_read16(const void *addr) {
  37. uptr pc = CALLERPC;
  38. ThreadState *thr = cur_thread();
  39. UnalignedMemoryAccess(thr, pc, (uptr)addr, 8, kAccessRead);
  40. UnalignedMemoryAccess(thr, pc, (uptr)addr + 8, 8, kAccessRead);
  41. }
  42. void __tsan_unaligned_write16(void *addr) {
  43. uptr pc = CALLERPC;
  44. ThreadState *thr = cur_thread();
  45. UnalignedMemoryAccess(thr, pc, (uptr)addr, 8, kAccessWrite);
  46. UnalignedMemoryAccess(thr, pc, (uptr)addr + 8, 8, kAccessWrite);
  47. }
  48. extern "C" {
  49. SANITIZER_INTERFACE_ATTRIBUTE
  50. void *__tsan_get_current_fiber() {
  51. return cur_thread();
  52. }
  53. SANITIZER_INTERFACE_ATTRIBUTE
  54. void *__tsan_create_fiber(unsigned flags) {
  55. return FiberCreate(cur_thread(), CALLERPC, flags);
  56. }
  57. SANITIZER_INTERFACE_ATTRIBUTE
  58. void __tsan_destroy_fiber(void *fiber) {
  59. FiberDestroy(cur_thread(), CALLERPC, static_cast<ThreadState *>(fiber));
  60. }
  61. SANITIZER_INTERFACE_ATTRIBUTE
  62. void __tsan_switch_to_fiber(void *fiber, unsigned flags) {
  63. FiberSwitch(cur_thread(), CALLERPC, static_cast<ThreadState *>(fiber), flags);
  64. }
  65. SANITIZER_INTERFACE_ATTRIBUTE
  66. void __tsan_set_fiber_name(void *fiber, const char *name) {
  67. ThreadSetName(static_cast<ThreadState *>(fiber), name);
  68. }
  69. } // extern "C"
  70. void __tsan_acquire(void *addr) {
  71. Acquire(cur_thread(), CALLERPC, (uptr)addr);
  72. }
  73. void __tsan_release(void *addr) {
  74. Release(cur_thread(), CALLERPC, (uptr)addr);
  75. }