Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
coverage:
name: Code Coverage
runs-on: [self-hosted, linux]
timeout-minutes: 30
timeout-minutes: 40
steps:
- name: Checkout Code
uses: actions/checkout@v4
Expand Down
11 changes: 1 addition & 10 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 @@ -1373,6 +1373,12 @@ public class Provide: HostCall {
public class Log: HostCall {
public static var identifier: UInt8 { 100 }

private static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm:ss"
return formatter
}()

public func gasCost(state _: VMState) -> Gas {
Gas(0)
}
Expand Down Expand Up @@ -1445,9 +1451,7 @@ public class Log: HostCall {
let target = regs[1] == 0 && regs[2] == 0 ? nil : try? state.readMemory(address: regs[1], length: Int(regs[2]))
let message = try? state.readMemory(address: regs[3], length: Int(regs[4]))

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd HH:mm:ss"
let time = dateFormatter.string(from: Date())
let time = Self.dateFormatter.string(from: Date())

let details = Details(
time: time,
Expand Down
51 changes: 32 additions & 19 deletions Codec/Sources/Codec/IntegerCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,39 @@ extension Collection<UInt8> where SubSequence == Self {
IntegerCodec.decode { self.next() }
}

// this is pretty inefficient
// so need to ensure the usage of this is minimal
public mutating func decode<T: UnsignedInteger>(length: Int) -> T? {
IntegerCodec.decode(length: length) { self.next() }
}
}
guard length > 0, length <= count else { return nil }

public enum IntegerCodec {
public static func decode<T: UnsignedInteger>(length: Int, next: () throws -> UInt8?) rethrows -> T? {
guard length > 0 else {
return nil
}
var res: T = 0
for l in 0 ..< length {
guard let byte = try next() else {
return nil
// fast path for Data
if let data = self as? Data {
let result: T? = data.withUnsafeBytes { buffer in
guard length <= buffer.count else { return nil }
let ptr = buffer.bindMemory(to: UInt8.self)

var result: T = 0
for i in 0 ..< length {
let byte = T(ptr[i])
result |= byte << (8 * i)
}
return result
}
res = res | T(byte) << (8 * l)
self = dropFirst(length)
return result
}

// fallback
var result: T = 0
for i in 0 ..< length {
let index = index(startIndex, offsetBy: i)
let byte = T(self[index])
result |= byte << (8 * i)
}
return res
self = dropFirst(length)
return result
}
}

public enum IntegerCodec {
public static func decode(next: () throws -> UInt8?) rethrows -> UInt64? {
guard let firstByte = try next() else {
return nil
Expand All @@ -45,10 +56,12 @@ public enum IntegerCodec {
let byteLength = (~firstByte).leadingZeroBitCount
var res: UInt64 = 0
if byteLength > 0 {
guard let rest: UInt64 = try decode(length: byteLength, next: next) else {
return nil
for i in 0 ..< byteLength {
guard let byte = try next() else {
return nil
}
res |= UInt64(byte) << (8 * i)
}
res = rest
}

let mask = UInt8(UInt(1) << (8 - byteLength) - 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1600"
version = "1.7">
version = "2.2">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<AutocreatedTestPlanReference>
</AutocreatedTestPlanReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
buildConfiguration = "Release"
selectedDebuggerIdentifier = ""
selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
Expand All @@ -27,7 +38,7 @@
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand All @@ -40,12 +51,21 @@
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
savedToolIdentifier = "Time Profiler"
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "JAMTestsTests"
BuildableName = "JAMTestsTests"
BlueprintName = "JAMTestsTests"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
buildConfiguration = "Release">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
Expand Down
2 changes: 1 addition & 1 deletion JAMTests/Tests/JAMTests/jamtestnet/FuzzTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct FuzzTests {
("0.7.0/1756792661", "00000027"),
],
ignore: [
("0.7.0/1756548583", "00000009"), // TODO: hard
("0.7.0/1756548583", "00000009"), // TODO: find root cause
]
))
func v070(_ input: TestInput) async throws {
Expand Down
2 changes: 0 additions & 2 deletions PolkaVM/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ let package = Package(
.package(path: "../TracingUtils"),
.package(url: "https://github.com/apple/swift-testing.git", branch: "6.0.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 @@ -29,7 +28,6 @@ let package = Package(
"TracingUtils",
"CppHelper",
.product(name: "Logging", package: "swift-log"),
.product(name: "LRUCache", package: "LRUCache"),
],
swiftSettings: [
.interoperabilityMode(.Cxx),
Expand Down
11 changes: 11 additions & 0 deletions PolkaVM/Sources/PolkaVM/Executors/JIT/VMStateJIT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ final class VMStateJIT: VMState, @unchecked Sendable {
}
}

func writeMemory(address: some FixedWidthInteger, values: Data) throws {
let addr = UInt32(truncatingIfNeeded: address)
guard isMemoryWritable(address: addr, length: values.count) else {
throw VMError.invalidInstructionMemoryAccess
}
values.withUnsafeBytes { bytes in
let sourcePtr = bytes.bindMemory(to: UInt8.self).baseAddress!
memcpy(jitMemoryBasePtr.advanced(by: Int(addr)), sourcePtr, values.count)
}
}

func sbrk(_: UInt32) throws -> UInt32 {
// In JIT mode, memory allocation would need to be handled by the JIT runtime
// This would require coordination with the memory sandbox mechanism
Expand Down
33 changes: 3 additions & 30 deletions PolkaVM/Sources/PolkaVM/Instructions/Instructions+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,46 +75,19 @@ extension Instructions {
logger.trace("djump target data (\(targetAlignedData.map(\.self)))")
#endif

var targetAligned: any UnsignedInteger

switch entrySize {
case 1:
let u8: UInt8? = targetAlignedData.decode(length: entrySize)
guard let u8 else {
return .exit(.panic(.invalidDynamicJump))
}
targetAligned = u8
case 2:
let u16: UInt16? = targetAlignedData.decode(length: entrySize)
guard let u16 else {
return .exit(.panic(.invalidDynamicJump))
}
targetAligned = u16
case 3:
let u32: UInt32? = targetAlignedData.decode(length: entrySize)
guard let u32 else {
return .exit(.panic(.invalidDynamicJump))
}
targetAligned = u32
case 4:
let u32: UInt32? = targetAlignedData.decode(length: entrySize)
guard let u32 else {
return .exit(.panic(.invalidDynamicJump))
}
targetAligned = u32
default:
guard let targetAligned: UInt32 = targetAlignedData.decode(length: entrySize) else {
return .exit(.panic(.invalidDynamicJump))
}

#if DEBUG
logger.trace("djump target decoded (\(targetAligned))")
#endif

guard context.state.program.basicBlockIndices.contains(UInt32(targetAligned)) else {
guard context.state.program.basicBlockIndices.contains(targetAligned) else {
return .exit(.panic(.invalidDynamicJump))
}

context.state.updatePC(UInt32(targetAligned))
context.state.updatePC(targetAligned)
return .continued
}

Expand Down
24 changes: 12 additions & 12 deletions PolkaVM/Sources/PolkaVM/Instructions/Instructions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ extension CppHelper.Instructions.StoreImmU16: Instruction {
}

public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome {
try context.state.writeMemory(address: address, values: value.encode(method: .fixedWidth(2)))
try context.state.writeMemory(address: address, values: value.encode())
return .continued
}
}
Expand All @@ -102,7 +102,7 @@ extension CppHelper.Instructions.StoreImmU32: Instruction {
}

public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome {
try context.state.writeMemory(address: address, values: value.encode(method: .fixedWidth(4)))
try context.state.writeMemory(address: address, values: value.encode())
return .continued
}
}
Expand All @@ -114,7 +114,7 @@ extension CppHelper.Instructions.StoreImmU64: Instruction {
}

public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome {
try context.state.writeMemory(address: address, values: value.encode(method: .fixedWidth(8)))
try context.state.writeMemory(address: address, values: value.encode())
return .continued
}
}
Expand Down Expand Up @@ -289,7 +289,7 @@ extension CppHelper.Instructions.StoreU16: Instruction {

public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome {
let value: UInt16 = context.state.readRegister(reg)
try context.state.writeMemory(address: address, values: value.encode(method: .fixedWidth(2)))
try context.state.writeMemory(address: address, values: value.encode())
return .continued
}
}
Expand All @@ -303,7 +303,7 @@ extension CppHelper.Instructions.StoreU32: Instruction {

public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome {
let value: UInt32 = context.state.readRegister(reg)
try context.state.writeMemory(address: address, values: value.encode(method: .fixedWidth(4)))
try context.state.writeMemory(address: address, values: value.encode())
return .continued
}
}
Expand All @@ -317,7 +317,7 @@ extension CppHelper.Instructions.StoreU64: Instruction {

public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome {
let value: UInt64 = context.state.readRegister(reg)
try context.state.writeMemory(address: address, values: value.encode(method: .fixedWidth(8)))
try context.state.writeMemory(address: address, values: value.encode())
return .continued
}
}
Expand Down Expand Up @@ -345,7 +345,7 @@ extension CppHelper.Instructions.StoreImmIndU16: Instruction {
public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome {
try context.state.writeMemory(
address: context.state.readRegister(reg) &+ address,
values: value.encode(method: .fixedWidth(2))
values: value.encode()
)
return .continued
}
Expand All @@ -361,7 +361,7 @@ extension CppHelper.Instructions.StoreImmIndU32: Instruction {
public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome {
try context.state.writeMemory(
address: context.state.readRegister(reg) &+ address,
values: value.encode(method: .fixedWidth(4))
values: value.encode()
)
return .continued
}
Expand All @@ -377,7 +377,7 @@ extension CppHelper.Instructions.StoreImmIndU64: Instruction {
public func _executeImpl(context: ExecutionContext) throws -> ExecOutcome {
try context.state.writeMemory(
address: context.state.readRegister(reg) &+ address,
values: value.encode(method: .fixedWidth(8))
values: value.encode()
)
return .continued
}
Expand Down Expand Up @@ -719,7 +719,7 @@ extension CppHelper.Instructions.StoreIndU16: Instruction {
let value: UInt16 = context.state.readRegister(src)
try context.state.writeMemory(
address: context.state.readRegister(dest) &+ offset,
values: value.encode(method: .fixedWidth(2))
values: value.encode()
)
return .continued
}
Expand All @@ -736,7 +736,7 @@ extension CppHelper.Instructions.StoreIndU32: Instruction {
let value: UInt32 = context.state.readRegister(src)
try context.state.writeMemory(
address: context.state.readRegister(dest) &+ offset,
values: value.encode(method: .fixedWidth(4))
values: value.encode()
)
return .continued
}
Expand All @@ -753,7 +753,7 @@ extension CppHelper.Instructions.StoreIndU64: Instruction {
let value: UInt64 = context.state.readRegister(src)
try context.state.writeMemory(
address: context.state.readRegister(dest) &+ offset,
values: value.encode(method: .fixedWidth(8))
values: value.encode()
)
return .continued
}
Expand Down
Loading