Help with NIF upgrade implementation
"Harris, Robert" <[email protected]>
| Newsgroups | gmane.comp.lang.erlang.general |
|---|---|
| Message-ID | <[email protected]> |
I observe a reproducible SEGV of the 21.3.8.4 VM on macos Catalina when repeatedly recompiling a module that has a skeletal NIF with an upgrade callback. The skeleton is based on the examples in the documentation but I am assuming that I have made an error somewhere. I would be grateful for any pointers. The very simple NIF library is attached, together with its makefile and the Erlang module's source. Once built, the failure is seen very quickly: $ erl Erlang/OTP 21 [erts-10.3.5.3] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe] Eshell V10.3.5.3 (abort with ^G) 1> c(rtc), rtc:foo(), rtc:bar(). ok 2> c(rtc), rtc:foo(), rtc:bar(). ok 3> c(rtc), rtc:foo(), rtc:bar(). Segmentation fault: 11 $ The stack is * thread #10, name = '6_scheduler', stop reason = EXC_BAD_ACCESS (code=1, address=0x70) frame #0: 0x000000001e41bc0e beam.smp`nif_resource_dtor at atomic.h:240 237 ETHR_AINT_T__ tmp; 238 239 tmp = incr; -> 240 __asm__ __volatile__( 241 "lock; xadd" ETHR_AINT_SUFFIX__ " %0, %1" /* xadd didn't exist prior to the 486 */ 242 : "=r"(tmp) 243 : "m"(var->counter), "0"(tmp) * thread #10, name = '6_scheduler', stop reason = EXC_BAD_ACCESS (code=1, address=0x70) * frame #0: 0x000000001e41bc0e beam.smp`nif_resource_dtor at atomic.h:240 frame #1: 0x000000001e41bc07 beam.smp`nif_resource_dtor frame #2: 0x000000001e41bc07 beam.smp`nif_resource_dtor frame #3: 0x000000001e41bc07 beam.smp`nif_resource_dtor frame #4: 0x000000001e41bc07 beam.smp`nif_resource_dtor frame #5: 0x000000001e41bbf0 beam.smp`nif_resource_dtor(bin=0x000000001fc800f8) frame #6: 0x000000001e3d390d beam.smp`sweep_off_heap(p=0x0000000020740778, fullsweep=<unavailable>) at erl_binary.h:453 frame #7: 0x000000001e3d73f7 beam.smp`do_minor(p=0x0000000020740778, live_hf_end=<unavailable>, mature=<unavailable>, mature_size=696, new_sz=376, objv=<unavailable>, nobj=3) at erl_gc.c:1674 frame #8: 0x000000001e3d9af4 beam.smp`garbage_collect(p=0x0000000020740778, live_hf_end=0xfffffffffffffff8, need=4, objv=0x000000001f7d4440, nobj=3, fcalls=3047, max_young_gen_usage=0) at erl_gc.c:1417 frame #9: 0x000000001e3dabb5 beam.smp`erts_garbage_collect_nobump(p=0x0000000020740778, need=<unavailable>, objv=<unavailable>, nobj=<unavailable>, fcalls=3047) at erl_gc.c:878 frame #10: 0x000000001e4e8097 beam.smp`process_main(x_reg_array=0x000000001f7d4440, f_reg_array=<unavailable>) at beam_hot.h:104 frame #11: 0x000000001e287916 beam.smp`sched_thread_func(vesdp=0x0000000020bb3400) at erl_process.c:8469 frame #12: 0x000000001e4cbd52 beam.smp`thr_wrapper(vtwd=0x00007ffee197c140) at ethread.c:118 frame #13: 0x00007fff692aa109 libsystem_pthread.dylib`_pthread_start + 148 frame #14: 0x00007fff692a5b8b libsystem_pthread.dylib`thread_start + 15 Robert Harris Confidentiality Notice | This email and any included attachments may be privileged, confidential and/or otherwise protected from disclosure. Access to this email by anyone other than the intended recipient is unauthorized. If you believe you have received this email in error, please contact the sender immediately and delete all copies. If you are not the intended recipient, you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited. Disclaimer The information contained in this communication from the sender is confidential. It is intended solely for use by the recipient and others authorized to receive it. If you are not the recipient, you are hereby notified that any disclosure, copying, distribution or taking action in relation of the contents of this information is strictly prohibited and may be unlawful. This email has been scanned for viruses and malware, and may have been automatically archived by Mimecast, a leader in email security and cyber resilience. Mimecast integrates email defenses with brand protection, security awareness training, web security, compliance and other essential capabilities. Mimecast helps protect large and small organizations from malicious activity, human error and technology failure; and to lead the movement toward building a more resilient world. To find out more, visit our website.
rtc.c
(application/octet-stream, 1.5 KB)
#include <assert.h>
#include <stdlib.h>
#include <erl_nif.h>
static int rtc_load(ErlNifEnv *, void **, ERL_NIF_TERM);
static void rtc_resource_destroy(ErlNifEnv *, void *);
static int rtc_upgrade(ErlNifEnv *, void **, void **, ERL_NIF_TERM);
static ERL_NIF_TERM foo(ErlNifEnv *, int, const ERL_NIF_TERM[]);
static ERL_NIF_TERM bar(ErlNifEnv *, int, const ERL_NIF_TERM[]);
static ErlNifFunc api[] = {
{ "foo", 0, foo, 0 },
{ "bar", 0, bar, 0 },
};
ERL_NIF_INIT(rtc, api, &rtc_load, NULL, &rtc_upgrade, NULL);
static ErlNifResourceType *rtc_resource;
static int
rtc_load(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM nif_context)
{
rtc_resource = enif_open_resource_type(env, NULL, "rtc",
rtc_resource_destroy, ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER,
NULL);
assert(rtc_resource != NULL);
return (0);
}
static void
rtc_resource_destroy(ErlNifEnv *env, void *obj)
{
}
static int
rtc_upgrade(ErlNifEnv *env, void **priv_data, void **old_priv_data,
ERL_NIF_TERM load_info)
{
return (0);
}
static ERL_NIF_TERM
foo(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
void **resource;
ERL_NIF_TERM result;
resource = enif_alloc_resource(rtc_resource, sizeof (void *));
assert(resource != NULL);
result = enif_make_resource(env, resource);
enif_release_resource(resource);
return (result);
}
static ERL_NIF_TERM
bar(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
return (enif_make_atom(env, "ok"));
}
Makefile.rtc
(application/octet-stream, 1.1 KB) - not displayed
rtc.erl
(application/octet-stream, 227 B) - not displayed