Skip to content

Instantly share code, notes, and snippets.

@philzook58
Last active December 31, 2021 13:21
Embed
What would you like to do?
Bap Stack overflow error in js_of_ocaml

Build with dune build main.bc.js --release Demonstrates stack overflow on dynlinking the bap_c library.

Troubleshooting is difficult. Unfortunately the overflow error is being caught and translated in a way that obscures the original cause. To find what I believe is an accurate stack trace do the following: Go into _build/default/main.bc.js. Add Error.stackTraceLimit = Infinity; to the top. Grep for RangeError. Add in console.log(e.stack); into the exception handler like so

     {if(e instanceof Array)return e;
      if
       (joo_global_object.RangeError
        &&
        e instanceof joo_global_object.RangeError
        &&
        e.message
        &&
        e.message.match(/maximum call stack/i)) {
        //console.trace()
        console.log(e.stack);

Maybe there is a better way of going about this?

You can run node --stack-size=5000 _build/default/main.bc.js to confirm it is a stack size issue.

(executable
(name main)
(modes byte js)
(flags -linkall -g)
(js_of_ocaml (flags --toplevel --dynlink +toplevel.js +dynlink.js --disable inline --pretty --source-map-inline)
(javascript_files helpers.js)
)
(libraries bap zarith_stubs_js
; comment out for main.bc build
js_of_ocaml-toplevel
))
open Core_kernel
open Js_of_ocaml_toplevel
let () = JsooTop.initialize ()
let () = Sys.interactive := false
let () = match Bap_main.init ~requires:["semantics"] ~argv:[|"bap"; "show-lisp"; "foo"; "--primus-lisp-load=test"|] ~log:(`Formatter Format.std_formatter) ~err:Format.std_formatter () with
| Ok () -> ()
| Error _s -> failwith "Something has gone awry"
(defun foo (x) (set x (+ x 3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment