sanitizer_win.cpp 35 KB

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