Coverage Report

Created: 2026-05-30 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vulkan-loader/loader/loader_json.c
Line
Count
Source
1
/*
2
  Copyright (c) 2015-2021 The Khronos Group Inc.
3
  Copyright (c) 2015-2021 Valve Corporation
4
  Copyright (c) 2015-2021 LunarG, Inc.
5
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
7
  of this software and associated documentation files (the "Software"), to deal
8
  in the Software without restriction, including without limitation the rights
9
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
  copies of the Software, and to permit persons to whom the Software is
11
  furnished to do so, subject to the following conditions:
12
13
  The above copyright notice and this permission notice shall be included in
14
  all copies or substantial portions of the Software.
15
16
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
  THE SOFTWARE.
23
*/
24
25
#include "loader_json.h"
26
27
#include <assert.h>
28
#include <float.h>
29
#include <limits.h>
30
#include <math.h>
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <string.h>
34
35
#include "cJSON.h"
36
37
#include "allocation.h"
38
#include "loader.h"
39
#include "log.h"
40
41
extern void create_callback_file(const char *filename);
42
43
#if COMMON_UNIX_PLATFORMS
44
#include <fcntl.h>
45
#include <sys/stat.h>
46
#endif
47
48
#ifdef _WIN32
49
static VkResult loader_read_entire_file(const struct loader_instance *inst, const char *filename, size_t *out_len,
50
                                        char **out_buff) {
51
    HANDLE file_handle = INVALID_HANDLE_VALUE;
52
    DWORD len = 0, read_len = 0;
53
    VkResult res = VK_SUCCESS;
54
    BOOL read_ok = false;
55
56
    int filename_utf16_size = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
57
    if (filename_utf16_size > 0) {
58
        wchar_t *filename_utf16 = (wchar_t *)loader_stack_alloc(filename_utf16_size * sizeof(wchar_t));
59
        if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filename_utf16, filename_utf16_size) == filename_utf16_size) {
60
            file_handle =
61
                CreateFileW(filename_utf16, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
62
        }
63
    }
64
    if (INVALID_HANDLE_VALUE == file_handle) {
65
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to open JSON file %s", filename);
66
        res = VK_ERROR_INITIALIZATION_FAILED;
67
        goto out;
68
    }
69
    len = GetFileSize(file_handle, NULL);
70
    if (INVALID_FILE_SIZE == len) {
71
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to read file size of JSON file %s", filename);
72
        res = VK_ERROR_INITIALIZATION_FAILED;
73
        goto out;
74
    }
