Skip to content

Commit 2f70693

Browse files
committedFeb 7, 2018
[Fuzzer] Use Zircon's public API on Fuchsia
The original libFuzzer Fuchsia port relied on convenience libraries, but these are not exported as part of Fuchsia sysroot. This change eliminates the use of these libraries and relies on public API only. Differential Revision: https://reviews.llvm.org/D42996 llvm-svn: 324454
1 parent dd5ee6f commit 2f70693

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed
 

‎compiler-rt/lib/fuzzer/FuzzerUtilFuchsia.cpp

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,17 @@
1717
#include <cerrno>
1818
#include <cinttypes>
1919
#include <cstdint>
20-
#include <fbl/unique_fd.h>
2120
#include <fcntl.h>
2221
#include <launchpad/launchpad.h>
2322
#include <string>
2423
#include <thread>
24+
#include <unistd.h>
2525
#include <zircon/errors.h>
26+
#include <zircon/process.h>
2627
#include <zircon/status.h>
2728
#include <zircon/syscalls.h>
2829
#include <zircon/syscalls/port.h>
2930
#include <zircon/types.h>
30-
#include <zx/object.h>
31-
#include <zx/port.h>
32-
#include <zx/process.h>
33-
#include <zx/time.h>
3431

3532
namespace fuzzer {
3633

@@ -53,13 +50,13 @@ void InterruptHandler() {
5350
Fuzzer::StaticInterruptCallback();
5451
}
5552

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);
5855
zx_port_packet_t Packet;
59-
ExceptionPort->wait(zx::time::infinite(), &Packet, 0);
56+
_zx_port_wait(*ExceptionPort, ZX_TIME_INFINITE, &Packet, 1);
6057
// 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) {
6360
// Shouldn't happen; if it does the safest option is to just exit.
6461
Printf("libFuzzer: unable to unbind exception port; aborting!\n");
6562
exit(1);
@@ -97,15 +94,15 @@ void SetSignalHandler(const FuzzingOptions &Options) {
9794
return;
9895

9996
// 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));
103100
exit(1);
104101
}
105102

106103
// 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) {
109106
Printf("libFuzzer: unable to bind exception port: %s\n",
110107
zx_status_get_string(rc));
111108
exit(1);
@@ -117,13 +114,13 @@ void SetSignalHandler(const FuzzingOptions &Options) {
117114
}
118115

119116
void SleepSeconds(int Seconds) {
120-
zx::nanosleep(zx::deadline_after(zx::sec(Seconds)));
117+
_zx_nanosleep(_zx_deadline_after(ZX_SEC(Seconds)));
121118
}
122119

123120
unsigned long GetPid() {
124121
zx_status_t rc;
125122
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,
127124
sizeof(Info), NULL, NULL)) != ZX_OK) {
128125
Printf("libFuzzer: unable to get info about self: %s\n",
129126
zx_status_get_string(rc));
@@ -135,15 +132,30 @@ unsigned long GetPid() {
135132
size_t GetPeakRSSMb() {
136133
zx_status_t rc;
137134
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,
139136
sizeof(Info), NULL, NULL)) != ZX_OK) {
140137
Printf("libFuzzer: unable to get info about self: %s\n",
141-
zx_status_get_string(rc));
138+
_zx_status_get_string(rc));
142139
exit(1);
143140
}
144141
return (Info.mem_private_bytes + Info.mem_shared_bytes) >> 20;
145142
}
146143

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+
147159
int ExecuteCommand(const Command &Cmd) {
148160
zx_status_t rc;
149161

@@ -164,17 +176,17 @@ int ExecuteCommand(const Command &Cmd) {
164176

165177
// Determine stdout
166178
int FdOut = STDOUT_FILENO;
167-
fbl::unique_fd OutputFile;
179+
168180
if (Cmd.hasOutputFile()) {
169181
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) {
172184
Printf("libFuzzer: failed to open %s: %s\n", Filename.c_str(),
173185
strerror(errno));
174186
return ZX_ERR_IO;
175187
}
176-
FdOut = OutputFile.get();
177188
}
189+
auto CloseFdOut = at_scope_exit([&]() { close(FdOut); } );
178190

179191
// Determine stderr
180192
int FdErr = STDERR_FILENO;
@@ -185,7 +197,7 @@ int ExecuteCommand(const Command &Cmd) {
185197
if ((rc = launchpad_clone_fd(lp, STDIN_FILENO, STDIN_FILENO)) != ZX_OK ||
186198
(rc = launchpad_clone_fd(lp, FdOut, STDOUT_FILENO)) != ZX_OK ||
187199
(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));
189201
return rc;
190202
}
191203

@@ -194,22 +206,22 @@ int ExecuteCommand(const Command &Cmd) {
194206
const char *ErrorMsg = nullptr;
195207
if ((rc = launchpad_go(lp, &ProcessHandle, &ErrorMsg)) != ZX_OK) {
196208
Printf("libFuzzer: failed to launch '%s': %s, %s\n", Argv[0], ErrorMsg,
197-
zx_status_get_string(rc));
209+
_zx_status_get_string(rc));
198210
return rc;
199211
}
200-
zx::process Process(ProcessHandle);
212+
auto CloseHandle = at_scope_exit([&]() { _zx_handle_close(ProcessHandle); });
201213

202214
// 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) {
205217
Printf("libFuzzer: failed to join '%s': %s\n", Argv[0],
206-
zx_status_get_string(rc));
218+
_zx_status_get_string(rc));
207219
return rc;
208220
}
209221

210222
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) {
213225
Printf("libFuzzer: unable to get return code from '%s': %s\n", Argv[0],
214226
zx_status_get_string(rc));
215227
return rc;

0 commit comments

Comments
 (0)