Coverage Report

Created: 2025-12-14 06:58

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
15.0k
void free_layer_configuration(const struct loader_instance* inst, loader_settings_layer_configuration* layer_configuration) {
42
15.0k
    loader_instance_heap_free(inst, layer_configuration->name);
43
15.0k
    loader_instance_heap_free(inst, layer_configuration->path);
44
15.0k
    memset(layer_configuration, 0, sizeof(loader_settings_layer_configuration));
45
15.0k
}
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
6.52k
void free_loader_settings(const struct loader_instance* inst, loader_settings* settings) {
58
6.52k
    if (NULL != settings->layer_configurations) {
59
15.7k
        for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
60
13.9k
            free_layer_configuration(inst, &settings->layer_configurations[i]);
61
13.9k
        }
62
1.80k
        loader_instance_heap_free(inst, settings->layer_configurations);
63
1.80k
    }
64
6.52k
    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
6.52k
    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
6.52k
    loader_instance_heap_free(inst, settings->settings_file_path);
77
6.52k
    memset(settings, 0, sizeof(loader_settings));
78
6.52k
}
79
80
14.1k
loader_settings_layer_control parse_control_string(char* control_string) {
81
14.1k
    loader_settings_layer_control layer_control = LOADER_SETTINGS_LAYER_CONTROL_DEFAULT;
82
14.1k
    if (strcmp(control_string, "auto") == 0)
83
2.85k
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_DEFAULT;
84
11.3k
    else if (strcmp(control_string, "on") == 0)
85
91
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_ON;
86
11.2k
    else if (strcmp(control_string, "off") == 0)
87
1.09k
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_OFF;
88
10.1k
    else if (strcmp(control_string, "unordered_layer_location") == 0)
89
791
        layer_control = LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION;
90
14.1k
    return layer_control;
91
14.1k
}
92
93
13.9k
const char* loader_settings_layer_control_to_string(loader_settings_layer_control control) {
94
13.9k
    switch (control) {
95
12.0k
        case (LOADER_SETTINGS_LAYER_CONTROL_DEFAULT):
96
12.0k
            return "auto";
97
78
        case (LOADER_SETTINGS_LAYER_CONTROL_ON):
98
78
            return "on";
99
1.07k
        case (LOADER_SETTINGS_LAYER_CONTROL_OFF):
100
1.07k
            return "off";
101
789
        case (LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION):
102
789
            return "unordered_layer_location";
103
0
        default:
104
0
            return "UNKNOWN_LAYER_CONTROl";
105
13.9k
    }
106
13.9k
}
107
108
328
uint32_t parse_log_filters_from_strings(struct loader_string_list* log_filters) {
109
328
    uint32_t filters = 0;
110
4.96k
    for (uint32_t i = 0; i < log_filters->count; i++) {
111
4.63k
        if (strcmp(log_filters->list[i], "all") == 0)
112
86
            filters |= VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_PERF_BIT | VULKAN_LOADER_ERROR_BIT |
113
86
                       VULKAN_LOADER_DEBUG_BIT | VULKAN_LOADER_LAYER_BIT | VULKAN_LOADER_DRIVER_BIT | VULKAN_LOADER_VALIDATION_BIT;
114
4.55k
        else if (strcmp(log_filters->list[i], "info") == 0)
115
222
            filters |= VULKAN_LOADER_INFO_BIT;
116
4.32k
        else if (strcmp(log_filters->list[i], "warn") == 0)
117
484
            filters |= VULKAN_LOADER_WARN_BIT;
118
3.84k
        else if (strcmp(log_filters->list[i], "perf") == 0)
119
375
            filters |= VULKAN_LOADER_PERF_BIT;
120
3.46k
        else if (strcmp(log_filters->list[i], "error") == 0)
121
50
            filters |= VULKAN_LOADER_ERROR_BIT;
122
3.41k
        else if (strcmp(log_filters->list[i], "debug") == 0)
123
149
            filters |= VULKAN_LOADER_DEBUG_BIT;
124
3.27k
        else if (strcmp(log_filters->list[i], "layer") == 0)
125
141
            filters |= VULKAN_LOADER_LAYER_BIT;
126
3.12k
        else if (strcmp(log_filters->list[i], "driver") == 0)
127
144
            filters |= VULKAN_LOADER_DRIVER_BIT;
128
2.98k
        else if (strcmp(log_filters->list[i], "validation") == 0)
129
34
            filters |= VULKAN_LOADER_VALIDATION_BIT;
130
4.63k
    }
131
328
    return filters;
132
328
}
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
14.2k
                                   loader_settings_layer_configuration* layer_configuration) {
148
14.2k
    char* control_string = NULL;
149
14.2k
    VkResult res = loader_parse_json_string(layer_configuration_json, "control", &control_string);
150
14.2k
    if (res != VK_SUCCESS) {
151
15
        goto out;
152
15
    }
153
14.1k
    layer_configuration->control = parse_control_string(control_string);
154
14.1k
    loader_instance_heap_free(inst, control_string);
155
156
    // If that is the only value - do no further parsing
157
14.1k
    if (layer_configuration->control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
158
791
        goto out;
159
791
    }
160
161
13.4k
    res = loader_parse_json_string(layer_configuration_json, "name", &(layer_configuration->name));
162
13.4k
    if (res != VK_SUCCESS) {
163
116
        goto out;
164
116
    }
165
166
13.2k
    res = loader_parse_json_string(layer_configuration_json, "path", &(layer_configuration->path));
167
13.2k
    if (res != VK_SUCCESS) {
168
20
        goto out;
169
20
    }
170
171
13.2k
    cJSON* treat_as_implicit_manifest = loader_cJSON_GetObjectItem(layer_configuration_json, "treat_as_implicit_manifest");
172
13.2k
    if (treat_as_implicit_manifest && treat_as_implicit_manifest->type == cJSON_True) {
173
629
        layer_configuration->treat_as_implicit_manifest = true;
174
629
    }
175
14.2k
out:
176
14.2k
    if (VK_SUCCESS != res) {
177
151
        free_layer_configuration(inst, layer_configuration);
178
151
    }
179
14.2k
    return res;
180
13.2k
}
181
182
2.23k
VkResult parse_layer_configurations(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
183
2.23k
    VkResult res = VK_SUCCESS;
184
185
2.23k
    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
2.23k
    if (NULL == layer_configurations) {
188
261
        return VK_SUCCESS;
189
261
    }
190
191
1.97k
    uint32_t layer_configurations_count = loader_cJSON_GetArraySize(layer_configurations);
192
1.97k
    if (layer_configurations_count == 0) {
193
11
        loader_settings->layer_configurations_active = true;
194
11
        return VK_SUCCESS;
195
11
    }
196
197
1.96k
    loader_settings->layer_configuration_count = layer_configurations_count;
198
199
1.96k
    loader_settings->layer_configurations = loader_instance_heap_calloc(
200
1.96k
        inst, sizeof(loader_settings_layer_configuration) * layer_configurations_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
201
1.96k
    if (NULL == loader_settings->layer_configurations) {
202
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
203
0
        goto out;
204
0
    }
205
206
1.96k
    cJSON* layer = NULL;
207
1.96k
    size_t i = 0;
208
14.2k
    cJSON_ArrayForEach(layer, layer_configurations) {
209
14.2k
        if (layer->type != cJSON_Object) {
210
10
            res = VK_ERROR_INITIALIZATION_FAILED;
211
10
            goto out;
212
10
        }
213
14.2k
        res = parse_layer_configuration(inst, layer, &(loader_settings->layer_configurations[i++]));
214
14.2k
        if (VK_SUCCESS != res) {
215
151
            goto out;
216
151
        }
217
14.2k
    }
218
1.80k
    loader_settings->layer_configurations_active = true;
219
1.96k
out:
220
1.96k
    if (res != VK_SUCCESS) {
221
161
        if (loader_settings->layer_configurations) {
222
1.13k
            for (size_t index = 0; index < loader_settings->layer_configuration_count; index++) {
223
974
                free_layer_configuration(inst, &(loader_settings->layer_configurations[index]));
224
974
            }
225
161
            loader_settings->layer_configuration_count = 0;
226
161
            loader_instance_heap_free(inst, loader_settings->layer_configurations);
227
161
            loader_settings->layer_configurations = NULL;
228
161
        }
229
161
    }
230
231
1.96k
    return res;
232
1.80k
}
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
2.23k
VkResult parse_additional_drivers(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
249
2.23k
    VkResult res = VK_SUCCESS;
250
251
2.23k
    cJSON* additional_drivers_use_exclusively_json =
252
2.23k
        loader_cJSON_GetObjectItem(settings_object, "additional_drivers_use_exclusively");
253
2.23k
    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
2.23k
    cJSON* additional_drivers_json = loader_cJSON_GetObjectItem(settings_object, "additional_drivers");
258
2.23k
    if (NULL == additional_drivers_json) {
259
2.23k
        return VK_SUCCESS;
260
2.23k
    }
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_layer_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
2.23k
VkResult parse_device_configurations(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
376
2.23k
    VkResult res = VK_SUCCESS;
377
378
2.23k
    cJSON* device_configurations = loader_cJSON_GetObjectItem(settings_object, "device_configurations");
379
2.23k
    if (NULL == device_configurations) {
380
2.23k
        return VK_SUCCESS;
381
2.23k
    }
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
0
            continue;
410
0
        }
411
0
        i++;
412
0
    }
413
0
    loader_settings->device_configurations_active = true;
414
0
out:
415
0
    if (res != VK_SUCCESS) {
416
0
        if (loader_settings->device_configurations) {
417
0
            for (size_t index = 0; index < loader_settings->device_configuration_count; index++) {
418
0
                free_device_configuration(inst, &(loader_settings->device_configurations[index]));
419
0
            }
420
0
            loader_settings->device_configuration_count = 0;
421
0
            loader_instance_heap_free(inst, loader_settings->device_configurations);
422
0
            loader_settings->device_configurations = NULL;
423
0
        }
424
0
    }
425
0
    return res;
426
0
}
427
428
#if COMMON_UNIX_PLATFORMS
429
// Given a base and suffix path, determine if a file at that location exists, and if it is return success.
430
// Since base may contain multiple paths separated by PATH_SEPARATOR, we must extract each segment and check segment + suffix
431
// individually
432
VkResult check_if_settings_path_exists(const struct loader_instance* inst, const char* base, const char* suffix,
433
29.1k
                                       char** settings_file_path) {
434
29.1k
    if (NULL == base || NULL == suffix) {
435
13.0k
        return VK_ERROR_INITIALIZATION_FAILED;
436
13.0k
    }
437
438
16.1k
    size_t base_len = strlen(base);
439
16.1k
    size_t suffix_len = strlen(suffix);
440
441
16.1k
    uint32_t start = 0;
442
16.1k
    uint32_t stop = 0;
443
27.2k
    while (base[start] != '\0' && start < base_len && stop < base_len) {
444
16.8k
        start = stop;
445
16.8k
        stop = start + 1;
446
92.9k
        while (base[stop] != PATH_SEPARATOR && base[stop] != '\0' && stop < base_len) {
447
76.0k
            stop++;
448
76.0k
        }
449
450
16.8k
        size_t segment_len = (stop - start);
451
16.8k
        if (segment_len <= 1) {
452
            // segment is *just* a PATH_SEPARATOR, skip it
453
0
            continue;
454
0
        }
455
16.8k
        size_t path_len = segment_len + suffix_len + 1;
456
16.8k
        *settings_file_path = loader_instance_heap_calloc(inst, path_len, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
457
16.8k
        if (NULL == *settings_file_path) {
458
0
            return VK_ERROR_OUT_OF_HOST_MEMORY;
459
0
        }
460
16.8k
        loader_strncpy(*settings_file_path, path_len, base + start, segment_len);
461
16.8k
        loader_strncat(*settings_file_path, path_len, suffix, suffix_len);
462
463
16.8k
        if (loader_platform_file_exists(*settings_file_path)) {
464
5.75k
            return VK_SUCCESS;
465
5.75k
        }
466
11.1k
        loader_instance_heap_free(inst, *settings_file_path);
467
11.1k
        *settings_file_path = NULL;
468
11.1k
    }
469
470
10.3k
    return VK_ERROR_INITIALIZATION_FAILED;
471
16.1k
}
472
473
// Follow the logic of read_data_files_in_search_paths but only look for "/vulkan/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME
474
6.52k
VkResult get_unix_settings_path(const struct loader_instance* inst, char** settings_file_path) {
475
    // First, get XDG env-vars we use. Don't need to worry about free'ing because on linux getenv is non-allocating
476
6.52k
    char* xdg_config_home = loader_secure_getenv("XDG_CONFIG_HOME", inst);
477
6.52k
    char* xdg_config_dirs = loader_secure_getenv("XDG_CONFIG_DIRS", inst);
478
6.52k
    char* xdg_data_home = loader_secure_getenv("XDG_DATA_HOME", inst);
479
6.52k
    char* xdg_data_dirs = loader_secure_getenv("XDG_DATA_DIRS", inst);
480
481
    // Use fallback directories for xdg_config_dirs and xdg_data_dirs if they are NULL.
482
6.52k
#if !defined(__Fuchsia__) && !defined(__QNX__)
483
6.52k
    if (NULL == xdg_config_dirs || '\0' == xdg_config_dirs[0]) {
484
6.52k
        xdg_config_dirs = FALLBACK_CONFIG_DIRS;
485
6.52k
    }
486
6.52k
#endif
487
488
6.52k
#if !defined(__Fuchsia__) && !defined(__QNX__)
489
6.52k
    if (NULL == xdg_data_dirs || '\0' == xdg_data_dirs[0]) {
490
6.52k
        xdg_data_dirs = FALLBACK_DATA_DIRS;
491
6.52k
    }
492
6.52k
#endif
493
494
6.52k
    VkResult res = check_if_settings_path_exists(
495
6.52k
        inst, xdg_config_home, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
496
6.52k
    if (res == VK_SUCCESS) {
497
0
        return res;
498
0
    }
499
500
6.52k
    res = check_if_settings_path_exists(inst, xdg_data_home, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
501
6.52k
                                        settings_file_path);
502
6.52k
    if (res == VK_SUCCESS) {
503
0
        return res;
504
0
    }
505
506
    // Check home if either xdg_config_home or xdg_data_home wasn't set
507
6.52k
    char* home = loader_secure_getenv("HOME", inst);
508
6.52k
    if (home != NULL) {
509
6.52k
        if (NULL == xdg_config_home || '\0' == xdg_config_home[0]) {
510
6.52k
            res = check_if_settings_path_exists(
511
6.52k
                inst, home, "/.config/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
512
6.52k
            if (res == VK_SUCCESS) {
513
0
                return res;
514
0
            }
515
6.52k
        }
516
6.52k
        if (NULL == xdg_data_home || '\0' == xdg_data_home[0]) {
517
6.52k
            res = check_if_settings_path_exists(
518
6.52k
                inst, home, "/.local/share/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
519
6.52k
            if (res == VK_SUCCESS) {
520
5.75k
                return res;
521
5.75k
            }
522
6.52k
        }
523
6.52k
    }
524
525
770
    res = check_if_settings_path_exists(inst, xdg_config_dirs, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
526
770
                                        settings_file_path);
527
770
    if (res == VK_SUCCESS) {
528
0
        return res;
529
0
    }
530
531
770
    res = check_if_settings_path_exists(inst, SYSCONFDIR, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
532
770
                                        settings_file_path);
533
770
    if (res == VK_SUCCESS) {
534
0
        return res;
535
0
    }
536
770
#if defined(EXTRASYSCONFDIR)
537
538
770
    res = check_if_settings_path_exists(inst, EXTRASYSCONFDIR, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
539
770
                                        settings_file_path);
540
770
    if (res == VK_SUCCESS) {
541
0
        return res;
542
0
    }
543
770
#endif
544
770
    res = check_if_settings_path_exists(inst, xdg_data_dirs, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
545
770
                                        settings_file_path);
546
770
    if (res == VK_SUCCESS) {
547
0
        return res;
548
0
    }
549
550
770
    return VK_ERROR_INITIALIZATION_FAILED;
551
770
}
552
#endif
553
554
0
bool check_if_layer_configurations_are_equal(loader_settings_layer_configuration* a, loader_settings_layer_configuration* b) {
555
0
    if (!a->name || !b->name || 0 != strcmp(a->name, b->name)) {
556
0
        return false;
557
0
    }
558
0
    if (!a->path || !b->path || 0 != strcmp(a->path, b->path)) {
559
0
        return false;
560
0
    }
561
0
    return a->control == b->control;
562
0
}
563
564
0
bool check_if_driver_configurations_are_equal(loader_settings_driver_configuration* a, loader_settings_driver_configuration* b) {
565
0
    if (!a->path || !b->path || 0 != strcmp(a->path, b->path)) {
566
0
        return false;
567
0
    }
568
0
    return true;
569
0
}
570
571
0
bool check_if_device_configurations_are_equal(loader_settings_device_configuration* a, loader_settings_device_configuration* b) {
572
0
    for (uint32_t i = 0; i < VK_UUID_SIZE; i++) {
573
0
        if (a->deviceUUID[i] != b->deviceUUID[i]) return false;
574
0
    }
575
0
    for (uint32_t i = 0; i < VK_UUID_SIZE; i++) {
576
0
        if (a->driverUUID[i] != b->driverUUID[i]) return false;
577
0
    }
578
0
    if (a->driverVersion != b->driverVersion) return false;
579
0
    return true;
580
0
}
581
582
0
bool check_if_settings_are_equal(loader_settings* a, loader_settings* b) {
583
    // If either pointer is null, return true
584
0
    if (NULL == a || NULL == b) return false;
585
0
    bool are_equal = true;
586
0
    are_equal &= a->settings_active == b->settings_active;
587
0
    are_equal &= a->has_unordered_layer_location == b->has_unordered_layer_location;
588
0
    are_equal &= a->debug_level == b->debug_level;
589
0
    are_equal &= a->layer_configurations_active == b->layer_configurations_active;
590
0
    are_equal &= a->layer_configuration_count == b->layer_configuration_count;
591
0
    are_equal &= a->additional_driver_count == b->additional_driver_count;
592
0
    are_equal &= a->device_configurations_active == b->device_configurations_active;
593
0
    are_equal &= a->device_configuration_count == b->device_configuration_count;
594
0
    if (!are_equal) return false;
595
0
    for (uint32_t i = 0; i < a->layer_configuration_count && i < b->layer_configuration_count; i++) {
596
0
        are_equal &= check_if_layer_configurations_are_equal(&a->layer_configurations[i], &b->layer_configurations[i]);
597
0
    }
598
0
    for (uint32_t i = 0; i < a->additional_driver_count && i < b->additional_driver_count; i++) {
599
0
        are_equal &= check_if_driver_configurations_are_equal(&a->additional_drivers[i], &b->additional_drivers[i]);
600
0
    }
601
0
    for (uint32_t i = 0; i < a->device_configuration_count && i < b->device_configuration_count; i++) {
602
0
        are_equal &= check_if_device_configurations_are_equal(&a->device_configurations[i], &b->device_configurations[i]);
603
0
    }
604
0
    return are_equal;
605
0
}
606
607
1.89k
void log_settings(const struct loader_instance* inst, loader_settings* settings) {
608
1.89k
    if (settings == NULL) {
609
0
        return;
610
0
    }
611
1.89k
    loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "Using layer configurations found in loader settings from %s",
612
1.89k
               settings->settings_file_path);
613
614
1.89k
    char cmd_line_msg[64] = {0};
615
1.89k
    size_t cmd_line_size = sizeof(cmd_line_msg);
616
617
1.89k
    cmd_line_msg[0] = '\0';
618
619
1.89k
    generate_debug_flag_str(settings->debug_level, cmd_line_size, cmd_line_msg);
620
1.89k
    if (strlen(cmd_line_msg)) {
621
168
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Loader Settings Filters for Logging to Standard Error: %s", cmd_line_msg);
622
168
    }
623
1.89k
    if (settings->layer_configurations_active) {
624
1.81k
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Layer Configurations count = %d", settings->layer_configuration_count);
625
15.7k
        for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
626
13.9k
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---- Layer Configuration [%d] ----", i);
627
13.9k
            if (settings->layer_configurations[i].control != LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
628
13.1k
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Name: %s", settings->layer_configurations[i].name);
629
13.1k
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Path: %s", settings->layer_configurations[i].path);
630
13.1k
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Layer Type: %s",
631
13.1k
                           settings->layer_configurations[i].treat_as_implicit_manifest ? "Implicit" : "Explicit");
632
13.1k
            }
633
13.9k
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Control: %s",
634
13.9k
                       loader_settings_layer_control_to_string(settings->layer_configurations[i].control));
635
13.9k
        }
636
1.81k
    }
637
1.89k
    if (settings->additional_driver_count > 0) {
638
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "----");
639
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Use Additional Drivers Exclusively = %s",
640
0
                   settings->additional_drivers_use_exclusively ? "true" : "false");
