From 02dc8c138610183666aa70469f972b62b1f7743f Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Fri, 11 Oct 2024 11:23:35 +0800 Subject: [PATCH] fix pvm tests (#158) --- JAMTests/Tests/JAMTests/PVMTests.swift | 26 ++----- .../Instructions/Instructions+Helpers.swift | 2 +- .../PolkaVM/Instructions/Instructions.swift | 76 +++++++++---------- README.md | 2 +- 4 files changed, 48 insertions(+), 58 deletions(-) diff --git a/JAMTests/Tests/JAMTests/PVMTests.swift b/JAMTests/Tests/JAMTests/PVMTests.swift index 2db3b9a1..14808010 100644 --- a/JAMTests/Tests/JAMTests/PVMTests.swift +++ b/JAMTests/Tests/JAMTests/PVMTests.swift @@ -64,12 +64,6 @@ struct PolkaVMTestcase: Codable, CustomStringConvertible { private let logger = Logger(label: "PVMTests") -// TODO: pass these -let knownFailedTestCases = [ - "inst_ret_halt", - "inst_ret_invalid", -] - struct PVMTests { init() { setupTestLogger() @@ -105,19 +99,15 @@ struct PVMTests { .trap } - try withKnownIssue("not yet implemented", isIntermittent: true) { - #expect(exitReason2 == testCase.expectedStatus) - #expect(vmState.getRegisters() == Registers(testCase.expectedRegs)) - #expect(vmState.pc == testCase.expectedPC) - for chunk in testCase.expectedMemory { - for (offset, byte) in chunk.contents.enumerated() { - let value = try vmState.getMemory().read(address: chunk.address + UInt32(offset)) - #expect(value == byte) - } + #expect(exitReason2 == testCase.expectedStatus) + #expect(vmState.getRegisters() == Registers(testCase.expectedRegs)) + #expect(vmState.pc == testCase.expectedPC) + for chunk in testCase.expectedMemory { + for (offset, byte) in chunk.contents.enumerated() { + let value = try vmState.getMemory().read(address: chunk.address + UInt32(offset)) + #expect(value == byte) } - #expect(vmState.getGas() == testCase.expectedGas) - } when: { - knownFailedTestCases.contains(testCase.name) } + #expect(vmState.getGas() == testCase.expectedGas) } } diff --git a/PolkaVM/Sources/PolkaVM/Instructions/Instructions+Helpers.swift b/PolkaVM/Sources/PolkaVM/Instructions/Instructions+Helpers.swift index c9584fe5..0d048f1b 100644 --- a/PolkaVM/Sources/PolkaVM/Instructions/Instructions+Helpers.swift +++ b/PolkaVM/Sources/PolkaVM/Instructions/Instructions+Helpers.swift @@ -53,7 +53,7 @@ extension Instructions { let end = start + entrySize var targetAlignedData = context.state.program.jumpTable[relative: start ..< end] guard let targetAligned = targetAlignedData.decode() else { - fatalError("unreachable: jump table entry should be valid") + return .exit(.panic(.trap)) } guard isDjumpValid(context: context, target: target, targetAligned: UInt32(truncatingIfNeeded: targetAligned)) else { diff --git a/PolkaVM/Sources/PolkaVM/Instructions/Instructions.swift b/PolkaVM/Sources/PolkaVM/Instructions/Instructions.swift index 07228b3f..727024c2 100644 --- a/PolkaVM/Sources/PolkaVM/Instructions/Instructions.swift +++ b/PolkaVM/Sources/PolkaVM/Instructions/Instructions.swift @@ -157,7 +157,7 @@ public enum Instructions { public init(data: Data) throws { register = try Registers.Index(ra: data.at(relative: 0)) - offset = try Instructions.decodeImmediate(data.at(relative: 1...)) + offset = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context _: ExecutionContext) -> ExecOutcome { .continued } @@ -176,7 +176,7 @@ public enum Instructions { public init(data: Data) throws { register = try Registers.Index(ra: data.at(relative: 0)) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -193,7 +193,7 @@ public enum Instructions { public init(data: Data) throws { register = try Registers.Index(ra: data.at(relative: 0)) - address = try Instructions.decodeImmediate(data.at(relative: 1...)) + address = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -211,7 +211,7 @@ public enum Instructions { public init(data: Data) throws { register = try Registers.Index(ra: data.at(relative: 0)) - address = try Instructions.decodeImmediate(data.at(relative: 1...)) + address = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -229,7 +229,7 @@ public enum Instructions { public init(data: Data) throws { register = try Registers.Index(ra: data.at(relative: 0)) - address = try Instructions.decodeImmediate(data.at(relative: 1...)) + address = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -248,7 +248,7 @@ public enum Instructions { public init(data: Data) throws { register = try Registers.Index(ra: data.at(relative: 0)) - address = try Instructions.decodeImmediate(data.at(relative: 1...)) + address = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -267,7 +267,7 @@ public enum Instructions { public init(data: Data) throws { register = try Registers.Index(ra: data.at(relative: 0)) - address = try Instructions.decodeImmediate(data.at(relative: 1...)) + address = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -286,7 +286,7 @@ public enum Instructions { public init(data: Data) throws { register = try Registers.Index(ra: data.at(relative: 0)) - address = try Instructions.decodeImmediate(data.at(relative: 1...)) + address = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -304,7 +304,7 @@ public enum Instructions { public init(data: Data) throws { register = try Registers.Index(ra: data.at(relative: 0)) - address = try Instructions.decodeImmediate(data.at(relative: 1...)) + address = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -322,7 +322,7 @@ public enum Instructions { public init(data: Data) throws { register = try Registers.Index(ra: data.at(relative: 0)) - address = try Instructions.decodeImmediate(data.at(relative: 1...)) + address = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -576,7 +576,7 @@ public enum Instructions { public init(data: Data) throws { (src, dest) = try Instructions.deocdeRegisters(data) - offset = try Instructions.decodeImmediate(data.at(relative: 1...)) + offset = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -595,7 +595,7 @@ public enum Instructions { public init(data: Data) throws { (src, dest) = try Instructions.deocdeRegisters(data) - offset = try Instructions.decodeImmediate(data.at(relative: 1...)) + offset = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -614,7 +614,7 @@ public enum Instructions { public init(data: Data) throws { (src, dest) = try Instructions.deocdeRegisters(data) - offset = try Instructions.decodeImmediate(data.at(relative: 1...)) + offset = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -633,7 +633,7 @@ public enum Instructions { public init(data: Data) throws { (dest, src) = try Instructions.deocdeRegisters(data) - offset = try Instructions.decodeImmediate(data.at(relative: 1...)) + offset = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -652,7 +652,7 @@ public enum Instructions { public init(data: Data) throws { (dest, src) = try Instructions.deocdeRegisters(data) - offset = try Instructions.decodeImmediate(data.at(relative: 1...)) + offset = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -671,7 +671,7 @@ public enum Instructions { public init(data: Data) throws { (dest, src) = try Instructions.deocdeRegisters(data) - offset = try Instructions.decodeImmediate(data.at(relative: 1...)) + offset = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -691,7 +691,7 @@ public enum Instructions { public init(data: Data) throws { (dest, src) = try Instructions.deocdeRegisters(data) - offset = try Instructions.decodeImmediate(data.at(relative: 1...)) + offset = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -711,7 +711,7 @@ public enum Instructions { public init(data: Data) throws { (dest, src) = try Instructions.deocdeRegisters(data) - offset = try Instructions.decodeImmediate(data.at(relative: 1...)) + offset = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome { @@ -731,7 +731,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -750,7 +750,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -769,7 +769,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -788,7 +788,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -807,7 +807,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -826,7 +826,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -848,7 +848,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -867,7 +867,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -886,7 +886,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -905,7 +905,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -925,7 +925,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -945,7 +945,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -965,7 +965,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -984,7 +984,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -1003,7 +1003,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -1022,7 +1022,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -1042,7 +1042,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -1062,7 +1062,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -1082,7 +1082,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { @@ -1103,7 +1103,7 @@ public enum Instructions { public init(data: Data) throws { (ra, rb) = try Instructions.deocdeRegisters(data) - value = try Instructions.decodeImmediate(data.at(relative: 1...)) + value = Instructions.decodeImmediate((try? data.at(relative: 1...)) ?? Data()) } public func _executeImpl(context: ExecutionContext) -> ExecOutcome { diff --git a/README.md b/README.md index ed0f9618..5078d6b5 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ JAM built with Swift ## Development Environment - Install tools and eps - - macos: `brwe install swiftlint swiftformat rocksdb` + - macos: `brew install swiftlint swiftformat rocksdb` - linux: `apt-get install librocksdb-dev libzstd-dev libbz2-dev liblz4-dev` - Precommit hooks: `make githooks` - Pull submodules: `git submodule update --init --recursive`