Skip to content

Commit

Permalink
Merge pull request #6644 from jhogberg/john/compiler/fix-bs-construct…
Browse files Browse the repository at this point in the history
…-crash/GH-6643

beam_ssa_opt: Don't optimize constants larger than 1<<24 bits
  • Loading branch information
jhogberg authored Jan 10, 2023
2 parents ba2e52d + d8a2328 commit 03a4d3e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/compiler/src/beam_ssa_opt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2100,18 +2100,20 @@ opt_create_bin_arg(Type, Unit, Flags, #b_literal{val=Val}, #b_literal{val=Size})
when is_integer(Size), is_integer(Unit) ->
EffectiveSize = Size * Unit,
if
EffectiveSize > 0 ->
EffectiveSize > (1 bsl 24) ->
%% Don't bother converting really huge segments as they might fail
%% with a `system_limit` exception in runtime. Keeping them as-is
%% ensures that the extended error information will be accurate.
%%
%% We'll also reduce the risk of crashing with an unhelpful "out of
%% memory" error message during compilation.
not_possible;
EffectiveSize > 0, EffectiveSize =< (1 bsl 24) ->
case {Type,opt_create_bin_endian(Flags)} of
{integer,big} when is_integer(Val) ->
if
EffectiveSize < 64 ->
[<<Val:EffectiveSize>>];
EffectiveSize > 1 bsl 24 ->
%% The binary construction could fail with a
%% system_limit. Don't optimize to ensure that
%% the extended error information will be
%% accurate.
not_possible;
true ->
opt_bs_put_split_int(Val, EffectiveSize)
end;
Expand Down
5 changes: 5 additions & 0 deletions lib/compiler/test/bs_construct_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ nasty_literals(Config) when is_list(Config) ->
I = 16#7777FFFF7777FFFF7777FFFF7777FFFF7777FFFF7777FFFF,
id(<<I:260>>),

%% GH-6643: Excessively large literals could cause the compiler to run out
%% of memory.
catch id(<<0:16777216/big-integer-unit:1>>),
catch id(<<0:(16777216*2)/big-integer-unit:1>>),

ok.

-define(COF(Int0),
Expand Down

0 comments on commit 03a4d3e

Please sign in to comment.