Coverage Report

Created: 2025-07-18 06:34

/src/vulkan-loader/loader/settings.c
Line
Count
Source (jump to first uncovered line)
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
192k
void free_layer_configuration(const struct loader_instance* inst, loader_settings_layer_configuration* layer_configuration) {
42
192k
    loader_instance_heap_free(inst, layer_configuration->name);
43
192k
    loader_instance_heap_free(inst, layer_configuration->path);
44
192k
    memset(layer_configuration, 0, sizeof(loader_settings_layer_configuration));
45
192k
}
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.6k
void free_loader_settings(const struct loader_instance* inst, loader_settings* settings) {
58
12.6k
    if (NULL != settings->layer_configurations) {
59
117k
        for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
60
117k
            free_layer_configuration(inst, &settings->layer_configurations[i]);
61
117k
        }
62
612
        loader_instance_heap_free(inst, settings->layer_configurations);
63
612
    }
64
12.6k
    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.6k
    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.6k
    loader_instance_heap_free(inst, settings->settings_file_path);
77
12.6k
    memset(settings, 0, sizeof(loader_settings));
78
12.6k
}
79
80
121k
loader_settings_layer_control parse_control_string(char* control_string) {
81
121k
    loader_settings_layer_control layer_control = LOADER_SETTINGS_LAYER_CONTROL_DEFAULT;
82
121k
    if (strcmp(control_string, "auto") == 0)
83
860
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_DEFAULT;
84
120k
    else if (strcmp(control_string, "on") == 0)
85
343
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_ON;
86
120k
    else if (strcmp(control_string, "off") == 0)
87
57.0k
        layer_control = LOADER_SETTINGS_LAYER_CONTROL_OFF;
88
63.2k
    else if (strcmp(control_string, "unordered_layer_location") == 0)
89
47.0k
        layer_control = LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION;
90
121k
    return layer_control;
91
121k
}
92
93
117k
const char* loader_settings_layer_control_to_string(loader_settings_layer_control control) {
94
117k
    switch (control) {
95
14.1k
        case (LOADER_SETTINGS_LAYER_CONTROL_DEFAULT):
96
14.1k
            return "auto";
97
16
        case (LOADER_SETTINGS_LAYER_CONTROL_ON):
98
16
            return "on";
99
56.7k
        case (LOADER_SETTINGS_LAYER_CONTROL_OFF):
100
56.7k
            return "off";
101
46.2k
        case (LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION):
102
46.2k
            return "unordered_layer_location";
103
0
        default:
104
0
            return "UNKNOWN_LAYER_CONTROl";
105
117k
    }
106
117k
}
107
108
1.49k
uint32_t parse_log_filters_from_strings(struct loader_string_list* log_filters) {
109
1.49k
    uint32_t filters = 0;
110
254k
    for (uint32_t i = 0; i < log_filters->count; i++) {
111
252k
        if (strcmp(log_filters->list[i], "all") == 0)
112
983
            filters |= VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_PERF_BIT | VULKAN_LOADER_ERROR_BIT |
113
983
                       VULKAN_LOADER_DEBUG_BIT | VULKAN_LOADER_LAYER_BIT | VULKAN_LOADER_DRIVER_BIT | VULKAN_LOADER_VALIDATION_BIT;
114
251k
        else if (strcmp(log_filters->list[i], "info") == 0)
115
1.19k
            filters |= VULKAN_LOADER_INFO_BIT;
116
250k
        else if (strcmp(log_filters->list[i], "warn") == 0)
117
11.7k
            filters |= VULKAN_LOADER_WARN_BIT;
118
238k
        else if (strcmp(log_filters->list[i], "perf") == 0)
119
1.07k
            filters |= VULKAN_LOADER_PERF_BIT;
120
237k
        else if (strcmp(log_filters->list[i], "error") == 0)
121
1.15k
            filters |= VULKAN_LOADER_ERROR_BIT;
122
236k
        else if (strcmp(log_filters->list[i], "debug") == 0)
123
1.03k
            filters |= VULKAN_LOADER_DEBUG_BIT;
124
235k
        else if (strcmp(log_filters->list[i], "layer") == 0)
125
828
            filters |= VULKAN_LOADER_LAYER_BIT;
126
234k
        else if (strcmp(log_filters->list[i], "driver") == 0)
127
1.78k
            filters |= VULKAN_LOADER_DRIVER_BIT;
128
232k
        else if (strcmp(log_filters->list[i], "validation") == 0)
129
1.27k
            filters |= VULKAN_LOADER_VALIDATION_BIT;
130
252k
    }
131
1.49k
    return filters;
132
1.49k
}
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
121k
                                   loader_settings_layer_configuration* layer_configuration) {
148
121k
    char* control_string = NULL;
149
121k
    VkResult res = loader_parse_json_string(layer_configuration_json, "control", &control_string);
150
121k
    if (res != VK_SUCCESS) {
151
34
        goto out;
152
34
    }
153
121k
    layer_configuration->control = parse_control_string(control_string);
154
121k
    loader_instance_heap_free(inst, control_string);
155
156
    // If that is the only value - do no further parsing
157
121k
    if (layer_configuration->control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
158
47.0k
        goto out;
159
47.0k
    }
160
161
74.4k
    res = loader_parse_json_string(layer_configuration_json, "name", &(layer_configuration->name));
162
74.4k
    if (res != VK_SUCCESS) {
163
684
        goto out;
164
684
    }
165
166
73.7k
    res = loader_parse_json_string(layer_configuration_json, "path", &(layer_configuration->path));
167
73.7k
    if (res != VK_SUCCESS) {
168
34
        goto out;
169
34
    }
170
171
73.7k
    cJSON* treat_as_implicit_manifest = loader_cJSON_GetObjectItem(layer_configuration_json, "treat_as_implicit_manifest");
172
73.7k
    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
121k
out:
176
121k
    if (VK_SUCCESS != res) {
177
752
        free_layer_configuration(inst, layer_configuration);
178
752
    }
179
121k
    return res;
180
73.7k
}
181
182
3.12k
VkResult parse_layer_configurations(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
183
3.12k
    VkResult res = VK_SUCCESS;
184
185
3.12k
    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.12k
    if (NULL == layer_configurations) {
188
1.65k
        return VK_SUCCESS;
189
1.65k
    }
190
191
1.46k
    uint32_t layer_configurations_count = loader_cJSON_GetArraySize(layer_configurations);
192
1.46k
    if (layer_configurations_count == 0) {
193
34
        return VK_SUCCESS;
194
34
    }
195
196
1.43k
    loader_settings->layer_configuration_count = layer_configurations_count;
197
198
1.43k
    loader_settings->layer_configurations = loader_instance_heap_calloc(
199
1.43k
        inst, sizeof(loader_settings_layer_configuration) * layer_configurations_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
200
1.43k
    if (NULL == loader_settings->layer_configurations) {
201
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
202
0
        goto out;
203
0
    }
204
205
1.43k
    cJSON* layer = NULL;
206
1.43k
    size_t i = 0;
207
121k
    cJSON_ArrayForEach(layer, layer_configurations) {
208
121k
        if (layer->type != cJSON_Object) {
209
68
            res = VK_ERROR_INITIALIZATION_FAILED;
210
68
            goto out;
211
68
        }
212
121k
        res = parse_layer_configuration(inst, layer, &(loader_settings->layer_configurations[i++]));
213
121k
        if (VK_SUCCESS != res) {
214
752
            goto out;
215
752
        }
216
121k
    }
217
1.43k
out:
218
1.43k
    if (res != VK_SUCCESS) {
219
820
        if (loader_settings->layer_configurations) {
220
75.4k
            for (size_t index = 0; index < loader_settings->layer_configuration_count; index++) {
221
74.6k
                free_layer_configuration(inst, &(loader_settings->layer_configurations[index]));
222
74.6k
            }
223
820
            loader_settings->layer_configuration_count = 0;
224
820
            loader_instance_heap_free(inst, loader_settings->layer_configurations);
225
820
            loader_settings->layer_configurations = NULL;
226
820
        }
227
820
    }
228
229
1.43k
    return res;
230
1.43k
}
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.12k
VkResult parse_additional_drivers(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
247
3.12k
    VkResult res = VK_SUCCESS;
248
249
3.12k
    cJSON* additional_drivers_use_exclusively_json =
250
3.12k
        loader_cJSON_GetObjectItem(settings_object, "additional_drivers_use_exclusively");
251
3.12k
    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.12k
    cJSON* additional_drivers_json = loader_cJSON_GetObjectItem(settings_object, "additional_drivers");
256
3.12k
    if (NULL == additional_drivers_json) {
257
3.12k
        return VK_SUCCESS;
258
3.12k
    }
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
VkResult parse_device_configuration(const struct loader_instance* inst, cJSON* device_configuration_json,
301
0
                                    loader_settings_device_configuration* device_configuration) {
302
0
    (void)inst;
303
0
    VkResult res = VK_SUCCESS;
304
0
    cJSON* deviceUUID_array = loader_cJSON_GetObjectItem(device_configuration_json, "deviceUUID");
305
0
    if (NULL == deviceUUID_array) {
306
0
        res = VK_ERROR_INITIALIZATION_FAILED;
307
0
        goto out;
308
0
    }
309
0
    if (VK_UUID_SIZE != loader_cJSON_GetArraySize(deviceUUID_array)) {
310
0
        res = VK_ERROR_INITIALIZATION_FAILED;
311
0
        goto out;
312
0
    }
313
314
0
    cJSON* uuid_field = NULL;
315
0
    size_t i = 0;
316
0
    cJSON_ArrayForEach(uuid_field, deviceUUID_array) {
317
0
        if (uuid_field->type != cJSON_Number) {
318
0
            res = VK_ERROR_INITIALIZATION_FAILED;
319
0
            goto out;
320
0
        }
321
0
        if (uuid_field->valueint < 0 || uuid_field->valueint > 255) {
322
0
            res = VK_ERROR_INITIALIZATION_FAILED;
323
0
            goto out;
324
0
        }
325
0
        device_configuration->deviceUUID[i] = (uint8_t)uuid_field->valueint;
326
0
        i++;
327
0
    }
328
329
0
    VkResult deviceNameRes = loader_parse_json_string_to_existing_str(
330
0
        device_configuration_json, "deviceName", VK_MAX_PHYSICAL_DEVICE_NAME_SIZE, device_configuration->deviceName);
331
0
    if (VK_ERROR_OUT_OF_HOST_MEMORY == deviceNameRes) {
332
0
        res = deviceNameRes;
333
0
        goto out;
334
0
    }
335
0
out:
336
0
    if (res != VK_SUCCESS) {
337
0
        memset(device_configuration, 0, sizeof(loader_settings_device_configuration));
338
0
    }
339
0
    return res;
340
0
}
341
342
3.12k
VkResult parse_device_configurations(const struct loader_instance* inst, cJSON* settings_object, loader_settings* loader_settings) {
343
3.12k
    VkResult res = VK_SUCCESS;
344
345
3.12k
    cJSON* device_configurations = loader_cJSON_GetObjectItem(settings_object, "device_configurations");
346
3.12k
    if (NULL == device_configurations) {
347
3.12k
        return VK_SUCCESS;
348
3.12k
    }
349
350
0
    uint32_t device_configuration_count = loader_cJSON_GetArraySize(device_configurations);
351
0
    if (device_configuration_count == 0) {
352
0
        return VK_SUCCESS;
353
0
    }
354
355
0
    loader_settings->device_configuration_count = device_configuration_count;
356
357
0
    loader_settings->device_configurations = loader_instance_heap_calloc(
358
0
        inst, sizeof(loader_settings_device_configuration) * device_configuration_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
359
0
    if (NULL == loader_settings->device_configurations) {
360
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
361
0
        goto out;
362
0
    }
363
364
0
    cJSON* device = NULL;
365
0
    size_t i = 0;
366
0
    cJSON_ArrayForEach(device, device_configurations) {
367
0
        if (device->type != cJSON_Object) {
368
0
            res = VK_ERROR_INITIALIZATION_FAILED;
369
0
            goto out;
370
0
        }
371
0
        res = parse_device_configuration(inst, device, &(loader_settings->device_configurations[i++]));
372
0
        if (VK_SUCCESS != res) {
373
0
            goto out;
374
0
        }
375
0
    }
376
0
out:
377
0
    if (res != VK_SUCCESS) {
378
0
        if (loader_settings->device_configurations) {
379
0
            for (size_t index = 0; index < loader_settings->device_configuration_count; index++) {
380
0
                free_device_configuration(inst, &(loader_settings->device_configurations[index]));
381
0
            }
382
0
            loader_settings->device_configuration_count = 0;
383
0
            loader_instance_heap_free(inst, loader_settings->device_configurations);
384
0
            loader_settings->device_configurations = NULL;
385
0
        }
386
0
    }
387
0
    return res;
388
0
}
389
390
VkResult check_if_settings_path_exists(const struct loader_instance* inst, const char* base, const char* suffix,
391
11.5k
                                       char** settings_file_path) {
392
11.5k
    if (NULL == base || NULL == suffix) {
393
1.00k
        return VK_ERROR_INITIALIZATION_FAILED;
394
1.00k
    }
395
10.5k
    size_t base_len = strlen(base);
396
10.5k
    size_t suffix_len = strlen(suffix);
397
10.5k
    size_t path_len = base_len + suffix_len + 1;
398
10.5k
    *settings_file_path = loader_instance_heap_calloc(inst, path_len, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
399
10.5k
    if (NULL == *settings_file_path) {
400
0
        return VK_ERROR_OUT_OF_HOST_MEMORY;
401
0
    }
402
10.5k
    loader_strncpy(*settings_file_path, path_len, base, base_len);
403
10.5k
    loader_strncat(*settings_file_path, path_len, suffix, suffix_len);
404
405
10.5k
    if (!loader_platform_file_exists(*settings_file_path)) {
406
2.00k
        loader_instance_heap_free(inst, *settings_file_path);
407
2.00k
        *settings_file_path = NULL;
408
2.00k
        return VK_ERROR_INITIALIZATION_FAILED;
409
2.00k
    }
410
8.50k
    return VK_SUCCESS;
411
10.5k
}
412
9.51k
VkResult get_unix_settings_path(const struct loader_instance* inst, char** settings_file_path) {
413
9.51k
    VkResult res =
414
9.51k
        check_if_settings_path_exists(inst, loader_secure_getenv("HOME", inst),
415
9.51k
                                      "/.local/share/vulkan/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
416
9.51k
    if (res == VK_SUCCESS) {
417
8.50k
        return res;
418
8.50k
    }
419
    // If HOME isn't set, fallback to XDG_DATA_HOME
420
1.00k
    res = check_if_settings_path_exists(inst, loader_secure_getenv("XDG_DATA_HOME", inst),
421
1.00k
                                        "/vulkan/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME, settings_file_path);
422
1.00k
    if (res == VK_SUCCESS) {
423
0
        return res;
424
0
    }
425
    // if XDG_DATA_HOME isn't set, fallback to /etc.
426
    // note that the settings_fil_path_suffix stays the same since its the same layout as for XDG_DATA_HOME
427
1.00k
    return check_if_settings_path_exists(inst, "/etc", "/vulkan/loader_settings.d/" VK_LOADER_SETTINGS_FILENAME,
428
1.00k
                                         settings_file_path);
429
1.00k
}
430
431
0
bool check_if_layer_configurations_are_equal(loader_settings_layer_configuration* a, loader_settings_layer_configuration* b) {
432
0
    if (!a->name || !b->name || 0 != strcmp(a->name, b->name)) {
433
0
        return false;
434
0
    }
435
0
    if (!a->path || !b->path || 0 != strcmp(a->path, b->path)) {
436
0
        return false;
437
0
    }
438
0
    return a->control == b->control;
439
0
}
440
441
0
bool check_if_driver_configurations_are_equal(loader_settings_driver_configuration* a, loader_settings_driver_configuration* b) {
442
0
    if (!a->path || !b->path || 0 != strcmp(a->path, b->path)) {
443
0
        return false;
444
0
    }
445
0
    return true;
446
0
}
447
448
0
bool check_if_device_configurations_are_equal(loader_settings_device_configuration* a, loader_settings_device_configuration* b) {
449
0
    for (uint32_t i = 0; i < VK_UUID_SIZE; i++) {
450
0
        if (a->deviceUUID[i] != b->deviceUUID[i]) return false;
451
0
    }
452
0
    return true;
453
0
}
454
455
8.21k
bool check_if_settings_are_equal(loader_settings* a, loader_settings* b) {
456
    // If either pointer is null, return true
457
8.21k
    if (NULL == a || NULL == b) return false;
458
8.21k
    bool are_equal = true;
459
8.21k
    are_equal &= a->settings_active == b->settings_active;
460
8.21k
    are_equal &= a->has_unordered_layer_location == b->has_unordered_layer_location;
461
8.21k
    are_equal &= a->debug_level == b->debug_level;
462
8.21k
    are_equal &= a->layer_configuration_count == b->layer_configuration_count;
463
8.21k
    are_equal &= a->additional_driver_count == b->additional_driver_count;
464
8.21k
    are_equal &= a->device_configuration_count == b->device_configuration_count;
465
8.21k
    if (!are_equal) return false;
466
7.29k
    for (uint32_t i = 0; i < a->layer_configuration_count && i < b->layer_configuration_count; i++) {
467
0
        are_equal &= check_if_layer_configurations_are_equal(&a->layer_configurations[i], &b->layer_configurations[i]);
468
0
    }
469
7.29k
    for (uint32_t i = 0; i < a->additional_driver_count && i < b->additional_driver_count; i++) {
470
0
        are_equal &= check_if_driver_configurations_are_equal(&a->additional_drivers[i], &b->additional_drivers[i]);
471
0
    }
472
7.29k
    for (uint32_t i = 0; i < a->device_configuration_count && i < b->device_configuration_count; i++) {
473
0
        are_equal &= check_if_device_configurations_are_equal(&a->device_configurations[i], &b->device_configurations[i]);
474
0
    }
475
7.29k
    return are_equal;
476
8.21k
}
477
478
922
void log_settings(const struct loader_instance* inst, loader_settings* settings) {
479
922
    if (settings == NULL) {
480
0
        return;
481
0
    }
482
922
    loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "Using layer configurations found in loader settings from %s",
483
922
               settings->settings_file_path);
484
485
922
    char cmd_line_msg[64] = {0};
486
922
    size_t cmd_line_size = sizeof(cmd_line_msg);
487
488
922
    cmd_line_msg[0] = '\0';
489
490
922
    generate_debug_flag_str(settings->debug_level, cmd_line_size, cmd_line_msg);
491
922
    if (strlen(cmd_line_msg)) {
492
299
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Loader Settings Filters for Logging to Standard Error: %s", cmd_line_msg);
493
299
    }
494
495
922
    loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Layer Configurations count = %d", settings->layer_configuration_count);
496
118k
    for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
497
117k
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---- Layer Configuration [%d] ----", i);
498
117k
        if (settings->layer_configurations[i].control != LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
499
70.9k
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Name: %s", settings->layer_configurations[i].name);
500
70.9k
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Path: %s", settings->layer_configurations[i].path);
501
70.9k
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Layer Type: %s",
502
70.9k
                       settings->layer_configurations[i].treat_as_implicit_manifest ? "Implicit" : "Explicit");
503
70.9k
        }
504
117k
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Control: %s",
505
117k
                   loader_settings_layer_control_to_string(settings->layer_configurations[i].control));
506
117k
    }
507
922
    if (settings->additional_driver_count > 0) {
508
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "----");
509
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Use Additional Drivers Exclusively = %s",
510
0
                   settings->additional_drivers_use_exclusively ? "true" : "false");
511
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Additional Driver Configurations count = %d",
512
0
                   settings->additional_driver_count);
513
0
        for (uint32_t i = 0; i < settings->additional_driver_count; i++) {
514
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---- Driver Configuration [%d] ----", i);
515
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Path: %s", settings->additional_drivers[i].path);
516
0
        }
517
0
    }
