Skip to content

Commit

Permalink
compiler: Fix crash in beam_ssa_private_append
Browse files Browse the repository at this point in the history
The beam_ssa_private_append pass can crash, if it, during initial
value tracking, ends up in operations which do not create bit
strings. This can happen as the initial value tracking in
beam_ssa_private_append doesn't consider types. As the decision to
apply the private append transform is using type information, tracking
values into not type-compatible execution paths is harmless.

This patch changes the beam_ssa_private_append pass to simply stop
tracking a value when it is known that the type is not compatible.
  • Loading branch information
frej committed Aug 2, 2023
1 parent 7ce8e02 commit 66ad2bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/compiler/src/beam_ssa_private_append.erl
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ track_value_in_fun([{#b_var{}=V,Element}|Rest], Fun, Work0, Defs,
Element, DefSt0),
track_value_in_fun(ToExplore ++ Rest, Fun, Work0,
Defs, ValuesInFun, DefSt);
{put_tuple,_,_} ->
{put_tuple,_,_} when Element =/= self ->
track_put_tuple(Args, Element, Rest, Fun, V, Work0,
Defs, ValuesInFun, DefSt0);
{put_list,_,_} ->
{put_list,_,_} when Element =/= self ->
track_put_list(Args, Element, Rest, Fun, V, Work0,
Defs, ValuesInFun, DefSt0);
{_,_,_} ->
Expand Down
26 changes: 25 additions & 1 deletion lib/compiler/test/beam_ssa_check_SUITE_data/private_append.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@

id/1,

bs_create_bin_on_literal/0]).
bs_create_bin_on_literal/0,

crash_in_value_tracking/3,
crash_in_value_tracking_inner/3]).

%% Trivial smoke test
transformable0(L) ->
Expand Down Expand Up @@ -1001,3 +1004,24 @@ bs_create_bin_on_literal() ->
end)/bytes
>>/binary
>>.

%% Check that the beam_ssa_private_append pass doesn't crash, if it,
%% during initial value tracking, ends up in operations which do not
%% create bit strings. This can happen as the initial value tracking
%% in beam_ssa_private_append doesn't consider types. As the decision
%% to apply the private append transform is using type information,
%% tracking values into not type-compatible execution paths is
%% harmless.
crash_in_value_tracking_inner(_, 1.0, _) ->
%ssa% (_, _, _) when post_ssa_opt ->
%ssa% _ = bs_init_writable(_).
(<<>>);
crash_in_value_tracking_inner(_V1, _, _) when _V1 ->
_V1.

crash_in_value_tracking(_, _V0, _) ->
%ssa% (_, _, _) when post_ssa_opt ->
%ssa% _ = bs_create_bin(private_append, ...).
((<<((crash_in_value_tracking_inner(
{#{#{ ok => ok || _ := _ <- ok} => ok},
_V0, false, _V0, "Bo"}, _V0, ok)))/bytes>>) =/= ok).

0 comments on commit 66ad2bd

Please sign in to comment.