wasi2ic 0.2.16

This tool converts WebAssembly System Interface (WASI) dependent Wasm files to be compatible with the Internet Computer (IC) platform.
Documentation
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::collections::HashMap;

use crate::*;

#[test]
fn test_add_start_entry() {
    let wat = r#"
        (module
            (func $add (param i32 i32) (result i32)
                local.get 0
                local.get 1
                i32.add
            )

            (func $_initialize
                i32.const 2
                i32.const 3
                call $add
                i32.const 5
                i32.const 7
                call $add
                drop
                drop
            )
        )
    "#;

    let binary = wat::parse_str(wat).unwrap();
    let mut module = walrus::Module::from_buffer(&binary).unwrap();

    assert!(module.start.is_none());

    common::add_start_entry(&mut module);

    assert!(module.start.is_some());
}

#[test]
fn test_remove_start_export() {
    let wat = r#"
        (module
            (func $add (param i32 i32) (result i32)
                local.get 0
                local.get 1
                i32.add
            )

            (func $_initialize
                i32.const 2
                i32.const 3
                call $add
                i32.const 5
                i32.const 7
                call $add
                drop
                drop
            )

            (export "_initialize" (func $_initialize))
        )
    "#;

    let binary = wat::parse_str(wat).unwrap();
    let mut module = walrus::Module::from_buffer(&binary).unwrap();

    let mut export_found: Option<walrus::ExportId> = None;

    // try to find the initialize export
    for export in module.exports.iter() {
        if !export.name.starts_with("_initialize") {
            continue;
        }

        if let walrus::ExportItem::Function(_) = export.item {
            export_found = Some(export.id());
        }
    }

    assert!(export_found.is_some());

    common::remove_start_export(&mut module);

    let mut export_found: Option<walrus::ExportId> = None;
    // try to find the initialize export
    for export in module.exports.iter() {
        if !export.name.starts_with("_initialize") {
            continue;
        }

        if let walrus::ExportItem::Function(_) = export.item {
            export_found = Some(export.id());
        }
    }

    assert!(export_found.is_none());
}

#[test]
fn test_gather_replacement_ids() {
    let wat = r#"
    (module
        (type (;0;) (func))
        (type (;1;) (func (param i32)))
        (type (;2;) (func (param i32 i32)))
        (type (;3;) (func (param i32 i32) (result i32)))
        (type (;4;) (func (param i32 i32 i32)))
        (type (;5;) (func (param i32 i32 i32 i32) (result i32)))

        (import "ic0" "debug_print" (func $_dprint (;0;) (type 2)))
        (import "ic0" "msg_reply" (func $_msg_reply (;1;) (type 0)))
        (import "wasi_unstable" "fd_write" (func $_wasi_unstable_fd_write (;2;) (type 5)))
        (import "wasi_unstable" "random_get" (func $_wasi_unstable_random_get (;3;) (type 3)))
        (import "wasi_unstable" "environ_get" (func $__imported_wasi_unstable_environ_get (;4;) (type 3)))
        (import "wasi_unstable" "proc_exit" (func $__imported_wasi_unstable_proc_exit (;5;) (type 1)))

        (func $_initialize (;6;) (type 0)
            i32.const 1
            i32.const 2
            call $__ic_custom_random_get
            i32.const 1
            i32.const 2
            call $_wasi_unstable_random_get
            i32.const 4
            i32.const 5
            call $_wasi_unstable_fd_write
            drop
        )

        (func $__ic_custom_random_get (;8;) (type 3) (param i32 i32) (result i32)
            call $_msg_reply

            i32.const 421
        )

        (func $ic_dummy_fd_write (;7;) (type 5) (param i32 i32 i32 i32) (result i32)
            i32.const 0
            i32.const 0
            call $_dprint
            i32.const 42
        )

        (export "__ic_custom_fd_write" (func $ic_dummy_fd_write))
        (export "_initialize" (func $_initialize))
    )
    "#;

    let binary = wat::parse_str(wat).unwrap();
    let module = walrus::Module::from_buffer(&binary).unwrap();

    let id_reps: HashMap<usize, usize> = common::gather_replacement_ids(&module)
        .iter()
        .map(|(x, y)| (x.index(), y.index()))
        .collect();

    assert!(id_reps[&2] == 8);
    assert!(id_reps[&3] == 7);
}

