From 48fd10aeeda3413c4a7ec30d9a8faaa6deb2cf10 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Fri, 23 Apr 2021 15:19:20 +0900 Subject: [PATCH] implemented SIGSEGV --- assets/disk0/segfault.js | 1 + assets/disk0/tvdos/TVDOS.SYS | 12 +++++++++++- assets/disk0/tvdos/bin/command.js | 11 ++++++++++- src/net/torvald/tsvm/VM.kt | 13 +++++++------ 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 assets/disk0/segfault.js diff --git a/assets/disk0/segfault.js b/assets/disk0/segfault.js new file mode 100644 index 0000000..5ad070d --- /dev/null +++ b/assets/disk0/segfault.js @@ -0,0 +1 @@ +sys.poke(66666,66); diff --git a/assets/disk0/tvdos/TVDOS.SYS b/assets/disk0/tvdos/TVDOS.SYS index bbaf8fe..91cc198 100644 --- a/assets/disk0/tvdos/TVDOS.SYS +++ b/assets/disk0/tvdos/TVDOS.SYS @@ -6,6 +6,13 @@ function InterruptedException(m) { InterruptedException.prototype = Object.create(Error.prototype); InterruptedException.prototype.name = 'InterruptedException'; InterruptedException.prototype.constructor = InterruptedException; +function IllegalAccessException(m) { + this.message = m; + this.stack = (new Error()).stack; +}; +IllegalAccessException.prototype = Object.create(Error.prototype); +IllegalAccessException.prototype.name = 'IllegalAccessException'; +IllegalAccessException.prototype.constructor = IllegalAccessException; class SIG { constructor(name, number) { this.name = "SIG" + name; @@ -155,9 +162,12 @@ var execApp = (cmdsrc, args) => { return status; } catch (e) { - serial.printerr(`app execution interrupted -- ${e}\n${e.stack}`); + serial.printerr(`app execution interrupted -- ${e}\n${e.stack || "(stack trace unavailable)"}`); + //serial.printerr(Object.entries(e)); if (e instanceof InterruptedException) return SIGTERM; + else if (e instanceof IllegalAccessException || `${e}`.startsWith("net.torvald.tsvm.ErrorIllegalAccess")) + return SIGSEGV; else return (undefined == status) ? 0 : status; } diff --git a/assets/disk0/tvdos/bin/command.js b/assets/disk0/tvdos/bin/command.js index 181f7a6..7ec061a 100644 --- a/assets/disk0/tvdos/bin/command.js +++ b/assets/disk0/tvdos/bin/command.js @@ -300,7 +300,7 @@ shell.coreutils = { } }, dir: function(args) { - var pathstr = (args[1] !== undefined) ? args[1] : "\\"+shell_pwd.join("\\"); + var pathstr = (args[1] !== undefined) ? args[1] : shell.getPwdString(); // check if path is valid var pathOpened = filesystem.open(CURRENT_DRIVE, pathstr, 'R'); @@ -309,6 +309,15 @@ shell.coreutils = { var port = filesystem._toPorts(CURRENT_DRIVE)[0] com.sendMessage(port, "LIST"); println(com.pullMessage(port)); + }, + cat: function(args) { + var pathstr = (args[1] !== undefined) ? args[1] : shell.getPwdString(); + + var pathOpened = filesystem.open(CURRENT_DRIVE, pathstr, 'R'); + if (!pathOpened) { printerrln("File not found"); return; } + let contents = filesystem.readAll(CURRENT_DRIVE); + // TODO just print out what's there + print(contents); } }; shell.coreutils.chdir = shell.coreutils.cd; diff --git a/src/net/torvald/tsvm/VM.kt b/src/net/torvald/tsvm/VM.kt index 6d9299b..268be99 100644 --- a/src/net/torvald/tsvm/VM.kt +++ b/src/net/torvald/tsvm/VM.kt @@ -10,6 +10,10 @@ import java.io.OutputStream import java.util.* import kotlin.math.ceil + +class ErrorIllegalAccess(vm: VM, addr: Long) : RuntimeException("Segmentation fault at 0x${addr.toString(16).padStart(8, '0')} on VM id ${vm.id}") + + /** * A class representing an instance of a Virtual Machine */ @@ -20,9 +24,6 @@ class VM( val roms: Array // first ROM must contain the BIOS ) { - class ErrorIllegalAccess(val addr: Long) : RuntimeException("Segmentation fault at 0x${addr.toString(16).padStart(8, '0')}") - - val id = java.util.Random().nextInt() val memsize = minOf(USER_SPACE_SIZE, _memsize.toLong()) @@ -117,10 +118,10 @@ class VM( internal fun poke(addr: Long, value: Byte) { val (memspace, offset) = translateAddr(addr) if (memspace == null) - throw ErrorIllegalAccess(addr) + throw ErrorIllegalAccess(this, addr) else if (memspace is UnsafePtr) { if (addr >= memspace.size) - throw ErrorIllegalAccess(addr) + throw ErrorIllegalAccess(this, addr) else memspace.set(offset, value) } @@ -134,7 +135,7 @@ class VM( null else if (memspace is UnsafePtr) { if (addr >= memspace.size) - throw ErrorIllegalAccess(addr) + throw ErrorIllegalAccess(this, addr) else memspace.get(offset) }