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 4853D5D5 for ; Wed, 2 May 2018 16:09:11 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.49,354,1520895600"; d="scan'208";a="325476013" Received: from sympa.inria.fr ([193.51.193.213]) by mail2-relais-roc.national.inria.fr with ESMTP; 02 May 2018 18:09:09 +0200 Received: by sympa.inria.fr (Postfix, from userid 20132) id 4897282450; Wed, 2 May 2018 18:09:09 +0200 (CEST) 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 C6AE88240C for ; Wed, 2 May 2018 18:09:05 +0200 (CEST) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=fperriot@gmail.com; spf=Pass smtp.mailfrom=fperriot@gmail.com; spf=None smtp.helo=postmaster@mail-lf0-f44.google.com IronPort-PHdr: =?us-ascii?q?9a23=3A8EEWWRDgPC5dCj399iG/UyQJP3N1i/DPJgcQr6Af?= =?us-ascii?q?oPdwSPryo8bcNUDSrc9gkEXOFd2Cra4c0KyO6+jJYi8p2d65qncMcZhBBVcuqP?= =?us-ascii?q?49uEgeOvODElDxN/XwbiY3T4xoXV5h+GynYwAOQJ6tL1LdrWev4jEMBx7xKRR6?= =?us-ascii?q?JvjvGo7Vks+7y/2+94fcbglUijexe69+IAmrpgjNq8cahpdvJLwswRXTuHtIfO?= =?us-ascii?q?pWxWJsJV2Nmhv3+9m98p1+/SlOovwt78FPX7n0cKQ+VrxYES8pM3sp683xtBnM?= =?us-ascii?q?VhWA630BWWgLiBVIAgzF7BbnXpfttybxq+Rw1DWGMcDwULs5Xymp4aV2Rx/ykC?= =?us-ascii?q?oJNzA3/mLKhMJukK1VrwmspwBmw4POfI2ZKOZyc6HbcNgHRWRBRMFRVylZD428?= =?us-ascii?q?dYsAEfcOMvhCr4n8u1QFsAGwChe2BOP1yz9IgWP21rA93uQvCQHJwhYgH8gPsH?= =?us-ascii?q?TTsNX4L6gSUeWvw6nJyTXPde9Z2TD46IXRdB0qvPKCXapofMbP1UUiExnJg1aQ?= =?us-ascii?q?pID/IT+Zy+QAv3KU4udvU++klnQppBtroje1w8chkonJiZwRylDD7Sh5xZw6Jd?= =?us-ascii?q?y8SEJiZd6kHodcuziUN4Z5Tc4uWW5ouCE9yr0JvZ60YjIGx4ggxx7ac/CHco6I?= =?us-ascii?q?7Qz/VOuJPzt0mHZodKi8ihuy60Ss1PDwW8mu3FtFoSdJitzMuWoM1xzX5MiHUP?= =?us-ascii?q?x9/kK51DaBzwDc9PtEIVo1larcN54s2bEwlp8JvkTCGi/6gln5jKiTdkk84Oin?= =?us-ascii?q?9/znYqn6pp+bL4J7lhvxMqEqmsCmBeQ4MxMOX3SA9OSn1Lzj+FX5T69Qgv03lK?= =?us-ascii?q?nZqpHaKt4Bqq63GQ8Gmrokvh22CjPj1NUDgVEGKkhEcVSJldvHIVbLdd7iAPuy?= =?us-ascii?q?mVWn2A9m3erHOaGpVpvVI2LZkbD9Vbl44k9YjgE0yIYMtNpvFrgdLaerCQfKv9?= =?us-ascii?q?vCA0phal3m86PcENx4k7gmdyeKC66dPrnVtAbRtO0qKuiIIoQSvWSkcqR317vV?= =?us-ascii?q?lXY83GQlU+yxx5JOMSK3G/1nJwOSZn++2o5cQ1dPhRI3SanRsHPHUTNXYCzvDa?= =?us-ascii?q?c15zV+DIz+SImfGsaih7uO2Cr9FZpTNDhL?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0B8BwCP4elagCzXVdFcHgEGDINnAYE2K?= =?us-ascii?q?AqDJD+DbZECgwOHWo04CwWHZQcZBwEENBQBAgEBAQEBAQEBARMBAQkNCQgmJQx?= =?us-ascii?q?CDgGBZCKCZBIFHQEbHgMSAw0PAiYCJAERAQUBhSsBAxWNGoxjgx08iwWBfwUBF?= =?us-ascii?q?4JwBYNHChkmDVRXgjoCBhJ3hxGBVD+DZYV0gjSCVAKYFAiOS4E1hjyEZ5AwDwM?= =?us-ascii?q?egQQzgXMzGggbFTsxghKCFAwXg0WKVD0wgz+KKSqCHAEB?= X-IPAS-Result: =?us-ascii?q?A0B8BwCP4elagCzXVdFcHgEGDINnAYE2KAqDJD+DbZECgwO?= =?us-ascii?q?HWo04CwWHZQcZBwEENBQBAgEBAQEBAQEBARMBAQkNCQgmJQxCDgGBZCKCZBIFH?= =?us-ascii?q?QEbHgMSAw0PAiYCJAERAQUBhSsBAxWNGoxjgx08iwWBfwUBF4JwBYNHChkmDVR?= =?us-ascii?q?XgjoCBhJ3hxGBVD+DZYV0gjSCVAKYFAiOS4E1hjyEZ5AwDwMegQQzgXMzGggbF?= =?us-ascii?q?TsxghKCFAwXg0WKVD0wgz+KKSqCHAEB?= X-IronPort-AV: E=Sophos;i="5.49,354,1520895600"; d="scan'208";a="264113489" Received: from mail-lf0-f44.google.com ([209.85.215.44]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 02 May 2018 18:09:04 +0200 Received: by mail-lf0-f44.google.com with SMTP id j16-v6so21668181lfb.7 for ; Wed, 02 May 2018 09:09:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to :content-transfer-encoding; bh=NGAaO4qG1FZmqymGvNHZlOyjRX5RpcdOLIRmcA40Xzo=; b=h1iomzdCg6bXsxd2vb8R+pk9AjTc/VnrUqlAsWdhLtEmmvTNH7ppPSwOwxs3Ic9kLu qyz6L+93l9mcXxKqlG+O+ko6LmrvgAuDCCpc/Nig25a7U0DhBTg/sf03J2ZVSNi4rVVl weVYfIzueJOxbXM+RPkOaUrcW20irAlKUEPdj1xETaJ1IlS6EA8y3GXJr0XuYcGHBUV4 kOxFw5nVuPY/M80L1UB9m2HPAZdzZe6BAgiJbabdD1ZROH7f6h6ZAuYUaBzrebuRmqmZ ahGKyT9L1RbhXlG73qMmCul2oVJqJAJenwHAFWx3+3ew+espUFPfmpTa3GaTrX05pNxQ ThoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to :content-transfer-encoding; bh=NGAaO4qG1FZmqymGvNHZlOyjRX5RpcdOLIRmcA40Xzo=; b=PGYdC1/489ZT9TzmviBlvN/8SzIZNZQkm8DrDa06Q4m1AeaGpwEC3nhpmMe7UvbOjI iG641NB+CZjDeV/x1WZ6mFdIQol5Kg/HA1cDMQIpNlur0lMhHkoloXrtOEQx0/TB+seb pHioNUxGfkODYexZf0NSWsr+srngN+1SdTuLZYT3g9/UUBsDd6wbuV1atIflTvTaZclS vSD3mpZFUC6HN6I2ZglQX+MIUTufatDsTxLNuC7/y/mWruZXvqZz6/BCP4tEADpkjJVb vk2fY7h3BSJO35j7x9LPsHoBN/pytXzJKSPOmqU6aJUINrIxd32pKMMddlr5tbkhPlMe 7EiA== X-Gm-Message-State: ALQs6tB/oY7aiKzXdTvQsEI+yCyqYXja4dykcOZQyZVDqHc5nwawodvQ sJq5VIn+aFhx5sIVy8kJUpqgWcp9Dv0rn766AS5J7FKi X-Google-Smtp-Source: AB8JxZqL+DNcV+6cd4yuAMZwmPrSFxzhVllPvQ9VEYflyRQ5cwNvWm8ovQCevGIL6vrlklChAu2RfSs6kQC+6jvUF1g= X-Received: by 2002:a2e:165a:: with SMTP id 26-v6mr9719957ljw.16.1525277343604; Wed, 02 May 2018 09:09:03 -0700 (PDT) MIME-Version: 1.0 Received: by 10.46.85.200 with HTTP; Wed, 2 May 2018 09:09:03 -0700 (PDT) From: Frederic Perriot Date: Wed, 2 May 2018 18:09:03 +0200 Message-ID: To: caml-list@inria.fr Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: [Caml-list] an implicit GC rule? Reply-To: Frederic Perriot X-Loop: caml-list@inria.fr X-Sequence: 16869 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: Hello caml-list, I have a GC-related question. To give you some context, I'm writing a tool to parse .cmi files and generate .h and .c files, to facilitate constructing OCaml variants from C bindings. For instance, given the following source: type 'a tree =3D Leaf of 'a | Tree of 'a tree * 'a tree [@@h_file] the tool produces C functions: CAMLprim value Leaf(value arg1) { CAMLparam1(arg1); CAMLlocal1(obj); obj =3D caml_alloc_small(1, 0); Field(obj, 0) =3D arg1; CAMLreturn(obj); } CAMLprim value Tree(value arg1, value arg2) { // similar code here } =46rom there, it's tempting to nest calls to variant constructors from C and write code such as: CAMLprim value left_comb(value a, value b, value c) { CAMLparam3(a, b, c); CAMLreturn(Tree(Tree(Leaf(a), Leaf(b)), Leaf(c))); } The problem with the above is the GC root loss due to the nesting of calls to allocating functions. Say Leaf(c) is constructed first, and the resulting value cached in a register, then Leaf(b) triggers a collection, thus invalidating the register contents, and leaving a dangling pointer in the top Tree. Here is an actual ocamlopt output, with Leaf(c) getting cached in rbx: 0x000000000040dbf4 <+149>: callq 0x40d8fd 0x000000000040dbf9 <+154>: mov %rax,%rbx 0x000000000040dbfc <+157>: mov -0x90(%rbp),%rax 0x000000000040dc03 <+164>: mov %rax,%rdi 0x000000000040dc06 <+167>: callq 0x40d8fd 0x000000000040dc0b <+172>: mov %rax,%r12 0x000000000040dc0e <+175>: mov -0x88(%rbp),%rax 0x000000000040dc15 <+182>: mov %rax,%rdi 0x000000000040dc18 <+185>: callq 0x40d8fd 0x000000000040dc1d <+190>: mov %r12,%rsi 0x000000000040dc20 <+193>: mov %rax,%rdi 0x000000000040dc23 <+196>: callq 0x40da19 0x000000000040dc28 <+201>: mov %rbx,%rsi 0x000000000040dc2b <+204>: mov %rax,%rdi 0x000000000040dc2e <+207>: callq 0x40da19 While the C code clearly violates the spirit of the GC rules, I can't help but feel this is still a pitfall. Rule 2 of the manual states: "Local variables of type value must be declared with one of the CAMLlocal macros. [...]" But here, I'm not declaring local variables, unless you count compiler temporaries as local variables? I can see some other people making the same mistake I did. Should there be an explicit warning in the rules? maybe underlining that compiler temps count as variables, or discouraging the kind of nested calls returning values displayed above? thanks, Fr=C3=A9d=C3=A9ric Perriot PS: this is also my first time posting to the list, so I take this opportunity to thank you for the great Q's and A's I've read here over the years --=20 Caml-list mailing list. Subscription management and archives: https://sympa.inria.fr/sympa/arc/caml-list Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs=