1*7c3d14c8STreehugger Robot //===-- sanitizer_win.cc --------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot // The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is shared between AddressSanitizer and ThreadSanitizer
11*7c3d14c8STreehugger Robot // run-time libraries and implements windows-specific functions from
12*7c3d14c8STreehugger Robot // sanitizer_libc.h.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot
15*7c3d14c8STreehugger Robot #include "sanitizer_platform.h"
16*7c3d14c8STreehugger Robot #if SANITIZER_WINDOWS
17*7c3d14c8STreehugger Robot
18*7c3d14c8STreehugger Robot #define WIN32_LEAN_AND_MEAN
19*7c3d14c8STreehugger Robot #define NOGDI
20*7c3d14c8STreehugger Robot #include <windows.h>
21*7c3d14c8STreehugger Robot #include <dbghelp.h>
22*7c3d14c8STreehugger Robot #include <io.h>
23*7c3d14c8STreehugger Robot #include <psapi.h>
24*7c3d14c8STreehugger Robot #include <stdlib.h>
25*7c3d14c8STreehugger Robot
26*7c3d14c8STreehugger Robot #include "sanitizer_common.h"
27*7c3d14c8STreehugger Robot #include "sanitizer_libc.h"
28*7c3d14c8STreehugger Robot #include "sanitizer_mutex.h"
29*7c3d14c8STreehugger Robot #include "sanitizer_placement_new.h"
30*7c3d14c8STreehugger Robot #include "sanitizer_stacktrace.h"
31*7c3d14c8STreehugger Robot
32*7c3d14c8STreehugger Robot namespace __sanitizer {
33*7c3d14c8STreehugger Robot
34*7c3d14c8STreehugger Robot #include "sanitizer_syscall_generic.inc"
35*7c3d14c8STreehugger Robot
36*7c3d14c8STreehugger Robot // --------------------- sanitizer_common.h
GetPageSize()37*7c3d14c8STreehugger Robot uptr GetPageSize() {
38*7c3d14c8STreehugger Robot SYSTEM_INFO si;
39*7c3d14c8STreehugger Robot GetSystemInfo(&si);
40*7c3d14c8STreehugger Robot return si.dwPageSize;
41*7c3d14c8STreehugger Robot }
42*7c3d14c8STreehugger Robot
GetMmapGranularity()43*7c3d14c8STreehugger Robot uptr GetMmapGranularity() {
44*7c3d14c8STreehugger Robot SYSTEM_INFO si;
45*7c3d14c8STreehugger Robot GetSystemInfo(&si);
46*7c3d14c8STreehugger Robot return si.dwAllocationGranularity;
47*7c3d14c8STreehugger Robot }
48*7c3d14c8STreehugger Robot
GetMaxVirtualAddress()49*7c3d14c8STreehugger Robot uptr GetMaxVirtualAddress() {
50*7c3d14c8STreehugger Robot SYSTEM_INFO si;
51*7c3d14c8STreehugger Robot GetSystemInfo(&si);
52*7c3d14c8STreehugger Robot return (uptr)si.lpMaximumApplicationAddress;
53*7c3d14c8STreehugger Robot }
54*7c3d14c8STreehugger Robot
FileExists(const char * filename)55*7c3d14c8STreehugger Robot bool FileExists(const char *filename) {
56*7c3d14c8STreehugger Robot return ::GetFileAttributesA(filename) != INVALID_FILE_ATTRIBUTES;
57*7c3d14c8STreehugger Robot }
58*7c3d14c8STreehugger Robot
internal_getpid()59*7c3d14c8STreehugger Robot uptr internal_getpid() {
60*7c3d14c8STreehugger Robot return GetProcessId(GetCurrentProcess());
61*7c3d14c8STreehugger Robot }
62*7c3d14c8STreehugger Robot
63*7c3d14c8STreehugger Robot // In contrast to POSIX, on Windows GetCurrentThreadId()
64*7c3d14c8STreehugger Robot // returns a system-unique identifier.
GetTid()65*7c3d14c8STreehugger Robot uptr GetTid() {
66*7c3d14c8STreehugger Robot return GetCurrentThreadId();
67*7c3d14c8STreehugger Robot }
68*7c3d14c8STreehugger Robot
GetThreadSelf()69*7c3d14c8STreehugger Robot uptr GetThreadSelf() {
70*7c3d14c8STreehugger Robot return GetTid();
71*7c3d14c8STreehugger Robot }
72*7c3d14c8STreehugger Robot
73*7c3d14c8STreehugger Robot #if !SANITIZER_GO
GetThreadStackTopAndBottom(bool at_initialization,uptr * stack_top,uptr * stack_bottom)74*7c3d14c8STreehugger Robot void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
75*7c3d14c8STreehugger Robot uptr *stack_bottom) {
76*7c3d14c8STreehugger Robot CHECK(stack_top);
77*7c3d14c8STreehugger Robot CHECK(stack_bottom);
78*7c3d14c8STreehugger Robot MEMORY_BASIC_INFORMATION mbi;
79*7c3d14c8STreehugger Robot CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
80*7c3d14c8STreehugger Robot // FIXME: is it possible for the stack to not be a single allocation?
81*7c3d14c8STreehugger Robot // Are these values what ASan expects to get (reserved, not committed;
82*7c3d14c8STreehugger Robot // including stack guard page) ?
83*7c3d14c8STreehugger Robot *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
84*7c3d14c8STreehugger Robot *stack_bottom = (uptr)mbi.AllocationBase;
85*7c3d14c8STreehugger Robot }
86*7c3d14c8STreehugger Robot #endif // #if !SANITIZER_GO
87*7c3d14c8STreehugger Robot
MmapOrDie(uptr size,const char * mem_type,bool raw_report)88*7c3d14c8STreehugger Robot void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {
89*7c3d14c8STreehugger Robot void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
90*7c3d14c8STreehugger Robot if (rv == 0)
91*7c3d14c8STreehugger Robot ReportMmapFailureAndDie(size, mem_type, "allocate",
92*7c3d14c8STreehugger Robot GetLastError(), raw_report);
93*7c3d14c8STreehugger Robot return rv;
94*7c3d14c8STreehugger Robot }
95*7c3d14c8STreehugger Robot
UnmapOrDie(void * addr,uptr size)96*7c3d14c8STreehugger Robot void UnmapOrDie(void *addr, uptr size) {
97*7c3d14c8STreehugger Robot if (!size || !addr)
98*7c3d14c8STreehugger Robot return;
99*7c3d14c8STreehugger Robot
100*7c3d14c8STreehugger Robot MEMORY_BASIC_INFORMATION mbi;
101*7c3d14c8STreehugger Robot CHECK(VirtualQuery(addr, &mbi, sizeof(mbi)));
102*7c3d14c8STreehugger Robot
103*7c3d14c8STreehugger Robot // MEM_RELEASE can only be used to unmap whole regions previously mapped with
104*7c3d14c8STreehugger Robot // VirtualAlloc. So we first try MEM_RELEASE since it is better, and if that
105*7c3d14c8STreehugger Robot // fails try MEM_DECOMMIT.
106*7c3d14c8STreehugger Robot if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
107*7c3d14c8STreehugger Robot if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
108*7c3d14c8STreehugger Robot Report("ERROR: %s failed to "
109*7c3d14c8STreehugger Robot "deallocate 0x%zx (%zd) bytes at address %p (error code: %d)\n",
110*7c3d14c8STreehugger Robot SanitizerToolName, size, size, addr, GetLastError());
111*7c3d14c8STreehugger Robot CHECK("unable to unmap" && 0);
112*7c3d14c8STreehugger Robot }
113*7c3d14c8STreehugger Robot }
114*7c3d14c8STreehugger Robot }
115*7c3d14c8STreehugger Robot
116*7c3d14c8STreehugger Robot // We want to map a chunk of address space aligned to 'alignment'.
MmapAlignedOrDie(uptr size,uptr alignment,const char * mem_type)117*7c3d14c8STreehugger Robot void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type) {
118*7c3d14c8STreehugger Robot CHECK(IsPowerOfTwo(size));
119*7c3d14c8STreehugger Robot CHECK(IsPowerOfTwo(alignment));
120*7c3d14c8STreehugger Robot
121*7c3d14c8STreehugger Robot // Windows will align our allocations to at least 64K.
122*7c3d14c8STreehugger Robot alignment = Max(alignment, GetMmapGranularity());
123*7c3d14c8STreehugger Robot
124*7c3d14c8STreehugger Robot uptr mapped_addr =
125*7c3d14c8STreehugger Robot (uptr)VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
126*7c3d14c8STreehugger Robot if (!mapped_addr)
127*7c3d14c8STreehugger Robot ReportMmapFailureAndDie(size, mem_type, "allocate aligned", GetLastError());
128*7c3d14c8STreehugger Robot
129*7c3d14c8STreehugger Robot // If we got it right on the first try, return. Otherwise, unmap it and go to
130*7c3d14c8STreehugger Robot // the slow path.
131*7c3d14c8STreehugger Robot if (IsAligned(mapped_addr, alignment))
132*7c3d14c8STreehugger Robot return (void*)mapped_addr;
133*7c3d14c8STreehugger Robot if (VirtualFree((void *)mapped_addr, 0, MEM_RELEASE) == 0)
134*7c3d14c8STreehugger Robot ReportMmapFailureAndDie(size, mem_type, "deallocate", GetLastError());
135*7c3d14c8STreehugger Robot
136*7c3d14c8STreehugger Robot // If we didn't get an aligned address, overallocate, find an aligned address,
137*7c3d14c8STreehugger Robot // unmap, and try to allocate at that aligned address.
138*7c3d14c8STreehugger Robot int retries = 0;
139*7c3d14c8STreehugger Robot const int kMaxRetries = 10;
140*7c3d14c8STreehugger Robot for (; retries < kMaxRetries &&
141*7c3d14c8STreehugger Robot (mapped_addr == 0 || !IsAligned(mapped_addr, alignment));
142*7c3d14c8STreehugger Robot retries++) {
143*7c3d14c8STreehugger Robot // Overallocate size + alignment bytes.
144*7c3d14c8STreehugger Robot mapped_addr =
145*7c3d14c8STreehugger Robot (uptr)VirtualAlloc(0, size + alignment, MEM_RESERVE, PAGE_NOACCESS);
146*7c3d14c8STreehugger Robot if (!mapped_addr)
147*7c3d14c8STreehugger Robot ReportMmapFailureAndDie(size, mem_type, "allocate aligned",
148*7c3d14c8STreehugger Robot GetLastError());
149*7c3d14c8STreehugger Robot
150*7c3d14c8STreehugger Robot // Find the aligned address.
151*7c3d14c8STreehugger Robot uptr aligned_addr = RoundUpTo(mapped_addr, alignment);
152*7c3d14c8STreehugger Robot
153*7c3d14c8STreehugger Robot // Free the overallocation.
154*7c3d14c8STreehugger Robot if (VirtualFree((void *)mapped_addr, 0, MEM_RELEASE) == 0)
155*7c3d14c8STreehugger Robot ReportMmapFailureAndDie(size, mem_type, "deallocate", GetLastError());
156*7c3d14c8STreehugger Robot
157*7c3d14c8STreehugger Robot // Attempt to allocate exactly the number of bytes we need at the aligned
158*7c3d14c8STreehugger Robot // address. This may fail for a number of reasons, in which case we continue
159*7c3d14c8STreehugger Robot // the loop.
160*7c3d14c8STreehugger Robot mapped_addr = (uptr)VirtualAlloc((void *)aligned_addr, size,
161*7c3d14c8STreehugger Robot MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
162*7c3d14c8STreehugger Robot }
163*7c3d14c8STreehugger Robot
164*7c3d14c8STreehugger Robot // Fail if we can't make this work quickly.
165*7c3d14c8STreehugger Robot if (retries == kMaxRetries && mapped_addr == 0)
166*7c3d14c8STreehugger Robot ReportMmapFailureAndDie(size, mem_type, "allocate aligned", GetLastError());
167*7c3d14c8STreehugger Robot
168*7c3d14c8STreehugger Robot return (void *)mapped_addr;
169*7c3d14c8STreehugger Robot }
170*7c3d14c8STreehugger Robot
MmapFixedNoReserve(uptr fixed_addr,uptr size,const char * name)171*7c3d14c8STreehugger Robot void *MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) {
172*7c3d14c8STreehugger Robot // FIXME: is this really "NoReserve"? On Win32 this does not matter much,
173*7c3d14c8STreehugger Robot // but on Win64 it does.
174*7c3d14c8STreehugger Robot (void)name; // unsupported
175*7c3d14c8STreehugger Robot #if SANITIZER_WINDOWS64
176*7c3d14c8STreehugger Robot // On Windows64, use MEM_COMMIT would result in error
177*7c3d14c8STreehugger Robot // 1455:ERROR_COMMITMENT_LIMIT.
178*7c3d14c8STreehugger Robot // We use exception handler to commit page on demand.
179*7c3d14c8STreehugger Robot void *p = VirtualAlloc((LPVOID)fixed_addr, size, MEM_RESERVE, PAGE_READWRITE);
180*7c3d14c8STreehugger Robot #else
181*7c3d14c8STreehugger Robot void *p = VirtualAlloc((LPVOID)fixed_addr, size, MEM_RESERVE | MEM_COMMIT,
182*7c3d14c8STreehugger Robot PAGE_READWRITE);
183*7c3d14c8STreehugger Robot #endif
184*7c3d14c8STreehugger Robot if (p == 0)
185*7c3d14c8STreehugger Robot Report("ERROR: %s failed to "
186*7c3d14c8STreehugger Robot "allocate %p (%zd) bytes at %p (error code: %d)\n",
187*7c3d14c8STreehugger Robot SanitizerToolName, size, size, fixed_addr, GetLastError());
188*7c3d14c8STreehugger Robot return p;
189*7c3d14c8STreehugger Robot }
190*7c3d14c8STreehugger Robot
191*7c3d14c8STreehugger Robot // Memory space mapped by 'MmapFixedOrDie' must have been reserved by
192*7c3d14c8STreehugger Robot // 'MmapFixedNoAccess'.
MmapFixedOrDie(uptr fixed_addr,uptr size)193*7c3d14c8STreehugger Robot void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
194*7c3d14c8STreehugger Robot void *p = VirtualAlloc((LPVOID)fixed_addr, size,
195*7c3d14c8STreehugger Robot MEM_COMMIT, PAGE_READWRITE);
196*7c3d14c8STreehugger Robot if (p == 0) {
197*7c3d14c8STreehugger Robot char mem_type[30];
198*7c3d14c8STreehugger Robot internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
199*7c3d14c8STreehugger Robot fixed_addr);
200*7c3d14c8STreehugger Robot ReportMmapFailureAndDie(size, mem_type, "allocate", GetLastError());
201*7c3d14c8STreehugger Robot }
202*7c3d14c8STreehugger Robot return p;
203*7c3d14c8STreehugger Robot }
204*7c3d14c8STreehugger Robot
MmapNoReserveOrDie(uptr size,const char * mem_type)205*7c3d14c8STreehugger Robot void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
206*7c3d14c8STreehugger Robot // FIXME: make this really NoReserve?
207*7c3d14c8STreehugger Robot return MmapOrDie(size, mem_type);
208*7c3d14c8STreehugger Robot }
209*7c3d14c8STreehugger Robot
MmapFixedNoAccess(uptr fixed_addr,uptr size,const char * name)210*7c3d14c8STreehugger Robot void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
211*7c3d14c8STreehugger Robot (void)name; // unsupported
212*7c3d14c8STreehugger Robot void *res = VirtualAlloc((LPVOID)fixed_addr, size,
213*7c3d14c8STreehugger Robot MEM_RESERVE, PAGE_NOACCESS);
214*7c3d14c8STreehugger Robot if (res == 0)
215*7c3d14c8STreehugger Robot Report("WARNING: %s failed to "
216*7c3d14c8STreehugger Robot "mprotect %p (%zd) bytes at %p (error code: %d)\n",
217*7c3d14c8STreehugger Robot SanitizerToolName, size, size, fixed_addr, GetLastError());
218*7c3d14c8STreehugger Robot return res;
219*7c3d14c8STreehugger Robot }
220*7c3d14c8STreehugger Robot
MmapNoAccess(uptr size)221*7c3d14c8STreehugger Robot void *MmapNoAccess(uptr size) {
222*7c3d14c8STreehugger Robot // FIXME: unsupported.
223*7c3d14c8STreehugger Robot return nullptr;
224*7c3d14c8STreehugger Robot }
225*7c3d14c8STreehugger Robot
MprotectNoAccess(uptr addr,uptr size)226*7c3d14c8STreehugger Robot bool MprotectNoAccess(uptr addr, uptr size) {
227*7c3d14c8STreehugger Robot DWORD old_protection;
228*7c3d14c8STreehugger Robot return VirtualProtect((LPVOID)addr, size, PAGE_NOACCESS, &old_protection);
229*7c3d14c8STreehugger Robot }
230*7c3d14c8STreehugger Robot
231*7c3d14c8STreehugger Robot
FlushUnneededShadowMemory(uptr addr,uptr size)232*7c3d14c8STreehugger Robot void FlushUnneededShadowMemory(uptr addr, uptr size) {
233*7c3d14c8STreehugger Robot // This is almost useless on 32-bits.
234*7c3d14c8STreehugger Robot // FIXME: add madvise-analog when we move to 64-bits.
235*7c3d14c8STreehugger Robot }
236*7c3d14c8STreehugger Robot
NoHugePagesInRegion(uptr addr,uptr size)237*7c3d14c8STreehugger Robot void NoHugePagesInRegion(uptr addr, uptr size) {
238*7c3d14c8STreehugger Robot // FIXME: probably similar to FlushUnneededShadowMemory.
239*7c3d14c8STreehugger Robot }
240*7c3d14c8STreehugger Robot
DontDumpShadowMemory(uptr addr,uptr length)241*7c3d14c8STreehugger Robot void DontDumpShadowMemory(uptr addr, uptr length) {
242*7c3d14c8STreehugger Robot // This is almost useless on 32-bits.
243*7c3d14c8STreehugger Robot // FIXME: add madvise-analog when we move to 64-bits.
244*7c3d14c8STreehugger Robot }
245*7c3d14c8STreehugger Robot
MemoryRangeIsAvailable(uptr range_start,uptr range_end)246*7c3d14c8STreehugger Robot bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
247*7c3d14c8STreehugger Robot MEMORY_BASIC_INFORMATION mbi;
248*7c3d14c8STreehugger Robot CHECK(VirtualQuery((void *)range_start, &mbi, sizeof(mbi)));
249*7c3d14c8STreehugger Robot return mbi.Protect == PAGE_NOACCESS &&
250*7c3d14c8STreehugger Robot (uptr)mbi.BaseAddress + mbi.RegionSize >= range_end;
251*7c3d14c8STreehugger Robot }
252*7c3d14c8STreehugger Robot
MapFileToMemory(const char * file_name,uptr * buff_size)253*7c3d14c8STreehugger Robot void *MapFileToMemory(const char *file_name, uptr *buff_size) {
254*7c3d14c8STreehugger Robot UNIMPLEMENTED();
255*7c3d14c8STreehugger Robot }
256*7c3d14c8STreehugger Robot
MapWritableFileToMemory(void * addr,uptr size,fd_t fd,OFF_T offset)257*7c3d14c8STreehugger Robot void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset) {
258*7c3d14c8STreehugger Robot UNIMPLEMENTED();
259*7c3d14c8STreehugger Robot }
260*7c3d14c8STreehugger Robot
261*7c3d14c8STreehugger Robot static const int kMaxEnvNameLength = 128;
262*7c3d14c8STreehugger Robot static const DWORD kMaxEnvValueLength = 32767;
263*7c3d14c8STreehugger Robot
264*7c3d14c8STreehugger Robot namespace {
265*7c3d14c8STreehugger Robot
266*7c3d14c8STreehugger Robot struct EnvVariable {
267*7c3d14c8STreehugger Robot char name[kMaxEnvNameLength];
268*7c3d14c8STreehugger Robot char value[kMaxEnvValueLength];
269*7c3d14c8STreehugger Robot };
270*7c3d14c8STreehugger Robot
271*7c3d14c8STreehugger Robot } // namespace
272*7c3d14c8STreehugger Robot
273*7c3d14c8STreehugger Robot static const int kEnvVariables = 5;
274*7c3d14c8STreehugger Robot static EnvVariable env_vars[kEnvVariables];
275*7c3d14c8STreehugger Robot static int num_env_vars;
276*7c3d14c8STreehugger Robot
GetEnv(const char * name)277*7c3d14c8STreehugger Robot const char *GetEnv(const char *name) {
278*7c3d14c8STreehugger Robot // Note: this implementation caches the values of the environment variables
279*7c3d14c8STreehugger Robot // and limits their quantity.
280*7c3d14c8STreehugger Robot for (int i = 0; i < num_env_vars; i++) {
281*7c3d14c8STreehugger Robot if (0 == internal_strcmp(name, env_vars[i].name))
282*7c3d14c8STreehugger Robot return env_vars[i].value;
283*7c3d14c8STreehugger Robot }
284*7c3d14c8STreehugger Robot CHECK_LT(num_env_vars, kEnvVariables);
285*7c3d14c8STreehugger Robot DWORD rv = GetEnvironmentVariableA(name, env_vars[num_env_vars].value,
286*7c3d14c8STreehugger Robot kMaxEnvValueLength);
287*7c3d14c8STreehugger Robot if (rv > 0 && rv < kMaxEnvValueLength) {
288*7c3d14c8STreehugger Robot CHECK_LT(internal_strlen(name), kMaxEnvNameLength);
289*7c3d14c8STreehugger Robot internal_strncpy(env_vars[num_env_vars].name, name, kMaxEnvNameLength);
290*7c3d14c8STreehugger Robot num_env_vars++;
291*7c3d14c8STreehugger Robot return env_vars[num_env_vars - 1].value;
292*7c3d14c8STreehugger Robot }
293*7c3d14c8STreehugger Robot return 0;
294*7c3d14c8STreehugger Robot }
295*7c3d14c8STreehugger Robot
GetPwd()296*7c3d14c8STreehugger Robot const char *GetPwd() {
297*7c3d14c8STreehugger Robot UNIMPLEMENTED();
298*7c3d14c8STreehugger Robot }
299*7c3d14c8STreehugger Robot
GetUid()300*7c3d14c8STreehugger Robot u32 GetUid() {
301*7c3d14c8STreehugger Robot UNIMPLEMENTED();
302*7c3d14c8STreehugger Robot }
303*7c3d14c8STreehugger Robot
304*7c3d14c8STreehugger Robot namespace {
305*7c3d14c8STreehugger Robot struct ModuleInfo {
306*7c3d14c8STreehugger Robot const char *filepath;
307*7c3d14c8STreehugger Robot uptr base_address;
308*7c3d14c8STreehugger Robot uptr end_address;
309*7c3d14c8STreehugger Robot };
310*7c3d14c8STreehugger Robot
311*7c3d14c8STreehugger Robot #ifndef SANITIZER_GO
CompareModulesBase(const void * pl,const void * pr)312*7c3d14c8STreehugger Robot int CompareModulesBase(const void *pl, const void *pr) {
313*7c3d14c8STreehugger Robot const ModuleInfo *l = (ModuleInfo *)pl, *r = (ModuleInfo *)pr;
314*7c3d14c8STreehugger Robot if (l->base_address < r->base_address)
315*7c3d14c8STreehugger Robot return -1;
316*7c3d14c8STreehugger Robot return l->base_address > r->base_address;
317*7c3d14c8STreehugger Robot }
318*7c3d14c8STreehugger Robot #endif
319*7c3d14c8STreehugger Robot } // namespace
320*7c3d14c8STreehugger Robot
321*7c3d14c8STreehugger Robot #ifndef SANITIZER_GO
DumpProcessMap()322*7c3d14c8STreehugger Robot void DumpProcessMap() {
323*7c3d14c8STreehugger Robot Report("Dumping process modules:\n");
324*7c3d14c8STreehugger Robot ListOfModules modules;
325*7c3d14c8STreehugger Robot modules.init();
326*7c3d14c8STreehugger Robot uptr num_modules = modules.size();
327*7c3d14c8STreehugger Robot
328*7c3d14c8STreehugger Robot InternalScopedBuffer<ModuleInfo> module_infos(num_modules);
329*7c3d14c8STreehugger Robot for (size_t i = 0; i < num_modules; ++i) {
330*7c3d14c8STreehugger Robot module_infos[i].filepath = modules[i].full_name();
331*7c3d14c8STreehugger Robot module_infos[i].base_address = modules[i].base_address();
332*7c3d14c8STreehugger Robot module_infos[i].end_address = modules[i].ranges().front()->end;
333*7c3d14c8STreehugger Robot }
334*7c3d14c8STreehugger Robot qsort(module_infos.data(), num_modules, sizeof(ModuleInfo),
335*7c3d14c8STreehugger Robot CompareModulesBase);
336*7c3d14c8STreehugger Robot
337*7c3d14c8STreehugger Robot for (size_t i = 0; i < num_modules; ++i) {
338*7c3d14c8STreehugger Robot const ModuleInfo &mi = module_infos[i];
339*7c3d14c8STreehugger Robot if (mi.end_address != 0) {
340*7c3d14c8STreehugger Robot Printf("\t%p-%p %s\n", mi.base_address, mi.end_address,
341*7c3d14c8STreehugger Robot mi.filepath[0] ? mi.filepath : "[no name]");
342*7c3d14c8STreehugger Robot } else if (mi.filepath[0]) {
343*7c3d14c8STreehugger Robot Printf("\t??\?-??? %s\n", mi.filepath);
344*7c3d14c8STreehugger Robot } else {
345*7c3d14c8STreehugger Robot Printf("\t???\n");
346*7c3d14c8STreehugger Robot }
347*7c3d14c8STreehugger Robot }
348*7c3d14c8STreehugger Robot }
349*7c3d14c8STreehugger Robot #endif
350*7c3d14c8STreehugger Robot
DisableCoreDumperIfNecessary()351*7c3d14c8STreehugger Robot void DisableCoreDumperIfNecessary() {
352*7c3d14c8STreehugger Robot // Do nothing.
353*7c3d14c8STreehugger Robot }
354*7c3d14c8STreehugger Robot
ReExec()355*7c3d14c8STreehugger Robot void ReExec() {
356*7c3d14c8STreehugger Robot UNIMPLEMENTED();
357*7c3d14c8STreehugger Robot }
358*7c3d14c8STreehugger Robot
PrepareForSandboxing(__sanitizer_sandbox_arguments * args)359*7c3d14c8STreehugger Robot void PrepareForSandboxing(__sanitizer_sandbox_arguments *args) {
360*7c3d14c8STreehugger Robot #if !SANITIZER_GO
361*7c3d14c8STreehugger Robot CovPrepareForSandboxing(args);
362*7c3d14c8STreehugger Robot #endif
363*7c3d14c8STreehugger Robot }
364*7c3d14c8STreehugger Robot
StackSizeIsUnlimited()365*7c3d14c8STreehugger Robot bool StackSizeIsUnlimited() {
366*7c3d14c8STreehugger Robot UNIMPLEMENTED();
367*7c3d14c8STreehugger Robot }
368*7c3d14c8STreehugger Robot
SetStackSizeLimitInBytes(uptr limit)369*7c3d14c8STreehugger Robot void SetStackSizeLimitInBytes(uptr limit) {
370*7c3d14c8STreehugger Robot UNIMPLEMENTED();
371*7c3d14c8STreehugger Robot }
372*7c3d14c8STreehugger Robot
AddressSpaceIsUnlimited()373*7c3d14c8STreehugger Robot bool AddressSpaceIsUnlimited() {
374*7c3d14c8STreehugger Robot UNIMPLEMENTED();
375*7c3d14c8STreehugger Robot }
376*7c3d14c8STreehugger Robot
SetAddressSpaceUnlimited()377*7c3d14c8STreehugger Robot void SetAddressSpaceUnlimited() {
378*7c3d14c8STreehugger Robot UNIMPLEMENTED();
379*7c3d14c8STreehugger Robot }
380*7c3d14c8STreehugger Robot
IsPathSeparator(const char c)381*7c3d14c8STreehugger Robot bool IsPathSeparator(const char c) {
382*7c3d14c8STreehugger Robot return c == '\\' || c == '/';
383*7c3d14c8STreehugger Robot }
384*7c3d14c8STreehugger Robot
IsAbsolutePath(const char * path)385*7c3d14c8STreehugger Robot bool IsAbsolutePath(const char *path) {
386*7c3d14c8STreehugger Robot UNIMPLEMENTED();
387*7c3d14c8STreehugger Robot }
388*7c3d14c8STreehugger Robot
SleepForSeconds(int seconds)389*7c3d14c8STreehugger Robot void SleepForSeconds(int seconds) {
390*7c3d14c8STreehugger Robot Sleep(seconds * 1000);
391*7c3d14c8STreehugger Robot }
392*7c3d14c8STreehugger Robot
SleepForMillis(int millis)393*7c3d14c8STreehugger Robot void SleepForMillis(int millis) {
394*7c3d14c8STreehugger Robot Sleep(millis);
395*7c3d14c8STreehugger Robot }
396*7c3d14c8STreehugger Robot
NanoTime()397*7c3d14c8STreehugger Robot u64 NanoTime() {
398*7c3d14c8STreehugger Robot return 0;
399*7c3d14c8STreehugger Robot }
400*7c3d14c8STreehugger Robot
Abort()401*7c3d14c8STreehugger Robot void Abort() {
402*7c3d14c8STreehugger Robot if (::IsDebuggerPresent())
403*7c3d14c8STreehugger Robot __debugbreak();
404*7c3d14c8STreehugger Robot internal__exit(3);
405*7c3d14c8STreehugger Robot }
406*7c3d14c8STreehugger Robot
407*7c3d14c8STreehugger Robot #ifndef SANITIZER_GO
408*7c3d14c8STreehugger Robot // Read the file to extract the ImageBase field from the PE header. If ASLR is
409*7c3d14c8STreehugger Robot // disabled and this virtual address is available, the loader will typically
410*7c3d14c8STreehugger Robot // load the image at this address. Therefore, we call it the preferred base. Any
411*7c3d14c8STreehugger Robot // addresses in the DWARF typically assume that the object has been loaded at
412*7c3d14c8STreehugger Robot // this address.
GetPreferredBase(const char * modname)413*7c3d14c8STreehugger Robot static uptr GetPreferredBase(const char *modname) {
414*7c3d14c8STreehugger Robot fd_t fd = OpenFile(modname, RdOnly, nullptr);
415*7c3d14c8STreehugger Robot if (fd == kInvalidFd)
416*7c3d14c8STreehugger Robot return 0;
417*7c3d14c8STreehugger Robot FileCloser closer(fd);
418*7c3d14c8STreehugger Robot
419*7c3d14c8STreehugger Robot // Read just the DOS header.
420*7c3d14c8STreehugger Robot IMAGE_DOS_HEADER dos_header;
421*7c3d14c8STreehugger Robot uptr bytes_read;
422*7c3d14c8STreehugger Robot if (!ReadFromFile(fd, &dos_header, sizeof(dos_header), &bytes_read) ||
423*7c3d14c8STreehugger Robot bytes_read != sizeof(dos_header))
424*7c3d14c8STreehugger Robot return 0;
425*7c3d14c8STreehugger Robot
426*7c3d14c8STreehugger Robot // The file should start with the right signature.
427*7c3d14c8STreehugger Robot if (dos_header.e_magic != IMAGE_DOS_SIGNATURE)
428*7c3d14c8STreehugger Robot return 0;
429*7c3d14c8STreehugger Robot
430*7c3d14c8STreehugger Robot // The layout at e_lfanew is:
431*7c3d14c8STreehugger Robot // "PE\0\0"
432*7c3d14c8STreehugger Robot // IMAGE_FILE_HEADER
433*7c3d14c8STreehugger Robot // IMAGE_OPTIONAL_HEADER
434*7c3d14c8STreehugger Robot // Seek to e_lfanew and read all that data.
435*7c3d14c8STreehugger Robot char buf[4 + sizeof(IMAGE_FILE_HEADER) + sizeof(IMAGE_OPTIONAL_HEADER)];
436*7c3d14c8STreehugger Robot if (::SetFilePointer(fd, dos_header.e_lfanew, nullptr, FILE_BEGIN) ==
437*7c3d14c8STreehugger Robot INVALID_SET_FILE_POINTER)
438*7c3d14c8STreehugger Robot return 0;
439*7c3d14c8STreehugger Robot if (!ReadFromFile(fd, &buf[0], sizeof(buf), &bytes_read) ||
440*7c3d14c8STreehugger Robot bytes_read != sizeof(buf))
441*7c3d14c8STreehugger Robot return 0;
442*7c3d14c8STreehugger Robot
443*7c3d14c8STreehugger Robot // Check for "PE\0\0" before the PE header.
444*7c3d14c8STreehugger Robot char *pe_sig = &buf[0];
445*7c3d14c8STreehugger Robot if (internal_memcmp(pe_sig, "PE\0\0", 4) != 0)
446*7c3d14c8STreehugger Robot return 0;
447*7c3d14c8STreehugger Robot
448*7c3d14c8STreehugger Robot // Skip over IMAGE_FILE_HEADER. We could do more validation here if we wanted.
449*7c3d14c8STreehugger Robot IMAGE_OPTIONAL_HEADER *pe_header =
450*7c3d14c8STreehugger Robot (IMAGE_OPTIONAL_HEADER *)(pe_sig + 4 + sizeof(IMAGE_FILE_HEADER));
451*7c3d14c8STreehugger Robot
452*7c3d14c8STreehugger Robot // Check for more magic in the PE header.
453*7c3d14c8STreehugger Robot if (pe_header->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC)
454*7c3d14c8STreehugger Robot return 0;
455*7c3d14c8STreehugger Robot
456*7c3d14c8STreehugger Robot // Finally, return the ImageBase.
457*7c3d14c8STreehugger Robot return (uptr)pe_header->ImageBase;
458*7c3d14c8STreehugger Robot }
459*7c3d14c8STreehugger Robot
init()460*7c3d14c8STreehugger Robot void ListOfModules::init() {
461*7c3d14c8STreehugger Robot clear();
462*7c3d14c8STreehugger Robot HANDLE cur_process = GetCurrentProcess();
463*7c3d14c8STreehugger Robot
464*7c3d14c8STreehugger Robot // Query the list of modules. Start by assuming there are no more than 256
465*7c3d14c8STreehugger Robot // modules and retry if that's not sufficient.
466*7c3d14c8STreehugger Robot HMODULE *hmodules = 0;
467*7c3d14c8STreehugger Robot uptr modules_buffer_size = sizeof(HMODULE) * 256;
468*7c3d14c8STreehugger Robot DWORD bytes_required;
469*7c3d14c8STreehugger Robot while (!hmodules) {
470*7c3d14c8STreehugger Robot hmodules = (HMODULE *)MmapOrDie(modules_buffer_size, __FUNCTION__);
471*7c3d14c8STreehugger Robot CHECK(EnumProcessModules(cur_process, hmodules, modules_buffer_size,
472*7c3d14c8STreehugger Robot &bytes_required));
473*7c3d14c8STreehugger Robot if (bytes_required > modules_buffer_size) {
474*7c3d14c8STreehugger Robot // Either there turned out to be more than 256 hmodules, or new hmodules
475*7c3d14c8STreehugger Robot // could have loaded since the last try. Retry.
476*7c3d14c8STreehugger Robot UnmapOrDie(hmodules, modules_buffer_size);
477*7c3d14c8STreehugger Robot hmodules = 0;
478*7c3d14c8STreehugger Robot modules_buffer_size = bytes_required;
479*7c3d14c8STreehugger Robot }
480*7c3d14c8STreehugger Robot }
481*7c3d14c8STreehugger Robot
482*7c3d14c8STreehugger Robot // |num_modules| is the number of modules actually present,
483*7c3d14c8STreehugger Robot size_t num_modules = bytes_required / sizeof(HMODULE);
484*7c3d14c8STreehugger Robot for (size_t i = 0; i < num_modules; ++i) {
485*7c3d14c8STreehugger Robot HMODULE handle = hmodules[i];
486*7c3d14c8STreehugger Robot MODULEINFO mi;
487*7c3d14c8STreehugger Robot if (!GetModuleInformation(cur_process, handle, &mi, sizeof(mi)))
488*7c3d14c8STreehugger Robot continue;
489*7c3d14c8STreehugger Robot
490*7c3d14c8STreehugger Robot // Get the UTF-16 path and convert to UTF-8.
491*7c3d14c8STreehugger Robot wchar_t modname_utf16[kMaxPathLength];
492*7c3d14c8STreehugger Robot int modname_utf16_len =
493*7c3d14c8STreehugger Robot GetModuleFileNameW(handle, modname_utf16, kMaxPathLength);
494*7c3d14c8STreehugger Robot if (modname_utf16_len == 0)
495*7c3d14c8STreehugger Robot modname_utf16[0] = '\0';
496*7c3d14c8STreehugger Robot char module_name[kMaxPathLength];
497*7c3d14c8STreehugger Robot int module_name_len =
498*7c3d14c8STreehugger Robot ::WideCharToMultiByte(CP_UTF8, 0, modname_utf16, modname_utf16_len + 1,
499*7c3d14c8STreehugger Robot &module_name[0], kMaxPathLength, NULL, NULL);
500*7c3d14c8STreehugger Robot module_name[module_name_len] = '\0';
501*7c3d14c8STreehugger Robot
502*7c3d14c8STreehugger Robot uptr base_address = (uptr)mi.lpBaseOfDll;
503*7c3d14c8STreehugger Robot uptr end_address = (uptr)mi.lpBaseOfDll + mi.SizeOfImage;
504*7c3d14c8STreehugger Robot
505*7c3d14c8STreehugger Robot // Adjust the base address of the module so that we get a VA instead of an
506*7c3d14c8STreehugger Robot // RVA when computing the module offset. This helps llvm-symbolizer find the
507*7c3d14c8STreehugger Robot // right DWARF CU. In the common case that the image is loaded at it's
508*7c3d14c8STreehugger Robot // preferred address, we will now print normal virtual addresses.
509*7c3d14c8STreehugger Robot uptr preferred_base = GetPreferredBase(&module_name[0]);
510*7c3d14c8STreehugger Robot uptr adjusted_base = base_address - preferred_base;
511*7c3d14c8STreehugger Robot
512*7c3d14c8STreehugger Robot LoadedModule cur_module;
513*7c3d14c8STreehugger Robot cur_module.set(module_name, adjusted_base);
514*7c3d14c8STreehugger Robot // We add the whole module as one single address range.
515*7c3d14c8STreehugger Robot cur_module.addAddressRange(base_address, end_address, /*executable*/ true);
516*7c3d14c8STreehugger Robot modules_.push_back(cur_module);
517*7c3d14c8STreehugger Robot }
518*7c3d14c8STreehugger Robot UnmapOrDie(hmodules, modules_buffer_size);
519*7c3d14c8STreehugger Robot };
520*7c3d14c8STreehugger Robot
521*7c3d14c8STreehugger Robot // We can't use atexit() directly at __asan_init time as the CRT is not fully
522*7c3d14c8STreehugger Robot // initialized at this point. Place the functions into a vector and use
523*7c3d14c8STreehugger Robot // atexit() as soon as it is ready for use (i.e. after .CRT$XIC initializers).
524*7c3d14c8STreehugger Robot InternalMmapVectorNoCtor<void (*)(void)> atexit_functions;
525*7c3d14c8STreehugger Robot
Atexit(void (* function)(void))526*7c3d14c8STreehugger Robot int Atexit(void (*function)(void)) {
527*7c3d14c8STreehugger Robot atexit_functions.push_back(function);
528*7c3d14c8STreehugger Robot return 0;
529*7c3d14c8STreehugger Robot }
530*7c3d14c8STreehugger Robot
RunAtexit()531*7c3d14c8STreehugger Robot static int RunAtexit() {
532*7c3d14c8STreehugger Robot int ret = 0;
533*7c3d14c8STreehugger Robot for (uptr i = 0; i < atexit_functions.size(); ++i) {
534*7c3d14c8STreehugger Robot ret |= atexit(atexit_functions[i]);
535*7c3d14c8STreehugger Robot }
536*7c3d14c8STreehugger Robot return ret;
537*7c3d14c8STreehugger Robot }
538*7c3d14c8STreehugger Robot
539*7c3d14c8STreehugger Robot #pragma section(".CRT$XID", long, read) // NOLINT
540*7c3d14c8STreehugger Robot __declspec(allocate(".CRT$XID")) int (*__run_atexit)() = RunAtexit;
541*7c3d14c8STreehugger Robot #endif
542*7c3d14c8STreehugger Robot
543*7c3d14c8STreehugger Robot // ------------------ sanitizer_libc.h
OpenFile(const char * filename,FileAccessMode mode,error_t * last_error)544*7c3d14c8STreehugger Robot fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *last_error) {
545*7c3d14c8STreehugger Robot // FIXME: Use the wide variants to handle Unicode filenames.
546*7c3d14c8STreehugger Robot fd_t res;
547*7c3d14c8STreehugger Robot if (mode == RdOnly) {
548*7c3d14c8STreehugger Robot res = CreateFileA(filename, GENERIC_READ,
549*7c3d14c8STreehugger Robot FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
550*7c3d14c8STreehugger Robot nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
551*7c3d14c8STreehugger Robot } else if (mode == WrOnly) {
552*7c3d14c8STreehugger Robot res = CreateFileA(filename, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS,
553*7c3d14c8STreehugger Robot FILE_ATTRIBUTE_NORMAL, nullptr);
554*7c3d14c8STreehugger Robot } else {
555*7c3d14c8STreehugger Robot UNIMPLEMENTED();
556*7c3d14c8STreehugger Robot }
557*7c3d14c8STreehugger Robot CHECK(res != kStdoutFd || kStdoutFd == kInvalidFd);
558*7c3d14c8STreehugger Robot CHECK(res != kStderrFd || kStderrFd == kInvalidFd);
559*7c3d14c8STreehugger Robot if (res == kInvalidFd && last_error)
560*7c3d14c8STreehugger Robot *last_error = GetLastError();
561*7c3d14c8STreehugger Robot return res;
562*7c3d14c8STreehugger Robot }
563*7c3d14c8STreehugger Robot
CloseFile(fd_t fd)564*7c3d14c8STreehugger Robot void CloseFile(fd_t fd) {
565*7c3d14c8STreehugger Robot CloseHandle(fd);
566*7c3d14c8STreehugger Robot }
567*7c3d14c8STreehugger Robot
ReadFromFile(fd_t fd,void * buff,uptr buff_size,uptr * bytes_read,error_t * error_p)568*7c3d14c8STreehugger Robot bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
569*7c3d14c8STreehugger Robot error_t *error_p) {
570*7c3d14c8STreehugger Robot CHECK(fd != kInvalidFd);
571*7c3d14c8STreehugger Robot
572*7c3d14c8STreehugger Robot // bytes_read can't be passed directly to ReadFile:
573*7c3d14c8STreehugger Robot // uptr is unsigned long long on 64-bit Windows.
574*7c3d14c8STreehugger Robot unsigned long num_read_long;
575*7c3d14c8STreehugger Robot
576*7c3d14c8STreehugger Robot bool success = ::ReadFile(fd, buff, buff_size, &num_read_long, nullptr);
577*7c3d14c8STreehugger Robot if (!success && error_p)
578*7c3d14c8STreehugger Robot *error_p = GetLastError();
579*7c3d14c8STreehugger Robot if (bytes_read)
580*7c3d14c8STreehugger Robot *bytes_read = num_read_long;
581*7c3d14c8STreehugger Robot return success;
582*7c3d14c8STreehugger Robot }
583*7c3d14c8STreehugger Robot
SupportsColoredOutput(fd_t fd)584*7c3d14c8STreehugger Robot bool SupportsColoredOutput(fd_t fd) {
585*7c3d14c8STreehugger Robot // FIXME: support colored output.
586*7c3d14c8STreehugger Robot return false;
587*7c3d14c8STreehugger Robot }
588*7c3d14c8STreehugger Robot
WriteToFile(fd_t fd,const void * buff,uptr buff_size,uptr * bytes_written,error_t * error_p)589*7c3d14c8STreehugger Robot bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
590*7c3d14c8STreehugger Robot error_t *error_p) {
591*7c3d14c8STreehugger Robot CHECK(fd != kInvalidFd);
592*7c3d14c8STreehugger Robot
593*7c3d14c8STreehugger Robot // Handle null optional parameters.
594*7c3d14c8STreehugger Robot error_t dummy_error;
595*7c3d14c8STreehugger Robot error_p = error_p ? error_p : &dummy_error;
596*7c3d14c8STreehugger Robot uptr dummy_bytes_written;
597*7c3d14c8STreehugger Robot bytes_written = bytes_written ? bytes_written : &dummy_bytes_written;
598*7c3d14c8STreehugger Robot
599*7c3d14c8STreehugger Robot // Initialize output parameters in case we fail.
600*7c3d14c8STreehugger Robot *error_p = 0;
601*7c3d14c8STreehugger Robot *bytes_written = 0;
602*7c3d14c8STreehugger Robot
603*7c3d14c8STreehugger Robot // Map the conventional Unix fds 1 and 2 to Windows handles. They might be
604*7c3d14c8STreehugger Robot // closed, in which case this will fail.
605*7c3d14c8STreehugger Robot if (fd == kStdoutFd || fd == kStderrFd) {
606*7c3d14c8STreehugger Robot fd = GetStdHandle(fd == kStdoutFd ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
607*7c3d14c8STreehugger Robot if (fd == 0) {
608*7c3d14c8STreehugger Robot *error_p = ERROR_INVALID_HANDLE;
609*7c3d14c8STreehugger Robot return false;
610*7c3d14c8STreehugger Robot }
611*7c3d14c8STreehugger Robot }
612*7c3d14c8STreehugger Robot
613*7c3d14c8STreehugger Robot DWORD bytes_written_32;
614*7c3d14c8STreehugger Robot if (!WriteFile(fd, buff, buff_size, &bytes_written_32, 0)) {
615*7c3d14c8STreehugger Robot *error_p = GetLastError();
616*7c3d14c8STreehugger Robot return false;
617*7c3d14c8STreehugger Robot } else {
618*7c3d14c8STreehugger Robot *bytes_written = bytes_written_32;
619*7c3d14c8STreehugger Robot return true;
620*7c3d14c8STreehugger Robot }
621*7c3d14c8STreehugger Robot }
622*7c3d14c8STreehugger Robot
RenameFile(const char * oldpath,const char * newpath,error_t * error_p)623*7c3d14c8STreehugger Robot bool RenameFile(const char *oldpath, const char *newpath, error_t *error_p) {
624*7c3d14c8STreehugger Robot UNIMPLEMENTED();
625*7c3d14c8STreehugger Robot }
626*7c3d14c8STreehugger Robot
internal_sched_yield()627*7c3d14c8STreehugger Robot uptr internal_sched_yield() {
628*7c3d14c8STreehugger Robot Sleep(0);
629*7c3d14c8STreehugger Robot return 0;
630*7c3d14c8STreehugger Robot }
631*7c3d14c8STreehugger Robot
internal__exit(int exitcode)632*7c3d14c8STreehugger Robot void internal__exit(int exitcode) {
633*7c3d14c8STreehugger Robot ExitProcess(exitcode);
634*7c3d14c8STreehugger Robot }
635*7c3d14c8STreehugger Robot
internal_ftruncate(fd_t fd,uptr size)636*7c3d14c8STreehugger Robot uptr internal_ftruncate(fd_t fd, uptr size) {
637*7c3d14c8STreehugger Robot UNIMPLEMENTED();
638*7c3d14c8STreehugger Robot }
639*7c3d14c8STreehugger Robot
GetRSS()640*7c3d14c8STreehugger Robot uptr GetRSS() {
641*7c3d14c8STreehugger Robot return 0;
642*7c3d14c8STreehugger Robot }
643*7c3d14c8STreehugger Robot
internal_start_thread(void (* func)(void * arg),void * arg)644*7c3d14c8STreehugger Robot void *internal_start_thread(void (*func)(void *arg), void *arg) { return 0; }
internal_join_thread(void * th)645*7c3d14c8STreehugger Robot void internal_join_thread(void *th) { }
646*7c3d14c8STreehugger Robot
647*7c3d14c8STreehugger Robot // ---------------------- BlockingMutex ---------------- {{{1
648*7c3d14c8STreehugger Robot const uptr LOCK_UNINITIALIZED = 0;
649*7c3d14c8STreehugger Robot const uptr LOCK_READY = (uptr)-1;
650*7c3d14c8STreehugger Robot
BlockingMutex(LinkerInitialized li)651*7c3d14c8STreehugger Robot BlockingMutex::BlockingMutex(LinkerInitialized li) {
652*7c3d14c8STreehugger Robot // FIXME: see comments in BlockingMutex::Lock() for the details.
653*7c3d14c8STreehugger Robot CHECK(li == LINKER_INITIALIZED || owner_ == LOCK_UNINITIALIZED);
654*7c3d14c8STreehugger Robot
655*7c3d14c8STreehugger Robot CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
656*7c3d14c8STreehugger Robot InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
657*7c3d14c8STreehugger Robot owner_ = LOCK_READY;
658*7c3d14c8STreehugger Robot }
659*7c3d14c8STreehugger Robot
BlockingMutex()660*7c3d14c8STreehugger Robot BlockingMutex::BlockingMutex() {
661*7c3d14c8STreehugger Robot CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
662*7c3d14c8STreehugger Robot InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
663*7c3d14c8STreehugger Robot owner_ = LOCK_READY;
664*7c3d14c8STreehugger Robot }
665*7c3d14c8STreehugger Robot
Lock()666*7c3d14c8STreehugger Robot void BlockingMutex::Lock() {
667*7c3d14c8STreehugger Robot if (owner_ == LOCK_UNINITIALIZED) {
668*7c3d14c8STreehugger Robot // FIXME: hm, global BlockingMutex objects are not initialized?!?
669*7c3d14c8STreehugger Robot // This might be a side effect of the clang+cl+link Frankenbuild...
670*7c3d14c8STreehugger Robot new(this) BlockingMutex((LinkerInitialized)(LINKER_INITIALIZED + 1));
671*7c3d14c8STreehugger Robot
672*7c3d14c8STreehugger Robot // FIXME: If it turns out the linker doesn't invoke our
673*7c3d14c8STreehugger Robot // constructors, we should probably manually Lock/Unlock all the global
674*7c3d14c8STreehugger Robot // locks while we're starting in one thread to avoid double-init races.
675*7c3d14c8STreehugger Robot }
676*7c3d14c8STreehugger Robot EnterCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
677*7c3d14c8STreehugger Robot CHECK_EQ(owner_, LOCK_READY);
678*7c3d14c8STreehugger Robot owner_ = GetThreadSelf();
679*7c3d14c8STreehugger Robot }
680*7c3d14c8STreehugger Robot
Unlock()681*7c3d14c8STreehugger Robot void BlockingMutex::Unlock() {
682*7c3d14c8STreehugger Robot CHECK_EQ(owner_, GetThreadSelf());
683*7c3d14c8STreehugger Robot owner_ = LOCK_READY;
684*7c3d14c8STreehugger Robot LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
685*7c3d14c8STreehugger Robot }
686*7c3d14c8STreehugger Robot
CheckLocked()687*7c3d14c8STreehugger Robot void BlockingMutex::CheckLocked() {
688*7c3d14c8STreehugger Robot CHECK_EQ(owner_, GetThreadSelf());
689*7c3d14c8STreehugger Robot }
690*7c3d14c8STreehugger Robot
GetTlsSize()691*7c3d14c8STreehugger Robot uptr GetTlsSize() {
692*7c3d14c8STreehugger Robot return 0;
693*7c3d14c8STreehugger Robot }
694*7c3d14c8STreehugger Robot
InitTlsSize()695*7c3d14c8STreehugger Robot void InitTlsSize() {
696*7c3d14c8STreehugger Robot }
697*7c3d14c8STreehugger Robot
GetThreadStackAndTls(bool main,uptr * stk_addr,uptr * stk_size,uptr * tls_addr,uptr * tls_size)698*7c3d14c8STreehugger Robot void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
699*7c3d14c8STreehugger Robot uptr *tls_addr, uptr *tls_size) {
700*7c3d14c8STreehugger Robot #ifdef SANITIZER_GO
701*7c3d14c8STreehugger Robot *stk_addr = 0;
702*7c3d14c8STreehugger Robot *stk_size = 0;
703*7c3d14c8STreehugger Robot *tls_addr = 0;
704*7c3d14c8STreehugger Robot *tls_size = 0;
705*7c3d14c8STreehugger Robot #else
706*7c3d14c8STreehugger Robot uptr stack_top, stack_bottom;
707*7c3d14c8STreehugger Robot GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
708*7c3d14c8STreehugger Robot *stk_addr = stack_bottom;
709*7c3d14c8STreehugger Robot *stk_size = stack_top - stack_bottom;
710*7c3d14c8STreehugger Robot *tls_addr = 0;
711*7c3d14c8STreehugger Robot *tls_size = 0;
712*7c3d14c8STreehugger Robot #endif
713*7c3d14c8STreehugger Robot }
714*7c3d14c8STreehugger Robot
715*7c3d14c8STreehugger Robot #if !SANITIZER_GO
SlowUnwindStack(uptr pc,u32 max_depth)716*7c3d14c8STreehugger Robot void BufferedStackTrace::SlowUnwindStack(uptr pc, u32 max_depth) {
717*7c3d14c8STreehugger Robot CHECK_GE(max_depth, 2);
718*7c3d14c8STreehugger Robot // FIXME: CaptureStackBackTrace might be too slow for us.
719*7c3d14c8STreehugger Robot // FIXME: Compare with StackWalk64.
720*7c3d14c8STreehugger Robot // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc
721*7c3d14c8STreehugger Robot size = CaptureStackBackTrace(2, Min(max_depth, kStackTraceMax),
722*7c3d14c8STreehugger Robot (void**)trace, 0);
723*7c3d14c8STreehugger Robot if (size == 0)
724*7c3d14c8STreehugger Robot return;
725*7c3d14c8STreehugger Robot
726*7c3d14c8STreehugger Robot // Skip the RTL frames by searching for the PC in the stacktrace.
727*7c3d14c8STreehugger Robot uptr pc_location = LocatePcInTrace(pc);
728*7c3d14c8STreehugger Robot PopStackFrames(pc_location);
729*7c3d14c8STreehugger Robot }
730*7c3d14c8STreehugger Robot
SlowUnwindStackWithContext(uptr pc,void * context,u32 max_depth)731*7c3d14c8STreehugger Robot void BufferedStackTrace::SlowUnwindStackWithContext(uptr pc, void *context,
732*7c3d14c8STreehugger Robot u32 max_depth) {
733*7c3d14c8STreehugger Robot CONTEXT ctx = *(CONTEXT *)context;
734*7c3d14c8STreehugger Robot STACKFRAME64 stack_frame;
735*7c3d14c8STreehugger Robot memset(&stack_frame, 0, sizeof(stack_frame));
736*7c3d14c8STreehugger Robot size = 0;
737*7c3d14c8STreehugger Robot #if defined(_WIN64)
738*7c3d14c8STreehugger Robot int machine_type = IMAGE_FILE_MACHINE_AMD64;
739*7c3d14c8STreehugger Robot stack_frame.AddrPC.Offset = ctx.Rip;
740*7c3d14c8STreehugger Robot stack_frame.AddrFrame.Offset = ctx.Rbp;
741*7c3d14c8STreehugger Robot stack_frame.AddrStack.Offset = ctx.Rsp;
742*7c3d14c8STreehugger Robot #else
743*7c3d14c8STreehugger Robot int machine_type = IMAGE_FILE_MACHINE_I386;
744*7c3d14c8STreehugger Robot stack_frame.AddrPC.Offset = ctx.Eip;
745*7c3d14c8STreehugger Robot stack_frame.AddrFrame.Offset = ctx.Ebp;
746*7c3d14c8STreehugger Robot stack_frame.AddrStack.Offset = ctx.Esp;
747*7c3d14c8STreehugger Robot #endif
748*7c3d14c8STreehugger Robot stack_frame.AddrPC.Mode = AddrModeFlat;
749*7c3d14c8STreehugger Robot stack_frame.AddrFrame.Mode = AddrModeFlat;
750*7c3d14c8STreehugger Robot stack_frame.AddrStack.Mode = AddrModeFlat;
751*7c3d14c8STreehugger Robot while (StackWalk64(machine_type, GetCurrentProcess(), GetCurrentThread(),
752*7c3d14c8STreehugger Robot &stack_frame, &ctx, NULL, &SymFunctionTableAccess64,
753*7c3d14c8STreehugger Robot &SymGetModuleBase64, NULL) &&
754*7c3d14c8STreehugger Robot size < Min(max_depth, kStackTraceMax)) {
755*7c3d14c8STreehugger Robot trace_buffer[size++] = (uptr)stack_frame.AddrPC.Offset;
756*7c3d14c8STreehugger Robot }
757*7c3d14c8STreehugger Robot }
758*7c3d14c8STreehugger Robot #endif // #if !SANITIZER_GO
759*7c3d14c8STreehugger Robot
Write(const char * buffer,uptr length)760*7c3d14c8STreehugger Robot void ReportFile::Write(const char *buffer, uptr length) {
761*7c3d14c8STreehugger Robot SpinMutexLock l(mu);
762*7c3d14c8STreehugger Robot ReopenIfNecessary();
763*7c3d14c8STreehugger Robot if (!WriteToFile(fd, buffer, length)) {
764*7c3d14c8STreehugger Robot // stderr may be closed, but we may be able to print to the debugger
765*7c3d14c8STreehugger Robot // instead. This is the case when launching a program from Visual Studio,
766*7c3d14c8STreehugger Robot // and the following routine should write to its console.
767*7c3d14c8STreehugger Robot OutputDebugStringA(buffer);
768*7c3d14c8STreehugger Robot }
769*7c3d14c8STreehugger Robot }
770*7c3d14c8STreehugger Robot
SetAlternateSignalStack()771*7c3d14c8STreehugger Robot void SetAlternateSignalStack() {
772*7c3d14c8STreehugger Robot // FIXME: Decide what to do on Windows.
773*7c3d14c8STreehugger Robot }
774*7c3d14c8STreehugger Robot
UnsetAlternateSignalStack()775*7c3d14c8STreehugger Robot void UnsetAlternateSignalStack() {
776*7c3d14c8STreehugger Robot // FIXME: Decide what to do on Windows.
777*7c3d14c8STreehugger Robot }
778*7c3d14c8STreehugger Robot
InstallDeadlySignalHandlers(SignalHandlerType handler)779*7c3d14c8STreehugger Robot void InstallDeadlySignalHandlers(SignalHandlerType handler) {
780*7c3d14c8STreehugger Robot (void)handler;
781*7c3d14c8STreehugger Robot // FIXME: Decide what to do on Windows.
782*7c3d14c8STreehugger Robot }
783*7c3d14c8STreehugger Robot
IsHandledDeadlySignal(int signum)784*7c3d14c8STreehugger Robot bool IsHandledDeadlySignal(int signum) {
785*7c3d14c8STreehugger Robot // FIXME: Decide what to do on Windows.
786*7c3d14c8STreehugger Robot return false;
787*7c3d14c8STreehugger Robot }
788*7c3d14c8STreehugger Robot
IsAccessibleMemoryRange(uptr beg,uptr size)789*7c3d14c8STreehugger Robot bool IsAccessibleMemoryRange(uptr beg, uptr size) {
790*7c3d14c8STreehugger Robot SYSTEM_INFO si;
791*7c3d14c8STreehugger Robot GetNativeSystemInfo(&si);
792*7c3d14c8STreehugger Robot uptr page_size = si.dwPageSize;
793*7c3d14c8STreehugger Robot uptr page_mask = ~(page_size - 1);
794*7c3d14c8STreehugger Robot
795*7c3d14c8STreehugger Robot for (uptr page = beg & page_mask, end = (beg + size - 1) & page_mask;
796*7c3d14c8STreehugger Robot page <= end;) {
797*7c3d14c8STreehugger Robot MEMORY_BASIC_INFORMATION info;
798*7c3d14c8STreehugger Robot if (VirtualQuery((LPCVOID)page, &info, sizeof(info)) != sizeof(info))
799*7c3d14c8STreehugger Robot return false;
800*7c3d14c8STreehugger Robot
801*7c3d14c8STreehugger Robot if (info.Protect == 0 || info.Protect == PAGE_NOACCESS ||
802*7c3d14c8STreehugger Robot info.Protect == PAGE_EXECUTE)
803*7c3d14c8STreehugger Robot return false;
804*7c3d14c8STreehugger Robot
805*7c3d14c8STreehugger Robot if (info.RegionSize == 0)
806*7c3d14c8STreehugger Robot return false;
807*7c3d14c8STreehugger Robot
808*7c3d14c8STreehugger Robot page += info.RegionSize;
809*7c3d14c8STreehugger Robot }
810*7c3d14c8STreehugger Robot
811*7c3d14c8STreehugger Robot return true;
812*7c3d14c8STreehugger Robot }
813*7c3d14c8STreehugger Robot
Create(void * siginfo,void * context)814*7c3d14c8STreehugger Robot SignalContext SignalContext::Create(void *siginfo, void *context) {
815*7c3d14c8STreehugger Robot EXCEPTION_RECORD *exception_record = (EXCEPTION_RECORD *)siginfo;
816*7c3d14c8STreehugger Robot CONTEXT *context_record = (CONTEXT *)context;
817*7c3d14c8STreehugger Robot
818*7c3d14c8STreehugger Robot uptr pc = (uptr)exception_record->ExceptionAddress;
819*7c3d14c8STreehugger Robot #ifdef _WIN64
820*7c3d14c8STreehugger Robot uptr bp = (uptr)context_record->Rbp;
821*7c3d14c8STreehugger Robot uptr sp = (uptr)context_record->Rsp;
822*7c3d14c8STreehugger Robot #else
823*7c3d14c8STreehugger Robot uptr bp = (uptr)context_record->Ebp;
824*7c3d14c8STreehugger Robot uptr sp = (uptr)context_record->Esp;
825*7c3d14c8STreehugger Robot #endif
826*7c3d14c8STreehugger Robot uptr access_addr = exception_record->ExceptionInformation[1];
827*7c3d14c8STreehugger Robot
828*7c3d14c8STreehugger Robot // The contents of this array are documented at
829*7c3d14c8STreehugger Robot // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363082(v=vs.85).aspx
830*7c3d14c8STreehugger Robot // The first element indicates read as 0, write as 1, or execute as 8. The
831*7c3d14c8STreehugger Robot // second element is the faulting address.
832*7c3d14c8STreehugger Robot WriteFlag write_flag = SignalContext::UNKNOWN;
833*7c3d14c8STreehugger Robot switch (exception_record->ExceptionInformation[0]) {
834*7c3d14c8STreehugger Robot case 0: write_flag = SignalContext::READ; break;
835*7c3d14c8STreehugger Robot case 1: write_flag = SignalContext::WRITE; break;
836*7c3d14c8STreehugger Robot case 8: write_flag = SignalContext::UNKNOWN; break;
837*7c3d14c8STreehugger Robot }
838*7c3d14c8STreehugger Robot bool is_memory_access = write_flag != SignalContext::UNKNOWN;
839*7c3d14c8STreehugger Robot return SignalContext(context, access_addr, pc, sp, bp, is_memory_access,
840*7c3d14c8STreehugger Robot write_flag);
841*7c3d14c8STreehugger Robot }
842*7c3d14c8STreehugger Robot
ReadBinaryName(char * buf,uptr buf_len)843*7c3d14c8STreehugger Robot uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
844*7c3d14c8STreehugger Robot // FIXME: Actually implement this function.
845*7c3d14c8STreehugger Robot CHECK_GT(buf_len, 0);
846*7c3d14c8STreehugger Robot buf[0] = 0;
847*7c3d14c8STreehugger Robot return 0;
848*7c3d14c8STreehugger Robot }
849*7c3d14c8STreehugger Robot
ReadLongProcessName(char * buf,uptr buf_len)850*7c3d14c8STreehugger Robot uptr ReadLongProcessName(/*out*/char *buf, uptr buf_len) {
851*7c3d14c8STreehugger Robot return ReadBinaryName(buf, buf_len);
852*7c3d14c8STreehugger Robot }
853*7c3d14c8STreehugger Robot
CheckVMASize()854*7c3d14c8STreehugger Robot void CheckVMASize() {
855*7c3d14c8STreehugger Robot // Do nothing.
856*7c3d14c8STreehugger Robot }
857*7c3d14c8STreehugger Robot
MaybeReexec()858*7c3d14c8STreehugger Robot void MaybeReexec() {
859*7c3d14c8STreehugger Robot // No need to re-exec on Windows.
860*7c3d14c8STreehugger Robot }
861*7c3d14c8STreehugger Robot
GetArgv()862*7c3d14c8STreehugger Robot char **GetArgv() {
863*7c3d14c8STreehugger Robot // FIXME: Actually implement this function.
864*7c3d14c8STreehugger Robot return 0;
865*7c3d14c8STreehugger Robot }
866*7c3d14c8STreehugger Robot
StartSubprocess(const char * program,const char * const argv[],fd_t stdin_fd,fd_t stdout_fd,fd_t stderr_fd)867*7c3d14c8STreehugger Robot pid_t StartSubprocess(const char *program, const char *const argv[],
868*7c3d14c8STreehugger Robot fd_t stdin_fd, fd_t stdout_fd, fd_t stderr_fd) {
869*7c3d14c8STreehugger Robot // FIXME: implement on this platform
870*7c3d14c8STreehugger Robot // Should be implemented based on
871*7c3d14c8STreehugger Robot // SymbolizerProcess::StarAtSymbolizerSubprocess
872*7c3d14c8STreehugger Robot // from lib/sanitizer_common/sanitizer_symbolizer_win.cc.
873*7c3d14c8STreehugger Robot return -1;
874*7c3d14c8STreehugger Robot }
875*7c3d14c8STreehugger Robot
IsProcessRunning(pid_t pid)876*7c3d14c8STreehugger Robot bool IsProcessRunning(pid_t pid) {
877*7c3d14c8STreehugger Robot // FIXME: implement on this platform.
878*7c3d14c8STreehugger Robot return false;
879*7c3d14c8STreehugger Robot }
880*7c3d14c8STreehugger Robot
WaitForProcess(pid_t pid)881*7c3d14c8STreehugger Robot int WaitForProcess(pid_t pid) { return -1; }
882*7c3d14c8STreehugger Robot
883*7c3d14c8STreehugger Robot } // namespace __sanitizer
884*7c3d14c8STreehugger Robot
885*7c3d14c8STreehugger Robot #endif // _WIN32
886