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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifdef _WIN32
|
2012-11-06 21:19:59 +08:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define NOGDI
|
2012-11-08 21:08:41 +08:00
|
|
|
#include <stdlib.h>
|
2012-12-19 16:51:39 +08:00
|
|
|
#include <io.h>
|
2012-06-05 15:05:10 +08:00
|
|
|
#include <windows.h>
|
|
|
|
|
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 15:51:39 +08:00
|
|
|
#include "sanitizer_placement_new.h"
|
2013-01-14 22:28:06 +08:00
|
|
|
#include "sanitizer_mutex.h"
|
2012-06-05 15:05:10 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
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?
|
|
|
|
}
|
|
|
|
|
2012-11-09 22:45:30 +08:00
|
|
|
bool FileExists(const char *filename) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2012-06-06 17:26:25 +08:00
|
|
|
int GetPid() {
|
|
|
|
return GetProcessId(GetCurrentProcess());
|
|
|
|
}
|
|
|
|
|
2012-06-15 14:37:34 +08:00
|
|
|
uptr GetThreadSelf() {
|
|
|
|
return GetCurrentThreadId();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
|
|
|
|
size, size, mem_type);
|
|
|
|
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) {
|
2012-06-07 00:15:07 +08:00
|
|
|
Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
|
|
|
|
size, size, addr);
|
|
|
|
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)
|
|
|
|
Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at %p (%d)\n",
|
|
|
|
size, size, fixed_addr, GetLastError());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2012-06-14 22:07:21 +08:00
|
|
|
const char *GetEnv(const char *name) {
|
|
|
|
static char env_buffer[32767] = {};
|
|
|
|
|
|
|
|
// Note: this implementation stores the result in a static buffer so we only
|
|
|
|
// allow it to be called just once.
|
|
|
|
static bool called_once = false;
|
|
|
|
if (called_once)
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
called_once = true;
|
|
|
|
|
|
|
|
DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer));
|
|
|
|
if (rv > 0 && rv < sizeof(env_buffer))
|
|
|
|
return env_buffer;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-18 16:44:30 +08:00
|
|
|
const char *GetPwd() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2012-06-15 14:08:19 +08:00
|
|
|
void DumpProcessMap() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisableCoreDumper() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2012-09-17 17:12:39 +08:00
|
|
|
void ReExec() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2012-12-10 21:10:40 +08:00
|
|
|
void PrepareForSandboxing() {
|
|
|
|
// Nothing here for now.
|
|
|
|
}
|
|
|
|
|
2012-09-17 17:12:39 +08:00
|
|
|
bool StackSizeIsUnlimited() {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetStackSizeLimitInBytes(uptr limit) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-06-15 14:37:34 +08:00
|
|
|
void Exit(int exitcode) {
|
|
|
|
_exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Abort() {
|
|
|
|
abort();
|
|
|
|
_exit(-1); // abort is not NORETURN on Windows.
|
|
|
|
}
|
|
|
|
|
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
|
2012-06-05 15:05:10 +08:00
|
|
|
void *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
|
|
|
}
|
|
|
|
|
2012-06-05 17:49:25 +08:00
|
|
|
int internal_munmap(void *addr, uptr length) {
|
2012-06-06 23:47:40 +08:00
|
|
|
UNIMPLEMENTED();
|
2012-06-05 17:49:25 +08:00
|
|
|
}
|
|
|
|
|
2012-06-05 16:32:53 +08:00
|
|
|
int 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-02-01 23:58:46 +08:00
|
|
|
fd_t internal_open(const char *filename, int flags) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2013-02-02 00:32:18 +08:00
|
|
|
fd_t internal_open(const char *filename, int flags, u32 mode) {
|
2013-02-01 23:58:46 +08:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
fd_t 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();
|
2012-06-05 16:32:53 +08:00
|
|
|
HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
if (err == 0)
|
|
|
|
return 0; // FIXME: this might not work on some apps.
|
|
|
|
DWORD ret;
|
|
|
|
if (!WriteFile(err, buf, count, &ret, 0))
|
|
|
|
return 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-04 18:16:50 +08:00
|
|
|
int internal_stat(const char *path, void *buf) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
int internal_lstat(const char *path, void *buf) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
int internal_fstat(fd_t fd, void *buf) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int 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();
|
|
|
|
}
|
|
|
|
|
2012-06-18 16:44:30 +08:00
|
|
|
int internal_sched_yield() {
|
2012-11-06 21:19:59 +08:00
|
|
|
Sleep(0);
|
|
|
|
return 0;
|
2012-06-18 16:44:30 +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_);
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
|
2012-06-05 15:05:10 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // _WIN32
|