518
922
    if (settings->device_configuration_count > 0) {
519
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "----");
520
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "Device Configurations count = %d", settings->device_configuration_count);
521
0
        for (uint32_t i = 0; i < settings->device_configuration_count; i++) {
522
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---- Device Configuration [%d] ----", i);
523
0
            uint8_t* id = settings->device_configurations[i].deviceUUID;
524
0
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "deviceUUID: %x%x%x%x-%x%x-%x%x-%x%x-%x%x%x%x%x%x", id[0], id[1], id[2],
525
0
                       id[3], id[4], id[5], id[6], id[7], id[8], id[9], id[10], id[11], id[12], id[13], id[14], id[15]);
526
0
        }
527
0
    }
528
922
    loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0, "---------------------------------");
529
922
}
530
531
// Loads the vk_loader_settings.json file
532
// Returns VK_SUCCESS if it was found & was successfully parsed. Otherwise, it returns VK_ERROR_INITIALIZATION_FAILED if it
533
// wasn't found or failed to parse, and returns VK_ERROR_OUT_OF_HOST_MEMORY if it was unable to allocate enough memory.
534
9.51k
VkResult get_loader_settings(const struct loader_instance* inst, loader_settings* loader_settings) {
535
9.51k
    VkResult res = VK_SUCCESS;
536
9.51k
    cJSON* json = NULL;
537
9.51k
    char* file_format_version_string = NULL;
538
9.51k
    char* settings_file_path = NULL;
539
#if defined(WIN32)
540
    res = windows_get_loader_settings_file_path(inst, &settings_file_path);
541
    if (res != VK_SUCCESS) {
542
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
543
                   "No valid vk_loader_settings.json file found, no loader settings will be active");
544
        goto out;
545
    }
546
547
#elif COMMON_UNIX_PLATFORMS
548
    res = get_unix_settings_path(inst, &settings_file_path);
549
9.51k
    if (res != VK_SUCCESS) {
550
1.00k
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
551
1.00k
                   "No valid vk_loader_settings.json file found, no loader settings will be active");
552
1.00k
        goto out;
553
1.00k
    }
554
#else
555
#warning "Unsupported platform - must specify platform specific location for vk_loader_settings.json"
556
#endif
557
558
8.50k
    res = loader_get_json(inst, settings_file_path, &json);
559
    // Make sure sure the top level json value is an object
560
8.50k
    if (res != VK_SUCCESS || NULL == json || json->type != cJSON_Object) {
561
3.86k
        goto out;
562
3.86k
    }
563
564
4.64k
    res = loader_parse_json_string(json, "file_format_version", &file_format_version_string);
565
4.64k
    if (res != VK_SUCCESS) {
566
73
        if (res != VK_ERROR_OUT_OF_HOST_MEMORY) {
567
73
            loader_log(
568
73
                inst, VULKAN_LOADER_DEBUG_BIT, 0,
569
73
                "Loader settings file from %s missing required field file_format_version - no loader settings will be active",
570
73
                settings_file_path);
571
73
        }
572
73
        goto out;
573
73
    }
