www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10462] New: interface thunk doesn't preserve EBX

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

           Summary: interface thunk doesn't preserve EBX
           Product: D
           Version: D2
          Platform: x86
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: code dawg.eu


--- Comment #0 from Martin Nowak <code dawg.eu> 2013-06-24 06:13:55 PDT ---
This is a followup of Bug 9729.
The generated interface thunk now look like this.

_TMP3   LABEL NEAR
        sub     eax, 8                                  ; 0050 _ 83. E8, 08
        call    ?_007                                   ; 0053 _ E8, 00000000

?_007   LABEL NEAR
        pop     ebx                                     ; 0058 _ 5B
        add     ebx, offset _GLOBAL_OFFSET_TABLE_-$+1H  ; 0059 _ 81. C3,
00000003(GOT r)
        jmp     _D3bug4Lock4lockMFZv                    ; 005F _ E9,
FFFFFFFC(PLT r)

The problem here is that the EBX is not restored after the direct jump which
leads to bug when it was used in the calling function.
Not sure what the best solution to this is. Replacing the jump with a call is
not a good solution because of it alters the stack, i.e. parameters and return
values don't fit.

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


Martin Nowak <code dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |critical


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


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com


--- Comment #1 from Walter Bright <bugzilla digitalmars.com> 2013-06-29
12:31:44 PDT ---
The code that generates this in cod3_thunk().

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



--- Comment #2 from Martin Nowak <code dawg.eu> 2013-06-29 12:45:31 PDT ---
We could probably assume, that any interface call kills EBX so that the caller
would have to save it. But a solution in accordance with the ABI would be
better.

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



--- Comment #3 from Walter Bright <bugzilla digitalmars.com> 2013-06-29
14:25:28 PDT ---
https://github.com/D-Programming-Language/dmd/pull/2278

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



--- Comment #4 from github-bugzilla puremagic.com 2013-07-01 19:03:54 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/c331f2021404ecc75e8e62bd4d46b92de573008c
fix Issue 10462 - interface thunk doesn't preserve EBX

https://github.com/D-Programming-Language/dmd/commit/51efce6654e35a3ccb737fd7146acfda7dbf1210
Merge pull request #2278 from WalterBright/fix10462

fix Issue 10462 - interface thunk doesn't preserve EBX

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



--- Comment #5 from github-bugzilla puremagic.com 2013-07-01 21:36:47 PDT ---
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/ed1174fb43c6abc3baa94c80c711227fc7ab6830
Merge pull request #2278 from WalterBright/fix10462

fix Issue 10462 - interface thunk doesn't preserve EBX

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



--- Comment #6 from Martin Nowak <code dawg.eu> 2013-07-19 14:50:12 PDT ---
The fix seems to work but I found another corner case.
Calling an interface thunk through a delegate still crashes.

----
cat > bug.d << CODE
void call(int delegate() dg)
{
    assert(dg() == 7);
}

interface I { int opCall(); }
class C : I { int opCall() { return 7; } }

void test()
{
    I i = new C;
    call(&i.opCall);
}
CODE

cat > main.d << CODE
import bug;
void main() { bug.test(); }
CODE

${DMD} -g -m32 -fPIC -shared bug.d -oflibbug.so
${DMD} -g -m32 main.d -L-L. -L-lbug -L-rpath=.
./main
----

The code generated to call the delegate trashes EBX.

<_D3bug4callFDFZiZv>:
...
mov    0x8(%ebp),%eax   // loads context ptr
mov    -0x4(%ebp),%ebx  // correctly loads GOT into EBX
mov    0xc(%ebp),%edx   // loads function ptr
mov    0x8(%ebp),%ebx   // overwrites EBX with context ptr ???
call   *%edx

The interface thunk call through call *%edx needs a correct EBX.

<_TMP3>:
sub    $0x8,%eax
jmp    d3e0 <_D3bug1C6opCallMFZi plt>

So the problematic instruction is the additional load into EBX.
This works correctly with optimized builds btw.

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



--- Comment #7 from Martin Nowak <code dawg.eu> 2013-07-19 15:30:46 PDT ---
https://github.com/D-Programming-Language/dmd/pull/2367

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



--- Comment #8 from github-bugzilla puremagic.com 2013-07-19 16:51:16 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/e3510d1b801808934ba074b357546960b0bc180e
fix Issue 10462 - call through function pointer might trash EBX

add EBX to the keepmsk after GOT was loaded

https://github.com/D-Programming-Language/dmd/commit/d4d0c61c510ed60b2601945a4252f6f6239594ca
Merge pull request #2367 from dawgfoto/fix10462

fix Issue 10462 - call through function pointer might trash EBX

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



--- Comment #9 from github-bugzilla puremagic.com 2013-07-19 16:51:56 PDT ---
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/109c41f5ff2f814982d02ce0932c95249934618d
Merge pull request #2367 from dawgfoto/fix10462

fix Issue 10462 - call through function pointer might trash EBX

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


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
            Version|D2                          |D1 & D2
         Resolution|                            |FIXED


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


Martin Nowak <code dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |siegelords_abode yahoo.com


--- Comment #10 from Martin Nowak <code dawg.eu> 2013-08-13 06:55:04 PDT ---
*** Issue 10515 has been marked as a duplicate of this issue. ***

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