Coverage Report

Created: 2026-08-13 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vulkan-loader/loader/loader_environment.c
Line
Count
Source
1
/*
2
 *
3
 * Copyright (c) 2014-2023 The Khronos Group Inc.
4
 * Copyright (c) 2014-2023 Valve Corporation
5
 * Copyright (c) 2014-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
 * Author: Jon Ashburn <jon@lunarg.com>
20
 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
21
 * Author: Chia-I Wu <olvaffe@gmail.com>
22
 * Author: Chia-I Wu <olv@lunarg.com>
23
 * Author: Mark Lobodzinski <mark@LunarG.com>
24
 * Author: Lenny Komow <lenny@lunarg.com>
25
 * Author: Charles Giessen <charles@lunarg.com>
26
 *
27
 */
28
29
#include "loader_environment.h"
30
31
#include "allocation.h"
32
#include "loader.h"
33
#include "log.h"
34
#include "stack_allocation.h"
35
36
#include <ctype.h>
37
38
// Environment variables
39
#if COMMON_UNIX_PLATFORMS
40
41
0
bool is_high_integrity(void) { return geteuid() != getuid() || getegid() != getgid(); }
42
43
6.54k
char *loader_getenv(const char *name, const struct loader_instance *inst) {
44
6.54k
    if (NULL == name) return NULL;
45
    // No allocation of memory necessary for Linux, but we should at least touch
46
    // the inst pointer to get rid of compiler warnings.
47
6.54k
    (void)inst;
48
6.54k
    return getenv(name);
49
6.54k
}
50
51
171k
char *loader_secure_getenv(const char *name, const struct loader_instance *inst) {
52
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
53
    // Apple does not appear to have a secure getenv implementation.
54
    // The main difference between secure getenv and getenv is that secure getenv
55
    // returns NULL if the process is being run with elevated privileges by a normal user.
56
    // The idea is to prevent the reading of malicious environment variables by a process
57
    // that can do damage.
58
    // This algorithm is derived from glibc code that sets an internal
59
    // variable (__libc_enable_secure) if the process is running under setuid or setgid.
60
    return is_high_integrity() ? NULL : loader_getenv(name, inst);
61
#elif defined(__Fuchsia__)
62
    return loader_getenv(name, inst);
63
#else
64
    // Linux
65
171k
    char *out;
66
171k
#if defined(HAVE_SECURE_GETENV) && !defined(LOADER_USE_UNSAFE_FILE_SEARCH)
67
171k
    (void)inst;
68
171k
    out = secure_getenv(name);
69
#elif defined(HAVE___SECURE_GETENV) && !defined(LOADER_USE_UNSAFE_FILE_SEARCH)
70
    (void)inst;
71
    out = __secure_getenv(name);
72
#else
73
    out = loader_getenv(name, inst);
74
#if !defined(LOADER_USE_UNSAFE_FILE_SEARCH)
75
    loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "Loader is using non-secure environment variable lookup for %s", name);
76
#endif
77
#endif
78
171k
    return out;
79
171k
#endif
80
171k
}
81
82
152k
void loader_free_getenv(char *val, const struct loader_instance *inst) {
83
    // No freeing of memory necessary for Linux, but we should at least touch
84
    // the val and inst pointers to get rid of compiler warnings.
85
152k
    (void)val;
86
152k
    (void)inst;
87
152k
}
88
89
#elif defined(WIN32)
90
91
bool is_high_integrity() {
92
    HANDLE process_token;
93
    if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_QUERY_SOURCE, &process_token)) {
94
        // Maximum possible size of SID_AND_ATTRIBUTES is maximum size of a SID + size of attributes DWORD.
95
        uint8_t mandatory_label_buffer[SECURITY_MAX_SID_SIZE + sizeof(DWORD)];
96
        DWORD buffer_size;
97
        if (GetTokenInformation(process_token, TokenIntegrityLevel, mandatory_label_buffer, sizeof(mandatory_label_buffer),
98
                                &buffer_size) != 0) {
99
            const TOKEN_MANDATORY_LABEL *mandatory_label = (const TOKEN_MANDATORY_LABEL *)mandatory_label_buffer;
100
            const DWORD sub_authority_count = *GetSidSubAuthorityCount(mandatory_label->Label.Sid);
101
            const DWORD integrity_level = *GetSidSubAuthority(mandatory_label->Label.Sid, sub_authority_count - 1);
102
103
            CloseHandle(process_token);
104
            return integrity_level >= SECURITY_MANDATORY_HIGH_RID;
105
        }
106
107
        CloseHandle(process_token);
108
    }
