1 //===-- sanitizer_win.cc --------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries and implements windows-specific functions from
12 // sanitizer_libc.h.
13 //===----------------------------------------------------------------------===//
14
15 #include "sanitizer_platform.h"
16 #if SANITIZER_WINDOWS
17
18 #define WIN32_LEAN_AND_MEAN
19 #define NOGDI
20 #include <windows.h>
21 #include <dbghelp.h>
22 #include <io.h>
23 #include <psapi.h>
24 #include <stdlib.h>
25
26 #include "sanitizer_common.h"
27 #include "sanitizer_libc.h"
28 #include "sanitizer_mutex.h"
29 #include "sanitizer_placement_new.h"
30 #include "sanitizer_stacktrace.h"
31
32 namespace __sanitizer {
33
34 #include "sanitizer_syscall_generic.inc"
35
36 // --------------------- sanitizer_common.h
GetPageSize()37 uptr GetPageSize() {
38 // FIXME: there is an API for getting the system page size (GetSystemInfo or
39 // GetNativeSystemInfo), but if we use it here we get test failures elsewhere.
40 return 1U << 14;
41 }
42
GetMmapGranularity()43 uptr GetMmapGranularity() {
44 return 1U << 16; // FIXME: is this configurable?
45 }
46
GetMaxVirtualAddress()47 uptr GetMaxVirtualAddress() {
48 SYSTEM_INFO si;
49 GetSystemInfo(&si);
50 return (uptr)si.lpMaximumApplicationAddress;
51 }
52
FileExists(const char * filename)53 bool FileExists(const char *filename) {
54 UNIMPLEMENTED();
55 }
56
internal_getpid()57 uptr internal_getpid() {
58 return GetProcessId(GetCurrentProcess());
59 }
60
61 // In contrast to POSIX, on Windows GetCurrentThreadId()
62 // returns a system-unique identifier.
GetTid()63 uptr GetTid() {
64 return GetCurrentThreadId();
65 }
66
GetThreadSelf()67 uptr GetThreadSelf() {
68 return GetTid();
69 }
70
71 #if !SANITIZER_GO
GetThreadStackTopAndBottom(bool at_initialization,uptr * stack_top,uptr * stack_bottom)72 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
73 uptr *stack_bottom) {
74 CHECK(stack_top);
75 CHECK(stack_bottom);
76 MEMORY_BASIC_INFORMATION mbi;
77 CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
78 // FIXME: is it possible for the stack to not be a single allocation?
79 // Are these values what ASan expects to get (reserved, not committed;
80 // including stack guard page) ?
81 *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
82 *stack_bottom = (uptr)mbi.AllocationBase;
83 }
84 #endif // #if !SANITIZER_GO
85
MmapOrDie(uptr size,const char * mem_type)86 void *MmapOrDie(uptr size, const char *mem_type) {
87 void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
88 if (rv == 0) {
89 Report("ERROR: %s failed to "
90 "allocate 0x%zx (%zd) bytes of %s (error code: %d)\n",
91 SanitizerToolName, size, size, mem_type, GetLastError());
92 CHECK("unable to mmap" && 0);
93 }
94 return rv;
95 }
96
UnmapOrDie(void * addr,uptr size)97 void UnmapOrDie(void *addr, uptr size) {
98 if (!size || !addr)
99 return;
100
101 if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
102 Report("ERROR: %s failed to "
103 "deallocate 0x%zx (%zd) bytes at address %p (error code: %d)\n",
104 SanitizerToolName, size, size, addr, GetLastError());
105 CHECK("unable to unmap" && 0);
106 }
107 }
108
MmapFixedNoReserve(uptr fixed_addr,uptr size,const char * name)109 void *MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) {
110 // FIXME: is this really "NoReserve"? On Win32 this does not matter much,
111 // but on Win64 it does.
112 (void)name; // unsupported
113 void *p = VirtualAlloc((LPVOID)fixed_addr, size,
114 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
115 if (p == 0)
116 Report("ERROR: %s failed to "
117 "allocate %p (%zd) bytes at %p (error code: %d)\n",
118 SanitizerToolName, size, size, fixed_addr, GetLastError());
119 return p;
120 }
121
MmapFixedOrDie(uptr fixed_addr,uptr size)122 void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
123 return MmapFixedNoReserve(fixed_addr, size);
124 }
125
MmapNoReserveOrDie(uptr size,const char * mem_type)126 void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
127 // FIXME: make this really NoReserve?
128 return MmapOrDie(size, mem_type);
129 }
130
MmapNoAccess(uptr fixed_addr,uptr size,const char * name)131 void *MmapNoAccess(uptr fixed_addr, uptr size, const char *name) {
132 (void)name; // unsupported
133 void *res = VirtualAlloc((LPVOID)fixed_addr, size,
134 MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
135 if (res == 0)
136 Report("WARNING: %s failed to "
137 "mprotect %p (%zd) bytes at %p (error code: %d)\n",
138 SanitizerToolName, size, size, fixed_addr, GetLastError());
139 return res;
140 }
141
MprotectNoAccess(uptr addr,uptr size)142 bool MprotectNoAccess(uptr addr, uptr size) {
143 DWORD old_protection;
144 return VirtualProtect((LPVOID)addr, size, PAGE_NOACCESS, &old_protection);
145 }
146
147
FlushUnneededShadowMemory(uptr addr,uptr size)148 void FlushUnneededShadowMemory(uptr addr, uptr size) {
149 // This is almost useless on 32-bits.
150 // FIXME: add madvise-analog when we move to 64-bits.
151 }
152
NoHugePagesInRegion(uptr addr,uptr size)153 void NoHugePagesInRegion(uptr addr, uptr size) {
154 // FIXME: probably similar to FlushUnneededShadowMemory.
155 }
156
DontDumpShadowMemory(uptr addr,uptr length)157 void DontDumpShadowMemory(uptr addr, uptr length) {
158 // This is almost useless on 32-bits.
159 // FIXME: add madvise-analog when we move to 64-bits.
160 }
161
MemoryRangeIsAvailable(uptr range_start,uptr range_end)162 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
163 MEMORY_BASIC_INFORMATION mbi;
164 CHECK(VirtualQuery((void *)range_start, &mbi, sizeof(mbi)));
165 return mbi.Protect == PAGE_NOACCESS &&
166 (uptr)mbi.BaseAddress + mbi.RegionSize >= range_end;
167 }
168
MapFileToMemory(const char * file_name,uptr * buff_size)169 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
170 UNIMPLEMENTED();
171 }
172
MapWritableFileToMemory(void * addr,uptr size,fd_t fd,OFF_T offset)173 void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset) {
174 UNIMPLEMENTED();
175 }
176
177 static const int kMaxEnvNameLength = 128;
178 static const DWORD kMaxEnvValueLength = 32767;
179
180 namespace {
181
182 struct EnvVariable {
183 char name[kMaxEnvNameLength];
184 char value[kMaxEnvValueLength];
185 };
186
187 } // namespace
188
189 static const int kEnvVariables = 5;
190 static EnvVariable env_vars[kEnvVariables];
191 static int num_env_vars;
192
GetEnv(const char * name)193 const char *GetEnv(const char *name) {
194 // Note: this implementation caches the values of the environment variables
195 // and limits their quantity.
196 for (int i = 0; i < num_env_vars; i++) {
197 if (0 == internal_strcmp(name, env_vars[i].name))
198 return env_vars[i].value;
199 }
200 CHECK_LT(num_env_vars, kEnvVariables);
201 DWORD rv = GetEnvironmentVariableA(name, env_vars[num_env_vars].value,
202 kMaxEnvValueLength);
203 if (rv > 0 && rv < kMaxEnvValueLength) {
204 CHECK_LT(internal_strlen(name), kMaxEnvNameLength);
205 internal_strncpy(env_vars[num_env_vars].name, name, kMaxEnvNameLength);
206 num_env_vars++;
207 return env_vars[num_env_vars - 1].value;
208 }
209 return 0;
210 }
211
GetPwd()212 const char *GetPwd() {
213 UNIMPLEMENTED();
214 }
215
GetUid()216 u32 GetUid() {
217 UNIMPLEMENTED();
218 }
219
220 namespace {
221 struct ModuleInfo {
222 const char *filepath;
223 uptr base_address;
224 uptr end_address;
225 };
226
CompareModulesBase(const void * pl,const void * pr)227 int CompareModulesBase(const void *pl, const void *pr) {
228 const ModuleInfo *l = (ModuleInfo *)pl, *r = (ModuleInfo *)pr;
229 if (l->base_address < r->base_address)
230 return -1;
231 return l->base_address > r->base_address;
232 }
233 } // namespace
234
235 #ifndef SANITIZER_GO
DumpProcessMap()236 void DumpProcessMap() {
237 Report("Dumping process modules:\n");
238 InternalScopedBuffer<LoadedModule> modules(kMaxNumberOfModules);
239 uptr num_modules =
240 GetListOfModules(modules.data(), kMaxNumberOfModules, nullptr);
241
242 InternalScopedBuffer<ModuleInfo> module_infos(num_modules);
243 for (size_t i = 0; i < num_modules; ++i) {
244 module_infos[i].filepath = modules[i].full_name();
245 module_infos[i].base_address = modules[i].base_address();
246 module_infos[i].end_address = modules[i].ranges().next()->end;
247 }
248 qsort(module_infos.data(), num_modules, sizeof(ModuleInfo),
249 CompareModulesBase);
250
251 for (size_t i = 0; i < num_modules; ++i) {
252 const ModuleInfo &mi = module_infos[i];
253 if (mi.end_address != 0) {
254 Printf("\t%p-%p %s\n", mi.base_address, mi.end_address,
255 mi.filepath[0] ? mi.filepath : "[no name]");
256 } else if (mi.filepath[0]) {
257 Printf("\t??\?-??? %s\n", mi.filepath);
258 } else {
259 Printf("\t???\n");
260 }
261 }
262 }
263 #endif
264
DisableCoreDumperIfNecessary()265 void DisableCoreDumperIfNecessary() {
266 // Do nothing.
267 }
268
ReExec()269 void ReExec() {
270 UNIMPLEMENTED();
271 }
272
PrepareForSandboxing(__sanitizer_sandbox_arguments * args)273 void PrepareForSandboxing(__sanitizer_sandbox_arguments *args) {
274 #if !SANITIZER_GO
275 CovPrepareForSandboxing(args);
276 #endif
277 }
278
StackSizeIsUnlimited()279 bool StackSizeIsUnlimited() {
280 UNIMPLEMENTED();
281 }
282
SetStackSizeLimitInBytes(uptr limit)283 void SetStackSizeLimitInBytes(uptr limit) {
284 UNIMPLEMENTED();
285 }
286
AddressSpaceIsUnlimited()287 bool AddressSpaceIsUnlimited() {
288 UNIMPLEMENTED();
289 }
290
SetAddressSpaceUnlimited()291 void SetAddressSpaceUnlimited() {
292 UNIMPLEMENTED();
293 }
294
FindPathToBinary(const char * name)295 char *FindPathToBinary(const char *name) {
296 // Nothing here for now.
297 return 0;
298 }
299
IsPathSeparator(const char c)300 bool IsPathSeparator(const char c) {
301 return c == '\\' || c == '/';
302 }
303
IsAbsolutePath(const char * path)304 bool IsAbsolutePath(const char *path) {
305 UNIMPLEMENTED();
306 }
307
SleepForSeconds(int seconds)308 void SleepForSeconds(int seconds) {
309 Sleep(seconds * 1000);
310 }
311
SleepForMillis(int millis)312 void SleepForMillis(int millis) {
313 Sleep(millis);
314 }
315
NanoTime()316 u64 NanoTime() {
317 return 0;
318 }
319
Abort()320 void Abort() {
321 if (::IsDebuggerPresent())
322 __debugbreak();
323 internal__exit(3);
324 }
325
GetListOfModules(LoadedModule * modules,uptr max_modules,string_predicate_t filter)326 uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
327 string_predicate_t filter) {
328 HANDLE cur_process = GetCurrentProcess();
329
330 // Query the list of modules. Start by assuming there are no more than 256
331 // modules and retry if that's not sufficient.
332 HMODULE *hmodules = 0;
333 uptr modules_buffer_size = sizeof(HMODULE) * 256;
334 DWORD bytes_required;
335 while (!hmodules) {
336 hmodules = (HMODULE *)MmapOrDie(modules_buffer_size, __FUNCTION__);
337 CHECK(EnumProcessModules(cur_process, hmodules, modules_buffer_size,
338 &bytes_required));
339 if (bytes_required > modules_buffer_size) {
340 // Either there turned out to be more than 256 hmodules, or new hmodules
341 // could have loaded since the last try. Retry.
342 UnmapOrDie(hmodules, modules_buffer_size);
343 hmodules = 0;
344 modules_buffer_size = bytes_required;
345 }
346 }
347
348 // |num_modules| is the number of modules actually present,
349 // |count| is the number of modules we return.
350 size_t nun_modules = bytes_required / sizeof(HMODULE),
351 count = 0;
352 for (size_t i = 0; i < nun_modules && count < max_modules; ++i) {
353 HMODULE handle = hmodules[i];
354 MODULEINFO mi;
355 if (!GetModuleInformation(cur_process, handle, &mi, sizeof(mi)))
356 continue;
357
358 char module_name[MAX_PATH];
359 bool got_module_name =
360 GetModuleFileNameA(handle, module_name, sizeof(module_name));
361 if (!got_module_name)
362 module_name[0] = '\0';
363
364 if (filter && !filter(module_name))
365 continue;
366
367 uptr base_address = (uptr)mi.lpBaseOfDll;
368 uptr end_address = (uptr)mi.lpBaseOfDll + mi.SizeOfImage;
369 LoadedModule *cur_module = &modules[count];
370 cur_module->set(module_name, base_address);
371 // We add the whole module as one single address range.
372 cur_module->addAddressRange(base_address, end_address, /*executable*/ true);
373 count++;
374 }
375 UnmapOrDie(hmodules, modules_buffer_size);
376
377 return count;
378 };
379
380 #ifndef SANITIZER_GO
381 // We can't use atexit() directly at __asan_init time as the CRT is not fully
382 // initialized at this point. Place the functions into a vector and use
383 // atexit() as soon as it is ready for use (i.e. after .CRT$XIC initializers).
384 InternalMmapVectorNoCtor<void (*)(void)> atexit_functions;
385
Atexit(void (* function)(void))386 int Atexit(void (*function)(void)) {
387 atexit_functions.push_back(function);
388 return 0;
389 }
390
RunAtexit()391 static int RunAtexit() {
392 int ret = 0;
393 for (uptr i = 0; i < atexit_functions.size(); ++i) {
394 ret |= atexit(atexit_functions[i]);
395 }
396 return ret;
397 }
398
399 #pragma section(".CRT$XID", long, read) // NOLINT
400 static __declspec(allocate(".CRT$XID")) int (*__run_atexit)() = RunAtexit;
401 #endif
402
403 // ------------------ sanitizer_libc.h
OpenFile(const char * filename,FileAccessMode mode,error_t * last_error)404 fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *last_error) {
405 if (mode != WrOnly)
406 UNIMPLEMENTED();
407 fd_t res = CreateFile(filename, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS,
408 FILE_ATTRIBUTE_NORMAL, nullptr);
409 CHECK(res != kStdoutFd || kStdoutFd == kInvalidFd);
410 CHECK(res != kStderrFd || kStderrFd == kInvalidFd);
411 if (res == kInvalidFd && last_error)
412 *last_error = GetLastError();
413 return res;
414 }
415
CloseFile(fd_t fd)416 void CloseFile(fd_t fd) {
417 CloseHandle(fd);
418 }
419
ReadFromFile(fd_t fd,void * buff,uptr buff_size,uptr * bytes_read,error_t * error_p)420 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
421 error_t *error_p) {
422 UNIMPLEMENTED();
423 }
424
SupportsColoredOutput(fd_t fd)425 bool SupportsColoredOutput(fd_t fd) {
426 // FIXME: support colored output.
427 return false;
428 }
429
WriteToFile(fd_t fd,const void * buff,uptr buff_size,uptr * bytes_written,error_t * error_p)430 bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
431 error_t *error_p) {
432 CHECK(fd != kInvalidFd);
433
434 if (fd == kStdoutFd) {
435 fd = GetStdHandle(STD_OUTPUT_HANDLE);
436 if (fd == 0) fd = kInvalidFd;
437 } else if (fd == kStderrFd) {
438 fd = GetStdHandle(STD_ERROR_HANDLE);
439 if (fd == 0) fd = kInvalidFd;
440 }
441
442 DWORD internal_bytes_written;
443 if (fd == kInvalidFd ||
444 WriteFile(fd, buff, buff_size, &internal_bytes_written, 0)) {
445 if (error_p) *error_p = GetLastError();
446 return false;
447 } else {
448 if (bytes_written) *bytes_written = internal_bytes_written;
449 return true;
450 }
451 }
452
RenameFile(const char * oldpath,const char * newpath,error_t * error_p)453 bool RenameFile(const char *oldpath, const char *newpath, error_t *error_p) {
454 UNIMPLEMENTED();
455 }
456
internal_sched_yield()457 uptr internal_sched_yield() {
458 Sleep(0);
459 return 0;
460 }
461
internal__exit(int exitcode)462 void internal__exit(int exitcode) {
463 ExitProcess(exitcode);
464 }
465
internal_ftruncate(fd_t fd,uptr size)466 uptr internal_ftruncate(fd_t fd, uptr size) {
467 UNIMPLEMENTED();
468 }
469
GetRSS()470 uptr GetRSS() {
471 return 0;
472 }
473
internal_start_thread(void (* func)(void * arg),void * arg)474 void *internal_start_thread(void (*func)(void *arg), void *arg) { return 0; }
internal_join_thread(void * th)475 void internal_join_thread(void *th) { }
476
477 // ---------------------- BlockingMutex ---------------- {{{1
478 const uptr LOCK_UNINITIALIZED = 0;
479 const uptr LOCK_READY = (uptr)-1;
480
BlockingMutex(LinkerInitialized li)481 BlockingMutex::BlockingMutex(LinkerInitialized li) {
482 // FIXME: see comments in BlockingMutex::Lock() for the details.
483 CHECK(li == LINKER_INITIALIZED || owner_ == LOCK_UNINITIALIZED);
484
485 CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
486 InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
487 owner_ = LOCK_READY;
488 }
489
BlockingMutex()490 BlockingMutex::BlockingMutex() {
491 CHECK(sizeof(CRITICAL_SECTION) <= sizeof(opaque_storage_));
492 InitializeCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
493 owner_ = LOCK_READY;
494 }
495
Lock()496 void BlockingMutex::Lock() {
497 if (owner_ == LOCK_UNINITIALIZED) {
498 // FIXME: hm, global BlockingMutex objects are not initialized?!?
499 // This might be a side effect of the clang+cl+link Frankenbuild...
500 new(this) BlockingMutex((LinkerInitialized)(LINKER_INITIALIZED + 1));
501
502 // FIXME: If it turns out the linker doesn't invoke our
503 // constructors, we should probably manually Lock/Unlock all the global
504 // locks while we're starting in one thread to avoid double-init races.
505 }
506 EnterCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
507 CHECK_EQ(owner_, LOCK_READY);
508 owner_ = GetThreadSelf();
509 }
510
Unlock()511 void BlockingMutex::Unlock() {
512 CHECK_EQ(owner_, GetThreadSelf());
513 owner_ = LOCK_READY;
514 LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
515 }
516
CheckLocked()517 void BlockingMutex::CheckLocked() {
518 CHECK_EQ(owner_, GetThreadSelf());
519 }
520
GetTlsSize()521 uptr GetTlsSize() {
522 return 0;
523 }
524
InitTlsSize()525 void InitTlsSize() {
526 }
527
GetThreadStackAndTls(bool main,uptr * stk_addr,uptr * stk_size,uptr * tls_addr,uptr * tls_size)528 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
529 uptr *tls_addr, uptr *tls_size) {
530 #ifdef SANITIZER_GO
531 *stk_addr = 0;
532 *stk_size = 0;
533 *tls_addr = 0;
534 *tls_size = 0;
535 #else
536 uptr stack_top, stack_bottom;
537 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
538 *stk_addr = stack_bottom;
539 *stk_size = stack_top - stack_bottom;
540 *tls_addr = 0;
541 *tls_size = 0;
542 #endif
543 }
544
545 #if !SANITIZER_GO
SlowUnwindStack(uptr pc,u32 max_depth)546 void BufferedStackTrace::SlowUnwindStack(uptr pc, u32 max_depth) {
547 CHECK_GE(max_depth, 2);
548 // FIXME: CaptureStackBackTrace might be too slow for us.
549 // FIXME: Compare with StackWalk64.
550 // FIXME: Look at LLVMUnhandledExceptionFilter in Signals.inc
551 size = CaptureStackBackTrace(2, Min(max_depth, kStackTraceMax),
552 (void**)trace, 0);
553 if (size == 0)
554 return;
555
556 // Skip the RTL frames by searching for the PC in the stacktrace.
557 uptr pc_location = LocatePcInTrace(pc);
558 PopStackFrames(pc_location);
559 }
560
SlowUnwindStackWithContext(uptr pc,void * context,u32 max_depth)561 void BufferedStackTrace::SlowUnwindStackWithContext(uptr pc, void *context,
562 u32 max_depth) {
563 CONTEXT ctx = *(CONTEXT *)context;
564 STACKFRAME64 stack_frame;
565 memset(&stack_frame, 0, sizeof(stack_frame));
566 size = 0;
567 #if defined(_WIN64)
568 int machine_type = IMAGE_FILE_MACHINE_AMD64;
569 stack_frame.AddrPC.Offset = ctx.Rip;
570 stack_frame.AddrFrame.Offset = ctx.Rbp;
571 stack_frame.AddrStack.Offset = ctx.Rsp;
572 #else
573 int machine_type = IMAGE_FILE_MACHINE_I386;
574 stack_frame.AddrPC.Offset = ctx.Eip;
575 stack_frame.AddrFrame.Offset = ctx.Ebp;
576 stack_frame.AddrStack.Offset = ctx.Esp;
577 #endif
578 stack_frame.AddrPC.Mode = AddrModeFlat;
579 stack_frame.AddrFrame.Mode = AddrModeFlat;
580 stack_frame.AddrStack.Mode = AddrModeFlat;
581 while (StackWalk64(machine_type, GetCurrentProcess(), GetCurrentThread(),
582 &stack_frame, &ctx, NULL, &SymFunctionTableAccess64,
583 &SymGetModuleBase64, NULL) &&
584 size < Min(max_depth, kStackTraceMax)) {
585 trace_buffer[size++] = (uptr)stack_frame.AddrPC.Offset;
586 }
587 }
588 #endif // #if !SANITIZER_GO
589
Write(const char * buffer,uptr length)590 void ReportFile::Write(const char *buffer, uptr length) {
591 SpinMutexLock l(mu);
592 ReopenIfNecessary();
593 if (!WriteToFile(fd, buffer, length)) {
594 // stderr may be closed, but we may be able to print to the debugger
595 // instead. This is the case when launching a program from Visual Studio,
596 // and the following routine should write to its console.
597 OutputDebugStringA(buffer);
598 }
599 }
600
SetAlternateSignalStack()601 void SetAlternateSignalStack() {
602 // FIXME: Decide what to do on Windows.
603 }
604
UnsetAlternateSignalStack()605 void UnsetAlternateSignalStack() {
606 // FIXME: Decide what to do on Windows.
607 }
608
InstallDeadlySignalHandlers(SignalHandlerType handler)609 void InstallDeadlySignalHandlers(SignalHandlerType handler) {
610 (void)handler;
611 // FIXME: Decide what to do on Windows.
612 }
613
IsDeadlySignal(int signum)614 bool IsDeadlySignal(int signum) {
615 // FIXME: Decide what to do on Windows.
616 return false;
617 }
618
IsAccessibleMemoryRange(uptr beg,uptr size)619 bool IsAccessibleMemoryRange(uptr beg, uptr size) {
620 SYSTEM_INFO si;
621 GetNativeSystemInfo(&si);
622 uptr page_size = si.dwPageSize;
623 uptr page_mask = ~(page_size - 1);
624
625 for (uptr page = beg & page_mask, end = (beg + size - 1) & page_mask;
626 page <= end;) {
627 MEMORY_BASIC_INFORMATION info;
628 if (VirtualQuery((LPCVOID)page, &info, sizeof(info)) != sizeof(info))
629 return false;
630
631 if (info.Protect == 0 || info.Protect == PAGE_NOACCESS ||
632 info.Protect == PAGE_EXECUTE)
633 return false;
634
635 if (info.RegionSize == 0)
636 return false;
637
638 page += info.RegionSize;
639 }
640
641 return true;
642 }
643
Create(void * siginfo,void * context)644 SignalContext SignalContext::Create(void *siginfo, void *context) {
645 EXCEPTION_RECORD *exception_record = (EXCEPTION_RECORD*)siginfo;
646 CONTEXT *context_record = (CONTEXT*)context;
647
648 uptr pc = (uptr)exception_record->ExceptionAddress;
649 #ifdef _WIN64
650 uptr bp = (uptr)context_record->Rbp;
651 uptr sp = (uptr)context_record->Rsp;
652 #else
653 uptr bp = (uptr)context_record->Ebp;
654 uptr sp = (uptr)context_record->Esp;
655 #endif
656 uptr access_addr = exception_record->ExceptionInformation[1];
657
658 return SignalContext(context, access_addr, pc, sp, bp);
659 }
660
ReadBinaryName(char * buf,uptr buf_len)661 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
662 // FIXME: Actually implement this function.
663 CHECK_GT(buf_len, 0);
664 buf[0] = 0;
665 return 0;
666 }
667
668 } // namespace __sanitizer
669
670 #endif // _WIN32
671