Coverage Report

Created: 2026-09-02 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vulkan-loader/loader/settings.c
Line
Count
Source
1
/*
2
 *
3
 * Copyright (c) 2023 The Khronos Group Inc.
4
 * Copyright (c) 2023 Valve Corporation
5
 * Copyright (c) 2023 LunarG, Inc.
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 *
19
 *
20
 * Author: Charles Giessen <charles@lunarg.com>
21
 *
22
 */
23
24
#include "settings.h"
25
26
#include "allocation.h"
27
#include "cJSON.h"
28
#include "loader.h"
29
#include "loader_environment.h"
30
#include "loader_json.h"
31
#if defined(WIN32)
32
#include "loader_windows.h"
33
#endif
34
#include "log.h"
35
#include "stack_allocation.h"
36
#include "vk_loader_platform.h"
37
38
loader_platform_thread_mutex global_loader_settings_lock;
39
loader_settings global_loader_settings;
40
41
32.5k
void free_layer_configuration(const struct loader_instance* inst, loader_settings_layer_configuration* layer_configuration) {
42
32.5k
    loader_instance_heap_free(inst, layer_configuration->name);
43
32.5k
    loader_instance_heap_free(inst, layer_configuration->path);
44
32.5k
    memset(layer_configuration, 0, sizeof(loader_settings_layer_configuration));
45
32.5k
}
46
47
0
void free_driver_configuration(const struct loader_instance* inst, loader_settings_driver_configuration* driver_configuration) {
48
0
    loader_instance_heap_free(inst, driver_configuration->path);
49
0
    memset(driver_configuration, 0, sizeof(loader_settings_driver_configuration));
50
0
}
51
52
0
void free_device_configuration(const struct loader_instance* inst, loader_settings_device_configuration* device_configuration) {
53
0
    (void)inst;
54
0
    memset(device_configuration, 0, sizeof(loader_settings_device_configuration));
55
0
}
56
57
2.85k
void free_loader_settings(const struct loader_instance* inst, loader_settings* settings) {
58
2.85k
    if (NULL != settings->layer_configurations) {
59
29.6k
        for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
60
29.0k
            free_layer_configuration(inst, &settings->layer_configurations[i]);
61
29.0k
        }
62
686
        loader_instance_heap_free(inst, settings->layer_configurations);
63
686
    }
64
2.85k
    if (NULL != settings->additional_drivers) {
65
0
        for (uint32_t i = 0; i < settings->additional_driver_count; i++) {
66
0
            free_driver_configuration(inst, &settings->additional_drivers[i]);
67
0
        }
68
0
        loader_instance_heap_free(inst, settings->additional_drivers);
69
0
    }
70
2.85k
    if (NULL != settings->device_configurations) {
71
0
        for (uint32_t i = 0; i < settings->device_configuration_count; i++) {
72
0
            free_device_configuration(inst, &settings->device_configurations[i]);
73
0
        }
74
0
        loader_instance_heap_free(inst, settings->device_configurations);
75
0
    }
76
2.85k
    loader_instance_heap_free(inst, settings->settings_file_path);
77
2.85k
    memset(settings, 0, sizeof(loader_settings));
