From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 5D44BBBCA for ; Fri, 29 Feb 2008 00:19:39 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAKrQxkfAXQInh2dsb2JhbACQcAEBAQgKKZ1F X-IronPort-AV: E=Sophos;i="4.25,422,1199660400"; d="scan'208";a="23184289" Received: from concorde.inria.fr ([192.93.2.39]) by mail4-smtp-sop.national.inria.fr with ESMTP; 29 Feb 2008 00:19:39 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id m1SNJctZ016483 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 29 Feb 2008 00:19:38 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAG7QxkdQRFuwh2dsb2JhbACQcAEBAQgKKZ1F X-IronPort-AV: E=Sophos;i="4.25,422,1199660400"; d="scan'208";a="8748420" Received: from furbychan.cocan.org ([80.68.91.176]) by mail1-smtp-roc.national.inria.fr with ESMTP; 29 Feb 2008 00:19:38 +0100 Received: from rich by furbychan.cocan.org with local (Exim 4.63) (envelope-from ) id 1JUs2J-0007SE-T5; Thu, 28 Feb 2008 23:19:35 +0000 Date: Thu, 28 Feb 2008 23:19:35 +0000 To: Jake Donham Cc: caml users Subject: Re: [Caml-list] Safe Obj.magic container ? Message-ID: <20080228231935.GA26592@annexia.org> References: <1204103370.7718.18.camel@Blefuscu> <20080227094329.GC13254@yquem.inria.fr> <37B36607-9F22-4537-B4DB-1E04348E2B90@inria.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.13 (2006-08-11) From: Richard Jones X-Miltered: at concorde with ID 47C7418A.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; damien:01 damien:01 val:01 abstr:01 val:01 abstr:01 'x':01 'x':01 pointer:01 unboxing:01 unboxing:01 pointer:01 segfault:01 28,:98 28,:98 On Thu, Feb 28, 2008 at 11:20:53AM -0800, Jake Donham wrote: > On Thu, Feb 28, 2008 at 6:23 AM, Damien Doligez > wrote: > > > Example (on a 32-bit machine): > > > > Objective Caml version 3.10.2+rc1 > > > > # let x = Obj.repr 1;; > > val x : Obj.t = > > # let y = Obj.repr 1.0;; > > val y : Obj.t = > > # [| x; y |];; > > - : Obj.t array = [|; |] > > # [| y; x |];; > > Bus error > > > Could you please explain this behavior? I _think_ this is what's going on: Obj.repr is just the %identity primitive. So 'x' and 'y' still look like an int and a float respectively to the garbage collector. ie. 'x' is still shifted left 1 place with the bottom bit set. 'y' is still a pointer to a boxed 8 bit float (with a header on the heap identifying it as such). Constructing the array calls the caml_make_array primitive, which looks at the tag of the first element to see if it's a float. This would cause the unboxing optimization. Unfortunately if caml_make_array spots the possible unboxing of an array of floats, then it blindly copies the remaining elements, which dereferences the int (thinking it's a pointer to a float) and that causes the segfault. This also explains why it works the other way around, because having the int first doesn't trigger the optimization. Rich. -- Richard Jones Red Hat