2019-08-14 10:47:11 +02:00
|
|
|
//=-- lsan_thread.cpp -----------------------------------------------------===//
|
2013-11-04 22:33:31 +01:00
|
|
|
//
|
2019-08-14 10:47:11 +02:00
|
|
|
// 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
|
2013-11-04 22:33:31 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of LeakSanitizer.
|
|
|
|
// See lsan_thread.h for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lsan_thread.h"
|
|
|
|
|
2020-06-01 21:15:18 +02:00
|
|
|
#include "lsan.h"
|
|
|
|
#include "lsan_allocator.h"
|
|
|
|
#include "lsan_common.h"
|
2013-11-04 22:33:31 +01:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_placement_new.h"
|
|
|
|
#include "sanitizer_common/sanitizer_thread_registry.h"
|
2016-11-08 23:04:09 +01:00
|
|
|
#include "sanitizer_common/sanitizer_tls_get_addr.h"
|
2013-11-04 22:33:31 +01:00
|
|
|
|
|
|
|
namespace __lsan {
|
|
|
|
|
|
|
|
static ThreadRegistry *thread_registry;
|
|
|
|
|
|
|
|
static ThreadContextBase *CreateThreadContext(u32 tid) {
|
|
|
|
void *mem = MmapOrDie(sizeof(ThreadContext), "ThreadContext");
|
2020-06-01 21:15:18 +02:00
|
|
|
return new (mem) ThreadContext(tid);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const uptr kMaxThreads = 1 << 13;
|
|
|
|
static const uptr kThreadQuarantineSize = 64;
|
|
|
|
|
|
|
|
void InitializeThreadRegistry() {
|
2016-11-08 23:04:09 +01:00
|
|
|
static ALIGNED(64) char thread_registry_placeholder[sizeof(ThreadRegistry)];
|
2020-06-01 21:15:18 +02:00
|
|
|
thread_registry = new (thread_registry_placeholder)
|
|
|
|
ThreadRegistry(CreateThreadContext, kMaxThreads, kThreadQuarantineSize);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
2020-06-01 21:15:18 +02:00
|
|
|
ThreadContextLsanBase::ThreadContextLsanBase(int tid)
|
|
|
|
: ThreadContextBase(tid) {}
|
2013-11-04 22:33:31 +01:00
|
|
|
|
2020-06-01 21:15:18 +02:00
|
|
|
void ThreadContextLsanBase::OnFinished() {
|
2013-11-04 22:33:31 +01:00
|
|
|
AllocatorThreadFinish();
|
2016-11-08 23:04:09 +01:00
|
|
|
DTLS_Destroy();
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
2020-06-01 21:15:18 +02:00
|
|
|
u32 ThreadCreate(u32 parent_tid, uptr user_id, bool detached, void *arg) {
|
|
|
|
return thread_registry->CreateThread(user_id, detached, parent_tid, arg);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
2020-06-01 21:15:18 +02:00
|
|
|
void ThreadContextLsanBase::ThreadStart(u32 tid, tid_t os_id,
|
|
|
|
ThreadType thread_type, void *arg) {
|
|
|
|
thread_registry->StartThread(tid, os_id, thread_type, arg);
|
|
|
|
SetCurrentThread(tid);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadFinish() {
|
|
|
|
thread_registry->FinishThread(GetCurrentThread());
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch
builtins, store max (log2 (align), 0) into uchar field instead of
align into uptr field.
(ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins,
store uchar 0 field instead of uptr 0 field.
(instrument_nonnull_return): Use _v1 suffixed nonnull return builtin,
instead of passing one address of struct with 2 locations pass
two addresses of structs with 1 location each.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed.
(BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins.
* c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword
from expected output regexps.
* c-c++-common/ubsan/float-cast-overflow-2.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-3.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-4.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-5.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-6.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-8.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-9.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-10.c: Likewise.
* g++.dg/ubsan/float-cast-overflow-bf.C: Likewise.
* gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise.
* g++.dg/asan/default-options-1.C (__asan_default_options): Add
used attribute.
* g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2
in the environment.
* All source files: Merge from upstream 315899.
* asan/Makefile.am (nodist_saninclude_HEADERS): Add
include/sanitizer/tsan_interface.h.
* asan/libtool-version: Bump the libasan SONAME.
* lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc.
(lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc.
* sanitizer_common/Makefile.am (sanitizer_common_files): Add
sancov_flags.cc, sanitizer_allocator_checks.cc,
sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc,
sanitizer_file.cc, sanitizer_mac_libcdep.cc and
sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc
and sanitizer_coverage_mapping_libcdep.cc.
* tsan/Makefile.am (tsan_files): Add tsan_external.cc.
* ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1.
(ubsan_files): Add ubsan_init_standalone.cc and
ubsan_signals_standalone.cc.
* ubsan/libtool-version: Bump the libubsan SONAME.
* asan/Makefile.in: Regenerate.
* lsan/Makefile.in: Regenerate.
* sanitizer_common/Makefile.in: Regenerate.
* tsan/Makefile.in: Regenerate.
* ubsan/Makefile.in: Regenerate.
From-SVN: r253887
2017-10-19 13:23:59 +02:00
|
|
|
SetCurrentThread(kInvalidTid);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadContext *CurrentThreadContext() {
|
2020-06-01 21:15:18 +02:00
|
|
|
if (!thread_registry)
|
|
|
|
return nullptr;
|
2013-11-04 22:33:31 +01:00
|
|
|
if (GetCurrentThread() == kInvalidTid)
|
2015-10-21 09:32:45 +02:00
|
|
|
return nullptr;
|
2013-11-04 22:33:31 +01:00
|
|
|
// No lock needed when getting current thread.
|
|
|
|
return (ThreadContext *)thread_registry->GetThreadLocked(GetCurrentThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
|
|
|
|
uptr uid = (uptr)arg;
|
|
|
|
if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 ThreadTid(uptr uid) {
|
2020-06-01 21:15:18 +02:00
|
|
|
return thread_registry->FindThread(FindThreadByUid, (void *)uid);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadJoin(u32 tid) {
|
|
|
|
CHECK_NE(tid, kInvalidTid);
|
2020-06-01 21:15:18 +02:00
|
|
|
thread_registry->JoinThread(tid, /* arg */ nullptr);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnsureMainThreadIDIsCorrect() {
|
|
|
|
if (GetCurrentThread() == 0)
|
|
|
|
CurrentThreadContext()->os_id = GetTid();
|
|
|
|
}
|
|
|
|
|
|
|
|
///// Interface to the common LSan module. /////
|
|
|
|
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch
builtins, store max (log2 (align), 0) into uchar field instead of
align into uptr field.
(ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins,
store uchar 0 field instead of uptr 0 field.
(instrument_nonnull_return): Use _v1 suffixed nonnull return builtin,
instead of passing one address of struct with 2 locations pass
two addresses of structs with 1 location each.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed.
(BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins.
* c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword
from expected output regexps.
* c-c++-common/ubsan/float-cast-overflow-2.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-3.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-4.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-5.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-6.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-8.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-9.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-10.c: Likewise.
* g++.dg/ubsan/float-cast-overflow-bf.C: Likewise.
* gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise.
* g++.dg/asan/default-options-1.C (__asan_default_options): Add
used attribute.
* g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2
in the environment.
* All source files: Merge from upstream 315899.
* asan/Makefile.am (nodist_saninclude_HEADERS): Add
include/sanitizer/tsan_interface.h.
* asan/libtool-version: Bump the libasan SONAME.
* lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc.
(lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc.
* sanitizer_common/Makefile.am (sanitizer_common_files): Add
sancov_flags.cc, sanitizer_allocator_checks.cc,
sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc,
sanitizer_file.cc, sanitizer_mac_libcdep.cc and
sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc
and sanitizer_coverage_mapping_libcdep.cc.
* tsan/Makefile.am (tsan_files): Add tsan_external.cc.
* ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1.
(ubsan_files): Add ubsan_init_standalone.cc and
ubsan_signals_standalone.cc.
* ubsan/libtool-version: Bump the libubsan SONAME.
* asan/Makefile.in: Regenerate.
* lsan/Makefile.in: Regenerate.
* sanitizer_common/Makefile.in: Regenerate.
* tsan/Makefile.in: Regenerate.
* ubsan/Makefile.in: Regenerate.
From-SVN: r253887
2017-10-19 13:23:59 +02:00
|
|
|
void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
|
2020-06-01 21:15:18 +02:00
|
|
|
void *arg) {}
|
2013-12-05 10:18:38 +01:00
|
|
|
|
2020-06-01 21:15:18 +02:00
|
|
|
void LockThreadRegistry() { thread_registry->Lock(); }
|
2013-11-04 22:33:31 +01:00
|
|
|
|
2020-06-01 21:15:18 +02:00
|
|
|
void UnlockThreadRegistry() { thread_registry->Unlock(); }
|
2013-11-04 22:33:31 +01:00
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
ThreadRegistry *GetThreadRegistryLocked() {
|
|
|
|
thread_registry->CheckLocked();
|
|
|
|
return thread_registry;
|
|
|
|
}
|
|
|
|
|
2020-06-01 21:15:18 +02:00
|
|
|
} // namespace __lsan
|