17
17
#include < cerrno>
18
18
#include < cinttypes>
19
19
#include < cstdint>
20
- #include < fbl/unique_fd.h>
21
20
#include < fcntl.h>
22
21
#include < launchpad/launchpad.h>
23
22
#include < string>
24
23
#include < thread>
24
+ #include < unistd.h>
25
25
#include < zircon/errors.h>
26
+ #include < zircon/process.h>
26
27
#include < zircon/status.h>
27
28
#include < zircon/syscalls.h>
28
29
#include < zircon/syscalls/port.h>
29
30
#include < zircon/types.h>
30
- #include < zx/object.h>
31
- #include < zx/port.h>
32
- #include < zx/process.h>
33
- #include < zx/time.h>
34
31
35
32
namespace fuzzer {
36
33
@@ -53,13 +50,13 @@ void InterruptHandler() {
53
50
Fuzzer::StaticInterruptCallback ();
54
51
}
55
52
56
- void CrashHandler (zx::port *Port) {
57
- std::unique_ptr<zx::port > ExceptionPort (Port);
53
+ void CrashHandler (zx_handle_t *Port) {
54
+ std::unique_ptr<zx_handle_t > ExceptionPort (Port);
58
55
zx_port_packet_t Packet;
59
- ExceptionPort-> wait ( zx::time::infinite (), &Packet, 0 );
56
+ _zx_port_wait (*ExceptionPort, ZX_TIME_INFINITE, &Packet, 1 );
60
57
// Unbind as soon as possible so we don't receive exceptions from this thread.
61
- if (zx_task_bind_exception_port (ZX_HANDLE_INVALID, ZX_HANDLE_INVALID,
62
- kFuzzingCrash , 0 ) != ZX_OK) {
58
+ if (_zx_task_bind_exception_port (ZX_HANDLE_INVALID, ZX_HANDLE_INVALID,
59
+ kFuzzingCrash , 0 ) != ZX_OK) {
63
60
// Shouldn't happen; if it does the safest option is to just exit.
64
61
Printf (" libFuzzer: unable to unbind exception port; aborting!\n " );
65
62
exit (1 );
@@ -97,15 +94,15 @@ void SetSignalHandler(const FuzzingOptions &Options) {
97
94
return ;
98
95
99
96
// Create an exception port
100
- zx::port *ExceptionPort = new zx::port () ;
101
- if ((rc = zx::port::create (0 , ExceptionPort)) != ZX_OK) {
102
- Printf (" libFuzzer: zx_port_create failed: %s\n " , zx_status_get_string (rc));
97
+ zx_handle_t *ExceptionPort = new zx_handle_t ;
98
+ if ((rc = _zx_port_create (0 , ExceptionPort)) != ZX_OK) {
99
+ Printf (" libFuzzer: zx_port_create failed: %s\n " , _zx_status_get_string (rc));
103
100
exit (1 );
104
101
}
105
102
106
103
// Bind the port to receive exceptions from our process
107
- if ((rc = zx_task_bind_exception_port ( zx_process_self (), ExceptionPort-> get () ,
108
- kFuzzingCrash , 0 )) != ZX_OK) {
104
+ if ((rc = _zx_task_bind_exception_port ( _zx_process_self (), * ExceptionPort,
105
+ kFuzzingCrash , 0 )) != ZX_OK) {
109
106
Printf (" libFuzzer: unable to bind exception port: %s\n " ,
110
107
zx_status_get_string (rc));
111
108
exit (1 );
@@ -117,13 +114,13 @@ void SetSignalHandler(const FuzzingOptions &Options) {
117
114
}
118
115
119
116
void SleepSeconds (int Seconds) {
120
- zx::nanosleep ( zx::deadline_after ( zx::sec (Seconds)));
117
+ _zx_nanosleep ( _zx_deadline_after ( ZX_SEC (Seconds)));
121
118
}
122
119
123
120
unsigned long GetPid () {
124
121
zx_status_t rc;
125
122
zx_info_handle_basic_t Info;
126
- if ((rc = zx_object_get_info (zx_process_self (), ZX_INFO_HANDLE_BASIC, &Info,
123
+ if ((rc = zx_object_get_info (_zx_process_self (), ZX_INFO_HANDLE_BASIC, &Info,
127
124
sizeof (Info), NULL , NULL )) != ZX_OK) {
128
125
Printf (" libFuzzer: unable to get info about self: %s\n " ,
129
126
zx_status_get_string (rc));
@@ -135,15 +132,30 @@ unsigned long GetPid() {
135
132
size_t GetPeakRSSMb () {
136
133
zx_status_t rc;
137
134
zx_info_task_stats_t Info;
138
- if ((rc = zx_object_get_info ( zx_process_self (), ZX_INFO_TASK_STATS, &Info,
135
+ if ((rc = _zx_object_get_info ( _zx_process_self (), ZX_INFO_TASK_STATS, &Info,
139
136
sizeof (Info), NULL , NULL )) != ZX_OK) {
140
137
Printf (" libFuzzer: unable to get info about self: %s\n " ,
141
- zx_status_get_string (rc));
138
+ _zx_status_get_string (rc));
142
139
exit (1 );
143
140
}
144
141
return (Info.mem_private_bytes + Info.mem_shared_bytes ) >> 20 ;
145
142
}
146
143
144
+ template <typename Fn>
145
+ class RunOnDestruction {
146
+ public:
147
+ explicit RunOnDestruction (Fn fn) : fn_(fn) {}
148
+ ~RunOnDestruction () { fn_ (); }
149
+
150
+ private:
151
+ Fn fn_;
152
+ };
153
+
154
+ template <typename Fn>
155
+ RunOnDestruction<Fn> at_scope_exit (Fn fn) {
156
+ return RunOnDestruction<Fn>(fn);
157
+ }
158
+
147
159
int ExecuteCommand (const Command &Cmd) {
148
160
zx_status_t rc;
149
161
@@ -164,17 +176,17 @@ int ExecuteCommand(const Command &Cmd) {
164
176
165
177
// Determine stdout
166
178
int FdOut = STDOUT_FILENO;
167
- fbl::unique_fd OutputFile;
179
+
168
180
if (Cmd.hasOutputFile ()) {
169
181
auto Filename = Cmd.getOutputFile ();
170
- OutputFile. reset ( open (Filename.c_str (), O_WRONLY | O_CREAT | O_TRUNC, 0 ) );
171
- if (!OutputFile ) {
182
+ FdOut = open (Filename.c_str (), O_WRONLY | O_CREAT | O_TRUNC, 0 );
183
+ if (FdOut == - 1 ) {
172
184
Printf (" libFuzzer: failed to open %s: %s\n " , Filename.c_str (),
173
185
strerror (errno));
174
186
return ZX_ERR_IO;
175
187
}
176
- FdOut = OutputFile.get ();
177
188
}
189
+ auto CloseFdOut = at_scope_exit ([&]() { close (FdOut); } );
178
190
179
191
// Determine stderr
180
192
int FdErr = STDERR_FILENO;
@@ -185,7 +197,7 @@ int ExecuteCommand(const Command &Cmd) {
185
197
if ((rc = launchpad_clone_fd (lp, STDIN_FILENO, STDIN_FILENO)) != ZX_OK ||
186
198
(rc = launchpad_clone_fd (lp, FdOut, STDOUT_FILENO)) != ZX_OK ||
187
199
(rc = launchpad_clone_fd (lp, FdErr, STDERR_FILENO)) != ZX_OK) {
188
- Printf (" libFuzzer: failed to clone FDIO: %s\n " , zx_status_get_string (rc));
200
+ Printf (" libFuzzer: failed to clone FDIO: %s\n " , _zx_status_get_string (rc));
189
201
return rc;
190
202
}
191
203
@@ -194,22 +206,22 @@ int ExecuteCommand(const Command &Cmd) {
194
206
const char *ErrorMsg = nullptr ;
195
207
if ((rc = launchpad_go (lp, &ProcessHandle, &ErrorMsg)) != ZX_OK) {
196
208
Printf (" libFuzzer: failed to launch '%s': %s, %s\n " , Argv[0 ], ErrorMsg,
197
- zx_status_get_string (rc));
209
+ _zx_status_get_string (rc));
198
210
return rc;
199
211
}
200
- zx::process Process ( ProcessHandle);
212
+ auto CloseHandle = at_scope_exit ([&]() { _zx_handle_close ( ProcessHandle); } );
201
213
202
214
// Now join the process and return the exit status.
203
- if ((rc = Process. wait_one (ZX_PROCESS_TERMINATED, zx::time::infinite () ,
204
- nullptr )) != ZX_OK) {
215
+ if ((rc = _zx_object_wait_one (ProcessHandle, ZX_PROCESS_TERMINATED ,
216
+ ZX_TIME_INFINITE, nullptr )) != ZX_OK) {
205
217
Printf (" libFuzzer: failed to join '%s': %s\n " , Argv[0 ],
206
- zx_status_get_string (rc));
218
+ _zx_status_get_string (rc));
207
219
return rc;
208
220
}
209
221
210
222
zx_info_process_t Info;
211
- if ((rc = Process. get_info ( ZX_INFO_PROCESS, &Info, sizeof (Info), nullptr ,
212
- nullptr )) != ZX_OK) {
223
+ if ((rc = _zx_object_get_info (ProcessHandle, ZX_INFO_PROCESS, &Info,
224
+ sizeof (Info), nullptr , nullptr )) != ZX_OK) {
213
225
Printf (" libFuzzer: unable to get return code from '%s': %s\n " , Argv[0 ],
214
226
zx_status_get_string (rc));
215
227
return rc;
0 commit comments