Coverage Report

Created: 2026-08-11 06:34

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
91.3k
void free_layer_configuration(const struct loader_instance* inst, loader_settings_layer_configuration* layer_configuration) {
42
91.3k
    loader_instance_heap_free(inst, layer_configuration->name);
43
91.3k
    loader_instance_heap_free(inst, layer_configuration->path);
44
91.3k
    memset(layer_configuration, 0, sizeof(loader_settings_layer_configuration));
45
91.3k
}
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.84k
void free_loader_settings(const struct loader_instance* inst, loader_settings* settings) {
58
2.84k
    if (NULL != settings->layer_configurations) {
59
38.7k
        for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
60
38.1k
            free_layer_configuration(inst, &settings->layer_configurations[i]);
61
38.1k
        }
62
664
        loader_instance_heap_free(inst, settings->layer_configurations);
63
664
    }
64
2.84k
    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.84k
    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.84k
    loader_instance_heap_free(inst, settings->settings_file_path);
77
2.84k
    memset(settings, 0, sizeof(loader_settings));
78
2.84k
}
79
80
38.9k
loader_settings_layer_control parse_control_string(char* control_string) {
81
38.9k
    loader_settings_layer_control layer_control = LOADER_SETTINGS_LAYER_CONTROL_DEFAULT;
82
38.9k
    if (strcmp(control_string, "auto") == 0)
83
357
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_DEFAULT;
84
38.6k
    else if (strcmp(control_string, "on") == 0)
85
620
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_ON;
86
38.0k
    else if (strcmp(control_string, "off") == 0)
87
7.72k
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_OFF;
88
30.2k
    else if (strcmp(control_string, "unordered_layer_location") == 0)
89
27.2k
        layer_control = LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION;
90
38.9k
    return layer_control;
91
38.9k
}
92
93
37.9k
const char* loader_settings_layer_control_to_string(loader_settings_layer_control control) {
94
37.9k
    switch (control) {
95
2.88k
        case (LOADER_SETTINGS_LAYER_CONTROL_DEFAULT):
96
2.88k
            return "auto";
97
468
        case (LOADER_SETTINGS_LAYER_CONTROL_ON):
98
468
            return "on";
99
7.63k
        case (LOADER_SETTINGS_LAYER_CONTROL_OFF):
100
7.63k
            return "off";
101
26.9k
        case (LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION):
102
26.9k
            return "unordered_layer_location";
103
0
        default:
104
0
            return "UNKNOWN_LAYER_CONTROl";
105
37.9k
    }
106
37.9k
}
107
108
276
uint32_t parse_log_filters_from_strings(struct loader_string_list* log_filters) {
109
276
    uint32_t filters = 0;
110
183k
    for (uint32_t i = 0; i < log_filters->count; i++) {
111
183k
        if (strcmp(log_filters->list[i], "all") == 0)
112
2.68k
            filters |= VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_PERF_BIT | VULKAN_LOADER_ERROR_BIT |
113
2.68k
                       VULKAN_LOADER_DEBUG_BIT | VULKAN_LOADER_LAYER_BIT | VULKAN_LOADER_DRIVER_BIT | VULKAN_LOADER_VALIDATION_BIT;
114
180k
        else if (strcmp(log_filters->list[i], "info") == 0)
115
687
            filters |= VULKAN_LOADER_INFO_BIT;
116
179k
        else if (strcmp(log_filters->list[i], "warn") == 0)
117
578
            filters |= VULKAN_LOADER_WARN_BIT;
118
179k
        else if (strcmp(log_filters->list[i], "perf") == 0)
119
1.28k
            filters |= VULKAN_LOADER_PERF_BIT;
120
178k
        else if (strcmp(log_filters->list[i], "error") == 0)
121
567
            filters |= VULKAN_LOADER_ERROR_BIT;
122
177k
        else if (strcmp(log_filters->list[i], "debug") == 0)
123
1.09k
            filters |= VULKAN_LOADER_DEBUG_BIT;
124
176k
        else if (strcmp(log_filters->list[i], "layer") == 0)
125
1.29k
            filters |= VULKAN_LOADER_LAYER_BIT;
126
175k
        else if (strcmp(log_filters->list[i], "driver") == 0)
127
1.98k
            filters |= VULKAN_LOADER_DRIVER_BIT;
128
173k
        else if (strcmp(log_filters->list[i], "validation") == 0)
129
264
            filters |= VULKAN_LOADER_VALIDATION_BIT;
130
183k
    }
131
276
    return filters;
132
276
}
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
39.0k
                                   loader_settings_layer_configuration* layer_configuration) {
148
39.0k
    char* control_string = NULL;
149
39.0k
    VkResult res = loader_parse_json_string(layer_configuration_json, "control", &control_string);
150
39.0k
    if (res != VK_SUCCESS) {
151
15
        goto out;
152
15
    }
153
38.9k
    layer_configuration->control = parse_control_string(control_string);
154
38.9k
    loader_instance_heap_free(inst, control_string);
155
156
    // If that is the only value - do no further parsing
157
38.9k
    if (layer_configuration->control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
158
27.2k
        goto out;
159
27.2k
    }
160
161
11.7k
    res = loader_parse_json_string(layer_configuration_json, "name", &(layer_configuration->name));
162
11.7k
    if (res != VK_SUCCESS) {
163
92
        goto out;
164
92
    }
165
166
11.6k
    res = loader_parse_json_string(layer_configuration_json, "path", &(layer_configuration->path));
167
11.6k
    if (res != VK_SUCCESS) {
168
7
        goto out;
169
7
    }
170
171
11.6k
    cJSON* treat_as_implicit_manifest = loader_cJSON_GetObjectItem(layer_configuration_json, "treat_as_implicit_manifest");
172
11.6k
    if (treat_as_implicit_manifest && treat_as_implicit_manifest->type == cJSON_True) {
173
257
        layer_configuration->treat_as_implicit_manifest = true;
174
257
    }
175
39.0k
out:
176
39.0k
    if (VK_SUCCESS != res) {
177
114
        free_layer_configuration(inst, layer_configuration);
178
114
    }
179
39.0k
    return res;
180
11.6k
}
181
182
1.11k
VkResult parse_layer_configurations(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
183
1.11k
    VkResult res = VK_SUCCESS;
184
185
1.11k
    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.11k
    if (NULL == layer_configurations) {
188
279
        return VK_SUCCESS;
189
279
    }
190
191
837
    uint32_t layer_configurations_count = loader_cJSON_GetArraySize(layer_configurations);
192
837
    if (layer_configurations_count == 0) {
193
45
        loader_settings->layer_configurations_active = true;
194
45
        return VK_SUCCESS;
195
45
    }
196
197
792
    loader_settings->layer_configuration_count = layer_configurations_count;
198
199
792
    loader_settings->layer_configurations = loader_instance_heap_calloc(
200
792
        inst, sizeof(loader_settings_layer_configuration) * layer_configurations_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
201
792
    if (NULL == loader_settings->layer_configurations) {
202
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
203
0
        goto out;
204
0
    }
205
206
792
    cJSON* layer = NULL;
207
792
    size_t i = 0;
208
39.0k
    cJSON_ArrayForEach(layer, layer_configurations) {
209
39.0k
        if (layer->type != cJSON_Object) {
210
14
            res = VK_ERROR_INITIALIZATION_FAILED;
211
14
            goto out;
212
14
        }
213
39.0k
        res = parse_layer_configuration(inst, layer, &(loader_settings->layer_configurations[i++]));
214
39.0k
        if (VK_SUCCESS != res) {
215
114
            goto out;
216
114
        }
217
39.0k
    }
218
664
    loader_settings->layer_configurations_active = true;
219
792
out:
220
792
    if (res != VK_SUCCESS) {
221
128
        if (loader_settings->layer_configurations) {
222
53.2k
            for (size_t index = 0; index < loader_settings->layer_configuration_count; index++) {
223
53.0k
                free_layer_configuration(inst, &(loader_settings->layer_configurations[index]));
224
53.0k
            }
225
128
            loader_settings->layer_configuration_count = 0;
226
128
            loader_instance_heap_free(inst, loader_settings->layer_configurations);
227
128
            loader_settings->layer_configurations = NULL;
228
128
        }
229
128
    }
230
231
792
    return res;
232
664
}
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.11k
VkResult parse_additional_drivers(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
249
1.11k
    VkResult res = VK_SUCCESS;
250
251
1.11k
    cJSON* additional_drivers_use_exclusively_json =
252
1.11k
        loader_cJSON_GetObjectItem(settings_object, "additional_drivers_use_exclusively");
253
1.11k
    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.11k
    cJSON* additional_drivers_json = loader_cJSON_GetObjectItem(settings_object, "additional_drivers");
258
1.11k
    if (NULL == additional_drivers_json) {
259
1.11k
        return VK_SUCCESS;
260
1.11k
    }
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.11k
VkResult parse_device_configurations(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
376
1.11k
    VkResult res = VK_SUCCESS;
377
378
1.11k
    cJSON* device_configurations = loader_cJSON_GetObjectItem(settings_object, "device_configurations");
379
1.11k
    if (NULL == device_configurations) {
380
1.11k
        return VK_SUCCESS;
381
1.11k
    }
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.8k
        return VK_ERROR_INITIALIZATION_FAILED;
440
11.8k
    }
441
442
13.7k
    size_t base_len = strlen(base);
443
13.7k
    size_t suffix_len = strlen(suffix);
444
445
13.7k
    uint32_t start = 0;
446
13.7k
    uint32_t stop = 0;
447
22.4k
    while (base[start] != '\0' && start < base_len && stop < base_len) {
448
14.1k
        start = stop;
449
14.1k
        stop = start + 1;
450
72.4k
        while (base[stop] != PATH_SEPARATOR && base[stop] != '\0' && stop < base_len) {
451
58.3k
            stop++;
452
58.3k
        }
453
454
14.1k
        size_t segment_len = (stop - start);
455
14.1k
        if (segment_len <= 1) {
456
            // segment is *just* a PATH_SEPARATOR, skip it
457
0
            continue;
458
0
        }
459
14.1k
        size_t path_len = segment_len + suffix_len + 1;
460
14.1k
        *settings_file_path = loader_instance_heap_calloc(inst, path_len, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
461
14.1k
        if (NULL == *settings_file_path) {
462
0
            return VK_ERROR_OUT_OF_HOST_MEMORY;
463
0
        }
464
14.1k
        loader_strncpy(*settings_file_path, path_len, base + start, segment_len);
465
14.1k
        loader_strncat(*settings_file_path, path_len, suffix, suffix_len);
466
467
14.1k
        if (loader_platform_file_exists(*settings_file_path)) {
468
5.42k
            return VK_SUCCESS;
469
5.42k
        }
470
8.76k
        loader_instance_heap_free(inst, *settings_file_path);
471
8.76k
        *settings_file_path = NULL;
472
8.76k
    }
473
474
8.28k
    return VK_ERROR_INITIALIZATION_FAILED;
475
13.7k
}
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.90k
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.90k
    char* xdg_config_home = loader_secure_getenv("XDG_CONFIG_HOME", inst);
481
5.90k
    char* xdg_config_dirs = loader_secure_getenv("XDG_CONFIG_DIRS", inst);
482
5.90k
    char* xdg_data_home = loader_secure_getenv("XDG_DATA_HOME", inst);
483
5.90k
    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.90k
#if !defined(__Fuchsia__) && !defined(__QNX__)
487
5.90k
    if (NULL == xdg_config_dirs || '\0' == xdg_config_dirs[0]) {
488
5.90k
        xdg_config_dirs = FALLBACK_CONFIG_DIRS;
489
5.90k
    }
490
5.90k
#endif
491
492
5.90k
#if !defined(__Fuchsia__) && !defined(__QNX__)
493
5.90k
    if (NULL == xdg_data_dirs || '\0' == xdg_data_dirs[0]) {
494
5.90k
        xdg_data_dirs = FALLBACK_DATA_DIRS;
495
5.90k
    }
496
5.90k
#endif
497
498
5.90k
    VkResult res = check_if_settings_path_exists(
499
5.90k
        inst, xdg_config_home, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
500
5.90k
    if (res == VK_SUCCESS) {
501
0
        return res;
502
0
    }
503
504
5.90k
    res = check_if_settings_path_exists(inst, xdg_data_home, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
505
5.90k
                                        settings_file_path);
506
5.90k
    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.90k
    char* home = loader_secure_getenv("HOME", inst);
512
5.90k
    if (home != NULL) {
513
5.90k
        if (NULL == xdg_config_home || '\0' == xdg_config_home[0]) {
514
5.90k
            res = check_if_settings_path_exists(
515
5.90k
                inst, home, "/.config/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
516
5.90k
            if (res == VK_SUCCESS) {
517
0
                return res;
518
0
            }
519
5.90k
        }
520
5.90k
        if (NULL == xdg_data_home || '\0' == xdg_data_home[0]) {
521
5.90k
            res = check_if_settings_path_exists(
522
5.90k
                inst, home, "/.local/share/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
523
5.90k
            if (res == VK_SUCCESS) {
524
5.42k
                return res;
525
5.42k
            }
526
5.90k
        }
527
5.90k
    }
528
529
477
    res = check_if_settings_path_exists(inst, xdg_config_dirs, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
530
477
                                        settings_file_path);
531
477
    if (res == VK_SUCCESS) {
532
0
        return res;
533
0
    }
534
535
477
    res = check_if_settings_path_exists(inst, SYSCONFDIR, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
536
477
                                        settings_file_path);
537
477
    if (res == VK_SUCCESS) {
538
0
        return res;
539
0
    }
540
477
#if defined(EXTRASYSCONFDIR)
541
542
477
    res = check_if_settings_path_exists(inst, EXTRASYSCONFDIR, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
543
477
                                        settings_file_path);
544
477
    if (res == VK_SUCCESS) {
545
0
        return res;
546
0
    }
547
477
#endif
548
477
    res = check_if_settings_path_exists(inst, xdg_data_dirs, "/" VULKAN_DIR "/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
549
477
                                        settings_file_path);
550
477
    if (res == VK_SUCCESS) {
551
0
        return res;
552
0
    }
553
554
477
    return VK_ERROR_INITIALIZATION_FAILED;
555
477
}
556
#endif
557
558
11.5k
bool check_if_layer_configurations_are_equal(loader_settings_layer_configuration* a, loader_settings_layer_configuration* b) {
559
11.5k
    if (!a->name || !b->name || 0 != strcmp(a->name, b->name)) {
560
10.6k
        return false;
561
10.6k
    }
562
993
    if (!a->path || !b->path || 0 != strcmp(a->path, b->path)) {
563
94
        return false;
564
94
    }
565
899
    return a->control == b->control && a->treat_as_implicit_manifest == b->treat_as_implicit_manifest;
566
993
}
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.83k
bool check_if_settings_are_equal(loader_settings* a, loader_settings* b) {
589
    // If either pointer is null, return true
590
2.83k
    if (NULL == a || NULL == b) return false;
591
2.83k
    bool are_equal = true;
592
2.83k
    are_equal &= a->settings_active == b->settings_active;
593
2.83k
    are_equal &= a->has_unordered_layer_location == b->has_unordered_layer_location;
594
2.83k
    are_equal &= a->debug_level == b->debug_level;
595
2.83k
    are_equal &= a->layer_configurations_active == b->layer_configurations_active;
596
2.83k
    are_equal &= a->layer_configuration_count == b->layer_configuration_count;
597
2.83k
    are_equal &= a->additional_drivers_use_exclusively == b->additional_drivers_use_exclusively;
598
2.83k
    are_equal &= a->additional_driver_count == b->additional_driver_count;
599
2.83k
    are_equal &= a->device_configurations_active == b->device_configurations_active;
600
2.83k
    are_equal &= a->device_configuration_count == b->device_configuration_count;
601
2.83k
    if (!are_equal) return false;
602
13.4k
    for (uint32_t i = 0; i < a->layer_configuration_count && i < b->layer_configuration_count; i++) {
603
11.5k
        are_equal &= check_if_layer_configurations_are_equal(&a->layer_configurations[i], &b->layer_configurations[i]);
604
11.5k
    }
605
1.82k
    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.82k
    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.82k
    return are_equal;
612
2.83k
}
613
614
1.11k
void log_settings(const struct loader_instance* inst, loader_settings* settings) {
615
1.11k
    if (settings == NULL) {
616
0
        return;
617
0
    }
618
1.11k
    loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "Using layer configurations found in loader settings from %s",
619
1.11k
               settings->settings_file_path);
620
621
1.11k
    char cmd_line_msg[64] = {0};
622
1.11k
    size_t cmd_line_size = sizeof(cmd_line_msg);
623
624
1.11k
    cmd_line_msg[0] = '\0';
625
626
1.11k
    generate_debug_flag_str(settings->debug_level, cmd_line_size, cmd_line_msg);
627
1.11k
    if (strlen(cmd_line_msg)) {
628
118
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Loader Settings Filters for Logging to Standard Error: %s", cmd_line_msg);
629
118
    }
630
1.11k
    if (settings->layer_configurations_active) {
631
684
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Layer Configurations count = %d", settings->layer_configuration_count);
632
38.6k
        for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
633
37.9k
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---- Layer Configuration [%d] ----", i);
634
37.9k
            if (settings->layer_configurations[i].control != LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
635
10.9k
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Name: %s", settings->layer_configurations[i].name);
636
10.9k
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Path: %s", settings->layer_configurations[i].path);
637
10.9k
                loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Layer Type: %s",
638
10.9k
                           settings->layer_configurations[i].treat_as_implicit_manifest ? "Implicit" : "Explicit");
639
10.9k
            }
640
37.9k
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Control: %s",
641
37.9k
                       loader_settings_layer_control_to_string(settings->layer_configurations[i].control));
642
37.9k
        }
643
684
    }
644
1.11k
    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.11k
    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.11k
    loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---------------------------------");
676
1.11k
}
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.90k
VkResult get_loader_settings(const struct loader_instance* inst, loader_settings* loader_settings) {
682
5.90k
    VkResult res = VK_SUCCESS;
683
5.90k
    cJSON* json = NULL;
684
5.90k
    char* file_format_version_string = NULL;
685
5.90k
    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.90k
    if (res != VK_SUCCESS) {
697
477
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
698
477
                   "No valid vk_loader_settings.json file found, no loader settings will be active");
699
477
        goto out;
700
477
    }
