Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
49 changes: 38 additions & 11 deletions libs/jit/src/jit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1333,10 +1333,6 @@ first_pass(<<?OP_BS_GET_BINARY2, Rest0/binary>>, MMod, MSt0, State0) ->
{MSt5, BSOffsetReg0} = MMod:get_array_element(MSt4, MatchStateRegPtr, 2),
MSt6 =
if
Unit =/= 8 ->
MMod:call_primitive_last(MSt5, ?PRIM_RAISE_ERROR, [
ctx, jit_state, offset, ?UNSUPPORTED_ATOM
]);
FlagsValue =/= 0 ->
MMod:call_primitive_last(MSt5, ?PRIM_RAISE_ERROR, [
ctx, jit_state, offset, ?UNSUPPORTED_ATOM
Expand All @@ -1356,13 +1352,22 @@ first_pass(<<?OP_BS_GET_BINARY2, Rest0/binary>>, MMod, MSt0, State0) ->
MSt11 = MMod:sub(MSt10, SizeReg, BSOffsetReg1),
{MSt11, SizeReg};
is_integer(Size) ->
% SizeReg is binary size
% SizeReg contains binary size in bytes as a term
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is true.
SizeReg is just the size in bytes AFAIK, not a term. We got it from get_array_element(...,1): it's the second word of the boxed binary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I accidentally pushed a half baked thing. But I got suggested by

Indeed, SizeReg contains the size in bytes as a term

#1978 (comment)

% Size is a tagged integer: (N bsl 4) bor 0xF
% SizeBytes is the raw byte count
SizeBytes = Size bsr 4,
MSt11 = MMod:sub(MSt10, SizeReg, SizeBytes),
MSt11 =
if
(Size * Unit) rem 8 =/= 0 ->
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Size is a tagged integer so you don't want to multiply it by Unit.

Since Unit may not be 8, you want to rename SizeBytes into SizeInUnits.

MMod:call_primitive_last(MSt10, ?PRIM_RAISE_ERROR, [
ctx, jit_state, offset, ?UNSUPPORTED_ATOM
]);
true ->
% Equivalent of SizeReg - (((SizeBytes * Unit) div 8) bsl 4)
MMod:sub(MSt10, SizeReg, (SizeBytes * Unit) bsl 1)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since SizeReg is the size in bytes I'm not sure this is correct

end,
MSt12 = cond_jump_to_label({{free, SizeReg}, '<', BSOffsetReg1}, Fail, MMod, MSt11),
{MSt12, SizeBytes};
{MSt12, (SizeBytes * Unit) div 8};
true ->
{MSt11, SizeValReg} = MMod:move_to_native_register(MSt10, Size),
MSt12 = MMod:if_else_block(
Expand All @@ -1374,10 +1379,32 @@ first_pass(<<?OP_BS_GET_BINARY2, Rest0/binary>>, MMod, MSt0, State0) ->
end,
fun(BSt0) ->
{BSt1, SizeValReg} = term_to_int(SizeValReg, 0, MMod, BSt0),
BSt2 = MMod:sub(BSt1, SizeReg, SizeValReg),
BSt3 = cond_jump_to_label({SizeReg, '<', BSOffsetReg1}, Fail, MMod, BSt2),
BSt4 = MMod:move_to_native_register(BSt3, SizeValReg, SizeReg),
MMod:free_native_registers(BSt4, [SizeValReg])
{BSt2, SizeValReg2} =
if
is_integer(SizeValReg) ->
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this cannot happen.

if
(SizeValReg * Unit) rem 8 =/= 0 ->
MMod:call_primitive_last(BSt1, ?PRIM_RAISE_ERROR, [
ctx, jit_state, offset, ?UNSUPPORTED_ATOM
]);
true ->
{BSt1, (SizeValReg * Unit) div 8}
end;
true ->
BBSt1 = MMod:mul(BSt1, SizeValReg, Unit),
BBSt2 = MMod:if_block(
BBSt1, {SizeValReg, '&', 16#7, '!=', 0}, fun(BlockSt) ->
MMod:call_primitive_last(BlockSt, ?PRIM_RAISE_ERROR, [
ctx, jit_state, offset, ?UNSUPPORTED_ATOM
])
end
),
MMod:shift_right(BBSt2, SizeValReg, 3)
end,
BSt3 = MMod:sub(BSt2, SizeReg, SizeValReg2),
BSt4 = cond_jump_to_label({SizeReg, '<', BSOffsetReg1}, Fail, MMod, BSt3),
BSt5 = MMod:move_to_native_register(BSt4, SizeValReg2, SizeReg),
MMod:free_native_registers(BSt5, [SizeValReg])
end
),
{MSt12, SizeReg}
Expand Down
22 changes: 12 additions & 10 deletions src/libAtomVM/opcodesswitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -5570,20 +5570,21 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
term bs_bin = term_get_match_state_binary(src);
avm_int_t bs_offset = term_get_match_state_offset(src);

if (unit != 8) {
TRACE("bs_get_binary2: Unsupported: unit must be 8.\n");
RAISE_ERROR(UNSUPPORTED_ATOM);
}
avm_int_t size_val = 0;
if (term_is_integer(size)) {
size_val = term_to_int(size);
size_val = term_to_int(size) * unit;
if (size_val % 8) {
TRACE("bs_get_binary2: Unsupported: size must be divisible by 8, got: %ld\n", size_val);
RAISE_ERROR(UNSUPPORTED_ATOM);
}
size_val = size_val / 8;
} else if (size == ALL_ATOM) {
size_val = term_binary_size(bs_bin) - bs_offset / 8;
} else {
TRACE("bs_get_binary2: size is neither an integer nor the atom `all`\n");
RAISE_ERROR(BADARG_ATOM);
}
if (bs_offset % unit != 0) {
if (bs_offset % 8 != 0) {
TRACE("bs_get_binary2: Unsupported. Offset on binary read must be aligned on byte boundaries.\n");
RAISE_ERROR(BADARG_ATOM);
}
Expand All @@ -5594,11 +5595,11 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)

TRACE("bs_get_binary2/7, fail=%u src=%p live=%u unit=%u\n", (unsigned) fail, (void *) bs_bin, (unsigned) live, (unsigned) unit);

if ((unsigned int) (bs_offset / unit + size_val) > term_binary_size(bs_bin)) {
if ((unsigned int) (bs_offset / 8 + size_val) > term_binary_size(bs_bin)) {
TRACE("bs_get_binary2: insufficient capacity -- bs_offset = %d, size_val = %d\n", (int) bs_offset, (int) size_val);
JUMP_TO_ADDRESS(mod->labels[fail]);
} else {
term_set_match_state_offset(src, bs_offset + size_val * unit);
term_set_match_state_offset(src, bs_offset + size_val * 8);

TRIM_LIVE_REGS(live);
// there is always room for a MAX_REG + 1 register, used as working register
Expand All @@ -5615,7 +5616,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
#ifdef IMPL_EXECUTE_LOOP
bs_bin = x_regs[live];

term t = term_maybe_create_sub_binary(bs_bin, bs_offset / unit, size_val, &ctx->heap, ctx->global);
term t = term_maybe_create_sub_binary(bs_bin, bs_offset / 8, size_val, &ctx->heap, ctx->global);
WRITE_REGISTER(dreg, t);
}
#endif
Expand Down Expand Up @@ -7109,7 +7110,8 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
JUMP_TO_LABEL(mod, fail);
}
}
segment_size = signed_size_value;
segment_size = size_in_bytes;
segment_unit = 8;
}
break;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/erlang_tests/test_bs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ start() ->

ok = test_bs_variable_size_bitstring(),
ok = test_float(),
ok = test_bs_match_bitstring_modifier(),

0.

Expand Down Expand Up @@ -701,6 +702,24 @@ test_integer_outside_float_limits() ->

create_float_binary(Value, Size) ->
<<Value:Size/float>>.
test_bs_match_bitstring_modifier() ->
ok =
try
bitstring_match(id(<<123, 234, 245>>), id(15)),
case erlang:system_info(machine) of
"BEAM" -> ok;
"ATOM" -> unexpected
end
catch
error:unsupported -> ok
end,

{<<123, 234>>, <<245>>} = bitstring_match(id(<<123, 234, 245>>), id(16)),
ok.

bitstring_match(BS, Size) ->
<<Matched:Size/bitstring, Rest/bits>> = BS,
{Matched, Rest}.

check_x86_64_jt(<<>>) -> ok;
check_x86_64_jt(<<16#e9, _Offset:32/little, Tail/binary>>) -> check_x86_64_jt(Tail);
Expand Down
Loading