Skip to content

ABI specification of interfaces misleading #4322

@fieldvoid

Description

@fieldvoid

The spec says "An interface is a pointer to a pointer to a vtbl[]. The vtbl[0] entry is a pointer to the corresponding instance of the object.Interface class." However an interface is actually a vtbl*.

interface A {
  void foo ();
}
interface B {
  void bar ();
}
interface C : A, B {
}
final class D : C {
  void foo () {
  }

  void bar () {
  }
}
void main () {
  C p = new D;
  p.foo;
  p.bar;
}
_Dmain:
0000:   55                       push      RBP
0001:   48 8B EC                 mov       RBP,RSP
0004:   48 83 EC 10              sub       RSP,010h
0008:   E8 00 00 00 00           call      L0
000d:   48 85 C0                 test      RAX,RAX
0010:   74 06                    je        L18
0012:   48 8D 40 10              lea       RAX,010h[RAX]
0016:   EB 02                    jmp short L1a
0018:   31 C0                    xor       EAX,EAX
001a:   48 89 45 F8              mov       -8[RBP],RAX
001e:   48 89 C7                 mov       RDI,RAX
0021:   48 8B 08                 mov       RCX,[RAX]
0024:   48 FF 51 08              call      qword ptr 8[RCX]
0028:   48 83 7D F8 00           cmp       qword ptr -8[RBP],0
002d:   74 0A                    je        L39
002f:   48 8B 55 F8              mov       RDX,-8[RBP]
0033:   48 8D 7A 08              lea       RDI,8[RDX]
0037:   EB 02                    jmp short L3b
0039:   31 FF                    xor       EDI,EDI
003b:   48 83 7D F8 00           cmp       qword ptr -8[RBP],0
0040:   74 0A                    je        L4c
0042:   48 8B 45 F8              mov       RAX,-8[RBP]
0046:   48 8D 48 08              lea       RCX,8[RAX]
004a:   EB 02                    jmp short L4e
004c:   31 C9                    xor       ECX,ECX
004e:   48 8B 11                 mov       RDX,[RCX]
0051:   48 FF 52 08              call      qword ptr 8[RDX]
0055:   31 C0                    xor       EAX,EAX
0057:   C9                       leave
0058:   C3                       ret
main:
0000:   55                       push      RBP
0001:   48 8B EC                 mov       RBP,RSP
0004:   48 8B 15 FC FF FF FF     mov       RDX,[0FFFFFFFCh][RIP]
000b:   E8 00 00 00 00           call      L0
0010:   5D                       pop       RBP
0011:   C3                       ret

The reduced form, or points of interest are:

test      RAX,RAX
...
mov       RCX,[RAX]
...
call      qword ptr 8[RCX]

This is roughly equivalent to:

vtbl* RAX = L0();
vtbl RCX = *RAX;
(RCX + 8)();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions