| /* | |
| * 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 <android-base/file.h> | |
| #include <graphicsenv/GraphicsEnv.h> | |
| #include <gtest/gtest.h> | |
| #include <vulkan/vulkan.h> | |
| #include <atomic> | |
| #include <chrono> | |
| #include <mutex> | |
| #include <thread> | |
| namespace android { | |
| namespace { | |
| std::timed_mutex g_app_lock; | |
| std::atomic<bool> g_t1_ready{false}; | |
| std::atomic<bool> g_t2_ready{false}; | |
| std::atomic<bool> g_start_signal{false}; | |
| std::atomic<bool> g_deadlock_detected{false}; | |
| thread_local bool g_is_t2 = false; | |
| static void* VKAPI_PTR MyAllocation(void*, | |
| size_t size, | |
| size_t, | |
| VkSystemAllocationScope) { | |
| if (g_is_t2) { | |
| g_t2_ready = true; | |
| // Wait for the main thread to drop the barrier | |
| while (!g_start_signal) | |
| std::this_thread::yield(); | |
| // T2 tries to acquire the app lock. | |
| // If SampleLayer held g_lock, T1 is stuck waiting for g_lock, and T1 | |
| // holds g_app_lock. So T2 will timeout here, proving the AB-BA | |
| // deadlock. | |
| if (!g_app_lock.try_lock_for(std::chrono::seconds(2))) { | |
| g_deadlock_detected = true; | |
| // Return to let T2 finish and release g_lock, safely breaking the | |
| // deadlock cycle! | |
| return malloc(size); | |
| } | |
| g_app_lock.unlock(); | |
| } | |
| return malloc(size); | |
| } | |
| static void* VKAPI_PTR MyReallocation(void*, | |
| void* pOrig, | |
| size_t size, | |
| size_t, | |
| VkSystemAllocationScope) { | |
| return realloc(pOrig, size); | |
| } | |
| static void VKAPI_PTR MyFree(void*, void* pMem) { | |
| free(pMem); | |
| } | |
| TEST(VulkanLayersTest, SampleLayerDeadlockTest) { | |
| GraphicsEnv::getInstance().setLayerPaths( | |
| GraphicsEnv::getInstance().getAppNamespace(), | |
| android::base::GetExecutableDirectory()); | |
| VkAllocationCallbacks allocator = {nullptr, MyAllocation, MyReallocation, | |
| MyFree, nullptr}; | |
| const char* layers[] = {"VK_LAYER_ANDROID_sample", | |
| "VK_LAYER_libVulkanTestLayer"}; | |
| VkInstanceCreateInfo instInfo = {}; | |
| instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; | |
| instInfo.enabledLayerCount = 2; | |
| instInfo.ppEnabledLayerNames = layers; | |
| VkInstance instance; | |
| ASSERT_EQ(VK_SUCCESS, vkCreateInstance(&instInfo, &allocator, &instance)); | |
| g_t1_ready = false; | |
| g_t2_ready = false; | |
| g_start_signal = false; | |
| g_deadlock_detected = false; | |
| // T2 represents the Async Thread | |
| std::thread t2([&]() { | |
| g_is_t2 = true; | |
| // Enters Vulkan -> acquires g_lock -> triggers MyAllocation callback | |
| vkGetInstanceProcAddr(instance, "vkTriggerAllocationT2"); | |
| }); | |
| // Wait until T2 is inside the callback | |
| while (!g_t2_ready) | |
| std::this_thread::yield(); | |
| // T1 represents the Main Render Thread | |
| std::thread t1([&]() { | |
| g_app_lock.lock(); | |
| g_t1_ready = true; | |
| while (!g_start_signal) | |
| std::this_thread::yield(); | |
| vkGetInstanceProcAddr(instance, "vkTriggerAllocationT1"); | |
| g_app_lock.unlock(); | |
| }); | |
| while (!g_t1_ready) | |
| std::this_thread::yield(); | |
| // Drop the barrier | |
| g_start_signal = true; | |
| t1.join(); | |
| t2.join(); | |
| EXPECT_FALSE(g_deadlock_detected) | |
| << "AB-BA Deadlock detected! SampleLayer held g_lock during callback."; | |
| vkDestroyInstance(instance, &allocator); | |
| } | |
| } // namespace | |
| } // namespace android |