Coverage Report

Created: 2026-04-01 06:45

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