Skip to content

Commit 891f8f1

Browse files
author
Requiem
committed
feat: improved timing checks
2 parents fda844a + 55d50c0 commit 891f8f1

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/cli.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ struct SHA256 {
150150
u32 s[8] = {}; // from h0 to h7
151151

152152
// Initialize state to SHA-256 IVs so that compiler doesn't complain
153-
void init() {
153+
SHA256() {
154154
len = 0;
155155
bits = 0;
156156
s[0] = 0x6a09e667;
@@ -291,10 +291,11 @@ static std::string exe_path() {
291291
std::string compute_self_sha256() {
292292
std::string path = exe_path();
293293
if (path.empty()) return {};
294+
294295
std::ifstream ifs(path, std::ios::binary);
295296
if (!ifs) return {};
297+
296298
SHA256 sha;
297-
sha.init();
298299

299300
std::vector<char> chunk(64 * 1024);
300301
while (ifs) {
@@ -307,9 +308,12 @@ std::string compute_self_sha256() {
307308

308309
u8 digest[32];
309310
sha.final(digest);
310-
static const char hex[] = "0123456789abcdef";
311+
311312
std::string out;
312313
out.reserve(64);
314+
315+
static constexpr char hex[] = "0123456789abcdef";
316+
313317
for (int i = 0; i < 32; ++i) {
314318
out.push_back(hex[(digest[i] >> 4) & 0xF]);
315319
out.push_back(hex[digest[i] & 0xF]);

src/vmaware.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@
375375
#include <type_traits>
376376
#include <stdexcept>
377377
#include <numeric>
378+
#include <atomic>
378379

379380
#if (WINDOWS)
380381
#include <windows.h>
@@ -4698,6 +4699,7 @@ struct VM {
46984699
#if (MSVC)
46994700
// make regs volatile so writes cannot be optimized out, if this isn't added and the code is compiled in release mode, cycles would be around 40 even under Hyper-V
47004701
volatile int regs[4]{};
4702+
47014703
// ensure the CPU pipeline is drained of previous loads before we start the clock
47024704
_mm_lfence();
47034705

@@ -4988,8 +4990,10 @@ struct VM {
49884990
// collect results
49894991
const u64 a = t1_start.load(std::memory_order_acquire);
49904992
const u64 b = t1_end.load(std::memory_order_acquire);
4991-
const u64 c = t2_start.load(std::memory_order_acquire);
4992-
const u64 d = t2_end.load(std::memory_order_acquire);
4993+
#ifdef __VMAWARE_DEBUG__
4994+
const u64 c = t2_start.load(std::memory_order_acquire);
4995+
const u64 d = t2_end.load(std::memory_order_acquire);
4996+
#endif
49934997
const u64 acc = t2_accum.load(std::memory_order_acquire);
49944998

49954999
const u64 t1_delta = (b > a) ? (b - a) : 0;

0 commit comments

Comments
 (0)