Bug 10462 – interface thunk doesn't preserve EBX

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2013-06-24T06:13:55Z
Last change time
2021-08-03T15:06:00Z
Assigned to
No Owner
Creator
Martin Nowak

Comments

Comment #0 by code — 2013-06-24T06:13:55Z
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.
Comment #1 by bugzilla — 2013-06-29T12:31:44Z
The code that generates this in cod3_thunk().
Comment #2 by code — 2013-06-29T12:45:31Z
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.
Comment #3 by bugzilla — 2013-06-29T14:25:28Z
Comment #4 by github-bugzilla — 2013-07-01T19:03:54Z
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
Comment #5 by github-bugzilla — 2013-07-01T21:36:47Z
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
Comment #6 by code — 2013-07-19T14:50:12Z
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.
Comment #7 by code — 2013-07-19T15:30:46Z
Comment #8 by github-bugzilla — 2013-07-19T16:51:16Z
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
Comment #9 by github-bugzilla — 2013-07-19T16:51:56Z
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
Comment #10 by code — 2013-08-13T06:55:04Z
*** Issue 10515 has been marked as a duplicate of this issue. ***
Comment #11 by dlang-bot — 2021-08-03T15:06:00Z
dlang/dmd pull request #12950 "fix Issue 22170 - interface thunk doesn't set EBX to GOT" was merged into stable: - 10ccea37a107097425396b303429e8ba31ccde52 by Iain Buclaw: dshell: Add dll tests for issue 10462 https://github.com/dlang/dmd/pull/12950