Thread overview
[Issue 9720] New: OSX wrong code with -o "Illegal instruction"
Apr 02, 2013
Walter Bright
Apr 02, 2013
Walter Bright
[Issue 9720] OSX wrong code with -O Illegal instruction
Apr 02, 2013
Walter Bright
Apr 03, 2013
Brad Roberts
March 14, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9720

           Summary: OSX wrong code with -o "Illegal instruction"
           Product: D
           Version: D2
          Platform: All
        OS/Version: Mac OS X
            Status: NEW
          Severity: blocker
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: monarchdodra@gmail.com


--- Comment #0 from monarchdodra@gmail.com 2013-03-14 07:12:20 PDT ---
Investigated from:
https://github.com/D-Programming-Language/phobos/pull/878
http://d.puremagic.com/test-results/pull-history.ghtml?projectid=1&repoid=3&pullid=878

Happens with OSX, when compile with -O.

Below a reduced test case:

//----
import std.stdio;

//Simplified RefCounted
struct MyRef(T)
{
    private struct Impl
    {
        T _payload;
        size_t _count;
    }
    Impl* _store;

    this(int i)
    {
        _store = new Impl;
        _store._payload = T.init;
        _store._count = 1;
    }

    ~this()
    {
        if (!_store) return;
        writeln("d: ", _store._count); //Comment me
        --_store._count;
    }
}

//Extracted from Array
struct Payload
{
    size_t _capacity; //Comment me
    int[] _pay;       //Comment me

    size_t insertBack(Stuff)(Stuff /+stuff+/)
    {
        immutable newLen   = _pay.length + 3;
        _pay.length = newLen;
        _pay = _pay[0 .. newLen]; //Comment me
        return 3;
    }
}
alias Data = MyRef!Payload;

unittest
{
    auto a = Data(1);
    auto b = Data(1);
    writeln(__LINE__);
    a._store._payload.insertBack(1); //Passes
    writeln(__LINE__);
    a._store._payload.insertBack(b); //Fails
    writeln(__LINE__);
}
//----
rdmd  -w -d -property -O --main -unittest test2.d
47
49
d: 1
Illegal instruction
//---
I was not able to simplify much more, as the compile-time behavior is non
deterministic: EG: removing an unused variable such as "capacity" is enough to
make the problem "disappear" (or at least, unobservable).

AFAIK, this code contains nothing unsafe, and the only requirement to reproduce is as simple as "pass by value something with a destructor"... (!)

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


monarchdodra@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
           Priority|P2                          |P1


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


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com
           Platform|All                         |x86_64


--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> 2013-04-01 19:19:59 PDT ---
64 bit only.

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



--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2013-04-01 21:11:59 PDT ---
Reduced test case:
------------------
import core.stdc.stdio;

struct Payload
{
    size_t _capacity; //Comment me
    int[] _pay;       //Comment me

    size_t insertBack(Data d)
    {
        immutable newLen   = _pay.length + 3;
        _pay.length = newLen;
        _pay = _pay[0 .. newLen]; //Comment me
        return 3;
    }
}

struct Impl
{
    Payload _payload;
    size_t _count;
}

struct Data
{
    Impl* _store;

    this(int i)
    {
        _store = new Impl;
        _store._payload = Payload.init;
    }

    ~this()
    {
        printf("%d\n", _store._count);
        --_store._count;
    }
}


void main()
{
    auto a = Data(1);
    auto b = Data(1);
    a._store._payload.insertBack(b); //Fails
}

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



--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> 2013-04-01 22:05:00 PDT ---
https://github.com/D-Programming-Language/dmd/pull/1823

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



--- Comment #4 from github-bugzilla@puremagic.com 2013-04-02 23:14:47 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/8176c41cb828b9800928d947926b9c0749222e1f fix Issue 9720 - OSX wrong code with -O Illegal instruction

https://github.com/D-Programming-Language/dmd/commit/614878714a702ca923b4910e487e4635226bc62c Merge pull request #1823 from WalterBright/fix9720

fix Issue 9720 - OSX wrong code with -O Illegal instruction

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



--- Comment #5 from github-bugzilla@puremagic.com 2013-04-02 23:41:38 PDT ---
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/6b2fb00442c38f9200d7d10a07e5435807651a7e Merge pull request #1823 from WalterBright/fix9720

fix Issue 9720 - OSX wrong code with -O Illegal instruction

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


Brad Roberts <braddr@puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |braddr@puremagic.com
         Resolution|                            |FIXED


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