From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by c5ff346549e7 (Postfix) with ESMTPS id 07A085D4 for ; Thu, 20 Jun 2019 18:03:16 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.63,397,1557180000"; d="ml'?c'?scan'208";a="388391397" Received: from sympa.inria.fr ([193.51.193.213]) by mail2-relais-roc.national.inria.fr with ESMTP; 20 Jun 2019 20:03:15 +0200 Received: by sympa.inria.fr (Postfix, from userid 20132) id A1912826E1; Thu, 20 Jun 2019 20:03:15 +0200 (CEST) Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id E85598247D for ; Thu, 20 Jun 2019 20:03:11 +0200 (CEST) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=rixed@happyleptic.org; spf=Pass smtp.mailfrom=rixed@happyleptic.org; spf=Pass smtp.helo=postmaster@out1-smtp.messagingengine.com IronPort-PHdr: =?us-ascii?q?9a23=3AgKrVCBA9Dhvxb0E2514wUyQJP3N1i/DPJgcQr6Af?= =?us-ascii?q?oPdwSPv4r8bcNUDSrc9gkEXOFd2Cra4d0ayO7+u5AzVIoc7Y9ixbKdoUD15NoP?= =?us-ascii?q?5VtjRoONSCB0z/IayiRA0BN+MGamVY+WqmO1NeAsf0ag6aiHSz6TkPBke3blIt?= =?us-ascii?q?daz6FYHIksu4yf259YHNbAVUnjq9Zq55IAmroQnLucQanJVuJrwtxhbGrXdEZv?= =?us-ascii?q?payX51Ll6Xgxrw+9288ZF+/yhNof4t69JMXaDndKkkULJUCygrPXoo78PxrxnD?= =?us-ascii?q?SgWP5noYUmoIlxdDHhbI4hLnUJrvqyX2ruVy1jWUMs3wVrA0RC+t77x3Rx/yiS?= =?us-ascii?q?cILCA2/WfKgcFtlq1boRahpxtiw47IZYyeKfRzcr/Bcd4cWGFMWNtaWS5cDYOm?= =?us-ascii?q?d4YBCOQOMulboYfzp1UAswawChW3CePq1jNFnGX70Lcg3ug9FwzNwQwuH8gJsH?= =?us-ascii?q?TRtNj1LqYSUO+rw6bV1jjDb+lZ2S/l6InIdhAuv+mMXa5xccXN00UvEBnFjlqW?= =?us-ascii?q?pIf4PD2VzvwAvmma4uZ6SO6jlnMrpxt+rzS12Mshio/EjZ8PxF/e7yV22oM1KM?= =?us-ascii?q?W4SEFlZd6kF4NdtyaHN4tqX88uWnpouCMmyr0eo5K7eTYFyI88xxHFZPyHco6I?= =?us-ascii?q?4h39W+aXLjd4hXRld6ylixmu9kigz/XwVsiy0FlUsipIitjBu3EX2xDO98SLV+?= =?us-ascii?q?Fx80i71TqR1w3f8uRELlo1larfJZ4h2Lkwlp8LvETZHy/3l1v5g7KNe0Uk4Oeo?= =?us-ascii?q?8/znY7XippCCL490lgD+Pbg0lsy4G+Q4PRACX3KH9uSkyL3j4Ur5Ta1WgfIsl6?= =?us-ascii?q?nZtInWJcAapq6iHw9YyZ0j6ha6Dze+ytsUh3gHLFRfeBKGlYflIV/OIOr+APyi?= =?us-ascii?q?nVmslCprx+vaPrL/GJXDM3nDkbb/crtl90IPgDY0mNtW4pYRDrAaPNryXFXwvZ?= =?us-ascii?q?rWFEwXKQuxlvzmGNhx/oMXQXqeBqKBLKLZ91iS6bF8a9KQbZMY7W6uY8Mu4OTj?= =?us-ascii?q?2CdgxA0tOJKx1J5SU0iWW/RrJ0LCOCjrn8tYVHwPohJ4SeHxk1SNUDhcanC9Xq?= =?us-ascii?q?QxoDo8DdD/VNuRdsWWmLWEmRyDMNhOfGkfVQKMGG/wZoGJQ+sNY2SVOMAzymVV?= =?us-ascii?q?B4jkcJco0FSVjCG/zrNmKuTO/ShB6sDj3cBt9unejQ0z/npzFcvPimw=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0CBJACzyQtdhxkEb0JlHAEBAR8EAQEFA?= =?us-ascii?q?QGBRAcBAQMBgS0CAQEBAQGCGwEBMSiNEYl9gWibBQkBAwEHAQQvAQGHIBwGAQQ?= =?us-ascii?q?1BQ0BAwEBBAEBAgECAwETAQEBCA0JCCmKPgyCOiKDNQEBOGIqg2YBgRxuplKCI?= =?us-ascii?q?oJ5AQEFgjyDS4EgBwmBMgIBAQEBAQEBgUqDIIIWghKBBIFBF4F/gTgMiC6FFZQ?= =?us-ascii?q?7h06NagmFN4IcLoU8iCohlz0tpA2BaIF4fUMuGlaBToJKEQmDTYUUhUE9AQIwj?= =?us-ascii?q?3UBAQ?= X-IPAS-Result: =?us-ascii?q?A0CBJACzyQtdhxkEb0JlHAEBAR8EAQEFAQGBRAcBAQMBgS0?= =?us-ascii?q?CAQEBAQGCGwEBMSiNEYl9gWibBQkBAwEHAQQvAQGHIBwGAQQ1BQ0BAwEBBAEBA?= =?us-ascii?q?gECAwETAQEBCA0JCCmKPgyCOiKDNQEBOGIqg2YBgRxuplKCIoJ5AQEFgjyDS4E?= =?us-ascii?q?gBwmBMgIBAQEBAQEBgUqDIIIWghKBBIFBF4F/gTgMiC6FFZQ7h06NagmFN4IcL?= =?us-ascii?q?oU8iCohlz0tpA2BaIF4fUMuGlaBToJKEQmDTYUUhUE9AQIwj3UBAQ?= X-IronPort-AV: E=Sophos;i="5.63,397,1557180000"; d="ml'?c'?scan'208";a="388391383" X-MGA-submission: =?us-ascii?q?MDFo2ejrcL16A8EdonI4mPOgGsyVwdhQJXEwpm?= =?us-ascii?q?a3KplHYLZ0uTpyziy+2prOGFkl0k0q49lsmDz1VTPTBnbHU+nHyyWf7v?= =?us-ascii?q?4unYPhJTH76ysSenavvRWNy4W9TqPt4HBpJRbm4Rh+mDdV0Crbf9pMzi?= =?us-ascii?q?5C7WB+xqwAXYcHyAX2FbngPg=3D=3D?= Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Jun 2019 20:03:10 +0200 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 776F021DFB; Thu, 20 Jun 2019 14:03:08 -0400 (EDT) Received: from imap2 ([10.202.2.52]) by compute3.internal (MEProxy); Thu, 20 Jun 2019 14:03:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=happyleptic.org; h=mime-version:message-id:date:from:to:subject:content-type; s= fm3; bh=DuOKLHitOXyrcVuTvz+Z6HlNFj7j+j0aAhwVyRAmp5s=; b=FGmmEOTr mcFXXqPcDG7164es885EOHQ2X+Dsb81uESXnwG0rYHf3mlPjwNKnnk6DEPIAra+E g27FD1XPe2ElbFzAv1ME8V6ti1dpsOTSvzHoibAPre0arK3YEA9aier+o8I+pkl1 zip4/u0f21ROR8c65iY9u7L8GhEczyOsJnHKNKbIPpTwgKwPgl2fhbCxCXugsWLF BKa49MLCrtTrJwdTy8Fq1OO9B9JJTHct6gwV5bk9H7Xw/FdFQTYBpMbk3Z4gBuCq H0eCt2nI5v/dSf5QyNPdWa5ZgUcbF4kjT/K8lYvggTE4lTX8VzI9FhsMx4aU8X0k RB+q5bl9JZAbQQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=DuOKLHitOXyrcVuTvz+Z6HlNFj7j+ j0aAhwVyRAmp5s=; b=jG2ZGfK0MKzT1eZW0PkuYlqB468miEMkDChRd9ffbqShi bba1rXaD/vqXf1sso4fLmOLNr15StEbHeBFrNFY4gi4+cpecrJSsPf936jTPkGI4 qjoQSiLNRbLe1xutUoJVatSCS08OlRtjpUUGNTc0P+sRxnh6WF1fuM9+MCX/S6L5 hMD8yt4wAyJYG8Jew0r/ZJHhsgPDo3hijU+Xl0bRg3NdHwvE9mMcXfjdEkZCC1ej olCfwZGEpHspHkvziWlC6rDTQbxmp2EPV/u8OrgDd4G8811y4ZWKfKNVRchpC31V 7PLhxZxKawG2w15l5tT3SUfEEJdaZmjqp1+jNzasA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrtdeggdduvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsehmtderre erredtnecuhfhrohhmpehrihigvggusehhrghpphihlhgvphhtihgtrdhorhhgnecurfgr rhgrmhepmhgrihhlfhhrohhmpehrihigvggusehhrghpphihlhgvphhtihgtrdhorhhgne cuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id AC433E00A1; Thu, 20 Jun 2019 14:03:07 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.1.6-728-g133121a-fmstable-20190620v1 Mime-Version: 1.0 Message-Id: Date: Thu, 20 Jun 2019 20:03:07 +0200 From: rixed@happyleptic.org To: caml-list@inria.fr Content-Type: multipart/mixed; boundary=0e611ad30496443eb91839955696fe55 Subject: [Caml-list] C, threads, callbacks, and corrupted local_roots Reply-To: rixed@happyleptic.org X-Loop: caml-list@inria.fr X-Sequence: 17627 Errors-to: caml-list-owner@inria.fr Precedence: list Precedence: bulk Sender: caml-list-request@inria.fr X-no-archive: yes List-Id: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --0e611ad30496443eb91839955696fe55 Content-Type: text/plain Hello ! I have a programs which entry point is in C++ and that spawn a few threads, some of which are calling back some OCaml code. My understanding is that for this to work, one must only follow those rules (in addition to the usual "how to leave in peace with the GC" rules: 1. call caml_startup(argv) before any OCaml code or any other function from the OCaml runtime is run; 2. for each thread that will eventually run some OCaml code (anything that could call the GC (and also, possibly, do anything with signals)), call caml_c_thread_register before; 3. before a thread terminates and another thread runs the OCaml GC, call (from that terminating thread) caml_c_thread_unregister (it is not clear exactly why to be honest, as a finished thread should have no root left) What is a bit unclear is how thread registering and caml_startup are supposed to deal with the giant lock. From testing and reading the code, it seems that caml_c_thread_(un)register expects the giant lock to be released, which caml_startup does not do, so calling caml_release_runtime_system right after caml_startup seems in order (and therefore reacquiring it before calling back to OCaml). Neither is it clear to me how to declare local roots in the function registering the thread (more specifically: should caml_c_thread_register be called first, then a new block created and CAMLparam and friends be called now that the current root list is initialized?); But in the exemple below it does not matter as there are no values requiring such protection. Yet, whatever I try, I get a crash in the Gc when it's scaninng the roots in caml_do_local_roots, with a corrupted local_root pointer that is suspiciously small (few hundred bytes, frequently but not always just 0xfff). I am not certain about those values though, as I'm not using a debug runtime (opam had such debug variants for a couple of old ocaml versions but not any longer it seems) Thankfully, the attached minimal example dies in the exact same circumstances. Do anyone know what I am doing wrong? --0e611ad30496443eb91839955696fe55 Content-Disposition: attachment;filename="Makefile" Content-Type: application/octet-stream; name="Makefile" Content-Transfer-Encoding: BASE64 T0NBTUxMSUIgPSAkKHNoZWxsIG9jYW1sZmluZCBvY2FtbGMgLXdoZXJlKQpDRkxBR1MgKz0g LWcgLU8wIC1XYWxsIC1XIC1mUElDIC1mc3RhbmRhbG9uZS1kZWJ1ZwpDUFBGTEFHUyArPSAt SSAkKE9DQU1MTElCKQpMREZMQUdTICs9IC1MJChPQ0FNTExJQikgLWx1bml4IC1sY2FtbHN0 ciAtbHRocmVhZHMgLWxhc21ydW5fc2hhcmVkCgphbGw6IGIKCmI6IGEubyBiLm8KCmEuY214 OiBhLm1sCglvY2FtbGZpbmQgb2NhbWxvcHQgLXRocmVhZCAtcGFja2FnZSB0aHJlYWRzIC1j ICQ8IC1vICRACgphLm86IGEuY214CglvY2FtbGZpbmQgb2NhbWxvcHQgLXRocmVhZCAtcGFj a2FnZSB0aHJlYWRzIC1saW5rcGtnICQ8IC1vdXRwdXQtb2JqIC1vIGEudG1wLm8KCW12IGEu dG1wLm8gJEAKCmNsZWFuOgoJJChSTSkgKi5vICouY214ICouY21pIGIgKi5zCg== --0e611ad30496443eb91839955696fe55 Content-Disposition: attachment;filename="a.ml" Content-Type: application/octet-stream; name="a.ml" Content-Transfer-Encoding: BASE64 bGV0IGNhbGxfbWVfZnJvbV9jICgpID0KICBQcmludGYucHJpbnRmICJXaWxsIEkgc3Vydml2 ZT9cbiUhIiA7CiAgR2MuY29tcGFjdCAoKSA7CiAgUHJpbnRmLnByaW50ZiAiSSBzdXJ2aXZl ZCFcbiUhIgoKbGV0IGluaXQgPQogIGlnbm9yZSAoVGhyZWFkLnNlbGYgKCkpIDsKICBpZ25v cmUgKENhbGxiYWNrLnJlZ2lzdGVyICJjYWxsX21lX2Zyb21fYyIgY2FsbF9tZV9mcm9tX2Mp Cg== --0e611ad30496443eb91839955696fe55 Content-Disposition: attachment;filename="b.c" Content-Type: application/octet-stream; name="b.c" Content-Transfer-Encoding: BASE64 I2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHB0aHJl YWQuaD4KI2luY2x1ZGUgPGNhbWwvbWx2YWx1ZXMuaD4KI2luY2x1ZGUgPGNhbWwvc3RhcnR1 cC5oPgojaW5jbHVkZSA8Y2FtbC9jYWxsYmFjay5oPgojaW5jbHVkZSA8Y2FtbC90aHJlYWRz Lmg+CgpzdGF0aWMgdm9pZCAqYW5vdGhlcl90aHJlYWQodm9pZCAqd3R2KQp7CiAgKHZvaWQp d3R2OwogIGNhbWxfY190aHJlYWRfcmVnaXN0ZXIoKTsKICBwcmludGYoImluIGFub3RoZXJf dGhyZWFkXG4iKTsKICBjYW1sX2FjcXVpcmVfcnVudGltZV9zeXN0ZW0oKTsKICB2YWx1ZSAq Y2IgPSBjYW1sX25hbWVkX3ZhbHVlKCJjYWxsX21lX2Zyb21fYyIpOwogIGNhbWxfY2FsbGJh Y2soKmNiLCBWYWxfdW5pdCk7CiAgY2FtbF9yZWxlYXNlX3J1bnRpbWVfc3lzdGVtKCk7CiAg Y2FtbF9jX3RocmVhZF91bnJlZ2lzdGVyKCk7CiAgcmV0dXJuIE5VTEw7Cn0KCmludCBtYWlu KGludCBhcmdjLCBjaGFyICphcmd2W10pCnsKICAodm9pZClhcmdjOwogIGNhbWxfc3RhcnR1 cChhcmd2KTsKICBjYW1sX3JlbGVhc2VfcnVudGltZV9zeXN0ZW0oKTsKCiAgcHRocmVhZF90 IHRoOwogICh2b2lkKXB0aHJlYWRfY3JlYXRlKCZ0aCwgTlVMTCwgYW5vdGhlcl90aHJlYWQs IE5VTEwpOwoKICAodm9pZClwdGhyZWFkX2pvaW4odGgsIE5VTEwpOwoKICByZXR1cm4gMDsK fQo= --0e611ad30496443eb91839955696fe55--