n-api: free instance data as reference

Instance data associated with a `napi_env` is no longer stored on the
env itself but is instead rendered as a reference. Since
`v8impl::Reference` is tied to a JS object, this modification factors
out the `v8impl::Reference` refcounting and the deletion process into
a base class for `v8impl::Reference`, called `v8impl::RefBase`. The
instance data is then stored as a `v8impl::RefBase`, along with other
references, preventing a segfault that arises from the fact that, up
until now, upon `napi_env` destruction, the instance data was freed
after all references had already been forcefully freed. If the addon
freed a reference during the `napi_set_instance_data` finalizer
callback, such a reference had already been freed during environment
teardown, causing a double free.

Re: https://github.com/nodejs/node-addon-api/pull/663
PR-URL: https://github.com/nodejs/node/pull/31638
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: David Carlier <devnexen@gmail.com>
This commit is contained in:
Gabriel Schulhof authored and Shelley Vohr committed 2020-02-17 12:06:14 -08:00
1 parent 6874deef28
commit ae2141effc
8 files changed
+231 -110

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#define NAPI_EXPERIMENTAL
#include <node_api.h>
static void addon_free(napi_env env, void* data, void* hint) {
napi_ref* ref = data;
napi_delete_reference(env, *ref);
free(ref);
fprintf(stderr, "addon_free");
}
napi_value addon_new(napi_env env, napi_value exports, bool ref_first) {
napi_ref* ref = malloc(sizeof(*ref));
if (ref_first) {
napi_create_reference(env, exports, 1, ref);
napi_set_instance_data(env, ref, addon_free, NULL);
} else {
napi_set_instance_data(env, ref, addon_free, NULL);
napi_create_reference(env, exports, 1, ref);
}
return exports;
}
+15 -1
View File
@@ -5,6 +5,20 @@
"sources": [
"test_instance_data.c"
]
}
},
{
"target_name": "test_set_then_ref",
"sources": [
"addon.c",
"test_set_then_ref.c",
]
},
{
"target_name": "test_ref_then_set",
"sources": [
"addon.c",
"test_ref_then_set.c",
]
},
]
}
+20
View File
@@ -24,12 +24,32 @@ if (module.parent) {
} else {
// When launched as a script, run tests in either a child process or in a
// worker thread.
const assert = require('assert');
const requireAs = require('../../common/require-as');
const runOptions = { stdio: ['inherit', 'pipe', 'inherit'] };
const { spawnSync } = require('child_process');
// Run tests in a child process.
requireAs(__filename, ['--expose-gc'], runOptions, 'child');
// Run tests in a worker thread in a child process.
requireAs(__filename, ['--expose-gc'], runOptions, 'worker');
function testProcessExit(addonName) {
// Make sure that process exit is clean when the instance data has
// references to JS objects.
const path = require
.resolve(`./build/${common.buildType}/${addonName}`)
// Replace any backslashes with double backslashes because they'll be re-
// interpreted back to single backslashes in the command line argument
// to the child process. Windows needs this.
.replace(/\\/g, '\\\\');
const child = spawnSync(process.execPath, ['-e', `require('${path}');`]);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.status, 0);
assert.strictEqual(child.stderr.toString(), 'addon_free');
}
testProcessExit('test_ref_then_set');
testProcessExit('test_set_then_ref');
}
@@ -0,0 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#define NAPI_EXPERIMENTAL
#include <node_api.h>
napi_value addon_new(napi_env env, napi_value exports, bool ref_first);
// static napi_value
NAPI_MODULE_INIT(/*napi_env env, napi_value exports */) {
return addon_new(env, exports, true);
}
@@ -0,0 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#define NAPI_EXPERIMENTAL
#include <node_api.h>
napi_value addon_new(napi_env env, napi_value exports, bool ref_first);
// static napi_value
NAPI_MODULE_INIT(/*napi_env env, napi_value exports */) {
return addon_new(env, exports, false);
}