Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment: perf and allocation counter based idl decoder limiting #4624

Closed
wants to merge 36 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
4c48d22
perf counter based idl decoder limiting
crusso Jul 22, 2024
b7a4df2
meter skip; add test
crusso Jul 23, 2024
7c87222
more tests (some broken?)
crusso Jul 24, 2024
8d0e223
more tests
crusso Jul 25, 2024
868d7e6
improver earlier tests (reporting cycles)
crusso Jul 25, 2024
1831132
finish port of tests
crusso Jul 25, 2024
d2cb2d8
assert low cost
crusso Jul 25, 2024
4d31ba4
cleanup tests
crusso Jul 25, 2024
f63dfa8
radical refactoring
crusso Jul 25, 2024
cbb3d0b
update test output, refactr
crusso Jul 25, 2024
7c8955c
don't meter during destabilization
crusso Jul 25, 2024
f132d3b
up limits
crusso Jul 25, 2024
e6a75ed
fix test values acc to https://github.com/dfinity/candid/pull/564
crusso Jul 26, 2024
73a8667
cleanup test; add blurb
crusso Jul 26, 2024
443d366
add simulated (idl) perf counter on non-IC targets|
crusso Jul 26, 2024
adaea7c
set up registers just once, including for skipped arguments (tricky b…
crusso Aug 1, 2024
f9a8fdb
update test output
crusso Aug 1, 2024
809b5e2
Rust formatting
crusso Aug 1, 2024
97bea78
allowing option fields of type Null too, to pass extended candid-tests
crusso Aug 2, 2024
c15b6e2
update test output
crusso Aug 2, 2024
7053400
Update rts/motoko-rts/src/idl.rs
crusso Aug 2, 2024
b79337c
test we can decode reasonable binary sizes
crusso Aug 5, 2024
4f72342
set limit as multiple of payload size
crusso Aug 6, 2024
a65bfae
add test output
crusso Aug 6, 2024
0d16cfd
up the limit to 1K
crusso Aug 6, 2024
5c93cc3
add bias term
crusso Aug 6, 2024
f7878cf
update bench numbers
crusso Aug 6, 2024
de22e66
update idl-sub test to allow optional fields of type Null
crusso Aug 7, 2024
abcee1d
remove noisy output
crusso Aug 7, 2024
5639400
test get/setCandidLimits
crusso Aug 7, 2024
119a304
add candid setCandidLimits/getCandidLimits prims to override default …
crusso Aug 7, 2024
a607238
update bench numbers
crusso Aug 7, 2024
d0ac039
update fail test output
crusso Aug 7, 2024
021d726
mock setCandidLimits/getCandidLimits in interpreter
crusso Aug 7, 2024
835bf46
update test output
crusso Aug 7, 2024
41e224e
move vec-any-bomb.mo to drun for reliable trap messages
crusso Aug 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 61 additions & 12 deletions src/codegen/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5131,7 +5131,7 @@ module IC = struct
)

(* For debugging *)
let _compile_static_print env s =
let compile_static_print env s =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert before merge

Blob.lit_ptr_len env s ^^ print_ptr_len env

let ic_trap env = system_call env "trap"
Expand Down Expand Up @@ -6341,7 +6341,12 @@ module RTS_Exports = struct
E.add_export env (nr {
name = Lib.Utf8.decode "moc_stable_mem_set_version";
edesc = nr (FuncExport (nr moc_stable_mem_set_version_fi))
})
});

E.add_export env (nr {
name = Lib.Utf8.decode "idl_limit_check";
edesc = nr (FuncExport (nr (E.built_in env "idl_limit_check")))
})

end (* RTS_Exports *)

Expand Down Expand Up @@ -6531,15 +6536,7 @@ module MakeSerialization (Strm : Stream) = struct
G.i (GlobalGet (nr (E.get_global env "__typtbl_idltyps")))

module Registers = struct
let register_globals env =
E.add_global32 env "@@rel_buf_opt" Mutable 0l;
E.add_global32 env "@@data_buf" Mutable 0l;
E.add_global32 env "@@ref_buf" Mutable 0l;
E.add_global32 env "@@typtbl" Mutable 0l;
E.add_global32 env "@@typtbl_end" Mutable 0l;
E.add_global32 env "@@typtbl_size" Mutable 0l

