Coverage Report

Created: 2024-09-08 07:41

/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
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 creation.
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
  // Create icd configuration file
73
  result = create_config_file(".local/share/vulkan/icd.d", "icd_test.json", data, size);
74
  if (result) {
75
    return 0;
76
  }
77
78
  setenv("VK_LOADER_LAYERS_ENABLE", "all", 1);
79
80
81
  VkInstance inst = {0};
82
  char *instance_layers[] = {
83
    "VK_LAYER_KHRONOS_validation",
84
    "VK_LAYER_test_layer_1",
85
    "VK_LAYER_test_layer_2"
86
  };
87
  const VkApplicationInfo app = {
88
      .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
89
      .pNext = NULL,
90
      .pApplicationName = "TEST_APP",
91
      .applicationVersion = 0,
92
      .pEngineName = "TEST_ENGINE",
93
      .engineVersion = 0,
94
      .apiVersion = VK_API_VERSION_1_0,
95
  };
96
  VkInstanceCreateInfo inst_info = {
97
      .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
98
      .pNext = NULL,
99
      .pApplicationInfo = &app,
100
      .enabledLayerCount = 1,
101
      .ppEnabledLayerNames = (const char *const *)instance_layers,
102
      .enabledExtensionCount = 0,
103
      .ppEnabledExtensionNames = NULL,
104
  };
105
  VkResult err = vkCreateInstance(&inst_info, NULL, &inst);
106
  if (err != VK_SUCCESS) {
107
    goto out;
108
  }
109
110
  vkDestroyInstance(inst, NULL);
111
112
out:
113
  // Clean up config files
114
  remove_config_file(".local/share/vulkan/implicit_layer.d", "complex_layer.json");
115
  remove_config_file(".local/share/vulkan/loader_settings.d", "vk_loader_settings.json");
116
  remove_config_file(".local/share/vulkan/icd.d", "icd_test.json");
117
118
  return 0;
119
}