-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathFortify.swift
More file actions
144 lines (122 loc) · 4.73 KB
/
Fortify.swift
File metadata and controls
144 lines (122 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Fortify.swift
// Fortify
//
// Created by John Holdsworth on 19/09/2017.
// Copyright © 2017 John Holdsworth. All rights reserved.
//
// Currently requires patched Swift toolchain from here:
// http://johnholdsworth.com/swift-LOCAL-2017-09-20-a-osx.tar.gz
//
import Foundation
open class ThreadLocal {
public required init() {
}
public class func getThreadLocal<T: ThreadLocal>(ofClass: T.Type,
keyVar: UnsafeMutablePointer<pthread_key_t>) -> T {
let needsKey = keyVar.pointee == 0
if needsKey {
let ret = pthread_key_create(keyVar, {
#if os(Linux)
Unmanaged<ThreadLocal>.fromOpaque($0!).release()
#else
Unmanaged<ThreadLocal>.fromOpaque($0).release()
#endif
})
if ret != 0 {
NSLog("Could not pthread_key_create: %s", strerror(ret))
}
}
if let existing = pthread_getspecific(keyVar.pointee) {
return Unmanaged<T>.fromOpaque(existing).takeUnretainedValue()
}
else {
let unmanaged = Unmanaged.passRetained(T())
let ret = pthread_setspecific(keyVar.pointee, unmanaged.toOpaque())
if ret != 0 {
NSLog("Could not pthread_setspecific: %s", strerror(ret))
}
return unmanaged.takeUnretainedValue()
}
}
}
@_silgen_name ("setjmp")
public func setjump(_: UnsafeMutablePointer<jmp_buf>!) -> Int32
@_silgen_name ("longjmp")
public func longjump(_: UnsafeMutablePointer<jmp_buf>!, _: Int32) -> Never
private let empty_buf = [UInt8](repeating: 0, count: MemoryLayout<jmp_buf>.size)
open class Fortify: ThreadLocal {
static private var pthreadKey: pthread_key_t = 0
open class var threadLocal: Fortify {
return getThreadLocal(ofClass: Fortify.self, keyVar: &pthreadKey)
}
private var stack = [jmp_buf]()
public var error: Error?
// Required as Swift assumes it has control of the stack
open class func disableExclusivityChecking() {
#if os(Android)
let libName = "libswiftCore.so"
#else
let libName: String? = nil
#endif
if let stdlibHandle = dlopen(libName, Int32(RTLD_LAZY | RTLD_NOLOAD)),
let disableExclusivity = dlsym(stdlibHandle, "_swift_disableExclusivityChecking") {
disableExclusivity.assumingMemoryBound(to: Bool.self).pointee = true
}
else {
NSLog("Could not disable exclusivity, failure likely...")
}
}
public static let installHandlerOnce: Void = {
// _swift_stdlib_errorHandler = {
// (prefix: StaticString, msg: String, file: StaticString,
// line: UInt, flags: UInt32, config: Int32) in
// escape(msg: msg, file: file, line: line)
// }
disableExclusivityChecking()
}()
open class func exec<T>( block: () throws -> T ) throws -> T {
_ = installHandlerOnce
let local = threadLocal
empty_buf.withUnsafeBytes {
let buf_ptr = $0.baseAddress!.assumingMemoryBound(to: jmp_buf.self)
local.stack.append(buf_ptr.pointee)
}
defer {
local.stack.removeLast()
}
if setjump(&local.stack[local.stack.count-1]) != 0 {
throw local.error ?? NSError(domain: "Error not available", code: -1, userInfo: nil)
}
return try block()
}
open class func escape(msg: String, file: StaticString = #file, line: UInt = #line) -> Never {
escape(withError: NSError(domain: msg, code: -1, userInfo: [
NSLocalizedDescriptionKey: "\(msg): \(file):\(line)",
"msg": msg, "file": file, "line": line
]))
}
open class func escape(withError error: Error) -> Never {
let local = threadLocal
local.error = error
if local.stack.count == 0 {
NSLog("escape without matching exec call: \(error)")
#if !os(Linux)
// pthread_exit(nil) just crashes
var oldState: Int32 = 0
pthread_setcancelstate(Int32(PTHREAD_CANCEL_ENABLE), &oldState)
pthread_setcanceltype(Int32(PTHREAD_CANCEL_DEFERRED), &oldState)
// pthread_cancel() never seems to be implemented
let cancelled = pthread_cancel(pthread_self())
if cancelled != 0 {
NSLog("pthread_cancel() failed: %s", strerror(cancelled))
}
sleep(1)
#endif
NSLog("cancel/exit not available/implemented or crashes, parking thread")
Thread.sleep(until: Date.distantFuture)
}
longjump(&local.stack[local.stack.count-1], 1)
NSLog("longjmp() failed, should not get here")
}
}