let get_rel_buf_opt env =
let get_rel_buf_opt env =
G.i (GlobalGet (nr (E.get_global env "@@rel_buf_opt")))
let set_rel_buf_opt env =
G.i (GlobalSet (nr (E.get_global env "@@rel_buf_opt")))
Expand Down Expand Up @@ -6568,6 +6565,53 @@ module MakeSerialization (Strm : Stream) = struct
G.i (GlobalGet (nr (E.get_global env "@@typtbl_size")))
let set_typtbl_size env =
G.i (GlobalSet (nr (E.get_global env "@@typtbl_size")))

let get_limit_counter env =
G.i (GlobalGet (nr (E.get_global env "@@limit_counter")))
let set_limit_counter env =
G.i (GlobalSet (nr (E.get_global env "@@limit_counter")))

let get_instruction_limit env =
G.i (GlobalGet (nr (E.get_global env "@@instruction_limit")))
let set_instruction_limit env =
G.i (GlobalSet (nr (E.get_global env "@@instruction_limit")))

let idl_limit_check env =
G.i (Call (nr (E.built_in env "idl_limit_check")))

let register_globals env =
E.add_global32 env "@@rel_buf_opt" Mutable 0l;
E.add_global32 env "@@data_buf" Mutable 0l;
E.add_global32 env "@@ref_buf" Mutable 0l;
E.add_global32 env "@@typtbl" Mutable 0l;
E.add_global32 env "@@typtbl_end" Mutable 0l;
E.add_global32 env "@@typtbl_size" Mutable 0l;
E.add_global32 env "@@limit_counter" Mutable 32l;
E.add_global64 env "@@instruction_limit" Mutable 0L;
Func.define_built_in env "idl_limit_check" [] [] (fun env ->
(* IC.compile_static_print env "idl_limit_check" ^^ *)
match E.mode env with
| Flags.ICMode | Flags.RefMode ->
get_limit_counter env ^^
G.if0
begin (* non-zero: Decrement counter *)
get_limit_counter env ^^
compile_sub_const 1l ^^
set_limit_counter env
end
begin (* zero: check limit and reset counter *)
compile_unboxed_const 0l ^^
IC.performance_counter env ^^
get_instruction_limit env ^^
G.i (Compare (Wasm.Values.I32 I32Op.LeU)) ^^
E.else_trap_with env "IDL error: exceeded instruction limit" ^^
compile_unboxed_const 32l ^^
set_limit_counter env
end
| Flags.WASIMode | Flags.WasmMode -> G.nop

) (* TBC *)

end

open Typ_hash
Expand Down Expand Up @@ -7224,6 +7268,10 @@ module MakeSerialization (Strm : Stream) = struct
let get_typtbl_end = Registers.get_typtbl_end env in
let get_typtbl_size = Registers.get_typtbl_size env in

(* Check instruction limit *)

Registers.idl_limit_check env ^^

(* Check recursion depth (protects against empty record etc.) *)
(* Factor 2 because at each step, the expected type could go through one
level of opt that is not present in the value type
Expand Down Expand Up @@ -8009,7 +8057,8 @@ module MakeSerialization (Strm : Stream) = struct
get_ref_buf ^^ Registers.set_ref_buf env ^^
get_typtbl_ptr ^^ load_unskewed_ptr ^^ Registers.set_typtbl env ^^
get_maintyps_ptr ^^ load_unskewed_ptr ^^ Registers.set_typtbl_end env ^^
get_typtbl_size_ptr ^^ load_unskewed_ptr ^^ Registers.set_typtbl_size env
get_typtbl_size_ptr ^^ load_unskewed_ptr ^^ Registers.set_typtbl_size env ^^
compile_const_64 (-1L) ^^ Registers.set_instruction_limit env
end ^^
(* set up variable frame arguments *)
Stack.with_frame env "frame_ptr" 3l (fun () ->
Expand Down
Loading