13979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light/* Copyright (C) 2017 The Android Open Source Project
23979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light *
43979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * This file implements interfaces from the file jdwpTransport.h. This implementation
53979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * is licensed under the same terms as the file jdwpTransport.h.  The
63979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * copyright and license information for the file jdwpTranport.h follows.
73979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light *
83979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
93979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
103979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light *
113979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * This code is free software; you can redistribute it and/or modify it
123979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * under the terms of the GNU General Public License version 2 only, as
133979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * published by the Free Software Foundation.  Oracle designates this
143979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * particular file as subject to the "Classpath" exception as provided
153979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * by Oracle in the LICENSE file that accompanied this code.
163979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light *
173979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * This code is distributed in the hope that it will be useful, but WITHOUT
183979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
193979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
203979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * version 2 for more details (a copy is included in the LICENSE file that
213979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * accompanied this code).
223979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light *
233979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * You should have received a copy of the GNU General Public License version
243979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * 2 along with this work; if not, write to the Free Software Foundation,
253979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
263979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light *
273979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
283979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * or visit www.oracle.com if you need additional information or have any
293979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light * questions.
303979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light */
313979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
323979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#ifndef ART_DT_FD_FORWARD_DT_FD_FORWARD_H_
333979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#define ART_DT_FD_FORWARD_DT_FD_FORWARD_H_
343979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
353979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <atomic>
363979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <condition_variable>
373979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <mutex>
383979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <string>
393979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
403979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <android-base/logging.h>
413979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <android-base/thread_annotations.h>
423979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <android-base/unique_fd.h>
433979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
443979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <arpa/inet.h>
453979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <sys/eventfd.h>
463979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <unistd.h>
473979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <poll.h>
483979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
493979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <jni.h>
503979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <jvmti.h>
513979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include <jdwpTransport.h>
523979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
533979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#include "fd_transport.h"
543979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
553979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Lightnamespace dt_fd_forward {
563979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
573979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Lightstatic constexpr uint8_t kReplyFlag = 0x80;
583979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light// Macro and constexpr to make error values less annoying to write.
593979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#define ERR(e) JDWPTRANSPORT_ERROR_ ## e
603979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Lightstatic constexpr jdwpTransportError OK = ERR(NONE);
613979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
623979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Lightstatic constexpr const char kJdwpHandshake[14] = {
633979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light    'J', 'D', 'W', 'P', '-', 'H', 'a', 'n', 'd', 's', 'h', 'a', 'k', 'e'
643979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light};  // "JDWP-Handshake"
653979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
663979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Lightenum class TransportState {
673979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  kClosed,       // Main state.
683979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  kListenSetup,  // Transient, wait for the state to change before proceeding.
693979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  kListening,    // Main state.
703979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  kOpening,      // Transient, wait for the state to change before proceeding.
713979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  kOpen,         // Main state.
723979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light};
733979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
743979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Lightenum class IOResult {
753979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  kOk, kInterrupt, kError, kEOF,
763979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light};
773979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
783979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Lightclass PacketReader;
793979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Lightclass PacketWriter;
803979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
813979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light// TODO It would be good to get the thread-safety analysis checks working but first we would need to
823979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light// use something other than std::mutex which does not have the annotations required.
833979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Lightclass FdForwardTransport : public jdwpTransportEnv {
843979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light public:
853979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  explicit FdForwardTransport(jdwpTransportCallback* cb);
863979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  ~FdForwardTransport();
873979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
883979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  jdwpTransportError PerformAttach(int listen_fd);
893979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  jdwpTransportError SetupListen(int listen_fd);
903979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  jdwpTransportError StopListening();
913979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
923979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  jboolean IsOpen();
933979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
943979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  jdwpTransportError WritePacket(const jdwpPacket* pkt);
953979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  jdwpTransportError ReadPacket(jdwpPacket* pkt);
963979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  jdwpTransportError Close();
973979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  jdwpTransportError Accept();
983979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  jdwpTransportError GetLastError(/*out*/char** description);
993979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1003979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  void* Alloc(size_t data);
1013979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  void Free(void* data);
1023979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1033979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light private:
1043979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  void SetLastError(const std::string& desc);
1053979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1063979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  bool ChangeState(TransportState old_state, TransportState new_state);  // REQUIRES(state_mutex_);
1073979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
10815b8113eb72b829e2026477a49e159635a48349bAlex Light  // Gets the fds from the server side. do_handshake returns whether the transport can skip the
10915b8113eb72b829e2026477a49e159635a48349bAlex Light  // jdwp handshake.
11015b8113eb72b829e2026477a49e159635a48349bAlex Light  IOResult ReceiveFdsFromSocket(/*out*/bool* do_handshake);
1113979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1123979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  IOResult WriteFully(const void* data, size_t ndata);  // REQUIRES(!state_mutex_);
1133979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  IOResult WriteFullyWithoutChecks(const void* data, size_t ndata);  // REQUIRES(state_mutex_);
1143979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  IOResult ReadFully(void* data, size_t ndata);  // REQUIRES(!state_mutex_);
1153979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  IOResult ReadUpToMax(void* data, size_t ndata, /*out*/size_t* amount_read);
1163979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light      // REQUIRES(state_mutex_);
1173979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  IOResult ReadFullyWithoutChecks(void* data, size_t ndata);  // REQUIRES(state_mutex_);
1183979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1193979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  void CloseFdsLocked();  // REQUIRES(state_mutex_)
1203979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1213979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  // The allocation/deallocation functions.
1223979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  jdwpTransportCallback mem_;
1233979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1243979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  // Input from the server;
1253979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  android::base::unique_fd read_fd_;  // GUARDED_BY(state_mutex_);
1263979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  // Output to the server;
1273979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  android::base::unique_fd write_fd_;  // GUARDED_BY(state_mutex_);
1283979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1293979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  // an eventfd passed with the write_fd to the transport that we will 'read' from to get a lock on
1303979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  // the write_fd_. The other side must not hold it for unbounded time.
1313979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  android::base::unique_fd write_lock_fd_;  // GUARDED_BY(state_mutex_);
1323979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1333979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  // Eventfd we will use to wake-up paused reads for close().
1343979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  android::base::unique_fd wakeup_fd_;
1353979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1363979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  // Socket we will get the read/write fd's from.
1373979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  android::base::unique_fd listen_fd_;
1383979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1393979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  // Fd we will write close notification to. This is a dup of listen_fd_.
1403979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  android::base::unique_fd close_notify_fd_;
1413979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1423979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  TransportState state_;  // GUARDED_BY(state_mutex_);
1433979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1443979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  std::mutex state_mutex_;
1453979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  std::condition_variable state_cv_;
1463979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1473979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  // A counter that we use to make sure we don't do half a read on one and half on another fd.
1483979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  std::atomic<uint64_t> current_seq_num_;
1493979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1503979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  friend class PacketReader;  // For ReadFullyWithInterrupt
1513979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light  friend class PacketWriter;  // For WriteFullyWithInterrupt
1523979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light};
1533979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1543979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light}  // namespace dt_fd_forward
1553979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light
1563979571aa1dfc907569fb7e27ab225ca89f6f86eAlex Light#endif  // ART_DT_FD_FORWARD_DT_FD_FORWARD_H_
157