From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id E9E167EE33 for ; Mon, 13 Feb 2017 16:13:02 +0100 (CET) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=stedolan@stedolan.net; spf=None smtp.mailfrom=stedolan@stedolan.net; spf=None smtp.helo=postmaster@mail-wm0-f43.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of stedolan@stedolan.net) identity=pra; client-ip=74.125.82.43; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="stedolan@stedolan.net"; x-sender="stedolan@stedolan.net"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of stedolan@stedolan.net) identity=mailfrom; client-ip=74.125.82.43; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="stedolan@stedolan.net"; x-sender="stedolan@stedolan.net"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wm0-f43.google.com) identity=helo; client-ip=74.125.82.43; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="stedolan@stedolan.net"; x-sender="postmaster@mail-wm0-f43.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3A81HM/ROjf385IUXaQQsl6mtUPXoX/o7sNwtQ0KIM?= =?us-ascii?q?zox0Iv3yrarrMEGX3/hxlliBBdydsKMYzbuN+P+5EUU7or+5+EgYd5JNUxJXwe?= =?us-ascii?q?43pCcHRPC/NEvgMfTxZDY7FskRHHVs/nW8LFQHUJ2mPw6arXK99yMdFQviPgRp?= =?us-ascii?q?OOv1BpTSj8Oq3Oyu5pHfeQtFiT6ybL9oKBi7owrdu8sLjYB/Nqs/1xzFr2dSde?=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P70+/J5zuyApADPjMOMVuLC3q9MtzcA/o=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0AjAQCyy6FYhitSfUpeGwEBAQMBAQEJA?= =?us-ascii?q?QEBFgEBAQMBAQEJAQEBhREHg1KcFJAKhzmJAwdCFQEBAQEBAQEBAQEBEgEBAQg?= =?us-ascii?q?LCwodL4IzGwGCGwEEASMdAQE3AQQLAQoEBzcCAiISAQUBHAYTiWIIBKF4P4saa?= =?us-ascii?q?IIlgwgBAQWEURqDSwwBHQgShjqEb4Qbgz+CX5VEhjOBf5AVi3iFDZFOFB6BFTW?= =?us-ascii?q?BIiAULkEXBYQagg11AYkgAYELAQEB?= X-IPAS-Result: =?us-ascii?q?A0AjAQCyy6FYhitSfUpeGwEBAQMBAQEJAQEBFgEBAQMBAQE?= =?us-ascii?q?JAQEBhREHg1KcFJAKhzmJAwdCFQEBAQEBAQEBAQEBEgEBAQgLCwodL4IzGwGCG?= =?us-ascii?q?wEEASMdAQE3AQQLAQoEBzcCAiISAQUBHAYTiWIIBKF4P4saaIIlgwgBAQWEURq?= =?us-ascii?q?DSwwBHQgShjqEb4Qbgz+CX5VEhjOBf5AVi3iFDZFOFB6BFTWBIiAULkEXBYQag?= =?us-ascii?q?g11AYkgAYELAQEB?= X-IronPort-AV: E=Sophos;i="5.35,156,1484002800"; d="scan'208,217";a="213052025" Received: from mail-wm0-f43.google.com ([74.125.82.43]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 13 Feb 2017 16:13:01 +0100 Received: by mail-wm0-f43.google.com with SMTP id r141so96428794wmg.1 for ; Mon, 13 Feb 2017 07:13:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stedolan-net.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=uPVh38fUkSOx6m+IdHalRBqydstjpv34UygWHsJInuQ=; b=p6I6KSH4Tsmnsuy694ML5Q8yNZPrPfeNz11IRWQosU3SlcJVaCaWIguv6e1IuKwYfe 1drEXboWS+bOXIt8OJdfCaJSKAu9M+RS0b1fZzkgeexOa4YLd3UJN7YQzCm150wggjqi MQOZjAETgwP7vaMVA82qbq21aUJzwBXhiSlDgukJOS79oLIO2vhPoZ+vJ25tw1cVgRac Re1lSCzD2Vr1pujlSpA74+X9VQ5jpJrlj0nMnlDGQJe9kG1ZKKL+2EJPWeNz9P5ZLpJS vitCK+i3xAuevWOCYXwkPHKx3q780qMKxeBVq4W1nynG6UH0OTebCehYNgmiR4YkBZuB 5BgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=uPVh38fUkSOx6m+IdHalRBqydstjpv34UygWHsJInuQ=; b=Oh7MK6St4wsPsw9TDcc89KMUw/erJ+bzTb82/LEWxVP1xe1ZJ271OB0epE7Qnu5mTn tnWf9oA/78qONAjL9CNomOvhxHQa4FoZbLK9gvGPWQEO8nZnFFLD7HQ7ay4+i+Mytmut dR23W/+Tu0yGns/J2ikSFdplI3sS8j50GK1hxG2V93UXQWpfYKL9qWlzsHetGzdIn5zU xgWVgy9QgJZlw/ITNVBSEyCds3P+JowtU6Ss7MnhV0euXTDNVab02sSclA8YAOeRKHT6 iS3L6/NNLYdQP4Ca/vZqCbHwc7umkgtpTnsPUEpmOVZuZhQJV0p0sYeABTKBoqOM4Mqh fjqw== X-Gm-Message-State: AMke39myakf6hL0H7vB6KR91MGeXn5giuCQlLDELSuRhzgUomO6rfnMA1bS7uam5WSg84a6y9NGsFBd/ogU9Bg== X-Received: by 10.28.189.135 with SMTP id n129mr18426885wmf.92.1486998781229; Mon, 13 Feb 2017 07:13:01 -0800 (PST) MIME-Version: 1.0 Sender: stedolan@stedolan.net Received: by 10.80.153.113 with HTTP; Mon, 13 Feb 2017 07:13:00 -0800 (PST) X-Originating-IP: [82.1.49.183] In-Reply-To: References: From: Stephen Dolan Date: Mon, 13 Feb 2017 15:13:00 +0000 X-Google-Sender-Auth: dXuAdpcWAEwuxxKfutdKNN8Aj9w Message-ID: To: =?UTF-8?Q?Christoph_H=C3=B6ger?= Cc: OCaml Mailing List Content-Type: multipart/alternative; boundary=001a114b10b4232e4005486ae21b Subject: Re: [Caml-list] How is the frametable handled when throwing exceptions? --001a114b10b4232e4005486ae21b Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 11 February 2017 at 14:02, Christoph H=C3=B6ger < 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 --001a114b10b4232e4005486ae21b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On 1= 1 February 2017 at 14:02, Christoph H=C3=B6ger <christoph.hoe= ger@celeraone.com> wrote:
<= div dir=3D"ltr">
Dear all,

while study= ing camls native code generation and garbage collection strategy, I came up= on Connor Benner's bachelor thesis from 2012, where he implemented a ll= vm backend for ocamlopt. One intriguing remark mentioned OCamls exception m= echanism as basically consisting a pointer to the stack frame of the last e= xception handler in a special register (r14, when I recall correctly). Thro= wing 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 lo= cal roots are stored in the frametable via some special macros (e.g. CAMLPa= ram, CAMLlocal).

When control just returns from a stack frame= , how are the entries removed from the frametable?

The frametable is a static data struct= ure, which is a hashtable mapping return addresses in OCaml functions to se= ts 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 co= de raises an exception, it simply discards the stack up to the exception ha= ndler.

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 th= e exception.

The tricky case is when OCaml code ra= ises 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_call= back (calling OCaml from C) unconditionally adds an exception handler. Shou= ld this exception handler catch anything, it's rethrown with caml_raise= .

The effect of this is that if an exception is th= rown and the stack has a mixture of OCaml and C frames before the exception= handler, then the exception will propagate by alternately cutting off OCam= l segments of the stack, and unwinding C segments of the stack.
<= br>
Stephen
--001a114b10b4232e4005486ae21b--