mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 21:27:34 +00:00
LibWasm: Read from and write to memory as little-endian
The spec says so, we must do so.
This commit is contained in:
parent
09cf1040ef
commit
9db418e1fb
1 changed files with 13 additions and 15 deletions
|
@ -94,10 +94,7 @@ void BytecodeInterpreter::load_and_push(Configuration& configuration, const Inst
|
||||||
}
|
}
|
||||||
dbgln_if(WASM_TRACE_DEBUG, "load({} : {}) -> stack", instance_address, sizeof(ReadType));
|
dbgln_if(WASM_TRACE_DEBUG, "load({} : {}) -> stack", instance_address, sizeof(ReadType));
|
||||||
auto slice = memory->data().bytes().slice(instance_address, sizeof(ReadType));
|
auto slice = memory->data().bytes().slice(instance_address, sizeof(ReadType));
|
||||||
if constexpr (sizeof(ReadType) == 1)
|
configuration.stack().peek() = Value(static_cast<PushType>(read_value<ReadType>(slice)));
|
||||||
configuration.stack().peek() = Value(static_cast<PushType>(slice[0]));
|
|
||||||
else
|
|
||||||
configuration.stack().peek() = Value(read_value<PushType>(slice));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BytecodeInterpreter::store_to_memory(Configuration& configuration, const Instruction& instruction, ReadonlyBytes data)
|
void BytecodeInterpreter::store_to_memory(Configuration& configuration, const Instruction& instruction, ReadonlyBytes data)
|
||||||
|
@ -210,12 +207,11 @@ void BytecodeInterpreter::call_address(Configuration& configuration, FunctionAdd
|
||||||
#define LOAD_AND_PUSH(read_type, push_type) \
|
#define LOAD_AND_PUSH(read_type, push_type) \
|
||||||
do { \
|
do { \
|
||||||
return load_and_push<read_type, push_type>(configuration, instruction); \
|
return load_and_push<read_type, push_type>(configuration, instruction); \
|
||||||
return; \
|
|
||||||
} while (false)
|
} while (false)
|
||||||
|
|
||||||
#define POP_AND_STORE(pop_type, store_type) \
|
#define POP_AND_STORE(pop_type, store_type) \
|
||||||
do { \
|
do { \
|
||||||
auto value = ConvertToRaw<pop_type> {}(*configuration.stack().pop().get<Value>().to<pop_type>()); \
|
auto value = ConvertToRaw<store_type> {}(*configuration.stack().pop().get<Value>().to<pop_type>()); \
|
||||||
dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> temporary({}b)", value, sizeof(store_type)); \
|
dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> temporary({}b)", value, sizeof(store_type)); \
|
||||||
store_to_memory(configuration, instruction, { &value, sizeof(store_type) }); \
|
store_to_memory(configuration, instruction, { &value, sizeof(store_type) }); \
|
||||||
return; \
|
return; \
|
||||||
|
@ -224,11 +220,13 @@ void BytecodeInterpreter::call_address(Configuration& configuration, FunctionAdd
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T BytecodeInterpreter::read_value(ReadonlyBytes data)
|
T BytecodeInterpreter::read_value(ReadonlyBytes data)
|
||||||
{
|
{
|
||||||
T value;
|
LittleEndian<T> value;
|
||||||
InputMemoryStream stream { data };
|
InputMemoryStream stream { data };
|
||||||
auto ok = IsSigned<T> ? LEB128::read_signed(stream, value) : LEB128::read_unsigned(stream, value);
|
stream >> value;
|
||||||
if (stream.handle_any_error() || !ok)
|
if (stream.handle_any_error()) {
|
||||||
|
dbgln("Read from {} failed", data.data());
|
||||||
m_do_trap = true;
|
m_do_trap = true;
|
||||||
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,7 +256,7 @@ template<typename T>
|
||||||
struct ConvertToRaw {
|
struct ConvertToRaw {
|
||||||
T operator()(T value)
|
T operator()(T value)
|
||||||
{
|
{
|
||||||
return value;
|
return LittleEndian<T>(value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue