nan: Weird and inexplicable crashes when using TypedArrayContents

I’m getting really weird crashes with v8 array buffers / typed arrays

Base:

size_t numEntries = 2 * TOTAL_NUM_BANDS;
size_t numBytes = sizeof(float) * numEntries;
v8::Local<v8::ArrayBuffer> dataBuffer = v8::ArrayBuffer::New(v8::Isolate::GetCurrent(), numBytes);
v8::Local<v8::Float32Array> dataArr = v8::Float32Array::New(minMaxDataBuffer, 0, numEntries);

works:

 auto dataPtr = static_cast<float*>(dataBuffer->GetContents().Data());

segfaults:

auto test = Nan::TypedArrayContents<float>(dataArr);

…but it doesn’t segfault immediately, but rather a couple of seconds later, probably when some GC happens.

I don’t even need to do anything with the pointer. It is enough to construct a (temporary) Nan::TypedArrayContents

I have digged through the nan_typedarray_contents.h header

once I comment out

data = static_cast<char*>(buffer->GetBackingStore()->Data()) + byte_offset;

there is no crash.

I tried to find out what this is, but now it’s getting really weird:

auto dataPtr2 = static_cast<float*>(dataBuffer->GetBackingStore()->Data());
//auto dataPtr3 = static_cast<float*>(dataBuffer->GetBackingStore()->Data());

works. Where as

auto dataPtr2 = static_cast<float*>(dataBuffer->GetBackingStore()->Data());
auto dataPtr3 = static_cast<float*>(dataBuffer->GetBackingStore()->Data());

(just the first call repeated) segfaults immediately… I suppose this is a node bug?

I’m compiling against electron 8.0.0, node version should be 12.13.0

I’ll try with a later node / electron version now

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 26 (4 by maintainers)

Most upvoted comments

NAN, being a header-only library, has never promised a stable ABI.

Right – to be clear, there’s also nothing wrong with NAN here. It’s Node.js that does more or less promise to expose a stable ABI through its module version number, and fails to do so here.

This is for sure a previously unknown issue and should be noted among the known issues. NAN, being a header-only library, has never promised a stable ABI. node-addon-api was created in order to provide a stable ABI.

On November 23, 2021 12:55:09 PM GMT+02:00, Robin Christ @.***> wrote:

Maybe this should be added to the “Known issues” section? And it would probably be good to place a warning at the very top

like

Please be aware that nan, as the name says, uses the native Node.js API
Due to the way Node.js (and Chromium) work, using nan or the native Node.js API may lead to a variety of issues, most importantly ABI issues which may crash the application when certain features are used.
This is especially important if you're planning to write an addon for Electron
Unless you're sure you want to go this way and know what you're doing, please consider using node-addon-api (based on the ABI stable Node-API / N-API) instead

Opinions on this?