701
#else
702
#warning "Unsupported platform - must specify platform specific location for vk_loader_settings.json"
703
#endif
704
705
5.42k
    res = loader_get_json(inst, settings_file_path, &json);
706
    // Make sure sure the top level json value is an object
707
5.42k
    if (res != VK_SUCCESS || NULL == json || json->type != cJSON_Object) {
708
1.73k
        goto out;
709
1.73k
    }
710
711
3.68k
    res = loader_parse_json_string(json, "file_format_version", &file_format_version_string);
712
3.68k
    if (res != VK_SUCCESS) {
713
41
        if (res != VK_ERROR_OUT_OF_HOST_MEMORY) {
714
41
            loader_log(
715
41
                inst, VULKAN_LOADER_DEBUG_BIT, 0,
716
41
                "Loader settings file from %s missing required field file_format_version - no loader settings will be active",
717
41
                settings_file_path);
718
41
        }
719
41
        goto out;
720
41
    }
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.64k
    cJSON settings_iter_parent = {0};
725
726
3.64k
    cJSON* settings_array = loader_cJSON_GetObjectItem(json, "settings_array");
727
3.64k
    cJSON* single_settings_object = loader_cJSON_GetObjectItem(json, "settings");
728
3.64k
    if (NULL != settings_array) {
729
1.06k
        memcpy(&settings_iter_parent, settings_array, sizeof(cJSON));
730
2.58k
    } else if (NULL != single_settings_object) {
731
154
        settings_iter_parent.child = single_settings_object;
732
2.42k
    } else {
733
2.42k
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
734
2.42k
                   "Loader settings file from %s missing required settings objects: Either one of the \"settings\" or "
735
2.42k
                   "\"settings_array\" objects must be present - no loader settings will be active",
736
2.42k
                   settings_file_path);
