Source file wp_multicore.ml
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
315
316
open Unix
open Cil_types
open Wp
let number_of_processes = ref 1
(** Process pool: a pool of unix processes that compute label statuses in parallel **)
let get_kf_name kf =
match kf.fundec with
| Definition (f,_) -> f.svar.vname
| Declaration (_,f,_,_) -> f.vname
let wait_for_children () =
let output_file = (string_of_int (Unix.getpid()))^"wfc.tmp" in
ignore(Sys.command ("pgrep -c -P "^(string_of_int(Unix.getpid()))^" --full \"[alt\\-ergo] <defunct>\" > "^output_file));
if Sys.file_exists output_file then begin
let ic = open_in output_file in
let line = input_line ic in
close_in ic;
Sys.remove output_file;
let nb_children = int_of_string (String.trim line) in
for i = 1 to nb_children do ignore(i); ignore(Unix.wait()) done
end
let get_resident_memory_usage pid =
let output_file = (string_of_int (Unix.getpid()))^"mu.tmp" in
ignore(Sys.command ("ps -p "^(string_of_int pid)^" --no-headers -o rss > "^output_file));
let ic = open_in output_file in
let line = input_line ic in
close_in ic;
Sys.remove output_file;
int_of_string (String.trim line)
let rec n_first_a l n a =
match (l,n) with
| (l,0) -> (a,l)
| (h::t,n) ->
let new_a = h::a in
let new_n = n-1 in
(n_first_a t new_n new_a)
| ([],_) -> (a,[])
let n_first l n = n_first_a l n []
let rec split_list_size_a l n a =
match n_first l n with
| (f,h::t) ->
let new_l = h::t in
let new_a = f::a in
(split_list_size_a new_l n new_a)
| (f,[]) -> f::a
let split_list_size l =
let n = Options.WPMaxNbLabelPerCall.get () in
if n > 0 then
split_list_size_a l n []
else [l]
let no_output () =
let devnull = descr_of_out_channel (open_out "/dev/null") in
dup2 devnull stdout
let wp_call kf labels channel =
let ips = Instrument_multicore.add_annotations_get_ips labels in
if not (Options.WPQedOptim.get ()) then begin
Wp_parameters.Simpl.set false;
Wp_parameters.Let.set false;
Wp_parameters.Prune.set false;
Wp_parameters.Clean.set false
end;
List.iter (fun s -> Wp_parameters.Model.add s) ["typed";"int";"float";"var"];
let flag = ref true in
begin
try
VC.(command (generate_kf kf))
with e ->
flag := false;
Options.feedback "WP was interrupted during check: aborting computation for these labels";
raise e
end;
Hashtbl.iter (fun lblid ip ->
let status =
(match ip with
| None -> "invalid"
| Some ip_v ->
let value_st = List.for_all VC.is_valid (VC.proof ip_v) in
if value_st && !flag then ("valid") else ("invalid")
)
in
let message = ("false"^"\n"^(string_of_int lblid)^"\n"^status^"\n") in
output_string channel message;
flush channel
) ips
let task tasks channel =
List.iter (fun (kf,labels) ->
match fork () with
| 0 ->
Sys.set_signal Sys.sigalrm (Sys.Signal_handle (fun _ -> ignore(Sys.command ("pkill --signal 9 --parent "^(string_of_int (getpid ())))); wait_for_children(); exit 0));
if not (Options.WPShowLogs.get ()) then no_output();
wp_call kf labels channel;
wait_for_children ();
exit 0
| child_pid ->
Options.feedback "Starting worker process with pid %d to process %d labels of function %s." child_pid (List.length labels) (get_kf_name kf);
let timeout = Options.MultiCoreKillTimeout.get() in
let maxmemory = Options.MultiCoreMaxMemory.get () in
let time = ref 0 in
let exited = ref false in
while !time <= timeout && not (!exited) && (get_resident_memory_usage child_pid) < maxmemory do
Unix.sleep 1;
let (pid,_) = Unix.waitpid [Unix.WNOHANG] child_pid in
if pid > 0 then exited := true;
incr time
done;
if !exited then begin
Options.feedback "Worker process with pid %d ended normally" child_pid; ()
end else
begin
Options.feedback "Killing worker process with pid %d" child_pid;
Unix.kill child_pid Sys.sigalrm;
ignore(Unix.waitpid [] child_pid);
List.iter (fun lblid ->
let message = ("false"^"\n"^(string_of_int lblid)^"\n"^"invalid"^"\n") in
output_string channel message;
flush channel
) labels
end;
) tasks;
let message = ("true"^"\n"^(string_of_int (getpid()))^"\n"^"na"^"\n") in
output_string channel message;
flush channel;
close_out channel
let rec initialize_pool (_,checker) schedule = create_children 0 ([],(Hashtbl.create !number_of_processes)) checker schedule
and create_children cnt acc_fd chk sch =
let parameter = (List.hd sch) in
flush_all ();
let (fd_in, fd_out) = pipe () in
match fork () with
| 0 -> close fd_in; task parameter (out_channel_of_descr fd_out); exit 0
| child_pid ->
close fd_out;
Options.feedback "Starting supervisor process with pid %d" child_pid;
let new_cnt = cnt + 1 in
if new_cnt < !number_of_processes then begin
let res = (create_children new_cnt acc_fd chk (List.tl sch)) in
(Hashtbl.add (snd res) fd_in (in_channel_of_descr fd_in));
(fd_in::(fst res),snd res)
end
else begin
(Hashtbl.add (snd acc_fd) fd_in (in_channel_of_descr fd_in));
(fd_in::(fst acc_fd),snd acc_fd)
end
let rec process_results fds chs force (checker_name,_) data = get_results fds chs 0 force checker_name data
and get_results fds chs cnt force checker_name data = match (Unix.select fds [] [] (-1.0)) with
| fdl, [], [] -> let process acc fd = (
let channel = Hashtbl.find chs fd in
let eof = (input_line channel = "true") in
let lblid = int_of_string (input_line channel) in
let status = if (input_line channel = "valid") then Data_labels.Uncoverable else Data_labels.Unknown in
ignore(Sys.command "pkill --signal 9 --parent 1 --full alt-ergo");
if eof then
begin
close_in channel;
Options.feedback "Exiting supervisor process with pid %d" lblid;
ignore(Unix.wait());
fd::acc
end
else begin
Data_labels.update data ~force ~status ~emitter:checker_name lblid;
acc
end
) in
let killed_children = (List.fold_left process [] fdl) in
let nb_killed_children = cnt + (List.length killed_children) in
if nb_killed_children < !number_of_processes then get_results (List.filter (fun fd -> not (List.mem fd killed_children)) fds) chs nb_killed_children force checker_name data else ()
| _, _, _ -> failwith "Unexpected pipe interaction with main process."
let process_in_parallel force checker schedule data =
let (fds,chs) = initialize_pool checker schedule in
process_results fds chs force checker data
let wp_property_checker =
let check ~label:_ _ = () in
("WP", check)
(** Task a-priori scheduling: given a set of tasks, provide an a-priori distribution between a set of processes **)
let rec head_nary l n = split_head [] l n
and split_head h t n =
if n>0 then begin
match t with
| hd::tl -> split_head (h @ [hd]) tl (n - 1)
| [] -> (h,t)
end
else (h,t)
let rec split_in_n l n =
match l with
| _::_ -> let first = head_nary l n in (fst first)::(split_in_n (snd first) n)
| [] -> []
let compare_tasks (_,labsl) (_,labsr) =
let nbl = List.length labsl in
let nbr = List.length labsr in
nbr-nbl
let rec invert_odds l =
match l with
| [] -> l
| _::[] -> l
| f::s::t -> f::(List.rev s)::(invert_odds t)
let compose_with_i_th the_split i =
List.fold_left (fun acc lt ->
if (List.length lt) <= i then
acc else
(List.nth lt i)::acc
) [] the_split
let rec create_up_to_n the_split n acc =
if n <= 0 then
acc else
(create_up_to_n the_split (n-1) ((compose_with_i_th the_split (n-1))::acc))
let split tasks nb_processes =
let the_split = invert_odds (split_in_n tasks nb_processes) in
create_up_to_n the_split nb_processes []
let split_tasks_for_fair_sharing_of_nb_lablels tasks nb_process =
let sorted_tasks = List.sort compare_tasks tasks in
split sorted_tasks nb_process
let generate_tasks (kfs,kf_lb) = let tasks = ref [] in
Instrument_multicore.KFSet.iter (fun kf ->
List.iter (fun el ->
tasks := (kf,el)::!tasks
) (split_list_size (Hashtbl.find_all kf_lb (get_kf_name kf)))
) kfs;
!tasks
let compute_multicore ?(force=false) data =
let checker = wp_property_checker in
if Options.Rte.get () then RteGen.Api.compute ();
Instrument_multicore.at := true;
ignore(Instrument_multicore.create_project ());
let tasks = generate_tasks (Instrument_multicore.get_kf_lbl_partition ()) in
Options.feedback "start weakest-precondition-based detection";
let nbTasks = (List.length tasks) in
if nbTasks > 0 then begin
let nb_processes = min nbTasks (Options.Multicore.get()) in
number_of_processes := nb_processes;
let schedule = split_tasks_for_fair_sharing_of_nb_lablels tasks nb_processes in
process_in_parallel force checker schedule data end;
Options.feedback "WP-based detection done"
let compute_multicoreAT ?(force=false) data =
let checker = wp_property_checker in
if Options.Rte.get () then RteGen.Api.compute ();
Instrument_multicore.at := false;
ignore(Instrument_multicore.create_project ());
let tasks = generate_tasks (Instrument_multicore.get_kf_lbl_partition ()) in
Options.feedback "start weakest-precondition-based detection (for always true labels)";
let nbTasks = (List.length tasks) in
if nbTasks > 0 then begin
let nb_processes = min nbTasks (Options.Multicore.get()) in
number_of_processes := nb_processes;
let schedule = split_tasks_for_fair_sharing_of_nb_lablels tasks nb_processes in
process_in_parallel force checker schedule data end;
Options.feedback "WP-based detection (for always true labels) done"