78
2.85k
}
79
80
29.6k
loader_settings_layer_control parse_control_string(char* control_string) {
81
29.6k
    loader_settings_layer_control layer_control = LOADER_SETTINGS_LAYER_CONTROL_DEFAULT;
82
29.6k
    if (strcmp(control_string, "auto") == 0)
83
469
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_DEFAULT;
84
29.1k
    else if (strcmp(control_string, "on") == 0)
85
602
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_ON;
86
28.5k
    else if (strcmp(control_string, "off") == 0)
87
7.41k
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_OFF;
88
21.1k
    else if (strcmp(control_string, "unordered_layer_location") == 0)
89
16.9k
        layer_control = LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION;
90
29.6k
    return layer_control;
91
29.6k
}
92
93
28.3k
const char* loader_settings_layer_control_to_string(loader_settings_layer_control control) {
94
28.3k
    switch (control) {
95
3.59k
        case LOADER_SETTINGS_LAYER_CONTROL_DEFAULT:
96
3.59k
            return "auto";
97
585
        case LOADER_SETTINGS_LAYER_CONTROL_ON:
98
585
            return "on";
99
7.35k
        case LOADER_SETTINGS_LAYER_CONTROL_OFF:
100
7.35k
            return "off";
101
16.8k
        case LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION:
102
16.8k
            return "unordered_layer_location";
103
0
        default:
104
0
            return "UNKNOWN_LAYER_CONTROl";
105
28.3k
    }
106
28.3k
}
107
108
295
uint32_t parse_log_filters_from_strings(struct loader_string_list* log_filters) {
109
295
    uint32_t filters = 0;
110
160k
    for (uint32_t i = 0; i < log_filters->count; i++) {
111
160k
        if (strcmp(log_filters->list[i], "all") == 0)
112
1.91k
            filters |= VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_PERF_BIT | VULKAN_LOADER_ERROR_BIT |
113
1.91k
                       VULKAN_LOADER_DEBUG_BIT | VULKAN_LOADER_LAYER_BIT | VULKAN_LOADER_DRIVER_BIT | VULKAN_LOADER_VALIDATION_BIT;
114
158k
        else if (strcmp(log_filters->list[i], "info") == 0)
115
798
            filters |= VULKAN_LOADER_INFO_BIT;
116
157k
        else if (strcmp(log_filters->list[i], "warn") == 0)
117
599
            filters |= VULKAN_LOADER_WARN_BIT;
118
157k
        else if (strcmp(log_filters->list[i], "perf") == 0)
119
767
            filters |= VULKAN_LOADER_PERF_BIT;
120
156k
        else if (strcmp(log_filters->list[i], "error") == 0)
121
546
            filters |= VULKAN_LOADER_ERROR_BIT;
122
155k
        else if (strcmp(log_filters->list[i], "debug") == 0)
123
546
            filters |= VULKAN_LOADER_DEBUG_BIT;
124
155k
        else if (strcmp(log_filters->list[i], "layer") == 0)
125
793
            filters |= VULKAN_LOADER_LAYER_BIT;
126
154k
        else if (strcmp(log_filters->list[i], "driver") == 0)
127
1.51k
            filters |= VULKAN_LOADER_DRIVER_BIT;
128
153k
        else if (strcmp(log_filters->list[i], "validation") == 0)
129
269
            filters |= VULKAN_LOADER_VALIDATION_BIT;
130
160k
    }
131
295
    return filters;
132
295
}
133
134
0
bool parse_json_enable_disable_option(cJSON* object) {
135
0
    char* str = loader_cJSON_GetStringValue(object);
136
0
    if (NULL == str) {
137
0
        return false;
138
0
    }
139
0
    bool enable = false;
140
0
    if (strcmp(str, "enabled") == 0) {
141
0
        enable = true;
142
0
    }
143
0
    return enable;
144
0
}
145
146
VkResult parse_layer_configuration(const struct loader_instance* inst, cJSON* layer_configuration_json,
147
29.6k
                                   loader_settings_layer_configuration* layer_configuration) {
148
29.6k
    char* control_string = NULL;
149
29.6k
    VkResult res = loader_parse_json_string(layer_configuration_json, "control", &control_string);
150
29.6k
    if (res != VK_SUCCESS) {
151
14
        goto out;
152
14
    }
153
29.6k
    layer_configuration->control = parse_control_string(control_string);
154
29.6k
    loader_instance_heap_free(inst, control_string);
155
156
    // If that is the only value - do no further parsing
157
29.6k
    if (layer_configuration->control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
158
16.9k
        goto out;
159
16.9k
    }
160
161
12.6k
    res = loader_parse_json_string(layer_configuration_json, "name", &(layer_configuration->name));
162
12.6k
    if (res != VK_SUCCESS) {
163
101
        goto out;
164
101
    }
165
166
12.5k
    res = loader_parse_json_string(layer_configuration_json, "path", &(layer_configuration->path));
167
12.5k
    if (res != VK_SUCCESS) {
168
5
        goto out;
169
5
    }
170
171
12.5k
    cJSON* treat_as_implicit_manifest = loader_cJSON_GetObjectItem(layer_configuration_json, "treat_as_implicit_manifest");
172
12.5k
    if (treat_as_implicit_manifest && treat_as_implicit_manifest->type == cJSON_True) {
173
295
        layer_configuration->treat_as_implicit_manifest = true;
174
295
    }
175
29.6k
out:
176
29.6k
    if (VK_SUCCESS != res) {
177
120
        free_layer_configuration(inst, layer_configuration);
178
120
    }
179
29.6k
    return res;
180
12.5k
}
181
182
1.17k
VkResult parse_layer_configurations(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
183
1.17k
    VkResult res = VK_SUCCESS;
184
185
1.17k
    cJSON* layer_configurations = loader_cJSON_GetObjectItem(settings_object, "layers");
186
    // If the layers object isn't present, return early with success to allow the settings file to still apply
187
1.17k
    if (NULL == layer_configurations) {
188
305
        return VK_SUCCESS;
189
305
    }
190
191
869
    uint32_t layer_configurations_count = loader_cJSON_GetArraySize(layer_configurations);
192
869
    if (layer_configurations_count == 0) {
193
50
        loader_settings->layer_configurations_active = true;
194
50
        return VK_SUCCESS;
195
50
    }
196
197
819
    loader_settings->layer_configuration_count = layer_configurations_count;
198
199
819
    loader_settings->layer_configurations = loader_instance_heap_calloc(
200
819
        inst, sizeof(loader_settings_layer_configuration) * layer_configurations_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
201
819
    if (NULL == loader_settings->layer_configurations) {
202
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
203
0
        goto out;
204
0
    }
205
206
819
    cJSON* layer = NULL;
207
819
    size_t i = 0;
208
29.6k
    cJSON_ArrayForEach(layer, layer_configurations) {
209
29.6k
        if (layer->type != cJSON_Object) {
210
13
            res = VK_ERROR_INITIALIZATION_FAILED;
211
13
            goto out;
212
13
        }
213
29.6k
        res = parse_layer_configuration(inst, layer, &(loader_settings->layer_configurations[i++]));
214
29.6k
        if (VK_SUCCESS != res) {
215
120
            goto out;
216
120
        }
217
29.6k
    }
218
686
    loader_settings->layer_configurations_active = true;
219
819
out:
220
819
    if (res != VK_SUCCESS) {
221
133
        if (loader_settings->layer_configurations) {
222
3.59k
            for (size_t index = 0; index < loader_settings->layer_configuration_count; index++) {
223
3.46k
                free_layer_configuration(inst, &(loader_settings->layer_configurations[index]));
224
3.46k
            }
225
133
            loader_settings->layer_configuration_count = 0;
226
133
            loader_instance_heap_free(inst, loader_settings->layer_configurations);
227
133
            loader_settings->layer_configurations = NULL;
228
133
        }
229
133
    }
230
231
819
    return res;
232
686
}
233
234
VkResult parse_additional_driver(const struct loader_instance* inst, cJSON* additional_driver_json,
235
0
                                 loader_settings_driver_configuration* additional_driver) {
236
0
    VkResult res = VK_SUCCESS;
237
0
    res = loader_parse_json_string(additional_driver_json, "path", &(additional_driver->path));
238
0
    if (res != VK_SUCCESS) {
239
0
        goto out;
240
0
    }
241
0
out:
242
0
    if (res != VK_SUCCESS) {
243
0
        free_driver_configuration(inst, additional_driver);
244
0
    }
245
0
    return res;
246
0
}
247
248
1.17k
VkResult parse_additional_drivers(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
249
1.17k
    VkResult res = VK_SUCCESS;
250
251
1.17k
    cJSON* additional_drivers_use_exclusively_json =
252
1.17k
        loader_cJSON_GetObjectItem(settings_object, "additional_drivers_use_exclusively");
253
1.17k
    if (additional_drivers_use_exclusively_json && additional_drivers_use_exclusively_json->type == cJSON_True) {
254
0
        loader_settings->additional_drivers_use_exclusively = true;
255
0
    }
256
257
1.17k
    cJSON* additional_drivers_json = loader_cJSON_GetObjectItem(settings_object, "additional_drivers");
258
1.17k
    if (NULL == additional_drivers_json) {
259
1.17k
        return VK_SUCCESS;
260
1.17k
    }
261
262
0
    uint32_t additional_driver_count = loader_cJSON_GetArraySize(additional_drivers_json);
263
0
    if (additional_driver_count == 0) {
264
0
        return VK_SUCCESS;
265
0
    }
266
267
0
    loader_settings->additional_driver_count = additional_driver_count;
268
269
0
    loader_settings->additional_drivers = loader_instance_heap_calloc(
270
0
        inst, sizeof(loader_settings_driver_configuration) * additional_driver_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
271
0
    if (NULL == loader_settings->additional_drivers) {
272
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
273
0
        goto out;
274
0
    }
275
276
0
    cJSON* driver = NULL;
277
0
    size_t i = 0;
278
0
    cJSON_ArrayForEach(driver, additional_drivers_json) {
279
0
        if (driver->type != cJSON_Object) {
280
0
            res = VK_ERROR_INITIALIZATION_FAILED;
281
0
            goto out;
282
0
        }
283
0
        res = parse_additional_driver(inst, driver, &(loader_settings->additional_drivers[i++]));
284
0
        if (VK_SUCCESS != res) {
285
0
            goto out;
286
0
        }
287
0
    }
288
0
out:
289
0
    if (res != VK_SUCCESS) {
290
0
        if (loader_settings->additional_drivers) {
291
0
            for (size_t index = 0; index < loader_settings->additional_driver_count; index++) {
292
0
                free_driver_configuration(inst, &(loader_settings->additional_drivers[index]));
293
0
            }
294
0
            loader_settings->additional_driver_count = 0;
295
0
            loader_instance_heap_free(inst, loader_settings->additional_drivers);
296
0
            loader_settings->additional_drivers = NULL;
297
0
        }
298
0
    }
299
0
    return res;
300
0
}
301
302
0
VkResult parse_uuid_array(cJSON* device_configuration_json, const char* uuid_name, uint8_t uuid[16]) {
303
0
    cJSON* uuid_array = loader_cJSON_GetObjectItem(device_configuration_json, uuid_name);
304
0
    if (NULL == uuid_array) {
305
0
        return VK_ERROR_INITIALIZATION_FAILED;
306
0
    }
307
308
0
    if (uuid_array->type != cJSON_Array) {
309
0
        return VK_ERROR_INITIALIZATION_FAILED;
310
0
    }
311
0
    if (VK_UUID_SIZE != loader_cJSON_GetArraySize(uuid_array)) {
312
0
        return VK_ERROR_INITIALIZATION_FAILED;
313
0
    }
314
315
0
    cJSON* uuid_field = NULL;
316
0
    size_t i = 0;
317
0
    cJSON_ArrayForEach(uuid_field, uuid_array) {
318
0
        if (i >= VK_UUID_SIZE) {
319
0
            break;
320
0
        }
321
0
        if (uuid_field->type != cJSON_Number) {
322
0
            return VK_ERROR_INITIALIZATION_FAILED;
323
0
        }
324
0
        if (uuid_field->valueint < 0 || uuid_field->valueint > 255) {
325
0
            return VK_ERROR_INITIALIZATION_FAILED;
326
0
        }
327
328
0
        uuid[i] = (uint8_t)uuid_field->valueint;
329
0
        i++;
330
0
    }
331
0
    return VK_SUCCESS;
332
0
}
333
334
VkResult parse_device_configuration(const struct loader_instance* inst, cJSON* device_configuration_json,
335
0
                                    loader_settings_device_configuration* device_configuration) {
336
0
    (void)inst;
337
0
    VkResult res = VK_SUCCESS;
338
339
0
    res = parse_uuid_array(device_configuration_json, "deviceUUID", device_configuration->deviceUUID);
340
0
    if (VK_SUCCESS != res) {
341
0
        goto out;
342
0
    }
343
344
0
    res = parse_uuid_array(device_configuration_json, "driverUUID", device_configuration->driverUUID);
345
0
    if (VK_SUCCESS != res) {
346
0
        goto out;
347
0
    }
348
349
0
    cJSON* driverVersion_json = loader_cJSON_GetObjectItem(device_configuration_json, "driverVersion");
350
0
    if (NULL == driverVersion_json || driverVersion_json->type != cJSON_Number) {
351
0
        return VK_ERROR_INITIALIZATION_FAILED;
352
0
    }
353
0
    device_configuration->driverVersion = driverVersion_json->valueint;
354
355
0
    VkResult deviceNameRes = loader_parse_json_string_to_existing_str(
356
0
        device_configuration_json, "deviceName", VK_MAX_PHYSICAL_DEVICE_NAME_SIZE, device_configuration->deviceName);
357
0
    if (VK_ERROR_OUT_OF_HOST_MEMORY == deviceNameRes) {
358
0
        res = deviceNameRes;
359
0
        goto out;
360
0
    }
361
362
0
    VkResult driverNameRes = loader_parse_json_string_to_existing_str(
363
0
        device_configuration_json, "driverName", VK_MAX_PHYSICAL_DEVICE_NAME_SIZE, device_configuration->driverName);
364
0
    if (VK_ERROR_OUT_OF_HOST_MEMORY == driverNameRes) {
365
0
        res = driverNameRes;
366
0
        goto out;
367
0
    }
368
0
out:
369
0
    if (res != VK_SUCCESS) {
370
0
        memset(device_configuration, 0, sizeof(loader_settings_device_configuration));
371
0
    }
372
0
    return res;
373
0
}
374
375
1.17k
VkResult parse_device_configurations(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
376
1.17k
    VkResult res = VK_SUCCESS;
377
378
1.17k
    cJSON* device_configurations = loader_cJSON_GetObjectItem(settings_object, "device_configurations");
379
1.17k
    if (NULL == device_configurations) {
380
1.17k
        return VK_SUCCESS;
381
1.17k
    }
382
383
0
    uint32_t device_configuration_count = loader_cJSON_GetArraySize(device_configurations);
384
0
    if (device_configuration_count == 0) {
385
0
        loader_settings->device_configurations_active = true;
386
0
        return VK_SUCCESS;
387
0
    }
388
389
0
    loader_settings->device_configuration_count = device_configuration_count;
390
391
0
    loader_settings->device_configurations = loader_instance_heap_calloc(
392
0
        inst, sizeof(loader_settings_device_configuration) * device_configuration_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
393
0
    if (NULL == loader_settings->device_configurations) {
394
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
395
0
        goto out;
396
0
    }
397
398
0
    cJSON* device = NULL;
399
0
    size_t i = 0;
400
0
    cJSON_ArrayForEach(device, device_configurations) {
401
0
        if (device->type != cJSON_Object) {
402
0
            res = VK_ERROR_INITIALIZATION_FAILED;
403
0
            goto out;
404
0
        }
405
0
        res = parse_device_configuration(inst, device, &(loader_settings->device_configurations[i]));
406
0
        if (res == VK_ERROR_OUT_OF_HOST_MEMORY) {
407
0
            goto out;
408
0
        } else if (res != VK_SUCCESS) {
409
            // Skip a malformed entry rather than discarding every device configuration in the file
410
0
            res = VK_SUCCESS;
411
0
            continue;
412
0
        }
413
0
        i++;
414
0
    }
415
    // Only the first i entries were successfully parsed; the trailing entries are unpopulated
416
0
    loader_settings->device_configuration_count = (uint32_t)i;
417
0
    loader_settings->device_configurations_active = true;
418
0
out:
419
0
    if (res != VK_SUCCESS) {
420
0
        if (loader_settings->device_configurations) {
421
0
            for (size_t index = 0; index < loader_settings->device_configuration_count; index++) {
422
0
                free_device_configuration(inst, &(loader_settings->device_configurations[index]));
423
0
            }
424
0
            loader_settings->device_configuration_count = 0;
425
0
            loader_instance_heap_free(inst, loader_settings->device_configurations);
426
0
            loader_settings->device_configurations = NULL;
427
0
        }
428
0
    }
429
0
    return res;
430
0
}
431
432
#if COMMON_UNIX_PLATFORMS
433
// Given a base and suffix path, determine if a file at that location exists, and if it is return success.
434
// Since base may contain multiple paths separated by PATH_SEPARATOR, we must extract each segment and check segment + suffix
435
// individually
436
VkResult check_if_settings_path_exists(const struct loader_instance* inst, const char* base, const char* suffix,
437
25.5k
                                       char** settings_file_path) {
438
25.5k
    if (NULL == base || NULL == suffix) {
439
11.9k
        return VK_ERROR_INITIALIZATION_FAILED;
440
11.9k
    }
441
442
13.6k
    size_t base_len = strlen(base);
443
13.6k
    size_t suffix_len = strlen(suffix);
444
445
13.6k
    uint32_t start = 0;
446
13.6k
    uint32_t stop = 0;
447
22.0k
    while (base[start] != '\0' && start < base_len && stop < base_len) {
448
14.0k
        start = stop;
449
14.0k
        stop = start + 1;
450
69.9k
        while (base[stop] != PATH_SEPARATOR && base[stop] != '\0' && stop < base_len) {
451
55.9k
            stop++;
452
55.9k
        }
453
454
14.0k
        size_t segment_len = (stop - start);
455
14.0k
        if (segment_len <= 1) {
456
            // segment is *just* a PATH_SEPARATOR, skip it
457
0
            continue;
458
0
        }
459
14.0k
        size_t path_len = segment_len + suffix_len + 1;
460
14.0k
        *settings_file_path = loader_instance_heap_calloc(inst, path_len, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
461
14.0k
        if (NULL == *settings_file_path) {
462
0
            return VK_ERROR_OUT_OF_HOST_MEMORY;
463
0
        }
464
14.0k
        loader_strncpy(*settings_file_path, path_len, base + start, segment_len);
465
14.0k
        loader_strncat(*settings_file_path, path_len, suffix, suffix_len);
466
467
14.0k
        if (loader_platform_file_exists(*settings_file_path)) {
468
5.54k
            return VK_SUCCESS;
469
5.54k
        }
470
8.48k
        loader_instance_heap_free(inst, *settings_file_path);
471
8.48k
        *settings_file_path = NULL;
472
8.48k
    }
473
474
8.06k
    return VK_ERROR_INITIALIZATION_FAILED;
475
13.6k
}
476
477
// Follow the logic of read_data_files_in_search_paths but only look for "/vulkan/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME
478
5.96k
VkResult get_unix_settings_path(const struct loader_instance* inst, char** settings_file_path) {
479
    // First, get XDG env-vars we use. Don't need to worry about free'ing because on linux getenv is non-allocating
480
5.96k
    char* xdg_config_home = loader_secure_getenv("XDG_CONFIG_HOME", inst);
481
5.96k
    char* xdg_config_dirs = loader_secure_getenv("XDG_CONFIG_DIRS", inst);
482
5.96k
    char* xdg_data_home = loader_secure_getenv("XDG_DATA_HOME", inst);
483
5.96k
    char* xdg_data_dirs = loader_secure_getenv("XDG_DATA_DIRS", inst);
484
485
    // Use fallback directories for xdg_config_dirs and xdg_data_dirs if they are NULL.
486
5.96k
#if !defined(__Fuchsia__) && !defined(__QNX__)
487
5.96k
    if (NULL == xdg_config_dirs || '\0' == xdg_config_dirs[0]) {
488
5.96k
        xdg_config_dirs = FALLBACK_CONFIG_DIRS;
489
5.96k
    }
490
5.96k
#endif
491
492
5.96k
#if !defined(__Fuchsia__) && !defined(__QNX__)
493
5.96k
    if (NULL == xdg_data_dirs || '\0' == xdg_data_dirs[0]) {
494
5.96k
        xdg_data_dirs = FALLBACK_DATA_DIRS;
495
5.96k
    }
496
5.96k
#endif
497
498
5.96k
    VkResult res = check_if_settings_path_exists(
499
5.96k
        inst, xdg_config_home, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
500
5.96k
    if (res == VK_SUCCESS) {
501
0
        return res;
502
0
    }
503
504
5.96k
    res = check_if_settings_path_exists(inst, xdg_data_home, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
505
5.96k
                                        settings_file_path);
506
5.96k
    if (res == VK_SUCCESS) {
507
0
        return res;
508
0
    }
509
510
    // Check home if either xdg_config_home or xdg_data_home wasn't set
511
5.96k
    char* home = loader_secure_getenv("HOME", inst);
512
5.96k
    if (home != NULL) {
513
5.96k
        if (NULL == xdg_config_home || '\0' == xdg_config_home[0]) {
514
5.96k
            res = check_if_settings_path_exists(
515
5.96k
                inst, home, "/.config/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
516
5.96k
            if (res == VK_SUCCESS) {
517
0
                return res;
518
0
            }
519
5.96k
        }
520
5.96k
        if (NULL == xdg_data_home || '\0' == xdg_data_home[0]) {
521
5.96k
            res = check_if_settings_path_exists(
522
5.96k
                inst, home, "/.local/share/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
523
5.96k
            if (res == VK_SUCCESS) {
524
5.54k
                return res;
525
5.54k
            }
526
5.96k
        }
527
5.96k
    }
528
529
420
    res = check_if_settings_path_exists(inst, xdg_config_dirs, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
530
420
                                        settings_file_path);
531
420
    if (res == VK_SUCCESS) {
532
0
        return res;
533
0
    }
534
535
420
    res = check_if_settings_path_exists(inst, SYSCONFDIR, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
536
420
                                        settings_file_path);
537
420
    if (res == VK_SUCCESS) {
538
0
        return res;
539
0
    }
540
420
#if defined(EXTRASYSCONFDIR)
541
542
420
    res = check_if_settings_path_exists(inst, EXTRASYSCONFDIR, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
543
420
                                        settings_file_path);
544
420
    if (res == VK_SUCCESS) {
545
0
        return res;
546
0
    }
547
420
#endif
548
420
    res = check_if_settings_path_exists(inst, xdg_data_dirs, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
549
420
                                        settings_file_path);
550
420
    if (res == VK_SUCCESS) {
551
0
        return res;
552
0
    }
553
554
420
    return VK_ERROR_INITIALIZATION_FAILED;
555
420
}
556
#endif
557
558
2.06k
bool check_if_layer_configurations_are_equal(loader_settings_layer_configuration* a, loader_settings_layer_configuration* b) {
559
2.06k
    if (!a->name || !b->name || 0 != strcmp(a->name, b->name)) {
560
381
        return false;
561
381
    }
562
1.68k
    if (!a->path || !b->path || 0 != strcmp(a->path, b->path)) {
563
43
        return false;
564
43
    }
565
1.63k
    return a->control == b->control && a->treat_as_implicit_manifest == b->treat_as_implicit_manifest;
566
1.68k
}
567
568
0
bool check_if_driver_configurations_are_equal(loader_settings_driver_configuration* a, loader_settings_driver_configuration* b) {
569
0
    if (!a->path || !b->path || 0 != strcmp(a->path, b->path)) {
570
0
        return false;
571
0
    }
572
0
    return true;
573
0
}
574
575
0
bool check_if_device_configurations_are_equal(loader_settings_device_configuration* a, loader_settings_device_configuration* b) {
576
0
    for (uint32_t i = 0; i < VK_UUID_SIZE; i++) {
577
0
        if (a->deviceUUID[i] != b->deviceUUID[i]) return false;
578
0
    }
579
0
    for (uint32_t i = 0; i < VK_UUID_SIZE; i++) {
580
0
        if (a->driverUUID[i] != b->driverUUID[i]) return false;
581
0
    }
582
0
    if (a->driverVersion != b->driverVersion) return false;
583
0
    if (0 != strcmp(a->deviceName, b->deviceName)) return false;
584
0
    if (0 != strcmp(a->driverName, b->driverName)) return false;
585
0
    return true;
586
0
}
587
588
2.85k
bool check_if_settings_are_equal(loader_settings* a, loader_settings* b) {
589
    // If either pointer is null, return true
590
2.85k
    if (NULL == a || NULL == b) return false;
591
2.85k
    bool are_equal = true;
592
2.85k
    are_equal &= a->settings_active == b->settings_active;
593
2.85k
    are_equal &= a->has_unordered_layer_location == b->has_unordered_layer_location;
594
2.85k
    are_equal &= a->debug_level == b->debug_level;
595
2.85k
    are_equal &= a->layer_configurations_active == b->layer_configurations_active;
596
2.85k
    are_equal &= a->layer_configuration_count == b->layer_configuration_count;
597
2.85k
    are_equal &= a->additional_drivers_use_exclusively == b->additional_drivers_use_exclusively;
598
2.85k
    are_equal &= a->additional_driver_count == b->additional_driver_count;
599
2.85k
    are_equal &= a->device_configurations_active == b->device_configurations_active;
600
2.85k
    are_equal &= a->device_configuration_count == b->device_configuration_count;
601
2.85k
    if (!are_equal) return false;
602
3.90k
    for (uint32_t i = 0; i < a->layer_configuration_count && i < b->layer_configuration_count; i++) {
603
2.06k
        are_equal &= check_if_layer_configurations_are_equal(&a->layer_configurations[i], &b->layer_configurations[i]);
604
2.06k
    }
605
1.84k
    for (uint32_t i = 0; i < a->additional_driver_count && i < b->additional_driver_count; i++) {
606
0
        are_equal &= check_if_driver_configurations_are_equal(&a->additional_drivers[i], &b->additional_drivers[i]);
607
0
    }
608
1.84k
    for (uint32_t i = 0; i < a->device_configuration_count && i < b->device_configuration_count; i++) {
609
0
        are_equal &= check_if_device_configurations_are_equal(&a->device_configurations[i], &b->device_configurations[i]);
610
0
    }
611
1.84k
    return are_equal;
612
2.85k
}
613
614
1.13k
void log_settings(const struct loader_instance* inst, loader_settings* settings) {
615
1.13k
    if (settings == NULL) {
616
0
        return;
617
0
    }
618
1.13k
    loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "Using layer configurations found in loader settings from %s",
619
1.13k
               settings->settings_file_path);
620
621
1.13k
    char cmd_line_msg[64] = {0};
622
1.13k
    size_t cmd_line_size = sizeof(cmd_line_msg);
623
624
1.13k
    cmd_line_msg[0] = '\0';
625
626
1.13k
    generate_debug_flag_str(settings->debug_level, cmd_line_size, cmd_line_msg);
627
1.13k
    if (strlen(cmd_line_msg)) {
628
121
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Loader Settings Filters for Logging to Standard Error: %s", cmd_line_msg);
629
121
    }
630
1.13k
    if (settings->layer_configurations_active) {
631
700
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Layer Configurations count = %d", settings->layer_configuration_count);
632
29.0k
        for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
633
28.3k
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---- Layer Configuration [%d] ----", i);
634
28.3k
            if (settings->layer_configurations[i].control != LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
635
11.5k
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Name: %s", settings->layer_configurations[i].name);
636
11.5k
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Path: %s", settings->layer_configurations[i].path);
637
11.5k
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Layer Type: %s",
638
11.5k
                           settings->layer_configurations[i].treat_as_implicit_manifest ? "Implicit" : "Explicit");
639
11.5k
            }
640
28.3k
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Control: %s",
641
28.3k
                       loader_settings_layer_control_to_string(settings->layer_configurations[i].control));
642
28.3k
        }
643
700
    }
644
1.13k
    if (settings->additional_driver_count > 0) {
645
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "----");
646
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Use Additional Drivers Exclusively = %s",
647
0
                   settings->additional_drivers_use_exclusively ? "true" : "false");
648
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Additional Driver Configurations count = %d",
649
0
                   settings->additional_driver_count);
650
0
        for (uint32_t i = 0; i < settings->additional_driver_count; i++) {
651
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---- Driver Configuration [%d] ----", i);
652
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Path: %s", settings->additional_drivers[i].path);
653
0
        }
654
0
    }
655
1.13k
    if (settings->device_configurations_active) {
656
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "----");
657
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Device Configurations count = %d", settings->device_configuration_count);
658
0
        for (uint32_t i = 0; i < settings->device_configuration_count; i++) {
659
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---- Device Configuration [%d] ----", i);
660
0
            char device_uuid_str[UUID_STR_LEN] = {0};
661
0
            loader_log_generate_uuid_string(settings->device_configurations[i].deviceUUID, device_uuid_str);
662
0
            char driver_uuid_str[UUID_STR_LEN] = {0};
663
0
            loader_log_generate_uuid_string(settings->device_configurations[i].driverUUID, driver_uuid_str);
664
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "deviceUUID: %s", device_uuid_str);
665
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "driverUUID: %s", driver_uuid_str);
666
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "driverVersion: %d", settings->device_configurations[i].driverVersion);
667
0
            if ('\0' != settings->device_configurations[i].deviceName[0]) {
668
0
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "deviceName: %s", settings->device_configurations[i].deviceName);
669
0
            }