574
575
    // Because the file may contain either a "settings_array" or a single "settings" object, we need to create a cJSON so that we
576
    // can iterate on both cases with common code
577
4.57k
    cJSON settings_iter_parent = {0};
578
579
4.57k
    cJSON* settings_array = loader_cJSON_GetObjectItem(json, "settings_array");
580
4.57k
    cJSON* single_settings_object = loader_cJSON_GetObjectItem(json, "settings");
581
4.57k
    if (NULL != settings_array) {
582
2.83k
        memcpy(&settings_iter_parent, settings_array, sizeof(cJSON));
583
2.83k
    } else if (NULL != single_settings_object) {
584
854
        settings_iter_parent.child = single_settings_object;
585
885
    } else if (settings_array == NULL && single_settings_object) {
586
0
        loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
587
0
                   "Loader settings file from %s missing required settings objects: Either one of the \"settings\" or "
588
0
                   "\"settings_array\" objects must be present - no loader settings will be active",
589
0
                   settings_file_path);
590
0
        res = VK_ERROR_INITIALIZATION_FAILED;
591
0
        goto out;
592
0
    }
593
594
    // Corresponds to the settings object that has no app keys
595
4.57k
    cJSON* global_settings = NULL;
596
    // Corresponds to the settings object which has a matching app key
597
4.57k
    cJSON* settings_to_use = NULL;
598
599
4.57k
    char current_process_path[1024];
600
4.57k
    bool valid_exe_path = NULL != loader_platform_executable_path(current_process_path, 1024);
601
602
4.57k
    cJSON* settings_object_iter = NULL;
603
6.35k
    cJSON_ArrayForEach(settings_object_iter, &settings_iter_parent) {
604
6.35k
        if (settings_object_iter->type != cJSON_Object) {
605
53
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
606
53
                       "Loader settings file from %s has a settings element that is not an object", settings_file_path);
607
53
            break;
608
53
        }
609
610
6.30k
        cJSON* app_keys = loader_cJSON_GetObjectItem(settings_object_iter, "app_keys");
611
6.30k
        if (NULL == app_keys) {
612
            // use the first 'global' settings that has no app keys as the global one
613
4.34k
            if (global_settings == NULL) {
614
3.12k
                global_settings = settings_object_iter;
615
3.12k
            }
616
4.34k
            continue;
617
4.34k
        }
618
        // No sense iterating if we couldn't get the executable path
619
1.96k
        if (!valid_exe_path) {
620
0
            break;
621
0
        }
622
1.96k
        cJSON* app_key = NULL;
623
3.68k
        cJSON_ArrayForEach(app_key, app_keys) {
624
3.68k
            char* app_key_str = loader_cJSON_GetStringValue(app_key);
625
3.68k
            if (app_key_str && strcmp(current_process_path, app_key_str) == 0) {
626
0
                settings_to_use = settings_object_iter;
627
0
                break;
628
0
            }
629
3.68k
        }
630
631
        // Break if we have found a matching current_process_path
632
1.96k
        if (NULL != settings_to_use) {
633
0
            break;
634
0
        }