#[test]
fn test_do_module_replacements() {
    let wat = r#"
    (module
        (type (;0;) (func))
        (type (;1;) (func (param i32)))
        (type (;2;) (func (param i32 i32)))
        (type (;3;) (func (param i32 i32) (result i32)))
        (type (;4;) (func (param i32 i32 i32)))
        (type (;5;) (func (param i32 i32 i32 i32) (result i32)))

        (import "ic0" "debug_print" (func $_dprint (;0;) (type 2)))
        (import "ic0" "msg_reply" (func $_msg_reply (;1;) (type 0)))
        (import "wasi_snapshot_preview1" "fd_write" (func $_wasi_snapshot_preview_fd_write (;2;) (type 5)))
        (import "wasi_snapshot_preview1" "random_get" (func $_wasi_snapshot_preview_random_get (;3;) (type 3)))
        (import "wasi_snapshot_preview1" "environ_get" (func $__imported_wasi_snapshot_preview1_environ_get (;4;) (type 3)))
        (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (;5;) (type 1)))
        
        (table (;0;) 5 5 funcref)
        (elem (;0;) (i32.const 1) func $_wasi_snapshot_preview_fd_write $_wasi_snapshot_preview_random_get )
        
        (memory (export "memory") 1 100)

        (data (i32.const 0x0000)
            "\65\66\67\68"
        )

        (func $_initialize (;6;) (type 0)
            i32.const 1
            i32.const 2
            call $_wasi_snapshot_preview_random_get

            (block $test_block 
                i32.const 0
                (if 
                    (then
                        (block $test_block3
                            i32.const 1
                            i32.const 2
                
                            call $_wasi_snapshot_preview_random_get
    
                            drop
                        )
                    )
                    (else 
                        (loop $test_loop (result i32)
                            i32.const 1
                            i32.const 2
                
                            call $_wasi_snapshot_preview_random_get
    
                            br_if $test_loop 

                            i32.const 2
                        )

                        drop

                    )
                )
            )

            i32.const 3
            i32.const 4
            i32.const 5
            call $_wasi_snapshot_preview_fd_write

            call $__imported_wasi_snapshot_preview1_proc_exit
        )

        (func $__ic_custom_random_get (;7;) (type 3) (param i32 i32) (result i32)
            i32.const 0
            ref.func $_wasi_snapshot_preview_random_get
            table.set 0

            call $_msg_reply
            i32.const 421
        )

        (func $__ic_custom_fd_write (;8;) (type 5) (param i32 i32 i32 i32) (result i32)
            i32.const 0
            i32.const 0
            call $_dprint
            i32.const 42
        )

        (export "_initialize" (func $_initialize))
    )
    "#;

    let binary = wat::parse_str(wat).unwrap();
    let mut module = walrus::Module::from_buffer(&binary).unwrap();

    common::do_module_replacements(&mut module);

    // we expect random_get and fd_write to be replaced, environ_get to be removed and the calls to the proc_exit to remain
    let imports = module.imports;

    let result = imports.find("ic0", "debug_print");
    assert!(result.is_some());
    let result = imports.find("ic0", "msg_reply");
    assert!(result.is_some());
    let result = imports.find("wasi_snapshot_preview1", "proc_exit");
    assert!(result.is_some());
    let result = imports.find("wasi_snapshot_preview1", "fd_write");
    assert!(result.is_none());
    let result = imports.find("wasi_snapshot_preview1", "random_get");
    assert!(result.is_none());
    let result = imports.find("wasi_snapshot_preview1", "environ_get");
    assert!(result.is_none());
}

