Skip to content

Add support for the Scudo sanitizer. #2438

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
merged 1 commit into from
Apr 29, 2020
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
18 changes: 18 additions & 0 deletions Fixtures/Miscellaneous/DoubleFree/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// swift-tools-version:5.1

import PackageDescription

let package = Package(
name: "double-free",
targets: [
.target(
name: "lib",
dependencies: []),
.target(
name: "exec",
dependencies: ["lib"]),
.testTarget(
name: "libTests",
dependencies: ["lib"]),
]
)
3 changes: 3 additions & 0 deletions Fixtures/Miscellaneous/DoubleFree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# double-free

A description of this package.
3 changes: 3 additions & 0 deletions Fixtures/Miscellaneous/DoubleFree/Sources/exec/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import lib

executeDoubleFree()
9 changes: 9 additions & 0 deletions Fixtures/Miscellaneous/DoubleFree/Sources/lib/lib.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public func executeDoubleFree() {
let size = 512

let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: size, alignment: 1)
buffer[0] = 0
buffer.deallocate()
buffer.deallocate()
print(buffer[0])
}
8 changes: 8 additions & 0 deletions Fixtures/Miscellaneous/DoubleFree/Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import XCTest

import libTests

var tests = [XCTestCaseEntry]()
tests += libTests.__allTests()

XCTMain(tests)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import XCTest

extension libTests {
static let __allTests = [
("testDoubleFree", testDoubleFree),
]
}

#if !os(macOS)
public func __allTests() -> [XCTestCaseEntry] {
return [
testCase(libTests.__allTests),
]
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import XCTest
import lib

final class libTests: XCTestCase {
func testDoubleFree() {
executeDoubleFree()
}
}
5 changes: 4 additions & 1 deletion Sources/SPMBuildCore/Sanitizers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public enum Sanitizer: String, Encodable {
case address
case thread
case undefined
case scudo

/// Return an established short name for a sanitizer, e.g. "asan".
public var shortName: String {
switch self {
case .address: return "asan"
case .thread: return "tsan"
case .undefined: return "ubsan"
case .scudo: return "scudo"
}
}
}
Expand Down Expand Up @@ -72,6 +74,7 @@ extension Sanitizer: StringEnumArgument {
public static let completion: ShellCompletion = .values([
(address.rawValue, "enable Address sanitizer"),
(thread.rawValue, "enable Thread sanitizer"),
(undefined.rawValue, "enable Undefined Behavior sanitizer")
(undefined.rawValue, "enable Undefined Behavior sanitizer"),
(scudo.rawValue, "enable Scudo hardened allocator")
])
}
23 changes: 23 additions & 0 deletions Tests/CommandsTests/RunToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,27 @@ final class RunToolTests: XCTestCase {
}
#endif
}

func testSanitizeScudo() throws {
// Scudo is only supported on Linux.
#if os(Linux)
fixture(name: "Miscellaneous/DoubleFree") { path in
// Ensure that we don't abort() when we find the race. This avoids
// generating the crash report on macOS.
let cmdline = {
try SwiftPMProduct.SwiftRun.execute(
["--sanitize=scudo"], packagePath: path)
}
XCTAssertThrows(try cmdline()) { (error: SwiftPMProductError) in
switch error {
case .executionFailure(_, _, let error):
XCTAssertMatch(error, .contains("invalid chunk state"))
return true
default:
return false
}
}
}
#endif
}
}
21 changes: 21 additions & 0 deletions Tests/CommandsTests/TestToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,25 @@ final class TestToolTests: XCTestCase {
}
#endif
}

func testSanitizeScudo() throws {
// This test only runs on Linux because Scudo only runs on Linux
#if os(Linux)
fixture(name: "Miscellaneous/DoubleFree") { path in
let cmdline = {
try SwiftPMProduct.SwiftTest.execute(
["--sanitize=scudo"], packagePath: path)
}
XCTAssertThrows(try cmdline()) { (error: SwiftPMProductError) in
switch error {
case .executionFailure(_, _, let error):
XCTAssertMatch(error, .contains("invalid chunk state"))
return true
default:
return false
}
}
}
#endif
}
}