Debian Codegraph @

checker/validate.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Analyze
12
13 (* This module defines validation functions to ensure an imported
14 value (using input_value) has the correct structure. *)
15
16 let rec pr_obj_rec mem o = match o with
17 | Int i ->
18 Format.print_int i
19 | Ptr p ->
20 let v = LargeArray.get mem p in
21 begin match v with
22 | Struct (tag, data) ->
23 let n = Array.length data in
24 Format.print_string ("#"^string_of_int tag^"(");
25 Format.open_hvbox 0;
26 for i = 0 to n-1 do
27 pr_obj_rec mem (Array.get data i);
28 if i<>n-1 then (Format.print_string ","; Format.print_cut())
29 done;
30 Format.close_box();
31 Format.print_string ")"
32 | String s ->
33 Format.print_string ("\""^String.escaped s^"\"")
34 | Int64 _
35 | Float64 _ ->
36 Format.print_string "?"
37 end
38 | Atm tag ->
39 Format.print_string ("#"^string_of_int tag^"()");
40 | Fun addr ->
41 Format.printf "fun@%x" addr
42
43 let pr_obj mem o = pr_obj_rec mem o; Format.print_newline()
44
45 (**************************************************************************)
46 (* Obj low-level validators *)
47
48 type error_frame =
49 | CtxAnnot of string
50 | CtxType of string
51 | CtxField of int
52 | CtxTag of int
53
54 type error_context = error_frame list
55 let mt_ec : error_context = []
56 let (/) (ctx:error_context) s : error_context = s::ctx
57
58 exception ValidObjError of string * error_context * data
59 let fail _mem ctx o s = raise (ValidObjError(s,ctx,o))
60
61 let is_block mem o = match o with
62 | Ptr _ | Atm _ -> true
63 | Fun _ | Int _ -> false
64
65 let is_int _mem o = match o with
66 | Int _ -> true
67 | Fun _ | Ptr _ | Atm _ -> false
68
69 let is_int64 mem o = match o with
70 | Int _ | Fun _ | Atm _ -> false
71 | Ptr p ->
72 match LargeArray.get mem p with
73 | Int64 _ -> true
74 | Float64 _ | Struct _ | String _ -> false
75
76 let is_float64 mem o = match o with
77 | Int _ | Fun _ | Atm _ -> false
78 | Ptr p ->
79 match LargeArray.get mem p with
80 | Float64 _ -> true
81 | Int64 _ | Struct _ | String _ -> false
82
83 let get_int _mem = function
84 | Int i -> i
85 | Fun _ | Ptr _ | Atm _ -> assert false
86
87 let tag mem o = match o with
88 | Atm tag -> tag
89 | Fun _ -> Obj.out_of_heap_tag
90 | Int _ -> Obj.int_tag
91 | Ptr p ->
92 match LargeArray.get mem p with
93 | Struct (tag, _) -> tag
94 | String _ -> Obj.string_tag
95 | Float64 _ -> Obj.double_tag
96 | Int64 _ -> Obj.custom_tag
97
98 let size mem o = match o with
99 | Atm _ -> 0
100 | Fun _ | Int _ -> assert false
101 | Ptr p ->
102 match LargeArray.get mem p with
103 | Struct (tag, blk) -> Array.length blk
104 | String _ | Float64 _ | Int64 _ -> assert false
105
106 let field mem o i = match o with
107 | Atm _ | Fun _ | Int _ -> assert false
108 | Ptr p ->
109 match LargeArray.get mem p with
110 | Struct (tag, blk) -> Array.get blk i
111 | String _ | Float64 _ | Int64 _ -> assert false
112
113 (* Check that object o is a block with tag t *)
114 let val_tag t mem ctx o =
115 if is_block mem o && tag mem o = t then ()
116 else fail mem ctx o ("expected tag "^string_of_int t)
117
118 let val_block mem ctx o =
119 if is_block mem o then
120 (if tag mem o > Obj.no_scan_tag then
121 fail mem ctx o "block: found no scan tag")
122 else fail mem ctx o "expected block obj"
123
124 open Values
125
126 type memory = {
127 mem : obj LargeArray.t;
128 seen : value list LargeArray.t;
129 }
130
131 let rec val_gen v mem ctx o = match o with
132 | Ptr p ->
133 let seen = LargeArray.get mem.seen p in
134 if List.exists (fun v' -> Values.equal v' v) seen then ()
135 else begin
136 (* Setting before we recurse means we allow recursive values.
137 Do we care? *)
138 LargeArray.set mem.seen p (v::seen);
139 val_gen_aux v mem ctx o
140 end
141 | Int _ | Atm _ | Fun _ -> val_gen_aux v mem ctx o
142
143 and val_gen_aux v mem ctx o = match kind v with
144 | Tuple (name,vs) -> val_tuple ~name vs mem ctx o
145 | Sum (name,cc,vv) -> val_sum name cc vv mem ctx o
146 | Array v -> val_array v mem ctx o
147 | List v0 -> val_sum "list" 1 [|[|v0;v|]|] mem ctx o
148 | Opt v -> val_sum "option" 1 [|[|v|]|] mem ctx o
149 | Int -> if not (is_int mem o) then fail mem ctx o "expected an int"
150 | String ->
151 (try val_tag Obj.string_tag mem.mem ctx o
152 with Failure _ -> fail mem ctx o "expected a string")
153 | Any -> ()
154 | Fail s -> fail mem ctx o ("unexpected object " ^ s)
155 | Annot (s,v) -> val_gen v mem (ctx/CtxAnnot s) o
156 | Int64 -> val_int64 mem ctx o
157 | Float64 -> val_float64 mem ctx o
158
159 (* Check that an object is a tuple (or a record). vs is an array of
160 value representation for each field. Its size corresponds to the
161 expected size of the object. *)
162 and val_tuple ?name vs mem ctx o =
163 let ctx = match name with
164 | Some n -> ctx/CtxType n
165 | _ -> ctx
166 in
167 let n = Array.length vs in
168 let val_fld i v =
169 val_gen v mem (ctx/(CtxField i)) (field mem.mem o i) in
170 val_block mem.mem ctx o;
171 if size mem.mem o = n then Array.iteri val_fld vs
172 else
173 fail mem ctx o
174 ("tuple size: found "^string_of_int (size mem.mem o)^
175 ", expected "^string_of_int n)
176
177 (* Check that the object is either a constant constructor of tag < cc,
178 or a constructed variant. each element of vv is an array of
179 value representations of the constructor arguments.
180 The size of vv corresponds to the number of non-constant
181 constructors, and the size of vv.(i) is the expected arity of the
182 i-th non-constant constructor. *)
183 and val_sum name cc vv mem ctx o =
184 let ctx = ctx/CtxType name in
185 if is_block mem o then
186 (val_block mem.mem ctx o;
187 let n = Array.length vv in
188 let i = tag mem.mem o in
189 let ctx' = if n=1 then ctx else ctx/CtxTag i in
190 if i < n then val_tuple vv.(i) mem ctx' o
191 else fail mem ctx' o ("sum: unexpected tag"))
192 else if is_int mem o then
193 let (n:int) = get_int mem o in
194 (if n<0 || n>=cc then
195 fail mem ctx o ("bad constant constructor "^string_of_int n))
196 else fail mem ctx o "not a sum"
197
198 (* Check the o is an array of values satisfying f. *)
199 and val_array v mem ctx o =
200 val_block mem.mem (ctx/CtxType "array") o;
201 for i = 0 to size mem.mem o - 1 do
202 val_gen v mem ctx (field mem.mem o i)
203 done
204
205 and val_int64 mem ctx o =
206 if not (is_int64 mem.mem o) then
207 fail mem ctx o "not a 63-bit unsigned integer"
208
209 and val_float64 mem ctx o =
210 if not (is_float64 mem.mem o) then
211 fail mem ctx o "not a 64-bit float"
212
213 let val_gen v mem ctx o =
214 let mem = {
215 mem;
216 seen = LargeArray.make (LargeArray.length mem) [];
217 }
218 in
219 val_gen v mem ctx o
220
221 let print_frame = function
222 | CtxType t -> t
223 | CtxAnnot t -> t
224 | CtxField i -> Printf.sprintf "fld=%i" i
225 | CtxTag i -> Printf.sprintf "tag=%i" i
226
227 let validate v (o, mem) : unit =
228 try NewProfile.profile "validate" (fun () -> val_gen v mem mt_ec o) ()
229 with ValidObjError(msg,ctx,obj) ->
230 let rctx = List.rev_map print_frame ctx in
231 print_endline ("Context: "^String.concat"/"rctx);
232 pr_obj mem obj;
233 failwith ("Validation failed: "^msg^" (in "^(print_frame (List.hd ctx))^")")