wasmgc 0.1.2

WasmGC types for Rust with a post-link toolchain
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
use super::{
    DataReader, FunctionExport, FunctionImport, Operation, Result, Schema, Struct, Value, root,
};
use crate::schema::{
    FUNCTION_BRIDGE_PREFIX, FUNCTION_DESC_PREFIX, FUNCTION_MAGIC, Layout, MAGIC_LEN,
    OPERATION_MAGIC, OperationKind, STRUCT_EXPORT_DESC_PREFIX, STRUCT_MAGIC, SchemaKind, ValueTag,
};
use core::ops::Range;
use std::collections::{HashMap, HashSet};
use wasmparser::{Operator, Parser, Payload};

pub struct Resolver<'a> {
    pub memory: DataReader<'a>,
    pub original_types: u32,
    pub original_globals: u32,
    pub original_global_imports: u32,
    pub original_tables: u32,
    pub original_function_imports: u32,
    pub new_function_start: u32,
    pub new_function_indices: Vec<Option<u32>>,
    pub new_global_start: u32,
    pub new_global_indices: Vec<Option<u32>>,
    pub new_data_count: u32,
    pub new_data_indices: Vec<u32>,
    pub new_data_parts: Vec<Vec<Range<usize>>>,
    pub type_groups: Vec<Vec<u64>>,
    pub type_indices: HashMap<u64, u32>,
    pub gc_table: Option<root::GcTable>,
    pub externref_table: Option<root::GcTable>,
    pub schema_set: HashMap<u64, Schema>,
    pub pending_resolve: Vec<u64>,
    pub discarded: Vec<Range<u64>>,
    pub operations: HashMap<u32, Operation>,
    pub intrinsics: Vec<(u32, u32, &'a str)>,
    pub struct_exports: Vec<Struct>,
    pub function_exports: Vec<FunctionExport>,
    pub function_imports: Vec<FunctionImport>,
}

impl<'a> Resolver<'a> {
    fn new(memory: DataReader<'a>) -> Self {
        Self {
            memory,
            original_types: 0,
            original_globals: 0,
            original_global_imports: 0,
            original_tables: 0,
            original_function_imports: 0,
            new_function_start: 0,
            new_function_indices: Vec::new(),
            new_global_start: 0,
            new_global_indices: Vec::new(),
            new_data_count: 0,
            new_data_indices: Vec::new(),
            new_data_parts: Vec::new(),
            type_groups: Vec::new(),
            type_indices: HashMap::new(),
            gc_table: None,
            externref_table: None,
            schema_set: HashMap::new(),
            pending_resolve: Vec::new(),
            discarded: Vec::new(),
            operations: HashMap::new(),
            intrinsics: Vec::new(),
            struct_exports: Vec::new(),
            function_exports: Vec::new(),
            function_imports: Vec::new(),
        }
    }