737
2.42k
        res = VK_ERROR_INITIALIZATION_FAILED;
738
2.42k
        goto out;
739
2.42k
    }
740
741
    // Corresponds to the settings object that has no app keys
742
1.21k
    cJSON* global_settings = NULL;
743
    // Corresponds to the settings object which has a matching app key
744
1.21k
    cJSON* settings_to_use = NULL;
745
746
1.21k
    char current_process_path[1024];
747
1.21k
    bool valid_exe_path = NULL != loader_platform_executable_path(current_process_path, 1024);
748
749
1.21k
    cJSON* settings_object_iter = NULL;
750
1.94k
    cJSON_ArrayForEach(settings_object_iter, &settings_iter_parent) {
751
1.94k
        if (settings_object_iter->type != cJSON_Object) {
752
31
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
753
31
                       "Loader settings file from %s has a settings element that is not an object", settings_file_path);
754
31
            break;
755
31
        }
756
757
1.91k
        cJSON* app_keys = loader_cJSON_GetObjectItem(settings_object_iter, "app_keys");
758
1.91k
        if (NULL == app_keys) {
759
            // use the first 'global' settings that has no app keys as the global one
760
1.44k
            if (global_settings == NULL) {
761
1.11k
                global_settings = settings_object_iter;
762
1.11k
            }
763
1.44k
            continue;
764
1.44k
        }
