From mboxrd@z Thu Jan 1 00:00:00 1970 X-Sympa-To: caml-list@inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p7GGETo7024851 for ; Tue, 16 Aug 2011 18:14:29 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnQCAP+WSk7RVaG2kGdsb2JhbABBDphlhzMBiAoIFAEBAQEJCQ0HFAQhgUABAQEBAgESAiwBASwLAQQLCwQHAwoNISISAQUBChIGExIQh04Em0QKjwwBjl4FhkiCVZBBjFk8gyg5 X-IronPort-AV: E=Sophos;i="4.68,234,1312149600"; d="scan'208";a="116005740" Received: from mail-gx0-f182.google.com ([209.85.161.182]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 16 Aug 2011 18:14:24 +0200 Received: by gxk28 with SMTP id 28so70381gxk.27 for ; Tue, 16 Aug 2011 09:14:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=F3zDDOZx7b5tOu3R7RN7WQHPdTLCVapMWvZfeywi2bs=; b=xqzy8KB3UDURrOllf8+w6iIXzEMLiP/1UlmAtTZn8zYOn1Ueg5uGuuWLcc1n5m1tAv USdWwIvQ1i8q29gyBnnhVF+6aCVikDnCMKmgtaiSP3q6GxC/w/L3QXrdw5AeZzIUT3KA G+Zh3ed/XOvXze4arBFWDmTp7ou1LGSSemnlI= MIME-Version: 1.0 Received: by 10.151.21.13 with SMTP id y13mr1820ybi.239.1313511263048; Tue, 16 Aug 2011 09:14:23 -0700 (PDT) Received: by 10.151.46.18 with HTTP; Tue, 16 Aug 2011 09:14:22 -0700 (PDT) In-Reply-To: <201108161606.p7GG6wR9029895@outgoing.mit.edu> References: <20110816152550.GA21081@annexia.org> <201108161606.p7GG6wR9029895@outgoing.mit.edu> Date: Tue, 16 Aug 2011 17:14:22 +0100 Message-ID: From: Wojciech Meyer To: John Carr Cc: "Richard W.M. Jones" , Caml List Content-Type: multipart/alternative; boundary=000e0cd4b31c3e13fd04aaa1ac36 X-Validation-by: wojciech.meyer@googlemail.com Subject: Re: [Caml-list] Interfacing with C: bad practice --000e0cd4b31c3e13fd04aaa1ac36 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Aug 16, 2011 at 5:06 PM, John Carr wrote: > > Richard W.M. Jones wrote: > > > On Tue, Aug 16, 2011 at 11:37:03AM +0400, Dmitry Bely wrote: > > > C compiler first puts "list" pointer on stack and then calls > > > caml_copy_string(*s), potentially invalidating "list". Of course, the > > > stack copy of "list" is not registered as a global root so wrp_ml_cons > > > gets an invalid value. > > > > I think this must be a bug in your C compiler. The address of list is > > stashed in the roots struct, so the C compiler should know that list > > can be changed by the call to caml_copy_string. > Maybe looking at the assembly output would help. If you can post the output with gcc -S flag. Cheers; Wojciech The call > > f(g(), x) > > can behave as either > > temp1 = g() > temp2 = x > f(temp1, temp2) > > or > > temp1 = x > temp2 = g() > f(temp2, temp1) > > The order does not need to be deterministic. > > If the call to g() changes x, the second order results in the > function f() receiving the "wrong" value. > > --John Carr (jfc@mit.edu) > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > --000e0cd4b31c3e13fd04aaa1ac36 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Tue, Aug 16, 2011 at 5:06 PM, John Carr <= span dir=3D"ltr"><jfc@mit.edu> wrote:

Richard W.M. Jones <rich@annexia.org= > wrote:

> On Tue, Aug 16, 2011 at 11:37:03AM +0400, Dmitry Bely wrote:
> > C compiler first puts "list" pointer on stack and then = calls
> > caml_copy_string(*s), potentially invalidating "list". = Of course, the
> > stack copy of "list" is not registered as a global root= so wrp_ml_cons
> > gets an invalid value.
>
> I think this must be a bug in your C compiler. =A0The address of list = is
> stashed in the roots struct, so the C compiler should know that list > can be changed by the call to caml_copy_string.
=

Maybe looking at the assembly output would help. If you can post t= he output with gcc -S flag.

Cheers;
Wojciech

The call

=A0 f(g(), x)

can behave as either

=A0temp1 =3D g()
=A0temp2 =3D x
=A0f(temp1, temp2)

or

=A0temp1 =3D x
=A0temp2 =3D g()
=A0f(temp2, temp1)

The order does not need to be deterministic.

If the call to g() changes x, the second order results in the
function f() receiving the "wrong" value.

=A0 =A0--John Carr (jfc@mit.edu)

--
Caml-list mailing list. =A0Subscrip= tion management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


--000e0cd4b31c3e13fd04aaa1ac36--