Skip to content

Commit ae3dfeb

Browse files
author
Requiem
committed
fix: TSC rewinds and WINE flags on AR,
1 parent 3b8edd8 commit ae3dfeb

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/cli.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ struct SHA256 {
184184
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
185185
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
186186
};
187-
u32 m[64];
187+
u32 m[64]{};
188188
for (u32 i = 0, j = 0; i < 16; ++i, j += 4) {
189189
m[i] = (u32)buf[j] << 24 | (u32)buf[j + 1] << 16 | (u32)buf[j + 2] << 8 | (u32)buf[j + 3];
190190
}
@@ -288,7 +288,7 @@ static std::string exe_path() {
288288
#endif
289289
}
290290

291-
std::string compute_self_sha256() {
291+
static std::string compute_self_sha256() {
292292
std::string path = exe_path();
293293
if (path.empty()) return {};
294294

src/vmaware.hpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4947,7 +4947,13 @@ struct VM {
49474947
for (unsigned i = 0; i < CPUID_ITER; ++i) {
49484948
// read rdtsc and accumulate delta
49494949
const u64 now = rdtsc();
4950-
acc += (now >= last) ? (now - last) : (u64)((u64)0 - last + now);
4950+
4951+
// If now < last, the hypervisor rewound the TSC or it's a very rare 64-bit overflow
4952+
// we do not increment acc to ensure ratio t2_delta / t1_delta drops below 0.95
4953+
if (now >= last) {
4954+
acc += (now - last);
4955+
}
4956+
49514957
last = now;
49524958

49534959
// store latency if buffer has space
@@ -4964,7 +4970,11 @@ struct VM {
49644970

49654971
// final rdtsc after detecting finish
49664972
const u64 final_now = rdtsc();
4967-
acc += (final_now >= last) ? (final_now - last) : (u64)((u64)0 - last + final_now);
4973+
4974+
if (final_now >= last) {
4975+
acc += (final_now - last);
4976+
}
4977+
49684978
last = final_now;
49694979

49704980
// publish results
@@ -5011,7 +5021,7 @@ struct VM {
50115021

50125022
if (cpuid_latency >= cycle_threshold) {
50135023
debug("TIMER: Detected a vmexit on CPUID");
5014-
return true;
5024+
return core::add(brands::NULL_BRAND, 100); // to prevent FPs due to kernel noise
50155025
}
50165026
else if (cpuid_latency <= 25) {
50175027
debug("TIMER: Detected a hypervisor downscaling CPUID latency");
@@ -6573,16 +6583,6 @@ struct VM {
65736583
debug("FIRMWARE: C2 and C3 latencies indicate VM");
65746584
return true;
65756585
}
6576-
6577-
if (buffer_len >= 276) {
6578-
u64 hypervisor_vid = 0;
6579-
memcpy(&hypervisor_vid, buffer + 268, 8);
6580-
6581-
if (hypervisor_vid != 0) {
6582-
debug("FIRMWARE: FACP 'Hypervisor Vendor Identity' field is occupied");
6583-
return true;
6584-
}
6585-
}
65866586
}
65876587

65886588
return false;
@@ -7513,6 +7513,10 @@ struct VM {
75137513
}
75147514
#endif
75157515

7516+
// ARM CPUs trigger this check
7517+
if (util::is_running_under_translator())
7518+
return false;
7519+
75167520
const HMODULE kernel32 = GetModuleHandleA("kernel32.dll");
75177521
const HMODULE ntdll = util::get_ntdll();
75187522
if (!kernel32 || !ntdll) {
@@ -11217,8 +11221,6 @@ struct VM {
1121711221
const unsigned char* n =
1121811222
reinterpret_cast<const unsigned char*>(needle);
1121911223

11220-
const size_t nlen = strlen(needle);
11221-
1122211224
for (; *h; ++h) {
1122311225
size_t i = 0;
1122411226
for (;; ++i) {
@@ -11361,8 +11363,6 @@ struct VM {
1136111363
{ 0x4B564D00u, 0x4B564DFFu }
1136211364
};
1136311365

11364-
static thread_local bool g_msr_faulted = false;
11365-
1136611366
auto try_read = [](u32 msr_index) -> bool {
1136711367
#if (MSVC)
1136811368
unsigned __int64 value = 0;
@@ -11375,7 +11375,7 @@ struct VM {
1137511375
return false;
1137611376
}
1137711377
#elif (GCC || CLANG)
11378-
g_msr_faulted = false;
11378+
static thread_local bool g_msr_faulted = false;
1137911379

1138011380
auto veh_handler = [](PEXCEPTION_POINTERS info) -> LONG {
1138111381
if (info->ExceptionRecord->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION) {

0 commit comments

Comments
 (0)