| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open Util |
| 13 |
open Names |
| 14 |
|
| 15 |
let chk_pp = Feedback.msg_notice |
| 16 |
|
| 17 |
let pr_dirpath dp = str (DirPath.to_string dp) |
| 18 |
let default_root_prefix = DirPath.empty |
| 19 |
let split_dirpath d = |
| 20 |
let l = DirPath.repr d in (DirPath.make (List.tl l), List.hd l) |
| 21 |
let extend_dirpath p id = DirPath.make (id :: DirPath.repr p) |
| 22 |
|
| 23 |
type section_path = { |
| 24 |
dirpath : string list ; |
| 25 |
basename : string } |
| 26 |
|
| 27 |
type object_file = |
| 28 |
| PhysicalFile of CUnix.physical_path |
| 29 |
| LogicalFile of section_path |
| 30 |
|
| 31 |
let dir_of_path p = |
| 32 |
DirPath.make (List.map Id.of_string p.dirpath) |
| 33 |
let path_of_dirpath dir = |
| 34 |
match DirPath.repr dir with |
| 35 |
[] -> failwith "path_of_dirpath" |
| 36 |
| l::dir -> |
| 37 |
{dirpath=List.map Id.to_string dir;basename=Id.to_string l} |
| 38 |
let pr_dirlist dp = |
| 39 |
prlist_with_sep (fun _ -> str".") str (List.rev dp) |
| 40 |
let pr_path sp = |
| 41 |
match sp.dirpath with |
| 42 |
[] -> str sp.basename |
| 43 |
| sl -> pr_dirlist sl ++ str"." ++ str sp.basename |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
type compilation_unit_name = DirPath.t |
| 51 |
|
| 52 |
type seg_proofs = Opaqueproof.opaque_proofterm option array |
| 53 |
|
| 54 |
type library_t = { |
| 55 |
library_name : compilation_unit_name; |
| 56 |
library_filename : CUnix.physical_path; |
| 57 |
library_compiled : Safe_typing.compiled_library; |
| 58 |
library_opaques : seg_proofs; |
| 59 |
library_deps : (compilation_unit_name * Safe_typing.vodigest) array; |
| 60 |
library_digest : Safe_typing.vodigest; |
| 61 |
library_vm : Vmlibrary.on_disk; |
| 62 |
} |
| 63 |
|
| 64 |
module LibraryOrdered = |
| 65 |
struct |
| 66 |
type t = DirPath.t |
| 67 |
let compare d1 d2 = |
| 68 |
compare |
| 69 |
(List.rev (DirPath.repr d1)) (List.rev (DirPath.repr d2)) |
| 70 |
end |
| 71 |
|
| 72 |
module LibrarySet = Set.Make(LibraryOrdered) |
| 73 |
module LibraryMap = Map.Make(LibraryOrdered) |
| 74 |
|
| 75 |
|
| 76 |
let libraries_table = ref LibraryMap.empty |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
let find_library dir = |
| 81 |
LibraryMap.find dir !libraries_table |
| 82 |
|
| 83 |
let library_full_filename dir = (find_library dir).library_filename |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
let register_loaded_library m = |
| 89 |
libraries_table := LibraryMap.add m.library_name m !libraries_table |
| 90 |
|
| 91 |
|
| 92 |
let opaque_tables = ref LibraryMap.empty |
| 93 |
|
| 94 |
let access_opaque_table dp i = |
| 95 |
let t = |
| 96 |
try LibraryMap.find dp !opaque_tables |
| 97 |
with Not_found -> assert false |
| 98 |
in |
| 99 |
let i = Opaqueproof.repr_handle i in |
| 100 |
let () = assert (0 <= i && i < Array.length t) in |
| 101 |
t.(i) |
| 102 |
|
| 103 |
let indirect_accessor o = |
| 104 |
let (sub, ci, dp, i) = Opaqueproof.repr o in |
| 105 |
let c = access_opaque_table dp i in |
| 106 |
let c = match c with |
| 107 |
| None -> CErrors.user_err Pp.(str "Cannot access opaque delayed proof.") |
| 108 |
| Some c -> c |
| 109 |
in |
| 110 |
let (c, prv) = Discharge.cook_opaque_proofterm ci c in |
| 111 |
let c = Mod_subst.subst_mps_list sub c in |
| 112 |
(c, prv) |
| 113 |
|
| 114 |
let () = Mod_checking.set_indirect_accessor indirect_accessor |
| 115 |
|
| 116 |
let check_one_lib admit senv (dir,m) = |
| 117 |
let md = m.library_compiled in |
| 118 |
let dig = m.library_digest in |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
let senv = |
| 123 |
if LibrarySet.mem dir admit then |
| 124 |
(Flags.if_verbose Feedback.msg_notice |
| 125 |
(str "Admitting library: " ++ pr_dirpath dir); |
| 126 |
Safe_checking.unsafe_import (fst senv) md m.library_vm dig), |
| 127 |
(snd senv) |
| 128 |
else |
| 129 |
(Flags.if_verbose Feedback.msg_notice |
| 130 |
(str "Checking library: " ++ pr_dirpath dir); |
| 131 |
Safe_checking.import (fst senv) (snd senv) md m.library_vm dig) |
| 132 |
in |
| 133 |
register_loaded_library m; senv |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
type logical_path = DirPath.t |
| 139 |
|
| 140 |
let load_paths = ref ([],[] : CUnix.physical_path list * logical_path list) |
| 141 |
|
| 142 |
|
| 143 |
let find_logical_path phys_dir = |
| 144 |
let phys_dir = CUnix.canonical_path_name phys_dir in |
| 145 |
let physical, logical = !load_paths in |
| 146 |
match List.filter2 (fun p d -> p = phys_dir) physical logical with |
| 147 |
| _,[dir] -> dir |
| 148 |
| _,[] -> default_root_prefix |
| 149 |
| _,l -> CErrors.anomaly (Pp.str ("Two logical paths are associated to "^phys_dir^".")) |
| 150 |
|
| 151 |
let remove_load_path dir = |
| 152 |
let physical, logical = !load_paths in |
| 153 |
load_paths := List.filter2 (fun p d -> p <> dir) physical logical |
| 154 |
|
| 155 |
let add_load_path (phys_path,rocq_path) = |
| 156 |
if CDebug.(get_flag misc) then |
| 157 |
Feedback.msg_notice (str "path: " ++ pr_dirpath rocq_path ++ str " ->" ++ spc() ++ |
| 158 |
str phys_path); |
| 159 |
let phys_path = CUnix.canonical_path_name phys_path in |
| 160 |
let physical, logical = !load_paths in |
| 161 |
match List.filter2 (fun p d -> p = phys_path) physical logical with |
| 162 |
| _,[dir] -> |
| 163 |
if rocq_path <> dir |
| 164 |
|
| 165 |
&& not |
| 166 |
(phys_path = CUnix.canonical_path_name Filename.current_dir_name |
| 167 |
&& rocq_path = default_root_prefix) |
| 168 |
then |
| 169 |
begin |
| 170 |
|
| 171 |
if dir <> default_root_prefix then |
| 172 |
Feedback.msg_warning |
| 173 |
(str phys_path ++ strbrk " was previously bound to " ++ |
| 174 |
pr_dirpath dir ++ strbrk "; it is remapped to " ++ |
| 175 |
pr_dirpath rocq_path); |
| 176 |
remove_load_path phys_path; |
| 177 |
load_paths := (phys_path::fst !load_paths, rocq_path::snd !load_paths) |
| 178 |
end |
| 179 |
| _,[] -> |
| 180 |
load_paths := (phys_path :: fst !load_paths, rocq_path :: snd !load_paths) |
| 181 |
| _ -> CErrors.anomaly (Pp.str ("Two logical paths are associated to "^phys_path^".")) |
| 182 |
|
| 183 |
let load_paths_of_dir_path dir = |
| 184 |
let physical, logical = !load_paths in |
| 185 |
fst (List.filter2 (fun p d -> d = dir) physical logical) |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
exception LibUnmappedDir |
| 191 |
exception LibNotFound |
| 192 |
|
| 193 |
let locate_absolute_library dir = |
| 194 |
|
| 195 |
let pref, base = split_dirpath dir in |
| 196 |
let loadpath = load_paths_of_dir_path pref in |
| 197 |
if loadpath = [] then raise LibUnmappedDir; |
| 198 |
try |
| 199 |
let name = Id.to_string base^".vo" in |
| 200 |
let _, file = System.where_in_path ~warn:false loadpath name in |
| 201 |
(dir, file) |
| 202 |
with Not_found -> |
| 203 |
|
| 204 |
try |
| 205 |
(dir, library_full_filename dir) |
| 206 |
with Not_found -> |
| 207 |
raise LibNotFound |
| 208 |
|
| 209 |
let locate_qualified_library qid = |
| 210 |
try |
| 211 |
|
| 212 |
let loadpath = load_paths_of_dir_path (dir_of_path qid) in |
| 213 |
if loadpath = [] then raise LibUnmappedDir; |
| 214 |
let name = qid.basename^".vo" in |
| 215 |
let path, file = System.where_in_path loadpath name in |
| 216 |
let dir = |
| 217 |
extend_dirpath (find_logical_path path) (Id.of_string qid.basename) in |
| 218 |
|
| 219 |
try |
| 220 |
(dir, library_full_filename dir) |
| 221 |
with Not_found -> |
| 222 |
(dir, file) |
| 223 |
with Not_found -> raise LibNotFound |
| 224 |
|
| 225 |
let error_unmapped_dir qid = |
| 226 |
let prefix = qid.dirpath in |
| 227 |
CErrors.user_err |
| 228 |
(str "Cannot load " ++ pr_path qid ++ str ":" ++ spc () ++ |
| 229 |
str "no physical path bound to" ++ spc () ++ pr_dirlist prefix |
| 230 |
++ str "." ++ fnl ()) |
| 231 |
|
| 232 |
let error_lib_not_found qid = |
| 233 |
CErrors.user_err |
| 234 |
(str "Cannot find library " ++ pr_path qid ++ str " in loadpath.") |
| 235 |
|
| 236 |
let try_locate_absolute_library dir = |
| 237 |
try |
| 238 |
locate_absolute_library dir |
| 239 |
with |
| 240 |
| LibUnmappedDir -> error_unmapped_dir (path_of_dirpath dir) |
| 241 |
| LibNotFound -> error_lib_not_found (path_of_dirpath dir) |
| 242 |
|
| 243 |
let try_locate_qualified_library lib = match lib with |
| 244 |
| PhysicalFile f -> |
| 245 |
let () = |
| 246 |
if not (Sys.file_exists f) then |
| 247 |
CErrors.user_err Pp.(str f ++ str ": file not found.") |
| 248 |
in |
| 249 |
let dir = Filename.dirname f in |
| 250 |
let base = Filename.chop_extension (Filename.basename f) in |
| 251 |
let dir = extend_dirpath (find_logical_path dir) (Id.of_string base) in |
| 252 |
(dir, f) |
| 253 |
| LogicalFile qid -> |
| 254 |
try |
| 255 |
locate_qualified_library qid |
| 256 |
with |
| 257 |
| LibUnmappedDir -> error_unmapped_dir qid |
| 258 |
| LibNotFound -> error_lib_not_found qid |
| 259 |
|
| 260 |
let lib_to_string = function |
| 261 |
| PhysicalFile f -> f |
| 262 |
| LogicalFile qid -> String.concat "." (List.rev (qid.basename :: qid.dirpath)) |
| 263 |
|
| 264 |
let try_locate_qualified_library lib : _ * _ = |
| 265 |
NewProfile.profile "try_locate_qualified_library" |
| 266 |
~args:(fun () -> [("name", `String (lib_to_string lib))]) |
| 267 |
(fun () -> try_locate_qualified_library lib) |
| 268 |
() |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
let raw_intern_library f = |
| 274 |
ObjFile.open_in ~file:f |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
type library_info |
| 280 |
|
| 281 |
type summary_disk = { |
| 282 |
md_name : compilation_unit_name; |
| 283 |
md_deps : (compilation_unit_name * Safe_typing.vodigest) array; |
| 284 |
md_ocaml : string; |
| 285 |
md_info : library_info; |
| 286 |
} |
| 287 |
|
| 288 |
type library_objects |
| 289 |
|
| 290 |
type library_disk = { |
| 291 |
md_compiled : Safe_typing.compiled_library; |
| 292 |
md_syntax_objects : library_objects; |
| 293 |
md_objects : library_objects; |
| 294 |
} |
| 295 |
|
| 296 |
let mk_library sd md f table digest vm = { |
| 297 |
library_name = sd.md_name; |
| 298 |
library_filename = f; |
| 299 |
library_compiled = md.md_compiled; |
| 300 |
library_opaques = table; |
| 301 |
library_deps = sd.md_deps; |
| 302 |
library_digest = digest; |
| 303 |
library_vm = vm; |
| 304 |
} |
| 305 |
|
| 306 |
let name_clash_message dir mdir f = |
| 307 |
str ("The file " ^ f ^ " contains library") ++ spc () ++ |
| 308 |
pr_dirpath mdir ++ spc () ++ str "and not library" ++ spc() ++ |
| 309 |
pr_dirpath dir |
| 310 |
|
| 311 |
type intern_mode = Rec | Root | Dep |
| 312 |
|
| 313 |
|
| 314 |
let depgraph = ref LibraryMap.empty |
| 315 |
|
| 316 |
let marshal_in_segment (type a) ~validate ~value ~(segment : a ObjFile.segment) f ch : a = |
| 317 |
NewProfile.profile "marshal_in_segment" (fun () -> |
| 318 |
let () = LargeFile.seek_in ch segment.ObjFile.pos in |
| 319 |
if validate then |
| 320 |
let v = |
| 321 |
try |
| 322 |
let v = Analyze.parse_channel ch in |
| 323 |
let digest = Digest.input ch in |
| 324 |
let () = if not (String.equal digest segment.ObjFile.hash) then raise_notrace Exit in |
| 325 |
v |
| 326 |
with exn when CErrors.noncritical exn -> |
| 327 |
CErrors.user_err (str "Corrupted file " ++ quote (str f)) |
| 328 |
in |
| 329 |
let () = Validate.validate value v in |
| 330 |
let v = Analyze.instantiate v in |
| 331 |
Obj.obj v |
| 332 |
else |
| 333 |
System.marshal_in f ch) |
| 334 |
() |
| 335 |
|
| 336 |
let summary_seg : summary_disk ObjFile.id = ObjFile.make_id "summary" |
| 337 |
let library_seg : library_disk ObjFile.id = ObjFile.make_id "library" |
| 338 |
let opaques_seg : seg_proofs ObjFile.id = ObjFile.make_id "opaques" |
| 339 |
let vm_seg = Vmlibrary.vm_segment |
| 340 |
|
| 341 |
let intern_from_file ~intern_mode ~enable_VM (dir, f) = |
| 342 |
let validate = intern_mode <> Dep in |
| 343 |
Flags.if_verbose chk_pp (str"[intern "++str f++str" ..."); |
| 344 |
let (sd,md,table,vmlib,digest) = |
| 345 |
try |
| 346 |
|
| 347 |
let ch = System.with_magic_number_check raw_intern_library f in |
| 348 |
let seg_sd = ObjFile.get_segment ch ~segment:summary_seg in |
| 349 |
let seg_md = ObjFile.get_segment ch ~segment:library_seg in |
| 350 |
let seg_opaque = ObjFile.get_segment ch ~segment:opaques_seg in |
| 351 |
let seg_vmlib = ObjFile.get_segment ch ~segment:vm_seg in |
| 352 |
let () = ObjFile.close_in ch in |
| 353 |
|
| 354 |
let ch = open_in_bin f in |
| 355 |
|
| 356 |
let sd = marshal_in_segment ~validate ~value:Values.v_libsum ~segment:seg_sd f ch in |
| 357 |
let md = marshal_in_segment ~validate ~value:Values.v_lib ~segment:seg_md f ch in |
| 358 |
let table = marshal_in_segment ~validate ~value:Values.v_opaquetable ~segment:seg_opaque f ch in |
| 359 |
let vmlib = if enable_VM |
| 360 |
then marshal_in_segment ~validate ~value:Values.v_vmlib ~segment:seg_vmlib f ch |
| 361 |
else Vmlibrary.(export (set_path dir empty)) |
| 362 |
in |
| 363 |
|
| 364 |
let () = close_in ch in |
| 365 |
let () = System.check_caml_version ~caml:sd.md_ocaml ~file:f in |
| 366 |
if dir <> sd.md_name then |
| 367 |
CErrors.user_err |
| 368 |
(name_clash_message dir sd.md_name f); |
| 369 |
Flags.if_verbose chk_pp (str" done]" ++ fnl ()); |
| 370 |
let digest = |
| 371 |
Safe_typing.Dvo_or_vi seg_md.hash in |
| 372 |
sd,md,table,vmlib,digest |
| 373 |
with e -> Flags.if_verbose chk_pp (str" failed!]" ++ fnl ()); raise e in |
| 374 |
depgraph := LibraryMap.add sd.md_name sd.md_deps !depgraph; |
| 375 |
opaque_tables := LibraryMap.add sd.md_name table !opaque_tables; |
| 376 |
mk_library sd md f table digest (Vmlibrary.inject vmlib) |
| 377 |
|
| 378 |
let intern_from_file ~intern_mode ~enable_VM dirf : library_t = |
| 379 |
NewProfile.profile "intern_from_file" |
| 380 |
~args:(fun () -> [("name", `String (DirPath.to_string (fst dirf)))]) |
| 381 |
(fun () -> intern_from_file ~intern_mode ~enable_VM dirf) |
| 382 |
() |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
let rec intern_library ~intern_mode ~enable_VM seen (dir, f) needed = |
| 387 |
if LibrarySet.mem dir seen then failwith "Recursive dependencies!"; |
| 388 |
|
| 389 |
try let _ = find_library dir in needed |
| 390 |
with Not_found -> |
| 391 |
|
| 392 |
if List.mem_assoc_f DirPath.equal dir needed then needed |
| 393 |
else |
| 394 |
|
| 395 |
let m = intern_from_file ~intern_mode ~enable_VM (dir,f) in |
| 396 |
let seen' = LibrarySet.add dir seen in |
| 397 |
let deps = |
| 398 |
Array.map (fun (d,_) -> try_locate_absolute_library d) m.library_deps |
| 399 |
in |
| 400 |
let intern_mode = match intern_mode with Rec -> Rec | Root | Dep -> Dep in |
| 401 |
(dir,m) :: Array.fold_right (intern_library ~intern_mode ~enable_VM seen') deps needed |
| 402 |
|
| 403 |
|
| 404 |
let rec fold_deps seen ff (dir,f) (s,acc) = |
| 405 |
if LibrarySet.mem dir seen then failwith "Recursive dependencies!"; |
| 406 |
if LibrarySet.mem dir s then (s,acc) |
| 407 |
else |
| 408 |
let deps = match LibraryMap.find_opt dir !depgraph with |
| 409 |
| Some deps -> deps |
| 410 |
| None -> |
| 411 |
CErrors.anomaly Pp.(str "missing dep when computing closure (" ++ DirPath.print dir ++ str ")") |
| 412 |
in |
| 413 |
let deps = Array.map (fun (d,_) -> try_locate_absolute_library d) deps in |
| 414 |
let seen' = LibrarySet.add dir seen in |
| 415 |
let (s',acc') = Array.fold_right (fold_deps seen' ff) deps (s,acc) in |
| 416 |
(LibrarySet.add dir s', ff dir acc') |
| 417 |
|
| 418 |
and fold_deps_list seen ff modl needed = |
| 419 |
List.fold_right (fold_deps seen ff) modl needed |
| 420 |
|
| 421 |
let fold_deps_list ff modl acc = |
| 422 |
snd (fold_deps_list LibrarySet.empty ff modl (LibrarySet.empty,acc)) |
| 423 |
|
| 424 |
let recheck_library senv ~norec ~admit ~check = |
| 425 |
let enable_VM = (Environ.typing_flags (Safe_typing.env_of_safe_env senv)).enable_VM in |
| 426 |
let ml = List.map try_locate_qualified_library check in |
| 427 |
let nrl = List.map try_locate_qualified_library norec in |
| 428 |
let al = List.map try_locate_qualified_library admit in |
| 429 |
let needed = List.fold_right (intern_library ~intern_mode:Rec ~enable_VM LibrarySet.empty) ml [] in |
| 430 |
let needed = List.fold_right (intern_library ~intern_mode:Root ~enable_VM LibrarySet.empty) nrl needed in |
| 431 |
let needed = List.rev needed in |
| 432 |
|
| 433 |
|
| 434 |
let nochk = fold_deps_list LibrarySet.add nrl LibrarySet.empty in |
| 435 |
let nochk = fold_deps_list LibrarySet.remove ml nochk in |
| 436 |
let nochk = fold_deps_list LibrarySet.add al nochk in |
| 437 |
|
| 438 |
let nochk = |
| 439 |
List.fold_right LibrarySet.remove (List.map fst (nrl@ml)) nochk in |
| 440 |
|
| 441 |
Flags.if_verbose Feedback.msg_notice (fnl()++hv 2 (str "Ordered list:" ++ fnl() ++ |
| 442 |
prlist |
| 443 |
(fun (dir,_) -> pr_dirpath dir ++ fnl()) needed)); |
| 444 |
let senv = List.fold_left (check_one_lib nochk) (senv, Cmap.empty) needed in |
| 445 |
Flags.if_verbose Feedback.msg_notice (str"Modules were successfully checked"); |
| 446 |
senv |