From de1f1f26d0c05f10c4e32016bb445608a6b057eb Mon Sep 17 00:00:00 2001 From: Ahmad Sayed Alsayed Ebrahim Date: Sat, 10 Oct 2026 16:47:52 +0300 Subject: [PATCH] fix(vkbridge): AHB sharing takes the handle fd that holds the memory Mali's gralloc can put several fds in an AHardwareBuffer's native handle; the one handed to the client is now the first at least as large as the allocation. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01S1U9H1evGNjHRQ3wRUdv5V --- app/src/main/cpp/vkbridge/server_memory.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/cpp/vkbridge/server_memory.c b/app/src/main/cpp/vkbridge/server_memory.c index 88c1259da..36ca6061e 100644 --- a/app/src/main/cpp/vkbridge/server_memory.c +++ b/app/src/main/cpp/vkbridge/server_memory.c @@ -301,7 +301,22 @@ static VkResult alloc_shared(const vkb_dispatch *dt, VkDevice dev, uint32_t s, c r = VK_ERROR_INVALID_EXTERNAL_HANDLE; break; } - *out_fd = fcntl(nh->data[0], F_DUPFD_CLOEXEC, 0); + /* Mali's gralloc can put more than one fd in the handle (metadata beside the pages): the + * memory is the one at least as large as the allocation. */ + int pick = -1; + for (int i = 0; i < nh->numFds && pick < 0; i++) { + off_t end = lseek(nh->data[i], 0, SEEK_END); + if (end >= (off_t)sz) pick = nh->data[i]; + if (nh->numFds > 1) VKB_INFO("ahb: handle fd %d of %d is %lld bytes", i + 1, nh->numFds, (long long)end); + } + if (pick < 0) { + VKB_INFO("ahb: no fd in the handle covers %llu bytes", (unsigned long long)sz); + dt->vkFreeMemory(dev, m->mem, NULL); + m->mem = VK_NULL_HANDLE; + r = VK_ERROR_INVALID_EXTERNAL_HANDLE; + break; + } + *out_fd = fcntl(pick, F_DUPFD_CLOEXEC, 0); break; } #endif