aboutsummaryrefslogtreecommitdiffstats
path: root/lib/debugger/test/int_eval_SUITE.erl
blob: 324a44bad833ba79f3b349c9d730257d3f8f283f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1999-2018. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

%%
-module(int_eval_SUITE).

%% Purpose: Deeper test of the evaluator.

-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
	 init_per_group/2,end_per_group/2,
	 init_per_testcase/2, end_per_testcase/2,
	 bifs_outside_erlang/1, spawning/1, applying/1,
	 catch_and_throw/1, external_call/1, test_module_info/1,
	 apply_interpreted_fun/1, apply_uninterpreted_fun/1,
	 interpreted_exit/1, otp_8310/1, stacktrace/1, maps/1,
	 call_inside_binary/1]).

%% Helpers.
-export([applier/3]).

-define(IM, my_int_eval_module).

-include_lib("common_test/include/ct.hrl").

suite() -> [{ct_hooks,[ts_install_cth]},
	    {timetrap,{minutes,1}}].

all() -> 
    [bifs_outside_erlang, spawning, applying,
     catch_and_throw, external_call, test_module_info,
     apply_interpreted_fun, apply_uninterpreted_fun,
     interpreted_exit, otp_8310, stacktrace, maps,
     call_inside_binary].

groups() -> 
    [].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


