• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===------ TargetProcessControl.cpp -- Target process control APIs -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ExecutionEngine/Orc/TargetProcessControl.h"
10 
11 #include "llvm/ExecutionEngine/Orc/Core.h"
12 #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
13 #include "llvm/Support/Host.h"
14 #include "llvm/Support/Process.h"
15 
16 #include <mutex>
17 
18 namespace llvm {
19 namespace orc {
20 
~MemoryAccess()21 TargetProcessControl::MemoryAccess::~MemoryAccess() {}
22 
~TargetProcessControl()23 TargetProcessControl::~TargetProcessControl() {}
24 
SelfTargetProcessControl(std::shared_ptr<SymbolStringPool> SSP,Triple TargetTriple,unsigned PageSize,std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr)25 SelfTargetProcessControl::SelfTargetProcessControl(
26     std::shared_ptr<SymbolStringPool> SSP, Triple TargetTriple,
27     unsigned PageSize, std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr)
28     : TargetProcessControl(std::move(SSP)) {
29 
30   OwnedMemMgr = std::move(MemMgr);
31   if (!OwnedMemMgr)
32     OwnedMemMgr = std::make_unique<jitlink::InProcessMemoryManager>();
33 
34   this->TargetTriple = std::move(TargetTriple);
35   this->PageSize = PageSize;
36   this->MemMgr = OwnedMemMgr.get();
37   this->MemAccess = this;
38   if (this->TargetTriple.isOSBinFormatMachO())
39     GlobalManglingPrefix = '_';
40 }
41 
42 Expected<std::unique_ptr<SelfTargetProcessControl>>
Create(std::shared_ptr<SymbolStringPool> SSP,std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr)43 SelfTargetProcessControl::Create(
44     std::shared_ptr<SymbolStringPool> SSP,
45     std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr) {
46   auto PageSize = sys::Process::getPageSize();
47   if (!PageSize)
48     return PageSize.takeError();
49 
50   Triple TT(sys::getProcessTriple());
51 
52   return std::make_unique<SelfTargetProcessControl>(
53       std::move(SSP), std::move(TT), *PageSize, std::move(MemMgr));
54 }
55 
56 Expected<tpctypes::DylibHandle>
loadDylib(const char * DylibPath)57 SelfTargetProcessControl::loadDylib(const char *DylibPath) {
58   std::string ErrMsg;
59   auto Dylib = std::make_unique<sys::DynamicLibrary>(
60       sys::DynamicLibrary::getPermanentLibrary(DylibPath, &ErrMsg));
61   if (!Dylib->isValid())
62     return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
63   DynamicLibraries.push_back(std::move(Dylib));
64   return pointerToJITTargetAddress(DynamicLibraries.back().get());
65 }
66 
67 Expected<std::vector<tpctypes::LookupResult>>
lookupSymbols(ArrayRef<tpctypes::LookupRequest> Request)68 SelfTargetProcessControl::lookupSymbols(
69     ArrayRef<tpctypes::LookupRequest> Request) {
70   std::vector<tpctypes::LookupResult> R;
71 
72   for (auto &Elem : Request) {
73     auto *Dylib = jitTargetAddressToPointer<sys::DynamicLibrary *>(Elem.Handle);
74     assert(llvm::find_if(DynamicLibraries,
75                          [=](const std::unique_ptr<sys::DynamicLibrary> &DL) {
76                            return DL.get() == Dylib;
77                          }) != DynamicLibraries.end() &&
78            "Invalid handle");
79 
80     R.push_back(std::vector<JITTargetAddress>());
81     for (auto &KV : Elem.Symbols) {
82       auto &Sym = KV.first;
83       std::string Tmp((*Sym).data() + !!GlobalManglingPrefix,
84                       (*Sym).size() - !!GlobalManglingPrefix);
85       void *Addr = Dylib->getAddressOfSymbol(Tmp.c_str());
86       if (!Addr && KV.second == SymbolLookupFlags::RequiredSymbol) {
87         // FIXME: Collect all failing symbols before erroring out.
88         SymbolNameVector MissingSymbols;
89         MissingSymbols.push_back(Sym);
90         return make_error<SymbolsNotFound>(std::move(MissingSymbols));
91       }
92       R.back().push_back(pointerToJITTargetAddress(Addr));
93     }
94   }
95 
96   return R;
97 }
98 
99 Expected<int32_t>
runAsMain(JITTargetAddress MainFnAddr,ArrayRef<std::string> Args)100 SelfTargetProcessControl::runAsMain(JITTargetAddress MainFnAddr,
101                                     ArrayRef<std::string> Args) {
102   using MainTy = int (*)(int, char *[]);
103   return orc::runAsMain(jitTargetAddressToFunction<MainTy>(MainFnAddr), Args);
104 }
105 
106 Expected<tpctypes::WrapperFunctionResult>
runWrapper(JITTargetAddress WrapperFnAddr,ArrayRef<uint8_t> ArgBuffer)107 SelfTargetProcessControl::runWrapper(JITTargetAddress WrapperFnAddr,
108                                      ArrayRef<uint8_t> ArgBuffer) {
109   using WrapperFnTy =
110       tpctypes::CWrapperFunctionResult (*)(const uint8_t *Data, uint64_t Size);
111   auto *WrapperFn = jitTargetAddressToFunction<WrapperFnTy>(WrapperFnAddr);
112   return WrapperFn(ArgBuffer.data(), ArgBuffer.size());
113 }
114 
disconnect()115 Error SelfTargetProcessControl::disconnect() { return Error::success(); }
116 
writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws,WriteResultFn OnWriteComplete)117 void SelfTargetProcessControl::writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws,
118                                            WriteResultFn OnWriteComplete) {
119   for (auto &W : Ws)
120     *jitTargetAddressToPointer<uint8_t *>(W.Address) = W.Value;
121   OnWriteComplete(Error::success());
122 }
123 
writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws,WriteResultFn OnWriteComplete)124 void SelfTargetProcessControl::writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws,
125                                             WriteResultFn OnWriteComplete) {
126   for (auto &W : Ws)
127     *jitTargetAddressToPointer<uint16_t *>(W.Address) = W.Value;
128   OnWriteComplete(Error::success());
129 }
130 
writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws,WriteResultFn OnWriteComplete)131 void SelfTargetProcessControl::writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws,
132                                             WriteResultFn OnWriteComplete) {
133   for (auto &W : Ws)
134     *jitTargetAddressToPointer<uint32_t *>(W.Address) = W.Value;
135   OnWriteComplete(Error::success());
136 }
137 
writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws,WriteResultFn OnWriteComplete)138 void SelfTargetProcessControl::writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws,
139                                             WriteResultFn OnWriteComplete) {
140   for (auto &W : Ws)
141     *jitTargetAddressToPointer<uint64_t *>(W.Address) = W.Value;
142   OnWriteComplete(Error::success());
143 }
144 
writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws,WriteResultFn OnWriteComplete)145 void SelfTargetProcessControl::writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws,
146                                             WriteResultFn OnWriteComplete) {
147   for (auto &W : Ws)
148     memcpy(jitTargetAddressToPointer<char *>(W.Address), W.Buffer.data(),
149            W.Buffer.size());
150   OnWriteComplete(Error::success());
151 }
152 
153 } // end namespace orc
154 } // end namespace llvm
155