From 35897ec74ef15f91ab1aac379ef9318dd62aeaae Mon Sep 17 00:00:00 2001 From: JohnMcPMS Date: Tue, 7 Sep 2021 22:25:26 -0700 Subject: [PATCH 1/3] Save arch before reverting others --- src/AppInstallerCommonCore/Architecture.cpp | 51 ++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/AppInstallerCommonCore/Architecture.cpp b/src/AppInstallerCommonCore/Architecture.cpp index 95a811b529..e230e514f6 100644 --- a/src/AppInstallerCommonCore/Architecture.cpp +++ b/src/AppInstallerCommonCore/Architecture.cpp @@ -21,6 +21,52 @@ namespace AppInstaller::Utility } } + // These types are defined in a future SDK and can be removed when we actually have them available. + // The exception is that None was added (and this is an enum class). + enum class MACHINE_ATTRIBUTES { + None = 0, + UserEnabled = 0x00000001, + KernelEnabled = 0x00000002, + Wow64Container = 0x00000004 + }; + + DEFINE_ENUM_FLAG_OPERATORS(MACHINE_ATTRIBUTES); + + using GetMachineTypeAttributesPtr = HRESULT (WINAPI *)(USHORT Machine, MACHINE_ATTRIBUTES* MachineTypeAttributes); + + void AddArchitectureIfMachineTypeAttributesUserEnabled(std::vector& target, Architecture architecture, USHORT guestMachine) + { + wil::unique_hmodule onecoreModule; + onecoreModule.reset(LoadLibraryEx(L"api-ms-win-core-processthreads-l1-1-7.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32)); + if (!onecoreModule) + { + AICLI_LOG(Core, Verbose, << "Could not load api-ms-win-core-processthreads-l1-1-7.dll"); + return; + } + + GetMachineTypeAttributesPtr getMachineTypeAttributes = + reinterpret_cast(GetProcAddress(onecoreModule.get(), "GetMachineTypeAttributes")); + if (!getMachineTypeAttributes) + { + AICLI_LOG(Core, Verbose, << "Could not get proc address of GetMachineTypeAttributes"); + return; + } + + MACHINE_ATTRIBUTES attributes = MACHINE_ATTRIBUTES::None; + if (SUCCEEDED_LOG(getMachineTypeAttributes(guestMachine, &attributes))) + { + if (WI_IsFlagSet(attributes, MACHINE_ATTRIBUTES::UserEnabled)) + { + AICLI_LOG(Test, Info, << "UserEnabled for guest machine: " << guestMachine); + target.push_back(architecture); + } + else + { + AICLI_LOG(Test, Info, << "UserNOTEnabled for guest machine: " << guestMachine); + } + } + } + // Gets the applicable architectures for the current machine. std::vector CreateApplicableArchitecturesVector() { @@ -31,6 +77,7 @@ namespace AppInstaller::Utility case Architecture::Arm64: applicableArchs.push_back(Architecture::Arm64); AddArchitectureIfGuestMachineSupported(applicableArchs, Architecture::Arm, IMAGE_FILE_MACHINE_ARMNT); + AddArchitectureIfMachineTypeAttributesUserEnabled(applicableArchs, Architecture::X64, IMAGE_FILE_MACHINE_AMD64); AddArchitectureIfGuestMachineSupported(applicableArchs, Architecture::X86, IMAGE_FILE_MACHINE_I386); applicableArchs.push_back(Architecture::Neutral); break; @@ -44,7 +91,9 @@ namespace AppInstaller::Utility break; case Architecture::X64: applicableArchs.push_back(Architecture::X64); - AddArchitectureIfGuestMachineSupported(applicableArchs, Architecture::X86, IMAGE_FILE_MACHINE_I386); + AddArchitectureIfMachineTypeAttributesUserEnabled(applicableArchs, Architecture::X86, IMAGE_FILE_MACHINE_I386); + AddArchitectureIfMachineTypeAttributesUserEnabled(applicableArchs, Architecture::Arm, IMAGE_FILE_MACHINE_ARMNT); + //AddArchitectureIfGuestMachineSupported(applicableArchs, Architecture::X86, IMAGE_FILE_MACHINE_I386); applicableArchs.push_back(Architecture::Neutral); break; default: From da217fb022bcda55f94b00fca9bae683790c9d9f Mon Sep 17 00:00:00 2001 From: JohnMcPMS Date: Tue, 7 Sep 2021 22:46:42 -0700 Subject: [PATCH 2/3] Productize --- src/AppInstallerCommonCore/Architecture.cpp | 66 ++++++++++++--------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/src/AppInstallerCommonCore/Architecture.cpp b/src/AppInstallerCommonCore/Architecture.cpp index e230e514f6..7534d0309b 100644 --- a/src/AppInstallerCommonCore/Architecture.cpp +++ b/src/AppInstallerCommonCore/Architecture.cpp @@ -32,40 +32,48 @@ namespace AppInstaller::Utility DEFINE_ENUM_FLAG_OPERATORS(MACHINE_ATTRIBUTES); - using GetMachineTypeAttributesPtr = HRESULT (WINAPI *)(USHORT Machine, MACHINE_ATTRIBUTES* MachineTypeAttributes); + using GetMachineTypeAttributesPtr = HRESULT(WINAPI*)(USHORT Machine, MACHINE_ATTRIBUTES* MachineTypeAttributes); - void AddArchitectureIfMachineTypeAttributesUserEnabled(std::vector& target, Architecture architecture, USHORT guestMachine) + // GetMachineTypeAttributes can apparently replace IsWow64GuestMachineSupported, but no reason to do so right now. + struct GetMachineTypeAttributesHelper { - wil::unique_hmodule onecoreModule; - onecoreModule.reset(LoadLibraryEx(L"api-ms-win-core-processthreads-l1-1-7.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32)); - if (!onecoreModule) + GetMachineTypeAttributesHelper() { - AICLI_LOG(Core, Verbose, << "Could not load api-ms-win-core-processthreads-l1-1-7.dll"); - return; - } + m_module.reset(LoadLibraryEx(L"api-ms-win-core-processthreads-l1-1-7.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32)); + if (!m_module) + { + AICLI_LOG(Core, Verbose, << "Could not load api-ms-win-core-processthreads-l1-1-7.dll"); + return; + } - GetMachineTypeAttributesPtr getMachineTypeAttributes = - reinterpret_cast(GetProcAddress(onecoreModule.get(), "GetMachineTypeAttributes")); - if (!getMachineTypeAttributes) - { - AICLI_LOG(Core, Verbose, << "Could not get proc address of GetMachineTypeAttributes"); - return; + m_getMachineTypeAttributes = + reinterpret_cast(GetProcAddress(m_module.get(), "GetMachineTypeAttributes")); + if (!m_getMachineTypeAttributes) + { + AICLI_LOG(Core, Verbose, << "Could not get proc address of GetMachineTypeAttributes"); + return; + } } - MACHINE_ATTRIBUTES attributes = MACHINE_ATTRIBUTES::None; - if (SUCCEEDED_LOG(getMachineTypeAttributes(guestMachine, &attributes))) + void AddArchitectureIfMachineTypeAttributesUserEnabled(std::vector& target, Architecture architecture, USHORT guestMachine) { - if (WI_IsFlagSet(attributes, MACHINE_ATTRIBUTES::UserEnabled)) - { - AICLI_LOG(Test, Info, << "UserEnabled for guest machine: " << guestMachine); - target.push_back(architecture); - } - else + if (m_getMachineTypeAttributes) { - AICLI_LOG(Test, Info, << "UserNOTEnabled for guest machine: " << guestMachine); + MACHINE_ATTRIBUTES attributes = MACHINE_ATTRIBUTES::None; + if (SUCCEEDED_LOG(m_getMachineTypeAttributes(guestMachine, &attributes))) + { + if (WI_IsFlagSet(attributes, MACHINE_ATTRIBUTES::UserEnabled)) + { + target.push_back(architecture); + } + } } } - } + + private: + wil::unique_hmodule m_module; + GetMachineTypeAttributesPtr m_getMachineTypeAttributes = nullptr; + }; // Gets the applicable architectures for the current machine. std::vector CreateApplicableArchitecturesVector() @@ -75,11 +83,15 @@ namespace AppInstaller::Utility switch (GetSystemArchitecture()) { case Architecture::Arm64: + { + GetMachineTypeAttributesHelper gmtaHelper; + applicableArchs.push_back(Architecture::Arm64); AddArchitectureIfGuestMachineSupported(applicableArchs, Architecture::Arm, IMAGE_FILE_MACHINE_ARMNT); - AddArchitectureIfMachineTypeAttributesUserEnabled(applicableArchs, Architecture::X64, IMAGE_FILE_MACHINE_AMD64); + gmtaHelper.AddArchitectureIfMachineTypeAttributesUserEnabled(applicableArchs, Architecture::X64, IMAGE_FILE_MACHINE_AMD64); AddArchitectureIfGuestMachineSupported(applicableArchs, Architecture::X86, IMAGE_FILE_MACHINE_I386); applicableArchs.push_back(Architecture::Neutral); + } break; case Architecture::Arm: applicableArchs.push_back(Architecture::Arm); @@ -91,9 +103,7 @@ namespace AppInstaller::Utility break; case Architecture::X64: applicableArchs.push_back(Architecture::X64); - AddArchitectureIfMachineTypeAttributesUserEnabled(applicableArchs, Architecture::X86, IMAGE_FILE_MACHINE_I386); - AddArchitectureIfMachineTypeAttributesUserEnabled(applicableArchs, Architecture::Arm, IMAGE_FILE_MACHINE_ARMNT); - //AddArchitectureIfGuestMachineSupported(applicableArchs, Architecture::X86, IMAGE_FILE_MACHINE_I386); + AddArchitectureIfGuestMachineSupported(applicableArchs, Architecture::X86, IMAGE_FILE_MACHINE_I386); applicableArchs.push_back(Architecture::Neutral); break; default: From 30ec1b16033e3091b2b3a4ed18b63803de0a2f41 Mon Sep 17 00:00:00 2001 From: JohnMcPMS Date: Tue, 7 Sep 2021 23:08:40 -0700 Subject: [PATCH 3/3] Spelling --- .github/actions/spelling/expect.txt | 2 ++ src/AppInstallerCommonCore/Architecture.cpp | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index 2b3d7b00ce..6bc0add1e7 100644 --- a/.github/actions/spelling/expect.txt +++ b/.github/actions/spelling/expect.txt @@ -130,6 +130,7 @@ hhx HINSTANCE hkey hlocal +hmodule hre hresults htm @@ -246,6 +247,7 @@ pkindex PMS positionals powertoys +processthreads productcode pseudocode pvk diff --git a/src/AppInstallerCommonCore/Architecture.cpp b/src/AppInstallerCommonCore/Architecture.cpp index 7534d0309b..7960dc4d10 100644 --- a/src/AppInstallerCommonCore/Architecture.cpp +++ b/src/AppInstallerCommonCore/Architecture.cpp @@ -84,11 +84,11 @@ namespace AppInstaller::Utility { case Architecture::Arm64: { - GetMachineTypeAttributesHelper gmtaHelper; + GetMachineTypeAttributesHelper helper; applicableArchs.push_back(Architecture::Arm64); AddArchitectureIfGuestMachineSupported(applicableArchs, Architecture::Arm, IMAGE_FILE_MACHINE_ARMNT); - gmtaHelper.AddArchitectureIfMachineTypeAttributesUserEnabled(applicableArchs, Architecture::X64, IMAGE_FILE_MACHINE_AMD64); + helper.AddArchitectureIfMachineTypeAttributesUserEnabled(applicableArchs, Architecture::X64, IMAGE_FILE_MACHINE_AMD64); AddArchitectureIfGuestMachineSupported(applicableArchs, Architecture::X86, IMAGE_FILE_MACHINE_I386); applicableArchs.push_back(Architecture::Neutral); }