109
110
    return false;
111
}
112
113
char *loader_getenv(const char *name, const struct loader_instance *inst) {
114
    int name_utf16_size = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
115
    if (name_utf16_size <= 0) {
116
        return NULL;
117
    }
118
    wchar_t *name_utf16 = (wchar_t *)loader_stack_alloc(name_utf16_size * sizeof(wchar_t));
119
    if (MultiByteToWideChar(CP_UTF8, 0, name, -1, name_utf16, name_utf16_size) != name_utf16_size) {
120
        return NULL;
121
    }
122
123
    DWORD val_size = GetEnvironmentVariableW(name_utf16, NULL, 0);
124
    // val_size DOES include the null terminator, so for any set variable
125
    // will always be at least 1. If it's 0, the variable wasn't set.
126
    if (val_size == 0) {
127
        return NULL;
128
    }
129
130
    wchar_t *val = (wchar_t *)loader_stack_alloc(val_size * sizeof(wchar_t));
131
    if (GetEnvironmentVariableW(name_utf16, val, val_size) != val_size - 1) {
132
        return NULL;
133
    }
134
135
    int val_utf8_size = WideCharToMultiByte(CP_UTF8, 0, val, -1, NULL, 0, NULL, NULL);
136
    if (val_utf8_size <= 0) {
137
        return NULL;
138
    }
139
    char *val_utf8 = (char *)loader_instance_heap_alloc(inst, val_utf8_size * sizeof(char), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
140
    if (val_utf8 == NULL) {
141
        return NULL;
142
    }
143
    if (WideCharToMultiByte(CP_UTF8, 0, val, -1, val_utf8, val_utf8_size, NULL, NULL) != val_utf8_size) {
144
        loader_instance_heap_free(inst, val_utf8);
145
        return NULL;
146
    }
147
    return val_utf8;
148
}
149
150
char *loader_secure_getenv(const char *name, const struct loader_instance *inst) {
151
    if (NULL == name) return NULL;
152
#if !defined(LOADER_USE_UNSAFE_FILE_SEARCH)
153
    if (is_high_integrity()) {
154
        loader_log(inst, VULKAN_LOADER_INFO_BIT, 0,
155
                   "Loader is running with elevated permissions. Environment variable %s will be ignored", name);
156
        return NULL;
157
    }
158
#endif
159
160
    return loader_getenv(name, inst);
161
}
162
163
void loader_free_getenv(char *val, const struct loader_instance *inst) { loader_instance_heap_free(inst, (void *)val); }
164
165
#else
166
167
#warning \
168
    "This platform does not support environment variables! If this is not intended, please implement the stubs functions loader_getenv and loader_free_getenv"
169
170
char *loader_getenv(const char *name, const struct loader_instance *inst) {
171
    // stub func
172
    (void)inst;
173
    (void)name;
174
    return NULL;
175
}
176
void loader_free_getenv(char *val, const struct loader_instance *inst) {
177
    // stub func
178
    (void)val;
179
    (void)inst;
180
}
181
182
#endif
183
184
// Determine the type of filter string based on the contents of it.
185
// This will properly check against:
186
//  - substrings "*string*"
187
//  - prefixes "string*"
188
//  - suffixes "*string"
189
//  - full string names "string"
190
// It will also return the correct start and finish to remove any star '*' characters for the actual string compare
191
void determine_filter_type(const char *filter_string, enum loader_filter_string_type *filter_type, const char **new_start,
192
11.7k
                           size_t *new_length) {
193
11.7k
    size_t filter_length = strlen(filter_string);
194
11.7k
    bool star_begin = false;
195
11.7k
    bool star_end = false;
196
11.7k
    if ('~' == filter_string[0]) {
197
        // One of the special identifiers like: ~all~, ~implicit~, or ~explicit~
198
0
        *filter_type = FILTER_STRING_SPECIAL;
199
0
        *new_start = filter_string;
200
0
        *new_length = filter_length;
201
11.7k
    } else {
202
11.7k
        if ('*' == filter_string[0]) {
203
            // Only the * means everything
204
0
            if (filter_length == 1) {
205
0
                *filter_type = FILTER_STRING_SPECIAL;
206
0
                *new_start = filter_string;
207
0
                *new_length = filter_length;
208
0
                return;
209
0
            } else {
210
0
                star_begin = true;
211
0
            }
212
0
        }
213
11.7k
        if ('*' == filter_string[filter_length - 1]) {
214
            // Not really valid, but just catch this case so if someone accidentally types "**" it will also mean everything
215
0
            if (star_begin && filter_length == 2) {
216
0
                *filter_type = FILTER_STRING_SPECIAL;
217
0
                *new_start = filter_string;
218
0
                *new_length = filter_length;
219
0
                return;
220
0
            } else {
221
0
                star_end = true;
222
0
            }
223
0
        }
224
11.7k
        if (star_begin && star_end) {
225
0
            *filter_type = FILTER_STRING_SUBSTRING;
226
0
            *new_start = &filter_string[1];
227
0
            *new_length = filter_length - 2;
228
11.7k
        } else if (star_begin) {
229
0
            *new_start = &filter_string[1];
230
0
            *new_length = filter_length - 1;
231
0
            *filter_type = FILTER_STRING_SUFFIX;
232
11.7k
        } else if (star_end) {
233
0
            *filter_type = FILTER_STRING_PREFIX;
234
0
            *new_start = filter_string;
235
0
            *new_length = filter_length - 1;
236
11.7k
        } else {
237
11.7k
            *filter_type = FILTER_STRING_FULLNAME;
238
11.7k
            *new_start = filter_string;
239
11.7k
            *new_length = filter_length;
240
11.7k
        }
241
11.7k
    }
242
11.7k
}
243
244
// Parse the provided filter string provided by the envrionment variable into the appropriate filter
245
// struct variable.
246
VkResult parse_generic_filter_environment_var(const struct loader_instance *inst, const char *env_var_name,
247
23.5k
                                              struct loader_envvar_filter *filter_struct) {
248
23.5k
    VkResult result = VK_SUCCESS;
249
23.5k
    memset(filter_struct, 0, sizeof(struct loader_envvar_filter));
250
23.5k
    char *parsing_string = NULL;
251
23.5k
    char *env_var_value = loader_secure_getenv(env_var_name, inst);
252
23.5k
    if (NULL == env_var_value) {
253
11.7k
        return result;
254
11.7k
    }
255
11.7k
    const size_t env_var_len = strlen(env_var_value);
256
11.7k
    if (env_var_len == 0) {
257
0
        goto out;
258
0
    }
259
    // Allocate a separate string since scan_for_next_comma modifies the original string
260
11.7k
    parsing_string = loader_instance_heap_calloc(inst, env_var_len + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
261
11.7k
    if (NULL == parsing_string) {
262
0
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0,
263
0
                   "parse_generic_filter_environment_var: Failed to allocate space for parsing env var \'%s\'", env_var_name);
264
0
        result = VK_ERROR_OUT_OF_HOST_MEMORY;
265
0
        goto out;
266
0
    }
267
268
47.0k
    for (uint32_t iii = 0; iii < env_var_len; ++iii) {
269
35.2k
        parsing_string[iii] = (char)tolower((unsigned char)env_var_value[iii]);
270
35.2k
    }
271
11.7k
    parsing_string[env_var_len] = '\0';
272
273
11.7k
    char *context = NULL;
274
11.7k
    char *token = thread_safe_strtok(parsing_string, ",", &context);
275
23.5k
    while (NULL != token) {
276
11.7k
        enum loader_filter_string_type cur_filter_type;
277
11.7k
        const char *actual_start;
278
11.7k
        size_t actual_len;
279
11.7k
        determine_filter_type(token, &cur_filter_type, &actual_start, &actual_len);
280
        // value holds at most VK_MAX_EXTENSION_NAME_SIZE - 1 chars plus the null terminator. Keep the stored length in sync
281
        // with what actually fits so the matcher never walks past the buffer, and make sure it stays null terminated.
282
11.7k
        size_t stored_len = actual_len < VK_MAX_EXTENSION_NAME_SIZE - 1 ? actual_len : VK_MAX_EXTENSION_NAME_SIZE - 1;
283
11.7k
        loader_strncpy(filter_struct->filters[filter_struct->count].value, VK_MAX_EXTENSION_NAME_SIZE, actual_start, stored_len);
284
11.7k
        filter_struct->filters[filter_struct->count].value[stored_len] = '\0';
285
11.7k
        filter_struct->filters[filter_struct->count].length = stored_len;
286
11.7k
        filter_struct->filters[filter_struct->count++].type = cur_filter_type;
287
11.7k
        if (filter_struct->count >= MAX_ADDITIONAL_FILTERS) {
288
0
            break;
289
0
        }
290
11.7k
        token = thread_safe_strtok(NULL, ",", &context);
291
11.7k
    }
292
293
11.7k
out:
294
295
11.7k
    loader_instance_heap_free(inst, parsing_string);
296
11.7k
    loader_free_getenv(env_var_value, inst);
297
11.7k
    return result;
298
11.7k
}
299
300
// Parse the disable layer string.  The layer disable has some special behavior because we allow it to disable
301
// all layers (either with "~all~", "*", or "**"), all implicit layers (with "~implicit~"), and all explicit layers
302
// (with "~explicit~"), in addition to the other layer filtering behavior.
303
VkResult parse_layers_disable_filter_environment_var(const struct loader_instance *inst,
304
11.7k
                                                     struct loader_envvar_disable_layers_filter *disable_struct) {
305
11.7k
    VkResult result = VK_SUCCESS;
306
11.7k
    memset(disable_struct, 0, sizeof(struct loader_envvar_disable_layers_filter));
307
11.7k
    char *parsing_string = NULL;
308
11.7k
    char *env_var_value = loader_secure_getenv(VK_LAYERS_DISABLE_ENV_VAR, inst);
309
11.7k
    if (NULL == env_var_value) {
310
11.7k
        goto out;
311
11.7k
    }
312
0
    const size_t env_var_len = strlen(env_var_value);
313
0
    if (env_var_len == 0) {
314
0
        goto out;
315
0
    }
316
    // Allocate a separate string since scan_for_next_comma modifies the original string
317
0
    parsing_string = loader_instance_heap_calloc(inst, env_var_len + 1, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
318
0
    if (NULL == parsing_string) {
319
0
        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0,
320
0
                   "parse_layers_disable_filter_environment_var: Failed to allocate space for parsing env var "
321
0
                   "\'VK_LAYERS_DISABLE_ENV_VAR\'");
322
0
        result = VK_ERROR_OUT_OF_HOST_MEMORY;
323
0
        goto out;
324
0
    }
325
326
0
    for (uint32_t iii = 0; iii < env_var_len; ++iii) {
327
0
        parsing_string[iii] = (char)tolower((unsigned char)env_var_value[iii]);
328
0
    }
329
0
    parsing_string[env_var_len] = '\0';
330
331
0
    char *context = NULL;
332
0
    char *token = thread_safe_strtok(parsing_string, ",", &context);
333
0
    while (NULL != token) {
334
0
        uint32_t cur_count = disable_struct->additional_filters.count;
335
0
        enum loader_filter_string_type cur_filter_type;
336
0
        const char *actual_start;
337
0
        size_t actual_len;
338
0
        determine_filter_type(token, &cur_filter_type, &actual_start, &actual_len);
339
0
        if (cur_filter_type == FILTER_STRING_SPECIAL) {
340
0
            if (!strcmp(VK_LOADER_DISABLE_ALL_LAYERS_VAR_1, token) || !strcmp(VK_LOADER_DISABLE_ALL_LAYERS_VAR_2, token) ||
341
0
                !strcmp(VK_LOADER_DISABLE_ALL_LAYERS_VAR_3, token)) {
342
0
                disable_struct->disable_all = true;
343
0
            } else if (!strcmp(VK_LOADER_DISABLE_IMPLICIT_LAYERS_VAR, token)) {
344
0
                disable_struct->disable_all_implicit = true;
345
0
            } else if (!strcmp(VK_LOADER_DISABLE_EXPLICIT_LAYERS_VAR, token)) {
346
0
                disable_struct->disable_all_explicit = true;
347
0
            }
348
0
        } else {
349
            // Keep the stored length in sync with what actually fits (value is VK_MAX_EXTENSION_NAME_SIZE bytes including
350
            // the null terminator) so the matcher never reads past the buffer.
351
0
            size_t stored_len = actual_len < VK_MAX_EXTENSION_NAME_SIZE - 1 ? actual_len : VK_MAX_EXTENSION_NAME_SIZE - 1;
352
0
            loader_strncpy(disable_struct->additional_filters.filters[cur_count].value, VK_MAX_EXTENSION_NAME_SIZE, actual_start,
353
0
                           stored_len);
354
0
            disable_struct->additional_filters.filters[cur_count].value[stored_len] = '\0';
355
0
            disable_struct->additional_filters.filters[cur_count].length = stored_len;
356
0
            disable_struct->additional_filters.filters[cur_count].type = cur_filter_type;
357
0
            disable_struct->additional_filters.count++;
358
0
            if (disable_struct->additional_filters.count >= MAX_ADDITIONAL_FILTERS) {
359
0
                break;
360
0
            }
361
0
        }
362
0
        token = thread_safe_strtok(NULL, ",", &context);
363
0
    }
364
11.7k
out:
365
11.7k
    loader_instance_heap_free(inst, parsing_string);
366
11.7k
    loader_free_getenv(env_var_value, inst);
367
11.7k
    return result;
368
0
}
369
370
// Parses the filter environment variables to determine if we have any special behavior
371
11.7k
VkResult parse_layer_environment_var_filters(const struct loader_instance *inst, struct loader_envvar_all_filters *layer_filters) {
372
11.7k
    VkResult res = parse_generic_filter_environment_var(inst, VK_LAYERS_ENABLE_ENV_VAR, &layer_filters->enable_filter);
373
11.7k
    if (VK_SUCCESS != res) {
374
0
        return res;
375
0
    }
376
11.7k
    res = parse_layers_disable_filter_environment_var(inst, &layer_filters->disable_filter);
377
11.7k
    if (VK_SUCCESS != res) {
378
0
        return res;
379
0
    }
380
11.7k
    res = parse_generic_filter_environment_var(inst, VK_LAYERS_ALLOW_ENV_VAR, &layer_filters->allow_filter);
381
11.7k
    if (VK_SUCCESS != res) {
382
0
        return res;
383
0
    }
384
11.7k
    return res;
385
11.7k
}
386
387
// Case-insensitive compare of `count` bytes of a name against a filter value. Filter values are already lowercased when
388
// they get parsed (see parse_generic_filter_environment_var), so we only need to fold the name side as we go. The caller
389
// guarantees both sides have at least `count` valid bytes.
390
283
static bool name_segment_matches_filter_value(const char *name_segment, const char *lowercase_filter_value, size_t count) {
391
591
    for (size_t iii = 0; iii < count; ++iii) {
392
496
        if ((char)tolower((unsigned char)name_segment[iii]) != lowercase_filter_value[iii]) {
393
188
            return false;
394
188
        }
395
496
    }
396
95
    return true;
397
283
}
398
399
// Check to see if the provided layer name matches any of the filter strings.
400
// This will properly check against:
401
//  - substrings "*string*"
402
//  - prefixes "string*"
403
//  - suffixes "*string"
404
//  - full string names "string"
405
22.1k
bool check_name_matches_filter_environment_var(const char *name, const struct loader_envvar_filter *filter_struct) {
406
22.1k
    bool ret_value = false;
407
22.1k
    size_t name_len = strlen(name);
408
    // Compare each filter against `name` directly. name_segment_matches_filter_value does a case-insensitive compare
409
    // (filter values are already lowercased at parse time), so there's no need to make a lowercased copy of name first
410
    // and no limit on how long name can be.
411
33.1k
    for (uint32_t filt = 0; filt < filter_struct->count; ++filt) {
412
11.0k
        const struct loader_envvar_filter_value *filter = &filter_struct->filters[filt];
413
        // The filter (with its wildcards stripped) is longer than the name, so it can't possibly match.
414
11.0k
        if (filter->length > name_len) {
415
2.45k
            continue;
416
2.45k
        }
417
8.62k
        switch (filter->type) {
418
0
            case FILTER_STRING_SPECIAL:
419
0
                if (!strcmp(VK_LOADER_DISABLE_ALL_LAYERS_VAR_1, filter->value) ||
420
0
                    !strcmp(VK_LOADER_DISABLE_ALL_LAYERS_VAR_2, filter->value) ||
421
0
                    !strcmp(VK_LOADER_DISABLE_ALL_LAYERS_VAR_3, filter->value)) {
422
0
                    ret_value = true;
423
0
                }
424
0
                break;
425
426
0
            case FILTER_STRING_SUBSTRING:
427
                // Slide the filter along the name looking for a match, stopping as soon as one is found.
428
0
                for (size_t start = 0; start + filter->length <= name_len; ++start) {
429
0
                    if (name_segment_matches_filter_value(name + start, filter->value, filter->length)) {
430
0
                        ret_value = true;
431
0
                        break;
432
0
                    }
433
0
                }
434
0
                break;
435
436
0
            case FILTER_STRING_SUFFIX:
437
0
                ret_value = name_segment_matches_filter_value(name + name_len - filter->length, filter->value, filter->length);
438
0
                break;
439
440
0
            case FILTER_STRING_PREFIX:
441
0
                ret_value = name_segment_matches_filter_value(name, filter->value, filter->length);
442
0
                break;
443
444
8.62k
            case FILTER_STRING_FULLNAME:
445
8.62k
                ret_value = (name_len == filter->length) && name_segment_matches_filter_value(name, filter->value, filter->length);
446
8.62k
                break;
447
8.62k
        }
448
8.62k
        if (ret_value) {
449
95
            break;
450
95
        }
451
8.62k
    }
452
22.1k
    return ret_value;
453
22.1k
}
454
455
// Get the layer name(s) from the env_name environment variable. If layer is found in
456
// search_list then add it to layer_list.  But only add it to layer_list if type_flags matches.
457
VkResult loader_add_environment_layers(struct loader_instance *inst, const char *enabled_layers_env,
458
                                       const struct loader_envvar_all_filters *filters,
459
                                       struct loader_pointer_layer_list *target_list,
460
                                       struct loader_pointer_layer_list *expanded_target_list,
461
0
                                       const struct loader_layer_list *source_list) {
462
0
    VkResult res = VK_SUCCESS;
463
0
    const enum layer_type_flags type_flags = VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER;
464
465
    // If the layer environment variable is present (i.e. VK_INSTANCE_LAYERS), we will always add it to the layer list.
466
0
    if (enabled_layers_env != NULL) {
467
0
        size_t layer_env_len = strlen(enabled_layers_env) + 1;
468
0
        char *name = loader_stack_alloc(layer_env_len);
469
0
        if (name != NULL) {
470
0
            loader_strncpy(name, layer_env_len, enabled_layers_env, layer_env_len);
471
472
0
            loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0, "env var \'%s\' defined and adding layers \"%s\"",
473
0
                       ENABLED_LAYERS_ENV, name);
474
475
            // First look for the old-fashion layers forced on with VK_INSTANCE_LAYERS
476
0
            while (name && *name) {
477
0
                char *next = loader_get_next_path(name);
478
479
0
                if (strlen(name) > 0) {
480
0
                    bool found = false;
481
0
                    for (uint32_t i = 0; i < source_list->count; i++) {
482
0
                        struct loader_layer_properties *source_prop = &source_list->list[i];
483
484
0
                        if (0 == strcmp(name, source_prop->info.layerName)) {
485
0
                            found = true;
486
                            // Only add it if it doesn't already appear in the layer list
487
0
                            if (!loader_find_layer_name_in_list(source_prop->info.layerName, target_list)) {
488
0
                                if (0 == (source_prop->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER)) {
489
0
                                    source_prop->enabled_by_what = ENABLED_BY_WHAT_VK_INSTANCE_LAYERS;
490
0
                                    res = loader_add_layer_properties_to_list(inst, target_list, source_prop);
491
0
                                    if (res == VK_ERROR_OUT_OF_HOST_MEMORY) goto out;
492
0
                                    res = loader_add_layer_properties_to_list(inst, expanded_target_list, source_prop);
493
0
                                    if (res == VK_ERROR_OUT_OF_HOST_MEMORY) goto out;
494
0
                                } else {
495
0
                                    res = loader_add_meta_layer(inst, filters, source_prop, target_list, expanded_target_list,
496
0
                                                                source_list, NULL);
497
0
                                    if (res == VK_ERROR_OUT_OF_HOST_MEMORY) goto out;
498
0
                                }
499
0
                                break;
500
0
                            }
501
0
                        }
502
0
                    }
503
0
                    if (!found) {
504
0
                        loader_log(inst, VULKAN_LOADER_ERROR_BIT | VULKAN_LOADER_LAYER_BIT, 0,
505
0
                                   "Layer \"%s\" was not found but was requested by env var VK_INSTANCE_LAYERS!", name);
506
0
                    }
507
0
                }
508
0
                name = next;
509
0
            }
510
0
        }
511
0
    }
512
513
    // Loop through all the layers and check the enable/disable filters
514
0
    for (uint32_t i = 0; i < source_list->count; i++) {
515
0
        struct loader_layer_properties *source_prop = &source_list->list[i];
516
517
        // If it doesn't match the type, or the name isn't what we're looking for, just continue
518
0
        if ((source_prop->type_flags & type_flags) != type_flags) {
519
0
            continue;
520
0
        }
521
522
        // We found a layer we're interested in, but has it been disabled...
523
0
        bool adding = true;
524
0
        bool is_implicit = (0 == (source_prop->type_flags & VK_LAYER_TYPE_FLAG_EXPLICIT_LAYER));
525
0
        bool disabled_by_type =
526
0
            (is_implicit) ? (filters->disable_filter.disable_all_implicit) : (filters->disable_filter.disable_all_explicit);
527
0
        if ((filters->disable_filter.disable_all || disabled_by_type ||
528
0
             check_name_matches_filter_environment_var(source_prop->info.layerName, &filters->disable_filter.additional_filters)) &&
529
0
            !check_name_matches_filter_environment_var(source_prop->info.layerName, &filters->allow_filter)) {
530
0
            loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0,
531
0
                       "Layer \"%s\" ignored because it has been disabled by env var \'%s\'", source_prop->info.layerName,
532
0
                       VK_LAYERS_DISABLE_ENV_VAR);
533
0
            adding = false;
534
0
        }
535
536
        // If we are supposed to filter through all layers, we need to compare the layer name against the filter.
537
        // This can override the disable above, so we want to do it second.
538
        // Also make sure the layer isn't already in the output_list, skip adding it if it is.
539
0
        if (check_name_matches_filter_environment_var(source_prop->info.layerName, &filters->enable_filter) &&
540
0
            !loader_find_layer_name_in_list(source_prop->info.layerName, target_list)) {
541
0
            adding = true;
542
            // Only way is_substring is true is if there are enable variables.  If that's the case, and we're past the
543
            // above, we should indicate that it was forced on in this way.
544
0
            loader_log(inst, VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_LAYER_BIT, 0,
545
0
                       "Layer \"%s\" forced enabled due to env var \'%s\'", source_prop->info.layerName, VK_LAYERS_ENABLE_ENV_VAR);
546
0
        } else {
547
0
            adding = false;
548
0
        }
549
550
0
        if (!adding) {
551
0
            continue;
552
0
        }
553
554
        // If not a meta-layer, simply add it.
555
0
        if (0 == (source_prop->type_flags & VK_LAYER_TYPE_FLAG_META_LAYER)) {
556
0
            source_prop->enabled_by_what = ENABLED_BY_WHAT_VK_LOADER_LAYERS_ENABLE;
557
0
            res = loader_add_layer_properties_to_list(inst, target_list, source_prop);
558
0
            if (res == VK_ERROR_OUT_OF_HOST_MEMORY) goto out;
559
0
            res = loader_add_layer_properties_to_list(inst, expanded_target_list, source_prop);
560
0
            if (res == VK_ERROR_OUT_OF_HOST_MEMORY) goto out;
561
0
        } else {
562
0
            res = loader_add_meta_layer(inst, filters, source_prop, target_list, expanded_target_list, source_list, NULL);
563
0
            if (res == VK_ERROR_OUT_OF_HOST_MEMORY) goto out;
564
0
        }
565
0
    }
