Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
kernelwernel committed Oct 21, 2023
2 parents 0b3a508 + 6bbe85f commit df95e3d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 116 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ project(
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXE_LINKER_FLAGS "-lstdc++ -lm")

if (MSVC)
set(CMAKE_CXX_FLAGS "/W4 /std:c++20 /EHsc")
Expand Down
4 changes: 2 additions & 2 deletions docs/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ int main() {


# `VM::brand()`
This will essentially return the VM brand as a std::string_view (assuming you're using C++17 and above, else it will return a const char*). The possible brand string return values are: `VMware`, `VirtualBox`, `KVM`, `bhyve`, `QEMU`, `Microsoft Hyper-V`, `Microsoft x86-to-ARM`, `Parallels`, `Xen HVM`, `ACRN`, `QNX hypervisor`, `Hybrid Analysis`, `Sandboxie`, `Docker`, `Wine`, `Virtual Apple`, and `Virtual PC`. If none were detected, it will return `Unknown`. It's often not going to produce a satisfying result due to technical difficulties with accomplishing this, on top of being highly dependant on what mechanisms detected a VM.
This will essentially return the VM brand as a std::string. The possible brand string return values are: `VMware`, `VirtualBox`, `KVM`, `bhyve`, `QEMU`, `Microsoft Hyper-V`, `Microsoft x86-to-ARM`, `Parallels`, `Xen HVM`, `ACRN`, `QNX hypervisor`, `Hybrid Analysis`, `Sandboxie`, `Docker`, `Wine`, `Virtual Apple`, and `Virtual PC`. If none were detected, it will return `Unknown`. It's often not going to produce a satisfying result due to technical difficulties with accomplishing this, on top of being highly dependant on what mechanisms detected a VM.

```cpp
int main() {
std::string_view result = VM::brand();
const std::string result = VM::brand();

if (result == "KVM") {
// do KVM specific stuff
Expand Down
144 changes: 31 additions & 113 deletions src/vmaware.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,56 +186,28 @@ struct VM {
#endif

// official aliases for VM brands. This is added to avoid accidental typos which could really fuck up the result. Also, no errors/warnings are issued if the string is invalid.
#if (CPP >= 17)
static constexpr sv \
VMWARE = "VMware",
VBOX = "VirtualBox",
KVM = "KVM",
BHYVE = "bhyve",
QEMU = "QEMU",
HYPERV = "Microsoft Hyper-V",
MSXTA = "Microsoft x86-to-ARM",
PARALLELS = "Parallels",
XEN = "Xen HVM",
ACRN = "ACRN",
QNX = "QNX hypervisor",
HYBRID = "Hybrid Analysis",
SANDBOXIE = "Sandboxie",
DOCKER = "Docker",
WINE = "Wine",
VAPPLE = "Virtual Apple",
VPC = "Virtual PC",
ANUBIS = "Anubis",
JOEBOX = "JoeBox";
#else
static const char* VMWARE;
static const char* VBOX;
static const char* KVM;
static const char* BHYVE;
static const char* QEMU;
static const char* HYPERV;
static const char* MSXTA;
static const char* PARALLELS;
static const char* XEN;
static const char* ACRN;
static const char* QNX;
static const char* HYBRID;
static const char* SANDBOXIE;
static const char* DOCKER;
static const char* WINE;
static const char* VAPPLE;
static const char* VPC;
static const char* ANUBIS;
static const char* JOEBOX;
#endif
static constexpr const char* VMWARE = "VMware";
static constexpr const char* VBOX = "VirtualBox";
static constexpr const char* KVM = "KVM";
static constexpr const char* BHYVE = "bhyve";
static constexpr const char* QEMU = "QEMU";
static constexpr const char* HYPERV = "Microsoft Hyper-V";
static constexpr const char* MSXTA = "Microsoft x86-to-ARM";
static constexpr const char* PARALLELS = "Parallels";
static constexpr const char* XEN = "Xen HVM";
static constexpr const char* ACRN = "ACRN";
static constexpr const char* QNX = "QNX hypervisor";
static constexpr const char* HYBRID = "Hybrid Analysis";
static constexpr const char* SANDBOXIE = "Sandboxie";
static constexpr const char* DOCKER = "Docker";
static constexpr const char* WINE = "Wine";
static constexpr const char* VAPPLE = "Virtual Apple";
static constexpr const char* VPC = "Virtual PC";
static constexpr const char* ANUBIS = "Anubis";
static constexpr const char* JOEBOX = "JoeBox";


// VM scoreboard table specifically for VM::brand()
#if (CPP >= 17)
static std::map<sv, u8> scoreboard;
#else
static std::map<const char*, u8> scoreboard;
#endif
static std::map<const char*, u8> scoreboard;

// check if cpuid is supported
[[nodiscard]] static bool check_cpuid(void) {
Expand Down Expand Up @@ -344,18 +316,10 @@ struct VM {
#endif

// directly return when adding a brand to the scoreboard for a more succint expression
#if (CPP >= 17)
//a
[[nodiscard]] static inline bool add(const sv p_brand) noexcept {
scoreboard[p_brand]++;
return true;
}
#else
[[nodiscard]] static inline bool add(const char* p_brand) noexcept {
scoreboard[p_brand]++;
return true;
}
#endif
[[nodiscard]] static inline bool add(const char* p_brand) noexcept {
scoreboard[p_brand]++;
return true;
}

// get disk size in GB
// TODO: finish the MSVC section
Expand Down Expand Up @@ -493,11 +457,7 @@ struct VM {
}

// memoize the value from VM::detect() in case it's ran again
#if (CPP >= 17)
static std::map<bool, std::pair<bool, sv>> memo;
#else
static std::map<bool, std::pair<bool, const char*>> memo;
#endif
static std::map<bool, std::pair<bool, const char*>> memo;

// cpuid check value
static bool cpuid_supported;
Expand Down Expand Up @@ -2406,14 +2366,11 @@ struct VM {

/**
* @brief Fetch the VM brand
* @return std::string
* @returns VMware, VirtualBox, KVM, bhyve, QEMU, Microsoft Hyper-V, Microsoft x86-to-ARM, Parallels, Xen HVM, ACRN, QNX hypervisor, Hybrid Analysis, Sandboxie, Docker, Wine, Virtual Apple, Virtual PC, Unknown
* @link https://github.com/kernelwernel/VMAware/blob/main/docs/documentation.md#vmbrand
*/
#if (CPP >= 17)
[[nodiscard]] static sv brand(void) {
#else
[[nodiscard]] static const char* brand(void) {
#endif
[[nodiscard]] static std::string brand(void) {
// check if result hasn't been memoized already
if (memo.find(true) == memo.end()) {
#ifdef __VMAWARE_DEBUG__
Expand All @@ -2427,7 +2384,7 @@ struct VM {
return "Unknown";
}

return (memo[true].second);
return (std::string(memo[true].second));
}


Expand Down Expand Up @@ -2473,11 +2430,7 @@ struct VM {
// threshold score
const bool result = (points >= 100);

#if (CPP >= 17)
sv current_brand = "";
#else
const char* current_brand = "";
#endif
const char* current_brand = "";

#ifdef __VMAWARE_DEBUG__
for (const auto p : scoreboard) {
Expand Down Expand Up @@ -2534,36 +2487,7 @@ struct VM {
};


#if (CPP < 17)
//std::map<const char*, std::uint8_t> VM::scoreboard{};

// hacky solution to problems with inline variables being incompatible with standards under C++17
const char* VM::VMWARE = "VMware";
const char* VM::VBOX = "VirtualBox";
const char* VM::KVM = "KVM";
const char* VM::BHYVE = "bhyve";
const char* VM::QEMU = "QEMU";
const char* VM::HYPERV = "Microsoft Hyper-V";
const char* VM::MSXTA = "Microsoft x86-to-ARM";
const char* VM::PARALLELS = "Parallels";
const char* VM::XEN = "Xen HVM";
const char* VM::ACRN = "ACRN";
const char* VM::QNX = "QNX hypervisor";
const char* VM::HYBRID = "Hybrid Analysis";
const char* VM::SANDBOXIE = "Sandboxie";
const char* VM::DOCKER = "Docker";
const char* VM::WINE = "Wine";
const char* VM::VAPPLE = "Virtual Apple";
const char* VM::VPC = "Virtual PC";
const char* VM::ANUBIS = "Anubis";
const char* VM::JOEBOX = "JoeBox";
#endif

#if (CPP >= 17)
std::map<VM::sv, VM::u8> VM::scoreboard {
#else
std::map<const char*, VM::u8> VM::scoreboard {
#endif
std::map<const char*, VM::u8> VM::scoreboard {
{ VM::VMWARE, 0 },
{ VM::VBOX, 0 },
{ VM::KVM, 0 },
Expand All @@ -2588,13 +2512,7 @@ struct VM {

VM::u64 VM::flags = 0;
bool VM::cpuid_supported = false;


#if (CPP >= 17)
std::map<bool, std::pair<bool, VM::sv>> VM::memo;
#else
std::map<bool, std::pair<bool, const char*>> VM::memo;
#endif
std::map<bool, std::pair<bool, const char*>> VM::memo;


const std::map<VM::u64, VM::technique> VM::table = {
Expand Down

0 comments on commit df95e3d

Please sign in to comment.