    pub fn parse(module: &'a [u8]) -> Result<Self> {
        let mut next_func_idx = 0;
        let mut removed_function_imports = 0;
        let mut original_function_imports = 0;
        let mut new_function_indices = Vec::new();
        let mut markers = HashSet::new();
        let mut marker_functions = HashMap::new();
        let mut intrinsics = Vec::new();
        let mut segments = Vec::new();
        let mut memory_type = None;
        let mut original_types = 0;
        let mut original_globals = 0;
        let mut original_global_imports = 0;
        let mut original_tables = 0;
        let mut global_addresses = HashMap::new();
        let mut struct_descriptors = Vec::new();
        let mut function_descriptors = Vec::new();
        let mut function_bridges = HashMap::new();
        let mut export_names = HashSet::new();

        for payload in Parser::new(0).parse_all(module) {
            match payload? {
                Payload::TypeSection(section_limited) => {
                    for section in section_limited {
                        original_types += section?.types().len() as u32;
                    }
                }
                Payload::ImportSection(section_limited) => {
                    for section in section_limited {
                        for import in section? {
                            let (_, import) = import?;
                            match import.ty {
                                wasmparser::TypeRef::Func(ty)
                                | wasmparser::TypeRef::FuncExact(ty) => {
                                    if import.module == "wasmgc" {
                                        removed_function_imports += 1;
                                        new_function_indices.push(None);
                                        if import.name.starts_with("marker") {
                                            assert!(matches!(
                                                import.ty,
                                                wasmparser::TypeRef::Func(_)
                                            ));
                                            markers.insert(next_func_idx);
                                        } else if matches!(
                                            import.name,
                                            "ref_clone"
                                                | "ref_release"
                                                | "ref_eq"
                                                | "externref_null"
                                                | "externref_is_null"
                                                | "externref_clone"
                                                | "externref_release"
                                        ) {
                                            intrinsics.push((next_func_idx, ty, import.name));
                                        }
                                    } else {
                                        new_function_indices
                                            .push(Some(next_func_idx - removed_function_imports));
                                    }
                                    original_function_imports += 1;
                                    next_func_idx += 1;
                                }
                                wasmparser::TypeRef::Memory(t) => {
                                    memory_type.get_or_insert(t);
                                }
                                wasmparser::TypeRef::Global(_) => {
                                    original_globals += 1;
                                    original_global_imports += 1;
                                }
                                wasmparser::TypeRef::Table(_) => original_tables += 1,
                                _ => (),
                            }
                        }
                    }
                }
                Payload::TableSection(section_limited) => {
                    original_tables += section_limited.count()
                }
                Payload::MemorySection(section_limited) => {
                    for section in section_limited {
                        memory_type.get_or_insert(section?);
                    }
                }
                Payload::GlobalSection(section_limited) => {
                    for global in section_limited {
                        let global = global?;
                        let mut operators = global.init_expr.get_operators_reader();
                        let address = match operators.read()? {
                            Operator::I32Const { value } => Some(u64::from(value as u32)),
                            Operator::I64Const { value } => Some(value as u64),
                            _ => None,
                        };
                        if let Some(address) = address
                            && matches!(operators.read()?, Operator::End)
                        {
                            global_addresses.insert(original_globals, address);
                        }
                        original_globals += 1;
                    }
                }
                Payload::ExportSection(section_limited) => {
                    for export in section_limited {
                        let export = export?;
                        if export.kind == wasmparser::ExternalKind::Global
                            && export.name.starts_with(STRUCT_EXPORT_DESC_PREFIX)
                        {
                            let address = *global_addresses.get(&export.index).ok_or(
                                "wasmgc struct descriptor requires a constant global address",
                            )?;
                            struct_descriptors.push((export.index, address));
                        } else if export.kind == wasmparser::ExternalKind::Global
                            && let Some(name) = export.name.strip_prefix(FUNCTION_DESC_PREFIX)
                        {
                            let address = *global_addresses.get(&export.index).ok_or(
                                "wasmgc function descriptor requires a constant global address",
                            )?;
                            function_descriptors.push((export.index, name, address));
                        } else if export.kind == wasmparser::ExternalKind::Func
                            && let Some(name) = export.name.strip_prefix(FUNCTION_BRIDGE_PREFIX)
                        {
                            function_bridges.insert(name, export.index);
                        } else {
                            export_names.insert(export.name.to_owned());
                        }
                    }
                }
                Payload::DataSection(section_limited) => {
                    for section in section_limited {
                        let data = section?;
                        let segment = if let wasmparser::DataKind::Active {
                            memory_index: 0,
                            offset_expr,
                        } = data.kind
                        {
                            let mut operator = offset_expr.get_operators_reader();
                            let offset = match operator.read()? {
                                Operator::I32Const { value } => u64::from(value as u32),
                                Operator::I64Const { value } => value as u64,
                                _ => return Err("wasmgc requires a statically known offset for active data in memory zero".into()),
                            };
                            if !matches!(operator.read()?, Operator::End) {
                                return Err(
                                    "expected end after data segment offset constant".into()
                                );
                            }
                            Some((offset, data.data))
                        } else {
                            None
                        };
                        segments.push(segment);
                    }
                }
                Payload::CodeSectionEntry(function_body) => {
                    new_function_indices.push(Some(next_func_idx - removed_function_imports));

                    // No marker
                    if markers.is_empty() {
                        next_func_idx += 1;
                        continue;
                    }

                    let mut may_addresses = vec![];
                    let mut call_marker = false;
                    let mut operators = function_body.get_operators_reader()?;

                    while !operators.eof() {
                        match operators.read()? {
                            Operator::Call { function_index }
                            | Operator::ReturnCall { function_index } => {
                                if markers.contains(&function_index) {
                                    call_marker = true;
                                }
                            }
                            Operator::I32Const { value } => {
                                may_addresses.push(u64::from(value as u32));
                            }
                            Operator::I64Const { value } => {
                                may_addresses.push(value as u64);
                            }
                            _ => (),
                        }
                    }

                    if call_marker {
                        marker_functions.insert(next_func_idx, may_addresses);
                    }

                    next_func_idx += 1;
                }
                _ => continue,
            }
        }

        let memory = DataReader {
            layout: if memory_type.is_some_and(|memory| memory.memory64) {
                Layout::WASM64
            } else {
                Layout::WASM32
            },
            segments,
        };

        let mut resolver = Self::new(memory);
        resolver.original_types = original_types;
        resolver.original_globals = original_globals;
        resolver.original_global_imports = original_global_imports;
        resolver.original_tables = original_tables;
        resolver.original_function_imports = original_function_imports;

        resolver.new_function_start = next_func_idx - removed_function_imports;
        resolver.new_function_indices = new_function_indices;
        resolver.intrinsics = intrinsics;
        resolver.resolve(
            marker_functions,
            struct_descriptors,
            function_descriptors,
            function_bridges,
            export_names,
        )?;

        Ok(resolver)
    }

