sanitizer_win.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. //===-- sanitizer_win.cpp -------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is shared between AddressSanitizer and ThreadSanitizer
  10. // run-time libraries and implements windows-specific functions from
  11. // sanitizer_libc.h.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_platform.h"
  14. #if SANITIZER_WINDOWS
  15. #define WIN32_LEAN_AND_MEAN
  16. #define NOGDI
  17. #include <windows.h>
  18. #include <io.h>
  19. #include <psapi.h>
  20. #include <stdlib.h>
  21. #include "sanitizer_common.h"
  22. #include "sanitizer_file.h"
  23. #include "sanitizer_libc.h"
  24. #include "sanitizer_mutex.h"
  25. #include "sanitizer_placement_new.h"
  26. #include "sanitizer_win_defs.h"
  27. #if defined(PSAPI_VERSION) && PSAPI_VERSION == 1
  28. #pragma comment(lib, "psapi")
  29. #endif
  30. #if SANITIZER_WIN_TRACE
  31. #error #include <traceloggingprovider.h>
  32. // Windows trace logging provider init
  33. #pragma comment(lib, "advapi32.lib")
  34. TRACELOGGING_DECLARE_PROVIDER(g_asan_provider);
  35. // GUID must be the same in utils/AddressSanitizerLoggingProvider.wprp
  36. TRACELOGGING_DEFINE_PROVIDER(g_asan_provider, "AddressSanitizerLoggingProvider",
  37. (0x6c6c766d, 0x3846, 0x4e6a, 0xa4, 0xfb, 0x5b,
  38. 0x53, 0x0b, 0xd0, 0xf3, 0xfa));
  39. #else
  40. #define TraceLoggingUnregister(x)
  41. #endif
  42. // For WaitOnAddress
  43. # pragma comment(lib, "synchronization.lib")
  44. // A macro to tell the compiler that this part of the code cannot be reached,
  45. // if the compiler supports this feature. Since we're using this in
  46. // code that is called when terminating the process, the expansion of the
  47. // macro should not terminate the process to avoid infinite recursion.
  48. #if defined(__clang__)
  49. # define BUILTIN_UNREACHABLE() __builtin_unreachable()
  50. #elif defined(__GNUC__) && \
  51. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
  52. # define BUILTIN_UNREACHABLE() __builtin_unreachable()
  53. #elif defined(_MSC_VER)
  54. # define BUILTIN_UNREACHABLE() __assume(0)
  55. #else
  56. # define BUILTIN_UNREACHABLE()
  57. #endif
  58. namespace __sanitizer {
  59. #include "sanitizer_syscall_generic.inc"
  60. // --------------------- sanitizer_common.h
  61. uptr GetPageSize() {
  62. SYSTEM_INFO si;
  63. GetSystemInfo(&si);
  64. return si.dwPageSize;
  65. }
  66. uptr GetMmapGranularity() {
  67. SYSTEM_INFO si;
  68. GetSystemInfo(&si);
  69. return si.dwAllocationGranularity;
  70. }
  71. uptr GetMaxUserVirtualAddress() {
  72. SYSTEM_INFO si;
  73. GetSystemInfo(&si);
  74. return (uptr)si.lpMaximumApplicationAddress;
  75. }
  76. uptr GetMaxVirtualAddress() {
  77. return GetMaxUserVirtualAddress();
  78. }
  79. bool FileExists(const char *filename) {
  80. return ::GetFileAttributesA(filename) != INVALID_FILE_ATTRIBUTES;
  81. }
  82. bool DirExists(const char *path) {
  83. auto attr = ::GetFileAttributesA(path);
  84. return (attr != INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY);
  85. }
  86. uptr internal_getpid() {
  87. return GetProcessId(GetCurrentProcess());
  88. }
  89. int internal_dlinfo(void *handle, int request, void *p) {
  90. UNIMPLEMENTED();
  91. }
  92. // In contrast to POSIX, on Windows GetCurrentThreadId()
  93. // returns a system-unique identifier.
  94. tid_t GetTid() {
  95. return GetCurrentThreadId();
  96. }
  97. uptr GetThreadSelf() {
  98. return GetTid();
  99. }
  100. #if !SANITIZER_GO
  101. void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
  102. uptr *stack_bottom) {
  103. CHECK(stack_top);
  104. CHECK(stack_bottom);
  105. MEMORY_BASIC_INFORMATION mbi;
  106. CHECK_NE(VirtualQuery(&mbi /* on stack */, &mbi, sizeof(mbi)), 0);
  107. // FIXME: is it possible for the stack to not be a single allocation?
  108. // Are these values what ASan expects to get (reserved, not committed;
  109. // including stack guard page) ?
  110. *stack_top = (uptr)mbi.BaseAddress + mbi.RegionSize;
  111. *stack_bottom = (uptr)mbi.AllocationBase;
  112. }
  113. #endif // #if !SANITIZER_GO
  114. bool ErrorIsOOM(error_t err) {
  115. // TODO: This should check which `err`s correspond to OOM.
  116. return false;
  117. }
  118. void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {
  119. void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  120. if (rv == 0)
  121. ReportMmapFailureAndDie(size, mem_type, "allocate",
  122. GetLastError(), raw_report);
  123. return rv;
  124. }
  125. void UnmapOrDie(void *addr, uptr size) {
  126. if (!size || !addr)
  127. return;
  128. MEMORY_BASIC_INFORMATION mbi;
  129. CHECK(VirtualQuery(addr, &mbi, sizeof(mbi)));
  130. // MEM_RELEASE can only be used to unmap whole regions previously mapped with
  131. // VirtualAlloc. So we first try MEM_RELEASE since it is better, and if that
  132. // fails try MEM_DECOMMIT.
  133. if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
  134. if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
  135. Report("ERROR: %s failed to "
  136. "deallocate 0x%zx (%zd) bytes at address %p (error code: %d)\n",
  137. SanitizerToolName, size, size, addr, GetLastError());
  138. CHECK("unable to unmap" && 0);
  139. }
  140. }
  141. }
  142. static void *ReturnNullptrOnOOMOrDie(uptr size, const char *mem_type,
  143. const char *mmap_type) {
  144. error_t last_error = GetLastError();
  145. if (last_error == ERROR_NOT_ENOUGH_MEMORY)
  146. return nullptr;
  147. ReportMmapFailureAndDie(size, mem_type, mmap_type, last_error);
  148. }
  149. void *MmapOrDieOnFatalError(uptr size, const char *mem_type) {
  150. void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  151. if (rv == 0)
  152. return ReturnNullptrOnOOMOrDie(size, mem_type, "allocate");
  153. return rv;
  154. }
  155. // We want to map a chunk of address space aligned to 'alignment'.
  156. void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
  157. const char *mem_type) {
  158. CHECK(IsPowerOfTwo(size));
  159. CHECK(IsPowerOfTwo(alignment));
  160. // Windows will align our allocations to at least 64K.
  161. alignment = Max(alignment, GetMmapGranularity());
  162. uptr mapped_addr =
  163. (uptr)VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  164. if (!mapped_addr)
  165. return ReturnNullptrOnOOMOrDie(size, mem_type, "allocate aligned");
  166. // If we got it right on the first try, return. Otherwise, unmap it and go to
  167. // the slow path.
  168. if (IsAligned(mapped_addr, alignment))
  169. return (void*)mapped_addr;
  170. if (VirtualFree((void *)mapped_addr, 0, MEM_RELEASE) == 0)
  171. ReportMmapFailureAndDie(size, mem_type, "deallocate", GetLastError());
  172. // If we didn't get an aligned address, overallocate, find an aligned address,
  173. // unmap, and try to allocate at that aligned address.
  174. int retries = 0;
  175. const int kMaxRetries = 10;
  176. for (; retries < kMaxRetries &&
  177. (mapped_addr == 0 || !IsAligned(mapped_addr, alignment));
  178. retries++) {
  179. // Overallocate size + alignment bytes.
  180. mapped_addr =
  181. (uptr)VirtualAlloc(0, size + alignment, MEM_RESERVE, PAGE_NOACCESS);
  182. if (!mapped_addr)
  183. return ReturnNullptrOnOOMOrDie(size, mem_type, "allocate aligned");
  184. // Find the aligned address.
  185. uptr aligned_addr = RoundUpTo(mapped_addr, alignment);
  186. // Free the overallocation.
  187. if (VirtualFree((void *)mapped_addr, 0, MEM_RELEASE) == 0)
  188. ReportMmapFailureAndDie(size, mem_type, "deallocate", GetLastError());
  189. // Attempt to allocate exactly the number of bytes we need at the aligned
  190. // address. This may fail for a number of reasons, in which case we continue
  191. // the loop.
  192. mapped_addr = (uptr)VirtualAlloc((void *)aligned_addr, size,
  193. MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  194. }
  195. // Fail if we can't make this work quickly.
  196. if (retries == kMaxRetries && mapped_addr == 0)
  197. return ReturnNullptrOnOOMOrDie(size, mem_type, "allocate aligned");
  198. return (void *)mapped_addr;
  199. }
  200. // ZeroMmapFixedRegion zero's out a region of memory previously returned from a
  201. // call to one of the MmapFixed* helpers. On non-windows systems this would be
  202. // done with another mmap, but on windows remapping is not an option.
  203. // VirtualFree(DECOMMIT)+VirtualAlloc(RECOMMIT) would also be a way to zero the
  204. // memory, but we can't do this atomically, so instead we fall back to using
  205. // internal_memset.
  206. bool ZeroMmapFixedRegion(uptr fixed_addr, uptr size) {
  207. internal_memset((void*) fixed_addr, 0, size);
  208. return true;
  209. }
  210. bool MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) {
  211. // FIXME: is this really "NoReserve"? On Win32 this does not matter much,
  212. // but on Win64 it does.
  213. (void)name; // unsupported
  214. #if !SANITIZER_GO && SANITIZER_WINDOWS64
  215. // On asan/Windows64, use MEM_COMMIT would result in error
  216. // 1455:ERROR_COMMITMENT_LIMIT.
  217. // Asan uses exception handler to commit page on demand.
  218. void *p = VirtualAlloc((LPVOID)fixed_addr, size, MEM_RESERVE, PAGE_READWRITE);
  219. #else
  220. void *p = VirtualAlloc((LPVOID)fixed_addr, size, MEM_RESERVE | MEM_COMMIT,
  221. PAGE_READWRITE);
  222. #endif
  223. if (p == 0) {
  224. Report("ERROR: %s failed to "
  225. "allocate %p (%zd) bytes at %p (error code: %d)\n",
  226. SanitizerToolName, size, size, fixed_addr, GetLastError());
  227. return false;
  228. }
  229. return true;
  230. }
  231. bool MmapFixedSuperNoReserve(uptr fixed_addr, uptr size, const char *name) {
  232. // FIXME: Windows support large pages too. Might be worth checking
  233. return MmapFixedNoReserve(fixed_addr, size, name);
  234. }
  235. // Memory space mapped by 'MmapFixedOrDie' must have been reserved by
  236. // 'MmapFixedNoAccess'.
  237. void *MmapFixedOrDie(uptr fixed_addr, uptr size, const char *name) {
  238. void *p = VirtualAlloc((LPVOID)fixed_addr, size,
  239. MEM_COMMIT, PAGE_READWRITE);
  240. if (p == 0) {
  241. char mem_type[30];
  242. internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
  243. fixed_addr);
  244. ReportMmapFailureAndDie(size, mem_type, "allocate", GetLastError());
  245. }
  246. return p;
  247. }
  248. // Uses fixed_addr for now.
  249. // Will use offset instead once we've implemented this function for real.
  250. uptr ReservedAddressRange::Map(uptr fixed_addr, uptr size, const char *name) {
  251. return reinterpret_cast<uptr>(MmapFixedOrDieOnFatalError(fixed_addr, size));
  252. }
  253. uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr size,
  254. const char *name) {
  255. return reinterpret_cast<uptr>(MmapFixedOrDie(fixed_addr, size));
  256. }
  257. void ReservedAddressRange::Unmap(uptr addr, uptr size) {
  258. // Only unmap if it covers the entire range.
  259. CHECK((addr == reinterpret_cast<uptr>(base_)) && (size == size_));
  260. // We unmap the whole range, just null out the base.
  261. base_ = nullptr;
  262. size_ = 0;
  263. UnmapOrDie(reinterpret_cast<void*>(addr), size);
  264. }
  265. void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size, const char *name) {
  266. void *p = VirtualAlloc((LPVOID)fixed_addr, size,
  267. MEM_COMMIT, PAGE_READWRITE);
  268. if (p == 0) {
  269. char mem_type[30];
  270. internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
  271. fixed_addr);
  272. return ReturnNullptrOnOOMOrDie(size, mem_type, "allocate");
  273. }
  274. return p;
  275. }
  276. void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
  277. // FIXME: make this really NoReserve?
  278. return MmapOrDie(size, mem_type);
  279. }
  280. uptr ReservedAddressRange::Init(uptr size, const char *name, uptr fixed_addr) {
  281. base_ = fixed_addr ? MmapFixedNoAccess(fixed_addr, size) : MmapNoAccess(size);
  282. size_ = size;
  283. name_ = name;
  284. (void)os_handle_; // unsupported
  285. return reinterpret_cast<uptr>(base_);
  286. }
  287. void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
  288. (void)name; // unsupported
  289. void *res = VirtualAlloc((LPVOID)fixed_addr, size,
  290. MEM_RESERVE, PAGE_NOACCESS);
  291. if (res == 0)
  292. Report("WARNING: %s failed to "
  293. "mprotect %p (%zd) bytes at %p (error code: %d)\n",
  294. SanitizerToolName, size, size, fixed_addr, GetLastError());
  295. return res;
  296. }
  297. void *MmapNoAccess(uptr size) {
  298. void *res = VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_NOACCESS);
  299. if (res == 0)
  300. Report("WARNING: %s failed to "
  301. "mprotect %p (%zd) bytes (error code: %d)\n",
  302. SanitizerToolName, size, size, GetLastError());
  303. return res;
  304. }
  305. bool MprotectNoAccess(uptr addr, uptr size) {
  306. DWORD old_protection;
  307. return VirtualProtect((LPVOID)addr, size, PAGE_NOACCESS, &old_protection);
  308. }
  309. bool MprotectReadOnly(uptr addr, uptr size) {
  310. DWORD old_protection;
  311. return VirtualProtect((LPVOID)addr, size, PAGE_READONLY, &old_protection);
  312. }
  313. void ReleaseMemoryPagesToOS(uptr beg, uptr end) {
  314. uptr beg_aligned = RoundDownTo(beg, GetPageSizeCached()),
  315. end_aligned = RoundDownTo(end, GetPageSizeCached());
  316. CHECK(beg < end); // make sure the region is sane
  317. if (beg_aligned == end_aligned) // make sure we're freeing at least 1 page;
  318. return;
  319. UnmapOrDie((void *)beg, end_aligned - beg_aligned);
  320. }
  321. void SetShadowRegionHugePageMode(uptr addr, uptr size) {
  322. // FIXME: probably similar to ReleaseMemoryToOS.
  323. }
  324. bool DontDumpShadowMemory(uptr addr, uptr length) {
  325. // This is almost useless on 32-bits.
  326. // FIXME: add madvise-analog when we move to 64-bits.
  327. return true;
  328. }
  329. uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
  330. uptr min_shadow_base_alignment,
  331. UNUSED uptr &high_mem_end) {
  332. const uptr granularity = GetMmapGranularity();
  333. const uptr alignment =
  334. Max<uptr>(granularity << shadow_scale, 1ULL << min_shadow_base_alignment);
  335. const uptr left_padding =
  336. Max<uptr>(granularity, 1ULL << min_shadow_base_alignment);
  337. uptr space_size = shadow_size_bytes + left_padding;
  338. uptr shadow_start = FindAvailableMemoryRange(space_size, alignment,
  339. granularity, nullptr, nullptr);
  340. CHECK_NE((uptr)0, shadow_start);
  341. CHECK(IsAligned(shadow_start, alignment));
  342. return shadow_start;
  343. }
  344. uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
  345. uptr *largest_gap_found,
  346. uptr *max_occupied_addr) {
  347. uptr address = 0;
  348. while (true) {
  349. MEMORY_BASIC_INFORMATION info;
  350. if (!::VirtualQuery((void*)address, &info, sizeof(info)))
  351. return 0;
  352. if (info.State == MEM_FREE) {
  353. uptr shadow_address = RoundUpTo((uptr)info.BaseAddress + left_padding,
  354. alignment);
  355. if (shadow_address + size < (uptr)info.BaseAddress + info.RegionSize)
  356. return shadow_address;
  357. }
  358. // Move to the next region.
  359. address = (uptr)info.BaseAddress + info.RegionSize;
  360. }
  361. return 0;
  362. }
  363. uptr MapDynamicShadowAndAliases(uptr shadow_size, uptr alias_size,
  364. uptr num_aliases, uptr ring_buffer_size) {
  365. CHECK(false && "HWASan aliasing is unimplemented on Windows");
  366. return 0;
  367. }
  368. bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
  369. MEMORY_BASIC_INFORMATION mbi;
  370. CHECK(VirtualQuery((void *)range_start, &mbi, sizeof(mbi)));
  371. return mbi.Protect == PAGE_NOACCESS &&
  372. (uptr)mbi.BaseAddress + mbi.RegionSize >= range_end;
  373. }
  374. void *MapFileToMemory(const char *file_name, uptr *buff_size) {
  375. UNIMPLEMENTED();
  376. }
  377. void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset) {
  378. UNIMPLEMENTED();
  379. }
  380. static const int kMaxEnvNameLength = 128;
  381. static const DWORD kMaxEnvValueLength = 32767;
  382. namespace {
  383. struct EnvVariable {
  384. char name[kMaxEnvNameLength];
  385. char value[kMaxEnvValueLength];
  386. };
  387. } // namespace
  388. static const int kEnvVariables = 5;
  389. static EnvVariable env_vars[kEnvVariables];
  390. static int num_env_vars;
  391. const char *GetEnv(const char *name) {
  392. // Note: this implementation caches the values of the environment variables
  393. // and limits their quantity.
  394. for (int i = 0; i < num_env_vars; i++) {
  395. if (0 == internal_strcmp(name, env_vars[i].name))
  396. return env_vars[i].value;
  397. }
  398. CHECK_LT(num_env_vars, kEnvVariables);
  399. DWORD rv = GetEnvironmentVariableA(name, env_vars[num_env_vars].value,
  400. kMaxEnvValueLength);
  401. if (rv > 0 && rv < kMaxEnvValueLength) {
  402. CHECK_LT(internal_strlen(name), kMaxEnvNameLength);
  403. internal_strncpy(env_vars[num_env_vars].name, name, kMaxEnvNameLength);
  404. num_env_vars++;
  405. return env_vars[num_env_vars - 1].value;
  406. }
  407. return 0;
  408. }
  409. const char *GetPwd() {
  410. UNIMPLEMENTED();
  411. }
  412. u32 GetUid() {
  413. UNIMPLEMENTED();
  414. }
  415. namespace {
  416. struct ModuleInfo {
  417. const char *filepath;
  418. uptr base_address;
  419. uptr end_address;
  420. };
  421. #if !SANITIZER_GO
  422. int CompareModulesBase(const void *pl, const void *pr) {
  423. const ModuleInfo *l = (const ModuleInfo *)pl, *r = (const ModuleInfo *)pr;
  424. if (l->base_address < r->base_address)
  425. return -1;
  426. return l->base_address > r->base_address;
  427. }
  428. #endif
  429. } // namespace
  430. #if !SANITIZER_GO
  431. void DumpProcessMap() {
  432. Report("Dumping process modules:\n");
  433. ListOfModules modules;
  434. modules.init();
  435. uptr num_modules = modules.size();
  436. InternalMmapVector<ModuleInfo> module_infos(num_modules);
  437. for (size_t i = 0; i < num_modules; ++i) {
  438. module_infos[i].filepath = modules[i].full_name();
  439. module_infos[i].base_address = modules[i].ranges().front()->beg;
  440. module_infos[i].end_address = modules[i].ranges().back()->end;
  441. }
  442. qsort(module_infos.data(), num_modules, sizeof(ModuleInfo),
  443. CompareModulesBase);
  444. for (size_t i = 0; i < num_modules; ++i) {
  445. const ModuleInfo &mi = module_infos[i];
  446. if (mi.end_address != 0) {
  447. Printf("\t%p-%p %s\n", mi.base_address, mi.end_address,
  448. mi.filepath[0] ? mi.filepath : "[no name]");
  449. } else if (mi.filepath[0]) {
  450. Printf("\t??\?-??? %s\n", mi.filepath);
  451. } else {
  452. Printf("\t???\n");
  453. }
  454. }
  455. }
  456. #endif
  457. void DisableCoreDumperIfNecessary() {
  458. // Do nothing.
  459. }
  460. void ReExec() {
  461. UNIMPLEMENTED();
  462. }
  463. void PlatformPrepareForSandboxing(void *args) {}
  464. bool StackSizeIsUnlimited() {
  465. UNIMPLEMENTED();
  466. }
  467. void SetStackSizeLimitInBytes(uptr limit) {
  468. UNIMPLEMENTED();
  469. }
  470. bool AddressSpaceIsUnlimited() {
  471. UNIMPLEMENTED();
  472. }
  473. void SetAddressSpaceUnlimited() {
  474. UNIMPLEMENTED();
  475. }
  476. bool IsPathSeparator(const char c) {
  477. return c == '\\' || c == '/';
  478. }
  479. static bool IsAlpha(char c) {
  480. c = ToLower(c);
  481. return c >= 'a' && c <= 'z';
  482. }
  483. bool IsAbsolutePath(const char *path) {
  484. return path != nullptr && IsAlpha(path[0]) && path[1] == ':' &&
  485. IsPathSeparator(path[2]);
  486. }
  487. void internal_usleep(u64 useconds) { Sleep(useconds / 1000); }
  488. u64 NanoTime() {
  489. static LARGE_INTEGER frequency = {};
  490. LARGE_INTEGER counter;
  491. if (UNLIKELY(frequency.QuadPart == 0)) {
  492. QueryPerformanceFrequency(&frequency);
  493. CHECK_NE(frequency.QuadPart, 0);
  494. }
  495. QueryPerformanceCounter(&counter);
  496. counter.QuadPart *= 1000ULL * 1000000ULL;
  497. counter.QuadPart /= frequency.QuadPart;
  498. return counter.QuadPart;
  499. }
  500. u64 MonotonicNanoTime() { return NanoTime(); }
  501. void Abort() {
  502. internal__exit(3);
  503. }
  504. bool CreateDir(const char *pathname) {
  505. return CreateDirectoryA(pathname, nullptr) != 0;
  506. }
  507. #if !SANITIZER_GO
  508. // Read the file to extract the ImageBase field from the PE header. If ASLR is
  509. // disabled and this virtual address is available, the loader will typically
  510. // load the image at this address. Therefore, we call it the preferred base. Any
  511. // addresses in the DWARF typically assume that the object has been loaded at
  512. // this address.
  513. static uptr GetPreferredBase(const char *modname, char *buf, size_t buf_size) {
  514. fd_t fd = OpenFile(modname, RdOnly, nullptr);
  515. if (fd == kInvalidFd)
  516. return 0;
  517. FileCloser closer(fd);
  518. // Read just the DOS header.
  519. IMAGE_DOS_HEADER dos_header;
  520. uptr bytes_read;
  521. if (!ReadFromFile(fd, &dos_header, sizeof(dos_header), &bytes_read) ||
  522. bytes_read != sizeof(dos_header))
  523. return 0;
  524. // The file should start with the right signature.
  525. if (dos_header.e_magic != IMAGE_DOS_SIGNATURE)
  526. return 0;
  527. // The layout at e_lfanew is:
  528. // "PE\0\0"
  529. // IMAGE_FILE_HEADER
  530. // IMAGE_OPTIONAL_HEADER
  531. // Seek to e_lfanew and read all that data.
  532. if (::SetFilePointer(fd, dos_header.e_lfanew, nullptr, FILE_BEGIN) ==
  533. INVALID_SET_FILE_POINTER)
  534. return 0;
  535. if (!ReadFromFile(fd, buf, buf_size, &bytes_read) || bytes_read != buf_size)
  536. return 0;
  537. // Check for "PE\0\0" before the PE header.
  538. char *pe_sig = &buf[0];
  539. if (internal_memcmp(pe_sig, "PE\0\0", 4) != 0)
  540. return 0;
  541. // Skip over IMAGE_FILE_HEADER. We could do more validation here if we wanted.
  542. IMAGE_OPTIONAL_HEADER *pe_header =
  543. (IMAGE_OPTIONAL_HEADER *)(pe_sig + 4 + sizeof(IMAGE_FILE_HEADER));
  544. // Check for more magic in the PE header.
  545. if (pe_header->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC)
  546. return 0;
  547. // Finally, return the ImageBase.
  548. return (uptr)pe_header->ImageBase;
  549. }
  550. void ListOfModules::init() {
  551. clearOrInit();
  552. HANDLE cur_process = GetCurrentProcess();
  553. // Query the list of modules. Start by assuming there are no more than 256
  554. // modules and retry if that's not sufficient.
  555. HMODULE *hmodules = 0;
  556. uptr modules_buffer_size = sizeof(HMODULE) * 256;
  557. DWORD bytes_required;
  558. while (!hmodules) {
  559. hmodules = (HMODULE *)MmapOrDie(modules_buffer_size, __FUNCTION__);
  560. CHECK(EnumProcessModules(cur_process, hmodules, modules_buffer_size,
  561. &bytes_required));
  562. if (bytes_required > modules_buffer_size) {
  563. // Either there turned out to be more than 256 hmodules, or new hmodules
  564. // could have loaded since the last try. Retry.
  565. UnmapOrDie(hmodules, modules_buffer_size);
  566. hmodules = 0;
  567. modules_buffer_size = bytes_required;
  568. }
  569. }
  570. InternalMmapVector<char> buf(4 + sizeof(IMAGE_FILE_HEADER) +
  571. sizeof(IMAGE_OPTIONAL_HEADER));
  572. InternalMmapVector<wchar_t> modname_utf16(kMaxPathLength);
  573. InternalMmapVector<char> module_name(kMaxPathLength);
  574. // |num_modules| is the number of modules actually present,
  575. size_t num_modules = bytes_required / sizeof(HMODULE);
  576. for (size_t i = 0; i < num_modules; ++i) {
  577. HMODULE handle = hmodules[i];
  578. MODULEINFO mi;
  579. if (!GetModuleInformation(cur_process, handle, &mi, sizeof(mi)))
  580. continue;
  581. // Get the UTF-16 path and convert to UTF-8.
  582. int modname_utf16_len =
  583. GetModuleFileNameW(handle, &modname_utf16[0], kMaxPathLength);
  584. if (modname_utf16_len == 0)
  585. modname_utf16[0] = '\0';
  586. int module_name_len = ::WideCharToMultiByte(
  587. CP_UTF8, 0, &modname_utf16[0], modname_utf16_len + 1, &module_name[0],
  588. kMaxPathLength, NULL, NULL);
  589. module_name[module_name_len] = '\0';
  590. uptr base_address = (uptr)mi.lpBaseOfDll;
  591. uptr end_address = (uptr)mi.lpBaseOfDll + mi.SizeOfImage;
  592. // Adjust the base address of the module so that we get a VA instead of an
  593. // RVA when computing the module offset. This helps llvm-symbolizer find the
  594. // right DWARF CU. In the common case that the image is loaded at it's
  595. // preferred address, we will now print normal virtual addresses.
  596. uptr preferred_base =
  597. GetPreferredBase(&module_name[0], &buf[0], buf.size());
  598. uptr adjusted_base = base_address - preferred_base;
  599. modules_.push_back(LoadedModule());
  600. LoadedModule &cur_module = modules_.back();
  601. cur_module.set(&module_name[0], adjusted_base);
  602. // We add the whole module as one single address range.
  603. cur_module.addAddressRange(base_address, end_address, /*executable*/ true,
  604. /*writable*/ true);
  605. }
  606. UnmapOrDie(hmodules, modules_buffer_size);
  607. }
  608. void ListOfModules::fallbackInit() { clear(); }
  609. // We can't use atexit() directly at __asan_init time as the CRT is not fully
  610. // initialized at this point. Place the functions into a vector and use
  611. // atexit() as soon as it is ready for use (i.e. after .CRT$XIC initializers).
  612. InternalMmapVectorNoCtor<void (*)(void)> atexit_functions;
  613. int Atexit(void (*function)(void)) {
  614. atexit_functions.push_back(function);
  615. return 0;
  616. }
  617. static int RunAtexit() {
  618. TraceLoggingUnregister(g_asan_provider);
  619. int ret = 0;
  620. for (uptr i = 0; i < atexit_functions.size(); ++i) {
  621. ret |= atexit(atexit_functions[i]);
  622. }
  623. return ret;
  624. }
  625. #pragma section(".CRT$XID", long, read)
  626. __declspec(allocate(".CRT$XID")) int (*__run_atexit)() = RunAtexit;
  627. #endif
  628. // ------------------ sanitizer_libc.h
  629. fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *last_error) {
  630. // FIXME: Use the wide variants to handle Unicode filenames.
  631. fd_t res;
  632. if (mode == RdOnly) {
  633. res = CreateFileA(filename, GENERIC_READ,
  634. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  635. nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  636. } else if (mode == WrOnly) {
  637. res = CreateFileA(filename, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS,
  638. FILE_ATTRIBUTE_NORMAL, nullptr);
  639. } else {
  640. UNIMPLEMENTED();
  641. }
  642. CHECK(res != kStdoutFd || kStdoutFd == kInvalidFd);
  643. CHECK(res != kStderrFd || kStderrFd == kInvalidFd);
  644. if (res == kInvalidFd && last_error)
  645. *last_error = GetLastError();
  646. return res;
  647. }
  648. void CloseFile(fd_t fd) {
  649. CloseHandle(fd);
  650. }
  651. bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
  652. error_t *error_p) {
  653. CHECK(fd != kInvalidFd);
  654. // bytes_read can't be passed directly to ReadFile:
  655. // uptr is unsigned long long on 64-bit Windows.
  656. unsigned long num_read_long;
  657. bool success = ::ReadFile(fd, buff, buff_size, &num_read_long, nullptr);
  658. if (!success && error_p)
  659. *error_p = GetLastError();
  660. if (bytes_read)
  661. *bytes_read = num_read_long;
  662. return success;
  663. }
  664. bool SupportsColoredOutput(fd_t fd) {
  665. // FIXME: support colored output.
  666. return false;
  667. }
  668. bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
  669. error_t *error_p) {
  670. CHECK(fd != kInvalidFd);
  671. // Handle null optional parameters.
  672. error_t dummy_error;
  673. error_p = error_p ? error_p : &dummy_error;
  674. uptr dummy_bytes_written;
  675. bytes_written = bytes_written ? bytes_written : &dummy_bytes_written;
  676. // Initialize output parameters in case we fail.
  677. *error_p = 0;
  678. *bytes_written = 0;
  679. // Map the conventional Unix fds 1 and 2 to Windows handles. They might be
  680. // closed, in which case this will fail.
  681. if (fd == kStdoutFd || fd == kStderrFd) {
  682. fd = GetStdHandle(fd == kStdoutFd ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
  683. if (fd == 0) {
  684. *error_p = ERROR_INVALID_HANDLE;
  685. return false;
  686. }
  687. }
  688. DWORD bytes_written_32;
  689. if (!WriteFile(fd, buff, buff_size, &bytes_written_32, 0)) {
  690. *error_p = GetLastError();
  691. return false;
  692. } else {
  693. *bytes_written = bytes_written_32;
  694. return true;
  695. }
  696. }
  697. uptr internal_sched_yield() {
  698. Sleep(0);
  699. return 0;
  700. }
  701. void internal__exit(int exitcode) {
  702. TraceLoggingUnregister(g_asan_provider);
  703. // ExitProcess runs some finalizers, so use TerminateProcess to avoid that.
  704. // The debugger doesn't stop on TerminateProcess like it does on ExitProcess,
  705. // so add our own breakpoint here.
  706. if (::IsDebuggerPresent())
  707. __debugbreak();
  708. TerminateProcess(GetCurrentProcess(), exitcode);
  709. BUILTIN_UNREACHABLE();
  710. }
  711. uptr internal_ftruncate(fd_t fd, uptr size) {
  712. UNIMPLEMENTED();
  713. }
  714. uptr GetRSS() {
  715. PROCESS_MEMORY_COUNTERS counters;
  716. if (!GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters)))
  717. return 0;
  718. return counters.WorkingSetSize;
  719. }
  720. void *internal_start_thread(void *(*func)(void *arg), void *arg) { return 0; }
  721. void internal_join_thread(void *th) { }
  722. void FutexWait(atomic_uint32_t *p, u32 cmp) {
  723. WaitOnAddress(p, &cmp, sizeof(cmp), INFINITE);
  724. }
  725. void FutexWake(atomic_uint32_t *p, u32 count) {
  726. if (count == 1)
  727. WakeByAddressSingle(p);
  728. else
  729. WakeByAddressAll(p);
  730. }
  731. uptr GetTlsSize() {
  732. return 0;
  733. }
  734. void InitTlsSize() {
  735. }
  736. void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
  737. uptr *tls_addr, uptr *tls_size) {
  738. #if SANITIZER_GO
  739. *stk_addr = 0;
  740. *stk_size = 0;
  741. *tls_addr = 0;
  742. *tls_size = 0;
  743. #else
  744. uptr stack_top, stack_bottom;
  745. GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
  746. *stk_addr = stack_bottom;
  747. *stk_size = stack_top - stack_bottom;
  748. *tls_addr = 0;
  749. *tls_size = 0;
  750. #endif
  751. }
  752. void ReportFile::Write(const char *buffer, uptr length) {
  753. SpinMutexLock l(mu);
  754. ReopenIfNecessary();
  755. if (!WriteToFile(fd, buffer, length)) {
  756. // stderr may be closed, but we may be able to print to the debugger
  757. // instead. This is the case when launching a program from Visual Studio,
  758. // and the following routine should write to its console.
  759. OutputDebugStringA(buffer);
  760. }
  761. }
  762. void SetAlternateSignalStack() {
  763. // FIXME: Decide what to do on Windows.
  764. }
  765. void UnsetAlternateSignalStack() {
  766. // FIXME: Decide what to do on Windows.
  767. }
  768. void InstallDeadlySignalHandlers(SignalHandlerType handler) {
  769. (void)handler;
  770. // FIXME: Decide what to do on Windows.
  771. }
  772. HandleSignalMode GetHandleSignalMode(int signum) {
  773. // FIXME: Decide what to do on Windows.
  774. return kHandleSignalNo;
  775. }
  776. // Check based on flags if we should handle this exception.
  777. bool IsHandledDeadlyException(DWORD exceptionCode) {
  778. switch (exceptionCode) {
  779. case EXCEPTION_ACCESS_VIOLATION:
  780. case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  781. case EXCEPTION_STACK_OVERFLOW:
  782. case EXCEPTION_DATATYPE_MISALIGNMENT:
  783. case EXCEPTION_IN_PAGE_ERROR:
  784. return common_flags()->handle_segv;
  785. case EXCEPTION_ILLEGAL_INSTRUCTION:
  786. case EXCEPTION_PRIV_INSTRUCTION:
  787. case EXCEPTION_BREAKPOINT:
  788. return common_flags()->handle_sigill;
  789. case EXCEPTION_FLT_DENORMAL_OPERAND:
  790. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  791. case EXCEPTION_FLT_INEXACT_RESULT:
  792. case EXCEPTION_FLT_INVALID_OPERATION:
  793. case EXCEPTION_FLT_OVERFLOW:
  794. case EXCEPTION_FLT_STACK_CHECK:
  795. case EXCEPTION_FLT_UNDERFLOW:
  796. case EXCEPTION_INT_DIVIDE_BY_ZERO:
  797. case EXCEPTION_INT_OVERFLOW:
  798. return common_flags()->handle_sigfpe;
  799. }
  800. return false;
  801. }
  802. bool IsAccessibleMemoryRange(uptr beg, uptr size) {
  803. SYSTEM_INFO si;
  804. GetNativeSystemInfo(&si);
  805. uptr page_size = si.dwPageSize;
  806. uptr page_mask = ~(page_size - 1);
  807. for (uptr page = beg & page_mask, end = (beg + size - 1) & page_mask;
  808. page <= end;) {
  809. MEMORY_BASIC_INFORMATION info;
  810. if (VirtualQuery((LPCVOID)page, &info, sizeof(info)) != sizeof(info))
  811. return false;
  812. if (info.Protect == 0 || info.Protect == PAGE_NOACCESS ||
  813. info.Protect == PAGE_EXECUTE)
  814. return false;
  815. if (info.RegionSize == 0)
  816. return false;
  817. page += info.RegionSize;
  818. }
  819. return true;
  820. }
  821. bool SignalContext::IsStackOverflow() const {
  822. return (DWORD)GetType() == EXCEPTION_STACK_OVERFLOW;
  823. }
  824. void SignalContext::InitPcSpBp() {
  825. EXCEPTION_RECORD *exception_record = (EXCEPTION_RECORD *)siginfo;
  826. CONTEXT *context_record = (CONTEXT *)context;
  827. pc = (uptr)exception_record->ExceptionAddress;
  828. # if SANITIZER_WINDOWS64
  829. # if SANITIZER_ARM64
  830. bp = (uptr)context_record->Fp;
  831. sp = (uptr)context_record->Sp;
  832. # else
  833. bp = (uptr)context_record->Rbp;
  834. sp = (uptr)context_record->Rsp;
  835. # endif
  836. # else
  837. bp = (uptr)context_record->Ebp;
  838. sp = (uptr)context_record->Esp;
  839. # endif
  840. }
  841. uptr SignalContext::GetAddress() const {
  842. EXCEPTION_RECORD *exception_record = (EXCEPTION_RECORD *)siginfo;
  843. if (exception_record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
  844. return exception_record->ExceptionInformation[1];
  845. return (uptr)exception_record->ExceptionAddress;
  846. }
  847. bool SignalContext::IsMemoryAccess() const {
  848. return ((EXCEPTION_RECORD *)siginfo)->ExceptionCode ==
  849. EXCEPTION_ACCESS_VIOLATION;
  850. }
  851. bool SignalContext::IsTrueFaultingAddress() const { return true; }
  852. SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
  853. EXCEPTION_RECORD *exception_record = (EXCEPTION_RECORD *)siginfo;
  854. // The write flag is only available for access violation exceptions.
  855. if (exception_record->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
  856. return SignalContext::Unknown;
  857. // The contents of this array are documented at
  858. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-exception_record
  859. // The first element indicates read as 0, write as 1, or execute as 8. The
  860. // second element is the faulting address.
  861. switch (exception_record->ExceptionInformation[0]) {
  862. case 0:
  863. return SignalContext::Read;
  864. case 1:
  865. return SignalContext::Write;
  866. case 8:
  867. return SignalContext::Unknown;
  868. }
  869. return SignalContext::Unknown;
  870. }
  871. void SignalContext::DumpAllRegisters(void *context) {
  872. // FIXME: Implement this.
  873. }
  874. int SignalContext::GetType() const {
  875. return static_cast<const EXCEPTION_RECORD *>(siginfo)->ExceptionCode;
  876. }
  877. const char *SignalContext::Describe() const {
  878. unsigned code = GetType();
  879. // Get the string description of the exception if this is a known deadly
  880. // exception.
  881. switch (code) {
  882. case EXCEPTION_ACCESS_VIOLATION:
  883. return "access-violation";
  884. case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  885. return "array-bounds-exceeded";
  886. case EXCEPTION_STACK_OVERFLOW:
  887. return "stack-overflow";
  888. case EXCEPTION_DATATYPE_MISALIGNMENT:
  889. return "datatype-misalignment";
  890. case EXCEPTION_IN_PAGE_ERROR:
  891. return "in-page-error";
  892. case EXCEPTION_ILLEGAL_INSTRUCTION:
  893. return "illegal-instruction";
  894. case EXCEPTION_PRIV_INSTRUCTION:
  895. return "priv-instruction";
  896. case EXCEPTION_BREAKPOINT:
  897. return "breakpoint";
  898. case EXCEPTION_FLT_DENORMAL_OPERAND:
  899. return "flt-denormal-operand";
  900. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  901. return "flt-divide-by-zero";
  902. case EXCEPTION_FLT_INEXACT_RESULT:
  903. return "flt-inexact-result";
  904. case EXCEPTION_FLT_INVALID_OPERATION:
  905. return "flt-invalid-operation";
  906. case EXCEPTION_FLT_OVERFLOW:
  907. return "flt-overflow";
  908. case EXCEPTION_FLT_STACK_CHECK:
  909. return "flt-stack-check";
  910. case EXCEPTION_FLT_UNDERFLOW:
  911. return "flt-underflow";
  912. case EXCEPTION_INT_DIVIDE_BY_ZERO:
  913. return "int-divide-by-zero";
  914. case EXCEPTION_INT_OVERFLOW:
  915. return "int-overflow";
  916. }
  917. return "unknown exception";
  918. }
  919. uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
  920. if (buf_len == 0)
  921. return 0;
  922. // Get the UTF-16 path and convert to UTF-8.
  923. InternalMmapVector<wchar_t> binname_utf16(kMaxPathLength);
  924. int binname_utf16_len =
  925. GetModuleFileNameW(NULL, &binname_utf16[0], kMaxPathLength);
  926. if (binname_utf16_len == 0) {
  927. buf[0] = '\0';
  928. return 0;
  929. }
  930. int binary_name_len =
  931. ::WideCharToMultiByte(CP_UTF8, 0, &binname_utf16[0], binname_utf16_len,
  932. buf, buf_len, NULL, NULL);
  933. if ((unsigned)binary_name_len == buf_len)
  934. --binary_name_len;
  935. buf[binary_name_len] = '\0';
  936. return binary_name_len;
  937. }
  938. uptr ReadLongProcessName(/*out*/char *buf, uptr buf_len) {
  939. return ReadBinaryName(buf, buf_len);
  940. }
  941. void CheckVMASize() {
  942. // Do nothing.
  943. }
  944. void InitializePlatformEarly() {
  945. // Do nothing.
  946. }
  947. void CheckASLR() {
  948. // Do nothing
  949. }
  950. void CheckMPROTECT() {
  951. // Do nothing
  952. }
  953. char **GetArgv() {
  954. // FIXME: Actually implement this function.
  955. return 0;
  956. }
  957. char **GetEnviron() {
  958. // FIXME: Actually implement this function.
  959. return 0;
  960. }
  961. pid_t StartSubprocess(const char *program, const char *const argv[],
  962. const char *const envp[], fd_t stdin_fd, fd_t stdout_fd,
  963. fd_t stderr_fd) {
  964. // FIXME: implement on this platform
  965. // Should be implemented based on
  966. // SymbolizerProcess::StarAtSymbolizerSubprocess
  967. // from lib/sanitizer_common/sanitizer_symbolizer_win.cpp.
  968. return -1;
  969. }
  970. bool IsProcessRunning(pid_t pid) {
  971. // FIXME: implement on this platform.
  972. return false;
  973. }
  974. int WaitForProcess(pid_t pid) { return -1; }
  975. // FIXME implement on this platform.
  976. void GetMemoryProfile(fill_profile_f cb, uptr *stats) {}
  977. void CheckNoDeepBind(const char *filename, int flag) {
  978. // Do nothing.
  979. }
  980. // FIXME: implement on this platform.
  981. bool GetRandom(void *buffer, uptr length, bool blocking) {
  982. UNIMPLEMENTED();
  983. }
  984. u32 GetNumberOfCPUs() {
  985. SYSTEM_INFO sysinfo = {};
  986. GetNativeSystemInfo(&sysinfo);
  987. return sysinfo.dwNumberOfProcessors;
  988. }
  989. #if SANITIZER_WIN_TRACE
  990. // TODO(mcgov): Rename this project-wide to PlatformLogInit
  991. void AndroidLogInit(void) {
  992. HRESULT hr = TraceLoggingRegister(g_asan_provider);
  993. if (!SUCCEEDED(hr))
  994. return;
  995. }
  996. void SetAbortMessage(const char *) {}
  997. void LogFullErrorReport(const char *buffer) {
  998. if (common_flags()->log_to_syslog) {
  999. InternalMmapVector<wchar_t> filename;
  1000. DWORD filename_length = 0;
  1001. do {
  1002. filename.resize(filename.size() + 0x100);
  1003. filename_length =
  1004. GetModuleFileNameW(NULL, filename.begin(), filename.size());
  1005. } while (filename_length >= filename.size());
  1006. TraceLoggingWrite(g_asan_provider, "AsanReportEvent",
  1007. TraceLoggingValue(filename.begin(), "ExecutableName"),
  1008. TraceLoggingValue(buffer, "AsanReportContents"));
  1009. }
  1010. }
  1011. #endif // SANITIZER_WIN_TRACE
  1012. void InitializePlatformCommonFlags(CommonFlags *cf) {}
  1013. } // namespace __sanitizer
  1014. #endif // _WIN32