765
        // No sense iterating if we couldn't get the executable path
766
472
        if (!valid_exe_path) {
767
0
            break;
768
0
        }
769
472
        cJSON* app_key = NULL;
770
1.19k
        cJSON_ArrayForEach(app_key, app_keys) {
771
1.19k
            char* app_key_str = loader_cJSON_GetStringValue(app_key);
772
1.19k
            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
1.19k
        }
777
778
        // Break if we have found a matching current_process_path
779
472
        if (NULL != settings_to_use) {
780
0
            break;
781
0
        }
782
472
    }
783
784
    // No app specific settings match - either use global settings or exit
785
1.21k
    if (settings_to_use == NULL) {
786
1.21k
        if (global_settings == NULL) {
787
103
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
788
103
                       "Loader settings file from %s missing global settings and none of the app specific settings matched the "
789
103
                       "current application - no loader settings will be active",
790
103
                       settings_file_path);
791
103
            goto out;  // No global settings were found - exit
792
1.11k
        } else {
793
1.11k
            settings_to_use = global_settings;  // Global settings are present - use it
794
1.11k
        }
795
1.21k
    }
796
797
    // optional
798
1.11k
    cJSON* stderr_filter = loader_cJSON_GetObjectItem(settings_to_use, "stderr_log");
799
1.11k
    if (NULL != stderr_filter) {
800
276
        struct loader_string_list stderr_log = {0};
801
276
        VkResult stderr_log_result = VK_SUCCESS;
802
276
        stderr_log_result = loader_parse_json_array_of_strings(inst, settings_to_use, "stderr_log", &stderr_log);
803
276
        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
276
        loader_settings->debug_level = parse_log_filters_from_strings(&stderr_log);
808
276
        free_string_list(inst, &stderr_log);
809
276
    }
810
811
    // optional
812
1.11k
    cJSON* logs_to_use = loader_cJSON_GetObjectItem(settings_to_use, "log_locations");
813
1.11k
    if (NULL != logs_to_use) {
814
38
        cJSON* log_element = NULL;
815
595
        cJSON_ArrayForEach(log_element, logs_to_use) {
816
            // bool is_valid = true;
817
595
            struct loader_string_list log_destinations = {0};
818
595
            VkResult parse_dest_res = loader_parse_json_array_of_strings(inst, log_element, "destinations", &log_destinations);
819
595
            if (parse_dest_res != VK_SUCCESS) {
820
                // is_valid = false;
821
591
            }
822
595
            free_string_list(inst, &log_destinations);
823
595
            struct loader_string_list log_filters = {0};
824
595
            VkResult parse_filters_res = loader_parse_json_array_of_strings(inst, log_element, "filters", &log_filters);
825
595
            if (parse_filters_res != VK_SUCCESS) {
826
                // is_valid = false;
827
420
            }
828
595
            free_string_list(inst, &log_filters);
829
595
        }
830
38
    }