635
1.96k
    }
636
637
    // No app specific settings match - either use global settings or exit
638
4.57k
    if (settings_to_use == NULL) {
639
4.57k
        if (global_settings == NULL) {
640
1.44k
            loader_log(inst, VULKAN_LOADER_DEBUG_BIT, 0,
641
1.44k
                       "Loader settings file from %s missing global settings and none of the app specific settings matched the "
642
1.44k
                       "current application - no loader settings will be active",
643
1.44k
                       settings_file_path);
644
1.44k
            goto out;  // No global settings were found - exit
645
3.12k
        } else {
646
3.12k
            settings_to_use = global_settings;  // Global settings are present - use it
647
3.12k
        }
648
4.57k
    }
649
650
    // optional
651
3.12k
    cJSON* stderr_filter = loader_cJSON_GetObjectItem(settings_to_use, "stderr_log");
652
3.12k
    if (NULL != stderr_filter) {
653
1.49k
        struct loader_string_list stderr_log = {0};
654
1.49k
        res = loader_parse_json_array_of_strings(inst, settings_to_use, "stderr_log", &stderr_log);
655
1.49k
        if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
656
0
            goto out;
657
0
        }
658
1.49k
        loader_settings->debug_level = parse_log_filters_from_strings(&stderr_log);
659
1.49k
        free_string_list(inst, &stderr_log);
660
1.49k
    }
661
662
    // optional
663
3.12k
    cJSON* logs_to_use = loader_cJSON_GetObjectItem(settings_to_use, "log_locations");
664
3.12k
    if (NULL != logs_to_use) {
665
191
        cJSON* log_element = NULL;
666
8.07k
        cJSON_ArrayForEach(log_element, logs_to_use) {
667
            // bool is_valid = true;
668
8.07k
            struct loader_string_list log_destinations = {0};
669
8.07k
            res = loader_parse_json_array_of_strings(inst, log_element, "destinations", &log_destinations);
670
8.07k
            if (res != VK_SUCCESS) {
671
                // is_valid = false;
672
8.05k
            }
673
8.07k
            free_string_list(inst, &log_destinations);
674
8.07k
            struct loader_string_list log_filters = {0};
675
8.07k
            res = loader_parse_json_array_of_strings(inst, log_element, "filters", &log_filters);
676
8.07k
            if (res != VK_SUCCESS) {
677
                // is_valid = false;
678
6.40k
            }
679
8.07k
            free_string_list(inst, &log_filters);
680
8.07k
        }
681
191
    }
682
683
3.12k
    VkResult layer_configurations_res = parse_layer_configurations(inst, settings_to_use, loader_settings);
684
3.12k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == layer_configurations_res) {
685
0
        res = layer_configurations_res;
686
0
        goto out;
687
0
    }
688
689
    // Determine if there exists a layer configuration indicating where to put layers not contained in the settings file
690
    // LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION
691
73.9k
    for (uint32_t i = 0; i < loader_settings->layer_configuration_count; i++) {
692
70.9k
        if (loader_settings->layer_configurations[i].control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
693
92
            loader_settings->has_unordered_layer_location = true;
694
92
            break;
695
92
        }
696
70.9k
    }
697
698
3.12k
    VkResult additional_drivers_res = parse_additional_drivers(inst, settings_to_use, loader_settings);
699
3.12k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == additional_drivers_res) {
700
0
        res = additional_drivers_res;
701
0
        goto out;
702
0
    }
703
704
3.12k
    VkResult device_configurations_res = parse_device_configurations(inst, settings_to_use, loader_settings);
705
3.12k
    if (VK_ERROR_OUT_OF_HOST_MEMORY == device_configurations_res) {
706
0
        res = device_configurations_res;
707
0
        goto out;
708
0
    }
709
710
    // Only consider the settings active if there is at least one "setting" active.
711
    // Those are either logging, layers, additional_drivers, or device_configurations.
712
3.12k
    if (loader_settings->debug_level != 0 || loader_settings->layer_configuration_count != 0 ||
713
3.12k
        loader_settings->additional_driver_count != 0 || loader_settings->device_configuration_count != 0) {
714
936
        loader_settings->settings_file_path = settings_file_path;
715
936
        settings_file_path = NULL;
716
936
        loader_settings->settings_active = true;
717
2.18k
    } else {
718
2.18k
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
719
2.18k
                   "vk_loader_settings.json file found at \"%s\" but did not contain any valid settings.", settings_file_path);
720
2.18k
    }
721
9.51k
out:
722
9.51k
    if (NULL != json) {
723
5.55k
        loader_cJSON_Delete(json);
724
5.55k
    }
725
726
9.51k
    loader_instance_heap_free(inst, settings_file_path);
727
728
9.51k
    loader_instance_heap_free(inst, file_format_version_string);
729
9.51k
    return res;
