Last active
December 19, 2015 16:07
-
-
Save bucko909/a841c716ede6d3903a13 to your computer and use it in GitHub Desktop.
erlang nif crash
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
#include "erl_nif.h" | |
typedef struct private_data_TAG { | |
ErlNifResourceType* res_TYPE; | |
} private_data; | |
static int load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info) | |
{ | |
ErlNifResourceFlags flags = (ErlNifResourceFlags)(ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER); | |
private_data *pdata = (private_data*)malloc(sizeof(private_data)); | |
*priv = pdata; | |
pdata->res_TYPE = enif_open_resource_type(env, "nif_breakage", "res", NULL, flags, NULL); | |
return 0; | |
} | |
static int upgrade(ErlNifEnv* env, void** priv, void** old_priv, ERL_NIF_TERM load_info) { | |
*priv = *old_priv; | |
return 0; | |
} | |
static void unload(ErlNifEnv* env, void* priv) { | |
return; | |
} | |
static ERL_NIF_TERM get_res(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { | |
private_data *pdata = (private_data*)enif_priv_data(env); | |
void *res = enif_alloc_resource(pdata->res_TYPE, 8); | |
if (res == NULL) return enif_make_badarg(env); | |
ERL_NIF_TERM t = enif_make_resource(env, res); | |
enif_release_resource(res); | |
return t; | |
} | |
static ErlNifFunc nif_funcs[] = { | |
{"get_res", 0, get_res} | |
}; | |
ERL_NIF_INIT(nif_breakage, nif_funcs, &load, NULL, &upgrade, &unload); |
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
-module(nif_breakage). | |
-compile([export_all]). | |
init() -> | |
ok = erlang:load_nif("./nif_breakage", 0). | |
get_res() -> | |
erlang:error(nif_not_loaded). |
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
-module(nif_breakage_1). | |
-compile([export_all]). | |
wtf() -> | |
code:soft_purge(nif_breakage), | |
code:load_file(nif_breakage), | |
nif_breakage:init(), | |
nif_breakage:get_res(), | |
wtf(). |
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
$ gcc -v | |
Using built-in specs. | |
COLLECT_GCC=gcc | |
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper | |
Target: x86_64-linux-gnu | |
Configured with: ../src/configure -v --with-pkgversion='Debian 5.2.1-23' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --with-arch-32=i586 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu | |
Thread model: posix | |
gcc version 5.2.1 20151028 (Debian 5.2.1-23) | |
$ erl -version | |
Erlang (SMP,ASYNC_THREADS) (BEAM) emulator version 7.1 | |
$ gcc -c -g -Wall -Werror -fpic nif_breakage.c | |
$ gcc -g -shared -o nif_breakage.so nif_breakage.o | |
$ erlc nif_breakage.erl | |
$ erlc nif_breakage_1.erl | |
$ time erl -noshell -s nif_breakage_1 wtf | |
Segmentation fault | |
real 0m0.100s | |
user 0m0.104s | |
sys 0m0.008s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment