gcc/libsanitizer/tsan/tsan_interface_java.cpp

259 lines
8.0 KiB
C++
Raw Normal View History

//===-- tsan_interface_java.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 ThreadSanitizer (TSan), a race detector.
//
//===----------------------------------------------------------------------===//
#include "tsan_interface_java.h"
#include "tsan_rtl.h"
#include "sanitizer_common/sanitizer_internal_defs.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_placement_new.h"
#include "sanitizer_common/sanitizer_stacktrace.h"
#include "sanitizer_common/sanitizer_procmaps.h"
using namespace __tsan;
const jptr kHeapAlignment = 8;
namespace __tsan {
struct JavaContext {
const uptr heap_begin;
const uptr heap_size;
JavaContext(jptr heap_begin, jptr heap_size)
: heap_begin(heap_begin)
, heap_size(heap_size) {
}
};
static u64 jctx_buf[sizeof(JavaContext) / sizeof(u64) + 1];
static JavaContext *jctx;
MBlock *JavaHeapBlock(uptr addr, uptr *start) {
if (!jctx || addr < jctx->heap_begin ||
addr >= jctx->heap_begin + jctx->heap_size)
return nullptr;
for (uptr p = RoundDown(addr, kMetaShadowCell); p >= jctx->heap_begin;
p -= kMetaShadowCell) {
MBlock *b = ctx->metamap.GetBlock(p);
if (!b)
continue;
if (p + b->siz <= addr)
return nullptr;
*start = p;
return b;
}
return nullptr;
}
} // namespace __tsan
#define JAVA_FUNC_ENTER(func) \
ThreadState *thr = cur_thread(); \
(void)thr;
void __tsan_java_init(jptr heap_begin, jptr heap_size) {
JAVA_FUNC_ENTER(__tsan_java_init);
Initialize(thr);
DPrintf("#%d: java_init(0x%zx, 0x%zx)\n", thr->tid, heap_begin, heap_size);
DCHECK_EQ(jctx, 0);
DCHECK_GT(heap_begin, 0);
DCHECK_GT(heap_size, 0);
DCHECK_EQ(heap_begin % kHeapAlignment, 0);
DCHECK_EQ(heap_size % kHeapAlignment, 0);
DCHECK_LT(heap_begin, heap_begin + heap_size);
jctx = new(jctx_buf) JavaContext(heap_begin, heap_size);
}
int __tsan_java_fini() {
JAVA_FUNC_ENTER(__tsan_java_fini);
DPrintf("#%d: java_fini()\n", thr->tid);
DCHECK_NE(jctx, 0);
// FIXME(dvyukov): this does not call atexit() callbacks.
int status = Finalize(thr);
DPrintf("#%d: java_fini() = %d\n", thr->tid, status);
return status;
}
void __tsan_java_alloc(jptr ptr, jptr size) {
JAVA_FUNC_ENTER(__tsan_java_alloc);
DPrintf("#%d: java_alloc(0x%zx, 0x%zx)\n", thr->tid, ptr, size);
DCHECK_NE(jctx, 0);
DCHECK_NE(size, 0);
DCHECK_EQ(ptr % kHeapAlignment, 0);
DCHECK_EQ(size % kHeapAlignment, 0);
DCHECK_GE(ptr, jctx->heap_begin);
DCHECK_LE(ptr + size, jctx->heap_begin + jctx->heap_size);
OnUserAlloc(thr, 0, ptr, size, false);
}
void __tsan_java_free(jptr ptr, jptr size) {
JAVA_FUNC_ENTER(__tsan_java_free);
DPrintf("#%d: java_free(0x%zx, 0x%zx)\n", thr->tid, ptr, size);
DCHECK_NE(jctx, 0);
DCHECK_NE(size, 0);
DCHECK_EQ(ptr % kHeapAlignment, 0);
DCHECK_EQ(size % kHeapAlignment, 0);
DCHECK_GE(ptr, jctx->heap_begin);
DCHECK_LE(ptr + size, jctx->heap_begin + jctx->heap_size);
ctx->metamap.FreeRange(thr->proc(), ptr, size);
}
void __tsan_java_move(jptr src, jptr dst, jptr size) {
JAVA_FUNC_ENTER(__tsan_java_move);
DPrintf("#%d: java_move(0x%zx, 0x%zx, 0x%zx)\n", thr->tid, src, dst, size);
DCHECK_NE(jctx, 0);
DCHECK_NE(size, 0);
DCHECK_EQ(src % kHeapAlignment, 0);
DCHECK_EQ(dst % kHeapAlignment, 0);
DCHECK_EQ(size % kHeapAlignment, 0);
DCHECK_GE(src, jctx->heap_begin);
DCHECK_LE(src + size, jctx->heap_begin + jctx->heap_size);
DCHECK_GE(dst, jctx->heap_begin);
DCHECK_LE(dst + size, jctx->heap_begin + jctx->heap_size);
DCHECK_NE(dst, src);
DCHECK_NE(size, 0);
// Assuming it's not running concurrently with threads that do
// memory accesses and mutex operations (stop-the-world phase).
ctx->metamap.MoveMemory(src, dst, size);
// Clear the destination shadow range.
// We used to move shadow from src to dst, but the trace format does not
// support that anymore as it contains addresses of accesses.
RawShadow *d = MemToShadow(dst);
RawShadow *dend = MemToShadow(dst + size);
internal_memset(d, 0, (dend - d) * sizeof(*d));
}
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
jptr __tsan_java_find(jptr *from_ptr, jptr to) {
JAVA_FUNC_ENTER(__tsan_java_find);
DPrintf("#%d: java_find(&0x%zx, 0x%zx)\n", thr->tid, *from_ptr, to);
DCHECK_EQ((*from_ptr) % kHeapAlignment, 0);
DCHECK_EQ(to % kHeapAlignment, 0);
DCHECK_GE(*from_ptr, jctx->heap_begin);
DCHECK_LE(to, jctx->heap_begin + jctx->heap_size);
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
for (uptr from = *from_ptr; from < to; from += kHeapAlignment) {
MBlock *b = ctx->metamap.GetBlock(from);
if (b) {
*from_ptr = from;
return b->siz;
}
}
return 0;
}
void __tsan_java_finalize() {
JAVA_FUNC_ENTER(__tsan_java_finalize);
DPrintf("#%d: java_finalize()\n", thr->tid);
AcquireGlobal(thr);
}
void __tsan_java_mutex_lock(jptr addr) {
JAVA_FUNC_ENTER(__tsan_java_mutex_lock);
DPrintf("#%d: java_mutex_lock(0x%zx)\n", thr->tid, addr);
DCHECK_NE(jctx, 0);
DCHECK_GE(addr, jctx->heap_begin);
DCHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
MutexPostLock(thr, 0, addr,
MutexFlagLinkerInit | MutexFlagWriteReentrant |
MutexFlagDoPreLockOnPostLock);
}
void __tsan_java_mutex_unlock(jptr addr) {
JAVA_FUNC_ENTER(__tsan_java_mutex_unlock);
DPrintf("#%d: java_mutex_unlock(0x%zx)\n", thr->tid, addr);
DCHECK_NE(jctx, 0);
DCHECK_GE(addr, jctx->heap_begin);
DCHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
MutexUnlock(thr, 0, addr);
}
void __tsan_java_mutex_read_lock(jptr addr) {
JAVA_FUNC_ENTER(__tsan_java_mutex_read_lock);
DPrintf("#%d: java_mutex_read_lock(0x%zx)\n", thr->tid, addr);
DCHECK_NE(jctx, 0);
DCHECK_GE(addr, jctx->heap_begin);
DCHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
MutexPostReadLock(thr, 0, addr,
MutexFlagLinkerInit | MutexFlagWriteReentrant |
MutexFlagDoPreLockOnPostLock);
}
void __tsan_java_mutex_read_unlock(jptr addr) {
JAVA_FUNC_ENTER(__tsan_java_mutex_read_unlock);
DPrintf("#%d: java_mutex_read_unlock(0x%zx)\n", thr->tid, addr);
DCHECK_NE(jctx, 0);
DCHECK_GE(addr, jctx->heap_begin);
DCHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
MutexReadUnlock(thr, 0, addr);
}
void __tsan_java_mutex_lock_rec(jptr addr, int rec) {
JAVA_FUNC_ENTER(__tsan_java_mutex_lock_rec);
DPrintf("#%d: java_mutex_lock_rec(0x%zx, %d)\n", thr->tid, addr, rec);
DCHECK_NE(jctx, 0);
DCHECK_GE(addr, jctx->heap_begin);
DCHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
DCHECK_GT(rec, 0);
MutexPostLock(thr, 0, addr,
MutexFlagLinkerInit | MutexFlagWriteReentrant |
MutexFlagDoPreLockOnPostLock | MutexFlagRecursiveLock,
rec);
}
int __tsan_java_mutex_unlock_rec(jptr addr) {
JAVA_FUNC_ENTER(__tsan_java_mutex_unlock_rec);
DPrintf("#%d: java_mutex_unlock_rec(0x%zx)\n", thr->tid, addr);
DCHECK_NE(jctx, 0);
DCHECK_GE(addr, jctx->heap_begin);
DCHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
return MutexUnlock(thr, 0, addr, MutexFlagRecursiveUnlock);
}
void __tsan_java_acquire(jptr addr) {
JAVA_FUNC_ENTER(__tsan_java_acquire);
DPrintf("#%d: java_acquire(0x%zx)\n", thr->tid, addr);
DCHECK_NE(jctx, 0);
DCHECK_GE(addr, jctx->heap_begin);
DCHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
Acquire(thr, 0, addr);
}
void __tsan_java_release(jptr addr) {
JAVA_FUNC_ENTER(__tsan_java_release);
DPrintf("#%d: java_release(0x%zx)\n", thr->tid, addr);
DCHECK_NE(jctx, 0);
DCHECK_GE(addr, jctx->heap_begin);
DCHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
Release(thr, 0, addr);
}
void __tsan_java_release_store(jptr addr) {
JAVA_FUNC_ENTER(__tsan_java_release);
DPrintf("#%d: java_release_store(0x%zx)\n", thr->tid, addr);
DCHECK_NE(jctx, 0);
DCHECK_GE(addr, jctx->heap_begin);
DCHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
ReleaseStore(thr, 0, addr);
}