llvm-project/llvm/lib/ExecutionEngine/Orc/COFFVCRuntimeSupport.cpp
Lang Hames 6fa8657a62
[ORC] Refactor visit-members in StaticLibraryDefinitionGenerator. (#141546)
This refactor was motivated by two bugs identified in out-of-tree
builds:

1. Some implementations of the VisitMembersFunction type (often used to	
implement special loading semantics, e.g. -all_load or -ObjC) were assuming
that buffers for archive members were null-terminated, which they are not in
general. This was triggering occasional assertions.

2. Archives may include multiple members with the same file name, e.g.
when constructed by appending files with the same name:
  % llvm-ar crs libfoo.a foo.o
  % llvm-ar q libfoo.a foo.o
  % llvm-ar t libfoo.a foo.o
  foo.o

   While confusing, these members may be safe to link (provided that they're
   individually valid and don't define duplicate symbols). In ORC however, the
   archive member name may be used to construct an ORC initializer symbol,
   which must also be unique. In that case the duplicate member names lead to a
   duplicate definition error even if the members define unrelated symbols.

In addition to these bugs, StaticLibraryDefinitionGenerator had grown a
collection of all member buffers (ObjectFilesMap), a BumpPtrAllocator
that was redundantly storing synthesized archive member names (these are
copied into the MemoryBuffers created for each Object, but were never
freed in the allocator), and a set of COFF-specific import files.

To fix the bugs above and simplify StaticLibraryDefinitionGenerator this
patch makes the following changes:

1. StaticLibraryDefinitionGenerator::VisitMembersFunction is generalized
   to take a reference to the containing archive, and the index of the
   member within the archive. It now returns an Expected<bool> indicating
   whether the member visited should be treated as loadable, not loadable,
   or as invalidating the entire archive.
2. A static StaticLibraryDefinitionGenerator::createMemberBuffer method
   is added which creates MemoryBuffers with unique names of the form
   `<archive-name>[<index>](<member-name>)`. This defers construction of
   member names until they're loaded, allowing the BumpPtrAllocator (with
   its redundant name storage) to be removed.
3. The ObjectFilesMap (symbol name -> memory-buffer-ref) is replaced
   with a SymbolToMemberIndexMap (symbol name -> index) which should be
   smaller and faster to construct.
4. The 'loadability' result from VisitMemberFunctions is now taken into
   consideration when building the SymbolToMemberIndexMap so that members
   that have already been loaded / filtered out can be skipped, and do not
   take up any ongoing space.
5. The COFF ImportedDynamicLibraries member is moved out into the
   COFFImportFileScanner utility, which can be used as a
   VisitMemberFunction.

This fixes the bugs described above; and should lower memory consumption
slightly, especially for archives with many files and / or symbol where
most files are eventually loaded.
2025-05-27 20:58:53 +10:00

187 lines
6.8 KiB
C++

//===------- COFFVCRuntimeSupport.cpp - VC runtime support in ORC ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/COFFVCRuntimeSupport.h"
#include "llvm/ExecutionEngine/Orc/COFF.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/WindowsDriver/MSVCPaths.h"
#define DEBUG_TYPE "orc"
using namespace llvm;
using namespace llvm::orc;
using namespace llvm::orc::shared;
Expected<std::unique_ptr<COFFVCRuntimeBootstrapper>>
COFFVCRuntimeBootstrapper::Create(ExecutionSession &ES,
ObjectLinkingLayer &ObjLinkingLayer,
const char *RuntimePath) {
return std::unique_ptr<COFFVCRuntimeBootstrapper>(
new COFFVCRuntimeBootstrapper(ES, ObjLinkingLayer, RuntimePath));
}
COFFVCRuntimeBootstrapper::COFFVCRuntimeBootstrapper(
ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer,
const char *RuntimePath)
: ES(ES), ObjLinkingLayer(ObjLinkingLayer) {
if (RuntimePath)
this->RuntimePath = RuntimePath;
}
Expected<std::vector<std::string>>
COFFVCRuntimeBootstrapper::loadStaticVCRuntime(JITDylib &JD,
bool DebugVersion) {
StringRef VCLibs[] = {"libvcruntime.lib", "libcmt.lib", "libcpmt.lib"};
StringRef UCRTLibs[] = {"libucrt.lib"};
std::vector<std::string> ImportedLibraries;
if (auto Err = loadVCRuntime(JD, ImportedLibraries, ArrayRef(VCLibs),
ArrayRef(UCRTLibs)))
return std::move(Err);
return ImportedLibraries;
}
Expected<std::vector<std::string>>
COFFVCRuntimeBootstrapper::loadDynamicVCRuntime(JITDylib &JD,
bool DebugVersion) {
StringRef VCLibs[] = {"vcruntime.lib", "msvcrt.lib", "msvcprt.lib"};
StringRef UCRTLibs[] = {"ucrt.lib"};
std::vector<std::string> ImportedLibraries;
if (auto Err = loadVCRuntime(JD, ImportedLibraries, ArrayRef(VCLibs),
ArrayRef(UCRTLibs)))
return std::move(Err);
return ImportedLibraries;
}
Error COFFVCRuntimeBootstrapper::loadVCRuntime(
JITDylib &JD, std::vector<std::string> &ImportedLibraries,
ArrayRef<StringRef> VCLibs, ArrayRef<StringRef> UCRTLibs) {
MSVCToolchainPath Path;
if (!RuntimePath.empty()) {
Path.UCRTSdkLib = RuntimePath;
Path.VCToolchainLib = RuntimePath;
} else {
auto ToolchainPath = getMSVCToolchainPath();
if (!ToolchainPath)
return ToolchainPath.takeError();
Path = *ToolchainPath;
}
LLVM_DEBUG({
dbgs() << "Using VC toolchain pathes\n";
dbgs() << " VC toolchain path: " << Path.VCToolchainLib << "\n";
dbgs() << " UCRT path: " << Path.UCRTSdkLib << "\n";
});
auto LoadLibrary = [&](SmallString<256> LibPath, StringRef LibName) -> Error {
sys::path::append(LibPath, LibName);
std::set<std::string> NewImportedLibraries;
auto G = StaticLibraryDefinitionGenerator::Load(
ObjLinkingLayer, LibPath.c_str(),
COFFImportFileScanner(NewImportedLibraries));
if (!G)
return G.takeError();
llvm::append_range(ImportedLibraries, NewImportedLibraries);
JD.addGenerator(std::move(*G));
return Error::success();
};
for (auto &Lib : UCRTLibs)
if (auto Err = LoadLibrary(Path.UCRTSdkLib, Lib))
return Err;
for (auto &Lib : VCLibs)
if (auto Err = LoadLibrary(Path.VCToolchainLib, Lib))
return Err;
ImportedLibraries.push_back("ntdll.dll");
ImportedLibraries.push_back("Kernel32.dll");
return Error::success();
}
Error COFFVCRuntimeBootstrapper::initializeStaticVCRuntime(JITDylib &JD) {
ExecutorAddr jit_scrt_initialize, jit_scrt_dllmain_before_initialize_c,
jit_scrt_initialize_type_info,
jit_scrt_initialize_default_local_stdio_options;
if (auto Err = lookupAndRecordAddrs(
ES, LookupKind::Static, makeJITDylibSearchOrder(&JD),
{{ES.intern("__scrt_initialize_crt"), &jit_scrt_initialize},
{ES.intern("__scrt_dllmain_before_initialize_c"),
&jit_scrt_dllmain_before_initialize_c},
{ES.intern("?__scrt_initialize_type_info@@YAXXZ"),
&jit_scrt_initialize_type_info},
{ES.intern("__scrt_initialize_default_local_stdio_options"),
&jit_scrt_initialize_default_local_stdio_options}}))
return Err;
auto RunVoidInitFunc = [&](ExecutorAddr Addr) -> Error {
if (auto Res = ES.getExecutorProcessControl().runAsVoidFunction(Addr))
return Error::success();
else
return Res.takeError();
};
auto R =
ES.getExecutorProcessControl().runAsIntFunction(jit_scrt_initialize, 0);
if (!R)
return R.takeError();
if (auto Err = RunVoidInitFunc(jit_scrt_dllmain_before_initialize_c))
return Err;
if (auto Err = RunVoidInitFunc(jit_scrt_initialize_type_info))
return Err;
if (auto Err =
RunVoidInitFunc(jit_scrt_initialize_default_local_stdio_options))
return Err;
SymbolAliasMap Alias;
Alias[ES.intern("__run_after_c_init")] = {
ES.intern("__scrt_dllmain_after_initialize_c"), JITSymbolFlags::Exported};
if (auto Err = JD.define(symbolAliases(Alias)))
return Err;
return Error::success();
}
Expected<COFFVCRuntimeBootstrapper::MSVCToolchainPath>
COFFVCRuntimeBootstrapper::getMSVCToolchainPath() {
std::string VCToolChainPath;
ToolsetLayout VSLayout;
IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem();
if (!findVCToolChainViaCommandLine(*VFS, std::nullopt, std::nullopt,
std::nullopt, VCToolChainPath, VSLayout) &&
!findVCToolChainViaEnvironment(*VFS, VCToolChainPath, VSLayout) &&
!findVCToolChainViaSetupConfig(*VFS, {}, VCToolChainPath, VSLayout) &&
!findVCToolChainViaRegistry(VCToolChainPath, VSLayout))
return make_error<StringError>("Couldn't find msvc toolchain.",
inconvertibleErrorCode());
std::string UniversalCRTSdkPath;
std::string UCRTVersion;
if (!getUniversalCRTSdkDir(*VFS, std::nullopt, std::nullopt, std::nullopt,
UniversalCRTSdkPath, UCRTVersion))
return make_error<StringError>("Couldn't find universal sdk.",
inconvertibleErrorCode());
MSVCToolchainPath ToolchainPath;
SmallString<256> VCToolchainLib(VCToolChainPath);
sys::path::append(VCToolchainLib, "lib", "x64");
ToolchainPath.VCToolchainLib = VCToolchainLib;
SmallString<256> UCRTSdkLib(UniversalCRTSdkPath);
sys::path::append(UCRTSdkLib, "Lib", UCRTVersion, "ucrt", "x64");
ToolchainPath.UCRTSdkLib = UCRTSdkLib;
return ToolchainPath;
}