|
| 1 | +//------------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (C) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. |
| 4 | +//------------------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +#include "RuntimeLibraryPch.h" |
| 7 | + |
| 8 | +#ifdef ENABLE_WASM |
| 9 | +#include "../WasmReader/WasmReaderPch.h" |
| 10 | +namespace Js |
| 11 | +{ |
| 12 | + |
| 13 | +WebAssemblyEnvironment::WebAssemblyEnvironment(WebAssemblyModule* module) |
| 14 | +{ |
| 15 | + this->module = module; |
| 16 | + ScriptContext* scriptContext = module->GetScriptContext(); |
| 17 | + uint32 size = module->GetModuleEnvironmentSize(); |
| 18 | + this->start = RecyclerNewArrayZ(scriptContext->GetRecycler(), Var, size); |
| 19 | + this->end = start + size; |
| 20 | + Assert(start < end); |
| 21 | + this->memory = this->start + module->GetMemoryOffset(); |
| 22 | + this->imports = this->start + module->GetImportFuncOffset(); |
| 23 | + this->functions = this->start + module->GetFuncOffset(); |
| 24 | + this->table = this->start + module->GetTableEnvironmentOffset(); |
| 25 | + this->globals = this->start + module->GetGlobalOffset(); |
| 26 | + |
| 27 | + uint32 globalsSize = WAsmJs::ConvertToJsVarOffset<byte>(module->GetGlobalsByteSize()); |
| 28 | + // Assumes globals are last |
| 29 | + Assert(globals > table && globals > functions && globals > imports && globals > memory); |
| 30 | + if (globals < start || |
| 31 | + (globals + globalsSize) > end) |
| 32 | + { |
| 33 | + // Something went wrong in the allocation and/or offset calculation failfast |
| 34 | + JavascriptError::ThrowOutOfMemoryError(scriptContext); |
| 35 | + } |
| 36 | + AssertMsg(globals + globalsSize + 0x10 > end, "We don't expect to allocate much more memory than what's needed"); |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +template<typename T> |
| 41 | +void Js::WebAssemblyEnvironment::CheckPtrIsValid(intptr_t ptr) const |
| 42 | +{ |
| 43 | + if (ptr < (intptr_t)start || (intptr_t)(ptr + sizeof(T)) > (intptr_t)end) |
| 44 | + { |
| 45 | + Js::Throw::InternalError(); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +template<typename T> |
| 50 | +T* Js::WebAssemblyEnvironment::GetVarElement(Var* ptr, uint32 index, uint32 maxCount) const |
| 51 | +{ |
| 52 | + if (index >= maxCount) |
| 53 | + { |
| 54 | + Js::Throw::InternalError(); |
| 55 | + } |
| 56 | + |
| 57 | + Var* functionPtr = ptr + index; |
| 58 | + CheckPtrIsValid<T*>((intptr_t)functionPtr); |
| 59 | + Var varFunc = *functionPtr; |
| 60 | + if (varFunc) |
| 61 | + { |
| 62 | + if (!T::Is(varFunc)) |
| 63 | + { |
| 64 | + Js::Throw::InternalError(); |
| 65 | + } |
| 66 | + return T::FromVar(varFunc); |
| 67 | + } |
| 68 | + return nullptr; |
| 69 | +} |
| 70 | + |
| 71 | +template<typename T> |
| 72 | +void Js::WebAssemblyEnvironment::SetVarElement(Var* ptr, T* val, uint32 index, uint32 maxCount) |
| 73 | +{ |
| 74 | + if (index >= maxCount || |
| 75 | + !T::Is(val)) |
| 76 | + { |
| 77 | + Js::Throw::InternalError(); |
| 78 | + } |
| 79 | + |
| 80 | + Var* dst = ptr + index; |
| 81 | + CheckPtrIsValid<Var>((intptr_t)dst); |
| 82 | + AssertMsg(*(T**)dst == nullptr, "We shouln't overwrite anything on the environment once it is set"); |
| 83 | + *dst = val; |
| 84 | +} |
| 85 | + |
| 86 | +AsmJsScriptFunction* WebAssemblyEnvironment::GetWasmFunction(uint32 index) const |
| 87 | +{ |
| 88 | + if (!(module->GetFunctionIndexType(index) == Wasm::FunctionIndexTypes::Function || |
| 89 | + module->GetFunctionIndexType(index) == Wasm::FunctionIndexTypes::ImportThunk)) |
| 90 | + { |
| 91 | + Js::Throw::InternalError(); |
| 92 | + } |
| 93 | + return GetVarElement<AsmJsScriptFunction>(functions, index, module->GetWasmFunctionCount()); |
| 94 | +} |
| 95 | + |
| 96 | +void WebAssemblyEnvironment::SetWasmFunction(uint32 index, AsmJsScriptFunction* func) |
| 97 | +{ |
| 98 | + if (!(module->GetFunctionIndexType(index) == Wasm::FunctionIndexTypes::Function || |
| 99 | + module->GetFunctionIndexType(index) == Wasm::FunctionIndexTypes::ImportThunk) || |
| 100 | + !AsmJsScriptFunction::IsWasmScriptFunction(func)) |
| 101 | + { |
| 102 | + Js::Throw::InternalError(); |
| 103 | + } |
| 104 | + SetVarElement<AsmJsScriptFunction>(functions, func, index, module->GetWasmFunctionCount()); |
| 105 | +} |
| 106 | + |
| 107 | +void WebAssemblyEnvironment::SetImportedFunction(uint32 index, Var importedFunc) |
| 108 | +{ |
| 109 | + SetVarElement<JavascriptFunction>(imports, (JavascriptFunction*)importedFunc, index, module->GetWasmFunctionCount()); |
| 110 | +} |
| 111 | + |
| 112 | +Js::WebAssemblyTable* WebAssemblyEnvironment::GetTable(uint32 index) const |
| 113 | +{ |
| 114 | + return GetVarElement<WebAssemblyTable>(table, index, 1); |
| 115 | +} |
| 116 | + |
| 117 | +void WebAssemblyEnvironment::SetTable(uint32 index, WebAssemblyTable* table) |
| 118 | +{ |
| 119 | + SetVarElement<WebAssemblyTable>(this->table, table, index, 1); |
| 120 | +} |
| 121 | + |
| 122 | +WebAssemblyMemory* WebAssemblyEnvironment::GetMemory(uint32 index) const |
| 123 | +{ |
| 124 | + return GetVarElement<WebAssemblyMemory>(memory, index, 1); |
| 125 | +} |
| 126 | + |
| 127 | +void WebAssemblyEnvironment::SetMemory(uint32 index, WebAssemblyMemory* mem) |
| 128 | +{ |
| 129 | + SetVarElement<WebAssemblyMemory>(this->memory, mem, index, 1); |
| 130 | +} |
| 131 | + |
| 132 | +template<typename T> |
| 133 | +T WebAssemblyEnvironment::GetGlobalInternal(uint32 offset) const |
| 134 | +{ |
| 135 | + T* ptr = (T*)start + offset; |
| 136 | + CheckPtrIsValid<T>((intptr_t)ptr); |
| 137 | + return *ptr; |
| 138 | +} |
| 139 | + |
| 140 | +template<typename T> |
| 141 | +void WebAssemblyEnvironment::SetGlobalInternal(uint32 offset, T val) |
| 142 | +{ |
| 143 | + T* ptr = (T*)start + offset; |
| 144 | + CheckPtrIsValid<T>((intptr_t)ptr); |
| 145 | + AssertMsg(*ptr == 0, "We shouln't overwrite anything on the environment once it is set"); |
| 146 | + *ptr = val; |
| 147 | +} |
| 148 | + |
| 149 | +Wasm::WasmConstLitNode WebAssemblyEnvironment::GetGlobalValue(Wasm::WasmGlobal* global) const |
| 150 | +{ |
| 151 | + if (!global) |
| 152 | + { |
| 153 | + Js::Throw::InternalError(); |
| 154 | + } |
| 155 | + Wasm::WasmConstLitNode cnst; |
| 156 | + uint32 offset = module->GetOffsetForGlobal(global); |
| 157 | + |
| 158 | + switch (global->GetType()) |
| 159 | + { |
| 160 | + case Wasm::WasmTypes::I32: cnst.i32 = GetGlobalInternal<int>(offset); break; |
| 161 | + case Wasm::WasmTypes::I64: cnst.i64 = GetGlobalInternal<int64>(offset); break; |
| 162 | + case Wasm::WasmTypes::F32: cnst.f32 = GetGlobalInternal<float>(offset); break; |
| 163 | + case Wasm::WasmTypes::F64: cnst.f64 = GetGlobalInternal<double>(offset); break; |
| 164 | + default: |
| 165 | + Js::Throw::InternalError(); |
| 166 | + } |
| 167 | + return cnst; |
| 168 | +} |
| 169 | + |
| 170 | +void WebAssemblyEnvironment::SetGlobalValue(Wasm::WasmGlobal* global, Wasm::WasmConstLitNode cnst) |
| 171 | +{ |
| 172 | + if (!global) |
| 173 | + { |
| 174 | + Js::Throw::InternalError(); |
| 175 | + } |
| 176 | + uint32 offset = module->GetOffsetForGlobal(global); |
| 177 | + |
| 178 | + switch (global->GetType()) |
| 179 | + { |
| 180 | + case Wasm::WasmTypes::I32: SetGlobalInternal<int>(offset, cnst.i32); break; |
| 181 | + case Wasm::WasmTypes::I64: SetGlobalInternal<int64>(offset, cnst.i64); break; |
| 182 | + case Wasm::WasmTypes::F32: SetGlobalInternal<float>(offset, cnst.f32); break; |
| 183 | + case Wasm::WasmTypes::F64: SetGlobalInternal<double>(offset, cnst.f64); break; |
| 184 | + default: |
| 185 | + Js::Throw::InternalError(); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +} // namespace Js |
| 190 | +#endif // ENABLE_WASM |
0 commit comments