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

napi_get_value_uint32 difference between Node 14 and previous #33117

Closed
emilbayes opened this issue Apr 28, 2020 · 4 comments
Closed

napi_get_value_uint32 difference between Node 14 and previous #33117

emilbayes opened this issue Apr 28, 2020 · 4 comments
Labels
node-api Issues and PRs related to the Node-API.

Comments

@emilbayes
Copy link

Hi,

In sodium-native we have a special malloc that allows you to allocated a mmap'ed buffer with special flags that provide extra protection. The signature is sodium_malloc(bytes) where bytes can be up to 0xffffffff. Until Node 14 napi_get_value_uint32 would fail for numbers outside this range (eg. -1 or Number.MAX_SAFE_INTEGER) but for Node 14 our tests now fail with varying errors on different operating system. My guess is that in Node 14, this wraps around, but it is unspecified by the napi docs whether wrap around does happen, so we might have relied on undefined behaviour.

Here is our failing tests:
https://github.com/sodium-friends/sodium-native/blob/91e28b44648164ba8054f22e9a96c826329462bd/test/memory.js#L117-L125

Here is the macro we use to read a uint32_t from a Number:
https://github.com/sodium-friends/sodium-native/blob/91e28b44648164ba8054f22e9a96c826329462bd/macros.h#L157-L161

@himself65 himself65 added the node-api Issues and PRs related to the Node-API. label Apr 28, 2020
@addaleax
Copy link
Member

Until Node 14 napi_get_value_uint32 would fail for numbers outside this range (eg. -1 or Number.MAX_SAFE_INTEGER)

@emilbayes Are you sure that that is the case? The code in N-API hasn’t changed here, and it has always implemented wrap-around behavior for e.g. negative numbers (i.e. it implements ToUint32 from the JS spec).

@emilbayes
Copy link
Author

No, we could have been doing the wrong thing all along 🤔

Just saw that it started breaking on Node 14: https://travis-ci.org/github/sodium-friends/sodium-native/builds/680466769

@gabrielschulhof
Copy link
Contributor

@emilbayes I checked all the way back to v10.x and napi_get_value_uint32() of -1 always returns 0xffffffff.

gabrielschulhof pushed a commit to gabrielschulhof/node that referenced this issue May 6, 2020
Adds a test to ensure that napi_get_value_uint32 returns 0xffffffff for
-1.

Re: nodejs#33117
Signed-off-by: Gabriel Schulhof <[email protected]>
gabrielschulhof pushed a commit that referenced this issue May 7, 2020
Adds a test to ensure that napi_get_value_uint32 returns 0xffffffff for
-1.

Re: #33117
Signed-off-by: Gabriel Schulhof <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Zeyu Yang <[email protected]>
codebytere pushed a commit that referenced this issue May 7, 2020
Adds a test to ensure that napi_get_value_uint32 returns 0xffffffff for
-1.

Re: #33117
Signed-off-by: Gabriel Schulhof <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Zeyu Yang <[email protected]>
@gabrielschulhof
Copy link
Contributor

@emilbayes I have added a test to ensure that the behaviour remains consistent going forward. Please re-open if you believe that the problem lies within Node.js.

codebytere pushed a commit that referenced this issue Jun 7, 2020
Adds a test to ensure that napi_get_value_uint32 returns 0xffffffff for
-1.

Re: #33117
Signed-off-by: Gabriel Schulhof <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Zeyu Yang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
node-api Issues and PRs related to the Node-API.
Projects
None yet
Development

No branches or pull requests

4 participants