641
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Additional Driver Configurations count = %d",
642
0
                   settings->additional_driver_count);
643
0
        for (uint32_t i = 0; i < settings->additional_driver_count; i++) {
644
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---- Driver Configuration [%d] ----", i);
645
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Path: %s", settings->additional_drivers[i].path);
646
0
        }
647
0
    }
648
1.89k
    if (settings->device_configurations_active) {
649
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "----");
650
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Device Configurations count = %d", settings->device_configuration_count);
651
0
        for (uint32_t i = 0; i < settings->device_configuration_count; i++) {
652
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---- Device Configuration [%d] ----", i);
653
0
            char device_uuid_str[UUID_STR_LEN] = {0};
654
0
            loader_log_generate_uuid_string(settings->device_configurations[i].deviceUUID, device_uuid_str);
655
0
            char driver_uuid_str[UUID_STR_LEN] = {0};
656
0
            loader_log_generate_uuid_string(settings->device_configurations[i].driverUUID, driver_uuid_str);
657
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "deviceUUID: %s", device_uuid_str);
658
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "driverUUID: %s", driver_uuid_str);
659
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "driverVersion: %d", settings->device_configurations[i].driverVersion);
660
0
            if ('\0' != settings->device_configurations[i].deviceName[0]) {
661
0
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "deviceName: %s", settings->device_configurations[i].deviceName);
662
0
            }
