Source file bisect_common.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
type point_definition = {
offset : int;
identifier : int;
}
let try_finally x f h =
let res =
try
f x
with e ->
(try h x with _ -> ());
raise e in
(try h x with _ -> ());
res
let try_in_channel bin x f =
let open_ch = if bin then open_in_bin else open_in in
try_finally (open_ch x) f (close_in_noerr)
let try_out_channel bin x f =
let open_ch = if bin then open_out_bin else open_out in
try_finally (open_ch x) f (close_out_noerr)
exception Invalid_file of string * string
let magic_number_rtd = "BISECTOUT3"
module Writer :
sig
type 'a t
val int : int t
val string : string t
val pair : 'a t -> 'b t -> ('a * 'b) t
val array : 'a t -> 'a array t
val write : 'a t -> 'a -> string
end =
struct
type 'a t = Buffer.t -> 'a -> unit
let w =
Printf.bprintf
let int b i =
w b " %i" i
let string b s =
w b " %i %s" (String.length s) s
let pair left right b (l, r) =
left b l;
right b r
let array element b a =
w b " %i" (Array.length a);
Array.iter (element b) a
let write writer v =
let b = Buffer.create 4096 in
Buffer.add_string b magic_number_rtd;
writer b v;
Buffer.contents b
end
module Reader :
sig
type 'a t
val int : int t
val string : string t
val pair : 'a t -> 'b t -> ('a * 'b) t
val array : 'a t -> 'a array t
val read : 'a t -> filename:string -> 'a
end =
struct
type 'a t = Buffer.t -> in_channel -> 'a
let junk c =
try ignore (input_char c)
with End_of_file -> ()
let int b c =
Buffer.clear b;
let rec loop () =
match input_char c with
| exception End_of_file -> ()
| ' ' -> ()
| c -> Buffer.add_char b c; loop ()
in
loop ();
int_of_string (Buffer.contents b)
let string b c =
let length = int b c in
let s = really_input_string c length in
junk c;
s
let pair left right b c =
let l = left b c in
let r = right b c in
l, r
let array element b c =
let length = int b c in
Array.init length (fun _index -> element b c)
let read reader ~filename =
try_in_channel true filename begin fun c ->
let magic_number_in_file =
try really_input_string c (String.length magic_number_rtd)
with End_of_file ->
raise
(Invalid_file
(filename, "unexpected end of file while reading magic number"))
in
if magic_number_in_file <> magic_number_rtd then
raise (Invalid_file (filename, "bad magic number"));
junk c;
let b = Buffer.create 4096 in
try reader b c
with e ->
raise
(Invalid_file
(filename, "exception reading data: " ^ Printexc.to_string e))
end
end
let table : (string, int array * string) Hashtbl.t Lazy.t =
lazy (Hashtbl.create 17)
let reset_counters () =
Lazy.force table
|> Hashtbl.iter begin fun _ (point_state, _) ->
match Array.length point_state with
| 0 -> ()
| n -> Array.fill point_state 0 (n - 1) 0
end
let runtime_data_to_string () =
let data = Hashtbl.fold (fun k v acc -> (k, v)::acc) (Lazy.force table) [] in
match data with
| [] ->
None
| _ ->
Array.of_list data
|> Writer.(write (array (pair string (pair (array int) string))))
|> fun s -> Some s
let write_runtime_data channel =
let data =
match runtime_data_to_string () with
| Some s -> s
| None -> Writer.(write (array int)) [||]
in
output_string channel data
let prng =
Random.State.make_self_init () [@coverage off]
let random_filename base_name =
Printf.sprintf "%s%09d.coverage"
base_name (abs (Random.State.int prng 1000000000))
let write_points points =
let points_array = Array.of_list points in
Array.sort compare points_array;
Marshal.to_string points_array []
let get_relative_path file =
if Filename.is_relative file then
file
else
let cwd = Sys.getcwd () in
let cwd_end = String.length cwd in
let sep_length = String.length Filename.dir_sep in
let sep_end = sep_length + cwd_end in
try
if String.sub file 0 cwd_end = cwd &&
String.sub file cwd_end sep_length = Filename.dir_sep then
String.sub file sep_end (String.length file - sep_end)
else
file
with Invalid_argument _ ->
file
let read_runtime_data filename =
Reader.(read (array (pair string (pair (array int) string)))) ~filename
|> Array.to_list
|> List.map (fun (file, data) -> get_relative_path file, data)
let read_points s =
let points_array : point_definition array = Marshal.from_string s 0 in
Array.sort compare points_array;
Array.to_list points_array
let register_file file ~point_count ~point_definitions =
let point_state = Array.make point_count 0 in
let table = Lazy.force table in
if not (Hashtbl.mem table file) then
Hashtbl.add table file (point_state, point_definitions);
`Staged (fun point_index ->
let current_count = point_state.(point_index) in
point_state.(point_index) <-
if current_count < max_int then
current_count + 1
else
current_count)
let bisect_file = ref None
let bisect_silent = ref None