llvm/compiler-rt/lib/msan/msan.cpp

//===-- msan.cpp ----------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of MemorySanitizer.
//
// MemorySanitizer runtime.
//===----------------------------------------------------------------------===//

#include "msan.h"

#include "msan_chained_origin_depot.h"
#include "msan_origin.h"
#include "msan_poisoning.h"
#include "msan_report.h"
#include "msan_thread.h"
#include "sanitizer_common/sanitizer_atomic.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_flag_parser.h"
#include "sanitizer_common/sanitizer_flags.h"
#include "sanitizer_common/sanitizer_interface_internal.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "sanitizer_common/sanitizer_procmaps.h"
#include "sanitizer_common/sanitizer_stackdepot.h"
#include "sanitizer_common/sanitizer_stacktrace.h"
#include "sanitizer_common/sanitizer_symbolizer.h"
#include "ubsan/ubsan_flags.h"
#include "ubsan/ubsan_init.h"

// ACHTUNG! No system header includes in this file.

usingnamespace__sanitizer;

// Globals.
static THREADLOCAL int msan_expect_umr =;
static THREADLOCAL int msan_expected_umr_found =;

// Function argument shadow. Each argument starts at the next available 8-byte
// aligned address.
SANITIZER_INTERFACE_ATTRIBUTE
THREADLOCAL u64 __msan_param_tls[kMsanParamTlsSize / sizeof(u64)];

// Function argument origin. Each argument starts at the same offset as the
// corresponding shadow in (__msan_param_tls). Slightly weird, but changing this
// would break compatibility with older prebuilt binaries.
SANITIZER_INTERFACE_ATTRIBUTE
THREADLOCAL u32 __msan_param_origin_tls[kMsanParamTlsSize / sizeof(u32)];

SANITIZER_INTERFACE_ATTRIBUTE
THREADLOCAL u64 __msan_retval_tls[kMsanRetvalTlsSize / sizeof(u64)];

SANITIZER_INTERFACE_ATTRIBUTE
THREADLOCAL u32 __msan_retval_origin_tls;

alignas(16) SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL u64
    __msan_va_arg_tls[kMsanParamTlsSize / sizeof(u64)];

alignas(16) SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL u32
    __msan_va_arg_origin_tls[kMsanParamTlsSize / sizeof(u32)];

SANITIZER_INTERFACE_ATTRIBUTE
THREADLOCAL u64 __msan_va_arg_overflow_size_tls;

SANITIZER_INTERFACE_ATTRIBUTE
THREADLOCAL u32 __msan_origin_tls;

extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_track_origins;

int __msan_get_track_origins() {}

extern "C" SANITIZER_WEAK_ATTRIBUTE const int __msan_keep_going;

namespace __msan {

static THREADLOCAL int is_in_symbolizer_or_unwinder;
static void EnterSymbolizerOrUnwider() {}
static void ExitSymbolizerOrUnwider() {}
bool IsInSymbolizerOrUnwider() {}

struct UnwinderScope {};

static Flags msan_flags;

Flags *flags() {}

int msan_inited =;
bool msan_init_is_running;

int msan_report_count =;

// Array of stack origins.
// FIXME: make it resizable.
// Although BSS memory doesn't cost anything until used, it is limited to 2GB
// in some configurations (e.g., "relocation R_X86_64_PC32 out of range:
// ... is not in [-2147483648, 2147483647]; references section '.bss'").
// We use kNumStackOriginDescrs * (sizeof(char*) + sizeof(uptr)) == 64MB.
#if SANITIZER_PPC
// soft_rss_limit test (release_origin.c) fails on PPC if kNumStackOriginDescrs
// is too high
static const uptr kNumStackOriginDescrs = 1 * 1024 * 1024;
#else
static const uptr kNumStackOriginDescrs =;
#endif  // SANITIZER_PPC
static const char *StackOriginDescr[kNumStackOriginDescrs];
static uptr StackOriginPC[kNumStackOriginDescrs];
static atomic_uint32_t NumStackOriginDescrs;

void Flags::SetDefaults() {}

// keep_going is an old name for halt_on_error,
// and it has inverse meaning.
class FlagHandlerKeepGoing final : public FlagHandlerBase {};

static void RegisterMsanFlags(FlagParser *parser, Flags *f) {}

static void InitializeFlags() {}

void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin) {}

void UnpoisonParam(uptr n) {}