75
    *out_buff = (char *)loader_instance_heap_calloc(inst, len + 1, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
76
    if (NULL == *out_buff) {
77
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to allocate memory to read JSON file %s", filename);
78
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
79
        goto out;
80
    }
81
    read_ok = ReadFile(file_handle, *out_buff, len, &read_len, NULL);
82
    if (len != read_len || false == read_ok) {
83
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to read entire JSON file %s", filename);
84
        res = VK_ERROR_INITIALIZATION_FAILED;
85
        goto out;
86
    }
87
    *out_len = len + 1;
88
    (*out_buff)[len] = '\0';
89
90
out:
91
    if (INVALID_HANDLE_VALUE != file_handle) {
92
        CloseHandle(file_handle);
93
    }
94
    return res;
95
}
96
#elif COMMON_UNIX_PLATFORMS
97
static VkResult loader_read_entire_file(const struct loader_instance *inst, const char *filename, size_t *out_len,
98
13.9k
                                        char **out_buff) {
99
13.9k
    FILE *file = NULL;
100
13.9k
    struct stat stats = {0};
101
13.9k
    VkResult res = VK_SUCCESS;
102
103
    // fprintf(stderr, "loader_get_json: Reading JSON file %s\n", filename);
104
13.9k
    file = fopen(filename, "rb");
105
13.9k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
106
13.9k
    if (file == NULL) {
107
4.00k
        create_callback_file(filename);
108
4.00k
        file = fopen(filename, "rb");
109
4.00k
    }
110
13.9k
#endif
111
13.9k
    if (NULL == file) {
112
3.99k
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to open JSON file %s", filename);
113
3.99k
        res = VK_ERROR_INITIALIZATION_FAILED;
114
3.99k
        goto out;
115
3.99k
    }
116
10.0k
    if (-1 == fstat(fileno(file), &stats)) {
117
0
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to read file size of JSON file %s", filename);
118
0
        res = VK_ERROR_INITIALIZATION_FAILED;
119
0
        goto out;
120
0
    }
121
10.0k
    *out_buff = (char *)loader_instance_heap_calloc(inst, stats.st_size + 1, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
122
10.0k
    if (NULL == *out_buff) {
123
0
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to allocate memory to read JSON file %s", filename);
124
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
125
0
        goto out;
126
0
    }
127
10.0k
    if (stats.st_size != (long int)fread(*out_buff, sizeof(char), stats.st_size, file)) {
128
1.20k
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to read entire JSON file %s", filename);
129
1.20k
        res = VK_ERROR_INITIALIZATION_FAILED;
130
1.20k
        goto out;
131
1.20k
    }
132
8.79k
    *out_len = stats.st_size + 1;
133
8.79k
    (*out_buff)[stats.st_size] = '\0';
134
135
13.9k
out:
136
13.9k
    if (NULL != file) {
137
10.0k
        fclose(file);
138
10.0k
    }
139
13.9k
    return res;
140
8.79k
}
141
#else
142
#warning fopen not available on this platform
143
VkResult loader_read_entire_file(const struct loader_instance *inst, const char *filename, size_t *out_len, char **out_buff) {
144
    return VK_ERROR_INITIALIZATION_FAILED;
145
}
146
#endif
147
148
13.9k
TEST_FUNCTION_EXPORT VkResult loader_get_json(const struct loader_instance *inst, const char *filename, cJSON **json) {
149
13.9k
    char *json_buf = NULL;
150
13.9k
    VkResult res = VK_SUCCESS;
151
152
13.9k
    assert(json != NULL);
153
154
13.9k
    size_t json_len = 0;
155
13.9k
    *json = NULL;
156
13.9k
    res = loader_read_entire_file(inst, filename, &json_len, &json_buf);
157
13.9k
    if (VK_SUCCESS != res) {
158
5.20k
        goto out;
159
5.20k
    }
160
13.9k
    bool out_of_memory = false;
161
    // Parse text from file
162
8.79k
    *json = loader_cJSON_ParseWithLength(inst ? &inst->alloc_callbacks : NULL, json_buf, json_len, &out_of_memory);
163
8.79k
    if (out_of_memory) {
164
0
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Out of Memory error occurred while parsing JSON file %s.",
165
0
                   filename);
166
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
167
0
        goto out;
168
8.79k
    } else if (*json == NULL) {
169
3.95k
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Invalid JSON file %s.", filename);
170
3.95k
        goto out;
171
3.95k
    }
172
173
13.9k
out:
174
13.9k
    loader_instance_heap_free(inst, json_buf);
175
13.9k
    if (res != VK_SUCCESS && *json != NULL) {
176
0
        loader_cJSON_Delete(*json);
177
0
        *json = NULL;
178
0
    }
179
180
13.9k
    return res;
181
8.79k
}
182
183
39.1k
VkResult loader_parse_json_string_to_existing_str(cJSON *object, const char *key, size_t out_str_len, char *out_string) {
184
39.1k
    if (NULL == key) {
185
0
        return VK_ERROR_INITIALIZATION_FAILED;
186
0
    }
187
39.1k
    cJSON *item = loader_cJSON_GetObjectItem(object, key);
188
39.1k
    if (NULL == item) {
189
6.34k
        return VK_ERROR_INITIALIZATION_FAILED;
190
6.34k
    }
191
192
32.8k
    if (item->type != cJSON_String || item->valuestring == NULL) {
193
6.09k
        return VK_ERROR_INITIALIZATION_FAILED;
194
6.09k
    }
195
32.8k
    bool out_of_memory = false;
196
26.7k
    bool success = loader_cJSON_PrintPreallocated(item, out_string, (int)out_str_len, cJSON_False);
197
26.7k
    if (out_of_memory) {
198
0
        return VK_ERROR_OUT_OF_HOST_MEMORY;
199
0
    }
200
26.7k
    if (!success) {
201
252
        return VK_ERROR_INITIALIZATION_FAILED;
202
252
    }
203
26.5k
    return VK_SUCCESS;
204
26.7k
}
205
206
1.08M
VkResult loader_parse_json_string(cJSON *object, const char *key, char **out_string) {
207
1.08M
    if (NULL == key) {
208
0
        return VK_ERROR_INITIALIZATION_FAILED;
209
0
    }
210
211
1.08M
    cJSON *item = loader_cJSON_GetObjectItem(object, key);
212
1.08M
    if (NULL == item || NULL == item->valuestring) {
213
1.36k
        return VK_ERROR_INITIALIZATION_FAILED;
214
1.36k
    }
215
216
1.08M
    bool out_of_memory = false;
217
1.08M
    char *str = loader_cJSON_Print(item, &out_of_memory);
218
1.08M
    if (out_of_memory || NULL == str) {
219
0
        return VK_ERROR_OUT_OF_HOST_MEMORY;
220
0
    }
221
1.08M
    if (NULL != out_string) {
222
1.08M
        *out_string = str;
223
1.08M
    }
224
1.08M
    return VK_SUCCESS;
225
1.08M
}
226
VkResult loader_parse_json_array_of_strings(const struct loader_instance *inst, cJSON *object, const char *key,
227
18.3k
                                            struct loader_string_list *string_list) {
228
18.3k
    if (NULL == key) {
229
0
        return VK_ERROR_INITIALIZATION_FAILED;
230
0
    }
231
18.3k
    cJSON *item = loader_cJSON_GetObjectItem(object, key);
232
18.3k
    if (NULL == item) {
233
11.9k
        return VK_ERROR_INITIALIZATION_FAILED;
234
11.9k
    }
235
236
6.40k
    uint32_t count = loader_cJSON_GetArraySize(item);
237
6.40k
    if (count == 0) {
238
1.09k
        return VK_SUCCESS;
239
1.09k
    }
240
241
5.30k
    VkResult res = create_string_list(inst, count, string_list);
242
5.30k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
243
0
        goto out;
244
0
    }
245
5.30k
    cJSON *element = NULL;
246
834k
    cJSON_ArrayForEach(element, item) {
247
834k
        if (element->type != cJSON_String) {
248
3.23k
            return VK_ERROR_INITIALIZATION_FAILED;
249
3.23k
        }
250
834k
        bool out_of_memory = false;
251
830k
        char *out_data = loader_cJSON_Print(element, &out_of_memory);
252
830k
        if (out_of_memory) {
253
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
254
0
            goto out;
255
0
        }
256
830k
        res = append_str_to_string_list(inst, string_list, out_data);
257
830k
        if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
258
0
            goto out;
259
0
        }
260
830k
    }
261
2.07k
out:
262
2.07k
    if (res == VK_ERROR_OUT_OF_HOST_MEMORY && NULL != string_list->list) {
263
0
        free_string_list(inst, string_list);
264
0
    }
265
266
2.07k
    return res;
267
5.30k
}