init_per_testcase(_Case, Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {module,?IM} = int:i(filename:join(DataDir, ?IM)),
    ok = io:format("Interpreted modules: ~p",[int:interpreted()]),
    Config.

end_per_testcase(_Case, _Config) ->
    ok = io:format("Interpreted modules: ~p", [int:interpreted()]),
    ok.

%% Test that BIFs outside the erlang module are correctly evaluated.
bifs_outside_erlang(Config) when is_list(Config) ->
    Fun = fun() ->
		  Id = ?IM:ets_new(),
		  Self = self(),
		  ok = io:format("Self: ~p", [Self]),
		  Info = ets:info(Id),
		  Self = proplists:get_value(owner, Info),
		  ?IM:ets_delete(Id),
		  ok
	  end,
    ok = spawn_eval(Fun),
    ok.

%% Try evalutate spawn_link/3.
spawning(Config) when is_list(Config) ->
    ok = spawn_eval(fun() -> ?IM:spawn_test() end).

%% Try various sorts of applies.
applying(Config) when is_list(Config) ->
    Fun = fun({number,X}, {number,Y}) -> X+Y end,
    ok = spawn_eval(fun() -> ?IM:apply_test(Fun) end).

%% Test catch and throw/1.
catch_and_throw(Config) when is_list(Config) ->
	{a,ball} = spawn_eval(fun() -> ok = ?IM:catch_a_ball(),
				       catch ?IM:throw_a_ball() end),

	%% Throw and catch without any extra outer catch.

	process_flag(trap_exit, true),
	Pid1 = spawn_link(fun() -> exit(?IM:catch_a_ball()) end),
	receive
	    {'EXIT',Pid1,ok} -> ok;
	    {'EXIT',Pid1,Bad1} -> ct:fail({bad_message,Bad1})
	after 5000 ->
		ct:fail(timeout)
	end,


	%% Throw without catch.

	Pid2 = spawn_link(fun() -> ?IM:throw_a_ball() end),
	receive
	    {'EXIT',Pid2,{{nocatch,{a,ball}},[_|_]}} -> ok;
	    {'EXIT',Pid2,Bad2} -> ct:fail({bad_message,Bad2})
	after 5000 ->
		ct:fail(timeout)
	end,

	ok = ?IM:more_catch(fun(_) -> ?IM:exit_me() end),
	ok = ?IM:more_catch(fun(_) -> exit({unint, exit}) end),
	{a, ball} = ?IM:more_catch(fun(_) -> ?IM:throw_a_ball() end),
	{b, ball} = ?IM:more_catch(fun(_) -> throw({b,ball}) end),

	ExitInt = {'EXIT',{int,exit}},
	ExitU   = {'EXIT',{unint,exit}},

	ExitInt = (catch ?IM:more_nocatch(fun(_) -> ?IM:exit_me() end)),
	ExitU   = (catch ?IM:more_nocatch(fun(_) -> exit({unint, exit}) end)),
	{a, ball} = (catch {error, ?IM:more_nocatch(fun(_) -> ?IM:throw_a_ball() end)}),
	{b, ball} = (catch {error, ?IM:more_nocatch(fun(_) -> throw({b,ball}) end)}),
	ok.

%% Test external calls.
external_call(Config) when is_list(Config) ->
    ok = spawn_eval(fun() -> ?IM:external_call_test({some,stupid,data}) end).

%% Test the module_info/0,1 functions.
test_module_info(Config) when is_list(Config) ->
    ModInfo = ?IM:module_info(),
    {value,{exports,Exp}} = lists:keysearch(exports, 1, ModInfo),
    {value,{attributes,Attr}} = lists:keysearch(attributes, 1, ModInfo),
    Exp = ?IM:module_info(exports),
    Attr = ?IM:module_info(attributes),
    {value,{stupid_attribute,[{a,b}]}} =
	lists:keysearch(stupid_attribute, 1, Attr),

    %% Check exports using a list comprehension in the module itself.

    ok = ?IM:check_exports(Exp),

    %% Call module_info/0,1 from the module itself.

    ok = ?IM:check_module_info(ModInfo, Exp),

    ok.

%% Apply a fun defined in interpreted code.
apply_interpreted_fun(Config) when is_list(Config) ->

    %% Called from uninterpreted code
    F1 = spawn_eval(fun() -> ?IM:give_me_a_fun_0() end),
    perfectly_alright = spawn_eval(fun() -> F1() end),
    ATerm = {a,term},
    F2 = spawn_eval(fun() -> ?IM:give_me_a_fun_0(ATerm) end),
    {ok,ATerm} = spawn_eval(fun() -> F2() end),

    %% Called from uninterpreted code, badarity
    {'EXIT',{{badarity,{F1,[snape]}},[{?MODULE,_,_,_}|_]}} =
	spawn_eval(fun() -> F1(snape) end),

    %% Called from uninterpreted code, error in fun
    F3 = spawn_eval(fun() -> ?IM:give_me_a_bad_fun() end),
    {'EXIT',{snape,[{?IM,_FunName,_,_}|_]}} =
	spawn_eval(fun() -> F3(snape) end),

    %% Called from within interpreted code
    perfectly_alright = spawn_eval(fun() -> ?IM:do_apply(F1) end),

    %% Called from within interpreted code, badarity
    {'EXIT',{{badarity,{F1,[snape]}},[{?IM,do_apply,_,_}|_]}} =
	spawn_eval(fun() -> ?IM:do_apply(F1, snape) end),

    %% Called from within interpreted code, error in fun
    {'EXIT',{snape,[{?IM,_FunName,_,_}|_]}} =
	spawn_eval(fun() -> ?IM:do_apply(F3, snape) end),

    %% Try some more complex funs.
    F4 = ?IM:give_me_a_fun_1(14, 42),
    {false,yes,yeah,false} =
	F4({{1,nope},{14,yes},{42,yeah},{100,forget_it}}),
    [this_is_ok,me_too] =
	F4([{-24,no_way},{15,this_is_ok},{1333,forget_me},{37,me_too}]),

    %% OTP-5837
    %% Try fun with guard containing variable bound in environment
    [yes,no,no,no] = ?IM:otp_5837(1),

    ok.

%% Apply a fun defined outside interpreted code.
apply_uninterpreted_fun(Config) when is_list(Config) ->

    F1 = fun(snape) ->
		 erlang:error(snape);
	    (_Arg) ->
		 perfectly_alright
	 end,

    %% Ok
    perfectly_alright =
	spawn_eval(fun() -> ?IM:do_apply(F1, any_arg) end),

    %% Badarity (evaluated in dbg_debugged, which calls erlang:apply/2)
    {'EXIT',{{badarity,{F1,[]}},[{erlang,apply,_,_}|_]}} =
	spawn_eval(fun() -> ?IM:do_apply(F1) end),

    %% Error in fun
    {'EXIT',{snape,[{?MODULE,_FunName,_,_}|_]}} =
	spawn_eval(fun() -> ?IM:do_apply(F1, snape) end),

    ok.

%%
%% Try executing an interpreted exit/1 call.
%%

interpreted_exit(Config) when is_list(Config) ->
    process_flag(trap_exit, true),
    Reason = make_ref(),
    Pid = spawn_link(fun() -> ?IM:please_call_exit(Reason) end),
    receive
	{'EXIT',Pid,Reason} ->
	    ok;
	{'EXIT',Pid,BadReason} ->
	    ct:fail({bad_message,BadReason})
    after 10000 ->
	    ct:fail(timeout)
    end,
    ok.

%% OTP-8310. Bugfixes lc/bc and andalso/orelse.
otp_8310(Config) when is_list(Config) ->
    ok = ?IM:otp_8310(),
    ok.

applier(M, F, A) ->
    Res = apply(M, F, A),
    io:format("~p:~p(~p) => ~p\n", [M,F,A,Res]),
    Res.

stacktrace(Config) when is_list(Config) ->
    {done,Stk} = do_eval(Config, stacktrace),
    13 = length(Stk),
    OldStackTraceFlag = int:stack_trace(),
    int:stack_trace(no_tail),
    try
	Res = spawn_eval(fun() -> stacktrace:stacktrace() end),
	io:format("\nInterpreted (no_tail):\n~p", [Res]),
	{done,Stk} = Res
    after
	int:stack_trace(OldStackTraceFlag)
    end,
    ok.

maps(Config) when is_list(Config) ->
    Fun = fun () -> ?IM:empty_map_update([camembert]) end,
    {'EXIT',{{badmap,[camembert]},_}} = spawn_eval(Fun),
    [#{hello := 0, price := 0}] = spawn_eval(fun () -> ?IM:update_in_fun() end),
    ok.

call_inside_binary(Config) when is_list(Config) ->
    <<"1">> = ?IM:call_inside_binary(fun erlang:integer_to_binary/1),
    ok.

do_eval(Config, Mod) ->
    DataDir = proplists:get_value(data_dir, Config),
    ok = file:set_cwd(DataDir),

    %% Turn off type-based optimizations across function calls, as it
    %% would turn many body-recursive calls into tail-recursive calls,
    %% which would change the stacktrace.
    {ok,Mod} = compile:file(Mod, [no_module_opt,report,debug_info]),
    {module,Mod} = code:load_file(Mod),
    CompiledRes = Mod:Mod(),
    ok = io:format("Compiled:\n~p", [CompiledRes]),
    io:nl(),

    {module,Mod} = int:i(Mod),
    IntRes = Mod:Mod(),
    ok = io:format("Interpreted:\n~p", [IntRes]),

    CompiledRes = IntRes.

%%
%% Evaluate in another process, to prevent the test_case process to become
%% interpreted.
%%

spawn_eval(Fun) ->
    Self = self(),
    spawn_link(fun() -> Self ! (catch Fun()) end),
    receive
	Result ->
	    Result
    end.