| 1 | //===-- ubsan_offload_hsa_interceptors.cpp ----------------------*- 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 | #include "interception/interception.h" |
| 10 | #include "sanitizer_common/sanitizer_atomic.h" |
| 11 | #include "sanitizer_common/sanitizer_common.h" |
| 12 | #include "sanitizer_common/sanitizer_mutex.h" |
| 13 | #include "sanitizer_common/sanitizer_offload.h" |
| 14 | #include "sanitizer_common/sanitizer_platform.h" |
| 15 | #include "ubsan_diag.h" |
| 16 | #include "ubsan_offload.h" |
| 17 | |
| 18 | #if !SANITIZER_LINUX |
| 19 | #error "Offload UBSan reporting is supported on Linux only" |
| 20 | #endif |
| 21 | |
| 22 | using namespace __sanitizer; |
| 23 | using namespace __ubsan; |
| 24 | |
| 25 | namespace __ubsan { |
| 26 | |
| 27 | static StaticSpinMutex InitMutex; |
| 28 | static atomic_uint8_t Initialized; |
| 29 | |
| 30 | void Initialize() { |
| 31 | if (LIKELY(atomic_load(&Initialized, memory_order_acquire))) |
| 32 | return; |
| 33 | SpinMutexLock L(&InitMutex); |
| 34 | if (atomic_load(a: &Initialized, mo: memory_order_relaxed)) |
| 35 | return; |
| 36 | SanitizerToolName = "UndefinedBehaviorSanitizer" ; |
| 37 | __ubsan_set_offload_symbolize( |
| 38 | Fn: [](uptr PC) { return Offload::Get().Symbolize(PC); }); |
| 39 | Offload::Get().RegisterHandler(Fn: HandleOffloadReport); |
| 40 | Atexit(function: [] { Offload::Get().UntrackImages(); }); |
| 41 | AddDieCallback(callback: [] { Offload::Get().UntrackImages(); }); |
| 42 | atomic_store(a: &Initialized, v: 1, mo: memory_order_release); |
| 43 | } |
| 44 | |
| 45 | } // namespace __ubsan |
| 46 | |
| 47 | // The shared runtime exports these to every program, act as if HSA is absent |
| 48 | // when it is not loaded. |
| 49 | #define UBSAN_HSA_ENTER(name) \ |
| 50 | Initialize(); \ |
| 51 | if (UNLIKELY(!REAL(name))) { \ |
| 52 | INTERCEPT_FUNCTION(name); \ |
| 53 | if (UNLIKELY(!REAL(name))) { \ |
| 54 | VReport(1, "%s: cannot find %s in this process\n", SanitizerToolName, \ |
| 55 | #name); \ |
| 56 | return HSA_STATUS_ERROR; \ |
| 57 | } \ |
| 58 | } |
| 59 | |
| 60 | #define UBSAN_HSA_FORWARD(name, ...) \ |
| 61 | UBSAN_HSA_ENTER(name); \ |
| 62 | if (UNLIKELY(!Offload::Get().Ready())) \ |
| 63 | return REAL(name)(__VA_ARGS__); |
| 64 | |
| 65 | INTERCEPTOR(hsa_status_t, hsa_init, void) { |
| 66 | UBSAN_HSA_ENTER(hsa_init); |
| 67 | |
| 68 | hsa_status_t Status = REAL(hsa_init)(); |
| 69 | if (Status != HSA_STATUS_SUCCESS) |
| 70 | return Status; |
| 71 | |
| 72 | Offload::Get().Init(); |
| 73 | return Status; |
| 74 | } |
| 75 | |
| 76 | INTERCEPTOR(hsa_status_t, hsa_shut_down, void) { |
| 77 | UBSAN_HSA_ENTER(hsa_shut_down); |
| 78 | |
| 79 | Offload::Get().Shutdown(); |
| 80 | return REAL(hsa_shut_down)(); |
| 81 | } |
| 82 | |
| 83 | INTERCEPTOR(hsa_status_t, hsa_executable_freeze, hsa_executable_t Executable, |
| 84 | const char *Options) { |
| 85 | UBSAN_HSA_FORWARD(hsa_executable_freeze, Executable, Options); |
| 86 | |
| 87 | hsa_status_t Status = REAL(hsa_executable_freeze)(Executable, Options); |
| 88 | if (Status == HSA_STATUS_SUCCESS) |
| 89 | Offload::Get().TrackExecutable(Exec: Executable); |
| 90 | return Status; |
| 91 | } |
| 92 | |
| 93 | INTERCEPTOR(hsa_status_t, hsa_executable_destroy, hsa_executable_t Executable) { |
| 94 | UBSAN_HSA_FORWARD(hsa_executable_destroy, Executable); |
| 95 | |
| 96 | Offload::Get().UntrackExecutable(Exec: Executable); |
| 97 | return REAL(hsa_executable_destroy)(Executable); |
| 98 | } |
| 99 | |
| 100 | extern "C" void __ubsan_offload_init() { __ubsan::Initialize(); } |
| 101 | |
| 102 | __attribute__((constructor(0))) static void UbsanOffloadDynInit() { |
| 103 | __ubsan_offload_init(); |
| 104 | WarnIfHsaNotInterposed(); |
| 105 | } |
| 106 | |