831
832
1.11k
    VkResult layer_configurations_res = parse_layer_configurations(inst, settings_to_use, loader_settings);
833
1.11k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == layer_configurations_res) {
834
0
        res = layer_configurations_res;
835
0
        goto out;
836
0
    }
837
838
    // Determine if there exists a layer configuration indicating where to put layers not contained in the settings file
839
    // LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION
840
5.36k
    for (uint32_t i = 0; i < loader_settings->layer_configuration_count; i++) {
841
4.63k
        if (loader_settings->layer_configurations[i].control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
842
388
            loader_settings->has_unordered_layer_location = true;
843
388
            break;
844
388
        }
845
4.63k
    }
846
847
1.11k
    VkResult additional_drivers_res = parse_additional_drivers(inst, settings_to_use, loader_settings);
848
1.11k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == additional_drivers_res) {
849
0
        res = additional_drivers_res;
850
0
        goto out;
851
0
    }
852
853
1.11k
    VkResult device_configurations_res = parse_device_configurations(inst, settings_to_use, loader_settings);
854
1.11k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == device_configurations_res) {
855
0
        res = device_configurations_res;
856
0
        goto out;
857
0
    }
858
859
    // Only consider the settings active if there is at least one "setting" active.
860
    // Those are either logging, layers, additional_drivers, or device_configurations.
861
1.11k
    if (loader_settings->debug_level != 0 || loader_settings->layer_configurations_active ||
862
805
        loader_settings->additional_driver_count != 0 || loader_settings->device_configurations_active) {
863
805
        loader_settings->settings_file_path = settings_file_path;
864
805
        settings_file_path = NULL;
865
805
        loader_settings->settings_active = true;
866
805
    } else {
867
311
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
868
311
                   "vk_loader_settings.json file found at \"%s\" but did not contain any valid settings.", settings_file_path);
869
311
    }
870
5.90k
out:
871
5.90k
    if (NULL != json) {
872
4.06k
        loader_cJSON_Delete(json);
873
4.06k
    }
874
875
5.90k
    loader_instance_heap_free(inst, settings_file_path);
876
877
5.90k
    loader_instance_heap_free(inst, file_format_version_string);
878
5.90k
    return res;
879
1.11k
}
880
881
5.90k
TEST_FUNCTION_EXPORT VkResult update_global_loader_settings(void) {
882
5.90k
    loader_settings settings = {0};
883
5.90k
    VkResult res = get_loader_settings(NULL, &settings);
884
5.90k
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
885
886
5.90k
    if (res == VK_SUCCESS) {
887
2.83k
        if (!check_if_settings_are_equal(&settings, &global_loader_settings)) {
888
1.11k
            log_settings(NULL, &settings);
889
1.11k
        }
890
2.83k
        free_loader_settings(NULL, &global_loader_settings);
891
2.83k
        memcpy(&global_loader_settings, &settings, sizeof(loader_settings));
892
2.83k
        if (global_loader_settings.settings_active && global_loader_settings.debug_level > 0) {
893
122
            loader_set_global_debug_level(global_loader_settings.debug_level);
894
122
        }
895
2.83k
    }
896
5.90k
    loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
897
5.90k
    return res;
898
5.90k
}
899
900
2
void init_global_loader_settings(void) {
901
2
    loader_platform_thread_create_mutex(&global_loader_settings_lock);
902
    // Free out the global settings in case the process was loaded & unloaded
903
2
    free_loader_settings(NULL, &global_loader_settings);
904
2
}
905
0
void teardown_global_loader_settings(void) {
906
0
    free_loader_settings(NULL, &global_loader_settings);
907
0
    loader_platform_thread_delete_mutex(&global_loader_settings_lock);
908
0
}
909
910
0
bool should_skip_logging_global_messages(VkFlags msg_type) {
911
0
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
912
0
    bool should_skip = global_loader_settings.settings_active && 0 != (msg_type & global_loader_settings.debug_level);
913
0
    loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
914
0
    return should_skip;
915
0
}
916
917
// Use this function to get the correct settings to use based on the context
918
// If inst is NULL - use the global settings and lock the mutex
919
// Else return the settings local to the instance - but do nto lock the mutex
920
11.8k
const loader_settings* get_current_settings_and_lock(const struct loader_instance* inst) {
921
11.8k
    if (inst) {
922
0
        return &inst->settings;
923
0
    }
924
11.8k
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
925
11.8k
    return &global_loader_settings;
926
11.8k
}
927
// Release the global settings lock if we are using the global settings - aka if inst is NULL
928
11.8k
void release_current_settings_lock(const struct loader_instance* inst) {
929
11.8k
    if (inst == NULL) {
930
11.8k
        loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
931
11.8k
    }
932
11.8k
}
933
934
TEST_FUNCTION_EXPORT VkResult get_settings_layers(const struct loader_instance* inst, struct loader_layer_list* settings_layers,
935
11.8k
                                                  bool* should_search_for_other_layers) {
936
11.8k
    VkResult res = VK_SUCCESS;
937
11.8k
    *should_search_for_other_layers = true;  // default to true
938
939
11.8k
    const loader_settings* settings = get_current_settings_and_lock(inst);
940
941
11.8k
    if (NULL == settings || !settings->settings_active || !settings->layer_configurations_active) {
942
8.14k
        goto out;
943
8.14k
    }
944
945
    // Assume the list doesn't contain LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION at first
946
3.66k
    *should_search_for_other_layers = false;
947
948
131k
    for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
949
128k
        loader_settings_layer_configuration* layer_config = &settings->layer_configurations[i];
950
951
        // If we encountered a layer that should be forced off, we add it to the settings_layers list but only
952
        // with the data required to compare it with layers not in the settings file (aka name and manifest path)
953
128k
        if (layer_config->control == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
954
29.1k
            struct loader_layer_properties props = {0};
955
29.1k
            props.settings_control_value = LOADER_SETTINGS_LAYER_CONTROL_OFF;
956
29.1k
            loader_strncpy(props.info.layerName, VK_MAX_EXTENSION_NAME_SIZE, layer_config->name, VK_MAX_EXTENSION_NAME_SIZE);
957
29.1k
            props.info.layerName[VK_MAX_EXTENSION_NAME_SIZE - 1] = '\0';
958
29.1k
            res = loader_copy_to_new_str(inst, layer_config->path, &props.manifest_file_name);
959
29.1k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
960
0
                goto out;
961
0
            }
962
29.1k
            res = loader_append_layer_property(inst, settings_layers, &props);
963
29.1k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
964
0
                loader_free_layer_properties(inst, &props);
965
0
                goto out;
966
0
            }
