From 1ff86021a7a06d2548482c40b1584042e298f58e Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Wed, 19 Jun 2024 19:19:11 +0200 Subject: [PATCH] rusticl: add new CL_INVALID_BUFFER_SIZE condition for clCreateBuffer Cc: mesa-stable Part-of: --- src/gallium/frontends/rusticl/api/memory.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/gallium/frontends/rusticl/api/memory.rs b/src/gallium/frontends/rusticl/api/memory.rs index 144a5d07221..07f10aca482 100644 --- a/src/gallium/frontends/rusticl/api/memory.rs +++ b/src/gallium/frontends/rusticl/api/memory.rs @@ -275,13 +275,26 @@ fn create_buffer_with_properties( return Err(CL_INVALID_BUFFER_SIZE); } - // ... or if size is greater than CL_DEVICE_MAX_MEM_ALLOC_SIZE for all devices in context. + // ... or if size is greater than CL_DEVICE_MAX_MEM_ALLOC_SIZE for all devices in context, if checked_compare(size, Ordering::Greater, c.max_mem_alloc()) { return Err(CL_INVALID_BUFFER_SIZE); } validate_host_ptr(host_ptr, flags)?; + // or if CL_MEM_USE_HOST_PTR is set in flags and host_ptr is a pointer returned by clSVMAlloc + // and size is greater than the size passed to clSVMAlloc. + if let Some((svm_ptr, svm_layout)) = c.find_svm_alloc(host_ptr as usize) { + // SAFETY: they are part of the same allocation, and because host_ptr >= svm_ptr we can cast + // to usize. + let diff = unsafe { host_ptr.offset_from(svm_ptr) } as usize; + + // technically we don't have to account for the offset, but it's almost for free. + if size > svm_layout.size() - diff { + return Err(CL_INVALID_BUFFER_SIZE); + } + } + let props = Properties::from_ptr_raw(properties); // CL_INVALID_PROPERTY if a property name in properties is not a supported property name, if // the value specified for a supported property name is not valid, or if the same property name