Skip to content

Commit

Permalink
fix pvm tests (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiweiii authored Oct 11, 2024
1 parent 9211cc8 commit 02dc8c1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 58 deletions.
26 changes: 8 additions & 18 deletions JAMTests/Tests/JAMTests/PVMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
76 changes: 38 additions & 38 deletions PolkaVM/Sources/PolkaVM/Instructions/Instructions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 02dc8c1

Please sign in to comment.