erlang / otp

Erlang/OTP
http://erlang.org
Apache License 2.0
11.38k stars 2.95k forks source link

ERL-1358: vm crash when use nif and memset big char arrary #4409

Closed OTP-Maintainer closed 3 years ago

OTP-Maintainer commented 4 years ago

Original reporter: JIRAUSER16702 Affected version: OTP-20.0 Component: Not Specified Migrated from: https://bugs.erlang.org/browse/ERL-1358


when use nif like this, and erlang version upper than 20, vm will crash, erl19 not;
{code:erlang}
static ERL_NIF_TERM print(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
    char buf[1024*1024];
    char buf2[1024*1024];
        memset(buf, 0, sizeof(buf)/sizeof(char));
    memset(buf2, 0, sizeof(buf)/sizeof(char));
    return enif_make_string(env, "1", ERL_NIF_LATIN1);
}
{code}

the core file is too big, so i can only upload code;

it hanppen every times

and if i delete memset of buf2 it works fine
OTP-Maintainer commented 4 years ago

mikael pettersson said:

You're allocating 2MB on your thread's stack.  I don't know how large stacks NIFs get, but 2MB on stack seems excessive.  Just change that to dynamic allocation.
OTP-Maintainer commented 4 years ago

JIRAUSER16702 said:

yeah, change it into staic or malloc could solve this problem. and use large array is bad idea.
i also solve my problem by malloc it. 

maybe it's  necessary to notice that large allocating on stack may clause crash in doc.  just for notice, it's really hard to debug this problem with many lib used.
and only with erl20 and after it happends, erl19 works fine.
OTP-Maintainer commented 4 years ago

JIRAUSER16702 said:

sry, it seems not erlang problem, i know C too less.
large array wolud clause problem is normal in C. not  because of nif used.
OTP-Maintainer commented 4 years ago

JIRAUSER16702 said:

tks for your help. you can just close this. after change  to malloc everything works fine.
OTP-Maintainer commented 4 years ago

JIRAUSER16702 said:

use large is not good idea.
erlang change default staick size.

in OTP 18 and early default stack size is OS dependent.
after OTP19 include OTP19 default suggested stack size is 128 kilowords.
this is set by erl arg '+sss'