forked from googleprojectzero/fuzzilli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadSync.swift
152 lines (123 loc) · 5.64 KB
/
ThreadSync.swift
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
145
146
147
148
149
150
151
152
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
//
// Implementation of the distributed fuzzing protocol for synchronizing instances within the same process.
//
public class ThreadParent: DistributedFuzzingParentNode {
fileprivate let transport: Transport
public init(for fuzzer: Fuzzer) {
self.transport = Transport(for: fuzzer)
super.init(for: fuzzer, name: "ThreadParent", corpusSynchronizationMode: .full, transport: transport)
}
fileprivate class Transport: DistributedFuzzingParentNodeTransport {
private let fuzzer: Fuzzer
private let logger = Logger(withLabel: "ThreadParentTransport")
private var clients: [UUID: Fuzzer] = [:]
/// Used to ensure that all child nodes have shut down before this node terminates.
private let shutdownGroup = DispatchGroup()
// The other side simply directly invokes this callback.
fileprivate var onMessageCallback: OnMessageCallback? = nil
// This callback is invoked by this class.
private var onChildConnectedCallback: OnChildConnectedCallback? = nil
init(for fuzzer: Fuzzer) {
self.fuzzer = fuzzer
}
func initialize() {
fuzzer.registerEventListener(for: fuzzer.events.ShutdownComplete) { _ in
self.shutdownGroup.wait()
}
}
func registerClient(_ client: Fuzzer) {
client.async {
self.shutdownGroup.enter()
client.registerEventListener(for: client.events.ShutdownComplete) { _ in
self.shutdownGroup.leave()
// The parent is responsible for terminating the process, so just sleep here now.
while true { Thread.sleep(forTimeInterval: 60) }
}
}
clients[client.id] = client
onChildConnectedCallback?(client.id)
}
func send(_ messageType: MessageType, to child: UUID, contents: Data) {
guard let client = clients[child] else {
fatalError("Unknown child node \(child)")
}
client.async {
guard let module = ThreadChild.instance(for: client) else { fatalError("No active ThreadChild module on client instance") }
module.transport.onMessageCallback?(messageType, contents)
}
}
func send(_ messageType: MessageType, to child: UUID, contents: Data, synchronizeWith synchronizationGroup: DispatchGroup) {
send(messageType, to: child, contents: contents)
}
func disconnect(_ child: UUID) {
// This should only happen if the child encountered a fatal error or something like that, in which case we probably
// want to terminate the whole fuzzing session since the child cannot continue fuzzing.
logger.fatal("Child node \(child) unexpectedly terminated")
}
func setOnMessageCallback(_ callback: @escaping OnMessageCallback) {
assert(onMessageCallback == nil)
onMessageCallback = callback
}
func setOnChildConnectedCallback(_ callback: @escaping OnChildConnectedCallback) {
assert(onChildConnectedCallback == nil)
onChildConnectedCallback = callback
}
func setOnChildDisconnectedCallback(_ callback: @escaping OnChildDisconnectedCallback) {
// We don't use this callback.
}
}
}
public class ThreadChild: DistributedFuzzingChildNode {
fileprivate let transport: Transport
public init(for fuzzer: Fuzzer, parent: Fuzzer) {
self.transport = Transport(child: fuzzer, parent: parent)
super.init(for: fuzzer, name: "ThreadChild", corpusSynchronizationMode: .full, transport: transport)
}
fileprivate class Transport: DistributedFuzzingChildNodeTransport {
private let child: Fuzzer
private let parent: Fuzzer
private var parentModule: ThreadParent! = nil
// The other side simply directly invokes this callback.
fileprivate var onMessageCallback: OnMessageCallback? = nil
init(child: Fuzzer, parent: Fuzzer) {
self.child = child
self.parent = parent
}
func initialize() {
guard let parentModule = ThreadParent.instance(for: parent) else {
fatalError("No active ThreadParent module on parent instance")
}
self.parentModule = parentModule
parent.async {
self.parentModule.transport.registerClient(self.child)
}
}
func send(_ messageType: MessageType, contents: Data) {
let ourId = child.id
parent.async {
self.parentModule.transport.onMessageCallback?(messageType, contents, ourId)
}
}
func send(_ messageType: MessageType, contents: Data, synchronizeWith: DispatchGroup) {
send(messageType, contents: contents)
}
func setOnMessageCallback(_ callback: @escaping OnMessageCallback) {
assert(onMessageCallback == nil)
onMessageCallback = callback
}
}
}