Skip to content

Commit

Permalink
Merge pull request erlang#7272 from frej/frej/bif_binary_part3-is-ali…
Browse files Browse the repository at this point in the history
…asing

compiler: bif:binary_part/3 should alias its arguments
  • Loading branch information
bjorng authored May 25, 2023
2 parents 87b7241 + 1dd55c4 commit 646be91
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/compiler/src/beam_ssa_alias.erl
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,11 @@ aa_bif(Dst, tl, Args, SS, AAS) ->
%% TODO: Ignored for now, as we don't track what's inside maps.
%% aa_bif(_Dst, map_get, _Args, SS, _AAS) ->
%% SS;
aa_bif(Dst, binary_part, Args, SS, _AAS) ->
%% bif:binary_part/{2,3} is the only guard bif which could lead to
%% aliasing, it extracts a sub-binary with a reference to its
%% argument.
aa_set_aliased([Dst|Args], SS);
aa_bif(Dst, Bif, Args, SS, _AAS) ->
Arity = length(Args),
case erl_internal:guard_bif(Bif, Arity)
Expand Down
13 changes: 12 additions & 1 deletion lib/compiler/test/beam_ssa_check_SUITE_data/alias.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
stacktrace1/0,
in_cons/0,
make_fun/0,
gh6925/0]).
gh6925/0,
binary_part_aliases/2]).

%% Trivial smoke test
transformable0(L) ->
Expand Down Expand Up @@ -672,3 +673,13 @@ gh6925() ->
A = << <<"x">> || true >>,
B = <<A/binary, "z">>,
{A, B}.

%% Check that bif:binary_part/3 is correctly flagged as an operation
%% which aliases its operands
binary_part_aliases(A, B) ->
%ssa% (_,_) when post_ssa_opt ->
%ssa% X = bif:binary_part(_, _, _),
%ssa% ret(X) {aliased => [X]}.
binary_part(<<>>, A, B).


0 comments on commit 646be91

Please sign in to comment.