Skip to content

Sanitization facility #81

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

Closed
wants to merge 12 commits into from
Closed
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
79 changes: 78 additions & 1 deletion Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public struct Driver {
/// The set of sanitizers that were requested
public let enabledSanitizers: Set<Sanitizer>

/// The sanitizer coverage options that were requested
public let sanitizerCoverageOptions: Sanitizer.CoverageOptions?

/// The debug info format to use.
public let debugInfoFormat: DebugInfoFormat

Expand Down Expand Up @@ -282,6 +285,7 @@ public struct Driver {
self.importedObjCHeader = try Self.computeImportedObjCHeader(&parsedOptions, compilerMode: compilerMode, diagnosticEngine: diagnosticEngine)

self.enabledSanitizers = try Self.parseSanitizerArgValues(&parsedOptions, diagnosticEngine: diagnosticEngine, toolchain: toolchain, targetTriple: targetTriple)
self.sanitizerCoverageOptions = Self.parseSanitizerCoverageArgs(&parsedOptions, diagnosticEngine: diagnosticEngine, sanitizers: enabledSanitizers)

// Supplemental outputs.
self.dependenciesFilePath = try Self.computeSupplementaryOutputPath(&parsedOptions, type: .dependencies, isOutput: .emitDependencies, outputPath: .emitDependenciesPath, compilerOutputType: compilerOutputType, moduleName: moduleName)
Expand Down Expand Up @@ -581,7 +585,11 @@ extension Driver {
})
}

parsedOption = .init(option: parsedOption.option, argument: translatedArgument)
parsedOption = .init(
option: parsedOption.option,
argument: translatedArgument,
index: parsedOption.index
)
}
}

Expand Down Expand Up @@ -889,6 +897,75 @@ extension Driver {
return set
}

static func parseSanitizerCoverageArgs(
_ parsedOptions: inout ParsedOptions,
diagnosticEngine: DiagnosticsEngine,
sanitizers: Set<Sanitizer>
) -> Sanitizer.CoverageOptions? {
var typeOpt: Sanitizer.CoverageType?
var flags = Sanitizer.CoverageOptions.Flags()

let args = parsedOptions
.arguments(for: .sanitizeCoverageEQ)
.flatMap { $0.argument.asMultiple }

for arg in args {
switch arg {
case "func" where typeOpt == nil:
typeOpt = .function
case "bb" where typeOpt == nil:
typeOpt = .basicBlock
case "edge" where typeOpt == nil:
typeOpt = .edge
case "indirect-calls":
flags.insert(.indirectCalls)
case "trace-bb":
flags.insert(.traceBasicBlock)
case "trace-cmp":
flags.insert(.traceComparisons)
case "8bit-counters":
flags.insert(.use8BitCounters)
case "trace-pc":
flags.insert(.traceProgramCounter)
case "trace-pc-guard":
flags.insert(.traceProgramCounterGuard)
default:
diagnosticEngine.emit(
.error_unsupported_option_argument(
option: .sanitizeCoverageEQ,
arg: arg
)
)
return nil
}
}

guard let type = typeOpt else {
diagnosticEngine.emit(
.error_option_missing_required_argument(
option: .sanitizeCoverageEQ,
requiredArg: #""func", "bb", "edge""#
)
)
return nil
}

// Running the sanitizer coverage pass will add undefined symbols to
// functions in compiler-rt's "sanitizer_common". "sanitizer_common" isn't
// shipped as a separate library we can link with. However those are defined
// in the various sanitizer runtime libraries so we require that we are
// doing a sanitized build so we pick up the required functions during
// linking.
if sanitizers.isEmpty {
diagnosticEngine.emit(
.error_option_requires_sanitizer(option: .sanitizeCoverageEQ)
)
return nil
}

return Sanitizer.CoverageOptions(coverageType: type, flags: flags)
}

}

