llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[MS ABI] Clang doesn't generate local vftables with `dllimport`

Open majnemer opened this issue 11 years ago • 3 comments

Bugzilla Link 23549
Version unspecified
OS All
Blocks llvm/llvm-project#12849
CC @DougGregor,@jyu2-git,@zahiraam

Extended Description

consider:

struct _declspec(dllimport) A {
  A()
  virtual ~A();
};

A *f() { return new A();}

MSVC generates: This calls operator new and the imported A constructor. However, it also overwrites the VFPtr with a so-called "local vftable". I suspect this is to support overriding the global operator new and delete.

More information is available here: https://groups.google.com/d/msg/microsoft.public.vc.language/atSh_2VSc2w/EgJ3r_7OzVUJ

majnemer avatar May 17 '15 05:05 majnemer

I am sorry. I was past wrong result for x_cl.sh:

[0 llvm-23549]$ sh x_cl.sh
x_a.cpp
   Creating library x_a.lib and object x_a.exp
x.cpp
In EXE's ::operator new
Drawbot()
~Drawbot()
In EXE's ::operator delete
PASSED

while x_clang_cl.sh give:

[0 llvm-23549]$ sh x_clang_cl.sh
[0 llvm-23549]$ ./x.exe
In EXE's ::operator new
Drawbot()
~Drawbot()
In DLL's ::operator delete
FAILED
[116 llvm-23549]$

jyu2-git avatar Apr 23 '18 19:04 jyu2-git

small test case I attach a small test case, that fails with clang-cl but pass with MS cl

sh x_clang_cl.sh In EXE's ::operator new Drawbot() ~Drawbot() In DLL's ::operator delete FAILED sh x_cl.sh In EXE's ::operator new Drawbot() ~Drawbot() In DLL's ::operator delete FAILED

The test is to demonstrate a potential heap problem. Where pragma is define both operator new/delete in two different TU. The operator new is called from one TU, but operator delete is call from other TU (dll).

I think local vatble is still needed.

jyu2-git avatar Feb 02 '18 23:02 jyu2-git

Array new creates another symbol, the "local vftable constructor closure". I don't believe we will need this though.

majnemer avatar May 17 '15 05:05 majnemer