16 #include "config/WasmSequencerConfig.hpp" 24 U8* WasmSequencer ::globalAlloc(
const U32 size,
const U32 align) {
28 FW_ASSERT(align <= 8, static_cast<FwAssertArgType>(align));
31 if (this->m_heapPagesUsed < this->m_config.
heapPages) {
32 auto page = this->m_heapPages[this->m_heapPagesUsed];
34 this->m_heapPagesUsed += 1;
42 void WasmSequencer ::globalDealloc(
const U8* ptr) {
50 if (ptr == this->m_heapPages[i]) {
62 this->m_heapPagesUsed -= 1;
63 this->m_heapPoisoned =
true;
66 U8* WasmSequencer ::guestAlloc(
FwSizeType size, U32 align) {
77 const FwSizeType a = (align < 1) ? 1 : static_cast<FwSizeType>(align);
85 this->m_guestPoolOffset =
start + size;
86 return &this->m_guestPool[
start];
96 std::ptrdiff_t moduleMemOffset = ptr - this->m_guestPool;
99 FW_ASSERT(moduleMemOffset >= 0, static_cast<FwAssertArgType>(moduleMemOffset));
103 if (offset + oldSize == this->m_guestPoolOffset) {
108 this->m_guestPoolOffset = offset + newSize;
117 static_cast<U64>(newSize));
123 static_cast<U64>(newSize));
127 void WasmSequencer ::guestDealloc(
const U8* ptr,
const FwSizeType size) {
134 U8* WasmSequencer ::guestAllocCallback(
void* userdata,
size_t size,
size_t align) {
136 return static_cast<WasmSequencer*
>(userdata)->guestAlloc(static_cast<FwSizeType>(size),
static_cast<U32
>(align));
139 U8* WasmSequencer ::guestReallocCallback(
void* userdata,
U8* ptr,
size_t old_size,
size_t new_size,
size_t align) {
144 ptr, static_cast<FwSizeType>(old_size),
static_cast<FwSizeType>(new_size), static_cast<U32>(align));
147 void WasmSequencer ::guestDeallocCallback(
void* userdata,
U8* ptr,
size_t size,
size_t align) {
150 static_cast<WasmSequencer*
>(userdata)->guestDealloc(ptr, static_cast<FwSizeType>(size));
153 void WasmSequencer ::createStore() {
156 this->takeAllocatorLock();
162 this->hostFprimeV1(&host);
169 status =
spacewasm_new(&host, this->m_config.
stackSize, this->m_config.maxGuestModules, options, &this->m_wasm);
171 this->m_guest_allocator =
173 &WasmSequencer::guestDeallocCallback,
this);
175 this->releaseAllocatorLock();
189 FW_ASSERT(this->m_guest_allocator !=
nullptr);
194 void WasmSequencer ::destroyStore() {
196 FW_ASSERT(this->m_guest_allocator !=
nullptr);
198 this->takeAllocatorLock();
201 this->releaseAllocatorLock();
202 this->m_wasm =
nullptr;
203 this->m_guest_allocator =
nullptr;
206 FW_ASSERT(this->m_heapPagesUsed == 0, static_cast<FwAssertArgType>(this->m_heapPagesUsed));
210 this->m_guestPoolOffset = 0;
211 this->m_heapPoisoned =
false;
234 U32 WasmSequencer ::makeCmdUid()
const {
240 return static_cast<U32
>(((this->m_sequencesStarted & 0xFFFF) << 16) | (this->m_tlm.commandsDispatched & 0xFFFF));
261 void WasmSequencer ::respondToWaiting(
const Fw::CmdResponse& response) {
358 if (moduleName.
length() > 0) {
359 this->m_tlm.sequenceName = moduleName;
364 const char*
const path = filePath.
toChar();
369 const char*
const base = WasmSequencer::pathBaseName(path, len, nameLen);
372 static const char suffix[] =
".wasm";
374 if (nameLen >= suffixLen) {
377 if (base[nameLen - suffixLen + i] != suffix[i]) {
383 nameLen -= suffixLen;
389 for (
FwSizeType i = 0; i < nameLen && n < static_cast<FwSizeType>(
sizeof(name) - 1); i++) {
393 this->m_tlm.sequenceName = name;
396 const char* WasmSequencer ::pathBaseName(
const char* path,
FwSizeType len,
FwSizeType& outLen) {
401 if (path[i] ==
'/') {
405 outLen = len -
start;
409 bool WasmSequencer ::pathHasParentTraversal(
const Fw::StringBase& path) {
410 const char*
const s = path.
toChar();
416 if (i == len || s[i] ==
'/') {
418 if (segLen == 2 && s[segStart] ==
'.' && s[segStart + 1] ==
'.') {
427 Os::Mutex* WasmSequencer::getGlobalAllocatorLock() {
431 return &s_globalAllocatorLock;
434 void WasmSequencer ::takeAllocatorLock() {
435 getGlobalAllocatorLock()->
lock();
441 void WasmSequencer ::releaseAllocatorLock() {
445 getGlobalAllocatorLock()->
unlock();
451 std::size_t filename_len,
456 (void)fmtMsg.
format(
"Rust panic %.*s:%d: %.*s\n", static_cast<int>(filename_len),
457 reinterpret_cast<const char*>(filename), static_cast<int>(line), static_cast<int>(len),
458 reinterpret_cast<const char*>(msg));
An indirect call tried to map to a table function out of range.
void spacewasm_panic(const U8 *filename, std::size_t filename_len, U32 line, const U8 *msg, std::size_t len)
Panic hook the spacewasm interpreter calls on a fatal internal error.
uint32_t max_backpatch_iterations
PlatformSizeType FwSizeType
constexpr FwSizeType SPACEWASM_PAGE_SIZE
static void write(const Fw::ConstStringBase &message)
write message to console
struct spacewasm_allocator_t * spacewasm_allocator_new(spacewasm_alloc_fn_t alloc, spacewasm_realloc_fn_t realloc, spacewasm_dealloc_fn_t dealloc, void *userdata)
U32 get_cmdSeq() const
Get member cmdSeq.
The grown size would exceed the configured guest memory pool.
U8 maxGuestModules
Maximum number of Wasm modules that may be loaded into the sequencer's store.
The interpreter ran out of memory.
WasmSequencer(const char *const compName)
Construct WasmSequencer object.
virtual const CHAR * toChar() const =0
Convert to a C-style char*.
void cmdResponse_out(FwOpcodeType opCode, U32 cmdSeq, Fw::CmdResponse response)
Emit command response.
spacewasm_status_t spacewasm_get_global(struct spacewasm_t *engine, uint32_t module_idx, uint32_t global_index, struct spacewasm_value_t *out)
Enum representing a command response.
void log_WARNING_LO_MemoryGrowRejected(const Svc::WasmSequencer_MemoryGrowFailReason &reason, U64 requestedSize) const
An indirect call referenced an uninitialized table element.
spacewasm_status_t spacewasm_find_export_func(struct spacewasm_t *engine, uint32_t module_idx, const char *name, uint32_t *out_index)
static constexpr FwSizeType SPACEWASM_MEMORY_ALIGNMENT
SpaceWasm has a hard-coded memory alignment requirement.
FwSizeType guestMemorySize
Svc::WasmSequencer_SignalSource::T get_source() const
Get member source.
The function type in an indirect call does not match the function pointer's type. ...
void unlock()
alias for unLock to meet BasicLockable requirements
Attempting to convert Inf to integer.
void log_DIAGNOSTIC_StoreAllocationSucceeded(U16 moduleCount) const
FwOpcodeType get_opcode() const
Get member opcode.
Integer or floating point division by zero.
memory.grow failed because a host function has taken ownership of a memory
void seqDoneOut_out(FwIndexType portNum, FwOpcodeType opCode, U32 cmdSeq, const Fw::CmdResponse &response) const
Invoke output port seqDoneOut.
bool isConnected_seqDoneOut_OutputPort(FwIndexType portNum) const
Attempting to convert NaN to integer.
constexpr U32 MAX_BACKPATCH_ITERATIONS
A host function has noted an unrecoverable failure.
spacewasm_status_t spacewasm_fprime_release_global_allocator(void *userdata)
U8 WasmSequencer_ModuleIdx
Triggered by unreachable instruction.
Svc::WasmSequencer_CommandRequest & get_cmdCtx()
Get member cmdCtx.
spacewasm_status_t spacewasm_find_global(struct spacewasm_t *engine, uint32_t module_idx, const char *name, uint32_t *out_index)
The memory is not the last allocation in the guest pool; it cannot grow in place. ...
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Success dequeue(T &e) override
uint8_t U8
8-bit unsigned integer
An imported global could not be read.
spacewasm_status_t spacewasm_new(struct spacewasm_host_t *host, size_t stack_size, size_t max_modules, struct spacewasm_compiler_options_t options, struct spacewasm_t **out_engine)
spacewasm_status_t spacewasm_check_func_signature(struct spacewasm_t *engine, uint32_t module_idx, uint32_t func_index, const char *params_sig, const char *returns_sig)
Signed division causes integer overflow.
spacewasm_status_t spacewasm_host_new(size_t len, struct spacewasm_host_t *dest)
void spacewasm_destroy(struct spacewasm_t *engine)
RateGroupDivider component implementation.
spacewasm_status_t spacewasm_fprime_acquire_global_allocator(void *userdata)
virtual SizeType length() const
Get the length of the string.
spacewasm_status_t spacewasm_find_module(struct spacewasm_t *engine, const char *name, uint32_t *out_index)
A memory operation is out of bounds.
Declares F Prime string base class.
void start(FwTaskPriorityType priority=Os::Task::TASK_PRIORITY_DEFAULT, FwSizeType stackSize=Os::Task::TASK_DEFAULT, FwSizeType cpuAffinity=Os::Task::TASK_DEFAULT, FwTaskIdType identifier=static_cast< FwTaskIdType >(Os::Task::TASK_DEFAULT))
called by instantiator when task is to be started
spacewasm_status_t spacewasm_set_global(struct spacewasm_t *engine, uint32_t module_idx, uint32_t global_index, struct spacewasm_value_t value)
void spacewasm_allocator_destroy(struct spacewasm_allocator_t *allocator)
An imported global could not be set.
void lock()
lock the mutex and assert success