1 /*
2  * Copyright (c) 2021 The Khronos Group Inc.
3  * Copyright (c) 2021 Valve Corporation
4  * Copyright (c) 2021 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 <charles@lunarg.com>
26  */
27 
28 #include "test_environment.h"
29 
30 class UnknownExtension : public ::testing::Test {
31    protected:
SetUp()32     virtual void SetUp() {
33         env = std::unique_ptr<FrameworkEnvironment>(new FrameworkEnvironment());
34         env->add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA));
35     }
36 
TearDown()37     virtual void TearDown() { env.reset(); }
38     std::unique_ptr<FrameworkEnvironment> env;
39 };
40 
41 /*
42  Creates a TestICD with a function unknown to the loader called vkNotRealFuncTEST. The TestICD, when vk_icdGetPhysicalDeviceProcAddr
43  is called, will return the custom_physical_device_function if the function name matches vkNotRealFuncTEST. The test then calls the
44  function to verify that the unknown physical device function dispatching is working correctly.
45 */
46 
custom_physical_device_function(VkPhysicalDevice device,int foo,int bar)47 VkResult custom_physical_device_function(VkPhysicalDevice device, int foo, int bar) { return VK_SUCCESS; }
48 using PFN_custom_physical_device_function = decltype(&custom_physical_device_function);
49 
TEST_F(UnknownExtension,ICDKnownExtension)50 TEST_F(UnknownExtension, ICDKnownExtension) {
51     auto& driver = env->get_test_icd();
52     const char* fake_function_name = "vkNotRealFuncTEST";
53 
54     driver.physical_devices.emplace_back("physical_device_0");
55     driver.custom_physical_device_functions.push_back(
56         VulkanFunction{fake_function_name, reinterpret_cast<void*>(custom_physical_device_function)});
57 
58     InstWrapper inst{env->vulkan_functions};
59     inst.CheckCreate();
60 
61     VkPhysicalDevice phys_dev = inst.GetPhysDev();
62 
63     PFN_custom_physical_device_function returned_func = reinterpret_cast<PFN_custom_physical_device_function>(
64         env->vulkan_functions.vkGetInstanceProcAddr(inst.inst, fake_function_name));
65     ASSERT_NE(returned_func, nullptr);
66     ASSERT_EQ(returned_func(phys_dev, 42, 58008), VK_SUCCESS);
67 }