663
0
            if ('\0' != settings->device_configurations[i].driverName[0]) {
664
0
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "driverName: %s", settings->device_configurations[i].driverName);
665
0
            }
666
0
        }
667
0
    }
668
1.89k
    loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---------------------------------");
669
1.89k
}
670
671
// Loads the vk_loader_settings.json file
672
// Returns VK_SUCCESS if it was found & was successfully parsed. Otherwise, it returns VK_ERROR_INITIALIZATION_FAILED if it
673
// wasn't found or failed to parse, and returns VK_ERROR_OUT_OF_HOST_MEMORY if it was unable to allocate enough memory.
674
6.52k
VkResult get_loader_settings(const struct loader_instance* inst, loader_settings* loader_settings) {
675
6.52k
    VkResult res = VK_SUCCESS;
676
6.52k
    cJSON* json = NULL;
677
6.52k
    char* file_format_version_string = NULL;
678
6.52k
    char* settings_file_path = NULL;
679
#if defined(WIN32)
680
    res = windows_get_loader_settings_file_path(inst, &settings_file_path);
681
    if (res != VK_SUCCESS) {
682
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
683
                   "No valid vk_loader_settings.json file found, no loader settings will be active");
684
        goto out;
685
    }
686
687
#elif COMMON_UNIX_PLATFORMS
688
    res = get_unix_settings_path(inst, &settings_file_path);
689
6.52k
    if (res != VK_SUCCESS) {
690
770
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
691
770
                   "No valid vk_loader_settings.json file found, no loader settings will be active");
692
770
        goto out;
693
770
    }
694
#else
695
#warning "Unsupported platform - must specify platform specific location for vk_loader_settings.json"
696
#endif
697
698
5.75k
    res = loader_get_json(inst, settings_file_path, &json);
699
    // Make sure sure the top level json value is an object
700
5.75k
    if (res != VK_SUCCESS || NULL == json || json->type != cJSON_Object) {
701
3.00k
        goto out;
702
3.00k
    }
703
704
2.75k
    res = loader_parse_json_string(json, "file_format_version", &file_format_version_string);
705
2.75k
    if (res != VK_SUCCESS) {
706
16
        if (res != VK_ERROR_OUT_OF_HOST_MEMORY) {
707
16
            loader_log(
708
16
                inst, VULKAN_LOADER_DEBUG_BIT, 0,
709
16
                "Loader settings file from %s missing required field file_format_version - no loader settings will be active",
710
16
                settings_file_path);
711
16
        }
712
16
        goto out;
713
16
    }
714
715
    // Because the file may contain either a "settings_array" or a single "settings" object, we need to create a cJSON so that we
716
    // can iterate on both cases with common code
717
2.73k
    cJSON settings_iter_parent = {0};
718
719
2.73k
    cJSON* settings_array = loader_cJSON_GetObjectItem(json, "settings_array");
720
2.73k
    cJSON* single_settings_object = loader_cJSON_GetObjectItem(json, "settings");
721
2.73k
    if (NULL != settings_array) {
722
2.33k
        memcpy(&settings_iter_parent, settings_array, sizeof(cJSON));
723
2.33k
    } else if (NULL != single_settings_object) {
724
14
        settings_iter_parent.child = single_settings_object;
725
390
    } else if (settings_array == NULL && single_settings_object) {
726
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
727
0
                   "Loader settings file from %s missing required settings objects: Either one of the \"settings\" or "
728
0
                   "\"settings_array\" objects must be present - no loader settings will be active",
729
0
                   settings_file_path);
730
0
        res = VK_ERROR_INITIALIZATION_FAILED;
731
0
        goto out;
732
0
    }
733
734
    // Corresponds to the settings object that has no app keys
735
2.73k
    cJSON* global_settings = NULL;
736
    // Corresponds to the settings object which has a matching app key
737
2.73k
    cJSON* settings_to_use = NULL;
738
739
2.73k
    char current_process_path[1024];
740
2.73k
    bool valid_exe_path = NULL != loader_platform_executable_path(current_process_path, 1024);
741
742
2.73k
    cJSON* settings_object_iter = NULL;
743
4.40k
    cJSON_ArrayForEach(settings_object_iter, &settings_iter_parent) {
744
4.40k
        if (settings_object_iter->type != cJSON_Object) {
745
259
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
746
259
                       "Loader settings file from %s has a settings element that is not an object", settings_file_path);
747
259
            break;
748
259
        }
749
750
4.14k
        cJSON* app_keys = loader_cJSON_GetObjectItem(settings_object_iter, "app_keys");
751
4.14k
        if (NULL == app_keys) {
752
            // use the first 'global' settings that has no app keys as the global one
753
3.65k
            if (global_settings == NULL) {
754
2.23k
                global_settings = settings_object_iter;
755
2.23k
            }
756
3.65k
            continue;
757
3.65k
        }
