www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9834] New: template+class+FunctionLiteral=AccessViolation

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9834

           Summary: template+class+FunctionLiteral=AccessViolation
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: zan77137 nifty.com



This code makes AccessViolation on git master head:
-------------------
struct Event()
{
    void delegate() dg;

    void set(void delegate() handler)
    {
        dg = handler;
    }

    void call()
    {
        dg();
    }
}


void main()
{
    Event!() ev;
    auto a = new class
    {
        Object o;
        this()
        {
            o = new Object;
            ev.set((){o.toString();});
        }
    };
    ev.call();
}
-------------------
$ dmd -run main
object.Error: Access Violation
----------------
0x004020A5
0x00402128
0x00402057
0x004029EC
0x00402A27
0x00402625
0x00402140
0x769133AA in BaseThreadInitThunk
0x774E9EF2 in RtlInitializeExceptionChain
0x774E9EC5 in RtlInitializeExceptionChain



And, following is git bisect result:
$ git bisect bad
67fbf5753e9d4a276c354e952ed2f888efcb714c is the first bad commit

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 30 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9834





 And, following is git bisect result:
 $ git bisect bad
 67fbf5753e9d4a276c354e952ed2f888efcb714c is the first bad commit
The commit is not a root cause. If change Event struct to non-template, and add pure to set method, AV still occurs. struct Event { void delegate() dg; //void set(void delegate() h) { dg = h; } // no AV void set(void delegate() h) pure { dg = h; } // AV occurs void call() { dg(); } } void main() { Event ev; auto a = new class { Object o; this() { o = new Object; ev.set((){ o.toString(); }); } }; ev.call(); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9834




Ok, I found the root cause.

    version(bad) void set(void delegate() h) pure { dg = h; }  // AV occurs
    else         void set(void delegate() h)      { dg = h; }  // no AV

// -version=bad
c:\d\test.d:14         this() {
004020ec: 55                      push ebp
004020ed: 8bec                    mov ebp, esp
004020ef: 83ec04                  sub esp, 0x4
004020f2: 53                      push ebx
004020f3: 56                      push esi
004020f4: 8945fc                  mov [ebp-0x4], eax
c:\d\test.d:15             o = new Object;
004020f7: b880f34100              mov eax, 0x41f380
004020fc: 50                      push eax
004020fd: e87e020000              call 0x402380 __d_newclass
00402102: 8b4dfc                  mov ecx, [ebp-0x4]
00402105: 894108                  mov [ecx+0x8], eax
c:\d\test.d:16             ev.set((){
00402108: ba2c214000              mov edx, 0x40212c
0040210d: 52                      push edx
0040210e: 8d5dfc                  lea ebx, [ebp-0x4]
00402111: 53                      push ebx
00402112: 8b75fc                  mov esi, [ebp-0x4]
00402115: 8b460c                  mov eax, [esi+0xc]
00402118: 83c004                  add eax, 0x4
0040211b: e8f0feffff              call 0x402010 test.Event.set c:\d\test.d:6
00402120: 8b45fc                  mov eax, [ebp-0x4]
00402123: 83c404                  add esp, 0x4
c:\d\test.d:19         }
00402126: 5e                      pop esi
00402127: 5b                      pop ebx
00402128: c9                      leave
00402129: c3                      ret


c:\d\test.d:14         this() {
004020ec: 55                      push ebp
004020ed: 8bec                    mov ebp, esp
004020ef: 83ec08                  sub esp, 0x8
004020f2: 53                      push ebx
004020f3: 56                      push esi
004020f4: 8945fc                  mov [ebp-0x4], eax
004020f7: 6a08                    push 0x8                        <--
004020f9: e8ee020000              call 0x4023ec __d_allocmemory    <--
004020fe: 8945f8                  mov [ebp-0x8], eax            <--
00402101: 8b4dfc                  mov ecx, [ebp-0x4]            <--
00402104: 8908                    mov [eax], ecx                <--
00402106: 8b55fc                  mov edx, [ebp-0x4]            <--
00402109: 895004                  mov [eax+0x4], edx            <--
c:\d\test.d:15             o = new Object;
0040210c: bb80f34100              mov ebx, 0x41f380
00402111: 53                      push ebx
00402112: e87d020000              call 0x402394 __d_newclass
00402117: 8b75fc                  mov esi, [ebp-0x4]
0040211a: 894608                  mov [esi+0x8], eax
c:\d\test.d:16             ev.set((){
0040211d: b840214000              mov eax, 0x402140
00402122: 50                      push eax
00402123: ff75f8                  push dword [ebp-0x8]
00402126: 8b4dfc                  mov ecx, [ebp-0x4]
00402129: 8b410c                  mov eax, [ecx+0xc]
0040212c: 83c004                  add eax, 0x4
0040212f: e8dcfeffff              call 0x402010 test.Event.set c:\d\test.d:7
00402134: 8b45fc                  mov eax, [ebp-0x4]
00402137: 83c408                  add esp, 0x8
c:\d\test.d:19         }
0040213a: 5e                      pop esi
0040213b: 5b                      pop ebx
0040213c: c9                      leave
0040213d: c3                      ret

With -version=bad, heap allocation for closure variable does not performed.
Therefore, compiler wrongly judges that the lambda (){o.toString();} is "scope
delegate".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9834


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull, wrong-code
           Severity|normal                      |regression



https://github.com/D-Programming-Language/dmd/pull/1817

This is a regression happened in 2.063a.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9834




Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/ac859ee5103f9204bf22373f429c67f3fe286697
fix Issue 9834 - incorrect detection of lambda locality.

https://github.com/D-Programming-Language/dmd/commit/767a0293429fbeed6f812e5a514c028b5ecb2cca


[REG2.063] Issue 9834 - incorrect detection of lambda locality.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 30 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9834


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


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