Skip to content

Commit

Permalink
move some validation out of rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
zapashcanon committed Jul 4, 2024
1 parent a286e0a commit 655fb84
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 292 deletions.
2 changes: 1 addition & 1 deletion src/ast/binary.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type global =
type data_mode =
| Data_passive
(* TODO: Data_active binary+const expr*)
| Data_active of int option * binary expr
| Data_active of int * binary expr

type data =
{ id : string option
Expand Down
5 changes: 2 additions & 3 deletions src/ast/binary_encoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -604,16 +604,15 @@ let write_data buf ({ init; mode; _ } : data) =
| Data_passive ->
write_u32_of_int buf 1;
write_string buf init
| Data_active (Some 0, expr) ->
| Data_active (0, expr) ->
write_u32_of_int buf 0;
write_expr buf expr ~end_op_code:None;
write_string buf init
| Data_active (Some i, expr) ->
| Data_active (i, expr) ->
write_u32_of_int buf 2;
write_u32_of_int buf i;
write_expr buf expr ~end_op_code:None;
write_string buf init
| Data_active (None, _) -> assert false

let encode_section buf id encode_func data =
let section_buf = Buffer.create 16 in
Expand Down
3 changes: 1 addition & 2 deletions src/ast/binary_to_text.ml
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ let convert_data_mode (m : Binary.data_mode) : Text.data_mode =
match m with
| Data_passive -> Data_passive
| Data_active (i, e) ->
let i = Option.map (fun i -> Raw i) i in
let e = convert_expr e in
Data_active (i, e)
Data_active (Some (Raw i), e)

let convert_data (e : Binary.data) : Text.data =
let { Binary.id; init; mode } : Binary.data = e in
Expand Down
4 changes: 2 additions & 2 deletions src/ast/binary_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ let convert_heap_type tbl = function
Ok t
| Def_ht (Text i) -> begin
match tbl with
| None -> Error `Unknown_type
| None -> Error (`Unknown_type (Text i))
| Some tbl -> begin
match Hashtbl.find_opt tbl i with
| None -> Error `Unknown_type
| None -> Error (`Unknown_type (Text i))
| Some i -> ok @@ Def_ht (Raw i)
end
end
Expand Down
25 changes: 14 additions & 11 deletions src/bin/owi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,20 @@ let exit_code =
| `Unbound_name _id -> 38
| `Undeclared_function_reference -> 39
| `Unexpected_token _token -> 40
| `Unknown_function _id -> 41
| `Unknown_global -> 42
| `Unknown_import _ -> 43
| `Unknown_label -> 44
| `Unknown_local _id -> 45
| `Unknown_memory _id -> 46
| `Unknown_module _id -> 47
| `Unknown_operator -> 48
| `Unknown_type -> 49
| `Unsupported_file_extension _ext -> 50
| `Failed_with_but_expected (_got, _expected) -> 51
| `Unknown_data _id -> 41
| `Unknown_elem _id -> 42
| `Unknown_func _id -> 43
| `Unknown_global _id -> 44
| `Unknown_import _ -> 45
| `Unknown_label _id -> 46
| `Unknown_local _id -> 47
| `Unknown_memory _id -> 48
| `Unknown_module _id -> 49
| `Unknown_operator -> 50
| `Unknown_table _id -> 51
| `Unknown_type _id -> 52
| `Unsupported_file_extension _ext -> 53
| `Failed_with_but_expected (_got, _expected) -> 54
end
end
| Error e -> (
Expand Down
5 changes: 2 additions & 3 deletions src/link/link.ml
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ let active_elem_expr ~offset ~length ~table ~elem =
]

let active_data_expr ~offset ~length ~mem ~data =
if mem <> 0 then Error (`Unknown_memory mem)
if mem <> 0 then Error (`Unknown_memory (Raw mem))
else
Ok
[ I32_const offset
Expand All @@ -293,8 +293,7 @@ let define_data env data =
let env = Link_env.Build.add_data id data' env in
let* init =
match data.mode with
| Data_active (None, _) -> assert false
| Data_active (Some mem, offset) ->
| Data_active (mem, offset) ->
let* offset = Eval_const.expr env offset in
let length = Int32.of_int @@ String.length data.init in
let* offset = get_i32 offset in
Expand Down
4 changes: 2 additions & 2 deletions src/link/link_env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module Build = struct

let get_global (env : t) id =
match IMap.find_opt id env.globals with
| None -> Error `Unknown_global
| None -> Error (`Unknown_global (Raw id))
| Some v -> Ok v

let get_const_global (env : t) id =
Expand All @@ -133,7 +133,7 @@ module Build = struct

let get_func (env : t) id =
match IMap.find_opt id env.functions with
| None -> Error (`Unknown_function id)
| None -> Error (`Unknown_func (Raw id))
| Some v -> Ok v
end

Expand Down
14 changes: 5 additions & 9 deletions src/parser/binary_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -892,24 +892,20 @@ let read_code types input =
let read_data_active types input =
let* Raw index, input = read_indice input in
let+ offset, input = read_const types input in
(Data_active (Some index, offset), input)
(Data_active (index, offset), input)

let read_data_active_zero types input =
let+ offset, input = read_const types input in
(Data_active (Some 0, offset), input)
(Data_active (0, offset), input)

let read_data types memories input =
let read_data types input =
let* i, input = read_U32 input in
let id = None in
match i with
| 0 ->
let* mode, input = read_data_active_zero types input in
let* init, input = read_bytes ~msg:"read_data 0" input in
let+ init, input = read_bytes ~msg:"read_data 0" input in
let init = string_of_char_list init in
(* TODO: this should be removed once we do proper validation of binary modules *)
let+ () =
if List.is_empty memories then Error (`Unknown_memory 0) else Ok ()
in
({ id; init; mode }, input)
| 1 ->
let mode = Data_passive in
Expand Down Expand Up @@ -1052,7 +1048,7 @@ let sections_iterate (input : Input.t) =
(* Data *)
let+ data_section, input =
section_parse input ~expected_id:'\x0B' []
(vector_no_id (read_data type_section memory_section))
(vector_no_id (read_data type_section))
in

let* () =
Expand Down
2 changes: 1 addition & 1 deletion src/text_to_binary/assigned.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ let check_type_id (types : binary str_type Named.t) (check : Grouped.type_check)
in
(* TODO more efficient version of that *)
match Indexed.get_at id types.values with
| None -> Error `Unknown_type
| None -> Error (`Unknown_type (Raw id))
| Some (Def_func_t func_type') ->
let* func_type = Binary_types.convert_func_type None func_type in
if not (equal_func_types func_type func_type') then
Expand Down
Loading

0 comments on commit 655fb84

Please sign in to comment.