Skip to content

Commit 89168bd

Browse files
Pass environment variables to the engine in startup tests (#535)
* Make REPRL env property a list of tuples of (key, value) * Pass env vars when running startup tests
1 parent 11402cc commit 89168bd

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
@@ -50,7 +50,7 @@ public class REPRL: ComponentBase, ScriptRunner {
5050
super.init(name: "REPRL")
5151

5252
for (key, value) in processEnvironment {
53-
env.append(key + "=" + value)
53+
env.append((key, value))
5454
}
5555
}
5656

@@ -61,7 +61,7 @@ public class REPRL: ComponentBase, ScriptRunner {
6161
}
6262

6363
let argv = convertToCArray(processArguments)
64-
let envp = convertToCArray(env)
64+
let envp = convertToCArray(env.map({ $0 + "=" + $1 }))
6565

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

7878
public func setEnvironmentVariable(_ key: String, to value: String) {
79-
env.append(key + "=" + value)
79+
env.append((key, value))
8080
}
8181

8282
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
@@ -1013,7 +1013,9 @@ public class Fuzzer {
10131013

10141014
// Wrap the executor in a JavaScriptTestRunner
10151015
// If we can execute it standalone, it could inform us if any flags that were passed are incorrect, stale or conflicting.
1016-
let executor = JavaScriptExecutor(withExecutablePath: runner.processArguments[0], arguments: Array(runner.processArguments[1...]))
1016+
let executor = JavaScriptExecutor(
1017+
withExecutablePath: runner.processArguments[0],
1018+
arguments: Array(runner.processArguments[1...]), env: runner.env)
10171019
do {
10181020
let output = try executor.executeScript("", withTimeout: 300).output
10191021
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)