967
29.1k
            continue;
968
29.1k
        }
969
970
        // The special layer location that indicates where unordered layers should go only should have the
971
        // settings_control_value set - everything else should be NULL
972
99.0k
        if (layer_config->control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
973
85.3k
            struct loader_layer_properties props = {0};
974
85.3k
            props.settings_control_value = LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION;
975
85.3k
            res = loader_append_layer_property(inst, settings_layers, &props);
976
85.3k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
977
0
                loader_free_layer_properties(inst, &props);
978
0
                goto out;
979
0
            }
980
85.3k
            *should_search_for_other_layers = true;
981
85.3k
            continue;
982
85.3k
        }
983
984
13.7k
        if (layer_config->path == NULL) {
985
0
            continue;
986
0
        }
987
988
13.7k
        cJSON* json = NULL;
989
13.7k
        VkResult local_res = loader_get_json(inst, layer_config->path, &json);
990
13.7k
        if (VK_ERROR_OUT_OF_HOST_MEMORY == local_res) {
991
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
992
0
            goto out;
993
13.7k
        } else if (VK_SUCCESS != local_res || NULL == json) {
994
13.2k
            continue;
995
13.2k
        }
996
997
        // Makes it possible to know if a new layer was added or not, since the only return value is VkResult
998
443
        uint32_t count_before_adding = settings_layers->count;
999
1000
443
        local_res =
1001
443
            loader_add_layer_properties(inst, settings_layers, json, layer_config->treat_as_implicit_manifest, layer_config->path);
1002
443
        loader_cJSON_Delete(json);
1003
1004
        // If the error is anything other than out of memory we still want to try to load the other layers
1005
443
        if (VK_ERROR_OUT_OF_HOST_MEMORY == local_res) {
1006
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
1007
0
            goto out;
1008
443
        } else if (local_res != VK_SUCCESS || count_before_adding == settings_layers->count) {
1009
            // Indicates something was wrong with the layer, can't add it to the list
1010
318
            continue;
1011
318
        }
1012
1013
283
        for (uint32_t j = count_before_adding; j < settings_layers->count; j++) {
1014
158
            struct loader_layer_properties* newly_added_layer = &settings_layers->list[j];
1015
158
            newly_added_layer->settings_control_value = layer_config->control;
1016
            // If the manifest file found has a name that differs from the one in the settings, remove this layer from
1017
            // consideration
1018
158
            if (strncmp(newly_added_layer->info.layerName, layer_config->name, VK_MAX_EXTENSION_NAME_SIZE) != 0) {
1019
117
                loader_remove_layer_in_list(inst, settings_layers, j);
1020
117
                j--;
1021
117
                continue;
1022
117
            }
1023
158
            bool should_remove = false;
1024
            // Make sure the layer isn't already in the list
1025
69
            for (uint32_t k = 0; k < j; k++) {
1026
34
                if (0 == strncmp(settings_layers->list[k].info.layerName, newly_added_layer->info.layerName,
1027
34
                                 VK_MAX_EXTENSION_NAME_SIZE)) {
1028
22
                    if (0 == (newly_added_layer->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER) &&
1029
19
                        settings_layers->list[k].lib_name != NULL && newly_added_layer->lib_name != NULL &&
1030
19
                        strcmp(settings_layers->list[k].lib_name, newly_added_layer->lib_name) == 0) {
1031
6
                        should_remove = true;
1032
6
                        break;
1033
6
                    }
1034
22
                }
1035
34
            }
1036
41
            if (should_remove) {
1037
6
                loader_remove_layer_in_list(inst, settings_layers, j);
1038
6
                j--;
1039
6
            }
1040
41
        }
1041
125
    }
1042
1043
11.8k
out:
1044
11.8k
    release_current_settings_lock(inst);
1045
11.8k
    return res;