670
0
            if ('\0' != settings->device_configurations[i].driverName[0]) {
671
0
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "driverName: %s", settings->device_configurations[i].driverName);
672
0
            }
673
0
        }
674
0
    }
675
1.13k
    loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---------------------------------");
676
1.13k
}
677
678
// Loads the vk_loader_settings.json file
679
// Returns VK_SUCCESS if it was found & was successfully parsed. Otherwise, it returns VK_ERROR_INITIALIZATION_FAILED if it
680
// wasn't found or failed to parse, and returns VK_ERROR_OUT_OF_HOST_MEMORY if it was unable to allocate enough memory.
681
5.96k
VkResult get_loader_settings(const struct loader_instance* inst, loader_settings* loader_settings) {
682
5.96k
    VkResult res = VK_SUCCESS;
683
5.96k
    cJSON* json = NULL;
684
5.96k
    char* file_format_version_string = NULL;
685
5.96k
    char* settings_file_path = NULL;
686
#if defined(WIN32)
687
    res = windows_get_loader_settings_file_path(inst, &settings_file_path);
688
    if (res != VK_SUCCESS) {
689
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
690
                   "No valid vk_loader_settings.json file found, no loader settings will be active");
691
        goto out;
692
    }
693
694
#elif COMMON_UNIX_PLATFORMS
695
    res = get_unix_settings_path(inst, &settings_file_path);
