1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
diff --git a/contrib/restricted/wavm/Include/WAVM/LLVMJIT/LLVMJIT.h b/contrib/restricted/wavm/Include/WAVM/LLVMJIT/LLVMJIT.h
--- a/contrib/restricted/wavm/Include/WAVM/LLVMJIT/LLVMJIT.h
+++ b/contrib/restricted/wavm/Include/WAVM/LLVMJIT/LLVMJIT.h
@@ -125,6 +125,7 @@ namespace WAVM { namespace LLVMJIT {
InstanceBinding instance,
Uptr tableReferenceBias,
const std::vector<Runtime::FunctionMutableData*>& functionDefMutableDatas,
+ const std::unordered_map<Uptr, Uptr>& importIndexToSelfDefinedFunctionIndex,
std::string&& debugName);
struct InstructionSource
diff --git a/contrib/restricted/wavm/Include/WAVM/RuntimeABI/RuntimeABI.h b/contrib/restricted/wavm/Include/WAVM/RuntimeABI/RuntimeABI.h
--- a/contrib/restricted/wavm/Include/WAVM/RuntimeABI/RuntimeABI.h
+++ b/contrib/restricted/wavm/Include/WAVM/RuntimeABI/RuntimeABI.h
@@ -43,6 +43,8 @@ namespace WAVM { namespace Runtime {
context,
compartment,
foreign,
+
+ dynamicLinkingWeakFunctionImport,
};
static_assert(Uptr(IR::ExternKind::function) == Uptr(ObjectKind::function),
"IR::ExternKind::function != ObjectKind::function");
@@ -160,6 +162,19 @@ namespace WAVM { namespace Runtime {
const ObjectKind kind;
};
+ struct WeakFunction
+ : public Object
+ {
+ const std::string name;
+ const Uptr index; // The index in the array with such layout: [imports][functions].
+
+ WeakFunction(std::string name, Uptr index)
+ : Object(ObjectKind::dynamicLinkingWeakFunctionImport)
+ , name(std::move(name))
+ , index(index)
+ { }
+ };
+
typedef Runtime::ContextRuntimeData* (*InvokeThunkPointer)(const Runtime::Function*,
Runtime::ContextRuntimeData*,
const IR::UntaggedValue* arguments,
diff --git a/contrib/restricted/wavm/Lib/LLVMJIT/LLVMJITPrivate.h b/contrib/restricted/wavm/Lib/LLVMJIT/LLVMJITPrivate.h
--- a/contrib/restricted/wavm/Lib/LLVMJIT/LLVMJITPrivate.h
+++ b/contrib/restricted/wavm/Lib/LLVMJIT/LLVMJITPrivate.h
@@ -394,9 +394,11 @@ namespace WAVM { namespace LLVMJIT {
#endif
Module(const std::vector<U8>& inObjectBytes,
- const HashMap<std::string, Uptr>& importedSymbolMap,
+ HashMap<std::string, Uptr>* importedSymbolMap,
bool shouldLogMetrics,
- std::string&& inDebugName);
+ std::string&& inDebugName,
+ const std::unordered_map<std::string, std::string>& weakFunctionsToPatch = {});
+
~Module();
private:
diff --git a/contrib/restricted/wavm/Lib/LLVMJIT/LLVMModule.cpp b/contrib/restricted/wavm/Lib/LLVMJIT/LLVMModule.cpp
--- a/contrib/restricted/wavm/Lib/LLVMJIT/LLVMModule.cpp
+++ b/contrib/restricted/wavm/Lib/LLVMJIT/LLVMModule.cpp
@@ -323,9 +323,10 @@ private:
};
Module::Module(const std::vector<U8>& objectBytes,
- const HashMap<std::string, Uptr>& importedSymbolMap,
+ HashMap<std::string, Uptr>* importedSymbolMap,
bool shouldLogMetrics,
- std::string&& inDebugName)
+ std::string&& inDebugName,
+ const std::unordered_map<std::string, std::string>& weakFunctionsToPatch)
: debugName(std::move(inDebugName))
, memoryManager(new ModuleMemoryManager())
, globalModuleState(GlobalModuleState::get())
@@ -403,7 +404,7 @@ Module::Module(const std::vector<U8>& objectBytes,
}
}
};
- SymbolResolver symbolResolver(importedSymbolMap);
+ SymbolResolver symbolResolver(*importedSymbolMap);
llvm::RuntimeDyld loader(*memoryManager, symbolResolver);
// Process all sections on non-Windows platforms. On Windows, this triggers errors due to
// unimplemented relocation types in the debug sections.
@@ -467,6 +468,12 @@ Module::Module(const std::vector<U8>& objectBytes,
// Use the LLVM object loader to load the object.
std::unique_ptr<llvm::RuntimeDyld::LoadedObjectInfo> loadedObject = loader.loadObject(*object);
+ auto symbolTable = loader.getSymbolTable();
+ for (auto& [function, import] : weakFunctionsToPatch) {
+ WAVM_ASSERT(symbolTable.contains(function));
+ WAVM_ASSERT(importedSymbolMap->contains(import));
+ (*importedSymbolMap)[import] = symbolTable[function].getAddress();
+ }
loader.finalizeWithMemoryManagerLocking();
if(loader.hasError())
{ Errors::fatalf("RuntimeDyld failed: %s", loader.getErrorString().data()); }
@@ -661,6 +668,7 @@ std::shared_ptr<LLVMJIT::Module> LLVMJIT::loadModule(
InstanceBinding instance,
Uptr tableReferenceBias,
const std::vector<Runtime::FunctionMutableData*>& functionDefMutableDatas,
+ const std::unordered_map<Uptr, Uptr>& importIndexToSelfDefinedFunctionIndex,
std::string&& debugName)
{
// Bind undefined symbols in the compiled object to values.
@@ -684,8 +692,10 @@ std::shared_ptr<LLVMJIT::Module> LLVMJIT::loadModule(
// Bind imported function symbols.
for(Uptr importIndex = 0; importIndex < functionImports.size(); ++importIndex)
{
- importedSymbolMap.addOrFail(getExternalName("functionImport", importIndex),
- reinterpret_cast<Uptr>(functionImports[importIndex].code));
+ if (!importIndexToSelfDefinedFunctionIndex.contains(importIndex)) {
+ importedSymbolMap.addOrFail(getExternalName("functionImport", importIndex),
+ reinterpret_cast<Uptr>(functionImports[importIndex].code));
+ }
}
// Bind the table symbols. The compiled module uses the symbol's value as an offset into
@@ -736,6 +746,15 @@ std::shared_ptr<LLVMJIT::Module> LLVMJIT::loadModule(
exceptionTypes[exceptionTypeIndex].id + 1);
}
+ std::unordered_map<Uptr, Uptr> selfDefinedFunctionIndexToimportIndex;
+ for (auto [importIndex, selfDefinedFunctionIndex] : importIndexToSelfDefinedFunctionIndex) {
+ selfDefinedFunctionIndexToimportIndex[selfDefinedFunctionIndex] = importIndex;
+ }
+
+ WAVM_ASSERT(selfDefinedFunctionIndexToimportIndex.size() == importIndexToSelfDefinedFunctionIndex.size());
+
+ std::unordered_map<std::string, std::string> weakFunctionsToPatch;
+
// Allocate FunctionMutableData objects for each function def, and bind them to the symbols
// imported by the compiled module.
for(Uptr functionDefIndex = 0; functionDefIndex < functionDefMutableDatas.size();
@@ -745,8 +764,19 @@ std::shared_ptr<LLVMJIT::Module> LLVMJIT::loadModule(
= functionDefMutableDatas[functionDefIndex];
importedSymbolMap.addOrFail(getExternalName("functionDefMutableDatas", functionDefIndex),
reinterpret_cast<Uptr>(functionMutableData));
+
+ Uptr indexWithFunctionOffsets = functionDefIndex + functionImports.size();
+ auto it = selfDefinedFunctionIndexToimportIndex.find(indexWithFunctionOffsets);
+ if (it != selfDefinedFunctionIndexToimportIndex.end()) {
+ Uptr importIndex = it->second;
+ WAVM_ASSERT(!weakFunctionsToPatch.contains(getExternalName("functionDef", functionDefIndex)));
+ weakFunctionsToPatch[getExternalName("functionDef", functionDefIndex)] = getExternalName("functionImport", importIndex);
+ importedSymbolMap.addOrFail(getExternalName("functionImport", importIndex), 0ul);
+ }
}
+ WAVM_ASSERT(weakFunctionsToPatch.size() == importIndexToSelfDefinedFunctionIndex.size());
+
// Bind the instance symbol to point to the Instance.
WAVM_ASSERT(instance.id != UINTPTR_MAX);
importedSymbolMap.addOrFail("biasedInstanceId", instance.id + 1);
@@ -780,7 +810,7 @@ std::shared_ptr<LLVMJIT::Module> LLVMJIT::loadModule(
#endif
// Load the module.
- return std::make_shared<Module>(objectFileBytes, importedSymbolMap, true, std::move(debugName));
+ return std::make_shared<Module>(objectFileBytes, &importedSymbolMap, true, std::move(debugName), weakFunctionsToPatch);
}
bool LLVMJIT::getInstructionSourceByAddress(Uptr address, InstructionSource& outSource)
diff --git a/contrib/restricted/wavm/Lib/LLVMJIT/Thunk.cpp b/contrib/restricted/wavm/Lib/LLVMJIT/Thunk.cpp
--- a/contrib/restricted/wavm/Lib/LLVMJIT/Thunk.cpp
+++ b/contrib/restricted/wavm/Lib/LLVMJIT/Thunk.cpp
@@ -170,8 +170,9 @@ InvokeThunkPointer LLVMJIT::getInvokeThunk(FunctionType functionType)
= compileLLVMModule(llvmContext, std::move(llvmModule), false, targetMachine.get());
// Load the object code.
+ auto importedSymbolMap = HashMap<std::string, Uptr>();
auto jitModule
- = new LLVMJIT::Module(objectBytes, {}, false, std::string(functionMutableData->debugName));
+ = new LLVMJIT::Module(objectBytes, &importedSymbolMap, false, std::string(functionMutableData->debugName));
invokeThunkCache.modules.push_back(std::unique_ptr<LLVMJIT::Module>(jitModule));
invokeThunkFunction = jitModule->nameToFunctionMap[mangleSymbol("thunk")];
diff --git a/contrib/restricted/wavm/Lib/Runtime/Instance.cpp b/contrib/restricted/wavm/Lib/Runtime/Instance.cpp
--- a/contrib/restricted/wavm/Lib/Runtime/Instance.cpp
+++ b/contrib/restricted/wavm/Lib/Runtime/Instance.cpp
@@ -78,6 +78,7 @@ Instance* Runtime::instantiateModule(Compartment* compartment,
// Check the types of the Instance's imports, and build per-kind import arrays.
std::vector<FunctionImportBinding> functionImports;
+ std::unordered_map<Uptr, Uptr> importIndexToSelfDefinedFunctionIndex;
std::vector<Table*> tableImports;
std::vector<Memory*> memoryImports;
std::vector<Global*> globalImports;
std::vector<ExceptionType*> exceptionTypeImports;
@@ -88,18 +89,23 @@ Instance* Runtime::instantiateModule(Compartment* compartment,
Object* importObject = imports[importIndex];
WAVM_ERROR_UNLESS(importObject);
- WAVM_ERROR_UNLESS(isInCompartment(importObject, compartment));
- WAVM_ERROR_UNLESS(importObject->kind == ObjectKind(kindIndex.kind));
+ WAVM_ERROR_UNLESS((importObject->kind == ObjectKind::dynamicLinkingWeakFunctionImport) || (isInCompartment(importObject, compartment) && importObject->kind == ObjectKind(kindIndex.kind)));
switch(kindIndex.kind)
{
case ExternKind::function: {
- Function* function = asFunction(importObject);
- const auto& importType
- = module->ir.types[module->ir.functions.getType(kindIndex.index).index];
- WAVM_ERROR_UNLESS(function->encodedType == importType);
- WAVM_ERROR_UNLESS(importType.callingConvention() == CallingConvention::wasm);
- functionImports.push_back({function});
+ if (importObject->kind == ObjectKind::dynamicLinkingWeakFunctionImport) {
+ auto* weakFunction = static_cast<WeakFunction*>(importObject);
+ importIndexToSelfDefinedFunctionIndex[functionImports.size()] = weakFunction->index;
+ functionImports.push_back({static_cast<Function*>(nullptr)});
+ } else {
+ Function* function = asFunction(importObject);
+ const auto& importType
+ = module->ir.types[module->ir.functions.getType(kindIndex.index).index];
+ WAVM_ERROR_UNLESS(function->encodedType == importType);
+ WAVM_ERROR_UNLESS(importType.callingConvention() == CallingConvention::wasm);
+ functionImports.push_back({function});
+ }
break;
}
case ExternKind::table: {
@@ -138,6 +144,7 @@ Instance* Runtime::instantiateModule(Compartment* compartment,
return instantiateModuleInternal(compartment,
module,
std::move(functionImports),
+ std::move(importIndexToSelfDefinedFunctionIndex),
std::move(tableImports),
std::move(memoryImports),
std::move(globalImports),
@@ -149,6 +156,7 @@ Instance* Runtime::instantiateModule(Compartment* compartment,
Instance* Runtime::instantiateModuleInternal(Compartment* compartment,
ModuleConstRefParam module,
std::vector<FunctionImportBinding>&& functionImports,
+ std::unordered_map<Uptr, Uptr>&& importIndexToSelfDefinedFunctionIndex,
std::vector<Table*>&& tables,
std::vector<Memory*>&& memories,
std::vector<Global*>&& globals,
@@ -327,6 +335,7 @@ Instance* Runtime::instantiateModuleInternal(Compartment* compartment,
{id},
reinterpret_cast<Uptr>(getOutOfBoundsElement()),
functionDefMutableDatas,
+ importIndexToSelfDefinedFunctionIndex,
std::string(moduleDebugName));
// LLVMJIT::loadModule filled in the functionDefMutableDatas' function pointers with the
diff --git a/contrib/restricted/wavm/Lib/Runtime/Intrinsics.cpp b/contrib/restricted/wavm/Lib/Runtime/Intrinsics.cpp
--- a/contrib/restricted/wavm/Lib/Runtime/Intrinsics.cpp
+++ b/contrib/restricted/wavm/Lib/Runtime/Intrinsics.cpp
@@ -232,6 +232,7 @@ Instance* Intrinsics::instantiateModule(
{},
{},
{},
+ {},
std::move(debugName));
Timing::logTimer("Instantiated intrinsic module", timer);
diff --git a/contrib/restricted/wavm/Lib/Runtime/Linker.cpp b/contrib/restricted/wavm/Lib/Runtime/Linker.cpp
--- a/contrib/restricted/wavm/Lib/Runtime/Linker.cpp
+++ b/contrib/restricted/wavm/Lib/Runtime/Linker.cpp
@@ -5,6 +5,7 @@
#include "WAVM/Inline/Assert.h"
#include "WAVM/Logging/Logging.h"
#include "WAVM/Runtime/Runtime.h"
+#include "WAVM/RuntimeABI/RuntimeABI.h"
using namespace WAVM;
using namespace WAVM::IR;
@@ -158,7 +158,7 @@ static void linkImport(const IR::Module& module,
{
// Sanity check that the resolver returned an object of the right type.
WAVM_ASSERT(importValue);
- WAVM_ASSERT(isA(importValue, resolvedType));
+ WAVM_ASSERT(importValue->kind == ObjectKind::dynamicLinkingWeakFunctionImport || isA(importValue, resolvedType));
linkResult.resolvedImports.push_back(importValue);
}
else
diff --git a/contrib/restricted/wavm/Lib/Runtime/RuntimePrivate.h b/contrib/restricted/wavm/Lib/Runtime/RuntimePrivate.h
--- a/contrib/restricted/wavm/Lib/Runtime/RuntimePrivate.h
+++ b/contrib/restricted/wavm/Lib/Runtime/RuntimePrivate.h
@@ -401,6 +401,7 @@ namespace WAVM { namespace Runtime {
Instance* instantiateModuleInternal(Compartment* compartment,
ModuleConstRefParam module,
std::vector<FunctionImportBinding>&& functionImports,
+ std::unordered_map<Uptr, Uptr>&& importIndexToSelfDefinedFunctionIndex,
std::vector<Table*>&& tableImports,
std::vector<Memory*>&& memoryImports,
std::vector<Global*>&& globalImports,
|