[llvm-bugs] [Bug 26905] novtable support

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Mar 10 13:56:34 PST 2016


https://llvm.org/bugs/show_bug.cgi?id=26905

Reid Kleckner <rnk at google.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |rnk at google.com
         Resolution|---                         |INVALID

--- Comment #1 from Reid Kleckner <rnk at google.com> ---
Your program is working because MSVC aggressively devirtualizes method calls in
constructors. Consider this reduction and the code generated by both compilers:

$ cat t.cpp
struct __declspec(novtable) A { A(); virtual void Init() = 0; };
struct __declspec(novtable) B : A { B(); void Init(); };
B::B() { this->Init(); }
$ clang -S t.cpp  -o -
...
        movq    %rcx, 48(%rsp)
        movq    %rcx, 40(%rsp)          # 8-byte Spill
        callq   "??0A@@QEAA at XZ"
        movq    40(%rsp), %rcx          # 8-byte Reload
        movq    (%rcx), %rdx
        movq    (%rdx), %rdx
        movq    %rax, 32(%rsp)          # 8-byte Spill
        callq   *%rdx
$ cl -c t.cpp -Facl.asm && cat cl.asm
...
        mov     QWORD PTR [rsp+8], rcx
        sub     rsp, 40                                 ; 00000028H
        mov     rcx, QWORD PTR this$[rsp]
        call    ??0A@@QEAA at XZ                           ; A::A
        mov     rcx, QWORD PTR this$[rsp]
        call    ?Init at B@@UEAAXXZ                        ; B::Init
        mov     rax, QWORD PTR this$[rsp]
        add     rsp, 40                                 ; 00000028H
        ret     0

The difference is that Clang's constructor for IteratedHashWithStaticTransform
is doing a virtual call for IteratedHashWithStaticTransform::Init, instead of
doing a direct call. If you annotate this class with declspec(novtable), then
the vptr slot is not initialized, and you will crash.

To me, the user is misusing novtable by applying it to a non-abstract class. If
you changed the source program slightly to invoke another virtual method from
within 'Init', your program would crash with MSVC as well.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160310/79dface3/attachment-0001.html>


More information about the llvm-bugs mailing list