Tri Vo | 5b40e89 | 2020-11-01 13:01:29 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Sourete Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "coverage" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/unique_fd.h> |
| 21 | #include <assert.h> |
| 22 | #include <sys/mman.h> |
| 23 | #include <sys/uio.h> |
| 24 | #include <trusty/coverage/coverage.h> |
| 25 | #include <trusty/coverage/tipc.h> |
| 26 | #include <trusty/tipc.h> |
| 27 | |
| 28 | #define COVERAGE_CLIENT_PORT "com.android.trusty.coverage.client" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace trusty { |
| 32 | namespace coverage { |
| 33 | |
| 34 | using android::base::ErrnoError; |
| 35 | using android::base::Error; |
| 36 | using std::string; |
| 37 | |
| 38 | static inline uintptr_t RoundPageUp(uintptr_t val) { |
| 39 | return (val + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1); |
| 40 | } |
| 41 | |
| 42 | CoverageRecord::CoverageRecord(string tipc_dev, struct uuid* uuid) |
| 43 | : tipc_dev_(std::move(tipc_dev)), |
| 44 | coverage_srv_fd_(-1), |
| 45 | uuid_(*uuid), |
| 46 | record_len_(0), |
| 47 | shm_(NULL), |
| 48 | shm_len_(0) {} |
| 49 | |
| 50 | CoverageRecord::~CoverageRecord() { |
| 51 | if (shm_) { |
| 52 | munmap((void*)shm_, shm_len_); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | Result<void> CoverageRecord::Rpc(coverage_client_req* req, int req_fd, coverage_client_resp* resp) { |
| 57 | int rc; |
| 58 | |
| 59 | if (req_fd < 0) { |
| 60 | rc = write(coverage_srv_fd_, req, sizeof(*req)); |
| 61 | } else { |
| 62 | iovec iov = { |
| 63 | .iov_base = req, |
| 64 | .iov_len = sizeof(*req), |
| 65 | }; |
| 66 | |
| 67 | trusty_shm shm = { |
| 68 | .fd = req_fd, |
| 69 | .transfer = TRUSTY_SHARE, |
| 70 | }; |
| 71 | |
| 72 | rc = tipc_send(coverage_srv_fd_, &iov, 1, &shm, 1); |
| 73 | } |
| 74 | |
| 75 | if (rc != (int)sizeof(*req)) { |
| 76 | return ErrnoError() << "failed to send request to coverage server: "; |
| 77 | } |
| 78 | |
| 79 | rc = read(coverage_srv_fd_, resp, sizeof(*resp)); |
| 80 | if (rc != (int)sizeof(*resp)) { |
| 81 | return ErrnoError() << "failed to read reply from coverage server: "; |
| 82 | } |
| 83 | |
| 84 | if (resp->hdr.cmd != (req->hdr.cmd | COVERAGE_CLIENT_CMD_RESP_BIT)) { |
| 85 | return ErrnoError() << "unknown response cmd: " << resp->hdr.cmd; |
| 86 | } |
| 87 | |
| 88 | return {}; |
| 89 | } |
| 90 | |
| 91 | Result<void> CoverageRecord::Open() { |
| 92 | coverage_client_req req; |
| 93 | coverage_client_resp resp; |
| 94 | |
| 95 | if (shm_) { |
| 96 | return {}; /* already initialized */ |
| 97 | } |
| 98 | |
| 99 | int fd = tipc_connect(tipc_dev_.c_str(), COVERAGE_CLIENT_PORT); |
| 100 | if (fd < 0) { |
| 101 | return ErrnoError() << "failed to connect to Trusty coverarge server: "; |
| 102 | } |
| 103 | coverage_srv_fd_.reset(fd); |
| 104 | |
| 105 | req.hdr.cmd = COVERAGE_CLIENT_CMD_OPEN; |
| 106 | req.open_args.uuid = uuid_; |
| 107 | auto ret = Rpc(&req, -1, &resp); |
| 108 | if (!ret.ok()) { |
| 109 | return Error() << "failed to open coverage client: "; |
| 110 | } |
| 111 | record_len_ = resp.open_args.record_len; |
| 112 | shm_len_ = RoundPageUp(record_len_); |
| 113 | |
| 114 | fd = memfd_create("trusty-coverage", 0); |
| 115 | if (fd < 0) { |
| 116 | return ErrnoError() << "failed to create memfd: "; |
| 117 | } |
| 118 | unique_fd memfd(fd); |
| 119 | |
| 120 | if (ftruncate(memfd, shm_len_) < 0) { |
| 121 | return ErrnoError() << "failed to resize memfd: "; |
| 122 | } |
| 123 | |
| 124 | void* shm = mmap(0, shm_len_, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, 0); |
| 125 | if (shm == MAP_FAILED) { |
| 126 | return ErrnoError() << "failed to map memfd: "; |
| 127 | } |
| 128 | |
| 129 | req.hdr.cmd = COVERAGE_CLIENT_CMD_SHARE_RECORD; |
| 130 | req.share_record_args.shm_len = shm_len_; |
| 131 | ret = Rpc(&req, memfd, &resp); |
| 132 | if (!ret.ok()) { |
| 133 | return Error() << "failed to send shared memory: "; |
| 134 | } |
| 135 | |
| 136 | shm_ = shm; |
| 137 | return {}; |
| 138 | } |
| 139 | |
| 140 | void CoverageRecord::Reset() { |
| 141 | for (size_t i = 0; i < shm_len_; i++) { |
| 142 | *((volatile uint8_t*)shm_ + i) = 0; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void CoverageRecord::GetRawData(volatile void** begin, volatile void** end) { |
| 147 | assert(shm_); |
| 148 | |
| 149 | *begin = shm_; |
| 150 | *end = (uint8_t*)(*begin) + record_len_; |
| 151 | } |
| 152 | |
| 153 | uint64_t CoverageRecord::CountEdges() { |
| 154 | assert(shm_); |
| 155 | |
| 156 | uint64_t counter = 0; |
| 157 | |
| 158 | volatile uint8_t* begin = NULL; |
| 159 | volatile uint8_t* end = NULL; |
| 160 | |
| 161 | GetRawData((volatile void**)&begin, (volatile void**)&end); |
| 162 | |
| 163 | for (volatile uint8_t* x = begin; x < end; x++) { |
| 164 | counter += *x; |
| 165 | } |
| 166 | |
| 167 | return counter; |
| 168 | } |
| 169 | |
| 170 | } // namespace coverage |
| 171 | } // namespace trusty |
| 172 | } // namespace android |