-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWasmModule.cpp
More file actions
369 lines (321 loc) · 12.6 KB
/
Copy pathWasmModule.cpp
File metadata and controls
369 lines (321 loc) · 12.6 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
///
/// @file WasmModule.cpp
/// @copyright Copyright (C) 2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
/// SPDX-License-Identifier: Apache-2.0
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
#include "src/config.hpp"
//
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include "src/core/compiler/Compiler.hpp"
//
#include "WasmModule.hpp"
#include "src/core/common/ExtendableMemory.hpp"
#include "src/core/common/GlobalSymbol.hpp"
#include "src/core/common/ILogger.hpp"
#include "src/core/common/NativeSymbol.hpp"
#include "src/core/common/Span.hpp"
#include "src/core/common/TrapCode.hpp"
#include "src/core/common/VbExceptions.hpp"
#include "src/core/common/util.hpp"
#include "src/core/compiler/common/ManagedBinary.hpp"
#include "src/core/runtime/Runtime.hpp"
#include "src/extensions/Extension.hpp"
#ifdef VB_WIN32_OR_POSIX
#include <atomic>
#include "src/utils/ExecutableMemory.hpp"
#include "src/utils/MemUtils.hpp"
#include "src/utils/RAIISignalHandler.hpp"
#include "src/utils/SignalFunctionWrapper.hpp"
#endif
#if LINEAR_MEMORY_BOUNDS_CHECKS && !(defined JIT_TARGET_TRICORE)
#include <mutex>
#endif
#if LINEAR_MEMORY_BOUNDS_CHECKS == 0
#include "src/utils/LinearMemoryAllocator.hpp"
#endif
namespace vb {
WasmModule::MallocFunction WasmModule::mallocFunction_{nullptr};
WasmModule::ReallocFunction WasmModule::reallocFunction_{nullptr};
WasmModule::FreeFunction WasmModule::freeFunction_{nullptr};
// coverity[autosar_cpp14_a15_4_4_violation]
void WasmModule::initEnvironment(MallocFunction const mallocFunction, ReallocFunction const reallocFunction, FreeFunction const freeFunction) {
#ifdef VB_NEED_SIGNAL_HANDLER
SignalFunctionWrapper::setPersistentHandler();
#endif
mallocFunction_ = mallocFunction;
reallocFunction_ = reallocFunction;
freeFunction_ = freeFunction;
}
// coverity[autosar_cpp14_m0_1_8_violation]
void WasmModule::destroyEnvironment() VB_NOEXCEPT {
#ifdef VB_NEED_SIGNAL_HANDLER
RAIISignalHandler::restoreSignalHandler();
#endif
#if ENABLE_EXTENSIONS
extension::stop();
#endif
}
#if ENABLE_EXTENSIONS
WasmModule::~WasmModule() VB_NOEXCEPT {
extension::unregisterRuntime(runtime_);
}
#else
WasmModule::~WasmModule() VB_NOEXCEPT = default;
#endif
void WasmModule::initFromBytecode(Span<uint8_t const> const &bytecode, Span<NativeSymbol const> const &linkedFunctions,
bool const allowUnknownImports) {
CompileResult const compileResult{compile(bytecode, linkedFunctions, allowUnknownImports, false)};
rawDebugSymbol_ = compileResult.getDebugSymbol().span();
setupRuntime(compileResult.getModule().span(), linkedFunctions, rawDebugSymbol_);
}
WasmModule::CompileResult WasmModule::compile(Span<uint8_t const> const &bytecode, Span<NativeSymbol const> const &linkedFunctions,
bool const allowUnknownImports, bool const highPressureMode) {
return compile(bytecode, linkedFunctions, Span<GlobalSymbol const>(), allowUnknownImports, highPressureMode);
}
WasmModule::CompileResult WasmModule::compile(Span<uint8_t const> const &bytecode, Span<NativeSymbol const> const &linkedFunctions,
Span<GlobalSymbol const> const &linkedGlobals, bool const allowUnknownImports,
bool const highPressureMode) {
// coverity[autosar_cpp14_a16_2_3_violation] fake positive
Compiler compiler{&compilerRealloc, &compilerMemoryAllocFnc, &compilerMemoryFreeFnc, this, &jitRealloc, allowUnknownImports};
compiler.setLogger(&logger_);
if (stackRecordCount_ != 0U) {
compiler.setStacktraceRecordCount(stackRecordCount_);
}
if (highPressureMode) {
compiler.forceHighRegisterPressureForTesting();
}
if (debugBuild_) {
compiler.enableDebugMode(&debugLineFnc);
}
ManagedBinary module{compiler.compile(bytecode, linkedFunctions, linkedGlobals)};
ManagedBinary debugSymbol{compiler.retrieveDebugMap()};
CompileResult compileResult{std::move(module), std::move(debugSymbol)};
return compileResult;
}
void WasmModule::initFromCompiledBinary(Span<uint8_t const> const &compiledBinary, Span<NativeSymbol const> const &linkedFunctions,
Span<uint8_t const> const &rawDebugSymbol) {
for (size_t i{0U}; i < linkedFunctions.size(); ++i) {
if (linkedFunctions[i].linkage == NativeSymbol::Linkage::STATIC) {
throw RuntimeError(ErrorCode::Wrong_type);
}
}
rawDebugSymbol_ = rawDebugSymbol;
setupRuntime(compiledBinary, linkedFunctions, rawDebugSymbol_);
}
// coverity[autosar_cpp14_m7_1_2_violation]
void *WasmModule::compilerMemoryAllocFnc(uint32_t const size, void *const ctx) VB_NOEXCEPT {
static_cast<void>(ctx);
// GCOVR_EXCL_START
assert(mallocFunction_ != nullptr && "Realloc function is not set");
if (mallocFunction_ == nullptr) {
return nullptr;
}
// GCOVR_EXCL_STOP
return mallocFunction_(static_cast<size_t>(size));
}
// coverity[autosar_cpp14_m7_1_2_violation]
void WasmModule::compilerMemoryFreeFnc(void *const ptr, void *const ctx) VB_NOEXCEPT {
static_cast<void>(ctx);
if (ptr != nullptr) {
memFree(ptr);
}
}
// coverity[autosar_cpp14_m7_1_2_violation]
void WasmModule::compilerRealloc(ExtendableMemory ¤tObject, uint32_t minimumLength, void *const ctx) VB_NOEXCEPT {
static_cast<void>(ctx);
// GCOVR_EXCL_START
assert(reallocFunction_ != nullptr && "Realloc function is not set");
if (reallocFunction_ == nullptr) {
return;
}
// GCOVR_EXCL_STOP
if (minimumLength == 0U) {
memFree(currentObject.data());
} else {
if (minimumLength < 4096U) {
minimumLength = minimumLength * 2U;
} else {
minimumLength = minimumLength + 4096U;
}
void *const newPtr{reallocFunction_(currentObject.data(), static_cast<size_t>(minimumLength))};
currentObject.reset(vb::pCast<uint8_t *>(newPtr), minimumLength);
}
}
// coverity[autosar_cpp14_m7_1_2_violation]
void WasmModule::jitRealloc(ExtendableMemory ¤tObject, uint32_t const minimumLength, void *const ctx) VB_NOEXCEPT {
static_cast<void>(ctx);
return compilerRealloc(currentObject, minimumLength, ctx);
}
// coverity[autosar_cpp14_m7_1_2_violation]
void WasmModule::debugLineFnc(vb::ExtendableMemory ¤tObject, uint32_t const minimumLength, void *const ctx) VB_NOEXCEPT {
static_cast<void>(ctx);
if (minimumLength == 0U) {
memFree(currentObject.data());
return;
}
void *const newPtr{reallocFunction_(currentObject.data(), static_cast<size_t>(minimumLength))};
currentObject.reset(vb::pCast<uint8_t *>(newPtr), minimumLength);
}
#if LINEAR_MEMORY_BOUNDS_CHECKS
void WasmModule::runtimeMemoryAllocFncRaw(ExtendableMemory ¤tObject, uint32_t const minimumLength, void *const ctx) VB_NOEXCEPT {
WasmModule *const pWasmModule{pCast<WasmModule *>(ctx)};
pWasmModule->runtimeMemoryAllocFnc(currentObject, minimumLength);
}
void WasmModule::runtimeMemoryAllocFnc(ExtendableMemory ¤tObject, uint32_t const minimumLength) VB_NOEXCEPT {
// GCOVR_EXCL_START
assert(reallocFunction_ != nullptr && "Realloc function is not set");
if (reallocFunction_ == nullptr) {
return;
}
// GCOVR_EXCL_STOP
if (minimumLength == 0U) {
memFree(currentObject.data());
return;
}
if (minimumLength > maxRam_) {
// out of limitation
maxDesiredRamOnMemoryExtendFailed_ = minimumLength;
return;
}
// Current limitation of the runtime
uint64_t const effectiveLimit{std::min(maxRam_, static_cast<uint64_t>(UINT32_MAX))};
uint64_t const proposedLength{std::max(static_cast<uint64_t>(512U), static_cast<uint64_t>(minimumLength)) + 4096U};
uint32_t const expectedLength{static_cast<uint32_t>(std::min(effectiveLimit, proposedLength))};
#if !(defined JIT_TARGET_TRICORE)
std::unique_lock<std::mutex> const lock(linearMemoryMutex_);
#endif
void *newPtr{nullptr};
newPtr = reallocFunction_(currentObject.data(), static_cast<size_t>(expectedLength));
if (newPtr != nullptr) {
currentObject.reset(vb::pCast<uint8_t *const>(newPtr), expectedLength);
return;
}
newPtr = reallocFunction_(currentObject.data(), static_cast<size_t>(minimumLength));
if (newPtr != nullptr) {
currentObject.reset(vb::pCast<uint8_t *const>(newPtr), minimumLength);
return;
}
maxDesiredRamOnMemoryExtendFailed_ = minimumLength;
}
#endif
void WasmModule::requestInterruption(vb::TrapCode const trapCode) VB_NOEXCEPT {
if (runtime_.hasBinaryModule()) {
#if LINEAR_MEMORY_BOUNDS_CHECKS && !(defined JIT_TARGET_TRICORE)
std::unique_lock<std::mutex> const lock(linearMemoryMutex_);
#endif
#if INTERRUPTION_REQUEST
runtime_.requestInterruption(trapCode);
MemUtils::setPermissionRW(const_cast<uint8_t *>(executableMemory_.data()), executableMemory_.size());
#else
static_cast<void>(trapCode);
UNREACHABLE(_, "Should not be called if interruption is not requested");
#endif
#ifdef VB_WIN32_OR_POSIX
std::atomic_thread_fence(std::memory_order_seq_cst);
#endif
}
}
void WasmModule::start(uint8_t const *const stackTop) {
setStackTop(stackTop);
#ifdef VB_NEED_SIGNAL_HANDLER
SignalFunctionWrapper::start(runtime_);
#else
runtime_.start();
#endif
}
// coverity[autosar_cpp14_m0_1_8_violation]
// coverity[autosar_cpp14_a7_1_1_violation]
// coverity[autosar_cpp14_a15_4_4_violation]
void WasmModule::setStackTop(uint8_t const *stackTop) const {
static_cast<void>(stackTop);
static_cast<void>(this);
#if ACTIVE_STACK_OVERFLOW_CHECK
#ifdef VB_RESERVED_STACK_SIZE
stackTop = vb::pAddI(stackTop, VB_RESERVED_STACK_SIZE);
#endif
runtime_.setStackFence(stackTop);
#endif
}
void WasmModule::setupRuntime(Span<uint8_t const> const &compiledBinary, Span<NativeSymbol const> const &linkedFunctions,
Span<uint8_t const> const &rawDebugSymbol) {
Span<uint8_t const> machineCode{};
#ifndef JIT_TARGET_TRICORE
executableMemory_ = vb::ExecutableMemory::make_executable_copy(compiledBinary);
machineCode = executableMemory_.span();
#else
machineCode = compiledBinary;
#endif
#if LINEAR_MEMORY_BOUNDS_CHECKS
runtime_ = vb::Runtime(machineCode, &runtimeMemoryAllocFncRaw, linkedFunctions, this);
#else
linearMemoryAllocator_.setMemoryLimit(maxRam_);
// coverity[autosar_cpp14_a15_0_2_violation]
runtime_ = Runtime(machineCode, linearMemoryAllocator_, linkedFunctions, this);
#endif
#if ENABLE_EXTENSIONS
extension::registerRuntime(runtime_);
#endif
sendDebugSymbolToDebugger(rawDebugSymbol);
}
#if LINEAR_MEMORY_BOUNDS_CHECKS
uint64_t WasmModule::getMaxDesiredRamOnMemoryExtendFailed() const VB_NOEXCEPT {
return maxDesiredRamOnMemoryExtendFailed_;
}
#else
uint64_t WasmModule::getMaxDesiredRamOnMemoryExtendFailed() const VB_NOEXCEPT {
return linearMemoryAllocator_.getMaxDesiredRamOnMemoryExtendFailed();
}
#endif // LINEAR_MEMORY_BOUNDS_CHECKS
void WasmModule::sendDebugSymbolToDebugger(Span<uint8_t const> const &debugSymbol) VB_NOEXCEPT {
// fixme: This function is a placeholder for sending debug symbols to a debugger.
static_cast<void>(debugSymbol);
}
void WasmModule::shrinkMemory(uint32_t const minimumLength) {
// GCOVR_EXCL_START
assert(runtime_.hasBinaryModule());
// GCOVR_EXCL_STOP
runtime_.shrinkToSize(minimumLength);
}
Span<uint8_t const> WasmModule::getCompiledBinary() const VB_NOEXCEPT {
#ifndef JIT_TARGET_TRICORE
return Span<uint8_t const>(executableMemory_.data(), executableMemory_.size());
#else
return Span<uint8_t const>(nullptr, 0U);
#endif
}
void WasmModule::memFree(void *const ptr) VB_NOEXCEPT {
// GCOVR_EXCL_START
assert(freeFunction_ != nullptr && "Realloc function is not set");
if (freeFunction_ == nullptr) {
return;
}
freeFunction_(ptr);
// GCOVR_EXCL_STOP
}
#if ENABLE_ADVANCED_APIS
void WasmModule::callRawExportedFunctionByName(Span<char const> const &functionName, uint8_t const *const stackTop,
uint8_t const *const serializedArgs, uint8_t *const results) {
RawModuleFunction const wasmFunction{runtime_.getRawExportedFunctionByName(functionName, Span<char const>{})};
setStackTop(stackTop);
#ifdef VB_NEED_SIGNAL_HANDLER
SignalFunctionWrapper::call(wasmFunction, serializedArgs, results);
#else
wasmFunction(serializedArgs, results);
#endif
}
#endif
} // namespace vb