Skip to content

Commit

Permalink
[VS] Added extracting symbol names demangling and diff before / after…
Browse files Browse the repository at this point in the history
… obfuscation

Reviewers: krzysztof.siejkowski

Reviewed By: krzysztof.siejkowski

Differential Revision: https://phabricator.polidea.com/D2716
  • Loading branch information
jerzyKleszcz committed Dec 21, 2017
1 parent 2176aa6 commit bed6c1e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "VerificationSuite",
dependencies: [
.package(url: "https://github.com/JohnSundell/ShellOut.git", from: "2.0.0")
.package(url: "https://github.com/JohnSundell/ShellOut.git", from: "2.0.0"),
],
targets: [
.target(
Expand Down
35 changes: 35 additions & 0 deletions Sources/VerificationSuite/VerificationSuite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ func xcodebuild(project: String, scheme: String, outputBuildPath: String) -> Str
return try! shellOut(to: "xcodebuild", arguments: arguments)
}

func extractSymbolNames(executable: String) -> [String] {
// TODO: for now, we expect the system to provide us with the nm tool available and globally linked
// - consider adding nm tool path as script parameter
let nmOutput = try! shellOut(to: "nm", arguments: [executable, "-format=posix"])
let lines = nmOutput.components(separatedBy: "\n").filter { !$0.isEmpty }
let symbolNames = lines.map { $0.components(separatedBy: " ").first! }
return symbolNames
}

func demangle(symbols: [String]) -> String {
// TODO: for now, we expect the system to provide us with the xcrun tool available and globally linked
// - consider adding xcrun tool path as script parameter
let demangleExec = try! shellOut(to: "xcrun", arguments: ["--find", "swift-demangle"])
return try! shellOut(to: demangleExec, arguments: ["-compact"] + symbols)
}

func printToFile(string: String, filename: String) {
try! shellOut(to: "echo", arguments: ["'\(string)'", ">", filename])
}

func diffFiles(before: String, after: String) -> String {
do {
let diff = try shellOut(to: "diff", arguments: [before, after])
return diff
} catch let error {
print("Error while diffing files: \(error.localizedDescription)")
return ""
}
}

func removeDirectory(_ path: String) {
try! shellOut(to: "rm", arguments: ["-rf", path])
}

func removeFile(_ path: String) {
try! shellOut(to: "rm", arguments: [path])
}

23 changes: 18 additions & 5 deletions Sources/VerificationSuite/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,27 @@ let buildPathAfterObfuscation = "ObfuscatedBuild"
let unobfuscatedExecutablePath = executableFromProjectBuild(xcodeproj: xcodeprojBeforeObfuscation, scheme: schemeBeforeObfuscation, outputPath: buildPathBeforeObfuscation)
let obfuscatedExecutablePath = executableFromProjectBuild(xcodeproj: xcodeprojAfterObfuscation, scheme: schemeAfterObfuscation, outputPath: buildPathAfterObfuscation)

// extract symbol names from executables and demangle them

// TODO:
// - extract swift symbols from executables
// - demangle them
// - compare symbols before / after obfuscation
let symbolsBefore = demangle(symbols: extractSymbolNames(executable: unobfuscatedExecutablePath))
let symbolsBeforeFile = "before.txt"
printToFile(string: symbolsBefore, filename: symbolsBeforeFile)

let symbolsAfter = demangle(symbols: extractSymbolNames(executable: obfuscatedExecutablePath))
let symbolsAfterFile = "after.txt"
printToFile(string: symbolsAfter, filename: symbolsAfterFile)

// remove build directories
// compare symbol names before / after obfuscation

print("\n\nDIFF BEFORE / AFTER OBFUSCATION:")
let diff = diffFiles(before: symbolsBeforeFile, after: symbolsAfterFile)
print(diff)

// remove build directories and symbol files

removeDirectory(buildPathBeforeObfuscation)
removeDirectory(buildPathAfterObfuscation)

removeFile(symbolsBeforeFile)
removeFile(symbolsAfterFile)

0 comments on commit bed6c1e

Please sign in to comment.