// Module computation.
Expand Down
24 changes: 9 additions & 15 deletions Sources/SwiftDriver/Jobs/CompileJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ extension Array where Element == Job.ArgTemplate {
/// Append the last parsed option that matches one of the given options
/// to this command line.
mutating func appendLast(_ options: Option..., from parsedOptions: inout ParsedOptions) throws {
guard let parsedOption = parsedOptions.last(where: { options.contains($0.option) }) else {
guard let parsedOption = parsedOptions.last(for: options) else {
return
}

Expand All @@ -276,17 +276,15 @@ extension Array where Element == Job.ArgTemplate {
/// Append all parsed options that match one of the given options
/// to this command line.
mutating func appendAll(_ options: Option..., from parsedOptions: inout ParsedOptions) throws {
let matchingOptions = parsedOptions.filter { options.contains($0.option) }
for matching in matchingOptions {
for matching in parsedOptions.arguments(for: options) {
try append(matching)
}
}

/// Append just the arguments from all parsed options that match one of the given options
/// to this command line.
mutating func appendAllArguments(_ options: Option..., from parsedOptions: inout ParsedOptions) throws {
let matchingOptions = parsedOptions.filter { options.contains($0.option) }
for matching in matchingOptions {
for matching in parsedOptions.arguments(for: options) {
try self.appendSingleArgument(option: matching.option, argument: matching.argument.asSingle)
}
}
Expand All @@ -295,16 +293,12 @@ extension Array where Element == Job.ArgTemplate {
/// or the flag that corresponds to the default value if neither
/// appears.
mutating func appendFlag(true trueFlag: Option, false falseFlag: Option, default defaultValue: Bool, from parsedOptions: inout ParsedOptions) {
guard let parsedOption = parsedOptions.last(where: { $0.option == trueFlag || $0.option == falseFlag }) else {
if defaultValue {
appendFlag(trueFlag)
} else {
appendFlag(falseFlag)
}
return
}

appendFlag(parsedOption.option)
let isTrue = parsedOptions.hasFlag(
positive: trueFlag,
negative: falseFlag,
default: defaultValue
)
appendFlag(isTrue ? trueFlag : falseFlag)
}
}

Expand Down
6 changes: 2 additions & 4 deletions Sources/SwiftDriver/Jobs/DarwinToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ extension DarwinToolchain {
fallthrough
case .executable:
linkerTool = .dynamicLinker
let fSystemArgs = parsedOptions.filter {
$0.option == .F || $0.option == .Fsystem
}
let fSystemArgs = parsedOptions.arguments(for: .F, .Fsystem)
for opt in fSystemArgs {
commandLine.appendFlag(.F)
commandLine.appendPath(try VirtualPath(path: opt.argument.asSingle))
Expand Down Expand Up @@ -245,7 +243,7 @@ extension DarwinToolchain {
// These custom arguments should be right before the object file at the
// end.
try commandLine.append(
contentsOf: parsedOptions.filter { $0.option.group == .linkerOption }
contentsOf: parsedOptions.arguments(in: .linkerOption)
)
try commandLine.appendAllArguments(.Xlinker, from: &parsedOptions)

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extension Driver {
try commandLine.appendLast(.AssertConfig, from: &parsedOptions)
try commandLine.appendLast(.autolinkForceLoad, from: &parsedOptions)

if let colorOption = parsedOptions.last(where: { $0.option == .colorDiagnostics || $0.option == .noColorDiagnostics }) {
if let colorOption = parsedOptions.last(for: .colorDiagnostics, .noColorDiagnostics) {
commandLine.appendFlag(colorOption.option)
} else if shouldColorDiagnostics() {
commandLine.appendFlag(.colorDiagnostics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ extension GenericUnixToolchain {
}
commandLine.append(contentsOf: inputFiles)

let fSystemArgs = parsedOptions.filter {
$0.option == .F || $0.option == .Fsystem
}
let fSystemArgs = parsedOptions.arguments(for: .F, .Fsystem)
for opt in fSystemArgs {
if opt.option == .Fsystem {
commandLine.appendFlag("-iframework")
Expand Down Expand Up @@ -250,7 +248,7 @@ extension GenericUnixToolchain {
// These custom arguments should be right before the object file at the
// end.
try commandLine.append(
contentsOf: parsedOptions.filter { $0.option.group == .linkerOption }
contentsOf: parsedOptions.arguments(in: .linkerOption)
)
try commandLine.appendAllArguments(.Xlinker, from: &parsedOptions)
try commandLine.appendAllArguments(.XclangLinker, from: &parsedOptions)
Expand Down
36 changes: 11 additions & 25 deletions Sources/SwiftDriver/Options/OptionParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,15 @@ public enum OptionParseError : Error {
}

extension OptionTable {
private func matchOption(_ argument: String) -> Option? {
var bestOption: Option? = nil

// FIXME: Use a binary search or trie or similar.
for option in options {
// If there isn't a prefix match, keep going.
if !argument.starts(with: option.spelling) { continue }

// If this is the first option we've seen, or if it's a longer
// match than the best option so far, then we have a new best
// option.
if let bestOption = bestOption,
bestOption.spelling.count >= option.spelling.count {
continue
}

bestOption = option
}

return bestOption
}

/// Parse the given command-line arguments into a set of options.
///
/// Throws an error if the command line contains any errors.
public func parse(_ arguments: [String]) throws -> ParsedOptions {
var trie = PrefixTrie<String.UTF8View, Option>()
for opt in options {
trie[opt.spelling.utf8] = opt
}

var parsedOptions = ParsedOptions()
var index = arguments.startIndex
while index < arguments.endIndex {
Expand All @@ -59,8 +42,11 @@ extension OptionTable {
continue
}

// Match to an option, identified by key.
guard let option = matchOption(argument) else {
// Match to an option, identified by key. Note that this is a prefix
// match -- if the option is a `.flag`, we'll explicitly check to see if
// there's an unmatched suffix at the end, and pop an error. Otherwise,
// we'll treat the unmatched suffix as the argument to the option.
guard let option = trie[argument.utf8] else {
throw OptionParseError.unknownOption(
index: index - 1, argument: argument)
}
Expand Down Expand Up @@ -123,7 +109,7 @@ extension OptionTable {
index += 1
}
}

parsedOptions.buildIndex()
return parsedOptions
}
}
8 changes: 8 additions & 0 deletions Sources/SwiftDriver/Options/OptionTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public struct OptionTable {

/// Retrieve the options.
public var options: [Option] = Option.allOptions + Option.extraOptions
public lazy var groupMap: [Option.Group: [Option]] = {
var map = [Option.Group: [Option]]()
for opt in options {
guard let group = opt.group else { continue }
map[group, default: []].append(opt)
}
return map
}()
}

extension String {
Expand Down
Loading