566
567
0
out:
568
569
0
    return res;
570
0
}
571
572
void parse_id_filter_environment_var(const struct loader_instance *inst, const char *env_var_name,
573
0
                                     struct loader_envvar_id_filter *filter_struct) {
574
0
    memset(filter_struct, 0, sizeof(struct loader_envvar_id_filter));
575
0
    char *parsing_string = NULL;
576
0
    char *env_var_value = loader_secure_getenv(env_var_name, inst);
577
0
    if (NULL == env_var_value) {
578
0
        return;
579
0
    }
580
0
    const size_t env_var_len = strlen(env_var_value);
581
0
    if (env_var_len == 0) {
582
0
        goto out;
583
0
    }
584
    // Allocate a separate string since scan_for_next_comma modifies the original string
585
0
    parsing_string = loader_stack_alloc(env_var_len + 1);
586
0
    for (uint32_t iii = 0; iii < env_var_len; ++iii) {
587
0
        parsing_string[iii] = (char)tolower((unsigned char)env_var_value[iii]);
588
0
    }
589
0
    parsing_string[env_var_len] = '\0';
590
591
0
    filter_struct->count = 0;
592
0
    char *context = NULL;
593
0
    char *token = thread_safe_strtok(parsing_string, ",", &context);
594
0
    while (NULL != token) {
595
0
        if (filter_struct->count >= MAX_ADDITIONAL_FILTERS) {
596
0
            loader_log(inst, VULKAN_LOADER_WARN_BIT, 0,
597
0
                       "parse_id_filter_environment_var: Exceeded maximum number of filters (%d) for env var '%s'. "
598
0
                       "Remaining entries will be ignored.",
599
0
                       MAX_ADDITIONAL_FILTERS, env_var_name);
600
0
            break;
601
0
        }
602
603
0
        struct loader_envvar_id_filter_value *filter_value = &filter_struct->filters[filter_struct->count];
604
605
0
        char *pEnd;
606
0
        filter_value->begin = (uint32_t)strtoul(token, &pEnd, 0);
607
608
0
        if (*pEnd != '\0') {
609
0
            pEnd++;
610
0
            filter_value->end = (uint32_t)strtoul(pEnd, NULL, 0);
611
0
        } else {
612
0
            filter_value->end = filter_value->begin;
613
0
        }
614
615
0
        filter_struct->count++;
616
0
        token = thread_safe_strtok(NULL, ",", &context);
617
0
    }
618
619
0
out:
620
621
0
    loader_free_getenv(env_var_value, inst);
622
0
}
623
624
0
bool check_id_matches_filter_environment_var(const uint32_t id, const struct loader_envvar_id_filter *filter_struct) {
625
0
    for (uint32_t i = 0; i < filter_struct->count; i++) {
626
0
        if ((filter_struct->filters[i].begin <= id) && (id <= filter_struct->filters[i].end)) {
627
0
            return true;
628
0
        }
629
0
    }
630
0
    return false;
631
0
}