blob: 13eafce2c6f193efa7424e2530c343e67b7ed386 [file] [log] [blame]
Jiyong Parka7266ac2021-05-17 21:57:24 +09001/*
2 * Copyright (C) 2021 The Android Open Source 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 */
Alan Stokesd1a30dd2022-11-30 09:39:54 +000016
Inseob Kim06a64d62021-09-07 21:21:45 +090017#include <aidl/com/android/microdroid/testservice/BnTestService.h>
Alan Stokes63fa37b2023-02-22 14:56:57 +000018#include <aidl/com/android/microdroid/testservice/BnVmCallback.h>
19#include <aidl/com/android/microdroid/testservice/IAppCallback.h>
Inseob Kimdb319702022-01-20 13:12:43 +090020#include <android-base/file.h>
Inseob Kim691df6a2022-01-20 12:54:30 +090021#include <android-base/properties.h>
Andrew Scull11cf0902021-06-22 12:08:10 +000022#include <android-base/result.h>
Nikita Ioffe3452ee22022-12-15 00:31:56 +000023#include <android-base/scopeguard.h>
Alan Stokesd1a30dd2022-11-30 09:39:54 +000024#include <android/log.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090025#include <fcntl.h>
Nikita Ioffe29e15c82023-02-25 02:31:51 +000026#include <fstab/fstab.h>
Inseob Kimdb319702022-01-20 13:12:43 +090027#include <fsverity_digests.pb.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090028#include <linux/vm_sockets.h>
29#include <stdint.h>
Jiyong Parka7266ac2021-05-17 21:57:24 +090030#include <stdio.h>
Nikita Ioffe3452ee22022-12-15 00:31:56 +000031#include <sys/capability.h>
Shraddha Basantwania9a9c4f2025-02-25 09:51:48 -080032#include <sys/statvfs.h>
Jiyong Park23934392021-06-16 01:59:10 +090033#include <sys/system_properties.h>
Nikita Ioffeaa9f6032025-01-21 18:06:46 +000034#ifdef __MICRODROID_TEST_PAYLOAD_USES_LIBICU__
35#include <unicode/uchar.h>
36#endif
Inseob Kim06a64d62021-09-07 21:21:45 +090037#include <unistd.h>
Alan Stokes52d3c722022-10-04 17:27:13 +010038#include <vm_main.h>
Alan Stokesd4ea5a82022-11-10 12:17:42 +000039#include <vm_payload_restricted.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090040
Nikita Ioffed5846dc2024-11-01 18:44:45 +000041#include <cstdint>
Inseob Kim691df6a2022-01-20 12:54:30 +090042#include <string>
Alan Stokesd1a30dd2022-11-30 09:39:54 +000043#include <thread>
Jiyong Parka7266ac2021-05-17 21:57:24 +090044
Alan Stokesd1a30dd2022-11-30 09:39:54 +000045using android::base::borrowed_fd;
Inseob Kim06a64d62021-09-07 21:21:45 +090046using android::base::ErrnoError;
Andrew Scull11cf0902021-06-22 12:08:10 +000047using android::base::Error;
Nikita Ioffe3452ee22022-12-15 00:31:56 +000048using android::base::make_scope_guard;
Andrew Scull11cf0902021-06-22 12:08:10 +000049using android::base::Result;
Alan Stokesd1a30dd2022-11-30 09:39:54 +000050using android::base::unique_fd;
Nikita Ioffe29e15c82023-02-25 02:31:51 +000051using android::fs_mgr::Fstab;
52using android::fs_mgr::FstabEntry;
53using android::fs_mgr::GetEntryForMountPoint;
54using android::fs_mgr::ReadFstabFromFile;
Alan Stokesd1a30dd2022-11-30 09:39:54 +000055
56using aidl::com::android::microdroid::testservice::BnTestService;
Alan Stokes63fa37b2023-02-22 14:56:57 +000057using aidl::com::android::microdroid::testservice::BnVmCallback;
58using aidl::com::android::microdroid::testservice::IAppCallback;
Alan Stokesd1a30dd2022-11-30 09:39:54 +000059using ndk::ScopedAStatus;
Andrew Scull66616612021-06-17 16:41:03 +000060
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090061extern void testlib_sub();
62
Andrew Scull66616612021-06-17 16:41:03 +000063namespace {
64
Alan Stokesd1a30dd2022-11-30 09:39:54 +000065constexpr char TAG[] = "testbinary";
66
Andrew Scull11cf0902021-06-22 12:08:10 +000067template <typename T>
Andrew Sculledbe75e2021-07-06 10:44:31 +000068Result<T> report_test(std::string name, Result<T> result) {
Andrew Scull11cf0902021-06-22 12:08:10 +000069 auto property = "debug.microdroid.test." + name;
70 std::stringstream outcome;
71 if (result.ok()) {
72 outcome << "PASS";
73 } else {
74 outcome << "FAIL: " << result.error();
Alan Stokesd1a30dd2022-11-30 09:39:54 +000075 // Log the error in case the property is truncated.
76 std::string message = name + ": " + outcome.str();
77 __android_log_write(ANDROID_LOG_WARN, TAG, message.c_str());
Andrew Scull11cf0902021-06-22 12:08:10 +000078 }
79 __system_property_set(property.c_str(), outcome.str().c_str());
Andrew Sculledbe75e2021-07-06 10:44:31 +000080 return result;
Andrew Scull66616612021-06-17 16:41:03 +000081}
82
Alan Stokesd1a30dd2022-11-30 09:39:54 +000083Result<void> run_echo_reverse_server(borrowed_fd listening_fd) {
84 struct sockaddr_vm client_sa = {};
85 socklen_t client_sa_len = sizeof(client_sa);
86 unique_fd connect_fd{accept4(listening_fd.get(), (struct sockaddr*)&client_sa, &client_sa_len,
87 SOCK_CLOEXEC)};
88 if (!connect_fd.ok()) {
89 return ErrnoError() << "Failed to accept vsock connection";
90 }
91
92 unique_fd input_fd{fcntl(connect_fd, F_DUPFD_CLOEXEC, 0)};
93 if (!input_fd.ok()) {
94 return ErrnoError() << "Failed to dup";
95 }
96 FILE* input = fdopen(input_fd.release(), "r");
97 if (!input) {
98 return ErrnoError() << "Failed to fdopen";
99 }
100
David Brazdilbcce3512023-02-23 15:32:55 +0000101 // Run forever, reverse one line at a time.
102 while (true) {
103 char* line = nullptr;
104 size_t size = 0;
105 if (getline(&line, &size, input) < 0) {
Jiyong Park2ff07882024-12-17 11:30:28 +0900106 if (errno == 0) {
107 return {}; // the input was closed
108 }
David Brazdilbcce3512023-02-23 15:32:55 +0000109 return ErrnoError() << "Failed to read";
110 }
111
112 std::string_view original = line;
113 if (!original.empty() && original.back() == '\n') {
114 original = original.substr(0, original.size() - 1);
115 }
116
117 std::string reversed(original.rbegin(), original.rend());
118 reversed += "\n";
119
120 if (write(connect_fd, reversed.data(), reversed.size()) < 0) {
121 return ErrnoError() << "Failed to write";
122 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000123 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000124}
125
126Result<void> start_echo_reverse_server() {
127 unique_fd server_fd{TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0))};
128 if (!server_fd.ok()) {
129 return ErrnoError() << "Failed to create vsock socket";
130 }
131 struct sockaddr_vm server_sa = (struct sockaddr_vm){
132 .svm_family = AF_VSOCK,
Alan Stokes10c47672022-12-13 17:17:08 +0000133 .svm_port = static_cast<uint32_t>(BnTestService::ECHO_REVERSE_PORT),
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000134 .svm_cid = VMADDR_CID_ANY,
135 };
136 int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr*)&server_sa, sizeof(server_sa)));
137 if (ret < 0) {
138 return ErrnoError() << "Failed to bind vsock socket";
139 }
140 ret = TEMP_FAILURE_RETRY(listen(server_fd, /*backlog=*/1));
141 if (ret < 0) {
142 return ErrnoError() << "Failed to listen";
143 }
144
145 std::thread accept_thread{[listening_fd = std::move(server_fd)] {
Jiyong Park2ff07882024-12-17 11:30:28 +0900146 Result<void> result;
147 while ((result = run_echo_reverse_server(listening_fd)).ok()) {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000148 }
Jiyong Park2ff07882024-12-17 11:30:28 +0900149 __android_log_write(ANDROID_LOG_ERROR, TAG, result.error().message().c_str());
150 // Make sure the VM exits so the test will fail solidly
151 exit(1);
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000152 }};
153 accept_thread.detach();
154
155 return {};
156}
157
Inseob Kim06a64d62021-09-07 21:21:45 +0900158Result<void> start_test_service() {
Alan Stokes63fa37b2023-02-22 14:56:57 +0000159 class VmCallbackImpl : public BnVmCallback {
160 private:
161 std::shared_ptr<IAppCallback> mAppCallback;
162
163 public:
164 explicit VmCallbackImpl(const std::shared_ptr<IAppCallback>& appCallback)
165 : mAppCallback(appCallback) {}
166
167 ScopedAStatus echoMessage(const std::string& message) override {
168 std::thread callback_thread{[=, appCallback = mAppCallback] {
169 appCallback->onEchoRequestReceived("Received: " + message);
170 }};
171 callback_thread.detach();
172 return ScopedAStatus::ok();
173 }
174 };
175
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000176 class TestService : public BnTestService {
Alan Stokes63fa37b2023-02-22 14:56:57 +0000177 public:
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000178 ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override {
Inseob Kim06a64d62021-09-07 21:21:45 +0900179 *out = a + b;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000180 return ScopedAStatus::ok();
Inseob Kim06a64d62021-09-07 21:21:45 +0900181 }
Inseob Kim691df6a2022-01-20 12:54:30 +0900182
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000183 ScopedAStatus readProperty(const std::string& prop, std::string* out) override {
Inseob Kim691df6a2022-01-20 12:54:30 +0900184 *out = android::base::GetProperty(prop, "");
185 if (out->empty()) {
186 std::string msg = "cannot find property " + prop;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000187 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
188 msg.c_str());
Inseob Kim691df6a2022-01-20 12:54:30 +0900189 }
190
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000191 return ScopedAStatus::ok();
Inseob Kim691df6a2022-01-20 12:54:30 +0900192 }
Andrew Scull2e6ab792022-01-30 16:04:08 +0000193
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000194 ScopedAStatus insecurelyExposeVmInstanceSecret(std::vector<uint8_t>* out) override {
Andrew Scull102067a2022-10-07 00:34:40 +0000195 const uint8_t identifier[] = {1, 2, 3, 4};
Andrew Scull655e98e2022-10-10 22:24:58 +0000196 out->resize(32);
Alan Stokes65bbb912022-11-23 09:39:34 +0000197 AVmPayload_getVmInstanceSecret(identifier, sizeof(identifier), out->data(),
198 out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000199 return ScopedAStatus::ok();
Andrew Scull2e6ab792022-01-30 16:04:08 +0000200 }
Andrew Scull1f6ca352022-02-20 22:51:12 +0000201
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000202 ScopedAStatus insecurelyExposeAttestationCdi(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000203 size_t cdi_size = AVmPayload_getDiceAttestationCdi(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000204 out->resize(cdi_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000205 AVmPayload_getDiceAttestationCdi(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000206 return ScopedAStatus::ok();
Andrew Scull1f6ca352022-02-20 22:51:12 +0000207 }
Andrew Scull61892082022-02-21 00:07:25 +0000208
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000209 ScopedAStatus getBcc(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000210 size_t bcc_size = AVmPayload_getDiceAttestationChain(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000211 out->resize(bcc_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000212 AVmPayload_getDiceAttestationChain(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000213 return ScopedAStatus::ok();
Andrew Scull61892082022-02-21 00:07:25 +0000214 }
Alice Wang6bbb6da2022-10-26 12:44:06 +0000215
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000216 ScopedAStatus getApkContentsPath(std::string* out) override {
Alice Wang6bbb6da2022-10-26 12:44:06 +0000217 const char* path_c = AVmPayload_getApkContentsPath();
218 if (path_c == nullptr) {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000219 return ScopedAStatus::
Alice Wang6bbb6da2022-10-26 12:44:06 +0000220 fromServiceSpecificErrorWithMessage(0, "Failed to get APK contents path");
221 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000222 *out = path_c;
223 return ScopedAStatus::ok();
Alice Wang6bbb6da2022-10-26 12:44:06 +0000224 }
Alan Stokes78d24702022-11-21 15:28:31 +0000225
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000226 ScopedAStatus getEncryptedStoragePath(std::string* out) override {
Alan Stokes78d24702022-11-21 15:28:31 +0000227 const char* path_c = AVmPayload_getEncryptedStoragePath();
228 if (path_c == nullptr) {
229 out->clear();
230 } else {
231 *out = path_c;
232 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000233 return ScopedAStatus::ok();
234 }
235
Shraddha Basantwania9a9c4f2025-02-25 09:51:48 -0800236 ScopedAStatus getEncryptedStorageSize(int64_t *out) override {
237 const char* path_c = AVmPayload_getEncryptedStoragePath();
238 if (path_c == nullptr) {
239 *out = 0;
240 return ScopedAStatus::ok();
241 }
242 struct statvfs buffer;
243 if (statvfs(path_c, &buffer) != 0) {
244 std::string msg = "statvfs " + std::string(path_c) + " failed : " +
245 std::strerror(errno);
246 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
247 msg.c_str());
248 }
249 *out= buffer.f_blocks * buffer.f_frsize;
250 return ScopedAStatus::ok();
251 }
252
Nikita Ioffe3452ee22022-12-15 00:31:56 +0000253 ScopedAStatus getEffectiveCapabilities(std::vector<std::string>* out) override {
254 if (out == nullptr) {
255 return ScopedAStatus::ok();
256 }
257 cap_t cap = cap_get_proc();
258 auto guard = make_scope_guard([&cap]() { cap_free(cap); });
259 for (cap_value_t cap_id = 0; cap_id < CAP_LAST_CAP + 1; cap_id++) {
260 cap_flag_value_t value;
261 if (cap_get_flag(cap, cap_id, CAP_EFFECTIVE, &value) != 0) {
262 return ScopedAStatus::
263 fromServiceSpecificErrorWithMessage(0, "cap_get_flag failed");
264 }
265 if (value == CAP_SET) {
266 // Ideally we would just send back the cap_ids, but I wasn't able to find java
267 // APIs for linux capabilities, hence we transform to the human readable name
268 // here.
269 char* name = cap_to_name(cap_id);
270 out->push_back(std::string(name) + "(" + std::to_string(cap_id) + ")");
271 }
272 }
273 return ScopedAStatus::ok();
274 }
275
Alan Stokes1294f942023-08-21 14:34:12 +0100276 ScopedAStatus getUid(int* out) override {
277 *out = getuid();
278 return ScopedAStatus::ok();
279 }
280
Alan Stokes63fa37b2023-02-22 14:56:57 +0000281 ScopedAStatus runEchoReverseServer() override {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000282 auto result = start_echo_reverse_server();
283 if (result.ok()) {
284 return ScopedAStatus::ok();
285 } else {
286 std::string message = result.error().message();
287 return ScopedAStatus::fromServiceSpecificErrorWithMessage(-1, message.c_str());
288 }
Alan Stokes78d24702022-11-21 15:28:31 +0000289 }
Shikha Panwardc11b7e2022-12-15 12:24:11 +0000290
291 ScopedAStatus writeToFile(const std::string& content, const std::string& path) override {
292 if (!android::base::WriteStringToFile(content, path)) {
293 std::string msg = "Failed to write " + content + " to file " + path +
294 ". Errono: " + std::to_string(errno);
295 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
296 msg.c_str());
297 }
Shikha Panwardc11b7e2022-12-15 12:24:11 +0000298 return ScopedAStatus::ok();
299 }
300
301 ScopedAStatus readFromFile(const std::string& path, std::string* out) override {
302 if (!android::base::ReadFileToString(path, out)) {
303 std::string msg =
304 "Failed to read " + path + " to string. Errono: " + std::to_string(errno);
305 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
306 msg.c_str());
307 }
308 return ScopedAStatus::ok();
309 }
Shikha Panwardef7ef92023-01-06 08:35:48 +0000310
Nikita Ioffea7cb3672023-02-24 23:10:34 +0000311 ScopedAStatus getFilePermissions(const std::string& path, int32_t* out) override {
312 struct stat sb;
313 if (stat(path.c_str(), &sb) != -1) {
314 *out = sb.st_mode;
315 } else {
316 std::string msg = "stat " + path + " failed : " + std::strerror(errno);
317 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
318 msg.c_str());
319 }
320 return ScopedAStatus::ok();
321 }
322
Nikita Ioffe29e15c82023-02-25 02:31:51 +0000323 ScopedAStatus getMountFlags(const std::string& mount_point, int32_t* out) override {
324 Fstab fstab;
325 if (!ReadFstabFromFile("/proc/mounts", &fstab)) {
326 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
327 "Failed to read /proc/mounts");
328 }
329 FstabEntry* entry = GetEntryForMountPoint(&fstab, mount_point);
330 if (entry == nullptr) {
331 std::string msg = mount_point + " not found in /proc/mounts";
332 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
333 msg.c_str());
334 }
335 *out = entry->flags;
336 return ScopedAStatus::ok();
337 }
338
Nikita Ioffed5846dc2024-11-01 18:44:45 +0000339 ScopedAStatus getPageSize(int32_t* out) override {
340 *out = getpagesize();
341 return ScopedAStatus::ok();
342 }
343
Alan Stokes63fa37b2023-02-22 14:56:57 +0000344 ScopedAStatus requestCallback(const std::shared_ptr<IAppCallback>& appCallback) {
345 auto vmCallback = ndk::SharedRefBase::make<VmCallbackImpl>(appCallback);
346 std::thread callback_thread{[=] { appCallback->setVmCallback(vmCallback); }};
347 callback_thread.detach();
348 return ScopedAStatus::ok();
349 }
350
Jiyong Park92e34722023-06-27 00:43:39 +0900351 ScopedAStatus readLineFromConsole(std::string* out) {
352 FILE* f = fopen("/dev/console", "r");
353 if (f == nullptr) {
354 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
355 "failed to open /dev/console");
356 }
357 char* line = nullptr;
358 size_t len = 0;
359 ssize_t nread = getline(&line, &len, f);
360
361 if (nread == -1) {
362 free(line);
363 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
364 "failed to read /dev/console");
365 }
366 out->append(line, nread);
367 free(line);
368 return ScopedAStatus::ok();
369 }
370
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000371 ScopedAStatus insecurelyReadPayloadRpData(std::array<uint8_t, 32>* out) override {
Nikita Ioffe3e3fdf02025-01-15 14:20:27 +0000372 if (__builtin_available(android 36, *)) {
373 int32_t ret = AVmPayload_readRollbackProtectedSecret(out->data(), 32);
374 if (ret != 32) {
375 return ScopedAStatus::fromServiceSpecificError(ret);
376 }
377 return ScopedAStatus::ok();
378 } else {
379 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
380 "not available before SDK 36");
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000381 }
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000382 }
383
384 ScopedAStatus insecurelyWritePayloadRpData(
385 const std::array<uint8_t, 32>& inputData) override {
Nikita Ioffe3e3fdf02025-01-15 14:20:27 +0000386 if (__builtin_available(android 36, *)) {
387 int32_t ret = AVmPayload_writeRollbackProtectedSecret(inputData.data(), 32);
388 if (ret != 32) {
389 return ScopedAStatus::fromServiceSpecificError(ret);
390 }
391 return ScopedAStatus::ok();
392 } else {
393 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
394 "not available before SDK 36");
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000395 }
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000396 }
397
Shikha Panwar5b7b4942024-12-18 15:32:49 +0000398 ScopedAStatus isNewInstance(bool* is_new_instance_out) override {
Nikita Ioffe3e3fdf02025-01-15 14:20:27 +0000399 if (__builtin_available(android 36, *)) {
400 *is_new_instance_out = AVmPayload_isNewInstance();
401 return ScopedAStatus::ok();
402 } else {
403 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
404 "not available before SDK 36");
405 }
Shikha Panwar5b7b4942024-12-18 15:32:49 +0000406 }
407
Nikita Ioffe901083f2025-01-20 01:54:28 +0000408 ScopedAStatus checkLibIcuIsAccessible() override {
Nikita Ioffeaa9f6032025-01-21 18:06:46 +0000409#ifdef __MICRODROID_TEST_PAYLOAD_USES_LIBICU__
Nikita Ioffe901083f2025-01-20 01:54:28 +0000410 static constexpr const char* kLibIcuPath = "/apex/com.android.i18n/lib64/libicu.so";
411 if (access(kLibIcuPath, R_OK) == 0) {
Nikita Ioffeaa9f6032025-01-21 18:06:46 +0000412 if (!u_hasBinaryProperty(U'❤' /* Emoji heart U+2764 */, UCHAR_EMOJI)) {
413 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
414 "libicu broken!");
415 }
Nikita Ioffe901083f2025-01-20 01:54:28 +0000416 return ScopedAStatus::ok();
417 } else {
418 std::string msg = "failed to access " + std::string(kLibIcuPath) + "(" +
419 std::to_string(errno) + ")";
420 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
421 msg.c_str());
422 }
Nikita Ioffeaa9f6032025-01-21 18:06:46 +0000423#else
424 return ScopedAStatus::
425 fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
426 "should be only used together with "
427 "MicrodroidTestNativeLibWithLibIcu.so payload");
428#endif
Nikita Ioffe901083f2025-01-20 01:54:28 +0000429 }
430
Shikha Panwardef7ef92023-01-06 08:35:48 +0000431 ScopedAStatus quit() override { exit(0); }
Inseob Kim06a64d62021-09-07 21:21:45 +0900432 };
433 auto testService = ndk::SharedRefBase::make<TestService>();
434
Alan Stokes65bbb912022-11-23 09:39:34 +0000435 auto callback = []([[maybe_unused]] void* param) { AVmPayload_notifyPayloadReady(); };
Devin Moorecaf7b952023-04-24 20:14:35 +0000436 AVmPayload_runVsockRpcServer(testService->asBinder().get(), testService->PORT, callback,
Alan Stokese0945ad2022-11-24 13:29:57 +0000437 nullptr);
Inseob Kim06a64d62021-09-07 21:21:45 +0900438
439 return {};
440}
441
Alan Stokes4b31a482024-02-01 17:56:06 +0000442Result<void> verify_build_manifest() {
Inseob Kimdb319702022-01-20 13:12:43 +0900443 const char* path = "/mnt/extra-apk/0/assets/build_manifest.pb";
444
445 std::string str;
446 if (!android::base::ReadFileToString(path, &str)) {
447 return ErrnoError() << "failed to read build_manifest.pb";
448 }
449
450 if (!android::security::fsverity::FSVerityDigests().ParseFromString(str)) {
451 return Error() << "invalid build_manifest.pb";
452 }
453
454 return {};
455}
456
Alan Stokes4b31a482024-02-01 17:56:06 +0000457Result<void> verify_vm_share() {
458 const char* path = "/mnt/extra-apk/0/assets/vmshareapp.txt";
459
460 std::string str;
461 if (!android::base::ReadFileToString(path, &str)) {
462 return ErrnoError() << "failed to read vmshareapp.txt";
463 }
464
465 return {};
466}
467
Andrew Scull66616612021-06-17 16:41:03 +0000468} // Anonymous namespace
469
Alan Stokes52d3c722022-10-04 17:27:13 +0100470extern "C" int AVmPayload_main() {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000471 __android_log_write(ANDROID_LOG_INFO, TAG, "Hello Microdroid");
Inseob Kim06a64d62021-09-07 21:21:45 +0900472
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000473 // Make sure we can call into other shared libraries.
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900474 testlib_sub();
Jiyong Park23934392021-06-16 01:59:10 +0900475
Alan Stokes4b31a482024-02-01 17:56:06 +0000476 // Report various things that aren't always fatal - these are checked in MicrodroidTests as
477 // appropriate.
478 report_test("extra_apk_build_manifest", verify_build_manifest());
479 report_test("extra_apk_vm_share", verify_vm_share());
Inseob Kimdb319702022-01-20 13:12:43 +0900480
Jiyong Park23934392021-06-16 01:59:10 +0900481 __system_property_set("debug.microdroid.app.run", "true");
Andrew Sculledbe75e2021-07-06 10:44:31 +0000482
Inseob Kim06a64d62021-09-07 21:21:45 +0900483 if (auto res = start_test_service(); res.ok()) {
484 return 0;
485 } else {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000486 __android_log_write(ANDROID_LOG_ERROR, TAG, res.error().message().c_str());
Inseob Kim06a64d62021-09-07 21:21:45 +0900487 return 1;
488 }
Jiyong Parka7266ac2021-05-17 21:57:24 +0900489}