Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Do not emit blatantly illformed Core Erlang apply expressions #256

Merged
merged 1 commit into from

3 participants

Anthony Ramine Björn Gustavsson Björn-Egil Dahlberg
Anthony Ramine
nox commented

(fun f/1)() should be compiled to let X = 'f'/1 in apply X () to let the compiler properly generate code that will fail with badarity at runtime.

@UlfNorell

Anthony Ramine nox Do not emit blatantly illformed Core Erlang apply expressions
(fun f/1)() should be compiled to let X = 'f'/1 in apply X () to let the compiler
properly generate code that will fail with badarity at runtime.

Reported-by: Ulf Norell
1b8ad68
Björn Gustavsson
Collaborator

Thanks! Will run it in our daily builds.

Björn Gustavsson
Collaborator

Thinking a little more about this one...

Will this solution always work? What if some Core Erlang optimisation pass would again inline the call of the fun? Would it not be a safer solution to handle this case in v3_kernel or v3_codegen?

Just thinking.

Anthony Ramine
nox commented

@bjorng Core lint specifically reject such code, in my opinion if you handle that elsewhere you should make it valid. I would rather say that such code should never be emitted.

Björn-Egil Dahlberg psyeugenic merged commit 1b8ad68 into from
Björn Gustavsson
Collaborator

I failed to notice that fun_inline_SUITE.erl failed to compile. That means that the inliner needs to be fixed too (or fixing the problem somewhere else, for instance in v3_kernel).

Anthony Ramine
nox commented

Interesting, will you fix it yourself as the suite in the repos fails or can I look at it tonight?

Björn Gustavsson
Collaborator

You can look at it tonight. I will probably be busy with other things today.

For now I have added to our daily builds a branch that reverts your commit.

Anthony Ramine
nox commented

@bjorng Oh right, I didn't run this one because I just ran ct_run directly on the compiler test dir. It's quite bothersome to have to run otp_build tests etc, couldn't these kind of suites include the other one with some macro trickery and be included in the repository?

Anthony Ramine
nox commented

Nevermind, I can just run make -C lib/compiler/test. Will do that now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 1, 2014
  1. Anthony Ramine

    Do not emit blatantly illformed Core Erlang apply expressions

    nox authored
    (fun f/1)() should be compiled to let X = 'f'/1 in apply X () to let the compiler
    properly generate code that will fail with badarity at runtime.
    
    Reported-by: Ulf Norell
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 3 deletions.
  1. +10 −1 lib/compiler/src/v3_core.erl
  2. +6 −2 lib/compiler/test/fun_SUITE.erl
11 lib/compiler/src/v3_core.erl
View
@@ -623,7 +623,7 @@ expr({call,Lc,{atom,Lf,F},As0}, St0) ->
Op = #c_var{anno=lineno_anno(Lf, St1),name={F,length(As1)}},
{#iapply{anno=#a{anno=lineno_anno(Lc, St1)},op=Op,args=As1},Aps,St1};
expr({call,L,FunExp,As0}, St0) ->
- {Fun,Fps,St1} = safe(FunExp, St0),
+ {Fun,Fps,St1} = safe_fun(length(As0), FunExp, St0),
{As1,Aps,St2} = safe_list(As0, St1),
Lanno = lineno_anno(L, St2),
{#iapply{anno=#a{anno=Lanno},op=Fun,args=As1},Fps ++ Aps,St2};
@@ -1408,6 +1408,15 @@ safe(E0, St0) ->
{Se,Sps,St2} = force_safe(E1, St1),
{Se,Eps ++ Sps,St2}.
+safe_fun(A0, E0, St0) ->
+ case safe(E0, St0) of
+ {#c_var{name={_,A1}}=E1,Eps,St1} when A1 =/= A0 ->
+ {V,St2} = new_var(St1),
+ {V,Eps ++ [#iset{var=V,arg=E1}],St2};
+ Result ->
+ Result
+ end.
+
safe_list(Es, St) ->
foldr(fun (E, {Ces,Esp,St0}) ->
{Ce,Ep,St1} = safe(E, St0),
8 lib/compiler/test/fun_SUITE.erl
View
@@ -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,
test1/1,overwritten_fun/1,otp_7202/1,bif_fun/1,
- external/1,eep37/1]).
+ external/1,eep37/1,badarity/1]).
%% Internal export.
-export([call_me/1]).
@@ -32,7 +32,7 @@ suite() -> [{ct_hooks,[ts_install_cth]}].
all() ->
test_lib:recompile(?MODULE),
- [test1,overwritten_fun,otp_7202,bif_fun,external,eep37].
+ [test1,overwritten_fun,otp_7202,bif_fun,external,eep37,badarity].
groups() ->
[].
@@ -206,5 +206,9 @@ eep37(Config) when is_list(Config) ->
50 = UnusedName(8),
ok.
+badarity(Config) when is_list(Config) ->
+ {'EXIT',{{badarity,{_,[]}},_}} = (catch (fun badarity/1)()),
+ ok.
+
id(I) ->
I.
Something went wrong with that request. Please try again.