Skip to content

[Swift] Add crash reproducer support #686

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

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
10 changes: 10 additions & 0 deletions Sources/SWBCore/LibSwiftDriver/PlannedBuild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,16 @@ extension LibSwiftDriver {
}
}

public func getCrashReproducerCommand(for job: PlannedSwiftDriverJob, output dir: Path) async throws -> [String]? {
try await dispatchQueue.sync {
let driverJob = try self.driverJob(for: job)
guard let reproJob = self.jobExecutionDelegate?.getReproducerJob(job: driverJob, output: try VirtualPath(path: dir.str)) else {
return nil
}
return try self.argsResolver.resolveArgumentList(for: reproJob, useResponseFiles: .heuristic)
}
}

public func getDiscoveredJobsAfterFinishing(job: PlannedSwiftDriverJob) throws -> [PlannedSwiftDriverJob] {

dispatchQueue.blocking_sync {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ public final class SwiftDriverJobTaskAction: TaskAction, BuildValueValidatingTas
private var pid = llbuild_pid_t.invalid

var executionError: String?
var wasSignaled: Bool = false
private var processStarted = false
private var _commandResult: CommandResult?
var commandResult: CommandResult? {
Expand Down Expand Up @@ -469,10 +470,15 @@ public final class SwiftDriverJobTaskAction: TaskAction, BuildValueValidatingTas
}

func processFinished(result: CommandExtendedResult) {
if wasSignaled {
// If the process was already signaled, this might be in a reproducer creation. No need to update finish status.
return
}
guard let status = Processes.ExitStatus.init(rawValue: result.exitStatus) else {
// nil means the job is stopped or continued. It should not call finished.
return
}
wasSignaled = status.wasSignaled
// This may be updated by commandStarted in the case of certain failures,
// so only update the exit status in output delegate if it is nil.
if outputDelegate.result == nil {
Expand Down Expand Up @@ -526,6 +532,21 @@ public final class SwiftDriverJobTaskAction: TaskAction, BuildValueValidatingTas

try await spawn(commandLine: options.commandLine, environment: environment, workingDirectory: task.workingDirectory, dynamicExecutionDelegate: dynamicExecutionDelegate, clientDelegate: clientDelegate, processDelegate: delegate)

// Generate crash reproducoer.
if delegate.wasSignaled {
// The output directory for crash reproducer is:
// * Specified by environment
// * Primary output path directory
// * Temp directory
let reproDir = environment["SWIFT_CRASH_DIAGNOSTICS_DIR"].map(Path.init) ?? driverJob.driverJob.outputs.first?.dirname
try await withTemporaryDirectory(dir: reproDir, prefix: "swift-crash-reproducer", removeTreeOnDeinit: false) { dir in
if let reproCommand = try await plannedBuild?.getCrashReproducerCommand(for: driverJob, output: dir) {
try await spawn(commandLine: reproCommand, environment: environment, workingDirectory: task.workingDirectory, dynamicExecutionDelegate: dynamicExecutionDelegate, clientDelegate: clientDelegate, processDelegate: delegate)
outputDelegate.note("Crash reproducer created in \(dir.str)")
}
}
}

if let error = delegate.executionError {
outputDelegate.error(error)
return .failed
Expand Down