Skip to content

Commit f66e017

Browse files
committed
JavaKit: check each classPath element exists before starting VM
Partial fix for #136: validate each classPath element by attempting to open it before starting VM. It would be more efficient to stat() it but SystemPackage lacks an abstraction for checking if a file exists, and we don't want to import Foundation.
1 parent 89b0a06 commit f66e017

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Package.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ let package = Package(
140140
.package(url: "https://github.com/swiftlang/swift-syntax.git", branch: "main"),
141141
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
142142
.package(url: "https://github.com/apple/swift-collections.git", .upToNextMinor(from: "1.1.0")),
143+
.package(url: "https://github.com/apple/swift-system.git", .upToNextMajor(from: "1.4.0")),
143144
.package(url: "https://github.com/ordo-one/package-benchmark", .upToNextMajor(from: "1.4.0")),
144145
],
145146
targets: [
@@ -161,7 +162,12 @@ let package = Package(
161162
),
162163
.target(
163164
name: "JavaKit",
164-
dependencies: ["JavaRuntime", "JavaKitMacros", "JavaTypes"],
165+
dependencies: [
166+
"JavaRuntime",
167+
"JavaKitMacros",
168+
"JavaTypes",
169+
.product(name: "SystemPackage", package: "swift-system")
170+
],
165171
exclude: ["Java2Swift.config"],
166172
swiftSettings: [
167173
.swiftLanguageMode(.v5),

Sources/JavaKit/JavaKitVM/JavaVirtualMachine.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import SystemPackage
16+
1517
typealias JavaVMPointer = UnsafeMutablePointer<JavaVM?>
1618

1719
public final class JavaVirtualMachine: @unchecked Sendable {
@@ -54,6 +56,14 @@ public final class JavaVirtualMachine: @unchecked Sendable {
5456
// Construct the complete list of VM options.
5557
var allVMOptions: [String] = []
5658
if !classPath.isEmpty {
59+
for path in classPath {
60+
do {
61+
let fd = try FileDescriptor.open(path, .readOnly)
62+
try? fd.close()
63+
} catch {
64+
throw(ClassPathError(path: path, error: error))
65+
}
66+
}
5767
let colonSeparatedClassPath = classPath.joined(separator: ":")
5868
allVMOptions.append("-Djava.class.path=\(colonSeparatedClassPath)")
5969
}
@@ -268,4 +278,9 @@ extension JavaVirtualMachine {
268278
}
269279
}
270280
}
281+
282+
struct ClassPathError: Error {
283+
let path: String
284+
let error: Error
285+
}
271286
}

0 commit comments

Comments
 (0)