730
3.12k
}
731
732
9.51k
TEST_FUNCTION_EXPORT VkResult update_global_loader_settings(void) {
733
9.51k
    loader_settings settings = {0};
734
9.51k
    VkResult res = get_loader_settings(NULL, &settings);
735
9.51k
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
736
737
9.51k
    free_loader_settings(NULL, &global_loader_settings);
738
9.51k
    if (res == VK_SUCCESS) {
739
8.21k
        if (!check_if_settings_are_equal(&settings, &global_loader_settings)) {
740
922
            log_settings(NULL, &settings);
741
922
        }
742
743
8.21k
        memcpy(&global_loader_settings, &settings, sizeof(loader_settings));
744
8.21k
        if (global_loader_settings.settings_active && global_loader_settings.debug_level > 0) {
745
326
            loader_set_global_debug_level(global_loader_settings.debug_level);
746
326
        }
747
8.21k
    }
748
9.51k
    loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
749
9.51k
    return res;
750
9.51k
}
751
752
2
void init_global_loader_settings(void) {
753
2
    loader_platform_thread_create_mutex(&global_loader_settings_lock);
754
    // Free out the global settings in case the process was loaded & unloaded
755
2
    free_loader_settings(NULL, &global_loader_settings);
756
2
}
757
3.17k
void teardown_global_loader_settings(void) {
758
3.17k
    free_loader_settings(NULL, &global_loader_settings);
759
3.17k
    loader_platform_thread_delete_mutex(&global_loader_settings_lock);
760
3.17k
}
761
762
3.17k
bool should_skip_logging_global_messages(VkFlags msg_type) {
763
3.17k
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
764
3.17k
    bool should_skip = global_loader_settings.settings_active && 0 != (msg_type & global_loader_settings.debug_level);
765
3.17k
    loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
766
3.17k
    return should_skip;
767
3.17k
}
768
769
// Use this function to get the correct settings to use based on the context
770
// If inst is NULL - use the global settings and lock the mutex
771
// Else return the settings local to the instance - but do nto lock the mutex
772
6.34k
const loader_settings* get_current_settings_and_lock(const struct loader_instance* inst) {
773
6.34k
    if (inst) {
774
0
        return &inst->settings;
775
0
    }
776
6.34k
    loader_platform_thread_lock_mutex(&global_loader_settings_lock);
777
6.34k
    return &global_loader_settings;
778
6.34k
}
779
// Release the global settings lock if we are using the global settings - aka if inst is NULL
780
6.34k
void release_current_settings_lock(const struct loader_instance* inst) {
781
6.34k
    if (inst == NULL) {
782
6.34k
        loader_platform_thread_unlock_mutex(&global_loader_settings_lock);
783
6.34k
    }
784
6.34k
}
785
786
TEST_FUNCTION_EXPORT VkResult get_settings_layers(const struct loader_instance* inst, struct loader_layer_list* settings_layers,
787
3.17k
                                                  bool* should_search_for_other_layers) {
788
3.17k
    VkResult res = VK_SUCCESS;
789
3.17k
    *should_search_for_other_layers = true;  // default to true
790
791
3.17k
    const loader_settings* settings = get_current_settings_and_lock(inst);
792
793
3.17k
    if (NULL == settings || !settings->settings_active) {
794
2.85k
        goto out;
795
2.85k
    }
796
797
    // Assume the list doesn't contain LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION at first
798
312
    *should_search_for_other_layers = false;
799
800
33.6k
    for (uint32_t i = 0; i < settings->layer_configuration_count; i++) {
801
33.3k
        loader_settings_layer_configuration* layer_config = &settings->layer_configurations[i];
802
803
        // If we encountered a layer that should be forced off, we add it to the settings_layers list but only
804
        // with the data required to compare it with layers not in the settings file (aka name and manifest path)
805
33.3k
        if (layer_config->control == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
806
13.0k
            struct loader_layer_properties props = {0};
807
13.0k
            props.settings_control_value = LOADER_SETTINGS_LAYER_CONTROL_OFF;
808
13.0k
            loader_strncpy(props.info.layerName, VK_MAX_EXTENSION_NAME_SIZE, layer_config->name, VK_MAX_EXTENSION_NAME_SIZE);
809
13.0k
            props.info.layerName[VK_MAX_EXTENSION_NAME_SIZE - 1] = '\0';
810
13.0k
            res = loader_copy_to_new_str(inst, layer_config->path, &props.manifest_file_name);
811
13.0k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
812
0
                goto out;
813
0
            }
814
13.0k
            res = loader_append_layer_property(inst, settings_layers, &props);
815
13.0k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
816
0
                loader_free_layer_properties(inst, &props);
817
0
                goto out;
818
0
            }
819
13.0k
            continue;
820
13.0k
        }
821
822
        // The special layer location that indicates where unordered layers should go only should have the
823
        // settings_control_value set - everything else should be NULL
824
20.3k
        if (layer_config->control == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
825
15.4k
            struct loader_layer_properties props = {0};
826
15.4k
            props.settings_control_value = LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION;
827
15.4k
            res = loader_append_layer_property(inst, settings_layers, &props);
828
15.4k
            if (VK_ERROR_OUT_OF_HOST_MEMORY == res) {
829
0
                loader_free_layer_properties(inst, &props);
830
0
                goto out;
831
0
            }
832
15.4k
            *should_search_for_other_layers = true;
833
15.4k
            continue;
834
15.4k
        }
835
836
4.93k
        if (layer_config->path == NULL) {
837
0
            continue;
838
0
        }
839
840
4.93k
        cJSON* json = NULL;
841
4.93k
        VkResult local_res = loader_get_json(inst, layer_config->path, &json);
842
4.93k
        if (VK_ERROR_OUT_OF_HOST_MEMORY == local_res) {
843
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
844
0
            goto out;
845
4.93k
        } else if (VK_SUCCESS != local_res || NULL == json) {
846
4.88k
            continue;
847
4.88k
        }
848
849
        // Makes it possible to know if a new layer was added or not, since the only return value is VkResult
850
48
        size_t count_before_adding = settings_layers->count;
851
852
48
        local_res =
853
48
            loader_add_layer_properties(inst, settings_layers, json, layer_config->treat_as_implicit_manifest, layer_config->path);
854
48
        loader_cJSON_Delete(json);
855
856
        // If the error is anything other than out of memory we still want to try to load the other layers
857
48
        if (VK_ERROR_OUT_OF_HOST_MEMORY == local_res) {
858
0
            res = VK_ERROR_OUT_OF_HOST_MEMORY;
859
0
            goto out;
860
48
        } else if (local_res != VK_SUCCESS || count_before_adding == settings_layers->count) {
861
            // Indicates something was wrong with the layer, can't add it to the list
862
17
            continue;
863
17
        }
864
865
31
        struct loader_layer_properties* newly_added_layer = &settings_layers->list[settings_layers->count - 1];
866
31
        newly_added_layer->settings_control_value = layer_config->control;
867
        // If the manifest file found has a name that differs from the one in the settings, remove this layer from
868
        // consideration
869
31
        bool should_remove = false;
870
31
        if (strncmp(newly_added_layer->info.layerName, layer_config->name, VK_MAX_EXTENSION_NAME_SIZE) != 0) {
871
28
            should_remove = true;
872
28
            loader_remove_layer_in_list(inst, settings_layers, settings_layers->count - 1);
873
28
        }
874
        // Make sure the layer isn't already in the list
875
200
        for (uint32_t j = 0; settings_layers->count > 0 && j < settings_layers->count - 1; j++) {
876
169
            if (0 ==
877
169
                strncmp(settings_layers->list[j].info.layerName, newly_added_layer->info.layerName, VK_MAX_EXTENSION_NAME_SIZE)) {
878
0
                if (0 == (newly_added_layer->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER) &&
879
0
                    strcmp(settings_layers->list[j].lib_name, newly_added_layer->lib_name) == 0) {
880
0
                    should_remove = true;
881
0
                    break;
882
0
                }
883
0
            }
884
169
        }
885
31
        if (should_remove) {
886
28
            loader_remove_layer_in_list(inst, settings_layers, settings_layers->count - 1);
887
28
        }
888
31
    }
889
890
3.17k
out:
891
3.17k
    release_current_settings_lock(inst);
892
3.17k
    return res;
893
312
}
894
895
// Check if layers has an element with the same name.
896
// LAYER_CONTROL_OFF layers are missing some fields, just make sure the layerName is the same
897
// If layer_property is a meta layer, just make sure the layerName is the same
898
// Skip comparing to UNORDERED_LAYER_LOCATION
899
// If layer_property is a regular layer, check if the lib_path is the same.
900
// Make sure that the lib_name pointers are non-null before calling strcmp.
901
0
bool check_if_layer_is_in_list(struct loader_layer_list* layer_list, struct loader_layer_properties* layer_property) {
902
    // If the layer is a meta layer, just check against the name
903
0
    for (uint32_t i = 0; i < layer_list->count; i++) {
904
0
        if (0 == strncmp(layer_list->list[i].info.layerName, layer_property->info.layerName, VK_MAX_EXTENSION_NAME_SIZE)) {
905
0
            if (layer_list->list[i].settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
906
0
                return true;
907
0
            }
908
0
            if (VK_LAYER_TYPE_FLAG_META_LAYER == (layer_property->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER)) {
909
0
                return true;
910
0
            }
911
0
            if (layer_list->list[i].lib_name && layer_property->lib_name) {
912
0
                return strcmp(layer_list->list[i].lib_name, layer_property->lib_name) == 0;
913
0
            }
914
0
        }
915
0
    }
916
0
    return false;
917
0
}
918
919
VkResult combine_settings_layers_with_regular_layers(const struct loader_instance* inst, struct loader_layer_list* settings_layers,
920
                                                     struct loader_layer_list* regular_layers,
921
0
                                                     struct loader_layer_list* output_layers) {
922
0
    VkResult res = VK_SUCCESS;
923
0
    bool has_unordered_layer_location = false;
924
0
    uint32_t unordered_layer_location_index = 0;
925
    // Location to put layers that aren't known to the settings file
926
    // Find it here so we dont have to pass in a loader_settings struct
927
0
    for (uint32_t i = 0; i < settings_layers->count; i++) {
928
0
        if (settings_layers->list[i].settings_control_value == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
929
0
            has_unordered_layer_location = true;
930
0
            unordered_layer_location_index = i;
931
0
            break;
932
0
        }
933
0
    }
934
935
0
    if (settings_layers->count == 0 && regular_layers->count == 0) {
936
        // No layers to combine
937
0
        goto out;
938
0
    } else if (settings_layers->count == 0) {
939
        // No settings layers - just copy regular to output_layers - memset regular layers to prevent double frees
940
0
        *output_layers = *regular_layers;
941
0
        memset(regular_layers, 0, sizeof(struct loader_layer_list));
942
0
        goto out;
943
0
    } else if (regular_layers->count == 0 || !has_unordered_layer_location) {
944
        // No regular layers or has_unordered_layer_location is false - just copy settings to output_layers -
945
        // memset settings layers to prevent double frees
946
0
        *output_layers = *settings_layers;
947
0
        memset(settings_layers, 0, sizeof(struct loader_layer_list));
948
0
        goto out;
949
0
    }
950
951
0
    res = loader_init_generic_list(inst, (struct loader_generic_list*)output_layers,
952
0
                                   (settings_layers->count + regular_layers->count) * sizeof(struct loader_layer_properties));
953
0
    if (VK_SUCCESS != res) {
954
0
        goto out;
955
0
    }
956
957
    // Insert the settings layers into output_layers up to unordered_layer_index
958
0
    for (uint32_t i = 0; i < unordered_layer_location_index; i++) {
959
0
        if (!check_if_layer_is_in_list(output_layers, &settings_layers->list[i])) {
960
0
            res = loader_append_layer_property(inst, output_layers, &settings_layers->list[i]);
961
0
            if (VK_SUCCESS != res) {
962
0
                goto out;
963
0
            }
964
0
        }
965
0
    }
966
967
0
    for (uint32_t i = 0; i < regular_layers->count; i++) {
968
        // Check if its already been put in the output_layers list as well as the remaining settings_layers
969
0
        bool regular_layer_is_ordered = check_if_layer_is_in_list(output_layers, &regular_layers->list[i]) ||
970
0
                                        check_if_layer_is_in_list(settings_layers, &regular_layers->list[i]);
971
        // If it isn't found, add it
972
0
        if (!regular_layer_is_ordered) {
973
0
            res = loader_append_layer_property(inst, output_layers, &regular_layers->list[i]);
974
0
            if (VK_SUCCESS != res) {
975
0
                goto out;
976
0
            }
977
0
        } else {
978
            // layer is already ordered and can be safely freed
979
0
            loader_free_layer_properties(inst, &regular_layers->list[i]);
980
0
        }
981
0
    }
982
983
    // Insert the rest of the settings layers into combined_layers from  unordered_layer_index to the end
984
    // start at one after the unordered_layer_index
985
0
    for (uint32_t i = unordered_layer_location_index + 1; i < settings_layers->count; i++) {
986
0
        res = loader_append_layer_property(inst, output_layers, &settings_layers->list[i]);
987
0
        if (VK_SUCCESS != res) {
988
0
            goto out;
989
0
        }
990
0
    }
991
992
0
out:
993
0
    if (res != VK_SUCCESS) {
994
0
        loader_delete_layer_list_and_properties(inst, output_layers);
995
0
    }
996
997
0
    return res;
998
0
}
999
1000
VkResult enable_correct_layers_from_settings(const struct loader_instance* inst, const struct loader_envvar_all_filters* filters,
1001
                                             uint32_t app_enabled_name_count, const char* const* app_enabled_names,
1002
                                             const struct loader_layer_list* instance_layers,
1003
                                             struct loader_pointer_layer_list* target_layer_list,
1004
0
                                             struct loader_pointer_layer_list* activated_layer_list) {
1005
0
    VkResult res = VK_SUCCESS;
1006
0
    char* vk_instance_layers_env = loader_getenv(ENABLED_LAYERS_ENV, inst);
1007
0
    size_t vk_instance_layers_env_len = 0;
1008
0
    char* vk_instance_layers_env_copy = NULL;
1009
0
    if (vk_instance_layers_env != NULL) {
1010
0
        vk_instance_layers_env_len = strlen(vk_instance_layers_env) + 1;
1011
0
        vk_instance_layers_env_copy = loader_stack_alloc(vk_instance_layers_env_len);
1012
0
        memset(vk_instance_layers_env_copy, 0, vk_instance_layers_env_len);
1013
1014
0
        loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0, "env var \'%s\' defined and adding layers: %s",
1015
0
                   ENABLED_LAYERS_ENV, vk_instance_layers_env);
1016
0
    }
