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 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id B7007BBCA for ; Thu, 28 Feb 2008 15:23:35 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAABJTxkfAXQInh2dsb2JhbACQcQEBAQgKKZx/ X-IronPort-AV: E=Sophos;i="4.25,419,1199660400"; d="scan'208";a="7816256" Received: from concorde.inria.fr ([192.93.2.39]) by mail2-smtp-roc.national.inria.fr with ESMTP; 28 Feb 2008 15:23:35 +0100 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id m1SENZH0029683 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Thu, 28 Feb 2008 15:23:35 +0100 X-IronPort-AV: E=Sophos;i="4.25,419,1199660400"; d="scan'208";a="7816254" Received: from macabane.inria.fr ([128.93.8.160]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/AES128-SHA; 28 Feb 2008 15:23:35 +0100 Message-Id: From: Damien Doligez To: caml users In-Reply-To: <37B36607-9F22-4537-B4DB-1E04348E2B90@inria.fr> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v919.2) Subject: Re: [Caml-list] Safe Obj.magic container ? Date: Thu, 28 Feb 2008 15:23:35 +0100 References: <1204103370.7718.18.camel@Blefuscu> <20080227094329.GC13254@yquem.inria.fr> <37B36607-9F22-4537-B4DB-1E04348E2B90@inria.fr> X-Mailer: Apple Mail (2.919.2) X-Miltered: at concorde with ID 47C6C3E7.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; damien:01 damien:01 maranget:01 semantics:01 ocaml:01 pointer:01 val:01 abstr:01 val:01 abstr:01 1.0:98 doligez:01 doligez:01 wrote:01 dynamically:01 On 2008-02-27, at 10:43, Luc Maranget wrote: > The semantics of Obj.magic is as follows: > > 1. Obj.magic does not exist. Don't use it. > > 2. Obj.magic is black magic, you can see it as the identity > (fun x -> x) with type 'a -> 'b. > > As I see it, 2. steems from the uniform representation of values by > OCaml. > > A value is either a scalar, or a pointer, and this can be tested > dynamically (by looking at the low order bit) by > Obj.is_int/Obj.is_block. If that was the whole story, Obj.magic would be pretty safe. But there are exceptions to the uniform representation of values, and they make Obj.magic really hard to use. The most important exception is floating-point numbers. They are not always represented as one word each, and that means even Obj.repr is unsafe. 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 My advice: don't use the Obj module if you were surprised by this example. -- Damien