1046
3.66k
}
1047
1048
// Check if layers has an element with the same name.
1049
// LAYER_CONTROL_OFF layers are missing some fields, just make sure the layerName is the same
1050
// If layer_property is a meta layer, just make sure the layerName is the same
1051
// Skip comparing to UNORDERED_LAYER_LOCATION
1052
// If layer_property is a regular layer, check if the lib_path is the same.
1053
// Make sure that the lib_name pointers are non-null before calling strcmp.
1054
// consider_lib_path - true if comparison should include the library_path in distinguishing layers. Used to prevent duplicates
1055
// between settings file provided layers and default found layers
1056
bool check_if_layer_is_in_list(struct loader_layer_list* layer_list, struct loader_layer_properties* layer_property,
1057
20.0k
                               bool consider_lib_path) {
1058
    // If the layer is a meta layer, just check against the name
1059
38.0M
    for (uint32_t i = 0; i < layer_list->count; i++) {
1060
37.9M
        if (0 == strncmp(layer_list->list[i].info.layerName, layer_property->info.layerName, VK_MAX_EXTENSION_NAME_SIZE)) {
1061
37.9M
            if (layer_list->list[i].settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
1062
4.22k
                return true;
1063
4.22k
            }
1064
37.9M
            if (VK_LAYER_TYPE_FLAG_META_LAYER == (layer_property->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER)) {
1065
105
                return true;
1066
105
            }
1067
37.9M
            if (!consider_lib_path) {
1068
246
                return true;
1069
246
            }
1070
37.9M
            if (layer_list->list[i].lib_name && layer_property->lib_name) {
1071
42
                return strcmp(layer_list->list[i].lib_name, layer_property->lib_name) == 0;
1072
42
            }
1073
37.9M
        }
1074
37.9M
    }
1075
15.4k
    return false;
1076
20.0k
}
1077
1078
VkResult combine_settings_layers_with_regular_layers(const struct loader_instance* inst, struct loader_layer_list* settings_layers,
1079
                                                     struct loader_layer_list* regular_layers,
1080
10.0k
                                                     struct loader_layer_list* output_layers) {
1081
10.0k
    VkResult res = VK_SUCCESS;
1082
10.0k
    bool has_unordered_layer_location = false;
1083
10.0k
    uint32_t unordered_layer_location_index = 0;
1084
    // Location to put layers that aren't known to the settings file
1085
    // Find it here so we dont have to pass in a loader_settings struct
1086
12.2k
    for (uint32_t i = 0; i < settings_layers->count; i++) {
1087
3.18k
        if (settings_layers->list[i].settings_control_value == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
1088
929
            has_unordered_layer_location = true;
1089
929
            unordered_layer_location_index = i;
1090
929
            break;
1091
929
        }
1092
3.18k
    }
1093
1094
10.0k
    if (settings_layers->count == 0 && regular_layers->count == 0) {
1095
        // No layers to combine
1096
7.88k
        goto out;
1097
7.88k
    } else if (settings_layers->count == 0) {
1098
        // No settings layers - just copy regular to output_layers - memset regular layers to prevent double frees
1099
1.18k
        *output_layers = *regular_layers;
1100
1.18k
        memset(regular_layers, 0, sizeof(struct loader_layer_list));
1101
1.18k
        goto out;
1102
1.18k
    } else if (regular_layers->count == 0 || !has_unordered_layer_location) {
1103
        // No regular layers or has_unordered_layer_location is false - just copy settings to output_layers -
1104
        // memset settings layers to prevent double frees
1105
692
        *output_layers = *settings_layers;
1106
692
        memset(settings_layers, 0, sizeof(struct loader_layer_list));
1107
692
        goto out;
1108
692
    }
1109
1110
237
    res = loader_init_generic_list(inst, (struct loader_generic_list*)output_layers,
1111
237
                                   (settings_layers->count + regular_layers->count) * sizeof(struct loader_layer_properties));
1112
237
    if (VK_SUCCESS != res) {
1113
0
        goto out;
1114
0
    }
1115
1116
    // Insert the settings layers into output_layers up to unordered_layer_index
1117
822
    for (uint32_t i = 0; i < unordered_layer_location_index; i++) {
1118
585
        if (!check_if_layer_is_in_list(output_layers, &settings_layers->list[i], true)) {
1119
212
            res = loader_append_layer_property(inst, output_layers, &settings_layers->list[i]);
1120
212
            if (VK_SUCCESS != res) {
1121
0
                goto out;
1122
0
            }
1123
212
        }
1124
585
    }
1125
1126
946
    for (uint32_t i = 0; i < regular_layers->count; i++) {
1127
        // Check if its already been put in the output_layers list as well as the remaining settings_layers
1128
709
        bool regular_layer_is_ordered = check_if_layer_is_in_list(output_layers, &regular_layers->list[i], false) ||
1129
443
                                        check_if_layer_is_in_list(settings_layers, &regular_layers->list[i], false);
1130
        // If it isn't found, add it
1131
709
        if (!regular_layer_is_ordered) {
1132
388
            res = loader_append_layer_property(inst, output_layers, &regular_layers->list[i]);
1133
388
            if (VK_SUCCESS != res) {
1134
0
                goto out;
1135
0
            }
1136
388
        } else {
1137
            // layer is already ordered and can be safely freed
1138
321
            loader_free_layer_properties(inst, &regular_layers->list[i]);
1139
321
        }
1140
709
    }
1141
1142
    // Insert the rest of the settings layers into combined_layers from  unordered_layer_index to the end
1143
    // start at one after the unordered_layer_index
1144
18.5k
    for (uint32_t i = unordered_layer_location_index + 1; i < settings_layers->count; i++) {
1145
18.2k
        if (!check_if_layer_is_in_list(output_layers, &settings_layers->list[i], true)) {
1146
14.3k
            res = loader_append_layer_property(inst, output_layers, &settings_layers->list[i]);
1147
14.3k
            if (VK_SUCCESS != res) {
1148
0
                goto out;
1149
0
            }
1150
14.3k
        }
1151
18.2k
    }
1152
1153
10.0k
out:
1154
10.0k
    if (res != VK_SUCCESS) {
1155
0
        loader_delete_layer_list_and_properties(inst, output_layers);
1156
0
    }
1157
1158
10.0k
    return res;
1159
237
}
1160
1161
VkResult enable_correct_layers_from_settings(const struct loader_instance* inst, const struct loader_envvar_all_filters* filters,
1162
                                             uint32_t app_enabled_name_count, const char* const* app_enabled_names,
1163
                                             const struct loader_layer_list* instance_layers,
1164
                                             struct loader_pointer_layer_list* target_layer_list,
1165
0
                                             struct loader_pointer_layer_list* activated_layer_list) {
1166
0
    VkResult res = VK_SUCCESS;
1167
0
    char* vk_instance_layers_env = loader_getenv(ENABLED_LAYERS_ENV, inst);
1168
0
    size_t vk_instance_layers_env_len = 0;
1169
0
    char* vk_instance_layers_env_copy = NULL;
1170
0
    if (vk_instance_layers_env != NULL) {
1171
0
        vk_instance_layers_env_len = strlen(vk_instance_layers_env) + 1;
1172
0
        vk_instance_layers_env_copy = loader_stack_alloc(vk_instance_layers_env_len);
1173
0
        memset(vk_instance_layers_env_copy, 0, vk_instance_layers_env_len);
1174
1175
0
        loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0, "env var \'%s\' defined and adding layers: %s",
1176
0
                   ENABLED_LAYERS_ENV, vk_instance_layers_env);
1177
0
    }
1178
0
    for (uint32_t i = 0; i < instance_layers->count; i++) {
1179
0
        bool enable_layer = false;
1180
0
        struct loader_layer_properties* props = &instance_layers->list[i];
1181
1182
        // Skip the sentinel unordered layer location
1183
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
1184
0
            continue;
1185
0
        }
1186
1187
        // Do not enable the layer if the settings have it set as off
1188
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
1189
0
            continue;
1190
0
        }
1191
        // Force enable it based on settings
