mirror of
https://github.com/fluencelabs/asmble
synced 2025-07-31 05:51:55 +00:00
initial commit
This commit is contained in:
@@ -3,6 +3,9 @@ package asmble.cli
|
||||
import asmble.ast.Script
|
||||
import asmble.compile.jvm.*
|
||||
import asmble.run.jvm.*
|
||||
import asmble.run.jvm.native_modules.LoggerModule
|
||||
import asmble.run.jvm.native_modules.EnvModule
|
||||
import asmble.run.jvm.native_modules.wasi.WASIModule
|
||||
import java.io.File
|
||||
import java.io.PrintWriter
|
||||
import java.util.*
|
||||
@@ -86,35 +89,40 @@ abstract class ScriptCommand<T> : Command<T>() {
|
||||
}
|
||||
}
|
||||
|
||||
val memBuilder = args.memoryBuilder
|
||||
// TODO: this hack should be refactored in future with rewritting the part of Asmble core code
|
||||
// check that there is only one main module
|
||||
if(context.modules.size == 1 && args.memoryBuilder != null) {
|
||||
val capacity = Math.multiplyExact(args.defaultMaxMemPages, Mem.PAGE_SIZE)
|
||||
val moduleMem = args.memoryBuilder.build(capacity)
|
||||
context.moduleMem = moduleMem
|
||||
|
||||
// add the WASI Wasm module for support a subset of WASI syscalls
|
||||
// WASI is enabled only for one module mode
|
||||
context = context.withModuleRegistered(
|
||||
"wasi_unstable",
|
||||
Module.Native(WASIModule(moduleMem))
|
||||
)
|
||||
}
|
||||
|
||||
// throws ArithmeticException if the result overflows an int
|
||||
val capacity = Math.multiplyExact(args.defaultMaxMemPages, Mem.PAGE_SIZE)
|
||||
|
||||
// Do registrations
|
||||
context = args.registrations.fold(context) { ctx, (moduleName, className) ->
|
||||
if (memBuilder != null) {
|
||||
ctx.withModuleRegistered(moduleName,
|
||||
Module.Native(Class.forName(className, true, ctx.classLoader)
|
||||
.getConstructor(MemoryBuffer::class.java)
|
||||
.newInstance(memBuilder.build(capacity))))
|
||||
} else {
|
||||
ctx.withModuleRegistered(moduleName,
|
||||
Module.Native(Class.forName(className, true, ctx.classLoader).newInstance()))
|
||||
}
|
||||
ctx.withModuleRegistered(moduleName,
|
||||
Module.Native(Class.forName(className, true, ctx.classLoader).newInstance()))
|
||||
}
|
||||
|
||||
if (args.specTestRegister) context = context.withHarnessRegistered()
|
||||
|
||||
if (args.enableLogger) {
|
||||
// add logger Wasm module for logging
|
||||
context =
|
||||
context.withModuleRegistered(
|
||||
// add the logger Wasm module for logging
|
||||
context = context.withModuleRegistered(
|
||||
"logger",
|
||||
Module.Native(LoggerModule(PrintWriter(System.out)))
|
||||
)
|
||||
}
|
||||
|
||||
// add env Wasm module for gas metering
|
||||
// add the env Wasm module for gas metering
|
||||
context =
|
||||
context.withModuleRegistered(
|
||||
"env",
|
||||
|
@@ -77,6 +77,7 @@ interface Module {
|
||||
// If there is a memory import, we have to get the one with the mem class as the first
|
||||
val memImport = mod.imports.find { it.kind is Node.Import.Kind.Memory }
|
||||
val builder = ctx.memoryBuilder
|
||||
val moduleMem = ctx.moduleMem
|
||||
|
||||
val memLimit = if (memImport != null) {
|
||||
constructor = cls.declaredConstructors.find { it.parameterTypes.firstOrNull()?.ref == mem.memType }
|
||||
@@ -91,6 +92,11 @@ interface Module {
|
||||
throw RunErr.ImportMemoryCapacityTooLarge(it * Mem.PAGE_SIZE, memCap)
|
||||
}
|
||||
memLimit
|
||||
} else if (moduleMem != null) {
|
||||
constructor = cls.declaredConstructors.find { it.parameterTypes.firstOrNull()?.ref == mem.memType }
|
||||
|
||||
constructorParams += moduleMem
|
||||
ctx.defaultMaxMemPages * Mem.PAGE_SIZE
|
||||
} else if (builder != null) {
|
||||
constructor = cls.declaredConstructors.find { it.parameterTypes.firstOrNull()?.ref == mem.memType }
|
||||
|
||||
|
@@ -6,6 +6,7 @@ import asmble.ast.Script
|
||||
import asmble.compile.jvm.*
|
||||
import asmble.io.AstToSExpr
|
||||
import asmble.io.SExprToStr
|
||||
import asmble.run.jvm.native_modules.TestHarness
|
||||
import asmble.util.Logger
|
||||
import asmble.util.toRawIntBits
|
||||
import asmble.util.toRawLongBits
|
||||
@@ -44,7 +45,8 @@ data class ScriptContext(
|
||||
val exceptionTranslator: ExceptionTranslator = ExceptionTranslator,
|
||||
val defaultMaxMemPages: Int = 1,
|
||||
val includeBinaryInCompiledClass: Boolean = false,
|
||||
val memoryBuilder: MemoryBufferBuilder? = null
|
||||
val memoryBuilder: MemoryBufferBuilder? = null,
|
||||
var moduleMem: MemoryBuffer? = null
|
||||
) : Logger by logger {
|
||||
|
||||
fun withHarnessRegistered(out: PrintWriter = PrintWriter(System.out, true)) =
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package asmble.run.jvm
|
||||
package asmble.run.jvm.native_modules
|
||||
|
||||
/**
|
||||
* Module used for gas and EIC metering.
|
@@ -1,4 +1,4 @@
|
||||
package asmble.run.jvm
|
||||
package asmble.run.jvm.native_modules
|
||||
|
||||
import asmble.compile.jvm.Mem
|
||||
import java.io.PrintWriter
|
@@ -1,7 +1,5 @@
|
||||
package asmble.run.jvm
|
||||
package asmble.run.jvm.native_modules
|
||||
|
||||
import asmble.annotation.WasmExport
|
||||
import asmble.annotation.WasmExternalKind
|
||||
import asmble.compile.jvm.Mem
|
||||
import asmble.compile.jvm.MemoryBuffer
|
||||
import asmble.compile.jvm.MemoryByteBuffer
|
@@ -0,0 +1,78 @@
|
||||
package asmble.run.jvm.native_modules.wasi
|
||||
|
||||
import asmble.ast.Script
|
||||
import asmble.compile.jvm.MemoryBuffer
|
||||
import asmble.run.jvm.ScriptAssertionError
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* Module used for support WASI, contains the realization of several WASI imports
|
||||
* for operating with file system. Should be registered as 'wasi_unstable'.
|
||||
*/
|
||||
open class WASIModule(private val mem: MemoryBuffer) {
|
||||
/*
|
||||
(import "wasi_unstable" "fd_prestat_get" (func $__wasi_fd_prestat_get (type 2)))
|
||||
(import "wasi_unstable" "fd_prestat_dir_name" (func $__wasi_fd_prestat_dir_name (type 1)))
|
||||
+ (import "wasi_unstable" "environ_sizes_get" (func $__wasi_environ_sizes_get (type 2)))
|
||||
+ (import "wasi_unstable" "environ_get" (func $__wasi_environ_get (type 2)))
|
||||
+ (import "wasi_unstable" "args_sizes_get" (func $__wasi_args_sizes_get (type 2)))
|
||||
+ (import "wasi_unstable" "args_get" (func $__wasi_args_get (type 2)))
|
||||
(import "wasi_unstable" "fd_close" (func $fd_close (type 3)))
|
||||
(import "wasi_unstable" "fd_write" (func $fd_write (type 9)))
|
||||
(import "wasi_unstable" "fd_read" (func $fd_read (type 9)))
|
||||
(import "wasi_unstable" "path_open" (func $path_open (type 10)))
|
||||
+ (import "wasi_unstable" "proc_exit" (func $__wasi_proc_exit (type 0)))
|
||||
(import "wasi_unstable" "fd_fdstat_get" (func $__wasi_fd_fdstat_get (type 2)))
|
||||
*/
|
||||
|
||||
// this error code will be returned by all non-implemented methods
|
||||
private val wasiErrorCode: Int = __WASI_EACCES
|
||||
|
||||
private val stdin: Int = 0
|
||||
private val stdout: Int = 1
|
||||
private val stderr: Int = 2
|
||||
|
||||
// mapping between file descriptors and opened file
|
||||
private val openFiles = HashMap<Int, File>()
|
||||
|
||||
/**
|
||||
* [Wasm function]
|
||||
* Returns a command-line arguments size.
|
||||
*/
|
||||
fun args_sizes_get(_param1: Int, _param2: Int): Int {
|
||||
return wasiErrorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Wasm function]
|
||||
* Reads a command-line arguments.
|
||||
*/
|
||||
fun args_get(_param1: Int, _param2: Int): Int {
|
||||
return wasiErrorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Wasm function]
|
||||
* Returns a command-line arguments size.
|
||||
*/
|
||||
fun environ_sizes_get(_param1: Int, _param2: Int): Int {
|
||||
return wasiErrorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Wasm function]
|
||||
* Reads a command-line arguments.
|
||||
*/
|
||||
fun environ_get(_param1: Int, _param2: Int): Int {
|
||||
return wasiErrorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Wasm function]
|
||||
* Reads a command-line arguments.
|
||||
*/
|
||||
fun proc_exit(_param1: Int){
|
||||
throw ScriptAssertionError(Script.Cmd.Assertion.Trap(Script.Cmd.Action.Get("", ""), "trap"), "proc_exit")
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
package asmble.run.jvm.native_modules.wasi
|
||||
|
||||
val __WASI_ESUCCESS = 0
|
||||
val __WASI_E2BIG = 1
|
||||
val __WASI_EACCES = 2
|
||||
val __WASI_EADDRINUSE = 3
|
||||
val __WASI_EADDRNOTAVAIL = 4
|
||||
val __WASI_EAFNOSUPPORT = 5
|
||||
val __WASI_EAGAIN = 6
|
||||
val __WASI_EALREADY = 7
|
||||
val __WASI_EBADF = 8
|
||||
val __WASI_EBADMSG = 9
|
||||
val __WASI_EBUSY = 10
|
||||
val __WASI_ECANCELED = 11
|
||||
val __WASI_ECHILD = 12
|
||||
val __WASI_ECONNABORTED = 13
|
||||
val __WASI_ECONNREFUSED = 14
|
||||
val __WASI_ECONNRESET = 15
|
||||
val __WASI_EDEADLK = 16
|
||||
val __WASI_EDESTADDRREQ = 17
|
||||
val __WASI_EDOM = 18
|
||||
val __WASI_EDQUOT = 19
|
||||
val __WASI_EEXIST = 20
|
||||
val __WASI_EFAULT = 21
|
||||
val __WASI_EFBIG = 22
|
||||
val __WASI_EHOSTUNREACH = 23
|
||||
val __WASI_EIDRM = 24
|
||||
val __WASI_EILSEQ = 25
|
||||
val __WASI_EINPROGRESS = 26
|
||||
val __WASI_EINTR = 27
|
||||
val __WASI_EINVAL = 28
|
||||
val __WASI_EIO = 29
|
||||
val __WASI_EISCONN = 30
|
||||
val __WASI_EISDIR = 31
|
||||
val __WASI_ELOOP = 32
|
||||
val __WASI_EMFILE = 33
|
||||
val __WASI_EMLINK = 34
|
||||
val __WASI_EMSGSIZE = 35
|
||||
val __WASI_EMULTIHOP = 36
|
||||
val __WASI_ENAMETOOLONG = 37
|
||||
val __WASI_ENETDOWN = 38
|
||||
val __WASI_ENETRESET = 39
|
||||
val __WASI_ENETUNREACH = 40
|
||||
val __WASI_ENFILE = 41
|
||||
val __WASI_ENOBUFS = 42
|
||||
val __WASI_ENODEV = 43
|
||||
val __WASI_ENOENT = 44
|
||||
val __WASI_ENOEXEC = 45
|
||||
val __WASI_ENOLCK = 46
|
||||
val __WASI_ENOLINK = 47
|
||||
val __WASI_ENOMEM = 48
|
||||
val __WASI_ENOMSG = 49
|
||||
val __WASI_ENOPROTOOPT = 50
|
||||
val __WASI_ENOSPC = 51
|
||||
val __WASI_ENOSYS = 52
|
||||
val __WASI_ENOTCONN = 53
|
||||
val __WASI_ENOTDIR = 54
|
||||
val __WASI_ENOTEMPTY = 55
|
||||
val __WASI_ENOTRECOVERABLE = 56
|
||||
val __WASI_ENOTSOCK = 57
|
||||
val __WASI_ENOTSUP = 58
|
||||
val __WASI_ENOTTY = 59
|
||||
val __WASI_ENXIO = 60
|
||||
val __WASI_EOVERFLOW = 61
|
||||
val __WASI_EOWNERDEAD = 62
|
||||
val __WASI_EPERM = 63
|
||||
val __WASI_EPIPE = 64
|
||||
val __WASI_EPROTO = 65
|
||||
val __WASI_EPROTONOSUPPORT = 66
|
||||
val __WASI_EPROTOTYPE = 67
|
||||
val __WASI_ERANGE = 68
|
||||
val __WASI_EROFS = 69
|
||||
val __WASI_ESPIPE = 70
|
||||
val __WASI_ESRCH = 71
|
||||
val __WASI_ESTALE = 72
|
||||
val __WASI_ETIMEDOUT = 73
|
||||
val __WASI_ETXTBSY = 74
|
||||
val __WASI_EXDEV = 75
|
||||
val __WASI_ENOTCAPABLE = 76
|
@@ -1,6 +1,7 @@
|
||||
package asmble.run.jvm
|
||||
|
||||
import asmble.TestBase
|
||||
import asmble.run.jvm.native_modules.LoggerModule
|
||||
import org.junit.Test
|
||||
import java.io.PrintWriter
|
||||
import java.io.StringWriter
|
||||
|
Reference in New Issue
Block a user