1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef HDC_JDWP_H 16 #define HDC_JDWP_H 17 #include "daemon_common.h" 18 #include <poll.h> 19 #include <unordered_map> 20 namespace Hdc { 21 class HdcJdwp { 22 public: 23 HdcJdwp(uv_loop_t *loopIn); 24 virtual ~HdcJdwp(); 25 int Initial(); 26 void Stop(); 27 bool CreateJdwpTracker(HTaskInfo taskInfo); 28 void RemoveJdwpTracker(HTaskInfo taskInfo); 29 bool ReadyForRelease(); 30 string GetProcessList(); 31 bool SendJdwpNewFD(uint32_t targetPID, int fd); 32 bool SendArkNewFD(const std::string str, int fd); 33 bool CheckPIDExist(uint32_t targetPID); 34 bool SendFdToApp(int sockfd, uint8_t *buf, int size, int fd); 35 36 #ifdef FUZZ_TEST 37 public: 38 #else 39 private: 40 #endif 41 #ifdef JS_JDWP_CONNECT 42 static constexpr uint8_t JS_PKG_MIN_SIZE = 7; // JsMsgHeader + "pkgName:"uint8_t[7~128] 43 static constexpr uint8_t JS_PKG_MAX_SIZE = 128; 44 struct JsMsgHeader { 45 uint32_t msgLen; 46 uint32_t pid; 47 uint8_t isDebug; // 1:debugApp 0:releaseApp 48 }; 49 string GetProcessListExtendPkgName(uint8_t dr); 50 #endif // JS_JDWP_CONNECT 51 struct _PollFd { 52 int fd; 53 short events; 54 short revents; 55 }; 56 struct PollNode { 57 _PollFd pollfd; 58 uint32_t ppid; PollNodePollNode59 PollNode(int fd, uint32_t pid) 60 { 61 Base::ZeroStruct(pollfd); 62 pollfd.fd = fd; 63 pollfd.events = POLLNVAL | POLLRDHUP | POLLHUP | POLLERR; 64 pollfd.revents = 0; 65 ppid = pid; 66 } 67 }; 68 struct ContextJdwp { 69 uint32_t pid; 70 uv_pipe_t pipe; 71 HdcJdwp *thisClass; 72 bool finish; 73 #ifdef JS_JDWP_CONNECT 74 char buf[JS_PKG_MAX_SIZE + sizeof(JsMsgHeader)]; 75 string pkgName; 76 #else 77 char buf[sizeof(uint32_t)]; 78 #endif // JS_JDWP_CONNECT 79 uint8_t dummy; 80 uv_tcp_t jvmTCP; 81 uint8_t isDebug; 82 }; 83 using HCtxJdwp = struct ContextJdwp *; 84 85 bool JdwpListen(); 86 static int UvPipeBind(uv_pipe_t* handle, const char* name, size_t size); 87 static void AcceptClient(uv_stream_t *server, int status); 88 static void ReadStream(uv_stream_t *pipe, ssize_t nread, const uv_buf_t *buf); 89 static void SendCallbackJdwpNewFD(uv_write_t *req, int status); 90 static void *FdEventPollThread(void *args); 91 void RemoveFdFromPollList(uint32_t pid); 92 size_t JdwpProcessListMsg(char *buffer, size_t bufferlen, uint8_t dr); 93 void *MallocContext(); 94 void FreeContext(HCtxJdwp ctx); 95 void *AdminContext(const uint8_t op, const uint32_t pid, HCtxJdwp ctxJdwp); 96 int CreateFdEventPoll(); 97 void ProcessListUpdated(HTaskInfo task = nullptr); 98 void SendProcessList(HTaskInfo t, string data); 99 void DrainAwakenPollThread() const; 100 void WakePollThread(); 101 uv_loop_t *loop; 102 uv_pipe_t listenPipe = {}; 103 uint32_t refCount; 104 int32_t awakenPollFd; 105 map<uint32_t, HCtxJdwp> mapCtxJdwp; 106 uv_rwlock_t lockMapContext; 107 uv_rwlock_t lockJdwpTrack; 108 std::unordered_map<int, PollNode> pollNodeMap; // fd, PollNode 109 std::vector<HTaskInfo> jdwpTrackers; 110 bool stop; 111 std::mutex freeContextMutex; 112 }; 113 } // namespace Hdc 114 #endif 115