Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when matching out empty binaries #6838

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion erts/emulator/beam/jit/arm/instr_bs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3674,7 +3674,9 @@ void BeamModuleAssembler::emit_extract_binary(const arm::Gp bitdata,
mov_imm(TMP3, num_bytes);
a.rev64(TMP4, bitdata);
a.stp(TMP2, TMP3, arm::Mem(HTOP).post(sizeof(Eterm[2])));
a.str(TMP4, arm::Mem(HTOP).post(sizeof(Eterm[1])));
if (num_bytes != 0) {
a.str(TMP4, arm::Mem(HTOP).post(sizeof(Eterm[1])));
}
flush_var(dst);
}

Expand Down
8 changes: 6 additions & 2 deletions erts/emulator/beam/jit/x86/instr_bs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3781,8 +3781,12 @@ void BeamModuleAssembler::emit_extract_binary(const x86::Gp bitdata,
a.mov(x86::qword_ptr(HTOP, sizeof(Eterm)), imm(num_bytes));
a.mov(RET, bitdata);
a.bswap(RET);
a.mov(x86::qword_ptr(HTOP, 2 * sizeof(Eterm)), RET);
a.add(HTOP, imm(sizeof(Eterm[3])));
if (num_bytes == 0) {
a.add(HTOP, imm(sizeof(Eterm[2])));
} else {
a.mov(x86::qword_ptr(HTOP, 2 * sizeof(Eterm)), RET);
a.add(HTOP, imm(sizeof(Eterm[3])));
}
}

static std::vector<BsmSegment> opt_bsm_segments(
Expand Down
17 changes: 15 additions & 2 deletions erts/emulator/test/bs_match_bin_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
init_per_group/2,end_per_group/2,
byte_split_binary/1,bit_split_binary/1,match_huge_bin/1,
bs_match_string_edge_case/1,contexts/1]).
bs_match_string_edge_case/1,contexts/1,
empty_binary/1]).

-include_lib("common_test/include/ct.hrl").

suite() -> [{ct_hooks,[ts_install_cth]}].

all() ->
[byte_split_binary, bit_split_binary, match_huge_bin,
bs_match_string_edge_case, contexts].
bs_match_string_edge_case, contexts, empty_binary].

groups() ->
[].
Expand Down Expand Up @@ -270,3 +271,15 @@ get_binary_memory_ctx(A, B, C, D, E, F, Bin) ->
end,
{Res,{A,B,C,D,E,F}}.

empty_binary(_Config) ->
_ = do_empty_binary(1_000_000),
ok.

do_empty_binary(0) ->
ok;
do_empty_binary(N) ->
%% The new bs_match instruction would use more heap space
%% than reserved when matching out an empty binary.
<<V1:0/bits, V1:0/bitstring, V2:0/bytes, V2:0/bits>> = <<>>,
[0|do_empty_binary(N-1)].