Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 16 | #include <aidl/android/system/virtualmachineservice/IVirtualMachineService.h> |
| 17 | #include <aidl/com/android/microdroid/testservice/BnTestService.h> |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 18 | #include <android-base/file.h> |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 19 | #include <android-base/properties.h> |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 20 | #include <android-base/result.h> |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 21 | #include <android/binder_auto_utils.h> |
| 22 | #include <android/binder_manager.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 23 | #include <fcntl.h> |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 24 | #include <fsverity_digests.pb.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 25 | #include <linux/vm_sockets.h> |
| 26 | #include <stdint.h> |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 27 | #include <stdio.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 29 | #include <sys/system_properties.h> |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <binder_rpc_unstable.hpp> |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 33 | #include <string> |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 34 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 35 | using aidl::android::system::virtualmachineservice::IVirtualMachineService; |
| 36 | |
| 37 | using android::base::ErrnoError; |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 38 | using android::base::Error; |
| 39 | using android::base::Result; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 40 | |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 41 | extern void testlib_sub(); |
| 42 | |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 45 | template <typename T> |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 46 | Result<T> report_test(std::string name, Result<T> result) { |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 47 | auto property = "debug.microdroid.test." + name; |
| 48 | std::stringstream outcome; |
| 49 | if (result.ok()) { |
| 50 | outcome << "PASS"; |
| 51 | } else { |
| 52 | outcome << "FAIL: " << result.error(); |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 53 | // Pollute stderr with the error in case the property is truncated. |
| 54 | std::cerr << "[" << name << "] test failed: " << result.error() << "\n"; |
Andrew Scull | 11cf090 | 2021-06-22 12:08:10 +0000 | [diff] [blame] | 55 | } |
| 56 | __system_property_set(property.c_str(), outcome.str().c_str()); |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 57 | return result; |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 60 | Result<void> start_test_service() { |
| 61 | class TestService : public aidl::com::android::microdroid::testservice::BnTestService { |
| 62 | ndk::ScopedAStatus addInteger(int32_t a, int32_t b, int32_t* out) override { |
| 63 | *out = a + b; |
| 64 | return ndk::ScopedAStatus::ok(); |
| 65 | } |
Inseob Kim | 691df6a | 2022-01-20 12:54:30 +0900 | [diff] [blame] | 66 | |
| 67 | ndk::ScopedAStatus readProperty(const std::string& prop, std::string* out) override { |
| 68 | *out = android::base::GetProperty(prop, ""); |
| 69 | if (out->empty()) { |
| 70 | std::string msg = "cannot find property " + prop; |
| 71 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_SERVICE_SPECIFIC, |
| 72 | msg.c_str()); |
| 73 | } |
| 74 | |
| 75 | return ndk::ScopedAStatus::ok(); |
| 76 | } |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 77 | }; |
| 78 | auto testService = ndk::SharedRefBase::make<TestService>(); |
| 79 | |
| 80 | auto callback = []([[maybe_unused]] void* param) { |
| 81 | // Tell microdroid_manager that we're ready. |
| 82 | // Failing to notify is not a fatal error; the payload can continue. |
| 83 | ndk::SpAIBinder binder( |
| 84 | RpcClient(VMADDR_CID_HOST, IVirtualMachineService::VM_BINDER_SERVICE_PORT)); |
| 85 | auto virtualMachineService = IVirtualMachineService::fromBinder(binder); |
| 86 | if (virtualMachineService == nullptr) { |
| 87 | std::cerr << "failed to connect VirtualMachineService"; |
| 88 | return; |
| 89 | } |
Inseob Kim | c7d28c7 | 2021-10-25 14:28:10 +0000 | [diff] [blame] | 90 | if (!virtualMachineService->notifyPayloadReady().isOk()) { |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 91 | std::cerr << "failed to notify payload ready to virtualizationservice"; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | if (!RunRpcServerCallback(testService->asBinder().get(), testService->SERVICE_PORT, callback, |
| 96 | nullptr)) { |
| 97 | return Error() << "RPC Server failed to run"; |
| 98 | } |
| 99 | |
| 100 | return {}; |
| 101 | } |
| 102 | |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 103 | Result<void> verify_apk() { |
| 104 | const char* path = "/mnt/extra-apk/0/assets/build_manifest.pb"; |
| 105 | |
| 106 | std::string str; |
| 107 | if (!android::base::ReadFileToString(path, &str)) { |
| 108 | return ErrnoError() << "failed to read build_manifest.pb"; |
| 109 | } |
| 110 | |
| 111 | if (!android::security::fsverity::FSVerityDigests().ParseFromString(str)) { |
| 112 | return Error() << "invalid build_manifest.pb"; |
| 113 | } |
| 114 | |
| 115 | return {}; |
| 116 | } |
| 117 | |
Andrew Scull | 6661661 | 2021-06-17 16:41:03 +0000 | [diff] [blame] | 118 | } // Anonymous namespace |
| 119 | |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 120 | extern "C" int android_native_main(int argc, char* argv[]) { |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 121 | // disable buffering to communicate seamlessly |
| 122 | setvbuf(stdin, nullptr, _IONBF, 0); |
| 123 | setvbuf(stdout, nullptr, _IONBF, 0); |
| 124 | setvbuf(stderr, nullptr, _IONBF, 0); |
| 125 | |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 126 | printf("Hello Microdroid "); |
| 127 | for (int i = 0; i < argc; i++) { |
| 128 | printf("%s", argv[i]); |
| 129 | bool last = i == (argc - 1); |
| 130 | if (!last) { |
| 131 | printf(" "); |
| 132 | } |
| 133 | } |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 134 | testlib_sub(); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 135 | printf("\n"); |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 136 | |
Inseob Kim | db31970 | 2022-01-20 13:12:43 +0900 | [diff] [blame] | 137 | // Extra apks may be missing; this is not a fatal error |
| 138 | report_test("extra_apk", verify_apk()); |
| 139 | |
Jiyong Park | 2393439 | 2021-06-16 01:59:10 +0900 | [diff] [blame] | 140 | __system_property_set("debug.microdroid.app.run", "true"); |
Andrew Scull | edbe75e | 2021-07-06 10:44:31 +0000 | [diff] [blame] | 141 | |
Inseob Kim | 06a64d6 | 2021-09-07 21:21:45 +0900 | [diff] [blame] | 142 | if (auto res = start_test_service(); res.ok()) { |
| 143 | return 0; |
| 144 | } else { |
| 145 | std::cerr << "starting service failed: " << res.error(); |
| 146 | return 1; |
| 147 | } |
Jiyong Park | a7266ac | 2021-05-17 21:57:24 +0900 | [diff] [blame] | 148 | } |