Path: blob/main/contrib/llvm-project/compiler-rt/lib/rtsan/rtsan_context.cpp
35233 views
//===--- rtsan_context.cpp - Realtime Sanitizer -----------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8//===----------------------------------------------------------------------===//910#include <rtsan/rtsan_context.h>1112#include <rtsan/rtsan_stack.h>1314#include <sanitizer_common/sanitizer_allocator_internal.h>15#include <sanitizer_common/sanitizer_stacktrace.h>1617#include <new>18#include <pthread.h>19#include <stdio.h>20#include <stdlib.h>2122static pthread_key_t context_key;23static pthread_once_t key_once = PTHREAD_ONCE_INIT;2425// InternalFree cannot be passed directly to pthread_key_create26// because it expects a signature with only one arg27static void InternalFreeWrapper(void *ptr) { __sanitizer::InternalFree(ptr); }2829static __rtsan::Context &GetContextForThisThreadImpl() {30auto make_thread_local_context_key = []() {31CHECK_EQ(pthread_key_create(&context_key, InternalFreeWrapper), 0);32};3334pthread_once(&key_once, make_thread_local_context_key);35__rtsan::Context *current_thread_context =36static_cast<__rtsan::Context *>(pthread_getspecific(context_key));37if (current_thread_context == nullptr) {38current_thread_context = static_cast<__rtsan::Context *>(39__sanitizer::InternalAlloc(sizeof(__rtsan::Context)));40new (current_thread_context) __rtsan::Context();41pthread_setspecific(context_key, current_thread_context);42}4344return *current_thread_context;45}4647/*48This is a placeholder stub for a future feature that will allow49a user to configure RTSan's behaviour when a real-time safety50violation is detected. The RTSan developers intend for the51following choices to be made available, via a RTSAN_OPTIONS52environment variable, in a future PR:5354i) exit,55ii) continue, or56iii) wait for user input from stdin.5758Until then, and to keep the first PRs small, only the exit mode59is available.60*/61static void InvokeViolationDetectedAction() { exit(EXIT_FAILURE); }6263__rtsan::Context::Context() = default;6465void __rtsan::Context::RealtimePush() { realtime_depth++; }6667void __rtsan::Context::RealtimePop() { realtime_depth--; }6869void __rtsan::Context::BypassPush() { bypass_depth++; }7071void __rtsan::Context::BypassPop() { bypass_depth--; }7273void __rtsan::Context::ExpectNotRealtime(74const char *intercepted_function_name) {75if (InRealtimeContext() && !IsBypassed()) {76BypassPush();77PrintDiagnostics(intercepted_function_name);78InvokeViolationDetectedAction();79BypassPop();80}81}8283bool __rtsan::Context::InRealtimeContext() const { return realtime_depth > 0; }8485bool __rtsan::Context::IsBypassed() const { return bypass_depth > 0; }8687void __rtsan::Context::PrintDiagnostics(const char *intercepted_function_name) {88fprintf(stderr,89"Real-time violation: intercepted call to real-time unsafe function "90"`%s` in real-time context! Stack trace:\n",91intercepted_function_name);92__rtsan::PrintStackTrace();93}9495__rtsan::Context &__rtsan::GetContextForThisThread() {96return GetContextForThisThreadImpl();97}9899100