From a74d5b630c962ba89630f65c3d2f2a498581d061 Mon Sep 17 00:00:00 2001 From: Sascha Wessel Date: Sun, 26 Apr 2020 07:53:06 +0200 Subject: [PATCH] libuwac/window: Fix memory leak / SIGBUS `UwacWindowShmAllocBuffers()` allocates memory with `mmap` and never frees it resulting in SIGBUS errors and running out of memory after some time. Adding a corresponding `munmap` fixes this issue. --- uwac/libuwac/uwac-priv.h | 1 + uwac/libuwac/uwac-window.c | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/uwac/libuwac/uwac-priv.h b/uwac/libuwac/uwac-priv.h index ccf237f..75ad6e0 100644 --- a/uwac/libuwac/uwac-priv.h +++ b/uwac/libuwac/uwac-priv.h @@ -218,6 +218,7 @@ struct uwac_buffer #endif struct wl_buffer* wayland_buffer; void* data; + size_t size; }; typedef struct uwac_buffer UwacBuffer; diff --git a/uwac/libuwac/uwac-window.c b/uwac/libuwac/uwac-window.c index 28c2fd4..f76d43b 100644 --- a/uwac/libuwac/uwac-window.c +++ b/uwac/libuwac/uwac-window.c @@ -65,6 +65,7 @@ static void UwacWindowDestroyBuffers(UwacWindow* w) region16_uninit(&buffer->damage); #endif wl_buffer_destroy(buffer->wayland_buffer); + munmap(buffer->data, buffer->size); } w->nbuffers = 0; @@ -306,11 +307,15 @@ int UwacWindowShmAllocBuffers(UwacWindow* w, int nbuffers, int allocSize, uint32 int i, fd; void* data; struct wl_shm_pool* pool; + size_t pagesize = sysconf(_SC_PAGESIZE); newBuffers = xrealloc(w->buffers, (w->nbuffers + nbuffers) * sizeof(UwacBuffer)); if (!newBuffers) return UWAC_ERROR_NOMEMORY; + /* round up to a multiple of PAGESIZE to page align data for each buffer */ + allocSize = (allocSize + pagesize - 1) & ~(pagesize - 1); + w->buffers = newBuffers; memset(w->buffers + w->nbuffers, 0, sizeof(UwacBuffer) * nbuffers); fd = uwac_create_anonymous_file(allocSize * nbuffers); @@ -346,6 +351,7 @@ int UwacWindowShmAllocBuffers(UwacWindow* w, int nbuffers, int allocSize, uint32 region16_init(&buffer->damage); #endif buffer->data = data + (allocSize * i); + buffer->size = allocSize; buffer->wayland_buffer = wl_shm_pool_create_buffer(pool, allocSize * i, width, height, w->stride, format); wl_buffer_add_listener(buffer->wayland_buffer, &buffer_listener, buffer); -- 2.7.4