-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSysInfo.cpp
More file actions
555 lines (487 loc) · 14.5 KB
/
SysInfo.cpp
File metadata and controls
555 lines (487 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "CommonCorePch.h"
#ifdef _WIN32
#include <psapi.h>
#endif
#include <wincrypt.h>
#include <VersionHelpers.h>
#ifdef __APPLE__
#include <sys/sysctl.h> // sysctl*
#elif defined(__linux__) || defined(__FreeBSD__)
#include <unistd.h> // sysconf
#endif
// Initialization order
// AB AutoSystemInfo
// AD PerfCounter
// AE PerfCounterSet
// AM Output/Configuration
// AN MemProtectHeap
// AP DbgHelpSymbolManager
// AQ CFGLogger
// AR LeakReport
// AS JavascriptDispatch/RecyclerObjectDumper
// AT HeapAllocator/RecyclerHeuristic
// AU RecyclerWriteBarrierManager
#pragma warning(disable:4075) // initializers put in unrecognized initialization area on purpose
#pragma init_seg(".CRT$XCAB")
#if SYSINFO_IMAGE_BASE_AVAILABLE
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#endif
AutoSystemInfo AutoSystemInfo::Data INIT_PRIORITY(300);
#if DBG
bool
AutoSystemInfo::IsInitialized()
{
return AutoSystemInfo::Data.initialized;
}
#endif
bool
AutoSystemInfo::ShouldQCMoreFrequently()
{
return Data.shouldQCMoreFrequently;
}
bool
AutoSystemInfo::SupportsOnlyMultiThreadedCOM()
{
return Data.supportsOnlyMultiThreadedCOM;
}
bool
AutoSystemInfo::IsLowMemoryDevice()
{
return Data.isLowMemoryDevice;
}
void
AutoSystemInfo::Initialize()
{
Assert(!initialized);
#ifndef _WIN32
PAL_InitializeChakraCore();
majorVersion = CHAKRA_CORE_MAJOR_VERSION;
minorVersion = CHAKRA_CORE_MINOR_VERSION;
#endif
processHandle = GetCurrentProcess();
GetSystemInfo(this);
// Make the page size constant so calculation are faster.
Assert(this->dwPageSize == AutoSystemInfo::PageSize);
#if defined(_M_IX86) || defined(_M_X64)
get_cpuid(CPUInfo, 1);
isAtom = CheckForAtom();
#endif
#if defined(_M_ARM32_OR_ARM64)
armDivAvailable = IsProcessorFeaturePresent(PF_ARM_DIVIDE_INSTRUCTION_AVAILABLE) ? true : false;
#endif
allocationGranularityPageCount = dwAllocationGranularity / dwPageSize;
isWindows8OrGreater = IsWindows8OrGreater();
isWindows8Point1OrGreater = IsWindows8Point1OrGreater();
binaryName[0] = _u('\0');
#if SYSINFO_IMAGE_BASE_AVAILABLE
dllLoadAddress = (UINT_PTR)&__ImageBase;
dllHighAddress = (UINT_PTR)&__ImageBase +
((PIMAGE_NT_HEADERS)(((char *)&__ImageBase) + __ImageBase.e_lfanew))->OptionalHeader.SizeOfImage;
#endif
InitPhysicalProcessorCount();
#if DBG
initialized = true;
#endif
WCHAR DisableDebugScopeCaptureFlag[MAX_PATH];
if (::GetEnvironmentVariable(_u("JS_DEBUG_SCOPE"), DisableDebugScopeCaptureFlag, _countof(DisableDebugScopeCaptureFlag)) != 0)
{
disableDebugScopeCapture = true;
}
else
{
disableDebugScopeCapture = false;
}
this->supportsOnlyMultiThreadedCOM = false;
#if defined(__ANDROID__) || defined(__IOS__)
this->isLowMemoryDevice = true;
this->shouldQCMoreFrequently = true;
#else
this->shouldQCMoreFrequently = false;
this->isLowMemoryDevice = false;
#endif
// 0 indicates we haven't retrieved the available commit. We get it lazily.
this->availableCommit = 0;
ChakraBinaryAutoSystemInfoInit(this);
}
bool
AutoSystemInfo::InitPhysicalProcessorCount()
{
DWORD countPhysicalProcessor = 0;
#ifdef _WIN32
DWORD size = 0;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pBufferCurrent;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pBufferStart;
BOOL bResult;
#endif // _WIN32
Assert(!this->initialized);
// Initialize physical processor to number of logical processors.
// If anything below fails, we still need an approximate value
this->dwNumberOfPhysicalProcessors = this->dwNumberOfProcessors;
#if defined(_WIN32)
bResult = GetLogicalProcessorInformation(NULL, &size);
if (bResult || GetLastError() != ERROR_INSUFFICIENT_BUFFER || !size)
{
return false;
}
DWORD count = (size) / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
if (size != count * sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION))
{
Assert(false);
return false;
}
pBufferCurrent = pBufferStart = NoCheckHeapNewArray(SYSTEM_LOGICAL_PROCESSOR_INFORMATION, (size_t)count);
if (!pBufferCurrent)
{
return false;
}
bResult = GetLogicalProcessorInformation(pBufferCurrent, &size);
if (!bResult)
{
NoCheckHeapDeleteArray(count, pBufferStart);
return false;
}
while (pBufferCurrent < (pBufferStart + count))
{
if (pBufferCurrent->Relationship == RelationProcessorCore)
{
countPhysicalProcessor++;
}
pBufferCurrent++;
}
NoCheckHeapDeleteArray(count, pBufferStart);
#elif defined(__APPLE__)
std::size_t szCount = sizeof(countPhysicalProcessor);
sysctlbyname("hw.physicalcpu", &countPhysicalProcessor, &szCount, nullptr, 0);
if (countPhysicalProcessor < 1)
{
int nMIB[2] = {CTL_HW, HW_NCPU}; // fallback. Depracated on latest OS
sysctl(nMIB, 2, &countPhysicalProcessor, &szCount, nullptr, 0);
if (countPhysicalProcessor < 1)
{
countPhysicalProcessor = 1;
}
}
#elif defined(__linux__) || defined(__FreeBSD__)
countPhysicalProcessor = sysconf(_SC_NPROCESSORS_ONLN);
#else
// implementation for __linux__ should work for some others.
// same applies to __APPLE__ implementation
// instead of reimplementing, add corresponding preprocessors above
#error "NOT Implemented"
#endif
this->dwNumberOfPhysicalProcessors = countPhysicalProcessor;
return true;
}
#if SYSINFO_IMAGE_BASE_AVAILABLE
bool
AutoSystemInfo::IsJscriptModulePointer(void * ptr)
{
return ((UINT_PTR)ptr >= Data.dllLoadAddress && (UINT_PTR)ptr < Data.dllHighAddress);
}
UINT_PTR
AutoSystemInfo::GetChakraBaseAddr() const
{
return dllLoadAddress;
}
#endif
uint
AutoSystemInfo::GetAllocationGranularityPageCount() const
{
Assert(initialized);
return this->allocationGranularityPageCount;
}
uint
AutoSystemInfo::GetAllocationGranularityPageSize() const
{
Assert(initialized);
return this->allocationGranularityPageCount * PageSize;
}
#if defined(_M_IX86) || defined(_M_X64)
bool
AutoSystemInfo::VirtualSseAvailable(const int sseLevel) const
{
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
return CONFIG_FLAG(Sse) < 0 || CONFIG_FLAG(Sse) >= sseLevel;
#else
return true;
#endif
}
#endif
BOOL
AutoSystemInfo::SSE2Available() const
{
Assert(initialized);
#if defined(_M_X64) || defined(_M_ARM32_OR_ARM64)
return true;
#elif defined(_M_IX86)
#if defined(_WIN32)
return VirtualSseAvailable(2) && (CPUInfo[3] & (1 << 26));
#else
return false; // TODO: xplat support
#endif
#else
return false;
#endif
}
#if defined(_M_IX86) || defined(_M_X64)
BOOL
AutoSystemInfo::SSE3Available() const
{
Assert(initialized);
return VirtualSseAvailable(3) && (CPUInfo[2] & 0x1);
}
BOOL
AutoSystemInfo::SSE4_1Available() const
{
Assert(initialized);
return VirtualSseAvailable(4) && (CPUInfo[2] & (0x1 << 19));
}
BOOL
AutoSystemInfo::SSE4_2Available() const
{
Assert(initialized);
return VirtualSseAvailable(4) && (CPUInfo[2] & (0x1 << 20));
}
BOOL
AutoSystemInfo::PopCntAvailable() const
{
Assert(initialized);
return VirtualSseAvailable(4) && (CPUInfo[2] & (1 << 23));
}
BOOL
AutoSystemInfo::LZCntAvailable() const
{
Assert(initialized);
int CPUInfo[4];
get_cpuid(CPUInfo, 0x80000001);
return VirtualSseAvailable(4) && (CPUInfo[2] & (1 << 5));
}
BOOL
AutoSystemInfo::TZCntAvailable() const
{
Assert(initialized);
int CPUInfo[4];
get_cpuid(CPUInfo, 7);
return VirtualSseAvailable(4) && (CPUInfo[1] & (1 << 3));
}
bool
AutoSystemInfo::IsAtomPlatform() const
{
return isAtom;
}
bool
AutoSystemInfo::CheckForAtom() const
{
int CPUInfo[4];
const int GENUINE_INTEL_0 = 0x756e6547,
GENUINE_INTEL_1 = 0x49656e69,
GENUINE_INTEL_2 = 0x6c65746e;
const int PLATFORM_MASK = 0x0fff3ff0;
const int ATOM_PLATFORM_A = 0x0106c0, /* bonnell - extended model 1c, type 0, family code 6 */
ATOM_PLATFORM_B = 0x020660, /* lincroft - extended model 26, type 0, family code 6 */
ATOM_PLATFORM_C = 0x020670, /* saltwell - extended model 27, type 0, family code 6 */
ATOM_PLATFORM_D = 0x030650, /* tbd - extended model 35, type 0, family code 6 */
ATOM_PLATFORM_E = 0x030660, /* tbd - extended model 36, type 0, family code 6 */
ATOM_PLATFORM_F = 0x030670; /* tbd - extended model 37, type 0, family code 6 */
int platformSignature;
get_cpuid(CPUInfo, 0);
// See if CPU is ATOM HW. First check if CPU is genuine Intel.
if( CPUInfo[1]==GENUINE_INTEL_0 &&
CPUInfo[3]==GENUINE_INTEL_1 &&
CPUInfo[2]==GENUINE_INTEL_2)
{
get_cpuid(CPUInfo, 1);
// get platform signature
platformSignature = CPUInfo[0];
if((( PLATFORM_MASK & platformSignature) == ATOM_PLATFORM_A) ||
((PLATFORM_MASK & platformSignature) == ATOM_PLATFORM_B) ||
((PLATFORM_MASK & platformSignature) == ATOM_PLATFORM_C) ||
((PLATFORM_MASK & platformSignature) == ATOM_PLATFORM_D) ||
((PLATFORM_MASK & platformSignature) == ATOM_PLATFORM_E) ||
((PLATFORM_MASK & platformSignature) == ATOM_PLATFORM_F))
{
return true;
}
}
return false;
}
#endif
bool
AutoSystemInfo::IsWin8OrLater()
{
#if defined(WINVER) && WINVER >= _WIN32_WINNT_WIN8
return true;
#else
return isWindows8OrGreater;
#endif
}
bool
AutoSystemInfo::IsWin8Point1OrLater()
{
#if defined(WINVER) && WINVER >= _WIN32_WINNT_WINBLUE
return true;
#else
return isWindows8Point1OrGreater;
#endif
}
#if defined(_CONTROL_FLOW_GUARD)
bool
AutoSystemInfo::IsWinThresholdOrLater()
{
#if defined(_M_ARM64) || (defined(WINVER) && WINVER >= _WIN32_WINNT_WIN10)
return true;
#else
return IsWindowsThresholdOrGreater();
#endif
}
#endif
DWORD AutoSystemInfo::SaveModuleFileName(HANDLE hMod)
{
return ::GetModuleFileNameW((HMODULE)hMod, Data.binaryName, MAX_PATH);
}
LPCWSTR AutoSystemInfo::GetJscriptDllFileName()
{
return (LPCWSTR)Data.binaryName;
}
#ifdef _WIN32
/* static */
HMODULE AutoSystemInfo::GetCRTHandle()
{
return GetModuleHandle(_u("msvcrt.dll"));
}
bool
AutoSystemInfo::IsCRTModulePointer(uintptr_t ptr)
{
HMODULE base = GetCRTHandle();
if (Data.crtSize == 0)
{
MODULEINFO info;
if (!GetModuleInformation(GetCurrentProcess(), base, &info, sizeof(MODULEINFO)))
{
AssertOrFailFast(UNREACHED);
}
Data.crtSize = info.SizeOfImage;
Assert(base == info.lpBaseOfDll);
}
return (ptr >= (uintptr_t)base && ptr < (uintptr_t)base + Data.crtSize);
}
#endif
bool AutoSystemInfo::IsLowMemoryProcess()
{
ULONG64 commit = ULONG64(-1);
this->GetAvailableCommit(&commit);
return commit <= CONFIG_FLAG(LowMemoryCap);
}
BOOL AutoSystemInfo::GetAvailableCommit(ULONG64 *pCommit)
{
Assert(initialized);
// Non-zero value indicates we've been here before.
if (this->availableCommit == 0)
{
return false;
}
*pCommit = this->availableCommit;
return true;
}
void AutoSystemInfo::SetAvailableCommit(ULONG64 commit)
{
::InterlockedCompareExchange64((volatile LONG64 *)&this->availableCommit, commit, 0);
}
//
// Returns the major and minor version of the loaded binary. If the version info has been fetched once, it will be cached
// and returned without any system calls to find the version number.
//
HRESULT AutoSystemInfo::GetJscriptFileVersion(DWORD* majorVersion, DWORD* minorVersion, DWORD *buildDateHash, DWORD *buildTimeHash)
{
HRESULT hr = E_FAIL;
if(AutoSystemInfo::Data.majorVersion == 0 && AutoSystemInfo::Data.minorVersion == 0)
{
// uninitialized state - call the system API to get the version info.
LPCWSTR jscriptDllName = GetJscriptDllFileName();
hr = GetVersionInfo(jscriptDllName, majorVersion, minorVersion);
AutoSystemInfo::Data.majorVersion = *majorVersion;
AutoSystemInfo::Data.minorVersion = *minorVersion;
}
else if(AutoSystemInfo::Data.majorVersion != INVALID_VERSION)
{
// if the cached copy is valid, use it and return S_OK.
*majorVersion = AutoSystemInfo::Data.majorVersion;
*minorVersion = AutoSystemInfo::Data.minorVersion;
hr = S_OK;
}
if (buildDateHash)
{
*buildDateHash = AutoSystemInfo::Data.buildDateHash;
}
if (buildTimeHash)
{
*buildTimeHash = AutoSystemInfo::Data.buildTimeHash;
}
return hr;
}
//
// Returns the major and minor version of the binary passed as argument.
//
HRESULT AutoSystemInfo::GetVersionInfo(_In_ LPCWSTR pszPath, DWORD* majorVersion, DWORD* minorVersion)
{
#ifdef _WIN32
DWORD dwTemp;
DWORD cbVersionSz;
HRESULT hr = E_FAIL;
BYTE* pVerBuffer = NULL;
VS_FIXEDFILEINFO* pFileInfo = NULL;
cbVersionSz = GetFileVersionInfoSizeEx(FILE_VER_GET_LOCALISED, pszPath, &dwTemp);
if(cbVersionSz > 0)
{
pVerBuffer = NoCheckHeapNewArray(BYTE, cbVersionSz);
if(pVerBuffer)
{
if(GetFileVersionInfoEx(FILE_VER_GET_LOCALISED|FILE_VER_GET_NEUTRAL, pszPath, 0, cbVersionSz, pVerBuffer))
{
UINT uiSz = sizeof(VS_FIXEDFILEINFO);
if(!VerQueryValue(pVerBuffer, _u("\\"), (LPVOID*)&pFileInfo, &uiSz))
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
else
{
hr = S_OK;
}
}
else
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
}
else
{
hr = E_OUTOFMEMORY;
}
}
if(SUCCEEDED(hr))
{
*majorVersion = pFileInfo->dwFileVersionMS;
*minorVersion = pFileInfo->dwFileVersionLS;
}
else
{
*majorVersion = INVALID_VERSION;
*minorVersion = INVALID_VERSION;
}
if(pVerBuffer)
{
NoCheckHeapDeleteArray(cbVersionSz, pVerBuffer);
}
return hr;
#else // !_WIN32
// xplat-todo: how to handle version resource?
*majorVersion = INVALID_VERSION;
*minorVersion = INVALID_VERSION;
return NOERROR;
#endif
}