/src/instance_create_fuzzer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2023 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | |
13 | | #include <stdint.h> |
14 | | #include <stdio.h> |
15 | | #include <stdlib.h> |
16 | | |
17 | | #include "cJSON.h" |
18 | | #include "loader.h" |
19 | | #include "fuzz_header.h" |
20 | | |
21 | | /* |
22 | | * Create config files for given path and data. |
23 | | */ |
24 | 53.9k | int create_config_file(const char* config_path, const char* config_filename, const uint8_t* data, size_t size) { |
25 | 53.9k | char filename[512]; |
26 | 53.9k | char path[256]; |
27 | 53.9k | char command[256]; |
28 | | |
29 | 53.9k | sprintf(path, "%s/%s", getenv("HOME"), config_path); |
30 | 53.9k | sprintf(command, "mkdir -p %s", path); |
31 | | |
32 | 53.9k | system(command); |
33 | | |
34 | 53.9k | sprintf(filename, "%s/%s", path, config_filename); |
35 | | |
36 | 53.9k | FILE *fp = fopen(filename, "wb"); |
37 | 53.9k | if (!fp) { |
38 | 0 | return 1; |
39 | 0 | } |
40 | 53.9k | fwrite(data, size, 1, fp); |
41 | 53.9k | fclose(fp); |
42 | | |
43 | 53.9k | return 0; |
44 | 53.9k | } |
45 | | |
46 | | /* |
47 | | * Remove config file |
48 | | */ |
49 | 53.9k | void remove_config_file(const char* config_path, const char* config_filename) { |
50 | 53.9k | char filename[512]; |
51 | 53.9k | sprintf(filename, "%s/%s/%s", getenv("HOME"), config_path, config_filename); |
52 | 53.9k | unlink(filename); |
53 | 53.9k | } |
54 | | |
55 | | /* |
56 | | * Targets the instance creation. |
57 | | */ |
58 | 7.47k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
59 | 7.47k | setenv("HOME", "/tmp", 1); |
60 | | |
61 | | // Create implicit layer configuration file |
62 | 7.47k | int result = create_config_file(".local/share/vulkan/implicit_layer.d", "complex_layer.json", data, size); |
63 | 7.47k | if (result) { |
64 | 0 | return 0; |
65 | 0 | } |
66 | | |
67 | | // Create loader configuration file |
68 | 7.47k | result = create_config_file(".local/share/vulkan/loader_settings.d", "vk_loader_settings.json", data, size); |
69 | 7.47k | if (result) { |
70 | 0 | return 0; |
71 | 0 | } |
72 | | |
73 | | // Create icd configuration file |
74 | 7.47k | result = create_config_file(".local/share/vulkan/icd.d", "icd_test.json", data, size); |
75 | 7.47k | if (result) { |
76 | 0 | return 0; |
77 | 0 | } |
78 | | |
79 | 7.47k | setenv("VK_LOADER_LAYERS_ENABLE", "all", 1); |
80 | | |
81 | | |
82 | 7.47k | VkInstance inst = {0}; |
83 | 7.47k | char *instance_layers[] = { |
84 | 7.47k | "VK_LAYER_KHRONOS_validation", |
85 | 7.47k | "VK_LAYER_test_layer_1", |
86 | 7.47k | "VK_LAYER_test_layer_2" |
87 | 7.47k | }; |
88 | 7.47k | const VkApplicationInfo app = { |
89 | 7.47k | .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, |
90 | 7.47k | .pNext = NULL, |
91 | 7.47k | .pApplicationName = "TEST_APP", |
92 | 7.47k | .applicationVersion = 0, |
93 | 7.47k | .pEngineName = "TEST_ENGINE", |
94 | 7.47k | .engineVersion = 0, |
95 | 7.47k | .apiVersion = VK_API_VERSION_1_0, |
96 | 7.47k | }; |
97 | 7.47k | VkInstanceCreateInfo inst_info = { |
98 | 7.47k | .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
99 | 7.47k | .pNext = NULL, |
100 | 7.47k | .pApplicationInfo = &app, |
101 | 7.47k | .enabledLayerCount = 1, |
102 | 7.47k | .ppEnabledLayerNames = (const char *const *)instance_layers, |
103 | 7.47k | .enabledExtensionCount = 0, |
104 | 7.47k | .ppEnabledExtensionNames = NULL, |
105 | 7.47k | }; |
106 | 7.47k | VkResult err = vkCreateInstance(&inst_info, NULL, &inst); |
107 | 7.47k | if (err != VK_SUCCESS) { |
108 | 7.47k | goto out; |
109 | 7.47k | } |
110 | | |
111 | 0 | vkDestroyInstance(inst, NULL); |
112 | |
|
113 | 7.47k | out: |
114 | | // Clean up config files |
115 | 7.47k | remove_config_file(".local/share/vulkan/implicit_layer.d", "complex_layer.json"); |
116 | 7.47k | remove_config_file(".local/share/vulkan/loader_settings.d", "vk_loader_settings.json"); |
117 | 7.47k | remove_config_file(".local/share/vulkan/icd.d", "icd_test.json"); |
118 | | |
119 | 7.47k | return 0; |
120 | 0 | } |