Commit 6b48f624 authored by Daniele Castagna's avatar Daniele Castagna Committed by Commit Bot

exo: Fix shm_pool_resize

When we migrated GpuMemoryBufferHandle to the new shared memory API
we didn't take into account that the previous SharedMemoryHandle would
not enforce the size of the shared memory region stayed the same,
while UnsafeSharedMemoryRegion does.

Before using UnsafeSharedMemoryRegion we could pass the handle,
ignore shm_pool_resize and map to an offset that is out of the original
size we created the pool with.
This breaks with UnsafeSharedMemoryRegion that checks the region we're
mapping is contained in size.

This CL implements shm_pool_resize, adding protocol checks and recreating
UnsafeSharedMemoryRegion with the proper new size.

Bug: 961155
Change-Id: I443feca90ad332c40cb74f8a76182a20a565d7a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1608800
Commit-Queue: Daniele Castagna <dcastagna@chromium.org>
Reviewed-by: default avatarAlex Ilin <alexilin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#660082}
parent 471c7972
......@@ -91,4 +91,26 @@ std::unique_ptr<Buffer> SharedMemory::CreateBuffer(const gfx::Size& size,
false /* is_overlay_candidate */, false /* y_invert */);
}
size_t SharedMemory::GetSize() const {
return shared_memory_region_.GetSize();
}
bool SharedMemory::Resize(const size_t new_size) {
// The following code is to replace |shared_memory_region_| with an identical
// UnsafeSharedMemoryRegion with a new size.
base::subtle::PlatformSharedMemoryRegion platform_region =
base::UnsafeSharedMemoryRegion::TakeHandleForSerialization(
std::move(shared_memory_region_));
base::UnguessableToken guid = platform_region.GetGUID();
base::subtle::PlatformSharedMemoryRegion updated_platform_region =
base::subtle::PlatformSharedMemoryRegion::Take(
platform_region.PassPlatformHandle(),
base::subtle::PlatformSharedMemoryRegion::Mode::kUnsafe, new_size,
guid);
shared_memory_region_ = base::UnsafeSharedMemoryRegion::Deserialize(
std::move(updated_platform_region));
return shared_memory_region_.IsValid();
}
} // namespace exo
......@@ -33,6 +33,9 @@ class SharedMemory {
unsigned offset,
int stride);
size_t GetSize() const;
bool Resize(const size_t new_size);
private:
base::UnsafeSharedMemoryRegion shared_memory_region_;
......
......@@ -86,7 +86,14 @@ void shm_pool_destroy(wl_client* client, wl_resource* resource) {
}
void shm_pool_resize(wl_client* client, wl_resource* resource, int32_t size) {
// Nothing to do here.
auto* shm = GetUserDataAs<SharedMemory>(resource);
if (size < 0 || static_cast<size_t>(size) < shm->GetSize()) {
wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD,
"Can't shrink a shm pool.");
}
if (!shm->Resize(size))
wl_resource_post_no_memory(resource);
}
const struct wl_shm_pool_interface shm_pool_implementation = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment