Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(webgpu): buffer upload read past the end of the caller's data

yhh86b89d4259e3
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
index 53ea3ae5a..1e3b58800 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
@@ -410,11 +410,21 @@ BufferHandle WebGPUDevice::createBuffer(const BufferDesc& desc, const void* init
buffers_[id] = BufferRec{buffer, desc.size, desc.usage};
if (initialData && desc.size > 0) {
- wgpuQueueWriteBuffer(queue_, buffer, 0, initialData, (desc.size + 3u) & ~3u);
+ writeBufferFromStart(buffer, initialData, desc.size);
}
return BufferHandle{id};
}
+void WebGPUDevice::writeBufferFromStart(WGPUBuffer buffer, const void* data, u32 size) {
+ if (!webgpu::needsWriteStaging(size)) {
+ wgpuQueueWriteBuffer(queue_, buffer, 0, data, size);
+ return;
+ }
+ std::vector<u8> padded(webgpu::alignedWriteSize(size), 0);
+ std::memcpy(padded.data(), data, size);
+ wgpuQueueWriteBuffer(queue_, buffer, 0, padded.data(), padded.size());
+}
+
void WebGPUDevice::deleteBuffer(BufferHandle buffer) {
auto it = buffers_.find(static_cast<u32>(buffer));
if (it == buffers_.end()) return;
@@ -449,6 +459,24 @@ void WebGPUDevice::updateBuffer(BufferHandle buffer, u32 offsetBytes, const void
offsetBytes, sizeBytes, it->second.size);
return;
}
+ // GL takes any offset and size; WriteBuffer takes 4-byte multiples of both.
+ if ((offsetBytes & 3u) != 0) {
+ ES_LOG_ERROR("WebGPUDevice::updateBuffer: offset {} is not 4-byte aligned", offsetBytes);
+ return;
+ }
+ if (webgpu::needsWriteStaging(sizeBytes)) {
+ // Only a write ending at the logical end may pad, into the slack the rounded
+ // allocation already has. One ending mid-buffer would clobber the next field.
+ if (offsetBytes + sizeBytes != it->second.size) {
+ ES_LOG_ERROR("WebGPUDevice::updateBuffer: unaligned partial write {}+{} of {}",
+ offsetBytes, sizeBytes, it->second.size);
+ return;
+ }
+ std::vector<u8> padded(webgpu::alignedWriteSize(sizeBytes), 0);
+ std::memcpy(padded.data(), data, sizeBytes);
+ wgpuQueueWriteBuffer(queue_, it->second.buffer, offsetBytes, padded.data(), padded.size());
+ return;
+ }
wgpuQueueWriteBuffer(queue_, it->second.buffer, offsetBytes, data, sizeBytes);
}
@@ -467,7 +495,7 @@ void WebGPUDevice::resizeBuffer(BufferHandle buffer, u32 sizeBytes, const void*
it->second.size = sizeBytes;
if (data && sizeBytes > 0 && it->second.buffer) {
- wgpuQueueWriteBuffer(queue_, it->second.buffer, 0, data, (sizeBytes + 3u) & ~3u);
+ writeBufferFromStart(it->second.buffer, data, sizeBytes);
}
}
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
index a1c2f5ae9..d7bfd970f 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
@@ -388,6 +388,11 @@ private:
/// Drop every cached group referencing a dying resource id (see cache note).
void evictBindGroups(u64 id);
+ /// Fill @p buffer from offset 0 with @p size bytes of @p data. WriteBuffer's size
+ /// must be a 4-byte multiple; the caller's allocation is only @p size bytes, so a
+ /// short tail is padded through a staging copy rather than over-read.
+ void writeBufferFromStart(WGPUBuffer buffer, const void* data, u32 size);
+
// GPU timing — the WebGPU analog of GLDevice's GL_TIME_ELAPSED timer, so the
// profiler's gpuMs/gpuScopes populate on both backends. Active only when the
// device has the timestamp-query feature; otherwise createTimerQuery returns 0
diff --git a/src/esengine/renderer/webgpu/WebGPUMappings.hpp b/src/esengine/renderer/webgpu/WebGPUMappings.hpp
index 0d23ea589..92c1fb040 100644
--- a/src/esengine/renderer/webgpu/WebGPUMappings.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUMappings.hpp
@@ -402,4 +402,10 @@ inline u32 scanWGSLBindingMask(const char* source, u32 group) {
return mask;
}
+/// WriteBuffer's size (and offset) must be a 4-byte multiple; buffers are allocated
+/// rounded up so the slack exists. Callers supply exactly `size` bytes, so an
+/// unaligned size must be staged, never read up to `alignedWriteSize` in place.
+inline u32 alignedWriteSize(u32 size) { return (size + 3u) & ~3u; }
+inline bool needsWriteStaging(u32 size) { return (size & 3u) != 0; }
+
} // namespace esengine::webgpu
diff --git a/tests/renderer/test_webgpu_device.cpp b/tests/renderer/test_webgpu_device.cpp
index 42b638783..5f3517ad8 100644
--- a/tests/renderer/test_webgpu_device.cpp
+++ b/tests/renderer/test_webgpu_device.cpp
@@ -227,3 +227,21 @@ TEST_CASE("null-device readback: requests degrade to Invalid, polls report Faile
CHECK(!device.takeReadback(ReadbackHandle{42}, pixels, sizeof(pixels)));
device.discardReadback(ReadbackHandle{42});
}
+
+TEST_CASE("write sizes round the ALLOCATION, never the read out of the caller's data") {
+ // Buffers are allocated rounded up, so the slack to receive padding exists.
+ CHECK(alignedWriteSize(0) == 0);
+ CHECK(alignedWriteSize(4) == 4);
+ CHECK(alignedWriteSize(6) == 8);
+ CHECK(alignedWriteSize(13) == 16);
+
+ // A size that already fits is written straight through; anything else has to be
+ // staged, because reading alignedWriteSize bytes runs off the end of the source.
+ // 6 bytes is a three-index u16 draw — one triangle, the smallest real case.
+ for (u32 size = 0; size <= 64; ++size) {
+ const bool staged = needsWriteStaging(size);
+ CHECK(staged == (size % 4 != 0));
+ if (!staged) CHECK(alignedWriteSize(size) == size);
+ else CHECK(alignedWriteSize(size) > size);
+ }
+}