Coverage Report

Created: 2024-09-08 07:41

/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
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 settings parser.
56
 */
57
481
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
58
481
  setenv("HOME", "/tmp", 1);
59
60
  // Create loader configuration file
61
481
  int result = create_config_file(".local/share/vulkan/loader_settings.d", "vk_loader_settings.json", data, size);
62
481
  if (result) {
63
0
    return 0;
64
0
  }
65
66
481
  update_global_loader_settings();
67
481
  update_global_loader_settings();
68
481
  get_current_settings_and_lock(NULL);
69
481
  release_current_settings_lock(NULL);
70
481
  struct loader_layer_list settings_layers = {0};
71
72
481
  bool should_search_for_other_layers = true;
73
481
  get_settings_layers(NULL, &settings_layers, &should_search_for_other_layers);
74
481
  should_skip_logging_global_messages(0);
75
481
  update_global_loader_settings();
76
481
  teardown_global_loader_settings();
77
78
  // Clean up config file
79
481
  remove_config_file(".local/share/vulkan/loader_settings.d", "vk_loader_settings.json");
80
81
481
  return 0;
82
481
}