blob: bb096beab77915d6b77fde6ec0d9edc615bab7d8 [file] [log] [blame]
Tri Voe8823ff2020-10-13 21:59:07 -07001/*
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 "trusty-fuzz-utils"
18
19#include <trusty/fuzz/utils.h>
20
21#include <android-base/logging.h>
22#include <android-base/unique_fd.h>
23#include <linux/ioctl.h>
24#include <linux/types.h>
25#include <linux/uio.h>
Stephen Crane5e3a3ce2020-10-30 15:04:38 -070026#include <log/log_read.h>
27#include <time.h>
Tri Vo90c0e832020-11-18 16:17:09 -080028#include <trusty/tipc.h>
Stephen Crane5e3a3ce2020-10-30 15:04:38 -070029#include <iostream>
Tri Voe8823ff2020-10-13 21:59:07 -070030
31using android::base::ErrnoError;
32using android::base::Error;
33using android::base::Result;
34using android::base::unique_fd;
35
Stephen Crane5e3a3ce2020-10-30 15:04:38 -070036namespace {
37
38const size_t kTimeoutSeconds = 5;
39const std::string kTrustyLogTag = "trusty-log";
40
41const time_t kInitialTime = time(nullptr);
42
43void PrintTrustyLog() {
44 auto logger_list = android_logger_list_open(LOG_ID_KERNEL, ANDROID_LOG_NONBLOCK, 1000, 0);
45 if (logger_list == nullptr) {
46 std::cerr << "Could not open android kernel log\n";
47 return;
48 }
49
50 while (true) {
51 log_msg log_msg;
52 int rc = android_logger_list_read(logger_list, &log_msg);
53 if (rc < 0) {
54 break;
55 }
56 if (log_msg.entry.sec < kInitialTime) {
57 continue;
58 }
59 char* msg = log_msg.msg();
60 if (msg) {
61 std::string line(msg, log_msg.entry.len);
62 if (line.find(kTrustyLogTag) != std::string::npos) {
63 std::cerr << line.substr(kTrustyLogTag.length() + 2) << std::endl;
64 }
65 }
66 }
67
68 android_logger_list_free(logger_list);
69}
70
71} // namespace
Tri Voe8823ff2020-10-13 21:59:07 -070072
73namespace android {
74namespace trusty {
75namespace fuzz {
76
77TrustyApp::TrustyApp(std::string tipc_dev, std::string ta_port)
78 : tipc_dev_(tipc_dev), ta_port_(ta_port), ta_fd_(-1) {}
79
80Result<void> TrustyApp::Connect() {
Tri Voe8823ff2020-10-13 21:59:07 -070081 alarm(kTimeoutSeconds);
Tri Vo90c0e832020-11-18 16:17:09 -080082 int fd = tipc_connect(tipc_dev_.c_str(), ta_port_.c_str());
Tri Voe8823ff2020-10-13 21:59:07 -070083 alarm(0);
84 if (fd < 0) {
85 return ErrnoError() << "failed to open TIPC device: ";
86 }
87 ta_fd_.reset(fd);
88
Tri Voe8823ff2020-10-13 21:59:07 -070089 return {};
90}
91
92Result<void> TrustyApp::Read(void* buf, size_t len) {
93 if (ta_fd_ == -1) {
94 return Error() << "TA is not connected to yet: ";
95 }
96
97 alarm(kTimeoutSeconds);
98 int rc = read(ta_fd_, buf, len);
99 alarm(0);
100 if (rc < 0) {
101 return Error() << "failed to read TIPC message from TA: ";
102 }
103
104 return {};
105}
106
107Result<void> TrustyApp::Write(const void* buf, size_t len) {
108 if (ta_fd_ == -1) {
109 return Error() << "TA is not connected to yet: ";
110 }
111
112 alarm(kTimeoutSeconds);
113 int rc = write(ta_fd_, buf, len);
114 alarm(0);
115 if (rc < 0) {
Tri Voce812a22021-02-10 13:31:22 -0800116 return Error() << "failed to write TIPC message to TA: ";
Tri Voe8823ff2020-10-13 21:59:07 -0700117 }
118
119 return {};
120}
121
122Result<int> TrustyApp::GetRawFd() {
123 if (ta_fd_ == -1) {
124 return Error() << "TA is not connected to yet: ";
125 }
126
127 return ta_fd_;
128}
129
Stephen Cranee54e8d42021-04-14 20:57:50 +0000130void TrustyApp::Disconnect() {
131 ta_fd_.reset();
132}
133
Stephen Crane5e3a3ce2020-10-30 15:04:38 -0700134void Abort() {
135 PrintTrustyLog();
136 exit(-1);
137}
138
Tri Voe8823ff2020-10-13 21:59:07 -0700139} // namespace fuzz
140} // namespace trusty
141} // namespace android