Skip to content

Instantly share code, notes, and snippets.

@mocchira
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mocchira/2dc016c2c0630b95821b to your computer and use it in GitHub Desktop.
Save mocchira/2dc016c2c0630b95821b to your computer and use it in GitHub Desktop.
Segfault with Erlang otp #2

Case2 Binary Construction

  • Segfault when constructing a binary like this (<<A/binary, B/binary>>)
  • Code to reproduce this issue
    # Arguments might need to be changed according to executing environments
    erl -noshell -run case2 main 134217728 2147483648 192
  • Env

    name | value :----- |:----- cpu |32core memory |32G uname |Linux * 2.6.32-358.6.2.el6.x86_64 #1 SMP Thu May 16 20:59:36 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux Erlang |15B03,16B02,16B03

(gdb) bt
#0 0x00007f7bc511ec31 in memcpy () from /lib64/libc.so.6
#1 0x00000000004fbcc0 in erts_new_bs_put_binary_all (EBS=0x4a16958,
arg=<value optimized out>, unit=<value optimized out>)
at beam/erl_bits.c:994
#2 0x0000000000530558 in process_main ()
at x86_64-unknown-linux-gnu/opt/smp/beam_cold.h:209
#3 0x0000000000491ca9 in sched_thread_func (vesdp=0x4a16940)
at beam/erl_process.c:5801
#4 0x000000000059b956 in thr_wrapper (vtwd=0x7fff042f7cc0)
at pthread/ethread.c:106
#5 0x00007f7bc5637851 in start_thread () from /lib64/libpthread.so.0
#6 0x00007f7bc517d90d in clone () from /lib64/libc.so.6
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ft=erlang ts=4 sw=4 et
-module(case2).
-export([main/1]).
-define(TEST_FILE_PATH, "test.dat").
main([Arg1,Arg2,Arg3|_T]) ->
FileSize = list_to_integer(Arg1),
ReadBytes = list_to_integer(Arg2),
NumRepeat = list_to_integer(Arg3),
%% generate a test file
Data = crypto:rand_bytes(FileSize),
file:write_file(?TEST_FILE_PATH, Data),
{ok, IoDev} = file:open(?TEST_FILE_PATH, [read, raw, binary]),
try
Ret = random_read(IoDev, FileSize, ReadBytes, NumRepeat, <<>>),
io:format(user, "[info]ret size:~p~n", [size(Ret)])
catch
Class:Reason ->
io:format(user, "[error]class:~p reason:~p stacktrace:~p~n",[Class, Reason, erlang:get_stacktrace()])
after
file:close(IoDev)
end,
ok;
main(_) ->
io:format(user, "Usage: case2 (file size) (read bytes) (# of operations)~n", []).
random_read(_, _, _, 0, Acc) -> Acc;
random_read(IoDev, FileSize, MaxReadBytes, N, Acc) ->
Offset = random:uniform(FileSize - 1),
ReadBytes = random:uniform(round(MaxReadBytes/2)) + round(MaxReadBytes/2),
NewAcc = case file:pread(IoDev, Offset, ReadBytes) of
{ok, Data} ->
<<Acc/binary, Data/binary>>;
eof ->
Acc;
{error, Reason} ->
io:format(user, "[error] file read error:~p~n", [Reason]),
exit(Reason)
end,
NewAcc2 = case N rem 8 of
0 ->
io:format(user, "[info] memory usages:~p~n", [erlang:memory()]),
io:format(user, "[info] acc size:~p~n", [size(NewAcc)]),
<<Clipped:128/binary, _/binary>> = NewAcc,
Clipped;
_ -> NewAcc
end,
random_read(IoDev, FileSize, MaxReadBytes, N - 1, NewAcc2).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment