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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(engine): renderer texture/geometry safety — failed alloc, external double-free, empty-layout div0
esengine3bb377fab2f3
diff --git a/src/esengine/renderer/CustomGeometry.cpp b/src/esengine/renderer/CustomGeometry.cppindex a14e52a73..3d187e788 100644--- a/src/esengine/renderer/CustomGeometry.cpp+++ b/src/esengine/renderer/CustomGeometry.cpp@@ -21,6 +21,13 @@ void CustomGeometry::init(GfxDevice& device, const f32* vertices, u32 vertexCoundevice_ = &device;dynamic_ = dynamic;stride_ = layout.getStride();+ if (stride_ == 0) {+ // An empty vertex layout would make the divide below a divide-by-zero+ // (a wasm trap). Leave the geometry uninitialized: vao_ stays null, so+ // isValid() is false and bind()/draw become no-ops.+ ES_LOG_ERROR("CustomGeometry::init: empty vertex layout (stride 0); skipping setup");+ return;+ }vertexCount_ = vertexCount * sizeof(f32) / stride_;vao_ = VertexArray::create(device);diff --git a/src/esengine/renderer/Texture.cpp b/src/esengine/renderer/Texture.cppindex 4295b9163..ca86f39f8 100644--- a/src/esengine/renderer/Texture.cpp+++ b/src/esengine/renderer/Texture.cpp@@ -38,7 +38,7 @@ u32 bytesPerPixel(TextureFormat format) {} // namespaceTexture::~Texture() {- if (textureId_ != 0 && device_) {+ if (textureId_ != 0 && device_ && owns_) {device_->deleteTexture(textureId_);}}@@ -48,7 +48,8 @@ Texture::Texture(Texture&& other) noexcept, textureId_(other.textureId_), width_(other.width_), height_(other.height_)- , format_(other.format_) {+ , format_(other.format_)+ , owns_(other.owns_) {other.textureId_ = 0;other.width_ = 0;other.height_ = 0;@@ -57,7 +58,7 @@ Texture::Texture(Texture&& other) noexceptTexture& Texture::operator=(Texture&& other) noexcept {if (this != &other) {- if (textureId_ != 0 && device_) {+ if (textureId_ != 0 && device_ && owns_) {device_->deleteTexture(textureId_);}device_ = other.device_;@@ -65,6 +66,7 @@ Texture& Texture::operator=(Texture&& other) noexcept {width_ = other.width_;height_ = other.height_;format_ = other.format_;+ owns_ = other.owns_;other.textureId_ = 0;other.width_ = 0;other.height_ = 0;@@ -125,6 +127,7 @@ Unique<Texture> Texture::createFromExternalId(GfxDevice& device, u32 glTextureIdtexture->width_ = width;texture->height_ = height;texture->format_ = format;+ texture->owns_ = false; // external owner frees the GL id; don't double-free itreturn texture;}@@ -134,6 +137,12 @@ bool Texture::initialize(const TextureSpecification& spec) {format_ = spec.format;textureId_ = device_->createTexture();+ if (textureId_ == 0) {+ // Out of memory or a lost context: surface the failure instead of+ // returning a "valid" texture wrapping id 0 (which renders as black).+ ES_LOG_ERROR("Texture::initialize: createTexture failed for {}x{}", width_, height_);+ return false;+ }device_->texImage2D(textureId_, width_, height_, toGfxPixelFormat(spec.format), nullptr);device_->setTextureParams(textureId_, spec.minFilter, spec.magFilter, spec.wrapS, spec.wrapT);diff --git a/src/esengine/renderer/Texture.hpp b/src/esengine/renderer/Texture.hppindex 1cf9061d7..e0c18b513 100644--- a/src/esengine/renderer/Texture.hpp+++ b/src/esengine/renderer/Texture.hpp@@ -268,6 +268,9 @@ private:u32 width_ = 0;u32 height_ = 0;TextureFormat format_ = TextureFormat::None;+ // False for textures wrapping an externally-owned GL id (createFromExternalId):+ // the external owner frees it, so this wrapper must NOT delete it (double-free).+ bool owns_ = true;};} // namespace esenginediff --git a/tests/renderer/MockGfxDevice.hpp b/tests/renderer/MockGfxDevice.hppindex fb22d7c5d..b1b48c810 100644--- a/tests/renderer/MockGfxDevice.hpp+++ b/tests/renderer/MockGfxDevice.hpp@@ -32,6 +32,7 @@ struct MockGfxDevice final : GfxDevice {int setTextureParamsCalls = 0;int generateMipmapsCalls = 0;u32 nextTextureId = 100;+ bool createTextureFails = false; // toggle to exercise the OOM / lost-context path (createTexture -> 0)u32 lastDeletedTexture = 0;GfxCompressedFormat lastCompressedFormat = GfxCompressedFormat::ETC2_RGBA8;u32 lastCompressedByteLength = 0;@@ -119,7 +120,7 @@ struct MockGfxDevice final : GfxDevice {void drawArrays(u32, u32) override {}void drawElementsInstanced(u32, GfxDataType, u32, u32) override {}- u32 createTexture() override { ++createTextureCalls; return nextTextureId++; }+ u32 createTexture() override { ++createTextureCalls; return createTextureFails ? 0u : nextTextureId++; }void deleteTexture(u32 id) override { ++deleteTextureCalls; lastDeletedTexture = id; }void texImage2D(u32, u32, u32, GfxPixelFormat, const void*) override { ++texImage2DCalls; }void texSubImage2D(u32, i32, i32, u32, u32, GfxPixelFormat, const void*) override { ++texSubImage2DCalls; }diff --git a/tests/renderer/test_buffer_device.cpp b/tests/renderer/test_buffer_device.cppindex 747069dfe..44c9ebbe3 100644--- a/tests/renderer/test_buffer_device.cpp+++ b/tests/renderer/test_buffer_device.cpp@@ -89,6 +89,19 @@ int main() {CHECK(d.bindVertexArrayCalls >= 1, "geom.bind routes through device");}+ // --- CustomGeometry: an empty vertex layout must not divide-by-zero ---+ // stride 0 would trap on the vertexCount divide; init must bail gracefully.+ {+ MockGfxDevice d;+ float verts[] = { 0, 0, 0, 0 };+ CustomGeometry geom;+ geom.init(d, verts, 4, VertexLayout{}); // empty layout -> stride 0+ CHECK(!geom.isValid(), "init with an empty layout leaves the geometry invalid (no crash)");+ CHECK(d.createBufferCalls == 0, "no VBO is created for an empty layout");+ geom.bind(d);+ CHECK(d.bindVertexArrayCalls == 0, "bind on an empty-layout geometry is a no-op");+ }+if (g_failures == 0) {std::printf("\nALL BUFFER/GEOMETRY TESTS PASSED\n");return 0;diff --git a/tests/renderer/test_texture_fb.cpp b/tests/renderer/test_texture_fb.cppindex f0a32e53a..32421a6b6 100644--- a/tests/renderer/test_texture_fb.cpp+++ b/tests/renderer/test_texture_fb.cpp@@ -67,6 +67,39 @@ int main() {CHECK(d.texSubImage2DCalls == before + 1, "setDataRaw uploads when size is sufficient");}+ // --- create() fails (returns null) when the device can't allocate a texture ---+ // createTexture returns 0 on OOM / lost context; initialize() must surface that+ // instead of returning a "valid" texture wrapping id 0 (which renders as black).+ {+ MockGfxDevice d;+ d.createTextureFails = true;+ TextureSpecification spec;+ spec.width = 8; spec.height = 8; spec.format = TextureFormat::RGBA8;+ auto tex = Texture::create(d, spec);+ CHECK(tex == nullptr, "create returns null when device.createTexture fails");+ CHECK(d.texImage2DCalls == 0, "no upload is attempted after a failed allocation");+ }++ // --- createFromExternalId must NOT delete the externally-owned GL texture ---+ // The external owner frees that id; deleting it here too is a double-free.+ {+ MockGfxDevice d;+ {+ auto tex = Texture::createFromExternalId(d, 42, 8, 8);+ CHECK(tex != nullptr && tex->getId() == 42, "wrapper holds the external id");+ }+ CHECK(d.deleteTextureCalls == 0, "destructor does NOT delete an externally-owned texture");+ }++ // --- an engine-owned texture IS still deleted on destruction ---+ {+ MockGfxDevice d;+ TextureSpecification spec;+ spec.width = 4; spec.height = 4; spec.format = TextureFormat::RGBA8;+ { auto tex = Texture::create(d, spec); }+ CHECK(d.deleteTextureCalls == 1, "destructor deletes an engine-owned texture");+ }+// --- Framebuffer: color + depth-stencil ---{MockGfxDevice d;