758
        // No sense iterating if we couldn't get the executable path
759
494
        if (!valid_exe_path) {
760
0
            break;
761
0
        }
762
494
        cJSON* app_key = NULL;
763
989
        cJSON_ArrayForEach(app_key, app_keys) {
764
989
            char* app_key_str = loader_cJSON_GetStringValue(app_key);
765
989
            if (app_key_str && strcmp(current_process_path, app_key_str) == 0) {
766
0
                settings_to_use = settings_object_iter;
767
0
                break;
768
0
            }
769
989
        }
770
771
        // Break if we have found a matching current_process_path
772
494
        if (NULL != settings_to_use) {
773
0
            break;
774
0
        }
775
494
    }
776
777
    // No app specific settings match - either use global settings or exit
778
2.73k
    if (settings_to_use == NULL) {
779
2.73k
        if (global_settings == NULL) {
780
498
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
781
498
                       "Loader settings file from %s missing global settings and none of the app specific settings matched the "
782
498
                       "current application - no loader settings will be active",
783
498
                       settings_file_path);
784
498
            goto out;  // No global settings were found - exit
785
2.23k
        } else {
786
2.23k
            settings_to_use = global_settings;  // Global settings are present - use it
787
2.23k
        }
788
2.73k
    }
789
790
    // optional
791
2.23k
    cJSON* stderr_filter = loader_cJSON_GetObjectItem(settings_to_use, "stderr_log");
792
2.23k
    if (NULL != stderr_filter) {
793
328
        struct loader_string_list stderr_log = {0};
794
328
        VkResult stderr_log_result = VK_SUCCESS;
795
328
        stderr_log_result = loader_parse_json_array_of_strings(inst, settings_to_use, "stderr_log", &stderr_log);
796
328
        if (VK_ERROR_OUT_OF_HOST_MEMORY == stderr_log_result) {
797
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
798
0
            goto out;
799
0
        }
800
328
        loader_settings->debug_level = parse_log_filters_from_strings(&stderr_log);
801
328
        free_string_list(inst, &stderr_log);
802
328
    }
803
804
    // optional
805
2.23k
    cJSON* logs_to_use = loader_cJSON_GetObjectItem(settings_to_use, "log_locations");
806
2.23k
    if (NULL != logs_to_use) {
807
62
        cJSON* log_element = NULL;
808
354
        cJSON_ArrayForEach(log_element, logs_to_use) {
809
            // bool is_valid = true;
810
354
            struct loader_string_list log_destinations = {0};
811
354
            VkResult parse_dest_res = loader_parse_json_array_of_strings(inst, log_element, "destinations", &log_destinations);
812
354
            if (parse_dest_res != VK_SUCCESS) {
813
                // is_valid = false;
814
340
            }
815
354
            free_string_list(inst, &log_destinations);
816
354
            struct loader_string_list log_filters = {0};
817
354
            VkResult parse_filters_res = loader_parse_json_array_of_strings(inst, log_element, "filters", &log_filters);
818
354
            if (parse_filters_res != VK_SUCCESS) {
819
                // is_valid = false;
820
327
            }
821
354
            free_string_list(inst, &log_filters);
822
354
        }
823
62
    }
824
825
2.23k
    VkResult layer_configurations_res = parse_layer_configurations(inst, settings_to_use, loader_settings);
826
2.23k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == layer_configurations_res) {
827
0
        res = layer_configurations_res;
828
0
        goto out;
829
0
    }
830
831
    // Determine if there exists a layer configuration indicating where to put layers not contained in the settings file
832
    // LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION
833
14.6k
    for (uint32_t i = 0; i < loader_settings->layer_configuration_count; i++) {
834
13.0k
        if (loader_settings->layer_configurations[i].control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
835
664
            loader_settings->has_unordered_layer_location = true;
836
664
            break;
837
664
        }
838
13.0k
    }
839
840
2.23k
    VkResult additional_drivers_res = parse_additional_drivers(inst, settings_to_use, loader_settings);
841
2.23k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == additional_drivers_res) {
842
0
        res = additional_drivers_res;
843
0
        goto out;
844
0
    }
845
846
2.23k
    VkResult device_configurations_res = parse_device_configurations(inst, settings_to_use, loader_settings);
847
2.23k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == device_configurations_res) {
848
0
        res = device_configurations_res;
849
0
        goto out;
850
0
    }
851
852
    // Only consider the settings active if there is at least one "setting" active.
853
    // Those are either logging, layers, additional_drivers, or device_configurations.
854
2.23k
    if (loader_settings->debug_level != 0 || loader_settings->layer_configurations_active ||
855
1.89k
        loader_settings->additional_driver_count != 0 || loader_settings->device_configurations_active) {
856
1.89k
        loader_settings->settings_file_path = settings_file_path;
857
1.89k
        settings_file_path = NULL;
858
1.89k
        loader_settings->settings_active = true;
859
1.89k
    } else {
860
340
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
861
340
                   "vk_loader_settings.json file found at \"%s\" but did not contain any valid settings.", settings_file_path);
862
340
    }
863
6.52k
out:
864
6.52k
    if (NULL != json) {
865
2.94k
        loader_cJSON_Delete(json);
866
2.94k
    }
867
868
6.52k
    loader_instance_heap_free(inst, settings_file_path);
869
870
6.52k
    loader_instance_heap_free(inst, file_format_version_string);
871
6.52k
    return res;
