Skip to content

Commit

Permalink
Merge branch 'john/erts/fix-bitstring-match-bug/GH-7469/OTP-18672' in…
Browse files Browse the repository at this point in the history
…to maint

* john/erts/fix-bitstring-match-bug/GH-7469/OTP-18672:
  jit: Don't create heap binaries for non-binary bitstrings
  • Loading branch information
jhogberg committed Jul 6, 2023
2 parents 0a7f67b + bcec97c commit 3722c2d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
12 changes: 9 additions & 3 deletions erts/emulator/beam/jit/arm/instr_bs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2320,9 +2320,15 @@ void BeamModuleAssembler::emit_i_bs_create_bin(const ArgLabel &Fail,
estimated_num_bits += seg_size;
}
} else if (seg.unit > 0) {
auto max = std::min(std::get<1>(getClampedRange(seg.size)),
Sint((ERL_ONHEAP_BIN_LIMIT + 1) * 8));
estimated_num_bits += max * seg.unit;
if ((seg.unit % 8) == 0) {
auto max = std::min(std::get<1>(getClampedRange(seg.size)),
Sint((ERL_ONHEAP_BIN_LIMIT + 1) * 8));
estimated_num_bits += max * seg.unit;
} else {
/* May create a non-binary bitstring in some cases, suppress
* creation of heap binary. */
estimated_num_bits += (ERL_ONHEAP_BIN_LIMIT + 1) * 8;
}
} else {
switch (seg.type) {
case am_utf8:
Expand Down
34 changes: 32 additions & 2 deletions erts/emulator/test/bs_construct_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
copy_writable_binary/1, kostis/1, dynamic/1, bs_add/1,
otp_7422/1, zero_width/1, bad_append/1, bs_append_overflow/1,
bs_append_offheap/1,
reductions/1, fp16/1, zero_init/1, error_info/1, little/1]).
reductions/1, fp16/1, zero_init/1, error_info/1, little/1,
heap_binary_unit/1]).

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

Expand All @@ -44,7 +45,7 @@ all() ->
copy_writable_binary, kostis, dynamic, bs_add, otp_7422, zero_width,
bad_append, bs_append_overflow, bs_append_offheap,
reductions, fp16, zero_init,
error_info, little].
error_info, little, heap_binary_unit].

init_per_suite(Config) ->
Config.
Expand Down Expand Up @@ -1665,6 +1666,35 @@ do_little_1(126, I) -> <<I:126/little-integer>>;
do_little_1(127, I) -> <<I:127/little-integer>>;
do_little_1(128, I) -> <<I:128/little-integer>>.

%% GH-7469: The unit of variable-sized segments wasn't checked properly,
%% resulting in the creation of heap binaries for non-binary bitstrings.
heap_binary_unit(_Config) ->
{ok, 14524} = heap_binary_unit_1(id(<<184,188,2,66,172,19,0,3>>)),
ok.

heap_binary_unit_1(<<2:2/integer,Rest:62/bitstring>>) ->
heap_binary_unit_2(<<2:2/integer>>, Rest).

heap_binary_unit_2(Variant, Rest) ->
VariantSize = bit_size(Variant),
ClockHiSize = 8 - VariantSize,
ClockSize = 8 + ClockHiSize,
case Rest of
<<ClockHi:ClockHiSize/bitstring,
ClockLo:8/bitstring,
_:48/bitstring>> ->
case
<<ClockHi:ClockHiSize/bitstring,
ClockLo:8/bitstring>>
of
<<Clock:ClockSize/integer-unsigned>> ->
{ok, Clock};
Bin1 ->
{error1, Bin1}
end;
Bin2 ->
{error2, Bin2}
end.

%%%
%%% Common utilities.
Expand Down

0 comments on commit 3722c2d

Please sign in to comment.