Debian Codegraph @

tests/framework/test_environment.cpp

raw
1 /*
2 * Copyright (c) 2021-2023 The Khronos Group Inc.
3 * Copyright (c) 2021-2023 Valve Corporation
4 * Copyright (c) 2021-2023 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 #include "test_environment.h"
29
30 #include <array>
31 #include <filesystem>
32 #include <fstream>
33 #include <string>
34 #include <utility>
35
36 #include "json_writer.h"
37 #include "test_defines.h"
38
39 InstWrapper::InstWrapper(VulkanFunctions& functions, VkAllocationCallbacks* callbacks) noexcept
40 : functions(&functions), callbacks(callbacks) {}
41 InstWrapper::InstWrapper(VulkanFunctions& functions, VkInstance inst, VkAllocationCallbacks* callbacks) noexcept
42 : functions(&functions), inst(inst), callbacks(callbacks) {}
43 InstWrapper::~InstWrapper() noexcept {
44 if (inst != VK_NULL_HANDLE) functions->vkDestroyInstance(inst, callbacks);
45 }
46
47 InstWrapper::InstWrapper(InstWrapper&& other) noexcept {
48 functions = other.functions;
49 inst = other.inst;
50 callbacks = other.callbacks;
51 create_info = other.create_info;
52 other.inst = VK_NULL_HANDLE;
53 }
54 InstWrapper& InstWrapper::operator=(InstWrapper&& other) noexcept {
55 functions->vkDestroyInstance(inst, callbacks);
56 functions = other.functions;
57 inst = other.inst;
58 callbacks = other.callbacks;
59 create_info = other.create_info;
60 other.inst = VK_NULL_HANDLE;
61 return *this;
62 }
63
64 void InstWrapper::CheckCreate(VkResult result_to_check) {
65 handle_assert_null(inst);
66 ASSERT_EQ(result_to_check, functions->vkCreateInstance(create_info.get(), callbacks, &inst));
67 functions->load_instance_functions(inst);
68 }
69
70 void InstWrapper::CheckCreateWithInfo(InstanceCreateInfo& create_info, VkResult result_to_check) {
71 handle_assert_null(inst);
72 ASSERT_EQ(result_to_check, functions->vkCreateInstance(create_info.get(), callbacks, &inst));
73 functions->load_instance_functions(inst);
74 }
75
76 std::vector<VkPhysicalDevice> InstWrapper::GetPhysDevs(uint32_t phys_dev_count, VkResult result_to_check) {
77 uint32_t physical_count = phys_dev_count;
78 std::vector<VkPhysicalDevice> physical_devices;
79 physical_devices.resize(phys_dev_count);
80 VkResult res = functions->vkEnumeratePhysicalDevices(inst, &physical_count, physical_devices.data());
81 EXPECT_EQ(result_to_check, res);
82 return physical_devices;
83 }
84
85 std::vector<VkPhysicalDevice> InstWrapper::GetPhysDevs(VkResult result_to_check) {
86 uint32_t physical_count = 0;
87 VkResult res = functions->vkEnumeratePhysicalDevices(inst, &physical_count, nullptr);
88 EXPECT_EQ(result_to_check, res);
89 std::vector<VkPhysicalDevice> physical_devices;
90 physical_devices.resize(physical_count);
91 res = functions->vkEnumeratePhysicalDevices(inst, &physical_count, physical_devices.data());
92 EXPECT_EQ(result_to_check, res);
93 return physical_devices;
94 }
95
96 VkPhysicalDevice InstWrapper::GetPhysDev(VkResult result_to_check) {
97 uint32_t physical_count = 1;
98 VkPhysicalDevice physical_device = VK_NULL_HANDLE;
99 VkResult res = this->functions->vkEnumeratePhysicalDevices(inst, &physical_count, &physical_device);
100 EXPECT_EQ(result_to_check, res);
101 return physical_device;
102 }
103
104 std::vector<VkLayerProperties> InstWrapper::GetActiveLayers(VkPhysicalDevice phys_dev, uint32_t expected_count) {
105 uint32_t count = 0;
106 VkResult res = functions->vkEnumerateDeviceLayerProperties(phys_dev, &count, nullptr);
107 EXPECT_EQ(VK_SUCCESS, res);
108 EXPECT_EQ(count, expected_count);
109 std::vector<VkLayerProperties> layer_props{count};
110 res = functions->vkEnumerateDeviceLayerProperties(phys_dev, &count, layer_props.data());
111 EXPECT_EQ(VK_SUCCESS, res);
112 EXPECT_EQ(count, expected_count);
113 return layer_props;
114 }
115
116 std::vector<VkExtensionProperties> InstWrapper::EnumerateDeviceExtensions(VkPhysicalDevice physical_device,
117 uint32_t expected_count) {
118 uint32_t count = 0;
119 VkResult res = functions->vkEnumerateDeviceExtensionProperties(physical_device, nullptr, &count, nullptr);
120 EXPECT_EQ(VK_SUCCESS, res);
121 EXPECT_EQ(count, expected_count);
122 std::vector<VkExtensionProperties> extensions{count};
123 res = functions->vkEnumerateDeviceExtensionProperties(physical_device, nullptr, &count, extensions.data());
124 EXPECT_EQ(VK_SUCCESS, res);
125 EXPECT_EQ(count, expected_count);
126 return extensions;
127 }
128
129 std::vector<VkExtensionProperties> InstWrapper::EnumerateLayerDeviceExtensions(VkPhysicalDevice physical_device,
130 const char* layer_name, uint32_t expected_count) {
131 uint32_t count = 0;
132 VkResult res = functions->vkEnumerateDeviceExtensionProperties(physical_device, layer_name, &count, nullptr);
133 EXPECT_EQ(VK_SUCCESS, res);
134 EXPECT_EQ(count, expected_count);
135 std::vector<VkExtensionProperties> extensions{count};
136 res = functions->vkEnumerateDeviceExtensionProperties(physical_device, layer_name, &count, extensions.data());
137 EXPECT_EQ(VK_SUCCESS, res);
138 EXPECT_EQ(count, expected_count);
139 return extensions;
140 }
141
142 DeviceWrapper::DeviceWrapper(InstWrapper& inst_wrapper, VkAllocationCallbacks* callbacks) noexcept
143 : functions(inst_wrapper.functions), callbacks(callbacks){};
144 DeviceWrapper::DeviceWrapper(VulkanFunctions& functions, VkDevice device, VkAllocationCallbacks* callbacks) noexcept
145 : functions(&functions), dev(device), callbacks(callbacks){};
146 DeviceWrapper::~DeviceWrapper() noexcept { functions->vkDestroyDevice(dev, callbacks); }
147
148 DeviceWrapper::DeviceWrapper(DeviceWrapper&& other) noexcept {
149 functions = other.functions;
150 dev = other.dev;
151 callbacks = other.callbacks;
152 create_info = other.create_info;
153 other.dev = VK_NULL_HANDLE;
154 }
155 DeviceWrapper& DeviceWrapper::operator=(DeviceWrapper&& other) noexcept {
156 functions->vkDestroyDevice(dev, callbacks);
157 functions = other.functions;
158 dev = other.dev;
159 callbacks = other.callbacks;
160 create_info = other.create_info;
161 other.dev = VK_NULL_HANDLE;
162 return *this;
163 }
164
165 void DeviceWrapper::CheckCreate(VkPhysicalDevice phys_dev, VkResult result_to_check) {
166 handle_assert_null(dev);
167 ASSERT_EQ(result_to_check, functions->vkCreateDevice(phys_dev, create_info.get(), callbacks, &dev));
168 }
169
170 VkResult CreateDebugUtilsMessenger(DebugUtilsWrapper& debug_utils) {
171 handle_assert_null(debug_utils.messenger);
172 return debug_utils.local_vkCreateDebugUtilsMessengerEXT(debug_utils.inst, debug_utils.get(), debug_utils.callbacks,
173 &debug_utils.messenger);
174 }
175
176 void FillDebugUtilsCreateDetails(InstanceCreateInfo& create_info, DebugUtilsLogger& logger) {
177 create_info.add_extension("VK_EXT_debug_utils");
178 create_info.instance_info.pNext = logger.get();
179 }
180 void FillDebugUtilsCreateDetails(InstanceCreateInfo& create_info, DebugUtilsWrapper& wrapper) {
181 create_info.add_extension("VK_EXT_debug_utils");
182 create_info.instance_info.pNext = wrapper.get();
183 }
184
185 // Look through the event log. If you find a line containing the prefix we're interested in, look for the end of
186 // line character, and then see if the postfix occurs in it as well.
187 bool DebugUtilsLogger::find_prefix_then_postfix(const char* prefix, const char* postfix) const {
188 size_t new_start = 0;
189 size_t postfix_index = 0;
190 size_t next_eol = 0;
191 while ((new_start = returned_output.find(prefix, new_start)) != std::string::npos) {
192 next_eol = returned_output.find("\n", new_start);
193 if ((postfix_index = returned_output.find(postfix, new_start)) != std::string::npos) {
194 if (postfix_index < next_eol) {
195 return true;
196 }
197 }
198 new_start = next_eol + 1;
199 }
200 return false;
201 }
202
203 bool FindPrefixPostfixStringOnLine(DebugUtilsLogger const& env_log, const char* prefix, const char* postfix) {
204 return env_log.find_prefix_then_postfix(prefix, postfix);
205 }
206
207 PlatformShimWrapper::PlatformShimWrapper(fs::FileSystemManager& file_system_manager, const char* log_filter) noexcept
208 : loader_logging{"VK_LOADER_DEBUG"} {
209 #if defined(WIN32) || defined(__APPLE__)
210 shim_library = LibraryWrapper(SHIM_LIBRARY_NAME);
211 PFN_get_platform_shim get_platform_shim = shim_library.get_symbol(GET_PLATFORM_SHIM_STR);
212 assert(get_platform_shim != NULL && "Must be able to get \"platform_shim\"");
213 platform_shim = get_platform_shim();
214 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__GNU__)
215 platform_shim = get_platform_shim();
216 #endif
217 platform_shim->file_system_manager = &file_system_manager;
218
219 if (log_filter) {
220 loader_logging.set_new_value(log_filter);
221 }
222 }
223
224 template <>
225 TestBinaryHandle<TestICD, GetTestICDFunc, GetNewTestICDFunc>::TestBinaryHandle(std::filesystem::path const& binary_path) noexcept
226 : library(binary_path) {
227 proc_addr_get_test_binary = library.get_symbol(GET_TEST_ICD_FUNC_STR);
228 proc_addr_reset_binary = library.get_symbol(RESET_ICD_FUNC_STR);
229 }
230
231 template <>
232 TestBinaryHandle<TestLayer, GetTestLayerFunc, GetNewTestLayerFunc>::TestBinaryHandle(
233 std::filesystem::path const& binary_path) noexcept
234 : library(binary_path) {
235 proc_addr_get_test_binary = library.get_symbol(GET_TEST_LAYER_FUNC_STR);
236 proc_addr_reset_binary = library.get_symbol(RESET_LAYER_FUNC_STR);
237 }
238
239 FrameworkEnvironment::FrameworkEnvironment() noexcept : FrameworkEnvironment(FrameworkSettings{}) {}
240 FrameworkEnvironment::FrameworkEnvironment(FrameworkSettings const& settings) noexcept
241 : settings(settings), platform_shim(file_system_manager, settings.log_filter) {
242 // Setup default path redirections
243
244 platform_shim->set_elevated_privilege(settings.run_as_if_with_elevated_privleges);
245
246 #if TESTING_COMMON_UNIX_PLATFORMS
247 if (!settings.home_env_var.empty()) env_var_home.set_new_value(settings.home_env_var);
248 #if !defined(__APPLE__)
249 if (!settings.xdg_config_home_env_var.empty()) env_var_xdg_config_home.set_new_value(settings.xdg_config_home_env_var);
250 if (!settings.xdg_config_dirs_env_var.empty()) env_var_xdg_config_dirs.set_new_value(settings.xdg_config_dirs_env_var);
251 if (!settings.xdg_data_home_env_var.empty()) env_var_xdg_data_home.set_new_value(settings.xdg_data_home_env_var);
252 if (!settings.xdg_data_dirs_env_var.empty()) env_var_xdg_data_dirs.set_new_value(settings.xdg_data_dirs_env_var);
253 #endif
254 #endif
255
256 const std::array<std::pair<const char*, ManifestLocation>, 4> secured_redirection_map = {
257 std::pair{"icd.d", ManifestLocation::driver},
258 std::pair{"implicit_layer.d", ManifestLocation::implicit_layer},
259 std::pair{"explicit_layer.d", ManifestLocation::explicit_layer},
260 std::pair{"loader_settings.d", ManifestLocation::settings_location},
261 };
262
263 const std::array<std::pair<const char*, ManifestLocation>, 4> unsecured_redirection_map = {
264 std::pair{"icd.d", ManifestLocation::unsecured_driver},
265 std::pair{"implicit_layer.d", ManifestLocation::unsecured_implicit_layer},
266 std::pair{"explicit_layer.d", ManifestLocation::unsecured_explicit_layer},
267 std::pair{"loader_settings.d", ManifestLocation::unsecured_settings},
268 };
269
270 #if TESTING_COMMON_UNIX_PLATFORMS && !defined(__APPLE__)
271 // Always are searching SYSCONFDIR on unix (but not apple, which is handled separately)
272 secure_manifest_base_location = SYSCONFDIR;
273
274 for (auto const& [redirect, location] : secured_redirection_map) {
275 file_system_manager.add_path_redirect(secure_manifest_base_location + "/vulkan/" + redirect, location);
276 }
277
278 // Determines which unsecure path should be used
279 if (!settings.run_as_if_with_elevated_privleges) {
280 if (!settings.xdg_config_home_env_var.empty()) {
281 auto env_var_list = split_env_var_as_list(settings.xdg_config_home_env_var);
282 unsecure_manifest_base_location = env_var_list.at(0);
283 if (!env_var_list.empty()) {
284 for (auto const& [redirect, location] : unsecured_redirection_map) {
285 file_system_manager.add_path_redirect(env_var_list.at(0) + "/vulkan/" + redirect, location);
286 }
287 }
288 } else if (!settings.xdg_data_home_env_var.empty()) {
289 auto env_var_list = split_env_var_as_list(settings.xdg_data_home_env_var);
290 if (!env_var_list.empty()) {
291 unsecure_manifest_base_location = env_var_list.at(0);
292 for (auto const& [redirect, location] : unsecured_redirection_map) {
293 file_system_manager.add_path_redirect(env_var_list.at(0) + "/vulkan/" + redirect, location);
294 }
295 }
296 } else {
297 std::string home = settings.home_env_var;
298 for (auto const& [redirect, location] : unsecured_redirection_map) {
299 unsecure_manifest_base_location = home + "/.config";
300 file_system_manager.add_path_redirect(home + "/.config/vulkan/" + redirect, location);
301 }
302 }
303 }
304 #endif
305
306 #if defined(__APPLE__)
307 // Since XDG env-var shouldn't ever be defined on apple, FALLBACK_DATA_DIRS takes over as the 'global' location to search, eg
308 // /usr/local/share
309 auto env_var_list = split_env_var_as_list(FALLBACK_DATA_DIRS);
310 assert(env_var_list.size() > 0 && "FALLBACK_DATA_DIRS was set to an empty path");
311 secure_manifest_base_location = env_var_list.at(0);
312 for (auto const& [redirect, location] : secured_redirection_map) {
313 file_system_manager.add_path_redirect(secure_manifest_base_location + "/vulkan/" + redirect, location);
314 }
315
316 std::string home = settings.home_env_var;
317 unsecure_manifest_base_location = home + "/.config";
318 for (auto const& [redirect, location] : unsecured_redirection_map) {
319 file_system_manager.add_path_redirect(home + "/.config/vulkan/" + redirect, location);
320 }
321
322 // Necessary since bundles look in sub folders for manifests, not the test framework folder itself
323 auto bundle_location = get_folder(ManifestLocation::macos_bundle).location();
324 file_system_manager.add_path_redirect(bundle_location / "vulkan/icd.d", ManifestLocation::macos_bundle);
325 file_system_manager.add_path_redirect(bundle_location / "vulkan/explicit_layer.d", ManifestLocation::macos_bundle);
326 file_system_manager.add_path_redirect(bundle_location / "vulkan/implicit_layer.d", ManifestLocation::macos_bundle);
327 #endif
328
329 platform_shim->is_finished_setup = true;
330 }
331
332 FrameworkEnvironment::~FrameworkEnvironment() {
333 // This is necessary to prevent the folder manager from using dead memory during destruction.
334 // What happens is that each folder manager tries to cleanup itself. Except, folders that were never called did not have
335 // their DirEntry array's filled out. So when that folder calls delete_folder, which calls readdir, the shim tries to order
336 // the files. Except, the list of files is in a object that is currently being destroyed.
337 platform_shim->is_during_destruction = true;
338 }
339
340 ManifestLocation FrameworkEnvironment::map_discovery_type_to_location(ManifestDiscoveryType discovery_type,
341 ManifestCategory category) {
342 switch (discovery_type) {
343 case (ManifestDiscoveryType::env_var):
344 switch (category) {
345 case (ManifestCategory::explicit_layer):
346 return ManifestLocation::explicit_layer_env_var;
347 case (ManifestCategory::implicit_layer):
348 return ManifestLocation::implicit_layer_env_var;
349 case (ManifestCategory::icd):
350 return ManifestLocation::driver_env_var;
351 default:
352 return ManifestLocation::null;
353 }
354 case (ManifestDiscoveryType::add_env_var):
355 switch (category) {
356 case (ManifestCategory::explicit_layer):
357 return ManifestLocation::explicit_layer_add_env_var;
358 case (ManifestCategory::implicit_layer):
359 return ManifestLocation::implicit_layer_add_env_var;
360 case (ManifestCategory::icd):
361 return ManifestLocation::driver_env_var;
362 default:
363 return ManifestLocation::null;
364 }
365 #if defined(WIN32)
366 case (ManifestDiscoveryType::windows_app_package):
367 return ManifestLocation::windows_app_package;
368 #endif
369 case (ManifestDiscoveryType::override_folder):
370 return ManifestLocation::override_layer;
371 #if defined(__APPLE__)
372 case (ManifestDiscoveryType::macos_bundle):
373 return ManifestLocation::macos_bundle;
374 #endif
375 case (ManifestDiscoveryType::unsecured_generic):
376 switch (category) {
377 case (ManifestCategory::explicit_layer):
378 return ManifestLocation::unsecured_explicit_layer;
379 case (ManifestCategory::implicit_layer):
380 return ManifestLocation::unsecured_implicit_layer;
381 case (ManifestCategory::icd):
382 return ManifestLocation::unsecured_driver;
383 case (ManifestCategory::settings):
384 return ManifestLocation::unsecured_settings;
385 default:
386 return ManifestLocation::null;
387 }
388 case (ManifestDiscoveryType::null_dir):
389 case (ManifestDiscoveryType::none):
390 return ManifestLocation::null;
391 default:
392 case (ManifestDiscoveryType::generic):
393 switch (category) {
394 case (ManifestCategory::explicit_layer):
395 return ManifestLocation::explicit_layer;
396 case (ManifestCategory::implicit_layer):
397 return ManifestLocation::implicit_layer;
398 case (ManifestCategory::icd):
399 return ManifestLocation::driver;
400 case (ManifestCategory::settings):
401 return ManifestLocation::settings_location;
402 default:
403 return ManifestLocation::null;
404 }
405 }
406 }
407
408 TestICD& FrameworkEnvironment::add_icd(std::filesystem::path const& path, ManifestOptions args, ManifestICD manifest) noexcept {
409 manifest.set_lib_path(path);
410
411 if (args.json_name.empty()) {
412 args.json_name = "test_icd_" + std::to_string(icds.size()) + ".json";
413 }
414
415 size_t cur_icd_index = icds.size();
416 auto& folder = get_folder(map_discovery_type_to_location(args.discovery_type, ManifestCategory::icd));
417
418 if (args.discovery_type == ManifestDiscoveryType::env_var) {
419 if (args.is_dir) {
420 env_var_vk_icd_filenames.add_to_list(folder.location());
421 } else {
422 env_var_vk_icd_filenames.add_to_list(folder.location() / args.json_name);
423 }
424 } else if (args.discovery_type == ManifestDiscoveryType::add_env_var) {
425 if (args.is_dir) {
426 add_env_var_vk_icd_filenames.add_to_list(folder.location());
427 } else {
428 add_env_var_vk_icd_filenames.add_to_list(folder.location() / args.json_name);
429 }
430 }
431 if (!args.is_fake) {
432 std::filesystem::path new_lib_name = manifest.lib_path.stem();
433 new_lib_name += "_";
434 new_lib_name += std::to_string(cur_icd_index);
435 new_lib_name += manifest.lib_path.extension();
436 auto new_driver_location = folder.copy_file(manifest.lib_path, new_lib_name);
437
438 #if TESTING_COMMON_UNIX_PLATFORMS
439 if (args.library_path_type == LibraryPathType::default_search_paths) {
440 platform_shim->add_system_library(new_lib_name, new_driver_location);
441 }
442 #endif
443 #if defined(WIN32)
444 if (args.library_path_type == LibraryPathType::default_search_paths) {
445 SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_USER_DIRS);
446 AddDllDirectory(new_driver_location.parent_path().native().c_str());
447 }
448 #endif
449 icds.push_back(TestICDHandle(new_driver_location));
450 icds.back().reset();
451 if (args.library_path_type == LibraryPathType::relative) {
452 manifest.lib_path = std::filesystem::path(".") / new_lib_name;
453 } else if (args.library_path_type == LibraryPathType::default_search_paths) {
454 manifest.lib_path = new_lib_name;
455 } else {
456 manifest.lib_path = new_driver_location;
457 }
458 }
459 if (args.discovery_type != ManifestDiscoveryType::none) {
460 icds.back().manifest_path = folder.write_manifest(args.json_name, manifest.get_manifest_str());
461 icds.back().shimmed_manifest_path = icds.back().manifest_path;
462
463 #if defined(WIN32)
464 // only add the manifest to the registry if its a generic location (as if it was installed) - both system and user local
465 if (args.discovery_type == ManifestDiscoveryType::generic) {
466 platform_shim->add_manifest_to_registry(ManifestCategory::icd, icds.back().manifest_path);
467 }
468 if (args.discovery_type == ManifestDiscoveryType::unsecured_generic) {
469 platform_shim->add_unsecured_manifest_to_registry(ManifestCategory::icd, icds.back().manifest_path);
470 }
471 if (args.discovery_type == ManifestDiscoveryType::windows_app_package) {
472 platform_shim->set_app_package_path(folder.location());
473 }
474 #endif
475 #if TESTING_COMMON_UNIX_PLATFORMS
476 if (args.discovery_type == ManifestDiscoveryType::generic ||
477 args.discovery_type == ManifestDiscoveryType::unsecured_generic) {
478 icds.back().shimmed_manifest_path =
479 file_system_manager.get_path_redirect_by_manifest_location(
480 args.discovery_type == ManifestDiscoveryType::unsecured_generic ? ManifestLocation::driver
481 : ManifestLocation::unsecured_driver) /
482 args.json_name;
483 }
484 #endif
485 }
486 return icds.back().get_test_binary();
487 }
488
489 void FrameworkEnvironment::add_implicit_layer(ManifestOptions args, ManifestLayer layer_manifest) noexcept {
490 add_layer_impl(args, layer_manifest, ManifestCategory::implicit_layer);
491 }
492 void FrameworkEnvironment::add_explicit_layer(ManifestOptions args, ManifestLayer layer_manifest) noexcept {
493 add_layer_impl(args, layer_manifest, ManifestCategory::explicit_layer);
494 }
495
496 void FrameworkEnvironment::add_layer_impl(ManifestOptions args, ManifestLayer manifest, ManifestCategory category) {
497 if (args.json_name.empty()) {
498 args.json_name = "test_layer_" + std::to_string(created_layer_count) + ".json";
499 created_layer_count++;
500 }
501
502 auto& folder = get_folder(map_discovery_type_to_location(args.discovery_type, category));
503
504 if (args.discovery_type == ManifestDiscoveryType::env_var || args.discovery_type == ManifestDiscoveryType::add_env_var) {
505 std::filesystem::path path_to_add = folder.location();
506 if (!args.is_dir) {
507 path_to_add /= args.json_name;
508 }
509
510 EnvVarWrapper* env_var_to_use = nullptr;
511 if (args.discovery_type == ManifestDiscoveryType::env_var && category == ManifestCategory::explicit_layer) {
512 env_var_vk_layer_paths.add_to_list(path_to_add);
513 } else if (args.discovery_type == ManifestDiscoveryType::env_var && category == ManifestCategory::implicit_layer) {
514 env_var_vk_implicit_layer_paths.add_to_list(path_to_add);
515 } else if (args.discovery_type == ManifestDiscoveryType::add_env_var && category == ManifestCategory::explicit_layer) {
516 add_env_var_vk_layer_paths.add_to_list(path_to_add);
517 } else if (args.discovery_type == ManifestDiscoveryType::add_env_var && category == ManifestCategory::implicit_layer) {
518 add_env_var_vk_implicit_layer_paths.add_to_list(path_to_add);
519 }
520 }
521
522 size_t new_layers_start = layers.size();
523 for (auto& layer : manifest.layers) {
524 if (!layer.lib_path.empty()) {
525 std::filesystem::path new_lib_path = layer.lib_path.stem();
526 new_lib_path += "_";
527 new_lib_path += std::to_string(layers.size());
528 new_lib_path += layer.lib_path.extension();
529
530 auto new_layer_location = folder.copy_file(layer.lib_path, new_lib_path);
531
532 #if TESTING_COMMON_UNIX_PLATFORMS
533 if (args.library_path_type == LibraryPathType::default_search_paths) {
534 platform_shim->add_system_library(new_lib_path, new_layer_location);
535 }
536 #endif
537 #if defined(WIN32)
538 if (args.library_path_type == LibraryPathType::default_search_paths) {
539 SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_USER_DIRS);
540 AddDllDirectory(new_layer_location.parent_path().native().c_str());
541 }
542 #endif
543
544 // Don't load the layer binary if using any of the wrap objects layers, since it doesn't export the same interface
545 // functions
546 if (!args.is_fake && layer.lib_path.stem().string().find(
547 std::filesystem::path(TEST_LAYER_WRAP_OBJECTS).stem().string()) == std::string::npos) {
548 layers.push_back(TestLayerHandle(new_layer_location));
549 layers.back().reset();
550 }
551 if (args.library_path_type == LibraryPathType::relative) {
552 layer.lib_path = std::filesystem::path(".") / new_lib_path;
553 } else if (args.library_path_type == LibraryPathType::default_search_paths) {
554 layer.lib_path = new_lib_path;
555 } else {
556 layer.lib_path = new_layer_location;
557 }
558 }
559 }
560 if (args.discovery_type != ManifestDiscoveryType::none) {
561 // Write a manifest file to a folder as long as the discovery type isn't none
562 auto layer_manifest_loc = folder.write_manifest(args.json_name, manifest.get_manifest_str());
563 #if defined(WIN32)
564 // only add the manifest to the registry if its a generic location (as if it was installed) - both system and user local
565 if (args.discovery_type == ManifestDiscoveryType::generic) {
566 platform_shim->add_manifest_to_registry(category, layer_manifest_loc);
567 }
568 if (args.discovery_type == ManifestDiscoveryType::unsecured_generic) {
569 platform_shim->add_unsecured_manifest_to_registry(category, layer_manifest_loc);
570 }
571 if (args.discovery_type == ManifestDiscoveryType::windows_app_package) {
572 platform_shim->set_app_package_path(folder.location());
573 }
574 #endif
575 for (size_t i = new_layers_start; i < layers.size(); i++) {
576 layers.at(i).manifest_path = layer_manifest_loc;
577 layers.at(i).shimmed_manifest_path = layer_manifest_loc;
578 #if TESTING_COMMON_UNIX_PLATFORMS
579 if (args.discovery_type == ManifestDiscoveryType::generic ||
580 args.discovery_type == ManifestDiscoveryType::unsecured_generic) {
581 ManifestLocation location = ManifestLocation::null;
582 if (args.discovery_type == ManifestDiscoveryType::generic) {
583 location = (category == ManifestCategory::explicit_layer ? ManifestLocation::explicit_layer
584 : ManifestLocation::implicit_layer);
585 } else if (args.discovery_type == ManifestDiscoveryType::unsecured_generic) {
586 location = (category == ManifestCategory::explicit_layer ? ManifestLocation::unsecured_explicit_layer
587 : ManifestLocation::unsecured_implicit_layer);
588 }
589 layers.at(i).shimmed_manifest_path =
590 file_system_manager.get_path_redirect_by_manifest_location(location) / args.json_name;
591 }
592 #endif
593 }
594 }
595 }
596
597 std::string get_loader_settings_file_contents(const LoaderSettings& loader_settings) noexcept {
598 JsonWriter writer;
599 writer.StartObject();
600 writer.AddKeyedString("file_format_version", loader_settings.file_format_version.get_version_str());
601 bool one_setting_file = true;
602 if (loader_settings.app_specific_settings.size() > 1) {
603 writer.StartKeyedArray("settings_array");
604 one_setting_file = false;
605 }
606 for (const auto& setting : loader_settings.app_specific_settings) {
607 if (one_setting_file) {
608 writer.StartKeyedObject("settings");
609 } else {
610 writer.StartObject();
611 }
612 if (!setting.app_keys.empty()) {
613 writer.StartKeyedArray("app_keys");
614 for (const auto& app_key : setting.app_keys) {
615 writer.AddString(app_key);
616 }
617 writer.EndArray();
618 }
619 if (!setting.layer_configurations.empty()) {
620 writer.StartKeyedArray("layers");
621 for (const auto& config : setting.layer_configurations) {
622 writer.StartObject();
623 writer.AddKeyedString("name", config.name);
624 writer.AddKeyedString("path", config.path.native());
625 writer.AddKeyedString("control", config.control);
626 writer.AddKeyedBool("treat_as_implicit_manifest", config.treat_as_implicit_manifest);
627 writer.EndObject();
628 }
629 writer.EndArray();
630 }
631 if (!setting.stderr_log.empty()) {
632 writer.StartKeyedArray("stderr_log");
633 for (const auto& filter : setting.stderr_log) {
634 writer.AddString(filter);
635 }
636 writer.EndArray();
637 }
638 if (!setting.log_configurations.empty()) {
639 writer.StartKeyedArray("log_locations");
640 for (const auto& config : setting.log_configurations) {
641 writer.StartObject();
642 writer.StartKeyedArray("destinations");
643 for (const auto& dest : config.destinations) {
644 writer.AddString(dest);
645 }
646 writer.EndArray();
647 writer.StartKeyedArray("filter");
648 for (const auto& filter : config.filters) {
649 writer.AddString(filter);
650 }
651 writer.EndArray();
652 writer.EndObject();
653 }
654 writer.EndArray();
655 }
656 if (!setting.driver_configurations.empty()) {
657 writer.AddKeyedBool("additional_drivers_use_exclusively", setting.additional_drivers_use_exclusively);
658 writer.StartKeyedArray("additional_drivers");
659 for (const auto& driver : setting.driver_configurations) {
660 writer.StartObject();
661 writer.AddKeyedString("path", driver.path);
662 writer.EndObject();
663 }
664 writer.EndArray();
665 }
666 if (!setting.device_configurations.empty()) {
667 writer.StartKeyedArray("device_configurations");
668 for (const auto& device : setting.device_configurations) {
669 writer.StartObject();
670 if (!device.deviceName.empty()) {
671 writer.AddKeyedString("deviceName", device.deviceName);
672 }
673 if (!device.driverName.empty()) {
674 writer.AddKeyedString("driverName", device.driverName);
675 }
676 writer.StartKeyedArray("deviceUUID");
677 for (const auto& u : device.deviceUUID) {
678 writer.AddInteger(u);
679 }
680 writer.EndArray();
681
682 writer.StartKeyedArray("driverUUID");
683 for (const auto& u : device.driverUUID) {
684 writer.AddInteger(u);
685 }
686 writer.EndArray();
687 writer.AddKeyedInteger("driverVersion", device.driverVersion);
688 writer.EndObject();
689 }
690 writer.EndArray();
691 }
692 writer.EndObject();
693 }
694 if (!one_setting_file) {
695 writer.EndArray();
696 }
697
698 writer.EndObject();
699 return writer.output;
700 }
701 void FrameworkEnvironment::write_settings_file(std::string const& file_contents, bool write_to_secure_location) {
702 auto location = write_to_secure_location ? ManifestLocation::settings_location : ManifestLocation::unsecured_settings;
703 auto out_path = get_folder(location).write_manifest("vk_loader_settings.json", file_contents);
704 #if defined(WIN32)
705 if (write_to_secure_location) {
706 platform_shim->hkey_local_machine_settings.clear();
707 platform_shim->add_manifest_to_registry(ManifestCategory::settings, out_path);
708 } else {
709 platform_shim->hkey_current_user_settings.clear();
710 platform_shim->add_unsecured_manifest_to_registry(ManifestCategory::settings, out_path);
711 }
712 #endif
713 }
714 void FrameworkEnvironment::update_loader_settings(const LoaderSettings& settings, bool write_to_secure_location) noexcept {
715 write_settings_file(get_loader_settings_file_contents(settings), write_to_secure_location);
716 }
717 void FrameworkEnvironment::remove_loader_settings() {
718 get_folder(ManifestLocation::settings_location).remove("vk_loader_settings.json");
719 }
720 void FrameworkEnvironment::write_file_from_string(std::string const& source_string, ManifestCategory category,
721 ManifestLocation location, std::string const& file_name) {
722 auto out_path = get_folder(location).write_manifest(file_name, source_string);
723
724 #if defined(WIN32)
725 // Only writes to the hkey_local_machine registries, doesn't support hkey_current_user registries
726 platform_shim->add_manifest_to_registry(category, out_path);
727 #endif
728 }
729 void FrameworkEnvironment::write_file_from_source(const char* source_file, ManifestCategory category, ManifestLocation location,
730 std::string const& file_name) {
731 std::fstream file{source_file, std::ios_base::in};
732 ASSERT_TRUE(file.is_open());
733 std::stringstream file_stream;
734 file_stream << file.rdbuf();
735 write_file_from_string(file_stream.str(), category, location, file_name);
736 }
737
738 TestICD& FrameworkEnvironment::get_test_icd(size_t index) noexcept { return icds[index].get_test_binary(); }
739 TestICD& FrameworkEnvironment::reset_icd(size_t index) noexcept { return icds[index].reset(); }
740 std::filesystem::path FrameworkEnvironment::get_test_icd_path(size_t index) noexcept { return icds[index].get_full_path(); }
741 std::filesystem::path FrameworkEnvironment::get_icd_manifest_path(size_t index) noexcept { return icds[index].get_manifest_path(); }
742 std::filesystem::path FrameworkEnvironment::get_shimmed_icd_manifest_path(size_t index) noexcept {
743 return icds[index].get_shimmed_manifest_path();
744 }
745
746 TestLayer& FrameworkEnvironment::get_test_layer(size_t index) noexcept { return layers[index].get_test_binary(); }
747 TestLayer& FrameworkEnvironment::reset_layer(size_t index) noexcept { return layers[index].reset(); }
748 std::filesystem::path FrameworkEnvironment::get_test_layer_path(size_t index) noexcept { return layers[index].get_full_path(); }
749 std::filesystem::path FrameworkEnvironment::get_layer_manifest_path(size_t index) noexcept {
750 return layers[index].get_manifest_path();
751 }
752 std::filesystem::path FrameworkEnvironment::get_shimmed_layer_manifest_path(size_t index) noexcept {
753 return layers[index].get_shimmed_manifest_path();
754 }
755
756 fs::Folder& FrameworkEnvironment::get_folder(ManifestLocation location) noexcept {
757 return file_system_manager.get_folder(location);
758 }
759 fs::Folder const& FrameworkEnvironment::get_folder(ManifestLocation location) const noexcept {
760 return file_system_manager.get_folder(location);
761 }
762 #if defined(__APPLE__)
763 void FrameworkEnvironment::setup_macos_bundle() noexcept {
764 platform_shim->bundle_contents = file_system_manager.get_folder(ManifestLocation::macos_bundle).location();
765 }
766 #endif
767
768 void FrameworkEnvironment::add_symlink(ManifestLocation location, std::filesystem::path const& target,
769 std::filesystem::path const& link_name) {
770 auto symlinked_path = get_folder(location).add_symlink(target, link_name);
771 file_system_manager.add_path_redirect(symlinked_path, location);
772 }
773
774 std::vector<VkExtensionProperties> FrameworkEnvironment::GetInstanceExtensions(uint32_t expected_count, const char* layer_name) {
775 uint32_t count = 0;
776 VkResult res = vulkan_functions.vkEnumerateInstanceExtensionProperties(layer_name, &count, nullptr);
777 EXPECT_EQ(VK_SUCCESS, res);
778 EXPECT_EQ(count, expected_count);
779 std::vector<VkExtensionProperties> extension_props{count};
780 res = vulkan_functions.vkEnumerateInstanceExtensionProperties(layer_name, &count, extension_props.data());
781 EXPECT_EQ(VK_SUCCESS, res);
782 EXPECT_EQ(count, expected_count);
783 return extension_props;
784 }
785 std::vector<VkLayerProperties> FrameworkEnvironment::GetLayerProperties(uint32_t expected_count) {
786 uint32_t count = 0;
787 VkResult res = vulkan_functions.vkEnumerateInstanceLayerProperties(&count, nullptr);
788 EXPECT_EQ(VK_SUCCESS, res);
789 EXPECT_EQ(count, expected_count);
790 std::vector<VkLayerProperties> layer_props{count};
791 res = vulkan_functions.vkEnumerateInstanceLayerProperties(&count, layer_props.data());
792 EXPECT_EQ(VK_SUCCESS, res);
793 EXPECT_EQ(count, expected_count);
794 return layer_props;
795 }
796
797 template <typename CreationFunc, typename CreateInfo>
798 VkResult create_surface_helper(VulkanFunctions* functions, VkInstance inst, VkSurfaceKHR& surface, const char* load_func_name,
799 VkStructureType stype) {
800 CreationFunc pfn_CreateSurface = functions->load(inst, load_func_name);
801 if (!pfn_CreateSurface) return VK_ERROR_EXTENSION_NOT_PRESENT;
802 CreateInfo surf_create_info{stype};
803 return pfn_CreateSurface(inst, &surf_create_info, nullptr, &surface);
804 }
805 VkResult create_surface(VulkanFunctions* functions, VkInstance inst, VkSurfaceKHR& surface,
806 [[maybe_unused]] const char* api_selection) {
807 #if defined(VK_USE_PLATFORM_ANDROID_KHR)
808 return create_surface_helper<PFN_vkCreateAndroidSurfaceKHR, VkAndroidSurfaceCreateInfoKHR>(
809 functions, inst, surface, "vkCreateAndroidSurfaceKHR", VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR);
810 #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
811 return create_surface_helper<PFN_vkCreateDirectFBSurfaceEXT, VkDirectFBSurfaceCreateInfoEXT>(
812 functions, inst, surface, "vkCreateDirectFBSurfaceEXT", VK_STRUCTURE_TYPE_DIRECTFB_SURFACE_CREATE_INFO_EXT);
813 #elif defined(VK_USE_PLATFORM_FUCHSIA)
814 return create_surface_helper<PFN_vkCreateImagePipeSurfaceFUCHSIA, VkImagePipeSurfaceCreateInfoFUCHSIA>(
815 functions, inst, surface, "vkCreateImagePipeSurfaceFUCHSIA", VK_STRUCTURE_TYPE_IMAGEPIPE_SURFACE_CREATE_INFO_FUCHSIA);
816 #elif defined(VK_USE_PLATFORM_GGP)
817 return create_surface_helper<PFN__vkCreateStreamDescriptorSurfaceGGP, VkStreamDescriptorSurfaceCreateInfoGGP>(
818 functions, inst, surface, "vkCreateStreamDescriptorSurfaceGGP",
819 VK_STRUCTURE_TYPE_STREAM_DESCRIPTOR_SURFACE_CREATE_INFO_GGP);
820 #elif defined(VK_USE_PLATFORM_IOS_MVK)
821 return create_surface_helper<PFN_vkCreateIOSSurfaceMVK, VkIOSSurfaceCreateInfoMVK>(
822 functions, inst, surface, "vkCreateIOSSurfaceMVK", VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK);
823 #elif defined(VK_USE_PLATFORM_MACOS_MVK) || defined(VK_USE_PLATFORM_METAL_EXT)
824 #if defined(VK_USE_PLATFORM_MACOS_MVK)
825 if (api_selection != nullptr && string_eq(api_selection, "VK_USE_PLATFORM_MACOS_MVK"))
826 return create_surface_helper<PFN_vkCreateMacOSSurfaceMVK, VkMacOSSurfaceCreateInfoMVK>(
827 functions, inst, surface, "vkCreateMacOSSurfaceMVK", VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK);
828 #endif
829 #if defined(VK_USE_PLATFORM_METAL_EXT)
830 if (api_selection == nullptr || (api_selection != nullptr && string_eq(api_selection, "VK_USE_PLATFORM_METAL_EXT")))
831 return create_surface_helper<PFN_vkCreateMetalSurfaceEXT, VkMetalSurfaceCreateInfoEXT>(
832 functions, inst, surface, "vkCreateMetalSurfaceEXT", VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT);
833 #endif
834 return VK_ERROR_NOT_PERMITTED_KHR;
835 #elif defined(VK_USE_PLATFORM_SCREEN_QNX)
836 return create_surface_helper<PFN_vkCreateScreenSurfaceQNX, VkScreenSurfaceCreateInfoQNX>(
837 functions, inst, surface, "vkCreateScreenSurfaceQNX", VK_STRUCTURE_TYPE_SCREEN_SURFACE_CREATE_INFO_QNX);
838 #elif defined(VK_USE_PLATFORM_VI_NN)
839 return create_surface_helper<PFN_vkCreateViSurfaceNN, VkViSurfaceCreateInfoNN>(functions, inst, surface, "vkCreateViSurfaceNN",
840 VK_STRUCTURE_TYPE_VI_SURFACE_CREATE_INFO_NN);
841 #elif defined(VK_USE_PLATFORM_WIN32_KHR)
842 return create_surface_helper<PFN_vkCreateWin32SurfaceKHR, VkWin32SurfaceCreateInfoKHR>(
843 functions, inst, surface, "vkCreateWin32SurfaceKHR", VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR);
844 #elif defined(VK_USE_PLATFORM_XCB_KHR) || defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_WAYLAND_KHR)
845 #if defined(VK_USE_PLATFORM_XLIB_KHR)
846 if (string_eq(api_selection, "VK_USE_PLATFORM_XLIB_KHR"))
847 return create_surface_helper<PFN_vkCreateXlibSurfaceKHR, VkXlibSurfaceCreateInfoKHR>(
848 functions, inst, surface, "vkCreateXlibSurfaceKHR", VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR);
849 #endif
850 #if defined(VK_USE_PLATFORM_WAYLAND_KHR)
851 if (string_eq(api_selection, "VK_USE_PLATFORM_WAYLAND_KHR"))
852 return create_surface_helper<PFN_vkCreateWaylandSurfaceKHR, VkWaylandSurfaceCreateInfoKHR>(
853 functions, inst, surface, "vkCreateWaylandSurfaceKHR", VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR);
854 #endif
855 #if defined(VK_USE_PLATFORM_XCB_KHR)
856 if (api_selection == nullptr || string_eq(api_selection, "VK_USE_PLATFORM_XCB_KHR"))
857 return create_surface_helper<PFN_vkCreateXcbSurfaceKHR, VkXcbSurfaceCreateInfoKHR>(
858 functions, inst, surface, "vkCreateXcbSurfaceKHR", VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR);
859 #endif
860 return VK_ERROR_NOT_PERMITTED_KHR;
861 #else
862 return create_surface_helper<PFN_vkCreateDisplayPlaneSurfaceKHR, VkDisplaySurfaceCreateInfoKHR>(
863 functions, inst, surface, "vkCreateDisplayPlaneSurfaceKHR", VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR);
864 #endif
865 }
866 VkResult create_surface(InstWrapper& inst, VkSurfaceKHR& surface, const char* api_selection) {
867 return create_surface(inst.functions, inst.inst, surface, api_selection);
868 }
869
870 VkResult create_debug_callback(InstWrapper& inst, const VkDebugReportCallbackCreateInfoEXT& create_info,
871 VkDebugReportCallbackEXT& callback) {
872 return inst.functions->vkCreateDebugReportCallbackEXT(inst.inst, &create_info, nullptr, &callback);
873 }
874
875 extern "C" {
876 void __ubsan_on_report() { FAIL() << "Encountered an undefined behavior sanitizer error"; }
877 void __asan_on_error() { FAIL() << "Encountered an address sanitizer error"; }
878 void __tsan_on_report() { FAIL() << "Encountered a thread sanitizer error"; }
879 } // extern "C"