implemented SIGSEGV

This commit is contained in:
minjaesong
2021-04-23 15:19:20 +09:00
parent 04cafea3c5
commit 48fd10aeed
4 changed files with 29 additions and 8 deletions

View File

@@ -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<VMProgramRom?> // 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)
}