RuntimeDyLdCOFF and RTTI on Windows

HI Stefan,

CC’ing Reid Kleckner, who might have some insight here, and llvm-dev as this may be of interest to other windows JIT users.

I am facing the issue that C++ dynamic_cast doesn’t work for types
loaded from object files with RuntimeDyLd.

Do you think it is possible that RuntimeDyLd misses type info data in
the COFF file or doesn’t wire it up correctly?
I set ProcessAllSections = true, but I didn’t recognize any change. I
found that RuntimeDyLdCOFF does not override finalizeLoad like the ELF
and MachO versions. The function call’s comment reads “Give the
subclasses a chance to tie-up any loose ends” – possibly missing
functionality?

Unfortunately I don’t have a windows machine to test on, so it’s difficult to know for sure. From a quick look at the IR, it seems like the Window’s C++ ABI implementation of dynamic_cast works similarly to Darwin’s: The type info pointers for the reference type and cast type are passed in to the function, so as long as memory has been allocated for the type info I would have expected this to “just work”. My best guess for why it wouldn’t is that RuntimeDyldCOFF is a missing relocation somewhere. What happens when you run this code on a debug build? Do you hit the llvm_unreachable at the bottom of the resolveRelocation switch?

Just to rule out one other possibility, Reid: does Windows require any special calls to register C++ RTTI?

Finally, regarding the ProcessAllSections flag: it tells RuntimeDyld to call the memory manager interface for every section, not just the sections that RuntimeDyld thinks are necessary for execution. This was a hack to make debug info sections visible to clients who are interested in them. It can be important if your object file contains metadata sections that are required, but not referenced in the file. I don’t think it should affect this case though.

  • Lang.

Thanks Lang for forwarding this to the list
The symptom in a nutshell: I cannot get dynamic_cast to work in JITed code on Windows

Reid, do you have an idea whether: it’s a bug / it’s just not implemented yet / I am missing something?

Just to rule out one other possibility, Reid: does Windows require any special calls to register C++ RTTI?

There’s some more details in the original mail.

Thanks
Stefan

Hi Lang, hi dev-list (as it may be interesting for others too)

With the cpp file attached, the repro is actually as simple as this:
$ clang -std=c++11 -emit-llvm repro_input.cpp -c -o repro_input.bc
$ lli repro_input.bc

On Mac this prints:
dynamic_cast worked! dummy is 1

On Windows this prints:
dynamic_cast failed

As the issue is reproducible with lli, it’s probably unrelated to the COFF implementation as originally stated.
I built LLVM and Clang from release39 HEAD and used both, clang and lli from this build. OS versions and CMake flags are:

  • OSX 10.10.6 with -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_EH=ON -DLLVM_TARGETS_TO_BUILD=X86
  • Windows 10 64bit OS Build 14393.222 with -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_EH=ON -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_USE_CRT_DEBUG=MDd -DLLVM_USE_CRT_RELEASE=MD

Hope this helps nailing down the issue. Maybe I can help fixing it, if you can provide a few pointers where to start :slight_smile: If you need any more info please let me know.

Cheers
Stefan

repro_input.cpp (670 Bytes)

I have a similar build tree to what you describe on Windows, but I get this:

$ clang -c -emit-llvm repro_input.cpp -std=c++11 -o t.bc
$ lli t.bc

LLVM ERROR: Program used external function ‘??_7type_info@@6B@’ which could not be resolved!

It looks like people have already reported similar issues. Have you done anything to get past this kind of problem?

Hi Reid, thanks for looking into that.

but I get this:
LLVM ERROR: Program used external function '??_7type_info@@6B@' which
could not be resolved!

That's right, sorry I accidentally used our own adjusted version and it
silently skips unresolved symbols in release builds [1]. On head of
release39 I get this too.

Have you done anything to get past this kind of problem?

It's the external "const type_info::`vftable'" and it seems correctly
resolved by linking to msvcrt.lib/msvcrtd.lib. I think this is correct
because building the repro with Visual Studio and
/NODEFAULTLIB:"msvcrt.lib" brings up the same unresolved symbol.

Anyway, lli will report the next problem:

$ lli -extra-archive="C:\Program Files (x86)\Microsoft Visual Studio
14.0\VC\lib\amd64\msvcrt.lib" repro_input.bc
LLVM ERROR: Program used external function '??_Etype_info@@UEAAPEAXI@Z'
which could not be resolved!

This is a WeakExternal symbol referring to:
public: virtual void * __cdecl type_info::`vector deleting
destructor'(unsigned int))

In our production system we're currently just ignoring it, as I didn't
figure this one. I cannot reproduce it with Visual Studio and reading
about it a bit more let us assume it's probably not urgently relevant
yet [2].

However, this may well be related to the problem with RTTI!

Best
Stefan

[1]
https://github.com/weliveindetail/pj-llvm/commit/58e09372c29d6c60cd982cd1b560a692fcf85de8

[2] "6) Deleting Destructors" in

LLVM ERROR: Program used external function '??_7type_info@@6B@' which
could not be resolved!

Have you done anything to get past this kind of problem?

It's the external "const type_info::`vftable'" and it seems correctly
resolved by linking to msvcrt.lib/msvcrtd.lib. I think this is correct
because building the repro with Visual Studio and
/NODEFAULTLIB:"msvcrt.lib" brings up the same unresolved symbol.

Quoting VC\crt\src\vcruntime\std_type_info_static.cpp, line 6:
Definition of the std::type_info destructor. This symbol is needed in
the import library. Any class with virtual member functions compiled
with /GR will depend on the type_info::`vftable` symbol. This definition
of the destructor ensures that this symbol will be found, even if no
user source file includes <typeinfo>.

So IIUC it's meant to be a fallback. Anyway, this looks promising
VC\crt\src\vcruntime\vcstartup_internal.h, line 218:

void __CRTDECL __scrt_initialize_type_info();
void __CRTDECL __scrt_uninitialize_type_info();