Skip to content

Commit

Permalink
Move memory to wasi-emscripten-host (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
illarionov authored Jun 19, 2024
1 parent 033edf0 commit d35b1ed
Show file tree
Hide file tree
Showing 38 changed files with 265 additions and 326 deletions.
2 changes: 1 addition & 1 deletion sqlite-common/api/sqlite-common.api
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public final class ru/pixnews/wasm/sqlite/open/helper/sqlite/common/api/SqliteTr
}

public final class ru/pixnews/wasm/sqlite/open/helper/sqlite/common/capi/Sqlite3CApi {
public fun <init> (Lru/pixnews/wasm/sqlite/open/helper/embedder/exports/SqliteExports;Lru/pixnews/wasm/sqlite/open/helper/embedder/SQLiteEmbedderRuntimeInfo;Lru/pixnews/wasm/sqlite/open/helper/host/base/memory/EmbedderMemory;Lru/pixnews/wasm/sqlite/open/helper/embedder/callback/SqliteCallbackStore;Lru/pixnews/wasm/sqlite/open/helper/embedder/functiontable/Sqlite3CallbackFunctionIndexes;Lru/pixnews/wasm/sqlite/open/helper/common/api/Logger;)V
public fun <init> (Lru/pixnews/wasm/sqlite/open/helper/embedder/exports/SqliteExports;Lru/pixnews/wasm/sqlite/open/helper/embedder/SQLiteEmbedderRuntimeInfo;Lru/pixnews/wasm/sqlite/open/helper/host/base/memory/Memory;Lru/pixnews/wasm/sqlite/open/helper/embedder/callback/SqliteCallbackStore;Lru/pixnews/wasm/sqlite/open/helper/embedder/functiontable/Sqlite3CallbackFunctionIndexes;Lru/pixnews/wasm/sqlite/open/helper/common/api/Logger;)V
public final fun getConfig ()Lru/pixnews/wasm/sqlite/open/helper/sqlite/common/capi/Sqlite3ConfigFunctions;
public final fun getDb ()Lru/pixnews/wasm/sqlite/open/helper/sqlite/common/capi/Sqlite3DbFunctions;
public final fun getEmbedderInfo ()Lru/pixnews/wasm/sqlite/open/helper/embedder/SQLiteEmbedderRuntimeInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi
import ru.pixnews.wasm.sqlite.open.helper.embedder.callback.SqliteCallbackStore
import ru.pixnews.wasm.sqlite.open.helper.embedder.exports.SqliteExports
import ru.pixnews.wasm.sqlite.open.helper.embedder.functiontable.Sqlite3CallbackFunctionIndexes
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.EmbedderMemory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory

public interface SqliteEmbedder<E : SqliteEmbedderConfig, I : SqliteRuntimeInstance> {
@InternalWasmSqliteHelperApi
Expand All @@ -23,7 +23,7 @@ public interface SqliteEmbedder<E : SqliteEmbedderConfig, I : SqliteRuntimeInsta
@InternalWasmSqliteHelperApi
public interface SqliteWasmEnvironment<I : SqliteRuntimeInstance> {
public val sqliteExports: SqliteExports
public val memory: EmbedderMemory
public val memory: Memory
public val callbackStore: SqliteCallbackStore
public val callbackFunctionIndexes: Sqlite3CallbackFunctionIndexes
public val runtimeInstance: I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ package ru.pixnews.wasm.sqlite.open.helper.embedder.exports

import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi
import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.EmbedderMemory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.writeNullTerminatedString

@InternalWasmSqliteHelperApi
public fun SqliteDynamicMemoryExports.allocNullTerminatedString(
memory: EmbedderMemory,
memory: Memory,
string: String,
): WasmPtr<Byte> {
val bytes = string.encodeToByteArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import ru.pixnews.wasm.sqlite.open.helper.embedder.SQLiteEmbedderRuntimeInfo
import ru.pixnews.wasm.sqlite.open.helper.embedder.callback.SqliteCallbackStore
import ru.pixnews.wasm.sqlite.open.helper.embedder.exports.SqliteExports
import ru.pixnews.wasm.sqlite.open.helper.embedder.functiontable.Sqlite3CallbackFunctionIndexes
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.EmbedderMemory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory
import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.capi.databaseresources.SqliteDatabaseResourcesRegistry

public class Sqlite3CApi(
sqliteExports: SqliteExports,
public val embedderInfo: SQLiteEmbedderRuntimeInfo,
memory: EmbedderMemory,
memory: Memory,
callbackStore: SqliteCallbackStore,
callbackFunctionIndexes: Sqlite3CallbackFunctionIndexes,
rootLogger: Logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ru.pixnews.wasm.sqlite.open.helper.embedder.exports.allocNullTerminatedSt
import ru.pixnews.wasm.sqlite.open.helper.embedder.exports.sqliteFreeSilent
import ru.pixnews.wasm.sqlite.open.helper.embedder.functiontable.Sqlite3CallbackFunctionIndexes
import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.EmbedderMemory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.readPtr
import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.api.SqliteDb
import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.api.SqliteDbConfigParameter
Expand All @@ -32,7 +32,7 @@ import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.capi.databaseresources.S

public class Sqlite3DbFunctions internal constructor(
private val sqliteExports: SqliteExports,
private val memory: EmbedderMemory,
private val memory: Memory,
private val callbackStore: SqliteCallbackStore,
private val callbackFunctionIndexes: Sqlite3CallbackFunctionIndexes,
private val databaseResources: SqliteDatabaseResourcesRegistry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package ru.pixnews.wasm.sqlite.open.helper.sqlite.common.capi
import ru.pixnews.wasm.sqlite.open.helper.embedder.exports.SqliteExports
import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr
import ru.pixnews.wasm.sqlite.open.helper.host.base.isSqlite3Null
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.EmbedderMemory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.readNullableNullTerminatedString
import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.api.SqliteDb
import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.api.SqliteErrorInfo
Expand All @@ -21,7 +21,7 @@ import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.api.SqliteResultCode.Com
*/
public class Sqlite3ErrorFunctions internal constructor(
private val sqliteExports: SqliteExports,
private val memory: EmbedderMemory,
private val memory: Memory,
) {
public fun sqlite3ErrCode(
sqliteDb: WasmPtr<SqliteDb>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package ru.pixnews.wasm.sqlite.open.helper.sqlite.common.capi
import ru.pixnews.wasm.sqlite.open.helper.embedder.exports.SqliteExports
import ru.pixnews.wasm.sqlite.open.helper.embedder.exports.sqliteFreeSilent
import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.EmbedderMemory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.readNullableNullTerminatedString
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.readPtr
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.write
Expand All @@ -25,7 +25,7 @@ import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.capi.databaseresources.S

public class Sqlite3StatementFunctions internal constructor(
private val sqliteExports: SqliteExports,
private val memory: EmbedderMemory,
private val memory: Memory,
private val databaseResourcesRegistry: SqliteDatabaseResourcesRegistry,
private val sqliteErrorApi: Sqlite3ErrorFunctions,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import ru.pixnews.wasm.sqlite.open.helper.embedder.callback.SqliteCallbackStore
import ru.pixnews.wasm.sqlite.open.helper.embedder.exports.SqliteExports
import ru.pixnews.wasm.sqlite.open.helper.embedder.functiontable.Sqlite3CallbackFunctionIndexes
import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.EmbedderMemory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory
import ru.pixnews.wasm.sqlite.open.helper.host.emscripten.export.EmscriptenRuntime
import ru.pixnews.wasm.sqlite.open.helper.host.emscripten.export.stack.EmscriptenStack
import java.util.concurrent.atomic.AtomicReference
Expand Down Expand Up @@ -75,7 +75,7 @@ public object ChasmSqliteEmbedder : SqliteEmbedder<ChasmSqliteEmbedderConfig, Ch

return object : SqliteWasmEnvironment<ChasmRuntimeInstance> {
override val sqliteExports: SqliteExports = ChasmSqliteExports(chasmInstance)
override val memory: EmbedderMemory = chasmInstance.memory
override val memory: Memory = chasmInstance.memory
override val callbackStore: SqliteCallbackStore = callbackStore
override val callbackFunctionIndexes: Sqlite3CallbackFunctionIndexes =
chasmInstance.indirectFunctionIndexes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import ru.pixnews.wasm.sqlite.open.helper.embedder.sqlitecb.SqliteCallbacksModul
import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost
import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmModules.ENV_MODULE_NAME
import ru.pixnews.wasm.sqlite.open.helper.host.base.function.IndirectFunctionTableIndex
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.DefaultWasiMemoryReader
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.DefaultWasiMemoryWriter
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.WASM_MEMORY_PAGE_SIZE
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.WASM_MEMORY_SQLITE_MAX_PAGES
import ru.pixnews.wasm.sqlite.open.helper.host.emscripten.export.stack.EmscriptenStack
Expand All @@ -55,19 +57,16 @@ internal class ChasmInstanceBuilder(
val store: Store = store()

val memoryImport = setupMemory(store, minMemorySize)
val memory = ChasmMemoryAdapter(
store,
(memoryImport.value as Memory).address,
host.fileSystem,
host.rootLogger,
)
val memory = ChasmMemoryAdapter(store, (memoryImport.value as Memory).address)
val wasiMemoryReader = DefaultWasiMemoryReader(memory, host.fileSystem, host.rootLogger)
val wasiMemoryWriter = DefaultWasiMemoryWriter(memory, host.fileSystem, host.rootLogger)

val sqliteCallbacksHostFunctions = getSqliteCallbacksHostFunctions(store, memory, host, callbackStore)

val hostImports = buildList {
add(memoryImport)
addAll(getEmscriptenHostFunctions(store, memory, host, emscriptenStackRef))
addAll(getWasiPreview1HostFunctions(store, memory, host))
addAll(getWasiPreview1HostFunctions(store, memory, wasiMemoryReader, wasiMemoryWriter, host))
addAll(sqliteCallbacksHostFunctions)
}

Expand Down Expand Up @@ -109,7 +108,10 @@ internal class ChasmInstanceBuilder(
ChasmModuleRuntimeErrorException(it)
}
val oldSize = table.elements.size
val newTable = table.grow(callbackHostFunctions.size, ReferenceValue.Function(Address.Function(0))).getOrThrow {
val newTable = table.grow(
callbackHostFunctions.size,
ReferenceValue.Function(Address.Function(0)),
).getOrThrow {
ChasmModuleRuntimeErrorException(it)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,13 @@ import io.github.charlietap.chasm.executor.runtime.store.Address
import io.github.charlietap.chasm.executor.runtime.store.Store
import ru.pixnews.wasm.sqlite.open.helper.chasm.ext.orThrow
import ru.pixnews.wasm.sqlite.open.helper.chasm.host.exception.ChasmModuleRuntimeErrorException
import ru.pixnews.wasm.sqlite.open.helper.common.api.Logger
import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.DefaultWasiMemoryReader
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.DefaultWasiMemoryWriter
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.ReadWriteStrategy
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.preview1.type.CiovecArray
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.preview1.type.Fd
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.preview1.type.IovecArray

internal class ChasmMemoryAdapter(
private val store: Store,
private val memoryAddress: Address.Memory,
fileSystem: FileSystem<*>,
logger: Logger,
) : Memory {
private val memoryReader = DefaultWasiMemoryReader(this, fileSystem, logger)
private val memoryWriter = DefaultWasiMemoryWriter(this, fileSystem, logger)

val memoryInstance get() = store.memory(memoryAddress).getOrThrow { ChasmModuleRuntimeErrorException(it) }

override fun readI8(addr: WasmPtr<*>): Byte {
Expand Down Expand Up @@ -78,14 +65,6 @@ internal class ChasmMemoryAdapter(
}
}

override fun readFromChannel(fd: Fd, strategy: ReadWriteStrategy, iovecs: IovecArray): ULong {
return memoryReader.read(fd, strategy, iovecs)
}

override fun writeToChannel(fd: Fd, strategy: ReadWriteStrategy, cioVecs: CiovecArray): ULong {
return memoryWriter.write(fd, strategy, cioVecs)
}

// XXX
fun grow(pagesToAdd: Int): Int {
val oldPages = memoryInstance.data.min.amount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package ru.pixnews.wasm.sqlite.open.helper.chasm.host.module.wasi

import io.github.charlietap.chasm.embedding.function
import io.github.charlietap.chasm.executor.runtime.instance.HostFunction
import io.github.charlietap.chasm.executor.runtime.store.Store
import io.github.charlietap.chasm.executor.runtime.value.NumberValue.I32
import io.github.charlietap.chasm.import.Import
Expand All @@ -26,6 +25,8 @@ import ru.pixnews.wasm.sqlite.open.helper.chasm.host.module.wasi.function.NotImp
import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost
import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmModules.WASI_SNAPSHOT_PREVIEW1_MODULE_NAME
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.WasiMemoryReader
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.WasiMemoryWriter
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.WasiHostFunction
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.WasiHostFunction.ENVIRON_GET
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.WasiHostFunction.ENVIRON_SIZES_GET
Expand All @@ -36,10 +37,13 @@ import ru.pixnews.wasm.sqlite.open.helper.host.wasi.WasiHostFunction.FD_READ
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.WasiHostFunction.FD_SEEK
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.WasiHostFunction.FD_SYNC
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.WasiHostFunction.FD_WRITE
import io.github.charlietap.chasm.executor.runtime.instance.HostFunction as ChasmHostFunction

internal fun getWasiPreview1HostFunctions(
store: Store,
memory: ChasmMemoryAdapter,
wasiMemoryReader: WasiMemoryReader,
wasiMemoryWriter: WasiMemoryWriter,
host: EmbedderHost,
moduleName: String = WASI_SNAPSHOT_PREVIEW1_MODULE_NAME,
): List<Import> {
Expand All @@ -51,28 +55,31 @@ internal fun getWasiPreview1HostFunctions(
value = function(
store = store,
type = functionTypes.getValue(wasiFunc.type),
function = wasiFunc.createWasiHostFunctionHandle(host, memory).toChasmHostFunction(),
function = wasiFunc.createWasiHostFunctionHandle(host, memory, wasiMemoryReader, wasiMemoryWriter)
.toChasmHostFunction(),
),
)
}
}

private fun WasiHostFunctionHandle.toChasmHostFunction(): HostFunction = { args ->
private fun WasiHostFunctionHandle.toChasmHostFunction(): ChasmHostFunction = { args ->
listOf(I32(this.invoke(args).code))
}

private fun WasiHostFunction.createWasiHostFunctionHandle(
host: EmbedderHost,
memory: Memory,
wasiMemoryReader: WasiMemoryReader,
wasiMemoryWriter: WasiMemoryWriter,
): WasiHostFunctionHandle = when (this) {
ENVIRON_GET -> EnvironGet(host, memory)
ENVIRON_SIZES_GET -> EnvironSizesGet(host, memory)
FD_CLOSE -> FdClose(host)
FD_PREAD -> fdPread(host, memory)
FD_PWRITE -> fdPwrite(host, memory)
FD_READ -> fdRead(host, memory)
FD_PREAD -> fdPread(host, memory, wasiMemoryReader)
FD_PWRITE -> fdPwrite(host, memory, wasiMemoryWriter)
FD_READ -> fdRead(host, memory, wasiMemoryReader)
FD_SEEK -> FdSeek(host, memory)
FD_SYNC -> FdSync(host)
FD_WRITE -> fdWrite(host, memory)
FD_WRITE -> fdWrite(host, memory, wasiMemoryWriter)
else -> NotImplementedWasiFunction(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,36 @@ import ru.pixnews.wasm.sqlite.open.helper.chasm.host.module.wasi.WasiHostFunctio
import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost
import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory
import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.WasiMemoryReader
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.preview1.function.FdReadFdPreadFunctionHandle
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.preview1.type.Errno
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.preview1.type.Fd
import ru.pixnews.wasm.sqlite.open.helper.host.wasi.preview1.type.Iovec

internal class FdReadFdPread private constructor(
private val memory: Memory,
private val memoryReader: WasiMemoryReader,
private val handle: FdReadFdPreadFunctionHandle,
) : WasiHostFunctionHandle {
override fun invoke(args: List<ExecutionValue>): Errno {
val fd = Fd(args[0].asInt())
val pIov: WasmPtr<Iovec> = args[1].asWasmAddr()
val iovCnt = args[2].asInt()
val pNum: WasmPtr<Int> = args[3].asWasmAddr()
return handle.execute(memory, fd, pIov, iovCnt, pNum)
return handle.execute(memory, memoryReader, fd, pIov, iovCnt, pNum)
}

companion object {
fun fdRead(
host: EmbedderHost,
memory: Memory,
): WasiHostFunctionHandle = FdReadFdPread(memory, FdReadFdPreadFunctionHandle.fdRead(host))
memoryReader: WasiMemoryReader,
): WasiHostFunctionHandle = FdReadFdPread(memory, memoryReader, FdReadFdPreadFunctionHandle.fdRead(host))

fun fdPread(
host: EmbedderHost,
memory: Memory,
): WasiHostFunctionHandle = FdReadFdPread(memory, FdReadFdPreadFunctionHandle.fdPread(host))
memoryReader: WasiMemoryReader,
): WasiHostFunctionHandle = FdReadFdPread(memory, memoryReader, FdReadFdPreadFunctionHandle.fdPread(host))
}
}
Loading

0 comments on commit d35b1ed

Please sign in to comment.