forked from acieroid/wassail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.ml
32 lines (30 loc) · 1.3 KB
/
cfg.ml
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
open Core
open Wassail
open Utils
let cfg =
Command.basic
~summary:"Generate a DOT file representing the CFG of function [fid] from the wat file [in], in file [out]"
Command.Let_syntax.(
let%map_open file_in = anon ("in" %: string)
and fid = anon ("fid" %: int32)
and file_out = anon ("out" %: string) in
fun () ->
on_cfg file_in fid (fun cfg -> output_to_file file_out (Cfg.to_dot cfg)))
let cfgs =
Command.basic
~summary:"Generate DOT files representing the CFG of each function defined in the wat file [in], and outputs them in the directory [out_dir]"
Command.Let_syntax.(
let%map_open file_in = anon ("in" %: string)
and out_dir = anon ("out_dir" %: string) in
fun () ->
let wasm_mod = Wasm_module.of_file file_in in
Core_unix.mkdir_p out_dir;
List.iteri wasm_mod.funcs
~f:(fun i _ ->
let faddr = Int32.(wasm_mod.nfuncimports + (Int32.of_int_exn i)) in
let t = Sys.time () in
let cfg = Cfg_builder.build wasm_mod faddr in
Printf.printf "CFG generation took: %fs\n" (Sys.time() -. t);
Out_channel.with_file (Printf.sprintf "%s/%ld.dot" out_dir faddr)
~f:(fun ch ->
Out_channel.output_string ch (Cfg.to_dot cfg))))