caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Stephen Dolan <stephen.dolan@cl.cam.ac.uk>
To: "Christoph Höger" <christoph.hoeger@celeraone.com>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] How is the frametable handled when throwing exceptions?
Date: Mon, 13 Feb 2017 15:13:00 +0000	[thread overview]
Message-ID: <CA+mHimMjYTQttH0CZ3ti6doVLM+pR1yhFR_Z9OZGes4Z7VCWYQ@mail.gmail.com> (raw)
In-Reply-To: <CAOazmvtjUQo9ksbq=ox8EqbC25vE6oJbD08LzEPd_V1K=xfsBw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2202 bytes --]

On 11 February 2017 at 14:02, Christoph Höger <
christoph.hoeger@celeraone.com> wrote:

> Dear all,
>
> while studying camls native code generation and garbage collection
> strategy, I came upon Connor Benner's bachelor thesis from 2012, where he
> implemented a llvm backend for ocamlopt. One intriguing remark mentioned
> OCamls exception mechanism as basically consisting a pointer to the stack
> frame of the last exception handler in a special register (r14, when I
> recall correctly). Throwing an exception that becomes a mov/pop/ret
> operation. This however, seems to interfere with garbage collection: From
> the C-API, it seems that all local roots are stored in the frametable via
> some special macros (e.g. CAMLParam, CAMLlocal).
>
> When control just returns from a stack frame, how are the entries removed
> from the frametable?
>

The frametable is a static data structure, which is a hashtable mapping
return addresses in OCaml functions to sets of stack offsets. When the GC
wants to traverse the stack, it walks the stack, picking out the return
address for each stack frame and looking them up in the frametable to find
out which stack slots are live. When OCaml code raises an exception, it
simply discards the stack up to the exception handler.

Code using the C API registers local roots in a different data structure
(caml_local_roots) using the CAMLlocal/param macros. When C code raises an
exception using caml_raise (see asmrun/fail.c), local roots are popped off
caml_local_roots one by one before raising the exception.

The tricky case is when OCaml code raises an exception, but there are C
frames between the top of the stack and the exception handler. To handle
this case, the implementation of caml_callback (calling OCaml from C)
unconditionally adds an exception handler. Should this exception handler
catch anything, it's rethrown with caml_raise.

The effect of this is that if an exception is thrown and the stack has a
mixture of OCaml and C frames before the exception handler, then the
exception will propagate by alternately cutting off OCaml segments of the
stack, and unwinding C segments of the stack.

Stephen

[-- Attachment #2: Type: text/html, Size: 2650 bytes --]

      parent reply	other threads:[~2017-02-13 15:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-11 14:02 Christoph Höger
2017-02-11 14:10 ` Nicolás Ojeda Bär
2017-02-12 13:25   ` Christoph Höger
2017-02-12 14:55     ` Nicolás Ojeda Bär
2017-02-13 15:13 ` Stephen Dolan [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+mHimMjYTQttH0CZ3ti6doVLM+pR1yhFR_Z9OZGes4Z7VCWYQ@mail.gmail.com \
    --to=stephen.dolan@cl.cam.ac.uk \
    --cc=caml-list@inria.fr \
    --cc=christoph.hoeger@celeraone.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).