872
2.23k
}
873
874
0
TEST_FUNCTION_EXPORT VkResult update_global_loader_settings(void) {
875
0
    loader_settings settings = {0};
876
0
    VkResult res = get_loader_settings(NULL, &settings);
877
0
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
878
879
0
    free_loader_settings(NULL, &global_loader_settings);
880
0
    if (res == VK_SUCCESS) {
881
0
        if (!check_if_settings_are_equal(&settings, &global_loader_settings)) {
882
0
            log_settings(NULL, &settings);
883
0
        }
884
885
0
        memcpy(&global_loader_settings, &settings, sizeof(loader_settings));
886
0
        if (global_loader_settings.settings_active && global_loader_settings.debug_level > 0) {
887
0
            loader_set_global_debug_level(global_loader_settings.debug_level);
888
0
        }
889
0
    }
890
0
    loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
891
0
    return res;
892
0
}
893
894
2
void init_global_loader_settings(void) {
895
2
    loader_platform_thread_create_mutex(&global_loader_settings_lock);
896
    // Free out the global settings in case the process was loaded & unloaded
897
2
    free_loader_settings(NULL, &global_loader_settings);
898
2
}
899
0
void teardown_global_loader_settings(void) {
900
0
    free_loader_settings(NULL, &global_loader_settings);
901
0
    loader_platform_thread_delete_mutex(&global_loader_settings_lock);
902
0
}
903
904
0
bool should_skip_logging_global_messages(VkFlags msg_type) {
905
0
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
906
0
    bool should_skip = global_loader_settings.settings_active && 0 != (msg_type & global_loader_settings.debug_level);
907
0
    loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
908
0
    return should_skip;
909
0
}
910
911
// Use this function to get the correct settings to use based on the context
912
// If inst is NULL - use the global settings and lock the mutex
913
// Else return the settings local to the instance - but do nto lock the mutex
914
6.83k
const loader_settings* get_current_settings_and_lock(const struct loader_instance* inst) {
915
6.83k
    if (inst) {
916
6.83k
        return &inst->settings;
917
6.83k
    }
918
0
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
919
0
    return &global_loader_settings;
920
6.83k
}
921
// Release the global settings lock if we are using the global settings - aka if inst is NULL
922
6.83k
void release_current_settings_lock(const struct loader_instance* inst) {
923
6.83k
    if (inst == NULL) {
924
0
        loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
925
0
    }
926
6.83k
}
927
928
TEST_FUNCTION_EXPORT VkResult get_settings_layers(const struct loader_instance* inst, struct loader_layer_list* settings_layers,
929
6.52k
                                                  bool* should_search_for_other_layers) {
930
6.52k
    VkResult res = VK_SUCCESS;
931
6.52k
    *should_search_for_other_layers = true;  // default to true
932
933
6.52k
    const loader_settings* settings = get_current_settings_and_lock(inst);
934
935
6.52k
    if (NULL == settings || !settings->settings_active || !settings->layer_configurations_active) {
936
4.70k
        goto out;
937
4.70k
    }
938
939
    // Assume the list doesn't contain LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION at first
940
1.81k
    *should_search_for_other_layers = false;
941
942
15.7k
    for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
943
13.9k
        loader_settings_layer_configuration* layer_config = &settings->layer_configurations[i];
944
945
        // If we encountered a layer that should be forced off, we add it to the settings_layers list but only
946
        // with the data required to compare it with layers not in the settings file (aka name and manifest path)
947
13.9k
        if (layer_config->control == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
948
1.07k
            struct loader_layer_properties props = {0};
949
1.07k
            props.settings_control_value = LOADER_SETTINGS_LAYER_CONTROL_OFF;
950
1.07k
            loader_strncpy(props.info.layerName, VK_MAX_EXTENSION_NAME_SIZE, layer_config->name, VK_MAX_EXTENSION_NAME_SIZE);
951
1.07k
            props.info.layerName[VK_MAX_EXTENSION_NAME_SIZE - 1] = '\0';
952
1.07k
            res = loader_copy_to_new_str(inst, layer_config->path, &props.manifest_file_name);
953
1.07k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
954
0
                goto out;
955
0
            }
956
1.07k
            res = loader_append_layer_property(inst, settings_layers, &props);
957
1.07k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
958
0
                loader_free_layer_properties(inst, &props);
959
0
                goto out;
960
0
            }
961
1.07k
            continue;
962
1.07k
        }
963
964
        // The special layer location that indicates where unordered layers should go only should have the
965
        // settings_control_value set - everything else should be NULL
966
12.8k
        if (layer_config->control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
967
789
            struct loader_layer_properties props = {0};
968
789
            props.settings_control_value = LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION;
969
789
            res = loader_append_layer_property(inst, settings_layers, &props);
970
789
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
971
0
                loader_free_layer_properties(inst, &props);
972
0
                goto out;
973
0
            }
974
789
            *should_search_for_other_layers = true;
975
789
            continue;
976
789
        }
977
978
12.0k
        if (layer_config->path == NULL) {
979
0
            continue;
980
0
        }
981
982
12.0k
        cJSON* json = NULL;
983
12.0k
        VkResult local_res = loader_get_json(inst, layer_config->path, &json);
984
12.0k
        if (VK_ERROR_OUT_OF_HOST_MEMORY == local_res) {
985
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
986
0
            goto out;
987
12.0k
        } else if (VK_SUCCESS != local_res || NULL == json) {
988
4.21k
            continue;
989
4.21k
        }
990
991
        // Makes it possible to know if a new layer was added or not, since the only return value is VkResult
992
7.88k
        size_t count_before_adding = settings_layers->count;
993
994
7.88k
        local_res =
995
7.88k
            loader_add_layer_properties(inst, settings_layers, json, layer_config->treat_as_implicit_manifest, layer_config->path);
996
7.88k
        loader_cJSON_Delete(json);
997
998
        // If the error is anything other than out of memory we still want to try to load the other layers
999
7.88k
        if (VK_ERROR_OUT_OF_HOST_MEMORY == local_res) {
1000
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
1001
0
            goto out;
1002
7.88k
        } else if (local_res != VK_SUCCESS || count_before_adding == settings_layers->count) {
1003
            // Indicates something was wrong with the layer, can't add it to the list
1004
4.39k
            continue;
1005
4.39k
        }
1006
1007
3.48k
        struct loader_layer_properties* newly_added_layer = &settings_layers->list[settings_layers->count - 1];
1008
3.48k
        newly_added_layer->settings_control_value = layer_config->control;
1009
        // If the manifest file found has a name that differs from the one in the settings, remove this layer from
1010
        // consideration
1011
3.48k
        bool should_remove = false;
1012
3.48k
        if (strncmp(newly_added_layer->info.layerName, layer_config->name, VK_MAX_EXTENSION_NAME_SIZE) != 0) {
1013
2.48k
            should_remove = true;
1014
2.48k
            loader_remove_layer_in_list(inst, settings_layers, settings_layers->count - 1);
1015
2.48k
        }
1016
        // Make sure the layer isn't already in the list
1017
1.59M
        for (uint32_t j = 0; settings_layers->count > 0 && j < settings_layers->count - 1; j++) {
1018
1.59M
            if (0 ==
1019
1.59M
                strncmp(settings_layers->list[j].info.layerName, newly_added_layer->info.layerName, VK_MAX_EXTENSION_NAME_SIZE)) {
1020
22.0k
                if (0 == (newly_added_layer->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER) &&
1021
21.8k
                    settings_layers->list[j].lib_name != NULL && newly_added_layer->lib_name != NULL &&
1022
1.09k
                    strcmp(settings_layers->list[j].lib_name, newly_added_layer->lib_name) == 0) {
1023
297
                    should_remove = true;
1024
297
                    break;
1025
297
                }
1026
22.0k
            }
1027
1.59M
        }
1028
3.48k
        if (should_remove) {
1029
2.78k
            loader_remove_layer_in_list(inst, settings_layers, settings_layers->count - 1);
1030
2.78k
        }
1031
3.48k
    }
1032
1033
6.52k
out:
1034
6.52k
    release_current_settings_lock(inst);
1035
6.52k
    return res;
