Code segfaulting in bytecode compiler but running in native compiler

I’m not sure if this should be a bug report yet, hence starting a discussion here. I just wrote some seemingly innocuous code (no FFI, no Obj.magic or other tricks) that should run fine (and does when compiled with the native compiler), but segfaults when run with ocaml.

The file in question may be found here: nested-data-structure-traversal/ at 453837994b81188def0591b8ee6be2d3e3cd908a · josevalim/nested-data-structure-traversal · GitHub

I will say that there is moderate usage of objects, lists, and folding. But nothing extreme (in my opinion). Specifically the line that causes the segfault is the last line:

let () = List.iter print_section sections

Anyone experienced anything like this before/


On my side your code runs fine in the toplevel (I’ve tested 4.11.1 and 4.12.0).
It’s hard to find what happens in your case without more details, but one possibility is that your .ocamlinit contains some code that interferes in some way. You can try ocaml -noinit and see if it changes anything.

It would be helpful if you could also mention whether the problem occurs with the bytecode compiler too or only the toplevel.

I was able to reproduce in 4.10.1 with the following code, but it crashes only if I invoke it via ocaml, it works fine if the code is typed into the toplevel.

let x =
    method f = ""

let _ =
  (object method f = x#f end)#f

As far as I can tell, the bytecode compiler is confused by methods like

method name = lesson#name

that is, methods that are an immediate delegate to methods with the exact same name.

Not sure, I have the same segfault with

let x =
    method g = ""

let _ =
  (object method f = x#g end)#f

I posted a bug report upstream Segfault under bytecode in method call that directly calls another method · Issue #10324 · ocaml/ocaml · GitHub


Indeed, the issue seems to occur for all the methods generated by CamlinternalOO.send_*. I was able to reproduce the segfault for at least send_const and send_env.

1 Like

Ah, indeed, this is on 4.10.0, running with ocaml. Thanks for confirming, sorry for the missing info. Reproducing the minimal example (segfault) on my end too. Thanks!