Skip to content

fix(geometry): use Nan::CopyBuffer for V8 Memory Sandbox and Electron 32+ compatibility - #382

Open
CoryAlbrecht wants to merge 3 commits into
mmomtchev:mainfrom
CoryAlbrecht:fix/v8-sandbox-wkb
Open

CoryAlbrecht wants to merge 3 commits into
mmomtchev:mainfrom
CoryAlbrecht:fix/v8-sandbox-wkb

Conversation

@CoryAlbrecht

Copy link
Copy Markdown

Summary

Fixes part of #28 (V8 memory sandbox and backing store pointer compression crash under Electron 32+).

In Electron 32+ (and any Node.js runtime built with v8_enable_sandbox = true), Chromium enforces the V8 Memory Sandbox, restricting all ArrayBuffer backing stores to a 4-GB pointer compression cage. Wrapping native malloc or GDAL heap buffers with Nan::NewBuffer() triggers an uncatchable fatal abort:

FATAL ERROR: v8_ArrayBuffer_NewBackingStore: Backing store address is outside the sandbox

This PR replaces unmanaged Nan::NewBuffer allocations with Nan::CopyBuffer() and immediate native deallocation:

 exportToWKB(byte_order, wkb_variant)
-  data = malloc(size)
-  exportToWkb(data)
-  Nan::AdjustExternalMemory(size)
-  return Nan::NewBuffer(data, size, free_callback)
+  data = malloc(size)
+  exportToWkb(data)
+  buffer = Nan::CopyBuffer(data, size)
+  free(data)
+  return buffer

Files changed:

  • src/geometry/gdal_geometry.cpp: Replace Nan::NewBuffer with Nan::CopyBuffer in exportToWKB and free native malloc buffer immediately.
  • src/utils/typed_array.cpp: Replace Nan::NewBuffer with Nan::CopyBuffer in TypedArray::New so typed array backing stores reside within the V8 pointer cage.
  • src/gdal_memfile.cpp: Replace Nan::NewBuffer with Nan::CopyBuffer in Memfile::vsimemRelease, immediately freeing the unlinked GDAL buffer with CPLFree(data).
  • test/api_geometry.test.ts: Add regression unit tests for toWKB() and toWKBAsync() across Point (MSB/LSB), LineString, and Polygon.

Evidence

  • Before:
    Under Electron 32+, invoking geom.toWKB() crashes the process immediately:
    FATAL ERROR: v8_ArrayBuffer_NewBackingStore: Backing store address is outside the sandbox
    zsh: abort (core dumped)
    
  • After:
    All geometries export valid Buffer instances under Electron 32+ and standard Node.js without memory errors:
    $ npm test
    1452 passing
    
    Added automated regression test suite in test/api_geometry.test.ts verifying binary WKB headers, Point MSB/LSB orders, LineString coordinates, and Polygon ring geometry round-trips. Memory copy overhead is negligible (~0.05 ms per feature batch).

Merge Danger

Door: Two-way

Fully backwards compatible. Public TypeScript interfaces, method signatures, and return types (Buffer, Promise<Buffer>) are identical.

Blast Radius: Minimal

Only changes how native C/GDAL byte buffers are transferred into V8 memory for exportToWKB, TypedArray::New, and vsimemRelease.

@mmomtchev

Copy link
Copy Markdown
Owner

Are you using gdal-async with Electron? How are you building for Windows? With clang from Visual Studio?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants