Skip to content

Commit 18cc926

Browse files
vouillonhhugo
authored andcommitted
Runtime compilation: fail if importing from unexpected modules
1 parent f9aa37a commit 18cc926

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ let build_runtime ~runtime_file =
116116
~link_options:[ "-g" ]
117117
~opt_options:[ "-g"; "-O2" ]
118118
~variables
119+
~allowed_imports:
120+
(Some
121+
[ "bindings"
122+
; "Math"
123+
; "js"
124+
; "wasm:js-string"
125+
; "wasm:text-encoder"
126+
; "wasm:text-decoder"
127+
])
119128
~inputs
120129
~output_file:runtime_file
121130

compiler/bin-wasm_of_ocaml/link_wasm.ml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type options =
3030
{ input_modules : (string * string) list
3131
; output_file : string
3232
; variables : Preprocess.variables
33+
; allowed_imports : string list option
3334
; binaryen_options : binaryen_options
3435
}
3536

@@ -47,6 +48,13 @@ let options =
4748
let doc = "Specify the Wasm binary output file $(docv)." in
4849
Arg.(required & pos 0 (some string) None & info [] ~docv:"WASM_FILE" ~doc)
4950
in
51+
let allowed_imports =
52+
let doc = "List of modules we expect to import from." in
53+
Arg.(
54+
value
55+
& opt_all (list ~sep:',' string) []
56+
& info [ "allowed-imports" ] ~docv:"IMPORT" ~doc)
57+
in
5058
let binaryen_options =
5159
let doc = "Pass option $(docv) to binaryen tools" in
5260
Arg.(value & opt_all string [] & info [ "binaryen" ] ~docv:"OPT" ~doc)
@@ -59,30 +67,45 @@ let options =
5967
let doc = "Pass option $(docv) to $(b,wasm-merge)" in
6068
Arg.(value & opt_all string [] & info [ "binaryen-merge" ] ~docv:"OPT" ~doc)
6169
in
62-
let build_t input_modules output_file variables common opt merge =
70+
let build_t input_modules output_file variables allowed_imports common opt merge =
71+
let allowed_imports =
72+
if List.is_empty allowed_imports then None else Some (List.concat allowed_imports)
73+
in
6374
`Ok
64-
{ input_modules; output_file; variables; binaryen_options = { common; opt; merge } }
75+
{ input_modules
76+
; output_file
77+
; variables
78+
; allowed_imports
79+
; binaryen_options = { common; opt; merge }
80+
}
6581
in
6682
let t =
6783
Term.(
6884
const build_t
6985
$ input_modules
7086
$ output_file
7187
$ Preprocess.variable_options
88+
$ allowed_imports
7289
$ binaryen_options
7390
$ opt_options
7491
$ merge_options)
7592
in
7693
Term.ret t
7794

7895
let link
79-
{ input_modules; output_file; variables; binaryen_options = { common; merge; opt } } =
96+
{ input_modules
97+
; output_file
98+
; variables
99+
; allowed_imports
100+
; binaryen_options = { common; merge; opt }
101+
} =
80102
let inputs =
81103
List.map
82104
~f:(fun (module_name, file) -> { Wat_preprocess.module_name; file; source = File })
83105
input_modules
84106
in
85107
Runtime.build
108+
~allowed_imports
86109
~link_options:(common @ merge)
87110
~opt_options:(common @ opt)
88111
~variables:(Preprocess.set_variables variables)

compiler/lib-wasm/runtime.ml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
open Stdlib
22

3-
let build ~link_options ~opt_options ~variables ~inputs ~output_file =
3+
let build ~allowed_imports ~link_options ~opt_options ~variables ~inputs ~output_file =
44
Fs.with_intermediate_file (Filename.temp_file "runtime-merged" ".wasm")
55
@@ fun merge_file ->
66
(Wat_preprocess.with_preprocessed_files ~variables ~inputs
@@ -18,4 +18,20 @@ let build ~link_options ~opt_options ~variables ~inputs ~output_file =
1818
~input_file:merge_file
1919
~opt_output_sourcemap:None
2020
~output_file
21-
()
21+
();
22+
let imports = Link.Wasm_binary.read_imports ~file:output_file in
23+
Option.iter allowed_imports ~f:(fun allowed_imports ->
24+
let missing_imports =
25+
List.filter
26+
~f:(fun { Link.Wasm_binary.module_; _ } ->
27+
not (List.mem module_ ~set:allowed_imports))
28+
imports
29+
in
30+
if not (List.is_empty missing_imports)
31+
then (
32+
Format.eprintf "The runtime contains unknown imports:@.";
33+
List.iter
34+
~f:(fun { Link.Wasm_binary.module_; name } ->
35+
Format.eprintf " %s %s@." module_ name)
36+
missing_imports;
37+
exit 2))

compiler/lib-wasm/runtime.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
val build :
2-
link_options:string list
2+
allowed_imports:string list option
3+
-> link_options:string list
34
-> opt_options:string list
45
-> variables:(string * Wat_preprocess.value) list
56
-> inputs:Wat_preprocess.input list

runtime/wasm/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
--binaryen=-g
1818
--binaryen-opt=-O3
1919
--set=effects=jspi
20+
--allowed-imports=bindings,Math,js,wasm:js-string,wasm:text-encoder,wasm:text-decoder
2021
%{target}
2122
%{read-lines:args})))
2223

@@ -31,6 +32,7 @@
3132
--binaryen=-g
3233
--binaryen-opt=-O3
3334
--set=effects=cps
35+
--allowed-imports=bindings,Math,js,wasm:js-string,wasm:text-encoder,wasm:text-decoder
3436
%{target}
3537
%{read-lines:args})))
3638

0 commit comments

Comments
 (0)