/src/instance_enumerate_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 | | |
20 | | /* |
21 | | * Create config files for given path and data. |
22 | | */ |
23 | 481 | int create_config_file(const char* config_path, const char* config_filename, const uint8_t* data, size_t size) { |
24 | 481 | char filename[512]; |
25 | 481 | char path[256]; |
26 | 481 | char command[256]; |
27 | | |
28 | 481 | sprintf(path, "%s/%s", getenv("HOME"), config_path); |
29 | 481 | sprintf(command, "mkdir -p %s", path); |
30 | | |
31 | 481 | system(command); |
32 | | |
33 | 481 | sprintf(filename, "%s/%s", path, config_filename); |
34 | | |
35 | 481 | FILE *fp = fopen(filename, "wb"); |
36 | 481 | if (!fp) { |
37 | 0 | return 1; |
38 | 0 | } |
39 | 481 | fwrite(data, size, 1, fp); |
40 | 481 | fclose(fp); |
41 | | |
42 | 481 | return 0; |
43 | 481 | } |
44 | | |
45 | | /* |
46 | | * Remove config file |
47 | | */ |
48 | 481 | void remove_config_file(const char* config_path, const char* config_filename) { |
49 | 481 | char filename[512]; |
50 | 481 | sprintf(filename, "%s/%s/%s", getenv("HOME"), config_path, config_filename); |
51 | 481 | unlink(filename); |
52 | 481 | } |
53 | | |
54 | | /* |
55 | | * Targets the instance extension enumeration. |
56 | | */ |
57 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
58 | | setenv("HOME", "/tmp", 1); |
59 | | |
60 | | // Create implicit layer configuration file |
61 | | int result = create_config_file(".local/share/vulkan/implicit_layer.d", "complex_layer.json", data, size); |
62 | | if (result) { |
63 | | return 0; |
64 | | } |
65 | | |
66 | | // Create loader configuration file |
67 | | result = create_config_file(".local/share/vulkan/loader_settings.d", "vk_loader_settings.json", data, size); |
68 | | if (result) { |
69 | | return 0; |
70 | | } |
71 | | |
72 | | setenv("VK_LOADER_LAYERS_ENABLE", "all", 1); |
73 | | |
74 | | uint32_t pPropertyCount; |
75 | | VkExtensionProperties pProperties = {0}; |
76 | | |
77 | | vkEnumerateInstanceExtensionProperties("test_auto", &pPropertyCount, &pProperties); |
78 | | |
79 | | // Clean up config files |
80 | | remove_config_file(".local/share/vulkan/implicit_layer.d", "complex_layer.json"); |
81 | | remove_config_file(".local/share/vulkan/loader_settings.d", "vk_loader_settings.json"); |
82 | | |
83 | | return 0; |
84 | | } |