wasi_virt_layer-cli 0.5.0

CLI for the wasi_virt_layer crate
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
use crate::wasm_stream::{
    pipeline::StreamPass,
    tracker::IndexTracker,
    translator::{
        DefaultRebinder, Rebind, translate, translate_global_type, translate_memory_type,
        translate_sub_type, translate_table_type, translate_tag_type, translate_val_type,
    },
};
use eyre::Result;
use wasm_encoder::{
    CodeSection, ExportKind, ExportSection, Function, FunctionSection, GlobalSection,
    ImportSection, Instruction, Module, StartSection, TypeSection, ValType,
};
use wasmparser::{Payload, TypeRef};

/// Streaming pass that replaces `unreachable` instructions and hooks call sites
/// with a global flag check in a pre-target Wasm module.
///
/// This is the `wasm-encoder` equivalent of `WrapUnreachableGenerator::pre_target`.
pub struct WrapUnreachablePreTargetStreamPass {
    target_name: String,
    is_opted_in: bool,
}

impl WrapUnreachablePreTargetStreamPass {
    /// Creates a new pass for the given target.
    pub fn new(target_name: String, is_opted_in: bool) -> Self {
        Self {
            target_name,
            is_opted_in,
        }
    }
}

/// Returns a zero-valued instruction for the given Wasm value type.
fn dummy_value(ty: &wasmparser::ValType) -> Instruction<'static> {
    match ty {
        wasmparser::ValType::I32 => Instruction::I32Const(0),
        wasmparser::ValType::I64 => Instruction::I64Const(0),
        wasmparser::ValType::F32 => Instruction::F32Const(0.0_f32.into()),
        wasmparser::ValType::F64 => Instruction::F64Const(0.0_f64.into()),
        wasmparser::ValType::V128 => Instruction::V128Const(0),
        wasmparser::ValType::Ref(_) => unimplemented!("Ref types not supported for dummy returns"),
    }
}

/// Pushes dummy constants for each return type followed by a `return`.
fn push_dummy_return(func: &mut Function, return_types: &[wasmparser::ValType]) {
    for ty in return_types {
        func.instruction(&dummy_value(ty));
    }
    func.instruction(&Instruction::Return);
}

/// Rebinder that shifts function indices by the number of injected imports.
struct FuncRebinder {
    import_func_count: u32,
    shift_offset: u32,
}

impl Rebind for FuncRebinder {
    fn function(&self, index: u32) -> u32 {
        if index < self.import_func_count {
            index
        } else {
            index + self.shift_offset
        }
    }
}