1036
1.81k
}
1037
1038
// Check if layers has an element with the same name.
1039
// LAYER_CONTROL_OFF layers are missing some fields, just make sure the layerName is the same
1040
// If layer_property is a meta layer, just make sure the layerName is the same
1041
// Skip comparing to UNORDERED_LAYER_LOCATION
1042
// If layer_property is a regular layer, check if the lib_path is the same.
1043
// Make sure that the lib_name pointers are non-null before calling strcmp.
1044
30.7k
bool check_if_layer_is_in_list(struct loader_layer_list* layer_list, struct loader_layer_properties* layer_property) {
1045
    // If the layer is a meta layer, just check against the name
1046
11.7M
    for (uint32_t i = 0; i < layer_list->count; i++) {
1047
11.6M
        if (0 == strncmp(layer_list->list[i].info.layerName, layer_property->info.layerName, VK_MAX_EXTENSION_NAME_SIZE)) {
1048
3.47M
            if (layer_list->list[i].settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
1049
98
                return true;
1050
98
            }
1051
3.47M
            if (VK_LAYER_TYPE_FLAG_META_LAYER == (layer_property->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER)) {
1052
801
                return true;
1053
801
            }
1054
3.47M
            if (layer_list->list[i].lib_name && layer_property->lib_name) {
1055
7.48k
                return strcmp(layer_list->list[i].lib_name, layer_property->lib_name) == 0;
1056
7.48k
            }
1057
3.47M
        }
1058
11.6M
    }
1059
22.4k
    return false;
1060
30.7k
}
1061
1062
VkResult combine_settings_layers_with_regular_layers(const struct loader_instance* inst, struct loader_layer_list* settings_layers,
1063
                                                     struct loader_layer_list* regular_layers,
1064
5.37k
                                                     struct loader_layer_list* output_layers) {
1065
5.37k
    VkResult res = VK_SUCCESS;
1066
5.37k
    bool has_unordered_layer_location = false;
1067
5.37k
    uint32_t unordered_layer_location_index = 0;
1068
    // Location to put layers that aren't known to the settings file
1069
    // Find it here so we dont have to pass in a loader_settings struct
1070
51.6k
    for (uint32_t i = 0; i < settings_layers->count; i++) {
1071
46.9k
        if (settings_layers->list[i].settings_control_value == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
1072
663
            has_unordered_layer_location = true;
1073
663
            unordered_layer_location_index = i;
1074
663
            break;
1075
663
        }
1076
46.9k
    }
1077
1078
5.37k
    if (settings_layers->count == 0 && regular_layers->count == 0) {
1079
        // No layers to combine
1080
3.34k
        goto out;
1081
3.34k
    } else if (settings_layers->count == 0) {
1082
        // No settings layers - just copy regular to output_layers - memset regular layers to prevent double frees
1083
1.36k
        *output_layers = *regular_layers;
1084
1.36k
        memset(regular_layers, 0, sizeof(struct loader_layer_list));
1085
1.36k
        goto out;
1086
1.36k
    } else if (regular_layers->count == 0 || !has_unordered_layer_location) {
1087
        // No regular layers or has_unordered_layer_location is false - just copy settings to output_layers -
1088
        // memset settings layers to prevent double frees
1089
204
        *output_layers = *settings_layers;
1090
204
        memset(settings_layers, 0, sizeof(struct loader_layer_list));
1091
204
        goto out;
1092
204
    }
1093
1094
460
    res = loader_init_generic_list(inst, (struct loader_generic_list*)output_layers,
1095
460
                                   (settings_layers->count + regular_layers->count) * sizeof(struct loader_layer_properties));
1096
460
    if (VK_SUCCESS != res) {
1097
0
        goto out;
1098
0
    }
1099
1100
    // Insert the settings layers into output_layers up to unordered_layer_index
1101
14.6k
    for (uint32_t i = 0; i < unordered_layer_location_index; i++) {
1102
14.1k
        if (!check_if_layer_is_in_list(output_layers, &settings_layers->list[i])) {
1103
11.7k
            res = loader_append_layer_property(inst, output_layers, &settings_layers->list[i]);
1104
11.7k
            if (VK_SUCCESS != res) {
1105
0
                goto out;
1106
0
            }
1107
11.7k
        }
1108
14.1k
    }
1109
1110
9.06k
    for (uint32_t i = 0; i < regular_layers->count; i++) {
1111
        // Check if its already been put in the output_layers list as well as the remaining settings_layers
1112
8.60k
        bool regular_layer_is_ordered = check_if_layer_is_in_list(output_layers, &regular_layers->list[i]) ||
1113
8.03k
                                        check_if_layer_is_in_list(settings_layers, &regular_layers->list[i]);
1114
        // If it isn't found, add it
1115
8.60k
        if (!regular_layer_is_ordered) {
1116
8.02k
            res = loader_append_layer_property(inst, output_layers, &regular_layers->list[i]);
1117
8.02k
            if (VK_SUCCESS != res) {
1118
0
                goto out;
1119
0
            }
1120
8.02k
        } else {
1121
            // layer is already ordered and can be safely freed
1122
577
            loader_free_layer_properties(inst, &regular_layers->list[i]);
1123
577
        }
1124
8.60k
    }
1125
1126
    // Insert the rest of the settings layers into combined_layers from  unordered_layer_index to the end
1127
    // start at one after the unordered_layer_index
1128
1.07k
    for (uint32_t i = unordered_layer_location_index + 1; i < settings_layers->count; i++) {
1129
614
        res = loader_append_layer_property(inst, output_layers, &settings_layers->list[i]);
1130
614
        if (VK_SUCCESS != res) {
1131
0
            goto out;
1132
0
        }
1133
614
    }
1134
1135
5.37k
out:
1136
5.37k
    if (res != VK_SUCCESS) {
1137
0
        loader_delete_layer_list_and_properties(inst, output_layers);
1138
0
    }
1139
1140
5.37k
    return res;
1141
460
}
1142
1143
VkResult enable_correct_layers_from_settings(const struct loader_instance* inst, const struct loader_envvar_all_filters* filters,
1144
                                             uint32_t app_enabled_name_count, const char* const* app_enabled_names,
1145
                                             const struct loader_layer_list* instance_layers,
1146
                                             struct loader_pointer_layer_list* target_layer_list,
1147
0
                                             struct loader_pointer_layer_list* activated_layer_list) {
1148
0
    VkResult res = VK_SUCCESS;
1149
0
    char* vk_instance_layers_env = loader_getenv(ENABLED_LAYERS_ENV, inst);
1150
0
    size_t vk_instance_layers_env_len = 0;
1151
0
    char* vk_instance_layers_env_copy = NULL;
1152
0
    if (vk_instance_layers_env != NULL) {
1153
0
        vk_instance_layers_env_len = strlen(vk_instance_layers_env) + 1;
1154
0
        vk_instance_layers_env_copy = loader_stack_alloc(vk_instance_layers_env_len);
1155
0
        memset(vk_instance_layers_env_copy, 0, vk_instance_layers_env_len);
1156
1157
0
        loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0, "env var \'%s\' defined and adding layers: %s",
1158
0
                   ENABLED_LAYERS_ENV, vk_instance_layers_env);
1159
0
    }
