Skip to content

compiler: Eliminate crash in the beam_ssa_bsm pass #6417

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

Merged
merged 1 commit into from
Nov 3, 2022
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
25 changes: 21 additions & 4 deletions lib/compiler/src/beam_ssa_bsm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,27 @@ combine_matches(#b_function{bs=Blocks0,cnt=Counter0}=F, ModInfo) ->
%% so we can reuse the RPO computed for Blocks0.
Blocks2 = beam_ssa:rename_vars(State#cm.renames, RPO, Blocks1),

{Blocks, Counter} = alias_matched_binaries(Blocks2, Counter0,
State#cm.match_aliases),

F#b_function{ bs=beam_ssa:trim_unreachable(Blocks),
%% Replacing variables with the atom `true` can cause
%% branches to phi nodes to be omitted, with the phi nodes
%% still referencing the unreachable blocks. Therefore,
%% trim now to update the phi nodes.
Blocks3 = beam_ssa:trim_unreachable(Blocks2),

Aliases = State#cm.match_aliases,
{Blocks4, Counter} = alias_matched_binaries(Blocks3, Counter0,
Aliases),
Blocks = if
map_size(Aliases) =:= 0 ->
%% No need to trim because there were no aliases.
Blocks4;
true ->
%% Play it safe. It is unclear whether
%% the call to alias_matched_binaries/3
%% could ever make any blocks
%% unreachable.
beam_ssa:trim_unreachable(Blocks4)
end,
F#b_function{ bs=Blocks,
cnt=Counter };
false ->
F
Expand Down
31 changes: 27 additions & 4 deletions lib/compiler/test/bs_match_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
bad_phi_paths/1,many_clauses/1,
combine_empty_segments/1,hangs_forever/1,
bs_saved_position_units/1,empty_matches/1,
trim_bs_start_match_resume/1]).
trim_bs_start_match_resume/1,
gh_6410/1]).

-export([coverage_id/1,coverage_external_ignore/2]).

Expand Down Expand Up @@ -89,7 +90,8 @@ groups() ->
exceptions_after_match_failure,bad_phi_paths,
many_clauses,combine_empty_segments,hangs_forever,
bs_saved_position_units,empty_matches,
trim_bs_start_match_resume]}].
trim_bs_start_match_resume,
gh_6410]}].

init_per_suite(Config) ->
test_lib:recompile(?MODULE),
Expand Down Expand Up @@ -2502,8 +2504,6 @@ trim_bs_start_match_resume_1(<<Context/binary>>) ->
_ = id(Context),
Context.

id(I) -> I.

expand_and_squeeze(Config) when is_list(Config) ->
%% UTF8 literals are expanded and then squeezed into integer16
ensure_squeezed(16, [?Q("<<$á/utf8,_/binary>>"),
Expand Down Expand Up @@ -2641,3 +2641,26 @@ many_clauses(_Config) ->

one_clause(I) ->
?Q(<<"{_@I@,<<L:8,Val:L>>} -> _@I@ + Val">>).

%% GH-6410: Fix crash in beam_ssa_bsm.
gh_6410(_Config) ->
0 = do_gh_6410(<<42>>),
{'EXIT',{{case_clause,<<>>},[_|_]}} = catch do_gh_6410(<<>>),
{'EXIT',{{case_clause,a},[_|_]}} = catch do_gh_6410(a),
{'EXIT',{badarith,[_|_]}} = catch do_gh_6410([]),

ok.

do_gh_6410(<<_>>) ->
0;
do_gh_6410(X) ->
+(case X of
<<_>> ->
X;
[] ->
X
end).

%%% Utilities.
id(I) -> I.