1 /* Copyright (C) 2017 The Android Open Source Project
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This file implements interfaces from the file jdwpTransport.h. This
5  * implementation is licensed under the same terms as the file
6  * jdwpTransport.h. The copyright and license information for the file
7  * jdwpTransport.h follows.
8  *
9  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
10  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
11  *
12  * This code is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License version 2 only, as
14  * published by the Free Software Foundation.  Oracle designates this
15  * particular file as subject to the "Classpath" exception as provided
16  * by Oracle in the LICENSE file that accompanied this code.
17  *
18  * This code is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21  * version 2 for more details (a copy is included in the LICENSE file that
22  * accompanied this code).
23  *
24  * You should have received a copy of the GNU General Public License version
25  * 2 along with this work; if not, write to the Free Software Foundation,
26  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27  *
28  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
29  * or visit www.oracle.com if you need additional information or have any
30  * questions.
31  */
32 
33 #include "dt_fd_forward.h"
34 
35 #include <string>
36 #include <vector>
37 
38 #include <android-base/endian.h>
39 #include <android-base/logging.h>
40 #include <android-base/parseint.h>
41 #include <android-base/stringprintf.h>
42 
43 #include <sys/ioctl.h>
44 #include <sys/eventfd.h>
45 #include <sys/socket.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <poll.h>
49 
50 #include <jni.h>
51 #include <jdwpTransport.h>
52 
53 #include <base/strlcpy.h>
54 
55 #include "fd_transport.h"
56 
57 namespace dt_fd_forward {
58 
59 // Helper that puts line-number in error message.
60 #define DT_IO_ERROR(f) \
61     SetLastError(::android::base::StringPrintf("%s:%d - %s: %s", \
62                                                __FILE__, __LINE__, f, strerror(errno)))
63 
64 extern const jdwpTransportNativeInterface_ gTransportInterface;
65 
66 template <typename T> static T HostToNetwork(T in);
67 template <typename T> static T NetworkToHost(T in);
68 
HostToNetwork(int8_t in)69 template<> int8_t HostToNetwork(int8_t in) { return in; }
NetworkToHost(int8_t in)70 template<> int8_t NetworkToHost(int8_t in) { return in; }
HostToNetwork(int16_t in)71 template<> int16_t HostToNetwork(int16_t in) { return htons(in); }
NetworkToHost(int16_t in)72 template<> int16_t NetworkToHost(int16_t in) { return ntohs(in); }
HostToNetwork(int32_t in)73 template<> int32_t HostToNetwork(int32_t in) { return htonl(in); }
NetworkToHost(int32_t in)74 template<> int32_t NetworkToHost(int32_t in) { return ntohl(in); }
75 
FdForwardTransport(jdwpTransportCallback * cb)76 FdForwardTransport::FdForwardTransport(jdwpTransportCallback* cb)
77     : mem_(*cb),
78       read_fd_(-1),
79       write_fd_(-1),
80       wakeup_fd_(eventfd(0, EFD_NONBLOCK)),
81       listen_fd_(-1),
82       close_notify_fd_(-1),
83       state_(TransportState::kClosed),
84       current_seq_num_(0) {}
85 
~FdForwardTransport()86 FdForwardTransport::~FdForwardTransport() { }
87 
ChangeState(TransportState old_state,TransportState new_state)88 bool FdForwardTransport::ChangeState(TransportState old_state, TransportState new_state) {
89   if (old_state == state_) {
90     state_ = new_state;
91     state_cv_.notify_all();
92     return true;
93   } else {
94     return false;
95   }
96 }
97 
PerformAttach(int listen_fd)98 jdwpTransportError FdForwardTransport::PerformAttach(int listen_fd) {
99   jdwpTransportError err = SetupListen(listen_fd);
100   if (err != OK) {
101     return OK;
102   }
103   err = Accept();
104   StopListening();
105   return err;
106 }
107 
SendListenMessage(const android::base::unique_fd & fd)108 static void SendListenMessage(const android::base::unique_fd& fd) {
109   TEMP_FAILURE_RETRY(send(fd, kListenStartMessage, sizeof(kListenStartMessage), MSG_EOR));
110 }
111 
112 // Copy from file_utils, so we do not need to depend on libartbase.
DupCloexec(int fd)113 static int DupCloexec(int fd) {
114 #if defined(__linux__)
115   return fcntl(fd, F_DUPFD_CLOEXEC, 0);
116 #else
117   return dup(fd);
118 #endif
119 }
120 
SetupListen(int listen_fd)121 jdwpTransportError FdForwardTransport::SetupListen(int listen_fd) {
122   std::lock_guard<std::mutex> lk(state_mutex_);
123   if (!ChangeState(TransportState::kClosed, TransportState::kListenSetup)) {
124     return ERR(ILLEGAL_STATE);
125   } else {
126     listen_fd_.reset(DupCloexec(listen_fd));
127     SendListenMessage(listen_fd_);
128     CHECK(ChangeState(TransportState::kListenSetup, TransportState::kListening));
129     return OK;
130   }
131 }
132 
SendListenEndMessage(const android::base::unique_fd & fd)133 static void SendListenEndMessage(const android::base::unique_fd& fd) {
134   TEMP_FAILURE_RETRY(send(fd, kListenEndMessage, sizeof(kListenEndMessage), MSG_EOR));
135 }
136 
StopListening()137 jdwpTransportError FdForwardTransport::StopListening() {
138   std::lock_guard<std::mutex> lk(state_mutex_);
139   if (listen_fd_ != -1) {
140     SendListenEndMessage(listen_fd_);
141   }
142   // Don't close the listen_fd_ since we might need it for later calls to listen.
143   if (ChangeState(TransportState::kListening, TransportState::kClosed) ||
144       state_ == TransportState::kOpen) {
145     listen_fd_.reset();
146   }
147   return OK;
148 }
149 
150 // Last error message.
151 thread_local std::string global_last_error_;
152 
SetLastError(const std::string & desc)153 void FdForwardTransport::SetLastError(const std::string& desc) {
154   LOG(ERROR) << desc;
155   global_last_error_ = desc;
156 }
157 
ReadFullyWithoutChecks(void * data,size_t ndata)158 IOResult FdForwardTransport::ReadFullyWithoutChecks(void* data, size_t ndata) {
159   uint8_t* bdata = reinterpret_cast<uint8_t*>(data);
160   size_t nbytes = 0;
161   while (nbytes < ndata) {
162     int res = TEMP_FAILURE_RETRY(read(read_fd_, bdata + nbytes, ndata - nbytes));
163     if (res < 0) {
164       DT_IO_ERROR("Failed read()");
165       return IOResult::kError;
166     } else if (res == 0) {
167       return IOResult::kEOF;
168     } else {
169       nbytes += res;
170     }
171   }
172   return IOResult::kOk;
173 }
174 
ReadUpToMax(void * data,size_t ndata,size_t * read_amount)175 IOResult FdForwardTransport::ReadUpToMax(void* data, size_t ndata, /*out*/size_t* read_amount) {
176   CHECK_GE(read_fd_.get(), 0);
177   int avail;
178   int res = TEMP_FAILURE_RETRY(ioctl(read_fd_, FIONREAD, &avail));
179   if (res < 0) {
180     DT_IO_ERROR("Failed ioctl(read_fd_, FIONREAD, &avail)");
181     return IOResult::kError;
182   }
183   size_t to_read = std::min(static_cast<size_t>(avail), ndata);
184   *read_amount = to_read;
185   if (*read_amount == 0) {
186     // Check if the read would cause an EOF.
187     struct pollfd pollfd = { read_fd_, POLLRDHUP, 0 };
188     res = TEMP_FAILURE_RETRY(poll(&pollfd, /*nfds*/1, /*timeout*/0));
189     if (res < 0 || (pollfd.revents & POLLERR) == POLLERR) {
190       DT_IO_ERROR("Failed poll on read fd.");
191       return IOResult::kError;
192     }
193     return ((pollfd.revents & (POLLRDHUP | POLLHUP)) == 0) ? IOResult::kOk : IOResult::kEOF;
194   }
195 
196   return ReadFullyWithoutChecks(data, to_read);
197 }
198 
ReadFully(void * data,size_t ndata)199 IOResult FdForwardTransport::ReadFully(void* data, size_t ndata) {
200   uint64_t seq_num = current_seq_num_;
201   size_t nbytes = 0;
202   while (nbytes < ndata) {
203     size_t read_len;
204     struct pollfd pollfds[2];
205     {
206       std::lock_guard<std::mutex> lk(state_mutex_);
207       // Operations in this block must not cause an unbounded pause.
208       if (state_ != TransportState::kOpen || seq_num != current_seq_num_) {
209         // Async-close occurred!
210         return IOResult::kInterrupt;
211       } else {
212         CHECK_GE(read_fd_.get(), 0);
213       }
214       IOResult res = ReadUpToMax(reinterpret_cast<uint8_t*>(data) + nbytes,
215                                  ndata - nbytes,
216                                  /*out*/&read_len);
217       if (res != IOResult::kOk) {
218         return res;
219       } else {
220         nbytes += read_len;
221       }
222 
223       pollfds[0] = { read_fd_, POLLRDHUP | POLLIN, 0 };
224       pollfds[1] = { wakeup_fd_, POLLIN, 0 };
225     }
226     if (read_len == 0) {
227       // No more data. Sleep without locks until more is available. We don't actually check for any
228       // errors since possible ones are (1) the read_fd_ is closed or wakeup happens which are both
229       // fine since the wakeup_fd_ or the poll failing will wake us up.
230       int poll_res = TEMP_FAILURE_RETRY(poll(pollfds, 2, -1));
231       if (poll_res < 0) {
232         DT_IO_ERROR("Failed to poll!");
233       }
234       // Clear the wakeup_fd regardless.
235       uint64_t val;
236       int unused = TEMP_FAILURE_RETRY(read(wakeup_fd_, &val, sizeof(val)));
237       DCHECK(unused == sizeof(val) || errno == EAGAIN);
238       if (poll_res < 0) {
239         return IOResult::kError;
240       }
241     }
242   }
243   return IOResult::kOk;
244 }
245 
246 // A helper that allows us to lock the eventfd 'fd'.
247 class ScopedEventFdLock {
248  public:
ScopedEventFdLock(const android::base::unique_fd & fd)249   explicit ScopedEventFdLock(const android::base::unique_fd& fd) : fd_(fd), data_(0) {
250     TEMP_FAILURE_RETRY(read(fd_, &data_, sizeof(data_)));
251   }
252 
~ScopedEventFdLock()253   ~ScopedEventFdLock() {
254     TEMP_FAILURE_RETRY(write(fd_, &data_, sizeof(data_)));
255   }
256 
257  private:
258   const android::base::unique_fd& fd_;
259   uint64_t data_;
260 };
261 
WriteFullyWithoutChecks(const void * data,size_t ndata)262 IOResult FdForwardTransport::WriteFullyWithoutChecks(const void* data, size_t ndata) {
263   ScopedEventFdLock sefdl(write_lock_fd_);
264   const uint8_t* bdata = static_cast<const uint8_t*>(data);
265   size_t nbytes = 0;
266   while (nbytes < ndata) {
267     int res = TEMP_FAILURE_RETRY(write(write_fd_, bdata + nbytes, ndata - nbytes));
268     if (res < 0) {
269       DT_IO_ERROR("Failed write()");
270       return IOResult::kError;
271     } else if (res == 0) {
272       return IOResult::kEOF;
273     } else {
274       nbytes += res;
275     }
276   }
277   return IOResult::kOk;
278 }
279 
WriteFully(const void * data,size_t ndata)280 IOResult FdForwardTransport::WriteFully(const void* data, size_t ndata) {
281   std::lock_guard<std::mutex> lk(state_mutex_);
282   if (state_ != TransportState::kOpen) {
283     return IOResult::kInterrupt;
284   }
285   return WriteFullyWithoutChecks(data, ndata);
286 }
287 
SendAcceptMessage(int fd)288 static void SendAcceptMessage(int fd) {
289   TEMP_FAILURE_RETRY(send(fd, kAcceptMessage, sizeof(kAcceptMessage), MSG_EOR));
290 }
291 
SendHandshakeCompleteMessage(int fd)292 static void SendHandshakeCompleteMessage(int fd) {
293   TEMP_FAILURE_RETRY(
294       send(fd, kHandshakeCompleteMessage, sizeof(kHandshakeCompleteMessage), MSG_EOR));
295 }
296 
ReceiveFdsFromSocket(bool * do_handshake)297 IOResult FdForwardTransport::ReceiveFdsFromSocket(bool* do_handshake) {
298   union {
299     cmsghdr cm;
300     uint8_t buffer[CMSG_SPACE(sizeof(FdSet))];
301   } msg_union;
302   // This lets us know if we need to do a handshake or not.
303   char message[128];
304   iovec iov;
305   iov.iov_base = message;
306   iov.iov_len  = sizeof(message);
307 
308   msghdr msg;
309   memset(&msg, 0, sizeof(msg));
310   msg.msg_iov = &iov;
311   msg.msg_iovlen = 1;
312   msg.msg_control = msg_union.buffer;
313   msg.msg_controllen = sizeof(msg_union.buffer);
314 
315   cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
316   cmsg->cmsg_len   = msg.msg_controllen;
317   cmsg->cmsg_level = SOL_SOCKET;
318   cmsg->cmsg_type  = SCM_RIGHTS;
319   memset(reinterpret_cast<int*>(CMSG_DATA(cmsg)), -1, FdSet::kDataLength);
320 
321   int res = TEMP_FAILURE_RETRY(recvmsg(listen_fd_, &msg, 0));
322   if (res <= 0) {
323     DT_IO_ERROR("Failed to receive fds!");
324     return IOResult::kError;
325   }
326   FdSet out_fds = FdSet::ReadData(CMSG_DATA(cmsg));
327   bool failed = false;
328   if (out_fds.read_fd_ < 0 ||
329       out_fds.write_fd_ < 0 ||
330       out_fds.write_lock_fd_ < 0) {
331     DT_IO_ERROR("Received fds were invalid!");
332     failed = true;
333   } else if (strcmp(kPerformHandshakeMessage, message) == 0) {
334     *do_handshake = true;
335   } else if (strcmp(kSkipHandshakeMessage, message) == 0) {
336     *do_handshake = false;
337   } else {
338     DT_IO_ERROR("Unknown message sent with fds.");
339     failed = true;
340   }
341 
342   if (failed) {
343     if (out_fds.read_fd_ >= 0) {
344       close(out_fds.read_fd_);
345     }
346     if (out_fds.write_fd_ >= 0) {
347       close(out_fds.write_fd_);
348     }
349     if (out_fds.write_lock_fd_ >= 0) {
350       close(out_fds.write_lock_fd_);
351     }
352     return IOResult::kError;
353   }
354 
355   read_fd_.reset(out_fds.read_fd_);
356   write_fd_.reset(out_fds.write_fd_);
357   write_lock_fd_.reset(out_fds.write_lock_fd_);
358 
359   // We got the fds. Send ack.
360   close_notify_fd_.reset(DupCloexec(listen_fd_));
361   SendAcceptMessage(close_notify_fd_);
362 
363   return IOResult::kOk;
364 }
365 
366 // Accept the connection. Note that we match the behavior of other transports which is to just close
367 // the connection and try again if we get a bad handshake.
Accept()368 jdwpTransportError FdForwardTransport::Accept() {
369   // TODO Work with timeouts.
370   while (true) {
371     std::unique_lock<std::mutex> lk(state_mutex_);
372     while (!ChangeState(TransportState::kListening, TransportState::kOpening)) {
373       if (state_ == TransportState::kClosed ||
374           state_ == TransportState::kOpen) {
375         return ERR(ILLEGAL_STATE);
376       }
377       state_cv_.wait(lk);
378     }
379 
380     bool do_handshake = false;
381     DCHECK_NE(listen_fd_.get(), -1);
382     if (ReceiveFdsFromSocket(&do_handshake) != IOResult::kOk) {
383       CHECK(ChangeState(TransportState::kOpening, TransportState::kListening));
384       return ERR(IO_ERROR);
385     }
386 
387     current_seq_num_++;
388 
389     // Moved to the opening state.
390     if (do_handshake) {
391       // Perform the handshake
392       char handshake_recv[sizeof(kJdwpHandshake)];
393       memset(handshake_recv, 0, sizeof(handshake_recv));
394       IOResult res = ReadFullyWithoutChecks(handshake_recv, sizeof(handshake_recv));
395       if (res != IOResult::kOk ||
396           strncmp(handshake_recv, kJdwpHandshake, sizeof(kJdwpHandshake)) != 0) {
397         DT_IO_ERROR("Failed to read handshake");
398         CHECK(ChangeState(TransportState::kOpening, TransportState::kListening));
399         CloseFdsLocked();
400         // Retry.
401         continue;
402       }
403       res = WriteFullyWithoutChecks(kJdwpHandshake, sizeof(kJdwpHandshake));
404       if (res != IOResult::kOk) {
405         DT_IO_ERROR("Failed to write handshake");
406         CHECK(ChangeState(TransportState::kOpening, TransportState::kListening));
407         CloseFdsLocked();
408         // Retry.
409         continue;
410       }
411     }
412     // Tell everyone we have finished the handshake.
413     SendHandshakeCompleteMessage(close_notify_fd_);
414     break;
415   }
416   CHECK(ChangeState(TransportState::kOpening, TransportState::kOpen));
417   return OK;
418 }
419 
SendClosingMessage(int fd)420 void SendClosingMessage(int fd) {
421   if (fd >= 0) {
422     TEMP_FAILURE_RETRY(send(fd, kCloseMessage, sizeof(kCloseMessage), MSG_EOR));
423   }
424 }
425 
426 // Actually close the fds associated with this transport.
CloseFdsLocked()427 void FdForwardTransport::CloseFdsLocked() {
428   // We have a different set of fd's now. Increase the seq number.
429   current_seq_num_++;
430 
431   // All access to these is locked under the state_mutex_ so we are safe to close these.
432   {
433     ScopedEventFdLock sefdl(write_lock_fd_);
434     if (close_notify_fd_ >= 0) {
435       SendClosingMessage(close_notify_fd_);
436     }
437     close_notify_fd_.reset();
438     read_fd_.reset();
439     write_fd_.reset();
440     close_notify_fd_.reset();
441   }
442   write_lock_fd_.reset();
443 
444   // Send a wakeup in case we have any in-progress reads/writes.
445   uint64_t data = 1;
446   TEMP_FAILURE_RETRY(write(wakeup_fd_, &data, sizeof(data)));
447 }
448 
Close()449 jdwpTransportError FdForwardTransport::Close() {
450   std::lock_guard<std::mutex> lk(state_mutex_);
451   jdwpTransportError res =
452       ChangeState(TransportState::kOpen, TransportState::kClosed) ? OK : ERR(ILLEGAL_STATE);
453   // Send a wakeup after changing the state even if nothing actually happened.
454   uint64_t data = 1;
455   TEMP_FAILURE_RETRY(write(wakeup_fd_, &data, sizeof(data)));
456   if (res == OK) {
457     CloseFdsLocked();
458   }
459   return res;
460 }
461 
462 // A helper class to read and parse the JDWP packet.
463 class PacketReader {
464  public:
PacketReader(FdForwardTransport * transport,jdwpPacket * pkt)465   PacketReader(FdForwardTransport* transport, jdwpPacket* pkt)
466       : transport_(transport),
467         pkt_(pkt),
468         is_eof_(false),
469         is_err_(false) {}
ReadFully()470   bool ReadFully() {
471     // Zero out.
472     memset(pkt_, 0, sizeof(jdwpPacket));
473     int32_t len = ReadInt32();         // read len
474     if (is_err_) {
475       return false;
476     } else if (is_eof_) {
477       return true;
478     } else if (len < 11) {
479       transport_->DT_IO_ERROR("Packet with len < 11 received!");
480       return false;
481     }
482     pkt_->type.cmd.len = len;
483     pkt_->type.cmd.id = ReadInt32();
484     pkt_->type.cmd.flags = ReadByte();
485     if (is_err_) {
486       return false;
487     } else if (is_eof_) {
488       return true;
489     } else if ((pkt_->type.reply.flags & JDWPTRANSPORT_FLAGS_REPLY) == JDWPTRANSPORT_FLAGS_REPLY) {
490       ReadReplyPacket();
491     } else {
492       ReadCmdPacket();
493     }
494     return !is_err_;
495   }
496 
497  private:
ReadReplyPacket()498   void ReadReplyPacket() {
499     pkt_->type.reply.errorCode = ReadInt16();
500     pkt_->type.reply.data = ReadRemaining();
501   }
502 
ReadCmdPacket()503   void ReadCmdPacket() {
504     pkt_->type.cmd.cmdSet = ReadByte();
505     pkt_->type.cmd.cmd = ReadByte();
506     pkt_->type.cmd.data = ReadRemaining();
507   }
508 
509   template <typename T>
HandleResult(IOResult res,T val,T fail)510   T HandleResult(IOResult res, T val, T fail) {
511     switch (res) {
512       case IOResult::kError:
513         is_err_ = true;
514         return fail;
515       case IOResult::kOk:
516         return val;
517       case IOResult::kEOF:
518         is_eof_ = true;
519         pkt_->type.cmd.len = 0;
520         return fail;
521       case IOResult::kInterrupt:
522         transport_->DT_IO_ERROR("Failed to read, concurrent close!");
523         is_err_ = true;
524         return fail;
525     }
526   }
527 
ReadRemaining()528   jbyte* ReadRemaining() {
529     if (is_eof_ || is_err_) {
530       return nullptr;
531     }
532     jbyte* out = nullptr;
533     jint rem = pkt_->type.cmd.len - 11;
534     CHECK_GE(rem, 0);
535     if (rem == 0) {
536       return nullptr;
537     } else {
538       out = reinterpret_cast<jbyte*>(transport_->Alloc(rem));
539       IOResult res = transport_->ReadFully(out, rem);
540       jbyte* ret = HandleResult(res, out, static_cast<jbyte*>(nullptr));
541       if (ret != out) {
542         transport_->Free(out);
543       }
544       return ret;
545     }
546   }
547 
ReadByte()548   jbyte ReadByte() {
549     if (is_eof_ || is_err_) {
550       return -1;
551     }
552     jbyte out;
553     IOResult res = transport_->ReadFully(&out, sizeof(out));
554     return HandleResult(res, NetworkToHost(out), static_cast<jbyte>(-1));
555   }
556 
ReadInt16()557   jshort ReadInt16() {
558     if (is_eof_ || is_err_) {
559       return -1;
560     }
561     jshort out;
562     IOResult res = transport_->ReadFully(&out, sizeof(out));
563     return HandleResult(res, NetworkToHost(out), static_cast<jshort>(-1));
564   }
565 
ReadInt32()566   jint ReadInt32() {
567     if (is_eof_ || is_err_) {
568       return -1;
569     }
570     jint out;
571     IOResult res = transport_->ReadFully(&out, sizeof(out));
572     return HandleResult(res, NetworkToHost(out), -1);
573   }
574 
575   FdForwardTransport* transport_;
576   jdwpPacket* pkt_;
577   bool is_eof_;
578   bool is_err_;
579 };
580 
ReadPacket(jdwpPacket * pkt)581 jdwpTransportError FdForwardTransport::ReadPacket(jdwpPacket* pkt) {
582   if (pkt == nullptr) {
583     return ERR(ILLEGAL_ARGUMENT);
584   }
585   PacketReader reader(this, pkt);
586   if (reader.ReadFully()) {
587     return OK;
588   } else {
589     return ERR(IO_ERROR);
590   }
591 }
592 
593 // A class that writes a packet to the transport.
594 class PacketWriter {
595  public:
PacketWriter(FdForwardTransport * transport,const jdwpPacket * pkt)596   PacketWriter(FdForwardTransport* transport, const jdwpPacket* pkt)
597       : transport_(transport), pkt_(pkt), data_() {}
598 
WriteFully()599   bool WriteFully() {
600     PushInt32(pkt_->type.cmd.len);
601     PushInt32(pkt_->type.cmd.id);
602     PushByte(pkt_->type.cmd.flags);
603     if ((pkt_->type.reply.flags & JDWPTRANSPORT_FLAGS_REPLY) == JDWPTRANSPORT_FLAGS_REPLY) {
604       PushInt16(pkt_->type.reply.errorCode);
605       PushData(pkt_->type.reply.data, pkt_->type.reply.len - 11);
606     } else {
607       PushByte(pkt_->type.cmd.cmdSet);
608       PushByte(pkt_->type.cmd.cmd);
609       PushData(pkt_->type.cmd.data, pkt_->type.cmd.len - 11);
610     }
611     IOResult res = transport_->WriteFully(data_.data(), data_.size());
612     return res == IOResult::kOk;
613   }
614 
615  private:
PushInt32(int32_t data)616   void PushInt32(int32_t data) {
617     data = HostToNetwork(data);
618     PushData(&data, sizeof(data));
619   }
PushInt16(int16_t data)620   void PushInt16(int16_t data) {
621     data = HostToNetwork(data);
622     PushData(&data, sizeof(data));
623   }
PushByte(jbyte data)624   void PushByte(jbyte data) {
625     data_.push_back(HostToNetwork(data));
626   }
627 
PushData(void * d,size_t size)628   void PushData(void* d, size_t size) {
629     uint8_t* bytes = reinterpret_cast<uint8_t*>(d);
630     data_.insert(data_.end(), bytes, bytes + size);
631   }
632 
633   FdForwardTransport* transport_;
634   const jdwpPacket* pkt_;
635   std::vector<uint8_t> data_;
636 };
637 
WritePacket(const jdwpPacket * pkt)638 jdwpTransportError FdForwardTransport::WritePacket(const jdwpPacket* pkt) {
639   if (pkt == nullptr) {
640     return ERR(ILLEGAL_ARGUMENT);
641   }
642   PacketWriter writer(this, pkt);
643   if (writer.WriteFully()) {
644     return OK;
645   } else {
646     return ERR(IO_ERROR);
647   }
648 }
649 
IsOpen()650 jboolean FdForwardTransport::IsOpen() {
651   return state_ == TransportState::kOpen;
652 }
653 
Alloc(size_t s)654 void* FdForwardTransport::Alloc(size_t s) {
655   return mem_.alloc(s);
656 }
657 
Free(void * data)658 void FdForwardTransport::Free(void* data) {
659   mem_.free(data);
660 }
661 
GetLastError(char ** err)662 jdwpTransportError FdForwardTransport::GetLastError(/*out*/char** err) {
663   std::string data = global_last_error_;
664   *err = reinterpret_cast<char*>(Alloc(data.size() + 1));
665   strlcpy(*err, data.c_str(), data.size() + 1);
666   return OK;
667 }
668 
AsFdForward(jdwpTransportEnv * env)669 static FdForwardTransport* AsFdForward(jdwpTransportEnv* env) {
670   return reinterpret_cast<FdForwardTransport*>(env);
671 }
672 
ParseAddress(const std::string & addr,int * listen_sock)673 static jdwpTransportError ParseAddress(const std::string& addr,
674                                        /*out*/int* listen_sock) {
675   if (!android::base::ParseInt(addr.c_str(), listen_sock) || *listen_sock < 0) {
676     LOG(ERROR) << "address format is <fd_num> not " << addr;
677     return ERR(ILLEGAL_ARGUMENT);
678   }
679   return OK;
680 }
681 
682 class JdwpTransportFunctions {
683  public:
GetCapabilities(jdwpTransportEnv * env ATTRIBUTE_UNUSED,JDWPTransportCapabilities * capabilities_ptr)684   static jdwpTransportError GetCapabilities(jdwpTransportEnv* env ATTRIBUTE_UNUSED,
685                                             /*out*/ JDWPTransportCapabilities* capabilities_ptr) {
686     // We don't support any of the optional capabilities (can_timeout_attach, can_timeout_accept,
687     // can_timeout_handshake) so just return a zeroed capabilities ptr.
688     // TODO We should maybe support these timeout options.
689     memset(capabilities_ptr, 0, sizeof(JDWPTransportCapabilities));
690     return OK;
691   }
692 
693   // Address is <sock_fd>
Attach(jdwpTransportEnv * env,const char * address,jlong attach_timeout ATTRIBUTE_UNUSED,jlong handshake_timeout ATTRIBUTE_UNUSED)694   static jdwpTransportError Attach(jdwpTransportEnv* env,
695                                    const char* address,
696                                    jlong attach_timeout ATTRIBUTE_UNUSED,
697                                    jlong handshake_timeout ATTRIBUTE_UNUSED) {
698     if (address == nullptr || *address == '\0') {
699       return ERR(ILLEGAL_ARGUMENT);
700     }
701     int listen_fd;
702     jdwpTransportError err = ParseAddress(address, &listen_fd);
703     if (err != OK) {
704       return err;
705     }
706     return AsFdForward(env)->PerformAttach(listen_fd);
707   }
708 
StartListening(jdwpTransportEnv * env,const char * address,char ** actual_address)709   static jdwpTransportError StartListening(jdwpTransportEnv* env,
710                                            const char* address,
711                                            /*out*/ char** actual_address) {
712     if (address == nullptr || *address == '\0') {
713       return ERR(ILLEGAL_ARGUMENT);
714     }
715     int listen_fd;
716     jdwpTransportError err = ParseAddress(address, &listen_fd);
717     if (err != OK) {
718       return err;
719     }
720     err = AsFdForward(env)->SetupListen(listen_fd);
721     if (err != OK) {
722       return err;
723     }
724     if (actual_address != nullptr) {
725       *actual_address = reinterpret_cast<char*>(AsFdForward(env)->Alloc(strlen(address) + 1));
726       memcpy(*actual_address, address, strlen(address) + 1);
727     }
728     return OK;
729   }
730 
StopListening(jdwpTransportEnv * env)731   static jdwpTransportError StopListening(jdwpTransportEnv* env) {
732     return AsFdForward(env)->StopListening();
733   }
734 
Accept(jdwpTransportEnv * env,jlong accept_timeout ATTRIBUTE_UNUSED,jlong handshake_timeout ATTRIBUTE_UNUSED)735   static jdwpTransportError Accept(jdwpTransportEnv* env,
736                                    jlong accept_timeout ATTRIBUTE_UNUSED,
737                                    jlong handshake_timeout ATTRIBUTE_UNUSED) {
738     return AsFdForward(env)->Accept();
739   }
740 
IsOpen(jdwpTransportEnv * env)741   static jboolean IsOpen(jdwpTransportEnv* env) {
742     return AsFdForward(env)->IsOpen();
743   }
744 
Close(jdwpTransportEnv * env)745   static jdwpTransportError Close(jdwpTransportEnv* env) {
746     return AsFdForward(env)->Close();
747   }
748 
ReadPacket(jdwpTransportEnv * env,jdwpPacket * pkt)749   static jdwpTransportError ReadPacket(jdwpTransportEnv* env, jdwpPacket *pkt) {
750     return AsFdForward(env)->ReadPacket(pkt);
751   }
752 
WritePacket(jdwpTransportEnv * env,const jdwpPacket * pkt)753   static jdwpTransportError WritePacket(jdwpTransportEnv* env, const jdwpPacket* pkt) {
754     return AsFdForward(env)->WritePacket(pkt);
755   }
756 
GetLastError(jdwpTransportEnv * env,char ** error)757   static jdwpTransportError GetLastError(jdwpTransportEnv* env, char** error) {
758     return AsFdForward(env)->GetLastError(error);
759   }
760 };
761 
762 // The actual struct holding all the entrypoints into the jdwpTransport interface.
763 const jdwpTransportNativeInterface_ gTransportInterface = {
764   nullptr,  // reserved1
765   JdwpTransportFunctions::GetCapabilities,
766   JdwpTransportFunctions::Attach,
767   JdwpTransportFunctions::StartListening,
768   JdwpTransportFunctions::StopListening,
769   JdwpTransportFunctions::Accept,
770   JdwpTransportFunctions::IsOpen,
771   JdwpTransportFunctions::Close,
772   JdwpTransportFunctions::ReadPacket,
773   JdwpTransportFunctions::WritePacket,
774   JdwpTransportFunctions::GetLastError,
775 };
776 
777 extern "C"
jdwpTransport_OnLoad(JavaVM * vm ATTRIBUTE_UNUSED,jdwpTransportCallback * cb,jint version,jdwpTransportEnv ** env)778 JNIEXPORT jint JNICALL jdwpTransport_OnLoad(JavaVM* vm ATTRIBUTE_UNUSED,
779                                             jdwpTransportCallback* cb,
780                                             jint version,
781                                             jdwpTransportEnv** /*out*/env) {
782   if (version != JDWPTRANSPORT_VERSION_1_0) {
783     LOG(ERROR) << "unknown version " << version;
784     return JNI_EVERSION;
785   }
786   void* data = cb->alloc(sizeof(FdForwardTransport));
787   if (data == nullptr) {
788     LOG(ERROR) << "Failed to allocate data for transport!";
789     return JNI_ENOMEM;
790   }
791   FdForwardTransport* transport =
792       new (data) FdForwardTransport(cb);
793   transport->functions = &gTransportInterface;
794   *env = transport;
795   return JNI_OK;
796 }
797 
798 }  // namespace dt_fd_forward
799