-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
node-api: force env shutdown deferring behavior
The finalizer normally never gets called while a reference is strong. However, during environment shutdown all finalizers must get called. In order to unify the deferring behavior with that of a regular finalization, we must force the reference to be weak when we call its finalizer during environment shutdown. Fixes: #37236 Co-authored-by: Chengzhong Wu <[email protected]> PR-URL: #37303 Backport-PR-URL: #42512 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
1 parent
1193290
commit 171bb66
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "test_reference_double_free", | ||
"sources": [ | ||
"../entry_point.c", | ||
"test_reference_double_free.c" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
// This test makes no assertions. It tests a fix without which it will crash | ||
// with a double free. | ||
|
||
const { buildType } = require('../../common'); | ||
|
||
const addon = require(`./build/${buildType}/test_reference_double_free`); | ||
|
||
{ new addon.MyObject(true); } | ||
{ new addon.MyObject(false); } |
55 changes: 55 additions & 0 deletions
55
test/js-native-api/test_reference_double_free/test_reference_double_free.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <stdlib.h> | ||
#include <js_native_api.h> | ||
#include "../common.h" | ||
|
||
static size_t g_call_count = 0; | ||
|
||
static void Destructor(napi_env env, void* data, void* nothing) { | ||
napi_ref* ref = data; | ||
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, *ref)); | ||
free(ref); | ||
} | ||
|
||
static void NoDeleteDestructor(napi_env env, void* data, void* hint) { | ||
napi_ref* ref = data; | ||
size_t* call_count = hint; | ||
|
||
// This destructor must be called exactly once. | ||
if ((*call_count) > 0) abort(); | ||
*call_count = ((*call_count) + 1); | ||
free(ref); | ||
} | ||
|
||
static napi_value New(napi_env env, napi_callback_info info) { | ||
size_t argc = 1; | ||
napi_value js_this, js_delete; | ||
bool delete; | ||
napi_ref* ref = malloc(sizeof(*ref)); | ||
|
||
NAPI_CALL(env, | ||
napi_get_cb_info(env, info, &argc, &js_delete, &js_this, NULL)); | ||
NAPI_CALL(env, napi_get_value_bool(env, js_delete, &delete)); | ||
|
||
if (delete) { | ||
NAPI_CALL(env, | ||
napi_wrap(env, js_this, ref, Destructor, NULL, ref)); | ||
} else { | ||
NAPI_CALL(env, | ||
napi_wrap(env, js_this, ref, NoDeleteDestructor, &g_call_count, ref)); | ||
} | ||
NAPI_CALL(env, napi_reference_ref(env, *ref, NULL)); | ||
|
||
return js_this; | ||
} | ||
|
||
EXTERN_C_START | ||
napi_value Init(napi_env env, napi_value exports) { | ||
napi_value myobj_ctor; | ||
NAPI_CALL(env, | ||
napi_define_class( | ||
env, "MyObject", NAPI_AUTO_LENGTH, New, NULL, 0, NULL, &myobj_ctor)); | ||
NAPI_CALL(env, | ||
napi_set_named_property(env, exports, "MyObject", myobj_ctor)); | ||
return exports; | ||
} | ||
EXTERN_C_END |