From 57b700e5c883b3b5e3cfbb2e45948d1f46bb900b Mon Sep 17 00:00:00 2001 From: Zhicheng HUI Date: Wed, 31 Jul 2024 17:30:31 +0200 Subject: [PATCH] add custom section to binary module --- src/ast/binary.ml | 6 ++++ src/ast/binary_to_text.ml | 3 +- src/parser/binary_parser.ml | 59 ++++++++++++++++++++--------------- src/text_to_binary/rewrite.ml | 1 + 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/ast/binary.ml b/src/ast/binary.ml index aea8437c1..81871c39c 100644 --- a/src/ast/binary.ml +++ b/src/ast/binary.ml @@ -48,6 +48,10 @@ type elem = ; mode : elem_mode } +type custom = + | Uninterpreted of (string, Sexp.t) Either.t + | Contract of binary Contract.t + type modul = { id : string option ; types : binary rec_type Named.t @@ -60,6 +64,7 @@ type modul = ; data : data Named.t ; exports : exports ; start : int option + ; custom : custom list } let empty_modul = @@ -73,4 +78,5 @@ let empty_modul = ; data = Named.empty ; exports = { global = []; mem = []; table = []; func = [] } ; start = None + ; custom = [] } diff --git a/src/ast/binary_to_text.ml b/src/ast/binary_to_text.ml index 7047cb863..57f550701 100644 --- a/src/ast/binary_to_text.ml +++ b/src/ast/binary_to_text.ml @@ -250,7 +250,8 @@ let from_exports (exports : Binary.exports) : Text.module_field list = let from_start = function None -> [] | Some n -> [ MStart (Raw n) ] let modul - { Binary.id; types; global; table; mem; func; elem; data; start; exports } = + { Binary.id; types; global; table; mem; func; elem; data; start; exports; _ } + = let fields = from_types types @ from_global global @ from_table table @ from_mem mem @ from_func func @ from_elem elem @ from_data data @ from_exports exports diff --git a/src/parser/binary_parser.ml b/src/parser/binary_parser.ml index 677c9494c..396f5c300 100644 --- a/src/parser/binary_parser.ml +++ b/src/parser/binary_parser.ml @@ -927,7 +927,7 @@ let parse_many_custom_section input = let sections_iterate (input : Input.t) = (* Custom *) - let* _custom_sections, input = parse_many_custom_section input in + let* custom_sections, input = parse_many_custom_section input in (* Type *) let* type_section, input = @@ -936,8 +936,8 @@ let sections_iterate (input : Input.t) = let type_section = Array.of_list type_section in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Imports *) let* import_section, input = @@ -945,8 +945,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Function *) let* function_section, input = @@ -954,8 +954,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Tables *) let* table_section, input = @@ -963,8 +963,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Memory *) let* memory_section, input = @@ -972,8 +972,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Globals *) let* global_section, input = @@ -982,8 +982,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Exports *) let* export_section, input = @@ -991,8 +991,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Start *) let* start_section, input = @@ -1002,8 +1002,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Elements *) let* element_section, input = @@ -1012,8 +1012,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Data_count *) let* data_count_section, input = @@ -1023,8 +1023,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Code *) let* code_section, input = @@ -1039,8 +1039,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in (* Data *) let+ data_section, input = @@ -1066,9 +1066,8 @@ let sections_iterate (input : Input.t) = in (* Custom *) - (* TODO: actually use the various custom sections *) - let* _custom_sections', input = parse_many_custom_section input in - let _custom_sections = _custom_sections @ _custom_sections' in + let* custom_sections', input = parse_many_custom_section input in + let custom_sections = custom_sections @ custom_sections' in let+ () = if not @@ Input.is_empty input then Error (`Msg "malformed section id") @@ -1210,6 +1209,13 @@ let sections_iterate (input : Input.t) = } in + (* Custom *) + let custom = + List.filter_map + (Option.map (fun x -> Uninterpreted (Either.Left x))) + custom_sections + in + { id = None ; types ; global @@ -1220,6 +1226,7 @@ let sections_iterate (input : Input.t) = ; start = start_section ; data ; exports + ; custom } let from_string content = diff --git a/src/text_to_binary/rewrite.ml b/src/text_to_binary/rewrite.ml index a2e419670..4f5ac748c 100644 --- a/src/text_to_binary/rewrite.ml +++ b/src/text_to_binary/rewrite.ml @@ -445,6 +445,7 @@ let modul (modul : Assigned.t) : Binary.modul Result.t = ; exports ; func ; start + ; custom = [] } in modul