Skip to content

Commit b77c6db

Browse files
committed
[JITLink] Fix alloc action call signature in InProcessMemoryManager.
Alloc actions should return a CWrapperFunctionResult. JITLink does not have access to this type yet, due to library layering issues, so add a cut-down version with a fixme.
1 parent ada5458 commit b77c6db

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

llvm/lib/ExecutionEngine/JITLink/JITLinkMemoryManager.cpp

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,62 @@
1313

1414
#define DEBUG_TYPE "jitlink"
1515

16+
using namespace llvm;
17+
18+
namespace {
19+
20+
// FIXME: Remove this copy of CWrapperFunctionResult as soon as JITLink can
21+
// depend on shared utils from Orc.
22+
23+
// Must be kept in-sync with compiler-rt/lib/orc/c-api.h.
24+
union CWrapperFunctionResultDataUnion {
25+
char *ValuePtr;
26+
char Value[sizeof(ValuePtr)];
27+
};
28+
29+
// Must be kept in-sync with compiler-rt/lib/orc/c-api.h.
30+
typedef struct {
31+
CWrapperFunctionResultDataUnion Data;
32+
size_t Size;
33+
} CWrapperFunctionResult;
34+
35+
Error toError(CWrapperFunctionResult R) {
36+
bool HasError = false;
37+
std::string ErrMsg;
38+
if (R.Size) {
39+
bool Large = R.Size > sizeof(CWrapperFunctionResultDataUnion);
40+
char *Content = Large ? R.Data.ValuePtr : R.Data.Value;
41+
if (Content[0]) {
42+
HasError = true;
43+
ErrMsg.resize(R.Size - 1);
44+
memcpy(&ErrMsg[0], Content + 1, R.Size - 1);
45+
}
46+
if (Large)
47+
free(R.Data.ValuePtr);
48+
} else if (R.Data.ValuePtr) {
49+
HasError = true;
50+
ErrMsg = R.Data.ValuePtr;
51+
free(R.Data.ValuePtr);
52+
}
53+
54+
if (HasError)
55+
return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
56+
return Error::success();
57+
}
58+
} // namespace
59+
1660
namespace llvm {
1761
namespace jitlink {
1862

1963
JITLinkMemoryManager::~JITLinkMemoryManager() = default;
2064
JITLinkMemoryManager::InFlightAlloc::~InFlightAlloc() = default;
2165

2266
static Error runAllocAction(JITLinkMemoryManager::AllocActionCall &C) {
23-
using DeallocFnTy = char *(*)(const void *, size_t);
24-
auto *Fn = jitTargetAddressToPointer<DeallocFnTy>(C.FnAddr);
25-
26-
if (char *ErrMsg = Fn(jitTargetAddressToPointer<const void *>(C.CtxAddr),
27-
static_cast<size_t>(C.CtxSize))) {
28-
auto E = make_error<StringError>(ErrMsg, inconvertibleErrorCode());
29-
free(ErrMsg);
30-
return E;
31-
}
67+
using WrapperFnTy = CWrapperFunctionResult (*)(const void *, size_t);
68+
auto *Fn = jitTargetAddressToPointer<WrapperFnTy>(C.FnAddr);
3269

33-
return Error::success();
70+
return toError(Fn(jitTargetAddressToPointer<const void *>(C.CtxAddr),
71+
static_cast<size_t>(C.CtxSize)));
3472
}
3573

3674
// Align a JITTargetAddress to conform with block alignment requirements.

0 commit comments

Comments
 (0)