Skip to content

Commit 4579fdc

Browse files
author
Requiem
committed
fix: Clang and C++ 11 compilation
1 parent ffbb832 commit 4579fdc

1 file changed

Lines changed: 48 additions & 50 deletions

File tree

src/vmaware.hpp

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,23 @@
256256
#define VMA_CPLUSPLUS __cplusplus
257257
#endif
258258

259-
#if VMA_CPLUSPLUS >= 202300L
260-
#define CPP 23
259+
#if VMA_CPLUSPLUS >= 202302L
260+
#define VMA_CPP 23
261261
#elif VMA_CPLUSPLUS >= 202002L
262-
#define CPP 20
262+
#define VMA_CPP 20
263263
#elif VMA_CPLUSPLUS >= 201703L
264-
#define CPP 17
264+
#define VMA_CPP 17
265265
#elif VMA_CPLUSPLUS >= 201402L
266-
#define CPP 14
266+
#define VMA_CPP 14
267267
#elif VMA_CPLUSPLUS >= 201103L
268-
#define CPP 11
268+
#define VMA_CPP 11
269+
#elif VMA_CPLUSPLUS >= 199711L
270+
#define VMA_CPP 98 /* C++98 or C++03 */
269271
#else
270-
#error "Unsupported C++ standard (pre-C++11 or unknown)."
272+
#error "Unsupported C++ standard (pre-C++98 or unknown)."
271273
#endif
272274

273-
#if (CPP < 11 && !WINDOWS)
275+
#if (VMA_CPP < 11 && !WINDOWS)
274276
#error "VMAware only supports C++11 or above, set your compiler flag to '-std=c++20' for gcc/clang, or '/std:c++20' for MSVC"
275277
#endif
276278

@@ -326,15 +328,15 @@
326328
#warning "Unknown OS detected, tests will be severely limited"
327329
#endif
328330

329-
#if (CPP >= 23)
331+
#if (VMA_CPP >= 23)
330332
#include <limits>
331333
#endif
332-
#if (CPP >= 20)
334+
#if (VMA_CPP >= 20)
333335
#include <bit>
334336
#include <ranges>
335337
#include <source_location>
336338
#endif
337-
#if (CPP >= 17)
339+
#if (VMA_CPP >= 17)
338340
#include <filesystem>
339341
#endif
340342
#ifdef __VMAWARE_DEBUG__
@@ -1363,17 +1365,16 @@ struct VM {
13631365
return buffer;
13641366
}
13651367