696
5.96k
    if (res != VK_SUCCESS) {
697
420
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
698
420
                   "No valid vk_loader_settings.json file found, no loader settings will be active");
699
420
        goto out;
700
420
    }
701
#else
702
#warning "Unsupported platform - must specify platform specific location for vk_loader_settings.json"
703
#endif
704
705
5.54k
    res = loader_get_json(inst, settings_file_path, &json);
706
    // Make sure sure the top level json value is an object
707
5.54k
    if (res != VK_SUCCESS || NULL == json || json->type != cJSON_Object) {
708
1.66k
        goto out;
709
1.66k
    }
710
711
3.87k
    res = loader_parse_json_string(json, "file_format_version", &file_format_version_string);
712
3.87k
    if (res != VK_SUCCESS) {
713
47
        if (res != VK_ERROR_OUT_OF_HOST_MEMORY) {
714
47
            loader_log(
715
47
                inst, VULKAN_LOADER_DEBUG_BIT, 0,
716
47
                "Loader settings file from %s missing required field file_format_version - no loader settings will be active",
717
47
                settings_file_path);
718
47
        }
719
47
        goto out;
720
47
    }
721
722
    // Because the file may contain either a "settings_array" or a single "settings" object, we need to create a cJSON so that we
723
    // can iterate on both cases with common code