#[test]
fn test_do_module_replacements_remaining_imports() {
    let wat = r#"
    (module
        (type (;0;) (func))
        (type (;1;) (func (param i32)))
        (type (;2;) (func (param i32 i32)))
        (type (;3;) (func (param i32 i32) (result i32)))
        (type (;4;) (func (param i32 i32 i32)))
        (type (;5;) (func (param i32 i32 i32 i32) (result i32)))

        (import "ic0" "debug_print" (func $_dprint (;0;) (type 2)))
        (import "env" "some_function" (func $_some_function (;0;) (type 2)))
        (import "ic0" "msg_reply" (func $_msg_reply (;1;) (type 0)))
        (import "wasi_snapshot_preview1" "fd_write" (func $_wasi_snapshot_preview_fd_write (;2;) (type 5)))
        (import "wasi_snapshot_preview1" "random_get" (func $_wasi_snapshot_preview_random_get (;3;) (type 3)))
        (import "wasi_snapshot_preview1" "environ_get" (func $__imported_wasi_snapshot_preview1_environ_get (;4;) (type 3)))
        (import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (;5;) (type 1)))
        
        (table (;0;) 5 5 funcref)
        (elem (;0;) (i32.const 1) func $_wasi_snapshot_preview_fd_write $_wasi_snapshot_preview_random_get )
        
        (memory (export "memory") 1 100)

        (data (i32.const 0x0000)
            "\65\66\67\68"
        )

        (func $_initialize (;6;) (type 0)
            i32.const 1
            i32.const 2
            call $_wasi_snapshot_preview_random_get

            (block $test_block 
                i32.const 0
                (if 
                    (then
                        (block $test_block3
                            i32.const 1
                            i32.const 2
                
                            call $_wasi_snapshot_preview_random_get
    
                            drop
                        )
                    )
                    (else 
                        (loop $test_loop (result i32)
                            i32.const 1
                            i32.const 2
                
                            call $_wasi_snapshot_preview_random_get
    
                            br_if $test_loop 

                            i32.const 2
                        )

                        drop

                    )
                )
            )

            i32.const 3
            i32.const 4
            i32.const 5
            call $_wasi_snapshot_preview_fd_write

            call $__imported_wasi_snapshot_preview1_proc_exit
        )

        (func $__ic_custom_random_get (;7;) (type 3) (param i32 i32) (result i32)
            i32.const 0
            ref.func $_wasi_snapshot_preview_random_get
            table.set 0

            call $_msg_reply
            i32.const 421
        )

        (func $__ic_custom_fd_write (;8;) (type 5) (param i32 i32 i32 i32) (result i32)
            i32.const 0
            i32.const 0
            call $_dprint
            i32.const 0
            i32.const 0
            call $_some_function
            i32.const 42
        )

        (export "_initialize" (func $_initialize))
    )
    "#;

    let binary = wat::parse_str(wat).unwrap();
    let mut module = walrus::Module::from_buffer(&binary).unwrap();

    common::do_module_replacements(&mut module);

    // we expect random_get and fd_write to be replaced, environ_get to be removed and the calls to the proc_exit to remain
    let imports = module.imports;

    let result = imports.find("ic0", "debug_print");
    assert!(result.is_some());
    let result = imports.find("ic0", "msg_reply");
    assert!(result.is_some());
    let result = imports.find("wasi_snapshot_preview1", "proc_exit");
    assert!(result.is_some());
    let result = imports.find("wasi_snapshot_preview1", "fd_write");
    assert!(result.is_none());
    let result = imports.find("wasi_snapshot_preview1", "random_get");
    assert!(result.is_none());
    let result = imports.find("wasi_snapshot_preview1", "environ_get");
    assert!(result.is_none());

    let result = imports.find("env", "some_function");
    assert!(result.is_some());
}

#[test]
fn test_file_processing() {
    std::fs::create_dir_all("target/test").unwrap();

    let args: arguments::Wasm2icArgs = arguments::Wasm2icArgs {
        quiet: false,
        imports: false,
        input_file: "test/assets/main_test.wat".to_string(),
        output_file: "target/test/nowasi.wasm".to_string(),
    };

    let input_file = Path::new(&args.input_file);
    assert!(input_file.exists());

    let output_wasm = Path::new(&args.output_file);
    let _ = std::fs::remove_file(output_wasm);
    assert!(!output_wasm.exists());

    do_wasm_file_processing(&args).unwrap();

    assert!(output_wasm.exists());

    let module = walrus::Module::from_file(output_wasm).unwrap();

    // we expect random_get and fd_write to be replaced, environ_get to be removed and the calls to the proc_exit to remain
    let imports = module.imports;

    let result = imports.find("ic0", "debug_print");
    assert!(result.is_some());
    let result = imports.find("ic0", "msg_reply");
    assert!(result.is_some());
    let result = imports.find("wasi_snapshot_preview1", "fd_write");
    assert!(result.is_none());
    let result = imports.find("wasi_snapshot_preview1", "random_get");
    assert!(result.is_none());
    let result = imports.find("wasi_snapshot_preview1", "environ_get");
    assert!(result.is_none());
}

#[test]
fn test_bad_import() {
    std::fs::create_dir_all("target/test").unwrap();

    let args: arguments::Wasm2icArgs = arguments::Wasm2icArgs {
        quiet: false,
        imports: false,
        input_file: "test/assets/test_bad_imports.wat".to_string(),
        output_file: "target/test/nowasi1.wasm".to_string(),
    };

    let input_file = Path::new(&args.input_file);
    assert!(input_file.exists());

    let output_wasm = Path::new(&args.output_file);
    let _ = std::fs::remove_file(output_wasm);
    assert!(!output_wasm.exists());

    let process_result = do_wasm_file_processing(&args);

    assert!(output_wasm.exists());

    assert!(process_result.is_err());
}