[asan,tsan] introduce sanitizer_common/sanitizer_defs.h and perform some renaming in asan rt. More to come.

llvm-svn: 157743
This commit is contained in:
Kostya Serebryany 2012-05-31 14:11:07 +00:00
parent a39c2fec46
commit 1b71207f48
9 changed files with 98 additions and 75 deletions

View File

@ -243,10 +243,10 @@ $(BIN)/asan_break_optimization$(SUFF).o: tests/asan_break_optimization.cc $(MAKE
$(CLEANROOM_CXX) $(PIE) $(CFLAGS) -c $< -O0 -o $@
$(BIN)/%_test$(SUFF).o: tests/%_test.cc $(RTL_HDR) $(MAKEFILE)
$(ASAN_CXX) $(GTEST_INCLUDE) -I. -g -c $< -O2 -o $@ $(PIE) $(CFLAGS)
$(ASAN_CXX) $(GTEST_INCLUDE) -I. -I.. -g -c $< -O2 -o $@ $(PIE) $(CFLAGS)
$(BIN)/%_test$(SUFF).o: tests/%_test.mm $(RTL_HDR) $(MAKEFILE)
$(ASAN_CXX) $(GTEST_INCLUDE) -I. -g -c $< -O2 -o $@ -ObjC $(PIE) $(CFLAGS)
$(ASAN_CXX) $(GTEST_INCLUDE) -I. -I.. -g -c $< -O2 -o $@ -ObjC $(PIE) $(CFLAGS)
RTL_COMMON_FLAGS=$(PIE) $(CFLAGS) -fPIC -c -O2 -fno-exceptions -funwind-tables \
-Ithird_party -I.. $(ASAN_FLAGS)

View File

@ -55,7 +55,7 @@ static const size_t kMaxSizeForThreadLocalFreeList =
static const size_t kMallocSizeClassStepLog = 26;
static const size_t kMallocSizeClassStep = 1UL << kMallocSizeClassStepLog;
static const size_t kMaxAllowedMallocSize =
static const uptr kMaxAllowedMallocSize =
(__WORDSIZE == 32) ? 3UL << 30 : 8UL << 30;
static inline bool IsAligned(uintptr_t a, uintptr_t alignment) {
@ -1036,7 +1036,7 @@ void FakeStack::OnFree(size_t ptr, size_t size, size_t real_stack) {
// ---------------------- Interface ---------------- {{{1
using namespace __asan; // NOLINT
size_t __asan_stack_malloc(size_t size, size_t real_stack) {
uptr __asan_stack_malloc(size_t size, size_t real_stack) {
if (!FLAG_use_fake_stack) return real_stack;
AsanThread *t = asanThreadRegistry().GetCurrent();
if (!t) {
@ -1057,7 +1057,7 @@ void __asan_stack_free(size_t ptr, size_t size, size_t real_stack) {
// ASan allocator doesn't reserve extra bytes, so normally we would
// just return "size".
size_t __asan_get_estimated_allocated_size(size_t size) {
uptr __asan_get_estimated_allocated_size(uptr size) {
if (size == 0) return 1;
return Min(size, kMaxAllowedMallocSize);
}
@ -1066,7 +1066,7 @@ bool __asan_get_ownership(const void *p) {
return malloc_info.AllocationSize((uintptr_t)p) > 0;
}
size_t __asan_get_allocated_size(const void *p) {
uptr __asan_get_allocated_size(const void *p) {
if (p == NULL) return 0;
size_t allocated_size = malloc_info.AllocationSize((uintptr_t)p);
// Die if p is not malloced or if it is already freed.

View File

@ -139,7 +139,7 @@ static void UnregisterGlobal(const Global *g) {
using namespace __asan; // NOLINT
// Register one global with a default redzone.
void __asan_register_global(uintptr_t addr, size_t size,
void __asan_register_global(uptr addr, uptr size,
const char *name) {
if (!FLAG_report_globals) return;
ScopedLock lock(&mu_for_globals);
@ -152,20 +152,20 @@ void __asan_register_global(uintptr_t addr, size_t size,
}
// Register an array of globals.
void __asan_register_globals(__asan_global *globals, size_t n) {
void __asan_register_globals(__asan_global *globals, uptr n) {
if (!FLAG_report_globals) return;
ScopedLock lock(&mu_for_globals);
for (size_t i = 0; i < n; i++) {
for (uptr i = 0; i < n; i++) {
RegisterGlobal(&globals[i]);
}
}
// Unregister an array of globals.
// We must do it when a shared objects gets dlclosed.
void __asan_unregister_globals(__asan_global *globals, size_t n) {
void __asan_unregister_globals(__asan_global *globals, uptr n) {
if (!FLAG_report_globals) return;
ScopedLock lock(&mu_for_globals);
for (size_t i = 0; i < n; i++) {
for (uptr i = 0; i < n; i++) {
UnregisterGlobal(&globals[i]);
}
}

View File

@ -15,54 +15,44 @@
#ifndef ASAN_INTERFACE_H
#define ASAN_INTERFACE_H
#include "sanitizer_common/sanitizer_defs.h"
// ----------- ATTENTION -------------
// This header should NOT include any other headers from ASan runtime.
// All functions in this header are extern "C" and start with __asan_.
#if !defined(_WIN32)
#include <stdint.h> // for uintptr_t
#define ASAN_INTERFACE_FUNCTION_ATTRIBUTE __attribute__((visibility("default")))
#define ASAN_WEAK_ATTRIBUTE __attribute__((weak));
#else
// TODO(timurrrr): find out what we need on Windows. __declspec(dllexport) ?
#define ASAN_INTERFACE_FUNCTION_ATTRIBUTE
#define ASAN_WEAK_ATTRIBUTE
#endif
#include <stddef.h> // for size_t
extern "C" {
// This function should be called at the very beginning of the process,
// before any instrumented code is executed and before any call to malloc.
void __asan_init() ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_init() SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// This function should be called by the instrumented code.
// 'addr' is the address of a global variable called 'name' of 'size' bytes.
void __asan_register_global(uintptr_t addr, size_t size, const char *name)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_register_global(uptr addr, uptr size, const char *name)
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// This structure describes an instrumented global variable.
struct __asan_global {
size_t beg; // The address of the global.
size_t size; // The original size of the global.
size_t size_with_redzone; // The size with the redzone.
uptr beg; // The address of the global.
uptr size; // The original size of the global.
uptr size_with_redzone; // The size with the redzone.
const char *name; // Name as a C string.
};
// These two functions should be called by the instrumented code.
// 'globals' is an array of structures describing 'n' globals.
void __asan_register_globals(__asan_global *globals, size_t n)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_unregister_globals(__asan_global *globals, size_t n)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_register_globals(__asan_global *globals, uptr n)
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_unregister_globals(__asan_global *globals, uptr n)
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// These two functions are used by the instrumented code in the
// use-after-return mode. __asan_stack_malloc allocates size bytes of
// fake stack and __asan_stack_free poisons it. real_stack is a pointer to
// the real stack region.
size_t __asan_stack_malloc(size_t size, size_t real_stack)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_stack_free(size_t ptr, size_t size, size_t real_stack)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
uptr __asan_stack_malloc(uptr size, uptr real_stack)
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_stack_free(uptr ptr, uptr size, uptr real_stack)
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Marks memory region [addr, addr+size) as unaddressable.
// This memory must be previously allocated by the user program. Accessing
@ -72,8 +62,8 @@ extern "C" {
// to ASan alignment restrictions.
// Method is NOT thread-safe in the sense that no two threads can
// (un)poison memory in the same memory region simultaneously.
void __asan_poison_memory_region(void const volatile *addr, size_t size)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_poison_memory_region(void const volatile *addr, uptr size)
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Marks memory region [addr, addr+size) as addressable.
// This memory must be previously allocated by the user program. Accessing
// addresses in this region is allowed until this region is poisoned again.
@ -81,12 +71,12 @@ extern "C" {
// ASan alignment restrictions.
// Method is NOT thread-safe in the sense that no two threads can
// (un)poison memory in the same memory region simultaneously.
void __asan_unpoison_memory_region(void const volatile *addr, size_t size)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_unpoison_memory_region(void const volatile *addr, uptr size)
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Performs cleanup before a NoReturn function. Must be called before things
// like _exit and execl to avoid false positives on stack.
void __asan_handle_no_return() ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_handle_no_return() SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// User code should use macro instead of functions.
#if !defined(__has_feature)
@ -107,70 +97,69 @@ extern "C" {
// Returns true iff addr is poisoned (i.e. 1-byte read/write access to this
// address will result in error report from AddressSanitizer).
bool __asan_address_is_poisoned(void const volatile *addr)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// This is an internal function that is called to report an error.
// However it is still a part of the interface because users may want to
// set a breakpoint on this function in a debugger.
void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
uintptr_t addr, bool is_write, size_t access_size)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_report_error(uptr pc, uptr bp, uptr sp,
uptr addr, bool is_write, uptr access_size)
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Sets the exit code to use when reporting an error.
// Returns the old value.
int __asan_set_error_exit_code(int exit_code)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Sets the callback to be called right before death on error.
// Passing NULL will unset the callback.
void __asan_set_death_callback(void (*callback)(void))
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
void __asan_set_error_report_callback(void (*callback)(const char*))
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Returns the estimated number of bytes that will be reserved by allocator
// for request of "size" bytes. If ASan allocator can't allocate that much
// memory, returns the maximal possible allocation size, otherwise returns
// "size".
size_t __asan_get_estimated_allocated_size(size_t size)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
uptr __asan_get_estimated_allocated_size(uptr size)
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Returns true if p was returned by the ASan allocator and
// is not yet freed.
bool __asan_get_ownership(const void *p)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Returns the number of bytes reserved for the pointer p.
// Requires (get_ownership(p) == true) or (p == NULL).
size_t __asan_get_allocated_size(const void *p)
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
uptr __asan_get_allocated_size(const void *p)
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Number of bytes, allocated and not yet freed by the application.
size_t __asan_get_current_allocated_bytes()
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
uptr __asan_get_current_allocated_bytes()
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Number of bytes, mmaped by asan allocator to fulfill allocation requests.
// Generally, for request of X bytes, allocator can reserve and add to free
// lists a large number of chunks of size X to use them for future requests.
// All these chunks count toward the heap size. Currently, allocator never
// releases memory to OS (instead, it just puts freed chunks to free lists).
size_t __asan_get_heap_size()
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
uptr __asan_get_heap_size()
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Number of bytes, mmaped by asan allocator, which can be used to fulfill
// allocation requests. When a user program frees memory chunk, it can first
// fall into quarantine and will count toward __asan_get_free_bytes() later.
size_t __asan_get_free_bytes()
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
uptr __asan_get_free_bytes()
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Number of bytes in unmapped pages, that are released to OS. Currently,
// always returns 0.
size_t __asan_get_unmapped_bytes()
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
uptr __asan_get_unmapped_bytes()
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
// Prints accumulated stats to stderr. Used for debugging.
void __asan_print_accumulated_stats()
ASAN_INTERFACE_FUNCTION_ATTRIBUTE;
SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
#if !defined(_WIN32)
// We do not need to redefine the defaults right now on Windows.
char *__asan_default_options
ASAN_WEAK_ATTRIBUTE;
SANITIZER_WEAK_ATTRIBUTE;
#endif
} // namespace
#undef ASAN_INTERFACE_FUNCTION_ATTRIBUTE
#endif // ASAN_INTERFACE_H

View File

@ -74,7 +74,7 @@ using namespace __asan; // NOLINT
// at least [left, AlignDown(right)).
// * if user asks to unpoison region [left, right), the program unpoisons
// at most [AlignDown(left), right).
void __asan_poison_memory_region(void const volatile *addr, size_t size) {
void __asan_poison_memory_region(void const volatile *addr, uptr size) {
if (!FLAG_allow_user_poisoning || size == 0) return;
uintptr_t beg_addr = (uintptr_t)addr;
uintptr_t end_addr = beg_addr + size;
@ -115,7 +115,7 @@ void __asan_poison_memory_region(void const volatile *addr, size_t size) {
}
}
void __asan_unpoison_memory_region(void const volatile *addr, size_t size) {
void __asan_unpoison_memory_region(void const volatile *addr, uptr size) {
if (!FLAG_allow_user_poisoning || size == 0) return;
uintptr_t beg_addr = (uintptr_t)addr;
uintptr_t end_addr = beg_addr + size;

View File

@ -359,8 +359,8 @@ void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
}
}
void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
uintptr_t addr, bool is_write, size_t access_size) {
void __asan_report_error(uptr pc, uptr bp, uptr sp,
uptr addr, bool is_write, uptr access_size) {
// Do not print more than one report, otherwise they will mix up.
static int num_calls = 0;
if (AtomicInc(&num_calls) > 1) return;

View File

@ -67,19 +67,19 @@ static void PrintAccumulatedStats() {
// ---------------------- Interface ---------------- {{{1
using namespace __asan; // NOLINT
size_t __asan_get_current_allocated_bytes() {
uptr __asan_get_current_allocated_bytes() {
return asanThreadRegistry().GetCurrentAllocatedBytes();
}
size_t __asan_get_heap_size() {
uptr __asan_get_heap_size() {
return asanThreadRegistry().GetHeapSize();
}
size_t __asan_get_free_bytes() {
uptr __asan_get_free_bytes() {
return asanThreadRegistry().GetFreeBytes();
}
size_t __asan_get_unmapped_bytes() {
uptr __asan_get_unmapped_bytes() {
return 0;
}

View File

@ -0,0 +1,34 @@
//===-- sanitizer_defs.h ----------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is shared between AddressSanitizer and ThreadSanitizer.
//===----------------------------------------------------------------------===//
#ifndef SANITIZER_DEFS_H
#define SANITIZER_DEFS_H
// ----------- ATTENTION -------------
// This header should NOT include any other headers to avoid portability issues.
#if defined(_WIN32)
// FIXME find out what we need on Windows. __declspec(dllexport) ?
#define SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE
#define SANITIZER_WEAK_ATTRIBUTE
#else
#define SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE \
__attribute__((visibility("default")))
#define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak));
#endif
// For portability reasons we do not include stddef.h, stdint.h or any other
// system header, but we do need some basic types that are not defined
// in a portable way by the language itself.
typedef unsigned long uptr; // Unsigned integer of the same size as a pointer.
// FIXME: add u64, u32, etc.
#endif // SANITIZER_DEFS_H

View File

@ -15,8 +15,8 @@
// We also define several basic types here to avoid using system headers
// as the latter complicate portability of this low-level code.
//===----------------------------------------------------------------------===//
#ifndef MINILIBC_H
#define MINILIBC_H
#ifndef SANITIZER_LIBC_H
#define SANITIZER_LIBC_H
// No code here yet. Will move more code in the next changes.
namespace __sanitizer {
@ -25,4 +25,4 @@ void MiniLibcStub();
} // namespace __sanitizer
#endif // MINILIBC_H
#endif // SANITIZER_LIBC_H