1160
0
    for (uint32_t i = 0; i < instance_layers->count; i++) {
1161
0
        bool enable_layer = false;
1162
0
        struct loader_layer_properties* props = &instance_layers->list[i];
1163
1164
        // Skip the sentinel unordered layer location
1165
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
1166
0
            continue;
1167
0
        }
1168
1169
        // Do not enable the layer if the settings have it set as off
1170
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
1171
0
            continue;
1172
0
        }
1173
        // Force enable it based on settings
1174
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_ON) {
1175
0
            enable_layer = true;
1176
0
            props->enabled_by_what = ENABLED_BY_WHAT_LOADER_SETTINGS_FILE;
1177
0
        } else {
1178
            // Check if disable filter needs to skip the layer
1179
0
            if ((filters->disable_filter.disable_all || filters->disable_filter.disable_all_implicit ||
1180
0
                 check_name_matches_filter_environment_var(props->info.layerName, &filters->disable_filter.additional_filters)) &&
1181
0
                !check_name_matches_filter_environment_var(props->info.layerName, &filters->allow_filter)) {
1182
                // Report a message that we've forced off a layer if it would have been enabled normally.
1183
0
                loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0,
1184
0
                           "Layer \"%s\" forced disabled because name matches filter of env var \'%s\'.", props->info.layerName,
1185
0
                           VK_LAYERS_DISABLE_ENV_VAR);
1186
1187
0
                continue;
1188
0
            }
1189
0
        }
1190
        // Check the enable filter
1191
0
        if (!enable_layer && check_name_matches_filter_environment_var(props->info.layerName, &filters->enable_filter)) {
1192
0
            enable_layer = true;
1193
0
            props->enabled_by_what = ENABLED_BY_WHAT_VK_LOADER_LAYERS_ENABLE;
1194
0
            loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0,
1195
0
                       "Layer \"%s\" forced enabled due to env var \'%s\'.", props->info.layerName, VK_LAYERS_ENABLE_ENV_VAR);
1196
0
        }
1197
1198
        // First look for the old-fashion layers forced on with VK_INSTANCE_LAYERS
1199
0
        if (!enable_layer && vk_instance_layers_env && vk_instance_layers_env_copy && vk_instance_layers_env_len > 0) {
1200
            // Copy the env-var on each iteration, so that loader_get_next_path can correctly find the separators
1201
            // This solution only needs one stack allocation ahead of time rather than an allocation per layer in the
1202
            // env-var
1203
0
            loader_strncpy(vk_instance_layers_env_copy, vk_instance_layers_env_len, vk_instance_layers_env,
1204
0
                           vk_instance_layers_env_len);
1205
1206
0
            char* instance_layers_env_iter = vk_instance_layers_env_copy;
1207
0
            while (instance_layers_env_iter && *instance_layers_env_iter) {
1208
0
                char* next = loader_get_next_path(instance_layers_env_iter);
1209
0
                if (0 == strcmp(instance_layers_env_iter, props->info.layerName)) {
1210
0
                    enable_layer = true;
1211
0
                    props->enabled_by_what = ENABLED_BY_WHAT_VK_INSTANCE_LAYERS;
1212
0
                    break;
1213
0
                }
1214
0
                instance_layers_env_iter = next;
1215
0
            }
1216
0
        }
1217
1218
        // Check if it should be enabled by the application
1219
0
        if (!enable_layer) {
1220
0
            for (uint32_t j = 0; j < app_enabled_name_count; j++) {
1221
0
                if (strcmp(props->info.layerName, app_enabled_names[j]) == 0) {
1222
0
                    enable_layer = true;
1223
0
                    props->enabled_by_what = ENABLED_BY_WHAT_IN_APPLICATION_API;
1224
0
                    break;
1225
0
                }
1226
0
            }
1227
0
        }
1228
1229
        // Check if its an implicit layers and thus enabled by default
1230
0
        if (!enable_layer && (0 == (props->type_flags & VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER)) &&
1231
0
            loader_implicit_layer_is_enabled(inst, filters, props)) {
1232
0
            enable_layer = true;
1233
0
            props->enabled_by_what = ENABLED_BY_WHAT_IMPLICIT_LAYER;
1234
0
        }
1235
1236
0
        if (enable_layer) {
1237
            // Check if the layer is a meta layer reuse the existing function to add the meta layer
1238
0
            if (props->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER) {
1239
0
                res = loader_add_meta_layer(inst, filters, props, target_layer_list, activated_layer_list, instance_layers, NULL);
1240
0
                if (res == VK_ERROR_OUT_OF_HOST_MEMORY) goto out;
1241
0
            } else {
1242
0
                res = loader_add_layer_properties_to_list(inst, target_layer_list, props);
1243
0
                if (res != VK_SUCCESS) {
1244
0
                    goto out;
1245
0
                }
1246
0
                res = loader_add_layer_properties_to_list(inst, activated_layer_list, props);
1247
0
                if (res != VK_SUCCESS) {
1248
0
                    goto out;
1249
0
                }
1250
0
            }
1251
0
        }
1252
0
    }
1253
0
out:
1254
0
    return res;
1255
0
}
1256
1257
104
VkResult loader_settings_get_additional_driver_files(const struct loader_instance* inst, struct loader_string_list* out_files) {
1258
104
    VkResult res = VK_SUCCESS;
1259
1260
104
    const loader_settings* settings = get_current_settings_and_lock(inst);
1261
1262
104
    if (NULL == settings || !settings->settings_active) {
1263
0
        goto out;
1264
0
    }
1265
1266
104
    if (settings->additional_drivers_use_exclusively) {
1267
0
        free_string_list(inst, out_files);
1268
0
    }
1269
1270
104
    for (uint32_t i = 0; i < settings->additional_driver_count; i++) {
1271
0
        res = prepend_if_manifest_file(inst, settings->additional_drivers[i].path, out_files);
1272
0
    }
1273
1274
104
out:
1275
104
    release_current_settings_lock(inst);
1276
104
    return res;
1277
104
}
1278
1279
208
bool loader_settings_should_use_driver_environment_variables(const struct loader_instance* inst) {
1280
208
    bool should_use = true;
1281
208
    const loader_settings* settings = get_current_settings_and_lock(inst);
1282
208
    if (NULL == settings || !settings->settings_active) {
1283
0
        goto out;
1284
0
    }
1285
208
    if (settings->device_configurations_active) {
1286
0
        should_use = false;
1287
0
    }
1288
208
out:
1289
208
    release_current_settings_lock(inst);
1290
208
    return should_use;
1291
208
}