Skip to content

Commit 8570494

Browse files
CSharperMantlechase1k
authored andcommitted
Pass environment variables to the engine in startup tests (googleprojectzero#535)
* Make REPRL env property a list of tuples of (key, value) * Pass env vars when running startup tests
1 parent dfe0643 commit 8570494

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

Sources/Fuzzilli/Execution/REPRL.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class REPRL: ComponentBase, ScriptRunner {
2424
/// Commandline arguments for the executable
2525
public private(set) var processArguments: [String]
2626

27-
/// Environment variables for the child process
28-
private var env = [String]()
27+
/// Environment variables for the child process. Shape: [(key, value)]
28+
public private(set) var env = [(String, String)]()
2929

3030
/// Number of script executions since start of child process
3131
private var execsSinceReset = 0
@@ -53,7 +53,7 @@ public class REPRL: ComponentBase, ScriptRunner {
5353
super.init(name: "REPRL")
5454

5555
for (key, value) in processEnvironment {
56-
env.append(key + "=" + value)
56+
env.append((key, value))
5757
}
5858
}
5959

@@ -64,7 +64,7 @@ public class REPRL: ComponentBase, ScriptRunner {
6464
}
6565

6666
let argv = convertToCArray(processArguments)
67-
let envp = convertToCArray(env)
67+
let envp = convertToCArray(env.map({ $0 + "=" + $1 }))
6868

6969
if reprl_initialize_context(reprlContext, argv, envp, /* capture stdout */ 1, /* capture stderr: */ 1) != 0 {
7070
logger.fatal("Failed to initialize REPRL context: \(String(cString: reprl_get_last_error(reprlContext)))")
@@ -84,7 +84,7 @@ public class REPRL: ComponentBase, ScriptRunner {
8484
}
8585

8686
public func setEnvironmentVariable(_ key: String, to value: String) {
87-
env.append(key + "=" + value)
87+
env.append((key, value))
8888
}
8989

9090
public func run(_ script: String, withTimeout timeout: UInt32) -> Execution {

Sources/Fuzzilli/Execution/ScriptRunner.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
public protocol ScriptRunner: Component {
1616
var processArguments: [String] { get }
17+
var env: [(String, String)] { get }
1718

1819
/// Executes a script, waits for it to complete, and returns the result.
1920
func run(_ script: String, withTimeout timeout: UInt32) -> Execution

Sources/Fuzzilli/Fuzzer.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,9 @@ public class Fuzzer {
10241024

10251025
// Wrap the executor in a JavaScriptTestRunner
10261026
// If we can execute it standalone, it could inform us if any flags that were passed are incorrect, stale or conflicting.
1027-
let executor = JavaScriptExecutor(withExecutablePath: runner.processArguments[0], arguments: Array(runner.processArguments[1...]))
1027+
let executor = JavaScriptExecutor(
1028+
withExecutablePath: runner.processArguments[0],
1029+
arguments: Array(runner.processArguments[1...]), env: runner.env)
10281030
do {
10291031
let output = try executor.executeScript("", withTimeout: 300).output
10301032
if output.lengthOfBytes(using: .utf8) > 0 {

Sources/Fuzzilli/Util/JavaScriptExecutor.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ public class JavaScriptExecutor {
3838

3939
let arguments: [String]
4040

41+
let env: [(String, String)]
42+
4143
/// Depending on the type this constructor will try to find the requested shell or fail
42-
public init?(type: ExecutorType = .any, withArguments maybeArguments: [String]? = nil) {
44+
public init?(type: ExecutorType = .any, withArguments maybeArguments: [String]? = nil, withEnv maybeEnv: [(String, String)]? = nil) {
4345
self.arguments = maybeArguments ?? []
46+
self.env = maybeEnv ?? []
4447
let path: String?
4548

4649
switch type {
@@ -59,23 +62,24 @@ public class JavaScriptExecutor {
5962
self.executablePath = path!
6063
}
6164

62-
public init(withExecutablePath executablePath: String, arguments: [String]) {
65+
public init(withExecutablePath executablePath: String, arguments: [String], env: [(String, String)]) {
6366
self.executablePath = executablePath
6467
self.arguments = arguments
68+
self.env = env
6569
}
6670

6771
/// Executes the JavaScript script using the configured engine and returns the stdout.
6872
public func executeScript(_ script: String, withTimeout timeout: TimeInterval? = nil) throws -> Result {
69-
return try execute(executablePath, withInput: prefix + script.data(using: .utf8)!, withArguments: self.arguments, timeout: timeout)
73+
return try execute(executablePath, withInput: prefix + script.data(using: .utf8)!, withArguments: self.arguments, withEnv: self.env, timeout: timeout)
7074
}
7175

7276
/// Executes the JavaScript script at the specified path using the configured engine and returns the stdout.
7377
public func executeScript(at url: URL, withTimeout timeout: TimeInterval? = nil) throws -> Result {
7478
let script = try Data(contentsOf: url)
75-
return try execute(executablePath, withInput: prefix + script, withArguments: self.arguments, timeout: timeout)
79+
return try execute(executablePath, withInput: prefix + script, withArguments: self.arguments, withEnv: self.env, timeout: timeout)
7680
}
7781

78-
func execute(_ path: String, withInput input: Data = Data(), withArguments arguments: [String] = [], timeout maybeTimeout: TimeInterval? = nil) throws -> Result {
82+
func execute(_ path: String, withInput input: Data = Data(), withArguments arguments: [String] = [], withEnv env: [(String, String)] = [], timeout maybeTimeout: TimeInterval? = nil) throws -> Result {
7983
let inputPipe = Pipe()
8084
let outputPipe = Pipe()
8185
let errorPipe = Pipe()
@@ -89,13 +93,16 @@ public class JavaScriptExecutor {
8993
// Close stdin
9094
try inputPipe.fileHandleForWriting.close()
9195

96+
let environment = ProcessInfo.processInfo.environment.merging(env, uniquingKeysWith: { _, new in new })
97+
9298
// Execute the subprocess.
9399
let task = Process()
94100
task.standardOutput = outputPipe
95101
task.standardError = errorPipe
96102
task.arguments = arguments + [url.path]
97103
task.executableURL = URL(fileURLWithPath: path)
98104
task.standardInput = inputPipe
105+
task.environment = environment
99106
try task.run()
100107

101108
var timedOut = false

Sources/Fuzzilli/Util/MockFuzzer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct MockExecution: Execution {
2626

2727
class MockScriptRunner: ScriptRunner {
2828
var processArguments: [String] = []
29+
var env: [(String, String)] = []
2930

3031
func run(_ script: String, withTimeout timeout: UInt32) -> Execution {
3132
return MockExecution(outcome: .succeeded,

0 commit comments

Comments
 (0)