724
3.82k
    cJSON settings_iter_parent = {0};
725
726
3.82k
    cJSON* settings_array = loader_cJSON_GetObjectItem(json, "settings_array");
727
3.82k
    cJSON* single_settings_object = loader_cJSON_GetObjectItem(json, "settings");
728
3.82k
    if (NULL != settings_array) {
729
1.12k
        memcpy(&settings_iter_parent, settings_array, sizeof(cJSON));
730
2.70k
    } else if (NULL != single_settings_object) {
731
170
        settings_iter_parent.child = single_settings_object;
732
2.53k
    } else {
733
2.53k
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
734
2.53k
                   "Loader settings file from %s missing required settings objects: Either one of the \"settings\" or "
735
2.53k
                   "\"settings_array\" objects must be present - no loader settings will be active",
736
2.53k
                   settings_file_path);
737
2.53k
        res = VK_ERROR_INITIALIZATION_FAILED;
738
2.53k
        goto out;
739
2.53k
    }
740
741
    // Corresponds to the settings object that has no app keys
742
1.29k
    cJSON* global_settings = NULL;
743
    // Corresponds to the settings object which has a matching app key
744
1.29k
    cJSON* settings_to_use = NULL;
745
746
1.29k
    char current_process_path[1024];
747
1.29k
    bool valid_exe_path = NULL != loader_platform_executable_path(current_process_path, 1024);
748
749
1.29k
    cJSON* settings_object_iter = NULL;
750
1.90k
    cJSON_ArrayForEach(settings_object_iter, &settings_iter_parent) {
751
1.90k
        if (settings_object_iter->type != cJSON_Object) {
752
36
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
753
36
                       "Loader settings file from %s has a settings element that is not an object", settings_file_path);
754
36
            break;
755
36
        }
756
757
1.87k
        cJSON* app_keys = loader_cJSON_GetObjectItem(settings_object_iter, "app_keys");
758
1.87k
        if (NULL == app_keys) {
759
            // use the first 'global' settings that has no app keys as the global one
760
1.40k
            if (global_settings == NULL) {
761
1.17k
                global_settings = settings_object_iter;
762
1.17k
            }
763
1.40k
            continue;
764
1.40k
        }
765
        // No sense iterating if we couldn't get the executable path
766
466
        if (!valid_exe_path) {
767
0
            break;
768
0
        }
769
466
        cJSON* app_key = NULL;
770
944
        cJSON_ArrayForEach(app_key, app_keys) {
771
944
            char* app_key_str = loader_cJSON_GetStringValue(app_key);
772
944
            if (app_key_str && strcmp(current_process_path, app_key_str) == 0) {
773
0
                settings_to_use = settings_object_iter;
774
0
                break;
775
0
            }
776
944
        }
777
778
        // Break if we have found a matching current_process_path
779
466
        if (NULL != settings_to_use) {
780
0
            break;
781
0
        }
782
466
    }
783
784
    // No app specific settings match - either use global settings or exit
785
1.29k
    if (settings_to_use == NULL) {
786
1.29k
        if (global_settings == NULL) {
787
120
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
788
120
                       "Loader settings file from %s missing global settings and none of the app specific settings matched the "
789
120
                       "current application - no loader settings will be active",
790
120
                       settings_file_path);
791
120
            goto out;  // No global settings were found - exit
792
1.17k
        } else {
793
1.17k
            settings_to_use = global_settings;  // Global settings are present - use it
794
1.17k
        }
795
1.29k
    }
796
797
    // optional
798
1.17k
    cJSON* stderr_filter = loader_cJSON_GetObjectItem(settings_to_use, "stderr_log");
799
1.17k
    if (NULL != stderr_filter) {
800
295
        struct loader_string_list stderr_log = {0};
801
295
        VkResult stderr_log_result = VK_SUCCESS;
802
295
        stderr_log_result = loader_parse_json_array_of_strings(inst, settings_to_use, "stderr_log", &stderr_log);
803
295
        if (VK_ERROR_OUT_OF_HOST_MEMORY == stderr_log_result) {
804
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
805
0
            goto out;
806
0
        }
807
        // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) - 0xFF is a valid bitwise-OR of all flags (the "all" filter)
808
295
        loader_settings->debug_level = parse_log_filters_from_strings(&stderr_log);
809
295
        free_string_list(inst, &stderr_log);
810
295
    }
811
812
    // optional
813
1.17k
    cJSON* logs_to_use = loader_cJSON_GetObjectItem(settings_to_use, "log_locations");
814
1.17k
    if (NULL != logs_to_use) {
815
44
        cJSON* log_element = NULL;
816
663
        cJSON_ArrayForEach(log_element, logs_to_use) {
817
            // bool is_valid = true;
818
663
            struct loader_string_list log_destinations = {0};
819
663
            VkResult parse_dest_res = loader_parse_json_array_of_strings(inst, log_element, "destinations", &log_destinations);
820
663
            if (parse_dest_res != VK_SUCCESS) {
821
                // is_valid = false;
822
657
            }
823
663
            free_string_list(inst, &log_destinations);
824
663
            struct loader_string_list log_filters = {0};
825
663
            VkResult parse_filters_res = loader_parse_json_array_of_strings(inst, log_element, "filters", &log_filters);
826
663
            if (parse_filters_res != VK_SUCCESS) {
827
                // is_valid = false;
828
463
            }
829
663
            free_string_list(inst, &log_filters);
830
663
        }
831
44
    }
