Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 5e3df28

Browse files
authored
[Interpreter] Decode/encode the data count section (#59)
1 parent 1e03671 commit 5e3df28

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

interpreter/binary/decode.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ let id s =
485485
| 9 -> `ElemSection
486486
| 10 -> `CodeSection
487487
| 11 -> `DataSection
488+
| 12 -> `DataCountSection
488489
| _ -> error s (pos s) "invalid section id"
489490
) bo
490491

@@ -639,6 +640,12 @@ let data_section s =
639640
section `DataSection (vec (at memory_segment)) [] s
640641

641642

643+
(* DataCount section *)
644+
645+
let data_count_section s =
646+
section `DataCountSection (opt vu32 true) None s
647+
648+
642649
(* Custom section *)
643650

644651
let custom size s =
@@ -679,13 +686,18 @@ let module_ s =
679686
iterate custom_section s;
680687
let elems = elem_section s in
681688
iterate custom_section s;
689+
let data_count = data_count_section s in
690+
iterate custom_section s;
682691
let func_bodies = code_section s in
683692
iterate custom_section s;
684693
let data = data_section s in
685694
iterate custom_section s;
686695
require (pos s = len s) s (len s) "junk after last section";
687696
require (List.length func_types = List.length func_bodies)
688697
s (len s) "function and code section have inconsistent lengths";
698+
require
699+
(data_count = None || data_count = Some (Int32.of_int (List.length data)))
700+
s (len s) "data count and data section have inconsistent lengths";
689701
let funcs =
690702
List.map2 Source.(fun t f -> {f.it with ftype = t} @@ f.at)
691703
func_types func_bodies

interpreter/binary/encode.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ let encode m =
503503
let data_section data =
504504
section 11 (vec memory_segment) data (data <> [])
505505

506+
(* DataCount section *)
507+
508+
let data_count_section data =
509+
section 12 len (List.length data) (data <> [])
510+
506511
(* Module *)
507512

508513
let module_ m =
@@ -517,6 +522,7 @@ let encode m =
517522
export_section m.it.exports;
518523
start_section m.it.start;
519524
elem_section m.it.elems;
525+
data_count_section m.it.data;
520526
code_section m.it.funcs;
521527
data_section m.it.data
522528
end

test/core/custom.wast

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,13 @@
118118
)
119119
"length out of bounds"
120120
)
121+
122+
(assert_malformed
123+
(module binary
124+
"\00asm" "\01\00\00\00"
125+
"\05\03\01\00\01" ;; memory section
126+
"\0c\01\02" ;; data count section (2 segments)
127+
"\0b\06\01\00\41\00\0b\00" ;; data section (1 segment)
128+
)
129+
"data count and data section have inconsistent lengths"
130+
)

0 commit comments

Comments
 (0)