1192
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_ON) {
1193
0
            enable_layer = true;
1194
0
            props->enabled_by_what = ENABLED_BY_WHAT_LOADER_SETTINGS_FILE;
1195
0
        } else {
1196
            // Check if disable filter needs to skip the layer
1197
0
            if ((filters->disable_filter.disable_all || filters->disable_filter.disable_all_implicit ||
1198
0
                 check_name_matches_filter_environment_var(props->info.layerName, &filters->disable_filter.additional_filters)) &&
1199
0
                !check_name_matches_filter_environment_var(props->info.layerName, &filters->allow_filter)) {
1200
                // Report a message that we've forced off a layer if it would have been enabled normally.
1201
0
                loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0,
1202
0
                           "Layer \"%s\" forced disabled because name matches filter of env var \'%s\'.", props->info.layerName,
1203
0
                           VK_LAYERS_DISABLE_ENV_VAR);
1204
1205
0
                continue;
1206
0
            }
1207
0
        }
1208
        // Check the enable filter
1209
0
        if (!enable_layer && check_name_matches_filter_environment_var(props->info.layerName, &filters->enable_filter)) {
1210
0
            enable_layer = true;
1211
0
            props->enabled_by_what = ENABLED_BY_WHAT_VK_LOADER_LAYERS_ENABLE;
1212
0
            loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0,
1213
0
                       "Layer \"%s\" forced enabled due to env var \'%s\'.", props->info.layerName, VK_LAYERS_ENABLE_ENV_VAR);
1214
0
        }
1215
1216
        // First look for the old-fashion layers forced on with VK_INSTANCE_LAYERS
1217
0
        if (!enable_layer && vk_instance_layers_env && vk_instance_layers_env_copy && vk_instance_layers_env_len > 0) {
1218
            // Copy the env-var on each iteration, so that loader_get_next_path can correctly find the separators
1219
            // This solution only needs one stack allocation ahead of time rather than an allocation per layer in the
1220
            // env-var
1221
0
            loader_strncpy(vk_instance_layers_env_copy, vk_instance_layers_env_len, vk_instance_layers_env,
1222
0
                           vk_instance_layers_env_len);
1223
1224
0
            char* instance_layers_env_iter = vk_instance_layers_env_copy;
1225
0
            while (instance_layers_env_iter && *instance_layers_env_iter) {
1226
0
                char* next = loader_get_next_path(instance_layers_env_iter);
1227
0
                if (0 == strcmp(instance_layers_env_iter, props->info.layerName)) {
1228
0
                    enable_layer = true;
1229
0
                    props->enabled_by_what = ENABLED_BY_WHAT_VK_INSTANCE_LAYERS;
1230
0
                    break;
1231
0
                }
1232
0
                instance_layers_env_iter = next;
1233
0
            }
1234
0
        }
1235
1236
        // Check if it should be enabled by the application
1237
0
        if (!enable_layer) {
1238
0
            for (uint32_t j = 0; j < app_enabled_name_count; j++) {
1239
0
                if (strcmp(props->info.layerName, app_enabled_names[j]) == 0) {
1240
0
                    enable_layer = true;
1241
0
                    props->enabled_by_what = ENABLED_BY_WHAT_IN_APPLICATION_API;
1242
0
                    break;
1243
0
                }
1244
0
            }
1245
0
        }
1246
1247
        // Check if its an implicit layers and thus enabled by default
1248
0
        if (!enable_layer && (0 == (props->type_flags & VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER)) &&
1249
0
            loader_implicit_layer_is_enabled(inst, filters, props)) {
1250
0
            enable_layer = true;
1251
0
            props->enabled_by_what = ENABLED_BY_WHAT_IMPLICIT_LAYER;
1252
0
        }
1253
1254
0
        if (enable_layer) {
1255
            // Check if the layer is a meta layer reuse the existing function to add the meta layer
1256
0
            if (props->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER) {
1257
0
                res = loader_add_meta_layer(inst, filters, props, target_layer_list, activated_layer_list, instance_layers, NULL);
1258
0
                if (res == VK_ERROR_OUT_OF_HOST_MEMORY) goto out;
1259
0
            } else {
1260
0
                res = loader_add_layer_properties_to_list(inst, target_layer_list, props);
1261
0
                if (res != VK_SUCCESS) {
1262
0
                    goto out;
1263
0
                }
1264
0
                res = loader_add_layer_properties_to_list(inst, activated_layer_list, props);
1265
0
                if (res != VK_SUCCESS) {
1266
0
                    goto out;
1267
0
                }
1268
0
            }
1269
0
        }
1270
0
    }
1271
0
out:
1272
0
    if (vk_instance_layers_env != NULL) {
1273
0
        loader_free_getenv(vk_instance_layers_env, inst);
1274
0
    }
1275
1276
0
    return res;
1277
0
}
1278
1279
0
VkResult loader_settings_get_additional_driver_files(const struct loader_instance* inst, struct loader_string_list* out_files) {
1280
0
    VkResult res = VK_SUCCESS;
1281
1282
0
    const loader_settings* settings = get_current_settings_and_lock(inst);
1283
1284
0
    if (NULL == settings || !settings->settings_active) {
1285
0
        goto out;
1286
0
    }
1287
1288
0
    if (settings->additional_drivers_use_exclusively) {
1289
0
        free_string_list(inst, out_files);
1290
0
    }
1291
1292
0
    for (uint32_t i = 0; i < settings->additional_driver_count; i++) {
1293
0
        res = prepend_if_manifest_file(inst, settings->additional_drivers[i].path, out_files);
1294
0
    }
1295
1296
0
out:
1297
0
    release_current_settings_lock(inst);
1298
0
    return res;
1299
0
}
1300
1301
0
bool loader_settings_should_use_driver_environment_variables(const struct loader_instance* inst) {
1302
0
    bool should_use = true;
1303
0
    const loader_settings* settings = get_current_settings_and_lock(inst);
1304
0
    if (NULL == settings || !settings->settings_active) {
1305
0
        goto out;
1306
0
    }
1307
0
    if (settings->device_configurations_active) {
1308
0
        should_use = false;
1309
0
    }
1310
0
out:
1311
0
    release_current_settings_lock(inst);
1312
0
    return should_use;
1313
0
}