832
833
1.17k
    VkResult layer_configurations_res = parse_layer_configurations(inst, settings_to_use, loader_settings);
834
1.17k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == layer_configurations_res) {
835
0
        res = layer_configurations_res;
836
0
        goto out;
837
0
    }
838
839
    // Determine if there exists a layer configuration indicating where to put layers not contained in the settings file
840
    // LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION
841
6.90k
    for (uint32_t i = 0; i < loader_settings->layer_configuration_count; i++) {
842
6.14k
        if (loader_settings->layer_configurations[i].control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
843
414
            loader_settings->has_unordered_layer_location = true;
844
414
            break;
845
414
        }
846
6.14k
    }
847
848
1.17k
    VkResult additional_drivers_res = parse_additional_drivers(inst, settings_to_use, loader_settings);
849
1.17k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == additional_drivers_res) {
850
0
        res = additional_drivers_res;
851
0
        goto out;
852
0
    }
853
854
1.17k
    VkResult device_configurations_res = parse_device_configurations(inst, settings_to_use, loader_settings);
855
1.17k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == device_configurations_res) {
856
0
        res = device_configurations_res;
857
0
        goto out;
858
0
    }
859
860
    // Only consider the settings active if there is at least one "setting" active.
861
    // Those are either logging, layers, additional_drivers, or device_configurations.
862
1.17k
    if (loader_settings->debug_level != 0 || loader_settings->layer_configurations_active ||
863
834
        loader_settings->additional_driver_count != 0 || loader_settings->device_configurations_active) {
864
834
        loader_settings->settings_file_path = settings_file_path;
865
834
        settings_file_path = NULL;
866
834
        loader_settings->settings_active = true;
867
834
    } else {
868
340
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
869
340
                   "vk_loader_settings.json file found at \"%s\" but did not contain any valid settings.", settings_file_path);
870
340
    }
871
5.96k
out:
872
5.96k
    if (NULL != json) {
873
4.25k
        loader_cJSON_Delete(json);
874
4.25k
    }
875
876
5.96k
    loader_instance_heap_free(inst, settings_file_path);
877
878
5.96k
    loader_instance_heap_free(inst, file_format_version_string);
879
5.96k
    return res;
880
1.17k
}
881
882
5.96k
TEST_FUNCTION_EXPORT VkResult update_global_loader_settings(void) {
883
5.96k
    loader_settings settings = {0};
884
5.96k
    VkResult res = get_loader_settings(NULL, &settings);
885
5.96k
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
886
887
5.96k
    if (res == VK_SUCCESS) {
888
2.85k
        if (!check_if_settings_are_equal(&settings, &global_loader_settings)) {
889
1.13k
            log_settings(NULL, &settings);
890
1.13k
        }
891
2.85k
        free_loader_settings(NULL, &global_loader_settings);
892
2.85k
        memcpy(&global_loader_settings, &settings, sizeof(loader_settings));
893
2.85k
        if (global_loader_settings.settings_active && global_loader_settings.debug_level > 0) {
894
126
            loader_set_global_debug_level(global_loader_settings.debug_level);
895
126
        }
896
2.85k
    }
897
5.96k
    loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
898
5.96k
    return res;
899
5.96k
}
900
901
2
void init_global_loader_settings(void) {
902
2
    loader_platform_thread_create_mutex(&global_loader_settings_lock);
903
    // Free out the global settings in case the process was loaded & unloaded
904
2
    free_loader_settings(NULL, &global_loader_settings);
905
2
}
906
0
void teardown_global_loader_settings(void) {
907
0
    free_loader_settings(NULL, &global_loader_settings);
908
0
    loader_platform_thread_delete_mutex(&global_loader_settings_lock);
909
0
}
910
911
0
bool should_skip_logging_global_messages(VkFlags msg_type) {
912
0
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
913
0
    bool should_skip = global_loader_settings.settings_active && 0 != (msg_type & global_loader_settings.debug_level);
914
0
    loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
915
0
    return should_skip;
916
0
}
917
918
// Use this function to get the correct settings to use based on the context
919
// If inst is NULL - use the global settings and lock the mutex
920
// Else return the settings local to the instance - but do nto lock the mutex
921
11.9k
const loader_settings* get_current_settings_and_lock(const struct loader_instance* inst) {
922
11.9k
    if (inst) {
923
0
        return &inst->settings;
924
0
    }
925
11.9k
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
926
11.9k
    return &global_loader_settings;
927
11.9k
}
928
// Release the global settings lock if we are using the global settings - aka if inst is NULL
929
11.9k
void release_current_settings_lock(const struct loader_instance* inst) {
930
11.9k
    if (inst == NULL) {
931
11.9k
        loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
932
11.9k
    }
933
11.9k
}
934
935
TEST_FUNCTION_EXPORT VkResult get_settings_layers(const struct loader_instance* inst, struct loader_layer_list* settings_layers,
936
11.9k
                                                  bool* should_search_for_other_layers) {
937
11.9k
    VkResult res = VK_SUCCESS;
938
11.9k
    *should_search_for_other_layers = true;  // default to true
939
940
11.9k
    const loader_settings* settings = get_current_settings_and_lock(inst);
941
942
11.9k
    if (NULL == settings || !settings->settings_active || !settings->layer_configurations_active) {
943
8.08k
        goto out;
944
8.08k
    }
945
946
    // Assume the list doesn't contain LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION at first
947
3.83k
    *should_search_for_other_layers = false;
948
949
112k
    for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
950
109k
        loader_settings_layer_configuration* layer_config = &settings->layer_configurations[i];
951
952
        // If we encountered a layer that should be forced off, we add it to the settings_layers list but only
953
        // with the data required to compare it with layers not in the settings file (aka name and manifest path)
954
109k
        if (layer_config->control == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
955
38.0k
            struct loader_layer_properties props = {0};
956
38.0k
            props.settings_control_value = LOADER_SETTINGS_LAYER_CONTROL_OFF;
957
38.0k
            loader_strncpy(props.info.layerName, VK_MAX_EXTENSION_NAME_SIZE, layer_config->name, VK_MAX_EXTENSION_NAME_SIZE);
958
38.0k
            props.info.layerName[VK_MAX_EXTENSION_NAME_SIZE - 1] = '\0';
959
38.0k
            res = loader_copy_to_new_str(inst, layer_config->path, &props.manifest_file_name);
960
38.0k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
961
0
                goto out;
962
0
            }
963
38.0k
            res = loader_append_layer_property(inst, settings_layers, &props);
964
38.0k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
965
0
                loader_free_layer_properties(inst, &props);
966
0
                goto out;
967
0
            }
968
38.0k
            continue;
969
38.0k
        }
970
971
        // The special layer location that indicates where unordered layers should go only should have the
972
        // settings_control_value set - everything else should be NULL
973
71.0k
        if (layer_config->control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
974
55.0k
            struct loader_layer_properties props = {0};
975
55.0k
            props.settings_control_value = LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION;
976
55.0k
            res = loader_append_layer_property(inst, settings_layers, &props);
977
55.0k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
978
0
                loader_free_layer_properties(inst, &props);
979
0
                goto out;
980
0
            }
981
55.0k
            *should_search_for_other_layers = true;
982
55.0k
            continue;
983
55.0k
        }
984
985
16.0k
        if (layer_config->path == NULL) {
986
0
            continue;
987
0
        }
988
989
16.0k
        if (!is_json(layer_config->path, strlen(layer_config->path))) {
990
13.6k
            continue;
991
13.6k
        }
992
993
2.36k
        cJSON* json = NULL;
994
2.36k
        VkResult local_res = loader_get_json(inst, layer_config->path, &json);
995
2.36k
        if (VK_ERROR_OUT_OF_HOST_MEMORY == local_res) {
996
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
997
0
            goto out;
998
2.36k
        } else if (VK_SUCCESS != local_res || NULL == json) {
999
2.10k
            continue;
1000
2.10k
        }
1001
1002
        // Makes it possible to know if a new layer was added or not, since the only return value is VkResult
1003
266
        uint32_t count_before_adding = settings_layers->count;
1004
1005
266
        local_res =
1006
266
            loader_add_layer_properties(inst, settings_layers, json, layer_config->treat_as_implicit_manifest, layer_config->path);
