Skip to content

Commit fc19761

Browse files
authored
fix(compiler): Stabilize CRC checks (#2101)
1 parent d45ddfa commit fc19761

File tree

362 files changed

+723
-688
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

362 files changed

+723
-688
lines changed

compiler/grainc/grainc.re

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ let compile_file = (name, outfile_arg) => {
3636
~default=Compile.default_output_filename(name),
3737
outfile_arg,
3838
);
39-
if (Grain_utils.Config.debug^) {
40-
Compile.save_mashed(name, Compile.default_mashtree_filename(outfile));
41-
};
4239
let hook =
4340
if (Grain_utils.Config.statically_link^) {
4441
Compile.stop_after_assembled;

compiler/src/compile.re

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ let default_mashtree_filename = name =>
4949
let compile_prog = p =>
5050
Compcore.module_to_bytes @@ Compcore.compile_wasm_module(p);
5151

52+
let save_mashed = (mashed, outfile) => {
53+
switch (outfile) {
54+
| Some(outfile) =>
55+
let outfile = default_mashtree_filename(outfile);
56+
Grain_utils.Fs_access.ensure_parent_directory_exists(outfile);
57+
let mash_string =
58+
Sexplib.Sexp.to_string_hum @@ Mashtree.sexp_of_mash_program(mashed);
59+
let oc = open_out(outfile);
60+
output_string(oc, mash_string);
61+
close_out(oc);
62+
| None => ()
63+
};
64+
};
65+
5266
let log_state = state =>
5367
if (Grain_utils.Config.verbose^) {
5468
let prerr_sexp = (conv, x) =>
@@ -135,9 +149,7 @@ let next_state = (~is_root_file=false, {cstate_desc, cstate_filename} as cs) =>
135149
~no_pervasives=has_attr("noPervasives"),
136150
~runtime_mode=has_attr("runtimeMode"),
137151
);
138-
if (is_root_file) {
139-
Grain_utils.Config.set_root_config();
140-
};
152+
141153
Well_formedness.check_well_formedness(p);
142154
WellFormed(p);
143155
| WellFormed(p) =>
@@ -157,7 +169,11 @@ let next_state = (~is_root_file=false, {cstate_desc, cstate_filename} as cs) =>
157169
Linearized(Linearize.transl_anf_module(typed_mod))
158170
| Linearized(anfed) => Optimized(Optimize.optimize_program(anfed))
159171
| Optimized(optimized) =>
160-
Mashed(Transl_anf.transl_anf_program(optimized))
172+
let mashed = Transl_anf.transl_anf_program(optimized);
173+
if (Config.debug^) {
174+
save_mashed(mashed, cs.cstate_outfile);
175+
};
176+
Mashed(mashed);
161177
| Mashed(mashed) =>
162178
Compiled(Compmod.compile_wasm_module(~name=?cstate_filename, mashed))
163179
| Compiled(compiled) =>
@@ -289,6 +305,9 @@ let compile_string =
289305
reset_compiler_state();
290306
compile_wasi_polyfill();
291307
};
308+
if (is_root_file) {
309+
Grain_utils.Config.set_root_config();
310+
};
292311
let cstate = {
293312
cstate_desc: Initial(InputString(str)),
294313
cstate_filename: name,
@@ -305,6 +324,9 @@ let compile_file =
305324
reset_compiler_state();
306325
compile_wasi_polyfill();
307326
};
327+
if (is_root_file) {
328+
Grain_utils.Config.set_root_config();
329+
};
308330
let cstate = {
309331
cstate_desc: Initial(InputFile(filename)),
310332
cstate_filename: Some(filename),
@@ -317,18 +339,6 @@ let compile_file =
317339

318340
let anf = Linearize.transl_anf_module;
319341

320-
let save_mashed = (f, outfile) =>
321-
switch (compile_file(~is_root_file=false, ~hook=stop_after_mashed, f)) {
322-
| {cstate_desc: Mashed(mashed)} =>
323-
Grain_utils.Fs_access.ensure_parent_directory_exists(outfile);
324-
let mash_string =
325-
Sexplib.Sexp.to_string_hum @@ Mashtree.sexp_of_mash_program(mashed);
326-
let oc = open_out(outfile);
327-
output_string(oc, mash_string);
328-
close_out(oc);
329-
| _ => failwith("Should be impossible")
330-
};
331-
332342
let report_error = loc =>
333343
Location.(
334344
Printf.(

compiler/src/compile.rei

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ exception InlineFlagsError(Location.t, error);
4141

4242
let default_output_filename: string => string;
4343

44-
let default_mashtree_filename: string => string;
45-
4644
let stop_after_parse: compilation_state => compilation_action;
4745

4846
let stop_after_well_formed: compilation_state => compilation_action;
@@ -85,5 +83,3 @@ let compile_file:
8583
string
8684
) =>
8785
compilation_state;
88-
89-
let save_mashed: (string, string) => unit;

compiler/src/typed/cmi_format.re

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,25 @@ let cmi_digest_of_yojson =
4646
"cmi_digest_of_yojson: Invalid Digest: " ++ Yojson.Safe.to_string(d),
4747
);
4848

49+
let sexp_of_cmi_digest = d => Sexplib.Conv.sexp_of_string(Digest.to_hex(d));
50+
let cmi_digest_of_sexp =
51+
fun
52+
| Sexplib.Sexp.Atom(s) as d =>
53+
try(Digest.from_hex(s)) {
54+
| Invalid_argument(_) =>
55+
of_sexp_error("cmi_digest_of_sexp: invalid digest", d)
56+
}
57+
| d => of_sexp_error("cmi_digest_of_sexp: invalid digest", d);
58+
59+
[@deriving sexp]
60+
type cmi_crcs = list((string, cmi_digest));
61+
let rec cmi_crcs_of_yojson = [%of_yojson: list((string, cmi_digest))]
62+
and cmi_crcs_to_yojson = [%to_yojson: list((string, cmi_digest))];
63+
4964
[@deriving sexp]
50-
type cmi_crcs = [@sexp.opaque] list((string, option(Digest.t)));
51-
let rec cmi_crcs_of_yojson = [%of_yojson:
52-
list((string, option(cmi_digest)))
53-
]
54-
and cmi_crcs_to_yojson = [%to_yojson: list((string, option(cmi_digest)))];
65+
type cmi_crc = cmi_digest;
66+
let rec cmi_crc_of_yojson = [%of_yojson: cmi_digest]
67+
and cmi_crc_to_yojson = [%to_yojson: cmi_digest];
5568

5669
[@deriving (sexp, yojson)]
5770
type cmi_type_metadata = {
@@ -65,6 +78,7 @@ type cmi_infos = {
6578
cmi_name: string,
6679
cmi_sign: Types.signature,
6780
cmi_crcs,
81+
cmi_crc,
6882
cmi_flags: list(pers_flags),
6983
cmi_type_metadata,
7084
cmi_config_sum: string,
@@ -75,25 +89,18 @@ type config_opt =
7589

7690
let config_sum = Config.get_root_config_digest;
7791

78-
let build_full_cmi = (~name, ~sign, ~crcs, ~flags, ~type_metadata) => {
79-
let ns_sign = Marshal.to_bytes((name, sign, config_sum()), []);
80-
let crc = Digest.bytes(ns_sign);
81-
let crcs = [(name, Some(crc)), ...crcs];
82-
let cmi_config_sum = config_sum();
83-
{
84-
cmi_name: name,
85-
cmi_sign: sign,
86-
cmi_crcs: crcs,
87-
cmi_flags: flags,
88-
cmi_type_metadata: type_metadata,
89-
cmi_config_sum,
90-
};
91-
};
92+
let build_crc = (~name: string, sign: Types.signature) => {
93+
let subst_sign =
94+
Subst.with_reset_state(() =>
95+
Subst.signature(Subst.for_crc(Subst.identity), sign)
96+
);
9297

93-
let cmi_to_crc = ({cmi_name, cmi_sign, cmi_config_sum}) => {
94-
let ns_sign = Marshal.to_bytes((cmi_name, cmi_sign, cmi_config_sum), []);
95-
let crc = Digest.bytes(ns_sign);
96-
crc;
98+
let ns_sign =
99+
Marshal.to_bytes(
100+
(name, subst_sign, Config.get_root_config_digest()),
101+
[],
102+
);
103+
Digest.bytes(ns_sign);
97104
};
98105

99106
let input_cmi = ic =>

compiler/src/typed/cmi_format.rei

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,16 @@ type cmi_type_metadata = {
2828
type cmi_infos = {
2929
cmi_name: string,
3030
cmi_sign: list(Types.signature_item),
31-
cmi_crcs: list((string, option(Digest.t))),
31+
cmi_crcs: list((string, Digest.t)),
32+
cmi_crc: Digest.t,
3233
cmi_flags: list(pers_flags),
3334
cmi_type_metadata,
3435
cmi_config_sum: string,
3536
};
3637

3738
let config_sum: unit => string;
3839

39-
let build_full_cmi:
40-
(
41-
~name: string,
42-
~sign: list(Types.signature_item),
43-
~crcs: list((string, option(Digest.t))),
44-
~flags: list(pers_flags),
45-
~type_metadata: cmi_type_metadata
46-
) =>
47-
cmi_infos;
40+
let build_crc: (~name: string, Types.signature) => Digest.t;
4841

4942
/* write the magic + the cmi information */
5043
let serialize_cmi: cmi_infos => bytes;
@@ -55,8 +48,6 @@ let input_cmi: in_channel => cmi_infos;
5548
/* read a cmi from a filename, checking the magic */
5649
let read_cmi: string => cmi_infos;
5750

58-
let cmi_to_crc: cmi_infos => Digest.t;
59-
6051
/* Error report */
6152

6253
type error =

0 commit comments

Comments
 (0)