Debian Codegraph @

tests/framework/README.md

raw
1 # Loader Testing Framework
2
3 The loader testing framework is a mocking environment which fakes many of the global systems that vulkan requires to run. This allows the writing of tests against a known driver, layer, and system configuration.
4
5 The framework consists of:
6 * Test ICD
7 * Test Layer
8 * Shim
9 * Manifest Writer
10 * Utility
11 * Test Environment
12 * Tests
13
14
15 ## Running
16
17 By default the Vulkan-Loader repo doesn't enable testing.
18
19 To turn on building of the tests, set `BUILD_TESTS=ON` in the CMake configuration.
20
21 Use the CMake configuration `UPDATE_DEPS=ON` to automatically get all required test dependencies.
22 Or Ensure that `googletest` is in the `external` directory.
23 And on Windows only, ensure that the `Detours` library is in the `external` directory.
24
25 Linux & macOS only: The CMake Configuration `LOADER_ENABLE_ADDRESS_SANITIZER` can be used to
26 enable Address Sanitizer.
27
28 Run the test executables as normal
29
30 The executables available are:
31 * `test_regression`
32
33 Alternatively, in the build directory run `ctest` to start the test framework.
34
35 Use the `ctest` command line parameter `--output-on-failure` to printout logs in failing tests
36
37 Note: The test framework was not designed to allow multiple tests to be run in parallel due to the extensive use of files and folders on the system.
38
39
40 ## Components
41
42 ### Test ICD and Layer
43 The Test ICD and Test Layer have much of their configuration available at runtime to allow maximal test setup flexibility.
44 However exported functions in the icd or layer library binaries are an integral part of the configuration but are baked into the binary, thus not runtime configurable.
45 To account for that there are multiple binaries of the Test ICD and Test Layer, each being a distinct combination of exported functions.
46 The `test_icd.cpp` and `test_layer.cpp` files use macro defines to allow the build system to specify which functions should be exported.
47 ```c
48 #if defined(TEST_ICD_EXPORT_NEW_FUNCTION)
49 FRAMEWORK_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkNewFunction() { ... }
50 #endif
51 ```
52 The `CMakeLists.txt` is responsible for creating the various binaries for the Test ICD and Test Layer with the corresponding macros defined to alter which functions are exported.
53 Since the test framework needs to know where the binaries are located in the build tree, the `framework_config.h` file contains the absolute paths to all of the libraries that CMake has defined.
54 This allows C++ tests to include the header and know exactly what `.dll`/`.so` file to use.
55 Since the paths aren't known until build time, CMake processes the `framework_config.h.in` file which contains the names of the libraries that CMake recognizes and replaces the names with the path on the system to the library.
56
57 To add a new configuration for a ICD or layer, the changes needed are as follows:
58 * Add the relevant functions & macro guards in `test_icd.cpp` or `test_layer.cpp`
59 * In the `framework/icd/CMakeLists.txt` or `framework/layer/CMakeLists.txt`, add a new SHARED library using the icd/layer source files.
60 * We are going to use `new_function` as the name of this new configuration for clarity in this documentation.
61 ```cmake
62 add_library(test_icd_export_new_function SHARED ${TEST_ICD_SOURCES})
63 ```
64 * Use `target_compile_definitions()` cmake command on the new library to add the specified exports.
65 ```cmake
66 target_compile_definitions(test_icd_export_new_function PRIVATE TEST_ICD_EXPORT_NEW_FUNCTION=1)
67 ```
68 * Create a new `.def` file with the same name as the library and place it in the `icd\export_definitions` or `layer\export_definitions` folder.
69 * This file is needed in 32 bit windows to prevent name mangling in `.dll`.
70 * Add all of the exported functions in this file.
71 * Include all exported functions, not just the newly added functions.
72 ```
73 LIBRARY test_icd_export_new_function
74 EXPORTS
75 <exported_function_0>
76 <exported_function_1>
77 ...
78 ```
79 * Go back to the `CMakeLists.txt` and add this def file to the library by using `target_sources`, that way the compiler knows which .def file to use for each library
80 ```cmake
81 if (WIN32)
82 target_sources(test_icd_export_new_function PRIVATE export_definitions/test_icd_new_function.def)
83 endif()
84 ```
85 * To make the library accessible to tests, add the name of the library to `framework/framework_config.h.in`
86 ```c
87 #define TEST_ICD_PATH_NEW_FUNCTION "$<TARGET_FILE:test_icd_export_new_function>"
88 ```
89
90 ### Shim
91 Because the loader makes many calls to various OS functionality, the framework intercepts certain calls and makes a few of its own calls to OS functionality to allow proper isolation of the loader from the system it is running on.
92 The shim is a SHARED library on windows and apple and is a STATIC library on linux. This is due to the nature of the dynamic linker and how overriding functions operates on different operating systems.
93 #### Linux
94
95 On linux the dynamic linker will use functions defined in the program binary in loaded `.so`'s if the name matches, allowing easy interception of system calls.
96 ##### Overridden functions
97 * opendir
98 * access
99 * fopen
100
101 #### MacOS
102 Redirects the following functions: opendir, access fopen.
103 The dynamic linker on MacOS requires a bit of tweaking to make it use functions defined in the program binary to override system functions in `.so`'s.
104 ##### Overridden functions
105 * opendir
106 * access
107 * fopen
108
109 #### Windows
110 Windows requires a significantly larger number of functions to be intercepted to isolate it sufficiently enough for testing.
111 To facilitate that an external library `Detours` is used.
112 It is an open source Microsoft library and supports all of the necessary functionality needed for loader testing.
113 Note that the loader calls more system functions than are directly overridden with Detours.
114 This is due to some functions being used to query other functions, which the shim library intercepts and returns its own versions of system functions instead, such as `CreateDXGIFactory1`.
115
116 ##### Overridden functions
117 * GetSidSubAuthority
118 * EnumAdapters2
119 * QueryAdapterInfo
120 * CM_Get_Device_ID_List_SizeW
121 * CM_Get_Device_ID_ListW
122 * CM_Locate_DevNodeW
123 * CM_Get_DevNode_Status
124 * CM_Get_Device_IDW
125 * CM_Get_Child
126 * CM_Get_DevNode_Registry_PropertyW
127 * CM_Get_Sibling
128 * GetDesc1
129 * CreateDXGIFactory1
130
131 ### Utility
132
133 There are many utilities that the test framework and tests have access to. These include:
134 * Including common C and C++ headers
135 * `FRAMEWORK_EXPORT` - macro used for exporting shared library funtions
136 * Environment Variable Wrapper: `EnvVarWrapper` for creating, setting, getting, and removing environment variables in a RAII manner
137 * Windows API error handling helpers
138 * filesystem abstractions:
139 * `Folder`
140 * Creates a new folder with the given name at construction time.
141 * Allows writing manifests and files (eg, icd or layer binaries)
142 * Automatically destroys the folder and all contained files at destruction
143 * LibraryWrapper - load and unload `.dll`/`.so`'s automatically
144 * DispatchableHandle - helper class for managing the creation and freeing of dispatchable handles
145 * VulkanFunctions - Loads the vulkan-loader and queries all used functions from it
146 * If a test needs to use vulkan functions, they must be loaded here
147 * Vulkan Create Info Helpers - provide a nice interface for setting up a vulkan creat info struct.
148 * InstanceCreateInfo
149 * DeviceCreateInfo
150 * DeviceQueueCreateInfo
151 * Comparison operators for various vulkan structs
152
153 ### Test Environment
154
155 The `test_environment.h/.cpp` contains classes which organize all the disparate parts of the framework into an easy to use entity that allows setting up and configuring the environment tests run in.
156
157 The core components are:
158 * InstWrapper - helper to construct and then destroy a vulkan instance during tests
159 * DeviceWrapper - helper to construct and then destroy a vulkan device during tests
160 * PlatformShimWrapper - opens and configures the platform specific shim library
161 * Sets up the overrides
162 * Resets state (clearing out previous test state)
163 * Sets the `VK_LOADER_DEBUG` env var to `all`.
164 * TestICD|LayerHandle - corresponds to a single ICD or Layer on the system
165 * Loads the Test ICD/Layer library
166 * Allows easily 'resetting' the Test ICD/Layer
167 * TestICD|LayerDetails - holder of data used for constructing ICD and Layers
168 * Contains the name and api version of the icd or layer binary
169 * LayerDetails also contains the manifest,
170 * FrameworkEnvironment
171 * Owns the platform shim
172 * Creates folders for the various manifest search locations, including
173 * icd manifest
174 * explicit layer manifests
175 * implicit layer manifests
176 * null - necessary empty folder to make the loader find nothing when we want it to
177 * Sets these folders up as the redirection locations with the platform shim
178 * Allows adding ICD's and Layers
179 * Writes the json manifest file to the correct folder
180
181 The `FrameworkEnvironment` class is used to easily create 'environments'.
182
183 The `add_XXX()` member functions of `FrameworkEnvironment` make it easy to add drivers and layers to the environment a test runs in.
184
185 The `get_test_icd()` and `get_test_layer()` functions allow querying references to the underlying
186 drivers and layers that are in the environment, allowing quick modification of their behavior.
187
188 The `reset_test_icd()` and `reset_test_layer()` are similar to the above functions but additionally
189 reset the layer or driver to its initial state.
190 Use this if you need to reset a driver during a test.
191 These functions are called on the drivers and layers when the framework is being create in each test.