Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor pvm memory #257

Merged
merged 7 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Blockchain/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
do {
try await _callImpl(config: config, state: state)
return .continued
} catch let e as Memory.Error {
} catch let e as MemoryError {

Check warning on line 25 in Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCall.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCall.swift#L25

Added line #L25 was not covered by tests
logger.error("memory error: \(e)")
return .exit(.pageFault(e.address))
} catch VMInvocationsError.forceHalt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@
let innerVmIndex = UInt64(context.pvms.count)
let code = isReadable ? try state.readMemory(address: regs[0], length: Int(regs[1])) : nil
let pc = UInt32(truncatingIfNeeded: regs[2])
let mem = Memory(pageMap: [], chunks: [])
let mem = try GeneralMemory(pageMap: [], chunks: [])

Check warning on line 766 in Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift#L766

Added line #L766 was not covered by tests

if code == nil {
state.writeRegister(Registers.Index(raw: 7), HostCallResultCode.OOB.rawValue)
Expand Down Expand Up @@ -852,7 +852,7 @@
self.context = context
}

public func _callImpl(config: ProtocolConfigRef, state: VMState) async throws {
public func _callImpl(config _: ProtocolConfigRef, state: VMState) async throws {

Check warning on line 855 in Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift#L855

Added line #L855 was not covered by tests
let regs: [UInt64] = state.readRegisters(in: 7 ..< 10)

if context.pvms[regs[0]] == nil {
Expand All @@ -864,11 +864,7 @@
state.writeRegister(Registers.Index(raw: 7), HostCallResultCode.OOB.rawValue)
} else {
state.writeRegister(Registers.Index(raw: 7), HostCallResultCode.OK.rawValue)
// TODO: update after pvm memory is updated to have pages and able to allocate with accessiblity
try context.pvms[regs[0]]!.memory.write(
address: UInt32(truncatingIfNeeded: regs[1] * UInt64(config.value.pvmMemoryPageSize)),
values: Data(repeating: 0, count: Int(regs[2] * UInt64(config.value.pvmMemoryPageSize)))
)
try context.pvms[regs[0]]!.memory.zero(pageIndex: UInt32(truncatingIfNeeded: regs[1]), pages: Int(regs[2]))

Check warning on line 867 in Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift#L867

Added line #L867 was not covered by tests
}
}
}
Expand All @@ -883,24 +879,21 @@
self.context = context
}

public func _callImpl(config: ProtocolConfigRef, state: VMState) async throws {
public func _callImpl(config _: ProtocolConfigRef, state: VMState) async throws {

Check warning on line 882 in Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift#L882

Added line #L882 was not covered by tests
let regs: [UInt64] = state.readRegisters(in: 7 ..< 10)

if context.pvms[regs[0]] == nil {
state.writeRegister(Registers.Index(raw: 7), HostCallResultCode.WHO.rawValue)
return
}

// TODO: update when can check if pages are inaccessible
if (regs[1] + regs[2]) >= (1 << 32) {
if (regs[1] + regs[2]) >= (1 << 32) ||
!context.pvms[regs[0]]!.memory.isReadable(pageStart: UInt32(truncatingIfNeeded: regs[1]), pages: Int(regs[2]))
{

Check warning on line 892 in Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift#L890-L892

Added lines #L890 - L892 were not covered by tests
state.writeRegister(Registers.Index(raw: 7), HostCallResultCode.OOB.rawValue)
} else {
state.writeRegister(Registers.Index(raw: 7), HostCallResultCode.OK.rawValue)
// TODO: update after pvm memory is updated to have pages and able to allocate with accessiblity
try context.pvms[regs[0]]!.memory.write(
address: UInt32(truncatingIfNeeded: regs[1] * UInt64(config.value.pvmMemoryPageSize)),
values: Data(repeating: 0, count: Int(regs[2] * UInt64(config.value.pvmMemoryPageSize)))
)
try context.pvms[regs[0]]!.memory.void(pageIndex: UInt32(truncatingIfNeeded: regs[1]), pages: Int(regs[2]))

Check warning on line 896 in Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift#L896

Added line #L896 was not covered by tests
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions JAMTests/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions PolkaVM/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions PolkaVM/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let package = Package(
.package(path: "../TracingUtils"),
.package(url: "https://github.com/apple/swift-testing.git", branch: "0.10.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.6.0"),
.package(url: "https://github.com/nicklockwood/LRUCache.git", from: "1.0.7"),
],
targets: [
.target(
Expand All @@ -27,6 +28,7 @@ let package = Package(
"Utils",
"TracingUtils",
.product(name: "Logging", package: "swift-log"),
.product(name: "LRUCache", package: "LRUCache"),
]
),
.testTarget(
Expand Down
2 changes: 1 addition & 1 deletion PolkaVM/Sources/PolkaVM/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
}
logger.debug("execution success! updating pc...")
return updatePC(context: context, skip: skip)
} catch let e as Memory.Error {
} catch let e as MemoryError {

Check warning on line 38 in PolkaVM/Sources/PolkaVM/Instruction.swift

View check run for this annotation

Codecov / codecov/patch

PolkaVM/Sources/PolkaVM/Instruction.swift#L38

Added line #L38 was not covered by tests
logger.debug("memory error: \(e)")
return .exit(.pageFault(e.address))
} catch let e {
Expand Down
Loading