-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from milseman/mimus_polyglottos
Syscall mock testing
- Loading branch information
Showing
10 changed files
with
606 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
This source file is part of the Swift System open source project | ||
|
||
Copyright (c) 2020 Apple Inc. and the Swift System project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
*/ | ||
|
||
// Syscall mocking support. | ||
// | ||
// NOTE: This is currently the bare minimum needed for System's testing purposes, though we do | ||
// eventually want to expose some solution to users. | ||
// | ||
// Mocking is contextual, accessible through MockingDriver.withMockingEnabled. Mocking | ||
// state, including whether it is enabled, is stored in thread-local storage. Mocking is only | ||
// enabled in testing builds of System currently, to minimize runtime overhead of release builds. | ||
// | ||
|
||
#if ENABLE_MOCKING | ||
public struct Trace { | ||
public struct Entry: Hashable { | ||
var name: String | ||
var arguments: [AnyHashable] | ||
|
||
public init(name: String, _ arguments: [AnyHashable]) { | ||
self.name = name | ||
self.arguments = arguments | ||
} | ||
} | ||
|
||
private var entries: [Entry] = [] | ||
private var firstEntry: Int = 0 | ||
|
||
public var isEmpty: Bool { firstEntry >= entries.count } | ||
|
||
public mutating func dequeue() -> Entry? { | ||
guard !self.isEmpty else { return nil } | ||
defer { firstEntry += 1 } | ||
return entries[firstEntry] | ||
} | ||
|
||
internal mutating func add(_ e: Entry) { | ||
entries.append(e) | ||
} | ||
|
||
public mutating func clear() { entries.removeAll() } | ||
} | ||
|
||
// TODO: Track | ||
public struct WriteBuffer { | ||
public var enabled: Bool = false | ||
|
||
private var buffer: [UInt8] = [] | ||
private var chunkSize: Int? = nil | ||
|
||
internal mutating func write(_ buf: UnsafeRawBufferPointer) -> Int { | ||
guard enabled else { return 0 } | ||
let chunk = chunkSize ?? buf.count | ||
buffer.append(contentsOf: buf.prefix(chunk)) | ||
return chunk | ||
} | ||
|
||
public var contents: [UInt8] { buffer } | ||
} | ||
|
||
public enum ForceErrno: Equatable { | ||
case none | ||
case always(errno: CInt) | ||
|
||
case counted(errno: CInt, count: Int) | ||
} | ||
|
||
// Provide access to the driver, context, and trace stack of mocking | ||
public class MockingDriver { | ||
// Record syscalls and their arguments | ||
public var trace = Trace() | ||
|
||
// Mock errors inside syscalls | ||
public var forceErrno = ForceErrno.none | ||
|
||
// A buffer to put `write` bytes into | ||
public var writeBuffer = WriteBuffer() | ||
} | ||
|
||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) | ||
import Darwin | ||
#elseif os(Linux) || os(FreeBSD) || os(Android) | ||
import Glibc | ||
#else | ||
#error("Unsupported Platform") | ||
#endif | ||
|
||
internal let key: pthread_key_t = { | ||
var raw = pthread_key_t() | ||
guard 0 == pthread_key_create(&raw, nil) else { | ||
fatalError("Unable to create key") | ||
} | ||
return raw | ||
}() | ||
|
||
internal var currentMockingDriver: MockingDriver? { | ||
#if !ENABLE_MOCKING | ||
fatalError("Contextual mocking in non-mocking build") | ||
#endif | ||
|
||
guard let rawPtr = pthread_getspecific(key) else { return nil } | ||
|
||
return Unmanaged<MockingDriver>.fromOpaque(rawPtr).takeUnretainedValue() | ||
} | ||
|
||
extension MockingDriver { | ||
/// Enables mocking for the duration of `f` with a clean trace queue | ||
/// Restores prior mocking status and trace queue after execution | ||
public static func withMockingEnabled( | ||
_ f: (MockingDriver) throws -> () | ||
) rethrows { | ||
let priorMocking = currentMockingDriver | ||
let driver = MockingDriver() | ||
|
||
defer { | ||
if let object = priorMocking { | ||
pthread_setspecific(key, Unmanaged.passUnretained(object).toOpaque()) | ||
} else { | ||
pthread_setspecific(key, nil) | ||
} | ||
_fixLifetime(driver) | ||
} | ||
|
||
guard 0 == pthread_setspecific(key, Unmanaged.passUnretained(driver).toOpaque()) else { | ||
fatalError("Unable to set TLSData") | ||
} | ||
|
||
return try f(driver) | ||
} | ||
} | ||
|
||
// Check TLS for mocking | ||
@inline(never) | ||
private var contextualMockingEnabled: Bool { | ||
return currentMockingDriver != nil | ||
} | ||
|
||
extension MockingDriver { | ||
public static var enabled: Bool { mockingEnabled } | ||
} | ||
|
||
#endif // ENABLE_MOCKING | ||
|
||
@inline(__always) | ||
internal var mockingEnabled: Bool { | ||
// Fast constant-foldable check for release builds | ||
#if ENABLE_MOCKING | ||
return contextualMockingEnabled | ||
#else | ||
return false | ||
#endif | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
This source file is part of the Swift System open source project | ||
|
||
Copyright (c) 2020 Apple Inc. and the Swift System project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
*/ | ||
|
||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) | ||
import Darwin | ||
#elseif os(Linux) || os(FreeBSD) || os(Android) | ||
import Glibc | ||
#else | ||
#error("Unsupported Platform") | ||
#endif | ||
|
||
#if ENABLE_MOCKING | ||
// Strip the mock_system prefix and the arg list suffix | ||
private func originalSyscallName(_ s: String) -> String { | ||
precondition(s.starts(with: "system_")) | ||
return String(s.dropFirst("system_".count).prefix { $0.isLetter }) | ||
} | ||
|
||
private func mockImpl( | ||
name: String, | ||
_ args: [AnyHashable] | ||
) -> CInt { | ||
let origName = originalSyscallName(name) | ||
guard let driver = currentMockingDriver else { | ||
fatalError("Mocking requested from non-mocking context") | ||
} | ||
driver.trace.add(Trace.Entry(name: origName, args)) | ||
|
||
switch driver.forceErrno { | ||
case .none: break | ||
case .always(let e): | ||
errno = e | ||
return -1 | ||
case .counted(let e, let count): | ||
assert(count >= 1) | ||
errno = e | ||
driver.forceErrno = count > 1 ? .counted(errno: e, count: count-1) : .none | ||
return -1 | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
private func mock( | ||
name: String = #function, _ args: AnyHashable... | ||
) -> CInt { | ||
precondition(mockingEnabled) | ||
return mockImpl(name: name, args) | ||
} | ||
private func mockInt( | ||
name: String = #function, _ args: AnyHashable... | ||
) -> Int { | ||
Int(mockImpl(name: name, args)) | ||
} | ||
|
||
private func mockOffT( | ||
name: String = #function, _ args: AnyHashable... | ||
) -> off_t { | ||
off_t(mockImpl(name: name, args)) | ||
} | ||
#endif // ENABLE_MOCKING | ||
|
||
// Interacting with the mocking system, tracing, etc., is a potentially significant | ||
// amount of code size, so we hand outline that code for every syscall | ||
|
||
// open | ||
public func system_open(_ path: UnsafePointer<CChar>, _ oflag: Int32) -> CInt { | ||
#if ENABLE_MOCKING | ||
if mockingEnabled { return mock(String(cString: path), oflag) } | ||
#endif | ||
return open(path, oflag) | ||
} | ||
|
||
public func system_open( | ||
_ path: UnsafePointer<CChar>, _ oflag: Int32, _ mode: mode_t | ||
) -> CInt { | ||
#if ENABLE_MOCKING | ||
if mockingEnabled { return mock(String(cString: path), oflag, mode) } | ||
#endif | ||
return open(path, oflag, mode) | ||
} | ||
|
||
// close | ||
public func system_close(_ fd: Int32) -> Int32 { | ||
#if ENABLE_MOCKING | ||
if mockingEnabled { return mock(fd) } | ||
#endif | ||
return close(fd) | ||
} | ||
|
||
// read | ||
public func system_read( | ||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int | ||
) -> Int { | ||
#if ENABLE_MOCKING | ||
if mockingEnabled { return mockInt(fd, buf, nbyte) } | ||
#endif | ||
return read(fd, buf, nbyte) | ||
} | ||
|
||
// pread | ||
public func system_pread( | ||
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t | ||
) -> Int { | ||
#if ENABLE_MOCKING | ||
if mockingEnabled { return mockInt(fd, buf, nbyte, offset) } | ||
#endif | ||
return pread(fd, buf, nbyte, offset) | ||
} | ||
|
||
// lseek | ||
public func system_lseek( | ||
_ fd: Int32, _ off: off_t, _ whence: Int32 | ||
) -> off_t { | ||
#if ENABLE_MOCKING | ||
if mockingEnabled { return mockOffT(fd, off, whence) } | ||
#endif | ||
return lseek(fd, off, whence) | ||
} | ||
|
||
// write | ||
public func system_write( | ||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int | ||
) -> Int { | ||
#if ENABLE_MOCKING | ||
if mockingEnabled { return mockInt(fd, buf, nbyte) } | ||
#endif | ||
return write(fd, buf, nbyte) | ||
} | ||
|
||
// pwrite | ||
public func system_pwrite( | ||
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t | ||
) -> Int { | ||
#if ENABLE_MOCKING | ||
if mockingEnabled { return mockInt(fd, buf, nbyte, offset) } | ||
#endif | ||
return pwrite(fd, buf, nbyte, offset) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.