Files
clang-p2996/compiler-rt/lib/orc/executor_address.h
Lang Hames bb5f97e3ad [ORC][ORC-RT] Introduce ORC-runtime based MachO-Platform.
Adds support for MachO static initializers/deinitializers and eh-frame
registration via the ORC runtime.

This commit introduces cooperative support code into the ORC runtime and ORC
LLVM libraries (especially the MachOPlatform class) to support macho runtime
features for JIT'd code. This commit introduces support for static
initializers, static destructors (via cxa_atexit interposition), and eh-frame
registration. Near-future commits will add support for MachO native
thread-local variables, and language runtime registration (e.g. for Objective-C
and Swift).

The llvm-jitlink tool is updated to use the ORC runtime where available, and
regression tests for the new MachOPlatform support are added to compiler-rt.

Notable changes on the ORC runtime side:

1. The new macho_platform.h / macho_platform.cpp files contain the bulk of the
runtime-side support. This includes eh-frame registration; jit versions of
dlopen, dlsym, and dlclose; a cxa_atexit interpose to record static destructors,
and an '__orc_rt_macho_run_program' function that defines running a JIT'd MachO
program in terms of the jit- dlopen/dlsym/dlclose functions.

2. Replaces JITTargetAddress (and casting operations) with ExecutorAddress
(copied from LLVM) to improve type-safety of address management.

3. Adds serialization support for ExecutorAddress and unordered_map types to
the runtime-side Simple Packed Serialization code.

4. Adds orc-runtime regression tests to ensure that static initializers and
cxa-atexit interposes work as expected.

Notable changes on the LLVM side:

1. The MachOPlatform class is updated to:

  1.1. Load the ORC runtime into the ExecutionSession.
  1.2. Set up standard aliases for macho-specific runtime functions. E.g.
       ___cxa_atexit -> ___orc_rt_macho_cxa_atexit.
  1.3. Install the MachOPlatformPlugin to scrape LinkGraphs for information
       needed to support MachO features (e.g. eh-frames, mod-inits), and
       communicate this information to the runtime.
  1.4. Provide entry-points that the runtime can call to request initializers,
       perform symbol lookup, and request deinitialiers (the latter is
       implemented as an empty placeholder as macho object deinits are rarely
       used).
  1.5. Create a MachO header object for each JITDylib (defining the __mh_header
       and __dso_handle symbols).

2. The llvm-jitlink tool (and llvm-jitlink-executor) are updated to use the
runtime when available.

3. A `lookupInitSymbolsAsync` method is added to the Platform base class. This
can be used to issue an async lookup for initializer symbols. The existing
`lookupInitSymbols` method is retained (the GenericIRPlatform code is still
using it), but is deprecated and will be removed soon.

4. JIT-dispatch support code is added to ExecutorProcessControl.

The JIT-dispatch system allows handlers in the JIT process to be associated with
'tag' symbols in the executor, and allows the executor to make remote procedure
calls back to the JIT process (via __orc_rt_jit_dispatch) using those tags.

The primary use case is ORC runtime code that needs to call bakc to handlers in
orc::Platform subclasses. E.g. __orc_rt_macho_jit_dlopen calling back to
MachOPlatform::rt_getInitializers using __orc_rt_macho_get_initializers_tag.
(The system is generic however, and could be used by non-runtime code).

The new ExecutorProcessControl::JITDispatchInfo struct provides the address
(in the executor) of the jit-dispatch function and a jit-dispatch context
object, and implementations of the dispatch function are added to
SelfExecutorProcessControl and OrcRPCExecutorProcessControl.

5. OrcRPCTPCServer is updated to support JIT-dispatch calls over ORC-RPC.

6. Serialization support for StringMap is added to the LLVM-side Simple Packed
Serialization code.

7. A JITLink::allocateBuffer operation is introduced to allocate writable memory
attached to the graph. This is used by the MachO header synthesis code, and will
be generically useful for other clients who want to create new graph content
from scratch.
2021-07-19 19:50:16 +10:00

209 lines
6.5 KiB
C++

