Skip to content

Commit 068afd7

Browse files
committed
Fix bug in MapMemory2KHR where lseek on a non-memory fd can return errno ESPIPE
1 parent 55110a7 commit 068afd7

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/vulkan/wrapper/wrapper_device_memory.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ WRAPPER_MapMemory2KHR(VkDevice _device,
441441

442442
if (debug) WLOGD("Creating a memory map for mem %p (ahb=%p)", pMemoryMapInfo->memory, mem->ahardware_buffer);
443443

444+
int ahb_size = -1;
444445
if (mem->ahardware_buffer) {
445446
const native_handle_t *handle;
446447
const int *handle_fds;
@@ -450,34 +451,49 @@ WRAPPER_MapMemory2KHR(VkDevice _device,
450451

451452
int idx;
452453
for (idx = 0; idx < handle->numFds; idx++) {
453-
size_t size = lseek(handle_fds[idx], 0, SEEK_END);
454+
off_t size = lseek(handle_fds[idx], 0, SEEK_END);
455+
if (size < 0) {
456+
int error = errno;
457+
WLOG("lseek(0, SEEK_END) failed on the AHB fd (idx=%d, fd=%d), errno = %d, trying another fd", idx, handle_fds[idx], error);
458+
continue;
459+
}
454460
if (size >= mem->alloc_size) {
461+
if (debug) WLOGD("Found size from lseek(idx=%d, fd=%d) = %x (vs alloc_size of %x)", idx, handle_fds[idx], size, mem->alloc_size);
462+
ahb_size = size;
455463
break;
456464
}
457465
}
458-
assert(idx < handle->numFds);
466+
if (idx >= handle->numFds) {
467+
WLOGE("Failed to find an appropriate AHB fd >= alloc_size of 0x%x", mem->alloc_size);
468+
return vk_error(device, VK_ERROR_MEMORY_MAP_FAILED);
469+
}
459470
fd = handle_fds[idx];
460471
} else {
461472
fd = mem->dmabuf_fd;
462473
}
463474

464475
if (debug) WLOGD("mem %p associated with fd %d", pMemoryMapInfo->memory, fd);
465476

466-
if (pMemoryMapInfo->size == VK_WHOLE_SIZE)
467-
mem->map_size = mem->alloc_size > 0 ?
477+
if (pMemoryMapInfo->size == VK_WHOLE_SIZE) {
478+
int result = mem->alloc_size > 0 ?
468479
mem->alloc_size : lseek(fd, 0, SEEK_END);
469-
else
480+
if (result < 0) {
481+
WLOGE("Failed to lseek(fd=%d, 0, SEEK_END), previous ahb_size = %x, errno = %d", fd, ahb_size, errno);
482+
}
483+
mem->map_size = result;
484+
} else {
470485
mem->map_size = pMemoryMapInfo->size;
486+
}
471487

472-
if (debug) WLOGD("Mmapping mem %p with fd %d to %p", pMemoryMapInfo->memory, fd, placed_info->pPlacedAddress);
488+
if (debug) WLOGD("Mmapping mem %p with fd %d to %p with size %x", pMemoryMapInfo->memory, fd, placed_info->pPlacedAddress, mem->map_size);
473489
mem->map_address = mmap(placed_info->pPlacedAddress,
474490
mem->map_size, PROT_READ | PROT_WRITE,
475491
MAP_SHARED | MAP_FIXED, fd, 0);
476492

477493
if (mem->map_address == MAP_FAILED) {
478494
mem->map_address = NULL;
479495
mem->map_size = 0;
480-
WLOGE("mmap failed emulating MapMemory2KHR");
496+
WLOGE("mmap failed emulating MapMemory2KHR: errno = %d", errno);
481497
return vk_error(device, VK_ERROR_MEMORY_MAP_FAILED);
482498
}
483499

0 commit comments

Comments
 (0)