Merge "RESTRICT AUTOMERGE: snapuserd: Run snapuserd threads in-process."
diff --git a/debuggerd/Android.bp b/debuggerd/Android.bp
index 5da1b40..d20de6b 100644
--- a/debuggerd/Android.bp
+++ b/debuggerd/Android.bp
@@ -368,6 +368,7 @@
name: "crash_dump",
srcs: [
"crash_dump.cpp",
+ "tombstone_handler.cpp",
"util.cpp",
],
defaults: ["debuggerd_defaults"],
@@ -509,6 +510,9 @@
arm64: {
src: "seccomp_policy/crash_dump.arm.policy",
},
+ riscv64: {
+ enabled: false,
+ },
x86: {
src: "seccomp_policy/crash_dump.x86_64.policy",
},
diff --git a/debuggerd/TEST_MAPPING b/debuggerd/TEST_MAPPING
index 394447b..8633cb8 100644
--- a/debuggerd/TEST_MAPPING
+++ b/debuggerd/TEST_MAPPING
@@ -5,6 +5,9 @@
},
{
"name": "libtombstoned_client_rust_test"
+ },
+ {
+ "name": "MicrodroidHostTestCases"
}
],
"hwasan-presubmit": [
diff --git a/debuggerd/client/debuggerd_client.cpp b/debuggerd/client/debuggerd_client.cpp
index b302918..c9e097e 100644
--- a/debuggerd/client/debuggerd_client.cpp
+++ b/debuggerd/client/debuggerd_client.cpp
@@ -216,7 +216,7 @@
log_error(output_fd, 0,
"received packet of unexpected length from tombstoned while reading %s response: "
"expected %zd, received %zd",
- kind, sizeof(response), rc);
+ kind, sizeof(*response), rc);
return false;
}
return true;
diff --git a/debuggerd/crash_dump.cpp b/debuggerd/crash_dump.cpp
index 442392d..3563436 100644
--- a/debuggerd/crash_dump.cpp
+++ b/debuggerd/crash_dump.cpp
@@ -59,7 +59,7 @@
#include "libdebuggerd/utility.h"
#include "debuggerd/handler.h"
-#include "tombstoned/tombstoned.h"
+#include "tombstone_handler.h"
#include "protocol.h"
#include "util.h"
@@ -142,7 +142,8 @@
return false;
}
-static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data) {
+static bool activity_manager_notify(pid_t pid, int signal, const std::string& amfd_data,
+ bool recoverable_gwp_asan_crash) {
ATRACE_CALL();
android::base::unique_fd amfd(socket_local_client(
"/data/system/ndebugsocket", ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM));
@@ -165,19 +166,32 @@
return false;
}
- // Activity Manager protocol: binary 32-bit network-byte-order ints for the
- // pid and signal number, followed by the raw text of the dump, culminating
- // in a zero byte that marks end-of-data.
+ // Activity Manager protocol:
+ // - 32-bit network-byte-order: pid
+ // - 32-bit network-byte-order: signal number
+ // - byte: recoverable_gwp_asan_crash
+ // - bytes: raw text of the dump
+ // - null terminator
+
uint32_t datum = htonl(pid);
- if (!android::base::WriteFully(amfd, &datum, 4)) {
+ if (!android::base::WriteFully(amfd, &datum, sizeof(datum))) {
PLOG(ERROR) << "AM pid write failed";
return false;
}
+
datum = htonl(signal);
- if (!android::base::WriteFully(amfd, &datum, 4)) {
- PLOG(ERROR) << "AM signal write failed";
+ if (!android::base::WriteFully(amfd, &datum, sizeof(datum))) {
+ PLOG(ERROR) << "AM signo write failed";
return false;
}
+
+ uint8_t recoverable_gwp_asan_crash_byte = recoverable_gwp_asan_crash ? 1 : 0;
+ if (!android::base::WriteFully(amfd, &recoverable_gwp_asan_crash_byte,
+ sizeof(recoverable_gwp_asan_crash_byte))) {
+ PLOG(ERROR) << "AM recoverable_gwp_asan_crash_byte write failed";
+ return false;
+ }
+
if (!android::base::WriteFully(amfd, amfd_data.c_str(), amfd_data.size() + 1)) {
PLOG(ERROR) << "AM data write failed";
return false;
@@ -215,8 +229,8 @@
// If we abort before we get an output fd, contact tombstoned to let any
// potential listeners know that we failed.
if (!g_tombstoned_connected) {
- if (!tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd, &g_proto_fd,
- kDebuggerdAnyIntercept)) {
+ if (!connect_tombstone_server(g_target_thread, &g_tombstoned_socket, &g_output_fd,
+ &g_proto_fd, kDebuggerdAnyIntercept)) {
// We failed to connect, not much we can do.
LOG(ERROR) << "failed to connected to tombstoned to report failure";
_exit(1);
@@ -589,8 +603,8 @@
{
ATRACE_NAME("tombstoned_connect");
LOG(INFO) << "obtaining output fd from tombstoned, type: " << dump_type;
- g_tombstoned_connected = tombstoned_connect(g_target_thread, &g_tombstoned_socket, &g_output_fd,
- &g_proto_fd, dump_type);
+ g_tombstoned_connected = connect_tombstone_server(g_target_thread, &g_tombstoned_socket,
+ &g_output_fd, &g_proto_fd, dump_type);
}
if (g_tombstoned_connected) {
@@ -651,10 +665,10 @@
}
}
- if (fatal_signal && !recoverable_gwp_asan_crash) {
+ if (fatal_signal) {
// Don't try to notify ActivityManager if it just crashed, or we might hang until timeout.
if (thread_info[target_process].thread_name != "system_server") {
- activity_manager_notify(target_process, signo, amfd_data);
+ activity_manager_notify(target_process, signo, amfd_data, recoverable_gwp_asan_crash);
}
}
@@ -673,7 +687,8 @@
// Close stdout before we notify tombstoned of completion.
close(STDOUT_FILENO);
- if (g_tombstoned_connected && !tombstoned_notify_completion(g_tombstoned_socket.get())) {
+ if (g_tombstoned_connected &&
+ !notify_completion(g_tombstoned_socket.get(), g_output_fd.get(), g_proto_fd.get())) {
LOG(ERROR) << "failed to notify tombstoned of completion";
}
diff --git a/debuggerd/crasher/arm/crashglue.S b/debuggerd/crasher/arm/crashglue.S
index 8649056..e4adf40 100644
--- a/debuggerd/crasher/arm/crashglue.S
+++ b/debuggerd/crasher/arm/crashglue.S
@@ -1,6 +1,10 @@
.globl crash1
.type crash1, %function
crash1:
+ .cfi_startproc
+ push {lr}
+ .cfi_def_cfa_offset 4
+ .cfi_rel_offset lr, 0
ldr r0, =0xa5a50000
ldr r1, =0xa5a50001
ldr r2, =0xa5a50002
@@ -15,48 +19,19 @@
ldr r11, =0xa5a50011
ldr r12, =0xa5a50012
-
- fconstd d0, #0
- fconstd d1, #1
- fconstd d2, #2
- fconstd d3, #3
- fconstd d4, #4
- fconstd d5, #5
- fconstd d6, #6
- fconstd d7, #7
- fconstd d8, #8
- fconstd d9, #9
- fconstd d10, #10
- fconstd d11, #11
- fconstd d12, #12
- fconstd d13, #13
- fconstd d14, #14
- fconstd d15, #15
- fconstd d16, #16
- fconstd d17, #17
- fconstd d18, #18
- fconstd d19, #19
- fconstd d20, #20
- fconstd d21, #21
- fconstd d22, #22
- fconstd d23, #23
- fconstd d24, #24
- fconstd d25, #25
- fconstd d26, #26
- fconstd d27, #27
- fconstd d28, #28
- fconstd d29, #29
- fconstd d30, #30
- fconstd d31, #31
-
mov lr, #0
ldr lr, [lr]
b .
+ .cfi_endproc
.globl crashnostack
.type crashnostack, %function
crashnostack:
+ .cfi_startproc
+ mov r1, sp
+ .cfi_def_cfa_register r1
mov sp, #0
mov r0, #0
ldr r0, [r0]
b .
+ .cfi_endproc
diff --git a/debuggerd/crasher/arm64/crashglue.S b/debuggerd/crasher/arm64/crashglue.S
index e58b542..97c824e 100644
--- a/debuggerd/crasher/arm64/crashglue.S
+++ b/debuggerd/crasher/arm64/crashglue.S
@@ -1,6 +1,11 @@
.globl crash1
.type crash1, %function
crash1:
+ .cfi_startproc
+ stp x29, x30, [sp, -16]!
+ .cfi_def_cfa_offset 16
+ .cfi_rel_offset x29, 0
+ .cfi_rel_offset x30, 8
ldr x0, =0xa5a50000
ldr x1, =0xa5a50001
ldr x2, =0xa5a50002
@@ -32,48 +37,20 @@
ldr x28, =0xa5a50028
ldr x29, =0xa5a50029
- fmov d0, -1.0 // -1 is more convincing than 0.
- fmov d1, 1.0
- fmov d2, 2.0
- fmov d3, 3.0
- fmov d4, 4.0
- fmov d5, 5.0
- fmov d6, 6.0
- fmov d7, 7.0
- fmov d8, 8.0
- fmov d9, 9.0
- fmov d10, 10.0
- fmov d11, 11.0
- fmov d12, 12.0
- fmov d13, 13.0
- fmov d14, 14.0
- fmov d15, 15.0
- fmov d16, 16.0
- fmov d17, 17.0
- fmov d18, 18.0
- fmov d19, 19.0
- fmov d20, 20.0
- fmov d21, 21.0
- fmov d22, 22.0
- fmov d23, 23.0
- fmov d24, 24.0
- fmov d25, 25.0
- fmov d26, 26.0
- fmov d27, 27.0
- fmov d28, 28.0
- fmov d29, 29.0
- fmov d30, 30.0
- fmov d31, 31.0
-
mov x30, xzr
ldr x30, [x30]
b .
+ .cfi_endproc
.globl crashnostack
.type crashnostack, %function
crashnostack:
+ .cfi_startproc
+ mov x1, sp
+ .cfi_def_cfa_register x1
mov x0, xzr
add sp, x0, xzr
ldr x0, [x0]
b .
+ .cfi_endproc
diff --git a/debuggerd/crasher/crasher.cpp b/debuggerd/crasher/crasher.cpp
index 4043a6e..6a19878 100644
--- a/debuggerd/crasher/crasher.cpp
+++ b/debuggerd/crasher/crasher.cpp
@@ -164,7 +164,8 @@
}
noinline void readdir_null() {
- readdir(nullptr);
+ DIR* sneaky_null = nullptr;
+ readdir(sneaky_null);
}
noinline int strlen_null() {
diff --git a/debuggerd/crasher/riscv64/crashglue.S b/debuggerd/crasher/riscv64/crashglue.S
index 47dd93b..42f59b3 100644
--- a/debuggerd/crasher/riscv64/crashglue.S
+++ b/debuggerd/crasher/riscv64/crashglue.S
@@ -1,45 +1,56 @@
-
- .globl crash1
- .globl crashnostack
-
+.globl crash1
crash1:
- li x0,0xdead0000+0
- li x1,0xdead0000+1
- li x2,0xdead0000+2
- li x3,0xdead0000+3
- li x4,0xdead0000+4
- li x5,0xdead0000+5
- li x6,0xdead0000+6
- li x7,0xdead0000+7
- li x8,0xdead0000+8
- li x9,0xdead0000+9
- li x10,0xdead0000+10
- li x11,0xdead0000+11
- li x12,0xdead0000+12
- li x13,0xdead0000+13
- li x14,0xdead0000+14
- li x15,0xdead0000+15
- li x16,0xdead0000+16
- li x17,0xdead0000+17
- li x18,0xdead0000+18
- li x19,0xdead0000+19
- li x20,0xdead0000+20
- li x21,0xdead0000+21
- li x22,0xdead0000+22
- li x23,0xdead0000+23
- li x24,0xdead0000+24
- li x25,0xdead0000+25
- li x26,0xdead0000+26
- li x27,0xdead0000+27
- li x28,0xdead0000+28
- # don't trash the stack otherwise the signal handler won't run
- #li $29,0xdead0000+29
- li x30,0xdead0000+30
- li x31,0xdead0000+31
+ .cfi_startproc
+ addi sp, sp, -16
+ .cfi_def_cfa_offset 16
+ sd ra, 8(sp)
+ .cfi_offset ra, -8
+ li x0,0xa5a50000
+ li x1,0xa5a50001
+ li x2,0xa5a50002
+ li x3,0xa5a50003
+ li x4,0xa5a50004
+ li x5,0xa5a50005
+ li x6,0xa5a50006
+ li x7,0xa5a50007
+ li x8,0xa5a50008
+ li x9,0xa5a50009
+ li x10,0xa5a50010
+ li x11,0xa5a50011
+ li x12,0xa5a50012
+ li x13,0xa5a50013
+ li x14,0xa5a50014
+ li x15,0xa5a50015
+ li x16,0xa5a50016
+ li x17,0xa5a50017
+ li x18,0xa5a50018
+ li x19,0xa5a50019
+ li x20,0xa5a50020
+ li x21,0xa5a50021
+ li x22,0xa5a50022
+ li x23,0xa5a50023
+ li x24,0xa5a50024
+ li x25,0xa5a50025
+ li x26,0xa5a50026
+ li x27,0xa5a50027
+ li x28,0xa5a50028
+ li x29,0xa5a50029
+ li x30,0xa5a50030
+ li x31,0xa5a50031
+
+ li sp, 0
+ ld t2, 0(zero)
j .
+ .cfi_endproc
+.globl crashnostack
crashnostack:
- li sp, 0
+ .cfi_startproc
+ mv t1, sp
+ .cfi_def_cfa_register t1
+ li sp, 0
+ ld t2, 0(zero)
j .
+ .cfi_endproc
diff --git a/debuggerd/crasher/x86/crashglue.S b/debuggerd/crasher/x86/crashglue.S
index 59df432..e8eb3a7 100644
--- a/debuggerd/crasher/x86/crashglue.S
+++ b/debuggerd/crasher/x86/crashglue.S
@@ -1,6 +1,4 @@
.globl crash1
-.globl crashnostack
-
crash1:
movl $0xa5a50000, %eax
movl $0xa5a50001, %ebx
@@ -10,6 +8,11 @@
jmp *%edx
+.globl crashnostack
crashnostack:
- movl $0, %ebp
- jmp *%ebp
+ .cfi_startproc
+ movl %esp, %eax
+ .cfi_def_cfa_register %eax
+ movl $0, %esp
+ movl (%esp), %ebx
+ .cfi_endproc
diff --git a/debuggerd/crasher/x86_64/crashglue.S b/debuggerd/crasher/x86_64/crashglue.S
index 4d2a5c0..8f67214 100644
--- a/debuggerd/crasher/x86_64/crashglue.S
+++ b/debuggerd/crasher/x86_64/crashglue.S
@@ -1,6 +1,4 @@
.globl crash1
-.globl crashnostack
-
crash1:
movl $0xa5a50000, %eax
movl $0xa5a50001, %ebx
@@ -10,6 +8,11 @@
jmp *%rdx
+.globl crashnostack
crashnostack:
- movl $0, %ebp
- jmp *%rbp
+ .cfi_startproc
+ movq %rsp, %rax
+ .cfi_def_cfa_register %rax
+ movq $0, %rsp
+ movq (%rsp), %rbx
+ .cfi_endproc
diff --git a/debuggerd/debuggerd_test.cpp b/debuggerd/debuggerd_test.cpp
index 517f2df..a00a202 100644
--- a/debuggerd/debuggerd_test.cpp
+++ b/debuggerd/debuggerd_test.cpp
@@ -2437,35 +2437,42 @@
#if defined(__arm__)
asm volatile(
"mov r1, %[base]\n"
- "mov r2, 0\n"
- "str r3, [r2]\n"
+ "mov r2, #0\n"
+ "str r2, [r2]\n"
: [base] "+r"(ptr)
:
- : "r1", "r2", "r3", "memory");
+ : "r1", "r2", "memory");
#elif defined(__aarch64__)
asm volatile(
"mov x1, %[base]\n"
- "mov x2, 0\n"
- "str x3, [x2]\n"
+ "mov x2, #0\n"
+ "str xzr, [x2]\n"
: [base] "+r"(ptr)
:
- : "x1", "x2", "x3", "memory");
+ : "x1", "x2", "memory");
+#elif defined(__riscv)
+ // TODO: x1 is ra (the link register) on riscv64, so this might have
+ // unintended consequences, but we'll need to change the .cfi_escape if so.
+ asm volatile(
+ "mv x1, %[base]\n"
+ "sw zero, 0(zero)\n"
+ : [base] "+r"(ptr)
+ :
+ : "x1", "memory");
#elif defined(__i386__)
asm volatile(
"mov %[base], %%ecx\n"
- "movl $0, %%edi\n"
- "movl 0(%%edi), %%edx\n"
+ "movl $0, 0\n"
: [base] "+r"(ptr)
:
- : "edi", "ecx", "edx", "memory");
+ : "ecx", "memory");
#elif defined(__x86_64__)
asm volatile(
"mov %[base], %%rdx\n"
- "movq 0, %%rdi\n"
- "movq 0(%%rdi), %%rcx\n"
+ "movq $0, 0\n"
: [base] "+r"(ptr)
:
- : "rcx", "rdx", "rdi", "memory");
+ : "rdx", "memory");
#else
#error "Unsupported architecture"
#endif
diff --git a/debuggerd/libdebuggerd/include/libdebuggerd/utility.h b/debuggerd/libdebuggerd/include/libdebuggerd/utility.h
index 25b03af..198de37 100644
--- a/debuggerd/libdebuggerd/include/libdebuggerd/utility.h
+++ b/debuggerd/libdebuggerd/include/libdebuggerd/utility.h
@@ -51,7 +51,6 @@
HEADER,
THREAD,
REGISTERS,
- FP_REGISTERS,
BACKTRACE,
MAPS,
MEMORY,
diff --git a/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp b/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp
index e4d68f8..8e6abdf 100644
--- a/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp
+++ b/debuggerd/libdebuggerd/tombstone_proto_to_text.cpp
@@ -190,6 +190,7 @@
static void print_thread_backtrace(CallbackType callback, const Tombstone& tombstone,
const Thread& thread, bool should_log) {
CBS("");
+ CB(should_log, "%d total frames", thread.current_backtrace().size());
CB(should_log, "backtrace:");
if (!thread.backtrace_note().empty()) {
CB(should_log, " NOTE: %s",
diff --git a/debuggerd/libdebuggerd/utility.cpp b/debuggerd/libdebuggerd/utility.cpp
index 74a1423..d71fc6c 100644
--- a/debuggerd/libdebuggerd/utility.cpp
+++ b/debuggerd/libdebuggerd/utility.cpp
@@ -47,12 +47,7 @@
using android::base::unique_fd;
bool is_allowed_in_logcat(enum logtype ltype) {
- if ((ltype == HEADER)
- || (ltype == REGISTERS)
- || (ltype == BACKTRACE)) {
- return true;
- }
- return false;
+ return (ltype == HEADER) || (ltype == REGISTERS) || (ltype == BACKTRACE);
}
static bool should_write_to_kmsg() {
diff --git a/debuggerd/tombstone_handler.cpp b/debuggerd/tombstone_handler.cpp
new file mode 100644
index 0000000..09df6d9
--- /dev/null
+++ b/debuggerd/tombstone_handler.cpp
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2023, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "tombstoned/tombstoned.h"
+
+#include <unistd.h>
+
+#include <android-base/logging.h>
+#include <android-base/properties.h>
+#include <android-base/unique_fd.h>
+#include <cutils/sockets.h>
+#include <linux/vm_sockets.h>
+#include "util.h"
+
+using android::base::unique_fd;
+
+/*
+ Port number that VirtualMachineService listens on connections from the guest VMs.
+ Kep in sync with IVirtualMachineService.aidl
+*/
+const unsigned int VM_TOMBSTONES_SERVICE_PORT = 2000;
+
+static bool is_microdroid() {
+ return android::base::GetProperty("ro.hardware", "") == "microdroid";
+}
+
+static bool connect_tombstone_server_microdroid(unique_fd* text_output_fd,
+ unique_fd* proto_output_fd,
+ DebuggerdDumpType dump_type) {
+ // We do not wait for the property to be set, the default behaviour is not export tombstones.
+ if (!android::base::GetBoolProperty("microdroid_manager.export_tombstones.enabled", false)) {
+ LOG(WARNING) << "exporting tombstones is not enabled";
+ return false;
+ }
+
+ // Microdroid supports handling requests originating from crash_dump which
+ // supports limited dump types. Java traces and incept management are not supported.
+ switch (dump_type) {
+ case kDebuggerdNativeBacktrace:
+ case kDebuggerdTombstone:
+ case kDebuggerdTombstoneProto:
+ break;
+
+ default:
+ LOG(WARNING) << "Requested dump type: " << dump_type << " "
+ << "not supported";
+ }
+
+ int fd1 = TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0));
+ int fd2 = TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0));
+ if (fd1 < 0 || fd2 < 0) {
+ LOG(WARNING) << "Unable to create virtual socket for writing tombstones";
+ return false;
+ }
+
+ unique_fd vsock_output_fd(fd1), vsock_proto_fd(fd2);
+
+ struct sockaddr_vm sa = (struct sockaddr_vm){
+ .svm_family = AF_VSOCK,
+ .svm_port = VM_TOMBSTONES_SERVICE_PORT,
+ .svm_cid = VMADDR_CID_HOST,
+ };
+
+ if (TEMP_FAILURE_RETRY(connect(vsock_output_fd, (struct sockaddr*)&sa, sizeof(sa))) < 0) {
+ PLOG(WARNING) << "Unable to connect to tombstone service in host";
+ return false;
+ }
+
+ if (dump_type == kDebuggerdTombstoneProto) {
+ if (TEMP_FAILURE_RETRY(connect(vsock_proto_fd, (struct sockaddr*)&sa, sizeof(sa))) < 0) {
+ PLOG(WARNING) << "Unable to connect to tombstone service in host";
+ return false;
+ }
+ }
+
+ *text_output_fd = std::move(vsock_output_fd);
+ if (proto_output_fd) {
+ *proto_output_fd = std::move(vsock_proto_fd);
+ }
+ return true;
+}
+
+static bool notify_completion_microdroid(int vsock_out, int vsock_proto) {
+ if (shutdown(vsock_out, SHUT_WR) || shutdown(vsock_proto, SHUT_WR)) return false;
+ return true;
+}
+bool connect_tombstone_server(pid_t pid, unique_fd* tombstoned_socket, unique_fd* text_output_fd,
+ unique_fd* proto_output_fd, DebuggerdDumpType dump_type) {
+ if (is_microdroid()) {
+ return connect_tombstone_server_microdroid(text_output_fd, proto_output_fd, dump_type);
+ }
+ return tombstoned_connect(pid, tombstoned_socket, text_output_fd, proto_output_fd, dump_type);
+}
+
+bool notify_completion(int tombstoned_socket, int vsock_out, int vsock_proto) {
+ if (is_microdroid()) {
+ return notify_completion_microdroid(vsock_out, vsock_proto);
+ }
+ return tombstoned_notify_completion(tombstoned_socket);
+}
diff --git a/debuggerd/tombstone_handler.h b/debuggerd/tombstone_handler.h
new file mode 100644
index 0000000..8726bd3
--- /dev/null
+++ b/debuggerd/tombstone_handler.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2023, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <android-base/unique_fd.h>
+#include "dump_type.h"
+
+bool connect_tombstone_server(pid_t pid, android::base::unique_fd* tombstoned_socket,
+ android::base::unique_fd* text_output_fd,
+ android::base::unique_fd* proto_output_fd,
+ DebuggerdDumpType dump_type);
+
+bool notify_completion(int tombstoned_socket, int vsock_out, int vsock_proto);
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp
index 15d1874..d7d2091 100644
--- a/fastboot/fastboot.cpp
+++ b/fastboot/fastboot.cpp
@@ -988,7 +988,7 @@
}
const int files = sparse_file_resparse(s, max_size, nullptr, 0);
- if (files < 0) die("Failed to resparse");
+ if (files < 0) die("Failed to compute resparse boundaries");
auto temp = std::make_unique<sparse_file*[]>(files);
const int rv = sparse_file_resparse(s, max_size, temp.get(), files);
@@ -1057,6 +1057,10 @@
if (sparse_file* s = sparse_file_import(fd.get(), false, false)) {
buf->image_size = sparse_file_len(s, false, false);
+ if (buf->image_size < 0) {
+ LOG(ERROR) << "Could not compute length of sparse file";
+ return false;
+ }
sparse_file_destroy(s);
} else {
buf->image_size = sz;
@@ -1159,6 +1163,12 @@
fb->GetVar("partition-type:vbmeta_b", &partition_type) == fastboot::SUCCESS;
}
+static bool is_vbmeta_partition(const std::string& partition) {
+ return android::base::EndsWith(partition, "vbmeta") ||
+ android::base::EndsWith(partition, "vbmeta_a") ||
+ android::base::EndsWith(partition, "vbmeta_b");
+}
+
// Note: this only works in userspace fastboot. In the bootloader, use
// should_flash_in_userspace().
bool is_logical(const std::string& partition) {
@@ -1166,15 +1176,6 @@
return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes";
}
-static std::string fb_fix_numeric_var(std::string var) {
- // Some bootloaders (angler, for example), send spurious leading whitespace.
- var = android::base::Trim(var);
- // Some bootloaders (hammerhead, for example) use implicit hex.
- // This code used to use strtol with base 16.
- if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
- return var;
-}
-
static uint64_t get_partition_size(const std::string& partition) {
std::string partition_size_str;
if (fb->GetVar("partition-size:" + partition, &partition_size_str) != fastboot::SUCCESS) {
@@ -1196,10 +1197,10 @@
}
static void copy_avb_footer(const std::string& partition, struct fastboot_buffer* buf) {
- if (buf->sz < AVB_FOOTER_SIZE) {
+ if (buf->sz < AVB_FOOTER_SIZE || is_logical(partition) ||
+ should_flash_in_userspace(partition)) {
return;
}
-
// If overflows and negative, it should be < buf->sz.
int64_t partition_size = static_cast<int64_t>(get_partition_size(partition));
@@ -1247,24 +1248,22 @@
for (size_t i = 0; i < files.size(); i++) {
sparse_file* s = files[i].get();
int64_t sz = sparse_file_len(s, true, false);
+ if (sz < 0) {
+ LOG(FATAL) << "Could not compute length of sparse image for " << partition;
+ }
fb->FlashPartition(partition, s, sz, i + 1, files.size());
}
}
-static void flash_buf(const std::string& partition, struct fastboot_buffer* buf) {
- if (partition == "boot" || partition == "boot_a" || partition == "boot_b" ||
- partition == "init_boot" || partition == "init_boot_a" || partition == "init_boot_b" ||
- partition == "recovery" || partition == "recovery_a" || partition == "recovery_b") {
- copy_avb_footer(partition, buf);
- }
+static void flash_buf(const std::string& partition, struct fastboot_buffer* buf,
+ const bool apply_vbmeta) {
+ copy_avb_footer(partition, buf);
// Rewrite vbmeta if that's what we're flashing and modification has been requested.
if (g_disable_verity || g_disable_verification) {
// The vbmeta partition might have additional prefix if running in virtual machine
// e.g., guest_vbmeta_a.
- if (android::base::EndsWith(partition, "vbmeta") ||
- android::base::EndsWith(partition, "vbmeta_a") ||
- android::base::EndsWith(partition, "vbmeta_b")) {
+ if (apply_vbmeta) {
rewrite_vbmeta_buffer(buf, false /* vbmeta_in_boot */);
} else if (!has_vbmeta_partition() &&
(partition == "boot" || partition == "boot_a" || partition == "boot_b")) {
@@ -1499,7 +1498,7 @@
return partition;
}
-void do_flash(const char* pname, const char* fname) {
+void do_flash(const char* pname, const char* fname, const bool apply_vbmeta) {
verbose("Do flash %s %s", pname, fname);
struct fastboot_buffer buf;
@@ -1510,7 +1509,7 @@
fb->ResizePartition(pname, std::to_string(buf.image_size));
}
std::string flash_pname = repack_ramdisk(pname, &buf);
- flash_buf(flash_pname, &buf);
+ flash_buf(flash_pname, &buf, apply_vbmeta);
}
// Sets slot_override as the active slot. If slot_override is blank,
@@ -1588,11 +1587,6 @@
void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
- // If the image uses the default slot, or the user specified "all", then
- // the paired string will be empty. If the image requests a specific slot
- // (for example, system_other) it is specified instead.
- using ImageEntry = std::pair<const Image*, std::string>;
-
std::vector<ImageEntry> boot_images_;
std::vector<ImageEntry> os_images_;
FlashingPlan* fp_;
@@ -1606,10 +1600,10 @@
// Change the slot first, so we boot into the correct recovery image when
// using fastbootd.
- if (fp_->slot == "all") {
+ if (fp_->slot_override == "all") {
set_active("a");
} else {
- set_active(fp_->slot);
+ set_active(fp_->slot_override);
}
DetermineSlot();
@@ -1621,17 +1615,15 @@
// or in bootloader fastboot.
FlashImages(boot_images_);
- auto flash_super_task = FlashSuperLayoutTask::Initialize(fp_, os_images_);
+ std::vector<std::unique_ptr<Task>> tasks;
- if (flash_super_task) {
- flash_super_task->Run();
+ if (auto flash_super_task = FlashSuperLayoutTask::Initialize(fp_, os_images_)) {
+ tasks.emplace_back(std::move(flash_super_task));
} else {
// Sync the super partition. This will reboot to userspace fastboot if needed.
- std::unique_ptr<UpdateSuperTask> update_super_task = std::make_unique<UpdateSuperTask>(fp_);
- update_super_task->Run();
+ tasks.emplace_back(std::make_unique<UpdateSuperTask>(fp_));
// Resize any logical partition to 0, so each partition is reset to 0
// extents, and will achieve more optimal allocation.
- std::vector<std::unique_ptr<ResizeTask>> resize_tasks;
for (const auto& [image, slot] : os_images_) {
// Retrofit devices have two super partitions, named super_a and super_b.
// On these devices, secondary slots must be flashed as physical
@@ -1641,17 +1633,14 @@
std::string partition_name = image->part_name + "_"s + slot;
if (image->IsSecondary() && is_logical(partition_name)) {
fp_->fb->DeletePartition(partition_name);
- std::unique_ptr<DeleteTask> delete_task =
- std::make_unique<DeleteTask>(fp_, partition_name);
- delete_task->Run();
}
+ tasks.emplace_back(std::make_unique<DeleteTask>(fp_, partition_name));
}
- resize_tasks.emplace_back(
- std::make_unique<ResizeTask>(fp_, image->part_name, "0", slot));
+ tasks.emplace_back(std::make_unique<ResizeTask>(fp_, image->part_name, "0", slot));
}
- for (auto& i : resize_tasks) {
- i->Run();
- }
+ }
+ for (auto& task : tasks) {
+ task->Run();
}
FlashImages(os_images_);
}
@@ -1665,17 +1654,17 @@
}
void FlashAllTool::DetermineSlot() {
- if (fp_->slot.empty()) {
+ if (fp_->slot_override.empty()) {
fp_->current_slot = get_current_slot();
} else {
- fp_->current_slot = fp_->slot;
+ fp_->current_slot = fp_->slot_override;
}
if (fp_->skip_secondary) {
return;
}
- if (fp_->slot != "" && fp_->slot != "all") {
- fp_->secondary_slot = get_other_slot(fp_->slot);
+ if (fp_->slot_override != "" && fp_->slot_override != "all") {
+ fp_->secondary_slot = get_other_slot(fp_->slot_override);
} else {
fp_->secondary_slot = get_other_slot();
}
@@ -1689,7 +1678,7 @@
void FlashAllTool::CollectImages() {
for (size_t i = 0; i < images.size(); ++i) {
- std::string slot = fp_->slot;
+ std::string slot = fp_->slot_override;
if (images[i].IsSecondary()) {
if (fp_->skip_secondary) {
continue;
@@ -1729,7 +1718,8 @@
if (is_logical(partition_name)) {
fb->ResizePartition(partition_name, std::to_string(buf->image_size));
}
- flash_buf(partition_name.c_str(), buf);
+
+ flash_buf(partition_name.c_str(), buf, is_vbmeta_partition(partition_name));
};
do_for_partitions(image.part_name, slot, flash, false);
}
@@ -1904,7 +1894,7 @@
if (!load_buf_fd(std::move(fd), &buf)) {
die("Cannot read image: %s", strerror(errno));
}
- flash_buf(partition, &buf);
+ flash_buf(partition, &buf, is_vbmeta_partition(partition));
return;
failed:
@@ -1974,7 +1964,7 @@
auto image_path = temp_dir.path + "/"s + image_name;
auto flash = [&](const std::string& partition_name) {
- do_flash(partition_name.c_str(), image_path.c_str());
+ do_flash(partition_name.c_str(), image_path.c_str(), false);
};
do_for_partitions(partition, slot, flash, force_slot);
@@ -2027,7 +2017,6 @@
std::unique_ptr<FlashingPlan> fp = std::make_unique<FlashingPlan>();
int longindex;
- std::string slot_override;
std::string next_active;
g_boot_img_hdr.kernel_addr = 0x00008000;
@@ -2100,7 +2089,7 @@
} else if (name == "skip-secondary") {
fp->skip_secondary = true;
} else if (name == "slot") {
- slot_override = optarg;
+ fp->slot_override = optarg;
} else if (name == "dtb-offset") {
g_boot_img_hdr.dtb_addr = strtoul(optarg, 0, 16);
} else if (name == "tags-offset") {
@@ -2192,12 +2181,12 @@
const double start = now();
- if (slot_override != "") slot_override = verify_slot(slot_override);
+ if (fp->slot_override != "") fp->slot_override = verify_slot(fp->slot_override);
if (next_active != "") next_active = verify_slot(next_active, false);
if (fp->wants_set_active) {
if (next_active == "") {
- if (slot_override == "") {
+ if (fp->slot_override == "") {
std::string current_slot;
if (fb->GetVar("current-slot", ¤t_slot) == fastboot::SUCCESS) {
if (current_slot[0] == '_') current_slot.erase(0, 1);
@@ -2206,7 +2195,7 @@
fp->wants_set_active = false;
}
} else {
- next_active = verify_slot(slot_override, false);
+ next_active = verify_slot(fp->slot_override, false);
}
}
}
@@ -2231,7 +2220,7 @@
fb->Erase(partition);
};
- do_for_partitions(partition, slot_override, erase, true);
+ do_for_partitions(partition, fp->slot_override, erase, true);
} else if (android::base::StartsWith(command, "format")) {
// Parsing for: "format[:[type][:[size]]]"
// Some valid things:
@@ -2251,7 +2240,7 @@
auto format = [&](const std::string& partition) {
fb_perform_format(partition, 0, type_override, size_override, fp->fs_options);
};
- do_for_partitions(partition, slot_override, format, true);
+ do_for_partitions(partition, fp->slot_override, format, true);
} else if (command == "signature") {
std::string filename = next_arg(&args);
std::vector<char> data;
@@ -2265,7 +2254,7 @@
if (args.size() == 1) {
std::string reboot_target = next_arg(&args);
reboot_task = std::make_unique<RebootTask>(fp.get(), reboot_target);
- } else {
+ } else if (!fp->skip_reboot) {
reboot_task = std::make_unique<RebootTask>(fp.get());
}
if (!args.empty()) syntax_error("junk after reboot command");
@@ -2295,7 +2284,8 @@
fname = find_item(pname);
}
if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
- FlashTask task(slot_override, pname, fname);
+
+ FlashTask task(fp->slot_override, pname, fname, is_vbmeta_partition(pname));
task.Run();
} else if (command == "flash:raw") {
std::string partition = next_arg(&args);
@@ -2309,9 +2299,9 @@
auto flashraw = [&data](const std::string& partition) {
fb->FlashPartition(partition, data);
};
- do_for_partitions(partition, slot_override, flashraw, true);
+ do_for_partitions(partition, fp->slot_override, flashraw, true);
} else if (command == "flashall") {
- if (slot_override == "all") {
+ if (fp->slot_override == "all") {
fprintf(stderr,
"Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
fp->skip_secondary = true;
@@ -2319,9 +2309,11 @@
} else {
do_flashall(fp.get());
}
- reboot_task = std::make_unique<RebootTask>(fp.get());
+ if (!fp->skip_reboot) {
+ reboot_task = std::make_unique<RebootTask>(fp.get());
+ }
} else if (command == "update") {
- bool slot_all = (slot_override == "all");
+ bool slot_all = (fp->slot_override == "all");
if (slot_all) {
fprintf(stderr,
"Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
@@ -2331,7 +2323,9 @@
filename = next_arg(&args);
}
do_update(filename.c_str(), fp.get());
- reboot_task = std::make_unique<RebootTask>(fp.get());
+ if (!fp->skip_reboot) {
+ reboot_task = std::make_unique<RebootTask>(fp.get());
+ }
} else if (command == FB_CMD_SET_ACTIVE) {
std::string slot = verify_slot(next_arg(&args), false);
fb->SetActive(slot);
@@ -2370,7 +2364,7 @@
std::string partition = next_arg(&args);
std::string size = next_arg(&args);
std::unique_ptr<ResizeTask> resize_task =
- std::make_unique<ResizeTask>(fp.get(), partition, size, slot_override);
+ std::make_unique<ResizeTask>(fp.get(), partition, size, fp->slot_override);
resize_task->Run();
} else if (command == "gsi") {
std::string arg = next_arg(&args);
@@ -2388,7 +2382,7 @@
} else {
image = next_arg(&args);
}
- do_wipe_super(image, slot_override);
+ do_wipe_super(image, fp->slot_override);
} else if (command == "snapshot-update") {
std::string arg;
if (!args.empty()) {
@@ -2401,7 +2395,7 @@
} else if (command == FB_CMD_FETCH) {
std::string partition = next_arg(&args);
std::string outfile = next_arg(&args);
- do_fetch(partition, slot_override, outfile);
+ do_fetch(partition, fp->slot_override, outfile);
} else {
syntax_error("unknown command %s", command.c_str());
}
@@ -2420,7 +2414,7 @@
if (fp->wants_set_active) {
fb->SetActive(next_active);
}
- if (reboot_task && !fp->skip_reboot) {
+ if (reboot_task) {
reboot_task->Run();
}
fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index ed33481..6462a4f 100644
--- a/fastboot/fastboot.h
+++ b/fastboot/fastboot.h
@@ -80,7 +80,7 @@
bool skip_secondary = false;
bool force_flash = false;
- std::string slot;
+ std::string slot_override;
std::string current_slot;
std::string secondary_slot;
fastboot::FastBootDriver* fb;
@@ -88,7 +88,7 @@
bool should_flash_in_userspace(const std::string& partition_name);
bool is_userspace_fastboot();
-void do_flash(const char* pname, const char* fname);
+void do_flash(const char* pname, const char* fname, const bool apply_vbmeta);
void do_for_partitions(const std::string& part, const std::string& slot,
const std::function<void(const std::string&)>& func, bool force_slot);
std::string find_item(const std::string& item);
diff --git a/fastboot/fuzzer/Android.bp b/fastboot/fuzzer/Android.bp
index 1b59e4a..a898070 100644
--- a/fastboot/fuzzer/Android.bp
+++ b/fastboot/fuzzer/Android.bp
@@ -58,5 +58,13 @@
"android-media-fuzzing-reports@google.com",
],
componentid: 533764,
+ hotlists: [
+ "4593311",
+ ],
+ description: "The fuzzer targets the APIs of libfastboot library",
+ vector: "local_no_privileges_required",
+ service_privilege: "host_only",
+ users: "single_user",
+ fuzzed_code_usage: "shipped",
},
}
diff --git a/fastboot/task.cpp b/fastboot/task.cpp
index c70139b..de48a16 100644
--- a/fastboot/task.cpp
+++ b/fastboot/task.cpp
@@ -19,14 +19,12 @@
#include "filesystem.h"
#include "super_flash_helper.h"
-using namespace std::string_literals;
+#include <android-base/parseint.h>
-FlashTask::FlashTask(const std::string& slot, const std::string& pname)
- : pname_(pname), fname_(find_item(pname)), slot_(slot) {
- if (fname_.empty()) die("cannot determine image filename for '%s'", pname_.c_str());
-}
-FlashTask::FlashTask(const std::string& _slot, const std::string& _pname, const std::string& _fname)
- : pname_(_pname), fname_(_fname), slot_(_slot) {}
+using namespace std::string_literals;
+FlashTask::FlashTask(const std::string& _slot, const std::string& _pname, const std::string& _fname,
+ const bool apply_vbmeta)
+ : pname_(_pname), fname_(_fname), slot_(_slot), apply_vbmeta_(apply_vbmeta) {}
void FlashTask::Run() {
auto flash = [&](const std::string& partition) {
@@ -39,13 +37,13 @@
"And try again. If you are intentionally trying to "
"overwrite a fixed partition, use --force.");
}
- do_flash(partition.c_str(), fname_.c_str());
+ do_flash(partition.c_str(), fname_.c_str(), apply_vbmeta_);
};
do_for_partitions(pname_, slot_, flash, true);
}
-RebootTask::RebootTask(FlashingPlan* fp) : fp_(fp){};
-RebootTask::RebootTask(FlashingPlan* fp, const std::string& reboot_target)
+RebootTask::RebootTask(const FlashingPlan* fp) : fp_(fp){};
+RebootTask::RebootTask(const FlashingPlan* fp, const std::string& reboot_target)
: reboot_target_(reboot_target), fp_(fp){};
void RebootTask::Run() {
@@ -70,14 +68,18 @@
FlashSuperLayoutTask::FlashSuperLayoutTask(const std::string& super_name,
std::unique_ptr<SuperFlashHelper> helper,
- SparsePtr sparse_layout)
+ SparsePtr sparse_layout, uint64_t super_size)
: super_name_(super_name),
helper_(std::move(helper)),
- sparse_layout_(std::move(sparse_layout)) {}
+ sparse_layout_(std::move(sparse_layout)),
+ super_size_(super_size) {}
void FlashSuperLayoutTask::Run() {
+ // Use the reported super partition size as the upper limit, rather than
+ // sparse_file_len, which (1) can fail and (2) is kind of expensive, since
+ // it will map in all of the embedded fds.
std::vector<SparsePtr> files;
- if (int limit = get_sparse_limit(sparse_file_len(sparse_layout_.get(), false, false))) {
+ if (int limit = get_sparse_limit(super_size_)) {
files = resparse_file(sparse_layout_.get(), limit);
} else {
files.emplace_back(std::move(sparse_layout_));
@@ -93,7 +95,7 @@
LOG(VERBOSE) << "Cannot optimize flashing super on non-AB device";
return nullptr;
}
- if (fp->slot == "all") {
+ if (fp->slot_override == "all") {
LOG(VERBOSE) << "Cannot optimize flashing super for all slots";
return nullptr;
}
@@ -111,12 +113,19 @@
if (fp->fb->GetVar("super-partition-name", &super_name) != fastboot::SUCCESS) {
super_name = "super";
}
- std::string partition_size_str;
+ uint64_t partition_size;
+ std::string partition_size_str;
if (fp->fb->GetVar("partition-size:" + super_name, &partition_size_str) != fastboot::SUCCESS) {
LOG(VERBOSE) << "Cannot optimize super flashing: could not determine super partition";
return nullptr;
}
+ partition_size_str = fb_fix_numeric_var(partition_size_str);
+ if (!android::base::ParseUint(partition_size_str, &partition_size)) {
+ LOG(VERBOSE) << "Could not parse " << super_name << " size: " << partition_size_str;
+ return nullptr;
+ }
+
std::unique_ptr<SuperFlashHelper> helper = std::make_unique<SuperFlashHelper>(*fp->source);
if (!helper->Open(fd)) {
return nullptr;
@@ -140,10 +149,11 @@
};
os_images.erase(std::remove_if(os_images.begin(), os_images.end(), remove_if_callback),
os_images.end());
- return std::make_unique<FlashSuperLayoutTask>(super_name, std::move(helper), std::move(s));
+ return std::make_unique<FlashSuperLayoutTask>(super_name, std::move(helper), std::move(s),
+ partition_size);
}
-UpdateSuperTask::UpdateSuperTask(FlashingPlan* fp) : fp_(fp) {}
+UpdateSuperTask::UpdateSuperTask(const FlashingPlan* fp) : fp_(fp) {}
void UpdateSuperTask::Run() {
unique_fd fd = fp_->source->OpenFile("super_empty.img");
@@ -167,7 +177,7 @@
fp_->fb->RawCommand(command, "Updating super partition");
}
-ResizeTask::ResizeTask(FlashingPlan* fp, const std::string& pname, const std::string& size,
+ResizeTask::ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size,
const std::string& slot)
: fp_(fp), pname_(pname), size_(size), slot_(slot) {}
@@ -180,20 +190,24 @@
do_for_partitions(pname_, slot_, resize_partition, false);
}
-DeleteTask::DeleteTask(FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
+DeleteTask::DeleteTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
void DeleteTask::Run() {
fp_->fb->DeletePartition(pname_);
}
-WipeTask::WipeTask(FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
+WipeTask::WipeTask(const FlashingPlan* fp, const std::string& pname) : fp_(fp), pname_(pname){};
void WipeTask::Run() {
std::string partition_type;
if (fp_->fb->GetVar("partition-type:" + pname_, &partition_type) != fastboot::SUCCESS) {
+ LOG(ERROR) << "wipe task partition not found: " << pname_;
return;
}
if (partition_type.empty()) return;
- fp_->fb->Erase(pname_);
+ if (fp_->fb->Erase(pname_) != fastboot::SUCCESS) {
+ LOG(ERROR) << "wipe task erase failed with partition: " << pname_;
+ return;
+ }
fb_perform_format(pname_, 1, partition_type, "", fp_->fs_options);
}
diff --git a/fastboot/task.h b/fastboot/task.h
index 149c34c..e80f88d 100644
--- a/fastboot/task.h
+++ b/fastboot/task.h
@@ -32,8 +32,8 @@
class FlashTask : public Task {
public:
- FlashTask(const std::string& slot, const std::string& pname);
- FlashTask(const std::string& slot, const std::string& pname, const std::string& fname);
+ FlashTask(const std::string& slot, const std::string& pname, const std::string& fname,
+ const bool apply_vbmeta);
void Run() override;
@@ -41,12 +41,13 @@
const std::string pname_;
const std::string fname_;
const std::string slot_;
+ const bool apply_vbmeta_;
};
class RebootTask : public Task {
public:
- RebootTask(FlashingPlan* fp);
- RebootTask(FlashingPlan* fp, const std::string& reboot_target);
+ RebootTask(const FlashingPlan* fp);
+ RebootTask(const FlashingPlan* fp, const std::string& reboot_target);
void Run() override;
private:
@@ -57,7 +58,7 @@
class FlashSuperLayoutTask : public Task {
public:
FlashSuperLayoutTask(const std::string& super_name, std::unique_ptr<SuperFlashHelper> helper,
- SparsePtr sparse_layout);
+ SparsePtr sparse_layout, uint64_t super_size);
static std::unique_ptr<FlashSuperLayoutTask> Initialize(FlashingPlan* fp,
std::vector<ImageEntry>& os_images);
using ImageEntry = std::pair<const Image*, std::string>;
@@ -67,11 +68,12 @@
const std::string super_name_;
std::unique_ptr<SuperFlashHelper> helper_;
SparsePtr sparse_layout_;
+ uint64_t super_size_;
};
class UpdateSuperTask : public Task {
public:
- UpdateSuperTask(FlashingPlan* fp);
+ UpdateSuperTask(const FlashingPlan* fp);
void Run() override;
private:
@@ -80,7 +82,7 @@
class ResizeTask : public Task {
public:
- ResizeTask(FlashingPlan* fp, const std::string& pname, const std::string& size,
+ ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size,
const std::string& slot);
void Run() override;
@@ -93,7 +95,7 @@
class DeleteTask : public Task {
public:
- DeleteTask(FlashingPlan* _fp, const std::string& _pname);
+ DeleteTask(const FlashingPlan* _fp, const std::string& _pname);
void Run() override;
private:
@@ -103,7 +105,7 @@
class WipeTask : public Task {
public:
- WipeTask(FlashingPlan* fp, const std::string& pname);
+ WipeTask(const FlashingPlan* fp, const std::string& pname);
void Run() override;
private:
diff --git a/fastboot/util.cpp b/fastboot/util.cpp
index ded54a5..e03012a 100644
--- a/fastboot/util.cpp
+++ b/fastboot/util.cpp
@@ -33,6 +33,9 @@
#include <sys/stat.h>
#include <sys/time.h>
+#include <android-base/parseint.h>
+#include <android-base/strings.h>
+
#include "util.h"
using android::base::borrowed_fd;
@@ -106,3 +109,12 @@
}
return sb.st_size;
}
+
+std::string fb_fix_numeric_var(std::string var) {
+ // Some bootloaders (angler, for example), send spurious leading whitespace.
+ var = android::base::Trim(var);
+ // Some bootloaders (hammerhead, for example) use implicit hex.
+ // This code used to use strtol with base 16.
+ if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
+ return var;
+}
diff --git a/fastboot/util.h b/fastboot/util.h
index 290d0d5..fdbc1d6 100644
--- a/fastboot/util.h
+++ b/fastboot/util.h
@@ -30,6 +30,7 @@
const std::string& partition_name);
bool is_sparse_file(android::base::borrowed_fd fd);
int64_t get_file_size(android::base::borrowed_fd fd);
+std::string fb_fix_numeric_var(std::string var);
class ImageSource {
public:
diff --git a/fs_mgr/TEST_MAPPING b/fs_mgr/TEST_MAPPING
index b6710d5..db27cf0 100644
--- a/fs_mgr/TEST_MAPPING
+++ b/fs_mgr/TEST_MAPPING
@@ -28,9 +28,6 @@
"name": "vabc_legacy_tests"
},
{
- "name": "libsnapshot_fuzzer_test"
- },
- {
"name": "cow_api_test"
}
],
diff --git a/fs_mgr/libfs_avb/avb_util.cpp b/fs_mgr/libfs_avb/avb_util.cpp
index 85dbb36..90b65ce 100644
--- a/fs_mgr/libfs_avb/avb_util.cpp
+++ b/fs_mgr/libfs_avb/avb_util.cpp
@@ -101,7 +101,6 @@
if (wait_for_verity_dev) timeout = 1s;
std::string dev_path;
- const std::string mount_point(Basename(fstab_entry->mount_point));
const std::string device_name(GetVerityDeviceName(*fstab_entry));
android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance();
if (!dm.CreateDevice(device_name, table, &dev_path, timeout)) {
diff --git a/fs_mgr/libsnapshot/snapuserd/snapuserd_daemon.cpp b/fs_mgr/libsnapshot/snapuserd/snapuserd_daemon.cpp
index 0557214..36dad33 100644
--- a/fs_mgr/libsnapshot/snapuserd/snapuserd_daemon.cpp
+++ b/fs_mgr/libsnapshot/snapuserd/snapuserd_daemon.cpp
@@ -110,7 +110,7 @@
for (int i = arg_start; i < argc; i++) {
auto parts = android::base::Split(argv[i], ",");
if (parts.size() != 4) {
- LOG(ERROR) << "Malformed message, expected three sub-arguments.";
+ LOG(ERROR) << "Malformed message, expected four sub-arguments.";
return false;
}
auto handler = user_server_.AddHandler(parts[0], parts[1], parts[2], parts[3]);
diff --git a/fs_mgr/tools/dmctl.cpp b/fs_mgr/tools/dmctl.cpp
index 10efd0c..7273087 100644
--- a/fs_mgr/tools/dmctl.cpp
+++ b/fs_mgr/tools/dmctl.cpp
@@ -53,6 +53,7 @@
std::cerr << " getpath <dm-name>" << std::endl;
std::cerr << " getuuid <dm-name>" << std::endl;
std::cerr << " info <dm-name>" << std::endl;
+ std::cerr << " replace <dm-name> <targets...>" << std::endl;
std::cerr << " status <dm-name>" << std::endl;
std::cerr << " resume <dm-name>" << std::endl;
std::cerr << " suspend <dm-name>" << std::endl;
diff --git a/healthd/healthd_mode_charger.cpp b/healthd/healthd_mode_charger.cpp
index 1ce174b..26af13b 100644
--- a/healthd/healthd_mode_charger.cpp
+++ b/healthd/healthd_mode_charger.cpp
@@ -88,7 +88,7 @@
#define POWER_ON_KEY_TIME (2 * MSEC_PER_SEC)
#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
#define UNPLUGGED_DISPLAY_TIME (3 * MSEC_PER_SEC)
-#define MAX_BATT_LEVEL_WAIT_TIME (3 * MSEC_PER_SEC)
+#define MAX_BATT_LEVEL_WAIT_TIME (5 * MSEC_PER_SEC)
#define UNPLUGGED_SHUTDOWN_TIME_PROP "ro.product.charger.unplugged_shutdown_time"
#define LAST_KMSG_MAX_SZ (32 * 1024)
@@ -620,6 +620,18 @@
kick_animation(&batt_anim_);
}
health_info_ = health_info;
+
+ if (property_get_bool("ro.charger_mode_autoboot", false)) {
+ if (health_info_.battery_level >= boot_min_cap_) {
+ if (property_get_bool("ro.enable_boot_charger_mode", false)) {
+ LOGW("booting from charger mode\n");
+ property_set("sys.boot_from_charger_mode", "1");
+ } else {
+ LOGW("Battery SOC = %d%%, Automatically rebooting\n", health_info_.battery_level);
+ reboot(RB_AUTOBOOT);
+ }
+ }
+ }
}
int Charger::OnPrepareToWait(void) {
diff --git a/init/Android.bp b/init/Android.bp
index 1aba4b3..7b52903 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -502,11 +502,13 @@
"libbase",
"libcutils",
"libselinux",
- "libhidl-gen-utils",
"liblog",
"libprocessgroup",
"libprotobuf-cpp-lite",
],
+ static_libs: [
+ "libhidl-gen-utils",
+ ],
}
cc_library_static {
diff --git a/init/README.md b/init/README.md
index b006365..6bdff4a 100644
--- a/init/README.md
+++ b/init/README.md
@@ -642,17 +642,17 @@
the current SELinux policy or its parent if not specified in the policy. If
the directory exists, its security context will not be changed (even if
different from the policy).
-
- > _action_ can be one of:
- * `None`: take no encryption action; directory will be encrypted if parent is.
- * `Require`: encrypt directory, abort boot process if encryption fails
- * `Attempt`: try to set an encryption policy, but continue if it fails
- * `DeleteIfNecessary`: recursively delete directory if necessary to set
- encryption policy.
-
- > _key_ can be one of:
- * `ref`: use the systemwide DE key
- * `per_boot_ref`: use the key freshly generated on each boot.
+>
+> _action_ can be one of:
+> * `None`: take no encryption action; directory will be encrypted if parent is.
+> * `Require`: encrypt directory, abort boot process if encryption fails
+> * `Attempt`: try to set an encryption policy, but continue if it fails
+> * `DeleteIfNecessary`: recursively delete directory if necessary to set
+> encryption policy.
+>
+> _key_ can be one of:
+> * `ref`: use the systemwide DE key
+> * `per_boot_ref`: use the key freshly generated on each boot.
`mount_all [ <fstab> ] [--<option>]`
> Calls fs\_mgr\_mount\_all on the given fs\_mgr-format fstab with optional
diff --git a/init/fuzzer/init_parser_fuzzer.cpp b/init/fuzzer/init_parser_fuzzer.cpp
index e6a78a2..dc76465 100644
--- a/init/fuzzer/init_parser_fuzzer.cpp
+++ b/init/fuzzer/init_parser_fuzzer.cpp
@@ -125,7 +125,7 @@
std::string path = fdp_.ConsumeBool() ? fdp_.PickValueInArray(kValidPaths)
: fdp_.ConsumeRandomLengthString(kMaxBytes);
parser.ParseConfig(path);
- parser.ParseConfigFileInsecure(path);
+ parser.ParseConfigFileInsecure(path, false /* follow_symlinks */);
}
void InitParserFuzzer::Process() {
diff --git a/init/host_init_verifier.cpp b/init/host_init_verifier.cpp
index db127d3..f070776 100644
--- a/init/host_init_verifier.cpp
+++ b/init/host_init_verifier.cpp
@@ -326,7 +326,9 @@
}
}
} else {
- if (!parser.ParseConfigFileInsecure(*argv)) {
+ if (!parser.ParseConfigFileInsecure(*argv, true /* follow_symlinks */)) {
+ // Follow symlinks as inputs during build execution in Bazel's
+ // execution root are symlinks, unlike Soong or Make.
LOG(ERROR) << "Failed to open init rc script '" << *argv << "'";
return EXIT_FAILURE;
}
diff --git a/init/init_test.cpp b/init/init_test.cpp
index 305bf95..0fc3ffc 100644
--- a/init/init_test.cpp
+++ b/init/init_test.cpp
@@ -204,6 +204,10 @@
GTEST_SKIP() << "Must run on userdebug/eng builds. b/262090304";
return;
}
+ if (getuid() != 0) {
+ GTEST_SKIP() << "Must be run as root.";
+ return;
+ }
std::string init_script = R"init(
service console /system/bin/sh
class core
diff --git a/init/parser.cpp b/init/parser.cpp
index adb41ad..8c0bb2b 100644
--- a/init/parser.cpp
+++ b/init/parser.cpp
@@ -131,9 +131,9 @@
}
}
-bool Parser::ParseConfigFileInsecure(const std::string& path) {
+bool Parser::ParseConfigFileInsecure(const std::string& path, bool follow_symlinks = false) {
std::string config_contents;
- if (!android::base::ReadFileToString(path, &config_contents)) {
+ if (!android::base::ReadFileToString(path, &config_contents, follow_symlinks)) {
return false;
}
diff --git a/init/parser.h b/init/parser.h
index 980ae0c..8e5bca7 100644
--- a/init/parser.h
+++ b/init/parser.h
@@ -77,7 +77,7 @@
void AddSingleLineParser(const std::string& prefix, LineCallback callback);
// Host init verifier check file permissions.
- bool ParseConfigFileInsecure(const std::string& path);
+ bool ParseConfigFileInsecure(const std::string& path, bool follow_symlinks);
size_t parse_error_count() const { return parse_error_count_; }
diff --git a/init/selinux.cpp b/init/selinux.cpp
index 062ed39..907eb80 100644
--- a/init/selinux.cpp
+++ b/init/selinux.cpp
@@ -761,15 +761,7 @@
constexpr size_t kKlogMessageSize = 1024;
-void SelinuxAvcLog(char* buf, size_t buf_len) {
- CHECK_GT(buf_len, 0u);
-
- size_t str_len = strnlen(buf, buf_len);
- // trim newline at end of string
- if (buf[str_len - 1] == '\n') {
- buf[str_len - 1] = '\0';
- }
-
+void SelinuxAvcLog(char* buf) {
struct NetlinkMessage {
nlmsghdr hdr;
char buf[kKlogMessageSize];
@@ -835,8 +827,17 @@
if (length_written <= 0) {
return 0;
}
+
+ // libselinux log messages usually contain a new line character, while
+ // Android LOG() does not expect it. Remove it to avoid empty lines in
+ // the log buffers.
+ size_t str_len = strlen(buf);
+ if (buf[str_len - 1] == '\n') {
+ buf[str_len - 1] = '\0';
+ }
+
if (type == SELINUX_AVC) {
- SelinuxAvcLog(buf, sizeof(buf));
+ SelinuxAvcLog(buf);
} else {
android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
}
diff --git a/init/service.cpp b/init/service.cpp
index 8456d1e..35beaad 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -453,6 +453,7 @@
// disable services requiring the console. For older kernels and boot
// images, not setting this at all will fall back to the old behavior
if (GetProperty("ro.boot.serialconsole", "") == "0") {
+ flags_ |= SVC_DISABLED;
return {};
}
diff --git a/init/test_kill_services/init_kill_services_test.cpp b/init/test_kill_services/init_kill_services_test.cpp
index 66a3328..5355703 100644
--- a/init/test_kill_services/init_kill_services_test.cpp
+++ b/init/test_kill_services/init_kill_services_test.cpp
@@ -29,8 +29,8 @@
const std::string initial_pid = GetProperty(pid_prop, "");
- EXPECT_EQ("running", GetProperty(status_prop, "")) << status_prop;
- EXPECT_NE("", initial_pid) << pid_prop;
+ ASSERT_EQ("running", GetProperty(status_prop, "")) << status_prop;
+ ASSERT_NE("", initial_pid) << pid_prop;
EXPECT_EQ(0, system(("kill -9 " + initial_pid).c_str()));
diff --git a/libcutils/fs_config.cpp b/libcutils/fs_config.cpp
index a6835fc..e46774b 100644
--- a/libcutils/fs_config.cpp
+++ b/libcutils/fs_config.cpp
@@ -83,6 +83,8 @@
{ 00751, AID_ROOT, AID_SHELL, 0, "product/apex/*/bin" },
{ 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
{ 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" },
+ { 00750, AID_ROOT, AID_SYSTEM, 0, "system/apex/com.android.tethering/bin/for-system" },
+ { 00750, AID_ROOT, AID_SYSTEM, 0, "system/apex/com.android.tethering.inprocess/bin/for-system" },
{ 00751, AID_ROOT, AID_SHELL, 0, "system/bin" },
{ 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
@@ -194,6 +196,8 @@
// the following files have enhanced capabilities and ARE included
// in user builds.
+ { 06755, AID_CLAT, AID_CLAT, 0, "system/apex/com.android.tethering/bin/for-system/clatd" },
+ { 06755, AID_CLAT, AID_CLAT, 0, "system/apex/com.android.tethering.inprocess/bin/for-system/clatd" },
{ 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND),
"system/bin/inputflinger" },
{ 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
diff --git a/libmodprobe/Android.bp b/libmodprobe/Android.bp
index 525a880..1d94a96 100644
--- a/libmodprobe/Android.bp
+++ b/libmodprobe/Android.bp
@@ -10,6 +10,7 @@
vendor_available: true,
ramdisk_available: true,
recovery_available: true,
+ host_supported: true,
srcs: [
"libmodprobe.cpp",
"libmodprobe_ext.cpp",
diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp
index 38f19ff..38eb92f 100644
--- a/libprocessgroup/processgroup.cpp
+++ b/libprocessgroup/processgroup.cpp
@@ -446,6 +446,9 @@
static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries,
int* max_processes) {
+ CHECK_GE(uid, 0);
+ CHECK_GT(initialPid, 0);
+
std::string hierarchy_root_path;
if (CgroupsAvailable()) {
CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path);
@@ -582,7 +585,8 @@
}
int createProcessGroup(uid_t uid, int initialPid, bool memControl) {
- std::string cgroup;
+ CHECK_GE(uid, 0);
+ CHECK_GT(initialPid, 0);
if (memControl && !UsePerAppMemcg()) {
PLOG(ERROR) << "service memory controls are used without per-process memory cgroup support";
@@ -600,6 +604,7 @@
}
}
+ std::string cgroup;
CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cgroup);
return createProcessGroupInternal(uid, initialPid, cgroup, true);
}
diff --git a/libprocessgroup/profiles/cgroups.json b/libprocessgroup/profiles/cgroups.json
index 3e4393d..d013ec8 100644
--- a/libprocessgroup/profiles/cgroups.json
+++ b/libprocessgroup/profiles/cgroups.json
@@ -1,13 +1,6 @@
{
"Cgroups": [
{
- "Controller": "blkio",
- "Path": "/dev/blkio",
- "Mode": "0775",
- "UID": "system",
- "GID": "system"
- },
- {
"Controller": "cpu",
"Path": "/dev/cpuctl",
"Mode": "0755",
@@ -39,6 +32,12 @@
{
"Controller": "freezer",
"Path": "."
+ },
+ {
+ "Controller": "io",
+ "Path": ".",
+ "NeedsActivation": true,
+ "Optional": true
}
]
}
diff --git a/libprocessgroup/profiles/task_profiles.json b/libprocessgroup/profiles/task_profiles.json
index e44d3bf..12f7b44 100644
--- a/libprocessgroup/profiles/task_profiles.json
+++ b/libprocessgroup/profiles/task_profiles.json
@@ -457,14 +457,6 @@
"Name": "LowIoPriority",
"Actions": [
{
- "Name": "JoinCgroup",
- "Params":
- {
- "Controller": "blkio",
- "Path": "background"
- }
- },
- {
"Name": "SetAttribute",
"Params":
{
@@ -497,14 +489,6 @@
"Name": "NormalIoPriority",
"Actions": [
{
- "Name": "JoinCgroup",
- "Params":
- {
- "Controller": "blkio",
- "Path": ""
- }
- },
- {
"Name": "SetAttribute",
"Params":
{
@@ -537,14 +521,6 @@
"Name": "HighIoPriority",
"Actions": [
{
- "Name": "JoinCgroup",
- "Params":
- {
- "Controller": "blkio",
- "Path": ""
- }
- },
- {
"Name": "SetAttribute",
"Params":
{
@@ -577,14 +553,6 @@
"Name": "MaxIoPriority",
"Actions": [
{
- "Name": "JoinCgroup",
- "Params":
- {
- "Controller": "blkio",
- "Path": ""
- }
- },
- {
"Name": "SetAttribute",
"Params":
{
diff --git a/libprocessgroup/task_profiles_test.cpp b/libprocessgroup/task_profiles_test.cpp
index 09ac44c..c449e95 100644
--- a/libprocessgroup/task_profiles_test.cpp
+++ b/libprocessgroup/task_profiles_test.cpp
@@ -43,7 +43,7 @@
}
struct mntent* mnt;
while ((mnt = getmntent(mnts.get()))) {
- if (strcmp(mnt->mnt_fsname, "cgroup2") == 0) {
+ if (strcmp(mnt->mnt_type, "cgroup2") == 0) {
return true;
}
}
diff --git a/libsparse/backed_block.cpp b/libsparse/backed_block.cpp
index 6229e7c..a0d1cde 100644
--- a/libsparse/backed_block.cpp
+++ b/libsparse/backed_block.cpp
@@ -315,6 +315,10 @@
bb->len = len;
bb->type = BACKED_BLOCK_FILE;
bb->file.filename = strdup(filename);
+ if (!bb->file.filename) {
+ free(bb);
+ return -ENOMEM;
+ }
bb->file.offset = offset;
bb->next = nullptr;
@@ -359,14 +363,17 @@
new_bb->len = bb->len - max_len;
new_bb->block = bb->block + max_len / bbl->block_size;
new_bb->next = bb->next;
- bb->next = new_bb;
- bb->len = max_len;
switch (bb->type) {
case BACKED_BLOCK_DATA:
new_bb->data.data = (char*)bb->data.data + max_len;
break;
case BACKED_BLOCK_FILE:
+ new_bb->file.filename = strdup(bb->file.filename);
+ if (!new_bb->file.filename) {
+ free(new_bb);
+ return -ENOMEM;
+ }
new_bb->file.offset += max_len;
break;
case BACKED_BLOCK_FD:
@@ -376,5 +383,7 @@
break;
}
+ bb->next = new_bb;
+ bb->len = max_len;
return 0;
}
diff --git a/libsparse/output_file.cpp b/libsparse/output_file.cpp
index cb5d730..08312e4 100644
--- a/libsparse/output_file.cpp
+++ b/libsparse/output_file.cpp
@@ -58,6 +58,8 @@
#define container_of(inner, outer_t, elem) ((outer_t*)((char*)(inner)-offsetof(outer_t, elem)))
+static constexpr size_t kMaxMmapSize = 256 * 1024 * 1024;
+
struct output_file_ops {
int (*open)(struct output_file*, int fd);
int (*skip)(struct output_file*, int64_t);
@@ -71,6 +73,7 @@
int (*write_fill_chunk)(struct output_file* out, uint64_t len, uint32_t fill_val);
int (*write_skip_chunk)(struct output_file* out, uint64_t len);
int (*write_end_chunk)(struct output_file* out);
+ int (*write_fd_chunk)(struct output_file* out, uint64_t len, int fd, int64_t offset);
};
struct output_file {
@@ -318,6 +321,26 @@
return 0;
}
+template <typename T>
+static bool write_fd_chunk_range(int fd, int64_t offset, uint64_t len, T callback) {
+ uint64_t bytes_written = 0;
+ int64_t current_offset = offset;
+ while (bytes_written < len) {
+ size_t mmap_size = std::min(static_cast<uint64_t>(kMaxMmapSize), len - bytes_written);
+ auto m = android::base::MappedFile::FromFd(fd, current_offset, mmap_size, PROT_READ);
+ if (!m) {
+ error("failed to mmap region of length %zu", mmap_size);
+ return false;
+ }
+ if (!callback(m->data(), mmap_size)) {
+ return false;
+ }
+ bytes_written += mmap_size;
+ current_offset += mmap_size;
+ }
+ return true;
+}
+
static int write_sparse_skip_chunk(struct output_file* out, uint64_t skip_len) {
chunk_header_t chunk_header;
int ret;
@@ -424,6 +447,61 @@
return 0;
}
+static int write_sparse_fd_chunk(struct output_file* out, uint64_t len, int fd, int64_t offset) {
+ chunk_header_t chunk_header;
+ uint64_t rnd_up_len, zero_len;
+ int ret;
+
+ /* Round up the data length to a multiple of the block size */
+ rnd_up_len = ALIGN(len, out->block_size);
+ zero_len = rnd_up_len - len;
+
+ /* Finally we can safely emit a chunk of data */
+ chunk_header.chunk_type = CHUNK_TYPE_RAW;
+ chunk_header.reserved1 = 0;
+ chunk_header.chunk_sz = rnd_up_len / out->block_size;
+ chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len;
+ ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
+
+ if (ret < 0) return -1;
+ bool ok = write_fd_chunk_range(fd, offset, len, [&ret, out](char* data, size_t size) -> bool {
+ ret = out->ops->write(out, data, size);
+ if (ret < 0) return false;
+ if (out->use_crc) {
+ out->crc32 = sparse_crc32(out->crc32, data, size);
+ }
+ return true;
+ });
+ if (!ok) return -1;
+ if (zero_len) {
+ uint64_t len = zero_len;
+ uint64_t write_len;
+ while (len) {
+ write_len = std::min(len, (uint64_t)FILL_ZERO_BUFSIZE);
+ ret = out->ops->write(out, out->zero_buf, write_len);
+ if (ret < 0) {
+ return ret;
+ }
+ len -= write_len;
+ }
+
+ if (out->use_crc) {
+ uint64_t len = zero_len;
+ uint64_t write_len;
+ while (len) {
+ write_len = std::min(len, (uint64_t)FILL_ZERO_BUFSIZE);
+ out->crc32 = sparse_crc32(out->crc32, out->zero_buf, write_len);
+ len -= write_len;
+ }
+ }
+ }
+
+ out->cur_out_ptr += rnd_up_len;
+ out->chunk_cnt++;
+
+ return 0;
+}
+
int write_sparse_end_chunk(struct output_file* out) {
chunk_header_t chunk_header;
int ret;
@@ -454,6 +532,7 @@
.write_fill_chunk = write_sparse_fill_chunk,
.write_skip_chunk = write_sparse_skip_chunk,
.write_end_chunk = write_sparse_end_chunk,
+ .write_fd_chunk = write_sparse_fd_chunk,
};
static int write_normal_data_chunk(struct output_file* out, uint64_t len, void* data) {
@@ -495,6 +574,23 @@
return 0;
}
+static int write_normal_fd_chunk(struct output_file* out, uint64_t len, int fd, int64_t offset) {
+ int ret;
+ uint64_t rnd_up_len = ALIGN(len, out->block_size);
+
+ bool ok = write_fd_chunk_range(fd, offset, len, [&ret, out](char* data, size_t size) -> bool {
+ ret = out->ops->write(out, data, size);
+ return ret >= 0;
+ });
+ if (!ok) return ret;
+
+ if (rnd_up_len > len) {
+ ret = out->ops->skip(out, rnd_up_len - len);
+ }
+
+ return ret;
+}
+
static int write_normal_skip_chunk(struct output_file* out, uint64_t len) {
return out->ops->skip(out, len);
}
@@ -508,6 +604,7 @@
.write_fill_chunk = write_normal_fill_chunk,
.write_skip_chunk = write_normal_skip_chunk,
.write_end_chunk = write_normal_end_chunk,
+ .write_fd_chunk = write_normal_fd_chunk,
};
void output_file_close(struct output_file* out) {
@@ -670,10 +767,7 @@
}
int write_fd_chunk(struct output_file* out, uint64_t len, int fd, int64_t offset) {
- auto m = android::base::MappedFile::FromFd(fd, offset, len, PROT_READ);
- if (!m) return -errno;
-
- return out->sparse_ops->write_data_chunk(out, m->size(), m->data());
+ return out->sparse_ops->write_fd_chunk(out, len, fd, offset);
}
/* Write a contiguous region of data blocks from a file */
diff --git a/libsparse/sparse.cpp b/libsparse/sparse.cpp
index 396e7eb..ca7e5fe 100644
--- a/libsparse/sparse.cpp
+++ b/libsparse/sparse.cpp
@@ -260,8 +260,8 @@
return s->block_size;
}
-static struct backed_block* move_chunks_up_to_len(struct sparse_file* from, struct sparse_file* to,
- unsigned int len) {
+static int move_chunks_up_to_len(struct sparse_file* from, struct sparse_file* to, unsigned int len,
+ backed_block** out_bb) {
int64_t count = 0;
struct output_file* out_counter;
struct backed_block* last_bb = nullptr;
@@ -282,7 +282,7 @@
out_counter = output_file_open_callback(out_counter_write, &count, to->block_size, to->len, false,
true, 0, false);
if (!out_counter) {
- return nullptr;
+ return -1;
}
for (bb = start; bb; bb = backed_block_iter_next(bb)) {
@@ -319,7 +319,8 @@
out:
output_file_close(out_counter);
- return bb;
+ *out_bb = bb;
+ return 0;
}
int sparse_file_resparse(struct sparse_file* in_s, unsigned int max_len, struct sparse_file** out_s,
@@ -337,7 +338,15 @@
do {
s = sparse_file_new(in_s->block_size, in_s->len);
- bb = move_chunks_up_to_len(in_s, s, max_len);
+ if (move_chunks_up_to_len(in_s, s, max_len, &bb) < 0) {
+ sparse_file_destroy(s);
+ for (int i = 0; i < c && i < out_s_count; i++) {
+ sparse_file_destroy(out_s[i]);
+ out_s[i] = nullptr;
+ }
+ sparse_file_destroy(tmp);
+ return -1;
+ }
if (c < out_s_count) {
out_s[c] = s;
diff --git a/libstats/expresslog/Counter.cpp b/libstats/expresslog/Counter.cpp
index bee1303..9382041 100644
--- a/libstats/expresslog/Counter.cpp
+++ b/libstats/expresslog/Counter.cpp
@@ -28,5 +28,10 @@
stats_write(EXPRESS_EVENT_REPORTED, metricIdHash, amount);
}
+void Counter::logIncrementWithUid(const char* metricName, int32_t uid, int64_t amount) {
+ const int64_t metricIdHash = farmhash::Fingerprint64(metricName, strlen(metricName));
+ stats_write(EXPRESS_UID_EVENT_REPORTED, metricIdHash, amount, uid);
+}
+
} // namespace expresslog
} // namespace android
diff --git a/libstats/expresslog/Histogram.cpp b/libstats/expresslog/Histogram.cpp
index cb29a00..50bb343 100644
--- a/libstats/expresslog/Histogram.cpp
+++ b/libstats/expresslog/Histogram.cpp
@@ -71,5 +71,10 @@
stats_write(EXPRESS_HISTOGRAM_SAMPLE_REPORTED, mMetricIdHash, /*count*/ 1, binIndex);
}
+void Histogram::logSampleWithUid(int32_t uid, float sample) const {
+ const int binIndex = mBinOptions->getBinForSample(sample);
+ stats_write(EXPRESS_UID_HISTOGRAM_SAMPLE_REPORTED, mMetricIdHash, /*count*/ 1, binIndex, uid);
+}
+
} // namespace expresslog
} // namespace android
diff --git a/libstats/expresslog/include/Counter.h b/libstats/expresslog/include/Counter.h
index 57328f5..8d0ab6a 100644
--- a/libstats/expresslog/include/Counter.h
+++ b/libstats/expresslog/include/Counter.h
@@ -24,6 +24,8 @@
class Counter final {
public:
static void logIncrement(const char* metricId, int64_t amount = 1);
+
+ static void logIncrementWithUid(const char* metricId, int32_t uid, int64_t amount = 1);
};
} // namespace expresslog
diff --git a/libstats/expresslog/include/Histogram.h b/libstats/expresslog/include/Histogram.h
index 8fdc1b6..49aee3d 100644
--- a/libstats/expresslog/include/Histogram.h
+++ b/libstats/expresslog/include/Histogram.h
@@ -72,6 +72,11 @@
*/
void logSample(float sample) const;
+ /**
+ * Logs increment sample count for automatically calculated bin with uid
+ */
+ void logSampleWithUid(int32_t uid, float sample) const;
+
private:
const int64_t mMetricIdHash;
const std::shared_ptr<BinOptions> mBinOptions;
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 68191bb..b165778 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -221,26 +221,6 @@
write /dev/stune/nnapi-hal/schedtune.boost 1
write /dev/stune/nnapi-hal/schedtune.prefer_idle 1
- # Create blkio group and apply initial settings.
- # This feature needs kernel to support it, and the
- # device's init.rc must actually set the correct values.
- mkdir /dev/blkio/background
- chown system system /dev/blkio
- chown system system /dev/blkio/background
- chown system system /dev/blkio/tasks
- chown system system /dev/blkio/background/tasks
- chown system system /dev/blkio/cgroup.procs
- chown system system /dev/blkio/background/cgroup.procs
- chmod 0664 /dev/blkio/tasks
- chmod 0664 /dev/blkio/background/tasks
- chmod 0664 /dev/blkio/cgroup.procs
- chmod 0664 /dev/blkio/background/cgroup.procs
- write /dev/blkio/blkio.weight 1000
- write /dev/blkio/background/blkio.weight 200
- write /dev/blkio/background/blkio.bfq.weight 10
- write /dev/blkio/blkio.group_idle 0
- write /dev/blkio/background/blkio.group_idle 0
-
restorecon_recursive /mnt
mount configfs none /config nodev noexec nosuid
diff --git a/trusty/stats/aidl/Android.bp b/trusty/stats/aidl/Android.bp
new file mode 100644
index 0000000..078cc99
--- /dev/null
+++ b/trusty/stats/aidl/Android.bp
@@ -0,0 +1,39 @@
+// Copyright (C) 2023 The Android Open-Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+aidl_interface {
+ name: "android.trusty.stats.nw.setter",
+ unstable: true,
+ vendor_available: true,
+ srcs: [
+ "android/trusty/stats/nw/setter/IStatsSetter.aidl",
+ ],
+ imports: ["android.frameworks.stats-V1"],
+ backend: {
+ cpp: {
+ enabled: true,
+ },
+ java: {
+ enabled: false,
+ platform_apis: false,
+ },
+ ndk: {
+ enabled: false,
+ },
+ },
+}
diff --git a/trusty/stats/aidl/android/trusty/stats/nw/setter/IStatsSetter.aidl b/trusty/stats/aidl/android/trusty/stats/nw/setter/IStatsSetter.aidl
new file mode 100644
index 0000000..f44f4a3
--- /dev/null
+++ b/trusty/stats/aidl/android/trusty/stats/nw/setter/IStatsSetter.aidl
@@ -0,0 +1,28 @@
+//
+// Copyright (C) 2023 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package android.trusty.stats.nw.setter;
+
+import android.frameworks.stats.IStats;
+
+interface IStatsSetter {
+ /**
+ * Set the IStats interface facet.
+ *
+ * @param istats The IStats facet provided by the caller for the remote
+ * service to report IStats' VendorAtom.
+ */
+ void setInterface(in IStats istats);
+}
diff --git a/trusty/stats/test/Android.bp b/trusty/stats/test/Android.bp
new file mode 100644
index 0000000..6b2bce9
--- /dev/null
+++ b/trusty/stats/test/Android.bp
@@ -0,0 +1,47 @@
+// Copyright (C) 2021 The Android Open-Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+cc_test {
+ name: "trusty_stats_test",
+ vendor: true,
+ srcs: [
+ "stats_test.cpp",
+ ],
+ static_libs: [
+ "libgmock",
+ ],
+ shared_libs: [
+ "libbase",
+ "liblog",
+ "libtrusty",
+ "libbinder",
+ "libbinder_trusty",
+ "libutils",
+
+ // AIDL interface deps versions, please refer to below link
+ // https://source.android.com/docs/core/architecture/aidl/stable-aidl#module-naming-rules
+ "android.frameworks.stats-V1-cpp",
+ "android.trusty.stats.nw.setter-cpp",
+ ],
+ cflags: [
+ "-Wall",
+ "-Werror",
+ ],
+ require_root: true,
+ proprietary: true,
+}
diff --git a/trusty/stats/test/README.md b/trusty/stats/test/README.md
new file mode 100644
index 0000000..45e6af8
--- /dev/null
+++ b/trusty/stats/test/README.md
@@ -0,0 +1,97 @@
+# Development Notes
+
+* First get [repo_pull.py and gerrit.py](https://android.googlesource.com/platform/development/+/master/tools/repo_pull/) from aosp.
+
+* Although this repo is not currently in Trusty’s manifest, it’s sufficient to copy these two python scripts to the root of the Trusty project and run them from there. Make sure to follow the [repo_pull installation](https://android.googlesource.com/platform/development/+/master/tools/repo_pull/#installation) steps if necessary.
+
+## Build
+
+Build Android:
+
+```sh
+source build/envsetup.sh
+lunch qemu_trusty_arm64-userdebug
+m
+```
+
+Build Trusty:
+
+```sh
+./trusty/vendor/google/aosp/scripts/build.py qemu-generic-arm64-test-debug --skip-tests 2>stderr.log
+```
+
+## Trusty PORT_TEST
+
+On QEmu:
+
+```sh
+./build-root/build-qemu-generic-arm64-test-debug/run --headless --boot-test "com.android.trusty.stats.test" --verbose
+```
+
+On device: (Build for your device's debug target on both Adroid and Trusty)
+
+```sh
+/vendor/bin/trusty-ut-ctrl -D /dev/trusty-ipc-dev0 "com.android.trusty.stats.test"
+```
+
+On device, in a loop:
+
+```sh
+cat << 'EOF' > metrics.sh
+#!/system/bin/sh
+TIMES=${1:-0}
+X=0
+while [ "$TIMES" -eq 0 -o "$TIMES" -gt "$X" ]
+do
+ echo "######################## stats.test $X " $(( X++ ));
+ /vendor/bin/trusty-ut-ctrl -D /dev/trusty-ipc-dev0 "com.android.trusty.stats.test"
+done
+EOF
+
+adb wait-for-device
+adb push metrics.sh /data/user/test/metrics.sh
+adb shell sh /data/user/test/metrics.sh
+```
+
+## Android Native Test
+
+On QEmu:
+
+```sh
+./build-root/build-qemu-generic-arm64-test-debug/run --headless --android $ANDROID_PROJECT_ROOT --shell-command "/data/nativetest64/vendor/trusty_stats_test/trusty_stats_test" --verbose
+```
+
+On device: (Build for your device's debug target on both Adroid and Trusty)
+
+```sh
+/data/nativetest64/vendor/trusty_stats_test/trusty_stats_test
+```
+
+On device, in a loop:
+
+```sh
+cat << 'EOF' > metrics-nw.sh
+#!/system/bin/sh
+TIMES=${1:-0}
+X=0
+while [ "$TIMES" -eq 0 -o "$TIMES" -gt "$X" ]
+do
+ echo "######################## stats.test $X " $(( X++ ));
+ /data/nativetest64/vendor/trusty_stats_test/trusty_stats_test
+done
+EOF
+
+adb wait-for-device
+adb push metrics.sh /data/user/test/metrics-nw.sh
+adb shell sh /data/user/test/metrics-nw.sh
+```
+
+## Trusty Backtrace analysis
+
+
+```
+$ export A2L=./prebuilts/clang/host/linux-x86/llvm-binutils-stable/llvm-addr2line
+$ export OD=./prebuilts/clang/host/linux-x86/llvm-binutils-stable/llvm-objdump
+$ $OD -d -C build-root/build-qemu-generic-arm64-test-debug/user_tasks/trusty/user/base/app/metrics/metrics.syms.elf > objdump.lst
+$ $A2L -e build-root/build-qemu-generic-arm64-test-debug/user_tasks/trusty/user/base/app/metrics/metrics.syms.elf 0xe5104
+```
diff --git a/trusty/stats/test/stats_test.cpp b/trusty/stats/test/stats_test.cpp
new file mode 100644
index 0000000..1edddeb
--- /dev/null
+++ b/trusty/stats/test/stats_test.cpp
@@ -0,0 +1,364 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <errno.h>
+#include <getopt.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/uio.h>
+#include <unistd.h>
+
+#include <condition_variable>
+#include <cstddef>
+#include <mutex>
+#include <queue>
+
+#include <android-base/expected.h>
+#include <android-base/logging.h>
+#include <android/frameworks/stats/BnStats.h>
+#include <android/frameworks/stats/IStats.h>
+#include <android/trusty/stats/nw/setter/IStatsSetter.h>
+#include <binder/RpcServer.h>
+#include <binder/RpcSession.h>
+#include <binder/RpcTransportRaw.h>
+#include <binder/RpcTransportTipcAndroid.h>
+#include <binder/RpcTrusty.h>
+#include <trusty/tipc.h>
+
+/** DOC:
+ * ./build-root/build-qemu-generic-arm64-test-debug/run \
+ * --android $ANDROID_PROJECT_ROOT \
+ * --headless --shell-command \
+ * "/data/nativetest64/vendor/trusty_stats_test/trusty_stats_test"
+ *
+ * adb -s emulator-5554 shell \
+ * /data/nativetest64/vendor/trusty_stats_test/trusty_stats_test
+ */
+using ::android::base::unique_fd;
+using ::android::binder::Status;
+using ::android::frameworks::stats::BnStats;
+using ::android::frameworks::stats::IStats;
+using ::android::frameworks::stats::VendorAtom;
+using ::android::frameworks::stats::VendorAtomValue;
+using ::android::trusty::stats::nw::setter::IStatsSetter;
+
+constexpr const char kTrustyDefaultDeviceName[] = "/dev/trusty-ipc-dev0";
+constexpr const char kTrustyStatsSetterTest[] =
+ "com.android.frameworks.stats.trusty.test.relayer.istats_setter";
+constexpr const char kTrustyStatsSetterMetrics[] =
+ "com.android.frameworks.stats.trusty.metrics.istats_setter";
+constexpr const char kTrustyStatsPortTest[] = "com.android.trusty.stats.test";
+constexpr const char kTrustyCrashPortTest[] = "com.android.trusty.crashtest";
+constexpr const char kTrustyCrasherUuid[] = "7ee4dddc-177a-420a-96ea-5d413d88228e:crasher";
+
+enum TrustyAtoms : int32_t {
+ TrustyAppCrashed = 100072,
+ TrustyError = 100145,
+ TrustyStorageError = 100146
+};
+
+enum TestMsgHeader : int32_t {
+ TEST_PASSED = 0,
+ TEST_FAILED = 1,
+ TEST_MESSAGE = 2,
+};
+
+namespace android {
+namespace trusty {
+namespace stats {
+
+class Stats : public BnStats {
+ public:
+ Stats() : BnStats() {}
+
+ Status reportVendorAtom(const VendorAtom& vendorAtom) override {
+ const char* atomIdStr = vendorAtomStr(vendorAtom.atomId);
+ ALOGD("Vendor atom reported of type: %s\n", atomIdStr);
+ std::lock_guard lock(mLock);
+ mQueueVendorAtom.push(vendorAtom);
+ mCondVar.notify_one();
+ return Status::ok();
+ }
+
+ status_t getVendorAtom(VendorAtom* pVendorAtom, int64_t waitForMs) {
+ std::unique_lock lock(mLock);
+ while (mQueueVendorAtom.empty()) {
+ auto rc = mCondVar.wait_for(lock, std::chrono::milliseconds(waitForMs));
+ if (rc == std::cv_status::timeout) {
+ return TIMED_OUT;
+ }
+ }
+ *pVendorAtom = mQueueVendorAtom.front();
+ mQueueVendorAtom.pop();
+ return NO_ERROR;
+ }
+
+ private:
+ const char* vendorAtomStr(int32_t atomId) {
+ switch (atomId) {
+ case TrustyAtoms::TrustyAppCrashed:
+ return "TrustyAtoms::TrustyAppCrashed";
+ case TrustyAtoms::TrustyError:
+ return "TrustyAtoms::TrustyError";
+ case TrustyAtoms::TrustyStorageError:
+ return "TrustyAtoms::TrustyStorageError";
+ default:
+ return "unknown TrustyAtoms type";
+ }
+ }
+ std::mutex mLock;
+ std::condition_variable mCondVar;
+ std::queue<VendorAtom> mQueueVendorAtom;
+};
+
+class TrustyStatsTestBase : public ::testing::Test {
+ protected:
+ TrustyStatsTestBase(std::string&& portNameStatsSetter, std::string&& portNamePortTest)
+ : mPortTestFd(-1),
+ mPortNameStatsSetter(std::move(portNameStatsSetter)),
+ mPortNamePortTest(std::move(portNamePortTest)) {}
+
+ void SetUp() override {
+ // Commenting out the server portion because we do not have any direct
+ // incoming call Calls from TA are currently being handled on the mSession's
+ // extra thread. android::sp<::android::RpcServer> server =
+ // ::android::RpcServer::make(::android::RpcTransportCtxFactoryRaw::make());
+
+ mStats = android::sp<Stats>::make();
+ // Increasing number of incoming threads on mSession to be able to receive
+ // callbacks
+ auto session_initializer = [](sp<RpcSession>& session) {
+ session->setMaxIncomingThreads(1);
+ };
+
+ ASSERT_FALSE(mSession);
+ mSession = RpcTrustyConnectWithSessionInitializer(
+ kTrustyDefaultDeviceName, mPortNameStatsSetter.c_str(), session_initializer);
+ ASSERT_TRUE(mSession);
+
+ auto root = mSession->getRootObject();
+ ASSERT_TRUE(root);
+ auto statsSetter = IStatsSetter::asInterface(root);
+ ASSERT_TRUE(statsSetter);
+ statsSetter->setInterface(mStats);
+ }
+ void TearDown() override {
+ // close connection to unitest app
+ if (mPortTestFd != -1) {
+ tipc_close(mPortTestFd);
+ }
+ mPortTestFd = -1;
+
+ if (mSession) {
+ // shutdownAndWait here races with sending out the DecStrong
+ // messages after reportVendorAtom returns, so we delay it a little
+ // bit to give the messages time to go out over the transport
+ usleep(50000);
+ ASSERT_TRUE(mSession->shutdownAndWait(true));
+ }
+ mSession.clear();
+ mStats.clear();
+ }
+ void StartPortTest() {
+ // connect to unitest app
+ mPortTestFd = tipc_connect(kTrustyDefaultDeviceName, mPortNamePortTest.c_str());
+ if (mPortTestFd < 0) {
+ ALOGE("Failed to connect to '%s' app: %s\n", kTrustyStatsPortTest,
+ strerror(-mPortTestFd));
+ }
+ ASSERT_GT(mPortTestFd, 0);
+ }
+ void WaitPortTestDone() {
+ // wait for test to complete
+ char rxBuf[1024];
+ const char prolog[] = "Trusty PORT_TEST:";
+ strncpy(rxBuf, prolog, sizeof(prolog) - 1);
+ char* pRxBuf = rxBuf + sizeof(prolog) - 1;
+ size_t remainingBufSize = sizeof(rxBuf) - sizeof(prolog) - 1;
+
+ ASSERT_NE(mPortTestFd, -1);
+ for (;;) {
+ int rc = read(mPortTestFd, pRxBuf, remainingBufSize);
+ ASSERT_GT(rc, 0);
+ ASSERT_LT(rc, (int)remainingBufSize);
+ if (pRxBuf[0] == TEST_PASSED) {
+ break;
+ } else if (pRxBuf[0] == TEST_FAILED) {
+ break;
+ } else if (pRxBuf[0] == TEST_MESSAGE) {
+ pRxBuf[0] = ' ';
+ write(STDOUT_FILENO, rxBuf, rc + sizeof(prolog) - 1);
+ } else {
+ ALOGE("Bad message header: %d\n", rxBuf[0]);
+ break;
+ }
+ }
+ ASSERT_EQ(pRxBuf[0], TEST_PASSED);
+ }
+
+ android::sp<Stats> mStats;
+
+ private:
+ android::sp<RpcSession> mSession;
+ int mPortTestFd;
+ std::string mPortNameStatsSetter;
+ std::string mPortNamePortTest;
+};
+
+class TrustyStatsTest : public TrustyStatsTestBase {
+ protected:
+ TrustyStatsTest() : TrustyStatsTestBase(kTrustyStatsSetterTest, kTrustyStatsPortTest) {}
+};
+
+class TrustyMetricsCrashTest : public TrustyStatsTestBase {
+ protected:
+ TrustyMetricsCrashTest()
+ : TrustyStatsTestBase(kTrustyStatsSetterMetrics, kTrustyCrashPortTest) {}
+};
+
+TEST_F(TrustyStatsTest, CheckAtoms) {
+ int atomAppCrashedCnt = 0;
+ int atomStorageErrorCnt = 0;
+ int atomTrustyErrorCnt = 0;
+ uint64_t blockForMs = 500;
+ StartPortTest();
+ WaitPortTestDone();
+ for (;;) {
+ VendorAtom vendorAtom;
+ auto status = mStats->getVendorAtom(&vendorAtom, blockForMs);
+ ASSERT_THAT(status, ::testing::AnyOf(NO_ERROR, TIMED_OUT));
+ if (status == TIMED_OUT) {
+ // No more atoms
+ break;
+ }
+
+ ASSERT_THAT(vendorAtom.atomId,
+ ::testing::AnyOf(::testing::Eq(TrustyAtoms::TrustyAppCrashed),
+ ::testing::Eq(TrustyAtoms::TrustyError),
+ ::testing::Eq(TrustyAtoms::TrustyStorageError)));
+ ASSERT_STREQ(String8(vendorAtom.reverseDomainName), "google.android.trusty");
+ switch (vendorAtom.atomId) {
+ case TrustyAtoms::TrustyAppCrashed:
+ ++atomAppCrashedCnt;
+ ASSERT_STREQ(String8(vendorAtom.values[0].get<VendorAtomValue::stringValue>()),
+ "5247d19b-cf09-4272-a450-3ef20dbefc14");
+ break;
+ case TrustyAtoms::TrustyStorageError:
+ ++atomStorageErrorCnt;
+ ASSERT_EQ(vendorAtom.values[0].get<VendorAtomValue::intValue>(), 5);
+ ASSERT_STREQ(String8(vendorAtom.values[1].get<VendorAtomValue::stringValue>()),
+ "5247d19b-cf09-4272-a450-3ef20dbefc14");
+ ASSERT_STREQ(String8(vendorAtom.values[2].get<VendorAtomValue::stringValue>()),
+ "5247d19b-cf09-4272-a450-3ef20dbefc14");
+ ASSERT_EQ(vendorAtom.values[3].get<VendorAtomValue::intValue>(), 1);
+ ASSERT_EQ(vendorAtom.values[4].get<VendorAtomValue::intValue>(), 3);
+ ASSERT_EQ(vendorAtom.values[5].get<VendorAtomValue::longValue>(),
+ 0x4BCDEFABBAFEDCBALL);
+ ASSERT_EQ(vendorAtom.values[6].get<VendorAtomValue::intValue>(), 4);
+ ASSERT_EQ(vendorAtom.values[7].get<VendorAtomValue::longValue>(), 1023);
+ break;
+ case TrustyAtoms::TrustyError:
+ ++atomTrustyErrorCnt;
+ break;
+ default:
+ FAIL() << "Unknown vendor atom ID: " << vendorAtom.atomId;
+ break;
+ }
+ };
+ ASSERT_EQ(atomAppCrashedCnt, 1);
+ ASSERT_EQ(atomStorageErrorCnt, 1);
+ ASSERT_EQ(atomTrustyErrorCnt, 0);
+}
+
+TEST_F(TrustyMetricsCrashTest, CheckTrustyCrashAtoms) {
+ const std::vector<uint32_t> kExpectedCrashReasonsArm64{
+ 0x00000001U, // exit_failure (twice)
+ 0x00000001U,
+ 0x92000004U, // read_null_ptr
+ 0xf200002aU, // brk_instruction
+ 0x92000004U, // read_bad_ptr
+ 0x92000044U, // crash_write_bad_ptr
+ 0x9200004fU, // crash_write_ro_ptr
+ 0x8200000fU, // crash_exec_rodata
+ 0x8200000fU, // crash_exec_data
+ };
+ const std::vector<uint32_t> kExpectedCrashReasonsArm32{
+ 0x00000001U, // exit_failure (twice)
+ 0x00000001U,
+ 0x20000007U, // read_null_ptr
+ 0x20000007U, // read_bad_ptr
+ 0x20000807U, // crash_write_bad_ptr
+ 0x2000080fU, // crash_write_ro_ptr
+ 0x3000000fU, // crash_exec_rodata
+ 0x3000000fU, // crash_exec_data
+ };
+
+ int expectedAtomCnt = 7;
+ int atomAppCrashedCnt = 0;
+ int atomStorageErrorCnt = 0;
+ int atomTrustyErrorCnt = 0;
+ std::vector<uint32_t> atomCrashReasons;
+ uint64_t blockForMs = 500;
+ StartPortTest();
+ WaitPortTestDone();
+ for (;;) {
+ VendorAtom vendorAtom;
+ auto status = mStats->getVendorAtom(&vendorAtom, blockForMs);
+ ASSERT_THAT(status, ::testing::AnyOf(NO_ERROR, TIMED_OUT));
+ if (status == TIMED_OUT) {
+ // No more atoms
+ break;
+ }
+
+ ASSERT_THAT(vendorAtom.atomId,
+ ::testing::AnyOf(::testing::Eq(TrustyAtoms::TrustyAppCrashed),
+ ::testing::Eq(TrustyAtoms::TrustyError),
+ ::testing::Eq(TrustyAtoms::TrustyStorageError)));
+ ASSERT_STREQ(String8(vendorAtom.reverseDomainName), "google.android.trusty");
+
+ switch (vendorAtom.atomId) {
+ case TrustyAtoms::TrustyAppCrashed:
+ ++atomAppCrashedCnt;
+ ASSERT_STREQ(String8(vendorAtom.values[0].get<VendorAtomValue::stringValue>()),
+ kTrustyCrasherUuid);
+ atomCrashReasons.push_back(vendorAtom.values[1].get<VendorAtomValue::intValue>());
+ break;
+ case TrustyAtoms::TrustyStorageError:
+ ++atomStorageErrorCnt;
+ break;
+ case TrustyAtoms::TrustyError:
+ ++atomTrustyErrorCnt;
+ ASSERT_STREQ(String8(vendorAtom.values[1].get<VendorAtomValue::stringValue>()), "");
+ break;
+ default:
+ FAIL() << "Unknown vendor atom ID: " << vendorAtom.atomId;
+ }
+ }
+ ASSERT_GE(atomAppCrashedCnt, expectedAtomCnt - 1);
+ ASSERT_EQ(atomStorageErrorCnt, 0);
+ // There is one dropped event left over from Trusty boot,
+ // it may show up here
+ ASSERT_LE(atomTrustyErrorCnt, 1);
+ ASSERT_THAT(atomCrashReasons,
+ ::testing::AnyOf(kExpectedCrashReasonsArm64, kExpectedCrashReasonsArm32));
+};
+
+} // namespace stats
+} // namespace trusty
+} // namespace android
diff --git a/trusty/trusty-base.mk b/trusty/trusty-base.mk
index 5a3a320..1986c73 100644
--- a/trusty/trusty-base.mk
+++ b/trusty/trusty-base.mk
@@ -39,7 +39,6 @@
$(LOCAL_KEYMINT_PRODUCT_PACKAGE) \
android.hardware.gatekeeper-service.trusty \
trusty_apploader \
- RemoteProvisioner
PRODUCT_PROPERTY_OVERRIDES += \
ro.hardware.keystore_desede=true \