blob: 96e9dfd72dd059a841b12fc358d3470f556c6c27 [file]
/*
* Copyright (C) 2026 The Android Open Source Project
*
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "DiscoveredLayerLibraries.h"
#include "DiscoveredLayerLibrary.h"
#include "EnabledLayers.h"
#include "IdentifiedLayer.h"
#include "IdentifiedLayers.h"
#include "LayerEnvironment.h"
using namespace testing;
namespace vulkan {
namespace layers {
// Helpers to create Vulkan properties
static VkLayerProperties CreateLayerProps(const std::string& name,
uint32_t spec_ver,
uint32_t impl_ver,
const std::string& desc) {
VkLayerProperties props;
memset(&props, 0, sizeof(props));
strncpy(props.layerName, name.c_str(), VK_MAX_EXTENSION_NAME_SIZE - 1);
props.specVersion = spec_ver;
props.implementationVersion = impl_ver;
strncpy(props.description, desc.c_str(), VK_MAX_DESCRIPTION_SIZE - 1);
return props;
}
static VkExtensionProperties CreateExtProps(const std::string& name,
uint32_t spec_ver) {
VkExtensionProperties props;
memset(&props, 0, sizeof(props));
strncpy(props.extensionName, name.c_str(), VK_MAX_EXTENSION_NAME_SIZE - 1);
props.specVersion = spec_ver;
return props;
}
// Mock DynamicLibrary
class MockDynamicLibrary : public DynamicLibrary {
public:
MockDynamicLibrary() {
ON_CALL(*this, GetSymbol(_))
.WillByDefault(Invoke(this, &MockDynamicLibrary::DefaultGetSymbol));
}
MOCK_METHOD(void*, GetSymbol, (const std::string& name), (const, override));
void* DefaultGetSymbol(const std::string& name) const {
if (name == "vkGetInstanceProcAddr") {
return nullptr;
}
auto gipa = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
GetSymbol("vkGetInstanceProcAddr"));
if (gipa) {
return reinterpret_cast<void*>(gipa(VK_NULL_HANDLE, name.c_str()));
}
return nullptr;
}
};
// Mock LayerEnvironment
class MockLayerEnvironment : public LayerEnvironment {
public:
MOCK_METHOD(bool, IsDebuggable, (), (const, override));
MOCK_METHOD(bool, IsOSDebuggable, (), (const, override));
MOCK_METHOD(bool, IsAppDebuggable, (), (const, override));
MOCK_METHOD(bool, IsAppInjectable, (), (const, override));
MOCK_METHOD(std::string, GetProcessName, (), (const, override));
MOCK_METHOD(bool, IsZygoteProcess, (), (const, override));
MOCK_METHOD(std::string, GetPackageName, (), (const, override));
MOCK_METHOD(android::NativeLoaderNamespace*,
GetAppNamespace,
(),
(const, override));
MOCK_METHOD(std::string,
GetProperty,
(const std::string& key),
(const, override));
MOCK_METHOD(std::string, GetDebugLayers, (), (const, override));
MOCK_METHOD(std::string, GetLayerPaths, (), (const, override));
MOCK_METHOD(std::string, GetExplicitLayerPaths, (), (const, override));
MOCK_METHOD(std::string, GetImplicitLayerPaths, (), (const, override));
MOCK_METHOD(bool, FileExists, (const std::string& path), (const, override));
MOCK_METHOD(std::vector<std::string>,
ListDirectory,
(const std::string& path),
(const, override));
MOCK_METHOD(std::vector<std::string>,
ReadFileLines,
(const std::string& path),
(const, override));
MOCK_METHOD(std::vector<std::string>,
ListZipDirectory,
(const std::string& zipPath, const std::string& dirInZip),
(const, override));
MOCK_METHOD(std::unique_ptr<DynamicLibrary>,
LoadLibrary,
(const std::string& path),
(const, override));
MOCK_METHOD(std::unique_ptr<DynamicLibrary>,
LoadLibraryInNamespace,
(const std::string& path, android::NativeLoaderNamespace* ns),
(const, override));
MOCK_METHOD(std::unique_ptr<DynamicLibrary>,
LoadLibraryInExportedNamespace,
(const std::string& path, const std::string& ns_name),
(const, override));
void SetUpDefaultLoadLibrary() {
ON_CALL(*this, LoadLibrary(_))
.WillByDefault(Invoke([this](const std::string& path) {
return GetPreConfiguredOrNewMock(path);
}));
ON_CALL(*this, LoadLibraryInNamespace(_, _))
.WillByDefault(Invoke([this](const std::string& path,
android::NativeLoaderNamespace*) {
return GetPreConfiguredOrNewMock(path);
}));
ON_CALL(*this, LoadLibraryInExportedNamespace(_, _))
.WillByDefault(
Invoke([this](const std::string& path, const std::string&) {
return GetPreConfiguredOrNewMock(path);
}));
}
MockDynamicLibrary* PreConfigureLibrary(const std::string& path) {
auto mock_lib = std::make_unique<NiceMock<MockDynamicLibrary>>();
auto* raw = mock_lib.get();
pre_configured_libs_[path] = std::move(mock_lib);
return raw;
}
private:
std::unique_ptr<DynamicLibrary> GetPreConfiguredOrNewMock(
const std::string& path) const {
auto it = pre_configured_libs_.find(path);
if (it != pre_configured_libs_.end()) {
auto lib = std::move(it->second);
pre_configured_libs_.erase(it);
return lib;
}
return std::make_unique<NiceMock<MockDynamicLibrary>>();
}
mutable std::map<std::string, std::unique_ptr<MockDynamicLibrary>>
pre_configured_libs_;
};
// Static registries for mock Vulkan functions
struct MockLayerData {
VkLayerProperties properties;
std::vector<VkExtensionProperties> instance_extensions;
std::vector<VkExtensionProperties> device_extensions;
};
template <int N>
struct MockLibraryRegistry {
static std::vector<MockLayerData> layers;
static VKAPI_ATTR VkResult VKAPI_CALL
EnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
VkLayerProperties* pProperties) {
if (!pProperties) {
*pPropertyCount = static_cast<uint32_t>(layers.size());
return VK_SUCCESS;
}
uint32_t count =
std::min(*pPropertyCount, static_cast<uint32_t>(layers.size()));
for (uint32_t i = 0; i < count; ++i) {
pProperties[i] = layers[i].properties;
}
*pPropertyCount = count;
return VK_SUCCESS;
}
static VKAPI_ATTR VkResult VKAPI_CALL
EnumerateInstanceExtensionProperties(const char* pLayerName,
uint32_t* pPropertyCount,
VkExtensionProperties* pProperties) {
for (const auto& layer : layers) {
if (strcmp(layer.properties.layerName, pLayerName) == 0) {
if (!pProperties) {
*pPropertyCount =
static_cast<uint32_t>(layer.instance_extensions.size());
return VK_SUCCESS;
}
uint32_t count = std::min(
*pPropertyCount,
static_cast<uint32_t>(layer.instance_extensions.size()));
for (uint32_t i = 0; i < count; ++i) {
pProperties[i] = layer.instance_extensions[i];
}
*pPropertyCount = count;
return VK_SUCCESS;
}
}
return VK_ERROR_LAYER_NOT_PRESENT;
}
static VKAPI_ATTR VkResult VKAPI_CALL
EnumerateDeviceExtensionProperties(VkPhysicalDevice,
const char* pLayerName,
uint32_t* pPropertyCount,
VkExtensionProperties* pProperties) {
for (const auto& layer : layers) {
if (strcmp(layer.properties.layerName, pLayerName) == 0) {
if (!pProperties) {
*pPropertyCount =
static_cast<uint32_t>(layer.device_extensions.size());
return VK_SUCCESS;
}
uint32_t count = std::min(
*pPropertyCount,
static_cast<uint32_t>(layer.device_extensions.size()));
for (uint32_t i = 0; i < count; ++i) {
pProperties[i] = layer.device_extensions[i];
}
*pPropertyCount = count;
return VK_SUCCESS;
}
}
return VK_ERROR_LAYER_NOT_PRESENT;
}
static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GPA(VkInstance,
const char* pName) {
if (strcmp(pName, "vkEnumerateInstanceLayerProperties") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
EnumerateInstanceLayerProperties);
}
if (strcmp(pName, "vkEnumerateInstanceExtensionProperties") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
EnumerateInstanceExtensionProperties);
}
if (strcmp(pName, "vkEnumerateDeviceExtensionProperties") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
EnumerateDeviceExtensionProperties);
}
return nullptr;
}
};
template <int N>
std::vector<MockLayerData> MockLibraryRegistry<N>::layers;
using Lib0 = MockLibraryRegistry<0>;
using Lib1 = MockLibraryRegistry<1>;
using Lib2 = MockLibraryRegistry<2>;
using Lib3 = MockLibraryRegistry<3>;
using Lib4 = MockLibraryRegistry<4>;
static PFN_vkGetInstanceProcAddr g_mock_gpas[] = {
Lib0::GPA, Lib1::GPA, Lib2::GPA, Lib3::GPA, Lib4::GPA};
class VulkanLayersTest : public Test {
protected:
void SetUp() override {
ClearMockRegistries();
env_.SetUpDefaultLoadLibrary();
ON_CALL(env_, GetProperty(_)).WillByDefault(Return(""));
ON_CALL(env_, GetDebugLayers()).WillByDefault(Return(""));
}
void TearDown() override {}
void ClearMockRegistries() {
Lib0::layers.clear();
Lib1::layers.clear();
Lib2::layers.clear();
Lib3::layers.clear();
Lib4::layers.clear();
}
NiceMock<MockLayerEnvironment> env_;
IdentifiedLayers identified_layers_;
};
// Test the case of no libraries being discovered
TEST_F(VulkanLayersTest, DiscoverNoLibraries) {
// Since the app and OS are not debuggable, not ILs will be discovered
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
DiscoveredLayerLibraries libraries(env_);
libraries.DiscoverLibraries();
EXPECT_TRUE(libraries.GetExplicitLibraries().empty());
EXPECT_TRUE(libraries.GetImplicitLibraries().empty());
EXPECT_TRUE(libraries.GetSystemLibraries().empty());
}
// Test the case of 2 properly-named, and 2 improperly-named libraries are
// discovered
TEST_F(VulkanLayersTest, DiscoverImplicitLibraries) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{
"libVkLayer_foo.so", "libVkLayer_bar.so", "invalid.so"}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{"invalid2.so"}));
// Disable others
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
DiscoveredLayerLibraries libraries(env_);
libraries.DiscoverLibraries();
const auto& implicit = libraries.GetImplicitLibraries();
ASSERT_EQ(implicit.size(), 2u);
EXPECT_EQ(implicit[0]->GetFilename(), "libVkLayer_foo.so");
EXPECT_EQ(implicit[0]->GetPath(),
"/data/local/debug/vulkan/libVkLayer_foo.so");
EXPECT_EQ(implicit[0]->GetType(), LayerType::Implicit);
EXPECT_EQ(implicit[1]->GetFilename(), "libVkLayer_bar.so");
EXPECT_EQ(implicit[1]->GetPath(),
"/data/local/debug/vulkan/libVkLayer_bar.so");
EXPECT_EQ(implicit[1]->GetType(), LayerType::Implicit);
EXPECT_TRUE(libraries.GetExplicitLibraries().empty());
EXPECT_TRUE(libraries.GetSystemLibraries().empty());
}
// Test discovery for a non-APK, native process scenario, where the OS is
// debuggable, and the process name is equal to "debug.vulkan.progname"
TEST_F(VulkanLayersTest, DiscoverImplicitLibrariesForProgname) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, IsOSDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("debug.vulkan.progname"))
.WillRepeatedly(Return("my_process"));
EXPECT_CALL(env_, GetProcessName()).WillRepeatedly(Return("my_process"));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_foo.so"}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{"invalid2.so"}));
// Disable others
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
DiscoveredLayerLibraries libraries(env_);
libraries.DiscoverLibraries();
const auto& implicit = libraries.GetImplicitLibraries();
ASSERT_EQ(implicit.size(), 1u);
EXPECT_EQ(implicit[0]->GetFilename(), "libVkLayer_foo.so");
}
// Test native process scenario where the process name does not match
// "debug.vulkan.progname"
TEST_F(VulkanLayersTest, DiscoverImplicitLibraries_ProcessSpecificMismatch) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, IsOSDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("debug.vulkan.progname"))
.WillRepeatedly(Return("other_process"));
EXPECT_CALL(env_, GetProcessName()).WillRepeatedly(Return("my_process"));
// Disable others
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
DiscoveredLayerLibraries libraries(env_);
libraries.DiscoverLibraries();
EXPECT_TRUE(libraries.GetImplicitLibraries().empty());
}
// Test that the debug settings are ignored during a native process scenario
TEST_F(VulkanLayersTest, DiscoverImplicitLibraries_SettingsGuarded) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, IsOSDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("debug.vulkan.progname"))
.WillRepeatedly(Return("my_process"));
EXPECT_CALL(env_, GetProcessName()).WillRepeatedly(Return("my_process"));
// We have settings paths, but they should be ignored
EXPECT_CALL(env_, GetImplicitLayerPaths())
.WillRepeatedly(Return("/settings/path"));
// VVL/old debug are still scanned
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_foo.so"}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{"invalid2.so"}));
// Disable others
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
DiscoveredLayerLibraries libraries(env_);
libraries.DiscoverLibraries();
const auto& implicit = libraries.GetImplicitLibraries();
// Should only contain the one from /data/local/debug/vulkan, not
// /settings/path
ASSERT_EQ(implicit.size(), 1u);
EXPECT_EQ(implicit[0]->GetFilename(), "libVkLayer_foo.so");
EXPECT_EQ(implicit[0]->GetPath(),
"/data/local/debug/vulkan/libVkLayer_foo.so");
}
// Test the case of "layer APKs", each having 1 library, being discovered
TEST_F(VulkanLayersTest, DiscoverImplicitLibrariesFromSettings) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
// Mock default implicit directories to be empty
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{}));
// Mock paths: first is APK (zip), second is regular directory
EXPECT_CALL(env_, GetImplicitLayerPaths())
.WillRepeatedly(Return("/path/to/apk.apk!/lib/arm64-v8a:/other/path"));
EXPECT_CALL(env_, ListZipDirectory("/path/to/apk.apk", "lib/arm64-v8a"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_in_apk.so"}));
EXPECT_CALL(env_, ListDirectory("/other/path"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_in_dir.so"}));
DiscoveredLayerLibraries libraries(env_);
libraries.DiscoverLibraries();
const auto& implicit_libs = libraries.GetImplicitLibraries();
ASSERT_EQ(implicit_libs.size(), 2u);
EXPECT_EQ(implicit_libs[0]->GetFilename(), "libVkLayer_in_apk.so");
EXPECT_EQ(implicit_libs[0]->GetPath(),
"/path/to/apk.apk!/lib/arm64-v8a/libVkLayer_in_apk.so");
EXPECT_EQ(implicit_libs[0]->GetType(), LayerType::Implicit);
EXPECT_EQ(implicit_libs[1]->GetFilename(), "libVkLayer_in_dir.so");
EXPECT_EQ(implicit_libs[1]->GetPath(), "/other/path/libVkLayer_in_dir.so");
EXPECT_EQ(implicit_libs[1]->GetType(), LayerType::Implicit);
EXPECT_TRUE(libraries.GetExplicitLibraries().empty());
EXPECT_TRUE(libraries.GetSystemLibraries().empty());
}
// Test the case of an APK having 2 EL libraries being discovered
TEST_F(VulkanLayersTest, DiscoverExplicitLibraries) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
// Mock paths: first is APK (zip), second is regular directory
EXPECT_CALL(env_, GetExplicitLayerPaths())
.WillRepeatedly(Return("/path/to/apk.apk!/lib/arm64-v8a:/other/path"));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, ListZipDirectory("/path/to/apk.apk", "lib/arm64-v8a"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_in_apk.so"}));
EXPECT_CALL(env_, ListDirectory("/other/path"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_in_dir.so"}));
DiscoveredLayerLibraries libraries(env_);
libraries.DiscoverLibraries();
// Both paths should be Explicit for A17 parity
const auto& explicit_libs = libraries.GetExplicitLibraries();
ASSERT_EQ(explicit_libs.size(), 2u);
EXPECT_EQ(explicit_libs[0]->GetFilename(), "libVkLayer_in_apk.so");
EXPECT_EQ(explicit_libs[0]->GetPath(),
"/path/to/apk.apk!/lib/arm64-v8a/libVkLayer_in_apk.so");
EXPECT_EQ(explicit_libs[0]->GetType(), LayerType::Explicit);
EXPECT_EQ(explicit_libs[1]->GetFilename(), "libVkLayer_in_dir.so");
EXPECT_EQ(explicit_libs[1]->GetPath(), "/other/path/libVkLayer_in_dir.so");
EXPECT_EQ(explicit_libs[1]->GetType(), LayerType::Explicit);
EXPECT_TRUE(libraries.GetImplicitLibraries().empty());
EXPECT_TRUE(libraries.GetSystemLibraries().empty());
}
// Test the case of discovering SLs, where one of the library names mentioned in
// a property doesn't actually exist in the filesystem
TEST_F(VulkanLayersTest, DiscoverSystemLibraries) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
// Mock properties
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.platform.0"))
.WillRepeatedly(Return("libVkLayer_plat1.so:VK_LAYER_plat1_l1:10"));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.platform.1"))
.WillRepeatedly(Return("libVkLayer_plat1.so:VK_LAYER_plat1_l2:12"));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.platform.2"))
.WillRepeatedly(Return("libVkLayer_plat2.so:VK_LAYER_plat2:20"));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.soc.0"))
.WillRepeatedly(Return("libVkLayer_soc.so:VK_LAYER_soc:15"));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.oem.0"))
.WillRepeatedly(Return("libVkLayer_oem.so:VK_LAYER_oem:30"));
#if defined(__LP64__)
const std::string plat_dir = "/system/lib64/vulkan";
const std::string soc_dir = "/system_ext/lib64/vulkan";
const std::string oem_dir = "/product/lib64/vulkan";
#else
const std::string plat_dir = "/system/lib/vulkan";
const std::string soc_dir = "/system_ext/lib/vulkan";
const std::string oem_dir = "/product/lib/vulkan";
#endif
// Mock file existence, with the 2nd PSL not existing
EXPECT_CALL(env_, FileExists(plat_dir + "/libVkLayer_plat1.so"))
.WillRepeatedly(Return(true));
EXPECT_CALL(env_, FileExists(plat_dir + "/libVkLayer_plat2.so"))
.WillOnce(Return(false));
EXPECT_CALL(env_, FileExists(soc_dir + "/libVkLayer_soc.so"))
.WillOnce(Return(true));
EXPECT_CALL(env_, FileExists(oem_dir + "/libVkLayer_oem.so"))
.WillOnce(Return(true));
DiscoveredLayerLibraries libraries(env_);
libraries.DiscoverLibraries();
const auto& system = libraries.GetSystemLibraries();
ASSERT_EQ(system.size(), 3u); // plat1, soc, oem
// Platform first
EXPECT_EQ(system[0]->GetFilename(), "libVkLayer_plat1.so");
EXPECT_EQ(system[0]->GetPath(), plat_dir + "/libVkLayer_plat1.so");
EXPECT_EQ(system[0]->GetType(), LayerType::System);
{
const auto& layers = system[0]->GetLayerNames();
ASSERT_EQ(layers.size(), 2u);
auto it = layers.begin();
EXPECT_EQ(it->first, 10);
EXPECT_EQ(it->second, "VK_LAYER_plat1_l1");
it++;
EXPECT_EQ(it->first, 12);
EXPECT_EQ(it->second, "VK_LAYER_plat1_l2");
}
// SoC second
EXPECT_EQ(system[1]->GetFilename(), "libVkLayer_soc.so");
EXPECT_EQ(system[1]->GetPath(), soc_dir + "/libVkLayer_soc.so");
EXPECT_EQ(system[1]->GetType(), LayerType::System);
{
const auto& layers = system[1]->GetLayerNames();
ASSERT_EQ(layers.size(), 1u);
auto it = layers.begin();
EXPECT_EQ(it->first, 15);
EXPECT_EQ(it->second, "VK_LAYER_soc");
}
// OEM third
EXPECT_EQ(system[2]->GetFilename(), "libVkLayer_oem.so");
EXPECT_EQ(system[2]->GetPath(), oem_dir + "/libVkLayer_oem.so");
EXPECT_EQ(system[2]->GetType(), LayerType::System);
{
const auto& layers = system[2]->GetLayerNames();
ASSERT_EQ(layers.size(), 1u);
auto it = layers.begin();
EXPECT_EQ(it->first, 30);
EXPECT_EQ(it->second, "VK_LAYER_oem");
}
EXPECT_TRUE(libraries.GetExplicitLibraries().empty());
EXPECT_TRUE(libraries.GetImplicitLibraries().empty());
}
// Test identifying 1 EL and 1 SL, each of which has 1 or more extensions
TEST_F(VulkanLayersTest, IdentifyLayers) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, GetExplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, ListDirectory("/path/to"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_1.so"}));
ON_CALL(env_, GetProperty("ro.vulkan.layers.platform.0"))
.WillByDefault(Return("libVkLayer_sys1.so:VK_LAYER_sys:10"));
#if defined(__LP64__)
const std::string sys_dir = "/system/lib64/vulkan";
#else
const std::string sys_dir = "/system/lib/vulkan";
#endif
EXPECT_CALL(env_, FileExists(sys_dir + "/libVkLayer_sys1.so"))
.WillOnce(Return(true));
auto* mock_lib1 = env_.PreConfigureLibrary("/path/to/libVkLayer_1.so");
ON_CALL(*mock_lib1, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back({CreateLayerProps("VK_LAYER_foo", 1, 1, "Foo Layer"),
{CreateExtProps("VK_EXT_foo_ext", 1)},
{CreateExtProps("VK_EXT_foo_dev_ext", 1)}});
auto* mock_sys1 = env_.PreConfigureLibrary(sys_dir + "/libVkLayer_sys1.so");
ON_CALL(*mock_sys1, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[1])));
Lib1::layers.push_back(
{CreateLayerProps("VK_LAYER_sys", 1, 1, "System Layer"),
{CreateExtProps("VK_EXT_sys_ext", 1)},
{}});
identified_layers_.DoOneTimeSetup(env_);
const auto* foo = identified_layers_.FindLayer("VK_LAYER_foo");
ASSERT_NE(foo, nullptr);
EXPECT_EQ(foo->GetProperties().specVersion, 1u);
EXPECT_TRUE(foo->IsVisibleToApp());
EXPECT_FALSE(foo->IsImplicitlyEnabled());
ASSERT_EQ(foo->GetInstanceExtensions().size(), 1u);
EXPECT_EQ(foo->GetInstanceExtensions()[0].extensionName,
std::string("VK_EXT_foo_ext"));
ASSERT_EQ(foo->GetDeviceExtensions().size(), 1u);
EXPECT_EQ(foo->GetDeviceExtensions()[0].extensionName,
std::string("VK_EXT_foo_dev_ext"));
const auto* sys = identified_layers_.FindLayer("VK_LAYER_sys");
ASSERT_NE(sys, nullptr);
EXPECT_FALSE(sys->IsVisibleToApp());
EXPECT_FALSE(sys->IsImplicitlyEnabled());
auto enum_list = identified_layers_.GetLayersForEnumeration();
ASSERT_EQ(enum_list.size(), 1u);
EXPECT_EQ(enum_list[0]->GetName(), "VK_LAYER_foo");
auto sys_list = identified_layers_.GetSystemLayers();
ASSERT_EQ(sys_list.size(), 1u);
EXPECT_EQ(sys_list[0]->GetName(), "VK_LAYER_sys");
}
// Test a case of handling the same layer name from 2 different libraries
TEST_F(VulkanLayersTest, FilterDuplicateLayers) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetExplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, ListDirectory("/path/to"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_dup.so"}));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_dup.so"}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{"invalid2.so"}));
ON_CALL(env_, GetProperty("ro.vulkan.layers.platform.0"))
.WillByDefault(Return("libVkLayer_dup.so:VK_LAYER_dup:10"));
#if defined(__LP64__)
const std::string sys_dir = "/system/lib64/vulkan";
#else
const std::string sys_dir = "/system/lib/vulkan";
#endif
EXPECT_CALL(env_, FileExists(sys_dir + "/libVkLayer_dup.so"))
.WillOnce(Return(true));
auto* mock_imp =
env_.PreConfigureLibrary("/data/local/debug/vulkan/libVkLayer_dup.so");
ON_CALL(*mock_imp, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_dup", 1, 1, "Implicit Dup"), {}, {}});
auto* mock_exp = env_.PreConfigureLibrary("/path/to/libVkLayer_dup.so");
ON_CALL(*mock_exp, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[1])));
Lib1::layers.push_back(
{CreateLayerProps("VK_LAYER_dup", 1, 2, "Explicit Dup"), {}, {}});
auto* mock_sys = env_.PreConfigureLibrary(sys_dir + "/libVkLayer_dup.so");
ON_CALL(*mock_sys, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[2])));
Lib2::layers.push_back(
{CreateLayerProps("VK_LAYER_dup", 1, 3, "System Dup"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
const auto* layer = identified_layers_.FindLayer("VK_LAYER_dup");
ASSERT_NE(layer, nullptr);
EXPECT_EQ(layer->GetProperties().description, std::string("Implicit Dup"));
EXPECT_EQ(layer->GetLibrary().GetPath(),
"/data/local/debug/vulkan/libVkLayer_dup.so");
}
// Test a case where one EL is implicitly enabled via a setting in a debuggable
// scenario.
TEST_F(VulkanLayersTest, FilterImplicitEnablementSettings) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetExplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
ON_CALL(env_, ListDirectory("/path/to"))
.WillByDefault(Return(std::vector<std::string>{"libVkLayer_foo.so",
"libVkLayer_bar.so"}));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetDebugLayers()).WillRepeatedly(Return("VK_LAYER_foo"));
auto* mock_foo = env_.PreConfigureLibrary("/path/to/libVkLayer_foo.so");
ON_CALL(*mock_foo, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_foo", 1, 1, "Foo"), {}, {}});
auto* mock_bar = env_.PreConfigureLibrary("/path/to/libVkLayer_bar.so");
ON_CALL(*mock_bar, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[1])));
Lib1::layers.push_back(
{CreateLayerProps("VK_LAYER_bar", 1, 1, "Bar"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
const auto* foo = identified_layers_.FindLayer("VK_LAYER_foo");
ASSERT_NE(foo, nullptr);
EXPECT_TRUE(foo->IsImplicitlyEnabled());
// TODO: In the future, if a layer is implicitly enabled, IsVisibleToApp()
// should return false
EXPECT_TRUE(foo->IsVisibleToApp());
const auto* bar = identified_layers_.FindLayer("VK_LAYER_bar");
ASSERT_NE(bar, nullptr);
EXPECT_FALSE(bar->IsImplicitlyEnabled());
EXPECT_TRUE(bar->IsVisibleToApp());
auto enum_list = identified_layers_.GetLayersForEnumeration();
ASSERT_EQ(enum_list.size(), 2u);
EXPECT_EQ(enum_list[0]->GetName(), "VK_LAYER_foo");
EXPECT_EQ(enum_list[1]->GetName(), "VK_LAYER_bar");
auto imp_list = identified_layers_.GetImplicitlyEnabledLayers();
ASSERT_EQ(imp_list.size(), 1u);
EXPECT_EQ(imp_list[0]->GetName(), "VK_LAYER_foo");
}
// Test that layer settings (GetDebugLayers) overrides properties
TEST_F(VulkanLayersTest, ImplicitEnablement_SettingsOverridesProperties) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, ListDirectory("/path/to"))
.WillOnce(Return(std::vector<std::string>{
"libVkLayer_foo.so", "libVkLayer_bar.so", "libVkLayer_baz.so"}));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{}));
// Configure all enablement sources
EXPECT_CALL(env_, GetDebugLayers()).WillRepeatedly(Return("VK_LAYER_foo"));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layers"))
.WillRepeatedly(Return("VK_LAYER_bar"));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layer.0"))
.WillRepeatedly(Return("VK_LAYER_baz"));
auto* mock_foo = env_.PreConfigureLibrary("/path/to/libVkLayer_foo.so");
ON_CALL(*mock_foo, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_foo", 1, 1, "Foo"), {}, {}});
auto* mock_bar = env_.PreConfigureLibrary("/path/to/libVkLayer_bar.so");
ON_CALL(*mock_bar, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[1])));
Lib1::layers.push_back(
{CreateLayerProps("VK_LAYER_bar", 1, 1, "Bar"), {}, {}});
auto* mock_baz = env_.PreConfigureLibrary("/path/to/libVkLayer_baz.so");
ON_CALL(*mock_baz, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[2])));
Lib2::layers.push_back(
{CreateLayerProps("VK_LAYER_baz", 1, 1, "Baz"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
// Only VK_LAYER_foo should be implicitly enabled
const auto* foo = identified_layers_.FindLayer("VK_LAYER_foo");
ASSERT_NE(foo, nullptr);
EXPECT_TRUE(foo->IsImplicitlyEnabled());
const auto* bar = identified_layers_.FindLayer("VK_LAYER_bar");
ASSERT_NE(bar, nullptr);
EXPECT_FALSE(bar->IsImplicitlyEnabled());
const auto* baz = identified_layers_.FindLayer("VK_LAYER_baz");
ASSERT_NE(baz, nullptr);
EXPECT_FALSE(baz->IsImplicitlyEnabled());
}
// Test that global property (debug.vulkan.layers) overrides priority properties
TEST_F(VulkanLayersTest,
ImplicitEnablement_GlobalPropertyOverridesPriorityProperties) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, ListDirectory("/path/to"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_bar.so",
"libVkLayer_baz.so"}));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{}));
// Settings is empty
EXPECT_CALL(env_, GetDebugLayers()).WillRepeatedly(Return(""));
// Configure global and priority properties
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layers"))
.WillRepeatedly(Return("VK_LAYER_bar"));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layer.0"))
.WillRepeatedly(Return("VK_LAYER_baz"));
auto* mock_bar = env_.PreConfigureLibrary("/path/to/libVkLayer_bar.so");
ON_CALL(*mock_bar, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_bar", 1, 1, "Bar"), {}, {}});
auto* mock_baz = env_.PreConfigureLibrary("/path/to/libVkLayer_baz.so");
ON_CALL(*mock_baz, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[1])));
Lib1::layers.push_back(
{CreateLayerProps("VK_LAYER_baz", 1, 1, "Baz"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
// Only VK_LAYER_bar should be implicitly enabled
const auto* bar = identified_layers_.FindLayer("VK_LAYER_bar");
ASSERT_NE(bar, nullptr);
EXPECT_TRUE(bar->IsImplicitlyEnabled());
const auto* baz = identified_layers_.FindLayer("VK_LAYER_baz");
ASSERT_NE(baz, nullptr);
EXPECT_FALSE(baz->IsImplicitlyEnabled());
}
// Test that priority properties are applied in order
TEST_F(VulkanLayersTest, ImplicitEnablement_PriorityProperties) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, ListDirectory("/path/to"))
.WillOnce(Return(
std::vector<std::string>{"libVkLayer_l0.so", "libVkLayer_l1.so"}));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{}));
// Settings and global property are empty
EXPECT_CALL(env_, GetDebugLayers()).WillRepeatedly(Return(""));
// Configure priority properties
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layer.0"))
.WillRepeatedly(Return("VK_LAYER_l0"));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layer.1"))
.WillRepeatedly(Return("VK_LAYER_l1"));
auto* mock_l0 = env_.PreConfigureLibrary("/path/to/libVkLayer_l0.so");
ON_CALL(*mock_l0, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_l0", 1, 1, "L0"), {}, {}});
auto* mock_l1 = env_.PreConfigureLibrary("/path/to/libVkLayer_l1.so");
ON_CALL(*mock_l1, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[1])));
Lib1::layers.push_back(
{CreateLayerProps("VK_LAYER_l1", 1, 1, "L1"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
// Both should be implicitly enabled
const auto* l0 = identified_layers_.FindLayer("VK_LAYER_l0");
ASSERT_NE(l0, nullptr);
EXPECT_TRUE(l0->IsImplicitlyEnabled());
const auto* l1 = identified_layers_.FindLayer("VK_LAYER_l1");
ASSERT_NE(l1, nullptr);
EXPECT_TRUE(l1->IsImplicitlyEnabled());
// Verify ordering in GetImplicitlyEnabledLayers()
auto imp_list = identified_layers_.GetImplicitlyEnabledLayers();
ASSERT_EQ(imp_list.size(), 2u);
EXPECT_EQ(imp_list[0]->GetName(), "VK_LAYER_l0");
EXPECT_EQ(imp_list[1]->GetName(), "VK_LAYER_l1");
}
// Test that process-specific properties apply and override settings, even if
// properties are empty
TEST_F(
VulkanLayersTest,
ImplicitEnablement_ProcessSpecificPropertiesOverrideSettings_EmptyProperties) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, IsOSDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetProcessName()).WillRepeatedly(Return("my_app"));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, ListDirectory("/path/to"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_foo.so"}));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{}));
// Settings is NOT empty
EXPECT_CALL(env_, GetDebugLayers()).WillRepeatedly(Return("VK_LAYER_foo"));
// Properties are empty, but process-specific applies
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("debug.vulkan.progname"))
.WillRepeatedly(Return("my_app"));
auto* mock_foo = env_.PreConfigureLibrary("/path/to/libVkLayer_foo.so");
ON_CALL(*mock_foo, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_foo", 1, 1, "Foo"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
// VK_LAYER_foo should NOT be implicitly enabled because properties were
// checked and they were empty
const auto* foo = identified_layers_.FindLayer("VK_LAYER_foo");
ASSERT_NE(foo, nullptr);
EXPECT_FALSE(foo->IsImplicitlyEnabled());
}
// Test that process-specific properties apply and override settings, when
// properties are NOT empty
TEST_F(VulkanLayersTest,
ImplicitEnablement_ProcessSpecificPropertiesOverrideSettings_NotEmpty) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, IsOSDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetProcessName()).WillRepeatedly(Return("my_app"));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, ListDirectory("/path/to"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_foo.so",
"libVkLayer_bar.so"}));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{}));
// Settings has VK_LAYER_foo
EXPECT_CALL(env_, GetDebugLayers()).WillRepeatedly(Return("VK_LAYER_foo"));
// Properties has VK_LAYER_bar, process-specific applies
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("debug.vulkan.progname"))
.WillRepeatedly(Return("my_app"));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layers"))
.WillRepeatedly(Return("VK_LAYER_bar"));
auto* mock_foo = env_.PreConfigureLibrary("/path/to/libVkLayer_foo.so");
ON_CALL(*mock_foo, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_foo", 1, 1, "Foo"), {}, {}});
auto* mock_bar = env_.PreConfigureLibrary("/path/to/libVkLayer_bar.so");
ON_CALL(*mock_bar, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[1])));
Lib1::layers.push_back(
{CreateLayerProps("VK_LAYER_bar", 1, 1, "Bar"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
// Only VK_LAYER_bar should be implicitly enabled
const auto* foo = identified_layers_.FindLayer("VK_LAYER_foo");
ASSERT_NE(foo, nullptr);
EXPECT_FALSE(foo->IsImplicitlyEnabled());
const auto* bar = identified_layers_.FindLayer("VK_LAYER_bar");
ASSERT_NE(bar, nullptr);
EXPECT_TRUE(bar->IsImplicitlyEnabled());
}
// Test that process-specific properties do NOT apply if process name
// mismatches, falling back to settings
TEST_F(
VulkanLayersTest,
ImplicitEnablement_ProcessSpecificPropertiesMismatch_FallbackToSettings) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, IsOSDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetProcessName()).WillRepeatedly(Return("my_app"));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, ListDirectory("/path/to"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_foo.so",
"libVkLayer_bar.so"}));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{}));
// Settings has VK_LAYER_foo
EXPECT_CALL(env_, GetDebugLayers()).WillRepeatedly(Return("VK_LAYER_foo"));
// Properties has VK_LAYER_bar, but process-specific mismatches ("other_app"
// vs "my_app")
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("debug.vulkan.progname"))
.WillRepeatedly(Return("other_app"));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layers"))
.WillRepeatedly(Return("VK_LAYER_bar"));
auto* mock_foo = env_.PreConfigureLibrary("/path/to/libVkLayer_foo.so");
ON_CALL(*mock_foo, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_foo", 1, 1, "Foo"), {}, {}});
auto* mock_bar = env_.PreConfigureLibrary("/path/to/libVkLayer_bar.so");
ON_CALL(*mock_bar, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[1])));
Lib1::layers.push_back(
{CreateLayerProps("VK_LAYER_bar", 1, 1, "Bar"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
// Only VK_LAYER_foo should be implicitly enabled (from settings)
const auto* foo = identified_layers_.FindLayer("VK_LAYER_foo");
ASSERT_NE(foo, nullptr);
EXPECT_TRUE(foo->IsImplicitlyEnabled());
const auto* bar = identified_layers_.FindLayer("VK_LAYER_bar");
ASSERT_NE(bar, nullptr);
EXPECT_FALSE(bar->IsImplicitlyEnabled());
}
// Test that process-specific properties do NOT apply if process name
// mismatches, and Settings is empty, falling back to global properties (since
// debuggable)
TEST_F(
VulkanLayersTest,
ImplicitEnablement_ProcessSpecificPropertiesMismatch_FallbackToGlobalProperties) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, IsOSDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetProcessName()).WillRepeatedly(Return("my_app"));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, ListDirectory("/path/to"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_bar.so"}));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{}));
EXPECT_CALL(env_, ListDirectory("/apex/com.android.graphics/lib64"))
.WillOnce(Return(std::vector<std::string>{}));
// Settings is empty
EXPECT_CALL(env_, GetDebugLayers()).WillRepeatedly(Return(""));
// Properties has VK_LAYER_bar, process-specific mismatches, but applies
// globally
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("debug.vulkan.progname"))
.WillRepeatedly(Return("other_app"));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layers"))
.WillRepeatedly(Return("VK_LAYER_bar"));
auto* mock_bar = env_.PreConfigureLibrary("/path/to/libVkLayer_bar.so");
ON_CALL(*mock_bar, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_bar", 1, 1, "Bar"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
const auto* bar = identified_layers_.FindLayer("VK_LAYER_bar");
ASSERT_NE(bar, nullptr);
EXPECT_TRUE(bar->IsImplicitlyEnabled());
}
// Test a layer ordering case where the following order is honored:
// - An implicitly-enabled layer will come first
// - An explicitly-enabled layer will come second
// - An SL will come last
TEST_F(VulkanLayersTest, OrderParity) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetExplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
ON_CALL(env_, ListDirectory("/path/to"))
.WillByDefault(Return(std::vector<std::string>{"libVkLayer_exp1.so",
"libVkLayer_exp2.so"}));
ON_CALL(env_, GetProperty("ro.vulkan.layers.platform.0"))
.WillByDefault(Return("libVkLayer_sys.so:VK_LAYER_sys:10"));
#if defined(__LP64__)
const std::string sys_dir = "/system/lib64/vulkan";
#else
const std::string sys_dir = "/system/lib/vulkan";
#endif
EXPECT_CALL(env_, FileExists(sys_dir + "/libVkLayer_sys.so"))
.WillOnce(Return(true));
ON_CALL(env_, GetDebugLayers()).WillByDefault(Return("VK_LAYER_exp1"));
auto* mock_exp1 = env_.PreConfigureLibrary("/path/to/libVkLayer_exp1.so");
ON_CALL(*mock_exp1, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_exp1", 1, 1, "Exp1"), {}, {}});
auto* mock_exp2 = env_.PreConfigureLibrary("/path/to/libVkLayer_exp2.so");
ON_CALL(*mock_exp2, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[1])));
Lib1::layers.push_back(
{CreateLayerProps("VK_LAYER_exp2", 1, 1, "Exp2"), {}, {}});
auto* mock_sys = env_.PreConfigureLibrary(sys_dir + "/libVkLayer_sys.so");
ON_CALL(*mock_sys, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[2])));
Lib2::layers.push_back(
{CreateLayerProps("VK_LAYER_sys", 1, 1, "Sys"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
const char* requested[] = {"VK_LAYER_exp2"};
EnabledLayers enabled;
VkResult result = enabled.Build(identified_layers_, requested, 1, env_);
ASSERT_EQ(result, VK_SUCCESS);
const auto& active = enabled.GetActiveLayers();
ASSERT_EQ(active.size(), 3u);
EXPECT_EQ(active[0]->GetLayer().GetName(), "VK_LAYER_exp1");
EXPECT_EQ(active[1]->GetLayer().GetName(), "VK_LAYER_exp2");
EXPECT_EQ(active[2]->GetLayer().GetName(), "VK_LAYER_sys");
}
// Test ??
//
// TODO: Watch the code execute to see what this test is actually doing
TEST_F(VulkanLayersTest, OrderDuplicateExplicit) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, GetExplicitLayerPaths())
.WillRepeatedly(Return("/path/to"));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, ListDirectory("/path/to"))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_exp1.so"}));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetDebugLayers()).WillRepeatedly(Return("VK_LAYER_exp1"));
auto* mock_exp1 = env_.PreConfigureLibrary("/path/to/libVkLayer_exp1.so");
ON_CALL(*mock_exp1, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_exp1", 1, 1, "Exp1"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
const char* requested[] = {"VK_LAYER_exp1"};
EnabledLayers enabled;
VkResult result = enabled.Build(identified_layers_, requested, 1, env_);
ASSERT_EQ(result, VK_SUCCESS);
const auto& active = enabled.GetActiveLayers();
ASSERT_EQ(active.size(), 1u);
EXPECT_EQ(active[0]->GetLayer().GetName(), "VK_LAYER_exp1");
}
TEST_F(VulkanLayersTest, LoadGraphicsMainlineLibraryInCorrectNamespace) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(true));
// Mock paths to only discover in graphics mainline module
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, ListDirectory("/data/local/debug/vulkan"))
.WillOnce(Return(std::vector<std::string>{}));
EXPECT_CALL(env_, ListDirectory(kGraphicsMainlineModuleDir))
.WillOnce(Return(std::vector<std::string>{"libVkLayer_graphics.so"}));
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
// We expect the library to be loaded in the "com_android_graphics"
// namespace
std::string full_path =
std::string(kGraphicsMainlineModuleDir) + "/libVkLayer_graphics.so";
EXPECT_CALL(
env_, LoadLibraryInExportedNamespace(full_path, "com_android_graphics"))
.Times(1);
auto* mock_graphics = env_.PreConfigureLibrary(full_path);
ON_CALL(*mock_graphics, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_graphics", 1, 1, "Graphics Layer"),
{},
{}});
identified_layers_.DoOneTimeSetup(env_);
const auto* layer = identified_layers_.FindLayer("VK_LAYER_graphics");
ASSERT_NE(layer, nullptr);
EXPECT_EQ(layer->GetName(), "VK_LAYER_graphics");
}
// Test global ordering of system layers based on the 'order' field in
// properties
TEST_F(VulkanLayersTest, SystemLayersOrdering) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
// Mock properties: SoC (10) < Platform (20) < OEM (30)
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.platform.0"))
.WillRepeatedly(Return("libVkLayer_p1.so:VK_LAYER_p1:20"));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.soc.0"))
.WillRepeatedly(Return("libVkLayer_s1.so:VK_LAYER_s1:10"));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.oem.0"))
.WillRepeatedly(Return("libVkLayer_o1.so:VK_LAYER_o1:30"));
#if defined(__LP64__)
const std::string plat_dir = "/system/lib64/vulkan";
const std::string soc_dir = "/system_ext/lib64/vulkan";
const std::string oem_dir = "/product/lib64/vulkan";
#else
const std::string plat_dir = "/system/lib/vulkan";
const std::string soc_dir = "/system_ext/lib/vulkan";
const std::string oem_dir = "/product/lib/vulkan";
#endif
EXPECT_CALL(env_, FileExists(plat_dir + "/libVkLayer_p1.so"))
.WillOnce(Return(true));
EXPECT_CALL(env_, FileExists(soc_dir + "/libVkLayer_s1.so"))
.WillOnce(Return(true));
EXPECT_CALL(env_, FileExists(oem_dir + "/libVkLayer_o1.so"))
.WillOnce(Return(true));
auto* mock_s1 = env_.PreConfigureLibrary(soc_dir + "/libVkLayer_s1.so");
ON_CALL(*mock_s1, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_s1", 1, 1, "SoC Layer"), {}, {}});
auto* mock_p1 = env_.PreConfigureLibrary(plat_dir + "/libVkLayer_p1.so");
ON_CALL(*mock_p1, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[1])));
Lib1::layers.push_back(
{CreateLayerProps("VK_LAYER_p1", 1, 1, "Platform Layer"), {}, {}});
auto* mock_o1 = env_.PreConfigureLibrary(oem_dir + "/libVkLayer_o1.so");
ON_CALL(*mock_o1, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[2])));
Lib2::layers.push_back(
{CreateLayerProps("VK_LAYER_o1", 1, 1, "OEM Layer"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
auto sys_list = identified_layers_.GetSystemLayers();
ASSERT_EQ(sys_list.size(), 3u);
EXPECT_EQ(sys_list[0]->GetName(), "VK_LAYER_s1"); // Order 10
EXPECT_EQ(sys_list[1]->GetName(), "VK_LAYER_p1"); // Order 20
EXPECT_EQ(sys_list[2]->GetName(), "VK_LAYER_o1"); // Order 30
}
// Test that debug override properties are used when IsOSDebuggable is true
TEST_F(VulkanLayersTest, SystemLayersOverride_DebuggableMatches) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, IsOSDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
// Mock properties: ro. and debug. are both set for platform.
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.platform.0"))
.WillRepeatedly(Return("libVkLayer_plat_ro.so:VK_LAYER_plat_ro:10"));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layers.platform.0"))
.WillRepeatedly(Return("libVkLayer_plat_dbg.so:VK_LAYER_plat_dbg:5"));
#if defined(__LP64__)
const std::string plat_dir = "/system/lib64/vulkan";
#else
const std::string plat_dir = "/system/lib/vulkan";
#endif
// We expect only the debug one to be checked for existence
EXPECT_CALL(env_, FileExists(plat_dir + "/libVkLayer_plat_dbg.so"))
.WillOnce(Return(true));
EXPECT_CALL(env_, FileExists(plat_dir + "/libVkLayer_plat_ro.so"))
.Times(0); // Should NOT be checked
auto* mock_dbg =
env_.PreConfigureLibrary(plat_dir + "/libVkLayer_plat_dbg.so");
ON_CALL(*mock_dbg, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_plat_dbg", 1, 1, "Debug Plat Layer"),
{},
{}});
identified_layers_.DoOneTimeSetup(env_);
auto sys_list = identified_layers_.GetSystemLayers();
ASSERT_EQ(sys_list.size(), 1u);
EXPECT_EQ(sys_list[0]->GetName(), "VK_LAYER_plat_dbg");
}
// Test that debug override properties are ignored when IsOSDebuggable is false
TEST_F(VulkanLayersTest, SystemLayersOverride_NotDebuggableIgnoresDebug) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, IsOSDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
// Mock properties: ro. and debug. are both set for platform.
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.platform.0"))
.WillRepeatedly(Return("libVkLayer_plat_ro.so:VK_LAYER_plat_ro:10"));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layers.platform.0"))
.WillRepeatedly(Return("libVkLayer_plat_dbg.so:VK_LAYER_plat_dbg:5"));
#if defined(__LP64__)
const std::string plat_dir = "/system/lib64/vulkan";
#else
const std::string plat_dir = "/system/lib/vulkan";
#endif
// We expect only the ro one to be checked for existence
EXPECT_CALL(env_, FileExists(plat_dir + "/libVkLayer_plat_ro.so"))
.WillOnce(Return(true));
EXPECT_CALL(env_, FileExists(plat_dir + "/libVkLayer_plat_dbg.so"))
.Times(0); // Should NOT be checked
auto* mock_ro =
env_.PreConfigureLibrary(plat_dir + "/libVkLayer_plat_ro.so");
ON_CALL(*mock_ro, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_plat_ro", 1, 1, "RO Plat Layer"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
auto sys_list = identified_layers_.GetSystemLayers();
ASSERT_EQ(sys_list.size(), 1u);
EXPECT_EQ(sys_list[0]->GetName(), "VK_LAYER_plat_ro");
}
// Test that set-level override applies: if at least one debug property is set
// for a set, the entire ro set is ignored, even if some debug properties in
// that set are empty.
TEST_F(VulkanLayersTest, SystemLayersOverride_SetLevelOverride) {
EXPECT_CALL(env_, IsDebuggable()).WillRepeatedly(Return(false));
EXPECT_CALL(env_, IsOSDebuggable()).WillRepeatedly(Return(true));
EXPECT_CALL(env_, GetExplicitLayerPaths()).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetImplicitLayerPaths()).WillRepeatedly(Return(""));
// Platform set:
// ro.platform.0 = ro0
// ro.platform.1 = ro1
// debug.platform.0 = dbg0
// debug.platform.1 = (empty)
// Result: only dbg0 should be loaded. ro1 should NOT be loaded even
// though debug.platform.1 is empty.
EXPECT_CALL(env_, GetProperty(_)).WillRepeatedly(Return(""));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.platform.0"))
.WillRepeatedly(Return("libVkLayer_ro0.so:VK_LAYER_ro0:10"));
EXPECT_CALL(env_, GetProperty("ro.vulkan.layers.platform.1"))
.WillRepeatedly(Return("libVkLayer_ro1.so:VK_LAYER_ro1:20"));
EXPECT_CALL(env_, GetProperty("debug.vulkan.layers.platform.0"))
.WillRepeatedly(Return("libVkLayer_dbg0.so:VK_LAYER_dbg0:5"));
#if defined(__LP64__)
const std::string plat_dir = "/system/lib64/vulkan";
#else
const std::string plat_dir = "/system/lib/vulkan";
#endif
EXPECT_CALL(env_, FileExists(plat_dir + "/libVkLayer_dbg0.so"))
.WillOnce(Return(true));
EXPECT_CALL(env_, FileExists(plat_dir + "/libVkLayer_ro0.so")).Times(0);
EXPECT_CALL(env_, FileExists(plat_dir + "/libVkLayer_ro1.so")).Times(0);
auto* mock_dbg0 =
env_.PreConfigureLibrary(plat_dir + "/libVkLayer_dbg0.so");
ON_CALL(*mock_dbg0, GetSymbol("vkGetInstanceProcAddr"))
.WillByDefault(Return(reinterpret_cast<void*>(g_mock_gpas[0])));
Lib0::layers.push_back(
{CreateLayerProps("VK_LAYER_dbg0", 1, 1, "Debug 0"), {}, {}});
identified_layers_.DoOneTimeSetup(env_);
auto sys_list = identified_layers_.GetSystemLayers();
ASSERT_EQ(sys_list.size(), 1u);
EXPECT_EQ(sys_list[0]->GetName(), "VK_LAYER_dbg0");
}
} // namespace layers
} // namespace vulkan