Skip to content

Commit 335f42a

Browse files
author
Requiem
committed
fix: remove Hyper-V artifact if a VM is detected
1 parent f43289a commit 335f42a

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

src/vmaware.hpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@
346346
#endif
347347
#if (VMA_CPP >= 17)
348348
#include <filesystem>
349-
#include <system_error>
350-
#endif
349+
#include <system_error>
350+
#endif
351351
#ifdef __VMAWARE_DEBUG__
352352
#include <iomanip>
353353
#include <ios>
@@ -658,6 +658,7 @@ struct VM {
658658
static constexpr u8 INVALID = 255; // explicit invalid technique macro
659659
static constexpr u16 base_technique_count = HIGH_THRESHOLD; // original technique count, constant on purpose (can also be used as a base count value if custom techniques are added)
660660
static constexpr u16 maximum_points = 5510; // theoretical total points if all VM detections returned true (which is practically impossible)
661+
static constexpr u16 threshold_score = 150; // standard threshold score
661662
static constexpr u16 high_threshold_score = 300; // new threshold score from 150 to 300 if VM::HIGH_THRESHOLD flag is enabled
662663
static constexpr bool SHORTCUT = true; // macro for whether VM::core::run_all() should take a shortcut by skipping the rest of the techniques if the threshold score is already met
663664

@@ -11466,7 +11467,7 @@ struct VM {
1146611467
static u16 run_all(const flagset& flags, const bool shortcut = false) {
1146711468
u16 points = 0;
1146811469

11469-
u16 threshold_points = 150;
11470+
u16 threshold_points = threshold_score;
1147011471

1147111472
// set it to 300 if high threshold is enabled
1147211473
if (core::is_enabled(flags, HIGH_THRESHOLD)) {
@@ -11817,8 +11818,7 @@ struct VM {
1181711818
return brand(flags);
1181811819
}
1181911820

11820-
11821-
static std::string brand(const flagset &flags = core::generate_default()) {
11821+
static std::string brand(const flagset& flags = core::generate_default()) {
1182211822
// is the multiple setting flag enabled?
1182311823
const bool is_multiple = core::is_enabled(flags, MULTIPLE);
1182411824

@@ -11831,7 +11831,8 @@ struct VM {
1183111831
debug("VM::brand(): returned multi brand from cache");
1183211832
return memo::multi_brand::fetch();
1183311833
}
11834-
} else {
11834+
}
11835+
else {
1183511836
if (memo::brand::is_cached()) {
1183611837
debug("VM::brand(): returned brand from cache");
1183711838
return memo::brand::fetch();
@@ -11896,7 +11897,9 @@ struct VM {
1189611897
}
1189711898

1189811899
// if there's only a single brand, return it immediately
11899-
if (active_count == 1) {
11900+
// We skip this early return if the single brand is HYPERV_ARTIFACT,
11901+
// so that the removal logic at the end of the function can process it
11902+
if (active_count == 1 && active_brands[0].first != TMP_HYPERV_ARTIFACT) {
1190011903
return active_brands[0].first;
1190111904
}
1190211905

@@ -11966,9 +11969,7 @@ struct VM {
1196611969
merge(TMP_VPC, TMP_HYPERV, TMP_HYPERV_VPC);
1196711970
}
1196811971
else if (idx_hv != -1 && idx_vpc == -1) {
11969-
// before, if counts differ (and one is 0), we erased VPC
11970-
// but if VPC is -1, it's already "erased"
11971-
// so logic handled by merge check essentially
11972+
// logic handled by merge check essentially
1197211973
}
1197311974

1197411975
// Brand post-processing / merging
@@ -11999,17 +12000,28 @@ struct VM {
1199912000
merge(TMP_VMWARE_HARD, TMP_GSX, TMP_VMWARE_HARD);
1200012001
merge(TMP_VMWARE_HARD, TMP_WORKSTATION, TMP_VMWARE_HARD);
1200112002

12003+
// determine threshold (150 or 300)
12004+
u16 confirmed_vm_threshold = threshold_score;
12005+
if (core::is_enabled(flags, HIGH_THRESHOLD)) {
12006+
confirmed_vm_threshold = high_threshold_score;
12007+
}
12008+
12009+
// check if Hyper-V artifact is present
1200212010
const int idx_art = find_index(TMP_HYPERV_ARTIFACT);
12003-
if (idx_art != -1 && score > 0) {
12004-
remove_at(idx_art);
12011+
if (idx_art != -1) {
12012+
// If score confirms it is a VM, remove the "Artifact" label (because we're in a VM, not in a host machine)
12013+
// so it falls back to "Unknown" if no other brands exist
12014+
if (score >= confirmed_vm_threshold) {
12015+
remove_at(idx_art);
12016+
}
1200512017
}
1200612018

1200712019
if (active_count > 1) {
1200812020
std::sort(active_brands.begin(), active_brands.begin() + static_cast<std::ptrdiff_t>(active_count), [](
1200912021
const brand_element_t& a,
1201012022
const brand_element_t& b
1201112023
) {
12012-
return a.second > b.second;
12024+
return a.second > b.second;
1201312025
});
1201412026
}
1201512027

@@ -12024,7 +12036,8 @@ struct VM {
1202412036
memo::brand::store(active_brands[0].first);
1202512037
debug("VM::brand(): cached brand string");
1202612038
return memo::brand::fetch();
12027-
} else {
12039+
}
12040+
else {
1202812041
char* buffer = memo::multi_brand::brand_cache;
1202912042
buffer[0] = '\0';
1203012043
const size_t buf_size = sizeof(memo::multi_brand::brand_cache);
@@ -12067,7 +12080,7 @@ struct VM {
1206712080
[[assume(points < maximum_points)]];
1206812081
#endif
1206912082

12070-
u16 threshold = 150;
12083+
u16 threshold = threshold_score;
1207112084

1207212085
// if high threshold is set, the bar
1207312086
// will be 300. If not, leave it as 150
@@ -12110,7 +12123,7 @@ struct VM {
1211012123
#endif
1211112124

1211212125
u8 percent = 0;
12113-
u16 threshold = 150;
12126+
u16 threshold = threshold_score;
1211412127

1211512128
// set to 300 if high threshold is enabled
1211612129
if (core::is_enabled(flags, HIGH_THRESHOLD)) {

0 commit comments

Comments
 (0)