April 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9931



--- Comment #10 from John Colvin <john.loughran.colvin@gmail.com> 2013-04-17 10:58:11 BST ---
(In reply to comment #9)
> (In reply to comment #7)
> > 0x0000267c <D4test3barFZS4test3Foo+20>: call   0x26a0 <foo>
> > 0x00002681 <D4test3barFZS4test3Foo+25>: add    $0xc,%esp
> > 0x00002684 <D4test3barFZS4test3Foo+28>: mov    %eax,%esi // <- EAX is trashed
> > 0x00002686 <D4test3barFZS4test3Foo+30>: mov    -0x4(%ebp),%edi
> > 0x00002689 <D4test3barFZS4test3Foo+33>: movsl  %ds:(%esi),%es:(%edi)
> > 0x0000268a <D4test3barFZS4test3Foo+34>: movsl  %ds:(%esi),%es:(%edi)
> > 0x0000268b <D4test3barFZS4test3Foo+35>: movsl  %ds:(%esi),%es:(%edi)
> 
> You're reading it the wrong way around, at&t is src,des

Sorry, my mistake, I misunderstood you. Martin, you are completely correct. Dmd is presuming that the pointer to the struct is returned in eax but foo sets that to 0, as it is entitled to. Hence movs is reading from 0.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9931


Jacob Carlborg <doob@me.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|D2                          |D1 & D2
           Severity|normal                      |critical


--- Comment #11 from Jacob Carlborg <doob@me.com> 2013-04-17 04:16:38 PDT ---
Raising to critical.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9931



--- Comment #12 from Jacob Carlborg <doob@me.com> 2013-04-17 04:18:45 PDT ---
I'm still wondering though:

* Why does it work on Mac OS X 10.6.3, luck?
* Is using a temporary variable in "bar" a safe workaround?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9931



--- Comment #13 from Jacob Carlborg <doob@me.com> 2013-04-17 04:20:57 PDT ---
Would "Mac OS X ABI not followed when returning structs for extern (C)" be a better title for the issue. Can I changed that?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9931



--- Comment #14 from John Colvin <john.loughran.colvin@gmail.com> 2013-04-17 12:55:37 BST ---
(In reply to comment #12)
> I'm still wondering though:
> 
> * Why does it work on Mac OS X 10.6.3, luck?

http://forum.dlang.org/post/ixoawrwanlopvgzljost@forum.dlang.org

> * Is using a temporary variable in "bar" a safe workaround?

Based on how the codegen changes with adding a temporary variable in linux, it should be safe. Disassembly from OS X could confirm that.

BUT:

That's only for this particular example, with a particular set of compiler setting. It's exactly the sort of thing optimisers mess around with. Personally I would not risk it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9931



--- Comment #15 from John Colvin <john.loughran.colvin@gmail.com> 2013-04-17 12:56:05 BST ---
(In reply to comment #13)
> Would "Mac OS X ABI not followed when returning structs for extern (C)" be a better title for the issue. Can I changed that?

Much better. Changed it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9931



--- Comment #16 from Jacob Carlborg <doob@me.com> 2013-04-17 05:17:13 PDT ---
(In reply to comment #14)

> Based on how the codegen changes with adding a temporary variable in linux, it should be safe. Disassembly from OS X could confirm that.
> 
> BUT:
> 
> That's only for this particular example, with a particular set of compiler setting. It's exactly the sort of thing optimisers mess around with. Personally I would not risk it.

I suspected that. The thing is that I'm stuck with a D1 code base. I want to finished that before I port it to D2. So I'm hoping that a fix for this will be applied to the D1 branch as well.

Otherwise I guess I'll have to avoid any optimizations until I've ported it to D2.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9931



--- Comment #17 from Jacob Carlborg <doob@me.com> 2013-04-17 05:17:36 PDT ---
(In reply to comment #15)

> Much better. Changed it.

Cool, thanks.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9931



--- Comment #18 from Jacob Carlborg <doob@me.com> 2013-04-23 01:37:46 PDT ---
If it's not clear, this issue happens for both 32 and 64bit.

Also, if I implement the C function in D with extern (C) it won't segfault. This would indicate that DMD does something wrong in this case as well.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 24, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9931



--- Comment #19 from Martin Nowak <code@dawg.eu> 2013-04-23 17:56:10 PDT ---
(In reply to comment #18)
> If it's not clear, this issue happens for both 32 and 64bit.
> 
> Also, if I implement the C function in D with extern (C) it won't segfault. This would indicate that DMD does something wrong in this case as well.

It doesn't do anything wrong, it just sets EAX to contain the hidden pointer before returning. This is required by the SysV IA-32 ABI but apparently not on OSX.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------