1007
266
        loader_cJSON_Delete(json);
1008
1009
        // If the error is anything other than out of memory we still want to try to load the other layers
1010
266
        if (VK_ERROR_OUT_OF_HOST_MEMORY == local_res) {
1011
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
1012
0
            goto out;
1013
266
        } else if (local_res != VK_SUCCESS || count_before_adding == settings_layers->count) {
1014
            // Indicates something was wrong with the layer, can't add it to the list
1015
200
            continue;
1016
200
        }
1017
1018
1.99k
        for (uint32_t j = count_before_adding; j < settings_layers->count; j++) {
1019
1.92k
            struct loader_layer_properties* newly_added_layer = &settings_layers->list[j];
1020
1.92k
            newly_added_layer->settings_control_value = layer_config->control;
1021
            // If the manifest file found has a name that differs from the one in the settings, remove this layer from
1022
            // consideration
1023
1.92k
            if (strncmp(newly_added_layer->info.layerName, layer_config->name, VK_MAX_EXTENSION_NAME_SIZE) != 0) {
1024
483
                loader_remove_layer_in_list(inst, settings_layers, j);
1025
483
                j--;
1026
483
                continue;
1027
483
            }
1028
1.92k
            bool should_remove = false;
1029
            // Make sure the layer isn't already in the list
1030
963k
            for (uint32_t k = 0; k < j; k++) {
1031
961k
                if (0 == strncmp(settings_layers->list[k].info.layerName, newly_added_layer->info.layerName,
1032
961k
                                 VK_MAX_EXTENSION_NAME_SIZE)) {
1033
959k
                    if (0 == (newly_added_layer->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER) &&
1034
2.31k
                        settings_layers->list[k].lib_name != NULL && newly_added_layer->lib_name != NULL &&
1035
109
                        strcmp(settings_layers->list[k].lib_name, newly_added_layer->lib_name) == 0) {
1036
13
                        should_remove = true;
1037
13
                        break;
1038
13
                    }
1039
959k
                }
1040
961k
            }
1041
1.44k
            if (should_remove) {
1042
13
                loader_remove_layer_in_list(inst, settings_layers, j);
1043
13
                j--;
1044
13
            }
1045
1.44k
        }
1046
66
    }
1047
1048
11.9k
out:
1049
11.9k
    release_current_settings_lock(inst);
1050
11.9k
    return res;
1051
3.83k
}
1052
1053
// Check if layers has an element with the same name.
1054
// LAYER_CONTROL_OFF layers are missing some fields, just make sure the layerName is the same
1055
// If layer_property is a meta layer, just make sure the layerName is the same
1056
// Skip comparing to UNORDERED_LAYER_LOCATION
1057
// If layer_property is a regular layer, check if the lib_path is the same.
1058
// Make sure that the lib_name pointers are non-null before calling strcmp.
1059
// consider_lib_path - true if comparison should include the library_path in distinguishing layers. Used to prevent duplicates
1060
// between settings file provided layers and default found layers
1061
bool check_if_layer_is_in_list(struct loader_layer_list* layer_list, struct loader_layer_properties* layer_property,
1062
16.8k
                               bool consider_lib_path) {
1063
    // If the layer is a meta layer, just check against the name
1064
19.0M
    for (uint32_t i = 0; i < layer_list->count; i++) {
1065
19.0M
        if (0 == strncmp(layer_list->list[i].info.layerName, layer_property->info.layerName, VK_MAX_EXTENSION_NAME_SIZE)) {
1066
19.0M
            if (layer_list->list[i].settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
1067
2.49k
                return true;
1068
2.49k
            }
1069
19.0M
            if (VK_LAYER_TYPE_FLAG_META_LAYER == (layer_property->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER)) {
1070
109
                return true;
1071
109
            }
1072
19.0M
            if (!consider_lib_path) {
1073
328
                return true;
1074
328
            }
1075
19.0M
            if (layer_list->list[i].lib_name && layer_property->lib_name) {
1076
18
                return strcmp(layer_list->list[i].lib_name, layer_property->lib_name) == 0;
1077
18
            }
1078
19.0M
        }
1079
19.0M
    }
1080
13.9k
    return false;
1081
16.8k
}
1082
1083
VkResult combine_settings_layers_with_regular_layers(const struct loader_instance* inst, struct loader_layer_list* settings_layers,
1084
                                                     struct loader_layer_list* regular_layers,
1085
10.0k
                                                     struct loader_layer_list* output_layers) {
1086
10.0k
    VkResult res = VK_SUCCESS;
1087
10.0k
    bool has_unordered_layer_location = false;
1088
10.0k
    uint32_t unordered_layer_location_index = 0;
1089
    // Location to put layers that aren't known to the settings file
1090
    // Find it here so we dont have to pass in a loader_settings struct
1091
15.7k
    for (uint32_t i = 0; i < settings_layers->count; i++) {
1092
6.65k
        if (settings_layers->list[i].settings_control_value == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
1093
975
            has_unordered_layer_location = true;
1094
975
            unordered_layer_location_index = i;
1095
975
            break;
1096
975
        }
1097
6.65k
    }
1098
1099
10.0k
    if (settings_layers->count == 0 && regular_layers->count == 0) {
1100
        // No layers to combine
1101
7.74k
        goto out;
1102
7.74k
    } else if (settings_layers->count == 0) {
1103
        // No settings layers - just copy regular to output_layers - memset regular layers to prevent double frees
1104
1.32k
        *output_layers = *regular_layers;
1105
1.32k
        memset(regular_layers, 0, sizeof(struct loader_layer_list));
1106
1.32k
        goto out;
1107
1.32k
    } else if (regular_layers->count == 0 || !has_unordered_layer_location) {
1108
        // No regular layers or has_unordered_layer_location is false - just copy settings to output_layers -
1109
        // memset settings layers to prevent double frees
1110
727
        *output_layers = *settings_layers;
1111
727
        memset(settings_layers, 0, sizeof(struct loader_layer_list));
1112
727
        goto out;
1113
727
    }
1114
1115
248
    res = loader_init_generic_list(inst, (struct loader_generic_list*)output_layers,
1116
248
                                   (settings_layers->count + regular_layers->count) * sizeof(struct loader_layer_properties));
1117
248
    if (VK_SUCCESS != res) {
1118
0
        goto out;
1119
0
    }
1120
1121
    // Insert the settings layers into output_layers up to unordered_layer_index
1122
1.86k
    for (uint32_t i = 0; i < unordered_layer_location_index; i++) {
1123
1.61k
        if (!check_if_layer_is_in_list(output_layers, &settings_layers->list[i], true)) {
1124
357
            res = loader_append_layer_property(inst, output_layers, &settings_layers->list[i]);
1125
357
            if (VK_SUCCESS != res) {
1126
0
                goto out;
1127
0
            }
1128
357
        }
1129
1.61k
    }
1130
1131
1.12k
    for (uint32_t i = 0; i < regular_layers->count; i++) {
1132
        // Check if its already been put in the output_layers list as well as the remaining settings_layers
1133
872
        bool regular_layer_is_ordered = check_if_layer_is_in_list(output_layers, &regular_layers->list[i], false) ||
1134
739
                                        check_if_layer_is_in_list(settings_layers, &regular_layers->list[i], false);
1135
        // If it isn't found, add it
1136
872
        if (!regular_layer_is_ordered) {
1137
437
            res = loader_append_layer_property(inst, output_layers, &regular_layers->list[i]);
1138
437
            if (VK_SUCCESS != res) {
1139
0
                goto out;
1140
0
            }
1141
437
        } else {
1142
            // layer is already ordered and can be safely freed
1143
435
            loader_free_layer_properties(inst, &regular_layers->list[i]);
1144
435
        }
1145
872
    }
1146
1147
    // Insert the rest of the settings layers into combined_layers from  unordered_layer_index to the end
1148
    // start at one after the unordered_layer_index
1149
13.8k
    for (uint32_t i = unordered_layer_location_index + 1; i < settings_layers->count; i++) {
1150
13.6k
        if (!check_if_layer_is_in_list(output_layers, &settings_layers->list[i], true)) {
1151
12.3k
            res = loader_append_layer_property(inst, output_layers, &settings_layers->list[i]);
1152
12.3k
            if (VK_SUCCESS != res) {
1153
0
                goto out;
1154
0
            }
1155
12.3k
        }
1156
13.6k
    }
1157
1158
10.0k
out:
1159
10.0k
    if (res != VK_SUCCESS) {
1160
0
        loader_delete_layer_list_and_properties(inst, output_layers);
1161
0
    }
1162
1163
10.0k
    return res;
1164
248
}
1165
1166
VkResult enable_correct_layers_from_settings(const struct loader_instance* inst, const struct loader_envvar_all_filters* filters,
1167
                                             uint32_t app_enabled_name_count, const char* const* app_enabled_names,
1168
                                             const struct loader_layer_list* instance_layers,
1169
                                             struct loader_pointer_layer_list* target_layer_list,
1170
0
                                             struct loader_pointer_layer_list* activated_layer_list) {
1171
0
    VkResult res = VK_SUCCESS;
1172
0
    char* vk_instance_layers_env = loader_getenv(ENABLED_LAYERS_ENV, inst);
1173
0
    size_t vk_instance_layers_env_len = 0;
1174
0
    char* vk_instance_layers_env_copy = NULL;
1175
0
    if (vk_instance_layers_env != NULL) {
1176
0
        vk_instance_layers_env_len = strlen(vk_instance_layers_env) + 1;
1177
0
        vk_instance_layers_env_copy = loader_stack_alloc(vk_instance_layers_env_len);
1178
0
        memset(vk_instance_layers_env_copy, 0, vk_instance_layers_env_len);
1179
1180
0
        loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0, "env var \'%s\' defined and adding layers: %s",
1181
0
                   ENABLED_LAYERS_ENV, vk_instance_layers_env);
1182
0
    }
1183
0
    for (uint32_t i = 0; i < instance_layers->count; i++) {
1184
0
        bool enable_layer = false;
1185
0
        struct loader_layer_properties* props = &instance_layers->list[i];
1186
1187
        // Skip the sentinel unordered layer location
1188
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
1189
0
            continue;
1190
0
        }
1191
1192
        // Do not enable the layer if the settings have it set as off
1193
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
1194
0
            continue;
1195
0
        }
1196
        // Force enable it based on settings
1197
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_ON) {
1198
0
            enable_layer = true;
1199
0
            props->enabled_by_what = ENABLED_BY_WHAT_LOADER_SETTINGS_FILE;
1200
0
        } else {
1201
            // Check if disable filter needs to skip the layer
1202
0
            if ((filters->disable_filter.disable_all || filters->disable_filter.disable_all_implicit ||
1203
0
                 check_name_matches_filter_environment_var(props->info.layerName, &filters->disable_filter.additional_filters)) &&
1204
0
                !check_name_matches_filter_environment_var(props->info.layerName, &filters->allow_filter)) {
1205
                // Report a message that we've forced off a layer if it would have been enabled normally.
1206
0
                loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0,
1207
0
                           "Layer \"%s\" forced disabled because name matches filter of env var \'%s\'.", props->info.layerName,
1208
0
                           VK_LAYERS_DISABLE_ENV_VAR);
1209
1210
0
                continue;
1211
0
            }
1212
0
        }
1213
        // Check the enable filter
1214
0
        if (!enable_layer && check_name_matches_filter_environment_var(props->info.layerName, &filters->enable_filter)) {
1215
0
            enable_layer = true;
1216
0
            props->enabled_by_what = ENABLED_BY_WHAT_VK_LOADER_LAYERS_ENABLE;
1217
0
            loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0,
1218
0
                       "Layer \"%s\" forced enabled due to env var \'%s\'.", props->info.layerName, VK_LAYERS_ENABLE_ENV_VAR);
1219
0
        }
1220
1221
        // First look for the old-fashion layers forced on with VK_INSTANCE_LAYERS
1222
0
        if (!enable_layer && vk_instance_layers_env && vk_instance_layers_env_copy && vk_instance_layers_env_len > 0) {
1223
            // Copy the env-var on each iteration, so that loader_get_next_path can correctly find the separators
1224
            // This solution only needs one stack allocation ahead of time rather than an allocation per layer in the
1225
            // env-var
1226
0
            loader_strncpy(vk_instance_layers_env_copy, vk_instance_layers_env_len, vk_instance_layers_env,
1227
0
                           vk_instance_layers_env_len);
1228
1229
0
            char* instance_layers_env_iter = vk_instance_layers_env_copy;
1230
0
            while (instance_layers_env_iter && *instance_layers_env_iter) {
1231
0
                char* next = loader_get_next_path(instance_layers_env_iter);
1232
0
                if (0 == strcmp(instance_layers_env_iter, props->info.layerName)) {
1233
0
                    enable_layer = true;
1234
0
                    props->enabled_by_what = ENABLED_BY_WHAT_VK_INSTANCE_LAYERS;
1235
0
                    break;
1236
0
                }
1237
0
                instance_layers_env_iter = next;
1238
0
            }
1239
0
        }
1240
1241
        // Check if it should be enabled by the application
1242
0
        if (!enable_layer) {
1243
0
            for (uint32_t j = 0; j < app_enabled_name_count; j++) {
1244
0
                if (strcmp(props->info.layerName, app_enabled_names[j]) == 0) {
1245
0
                    enable_layer = true;
1246
0
                    props->enabled_by_what = ENABLED_BY_WHAT_IN_APPLICATION_API;
1247
0
                    break;
1248
0
                }
1249
0
            }
1250
0
        }
1251
1252
        // Check if its an implicit layers and thus enabled by default
1253
0
        if (!enable_layer && (0 == (props->type_flags & VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER)) &&
1254
0
            loader_implicit_layer_is_enabled(inst, filters, props)) {
1255
0
            enable_layer = true;
1256
0
            props->enabled_by_what = ENABLED_BY_WHAT_IMPLICIT_LAYER;
1257
0
        }
1258
1259
0
        if (enable_layer) {
1260
            // Check if the layer is a meta layer reuse the existing function to add the meta layer
1261
0
            if (props->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER) {
1262
0
                res = loader_add_meta_layer(inst, filters, props, target_layer_list, activated_layer_list, instance_layers, NULL);
1263
0
                if (res == VK_ERROR_OUT_OF_HOST_MEMORY) goto out;
1264
0
            } else {
1265
0
                res = loader_add_layer_properties_to_list(inst, target_layer_list, props);
1266
0
                if (res != VK_SUCCESS) {
1267
0
                    goto out;
1268
0
                }
1269
0
                res = loader_add_layer_properties_to_list(inst, activated_layer_list, props);
1270
0
                if (res != VK_SUCCESS) {
1271
0
                    goto out;
1272
0
                }
1273
0
            }
1274
0
        }
1275
0
    }
1276
0
out:
1277
0
    if (vk_instance_layers_env != NULL) {
1278
0
        loader_free_getenv(vk_instance_layers_env, inst);
1279
0
    }
1280
1281
0
    return res;
1282
0
}
1283
1284
0
VkResult loader_settings_get_additional_driver_files(const struct loader_instance* inst, struct loader_string_list* out_files) {
1285
0
    VkResult res = VK_SUCCESS;
1286
1287
0
    const loader_settings* settings = get_current_settings_and_lock(inst);
1288
1289
0
    if (NULL == settings || !settings->settings_active) {
1290
0
        goto out;
1291
0
    }
1292
1293
0
    if (settings->additional_drivers_use_exclusively) {
1294
0
        free_string_list(inst, out_files);
1295
0
    }
1296
1297
0
    for (uint32_t i = 0; i < settings->additional_driver_count; i++) {
1298
0
        res = prepend_if_manifest_file(inst, settings->additional_drivers[i].path, out_files);
1299
0
    }
1300
1301
0
out:
1302
0
    release_current_settings_lock(inst);
1303
0
    return res;
1304
0
}
1305
1306
0
bool loader_settings_should_use_driver_environment_variables(const struct loader_instance* inst) {
1307
0
    bool should_use = true;
1308
0
    const loader_settings* settings = get_current_settings_and_lock(inst);
1309
0
    if (NULL == settings || !settings->settings_active) {
1310
0
        goto out;
1311
0
    }
1312
0
    if (settings->device_configurations_active) {
1313
0
        should_use = false;
1314
0
    }
1315
0
out:
1316
0
    release_current_settings_lock(inst);
1317
0
    return should_use;
1318
0
}