    fn discard(&mut self, address: u64, size: u64) -> Result<()> {
        let size = size.next_multiple_of(u64::from(self.memory.layout.pointer_size));
        let end = address
            .checked_add(size)
            .ok_or("wasmgc descriptor address overflow")?;
        self.discarded.push(address..end);
        Ok(())
    }

    fn schema(&mut self, address: u64) -> Result<()> {
        if self.schema_set.contains_key(&address) {
            return Ok(());
        }

        let layout = self.memory.layout;
        let kind = SchemaKind::from_raw(self.memory.read_u32(address, layout.schema_kind_offset)?)
            .ok_or("invalid wasmgc schema kind")?;

        self.schema_set.insert(
            address,
            Schema {
                kind,
                fields: Vec::new(),
            },
        );

        self.pending_resolve.push(address);

        Ok(())
    }

    fn placeholder(&mut self, function: u32, may_addresses: Vec<u64>) -> Result<()> {
        let layout = self.memory.layout;
        for address in may_addresses {
            let Ok(magic) = self.memory.read::<MAGIC_LEN>(address) else {
                continue;
            };
            match magic {
                OPERATION_MAGIC => {
                    self.discard(address, u64::from(layout.operation_size))?;

                    let kind = OperationKind::from_raw(
                        self.memory
                            .read_u32(address, layout.operation_kind_offset)?,
                    )
                    .ok_or("invalid wasmgc operation kind")?;
                    let schema_addr = self
                        .memory
                        .pointer(address, layout.operation_schema_offset)?;
                    self.schema(schema_addr)?;

                    let operation = Operation {
                        schema_addr,
                        kind,
                        field_index: self
                            .memory
                            .read_u32(address, layout.operation_field_index_offset)?,
                    };
                    self.operations.insert(function, operation);
                    break;
                }
                FUNCTION_MAGIC => {
                    let (params, result, module, name) = self.function_signature(address)?;
                    self.function_imports.push(FunctionImport {
                        original_function_index: function,
                        function_index: 0,
                        type_index: 0,
                        params,
                        result,
                        module,
                        name,
                    });
                    break;
                }
                _ => continue,
            };
        }

        Ok(())
    }

    fn struct_export(&mut self, address: u64) -> Result<Struct> {
        let layout = self.memory.layout;
        if self
            .memory
            .read_at::<MAGIC_LEN>(address, layout.struct_export_magic_offset)?
            != STRUCT_MAGIC
        {
            return Err("invalid wasmgc struct export descriptor".into());
        }

        let kind = OperationKind::from_raw(
            self.memory
                .read_u32(address, layout.struct_export_kind_offset)?,
        )
        .filter(|kind| {
            matches!(
                kind,
                OperationKind::StructNew | OperationKind::StructGet | OperationKind::StructSet
            )
        })
        .ok_or("invalid wasmgc struct export kind")?;
        let schema_addr = self
            .memory
            .pointer(address, layout.struct_export_schema_offset)?;
        self.schema(schema_addr)?;

        let name_len = self
            .memory
            .read_u32(address, layout.struct_export_name_len_offset)?;
        let name_address = self
            .memory
            .address(address, u64::from(layout.struct_export_name_offset))?;
        self.discard(
            address,
            u64::from(layout.struct_export_name_offset) + u64::from(name_len),
        )?;

        Ok(Struct {
            schema_addr,
            kind,
            field_index: self
                .memory
                .read_u32(address, layout.struct_export_field_offset)?,
            name: self.memory.string(name_address, name_len)?,
            function_index: 0,
        })
    }