impl StreamPass for WrapUnreachablePreTargetStreamPass {
    fn run(&mut self, input_wasm: &[u8]) -> Result<Vec<u8>> {
        if !self.is_opted_in {
            return Ok(input_wasm.to_vec());
        }

        println!(
            "Applying WrapUnreachable streaming pass for target: {}",
            self.target_name
        );

        let mut encoder = Module::new();

        // ── First pass: collect metadata ──────────────────────────────────
        let mut types: Vec<wasmparser::SubType> = Vec::new();
        let mut func_type_indices: Vec<u32> = Vec::new();
        let mut import_func_count: u32 = 0;
        let mut import_global_count: u32 = 0;
        let mut local_global_count: u32 = 0;

        let mut orig_main_void_idx: Option<u32> = None;
        let mut orig_thread_start_indices: std::collections::HashSet<u32> =
            std::collections::HashSet::new();

        let export_names =
            <crate::abi::Wasip1ThreadsABIExportFunc as strum::VariantNames>::VARIANTS;
        let main_void_name = format!("__wasip1_vfs_{}___main_void", self.target_name);
        let thread_start_name = format!("__wasip1_vfs_{}_wasi_thread_start", self.target_name);

        for payload in wasmparser::Parser::new(0).parse_all(input_wasm) {
            match payload? {
                Payload::TypeSection(s) => {
                    for rec_group in s {
                        for sub_ty in rec_group?.into_types() {
                            types.push(sub_ty);
                        }
                    }
                }
                Payload::ImportSection(s) => {
                    for group in s {
                        for item in group?.into_iter() {
                            let (_, import) = item?;
                            match import.ty {
                                TypeRef::Func(_) | TypeRef::FuncExact(_) => import_func_count += 1,
                                TypeRef::Global(_) => import_global_count += 1,
                                _ => {}
                            }
                        }
                    }
                }
                Payload::FunctionSection(s) => {
                    for f in s {
                        func_type_indices.push(f?);
                    }
                }
                Payload::GlobalSection(s) => {
                    for g in s {
                        let _ = g?;
                        local_global_count += 1;
                    }
                }
                Payload::ExportSection(s) => {
                    for e in s {
                        let e = e?;
                        if let wasmparser::ExternalKind::Func
                        | wasmparser::ExternalKind::FuncExact = e.kind
                        {
                            if e.name == "__main_void" || e.name == main_void_name {
                                orig_main_void_idx = Some(e.index);
                            }
                            if export_names.contains(&e.name) || e.name == thread_start_name {
                                orig_thread_start_indices.insert(e.index);
                            }
                        }
                    }
                }
                _ => {}
            }
        }

        let total_global_count = import_global_count + local_global_count;

        // ── New types ─────────────────────────────────────────────────────
        let mut type_sec = TypeSection::new();
        // Keep track of whether we found existing matching types
        let mut type_count = 0u32;
        for payload in wasmparser::Parser::new(0).parse_all(input_wasm) {
            if let Ok(Payload::TypeSection(s)) = payload {
                for rec_group in s {
                    let rec_group = rec_group?;
                    if rec_group.is_explicit_rec_group() {
                        let rec_types = rec_group
                            .into_types()
                            .map(|sub_ty| translate_sub_type(&sub_ty, &DefaultRebinder))
                            .collect::<Vec<_>>();
                        type_sec.ty().rec(rec_types);
                    } else {
                        for sub_ty in rec_group.into_types() {
                            type_sec
                                .ty()
                                .subtype(&translate_sub_type(&sub_ty, &DefaultRebinder));
                        }
                    }
                    type_count += 1;
                }
            }
        }

        // Append 4 new types
        let get_flag_ty_idx = type_count;
        type_sec.ty().function(vec![], vec![ValType::I32]); // () -> i32

        let set_flag_ty_idx = type_count + 1;
        type_sec.ty().function(vec![ValType::I32], vec![]); // (i32) -> ()

        let fix_exit_ty_idx = type_count + 2;
        type_sec
            .ty()
            .function(vec![ValType::I32], vec![ValType::I32]); // (i32) -> i32

        let handle_exit_ty_idx = type_count + 3;
        type_sec.ty().function(vec![ValType::I32], vec![]); // (i32) -> ()

        encoder.section(&type_sec);

        // ── Index calculations ────────────────────────────────────────────
        // We inject 2 new imports (fix_exit_code, handle_thread_exit),
        // which shifts all original function indices by 2.
        let fix_exit_import_idx = import_func_count;
        let handle_exit_import_idx = import_func_count + 1;

        let mut func_tracker = IndexTracker::new();
        func_tracker.original_count = import_func_count + func_type_indices.len() as u32;
        func_tracker.injected_count = 2;
        func_tracker.shift_offset = 2;

        let rebinder = FuncRebinder {
            import_func_count,
            shift_offset: 2,
        };

        let flag_global_idx = total_global_count; // appended at end of globals

        // New function indices (appended after all existing + shifted functions)
        let total_orig_funcs = func_tracker.original_count + func_tracker.shift_offset;
        let get_flag_idx = total_orig_funcs;
        let set_flag_idx = get_flag_idx + 1;
        let mut next_new_idx = set_flag_idx + 1;
        let main_void_wrapper_idx = orig_main_void_idx.map(|_| {
            let idx = next_new_idx;
            next_new_idx += 1;
            idx
        });
        let thread_start_wrapper_indices: std::collections::HashMap<u32, u32> =
            orig_thread_start_indices
                .iter()
                .map(|&orig_idx| {
                    let idx = next_new_idx;
                    next_new_idx += 1;
                    (orig_idx, idx)
                })
                .collect();

        // ── Second pass: rewrite sections ─────────────────────────────────
        let mut code_bodies: Vec<wasmparser::FunctionBody> = Vec::new();
        let mut code_flushed = false;

        for payload in wasmparser::Parser::new(0).parse_all(input_wasm) {
            match payload? {
                Payload::TypeSection(_) => {} // Already emitted above

                Payload::ImportSection(s) => {
                    let mut import_sec = ImportSection::new();
                    for group in s {
                        for item in group?.into_iter() {
                            let (_, import) = item?;
                            let entity = match import.ty {
                                TypeRef::Func(idx) | TypeRef::FuncExact(idx) => {
                                    wasm_encoder::EntityType::Function(idx)
                                }
                                TypeRef::Table(t) => wasm_encoder::EntityType::Table(
                                    translate_table_type(t, &DefaultRebinder),
                                ),
                                TypeRef::Memory(t) => {
                                    wasm_encoder::EntityType::Memory(translate_memory_type(t))
                                }
                                TypeRef::Global(t) => wasm_encoder::EntityType::Global(
                                    translate_global_type(t, &DefaultRebinder),
                                ),
                                TypeRef::Tag(t) => {
                                    wasm_encoder::EntityType::Tag(translate_tag_type(t))
                                }
                            };
                            import_sec.import(import.module, import.name, entity);
                        }
                    }
                    // Inject the 2 new imports
                    import_sec.import(
                        "__wasip1_virt_layer",
                        &format!(
                            "__wasip1_virt_layer_{}_fix_main_raw_exit_code",
                            self.target_name
                        ),
                        wasm_encoder::EntityType::Function(fix_exit_ty_idx),
                    );
                    import_sec.import(
                        "__wasip1_virt_layer",
                        &format!(
                            "__wasip1_virt_layer_{}_handle_thread_exit",
                            self.target_name
                        ),
                        wasm_encoder::EntityType::Function(handle_exit_ty_idx),
                    );
                    encoder.section(&import_sec);
                }

                Payload::FunctionSection(s) => {
                    let mut func_sec = FunctionSection::new();
                    for f in s {
                        func_sec.function(f?);
                    }
                    // Declare new functions
                    func_sec.function(get_flag_ty_idx);
                    func_sec.function(set_flag_ty_idx);
                    if let Some(idx) = orig_main_void_idx {
                        // Wrapper has the same type as the original __main_void
                        let orig_ty_idx = func_type_indices[(idx - import_func_count) as usize];
                        func_sec.function(orig_ty_idx);
                    }
                    for &orig_idx in &orig_thread_start_indices {
                        let orig_ty_idx =
                            func_type_indices[(orig_idx - import_func_count) as usize];
                        func_sec.function(orig_ty_idx);
                    }
                    encoder.section(&func_sec);
                }

                Payload::TableSection(s) => {
                    let mut sec = wasm_encoder::TableSection::new();
                    for t in s {
                        sec.table(translate_table_type(t?.ty, &DefaultRebinder));
                    }
                    encoder.section(&sec);
                }

                Payload::MemorySection(s) => {
                    let mut sec = wasm_encoder::MemorySection::new();
                    for m in s {
                        sec.memory(translate_memory_type(m?));
                    }
                    encoder.section(&sec);
                }

                Payload::TagSection(s) => {
                    let mut sec = wasm_encoder::TagSection::new();
                    for t in s {
                        let t = t?;
                        sec.tag(wasm_encoder::TagType {
                            kind: wasm_encoder::TagKind::Exception,
                            func_type_idx: t.func_type_idx,
                        });
                    }
                    encoder.section(&sec);
                }

                Payload::GlobalSection(s) => {
                    let mut sec = GlobalSection::new();
                    for g in s {
                        let g = g?;
                        let mut instrs = Vec::new();
                        for op in g.init_expr.get_operators_reader() {
                            let op = op?;
                            if matches!(op, wasmparser::Operator::End) {
                                continue;
                            }
                            instrs.push(translate(&op, &rebinder));
                        }
                        let init_expr = wasm_encoder::ConstExpr::extended(instrs);
                        sec.global(translate_global_type(g.ty, &DefaultRebinder), &init_expr);
                    }
                    // Append the unreachable_flag global
                    sec.global(
                        wasm_encoder::GlobalType {
                            val_type: ValType::I32,
                            mutable: true,
                            shared: false,
                        },
                        &wasm_encoder::ConstExpr::i32_const(0),
                    );
                    encoder.section(&sec);
                }

                Payload::ExportSection(s) => {
                    let mut sec = ExportSection::new();
                    let marker_name =
                        format!("__wasip1_virt_layer_{}_wrap_unreachable", self.target_name);
                    for e in s {
                        let e = e?;
                        if e.name == marker_name {
                            continue;
                        }

                        let kind = match e.kind {
                            wasmparser::ExternalKind::Func
                            | wasmparser::ExternalKind::FuncExact => ExportKind::Func,
                            wasmparser::ExternalKind::Table => ExportKind::Table,
                            wasmparser::ExternalKind::Memory => ExportKind::Memory,
                            wasmparser::ExternalKind::Global => ExportKind::Global,
                            wasmparser::ExternalKind::Tag => ExportKind::Tag,
                        };

                        let idx = match e.kind {
                            wasmparser::ExternalKind::Func
                            | wasmparser::ExternalKind::FuncExact => rebinder.function(e.index),
                            _ => e.index,
                        };

                        sec.export(e.name, kind, idx);
                    }
                    // Export getter / setter for the flag
                    sec.export(
                        &format!(
                            "__wasip1_virt_layer_{}_get_unreachable_flag",
                            self.target_name
                        ),
                        ExportKind::Func,
                        get_flag_idx,
                    );
                    sec.export(
                        &format!(
                            "__wasip1_virt_layer_{}_set_unreachable_flag",
                            self.target_name
                        ),
                        ExportKind::Func,
                        set_flag_idx,
                    );
                    encoder.section(&sec);
                }

                Payload::StartSection { func, .. } => {
                    encoder.section(&StartSection {
                        function_index: rebinder.function(func),
                    });
                }

                Payload::ElementSection(s) => {
                    let mut sec = wasm_encoder::ElementSection::new();
                    for elem in s {
                        let elem = elem?;
                        let funcs_vec;
                        let items = match elem.items {
                            wasmparser::ElementItems::Functions(f) => {
                                funcs_vec = f
                                    .into_iter()
                                    .map(|idx| Ok(rebinder.function(idx?)))
                                    .collect::<Result<Vec<u32>, eyre::Error>>()?;
                                wasm_encoder::Elements::Functions(std::borrow::Cow::Borrowed(
                                    &funcs_vec,
                                ))
                            }
                            wasmparser::ElementItems::Expressions(ref_ty, exprs) => {
                                let mut const_exprs = Vec::new();
                                for expr in exprs {
                                    let mut instrs = Vec::new();
                                    for op in expr?.get_operators_reader() {
                                        let op = op?;
                                        if matches!(op, wasmparser::Operator::End) {
                                            continue;
                                        }
                                        instrs.push(translate(&op, &rebinder));
                                    }
                                    const_exprs.push(wasm_encoder::ConstExpr::extended(instrs));
                                }
                                let enc_ref_ty = crate::wasm_stream::translator::translate_ref_type(
                                    ref_ty,
                                    &DefaultRebinder,
                                );
                                wasm_encoder::Elements::Expressions(
                                    enc_ref_ty,
                                    std::borrow::Cow::Owned(const_exprs),
                                )
                            }
                        };
                        match elem.kind {
                            wasmparser::ElementKind::Passive => {
                                sec.passive(items);
                            }
                            wasmparser::ElementKind::Active {
                                table_index,
                                offset_expr,
                            } => {
                                let mut instrs = Vec::new();
                                for op in offset_expr.get_operators_reader() {
                                    let op = op?;
                                    if matches!(op, wasmparser::Operator::End) {
                                        continue;
                                    }
                                    instrs.push(translate(&op, &rebinder));
                                }
                                let offset = wasm_encoder::ConstExpr::extended(instrs);
                                sec.active(table_index, &offset, items);
                            }
                            wasmparser::ElementKind::Declared => {
                                sec.declared(items);
                            }
                        }
                    }
                    encoder.section(&sec);
                }

                Payload::DataCountSection { count, .. } => {
                    encoder.section(&wasm_encoder::DataCountSection { count });
                }

                Payload::CodeSectionStart { .. } => {}

                Payload::CodeSectionEntry(body) => {
                    code_bodies.push(body);
                }

                Payload::DataSection(s) => {
                    // Code section must be emitted before data section.
                    // Flush code bodies now.
                    if !code_flushed {
                        self.emit_code_section(
                            &mut encoder,
                            &code_bodies,
                            &types,
                            &func_type_indices,
                            import_func_count,
                            flag_global_idx,
                            &rebinder,
                            orig_main_void_idx,
                            &orig_thread_start_indices,
                            fix_exit_import_idx,
                            handle_exit_import_idx,
                            main_void_wrapper_idx,
                            &thread_start_wrapper_indices,
                        )?;
                        code_flushed = true;
                    }

                    let mut sec = wasm_encoder::DataSection::new();
                    for d in s {
                        let d = d?;
                        match d.kind {
                            wasmparser::DataKind::Passive => {
                                sec.passive(d.data.iter().copied());
                            }
                            wasmparser::DataKind::Active {
                                memory_index,
                                offset_expr,
                            } => {
                                let mut instrs = Vec::new();
                                for op in offset_expr.get_operators_reader() {
                                    let op = op?;
                                    if matches!(op, wasmparser::Operator::End) {
                                        continue;
                                    }
                                    instrs.push(translate(&op, &rebinder));
                                }
                                let offset = wasm_encoder::ConstExpr::extended(instrs);
                                sec.active(memory_index, &offset, d.data.iter().copied());
                            }
                        }
                    }
                    encoder.section(&sec);
                }

                Payload::CustomSection(c) => {
                    // Custom sections after code need code flushed first
                    if !code_flushed && !code_bodies.is_empty() {
                        self.emit_code_section(
                            &mut encoder,
                            &code_bodies,
                            &types,
                            &func_type_indices,
                            import_func_count,
                            flag_global_idx,
                            &rebinder,
                            orig_main_void_idx,
                            &orig_thread_start_indices,
                            fix_exit_import_idx,
                            handle_exit_import_idx,
                            main_void_wrapper_idx,
                            &thread_start_wrapper_indices,
                        )?;
                        code_flushed = true;
                    }
                    encoder.section(&wasm_encoder::CustomSection {
                        name: c.name().into(),
                        data: std::borrow::Cow::Borrowed(c.data()),
                    });
                }

                _ => {}
            }
        }

        // Flush code section if not already done
        if !code_flushed {
            self.emit_code_section(
                &mut encoder,
                &code_bodies,
                &types,
                &func_type_indices,
                import_func_count,
                flag_global_idx,
                &rebinder,
                orig_main_void_idx,
                &orig_thread_start_indices,
                fix_exit_import_idx,
                handle_exit_import_idx,
                main_void_wrapper_idx,
                &thread_start_wrapper_indices,
            )?;
        }

        Ok(encoder.finish())
    }
}

impl WrapUnreachablePreTargetStreamPass {
    /// Emits the code section with all original function bodies rewritten
    /// (unreachable replaced, call sites hooked) plus injected helper functions.
    #[allow(clippy::too_many_arguments)]
    fn emit_code_section(
        &self,
        encoder: &mut Module,
        code_bodies: &[wasmparser::FunctionBody<'_>],
        types: &[wasmparser::SubType],
        func_type_indices: &[u32],
        import_func_count: u32,
        flag_global_idx: u32,
        rebinder: &FuncRebinder,
        orig_main_void_idx: Option<u32>,
        orig_thread_start_indices: &std::collections::HashSet<u32>,
        fix_exit_import_idx: u32,
        handle_exit_import_idx: u32,
        main_void_wrapper_idx: Option<u32>,
        thread_start_wrapper_indices: &std::collections::HashMap<u32, u32>,
    ) -> Result<()> {
        let mut code_sec = CodeSection::new();
        let mut original_main_void_func = None;
        let mut original_thread_start_funcs = std::collections::HashMap::new();

        for (body_idx, body) in code_bodies.iter().enumerate() {
            let orig_idx = body_idx as u32 + import_func_count;

            let ty_idx = func_type_indices[body_idx];
            let ty = &types[ty_idx as usize];
            let return_types = match &ty.composite_type.inner {
                wasmparser::CompositeInnerType::Func(f) => f.results().to_vec(),
                _ => vec![],
            };

            let mut locals = Vec::new();
            for local in body.get_locals_reader()? {
                let local = local?;
                locals.push((local.0, translate_val_type(local.1, rebinder)));
            }
            let mut func = Function::new(locals);

            for op in body.get_operators_reader()? {
                let op = op?;
                match op {
                    wasmparser::Operator::Unreachable => {
                        func.instruction(&Instruction::I32Const(1));
                        func.instruction(&Instruction::GlobalSet(flag_global_idx));
                        push_dummy_return(&mut func, &return_types);
                    }
                    wasmparser::Operator::Call { function_index } => {
                        func.instruction(&Instruction::Call(rebinder.function(function_index)));
                        func.instruction(&Instruction::GlobalGet(flag_global_idx));
                        func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));
                        push_dummy_return(&mut func, &return_types);
                        func.instruction(&Instruction::End);
                    }
                    wasmparser::Operator::CallIndirect {
                        type_index,
                        table_index,
                        ..
                    } => {
                        func.instruction(&Instruction::CallIndirect {
                            type_index,
                            table_index,
                        });
                        func.instruction(&Instruction::GlobalGet(flag_global_idx));
                        func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));
                        push_dummy_return(&mut func, &return_types);
                        func.instruction(&Instruction::End);
                    }
                    _ => {
                        func.instruction(&translate(&op, rebinder));
                    }
                }
            }

            if Some(orig_idx) == orig_main_void_idx {
                original_main_void_func = Some(func);
                let mut wrapper = Function::new([(1, ValType::I32)]);
                wrapper.instruction(&Instruction::Call(main_void_wrapper_idx.unwrap()));
                wrapper.instruction(&Instruction::LocalSet(0));
                wrapper.instruction(&Instruction::GlobalGet(flag_global_idx));
                wrapper.instruction(&Instruction::If(wasm_encoder::BlockType::Result(
                    ValType::I32,
                )));
                wrapper.instruction(&Instruction::GlobalGet(flag_global_idx));
                wrapper.instruction(&Instruction::Call(fix_exit_import_idx));
                wrapper.instruction(&Instruction::Else);
                wrapper.instruction(&Instruction::LocalGet(0));
                wrapper.instruction(&Instruction::End);
                wrapper.instruction(&Instruction::End);
                code_sec.function(&wrapper);
            } else if orig_thread_start_indices.contains(&orig_idx) {
                original_thread_start_funcs.insert(orig_idx, func);
                let mut wrapper = Function::new([]);
                wrapper.instruction(&Instruction::LocalGet(0));
                wrapper.instruction(&Instruction::LocalGet(1));
                wrapper.instruction(&Instruction::Call(
                    *thread_start_wrapper_indices.get(&orig_idx).unwrap(),
                ));
                wrapper.instruction(&Instruction::GlobalGet(flag_global_idx));
                wrapper.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));
                wrapper.instruction(&Instruction::GlobalGet(flag_global_idx));
                wrapper.instruction(&Instruction::Call(handle_exit_import_idx));
                wrapper.instruction(&Instruction::End);
                wrapper.instruction(&Instruction::End);
                code_sec.function(&wrapper);
            } else {
                code_sec.function(&func);
            }
        }

        // ── Injected function bodies ──────────────────────────────────────

        // get_unreachable_flag: () -> i32
        let mut get_flag_func = Function::new([]);
        get_flag_func.instruction(&Instruction::GlobalGet(flag_global_idx));
        get_flag_func.instruction(&Instruction::End);
        code_sec.function(&get_flag_func);

        // set_unreachable_flag: (i32) -> ()
        let mut set_flag_func = Function::new([]);
        set_flag_func.instruction(&Instruction::LocalGet(0));
        set_flag_func.instruction(&Instruction::GlobalSet(flag_global_idx));
        set_flag_func.instruction(&Instruction::End);
        code_sec.function(&set_flag_func);

        if main_void_wrapper_idx.is_some() {
            code_sec.function(&original_main_void_func.unwrap());
        }
        for &orig_idx in orig_thread_start_indices {
            code_sec.function(original_thread_start_funcs.get(&orig_idx).unwrap());
        }

        encoder.section(&code_sec);
        Ok(())
    }
}