blob: f383dd14aeea9a380fcf6bdd3f2886960ed0be3a [file] [log] [blame]
Tri Vo5b40e892020-11-01 13:01:29 -08001/*
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
Stephen Cranee9629302020-11-16 18:00:53 -080019#include <android-base/file.h>
Tri Vo5b40e892020-11-01 13:01:29 -080020#include <android-base/logging.h>
21#include <android-base/unique_fd.h>
22#include <assert.h>
Stephen Cranee9629302020-11-16 18:00:53 -080023#include <stdio.h>
Tri Vo5b40e892020-11-01 13:01:29 -080024#include <sys/mman.h>
25#include <sys/uio.h>
26#include <trusty/coverage/coverage.h>
Stephen Cranee9629302020-11-16 18:00:53 -080027#include <trusty/coverage/record.h>
Tri Vo5b40e892020-11-01 13:01:29 -080028#include <trusty/coverage/tipc.h>
29#include <trusty/tipc.h>
30
31#define COVERAGE_CLIENT_PORT "com.android.trusty.coverage.client"
32
33namespace android {
34namespace trusty {
35namespace coverage {
36
37using android::base::ErrnoError;
38using android::base::Error;
39using std::string;
40
41static inline uintptr_t RoundPageUp(uintptr_t val) {
42 return (val + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
43}
44
45CoverageRecord::CoverageRecord(string tipc_dev, struct uuid* uuid)
46 : tipc_dev_(std::move(tipc_dev)),
47 coverage_srv_fd_(-1),
48 uuid_(*uuid),
49 record_len_(0),
50 shm_(NULL),
51 shm_len_(0) {}
52
53CoverageRecord::~CoverageRecord() {
54 if (shm_) {
55 munmap((void*)shm_, shm_len_);
56 }
57}
58
59Result<void> CoverageRecord::Rpc(coverage_client_req* req, int req_fd, coverage_client_resp* resp) {
60 int rc;
61
62 if (req_fd < 0) {
63 rc = write(coverage_srv_fd_, req, sizeof(*req));
64 } else {
65 iovec iov = {
66 .iov_base = req,
67 .iov_len = sizeof(*req),
68 };
69
70 trusty_shm shm = {
71 .fd = req_fd,
72 .transfer = TRUSTY_SHARE,
73 };
74
75 rc = tipc_send(coverage_srv_fd_, &iov, 1, &shm, 1);
76 }
77
78 if (rc != (int)sizeof(*req)) {
79 return ErrnoError() << "failed to send request to coverage server: ";
80 }
81
82 rc = read(coverage_srv_fd_, resp, sizeof(*resp));
83 if (rc != (int)sizeof(*resp)) {
84 return ErrnoError() << "failed to read reply from coverage server: ";
85 }
86
87 if (resp->hdr.cmd != (req->hdr.cmd | COVERAGE_CLIENT_CMD_RESP_BIT)) {
88 return ErrnoError() << "unknown response cmd: " << resp->hdr.cmd;
89 }
90
91 return {};
92}
93
94Result<void> CoverageRecord::Open() {
95 coverage_client_req req;
96 coverage_client_resp resp;
97
98 if (shm_) {
99 return {}; /* already initialized */
100 }
101
102 int fd = tipc_connect(tipc_dev_.c_str(), COVERAGE_CLIENT_PORT);
103 if (fd < 0) {
104 return ErrnoError() << "failed to connect to Trusty coverarge server: ";
105 }
106 coverage_srv_fd_.reset(fd);
107
108 req.hdr.cmd = COVERAGE_CLIENT_CMD_OPEN;
109 req.open_args.uuid = uuid_;
110 auto ret = Rpc(&req, -1, &resp);
111 if (!ret.ok()) {
112 return Error() << "failed to open coverage client: ";
113 }
114 record_len_ = resp.open_args.record_len;
115 shm_len_ = RoundPageUp(record_len_);
116
117 fd = memfd_create("trusty-coverage", 0);
118 if (fd < 0) {
119 return ErrnoError() << "failed to create memfd: ";
120 }
121 unique_fd memfd(fd);
122
123 if (ftruncate(memfd, shm_len_) < 0) {
124 return ErrnoError() << "failed to resize memfd: ";
125 }
126
127 void* shm = mmap(0, shm_len_, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, 0);
128 if (shm == MAP_FAILED) {
129 return ErrnoError() << "failed to map memfd: ";
130 }
131
132 req.hdr.cmd = COVERAGE_CLIENT_CMD_SHARE_RECORD;
133 req.share_record_args.shm_len = shm_len_;
134 ret = Rpc(&req, memfd, &resp);
135 if (!ret.ok()) {
136 return Error() << "failed to send shared memory: ";
137 }
138
139 shm_ = shm;
140 return {};
141}
142
Stephen Cranee9629302020-11-16 18:00:53 -0800143void CoverageRecord::ResetFullRecord() {
144 auto header_region = GetRegionBounds(COV_START);
Bernie Innocenti62ba2b12020-12-19 21:17:16 +0900145 if (!header_region.ok()) {
Stephen Cranee9629302020-11-16 18:00:53 -0800146 // If the header cannot be parsed, we can't reset the proper region yet.
147 return;
148 }
149
150 for (size_t i = header_region->second; i < shm_len_; i++) {
Tri Vo5b40e892020-11-01 13:01:29 -0800151 *((volatile uint8_t*)shm_ + i) = 0;
152 }
153}
154
Stephen Cranee9629302020-11-16 18:00:53 -0800155void CoverageRecord::ResetCounts() {
156 volatile uint8_t* begin = nullptr;
157 volatile uint8_t* end = nullptr;
158 GetRawCounts(&begin, &end);
159
160 for (volatile uint8_t* x = begin; x < end; x++) {
161 *x = 0;
162 }
163}
164
165void CoverageRecord::ResetPCs() {
166 volatile uintptr_t* begin = nullptr;
167 volatile uintptr_t* end = nullptr;
168 GetRawPCs(&begin, &end);
169
170 for (volatile uintptr_t* x = begin; x < end; x++) {
171 *x = 0;
172 }
173}
174
175Result<std::pair<size_t, size_t>> CoverageRecord::GetRegionBounds(uint32_t region_type) {
176 assert(shm_);
177
178 auto header = (volatile struct coverage_record_header*)shm_;
179
180 if (header->type != COV_START) {
181 return Error() << "Header not yet valid";
182 }
183
184 for (++header; header->type != COV_TOTAL_LENGTH; ++header) {
185 if (header->type == region_type) {
186 // Coverage record must end with a COV_TOTAL_LENGTH header entry, so
187 // it is always safe to read the next entry since we don't iterate
188 // over the COV_TOTAL_LENGTH entry.
189 return {{header->offset, (header + 1)->offset}};
190 }
191 }
192
193 return Error() << "Could not find coverage region type: " << region_type;
194}
195
Tri Vo5b40e892020-11-01 13:01:29 -0800196void CoverageRecord::GetRawData(volatile void** begin, volatile void** end) {
197 assert(shm_);
198
199 *begin = shm_;
200 *end = (uint8_t*)(*begin) + record_len_;
201}
202
Stephen Cranee9629302020-11-16 18:00:53 -0800203void CoverageRecord::GetRawCounts(volatile uint8_t** begin, volatile uint8_t** end) {
204 auto region = GetRegionBounds(COV_8BIT_COUNTERS);
Bernie Innocenti62ba2b12020-12-19 21:17:16 +0900205 if (!region.ok()) {
Stephen Cranee9629302020-11-16 18:00:53 -0800206 *begin = 0;
207 *end = 0;
208 return;
209 }
210
211 assert(region->second <= record_len_);
212
213 *begin = (volatile uint8_t*)shm_ + region->first;
214 *end = (volatile uint8_t*)shm_ + region->second;
215}
216
217void CoverageRecord::GetRawPCs(volatile uintptr_t** begin, volatile uintptr_t** end) {
218 auto region = GetRegionBounds(COV_INSTR_PCS);
Bernie Innocenti62ba2b12020-12-19 21:17:16 +0900219 if (!region.ok()) {
Stephen Cranee9629302020-11-16 18:00:53 -0800220 *begin = 0;
221 *end = 0;
222 return;
223 }
224
225 assert(region->second <= record_len_);
226
227 *begin = (volatile uintptr_t*)((volatile uint8_t*)shm_ + region->first);
228 *end = (volatile uintptr_t*)((volatile uint8_t*)shm_ + region->second);
229}
230
231uint64_t CoverageRecord::TotalEdgeCounts() {
Tri Vo5b40e892020-11-01 13:01:29 -0800232 assert(shm_);
233
234 uint64_t counter = 0;
235
236 volatile uint8_t* begin = NULL;
237 volatile uint8_t* end = NULL;
238
Stephen Cranee9629302020-11-16 18:00:53 -0800239 GetRawCounts(&begin, &end);
Tri Vo5b40e892020-11-01 13:01:29 -0800240
241 for (volatile uint8_t* x = begin; x < end; x++) {
242 counter += *x;
243 }
244
245 return counter;
246}
247
Stephen Cranee9629302020-11-16 18:00:53 -0800248Result<void> CoverageRecord::SaveSancovFile(const std::string& filename) {
249 android::base::unique_fd output_fd(TEMP_FAILURE_RETRY(creat(filename.c_str(), 00644)));
250 if (!output_fd.ok()) {
251 return ErrnoError() << "Could not open sancov file";
252 }
253
254 uint64_t magic;
255 if (sizeof(uintptr_t) == 8) {
256 magic = 0xC0BFFFFFFFFFFF64;
257 } else if (sizeof(uintptr_t) == 4) {
258 magic = 0xC0BFFFFFFFFFFF32;
259 }
260 WriteFully(output_fd, &magic, sizeof(magic));
261
262 volatile uintptr_t* begin = nullptr;
263 volatile uintptr_t* end = nullptr;
264
265 GetRawPCs(&begin, &end);
266
267 for (volatile uintptr_t* pc_ptr = begin; pc_ptr < end; pc_ptr++) {
268 uintptr_t pc = *pc_ptr;
269 if (pc) {
270 WriteFully(output_fd, &pc, sizeof(pc));
271 }
272 }
273
274 return {};
275}
276
Tri Vo5b40e892020-11-01 13:01:29 -0800277} // namespace coverage
278} // namespace trusty
279} // namespace android