blob: 57b702510da72bb2419fd8de355971cafa489e02 [file] [log] [blame]
Snehaled34b6e2023-06-30 14:49:48 +00001/*
2 * Copyright (C) 2023 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 "line-coverage"
18
19#include <BufferAllocator/BufferAllocator.h>
20#include <android-base/file.h>
21#include <android-base/logging.h>
22#include <android-base/unique_fd.h>
23#include <assert.h>
24#include <log/log.h>
25#include <stdio.h>
26#include <sys/mman.h>
27#include <sys/uio.h>
28#include <trusty/line-coverage/coverage.h>
29#include <trusty/line-coverage/tipc.h>
30#include <trusty/tipc.h>
31#include <iostream>
32
33#define LINE_COVERAGE_CLIENT_PORT "com.android.trusty.linecoverage.client"
34
35struct control {
36 /* Written by controller, read by instrumented TA */
37 uint64_t cntrl_flags;
38
39 /* Written by instrumented TA, read by controller */
40 uint64_t oper_flags;
41 uint64_t write_buffer_start_count;
42 uint64_t write_buffer_complete_count;
43};
44
45namespace android {
46namespace trusty {
47namespace line_coverage {
48
49using ::android::base::ErrnoError;
50using ::android::base::Error;
51using ::std::string;
52
53static inline uintptr_t RoundPageUp(uintptr_t val) {
54 return (val + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
55}
56
57CoverageRecord::CoverageRecord(string tipc_dev, struct uuid* uuid)
58 : tipc_dev_(std::move(tipc_dev)),
59 coverage_srv_fd_(-1),
60 uuid_(*uuid),
61 record_len_(0),
62 shm_(NULL),
63 shm_len_(0) {}
64
65CoverageRecord::~CoverageRecord() {
66 if (shm_) {
67 munmap((void*)shm_, shm_len_);
68 }
69}
70
71volatile void *CoverageRecord::getShm() {
72 if(!IsOpen()) {
73 fprintf(stderr, "Warning! SHM is NULL!\n");
74 }
75 return shm_;
76}
77
78Result<void> CoverageRecord::Rpc(struct line_coverage_client_req* req, \
79 int req_fd, \
80 struct line_coverage_client_resp* resp) {
81 int rc;
82
83 if (req_fd < 0) {
84 rc = write(coverage_srv_fd_, req, sizeof(*req));
85 } else {
86 iovec iov = {
87 .iov_base = req,
88 .iov_len = sizeof(*req),
89 };
90
91 trusty_shm shm = {
92 .fd = req_fd,
93 .transfer = TRUSTY_SHARE,
94 };
95
96 rc = tipc_send(coverage_srv_fd_, &iov, 1, &shm, 1);
97 }
98
99 if (rc != (int)sizeof(*req)) {
100 return ErrnoError() << "failed to send request to coverage server: ";
101 }
102
103 rc = read(coverage_srv_fd_, resp, sizeof(*resp));
104 if (rc != (int)sizeof(*resp)) {
105 return ErrnoError() << "failed to read reply from coverage server: ";
106 }
107
108 if (resp->hdr.cmd != (req->hdr.cmd | LINE_COVERAGE_CLIENT_CMD_RESP_BIT)) {
109 return ErrnoError() << "unknown response cmd: " << resp->hdr.cmd;
110 }
111
112 return {};
113}
114
115Result<void> CoverageRecord::Open(int fd) {
116 struct line_coverage_client_req req;
117 struct line_coverage_client_resp resp;
118
119 if (shm_) {
120 return {}; /* already initialized */
121 }
122
123 coverage_srv_fd_= fd;
124
125 req.hdr.cmd = LINE_COVERAGE_CLIENT_CMD_OPEN;
126 req.open_args.uuid = uuid_;
127 auto ret = Rpc(&req, -1, &resp);
128 if (!ret.ok()) {
129 return Error() << "failed to open coverage client: " << ret.error();
130 }
131 record_len_ = resp.open_args.record_len;
132 shm_len_ = RoundPageUp(record_len_);
133
134 BufferAllocator allocator;
135
136 fd = allocator.Alloc("system", shm_len_);
137 if (fd < 0) {
138 return ErrnoError() << "failed to create dmabuf of size " << shm_len_
139 << " err code: " << fd;
140 }
141 unique_fd dma_buf(fd);
142
143 void* shm = mmap(0, shm_len_, PROT_READ | PROT_WRITE, MAP_SHARED, dma_buf, 0);
144 if (shm == MAP_FAILED) {
145 return ErrnoError() << "failed to map memfd: ";
146 }
147
148 req.hdr.cmd = LINE_COVERAGE_CLIENT_CMD_SHARE_RECORD;
149 req.share_record_args.shm_len = shm_len_;
150 ret = Rpc(&req, dma_buf, &resp);
151 if (!ret.ok()) {
152 return Error() << "failed to send shared memory: " << ret.error();
153 }
154
155 shm_ = shm;
156
157 req.hdr.cmd = LINE_COVERAGE_CLIENT_CMD_OPEN;
158 req.open_args.uuid = uuid_;
159 ret = Rpc(&req, -1, &resp);
160 if (!ret.ok()) {
161 return Error() << "failed to open coverage client: " << ret.error();
162 }
163
164 return {};
165}
166
167bool CoverageRecord::IsOpen() {
168 return shm_;
169}
170
171Result<void> CoverageRecord::SaveFile(const std::string& filename) {
172 if(!IsOpen()) {
173 return ErrnoError() << "Warning! SHM is NULL!";
174 }
175 android::base::unique_fd output_fd(TEMP_FAILURE_RETRY(creat(filename.c_str(), 00644)));
176 if (!output_fd.ok()) {
177 return ErrnoError() << "Could not open output file";
178 }
179
180 uintptr_t* begin = (uintptr_t*)((char *)shm_ + sizeof(struct control));
181 bool ret = WriteFully(output_fd, begin, record_len_);
182 if(!ret) {
183 fprintf(stderr, "Coverage write to file failed\n");
184 }
185
186 return {};
187}
188
189} // namespace line_coverage
190} // namespace trusty
191} // namespace android