Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Check failed: result.second. #1124

Closed
mischnic opened this issue Apr 9, 2022 · 4 comments · Fixed by #1125
Closed

Fatal error: Check failed: result.second. #1124

mischnic opened this issue Apr 9, 2022 · 4 comments · Fixed by #1125
Assignees
Labels
bug Something isn't working

Comments

@mischnic
Copy link
Contributor

mischnic commented Apr 9, 2022

I get a native crash when returning an empty buffer for the second time:

<Buffer >


#
# Fatal error in , line 0
# Check failed: result.second.
#
#
#
#FailureMessage Object: 0x7ff7b74b9370
 1: 0x108b5adca node::NodePlatform::GetStackTracePrinter()::$_3::__invoke() [/usr/local/Cellar/node@16/16.14.2/bin/node]
 2: 0x10935c4c1 V8_Fatal(char const*, ...) [/usr/local/Cellar/node@16/16.14.2/bin/node]
 3: 0x108e48244 v8::internal::GlobalBackingStoreRegistry::Register(std::__1::shared_ptr<v8::internal::BackingStore>) [/usr/local/Cellar/node@16/16.14.2/bin/node]
 4: 0x108c4976b v8::ArrayBuffer::GetBackingStore() [/usr/local/Cellar/node@16/16.14.2/bin/node]
 5: 0x108b2415e node::ArrayBufferViewContents<char, 64ul>::Read(v8::Local<v8::ArrayBufferView>) [/usr/local/Cellar/node@16/16.14.2/bin/node]
 6: 0x108ae441f void node::Buffer::(anonymous namespace)::StringSlice<(node::encoding)5>(v8::FunctionCallbackInfo<v8::Value> const&) [/usr/local/Cellar/node@16/16.14.2/bin/node]
 7: 0x108c851e3 v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo) [/usr/local/Cellar/node@16/16.14.2/bin/node]
 8: 0x108c84c14 v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/usr/local/Cellar/node@16/16.14.2/bin/node]
 9: 0x108c843f3 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [/usr/local/Cellar/node@16/16.14.2/bin/node]
10: 0x10927ac39 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_BuiltinExit [/usr/local/Cellar/node@16/16.14.2/bin/node]
[1]    5260 trace trap  node x.js

Using

#[napi]
fn transform() -> Buffer {
  Buffer::from(vec![])
}
napi = {version = "2.2.0", default-features = false, features = ["napi4"]}

and then

const mod = require("./test.darwin-x64.node");

let result = mod.transform();
console.log(result.code);
result = mod.transform();
console.log(result.code);

This appears to also be broken when using the legacy mode (and also in napi-rs 1).

@mischnic
Copy link
Contributor Author

mischnic commented Apr 9, 2022

I believe this is related to:
nodejs/node#32463 (comment)

Except for the race condition, so, with node 14.x (also the 14.3 that implements the fix), it's no (more?) correct to have multiple create_node_buffer pointing at the same c buffer?

Yes – it’s unfortunate but it’s what it is.

because empty rust vectors are not actually allocated:

  let mut a = (vec![] as Vec<u8>);
  let mut b = (vec![] as Vec<u8>);
  println!("{:?}", a.as_mut_ptr());
  println!("{:?}", b.as_mut_ptr());

prints 0x1, 0x1

@Brooooooklyn
Copy link
Sponsor Member

After thinking about it for a while, I think this problem is hard to avoid at the NAPI-RS. Empty Vec case is easy to prevent, but there are many other cases that may provide the same pointer for different buffers.

@mischnic
Copy link
Contributor Author

So when building with napi build --platform, should I be seeing some warning from napi-rs with my reproduction? It's still behaving as I originally described

@Brooooooklyn
Copy link
Sponsor Member

It should panic in debug mode, napi build --platform and run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants