2012-06-05 15:46:31 +08:00
|
|
|
//===-- sanitizer_win.cc --------------------------------------------------===//
|
2012-06-05 15:05:10 +08:00
|
|
|
//
|
|
|
|
// 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 and implements windows-specific functions from
|
|
|
|
// sanitizer_libc.h.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-03-19 22:33:38 +08:00
|
|
|
|
|
|
|
#include "sanitizer_platform.h"
|
|
|
|
#if SANITIZER_WINDOWS
|
|
|
|
|
2012-11-06 21:19:59 +08:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define NOGDI
|
2012-06-05 15:05:10 +08:00
|
|
|
#include <windows.h>
|
2014-07-11 19:57:41 +08:00
|
|
|
#include <dbghelp.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <stdlib.h>
|
2012-06-05 15:05:10 +08:00
|
|
|
|
2012-06-06 17:43:32 +08:00
|
|
|
#include "sanitizer_common.h"
|
2012-06-05 15:05:10 +08:00
|
|
|
#include "sanitizer_libc.h"
|
2013-01-14 22:28:06 +08:00
|
|
|
#include "sanitizer_mutex.h"
|
2013-05-08 20:45:55 +08:00
|
|
|
#include "sanitizer_placement_new.h"
|
|
|
|
#include "sanitizer_stacktrace.h"
|
2012-06-05 15:05:10 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
#include "sanitizer_syscall_generic.inc"
|
|
|
|
|
2012-06-07 15:13:46 +08:00
|
|
|
// --------------------- sanitizer_common.h
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr GetPageSize() {
|
|
|
|
return 1U << 14; // FIXME: is this configurable?
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr GetMmapGranularity() {
|
|
|
|
return 1U << 16; // FIXME: is this configurable?
|
|
|
|
}
|
|
|
|
|
2013-07-16 17:47:39 +08:00
|
|
|
uptr GetMaxVirtualAddress() {
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
GetSystemInfo(&si);
|
|
|
|
return (uptr)si.lpMaximumApplicationAddress;
|
|
|
|
}
|
|
|
|
|
2012-11-09 22:45:30 +08:00
|
|
|
bool FileExists(const char *filename) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2013-05-18 00:56:53 +08:00
|
|
|
uptr internal_getpid() {
|
2012-06-06 17:26:25 +08:00
|
|
|
return GetProcessId(GetCurrentProcess());
|
|
|
|
}
|
|
|
|
|
2013-03-26 06:04:29 +08:00
|
|
|
// In contrast to POSIX, on Windows GetCurrentThreadId()
|
|
|
|
// returns a system-unique identifier.
|
|
|
|
uptr GetTid() {
|
2012-06-15 14:37:34 +08:00
|
|
|
return GetCurrentThreadId();
|
|
|
|
}
|
|
|
|
|
2013-03-26 06:04:29 +08:00
|
|
|
uptr GetThreadSelf() {
|
|
|
|
return GetTid();
|
|
|
|
}
|
|
|
|
|
2014-09-01 19:44:59 +08:00
|
|
|
#if !SANITIZER_GO
|
2012-06-07 15:32:00 +08:00
|
|
|
void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
|
2012-06-07 15:13:46 +08:00
|
|
|
uptr *stack_bottom) {
|
|
|
|
CHECK(stack_top);
|
|
|
|
CHECK(stack_bottom);
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
2012-06-20 23:19:17 +08:00
|
|
|
CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
|
2012-06-07 15:13:46 +08:00
|
|
|
// FIXME: is it possible for the stack to not be a single allocation?
|
|
|
|
// Are these values what ASan expects to get (reserved, not committed;
|
|
|
|
// including stack guard page) ?
|
|
|
|
*stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
|
|
|
|
*stack_bottom = (uptr)mbi.AllocationBase;
|
|
|
|
}
|
2014-09-01 19:44:59 +08:00
|
|
|
#endif // #if !SANITIZER_GO
|
2012-06-07 15:13:46 +08:00
|
|
|
|
2012-06-07 00:15:07 +08:00
|
|
|
void *MmapOrDie(uptr size, const char *mem_type) {
|
2012-06-06 17:26:25 +08:00
|
|
|
void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
2012-06-07 00:15:07 +08:00
|
|
|
if (rv == 0) {
|
2014-03-19 16:23:00 +08:00
|
|
|
Report("ERROR: %s failed to "
|
|
|
|
"allocate 0x%zx (%zd) bytes of %s (error code: %d)\n",
|
|
|
|
SanitizerToolName, size, size, mem_type, GetLastError());
|
2012-06-07 00:15:07 +08:00
|
|
|
CHECK("unable to mmap" && 0);
|
|
|
|
}
|
2012-06-06 17:26:25 +08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnmapOrDie(void *addr, uptr size) {
|
2012-06-06 23:47:40 +08:00
|
|
|
if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
|
2014-03-19 16:23:00 +08:00
|
|
|
Report("ERROR: %s failed to "
|
|
|
|
"deallocate 0x%zx (%zd) bytes at address %p (error code: %d)\n",
|
|
|
|
SanitizerToolName, size, size, addr, GetLastError());
|
2012-06-07 00:15:07 +08:00
|
|
|
CHECK("unable to unmap" && 0);
|
2012-06-06 23:47:40 +08:00
|
|
|
}
|
2012-06-06 17:26:25 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 22:42:58 +08:00
|
|
|
void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
|
2012-12-13 13:36:00 +08:00
|
|
|
// FIXME: is this really "NoReserve"? On Win32 this does not matter much,
|
|
|
|
// but on Win64 it does.
|
2012-11-06 21:19:59 +08:00
|
|
|
void *p = VirtualAlloc((LPVOID)fixed_addr, size,
|
|
|
|
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
if (p == 0)
|
2014-03-19 16:23:00 +08:00
|
|
|
Report("ERROR: %s failed to "
|
|
|
|
"allocate %p (%zd) bytes at %p (error code: %d)\n",
|
|
|
|
SanitizerToolName, size, size, fixed_addr, GetLastError());
|
2012-11-06 21:19:59 +08:00
|
|
|
return p;
|
2012-06-14 22:42:58 +08:00
|
|
|
}
|
|
|
|
|
2012-12-13 13:36:00 +08:00
|
|
|
void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
|
2012-12-13 13:51:02 +08:00
|
|
|
return MmapFixedNoReserve(fixed_addr, size);
|
2012-12-13 13:36:00 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 23:03:49 +08:00
|
|
|
void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
|
|
|
|
// FIXME: make this really NoReserve?
|
|
|
|
return MmapOrDie(size, mem_type);
|
|
|
|
}
|
|
|
|
|
2012-06-14 22:42:58 +08:00
|
|
|
void *Mprotect(uptr fixed_addr, uptr size) {
|
|
|
|
return VirtualAlloc((LPVOID)fixed_addr, size,
|
|
|
|
MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
|
|
|
|
}
|
|
|
|
|
2013-02-08 20:02:00 +08:00
|
|
|
void FlushUnneededShadowMemory(uptr addr, uptr size) {
|
|
|
|
// This is almost useless on 32-bits.
|
|
|
|
// FIXME: add madvice-analog when we move to 64-bits.
|
|
|
|
}
|
|
|
|
|
2012-06-15 15:29:14 +08:00
|
|
|
bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
|
|
|
|
// FIXME: shall we do anything here on Windows?
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-03 16:24:14 +08:00
|
|
|
void *MapFileToMemory(const char *file_name, uptr *buff_size) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2014-05-28 16:26:24 +08:00
|
|
|
void *MapWritableFileToMemory(void *addr, uptr size, uptr fd, uptr offset) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2013-03-14 19:29:06 +08:00
|
|
|
static const int kMaxEnvNameLength = 128;
|
2013-06-10 18:02:02 +08:00
|
|
|
static const DWORD kMaxEnvValueLength = 32767;
|
2013-03-14 19:10:23 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct EnvVariable {
|
|
|
|
char name[kMaxEnvNameLength];
|
|
|
|
char value[kMaxEnvValueLength];
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static const int kEnvVariables = 5;
|
|
|
|
static EnvVariable env_vars[kEnvVariables];
|
|
|
|
static int num_env_vars;
|
|
|
|
|
2012-06-14 22:07:21 +08:00
|
|
|
const char *GetEnv(const char *name) {
|
2013-03-14 19:10:23 +08:00
|
|
|
// Note: this implementation caches the values of the environment variables
|
|
|
|
// and limits their quantity.
|
|
|
|
for (int i = 0; i < num_env_vars; i++) {
|
|
|
|
if (0 == internal_strcmp(name, env_vars[i].name))
|
|
|
|
return env_vars[i].value;
|
|
|
|
}
|
|
|
|
CHECK_LT(num_env_vars, kEnvVariables);
|
|
|
|
DWORD rv = GetEnvironmentVariableA(name, env_vars[num_env_vars].value,
|
|
|
|
kMaxEnvValueLength);
|
|
|
|
if (rv > 0 && rv < kMaxEnvValueLength) {
|
|
|
|
CHECK_LT(internal_strlen(name), kMaxEnvNameLength);
|
|
|
|
internal_strncpy(env_vars[num_env_vars].name, name, kMaxEnvNameLength);
|
|
|
|
num_env_vars++;
|
|
|
|
return env_vars[num_env_vars - 1].value;
|
|
|
|
}
|
2012-06-14 22:07:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-18 16:44:30 +08:00
|
|
|
const char *GetPwd() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2013-02-18 15:17:12 +08:00
|
|
|
u32 GetUid() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2012-06-15 14:08:19 +08:00
|
|
|
void DumpProcessMap() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2014-08-13 06:31:19 +08:00
|
|
|
void DisableCoreDumperIfNecessary() {
|
2014-05-06 16:21:50 +08:00
|
|
|
// Do nothing.
|
2012-06-15 14:08:19 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 17:12:39 +08:00
|
|
|
void ReExec() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2014-05-19 20:53:03 +08:00
|
|
|
void PrepareForSandboxing(__sanitizer_sandbox_arguments *args) {
|
|
|
|
(void)args;
|
2012-12-10 21:10:40 +08:00
|
|
|
// Nothing here for now.
|
|
|
|
}
|
|
|
|
|
2012-09-17 17:12:39 +08:00
|
|
|
bool StackSizeIsUnlimited() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetStackSizeLimitInBytes(uptr limit) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2014-08-13 06:31:19 +08:00
|
|
|
bool AddressSpaceIsUnlimited() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAddressSpaceUnlimited() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2013-09-03 21:20:48 +08:00
|
|
|
char *FindPathToBinary(const char *name) {
|
2013-09-03 23:50:13 +08:00
|
|
|
// Nothing here for now.
|
|
|
|
return 0;
|
2013-09-03 21:20:48 +08:00
|
|
|
}
|
|
|
|
|
2012-06-15 14:37:34 +08:00
|
|
|
void SleepForSeconds(int seconds) {
|
|
|
|
Sleep(seconds * 1000);
|
|
|
|
}
|
|
|
|
|
2012-06-18 16:44:30 +08:00
|
|
|
void SleepForMillis(int millis) {
|
|
|
|
Sleep(millis);
|
|
|
|
}
|
|
|
|
|
2013-06-10 18:02:02 +08:00
|
|
|
u64 NanoTime() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-15 14:37:34 +08:00
|
|
|
void Abort() {
|
|
|
|
abort();
|
2013-11-26 17:40:39 +08:00
|
|
|
internal__exit(-1); // abort is not NORETURN on Windows.
|
2012-06-15 14:37:34 +08:00
|
|
|
}
|
|
|
|
|
2013-09-10 22:36:16 +08:00
|
|
|
uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
|
|
|
|
string_predicate_t filter) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
};
|
|
|
|
|
2012-11-06 21:19:59 +08:00
|
|
|
#ifndef SANITIZER_GO
|
2012-06-15 14:37:34 +08:00
|
|
|
int Atexit(void (*function)(void)) {
|
|
|
|
return atexit(function);
|
|
|
|
}
|
2012-11-06 21:19:59 +08:00
|
|
|
#endif
|
2012-06-15 14:37:34 +08:00
|
|
|
|
2012-06-07 15:13:46 +08:00
|
|
|
// ------------------ sanitizer_libc.h
|
2013-05-09 02:15:01 +08:00
|
|
|
uptr internal_mmap(void *addr, uptr length, int prot, int flags,
|
|
|
|
int fd, u64 offset) {
|
2012-06-06 23:47:40 +08:00
|
|
|
UNIMPLEMENTED();
|
2012-06-05 15:05:10 +08:00
|
|
|
}
|
|
|
|
|
2013-05-09 02:15:01 +08:00
|
|
|
uptr internal_munmap(void *addr, uptr length) {
|
2012-06-06 23:47:40 +08:00
|
|
|
UNIMPLEMENTED();
|
2012-06-05 17:49:25 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_close(fd_t fd) {
|
2012-06-06 23:47:40 +08:00
|
|
|
UNIMPLEMENTED();
|
2012-06-05 16:32:53 +08:00
|
|
|
}
|
|
|
|
|
2012-11-02 23:18:34 +08:00
|
|
|
int internal_isatty(fd_t fd) {
|
2012-12-19 15:57:42 +08:00
|
|
|
return _isatty(fd);
|
2012-11-02 23:18:34 +08:00
|
|
|
}
|
|
|
|
|
2013-05-09 02:15:01 +08:00
|
|
|
uptr internal_open(const char *filename, int flags) {
|
2013-02-01 23:58:46 +08:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2013-05-09 02:15:01 +08:00
|
|
|
uptr internal_open(const char *filename, int flags, u32 mode) {
|
2013-02-01 23:58:46 +08:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr OpenFile(const char *filename, bool write) {
|
2012-06-06 23:47:40 +08:00
|
|
|
UNIMPLEMENTED();
|
2012-06-05 15:05:10 +08:00
|
|
|
}
|
|
|
|
|
2012-06-05 16:32:53 +08:00
|
|
|
uptr internal_read(fd_t fd, void *buf, uptr count) {
|
2012-06-06 23:47:40 +08:00
|
|
|
UNIMPLEMENTED();
|
2012-06-05 16:32:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uptr internal_write(fd_t fd, const void *buf, uptr count) {
|
2012-11-02 17:38:47 +08:00
|
|
|
if (fd != kStderrFd)
|
2012-06-06 23:47:40 +08:00
|
|
|
UNIMPLEMENTED();
|
2014-02-05 07:28:30 +08:00
|
|
|
|
|
|
|
static HANDLE output_stream = 0;
|
|
|
|
// Abort immediately if we know printing is not possible.
|
|
|
|
if (output_stream == INVALID_HANDLE_VALUE)
|
2012-06-05 16:32:53 +08:00
|
|
|
return 0;
|
2014-02-05 07:28:30 +08:00
|
|
|
|
|
|
|
// If called for the first time, try to use stderr to output stuff,
|
|
|
|
// falling back to stdout if anything goes wrong.
|
|
|
|
bool fallback_to_stdout = false;
|
|
|
|
if (output_stream == 0) {
|
|
|
|
output_stream = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
// We don't distinguish "no such handle" from error.
|
|
|
|
if (output_stream == 0)
|
|
|
|
output_stream = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
if (output_stream == INVALID_HANDLE_VALUE) {
|
|
|
|
// Retry with stdout?
|
|
|
|
output_stream = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if (output_stream == 0)
|
|
|
|
output_stream = INVALID_HANDLE_VALUE;
|
|
|
|
if (output_stream == INVALID_HANDLE_VALUE)
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
// Successfully got an stderr handle. However, if WriteFile() fails,
|
|
|
|
// we can still try to fallback to stdout.
|
|
|
|
fallback_to_stdout = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD ret;
|
|
|
|
if (WriteFile(output_stream, buf, count, &ret, 0))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
// Re-try with stdout if using a valid stderr handle fails.
|
|
|
|
if (fallback_to_stdout) {
|
|
|
|
output_stream = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if (output_stream == 0)
|
|
|
|
output_stream = INVALID_HANDLE_VALUE;
|
|
|
|
if (output_stream != INVALID_HANDLE_VALUE)
|
|
|
|
return internal_write(fd, buf, count);
|
|
|
|
}
|
|
|
|
return 0;
|
2012-06-05 16:32:53 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_stat(const char *path, void *buf) {
|
2013-02-04 18:16:50 +08:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_lstat(const char *path, void *buf) {
|
2013-02-04 18:16:50 +08:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_fstat(fd_t fd, void *buf) {
|
2013-02-04 18:16:50 +08:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2012-06-06 15:30:33 +08:00
|
|
|
uptr internal_filesize(fd_t fd) {
|
2012-06-06 23:47:40 +08:00
|
|
|
UNIMPLEMENTED();
|
2012-06-06 15:30:33 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_dup2(int oldfd, int newfd) {
|
2012-06-06 23:47:40 +08:00
|
|
|
UNIMPLEMENTED();
|
2012-06-06 15:30:33 +08:00
|
|
|
}
|
|
|
|
|
2012-09-05 22:48:24 +08:00
|
|
|
uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_sched_yield() {
|
2012-11-06 21:19:59 +08:00
|
|
|
Sleep(0);
|
|
|
|
return 0;
|
2012-06-18 16:44:30 +08:00
|
|
|
}
|
|
|
|
|
2013-02-20 21:54:32 +08:00
|
|
|
void internal__exit(int exitcode) {
|
2013-11-26 17:40:39 +08:00
|
|
|
ExitProcess(exitcode);
|
2013-02-20 21:54:32 +08:00
|
|
|
}
|
|
|
|
|
2014-05-28 16:26:24 +08:00
|
|
|
uptr internal_ftruncate(fd_t fd, uptr size) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr internal_rename(const char *oldpath, const char *newpath) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2014-12-09 09:22:59 +08:00
|
|
|
uptr GetRSS() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-17 03:13:01 +08:00
|
|
|
void *internal_start_thread(void (*func)(void *arg), void *arg) { return 0; }
|
2014-12-17 04:46:05 +08:00
|
|
|
void internal_join_thread(void *th) { }
|
2014-12-17 03:13:01 +08:00
|
|
|
|
2013-01-14 15:51:39 +08:00
|
|
|
// ---------------------- BlockingMutex ---------------- {{{1
|
2013-02-04 18:42:38 +08:00
|
|
|
const uptr LOCK_UNINITIALIZED = 0;
|
|
|
|
const uptr LOCK_READY = (uptr)-1;
|
2013-01-14 15:51:39 +08:00
|
|
|
|
|
|
|
BlockingMutex::BlockingMutex(LinkerInitialized li) {
|
|
|
|
// FIXME: see comments in BlockingMutex::Lock() for the details.
|
|
|
|
CHECK(li == LINKER_INITIALIZED || owner_ == LOCK_UNINITIALIZED);
|
|
|
|
|
|
|
|
CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
|
|
|
|
InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
|
2013-03-14 21:30:56 +08:00
|
|
|
owner_ = LOCK_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockingMutex::BlockingMutex() {
|
|
|
|
CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
|
|
|
|
InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
|
2013-01-14 15:51:39 +08:00
|
|
|
owner_ = LOCK_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingMutex::Lock() {
|
|
|
|
if (owner_ == LOCK_UNINITIALIZED) {
|
|
|
|
// FIXME: hm, global BlockingMutex objects are not initialized?!?
|
|
|
|
// This might be a side effect of the clang+cl+link Frankenbuild...
|
|
|
|
new(this) BlockingMutex((LinkerInitialized)(LINKER_INITIALIZED + 1));
|
|
|
|
|
|
|
|
// FIXME: If it turns out the linker doesn't invoke our
|
|
|
|
// constructors, we should probably manually Lock/Unlock all the global
|
|
|
|
// locks while we're starting in one thread to avoid double-init races.
|
|
|
|
}
|
|
|
|
EnterCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
|
2013-02-04 16:07:45 +08:00
|
|
|
CHECK_EQ(owner_, LOCK_READY);
|
2013-01-14 15:51:39 +08:00
|
|
|
owner_ = GetThreadSelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingMutex::Unlock() {
|
2013-02-04 16:07:45 +08:00
|
|
|
CHECK_EQ(owner_, GetThreadSelf());
|
2013-01-14 15:51:39 +08:00
|
|
|
owner_ = LOCK_READY;
|
|
|
|
LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
|
|
|
|
}
|
|
|
|
|
2013-03-11 23:45:20 +08:00
|
|
|
void BlockingMutex::CheckLocked() {
|
|
|
|
CHECK_EQ(owner_, GetThreadSelf());
|
|
|
|
}
|
|
|
|
|
2013-03-13 16:19:53 +08:00
|
|
|
uptr GetTlsSize() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitTlsSize() {
|
|
|
|
}
|
|
|
|
|
2013-05-07 22:41:43 +08:00
|
|
|
void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
|
|
|
|
uptr *tls_addr, uptr *tls_size) {
|
2013-06-10 18:30:54 +08:00
|
|
|
#ifdef SANITIZER_GO
|
|
|
|
*stk_addr = 0;
|
|
|
|
*stk_size = 0;
|
|
|
|
*tls_addr = 0;
|
|
|
|
*tls_size = 0;
|
|
|
|
#else
|
2013-05-07 22:41:43 +08:00
|
|
|
uptr stack_top, stack_bottom;
|
|
|
|
GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
|
|
|
|
*stk_addr = stack_bottom;
|
|
|
|
*stk_size = stack_top - stack_bottom;
|
|
|
|
*tls_addr = 0;
|
|
|
|
*tls_size = 0;
|
2013-06-10 18:30:54 +08:00
|
|
|
#endif
|
2013-05-07 22:41:43 +08:00
|
|
|
}
|
|
|
|
|
2014-09-01 19:44:59 +08:00
|
|
|
#if !SANITIZER_GO
|
2014-10-26 11:35:14 +08:00
|
|
|
void BufferedStackTrace::SlowUnwindStack(uptr pc, uptr max_depth) {
|
2014-03-04 20:21:28 +08:00
|
|
|
CHECK_GE(max_depth, 2);
|
2013-05-08 20:45:55 +08:00
|
|
|
// FIXME: CaptureStackBackTrace might be too slow for us.
|
|
|
|
// FIXME: Compare with StackWalk64.
|
|
|
|
// FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc
|
2013-11-09 21:59:12 +08:00
|
|
|
size = CaptureStackBackTrace(2, Min(max_depth, kStackTraceMax),
|
|
|
|
(void**)trace, 0);
|
2013-12-10 16:30:39 +08:00
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
2013-05-08 20:45:55 +08:00
|
|
|
// Skip the RTL frames by searching for the PC in the stacktrace.
|
2013-11-09 21:59:12 +08:00
|
|
|
uptr pc_location = LocatePcInTrace(pc);
|
|
|
|
PopStackFrames(pc_location);
|
2013-05-08 20:45:55 +08:00
|
|
|
}
|
2014-02-11 21:57:17 +08:00
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
void BufferedStackTrace::SlowUnwindStackWithContext(uptr pc, void *context,
|
|
|
|
uptr max_depth) {
|
2014-07-11 19:57:41 +08:00
|
|
|
CONTEXT ctx = *(CONTEXT *)context;
|
|
|
|
STACKFRAME64 stack_frame;
|
|
|
|
memset(&stack_frame, 0, sizeof(stack_frame));
|
|
|
|
size = 0;
|
|
|
|
#if defined(_WIN64)
|
|
|
|
int machine_type = IMAGE_FILE_MACHINE_AMD64;
|
|
|
|
stack_frame.AddrPC.Offset = ctx.Rip;
|
|
|
|
stack_frame.AddrFrame.Offset = ctx.Rbp;
|
|
|
|
stack_frame.AddrStack.Offset = ctx.Rsp;
|
|
|
|
#else
|
|
|
|
int machine_type = IMAGE_FILE_MACHINE_I386;
|
|
|
|
stack_frame.AddrPC.Offset = ctx.Eip;
|
|
|
|
stack_frame.AddrFrame.Offset = ctx.Ebp;
|
|
|
|
stack_frame.AddrStack.Offset = ctx.Esp;
|
|
|
|
#endif
|
|
|
|
stack_frame.AddrPC.Mode = AddrModeFlat;
|
|
|
|
stack_frame.AddrFrame.Mode = AddrModeFlat;
|
|
|
|
stack_frame.AddrStack.Mode = AddrModeFlat;
|
|
|
|
while (StackWalk64(machine_type, GetCurrentProcess(), GetCurrentThread(),
|
|
|
|
&stack_frame, &ctx, NULL, &SymFunctionTableAccess64,
|
|
|
|
&SymGetModuleBase64, NULL) &&
|
|
|
|
size < Min(max_depth, kStackTraceMax)) {
|
2014-10-27 03:27:02 +08:00
|
|
|
trace_buffer[size++] = (uptr)stack_frame.AddrPC.Offset;
|
2014-07-11 19:57:41 +08:00
|
|
|
}
|
2014-02-11 21:57:17 +08:00
|
|
|
}
|
2014-09-01 19:44:59 +08:00
|
|
|
#endif // #if !SANITIZER_GO
|
2013-05-08 20:45:55 +08:00
|
|
|
|
2014-12-12 02:30:25 +08:00
|
|
|
void ReportFile::Write(const char *buffer, uptr length) {
|
|
|
|
SpinMutexLock l(mu);
|
|
|
|
ReopenIfNecessary();
|
|
|
|
if (length != internal_write(fd, buffer, length)) {
|
2013-09-05 11:19:57 +08:00
|
|
|
// stderr may be closed, but we may be able to print to the debugger
|
|
|
|
// instead. This is the case when launching a program from Visual Studio,
|
|
|
|
// and the following routine should write to its console.
|
|
|
|
OutputDebugStringA(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 19:12:29 +08:00
|
|
|
void SetAlternateSignalStack() {
|
|
|
|
// FIXME: Decide what to do on Windows.
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnsetAlternateSignalStack() {
|
|
|
|
// FIXME: Decide what to do on Windows.
|
|
|
|
}
|
|
|
|
|
2014-01-31 23:11:11 +08:00
|
|
|
void InstallDeadlySignalHandlers(SignalHandlerType handler) {
|
|
|
|
(void)handler;
|
2014-01-31 21:10:07 +08:00
|
|
|
// FIXME: Decide what to do on Windows.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDeadlySignal(int signum) {
|
|
|
|
// FIXME: Decide what to do on Windows.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-18 01:56:15 +08:00
|
|
|
bool IsAccessibleMemoryRange(uptr beg, uptr size) {
|
|
|
|
// FIXME: Actually implement this function.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-05 15:05:10 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // _WIN32
|