// Backup MSan runtime TLS state.
// Implementation must be async-signal-safe.
// Instances of this class may live on the signal handler stack, and data size
// may be an issue.
void ScopedThreadLocalStateBackup::Backup() {}

void ScopedThreadLocalStateBackup::Restore() {}

void UnpoisonThreadLocalState() {}

const char *GetStackOriginDescr(u32 id, uptr *pc) {}

u32 ChainOrigin(u32 id, StackTrace *stack) {}

// Current implementation separates the 'id_ptr' from the 'descr' and makes
// 'descr' constant.
// Previous implementation 'descr' is created at compile time and contains
// '----' in the beginning.  When we see descr for the first time we replace
// '----' with a uniq id and set the origin to (id | (31-th bit)).
static inline void SetAllocaOrigin(void *a, uptr size, u32 *id_ptr, char *descr,
                                   uptr pc) {}

}  // namespace __msan

void __sanitizer::BufferedStackTrace::UnwindImpl(
    uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {}

// Interface.

usingnamespace__msan;

#define MSAN_MAYBE_WARNING(type, size)

MSAN_MAYBE_WARNING(u8, 1)
MSAN_MAYBE_WARNING(u16, 2)
MSAN_MAYBE_WARNING(u32, 4)
MSAN_MAYBE_WARNING(u64, 8)

#define MSAN_MAYBE_STORE_ORIGIN(type, size)

MSAN_MAYBE_STORE_ORIGIN(u8, 1)
MSAN_MAYBE_STORE_ORIGIN(u16, 2)
MSAN_MAYBE_STORE_ORIGIN(u32, 4)
MSAN_MAYBE_STORE_ORIGIN(u64, 8)

void __msan_warning() {}

void __msan_warning_noreturn() {}

void __msan_warning_with_origin(u32 origin) {}

void __msan_warning_with_origin_noreturn(u32 origin) {}

static void OnStackUnwind(const SignalContext &sig, const void *,
                          BufferedStackTrace *stack) {}

static void MsanOnDeadlySignal(int signo, void *siginfo, void *context) {}

static void CheckUnwind() {}

void __msan_init() {}

void __msan_set_keep_going(int keep_going) {}

void __msan_set_expect_umr(int expect_umr) {}

void __msan_print_shadow(const void *x, uptr size) {}

void __msan_dump_shadow(const void *x, uptr size) {}

sptr __msan_test_shadow(const void *x, uptr size) {}

void __msan_check_mem_is_initialized(const void *x, uptr size) {}

int __msan_set_poison_in_malloc(int do_poison) {}

int __msan_has_dynamic_component() {}

NOINLINE
void __msan_clear_on_return() {}

void __msan_partial_poison(const void* data, void* shadow, uptr size) {}

void __msan_load_unpoisoned(const void *src, uptr size, void *dst) {}

void __msan_set_origin(const void *a, uptr size, u32 origin) {}

void __msan_set_alloca_origin(void *a, uptr size, char *descr) {}

void __msan_set_alloca_origin4(void *a, uptr size, char *descr, uptr pc) {}

void __msan_set_alloca_origin_with_descr(void *a, uptr size, u32 *id_ptr,
                                         char *descr) {}

void __msan_set_alloca_origin_no_descr(void *a, uptr size, u32 *id_ptr) {}

u32 __msan_chain_origin(u32 id) {}

u32 __msan_get_origin(const void *a) {}

int __msan_origin_is_descendant_or_same(u32 this_id, u32 prev_id) {}

u32 __msan_get_umr_origin() {}

u16 __sanitizer_unaligned_load16(const uu16 *p) {}
u32 __sanitizer_unaligned_load32(const uu32 *p) {}
u64 __sanitizer_unaligned_load64(const uu64 *p) {}
void __sanitizer_unaligned_store16(uu16 *p, u16 x) {}
void __sanitizer_unaligned_store32(uu32 *p, u32 x) {}
void __sanitizer_unaligned_store64(uu64 *p, u64 x) {}

void __msan_set_death_callback(void (*callback)(void)) {}

void __msan_start_switch_fiber(const void *bottom, uptr size) {}

void __msan_finish_switch_fiber(const void **bottom_old, uptr *size_old) {}

SANITIZER_INTERFACE_WEAK_DEF(const char *, __msan_default_options, void) {}

extern "C" {
SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_print_stack_trace() {}
} // extern "C"