aboutsummaryrefslogtreecommitdiffstats
path: root/lib/compiler
diff options
context:
space:
mode:
authorBjörn Gustavsson <[email protected]>2016-05-21 08:57:28 +0200
committerBjörn Gustavsson <[email protected]>2016-05-23 08:39:53 +0200
commit57c4432ed543898676ccc646ab6d556c7e6ea79a (patch)
treec420ae873c0fbb68eb2a9e7bb662376a7700a808 /lib/compiler
parent69bb7b21a552c6e7d0615d4ead0345370deec2a0 (diff)
downloadotp-57c4432ed543898676ccc646ab6d556c7e6ea79a.tar.gz
otp-57c4432ed543898676ccc646ab6d556c7e6ea79a.tar.bz2
otp-57c4432ed543898676ccc646ab6d556c7e6ea79a.zip
beam_reorder: Don't confuse beam_validator
Make sure we don't optimize code such as: is_tuple Fail Src test_arity Fail Src Arity get_tuple_element Src Pos Dst is_map Fail Src If we would reorder the instructions like this: is_tuple Fail Src test_arity Fail Src Arity is_map Fail Src get_tuple_element Src Pos Dst beam_validator would complain that the type for Src is a map instead of a tuple. Since the code has problems to begin with, there is no need to do the optimization.
Diffstat (limited to 'lib/compiler')
-rw-r--r--lib/compiler/src/beam_reorder.erl9
-rw-r--r--lib/compiler/test/beam_reorder_SUITE.erl16
2 files changed, 23 insertions, 2 deletions
diff --git a/lib/compiler/src/beam_reorder.erl b/lib/compiler/src/beam_reorder.erl
index f1c0b3ef91..6a7c033ec6 100644
--- a/lib/compiler/src/beam_reorder.erl
+++ b/lib/compiler/src/beam_reorder.erl
@@ -87,6 +87,15 @@ reorder_1([{test,_,_,_}=I,
%% instruction between the test instruction and the select
%% instruction.
reorder_1(Is, D, [S,I|Acc]);
+reorder_1([{test,_,{f,_},[Src|_]}=I|Is], D,
+ [{get_tuple_element,Src,_,_}|_]=Acc) ->
+ %% We want to avoid code that can confuse beam_validator such as:
+ %% is_tuple Fail Src
+ %% test_arity Fail Src Arity
+ %% is_map Fail Src
+ %% get_tuple_element Src Pos Dst
+ %% Therefore, don't reorder the instructions in such cases.
+ reorder_1(Is, D, [I|Acc]);
reorder_1([{test,_,{f,L},Ss}=I|Is0], D0,
[{get_tuple_element,_,_,El}=G|Acc0]=Acc) ->
case member(El, Ss) of
diff --git a/lib/compiler/test/beam_reorder_SUITE.erl b/lib/compiler/test/beam_reorder_SUITE.erl
index 4b2262f65b..ff31f2d3bd 100644
--- a/lib/compiler/test/beam_reorder_SUITE.erl
+++ b/lib/compiler/test/beam_reorder_SUITE.erl
@@ -21,7 +21,7 @@
-export([all/0,suite/0,groups/0,init_per_suite/1,end_per_suite/1,
init_per_group/2,end_per_group/2,
- alloc/1]).
+ alloc/1,confused_beam_validator/1]).
suite() -> [{ct_hooks,[ts_install_cth]}].
@@ -31,7 +31,8 @@ all() ->
groups() ->
[{p,[parallel],
- [alloc
+ [alloc,
+ confused_beam_validator
]}].
init_per_suite(Config) ->
@@ -65,5 +66,16 @@ alloc_b(_U1, _U2, R) ->
_ = id(0),
Res.
+confused_beam_validator(_Config) ->
+ {'EXIT',{{badmap,{any}},_}} = (catch efficient({any})),
+ ok.
+
+efficient({Var}=God) ->
+ id(God#{}),
+ catch
+ receive _ ->
+ Var
+ end.
+
id(I) ->
I.