1017
0
    for (uint32_t i = 0; i < instance_layers->count; i++) {
1018
0
        bool enable_layer = false;
1019
0
        struct loader_layer_properties* props = &instance_layers->list[i];
1020
1021
        // Skip the sentinel unordered layer location
1022
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_UNORDERED_LAYER_LOCATION) {
1023
0
            continue;
1024
0
        }
1025
1026
        // Do not enable the layer if the settings have it set as off
1027
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_OFF) {
1028
0
            continue;
1029
0
        }
1030
        // Force enable it based on settings
1031
0
        if (props->settings_control_value == LOADER_SETTINGS_LAYER_CONTROL_ON) {
1032
0
            enable_layer = true;
1033
0
            props->enabled_by_what = ENABLED_BY_WHAT_LOADER_SETTINGS_FILE;
1034
0
        } else {
1035
            // Check if disable filter needs to skip the layer
1036
0
            if ((filters->disable_filter.disable_all || filters->disable_filter.disable_all_implicit ||
1037
0
                 check_name_matches_filter_environment_var(props->info.layerName, &filters->disable_filter.additional_filters)) &&
1038
0
                !check_name_matches_filter_environment_var(props->info.layerName, &filters->allow_filter)) {
1039
                // Report a message that we've forced off a layer if it would have been enabled normally.
1040
0
                loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0,
1041
0
                           "Layer \"%s\" forced disabled because name matches filter of env var \'%s\'.", props->info.layerName,
1042
0
                           VK_LAYERS_DISABLE_ENV_VAR);
1043
1044
0
                continue;
1045
0
            }
1046
0
        }
1047
        // Check the enable filter
1048
0
        if (!enable_layer && check_name_matches_filter_environment_var(props->info.layerName, &filters->enable_filter)) {
1049
0
            enable_layer = true;
1050
0
            props->enabled_by_what = ENABLED_BY_WHAT_VK_LOADER_LAYERS_ENABLE;
1051
0
            loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0,
1052
0
                       "Layer \"%s\" forced enabled due to env var \'%s\'.", props->info.layerName, VK_LAYERS_ENABLE_ENV_VAR);
1053
0
        }
1054
1055
        // First look for the old-fashion layers forced on with VK_INSTANCE_LAYERS
1056
0
        if (!enable_layer && vk_instance_layers_env && vk_instance_layers_env_copy && vk_instance_layers_env_len > 0) {
1057
            // Copy the env-var on each iteration, so that loader_get_next_path can correctly find the separators
1058
            // This solution only needs one stack allocation ahead of time rather than an allocation per layer in the
1059
            // env-var
1060
0
            loader_strncpy(vk_instance_layers_env_copy, vk_instance_layers_env_len, vk_instance_layers_env,
1061
0
                           vk_instance_layers_env_len);
1062
1063
0
            char* instance_layers_env_iter = vk_instance_layers_env_copy;
1064
0
            while (instance_layers_env_iter && *instance_layers_env_iter) {
1065
0
                char* next = loader_get_next_path(instance_layers_env_iter);
1066
0
                if (0 == strcmp(instance_layers_env_iter, props->info.layerName)) {
1067
0
                    enable_layer = true;
1068
0
                    props->enabled_by_what = ENABLED_BY_WHAT_VK_INSTANCE_LAYERS;
1069
0
                    break;
1070
0
                }
1071
0
                instance_layers_env_iter = next;
1072
0
            }
1073
0
        }
1074
1075
        // Check if it should be enabled by the application
1076
0
        if (!enable_layer) {
1077
0
            for (uint32_t j = 0; j < app_enabled_name_count; j++) {
1078
0
                if (strcmp(props->info.layerName, app_enabled_names[j]) == 0) {
1079
0
                    enable_layer = true;
1080
0
                    props->enabled_by_what = ENABLED_BY_WHAT_IN_APPLICATION_API;
1081
0
                    break;
1082
0
                }
1083
0
            }
1084
0
        }
1085
1086
        // Check if its an implicit layers and thus enabled by default
1087
0
        if (!enable_layer && (0 == (props->type_flags & VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER)) &&
1088
0
            loader_implicit_layer_is_enabled(inst, filters, props)) {
1089
0
            enable_layer = true;
1090
0
            props->enabled_by_what = ENABLED_BY_WHAT_IMPLICIT_LAYER;
1091
0
        }
1092
1093
0
        if (enable_layer) {
1094
            // Check if the layer is a meta layer reuse the existing function to add the meta layer
1095
0
            if (props->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER) {
1096
0
                res = loader_add_meta_layer(inst, filters, props, target_layer_list, activated_layer_list, instance_layers, NULL);
1097
0
                if (res == VK_ERROR_OUT_OF_HOST_MEMORY) goto out;
1098
0
            } else {
1099
0
                res = loader_add_layer_properties_to_list(inst, target_layer_list, props);
1100
0
                if (res != VK_SUCCESS) {
1101
0
                    goto out;
1102
0
                }
1103
0
                res = loader_add_layer_properties_to_list(inst, activated_layer_list, props);
1104
0
                if (res != VK_SUCCESS) {
1105
0
                    goto out;
1106
0
                }
1107
0
            }
1108
0
        }
1109
0
    }
1110
0
out:
1111
0
    return res;
1112
0
}
1113
1114
0
VkResult loader_settings_get_additional_driver_files(const struct loader_instance* inst, struct loader_string_list* out_files) {
1115
0
    VkResult res = VK_SUCCESS;
1116
1117
0
    const loader_settings* settings = get_current_settings_and_lock(inst);
1118
1119
0
    if (NULL == settings || !settings->settings_active) {
1120
0
        goto out;
1121
0
    }
1122
1123
0
    if (settings->additional_drivers_use_exclusively) {
1124
0
        free_string_list(inst, out_files);
1125
0
    }
1126
1127
0
    for (uint32_t i = 0; i < settings->additional_driver_count; i++) {
1128
0
        res = prepend_if_manifest_file(inst, settings->additional_drivers[i].path, out_files);
1129
0
    }
1130
1131
0
out:
1132
0
    release_current_settings_lock(inst);
1133
0
    return res;
1134
0
}
1135
1136
0
bool loader_settings_should_use_driver_environment_variables(const struct loader_instance* inst) {
1137
0
    bool should_use = true;
1138
0
    const loader_settings* settings = get_current_settings_and_lock(inst);
1139
0
    if (NULL == settings || !settings->settings_active) {
1140
0
        goto out;
1141
0
    }
1142
0
    if (settings->device_configuration_count > 0) {
1143
0
        should_use = false;
1144
0
    }
1145
0
out:
1146
0
    release_current_settings_lock(inst);
1147
0
    return should_use;
1148
0
}