chromium/third_party/vulkan-validation-layers/src/layers/vk_layer_config.cpp

/**************************************************************************
 *
 * Copyright 2014-2024 Valve Software
 * Copyright 2015-2024 Google Inc.
 * Copyright 2019-2024 LunarG, Inc.
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **************************************************************************/
#include "vk_layer_config.h"

#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <charconv>
#include <sys/stat.h>

#include <vulkan/vk_layer.h>
#include "utils/vk_layer_utils.h"

#if defined(_WIN32)
#include <windows.h>
#include <direct.h>
#define GetCurrentDir
#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
#include <sys/system_properties.h>
#include <unistd.h>
#include "utils/android_ndk_types.h"
#define GetCurrentDir
#else
#include <unistd.h>
#define GetCurrentDir
#endif

string;

class ConfigFile {};

ConfigFile &GetLayerConfig() {}

#if defined(__ANDROID__)
static void PropCallback(void *cookie, [[maybe_unused]] const char *name, const char *value, [[maybe_unused]] uint32_t serial) {
    std::string *property = static_cast<std::string *>(cookie);
    *property = value;
}
#endif

std::string GetEnvironment(const char *variable) {}

void SetEnvironment(const char *variable, const char *value) {}

const char *getLayerOption(const char *option) {}

const SettingsFileInfo *GetLayerSettingsFileInfo() {}

// If option is NULL or stdout, return stdout, otherwise try to open option
// as a filename. If successful, return file handle, otherwise stdout
FILE *getLayerLogOutput(const char *option, const char *layer_name) {}

// Map option strings to flag enum values
VkFlags GetLayerOptionFlags(const string &option, vvl::unordered_map<string, VkFlags> const &enum_data, uint32_t option_default) {}

// Constructor for ConfigFile. Initialize layers to log error messages to stdout by default. If a vk_layer_settings file is present,
// its settings will override the defaults.
ConfigFile::ConfigFile() :{}

const char *ConfigFile::GetOption(const string &option) {}

void ConfigFile::SetOption(const string &option, const string &val) {}

#if defined(WIN32)
// Check for admin rights
static inline bool IsHighIntegrity() {
    HANDLE process_token;
    if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_QUERY_SOURCE, &process_token)) {
        // Maximum possible size of SID_AND_ATTRIBUTES is maximum size of a SID + size of attributes DWORD.
        uint8_t mandatory_label_buffer[SECURITY_MAX_SID_SIZE + sizeof(DWORD)];
        DWORD buffer_size;
        if (GetTokenInformation(process_token, TokenIntegrityLevel, mandatory_label_buffer, sizeof(mandatory_label_buffer),
                                &buffer_size) != 0) {
            const TOKEN_MANDATORY_LABEL *mandatory_label = (const TOKEN_MANDATORY_LABEL *)mandatory_label_buffer;
            const DWORD sub_authority_count = *GetSidSubAuthorityCount(mandatory_label->Label.Sid);
            const DWORD integrity_level = *GetSidSubAuthority(mandatory_label->Label.Sid, sub_authority_count - 1);

            CloseHandle(process_token);
            return integrity_level > SECURITY_MANDATORY_MEDIUM_RID;
        }

        CloseHandle(process_token);
    }

    return false;
}
#endif

string ConfigFile::FindSettings() {}

static inline std::string string_trim(const std::string &s) {}

void ConfigFile::ParseFile(const char *filename) {}

void PrintMessageFlags(VkFlags vk_flags, char *msg_flags) {}

void PrintMessageSeverity(VkFlags vk_flags, char *msg_flags) {}

void PrintMessageType(VkFlags vk_flags, char *msg_flags) {}

// Ensure we are properly setting VK_USE_PLATFORM_METAL_EXT, VK_USE_PLATFORM_IOS_MVK, and VK_USE_PLATFORM_MACOS_MVK.
#if __APPLE__

#ifndef VK_USE_PLATFORM_METAL_EXT
#error "VK_USE_PLATFORM_METAL_EXT not defined!"
#endif

#include <TargetConditionals.h>

#if TARGET_OS_IOS

#ifndef VK_USE_PLATFORM_IOS_MVK
#error "VK_USE_PLATFORM_IOS_MVK not defined!"
#endif

#endif  //  TARGET_OS_IOS

#if TARGET_OS_OSX

#ifndef VK_USE_PLATFORM_MACOS_MVK
#error "VK_USE_PLATFORM_MACOS_MVK not defined!"
#endif

#endif  // TARGET_OS_OSX

#endif  // __APPLE__

#ifdef VK_USE_PLATFORM_ANDROID_KHR

// Require at least NDK 25 to build Validation Layers. Makes everything simpler to just have people building the layers to use a
// recent version of the NDK.
//
// This avoids issues with older NDKs which complicate correct CMake builds:
// Example:
//
// The NDK toolchain file in r23 contains a bug which means CMAKE_ANDROID_EXCEPTIONS might not be set correctly in some
// circumstances, if not set directly by the developer.
#if __NDK_MAJOR__ < 25
#error "Validation Layers require at least NDK r25 or greater to build"
#endif

// This catches before dlopen fails if the default Android-26 layers are being used and attempted to be ran on Android 25 or below
void __attribute__((constructor)) CheckAndroidVersion() {
    const std::string version = GetEnvironment("ro.build.version.sdk");

    if (version.empty()) {
        return;
    }

    constexpr uint32_t target_android_api = 26;
    constexpr uint32_t android_api = __ANDROID_API__;

    static_assert(android_api >= target_android_api, "Vulkan-ValidationLayers is not supported on Android 25 and below");

    uint32_t queried_version{};

    if (std::from_chars(version.data(), version.data() + version.size(), queried_version).ec != std::errc()) {
        return;
    }

    if (queried_version < target_android_api) {
        LOGCONSOLE("ERROR - Android version is %d and needs to be 26 or above.", queried_version);
    }
}

#endif