1366-
1367-
[[nodiscard]] static bool exists(const char* path) {
1368-
#if (CPP >= 17)
1368+
#if (LINUX)
1369+
[[nodiscard]] static bool exists(const char* path) {
1370+
#if (VMA_CPP >= 17)
13691371
return std::filesystem::exists(path);
1370-
#elif (CPP >= 11)
1372+
#elif (VMA_CPP >= 11)
13711373
struct stat buffer;
13721374
return (stat(path, &buffer) == 0);
13731375
#endif
13741376
}
13751377

1376-
#if (LINUX)
13771378
static bool is_directory(const char* path) {
13781379
struct stat info;
13791380
if (stat(path, &info) != 0) {
@@ -1386,7 +1387,7 @@ struct VM {
13861387
// wrapper for std::make_unique because it's not available for C++11
13871388
template<typename T, typename... Args>
13881389
[[nodiscard]] static std::unique_ptr<T> make_unique(Args&&... args) {
1389-
#if (CPP < 14)
1390+
#if (VMA_CPP < 14)
13901391
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
13911392
#else
13921393
return std::make_unique<T>(std::forward<Args>(args)...);
@@ -1512,7 +1513,7 @@ struct VM {
15121513

15131514

15141515
[[nodiscard]] static std::unique_ptr<std::string> sys_result(const char* cmd) {
1515-
#if (CPP < 14)
1516+
#if (VMA_CPP < 14)
15161517
UNUSED(cmd);
15171518
return util::make_unique<std::string>();
15181519
#else
@@ -1555,7 +1556,7 @@ struct VM {
15551556

15561557
[[nodiscard]] static bool is_proc_running(const char* executable) {
15571558
#if (LINUX)
1558-
#if (CPP >= 17)
1559+
#if (VMA_CPP >= 17)
15591560
for (const auto& entry : std::filesystem::directory_iterator("/proc")) {
15601561
if (!entry.is_directory()) {
15611562
continue;
@@ -4262,7 +4263,6 @@ struct VM {
42624263

42634264
if (tscMHz < 800.0) return true;
42644265

4265-
const struct cpu::stepping_struct steps = cpu::fetch_steppings();
42664266
const u32 baseMHz = cpu::get_cpu_base_speed(); // wont probably work reliably on AMD, but its more reliable than fetching from SMBIOS
42674267

42684268
if (baseMHz == 0) {
@@ -4328,6 +4328,7 @@ struct VM {
43284328
dummy = xor_ptr(); // this loop won't be intercepted, it never switches to kernel-mode
43294329
}
43304330

4331+
UNUSED(dummy);
43314332
ULONG64 afterqit2 = 0;
43324333
QueryInterruptTime(&afterqit2);
43334334
const ULONG64 aftertsc2 = __rdtsc();
@@ -4584,7 +4585,7 @@ struct VM {
45844585
* @implements VM::DMESG
45854586
*/
45864587
[[nodiscard]] static bool dmesg() {
4587-
#if (CPP <= 11)
4588+
#if (VMA_CPP <= 11)
45884589
return false;
45894590
#else
45904591
if (!util::is_admin()) {
@@ -5971,7 +5972,7 @@ struct VM {
59715972

59725973
#if (LINUX)
59735974
const std::string pci_path = "/sys/bus/pci/devices";
5974-
#if (CPP >= 17)
5975+
#if (VMA_CPP >= 17)
59755976
for (const auto& entry : std::filesystem::directory_iterator(pci_path)) {
59765977
std::ifstream vf(entry.path() / "vendor"), df(entry.path() / "device");
59775978
if (!vf || !df) continue;
@@ -8782,8 +8783,7 @@ struct VM {
87828783
* @author Teselka (https://github.com/Teselka)
87838784
* @implements VM::BOOT_LOGO
87848785
*/
8785-
[[nodiscard]]
8786-
static bool boot_logo()
8786+
[[nodiscard]] static bool boot_logo()
87878787
#if (CLANG || GCC)
87888788
__attribute__((__target__("crc32")))
87898789
#endif
@@ -9096,12 +9096,12 @@ struct VM {
90969096
const auto NtEnum = reinterpret_cast<NtEnumerateSystemEnvironmentValuesEx_t>(functions[0]);
90979097
if (NtEnum) {
90989098
hasFunction = true;
9099-
NTSTATUS status = NtEnum(1, nullptr, &bufferLength);
9099+
NtEnum(1, nullptr, &bufferLength);
91009100
if (bufferLength != 0) {
91019101
try { resBuffer.resize(bufferLength); }
91029102
catch (...) { resBuffer.clear(); bufferLength = 0; }
91039103
if (!resBuffer.empty()) {
9104-
status = NtEnum(1, resBuffer.data(), &bufferLength);
9104+
const NTSTATUS status = NtEnum(1, resBuffer.data(), &bufferLength);
91059105
if (status == 0) { success = true; resBuffer.resize(bufferLength); }
91069106
else resBuffer.clear();
91079107
}
@@ -9373,7 +9373,11 @@ struct VM {
93739373
* @category Windows
93749374
* @implements VM::CPU_HEURISTIC
93759375
*/
9376-
[[nodiscard]] static bool cpu_heuristic() {
9376+
[[nodiscard]] static bool cpu_heuristic()
9377+
#if (CLANG || GCC)
9378+
__attribute__((__target__("aes")))
9379+
#endif
9380+
{
93779381
bool spoofed = false;
93789382
#if (x86)
93799383
if (util::is_running_under_translator()) {
@@ -9554,10 +9558,7 @@ struct VM {
95549558
PVOID base = nullptr;
95559559
SIZE_T sz = codeSize;
95569560
NTSTATUS st2 = pNtAllocateVirtualMemory(hCurrentProcess, &base, 0, &sz, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
9557-
if (!NT_SUCCESS(st2) || base == nullptr) {
9558-
proceed = false;
9559-
}
9560-
else {
9561+
if (NT_SUCCESS(st2) && base != nullptr) {
95619562
exec_mem = base;
95629563
memcpy(exec_mem, bytes, codeSize);
95639564

@@ -9566,10 +9567,7 @@ struct VM {
95669567
PVOID tmpBase = exec_mem;
95679568
SIZE_T tmpSz = codeSize;
95689569
st2 = pNtProtectVirtualMemory(hCurrentProcess, &tmpBase, &tmpSz, PAGE_EXECUTE_READ, &oldProt);
9569-
if (!NT_SUCCESS(st2)) {
9570-
proceed = false;
9571-
}
9572-
else {
9570+
if (NT_SUCCESS(st2)) {
95739571
pNtFlushInstructionCache(hCurrentProcess, exec_mem, codeSize);
95749572

95759573
using CodeFunc = void(*)();
@@ -10175,7 +10173,7 @@ struct VM {
1017510173
return (sizeof...(Args) == 0);
1017610174
}
1017710175

10178-
#if (CPP >= 17)
10176+
#if (VMA_CPP >= 17)
1017910177
#define VMAWARE_CONSTEXPR constexpr
1018010178
#else
1018110179
#define VMAWARE_CONSTEXPR
@@ -10240,7 +10238,7 @@ struct VM {
1024010238
*/
1024110239
static bool check(
1024210240
const enum_flags flag_bit
10243-
#if (CPP >= 20) && (!CLANG || __clang_major__ >= 16)
10241+
#if (VMA_CPP >= 20) && (!CLANG || __clang_major__ >= 16)
1024410242
, [[maybe_unused]] const std::source_location& loc = std::source_location::current()
1024510243
#endif
1024610244
) {
@@ -10253,7 +10251,7 @@ struct VM {
1025310251
// lambda to manage exceptions
1025410252
auto throw_error = [&](const char* text) -> void {
1025510253
std::stringstream ss;
10256-
#if (CPP >= 20 && !CLANG)
10254+
#if (VMA_CPP >= 20 && !CLANG)
1025710255
ss << ", error in " << loc.function_name() << " at " << loc.file_name() << ":" << loc.line() << ")";
1025810256
#endif
1025910257
ss << ". Consult the documentation's flag handler for VM::check()";
@@ -10274,7 +10272,7 @@ struct VM {
1027410272
throw_error("Flag argument must be a technique flag and not a settings flag");
1027510273
}
1027610274

10277-
#if (CPP >= 23)
10275+
#if (VMA_CPP >= 23)
1027810276
[[assume(flag_bit < technique_end)]];
1027910277
#endif
1028010278

@@ -10343,7 +10341,7 @@ struct VM {
1034310341

1034410342
// goofy ass C++11 and C++14 linker error workaround.
1034510343
// And yes, this does look stupid.
10346-
#if (CPP <= 14)
10344+
#if (VMA_CPP <= 14)
1034710345
constexpr const char* TMP_QEMU = "QEMU";
1034810346
constexpr const char* TMP_KVM = "KVM";
1034910347
constexpr const char* TMP_QEMU_KVM = "QEMU+KVM";
@@ -10595,7 +10593,7 @@ struct VM {
1059510593
// flags above, and get a total score
1059610594
const u16 points = core::run_all(flags, SHORTCUT);
1059710595

10598-
#if (CPP >= 23)
10596+
#if (VMA_CPP >= 23)
1059910597
[[assume(points < maximum_points)]];
1060010598
#endif
1060110599

@@ -10626,7 +10624,7 @@ struct VM {
1062610624
// flags above, and get a total score
1062710625
const u16 points = core::run_all(flags, SHORTCUT);
1062810626

10629-
#if (CPP >= 23)
10627+
#if (VMA_CPP >= 23)
1063010628
[[assume(points < maximum_points)]];
1063110629
#endif
1063210630

@@ -10663,14 +10661,14 @@ struct VM {
1066310661
static void add_custom(
1066410662
const u8 percent,
1066510663
std::function<bool()> detection_func
10666-
#if (CPP >= 20 && !CLANG)
10664+
#if (VMA_CPP >= 20 && !CLANG)
1066710665
, const std::source_location& loc = std::source_location::current()
1066810666
#endif
1066910667
) {
1067010668
// lambda to throw the error
1067110669
auto throw_error = [&](const char* text) -> void {
1067210670
std::stringstream ss;
10673-
#if (CPP >= 20 && !CLANG)
10671+
#if (VMA_CPP >= 20 && !CLANG)
1067410672
ss << ", error in " << loc.function_name() << " at " << loc.file_name() << ":" << loc.line() << ")";
1067510673
#endif
1067610674
ss << ". Consult the documentation's parameters for VM::add_custom()";
@@ -10681,7 +10679,7 @@ struct VM {
1068110679
throw_error("Percentage parameter must be between 0 and 100");
1068210680
}
1068310681

10684-
#if (CPP >= 23)
10682+
#if (VMA_CPP >= 23)
1068510683
[[assume(percent > 0 && percent <= 100)]];
1068610684
#endif
1068710685

@@ -10863,14 +10861,14 @@ struct VM {
1086310861
static void modify_score(
1086410862
const enum_flags flag,
1086510863
const u8 percent
10866-
#if (CPP >= 20) && (!CLANG || __clang_major__ >= 16)
10864+
#if (VMA_CPP >= 20) && (!CLANG || __clang_major__ >= 16)
1086710865
, const std::source_location& loc = std::source_location::current()
1086810866
#endif
1086910867
) {
1087010868
// lambda to throw the error
1087110869
auto throw_error = [&](const char* text) -> void {
1087210870
std::stringstream ss;
10873-
#if (CPP >= 20 && !CLANG)
10871+
#if (VMA_CPP >= 20 && !CLANG)
1087410872
ss << ", error in " << loc.function_name() << " at " << loc.file_name() << ":" << loc.line() << ")";
1087510873
#endif
1087610874
ss << ". Consult the documentation's parameters for VM::modify_score()";
@@ -10881,7 +10879,7 @@ struct VM {
1088110879
throw_error("Percentage parameter must be between 0 and 100");
1088210880
}
1088310881

10884-
#if (CPP >= 23)
10882+
#if (VMA_CPP >= 23)
1088510883
[[assume(percent <= 100)]];
1088610884
#endif
1088710885

@@ -11034,7 +11032,7 @@ struct VM {
1103411032
std::string brand_tmp = brand(flags);
1103511033
const u8 percent_tmp = percentage(flags);
1103611034

11037-
#if (CPP >= 17)
11035+
#if (VMA_CPP >= 17)
1103811036
constexpr std::string_view very_unlikely = "Very unlikely a";
1103911037
constexpr std::string_view unlikely = "Unlikely a";
1104011038
constexpr std::string_view potentially = "Potentially";
@@ -11052,7 +11050,7 @@ struct VM {
1105211050
const std::string inside_vm = "Running inside";
1105311051
#endif
1105411052

11055-
#if (CPP >= 17)
11053+
#if (VMA_CPP >= 17)
1105611054
auto make_conclusion = [&](const std::string_view category) -> std::string {
1105711055
#else
1105811056
auto make_conclusion = [&](const std::string &category) -> std::string {

0 commit comments

Comments
 (0)