# Code segfaulting in bytecode compiler but running in native compiler

**URL:** <https://discuss.ocaml.org/t/code-segfaulting-in-bytecode-compiler-but-running-in-native-compiler/7622>\
**Category:** Learning\
**Created:** [April 8, 2021, 1:04am UTC](https://discuss.ocaml.org/t/code-segfaulting-in-bytecode-compiler-but-running-in-native-compiler/7622 "2021-04-08T01:04:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [April 8, 2021, 1:04am UTC](https://discuss.ocaml.org/t/code-segfaulting-in-bytecode-compiler-but-running-in-native-compiler/7622/1 "2021-04-08T01:04:55Z")

</div>

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/object\_fold.ml at 453837994b81188def0591b8ee6be2d3e3cd908a · josevalim/nested-data-structure-traversal · GitHub](https://github.com/josevalim/nested-data-structure-traversal/blob/453837994b81188def0591b8ee6be2d3e3cd908a/ocaml/object_fold.ml)

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/

---

<div class="post-metadata">

**Author:** ![vlaviron](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@vlaviron](https://discuss.ocaml.org/u/vlaviron)\
**Post date:** [April 8, 2021, 6:50am UTC](https://discuss.ocaml.org/t/code-segfaulting-in-bytecode-compiler-but-running-in-native-compiler/7622/2 "2021-04-08T06:50:57Z")

</div>

Hi,

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.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [April 8, 2021, 7:21am UTC](https://discuss.ocaml.org/t/code-segfaulting-in-bytecode-compiler-but-running-in-native-compiler/7622/3 "2021-04-08T07:21:45Z")

</div>

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

```auto
let x =
  object
    method f = ""
  end

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

```

---

<div class="post-metadata">

**Author:** ![silene](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/silene/32/2707_2.png) [@silene](https://discuss.ocaml.org/u/silene)\
**Post date:** [April 8, 2021, 7:22am UTC](https://discuss.ocaml.org/t/code-segfaulting-in-bytecode-compiler-but-running-in-native-compiler/7622/4 "2021-04-08T07:22:37Z")

</div>

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

```ocaml
method name = lesson#name

```

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

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [April 8, 2021, 7:32am UTC](https://discuss.ocaml.org/t/code-segfaulting-in-bytecode-compiler-but-running-in-native-compiler/7622/5 "2021-04-08T07:32:45Z")

</div>

> [@silene](#):
>
> methods that are an immediate delegate to methods with the exact same name.

Not sure, I have the same segfault with

```auto
let x =
  object
    method g = ""
  end

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

```

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [April 8, 2021, 8:50am UTC](https://discuss.ocaml.org/t/code-segfaulting-in-bytecode-compiler-but-running-in-native-compiler/7622/6 "2021-04-08T08:50:42Z")

</div>

I posted a bug report upstream [Segfault under bytecode in method call that directly calls another method · Issue #10324 · ocaml/ocaml · GitHub](https://github.com/ocaml/ocaml/issues/10324)

---

<div class="post-metadata">

**Author:** ![silene](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/silene/32/2707_2.png) [@silene](https://discuss.ocaml.org/u/silene)\
**Post date:** [April 8, 2021, 9:25am UTC](https://discuss.ocaml.org/t/code-segfaulting-in-bytecode-compiler-but-running-in-native-compiler/7622/7 "2021-04-08T09:25:57Z")

</div>

> [@nojb](#):
>
> Not sure, I have the same segfault with

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`.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [April 8, 2021, 4:53pm UTC](https://discuss.ocaml.org/t/code-segfaulting-in-bytecode-compiler-but-running-in-native-compiler/7622/8 "2021-04-08T16:53:18Z")

</div>

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!