    fn function_signature(
        &mut self,
        address: u64,
    ) -> Result<(Vec<Value>, Option<Value>, String, String)> {
        let layout = self.memory.layout;
        if self
            .memory
            .read_at::<MAGIC_LEN>(address, layout.function_magic_offset)?
            != FUNCTION_MAGIC
        {
            return Err("invalid wasmgc function descriptor".into());
        }

        let param_count = self
            .memory
            .read_u32(address, layout.function_param_count_offset)?;
        let result_count = self
            .memory
            .read_u32(address, layout.function_result_count_offset)?;
        let module_len = self
            .memory
            .read_u32(address, layout.function_module_len_offset)?;
        let name_len = self
            .memory
            .read_u32(address, layout.function_name_len_offset)?;
        let params_address = self
            .memory
            .address(address, u64::from(layout.function_params_offset))?;
        let params = self.values(params_address, param_count)?;
        let result_offset = u64::from(layout.function_params_offset)
            + u64::from(param_count) * u64::from(layout.value_kind_size);
        let result = match result_count {
            0 => None,
            1 => Some(self.value(self.memory.address(address, result_offset)?)?),
            _ => return Err("invalid wasmgc function result count".into()),
        };
        let module_offset =
            result_offset + u64::from(result_count) * u64::from(layout.value_kind_size);
        let module_address = self.memory.address(address, module_offset)?;
        let module = self.memory.string(module_address, module_len)?;
        let name_offset = module_offset + u64::from(module_len);
        let name_address = self.memory.address(address, name_offset)?;
        let name = self.memory.string(name_address, name_len)?;
        self.discard(address, name_offset + u64::from(name_len))?;

        Ok((params, result, module, name))
    }

    fn function_export(&mut self, address: u64, function: u32) -> Result<FunctionExport> {
        let (params, result, _, name) = self.function_signature(address)?;
        Ok(FunctionExport {
            original_function_index: function,
            function_index: 0,
            params,
            result,
            name,
        })
    }

    fn value(&mut self, address: u64) -> Result<Value> {
        let layout = self.memory.layout;
        let tag = ValueTag::from_raw(
            self.memory
                .read_u32(address, layout.value_kind_tag_offset)?,
        )
        .ok_or("invalid wasmgc value tag")?;

        let target_addr = if matches!(
            tag,
            ValueTag::Ref | ValueTag::NullableRef | ValueTag::Array | ValueTag::NullableArray
        ) {
            let addr = self
                .memory
                .pointer(address, layout.value_kind_target_offset)?;
            self.schema(addr)?;
            Some(addr)
        } else {
            None
        };

        Ok(Value { tag, target_addr })
    }

    fn values(&mut self, address: u64, count: u32) -> Result<Vec<Value>> {
        let size = self.memory.layout.value_kind_size;
        let mut values = Vec::with_capacity(count as usize);
        for index in 0..count {
            let address = address
                .checked_add(u64::from(index) * u64::from(size))
                .ok_or("wasmgc values address overflow")?;
            values.push(self.value(address)?);
        }

        Ok(values)
    }

    pub fn resolve_data(&mut self) -> super::Result<()> {
        self.discarded.sort_unstable_by_key(|range| range.start);
        let mut merged: Vec<Range<u64>> = Vec::new();
        for range in self.discarded.drain(..) {
            if range.is_empty() {
                continue;
            }
            if let Some(previous) = merged.last_mut()
                && range.start <= previous.end
            {
                previous.end = previous.end.max(range.end);
            } else {
                merged.push(range);
            }
        }
        self.discarded = merged;

        let mut indices = Vec::with_capacity(self.memory.segments.len());
        let mut parts = Vec::with_capacity(self.memory.segments.len());
        let mut count = 0;
        for segment in &self.memory.segments {
            indices.push(count);
            let kept = if let Some((offset, bytes)) = segment {
                let end = self.memory.address(*offset, bytes.len() as u64)?;
                split(*offset..end, &self.discarded)
            } else {
                Vec::new()
            };
            count += kept.len().max(1) as u32;
            parts.push(kept);
        }

        self.new_data_indices = indices;
        self.new_data_count = count;
        self.new_data_parts = parts;
        Ok(())
    }

    fn resolve_globals(&mut self, discarded: &HashSet<u32>) {
        self.new_global_indices.clear();
        self.new_global_start = 0;
        for index in 0..self.original_globals {
            if discarded.contains(&index) {
                self.new_global_indices.push(None);
            } else {
                self.new_global_indices.push(Some(self.new_global_start));
                self.new_global_start += 1;
            }
        }
    }

    fn resolve_imports(&mut self) {
        self.function_imports
            .sort_unstable_by_key(|import| import.original_function_index);
        let count = self.function_imports.len() as u32;
        let start = self.new_function_indices[..self.original_function_imports as usize]
            .iter()
            .filter(|index| index.is_some())
            .count() as u32;
        for index in self
            .new_function_indices
            .iter_mut()
            .skip(self.original_function_imports as usize)
            .flatten()
        {
            *index += count;
        }
        self.new_function_start += count;
        for (offset, import) in self.function_imports.iter_mut().enumerate() {
            import.function_index = start + offset as u32;
            import.type_index =
                self.original_types + self.type_indices.len() as u32 + offset as u32;
        }
    }

    fn resolve_intrinsics(&mut self) {
        let start = self.new_function_start
            + u32::from(self.gc_table.is_some())
            + u32::from(self.externref_table.is_some());
        for (offset, &(function, _, _)) in self.intrinsics.iter().enumerate() {
            self.new_function_indices[function as usize] = Some(start + offset as u32);
        }
    }

    fn resolve_struct(&mut self) {
        let start = self.new_function_start
            + u32::from(self.gc_table.is_some())
            + u32::from(self.externref_table.is_some())
            + self.intrinsics.len() as u32;
        for (offset, export) in self.struct_exports.iter_mut().enumerate() {
            export.function_index = start + offset as u32;
        }
    }

    fn resolve_functions(&mut self) {
        let mut next = self.new_function_start
            + u32::from(self.gc_table.is_some())
            + u32::from(self.externref_table.is_some())
            + self.intrinsics.len() as u32
            + self.struct_exports.len() as u32;
        for export in &mut self.function_exports {
            if export
                .params
                .iter()
                .chain(&export.result)
                .any(|value| value.tag.is_reference())
            {
                export.function_index = next;
                next += 1;
            } else {
                export.function_index = self.new_function_indices
                    [export.original_function_index as usize]
                    .expect("retained Rust export bridge");
            }
        }
    }

    fn resolve(
        &mut self,
        marker_functions: HashMap<u32, Vec<u64>>,
        struct_descriptors: Vec<(u32, u64)>,
        function_descriptors: Vec<(u32, &str, u64)>,
        function_bridges: HashMap<&str, u32>,
        mut export_names: HashSet<String>,
    ) -> Result<()> {
        let layout = self.memory.layout;

        for (function, may_addresses) in marker_functions {
            self.placeholder(function, may_addresses)?;
        }

        let mut descriptor_globals = struct_descriptors
            .iter()
            .map(|(index, _)| *index)
            .collect::<HashSet<_>>();
        for (_, address) in struct_descriptors {
            let export = self.struct_export(address)?;
            if !export_names.insert(export.name.clone()) {
                return Err(format!("duplicate Wasm export name `{}`", export.name).into());
            }
            self.struct_exports.push(export);
        }

        for (global, name, address) in function_descriptors {
            let function = *function_bridges
                .get(name)
                .ok_or("wasmgc function descriptor has no bridge")?;
            let export = self.function_export(address, function)?;
            if !export_names.insert(export.name.clone()) {
                return Err(format!("duplicate Wasm export name `{}`", export.name).into());
            }
            descriptor_globals.insert(global);
            self.function_exports.push(export);
        }

        while let Some(address) = self.pending_resolve.pop() {
            let count = self
                .memory
                .read_u32(address, layout.schema_field_count_offset)?;

            let fields_address = self
                .memory
                .address(address, u64::from(layout.schema_fields_offset))?;
            let fields = self.values(fields_address, count)?;
            self.discard(
                address,
                u64::from(layout.schema_fields_offset)
                    + u64::from(count) * u64::from(layout.value_kind_size),
            )?;
            self.schema_set.get_mut(&address).unwrap().fields = fields;
        }

        self.resolve_types();
        self.resolve_globals(&descriptor_globals);
        self.resolve_imports();
        self.resolve_tables();
        self.resolve_intrinsics();
        self.resolve_struct();
        self.resolve_functions();
        self.resolve_data()
    }
}

fn split(segment: Range<u64>, discarded: &[Range<u64>]) -> Vec<Range<usize>> {
    let mut parts = Vec::new();
    let mut cursor = segment.start;
    let first = discarded.partition_point(|range| range.end <= segment.start);

    for range in &discarded[first..] {
        if range.start >= segment.end {
            break;
        }
        if cursor < range.start {
            parts.push((cursor - segment.start) as usize..(range.start - segment.start) as usize);
        }
        cursor = range.end.min(segment.end);
    }

    if cursor < segment.end {
        parts.push((cursor - segment.start) as usize..(segment.end - segment.start) as usize);
    }
    if parts.is_empty() {
        // Preserve the original active segment's index, even after removing all its bytes.
        parts.push(0..0);
    }
    parts
}