JSWebAssemblyInstance.cpp [plain text]
#include "config.h"
#include "JSWebAssemblyInstance.h"
#if ENABLE(WEBASSEMBLY)
#include "AbstractModuleRecord.h"
#include "JSCInlines.h"
#include "JSModuleEnvironment.h"
#include "JSModuleNamespaceObject.h"
#include "JSWebAssemblyHelpers.h"
#include "JSWebAssemblyLinkError.h"
#include "JSWebAssemblyMemory.h"
#include "JSWebAssemblyModule.h"
#include "WebAssemblyModuleRecord.h"
#include "WebAssemblyToJSCallee.h"
#include <wtf/StdLibExtras.h>
namespace JSC {
const ClassInfo JSWebAssemblyInstance::s_info = { "WebAssembly.Instance", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSWebAssemblyInstance) };
Structure* JSWebAssemblyInstance::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
JSWebAssemblyInstance::JSWebAssemblyInstance(VM& vm, Structure* structure, Ref<Wasm::Instance>&& instance)
: Base(vm, structure)
, m_instance(WTFMove(instance))
{
for (unsigned i = 0; i < this->instance().numImportFunctions(); ++i)
new (this->instance().importFunction<PoisonedBarrier<JSObject>>(i)) PoisonedBarrier<JSObject>();
}
void JSWebAssemblyInstance::finishCreation(VM& vm, JSWebAssemblyModule* module, JSModuleNamespaceObject* moduleNamespaceObject)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));
m_module.set(vm, this, module);
m_moduleNamespaceObject.set(vm, this, moduleNamespaceObject);
m_callee.set(vm, this, module->callee());
heap()->reportExtraMemoryAllocated(m_instance->extraMemoryAllocated());
}
void JSWebAssemblyInstance::destroy(JSCell* cell)
{
static_cast<JSWebAssemblyInstance*>(cell)->JSWebAssemblyInstance::~JSWebAssemblyInstance();
}
void JSWebAssemblyInstance::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
auto* thisObject = jsCast<JSWebAssemblyInstance*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
visitor.append(thisObject->m_module);
visitor.append(thisObject->m_codeBlock);
visitor.append(thisObject->m_moduleNamespaceObject);
visitor.append(thisObject->m_memory);
visitor.append(thisObject->m_table);
visitor.append(thisObject->m_callee);
visitor.reportExtraMemoryVisited(thisObject->m_instance->extraMemoryAllocated());
for (unsigned i = 0; i < thisObject->instance().numImportFunctions(); ++i)
visitor.append(*thisObject->instance().importFunction<PoisonedBarrier<JSObject>>(i)); }
void JSWebAssemblyInstance::finalizeCreation(VM& vm, ExecState* exec, Ref<Wasm::CodeBlock>&& wasmCodeBlock)
{
m_instance->finalizeCreation(this, wasmCodeBlock.copyRef());
auto scope = DECLARE_THROW_SCOPE(vm);
if (!wasmCodeBlock->runnable()) {
throwException(exec, scope, JSWebAssemblyLinkError::create(exec, vm, globalObject()->WebAssemblyLinkErrorStructure(), wasmCodeBlock->errorMessage()));
return;
}
RELEASE_ASSERT(wasmCodeBlock->isSafeToRun(memoryMode()));
JSWebAssemblyCodeBlock* jsCodeBlock = m_module->codeBlock(memoryMode());
if (jsCodeBlock) {
ASSERT(&jsCodeBlock->codeBlock() == wasmCodeBlock.ptr());
m_codeBlock.set(vm, this, jsCodeBlock);
} else {
jsCodeBlock = JSWebAssemblyCodeBlock::create(vm, WTFMove(wasmCodeBlock), module()->module().moduleInformation());
if (UNLIKELY(!jsCodeBlock->runnable())) {
throwException(exec, scope, JSWebAssemblyLinkError::create(exec, vm, globalObject()->WebAssemblyLinkErrorStructure(), jsCodeBlock->errorMessage()));
return;
}
m_codeBlock.set(vm, this, jsCodeBlock);
m_module->setCodeBlock(vm, memoryMode(), jsCodeBlock);
}
for (unsigned importFunctionNum = 0; importFunctionNum < instance().numImportFunctions(); ++importFunctionNum) {
auto* info = instance().importFunctionInfo(importFunctionNum);
info->wasmToEmbedderStubExecutableAddress = m_codeBlock->wasmToEmbedderStubExecutableAddress(importFunctionNum);
}
auto* moduleRecord = jsCast<WebAssemblyModuleRecord*>(m_moduleNamespaceObject->moduleRecord());
moduleRecord->link(exec, m_module.get(), this);
RETURN_IF_EXCEPTION(scope, void());
JSValue startResult = moduleRecord->evaluate(exec);
UNUSED_PARAM(startResult);
RETURN_IF_EXCEPTION(scope, void());
}
JSWebAssemblyInstance* JSWebAssemblyInstance::create(VM& vm, ExecState* exec, JSWebAssemblyModule* jsModule, JSObject* importObject, Structure* instanceStructure, Ref<Wasm::Module>&& module)
{
auto throwScope = DECLARE_THROW_SCOPE(vm);
auto* globalObject = exec->lexicalGlobalObject();
const Wasm::ModuleInformation& moduleInformation = jsModule->moduleInformation();
auto exception = [&] (JSObject* error) {
throwException(exec, throwScope, error);
return nullptr;
};
if (!globalObject->webAssemblyEnabled())
return exception(createEvalError(exec, globalObject->webAssemblyDisabledErrorMessage()));
auto importFailMessage = [&] (const Wasm::Import& import, const char* before, const char* after) {
return makeString(before, " ", String::fromUTF8(import.module), ":", String::fromUTF8(import.field), " ", after);
};
if (moduleInformation.imports.size() && !importObject)
return exception(createTypeError(exec, ASCIILiteral("can't make WebAssembly.Instance because there is no imports Object and the WebAssembly.Module requires imports")));
Identifier moduleKey = Identifier::fromUid(PrivateName(PrivateName::Description, "WebAssemblyInstance"));
WebAssemblyModuleRecord* moduleRecord = WebAssemblyModuleRecord::create(exec, vm, globalObject->webAssemblyModuleRecordStructure(), moduleKey, moduleInformation);
RETURN_IF_EXCEPTION(throwScope, nullptr);
JSModuleNamespaceObject* moduleNamespace = moduleRecord->getModuleNamespace(exec);
auto storeTopCallFrame = [&vm] (void* topCallFrame) {
vm.topCallFrame = bitwise_cast<ExecState*>(topCallFrame);
};
auto* jsInstance = new (NotNull, allocateCell<JSWebAssemblyInstance>(vm.heap)) JSWebAssemblyInstance(vm, instanceStructure,
Wasm::Instance::create(&vm.wasmContext, WTFMove(module), &vm.topEntryFrame, vm.addressOfSoftStackLimit(), WTFMove(storeTopCallFrame)));
jsInstance->finishCreation(vm, jsModule, moduleNamespace);
RETURN_IF_EXCEPTION(throwScope, nullptr);
unsigned numImportFunctions = 0;
unsigned numImportGlobals = 0;
bool hasMemoryImport = false;
bool hasTableImport = false;
for (auto& import : moduleInformation.imports) {
JSValue importModuleValue = importObject->get(exec, Identifier::fromString(&vm, String::fromUTF8(import.module)));
RETURN_IF_EXCEPTION(throwScope, nullptr);
if (!importModuleValue.isObject())
return exception(createTypeError(exec, importFailMessage(import, "import", "must be an object"), defaultSourceAppender, runtimeTypeForValue(importModuleValue)));
JSObject* object = jsCast<JSObject*>(importModuleValue);
JSValue value = object->get(exec, Identifier::fromString(&vm, String::fromUTF8(import.field)));
RETURN_IF_EXCEPTION(throwScope, nullptr);
switch (import.kind) {
case Wasm::ExternalKind::Function: {
if (!value.isFunction())
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "import function", "must be callable")));
Wasm::Instance* calleeInstance = nullptr;
Wasm::WasmEntrypointLoadLocation wasmEntrypoint = nullptr;
JSObject* function = jsCast<JSObject*>(value);
WebAssemblyFunction* wasmFunction;
WebAssemblyWrapperFunction* wasmWrapperFunction;
if (isWebAssemblyHostFunction(vm, function, wasmFunction, wasmWrapperFunction)) {
Wasm::SignatureIndex importedSignatureIndex;
if (wasmFunction) {
importedSignatureIndex = wasmFunction->signatureIndex();
calleeInstance = &wasmFunction->instance()->instance();
wasmEntrypoint = wasmFunction->wasmEntrypointLoadLocation();
}
else {
importedSignatureIndex = wasmWrapperFunction->signatureIndex();
function = wasmWrapperFunction->function();
}
Wasm::SignatureIndex expectedSignatureIndex = moduleInformation.importFunctionSignatureIndices[import.kindIndex];
if (importedSignatureIndex != expectedSignatureIndex)
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "imported function", "signature doesn't match the provided WebAssembly function's signature")));
}
ASSERT(numImportFunctions == import.kindIndex);
auto* info = jsInstance->instance().importFunctionInfo(numImportFunctions);
info->targetInstance = calleeInstance;
info->wasmEntrypoint = wasmEntrypoint;
jsInstance->instance().importFunction<PoisonedBarrier<JSObject>>(numImportFunctions)->set(vm, jsInstance, function);
++numImportFunctions;
break;
}
case Wasm::ExternalKind::Table: {
RELEASE_ASSERT(!hasTableImport); hasTableImport = true;
JSWebAssemblyTable* table = jsDynamicCast<JSWebAssemblyTable*>(vm, value);
if (!table)
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "Table import", "is not an instance of WebAssembly.Table")));
uint32_t expectedInitial = moduleInformation.tableInformation.initial();
uint32_t actualInitial = table->length();
if (actualInitial < expectedInitial)
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "Table import", "provided an 'initial' that is too small")));
if (std::optional<uint32_t> expectedMaximum = moduleInformation.tableInformation.maximum()) {
std::optional<uint32_t> actualMaximum = table->maximum();
if (!actualMaximum)
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "Table import", "does not have a 'maximum' but the module requires that it does")));
if (*actualMaximum > *expectedMaximum)
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "Imported Table", "'maximum' is larger than the module's expected 'maximum'")));
}
jsInstance->setTable(vm, table);
RETURN_IF_EXCEPTION(throwScope, nullptr);
break;
}
case Wasm::ExternalKind::Memory: {
RELEASE_ASSERT(!hasMemoryImport); RELEASE_ASSERT(moduleInformation.memory);
hasMemoryImport = true;
JSWebAssemblyMemory* memory = jsDynamicCast<JSWebAssemblyMemory*>(vm, value);
if (!memory)
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "Memory import", "is not an instance of WebAssembly.Memory")));
Wasm::PageCount declaredInitial = moduleInformation.memory.initial();
Wasm::PageCount importedInitial = memory->memory().initial();
if (importedInitial < declaredInitial)
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "Memory import", "provided an 'initial' that is smaller than the module's declared 'initial' import memory size")));
if (Wasm::PageCount declaredMaximum = moduleInformation.memory.maximum()) {
Wasm::PageCount importedMaximum = memory->memory().maximum();
if (!importedMaximum)
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "Memory import", "did not have a 'maximum' but the module requires that it does")));
if (importedMaximum > declaredMaximum)
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "Memory import", "provided a 'maximum' that is larger than the module's declared 'maximum' import memory size")));
}
jsInstance->setMemory(vm, memory);
RETURN_IF_EXCEPTION(throwScope, nullptr);
break;
}
case Wasm::ExternalKind::Global: {
ASSERT(moduleInformation.globals[import.kindIndex].mutability == Wasm::Global::Immutable);
if (moduleInformation.globals[import.kindIndex].type == Wasm::I64)
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "imported global", "cannot be an i64")));
if (!value.isNumber())
return exception(createJSWebAssemblyLinkError(exec, vm, importFailMessage(import, "imported global", "must be a number")));
ASSERT(numImportGlobals == import.kindIndex);
switch (moduleInformation.globals[import.kindIndex].type) {
case Wasm::I32:
jsInstance->instance().setGlobal(numImportGlobals++, value.toInt32(exec));
break;
case Wasm::F32:
jsInstance->instance().setGlobal(numImportGlobals++, bitwise_cast<uint32_t>(value.toFloat(exec)));
break;
case Wasm::F64:
jsInstance->instance().setGlobal(numImportGlobals++, bitwise_cast<uint64_t>(value.asNumber()));
break;
default:
RELEASE_ASSERT_NOT_REACHED();
}
throwScope.assertNoException();
break;
}
}
}
{
if (!!moduleInformation.memory && moduleInformation.memory.isImport()) {
RELEASE_ASSERT(hasMemoryImport);
}
if (moduleInformation.memory && !hasMemoryImport) {
RELEASE_ASSERT(!moduleInformation.memory.isImport());
auto* jsMemory = JSWebAssemblyMemory::create(exec, vm, exec->lexicalGlobalObject()->WebAssemblyMemoryStructure());
RETURN_IF_EXCEPTION(throwScope, nullptr);
RefPtr<Wasm::Memory> memory = Wasm::Memory::create(moduleInformation.memory.initial(), moduleInformation.memory.maximum(),
[&vm] (Wasm::Memory::NotifyPressure) { vm.heap.collectAsync(CollectionScope::Full); },
[&vm] (Wasm::Memory::SyncTryToReclaim) { vm.heap.collectSync(CollectionScope::Full); },
[&vm, jsMemory] (Wasm::Memory::GrowSuccess, Wasm::PageCount oldPageCount, Wasm::PageCount newPageCount) { jsMemory->growSuccessCallback(vm, oldPageCount, newPageCount); });
if (!memory)
return exception(createOutOfMemoryError(exec));
jsMemory->adopt(memory.releaseNonNull());
jsInstance->setMemory(vm, jsMemory);
RETURN_IF_EXCEPTION(throwScope, nullptr);
}
}
{
if (!!moduleInformation.tableInformation && moduleInformation.tableInformation.isImport()) {
RELEASE_ASSERT(hasTableImport);
}
if (!!moduleInformation.tableInformation && !hasTableImport) {
RELEASE_ASSERT(!moduleInformation.tableInformation.isImport());
RefPtr<Wasm::Table> wasmTable = Wasm::Table::create(moduleInformation.tableInformation.initial(), moduleInformation.tableInformation.maximum());
if (!wasmTable)
return exception(createJSWebAssemblyLinkError(exec, vm, "couldn't create Table"));
JSWebAssemblyTable* table = JSWebAssemblyTable::create(exec, vm, exec->lexicalGlobalObject()->WebAssemblyTableStructure(), wasmTable.releaseNonNull());
throwScope.assertNoException();
ASSERT(table);
jsInstance->setTable(vm, table);
RETURN_IF_EXCEPTION(throwScope, nullptr);
}
}
if (!jsInstance->memory()) {
auto* jsMemory = JSWebAssemblyMemory::create(exec, vm, exec->lexicalGlobalObject()->WebAssemblyMemoryStructure());
jsMemory->adopt(Wasm::Memory::create().releaseNonNull());
jsInstance->setMemory(vm, jsMemory);
RETURN_IF_EXCEPTION(throwScope, nullptr);
}
{
ASSERT(numImportGlobals == moduleInformation.firstInternalGlobal);
for (size_t globalIndex = numImportGlobals; globalIndex < moduleInformation.globals.size(); ++globalIndex) {
const auto& global = moduleInformation.globals[globalIndex];
ASSERT(global.initializationType != Wasm::Global::IsImport);
if (global.initializationType == Wasm::Global::FromGlobalImport) {
ASSERT(global.initialBitsOrImportNumber < numImportGlobals);
jsInstance->instance().setGlobal(globalIndex, jsInstance->instance().loadI64Global(global.initialBitsOrImportNumber));
} else
jsInstance->instance().setGlobal(globalIndex, global.initialBitsOrImportNumber);
}
}
return jsInstance;
}
}
#endif // ENABLE(WEBASSEMBLY)