blob: 355cfb102eb55632d3764443392399c33130e33a [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>
Jiyong Park23934392021-06-16 01:59:10 +090032#include <sys/system_properties.h>
Nikita Ioffeaa9f6032025-01-21 18:06:46 +000033#ifdef __MICRODROID_TEST_PAYLOAD_USES_LIBICU__
34#include <unicode/uchar.h>
35#endif
Inseob Kim06a64d62021-09-07 21:21:45 +090036#include <unistd.h>
Alan Stokes52d3c722022-10-04 17:27:13 +010037#include <vm_main.h>
Alan Stokesd4ea5a82022-11-10 12:17:42 +000038#include <vm_payload_restricted.h>
Inseob Kim06a64d62021-09-07 21:21:45 +090039
Nikita Ioffed5846dc2024-11-01 18:44:45 +000040#include <cstdint>
Inseob Kim691df6a2022-01-20 12:54:30 +090041#include <string>
Alan Stokesd1a30dd2022-11-30 09:39:54 +000042#include <thread>
Jiyong Parka7266ac2021-05-17 21:57:24 +090043
Alan Stokesd1a30dd2022-11-30 09:39:54 +000044using android::base::borrowed_fd;
Inseob Kim06a64d62021-09-07 21:21:45 +090045using android::base::ErrnoError;
Andrew Scull11cf0902021-06-22 12:08:10 +000046using android::base::Error;
Nikita Ioffe3452ee22022-12-15 00:31:56 +000047using android::base::make_scope_guard;
Andrew Scull11cf0902021-06-22 12:08:10 +000048using android::base::Result;
Alan Stokesd1a30dd2022-11-30 09:39:54 +000049using android::base::unique_fd;
Nikita Ioffe29e15c82023-02-25 02:31:51 +000050using android::fs_mgr::Fstab;
51using android::fs_mgr::FstabEntry;
52using android::fs_mgr::GetEntryForMountPoint;
53using android::fs_mgr::ReadFstabFromFile;
Alan Stokesd1a30dd2022-11-30 09:39:54 +000054
55using aidl::com::android::microdroid::testservice::BnTestService;
Alan Stokes63fa37b2023-02-22 14:56:57 +000056using aidl::com::android::microdroid::testservice::BnVmCallback;
57using aidl::com::android::microdroid::testservice::IAppCallback;
Alan Stokesd1a30dd2022-11-30 09:39:54 +000058using ndk::ScopedAStatus;
Andrew Scull66616612021-06-17 16:41:03 +000059
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090060extern void testlib_sub();
61
Andrew Scull66616612021-06-17 16:41:03 +000062namespace {
63
Alan Stokesd1a30dd2022-11-30 09:39:54 +000064constexpr char TAG[] = "testbinary";
65
Andrew Scull11cf0902021-06-22 12:08:10 +000066template <typename T>
Andrew Sculledbe75e2021-07-06 10:44:31 +000067Result<T> report_test(std::string name, Result<T> result) {
Andrew Scull11cf0902021-06-22 12:08:10 +000068 auto property = "debug.microdroid.test." + name;
69 std::stringstream outcome;
70 if (result.ok()) {
71 outcome << "PASS";
72 } else {
73 outcome << "FAIL: " << result.error();
Alan Stokesd1a30dd2022-11-30 09:39:54 +000074 // Log the error in case the property is truncated.
75 std::string message = name + ": " + outcome.str();
76 __android_log_write(ANDROID_LOG_WARN, TAG, message.c_str());
Andrew Scull11cf0902021-06-22 12:08:10 +000077 }
78 __system_property_set(property.c_str(), outcome.str().c_str());
Andrew Sculledbe75e2021-07-06 10:44:31 +000079 return result;
Andrew Scull66616612021-06-17 16:41:03 +000080}
81
Alan Stokesd1a30dd2022-11-30 09:39:54 +000082Result<void> run_echo_reverse_server(borrowed_fd listening_fd) {
83 struct sockaddr_vm client_sa = {};
84 socklen_t client_sa_len = sizeof(client_sa);
85 unique_fd connect_fd{accept4(listening_fd.get(), (struct sockaddr*)&client_sa, &client_sa_len,
86 SOCK_CLOEXEC)};
87 if (!connect_fd.ok()) {
88 return ErrnoError() << "Failed to accept vsock connection";
89 }
90
91 unique_fd input_fd{fcntl(connect_fd, F_DUPFD_CLOEXEC, 0)};
92 if (!input_fd.ok()) {
93 return ErrnoError() << "Failed to dup";
94 }
95 FILE* input = fdopen(input_fd.release(), "r");
96 if (!input) {
97 return ErrnoError() << "Failed to fdopen";
98 }
99
David Brazdilbcce3512023-02-23 15:32:55 +0000100 // Run forever, reverse one line at a time.
101 while (true) {
102 char* line = nullptr;
103 size_t size = 0;
104 if (getline(&line, &size, input) < 0) {
Jiyong Park2ff07882024-12-17 11:30:28 +0900105 if (errno == 0) {
106 return {}; // the input was closed
107 }
David Brazdilbcce3512023-02-23 15:32:55 +0000108 return ErrnoError() << "Failed to read";
109 }
110
111 std::string_view original = line;
112 if (!original.empty() && original.back() == '\n') {
113 original = original.substr(0, original.size() - 1);
114 }
115
116 std::string reversed(original.rbegin(), original.rend());
117 reversed += "\n";
118
119 if (write(connect_fd, reversed.data(), reversed.size()) < 0) {
120 return ErrnoError() << "Failed to write";
121 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000122 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000123}
124
125Result<void> start_echo_reverse_server() {
126 unique_fd server_fd{TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0))};
127 if (!server_fd.ok()) {
128 return ErrnoError() << "Failed to create vsock socket";
129 }
130 struct sockaddr_vm server_sa = (struct sockaddr_vm){
131 .svm_family = AF_VSOCK,
Alan Stokes10c47672022-12-13 17:17:08 +0000132 .svm_port = static_cast<uint32_t>(BnTestService::ECHO_REVERSE_PORT),
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000133 .svm_cid = VMADDR_CID_ANY,
134 };
135 int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr*)&server_sa, sizeof(server_sa)));
136 if (ret < 0) {
137 return ErrnoError() << "Failed to bind vsock socket";
138 }
139 ret = TEMP_FAILURE_RETRY(listen(server_fd, /*backlog=*/1));
140 if (ret < 0) {
141 return ErrnoError() << "Failed to listen";
142 }
143
144 std::thread accept_thread{[listening_fd = std::move(server_fd)] {
Jiyong Park2ff07882024-12-17 11:30:28 +0900145 Result<void> result;
146 while ((result = run_echo_reverse_server(listening_fd)).ok()) {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000147 }
Jiyong Park2ff07882024-12-17 11:30:28 +0900148 __android_log_write(ANDROID_LOG_ERROR, TAG, result.error().message().c_str());
149 // Make sure the VM exits so the test will fail solidly
150 exit(1);
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000151 }};
152 accept_thread.detach();
153
154 return {};
155}
156
Inseob Kim06a64d62021-09-07 21:21:45 +0900157Result<void> start_test_service() {
Alan Stokes63fa37b2023-02-22 14:56:57 +0000158 class VmCallbackImpl : public BnVmCallback {
159 private:
160 std::shared_ptr<IAppCallback> mAppCallback;
161
162 public:
163 explicit VmCallbackImpl(const std::shared_ptr<IAppCallback>& appCallback)
164 : mAppCallback(appCallback) {}
165
166 ScopedAStatus echoMessage(const std::string& message) override {
167 std::thread callback_thread{[=, appCallback = mAppCallback] {
168 appCallback->onEchoRequestReceived("Received: " + message);
169 }};
170 callback_thread.detach();
171 return ScopedAStatus::ok();
172 }
173 };
174
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000175 class TestService : public BnTestService {
Alan Stokes63fa37b2023-02-22 14:56:57 +0000176 public:
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000177 ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override {
Inseob Kim06a64d62021-09-07 21:21:45 +0900178 *out = a + b;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000179 return ScopedAStatus::ok();
Inseob Kim06a64d62021-09-07 21:21:45 +0900180 }
Inseob Kim691df6a2022-01-20 12:54:30 +0900181
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000182 ScopedAStatus readProperty(const std::string& prop, std::string* out) override {
Inseob Kim691df6a2022-01-20 12:54:30 +0900183 *out = android::base::GetProperty(prop, "");
184 if (out->empty()) {
185 std::string msg = "cannot find property " + prop;
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000186 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
187 msg.c_str());
Inseob Kim691df6a2022-01-20 12:54:30 +0900188 }
189
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000190 return ScopedAStatus::ok();
Inseob Kim691df6a2022-01-20 12:54:30 +0900191 }
Andrew Scull2e6ab792022-01-30 16:04:08 +0000192
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000193 ScopedAStatus insecurelyExposeVmInstanceSecret(std::vector<uint8_t>* out) override {
Andrew Scull102067a2022-10-07 00:34:40 +0000194 const uint8_t identifier[] = {1, 2, 3, 4};
Andrew Scull655e98e2022-10-10 22:24:58 +0000195 out->resize(32);
Alan Stokes65bbb912022-11-23 09:39:34 +0000196 AVmPayload_getVmInstanceSecret(identifier, sizeof(identifier), out->data(),
197 out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000198 return ScopedAStatus::ok();
Andrew Scull2e6ab792022-01-30 16:04:08 +0000199 }
Andrew Scull1f6ca352022-02-20 22:51:12 +0000200
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000201 ScopedAStatus insecurelyExposeAttestationCdi(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000202 size_t cdi_size = AVmPayload_getDiceAttestationCdi(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000203 out->resize(cdi_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000204 AVmPayload_getDiceAttestationCdi(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000205 return ScopedAStatus::ok();
Andrew Scull1f6ca352022-02-20 22:51:12 +0000206 }
Andrew Scull61892082022-02-21 00:07:25 +0000207
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000208 ScopedAStatus getBcc(std::vector<uint8_t>* out) override {
Alan Stokes65bbb912022-11-23 09:39:34 +0000209 size_t bcc_size = AVmPayload_getDiceAttestationChain(nullptr, 0);
Andrew Scull655e98e2022-10-10 22:24:58 +0000210 out->resize(bcc_size);
Alan Stokes65bbb912022-11-23 09:39:34 +0000211 AVmPayload_getDiceAttestationChain(out->data(), out->size());
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000212 return ScopedAStatus::ok();
Andrew Scull61892082022-02-21 00:07:25 +0000213 }
Alice Wang6bbb6da2022-10-26 12:44:06 +0000214
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000215 ScopedAStatus getApkContentsPath(std::string* out) override {
Alice Wang6bbb6da2022-10-26 12:44:06 +0000216 const char* path_c = AVmPayload_getApkContentsPath();
217 if (path_c == nullptr) {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000218 return ScopedAStatus::
Alice Wang6bbb6da2022-10-26 12:44:06 +0000219 fromServiceSpecificErrorWithMessage(0, "Failed to get APK contents path");
220 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000221 *out = path_c;
222 return ScopedAStatus::ok();
Alice Wang6bbb6da2022-10-26 12:44:06 +0000223 }
Alan Stokes78d24702022-11-21 15:28:31 +0000224
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000225 ScopedAStatus getEncryptedStoragePath(std::string* out) override {
Alan Stokes78d24702022-11-21 15:28:31 +0000226 const char* path_c = AVmPayload_getEncryptedStoragePath();
227 if (path_c == nullptr) {
228 out->clear();
229 } else {
230 *out = path_c;
231 }
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000232 return ScopedAStatus::ok();
233 }
234
Nikita Ioffe3452ee22022-12-15 00:31:56 +0000235 ScopedAStatus getEffectiveCapabilities(std::vector<std::string>* out) override {
236 if (out == nullptr) {
237 return ScopedAStatus::ok();
238 }
239 cap_t cap = cap_get_proc();
240 auto guard = make_scope_guard([&cap]() { cap_free(cap); });
241 for (cap_value_t cap_id = 0; cap_id < CAP_LAST_CAP + 1; cap_id++) {
242 cap_flag_value_t value;
243 if (cap_get_flag(cap, cap_id, CAP_EFFECTIVE, &value) != 0) {
244 return ScopedAStatus::
245 fromServiceSpecificErrorWithMessage(0, "cap_get_flag failed");
246 }
247 if (value == CAP_SET) {
248 // Ideally we would just send back the cap_ids, but I wasn't able to find java
249 // APIs for linux capabilities, hence we transform to the human readable name
250 // here.
251 char* name = cap_to_name(cap_id);
252 out->push_back(std::string(name) + "(" + std::to_string(cap_id) + ")");
253 }
254 }
255 return ScopedAStatus::ok();
256 }
257
Alan Stokes1294f942023-08-21 14:34:12 +0100258 ScopedAStatus getUid(int* out) override {
259 *out = getuid();
260 return ScopedAStatus::ok();
261 }
262
Alan Stokes63fa37b2023-02-22 14:56:57 +0000263 ScopedAStatus runEchoReverseServer() override {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000264 auto result = start_echo_reverse_server();
265 if (result.ok()) {
266 return ScopedAStatus::ok();
267 } else {
268 std::string message = result.error().message();
269 return ScopedAStatus::fromServiceSpecificErrorWithMessage(-1, message.c_str());
270 }
Alan Stokes78d24702022-11-21 15:28:31 +0000271 }
Shikha Panwardc11b7e2022-12-15 12:24:11 +0000272
273 ScopedAStatus writeToFile(const std::string& content, const std::string& path) override {
274 if (!android::base::WriteStringToFile(content, path)) {
275 std::string msg = "Failed to write " + content + " to file " + path +
276 ". Errono: " + std::to_string(errno);
277 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
278 msg.c_str());
279 }
Shikha Panwardc11b7e2022-12-15 12:24:11 +0000280 return ScopedAStatus::ok();
281 }
282
283 ScopedAStatus readFromFile(const std::string& path, std::string* out) override {
284 if (!android::base::ReadFileToString(path, out)) {
285 std::string msg =
286 "Failed to read " + path + " to string. Errono: " + std::to_string(errno);
287 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
288 msg.c_str());
289 }
290 return ScopedAStatus::ok();
291 }
Shikha Panwardef7ef92023-01-06 08:35:48 +0000292
Nikita Ioffea7cb3672023-02-24 23:10:34 +0000293 ScopedAStatus getFilePermissions(const std::string& path, int32_t* out) override {
294 struct stat sb;
295 if (stat(path.c_str(), &sb) != -1) {
296 *out = sb.st_mode;
297 } else {
298 std::string msg = "stat " + path + " failed : " + std::strerror(errno);
299 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
300 msg.c_str());
301 }
302 return ScopedAStatus::ok();
303 }
304
Nikita Ioffe29e15c82023-02-25 02:31:51 +0000305 ScopedAStatus getMountFlags(const std::string& mount_point, int32_t* out) override {
306 Fstab fstab;
307 if (!ReadFstabFromFile("/proc/mounts", &fstab)) {
308 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
309 "Failed to read /proc/mounts");
310 }
311 FstabEntry* entry = GetEntryForMountPoint(&fstab, mount_point);
312 if (entry == nullptr) {
313 std::string msg = mount_point + " not found in /proc/mounts";
314 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
315 msg.c_str());
316 }
317 *out = entry->flags;
318 return ScopedAStatus::ok();
319 }
320
Nikita Ioffed5846dc2024-11-01 18:44:45 +0000321 ScopedAStatus getPageSize(int32_t* out) override {
322 *out = getpagesize();
323 return ScopedAStatus::ok();
324 }
325
Alan Stokes63fa37b2023-02-22 14:56:57 +0000326 ScopedAStatus requestCallback(const std::shared_ptr<IAppCallback>& appCallback) {
327 auto vmCallback = ndk::SharedRefBase::make<VmCallbackImpl>(appCallback);
328 std::thread callback_thread{[=] { appCallback->setVmCallback(vmCallback); }};
329 callback_thread.detach();
330 return ScopedAStatus::ok();
331 }
332
Jiyong Park92e34722023-06-27 00:43:39 +0900333 ScopedAStatus readLineFromConsole(std::string* out) {
334 FILE* f = fopen("/dev/console", "r");
335 if (f == nullptr) {
336 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
337 "failed to open /dev/console");
338 }
339 char* line = nullptr;
340 size_t len = 0;
341 ssize_t nread = getline(&line, &len, f);
342
343 if (nread == -1) {
344 free(line);
345 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
346 "failed to read /dev/console");
347 }
348 out->append(line, nread);
349 free(line);
350 return ScopedAStatus::ok();
351 }
352
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000353 ScopedAStatus insecurelyReadPayloadRpData(std::array<uint8_t, 32>* out) override {
Nikita Ioffe3e3fdf02025-01-15 14:20:27 +0000354 if (__builtin_available(android 36, *)) {
355 int32_t ret = AVmPayload_readRollbackProtectedSecret(out->data(), 32);
356 if (ret != 32) {
357 return ScopedAStatus::fromServiceSpecificError(ret);
358 }
359 return ScopedAStatus::ok();
360 } else {
361 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
362 "not available before SDK 36");
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000363 }
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000364 }
365
366 ScopedAStatus insecurelyWritePayloadRpData(
367 const std::array<uint8_t, 32>& inputData) override {
Nikita Ioffe3e3fdf02025-01-15 14:20:27 +0000368 if (__builtin_available(android 36, *)) {
369 int32_t ret = AVmPayload_writeRollbackProtectedSecret(inputData.data(), 32);
370 if (ret != 32) {
371 return ScopedAStatus::fromServiceSpecificError(ret);
372 }
373 return ScopedAStatus::ok();
374 } else {
375 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
376 "not available before SDK 36");
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000377 }
Shikha Panwar0c3a2fa2024-12-06 18:38:06 +0000378 }
379
Shikha Panwar5b7b4942024-12-18 15:32:49 +0000380 ScopedAStatus isNewInstance(bool* is_new_instance_out) override {
Nikita Ioffe3e3fdf02025-01-15 14:20:27 +0000381 if (__builtin_available(android 36, *)) {
382 *is_new_instance_out = AVmPayload_isNewInstance();
383 return ScopedAStatus::ok();
384 } else {
385 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
386 "not available before SDK 36");
387 }
Shikha Panwar5b7b4942024-12-18 15:32:49 +0000388 }
389
Nikita Ioffe901083f2025-01-20 01:54:28 +0000390 ScopedAStatus checkLibIcuIsAccessible() override {
Nikita Ioffeaa9f6032025-01-21 18:06:46 +0000391#ifdef __MICRODROID_TEST_PAYLOAD_USES_LIBICU__
Nikita Ioffe901083f2025-01-20 01:54:28 +0000392 static constexpr const char* kLibIcuPath = "/apex/com.android.i18n/lib64/libicu.so";
393 if (access(kLibIcuPath, R_OK) == 0) {
Nikita Ioffeaa9f6032025-01-21 18:06:46 +0000394 if (!u_hasBinaryProperty(U'❤' /* Emoji heart U+2764 */, UCHAR_EMOJI)) {
395 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
396 "libicu broken!");
397 }
Nikita Ioffe901083f2025-01-20 01:54:28 +0000398 return ScopedAStatus::ok();
399 } else {
400 std::string msg = "failed to access " + std::string(kLibIcuPath) + "(" +
401 std::to_string(errno) + ")";
402 return ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
403 msg.c_str());
404 }
Nikita Ioffeaa9f6032025-01-21 18:06:46 +0000405#else
406 return ScopedAStatus::
407 fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC,
408 "should be only used together with "
409 "MicrodroidTestNativeLibWithLibIcu.so payload");
410#endif
Nikita Ioffe901083f2025-01-20 01:54:28 +0000411 }
412
Shikha Panwardef7ef92023-01-06 08:35:48 +0000413 ScopedAStatus quit() override { exit(0); }
Inseob Kim06a64d62021-09-07 21:21:45 +0900414 };
415 auto testService = ndk::SharedRefBase::make<TestService>();
416
Alan Stokes65bbb912022-11-23 09:39:34 +0000417 auto callback = []([[maybe_unused]] void* param) { AVmPayload_notifyPayloadReady(); };
Devin Moorecaf7b952023-04-24 20:14:35 +0000418 AVmPayload_runVsockRpcServer(testService->asBinder().get(), testService->PORT, callback,
Alan Stokese0945ad2022-11-24 13:29:57 +0000419 nullptr);
Inseob Kim06a64d62021-09-07 21:21:45 +0900420
421 return {};
422}
423
Alan Stokes4b31a482024-02-01 17:56:06 +0000424Result<void> verify_build_manifest() {
Inseob Kimdb319702022-01-20 13:12:43 +0900425 const char* path = "/mnt/extra-apk/0/assets/build_manifest.pb";
426
427 std::string str;
428 if (!android::base::ReadFileToString(path, &str)) {
429 return ErrnoError() << "failed to read build_manifest.pb";
430 }
431
432 if (!android::security::fsverity::FSVerityDigests().ParseFromString(str)) {
433 return Error() << "invalid build_manifest.pb";
434 }
435
436 return {};
437}
438
Alan Stokes4b31a482024-02-01 17:56:06 +0000439Result<void> verify_vm_share() {
440 const char* path = "/mnt/extra-apk/0/assets/vmshareapp.txt";
441
442 std::string str;
443 if (!android::base::ReadFileToString(path, &str)) {
444 return ErrnoError() << "failed to read vmshareapp.txt";
445 }
446
447 return {};
448}
449
Andrew Scull66616612021-06-17 16:41:03 +0000450} // Anonymous namespace
451
Alan Stokes52d3c722022-10-04 17:27:13 +0100452extern "C" int AVmPayload_main() {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000453 __android_log_write(ANDROID_LOG_INFO, TAG, "Hello Microdroid");
Inseob Kim06a64d62021-09-07 21:21:45 +0900454
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000455 // Make sure we can call into other shared libraries.
Jiyong Parkfe5b28e2021-06-24 00:19:02 +0900456 testlib_sub();
Jiyong Park23934392021-06-16 01:59:10 +0900457
Alan Stokes4b31a482024-02-01 17:56:06 +0000458 // Report various things that aren't always fatal - these are checked in MicrodroidTests as
459 // appropriate.
460 report_test("extra_apk_build_manifest", verify_build_manifest());
461 report_test("extra_apk_vm_share", verify_vm_share());
Inseob Kimdb319702022-01-20 13:12:43 +0900462
Jiyong Park23934392021-06-16 01:59:10 +0900463 __system_property_set("debug.microdroid.app.run", "true");
Andrew Sculledbe75e2021-07-06 10:44:31 +0000464
Inseob Kim06a64d62021-09-07 21:21:45 +0900465 if (auto res = start_test_service(); res.ok()) {
466 return 0;
467 } else {
Alan Stokesd1a30dd2022-11-30 09:39:54 +0000468 __android_log_write(ANDROID_LOG_ERROR, TAG, res.error().message().c_str());
Inseob Kim06a64d62021-09-07 21:21:45 +0900469 return 1;
470 }
Jiyong Parka7266ac2021-05-17 21:57:24 +0900471}