Coverage Report

Created: 2026-08-30 07:16

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
            // A path at or beyond MAX_PATH won't open unless we opt into long paths with the "\\?\" prefix, so retry with it.
63
            if (INVALID_HANDLE_VALUE == file_handle && filename_utf16_size >= MAX_PATH) {
64
                int prefixed_utf16_size = filename_utf16_size + 4;  // room for the leading "\\?\"
65
                wchar_t *prefixed_utf16 = (wchar_t *)loader_stack_alloc(prefixed_utf16_size * sizeof(wchar_t));
66
                prefixed_utf16[0] = L'\\';
67
                prefixed_utf16[1] = L'\\';
68
                prefixed_utf16[2] = L'?';
69
                prefixed_utf16[3] = L'\\';
70
                memcpy(prefixed_utf16 + 4, filename_utf16, filename_utf16_size * sizeof(wchar_t));
71
                file_handle =
72
                    CreateFileW(prefixed_utf16, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
73
            }
74
        }
75
    }
76
    if (INVALID_HANDLE_VALUE == file_handle) {
77
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to open JSON file %s", filename);
78
        res = VK_ERROR_INITIALIZATION_FAILED;
79
        goto out;
80
    }
81
    len = GetFileSize(file_handle, NULL);
82
    if (INVALID_FILE_SIZE == len) {
83
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to read file size of JSON file %s", filename);
84
        res = VK_ERROR_INITIALIZATION_FAILED;
85
        goto out;
86
    }
