Coverage Report

Created: 2025-07-11 06:48

/src/settings_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
3.06k
int create_config_file(const char* config_path, const char* config_filename, const uint8_t* data, size_t size) {
25
3.06k
  char filename[512];
26
3.06k
  char path[256];
27
3.06k
  char command[256];
28
29
3.06k
  sprintf(path, "%s/%s", getenv("HOME"), config_path);
30
3.06k
  sprintf(command, "mkdir -p %s", path);
31
32
3.06k
  system(command);
33
34
3.06k
  sprintf(filename, "%s/%s", path, config_filename);
35
36
3.06k
  FILE *fp = fopen(filename, "wb");
37
3.06k
  if (!fp) {
38
0
    return 1;
39
0
  }
40
3.06k
  fwrite(data, size, 1, fp);
41
3.06k
  fclose(fp);
42
43
3.06k
  return 0;
44
3.06k
}
45
46
/*
47
 * Remove config file
48
 */
49
3.06k
void remove_config_file(const char* config_path, const char* config_filename) {
50
3.06k
  char filename[512];
51
3.06k
  sprintf(filename, "%s/%s/%s", getenv("HOME"), config_path, config_filename);
52
3.06k
  unlink(filename);
53
3.06k
}
54
55
/*
56
 * Targets the settings parser.
57
 */
58
3.06k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
59
3.06k
  setenv("HOME", "/tmp", 1);
60
61
  // Create loader configuration file
62
3.06k
  int result = create_config_file(".local/share/vulkan/loader_settings.d", "vk_loader_settings.json", data, size);
63
3.06k
  if (result) {
64
0
    return 0;
65
0
  }
66
67
3.06k
  update_global_loader_settings();
68
3.06k
  update_global_loader_settings();
69
3.06k
  get_current_settings_and_lock(NULL);
70
3.06k
  release_current_settings_lock(NULL);
71
3.06k
  struct loader_layer_list settings_layers = {0};
72
73
3.06k
  bool should_search_for_other_layers = true;
74
3.06k
  get_settings_layers(NULL, &settings_layers, &should_search_for_other_layers);
75
  // Free allocated memory
76
3.06k
  loader_delete_layer_list_and_properties(NULL, &settings_layers);
77
3.06k
  should_skip_logging_global_messages(0);
78
3.06k
  update_global_loader_settings();
79
3.06k
  teardown_global_loader_settings();
80
81
  // Clean up config file
82
3.06k
  remove_config_file(".local/share/vulkan/loader_settings.d", "vk_loader_settings.json");
83
84
3.06k
  return 0;
85
3.06k
}