[Sanitizer] Move more functions/constants to sanitizer_common.

llvm-svn: 158056
This commit is contained in:
Alexey Samsonov 2012-06-06 09:26:25 +00:00
parent f1ef87ddbb
commit ee07290628
18 changed files with 119 additions and 46 deletions

View File

@ -220,12 +220,6 @@ s64 internal_atoll(const char *nptr) {
return internal_simple_strtoll(nptr, (char**)0, 10);
}
uptr internal_strlen(const char *s) {
uptr i = 0;
while (s[i]) i++;
return i;
}
uptr internal_strnlen(const char *s, uptr maxlen) {
#if ASAN_INTERCEPT_STRNLEN
if (REAL(strnlen) != 0) {

View File

@ -32,7 +32,6 @@ namespace __asan {
// __asan::internal_X() is the implementation of X() for use in RTL.
s64 internal_atoll(const char *nptr);
uptr internal_strlen(const char *s);
uptr internal_strnlen(const char *s, uptr maxlen);
char* internal_strchr(const char *s, int c);
void* internal_memset(void *s, int c, uptr n);

View File

@ -141,7 +141,6 @@ bool AsanInterceptsSignal(int signum);
void SetAlternateSignalStack();
void UnsetAlternateSignalStack();
void InstallSignalHandlers();
int GetPid();
uptr GetThreadSelf();
int AtomicInc(int *a);
u16 AtomicExchange(u16 *a, u16 new_val);
@ -242,11 +241,6 @@ int Atexit(void (*function)(void));
#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
const uptr kWordSize = __WORDSIZE / 8;
const uptr kWordSizeInBits = 8 * kWordSize;
const uptr kPageSizeBits = 12;
const uptr kPageSize = 1UL << kPageSizeBits;
#if !defined(_WIN32) || defined(__clang__)
# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
@ -291,16 +285,6 @@ const int kAsanInternalHeapMagic = 0xfe;
static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
static const uptr kRetiredStackFrameMagic = 0x45E0360E;
// --------------------------- Bit twiddling ------- {{{1
inline bool IsPowerOfTwo(uptr x) {
return (x & (x - 1)) == 0;
}
inline uptr RoundUpTo(uptr size, uptr boundary) {
CHECK(IsPowerOfTwo(boundary));
return (size + boundary - 1) & ~(boundary - 1);
}
// -------------------------- LowLevelAllocator ----- {{{1
// A simple low-level memory allocator for internal use.
class LowLevelAllocator {

View File

@ -154,10 +154,6 @@ void AsanDumpProcessMap() {
Report("End of process memory map.\n");
}
int GetPid() {
return getpid();
}
uptr GetThreadSelf() {
return (uptr)pthread_self();
}

View File

@ -239,10 +239,6 @@ void AsanDumpProcessMap() {
UNIMPLEMENTED();
}
int GetPid() {
return GetProcessId(GetCurrentProcess());
}
uptr GetThreadSelf() {
return GetCurrentThreadId();
}

View File

@ -0,0 +1,28 @@
//===-- sanitizer_common.cc -----------------------------------------------===//
//
// 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
// run-time libraries.
//===----------------------------------------------------------------------===//
#include "sanitizer_common.h"
#include "sanitizer_libc.h"
namespace __sanitizer {
void RawWrite(const char *buffer) {
static const char *kRawWriteError = "RawWrite can't output requested buffer!";
uptr length = (uptr)internal_strlen(buffer);
if (length != internal_write(2, buffer, length)) {
internal_write(2, kRawWriteError, internal_strlen(kRawWriteError));
Die();
}
}
} // namespace __sanitizer

View File

@ -9,7 +9,7 @@
//
// This file is shared between AddressSanitizer and ThreadSanitizer
// run-time libraries.
// It defines common functions and classes that are used in both runtimes.
// It declares common functions and classes that are used in both runtimes.
// Implementation of some functions are provided in sanitizer_common, while
// others must be defined by run-time library itself.
//===----------------------------------------------------------------------===//
@ -20,8 +20,30 @@
namespace __sanitizer {
// NOTE: Functions below must be defined in each run-time.
// NOTE: Functions below must be defined in each run-time. {{{
void NORETURN Die();
// }}}
// Constants.
const uptr kWordSize = __WORDSIZE / 8;
const uptr kWordSizeInBits = 8 * kWordSize;
const uptr kPageSizeBits = 12;
const uptr kPageSize = 1UL << kPageSizeBits;
int GetPid();
void RawWrite(const char *buffer);
void *MmapOrDie(uptr size);
void UnmapOrDie(void *addr, uptr size);
// Bit twiddling.
inline bool IsPowerOfTwo(uptr x) {
return (x & (x - 1)) == 0;
}
inline uptr RoundUpTo(uptr size, uptr boundary) {
// FIXME: Use CHECK here.
RAW_CHECK(IsPowerOfTwo(boundary));
return (size + boundary - 1) & ~(boundary - 1);
}
} // namespace __sanitizer

View File

@ -59,4 +59,14 @@ typedef unsigned long DWORD; // NOLINT
# endif
#endif // __WORDSIZE
// Raw check.
#define RAW_CHECK_MSG(expr, msg) do { \
if (!(expr)) { \
RawWrite(msg); \
Die(); \
} \
} while (0)
#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
#endif // SANITIZER_DEFS_H

View File

@ -38,6 +38,12 @@ int internal_strcmp(const char *s1, const char *s2) {
return 0;
}
uptr internal_strlen(const char *s) {
uptr i = 0;
while (s[i]) i++;
return i;
}
char *internal_strncpy(char *dst, const char *src, uptr n) {
uptr i;
for (i = 0; i < n && src[i]; i++)

View File

@ -26,6 +26,7 @@ void MiniLibcStub();
// String functions
void *internal_memchr(const void *s, int c, uptr n);
int internal_strcmp(const char *s1, const char *s2);
uptr internal_strlen(const char *s);
char *internal_strncpy(char *dst, const char *src, uptr n);
// Memory

View File

@ -13,14 +13,42 @@
//===----------------------------------------------------------------------===//
#if defined(__linux__) || defined(__APPLE__)
#include "sanitizer_internal_defs.h"
#include "sanitizer_common.h"
#include "sanitizer_libc.h"
#include <stdarg.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
namespace __sanitizer {
int GetPid() {
return getpid();
}
void *MmapOrDie(uptr size) {
size = RoundUpTo(size, kPageSize);
void *res = internal_mmap(0, size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
if (res == (void*)-1) {
RawWrite("Failed to map!\n");
Die();
}
return res;
}
void UnmapOrDie(void *addr, uptr size) {
if (!addr || !size) return;
int res = internal_munmap(addr, size);
if (res != 0) {
RawWrite("Failed to unmap!\n");
Die();
}
}
int internal_sscanf(const char *str, const char *format, ...) {
va_list args;
va_start(args, format);

View File

@ -23,6 +23,23 @@
namespace __sanitizer {
int GetPid() {
return GetProcessId(GetCurrentProcess());
}
void *MmapOrDie(uptr size) {
void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (rv == 0)
RawWrite("Failed to map!\n");
Die();
return rv;
}
void UnmapOrDie(void *addr, uptr size) {
// FIXME: Use CHECK here.
RAW_CHECK(VirtualFree(addr, size, MEM_DECOMMIT));
}
void *internal_mmap(void *addr, uptr length, int prot, int flags,
int fd, u64 offset) {
UNIMPLEMENTED_WIN();

View File

@ -23,7 +23,6 @@
namespace __tsan {
const uptr kPageSize = 4096;
const int kTidBits = 13;
const unsigned kMaxTid = 1 << kTidBits;
const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
@ -159,7 +158,6 @@ void internal_memcpy(void *dst, const void *src, uptr size);
int internal_memcmp(const void *s1, const void *s2, uptr size);
int internal_strncmp(const char *s1, const char *s2, uptr size);
void internal_strcpy(char *s1, const char *s2);
uptr internal_strlen(const char *s);
char* internal_strdup(const char *s);
const char *internal_strstr(const char *where, const char *what);
const char *internal_strchr(const char *where, char what);

View File

@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_libc.h"
#include "tsan_flags.h"
#include "tsan_rtl.h"
#include "tsan_mman.h"

View File

@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "interception/interception.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "tsan_rtl.h"
#include "tsan_interface.h"
#include "tsan_atomic.h"
@ -1540,10 +1541,6 @@ void internal_strcpy(char *s1, const char *s2) {
REAL(strcpy)(s1, s2); // NOLINT
}
uptr internal_strlen(const char *s) {
return REAL(strlen)(s);
}
char* internal_strdup(const char *s) {
uptr len = internal_strlen(s);
char *s2 = (char*)internal_alloc(MBlockString, len + 1);

View File

@ -69,7 +69,6 @@ void FlushShadowMemory();
const char *InitializePlatform();
void FinalizePlatform();
int GetPid();
void internal_yield();
void internal_sleep_ms(u32 ms);

View File

@ -273,8 +273,4 @@ void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
}
}
int GetPid() {
return getpid();
}
} // namespace __tsan

View File

@ -10,6 +10,7 @@
// This file is a part of ThreadSanitizer (TSan), a race detector.
//
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "tsan_symbolize.h"
#include "tsan_mman.h"
@ -84,7 +85,7 @@ static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
DlIteratePhdrCtx *ctx = (DlIteratePhdrCtx*)arg;
InternalScopedBuf<char> tmp(128);
if (ctx->is_first) {
Snprintf(tmp.Ptr(), tmp.Size(), "/proc/%d/exe", (int)getpid());
Snprintf(tmp.Ptr(), tmp.Size(), "/proc/%d/exe", GetPid());
info->dlpi_name = tmp.Ptr();
}
ctx->is_first = false;