87
    *out_buff = (char *)loader_instance_heap_calloc(inst, len + 1, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
88
    if (NULL == *out_buff) {
89
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to allocate memory to read JSON file %s", filename);
90
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
91
        goto out;
92
    }
93
    read_ok = ReadFile(file_handle, *out_buff, len, &read_len, NULL);
94
    if (len != read_len || false == read_ok) {
95
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to read entire JSON file %s", filename);
96
        res = VK_ERROR_INITIALIZATION_FAILED;
97
        goto out;
98
    }
99
    *out_len = len + 1;
100
    (*out_buff)[len] = '\0';
101
102
out:
103
    if (INVALID_HANDLE_VALUE != file_handle) {
104
        CloseHandle(file_handle);
105
    }
106
    return res;
107
}
108
#elif COMMON_UNIX_PLATFORMS
109
static VkResult loader_read_entire_file(const struct loader_instance *inst, const char *filename, size_t *out_len,
110
21.0k
                                        char **out_buff) {
111
21.0k
    FILE *file = NULL;
112
21.0k
    struct stat stats = {0};
113
21.0k
    VkResult res = VK_SUCCESS;
114
115
    // fprintf(stderr, "loader_get_json: Reading JSON file %s\n", filename);
116
21.0k
    file = fopen(filename, "rb");
117
21.0k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
118
21.0k
    if (file == NULL) {
119
3.50k
        create_callback_file(filename);
120
3.50k
        file = fopen(filename, "rb");
121
3.50k
    }
122
21.0k
#endif
123
21.0k
    if (NULL == file) {
124
3.50k
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to open JSON file %s", filename);
125
3.50k
        res = VK_ERROR_INITIALIZATION_FAILED;
126
3.50k
        goto out;
127
3.50k
    }
128
17.5k
    if (-1 == fstat(fileno(file), &stats)) {
129
0
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to read file size of JSON file %s", filename);
130
0
        res = VK_ERROR_INITIALIZATION_FAILED;
131
0
        goto out;
132
0
    }
133
17.5k
    *out_buff = (char *)loader_instance_heap_calloc(inst, stats.st_size + 1, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
134
17.5k
    if (NULL == *out_buff) {
135
0
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to allocate memory to read JSON file %s", filename);
136
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
137
0
        goto out;
138
0
    }
139
17.5k
    if (stats.st_size != (long int)fread(*out_buff, sizeof(char), stats.st_size, file)) {
140
96
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Failed to read entire JSON file %s", filename);
141
96
        res = VK_ERROR_INITIALIZATION_FAILED;
142
96
        goto out;
143
96
    }
144
17.4k
    *out_len = stats.st_size + 1;
145
    // out_buff was allocated with stats.st_size + 1 bytes above, and the fread check already verified the file's
146
    // contents did not change size out from under us; this is the last valid byte.
147
    // NOLINTNEXTLINE(clang-analyzer-security.ArrayBound)
148
17.4k
    (*out_buff)[stats.st_size] = '\0';
149
150
21.0k
out:
151
21.0k
    if (NULL != file) {
152
        // NOLINTNEXTLINE(cert-err33-c) - file was only opened for reading, nothing to flush; res already reflects any read failure
153
17.5k
        fclose(file);
154
17.5k
    }
155
21.0k
    return res;
156
17.4k
}
157
#else
158
#warning fopen not available on this platform
159
VkResult loader_read_entire_file(const struct loader_instance *inst, const char *filename, size_t *out_len, char **out_buff) {
160
    return VK_ERROR_INITIALIZATION_FAILED;
161
}
162
#endif
163
164
21.0k
TEST_FUNCTION_EXPORT VkResult loader_get_json(const struct loader_instance *inst, const char *filename, cJSON **json) {
165
21.0k
    char *json_buf = NULL;
166
21.0k
    VkResult res = VK_SUCCESS;
167
168
21.0k
    assert(json != NULL);
169
170
21.0k
    size_t json_len = 0;
171
21.0k
    *json = NULL;
172
21.0k
    res = loader_read_entire_file(inst, filename, &json_len, &json_buf);
173
21.0k
    if (VK_SUCCESS != res) {
174
3.60k
        goto out;
175
3.60k
    }
176
21.0k
    bool out_of_memory = false;
177
    // Parse text from file
178
17.4k
    *json = loader_cJSON_ParseWithLength(inst ? &inst->alloc_callbacks : NULL, json_buf, json_len, &out_of_memory);
179
17.4k
    if (out_of_memory) {
180
0
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Out of Memory error occurred while parsing JSON file %s.",
181
0
                   filename);
182
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
183
0
        goto out;
184
17.4k
    } else if (*json == NULL) {
185
8.61k
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_get_json: Invalid JSON file %s.", filename);
186
8.61k
        goto out;
187
8.61k
    }
188
189
21.0k
out:
190
21.0k
    loader_instance_heap_free(inst, json_buf);
191
21.0k
    if (res != VK_SUCCESS && *json != NULL) {
192
0
        loader_cJSON_Delete(*json);
193
0
        *json = NULL;
194
0
    }
195
196
21.0k
    return res;
197
17.4k
}
198
199
137k
VkResult loader_parse_json_string_to_existing_str(cJSON *object, const char *key, size_t out_str_len, char *out_string) {
200
137k
    if (NULL == key) {
201
0
        return VK_ERROR_INITIALIZATION_FAILED;
202
0
    }
203
137k
    cJSON *item = loader_cJSON_GetObjectItem(object, key);
204
137k
    if (NULL == item) {
205
10.0k
        return VK_ERROR_INITIALIZATION_FAILED;
206
10.0k
    }
207
208
127k
    if (item->type != cJSON_String || item->valuestring == NULL) {
209
307
        return VK_ERROR_INITIALIZATION_FAILED;
210
307
    }
211
127k
    bool out_of_memory = false;
212
126k
    bool success = loader_cJSON_PrintPreallocated(item, out_string, (int)out_str_len, cJSON_False);
213
126k
    if (out_of_memory) {
214
0
        return VK_ERROR_OUT_OF_HOST_MEMORY;
215
0
    }
216
126k
    if (!success) {
217
152
        return VK_ERROR_INITIALIZATION_FAILED;
218
152
    }
219
126k
    return VK_SUCCESS;
220
126k
}
221
222
138k
VkResult loader_parse_json_string(cJSON *object, const char *key, char **out_string) {
223
138k
    if (NULL == key) {
224
0
        return VK_ERROR_INITIALIZATION_FAILED;
225
0
    }
226
227
138k
    cJSON *item = loader_cJSON_GetObjectItem(object, key);
228
138k
    if (NULL == item || NULL == item->valuestring) {
229
53.5k
        return VK_ERROR_INITIALIZATION_FAILED;
230
53.5k
    }
231
232
138k
    bool out_of_memory = false;
233
84.6k
    char *str = loader_cJSON_Print(item, &out_of_memory);
234
84.6k
    if (out_of_memory || NULL == str) {
235
0
        return VK_ERROR_OUT_OF_HOST_MEMORY;
236
0
    }
237
84.6k
    if (NULL != out_string) {
238
84.6k
        *out_string = str;
239
84.6k
    }
240
84.6k
    return VK_SUCCESS;
241
84.6k
}
242
VkResult loader_parse_json_array_of_strings(const struct loader_instance *inst, cJSON *object, const char *key,
243
96.5k
                                            struct loader_string_list *string_list) {
244
96.5k
    if (NULL == key) {
245
0
        return VK_ERROR_INITIALIZATION_FAILED;
246
0
    }
247
96.5k
    cJSON *item = loader_cJSON_GetObjectItem(object, key);
248
96.5k
    if (NULL == item) {
249
46.3k
        return VK_ERROR_INITIALIZATION_FAILED;
250
46.3k
    }
251
252
50.1k
    uint32_t count = loader_cJSON_GetArraySize(item);
253
50.1k
    if (count == 0) {
254
36.7k
        return VK_SUCCESS;
255
36.7k
    }
256
257
13.3k
    VkResult res = create_string_list(inst, count, string_list);
258
13.3k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
259
0
        goto out;
260
0
    }
261
13.3k
    cJSON *element = NULL;
262
288k
    cJSON_ArrayForEach(element, item) {
263
288k
        if (element->type != cJSON_String) {
264
1.65k
            return VK_ERROR_INITIALIZATION_FAILED;
265
1.65k
        }
266
288k
        bool out_of_memory = false;
267
287k
        char *out_data = loader_cJSON_Print(element, &out_of_memory);
268
287k
        if (out_of_memory) {
269
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
270
0
            goto out;
271
0
        }
272
287k
        res = append_str_to_string_list(inst, string_list, out_data);
273
287k
        if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
274
0
            goto out;
275
0
        }
276
287k
    }
277
11.7k
out:
278
11.7k
    if (res == VK_ERROR_OUT_OF_HOST_MEMORY && NULL != string_list->list) {
279
0
        free_string_list(inst, string_list);
280
0
    }
281
282
11.7k
    return res;
283
13.3k
}