| 1 |
/* |
| 2 |
* Copyright (c) 2021-2022 The Khronos Group Inc. |
| 3 |
* Copyright (c) 2021-2022 Valve Corporation |
| 4 |
* Copyright (c) 2021-2022 LunarG, Inc. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and/or associated documentation files (the "Materials"), to |
| 8 |
* deal in the Materials without restriction, including without limitation the |
| 9 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 |
* sell copies of the Materials, and to permit persons to whom the Materials are |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice(s) and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Materials. |
| 15 |
* |
| 16 |
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 |
* |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 22 |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 23 |
* USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 |
* |
| 25 |
* Author: Charles Giessen <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#pragma once |
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
|
| 32 |
#include <array> |
| 33 |
#include <filesystem> |
| 34 |
#include <string> |
| 35 |
#include <unordered_map> |
| 36 |
#include <unordered_set> |
| 37 |
#include <vector> |
| 38 |
|
| 39 |
#if defined(_WIN32) |
| 40 |
|
| 41 |
#include <direct.h> |
| 42 |
#include <Windows.h> |
| 43 |
#include <strsafe.h> |
| 44 |
#include <cfgmgr32.h> |
| 45 |
#include <initguid.h> |
| 46 |
#include <devpkey.h> |
| 47 |
#include <winternl.h> |
| 48 |
#include <appmodel.h> |
| 49 |
|
| 50 |
#define CINTERFACE |
| 51 |
#include <dxgi1_6.h> |
| 52 |
#include <adapters.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#include "util/test_defines.h" |
| 56 |
|
| 57 |
#if TESTING_COMMON_UNIX_PLATFORMS |
| 58 |
#include <dirent.h> |
| 59 |
#endif |
| 60 |
|
| 61 |
#include "util/folder_manager.h" |
| 62 |
#include "util/functions.h" |
| 63 |
|
| 64 |
enum class GpuType { unspecified, integrated, discrete, external }; |
| 65 |
|
| 66 |
struct TempFile { |
| 67 |
explicit TempFile(std::filesystem::path filename) : filename(filename) {} |
| 68 |
~TempFile() { std::filesystem::remove(filename); } |
| 69 |
std::filesystem::path filename; |
| 70 |
}; |
| 71 |
|
| 72 |
#if defined(_WIN32) |
| 73 |
#define VK_VARIANT_REG_STR "" |
| 74 |
#define VK_VARIANT_REG_STR_W L"" |
| 75 |
|
| 76 |
#define VK_DRIVERS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\Vulkan" VK_VARIANT_REG_STR "\\Drivers" |
| 77 |
#define VK_ELAYERS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\Vulkan" VK_VARIANT_REG_STR "\\ExplicitLayers" |
| 78 |
#define VK_ILAYERS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\Vulkan" VK_VARIANT_REG_STR "\\ImplicitLayers" |
| 79 |
#define VK_SETTINGS_INFO_REGISTRY_LOC "SOFTWARE\\Khronos\\Vulkan" VK_VARIANT_REG_STR "\\LoaderSettings" |
| 80 |
|
| 81 |
struct RegistryEntry { |
| 82 |
RegistryEntry() = default; |
| 83 |
RegistryEntry(std::filesystem::path const& name) noexcept : name(name) {} |
| 84 |
RegistryEntry(std::filesystem::path const& name, DWORD value) noexcept : name(name), value(value) {} |
| 85 |
std::filesystem::path name; |
| 86 |
DWORD value{}; |
| 87 |
}; |
| 88 |
|
| 89 |
struct HKeyHandle { |
| 90 |
explicit HKeyHandle(const size_t value, const std::string& key_path) noexcept : key(HKEY{}), path(key_path) { |
| 91 |
key = reinterpret_cast<HKEY>(value); |
| 92 |
} |
| 93 |
|
| 94 |
HKEY get() const noexcept { return key; } |
| 95 |
|
| 96 |
HKEY key{}; |
| 97 |
std::string path; |
| 98 |
}; |
| 99 |
|
| 100 |
static const char* pnp_registry_path = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}"; |
| 101 |
|
| 102 |
// Needed for DXGI mocking |
| 103 |
struct KnownDriverData { |
| 104 |
const char* filename = nullptr; |
| 105 |
int vendor_id = 0; |
| 106 |
}; |
| 107 |
static std::array<KnownDriverData, 4> known_driver_list = { |
| 108 |
#if defined(_WIN64) |
| 109 |
KnownDriverData{"igvk64.json", 0x8086}, KnownDriverData{"nv-vk64.json", 0x10de}, KnownDriverData{"amd-vulkan64.json", 0x1002}, |
| 110 |
KnownDriverData{"amdvlk64.json", 0x1002} |
| 111 |
#else |
| 112 |
KnownDriverData{"igvk32.json", 0x8086}, KnownDriverData{"nv-vk32.json", 0x10de}, KnownDriverData{"amd-vulkan32.json", 0x1002}, |
| 113 |
KnownDriverData{"amdvlk32.json", 0x1002} |
| 114 |
#endif |
| 115 |
}; |
| 116 |
|
| 117 |
struct DXGIAdapter { |
| 118 |
GpuType gpu_preference = GpuType::unspecified; |
| 119 |
DXGI_ADAPTER_DESC1 desc1{}; |
| 120 |
uint32_t adapter_index = 0; |
| 121 |
IDXGIAdapter1 adapter_instance{}; |
| 122 |
IDXGIAdapter1Vtbl adapter_vtbl_instance{}; |
| 123 |
}; |
| 124 |
|
| 125 |
struct D3DKMT_Adapter { |
| 126 |
D3DKMT_Adapter& add_driver_manifest_path(std::filesystem::path const& src); |
| 127 |
D3DKMT_Adapter& add_implicit_layer_manifest_path(std::filesystem::path const& src); |
| 128 |
D3DKMT_Adapter& add_explicit_layer_manifest_path(std::filesystem::path const& src); |
| 129 |
|
| 130 |
UINT hAdapter; |
| 131 |
LUID adapter_luid; |
| 132 |
std::vector<std::wstring> driver_paths; |
| 133 |
std::vector<std::wstring> implicit_layer_paths; |
| 134 |
std::vector<std::wstring> explicit_layer_paths; |
| 135 |
|
| 136 |
private: |
| 137 |
D3DKMT_Adapter& add_path(std::filesystem::path src, std::vector<std::wstring>& dest); |
| 138 |
}; |
| 139 |
|
| 140 |
#elif TESTING_COMMON_UNIX_PLATFORMS |
| 141 |
|
| 142 |
struct DirEntry { |
| 143 |
DIR* directory = nullptr; |
| 144 |
fs::Folder* folder_represented; |
| 145 |
std::vector<struct dirent*> contents; |
| 146 |
// the current item being read by an app (incremented by readdir, reset to zero by opendir & closedir) |
| 147 |
size_t current_index = 0; |
| 148 |
// bool is_fake_path = false; // true when this entry is for folder redirection |
| 149 |
}; |
| 150 |
|
| 151 |
#endif |
| 152 |
|
| 153 |
struct FrameworkEnvironment; // forward declaration |
| 154 |
|
| 155 |
// Necessary to have inline definitions as shim is a dll and thus functions |
| 156 |
// defined in the .cpp wont be found by the rest of the application |
| 157 |
struct PlatformShim { |
| 158 |
PlatformShim() { fputs_stderr_log.reserve(65536); } |
| 159 |
|
| 160 |
// Used to get info about which drivers & layers have been added to folders |
| 161 |
fs::FileSystemManager* file_system_manager; |
| 162 |
|
| 163 |
// Captures the output to stderr from fputs & fputc - aka the output of loader_log() |
| 164 |
std::string fputs_stderr_log; |
| 165 |
|
| 166 |
// Test Framework interface |
| 167 |
void reset(); |
| 168 |
|
| 169 |
// fake paths are paths that the loader normally looks in but actually point to locations inside the test framework |
| 170 |
// void set_fake_path(ManifestCategory category, std::filesystem::path const& path); |
| 171 |
|
| 172 |
// known paths are real paths but since the test framework guarantee's the order files are found in, files in these paths |
| 173 |
// need to be ordered correctly |
| 174 |
// void add_known_path(std::filesystem::path const& path); |
| 175 |
|
| 176 |
void clear_logs() { fputs_stderr_log.clear(); } |
| 177 |
bool find_in_log(std::string const& search_text) const { return fputs_stderr_log.find(search_text) != std::string::npos; } |
| 178 |
|
| 179 |
// platform specific shim interface |
| 180 |
#if defined(_WIN32) |
| 181 |
// Control Platform Elevation Level |
| 182 |
void set_elevated_privilege(bool elev) { elevation_level = (elev) ? SECURITY_MANDATORY_HIGH_RID : SECURITY_MANDATORY_LOW_RID; } |
| 183 |
unsigned long elevation_level = SECURITY_MANDATORY_LOW_RID; |
| 184 |
|
| 185 |
void add_manifest_to_registry(ManifestCategory category, std::filesystem::path const& path); |
| 186 |
void add_unsecured_manifest_to_registry(ManifestCategory category, std::filesystem::path const& path); |
| 187 |
|
| 188 |
void add_dxgi_adapter(GpuType gpu_preference, DXGI_ADAPTER_DESC1 desc1); |
| 189 |
void add_d3dkmt_adapter(D3DKMT_Adapter const& adapter); |
| 190 |
void set_app_package_path(std::filesystem::path const& path); |
| 191 |
|
| 192 |
std::unordered_map<uint32_t, DXGIAdapter> dxgi_adapters; |
| 193 |
|
| 194 |
std::vector<D3DKMT_Adapter> d3dkmt_adapters; |
| 195 |
|
| 196 |
// TODO: |
| 197 |
void add_CM_Device_ID(std::wstring const& id, std::filesystem::path const& icd_path, std::filesystem::path const& layer_path); |
| 198 |
std::wstring CM_device_ID_list = {L'\0'}; |
| 199 |
std::vector<RegistryEntry> CM_device_ID_registry_keys; |
| 200 |
|
| 201 |
uint32_t random_base_path = 0; |
| 202 |
|
| 203 |
std::vector<std::filesystem::path> icd_paths; |
| 204 |
|
| 205 |
std::vector<RegistryEntry> hkey_current_user_explicit_layers; |
| 206 |
std::vector<RegistryEntry> hkey_current_user_implicit_layers; |
| 207 |
std::vector<RegistryEntry> hkey_local_machine_explicit_layers; |
| 208 |
std::vector<RegistryEntry> hkey_local_machine_implicit_layers; |
| 209 |
std::vector<RegistryEntry> hkey_local_machine_drivers; |
| 210 |
std::vector<RegistryEntry> hkey_local_machine_settings; |
| 211 |
std::vector<RegistryEntry> hkey_current_user_settings; |
| 212 |
|
| 213 |
std::wstring app_package_path; |
| 214 |
|
| 215 |
// When a key is created, return the index of the |
| 216 |
size_t created_key_count = 0; |
| 217 |
std::vector<HKeyHandle> created_keys; |
| 218 |
|
| 219 |
#elif TESTING_COMMON_UNIX_PLATFORMS |
| 220 |
// Add a filename that dlopen can "find" with no absolute or relative path given. |
| 221 |
// EG, dlopen("libfoobar.so") gets turned into dlopen(actual_path) |
| 222 |
void add_system_library(std::string const& filename, std::filesystem::path const& actual_path); |
| 223 |
|
| 224 |
// Returns the real path of the system library "filename", if it exists. Else returns empty path |
| 225 |
std::filesystem::path get_system_library(std::string const& filename); |
| 226 |
|
| 227 |
std::unordered_map<std::string, std::filesystem::path> system_library_redirection_map; |
| 228 |
|
| 229 |
void set_app_package_path(std::filesystem::path const& path); |
| 230 |
|
| 231 |
void set_elevated_privilege(bool elev) { use_fake_elevation = elev; } |
| 232 |
bool use_fake_elevation = false; |
| 233 |
|
| 234 |
std::vector<DirEntry> dir_entries; |
| 235 |
|
| 236 |
#if defined(__APPLE__) |
| 237 |
std::string bundle_contents; |
| 238 |
#endif |
| 239 |
#endif |
| 240 |
std::vector<uint8_t> fuzz_data; |
| 241 |
std::vector<TempFile> temp_fuzz_files; |
| 242 |
bool is_finished_setup = false; |
| 243 |
bool is_during_destruction = false; |
| 244 |
}; |
| 245 |
|
| 246 |
std::vector<std::string> parse_env_var_list(std::string const& var); |
| 247 |
std::string category_path_name(ManifestCategory category); |
| 248 |
|
| 249 |
extern "C" { |
| 250 |
// dynamically link on windows and macos |
| 251 |
#if defined(_WIN32) || defined(__APPLE__) |
| 252 |
using PFN_get_platform_shim = PlatformShim* (*)(); |
| 253 |
#define GET_PLATFORM_SHIM_STR "get_platform_shim" |
| 254 |
|
| 255 |
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__GNU__) || defined(__QNX__) |
| 256 |
// statically link on linux |
| 257 |
PlatformShim* get_platform_shim(); |
| 258 |
#endif |
| 259 |
} |