Coverage Report

Created: 2025-10-10 06:38

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