summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Gao2018-05-14 15:42:49 -0500
committerJosh Gao2018-05-22 16:55:36 -0500
commit3b37fa256f00abc6a6414984cabbb3dd9e0a0ad1 (patch)
treed61a5149997f33c85ee6625b71fea926007388ab
parentae9c1dc44a7cc6e6eb217ee3284f53fcf5332140 (diff)
downloadplatform-system-core-3b37fa256f00abc6a6414984cabbb3dd9e0a0ad1.tar.gz
platform-system-core-3b37fa256f00abc6a6414984cabbb3dd9e0a0ad1.tar.xz
platform-system-core-3b37fa256f00abc6a6414984cabbb3dd9e0a0ad1.zip
adb: convert fdevent over to unique_fd.
This adds fdsan deallocation sanitization to all fds monitored by fdevent, which is most of the ones in adb. Bug: http://b/79786774 Test: python test_device.py Change-Id: I465804fdb0fd0ac019445900a30ba3403f5bf711
-rw-r--r--adb/fdevent.cpp19
-rw-r--r--adb/fdevent.h4
2 files changed, 12 insertions, 11 deletions
diff --git a/adb/fdevent.cpp b/adb/fdevent.cpp
index f9e262c51..1b7758c57 100644
--- a/adb/fdevent.cpp
+++ b/adb/fdevent.cpp
@@ -57,7 +57,7 @@ struct PollNode {
57 57
58 explicit PollNode(fdevent* fde) : fde(fde) { 58 explicit PollNode(fdevent* fde) : fde(fde) {
59 memset(&pollfd, 0, sizeof(pollfd)); 59 memset(&pollfd, 0, sizeof(pollfd));
60 pollfd.fd = fde->fd; 60 pollfd.fd = fde->fd.get();
61 61
62#if defined(__linux__) 62#if defined(__linux__)
63 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect. 63 // Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
@@ -111,7 +111,7 @@ static std::string dump_fde(const fdevent* fde) {
111 if (fde->state & FDE_ERROR) { 111 if (fde->state & FDE_ERROR) {
112 state += "E"; 112 state += "E";
113 } 113 }
114 return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str()); 114 return android::base::StringPrintf("(fdevent %d %s)", fde->fd.get(), state.c_str());
115} 115}
116 116
117void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) { 117void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
@@ -126,7 +126,7 @@ fdevent* fdevent_create(int fd, fd_func func, void* arg) {
126 126
127 fdevent* fde = new fdevent(); 127 fdevent* fde = new fdevent();
128 fde->state = FDE_ACTIVE; 128 fde->state = FDE_ACTIVE;
129 fde->fd = fd; 129 fde->fd.reset(fd);
130 fde->func = func; 130 fde->func = func;
131 fde->arg = arg; 131 fde->arg = arg;
132 if (!set_file_block_mode(fd, false)) { 132 if (!set_file_block_mode(fd, false)) {
@@ -135,7 +135,7 @@ fdevent* fdevent_create(int fd, fd_func func, void* arg) {
135 // to handle it. 135 // to handle it.
136 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd; 136 LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
137 } 137 }
138 auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde)); 138 auto pair = g_poll_node_map.emplace(fde->fd.get(), PollNode(fde));
139 CHECK(pair.second) << "install existing fd " << fd; 139 CHECK(pair.second) << "install existing fd " << fd;
140 140
141 fde->state |= FDE_CREATED; 141 fde->state |= FDE_CREATED;
@@ -150,12 +150,11 @@ void fdevent_destroy(fdevent* fde) {
150 } 150 }
151 151
152 if (fde->state & FDE_ACTIVE) { 152 if (fde->state & FDE_ACTIVE) {
153 g_poll_node_map.erase(fde->fd); 153 g_poll_node_map.erase(fde->fd.get());
154 if (fde->state & FDE_PENDING) { 154 if (fde->state & FDE_PENDING) {
155 g_pending_list.remove(fde); 155 g_pending_list.remove(fde);
156 } 156 }
157 adb_close(fde->fd); 157 fde->fd.reset();
158 fde->fd = -1;
159 fde->state = 0; 158 fde->state = 0;
160 fde->events = 0; 159 fde->events = 0;
161 } 160 }
@@ -164,7 +163,7 @@ void fdevent_destroy(fdevent* fde) {
164} 163}
165 164
166static void fdevent_update(fdevent* fde, unsigned events) { 165static void fdevent_update(fdevent* fde, unsigned events) {
167 auto it = g_poll_node_map.find(fde->fd); 166 auto it = g_poll_node_map.find(fde->fd.get());
168 CHECK(it != g_poll_node_map.end()); 167 CHECK(it != g_poll_node_map.end());
169 PollNode& node = it->second; 168 PollNode& node = it->second;
170 if (events & FDE_READ) { 169 if (events & FDE_READ) {
@@ -263,7 +262,7 @@ static void fdevent_process() {
263 auto it = g_poll_node_map.find(pollfd.fd); 262 auto it = g_poll_node_map.find(pollfd.fd);
264 CHECK(it != g_poll_node_map.end()); 263 CHECK(it != g_poll_node_map.end());
265 fdevent* fde = it->second.fde; 264 fdevent* fde = it->second.fde;
266 CHECK_EQ(fde->fd, pollfd.fd); 265 CHECK_EQ(fde->fd.get(), pollfd.fd);
267 fde->events |= events; 266 fde->events |= events;
268 D("%s got events %x", dump_fde(fde).c_str(), events); 267 D("%s got events %x", dump_fde(fde).c_str(), events);
269 fde->state |= FDE_PENDING; 268 fde->state |= FDE_PENDING;
@@ -278,7 +277,7 @@ static void fdevent_call_fdfunc(fdevent* fde) {
278 CHECK(fde->state & FDE_PENDING); 277 CHECK(fde->state & FDE_PENDING);
279 fde->state &= (~FDE_PENDING); 278 fde->state &= (~FDE_PENDING);
280 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str()); 279 D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
281 fde->func(fde->fd, events, fde->arg); 280 fde->func(fde->fd.get(), events, fde->arg);
282} 281}
283 282
284static void fdevent_run_flush() EXCLUDES(run_queue_mutex) { 283static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
diff --git a/adb/fdevent.h b/adb/fdevent.h
index 9c6e06999..69c40721b 100644
--- a/adb/fdevent.h
+++ b/adb/fdevent.h
@@ -22,6 +22,8 @@
22 22
23#include <functional> 23#include <functional>
24 24
25#include "adb_unique_fd.h"
26
25/* events that may be observed */ 27/* events that may be observed */
26#define FDE_READ 0x0001 28#define FDE_READ 0x0001
27#define FDE_WRITE 0x0002 29#define FDE_WRITE 0x0002
@@ -33,7 +35,7 @@ struct fdevent {
33 fdevent* next = nullptr; 35 fdevent* next = nullptr;
34 fdevent* prev = nullptr; 36 fdevent* prev = nullptr;
35 37
36 int fd = -1; 38 unique_fd fd;
37 int force_eof = 0; 39 int force_eof = 0;
38 40
39 uint16_t state = 0; 41 uint16_t state = 0;