-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_k.erl
168 lines (145 loc) · 6.69 KB
/
execute_k.erl
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
-module(execute_k).
-export([execute/5, setup/0, report/0, update_coverage/1]).
-define(KDIR, "erlang-semantics/erl_env").
-define(TRACED_KDIR, "erlang-semantics/erl_env_traced").
-define(K_FILENAME, "./reports/k_coverage.csv").
-define(K_RULE_LOC, k_rule_coverage_map).
% wrapper
execute(TestPath, BaseName, ReportDirectory, Tracing, PID) ->
Res = execute(TestPath, BaseName, ReportDirectory, Tracing),
% io:format("K is ready!: ~p~n", [element(2, Res)]),
PID ! {Res, k_res}.
execute(BaseName, ModuleName, ReportDirectory, Tracing) ->
%% krun --config-var Exp="module:main(.Exps)" module.erl
KDir = if Tracing -> ?TRACED_KDIR;
true -> ?KDIR
end,
%io:format("~s~n~s", [BaseName, ModuleName]),
compile:file(BaseName, ['P']),
exec:shell_exec(io_lib:format("sed -i '1d' ~s", [ModuleName ++ ".P"])),
exec:shell_exec(io_lib:format("sed -Ei \"s/- ([0-9])/-\\1/g\" ~s", [ModuleName ++ ".P"])),
case
exec:shell_exec(
io_lib:format("krun -d ~s --config-var Exp=\"~s:main(.Exps)\" ~s", [
KDir,
ModuleName,
ModuleName ++ ".P"
])
)
of
{_, Output} ->
FileName = ReportDirectory ++ ModuleName ++ ".kresult",
misc:write_to_file(FileName, Output),
{St, Res} = get_k_result_from_string(Output),
if Tracing -> {St, Res, get_k_trace(Output)};
true -> {St, Res}
end;
_ ->
{error, "Krun failed!"}
end.
exceptions() -> ["%badmatch", "%badarity", "%badarith", "%badarg", "%badfun", "%undef"].
find_exception(Str, Elem) ->
case string:find(Str, Elem) of
nomatch -> false;
_ -> true
end.
get_k_result_from_string(Output) ->
try
case string:split(Output, "<k>", leading) of
[_ | [Tail]] -> case string:split(Tail, "</k>", leading) of
[Head | _] ->
begin
ToParse = lists:flatten(string:replace(Head, ".Exps", "", all)),
try
{ok, misc:parse(ToParse)}
catch
_:_ -> case lists:search(fun(Elem) -> find_exception(ToParse, Elem) end, exceptions()) of
{_, Exc} -> {ok, list_to_atom(tl(Exc))};
false -> {error, "Illegal K result format: ~n" ++ Output}
end
end
end;
_ -> {error, "Illegal K result format: ~n" ++ Output}
end;
_ -> {error, "Illegal K result format: ~n" ++ Output}
end
catch
_:_ -> {error, "Illegal K result format: ~n" ++ Output}
end
.
%% ---------------------------------------------------------------------
%% Erlang Semnatics Coverage Measurement
get_k_trace(Output) ->
try
case string:split(Output, "<trace>", leading) of
[_ | [Tail]] -> case string:split(Tail, "</trace>", leading) of
[Head | _] ->
begin
ToParse = "#{" ++ re:replace(
re:replace(string:trim(Head), "[\n]", ",", [global, {return, list}]),
"[|][-][>]",
"=>",
[global, {return, list}]) ++ "}",
misc:parse(ToParse)
end;
_ -> #{}
end;
_ -> #{}
end
catch
_:_ -> io:format("~nInvalid K trace format! The trace for this execution will not be logged!~n"), #{}
end
.
semantic_rules() -> exceptionfree_rules() ++ exceptional_rules().
exceptionfree_rules() -> ["lookup_var", "lookup_fun", "is_atom", "is_boolean", "is_integer", "is_number", "abs",
"hd", "tl", "element", "setelement",
"tuple_size", "list_to_tuple", "tuple_to_list", "length", "matches_and_restore", "matches_fun_and_restore", "matches", "matches_guard",
"matches_fun", "mult", "div", "rem", "plus", "minus", "lt", "le", "lt_list", "ge", "gt", "or", "eq", "neq",
"and", "andalso", "orelse", "not", "app", "diff", "listcomp", "implicit_call", "recursive_call", "anon_call", "anon_call_var",
%"mfa_call", "fa_import_call",
"fa_local_call",
"if", "case", "match", "begin_end"].
exceptional_rules() -> ["div_ex", "rem_ex", "or_ex", "and_ex", "anon_call_badarity", "fa_call_badfun", "andalso_ex", "orelse_ex", "not_ex", "app_ex", "abs_ex",
"diff_ex", "hd_ex", "tl_ex", "element_ex", "setelement_ex", "tuple_size_ex", "list_to_tuple_ex", "tuple_to_list_ex", "length_ex"]. % "fa_call_badarity", "fa_call_undef",
setup() ->
%% Initialize with the Coq coverage map, where all rules were used 0 times:
put(?K_RULE_LOC, misc:init_stat_map(semantic_rules())).
update_coverage(Result) ->
case Result of
%% [Erlresult, {Ok, {Coqresult, CoqTrace}} | Rest]
{_ ,_, RuleTrace} -> misc:process_trace(RuleTrace, ?K_RULE_LOC);
_ -> #{}
end.
report() ->
%% Rule coverage map:
RuleCoverage = get(?K_RULE_LOC),
%% used rule percent
UsedRulesNr = maps:size(maps:filter(fun(_, V) -> V > 0 end, RuleCoverage)),
Semantics_rules = semantic_rules(),
misc:hline(),
io:format("K coverage data:~n"),
io:format("Rule coverage: ~p %~n", [(UsedRulesNr / length(Semantics_rules)) * 100]),
ExcFreeRules = exceptionfree_rules(),
UsedExceptionFreeRulesNr = maps:size(maps:filter(fun(K, V) -> lists:member(K, ExcFreeRules) and (V > 0) end, RuleCoverage)),
io:format("Rule coverage without exceptions: ~p %~n", [(UsedExceptionFreeRulesNr / length(ExcFreeRules)) * 100]),
misc:report_coverage_to_csv(RuleCoverage, ?K_FILENAME),
misc:hline()
.
%% ---------------------------------------------------------------------
%% WARNING: xmerl cannot parse every k framework result!!
get_k_result_from_xml(FileName) ->
XMLResult = xmerl_scan:file(FileName),
try
%% get the XML result cell:
case element(9 ,element(1, XMLResult)) of
[_, KCell | _] ->
[KTuple] = element(9, KCell),
ToParse = element(5, KTuple),
{ok, misc:parse(ToParse)};
_ ->
{error, "Illegal K result format!"}
end
catch
_:_ -> {error, "Illegal K result format!"}
end
.