From 53f6c32aab2d95d49eb99c33882c2c30c9562db2 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Tue, 9 Feb 2021 23:26:42 -0800 Subject: [PATCH] add test --- .../test_reference_double_free/binding.gyp | 11 ++++++ .../test_reference_double_free/test.js | 10 ++++++ .../test_reference_double_free.c | 36 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 test/js-native-api/test_reference_double_free/binding.gyp create mode 100644 test/js-native-api/test_reference_double_free/test.js create mode 100644 test/js-native-api/test_reference_double_free/test_reference_double_free.c diff --git a/test/js-native-api/test_reference_double_free/binding.gyp b/test/js-native-api/test_reference_double_free/binding.gyp new file mode 100644 index 00000000000000..864846765d0132 --- /dev/null +++ b/test/js-native-api/test_reference_double_free/binding.gyp @@ -0,0 +1,11 @@ +{ + "targets": [ + { + "target_name": "test_reference_double_free", + "sources": [ + "../entry_point.c", + "test_reference_double_free.c" + ] + } + ] +} diff --git a/test/js-native-api/test_reference_double_free/test.js b/test/js-native-api/test_reference_double_free/test.js new file mode 100644 index 00000000000000..db9f1884a0f1c2 --- /dev/null +++ b/test/js-native-api/test_reference_double_free/test.js @@ -0,0 +1,10 @@ +'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`); + +{ const obj = new addon.MyObject(); } diff --git a/test/js-native-api/test_reference_double_free/test_reference_double_free.c b/test/js-native-api/test_reference_double_free/test_reference_double_free.c new file mode 100644 index 00000000000000..29d508e7708e74 --- /dev/null +++ b/test/js-native-api/test_reference_double_free/test_reference_double_free.c @@ -0,0 +1,36 @@ +#include +#include +#include "../common.h" + +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 InstanceDestructor(napi_env env, void* data, void* nothing) {} + +static napi_value New(napi_env env, napi_callback_info info) { + size_t argc = 0; + napi_value js_this; + napi_ref* ref = malloc(sizeof(*ref)); + + NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, NULL, &js_this, NULL)); + NODE_API_CALL(env, napi_wrap(env, js_this, ref, Destructor, NULL, ref)); + NODE_API_CALL(env, napi_reference_ref(env, *ref, NULL)); +// NODE_API_CALL(env, +// napi_set_instance_data(env, ref, InstanceDestructor, NULL)); + return js_this; +} + +EXTERN_C_START +napi_value Init(napi_env env, napi_value exports) { + napi_value myobj_ctor; + NODE_API_CALL(env, + napi_define_class( + env, "MyObject", NAPI_AUTO_LENGTH, New, NULL, 0, NULL, &myobj_ctor)); + NODE_API_CALL(env, + napi_set_named_property(env, exports, "MyObject", myobj_ctor)); + return exports; +} +EXTERN_C_END