//===------ ExecutorAddress.h - Executing process address -------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Represents an address in the executing program.
//
// This file was derived from
// llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h.
//
//===----------------------------------------------------------------------===//
#ifndef ORC_RT_EXECUTOR_ADDRESS_H
#define ORC_RT_EXECUTOR_ADDRESS_H
#include "adt.h"
#include "simple_packed_serialization.h"
#include <cassert>
#include <type_traits>
namespace __orc_rt {
/// Represents the difference between two addresses in the executor process.
class ExecutorAddrDiff {
public:
ExecutorAddrDiff() = default;
explicit ExecutorAddrDiff(uint64_t Value) : Value(Value) {}
uint64_t getValue() const { return Value; }
private:
int64_t Value = 0;
};
/// Represents an address in the executor process.
class ExecutorAddress {
public:
ExecutorAddress() = default;
explicit ExecutorAddress(uint64_t Addr) : Addr(Addr) {}
/// Create an ExecutorAddress from the given pointer.
/// Warning: This should only be used when JITing in-process.
template <typename T> static ExecutorAddress fromPtr(T *Value) {
return ExecutorAddress(
static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Value)));
}
/// Cast this ExecutorAddress to a pointer of the given type.
/// Warning: This should only be esude when JITing in-process.
template <typename T> T toPtr() const {
static_assert(std::is_pointer<T>::value, "T must be a pointer type");
uintptr_t IntPtr = static_cast<uintptr_t>(Addr);
assert(IntPtr == Addr &&
"JITTargetAddress value out of range for uintptr_t");
return reinterpret_cast<T>(IntPtr);
}
uint64_t getValue() const { return Addr; }
void setValue(uint64_t Addr) { this->Addr = Addr; }
bool isNull() const { return Addr == 0; }
explicit operator bool() const { return Addr != 0; }
friend bool operator==(const ExecutorAddress &LHS,
const ExecutorAddress &RHS) {
return LHS.Addr == RHS.Addr;
}
friend bool operator!=(const ExecutorAddress &LHS,
const ExecutorAddress &RHS) {
return LHS.Addr != RHS.Addr;
}
friend bool operator<(const ExecutorAddress &LHS,
const ExecutorAddress &RHS) {
return LHS.Addr < RHS.Addr;
}
friend bool operator<=(const ExecutorAddress &LHS,
const ExecutorAddress &RHS) {
return LHS.Addr <= RHS.Addr;
}
friend bool operator>(const ExecutorAddress &LHS,
const ExecutorAddress &RHS) {
return LHS.Addr > RHS.Addr;
}
friend bool operator>=(const ExecutorAddress &LHS,
const ExecutorAddress &RHS) {
return LHS.Addr >= RHS.Addr;
}
ExecutorAddress &operator++() {
++Addr;
return *this;
}
ExecutorAddress &operator--() {
--Addr;
return *this;
}
ExecutorAddress operator++(int) { return ExecutorAddress(Addr++); }
ExecutorAddress operator--(int) { return ExecutorAddress(Addr++); }
ExecutorAddress &operator+=(const ExecutorAddrDiff Delta) {
Addr += Delta.getValue();
return *this;
}
ExecutorAddress &operator-=(const ExecutorAddrDiff Delta) {
Addr -= Delta.getValue();
return *this;
}
private:
uint64_t Addr = 0;
};
/// Subtracting two addresses yields an offset.
inline ExecutorAddrDiff operator-(const ExecutorAddress &LHS,
const ExecutorAddress &RHS) {
return ExecutorAddrDiff(LHS.getValue() - RHS.getValue());
}
/// Adding an offset and an address yields an address.
inline ExecutorAddress operator+(const ExecutorAddress &LHS,
const ExecutorAddrDiff &RHS) {
return ExecutorAddress(LHS.getValue() + RHS.getValue());
}
/// Adding an address and an offset yields an address.
inline ExecutorAddress operator+(const ExecutorAddrDiff &LHS,
const ExecutorAddress &RHS) {
return ExecutorAddress(LHS.getValue() + RHS.getValue());
}
/// Represents an address range in the exceutor process.
struct ExecutorAddressRange {
ExecutorAddressRange() = default;
ExecutorAddressRange(ExecutorAddress StartAddress, ExecutorAddress EndAddress)
: StartAddress(StartAddress), EndAddress(EndAddress) {}
bool empty() const { return StartAddress == EndAddress; }
ExecutorAddrDiff size() const { return EndAddress - StartAddress; }
template <typename T> span<T> toSpan() const {
assert(size().getValue() % sizeof(T) == 0 &&
"AddressRange is not a multiple of sizeof(T)");
return span<T>(StartAddress.toPtr<T *>(), size().getValue() / sizeof(T));
}
ExecutorAddress StartAddress;
ExecutorAddress EndAddress;
};
/// SPS serializatior for ExecutorAddress.
template <> class SPSSerializationTraits<SPSExecutorAddress, ExecutorAddress> {
public:
static size_t size(const ExecutorAddress &EA) {
return SPSArgList<uint64_t>::size(EA.getValue());
}
static bool serialize(SPSOutputBuffer &BOB, const ExecutorAddress &EA) {
return SPSArgList<uint64_t>::serialize(BOB, EA.getValue());
}
static bool deserialize(SPSInputBuffer &BIB, ExecutorAddress &EA) {
uint64_t Tmp;
if (!SPSArgList<uint64_t>::deserialize(BIB, Tmp))
return false;
EA = ExecutorAddress(Tmp);
return true;
}
};
using SPSExecutorAddressRange =
SPSTuple<SPSExecutorAddress, SPSExecutorAddress>;
/// Serialization traits for address ranges.
template <>
class SPSSerializationTraits<SPSExecutorAddressRange, ExecutorAddressRange> {
public:
static size_t size(const ExecutorAddressRange &Value) {
return SPSArgList<SPSExecutorAddress, SPSExecutorAddress>::size(
Value.StartAddress, Value.EndAddress);
}
static bool serialize(SPSOutputBuffer &BOB,
const ExecutorAddressRange &Value) {
return SPSArgList<SPSExecutorAddress, SPSExecutorAddress>::serialize(
BOB, Value.StartAddress, Value.EndAddress);
}
static bool deserialize(SPSInputBuffer &BIB, ExecutorAddressRange &Value) {
return SPSArgList<SPSExecutorAddress, SPSExecutorAddress>::deserialize(
BIB, Value.StartAddress, Value.EndAddress);
}
};
using SPSExecutorAddressRangeSequence = SPSSequence<SPSExecutorAddressRange>;
} // End namespace __orc_rt
#endif // ORC_RT_EXECUTOR_ADDRESS_H