15#ifdef MINGW_HAS_SECURE_API
16#undef MINGW_HAS_SECURE_API
23#include <VersionHelpers.h>
27#include <processthreadsapi.h>
74#ifndef __MINGW64_VERSION_MAJOR
79inline void MemoryFence() {
81 __asm__ __volatile__(
"xchgl %%eax,%0 ":
"=r" (barrier));
87int localtime_s(tm* out_tm,
const time_t* time) {
88 tm* posix_local_time_struct = localtime_r(time, out_tm);
89 if (posix_local_time_struct ==
nullptr)
return 1;
94int fopen_s(FILE** pFile,
const char*
filename,
const char* mode) {
96 return *pFile !=
nullptr ? 0 : 1;
99int _wfopen_s(FILE** pFile,
const wchar_t*
filename,
const wchar_t* mode) {
101 return *pFile !=
nullptr ? 0 : 1;
104int _vsnprintf_s(
char* buffer,
size_t sizeOfBuffer,
size_t count,
105 const char* format, va_list argptr) {
106 DCHECK(count == _TRUNCATE);
107 return _vsnprintf(buffer, sizeOfBuffer, format, argptr);
111int strncpy_s(
char* dest,
size_t dest_size,
const char* source,
size_t count) {
112 CHECK(source !=
nullptr);
113 CHECK(dest !=
nullptr);
116 if (count == _TRUNCATE) {
117 while (dest_size > 0 && *source != 0) {
118 *(dest++) = *(source++);
121 if (dest_size == 0) {
126 while (dest_size > 0 && count > 0 && *source != 0) {
127 *(dest++) = *(source++);
166 if (GetTimeZoneInformation(&
tzinfo_) == TIME_ZONE_ID_INVALID) {
169 tzinfo_.StandardDate.wMonth = 10;
171 tzinfo_.StandardDate.wHour = 3;
173 tzinfo_.DaylightDate.wMonth = 3;
175 tzinfo_.DaylightDate.wHour = 2;
208 static const int kHour = 60;
210 case -9*kHour:
return "Alaska";
211 case -8*kHour:
return "Pacific";
212 case -7*kHour:
return "Mountain";
213 case -6*kHour:
return "Central";
214 case -5*kHour:
return "Eastern";
215 case -4*kHour:
return "Atlantic";
216 case 0*kHour:
return "GMT";
217 case +1*kHour:
return "Central Europe";
218 case +2*kHour:
return "Eastern Europe";
219 case +3*kHour:
return "Russia";
220 case +5*kHour + 30:
return "India";
221 case +8*kHour:
return "China";
222 case +9*kHour:
return "Japan";
223 case +12*kHour:
return "New Zealand";
224 default:
return "Local";
250 Win32Time(
int year,
int mon,
int day,
int hour,
int min,
int sec);
325 st.wMilliseconds = 0;
326 SystemTimeToFileTime(&st, &
ft());
355 static bool initialized =
false;
357 static DWORD init_ticks;
358 static const int64_t kHundredNanosecondsPerSecond = 10000000;
359 static const int64_t kMaxClockElapsedTime =
360 60*kHundredNanosecondsPerSecond;
363 bool needs_resync = !initialized;
367 GetSystemTimeAsFileTime(&time_now.
ft_);
368 DWORD ticks_now = timeGetTime();
371 needs_resync |= ticks_now < init_ticks;
374 needs_resync |= (time_now.
t_ - init_time.
t_) > kMaxClockElapsedTime;
377 needs_resync |= time_now.
t_ < init_time.
t_;
381 GetSystemTimeAsFileTime(&init_time.
ft_);
382 init_ticks = ticks_now = timeGetTime();
387 DWORD elapsed = ticks_now - init_ticks;
388 this->
time_.
t_ = init_time.
t_ + (
static_cast<int64_t
>(elapsed) * 10000);
398 cache->InitializeIfNeeded();
401 rounded_to_second.
t() =
409 double unchecked_posix_time = rounded_to_second.
ToJSTime() / 1000;
410 if (unchecked_posix_time > INT_MAX || unchecked_posix_time < 0) {
414 time_t posix_time =
static_cast<time_t
>(unchecked_posix_time);
417 tm posix_local_time_struct;
418 if (localtime_s(&posix_local_time_struct, &posix_time))
return 0;
420 if (posix_local_time_struct.tm_isdst > 0) {
421 return (cache->tzinfo_.Bias + cache->tzinfo_.DaylightBias) * -
kMsPerMinute;
422 }
else if (posix_local_time_struct.tm_isdst == 0) {
423 return (cache->tzinfo_.Bias + cache->tzinfo_.StandardBias) * -
kMsPerMinute;
432 cache->InitializeIfNeeded();
436 if (cache->tzinfo_.StandardDate.wMonth != 0 ||
437 cache->tzinfo_.DaylightDate.wMonth != 0) {
444 -(cache->tzinfo_.Bias + cache->tzinfo_.DaylightBias) *
kMsPerMinute;
448 in_dst =
offset == dstofs;
466 return InDST(cache) ? cache->dst_tz_name_ : cache->std_tz_name_;
476 if (!GetThreadTimes(GetCurrentThread(), &dummy, &dummy, &dummy,
477 reinterpret_cast<FILETIME*
>(&usertime)))
return -1;
483 *secs =
static_cast<uint32_t
>(usertime / 1000000);
484 *usecs =
static_cast<uint32_t
>(usertime % 1000000);
489 constexpr int KB = 1024;
491 PROCESS_MEMORY_COUNTERS mem_counters;
494 ret = GetProcessMemoryInfo(GetCurrentProcess(), &mem_counters,
495 sizeof(mem_counters));
496 if (ret == 0)
return -1;
498 return static_cast<int>(mem_counters.PeakWorkingSetSize /
KB);
510 return Win32Time(time).LocalTimezone(
this);
521 return static_cast<double>(t.LocalOffset(
this) -
522 t.DaylightSavingsOffset(
this));
529 return static_cast<double>(
offset);
535 return ::GetLastError();
552 TerminateProcess(GetCurrentProcess(), exit_code);
589 if (GetStdHandle(STD_OUTPUT_HANDLE) != INVALID_HANDLE_VALUE &&
590 GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)) != FILE_TYPE_UNKNOWN)
600 if ((stream == stdout || stream == stderr) && !
HasConsole()) {
605 OS::VSNPrintF(buffer,
sizeof(buffer), format,
args);
606 OutputDebugStringA(buffer);
608 vfprintf(stream, format,
args);
615 static_assert(
sizeof(wchar_t) == 2,
"wrong wchar_t size");
616 std::wstring utf16_str;
617 int name_length =
static_cast<int>(strlen(str));
618 int len = MultiByteToWideChar(CP_UTF8, 0, str, name_length,
nullptr, 0);
620 utf16_str.resize(len);
621 MultiByteToWideChar(CP_UTF8, 0, str, name_length, &utf16_str[0], len);
626FILE*
OS::FOpen(
const char* path,
const char* mode) {
630 if (_wfopen_s(&
result, utf16_path.c_str(), utf16_mode.c_str()) == 0) {
639 return (DeleteFileA(path) != 0);
645 return ch ==
'/' || ch ==
'\\';
651 char tempPathBuffer[MAX_PATH];
652 DWORD path_result = 0;
653 path_result = GetTempPathA(MAX_PATH, tempPathBuffer);
654 if (path_result > MAX_PATH || path_result == 0)
return nullptr;
655 UINT name_result = 0;
656 char tempNameBuffer[MAX_PATH];
657 name_result = GetTempFileNameA(tempPathBuffer,
"", 0, tempNameBuffer);
658 if (name_result == 0)
return nullptr;
671void OS::Print(
const char* format, ...) {
673 va_start(
args, format);
674 VPrint(format,
args);
679void OS::VPrint(
const char* format, va_list
args) {
684void OS::FPrint(FILE* out,
const char* format, ...) {
686 va_start(
args, format);
687 VFPrint(out, format,
args);
692void OS::VFPrint(FILE* out,
const char* format, va_list
args) {
698void OS::PrintError(
const char* format, ...) {
700 va_start(
args, format);
701 VPrintError(format,
args);
707void OS::VPrintError(
const char* format, va_list
args) {
712int OS::SNPrintF(
char* str,
int length,
const char* format, ...) {
714 va_start(
args, format);
721int OS::VSNPrintF(
char* str,
int length,
const char* format, va_list
args) {
722 int n = _vsnprintf_s(str, length, _TRUNCATE, format,
args);
727 str[length - 1] =
'\0';
735void OS::StrNCpy(
char* dest,
int length,
const char* src,
size_t n) {
737 size_t buffer_size =
static_cast<size_t>(
length);
738 if (n + 1 > buffer_size)
740 int result = strncpy_s(dest, length, src, n);
750 GetPlatformRandomNumberGenerator)
755bool UserShadowStackEnabled() {
756 auto is_user_cet_available_in_environment =
757 reinterpret_cast<decltype(&IsUserCetAvailableInEnvironment)
>(
758 ::GetProcAddress(::GetModuleHandleW(L
"kernel32.dll"),
759 "IsUserCetAvailableInEnvironment"));
760 auto get_process_mitigation_policy =
761 reinterpret_cast<decltype(&GetProcessMitigationPolicy)
>(::GetProcAddress(
762 ::GetModuleHandle(L
"Kernel32.dll"),
"GetProcessMitigationPolicy"));
764 if (!is_user_cet_available_in_environment || !get_process_mitigation_policy) {
768 if (!is_user_cet_available_in_environment(
769 USER_CET_ENVIRONMENT_WIN32_PROCESS)) {
773 PROCESS_MITIGATION_USER_SHADOW_STACK_POLICY uss_policy;
774 if (!get_process_mitigation_policy(GetCurrentProcess(),
775 ProcessUserShadowStackPolicy, &uss_policy,
776 sizeof(uss_policy))) {
780 return uss_policy.EnableUserShadowStack;
790 MEM_EXTENDED_PARAMETER*, ULONG);
794 SIZE_T, ULONG, ULONG,
795 MEM_EXTENDED_PARAMETER*, ULONG);
801void OS::EnsureWin32MemoryAPILoaded() {
802 static bool loaded =
false;
805 GetModuleHandle(L
"kernelbase.dll"),
"VirtualAlloc2");
808 GetModuleHandle(L
"kernelbase.dll"),
"MapViewOfFile3");
811 GetModuleHandle(L
"kernelbase.dll"),
"UnmapViewOfFile2");
819 static bool cet_enabled = UserShadowStackEnabled();
825 static size_t allocate_alignment = 0;
826 if (allocate_alignment == 0) {
828 GetSystemInfo(&info);
829 allocate_alignment = info.dwAllocationGranularity;
831 return allocate_alignment;
836 static size_t page_size = 0;
837 if (page_size == 0) {
839 GetSystemInfo(&info);
840 page_size = info.dwPageSize;
850 GetPlatformRandomNumberGenerator()->SetSeed(seed);
861#ifdef V8_HOST_ARCH_64_BIT
862 static const uintptr_t kAllocationRandomAddressMin = 0x0000000080000000;
863 static const uintptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000;
865 static const uintptr_t kAllocationRandomAddressMin = 0x04000000;
866 static const uintptr_t kAllocationRandomAddressMax = 0x3FFF0000;
871 GetPlatformRandomNumberGenerator()->NextBytes(&address,
sizeof(address));
874 address += kAllocationRandomAddressMin;
875 address &= kAllocationRandomAddressMax;
876 return reinterpret_cast<void*
>(address);
885 return PAGE_NOACCESS;
887 return PAGE_READONLY;
889 return PAGE_READWRITE;
891 if (IsWindows10OrGreater())
892 return PAGE_EXECUTE_READWRITE | PAGE_TARGETS_INVALID;
893 return PAGE_EXECUTE_READWRITE;
895 if (IsWindows10OrGreater())
896 return PAGE_EXECUTE_READ | PAGE_TARGETS_INVALID;
897 return PAGE_EXECUTE_READ;
908 return FILE_MAP_READ;
910 return FILE_MAP_READ | FILE_MAP_WRITE;
918void* VirtualAllocWrapper(
void* address,
size_t size,
DWORD flags,
921 return VirtualAlloc2(GetCurrentProcess(), address, size, flags, protect,
924 return VirtualAlloc(address, size, flags, protect);
928uint8_t* VirtualAllocWithHint(
size_t size,
DWORD flags,
DWORD protect,
930 LPVOID base = VirtualAllocWrapper(hint, size, flags, protect);
933 if (hint && base ==
nullptr) {
934 base = VirtualAllocWrapper(
nullptr, size, flags, protect);
937 return reinterpret_cast<uint8_t*
>(
base);
940void* AllocateInternal(
void* hint,
size_t size,
size_t alignment,
941 size_t page_size,
DWORD flags,
DWORD protect) {
943 uint8_t*
base = VirtualAllocWithHint(size, flags, protect, hint);
944 if (base ==
nullptr)
return nullptr;
947 uint8_t* aligned_base =
reinterpret_cast<uint8_t*
>(
948 RoundUp(
reinterpret_cast<uintptr_t
>(base), alignment));
949 if (base == aligned_base)
return reinterpret_cast<void*
>(
base);
952 CHECK(VirtualFree(base, 0, MEM_RELEASE));
959 size_t padded_size = size + (alignment - page_size);
960 const int kMaxAttempts = 3;
961 aligned_base =
nullptr;
962 for (
int i = 0;
i < kMaxAttempts; ++
i) {
963 base = VirtualAllocWithHint(padded_size, flags, protect, hint);
964 if (base ==
nullptr)
return nullptr;
968 CHECK(VirtualFree(base, 0, MEM_RELEASE));
969 aligned_base =
reinterpret_cast<uint8_t*
>(
970 RoundUp(
reinterpret_cast<uintptr_t
>(base), alignment));
971 base =
reinterpret_cast<uint8_t*
>(
972 VirtualAllocWrapper(aligned_base, size, flags, protect));
975 if (base !=
nullptr)
break;
978 return reinterpret_cast<void*
>(
base);
981void CheckIsOOMError(
int error) {
985 if (error != ERROR_NOT_ENOUGH_MEMORY)
CHECK_EQ(ERROR_COMMITMENT_LIMIT, error);
991void*
OS::Allocate(
void* hint,
size_t size,
size_t alignment,
992 MemoryPermission access) {
1001 : MEM_RESERVE | MEM_COMMIT;
1004 return AllocateInternal(hint, size, alignment, page_size, flags, protect);
1008void OS::Free(
void* address,
size_t size) {
1012 CHECK_NE(0, VirtualFree(address, 0, MEM_RELEASE));
1024 DWORD access = GetFileViewAccessFromMemoryPermission(permission);
1026 HANDLE file_mapping = FileMappingFromSharedMemoryHandle(handle);
1028 MapViewOfFileEx(file_mapping, access, off_hi, off_lo, size, hint);
1032 result = MapViewOfFile(file_mapping, access, off_hi, off_lo, size);
1040 CHECK(UnmapViewOfFile(address));
1047 CHECK_NE(0, VirtualFree(address, size, MEM_DECOMMIT));
1055 return VirtualFree(address, size, MEM_DECOMMIT) != 0;
1058 void*
result = VirtualAllocWrapper(address, size, MEM_COMMIT, protect);
1065 return result !=
nullptr;
1072 DWORD old_protection;
1073 CHECK(VirtualProtect(address, size, PAGE_READONLY, &old_protection));
1074 CHECK(old_protection == PAGE_READWRITE || old_protection == PAGE_WRITECOPY);
1086 using DiscardVirtualMemoryFunction =
1088 static std::atomic<DiscardVirtualMemoryFunction> discard_virtual_memory(
1089 reinterpret_cast<DiscardVirtualMemoryFunction
>(-1));
1090 if (discard_virtual_memory ==
1091 reinterpret_cast<DiscardVirtualMemoryFunction
>(-1))
1092 discard_virtual_memory =
1093 reinterpret_cast<DiscardVirtualMemoryFunction
>(GetProcAddress(
1094 GetModuleHandle(L
"Kernel32.dll"),
"DiscardVirtualMemory"));
1097 DiscardVirtualMemoryFunction discard_function = discard_virtual_memory.load();
1098 if (discard_function) {
1099 DWORD ret = discard_function(address, size);
1100 if (!ret)
return true;
1104 void* ptr = VirtualAllocWrapper(address, size, MEM_RESET, PAGE_READWRITE);
1121 return VirtualFree(address, size, MEM_DECOMMIT) != 0;
1125bool OS::SealPages(
void* address,
size_t size) {
return false; }
1135 void* hint,
size_t size,
size_t alignment,
1136 MemoryPermission max_permission) {
1147 AllocateInternal(hint, size, alignment, page_size,
1148 MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS);
1149 if (!reservation)
return {};
1156 OS::Free(reservation.base(), reservation.size());
1161 HANDLE handle = CreateFileMapping(INVALID_HANDLE_VALUE,
nullptr,
1162 PAGE_READWRITE, 0, size,
nullptr);
1164 return SharedMemoryHandleFromFileMapping(handle);
1170 HANDLE file_mapping = FileMappingFromSharedMemoryHandle(handle);
1171 CHECK(CloseHandle(file_mapping));
1181 ::Sleep(
static_cast<DWORD>(interval.InMilliseconds()));
1189 other.timer_ = NULL;
1195 other.timer_ = NULL;
1210 DWORD flags = CREATE_WAITABLE_TIMER_HIGH_RESOLUTION;
1213 DWORD desired_access = TIMER_MODIFY_STATE | SYNCHRONIZE;
1215 CreateWaitableTimerExW(NULL,
1217 flags, desired_access);
1223 LARGE_INTEGER due_time;
1224 due_time.QuadPart = -interval.InMicroseconds() * 10;
1226 PTIMERAPCROUTINE completion_routine = NULL;
1227 LPVOID arg_to_completion_routine = NULL;
1228 BOOL resume =
false;
1229 CHECK(SetWaitableTimer(
timer_, &due_time, period, completion_routine,
1230 arg_to_completion_routine, resume));
1232 DWORD timeout_interval = INFINITE;
1233 CHECK_EQ(WAIT_OBJECT_0, WaitForSingleObject(
timer_, timeout_interval));
1238 if (IsDebuggerPresent()) {
1298 DWORD access = GENERIC_READ;
1300 access |= GENERIC_WRITE;
1304 HANDLE file = CreateFileW(utf16_name.c_str(), access,
1305 FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
1306 OPEN_EXISTING, 0,
nullptr);
1307 if (file == INVALID_HANDLE_VALUE)
return nullptr;
1309 DWORD size = GetFileSize(file,
nullptr);
1310 if (size == 0)
return new Win32MemoryMappedFile(file,
nullptr,
nullptr, 0);
1316 CreateFileMapping(file,
nullptr, protection, 0, size,
nullptr);
1317 if (file_mapping ==
nullptr)
return nullptr;
1322 void* memory = MapViewOfFile(file_mapping, view_access, 0, 0, size);
1323 return new Win32MemoryMappedFile(file, file_mapping, memory, size);
1328 size_t size,
void* initial) {
1331 HANDLE file = CreateFileW(utf16_name.c_str(), GENERIC_READ | GENERIC_WRITE,
1332 FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
1333 OPEN_ALWAYS, 0,
nullptr);
1334 if (file ==
nullptr)
return nullptr;
1335 if (size == 0)
return new Win32MemoryMappedFile(file,
nullptr,
nullptr, 0);
1337 HANDLE file_mapping = CreateFileMapping(file,
nullptr, PAGE_READWRITE, 0,
1338 static_cast<DWORD>(size),
nullptr);
1339 if (file_mapping ==
nullptr)
return nullptr;
1341 void* memory = MapViewOfFile(file_mapping, FILE_MAP_ALL_ACCESS, 0, 0, size);
1342 if (memory) memmove(memory, initial, size);
1343 return new Win32MemoryMappedFile(file, file_mapping, memory, size);
1348 if (memory_) UnmapViewOfFile(memory_);
1349 if (file_mapping_) CloseHandle(file_mapping_);
1353std::optional<AddressSpaceReservation>
1357 DCHECK(Contains(address, size));
1371bool AddressSpaceReservation::SplitPlaceholder(
void* address,
size_t size) {
1372 DCHECK(Contains(address, size));
1373 return VirtualFree(address, size, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER);
1376bool AddressSpaceReservation::MergePlaceholders(
void* address,
size_t size) {
1377 DCHECK(Contains(address, size));
1378 return VirtualFree(address, size, MEM_RELEASE | MEM_COALESCE_PLACEHOLDERS);
1383 DCHECK(Contains(address, size));
1386 ? MEM_RESERVE | MEM_REPLACE_PLACEHOLDER
1387 : MEM_RESERVE | MEM_COMMIT | MEM_REPLACE_PLACEHOLDER;
1389 return VirtualAlloc2(GetCurrentProcess(), address, size, flags, protect,
1394 DCHECK(Contains(address, size));
1395 return VirtualFree(address, size, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER);
1402 DCHECK(Contains(address, size));
1406 HANDLE file_mapping = FileMappingFromSharedMemoryHandle(handle);
1408 size, MEM_REPLACE_PLACEHOLDER, protect,
nullptr, 0);
1412 DCHECK(Contains(address, size));
1416 MEM_PRESERVE_PLACEHOLDER);
1421 DCHECK(Contains(address, size));
1427 DCHECK(Contains(address, size));
1432 DCHECK(Contains(address, size));
1437 DCHECK(Contains(address, size));
1448#define DBGHELP_FUNCTION_LIST(V) \
1452 V(SymGetSearchPath) \
1453 V(SymLoadModule64) \
1455 V(SymGetSymFromAddr64) \
1456 V(SymGetLineFromAddr64) \
1457 V(SymFunctionTableAccess64) \
1458 V(SymGetModuleBase64)
1461#define TLHELP32_FUNCTION_LIST(V) \
1462 V(CreateToolhelp32Snapshot) \
1468#define DLL_FUNC_TYPE(name) _##name##_
1469#define DLL_FUNC_VAR(name) _##name
1486 IN PSTR UserSearchPath,
1494 IN DWORD64 BaseOfDll,
IN DWORD SizeOfDll);
1497 LPSTACKFRAME64 StackFrame,
PVOID ContextRecord,
1498 PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine,
1499 PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,
1500 PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine,
1501 PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress);
1503 IN HANDLE hProcess,
IN DWORD64 qwAddr, OUT PDWORD64 pdwDisplacement,
1504 OUT PIMAGEHLP_SYMBOL64 Symbol);
1507 OUT PDWORD pdwDisplacement, OUT PIMAGEHLP_LINE64 Line64);
1512using DLL_FUNC_TYPE(SymGetModuleBase64) = DWORD64(__stdcall*)(
1520 LPMODULEENTRY32W lpme);
1522 LPMODULEENTRY32W lpme);
1528#define DEF_DLL_FUNCTION(name) DLL_FUNC_TYPE(name) DLL_FUNC_VAR(name) = nullptr;
1531#undef DEF_DLL_FUNCTION
1537 static bool dbghelp_loaded =
false;
1539 if (dbghelp_loaded)
return true;
1544 module = LoadLibrary(TEXT("dbghelp.dll"));
1545 if (module ==
nullptr) {
1549#define LOAD_DLL_FUNC(name) \
1550 DLL_FUNC_VAR(name) = \
1551 reinterpret_cast<DLL_FUNC_TYPE(name)>(GetProcAddress(module, #name));
1559 module = LoadLibrary(TEXT(
"kernel32.dll"));
1560 if (module ==
nullptr) {
1564#define LOAD_DLL_FUNC(name) \
1565 DLL_FUNC_VAR(name) = \
1566 reinterpret_cast<DLL_FUNC_TYPE(name)>(GetProcAddress(module, #name));
1574#define DLL_FUNC_LOADED(name) (DLL_FUNC_VAR(name) != nullptr)&&
1579#undef DLL_FUNC_LOADED
1588#undef DBGHELP_FUNCTION_LIST
1589#undef TLHELP32_FUNCTION_LIST
1597 static std::vector<OS::SharedLibraryAddress>
result;
1599 static bool symbols_loaded =
false;
1601 if (symbols_loaded)
return result;
1606 ok = _SymInitialize(process_handle,
1611 DWORD options = _SymGetOptions();
1612 options |= SYMOPT_LOAD_LINES;
1613 options |= SYMOPT_FAIL_CRITICAL_ERRORS;
1614 options = _SymSetOptions(options);
1620 OS::Print(
"%d\n", err);
1624 HANDLE snapshot = _CreateToolhelp32Snapshot(
1627 if (snapshot == INVALID_HANDLE_VALUE)
return result;
1628 MODULEENTRY32W module_entry;
1629 module_entry.dwSize =
sizeof(module_entry);
1630 BOOL cont = _Module32FirstW(snapshot, &module_entry);
1635 base = _SymLoadModule64(
1638 reinterpret_cast<PSTR
>(module_entry.szExePath),
1639 reinterpret_cast<PSTR
>(module_entry.szModule),
1640 reinterpret_cast<DWORD64
>(module_entry.modBaseAddr),
1641 module_entry.modBaseSize);
1644 if (err != ERROR_MOD_NOT_FOUND &&
1645 err != ERROR_INVALID_HANDLE) {
1650 int lib_name_length = WideCharToMultiByte(
1651 CP_UTF8, 0, module_entry.szExePath, -1,
nullptr, 0,
nullptr,
nullptr);
1652 std::string lib_name(lib_name_length, 0);
1653 WideCharToMultiByte(CP_UTF8, 0, module_entry.szExePath, -1, &lib_name[0],
1654 lib_name_length,
nullptr,
nullptr);
1656 lib_name,
reinterpret_cast<uintptr_t
>(module_entry.modBaseAddr),
1657 reinterpret_cast<uintptr_t
>(module_entry.modBaseAddr +
1658 module_entry.modBaseSize)));
1659 cont = _Module32NextW(snapshot, &module_entry);
1661 CloseHandle(snapshot);
1663 symbols_loaded =
true;
1674 HANDLE process_handle = GetCurrentProcess();
1682 return std::vector<OS::SharedLibraryAddress>();
1692#elif defined(__MINGW32__)
1701#if defined(V8_OS_WIN)
1702void EnsureConsoleOutputWin32() {
1704 SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
1705 UINT existing_flags = SetErrorMode(new_flags);
1706 SetErrorMode(existing_flags | new_flags);
1707#if defined(_MSC_VER)
1708 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
1709 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
1710 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
1711 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
1712 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
1713 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
1714 _set_error_mode(_OUT_TO_STDERR);
1748 : stack_size_(options.stack_size()), start_semaphore_(nullptr) {
1800 return TlsGetValue(
static_cast<DWORD>(
key));
1818 uintptr_t vm_start =
RoundUp(boundary_start, alignment);
1819 uintptr_t vm_end = 0;
1820 MEMORY_BASIC_INFORMATION
mi;
1823 while (vm_start < boundary_end &&
1824 VirtualQuery(
reinterpret_cast<LPCVOID
>(vm_start), &mi,
sizeof(mi)) !=
1826 vm_start =
reinterpret_cast<uintptr_t
>(
mi.BaseAddress);
1827 vm_end = vm_start +
mi.RegionSize;
1828 if (
mi.State == MEM_FREE) {
1832 const uintptr_t overlap_start =
1833 RoundUp(std::max(vm_start, boundary_start), alignment);
1834 const uintptr_t overlap_end =
1835 RoundDown(std::min(vm_end, boundary_end), alignment);
1836 if (overlap_start < overlap_end &&
1837 overlap_end - overlap_start >= minimum_size) {
1838 return OS::MemoryRange{overlap_start, overlap_end};
1850#if defined(V8_TARGET_ARCH_X64)
1851 return reinterpret_cast<void*
>(
1852 reinterpret_cast<NT_TIB64*
>(NtCurrentTeb())->StackBase);
1853#elif defined(V8_TARGET_ARCH_32_BIT)
1854 return reinterpret_cast<void*
>(
1855 reinterpret_cast<NT_TIB*
>(NtCurrentTeb())->StackBase);
1856#elif defined(V8_TARGET_ARCH_ARM64)
1860 ::GetCurrentThreadStackLimits(&lowLimit, &highLimit);
1861 return reinterpret_cast<void*
>(highLimit);
1863#error Unsupported ObtainCurrentThreadStackStart.
1870 return _AddressOfReturnAddress();
1872 return __builtin_frame_address(0);
constexpr int kPageSizeBits
V8_WARN_UNUSED_RESULT bool DiscardSystemPages(void *address, size_t size)
V8_WARN_UNUSED_RESULT bool Allocate(void *address, size_t size, OS::MemoryPermission access)
V8_WARN_UNUSED_RESULT bool RecommitPages(void *address, size_t size, OS::MemoryPermission access)
V8_WARN_UNUSED_RESULT bool Free(void *address, size_t size)
V8_WARN_UNUSED_RESULT bool SetPermissions(void *address, size_t size, OS::MemoryPermission access)
V8_WARN_UNUSED_RESULT bool DecommitPages(void *address, size_t size)
V8_WARN_UNUSED_RESULT bool FreeShared(void *address, size_t size)
V8_WARN_UNUSED_RESULT std::optional< AddressSpaceReservation > CreateSubReservation(void *address, size_t size, OS::MemoryPermission max_permission)
V8_WARN_UNUSED_RESULT bool AllocateShared(void *address, size_t size, OS::MemoryPermission access, PlatformSharedMemoryHandle handle, uint64_t offset)
static V8_WARN_UNUSED_RESULT bool FreeSubReservation(AddressSpaceReservation reservation)
static MemoryMappedFile * create(const char *name, size_t size, void *initial)
static MemoryMappedFile * open(const char *name, FileMode mode=FileMode::kReadWrite)
static void Sleep(TimeDelta interval)
static V8_WARN_UNUSED_RESULT void * AllocateShared(size_t size, MemoryPermission access)
static void * GetRandomMmapAddr()
static V8_WARN_UNUSED_RESULT bool SealPages(void *address, size_t size)
static void SignalCodeMovingGC()
static static va_list args
static bool HasLazyCommits()
static V8_WARN_UNUSED_RESULT bool CanReserveAddressSpace()
static V8_WARN_UNUSED_RESULT bool DiscardSystemPages(void *address, size_t size)
static size_t AllocatePageSize()
static void ExitProcess(int exit_code)
static void SetDataReadOnly(void *address, size_t size)
static bool IsHardwareEnforcedShadowStacksEnabled()
static void FreeAddressSpaceReservation(AddressSpaceReservation reservation)
static const char *const LogFileOpenMode
static const int kStackWalkMaxNameLen
static int GetCurrentThreadIdInternal()
static int GetLastError()
static PlatformSharedMemoryHandle CreateSharedMemoryHandleForTesting(size_t size)
static void FreeShared(void *address, size_t size)
static V8_WARN_UNUSED_RESULT bool SetPermissions(void *address, size_t size, MemoryPermission access)
static FILE * OpenTemporaryFile()
static FILE * FOpen(const char *path, const char *mode)
static void Initialize(AbortMode abort_mode, const char *const gc_fake_mmap)
static V8_WARN_UNUSED_RESULT bool DecommitPages(void *address, size_t size)
static int GetCurrentProcessId()
static size_t CommitPageSize()
static int GetPeakMemoryUsageKb()
static void Release(void *address, size_t size)
static V8_WARN_UNUSED_RESULT bool RecommitPages(void *address, size_t size, MemoryPermission access)
static std::vector< SharedLibraryAddress > GetSharedLibraryAddresses()
static char DirectorySeparator()
static void StrNCpy(char *dest, int length, const char *src, size_t n)
static int GetUserTime(uint32_t *secs, uint32_t *usecs)
static int GetCurrentThreadId()
static int ActivationFrameAlignment()
static V8_WARN_UNUSED_RESULT void * Allocate(void *address, size_t size, size_t alignment, MemoryPermission access)
static void DestroySharedMemoryHandle(PlatformSharedMemoryHandle handle)
static TimezoneCache * CreateTimezoneCache()
static double TimeCurrentMillis()
static bool isDirectorySeparator(const char ch)
static void SetRandomMmapSeed(int64_t seed)
friend class MemoryMappedFile
static V8_WARN_UNUSED_RESULT std::optional< AddressSpaceReservation > CreateAddressSpaceReservation(void *hint, size_t size, size_t alignment, MemoryPermission max_permission)
friend class AddressSpaceReservation
static void Free(void *address, size_t size)
static std::optional< MemoryRange > GetFirstFreeMemoryRangeWithin(Address boundary_start, Address boundary_end, size_t minimum_size, size_t alignment)
static bool Remove(const char *path)
static void AdjustSchedulingParams()
void Sleep(TimeDelta interval) const
PreciseSleepTimer & operator=(const PreciseSleepTimer &other)=delete
bool IsInitialized() const
static V8_NOINLINE StackSlot GetCurrentStackPosition()
static Stack::StackSlot ObtainCurrentThreadStackStart()
Thread(const Options &options)
V8_WARN_UNUSED_RESULT bool Start()
static void DeleteThreadLocalKey(LocalStorageKey key)
static LocalStorageKey CreateThreadLocalKey()
static void * GetThreadLocal(LocalStorageKey key)
void set_name(const char *name)
char name_[kMaxThreadNameLength]
void NotifyStartedAndRun()
static void SetThreadLocal(LocalStorageKey key, void *value)
HANDLE const file_mapping_
size_t size() const final
~Win32MemoryMappedFile() final
Win32MemoryMappedFile(HANDLE file, HANDLE file_mapping, void *memory, size_t size)
static const int64_t kTimeScaler
bool InDST(WindowsTimezoneCache *cache)
static const bool kShortTzNames
int64_t LocalOffset(WindowsTimezoneCache *cache)
int64_t DaylightSavingsOffset(WindowsTimezoneCache *cache)
static const int64_t kMsPerMinute
char * LocalTimezone(WindowsTimezoneCache *cache)
static const int64_t kTimeEpoc
char dst_tz_name_[kTzNameSize]
double LocalTimeOffset(double time, bool is_utc) override
const char * LocalTimezone(double time) override
void InitializeIfNeeded()
void Clear(TimeZoneDetection) override
double DaylightSavingsOffset(double time) override
~WindowsTimezoneCache() override
TIME_ZONE_INFORMATION tzinfo_
const char * GuessTimezoneNameFromBias(int bias)
char std_tz_name_[kTzNameSize]
static const int kTzNameSize
Handle< SharedFunctionInfo > info
refactor address components for immediate indexing make OptimizeMaglevOnNextCall optimize to turbofan instead of maglev filter for tracing turbofan compilation trace turbo cfg trace TurboFan s graph trimmer trace TurboFan s control equivalence trace TurboFan s register allocator trace stack load store counters for optimized code in run fuzzing &&concurrent_recompilation trace_turbo trace_turbo_scheduled trace_turbo_stack_accesses verify TurboFan machine graph of code stubs enable FixedArray bounds checks print TurboFan statistics of wasm compilations maximum cumulative size of bytecode considered for inlining scale factor of bytecode size used to calculate the inlining budget * KB
ZoneVector< RpoNumber > & result
#define DEFINE_LAZY_LEAKY_OBJECT_GETTER(T, FunctionName,...)
base::ElapsedTimer timer_
#define LAZY_MUTEX_INITIALIZER
MapViewOfFile3_t MapViewOfFile3
int GetProtectionFromMemoryPermission(OS::MemoryPermission access)
PVOID(__stdcall * VirtualAlloc2_t)(HANDLE, PVOID, SIZE_T, ULONG, ULONG, MEM_EXTENDED_PARAMETER *, ULONG)
@ kExitWithFailureAndIgnoreDcheckFailures
@ kExitWithSuccessAndIgnoreDcheckFailures
PVOID(__stdcall * MapViewOfFile3_t)(HANDLE, HANDLE, PVOID, ULONG64, SIZE_T, ULONG, ULONG, MEM_EXTENDED_PARAMETER *, ULONG)
static std::wstring ConvertUtf8StringToUtf16(const char *str)
static void * ThreadEntry(void *arg)
VirtualAlloc2_t VirtualAlloc2
static bool LoadDbgHelpAndTlHelp32()
UnmapViewOfFile2_t UnmapViewOfFile2
PVOID(__stdcall * UnmapViewOfFile2_t)(HANDLE, PVOID, ULONG)
static std::vector< OS::SharedLibraryAddress > LoadSymbols(HANDLE process_handle)
V8_BASE_EXPORT int const char va_list args
LockGuard< Mutex > MutexGuard
static OutputMode output_mode
static const pthread_t kNoThread
int VSNPrintF(Vector< char > str, const char *format, va_list args)
static void VPrintHelper(FILE *stream, const char *format, va_list args)
V8_INLINE IndirectHandle< T > handle(Tagged< T > object, Isolate *isolate)
static constexpr PlatformSharedMemoryHandle kInvalidSharedMemoryHandle
intptr_t PlatformSharedMemoryHandle
#define DCHECK_LE(v1, v2)
#define CHECK_GT(lhs, rhs)
#define DCHECK_IMPLIES(v1, v2)
#define DCHECK_NE(v1, v2)
#define CHECK_NE(lhs, rhs)
#define CHECK_EQ(lhs, rhs)
#define DCHECK(condition)
#define DCHECK_EQ(v1, v2)
constexpr T RoundUp(T x, intptr_t m)
void * AlignedAddress(void *address, size_t alignment)
constexpr T RoundDown(T x, intptr_t m)
std::unique_ptr< ValueMirror > key