Merge "Add NDK folks to OWNERS for library configuration."
diff --git a/.clang-format-2 b/.clang-format-2
index 41591ce..ede5d7e 100644
--- a/.clang-format-2
+++ b/.clang-format-2
@@ -7,4 +7,3 @@
PointerAlignment: Left
TabWidth: 2
UseTab: Never
-PenaltyExcessCharacter: 32
diff --git a/.clang-format-4 b/.clang-format-4
index ae4a451..55773a2 100644
--- a/.clang-format-4
+++ b/.clang-format-4
@@ -5,7 +5,7 @@
CommentPragmas: NOLINT:.*
DerivePointerAlignment: false
IndentWidth: 4
+ContinuationIndentWidth: 8
PointerAlignment: Left
TabWidth: 4
UseTab: Never
-PenaltyExcessCharacter: 32
diff --git a/CleanSpec.mk b/CleanSpec.mk
index dc45959..0e43dae 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -74,3 +74,7 @@
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/etc/ld.config.txt)
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/etc/llndk.libraries.txt)
$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/etc/vndksp.libraries.txt)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/etc/ld.config.txt)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/etc/llndk.libraries.txt)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/etc/vndksp.libraries.txt)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/recovery/root/)
diff --git a/OWNERS b/OWNERS
index 1d319af..682a067 100644
--- a/OWNERS
+++ b/OWNERS
@@ -1,5 +1 @@
enh@google.com
-per-file libsysutils/src/Netlink* = ek@google.com
-per-file libsysutils/src/Netlink* = lorenzo@google.com
-per-file libsysutils/include/sysutils/Netlink* = ek@google.com
-per-file libsysutils/include/sysutils/Netlink* = lorenzo@google.com
diff --git a/adb/Android.bp b/adb/Android.bp
index ef425c1..41e752f 100644
--- a/adb/Android.bp
+++ b/adb/Android.bp
@@ -19,17 +19,13 @@
"-Wall",
"-Wextra",
"-Werror",
+ "-Wexit-time-destructors",
"-Wno-unused-parameter",
"-Wno-missing-field-initializers",
"-Wvla",
],
rtti: true,
- clang_cflags: [
- "-Wexit-time-destructors",
- "-Wthread-safety",
- ],
-
use_version_lib: true,
compile_multilib: "first",
@@ -72,8 +68,23 @@
"-DUNICODE=1",
"-D_UNICODE=1",
- // -std=gnu++14 doesn't set _GNU_SOURCE on Windows.
+ // -std=gnu++11 doesn't set _GNU_SOURCE on Windows.
"-D_GNU_SOURCE",
+
+ // MinGW hides some things behind _POSIX_SOURCE.
+ "-D_POSIX_SOURCE",
+ ],
+
+ host_ldlibs: [
+ "-lws2_32",
+ "-lgdi32",
+ "-luserenv",
+ ],
+ },
+
+ not_windows: {
+ cflags: [
+ "-Wthread-safety",
],
},
},
@@ -87,6 +98,7 @@
"adb_io.cpp",
"adb_listeners.cpp",
"adb_trace.cpp",
+ "adb_unique_fd.cpp",
"adb_utils.cpp",
"fdevent.cpp",
"services.cpp",
@@ -94,6 +106,7 @@
"socket_spec.cpp",
"sysdeps/errno.cpp",
"transport.cpp",
+ "transport_fd.cpp",
"transport_local.cpp",
"transport_usb.cpp",
]
@@ -113,6 +126,7 @@
"sysdeps_test.cpp",
"sysdeps/stat_test.cpp",
"transport_test.cpp",
+ "types_test.cpp",
]
cc_library_host_static {
@@ -173,11 +187,46 @@
"libdiagnose_usb",
"libusb",
],
+
+ target: {
+ windows: {
+ enabled: true,
+ shared_libs: ["AdbWinApi"],
+ },
+ },
+}
+
+cc_benchmark {
+ name: "adb_benchmark",
+ defaults: ["adb_defaults"],
+
+ srcs: ["transport_benchmark.cpp"],
+ target: {
+ android: {
+ static_libs: [
+ "libadbd",
+ ],
+ },
+ host: {
+ static_libs: [
+ "libadb_host",
+ ],
+ },
+ },
+
+ static_libs: [
+ "libbase",
+ "libcutils",
+ "libcrypto_utils",
+ "libcrypto",
+ "libdiagnose_usb",
+ "liblog",
+ "libusb",
+ ],
}
cc_binary_host {
name: "adb",
- tags: ["debug"],
defaults: ["adb_defaults"],
@@ -220,11 +269,6 @@
windows: {
enabled: true,
ldflags: ["-municode"],
- host_ldlibs: [
- "-lws2_32",
- "-lgdi32",
- ],
-
shared_libs: ["AdbWinApi"],
required: [
"AdbWinUsbApi",
@@ -233,42 +277,126 @@
},
}
+// libadbd_core contains the common sources to build libadbd and libadbd_services.
cc_library_static {
- name: "libadbd",
+ name: "libadbd_core",
defaults: ["adb_defaults"],
+ recovery_available: true,
- // libminadbd wants both, for some reason.
+ // libminadbd wants both, as it's used to build native tests.
compile_multilib: "both",
+
srcs: libadb_srcs + libadb_posix_srcs + [
"daemon/auth.cpp",
- "daemon/usb.cpp",
"daemon/jdwp_service.cpp",
+ "daemon/usb.cpp",
+ ],
+
+ local_include_dirs: [
+ "daemon/include",
],
static_libs: [
- "libasyncio",
- "libbootloader_message",
- "libcrypto_utils",
- "libcrypto",
"libdiagnose_usb",
"libqemu_pipe",
+ ],
+
+ shared_libs: [
+ "libasyncio",
"libbase",
+ "libcrypto",
+ "libcrypto_utils",
+ "libcutils",
+ "liblog",
+ ],
+}
+
+cc_library {
+ name: "libadbd_services",
+ defaults: ["adb_defaults"],
+ recovery_available: true,
+ compile_multilib: "both",
+
+ srcs: [
+ "daemon/file_sync_service.cpp",
+ "daemon/framebuffer_service.cpp",
+ "daemon/mdns.cpp",
+ "daemon/remount_service.cpp",
+ "daemon/services.cpp",
+ "daemon/set_verity_enable_state_service.cpp",
+ "daemon/shell_service.cpp",
+ "shell_service_protocol.cpp",
+ ],
+
+ cflags: [
+ "-D_GNU_SOURCE",
+ "-Wno-deprecated-declarations",
+ ],
+
+ static_libs: [
+ "libadbd_core",
+ "libavb_user",
+ "libdiagnose_usb",
+ "libqemu_pipe",
+
+ // `daemon/shell_service.cpp` uses selinux_android_setcon(), which is not exposed by
+ // libselinux.
+ "libselinux",
+ ],
+
+ shared_libs: [
+ "libasyncio",
+ "libbase",
+ "libbootloader_message",
+ "libcrypto",
+ "libcrypto_utils",
+ "libcutils",
+ "libext4_utils",
+ "libfec",
+ "libfs_mgr",
+ "liblog",
+ "libmdnssd",
+ ],
+}
+
+cc_library {
+ name: "libadbd",
+ defaults: ["adb_defaults"],
+ recovery_available: true,
+
+ // Avoid getting duplicate symbol of android::build::GetBuildNumber().
+ use_version_lib: false,
+
+ // libminadbd wants both, as it's used to build native tests.
+ compile_multilib: "both",
+
+ // libadbd doesn't build any additional source, but to expose libadbd_core as a shared library.
+ whole_static_libs: [
+ "libadbd_core",
+ ],
+
+ shared_libs: [
+ "libadbd_services",
+ "libasyncio",
+ "libbase",
+ "libcrypto",
+ "libcrypto_utils",
+ "libcutils",
+ "liblog",
+ ],
+
+ export_include_dirs: [
+ "daemon/include",
],
}
cc_binary {
name: "adbd",
defaults: ["adb_defaults"],
+ recovery_available: true,
srcs: [
"daemon/main.cpp",
- "daemon/mdns.cpp",
- "daemon/file_sync_service.cpp",
- "daemon/framebuffer_service.cpp",
- "daemon/remount_service.cpp",
- "daemon/set_verity_enable_state_service.cpp",
- "daemon/shell_service.cpp",
- "shell_service_protocol.cpp",
],
cflags: [
@@ -280,28 +408,16 @@
keep_symbols: true,
},
- static_libs: [
+ shared_libs: [
"libadbd",
- "libasyncio",
- "libavb_user",
- "libbootloader_message",
- "libcrypto_utils",
+ "libadbd_services",
+ "libbase",
+ "libcap",
"libcrypto",
- "libdiagnose_usb",
- "libfec",
- "libfec_rs",
- "libfs_mgr",
+ "libcutils",
"liblog",
- "libext4_utils",
- "libmdnssd",
"libminijail",
"libselinux",
- "libsquashfs_utils",
- "libqemu_pipe",
- "libdebuggerd_handler",
-
- "libbase",
- "libcutils",
],
}
@@ -309,6 +425,7 @@
name: "adbd_test",
defaults: ["adb_defaults"],
srcs: libadb_test_srcs + [
+ "daemon/services.cpp",
"daemon/shell_service.cpp",
"daemon/shell_service_test.cpp",
"shell_service_protocol.cpp",
@@ -318,6 +435,7 @@
static_libs: [
"libadbd",
"libbase",
+ "libbootloader_message",
"libcutils",
"libcrypto_utils",
"libcrypto",
@@ -325,24 +443,25 @@
"liblog",
"libusb",
"libmdnssd",
+ "libselinux",
],
+ test_suites: ["device-tests"],
}
-python_binary_host {
+python_test_host {
name: "adb_integration_test_adb",
main: "test_adb.py",
srcs: [
"test_adb.py",
],
- libs: [
- "adb_py",
- ],
+ test_config: "adb_integration_test_adb.xml",
+ test_suites: ["general-tests"],
version: {
py2: {
- enabled: true,
+ enabled: false,
},
py3: {
- enabled: false,
+ enabled: true,
},
},
}
diff --git a/adb/Android.mk b/adb/Android.mk
new file mode 100644
index 0000000..8b2d558
--- /dev/null
+++ b/adb/Android.mk
@@ -0,0 +1,8 @@
+LOCAL_PATH := $(call my-dir)
+
+# Archive adb, adb.exe.
+$(call dist-for-goals,dist_files sdk win_sdk,$(HOST_OUT_EXECUTABLES)/adb)
+
+ifdef HOST_CROSS_OS
+$(call dist-for-goals,dist_files sdk win_sdk,$(ALL_MODULES.host_cross_adb.BUILT))
+endif
diff --git a/adb/OVERVIEW.TXT b/adb/OVERVIEW.TXT
index 29a6992..f0b184c 100644
--- a/adb/OVERVIEW.TXT
+++ b/adb/OVERVIEW.TXT
@@ -103,9 +103,6 @@
4-byte hex length, followed by a string giving the reason
for failure.
- 3. As a special exception, for 'host:version', a 4-byte
- hex string corresponding to the server's internal version number
-
Note that the connection is still alive after an OKAY, which allows the
client to make other requests. But in certain cases, an OKAY will even
change the state of the connection.
diff --git a/adb/SERVICES.TXT b/adb/SERVICES.TXT
index 30c21f7..3e18a54 100644
--- a/adb/SERVICES.TXT
+++ b/adb/SERVICES.TXT
@@ -7,10 +7,6 @@
host:version
Ask the ADB server for its internal version number.
- As a special exception, the server will respond with a 4-byte
- hex string corresponding to its internal version number, without
- any OKAY or FAIL.
-
host:kill
Ask the ADB server to quit immediately. This is used when the
ADB client detects that an obsolete server is running after an
diff --git a/adb/adb.bash b/adb/adb.bash
new file mode 100644
index 0000000..b1b3957
--- /dev/null
+++ b/adb/adb.bash
@@ -0,0 +1,499 @@
+# /* vim: set ai ts=4 ft=sh: */
+#
+# Copyright 2011, 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.
+#
+
+_adb() {
+ if ! check_type "$1" >/dev/null; then
+ return
+ fi
+
+ if check_type _init_completion >/dev/null; then
+ _init_completion || return
+ fi
+
+ local where i cur serial
+ COMPREPLY=()
+
+ serial="${ANDROID_SERIAL:-none}"
+ where=OPTIONS
+ for ((i=1; i <= COMP_CWORD; i++)); do
+ cur="${COMP_WORDS[i]}"
+ case "${cur}" in
+ -s)
+ where=OPT_SERIAL
+ ;;
+ -p)
+ where=OPT_PATH
+ ;;
+ -*)
+ where=OPTIONS
+ ;;
+ *)
+ if [[ $where == OPT_SERIAL ]]; then
+ where=OPT_SERIAL_ARG
+ serial=${cur}
+ else
+ where=COMMAND
+ break
+ fi
+ ;;
+ esac
+ done
+
+ if [[ $where == COMMAND && $i -ge $COMP_CWORD ]]; then
+ where=OPTIONS
+ fi
+
+ OPTIONS="-d -e -s -p"
+ COMMAND="devices connect disconnect push pull sync shell emu logcat lolcat forward jdwp install uninstall bugreport help version start-server kill-server get-state get-serialno status-window remount reboot reboot-bootloader root usb tcpip disable-verity"
+
+ case $where in
+ OPTIONS|OPT_SERIAL|OPT_PATH)
+ COMPREPLY=( $(compgen -W "$OPTIONS $COMMAND" -- "$cur") )
+ ;;
+ OPT_SERIAL_ARG)
+ local devices=$(command adb devices 2> /dev/null | grep -v "List of devices" | awk '{ print $1 }')
+ COMPREPLY=( $(compgen -W "${devices}" -- ${cur}) )
+ ;;
+ COMMAND)
+ if [[ $i -eq $COMP_CWORD ]]; then
+ COMPREPLY=( $(compgen -W "$COMMAND" -- "$cur") )
+ else
+ i=$((i+1))
+ case "${cur}" in
+ install)
+ _adb_cmd_install "$serial" $i
+ ;;
+ sideload)
+ _adb_cmd_sideload "$serial" $i
+ ;;
+ pull)
+ _adb_cmd_pull "$serial" $i
+ ;;
+ push)
+ _adb_cmd_push "$serial" $i
+ ;;
+ reboot)
+ if [[ $COMP_CWORD == $i ]]; then
+ args="bootloader recovery"
+ COMPREPLY=( $(compgen -W "${args}" -- "${COMP_WORDS[i]}") )
+ fi
+ ;;
+ shell)
+ _adb_cmd_shell "$serial" $i
+ ;;
+ uninstall)
+ _adb_cmd_uninstall "$serial" $i
+ ;;
+ esac
+ fi
+ ;;
+ esac
+
+ return 0
+}
+
+_adb_cmd_install() {
+ local serial i cur where
+
+ serial=$1
+ i=$2
+
+ where=OPTIONS
+ for ((; i <= COMP_CWORD; i++)); do
+ cur="${COMP_WORDS[i]}"
+ case "${cur}" in
+ -*)
+ where=OPTIONS
+ ;;
+ *)
+ where=FILE
+ break
+ ;;
+ esac
+ done
+
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ if [[ $where == OPTIONS ]]; then
+ COMPREPLY=( $(compgen -W "-d -l -r -s" -- "${cur}") )
+ return
+ fi
+
+ _adb_util_complete_local_file "${cur}" '!*.apk'
+}
+
+_adb_cmd_sideload() {
+ local serial i cur
+
+ serial=$1
+ i=$2
+
+ cur="${COMP_WORDS[COMP_CWORD]}"
+
+ _adb_util_complete_local_file "${cur}" '!*.zip'
+}
+
+_adb_cmd_push() {
+ local serial IFS=$'\n' i cur
+
+ serial=$1
+ i=$2
+
+ cur="${COMP_WORDS[COMP_CWORD]}"
+
+ if [[ $COMP_CWORD == $i ]]; then
+ _adb_util_complete_local_file "${cur}"
+ elif [[ $COMP_CWORD == $(($i+1)) ]]; then
+ if [ "${cur}" == "" ]; then
+ cur="/"
+ fi
+ _adb_util_list_files $serial "${cur}"
+ fi
+}
+
+_adb_cmd_pull() {
+ local serial IFS=$'\n' i cur
+
+ serial=$1
+ i=$2
+
+ cur="${COMP_WORDS[COMP_CWORD]}"
+
+ if [[ $COMP_CWORD == $i ]]; then
+ if [ "${cur}" == "" ]; then
+ cur="/"
+ fi
+ _adb_util_list_files $serial "${cur}"
+ elif [[ $COMP_CWORD == $(($i+1)) ]]; then
+ _adb_util_complete_local_file "${cur}"
+ fi
+}
+
+_adb_cmd_shell() {
+ local serial IFS=$'\n' i cur
+ local -a args
+
+ serial=$1
+ i=$2
+
+ cur="${COMP_WORDS[i]}"
+ if [ "$serial" != "none" ]; then
+ args=(-s $serial)
+ fi
+
+ if [[ $i -eq $COMP_CWORD && ${cur:0:1} != "/" ]]; then
+ paths=$(command adb ${args[@]} shell echo '$'PATH 2> /dev/null | tr -d '\r' | tr : '\n')
+ COMMAND=$(command adb ${args[@]} shell ls $paths '2>' /dev/null | tr -d '\r' | {
+ while read -r tmp; do
+ command=${tmp##*/}
+ printf '%s\n' "$command"
+ done
+ })
+ COMPREPLY=( $(compgen -W "$COMMAND" -- "$cur") )
+ return 0
+ fi
+
+ i=$((i+1))
+ case "$cur" in
+ ls)
+ _adb_shell_file_command $serial $i "--color -A -C -F -H -L -R -S -Z -a -c -d -f -h -i -k -l -m -n -p -q -r -s -t -u -x -1"
+ ;;
+ cat)
+ _adb_shell_file_command $serial $i "-h -e -t -u -v"
+ ;;
+ dumpsys)
+ _adb_cmd_shell_dumpsys "$serial" $i
+ ;;
+ am)
+ _adb_cmd_shell_am "$serial" $i
+ ;;
+ pm)
+ _adb_cmd_shell_pm "$serial" $i
+ ;;
+ /*)
+ _adb_util_list_files $serial "$cur"
+ ;;
+ *)
+ COMPREPLY=( )
+ ;;
+ esac
+
+ return 0
+}
+
+_adb_cmd_shell_dumpsys() {
+ local serial i cur
+ local -a args
+ local candidates
+
+ unset IFS
+
+ serial=$1
+ i=$2
+
+ if [ "$serial" != "none" ]; then
+ args=(-s $serial)
+ fi
+
+ if (( $i == $COMP_CWORD )) ; then
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ # First line is a header, so need "1d".
+ candidates=$(command adb ${args[@]} shell dumpsys -l 2> /dev/null | sed -e '1d;s/^ *//' | tr -d '\r')
+ candidates="-l $candidates"
+ COMPREPLY=( $(compgen -W "$candidates" -- "$cur") )
+ return 0
+ fi
+
+ COMPREPLY=( )
+ return 0
+}
+
+_adb_cmd_shell_am() {
+ local serial i cur
+ local candidates
+
+ unset IFS
+
+ serial=$1
+ i=$2
+
+ if (( $i == $COMP_CWORD )) ; then
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ candidates="broadcast clear-debug-app clear-watch-heap dumpheap force-stop get-config get-inactive hang idle-maintenance instrument kill kill-all monitor package-importance profile restart screen-compat send-trim-memory set-debug-app set-inactive set-watch-heap stack start startservice start-user stopservice stop-user suppress-resize-config-changes switch-user task to-app-uri to-intent-uri to-uri"
+ COMPREPLY=( $(compgen -W "$candidates" -- "$cur") )
+ return 0
+ fi
+
+ COMPREPLY=( )
+ return 0
+}
+
+
+_adb_cmd_shell_pm() {
+ local serial i cur
+ local candidates
+
+ unset IFS
+
+ serial=$1
+ i=$2
+
+ if (( $i == $COMP_CWORD )) ; then
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ candidates="-l -lf -p clear create-user default-state disable"
+ candidates+=" disable-until-used disable-user dump enable"
+ candidates+=" get-app-link get-install-location get-max-users"
+ candidates+=" get-max-running-users grant hide install"
+ candidates+=" install-abandon install-commit install-create"
+ candidates+=" install-write list move-package"
+ candidates+=" move-primary-storage path remove-user"
+ candidates+=" reset-permissions revoke set-app-link"
+ candidates+=" set-installer set-install-location"
+ candidates+=" set-permission-enforced trim-caches unhide"
+ candidates+=" uninstall"
+ COMPREPLY=( $(compgen -W "$candidates" -- "$cur") )
+ return 0
+ fi
+
+ if (( $i + 1 == $COMP_CWORD )) && [[ "${COMP_WORDS[COMP_CWORD -1]}" == "list" ]] ; then
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ candidates="packages permission-groups permissions instrumentation features libraries users"
+ COMPREPLY=( $(compgen -W "$candidates" -- "$cur") )
+ return 0
+ fi
+
+ COMPREPLY=( )
+ return 0
+}
+
+_adb_cmd_uninstall() {
+ local serial i where cur packages
+
+ serial=$1
+ i=$2
+ if [ "$serial" != "none" ]; then
+ args=(-s $serial)
+ fi
+
+ where=OPTIONS
+ for ((; i <= COMP_CWORD; i++)); do
+ cur="${COMP_WORDS[i]}"
+ case "${cur}" in
+ -*)
+ where=OPTIONS
+ ;;
+ *)
+ where=FILE
+ break
+ ;;
+ esac
+ done
+
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ if [[ $where == OPTIONS ]]; then
+ COMPREPLY=( $(compgen -W "-k" -- "${cur}") )
+ fi
+
+ packages="$(
+ command adb ${args[@]} shell pm list packages '2>' /dev/null 2> /dev/null | tr -d '\r' | {
+ while read -r tmp; do
+ local package=${tmp#package:}
+ echo -n "${package} "
+ done
+ }
+ )"
+
+ COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "${packages}" -- "${cur}") )
+}
+
+_adb_shell_file_command() {
+ local serial i cur file options
+ local -a args
+
+ serial=$1
+ i=$2
+ if [ "$serial" != "none" ]; then
+ args=(-s $serial)
+ fi
+ options=$3
+
+ where=OPTIONS
+ for ((; i <= COMP_CWORD; i++)); do
+ cur="${COMP_WORDS[i]}"
+ case "${cur}" in
+ -*)
+ where=OPTIONS
+ ;;
+ *)
+ where=FILE
+ break
+ ;;
+ esac
+ done
+
+ file="${COMP_WORDS[COMP_CWORD]}"
+ if [[ ${file} == "" ]]; then
+ file="/"
+ fi
+
+ case $where in
+ OPTIONS)
+ unset IFS
+ COMPREPLY=( $(compgen -W "$options" -- "$cur") )
+ ;;
+ FILE)
+ _adb_util_list_files $serial "$file"
+ ;;
+ esac
+
+ return 0
+}
+
+_adb_util_list_files() {
+ local serial dir IFS=$'\n'
+ local -a toks
+ local -a args
+
+ serial="$1"
+ file="$2"
+
+ if [ "$serial" != "none" ]; then
+ args=(-s $serial)
+ fi
+
+ if [[ $( command adb ${args[@]} shell ls -dF / '2>/dev/null' | tr -d '\r' ) == "d /" ]] ; then
+ toks=( ${toks[@]-} $(
+ command adb ${args[@]} shell ls -dF ${file}"*" '2>' /dev/null 2> /dev/null | tr -d '\r' | {
+ while read -r tmp; do
+ filetype=${tmp%% *}
+ filename=${tmp:${#filetype}+1}
+ if [[ ${filetype:${#filetype}-1:1} == d ]]; then
+ printf '%s/\n' "$filename"
+ else
+ printf '%s\n' "$filename"
+ fi
+ done
+ }
+ ))
+ else
+ toks=( ${toks[@]-} $(
+ command adb ${args[@]} shell ls -dp ${file}"*" '2>/dev/null' 2> /dev/null | tr -d '\r'
+ ))
+ fi
+
+ # Since we're probably doing file completion here, don't add a space after.
+ if [[ $(check_type compopt) == "builtin" ]]; then
+ compopt -o nospace
+ fi
+
+ COMPREPLY=( ${COMPREPLY[@]:-} "${toks[@]}" )
+}
+
+_adb_util_complete_local_file()
+{
+ local file xspec i j IFS=$'\n'
+ local -a dirs files
+
+ file=$1
+ xspec=$2
+
+ # Since we're probably doing file completion here, don't add a space after.
+ if [[ $(check_type compopt) == "builtin" ]]; then
+ compopt -o plusdirs
+ if [[ "${xspec}" == "" ]]; then
+ COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -f -- "${cur}") )
+ else
+ compopt +o filenames
+ COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -f -X "${xspec}" -- "${cur}") )
+ fi
+ else
+ # Work-around for shells with no compopt
+
+ dirs=( $(compgen -d -- "${cur}" ) )
+
+ if [[ "${xspec}" == "" ]]; then
+ files=( ${COMPREPLY[@]:-} $(compgen -f -- "${cur}") )
+ else
+ files=( ${COMPREPLY[@]:-} $(compgen -f -X "${xspec}" -- "${cur}") )
+ fi
+
+ COMPREPLY=( $(
+ for i in "${files[@]}"; do
+ local skip=
+ for j in "${dirs[@]}"; do
+ if [[ $i == $j ]]; then
+ skip=1
+ break
+ fi
+ done
+ [[ -n $skip ]] || printf "%s\n" "$i"
+ done
+ ))
+
+ COMPREPLY=( ${COMPREPLY[@]:-} $(
+ for i in "${dirs[@]}"; do
+ printf "%s/\n" "$i"
+ done
+ ))
+ fi
+}
+
+
+if [[ $(check_type compopt) == "builtin" ]]; then
+ complete -F _adb adb
+else
+ complete -o nospace -F _adb adb
+fi
diff --git a/adb/adb.cpp b/adb/adb.cpp
index 65fa2e7..8e028f4 100644
--- a/adb/adb.cpp
+++ b/adb/adb.cpp
@@ -132,6 +132,7 @@
{
D("adb: online");
t->online = 1;
+ t->SetConnectionEstablished(true);
}
void handle_offline(atransport *t)
@@ -257,21 +258,12 @@
<< connection_str.length() << ")";
}
- cp->payload = std::move(connection_str);
+ cp->payload.assign(connection_str.begin(), connection_str.end());
cp->msg.data_length = cp->payload.size();
send_packet(cp, t);
}
-// qual_overwrite is used to overwrite a qualifier string. dst is a
-// pointer to a char pointer. It is assumed that if *dst is non-NULL, it
-// was malloc'ed and needs to freed. *dst will be set to a dup of src.
-// TODO: switch to std::string for these atransport fields instead.
-static void qual_overwrite(char** dst, const std::string& src) {
- free(*dst);
- *dst = strdup(src.c_str());
-}
-
void parse_banner(const std::string& banner, atransport* t) {
D("parse_banner: %s", banner.c_str());
@@ -295,11 +287,11 @@
const std::string& key = key_value[0];
const std::string& value = key_value[1];
if (key == "ro.product.name") {
- qual_overwrite(&t->product, value);
+ t->product = value;
} else if (key == "ro.product.model") {
- qual_overwrite(&t->model, value);
+ t->model = value;
} else if (key == "ro.product.device") {
- qual_overwrite(&t->device, value);
+ t->device = value;
} else if (key == "features") {
t->SetFeatures(value);
}
@@ -329,7 +321,8 @@
handle_offline(t);
t->update_version(p->msg.arg0, p->msg.arg1);
- parse_banner(p->payload, t);
+ std::string banner(p->payload.begin(), p->payload.end());
+ parse_banner(banner, t);
#if ADB_HOST
handle_online(t);
@@ -363,14 +356,16 @@
switch (p->msg.arg0) {
#if ADB_HOST
case ADB_AUTH_TOKEN:
- if (t->GetConnectionState() == kCsOffline) {
- t->SetConnectionState(kCsUnauthorized);
+ if (t->GetConnectionState() != kCsAuthorizing) {
+ t->SetConnectionState(kCsAuthorizing);
}
send_auth_response(p->payload.data(), p->msg.data_length, t);
break;
#else
- case ADB_AUTH_SIGNATURE:
- if (adbd_auth_verify(t->token, sizeof(t->token), p->payload)) {
+ case ADB_AUTH_SIGNATURE: {
+ // TODO: Switch to string_view.
+ std::string signature(p->payload.begin(), p->payload.end());
+ if (adbd_auth_verify(t->token, sizeof(t->token), signature)) {
adbd_auth_verified(t);
t->failed_auth_attempts = 0;
} else {
@@ -378,6 +373,7 @@
send_auth_request(t);
}
break;
+ }
case ADB_AUTH_RSAPUBLICKEY:
adbd_auth_confirm_key(p->payload.data(), p->msg.data_length, t);
@@ -392,7 +388,9 @@
case A_OPEN: /* OPEN(local-id, 0, "destination") */
if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
- asocket* s = create_local_service_socket(p->payload.c_str(), t);
+ // TODO: Switch to string_view.
+ std::string address(p->payload.begin(), p->payload.end());
+ asocket* s = create_local_service_socket(address.c_str(), t);
if (s == nullptr) {
send_close(0, p->msg.arg0, t);
} else {
@@ -408,7 +406,7 @@
if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
asocket* s = find_local_socket(p->msg.arg1, 0);
if (s) {
- if(s->peer == 0) {
+ if(s->peer == nullptr) {
/* On first READY message, create the connection. */
s->peer = create_remote_socket(p->msg.arg0, t);
s->peer->peer = s;
@@ -417,8 +415,8 @@
/* Other READY messages must use the same local-id */
s->ready(s);
} else {
- D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s",
- p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial);
+ D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s", p->msg.arg0,
+ p->msg.arg1, s->peer->id, p->msg.arg1, t->serial.c_str());
}
} else {
// When receiving A_OKAY from device for A_OPEN request, the host server may
@@ -444,8 +442,8 @@
* socket has a peer on the same transport.
*/
if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
- D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s",
- p->msg.arg1, t->serial, s->peer->transport->serial);
+ D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s", p->msg.arg1,
+ t->serial.c_str(), s->peer->transport->serial.c_str());
} else {
s->close(s);
}
@@ -878,9 +876,8 @@
}
#else /* !defined(_WIN32) */
// set up a pipe so the child can tell us when it is ready.
- // fd[0] will be parent's end, and the child will write on fd[1]
- int fd[2];
- if (pipe(fd)) {
+ unique_fd pipe_read, pipe_write;
+ if (!Pipe(&pipe_read, &pipe_write)) {
fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
return -1;
}
@@ -892,11 +889,14 @@
if (pid == 0) {
// child side of the fork
+ pipe_read.reset();
- adb_close(fd[0]);
+ // android::base::Pipe unconditionally opens the pipe with O_CLOEXEC.
+ // Undo this manually.
+ fcntl(pipe_write.get(), F_SETFD, 0);
char reply_fd[30];
- snprintf(reply_fd, sizeof(reply_fd), "%d", fd[1]);
+ snprintf(reply_fd, sizeof(reply_fd), "%d", pipe_write.get());
// child process
int result = execl(path.c_str(), "adb", "-L", socket_spec.c_str(), "fork-server", "server",
"--reply-fd", reply_fd, NULL);
@@ -906,10 +906,10 @@
// parent side of the fork
char temp[3] = {};
// wait for the "OK\n" message
- adb_close(fd[1]);
- int ret = adb_read(fd[0], temp, 3);
+ pipe_write.reset();
+ int ret = adb_read(pipe_read.get(), temp, 3);
int saved_errno = errno;
- adb_close(fd[0]);
+ pipe_read.reset();
if (ret < 0) {
fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
return -1;
@@ -924,18 +924,23 @@
}
#endif /* ADB_HOST */
+bool handle_forward_request(const char* service, atransport* transport, int reply_fd) {
+ return handle_forward_request(service, [transport](std::string*) { return transport; },
+ reply_fd);
+}
+
// Try to handle a network forwarding request.
-// This returns 1 on success, 0 on failure, and -1 to indicate this is not
-// a forwarding-related request.
-int handle_forward_request(const char* service, TransportType type, const char* serial,
- TransportId transport_id, int reply_fd) {
+bool handle_forward_request(const char* service,
+ std::function<atransport*(std::string* error)> transport_acquirer,
+ int reply_fd) {
if (!strcmp(service, "list-forward")) {
// Create the list of forward redirections.
std::string listeners = format_listeners();
#if ADB_HOST
SendOkay(reply_fd);
#endif
- return SendProtocolString(reply_fd, listeners);
+ SendProtocolString(reply_fd, listeners);
+ return true;
}
if (!strcmp(service, "killforward-all")) {
@@ -945,12 +950,19 @@
SendOkay(reply_fd);
#endif
SendOkay(reply_fd);
- return 1;
+ return true;
}
if (!strncmp(service, "forward:", 8) || !strncmp(service, "killforward:", 12)) {
// killforward:local
// forward:(norebind:)?local;remote
+ std::string error;
+ atransport* transport = transport_acquirer(&error);
+ if (!transport) {
+ SendFail(reply_fd, error);
+ return true;
+ }
+
bool kill_forward = false;
bool no_rebind = false;
if (android::base::StartsWith(service, "killforward:")) {
@@ -970,25 +982,16 @@
// Check killforward: parameter format: '<local>'
if (pieces.size() != 1 || pieces[0].empty()) {
SendFail(reply_fd, android::base::StringPrintf("bad killforward: %s", service));
- return 1;
+ return true;
}
} else {
// Check forward: parameter format: '<local>;<remote>'
if (pieces.size() != 2 || pieces[0].empty() || pieces[1].empty() || pieces[1][0] == '*') {
SendFail(reply_fd, android::base::StringPrintf("bad forward: %s", service));
- return 1;
+ return true;
}
}
- std::string error_msg;
- atransport* transport =
- acquire_one_transport(type, serial, transport_id, nullptr, &error_msg);
- if (!transport) {
- SendFail(reply_fd, error_msg);
- return 1;
- }
-
- std::string error;
InstallStatus r;
int resolved_tcp_port = 0;
if (kill_forward) {
@@ -1009,7 +1012,7 @@
SendProtocolString(reply_fd, android::base::StringPrintf("%d", resolved_tcp_port));
}
- return 1;
+ return true;
}
std::string message;
@@ -1028,9 +1031,10 @@
break;
}
SendFail(reply_fd, message);
- return 1;
+ return true;
}
- return 0;
+
+ return false;
}
#if ADB_HOST
@@ -1040,7 +1044,7 @@
return 0;
}
-int handle_host_request(const char* service, TransportType type, const char* serial,
+bool handle_host_request(const char* service, TransportType type, const char* serial,
TransportId transport_id, int reply_fd, asocket* s) {
if (strcmp(service, "kill") == 0) {
fprintf(stderr, "adb server killed by remote request\n");
@@ -1066,7 +1070,7 @@
transport_id = strtoll(service, const_cast<char**>(&service), 10);
if (*service != '\0') {
SendFail(reply_fd, "invalid transport id");
- return 1;
+ return true;
}
} else if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
type = kTransportUsb;
@@ -1084,10 +1088,13 @@
if (t != nullptr) {
s->transport = t;
SendOkay(reply_fd);
+
+ // We succesfully handled the device selection, but there's another request coming.
+ return false;
} else {
SendFail(reply_fd, error);
+ return true;
}
- return 1;
}
// return a list of all connected devices
@@ -1097,28 +1104,25 @@
D("Getting device list...");
std::string device_list = list_transports(long_listing);
D("Sending device list...");
- return SendOkay(reply_fd, device_list);
+ SendOkay(reply_fd, device_list);
}
- return 1;
+ return true;
}
if (!strcmp(service, "reconnect-offline")) {
std::string response;
close_usb_devices([&response](const atransport* transport) {
- switch (transport->GetConnectionState()) {
- case kCsOffline:
- case kCsUnauthorized:
- response += "reconnecting " + transport->serial_name() + "\n";
- return true;
- default:
- return false;
+ if (!ConnectionStateIsOnline(transport->GetConnectionState())) {
+ response += "reconnecting " + transport->serial_name() + "\n";
+ return true;
}
+ return false;
});
if (!response.empty()) {
response.resize(response.size() - 1);
}
SendOkay(reply_fd, response);
- return 0;
+ return true;
}
if (!strcmp(service, "features")) {
@@ -1129,7 +1133,7 @@
} else {
SendFail(reply_fd, error);
}
- return 0;
+ return true;
}
if (!strcmp(service, "host-features")) {
@@ -1140,7 +1144,7 @@
}
features.insert(kFeaturePushSync);
SendOkay(reply_fd, FeatureSetToString(features));
- return 0;
+ return true;
}
// remove TCP transport
@@ -1148,7 +1152,8 @@
const std::string address(service + 11);
if (address.empty()) {
kick_all_tcp_devices();
- return SendOkay(reply_fd, "disconnected everything");
+ SendOkay(reply_fd, "disconnected everything");
+ return true;
}
std::string serial;
@@ -1156,21 +1161,24 @@
int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
std::string error;
if (!android::base::ParseNetAddress(address, &host, &port, &serial, &error)) {
- return SendFail(reply_fd, android::base::StringPrintf("couldn't parse '%s': %s",
- address.c_str(), error.c_str()));
+ SendFail(reply_fd, android::base::StringPrintf("couldn't parse '%s': %s",
+ address.c_str(), error.c_str()));
+ return true;
}
atransport* t = find_transport(serial.c_str());
if (t == nullptr) {
- return SendFail(reply_fd, android::base::StringPrintf("no such device '%s'",
- serial.c_str()));
+ SendFail(reply_fd, android::base::StringPrintf("no such device '%s'", serial.c_str()));
+ return true;
}
kick_transport(t);
- return SendOkay(reply_fd, android::base::StringPrintf("disconnected %s", address.c_str()));
+ SendOkay(reply_fd, android::base::StringPrintf("disconnected %s", address.c_str()));
+ return true;
}
// Returns our value for ADB_SERVER_VERSION.
if (!strcmp(service, "version")) {
- return SendOkay(reply_fd, android::base::StringPrintf("%04x", ADB_SERVER_VERSION));
+ SendOkay(reply_fd, android::base::StringPrintf("%04x", ADB_SERVER_VERSION));
+ return true;
}
// These always report "unknown" rather than the actual error, for scripts.
@@ -1178,28 +1186,31 @@
std::string error;
atransport* t = acquire_one_transport(type, serial, transport_id, nullptr, &error);
if (t) {
- return SendOkay(reply_fd, t->serial ? t->serial : "unknown");
+ SendOkay(reply_fd, !t->serial.empty() ? t->serial : "unknown");
} else {
- return SendFail(reply_fd, error);
+ SendFail(reply_fd, error);
}
+ return true;
}
if (!strcmp(service, "get-devpath")) {
std::string error;
atransport* t = acquire_one_transport(type, serial, transport_id, nullptr, &error);
if (t) {
- return SendOkay(reply_fd, t->devpath ? t->devpath : "unknown");
+ SendOkay(reply_fd, !t->devpath.empty() ? t->devpath : "unknown");
} else {
- return SendFail(reply_fd, error);
+ SendFail(reply_fd, error);
}
+ return true;
}
if (!strcmp(service, "get-state")) {
std::string error;
atransport* t = acquire_one_transport(type, serial, transport_id, nullptr, &error);
if (t) {
- return SendOkay(reply_fd, t->connection_state_name());
+ SendOkay(reply_fd, t->connection_state_name());
} else {
- return SendFail(reply_fd, error);
+ SendFail(reply_fd, error);
}
+ return true;
}
// Indicates a new emulator instance has started.
@@ -1207,7 +1218,7 @@
int port = atoi(service+9);
local_connect(port);
/* we don't even need to send a reply */
- return 0;
+ return true;
}
if (!strcmp(service, "reconnect")) {
@@ -1218,13 +1229,20 @@
response =
"reconnecting " + t->serial_name() + " [" + t->connection_state_name() + "]\n";
}
- return SendOkay(reply_fd, response);
+ SendOkay(reply_fd, response);
+ return true;
}
- int ret = handle_forward_request(service, type, serial, transport_id, reply_fd);
- if (ret >= 0)
- return ret - 1;
- return -1;
+ if (handle_forward_request(service,
+ [=](std::string* error) {
+ return acquire_one_transport(type, serial, transport_id, nullptr,
+ error);
+ },
+ reply_fd)) {
+ return true;
+ }
+
+ return false;
}
static auto& init_mutex = *new std::mutex();
diff --git a/adb/adb.h b/adb/adb.h
index a6d0463..f434e2d 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -28,6 +28,7 @@
#include "adb_trace.h"
#include "fdevent.h"
#include "socket.h"
+#include "types.h"
#include "usb.h"
constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
@@ -63,20 +64,6 @@
using TransportId = uint64_t;
class atransport;
-struct amessage {
- uint32_t command; /* command identifier constant */
- uint32_t arg0; /* first argument */
- uint32_t arg1; /* second argument */
- uint32_t data_length; /* length of payload (0 is allowed) */
- uint32_t data_check; /* checksum of data payload */
- uint32_t magic; /* command ^ 0xffffffff */
-};
-
-struct apacket {
- amessage msg;
- std::string payload;
-};
-
uint32_t calculate_apacket_checksum(const apacket* packet);
/* the adisconnect structure is used to record a callback that
@@ -108,20 +95,35 @@
enum ConnectionState {
kCsAny = -1,
- kCsOffline = 0,
+
+ kCsConnecting = 0, // Haven't received a response from the device yet.
+ kCsAuthorizing, // Authorizing with keys from ADB_VENDOR_KEYS.
+ kCsUnauthorized, // ADB_VENDOR_KEYS exhausted, fell back to user prompt.
+ kCsNoPerm, // Insufficient permissions to communicate with the device.
+ kCsOffline,
+
kCsBootloader,
kCsDevice,
kCsHost,
kCsRecovery,
- kCsNoPerm, // Insufficient permissions to communicate with the device.
kCsSideload,
- kCsUnauthorized,
};
+inline bool ConnectionStateIsOnline(ConnectionState state) {
+ switch (state) {
+ case kCsBootloader:
+ case kCsDevice:
+ case kCsHost:
+ case kCsRecovery:
+ case kCsSideload:
+ return true;
+ default:
+ return false;
+ }
+}
+
void print_packet(const char* label, apacket* p);
-// These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they
-// shouldn't be tagged with ADB_FORMAT_ARCHETYPE.
void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
@@ -131,7 +133,7 @@
int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd);
/* initialize a transport object's func pointers and state */
-int init_socket_transport(atransport* t, int s, int port, int local);
+int init_socket_transport(atransport* t, unique_fd s, int port, int local);
void init_usb_transport(atransport* t, usb_handle* usb);
std::string getEmulatorSerialString(int console_port);
@@ -140,7 +142,11 @@
atransport* find_emulator_transport_by_console_port(int console_port);
#endif
-int service_to_fd(const char* name, const atransport* transport);
+int service_to_fd(const char* name, atransport* transport);
+#if !ADB_HOST
+unique_fd daemon_service_to_fd(const char* name, atransport* transport);
+#endif
+
#if ADB_HOST
asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id);
#endif
@@ -149,16 +155,13 @@
int init_jdwp(void);
asocket* create_jdwp_service_socket();
asocket* create_jdwp_tracker_service_socket();
-int create_jdwp_connection_fd(int jdwp_pid);
+unique_fd create_jdwp_connection_fd(int jdwp_pid);
#endif
-int handle_forward_request(const char* service, TransportType type, const char* serial,
- TransportId transport_id, int reply_fd);
-
-#if !ADB_HOST
-void framebuffer_service(int fd, void* cookie);
-void set_verity_enabled_state_service(int fd, void* cookie);
-#endif
+bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
+bool handle_forward_request(const char* service,
+ std::function<atransport*(std::string* error)> transport_acquirer,
+ int reply_fd);
/* packet allocator */
apacket* get_apacket(void);
@@ -196,10 +199,6 @@
extern const char* adb_device_banner;
-#if !ADB_HOST
-extern int SHELL_EXIT_NOTIFY_FD;
-#endif // !ADB_HOST
-
#define CHUNK_SIZE (64 * 1024)
#if !ADB_HOST
@@ -211,8 +210,8 @@
#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
#endif
-int handle_host_request(const char* service, TransportType type, const char* serial,
- TransportId transport_id, int reply_fd, asocket* s);
+bool handle_host_request(const char* service, TransportType type, const char* serial,
+ TransportId transport_id, int reply_fd, asocket* s);
void handle_online(atransport* t);
void handle_offline(atransport* t);
diff --git a/adb/adb_integration_test_adb.xml b/adb/adb_integration_test_adb.xml
new file mode 100644
index 0000000..e722956
--- /dev/null
+++ b/adb/adb_integration_test_adb.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2018 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.
+-->
+<configuration description="Config to run adb integration tests">
+ <option name="test-suite-tag" value="adb_tests" />
+ <option name="test-suite-tag" value="adb_integration" />
+ <target_preparer class="com.android.tradefed.targetprep.SemaphoreTokenTargetPreparer">
+ <option name="disable" value="false" />
+ </target_preparer>
+
+ <target_preparer class="com.android.tradefed.targetprep.adb.AdbStopServerPreparer" />
+ <test class="com.android.tradefed.testtype.python.PythonBinaryHostTest" >
+ <option name="par-file-name" value="adb_integration_test_adb" />
+ <option name="inject-android-serial" value="true" />
+ <option name="test-timeout" value="2m" />
+ </test>
+</configuration>
diff --git a/adb/adb_io.cpp b/adb/adb_io.cpp
index 38e3116..6cc274b 100644
--- a/adb/adb_io.cpp
+++ b/adb/adb_io.cpp
@@ -49,7 +49,7 @@
}
buf[4] = 0;
- unsigned long len = strtoul(buf, 0, 16);
+ unsigned long len = strtoul(buf, nullptr, 16);
s->resize(len, '\0');
if (!ReadFdExactly(fd, &(*s)[0], len)) {
*error = perror_str("protocol fault (couldn't read status message)");
diff --git a/adb/adb_listeners.cpp b/adb/adb_listeners.cpp
index a142384..f4a92e3 100644
--- a/adb/adb_listeners.cpp
+++ b/adb/adb_listeners.cpp
@@ -29,6 +29,7 @@
#include "socket_spec.h"
#include "sysdeps.h"
+#include "sysdeps/memory.h"
#include "transport.h"
// A listener is an entity which binds to a local port and, upon receiving a connection on that
@@ -41,7 +42,7 @@
alistener(const std::string& _local_name, const std::string& _connect_to);
~alistener();
- fdevent fde;
+ fdevent* fde = nullptr;
int fd = -1;
std::string local_name;
@@ -59,7 +60,7 @@
alistener::~alistener() {
// Closes the corresponding fd.
- fdevent_remove(&fde);
+ fdevent_destroy(fde);
if (transport) {
transport->RemoveDisconnect(&disconnect);
@@ -135,9 +136,10 @@
}
// <device-serial> " " <local-name> " " <remote-name> "\n"
// Entries from "adb reverse" have no serial.
- android::base::StringAppendF(&result, "%s %s %s\n",
- l->transport->serial ? l->transport->serial : "(reverse)",
- l->local_name.c_str(), l->connect_to.c_str());
+ android::base::StringAppendF(
+ &result, "%s %s %s\n",
+ !l->transport->serial.empty() ? l->transport->serial.c_str() : "(reverse)",
+ l->local_name.c_str(), l->connect_to.c_str());
}
return result;
}
@@ -203,7 +205,7 @@
}
}
- std::unique_ptr<alistener> listener(new alistener(local_name, connect_to));
+ auto listener = std::make_unique<alistener>(local_name, connect_to);
int resolved = 0;
listener->fd = socket_spec_listen(listener->local_name, error, &resolved);
@@ -221,11 +223,11 @@
close_on_exec(listener->fd);
if (listener->connect_to == "*smartsocket*") {
- fdevent_install(&listener->fde, listener->fd, ss_listener_event_func, listener.get());
+ listener->fde = fdevent_create(listener->fd, ss_listener_event_func, listener.get());
} else {
- fdevent_install(&listener->fde, listener->fd, listener_event_func, listener.get());
+ listener->fde = fdevent_create(listener->fd, listener_event_func, listener.get());
}
- fdevent_set(&listener->fde, FDE_READ);
+ fdevent_set(listener->fde, FDE_READ);
listener->transport = transport;
diff --git a/adb/remount_service.h b/adb/adb_unique_fd.cpp
similarity index 72%
copy from adb/remount_service.h
copy to adb/adb_unique_fd.cpp
index 7bda1be..dec73bc 100644
--- a/adb/remount_service.h
+++ b/adb/adb_unique_fd.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,12 +14,15 @@
* limitations under the License.
*/
-#ifndef _REMOUNT_SERVICE_H_
-#define _REMOUNT_SERVICE_H_
+#include "adb_unique_fd.h"
-#include <string>
+#include <errno.h>
+#include <unistd.h>
-bool make_block_device_writable(const std::string&);
-void remount_service(int, void*);
+#include "sysdeps.h"
+#if defined(_WIN32)
+void AdbCloser::Close(int fd) {
+ adb_close(fd);
+}
#endif
diff --git a/adb/adb_unique_fd.h b/adb/adb_unique_fd.h
index 34c1bbc..d47213d 100644
--- a/adb/adb_unique_fd.h
+++ b/adb/adb_unique_fd.h
@@ -16,11 +16,22 @@
#pragma once
+#include <errno.h>
+#include <unistd.h>
+
#include <android-base/unique_fd.h>
+#if defined(_WIN32)
// Helper to automatically close an FD when it goes out of scope.
struct AdbCloser {
static void Close(int fd);
};
using unique_fd = android::base::unique_fd_impl<AdbCloser>;
+#else
+using unique_fd = android::base::unique_fd;
+#endif
+
+template <typename T>
+int adb_close(const android::base::unique_fd_impl<T>&)
+ __attribute__((__unavailable__("adb_close called on unique_fd")));
diff --git a/adb/adb_utils.cpp b/adb/adb_utils.cpp
index b236fb3..ffac315 100644
--- a/adb/adb_utils.cpp
+++ b/adb/adb_utils.cpp
@@ -79,22 +79,24 @@
}
std::string escape_arg(const std::string& s) {
- std::string result = s;
-
// Escape any ' in the string (before we single-quote the whole thing).
// The correct way to do this for the shell is to replace ' with '\'' --- that is,
// close the existing single-quoted string, escape a single single-quote, and start
// a new single-quoted string. Like the C preprocessor, the shell will concatenate
// these pieces into one string.
- for (size_t i = 0; i < s.size(); ++i) {
- if (s[i] == '\'') {
- result.insert(i, "'\\'");
- i += 2;
- }
+
+ std::string result;
+ result.push_back('\'');
+
+ size_t base = 0;
+ while (true) {
+ size_t found = s.find('\'', base);
+ result.append(s, base, found - base);
+ if (found == s.npos) break;
+ result.append("'\\''");
+ base = found + 1;
}
- // Prefix and suffix the whole string with '.
- result.insert(result.begin(), '\'');
result.push_back('\'');
return result;
}
@@ -272,10 +274,6 @@
return android_dir;
}
-void AdbCloser::Close(int fd) {
- adb_close(fd);
-}
-
int syntax_error(const char* fmt, ...) {
fprintf(stderr, "adb: usage: ");
diff --git a/adb/adb_utils_test.cpp b/adb/adb_utils_test.cpp
index e1b6287..341323f 100644
--- a/adb/adb_utils_test.cpp
+++ b/adb/adb_utils_test.cpp
@@ -82,30 +82,38 @@
#endif
TEST(adb_utils, escape_arg) {
- ASSERT_EQ(R"('')", escape_arg(""));
+ EXPECT_EQ(R"('')", escape_arg(""));
- ASSERT_EQ(R"('abc')", escape_arg("abc"));
+ EXPECT_EQ(R"('abc')", escape_arg("abc"));
- ASSERT_EQ(R"(' abc')", escape_arg(" abc"));
- ASSERT_EQ(R"(''\''abc')", escape_arg("'abc"));
- ASSERT_EQ(R"('"abc')", escape_arg("\"abc"));
- ASSERT_EQ(R"('\abc')", escape_arg("\\abc"));
- ASSERT_EQ(R"('(abc')", escape_arg("(abc"));
- ASSERT_EQ(R"(')abc')", escape_arg(")abc"));
+ auto wrap = [](const std::string& x) { return '\'' + x + '\''; };
+ const std::string q = R"('\'')";
+ EXPECT_EQ(wrap(q), escape_arg("'"));
+ EXPECT_EQ(wrap(q + q), escape_arg("''"));
+ EXPECT_EQ(wrap(q + "abc" + q), escape_arg("'abc'"));
+ EXPECT_EQ(wrap(q + "abc"), escape_arg("'abc"));
+ EXPECT_EQ(wrap("abc" + q), escape_arg("abc'"));
+ EXPECT_EQ(wrap("abc" + q + "def"), escape_arg("abc'def"));
+ EXPECT_EQ(wrap("a" + q + "b" + q + "c"), escape_arg("a'b'c"));
+ EXPECT_EQ(wrap("a" + q + "bcde" + q + "f"), escape_arg("a'bcde'f"));
- ASSERT_EQ(R"('abc abc')", escape_arg("abc abc"));
- ASSERT_EQ(R"('abc'\''abc')", escape_arg("abc'abc"));
- ASSERT_EQ(R"('abc"abc')", escape_arg("abc\"abc"));
- ASSERT_EQ(R"('abc\abc')", escape_arg("abc\\abc"));
- ASSERT_EQ(R"('abc(abc')", escape_arg("abc(abc"));
- ASSERT_EQ(R"('abc)abc')", escape_arg("abc)abc"));
+ EXPECT_EQ(R"(' abc')", escape_arg(" abc"));
+ EXPECT_EQ(R"('"abc')", escape_arg("\"abc"));
+ EXPECT_EQ(R"('\abc')", escape_arg("\\abc"));
+ EXPECT_EQ(R"('(abc')", escape_arg("(abc"));
+ EXPECT_EQ(R"(')abc')", escape_arg(")abc"));
- ASSERT_EQ(R"('abc ')", escape_arg("abc "));
- ASSERT_EQ(R"('abc'\''')", escape_arg("abc'"));
- ASSERT_EQ(R"('abc"')", escape_arg("abc\""));
- ASSERT_EQ(R"('abc\')", escape_arg("abc\\"));
- ASSERT_EQ(R"('abc(')", escape_arg("abc("));
- ASSERT_EQ(R"('abc)')", escape_arg("abc)"));
+ EXPECT_EQ(R"('abc abc')", escape_arg("abc abc"));
+ EXPECT_EQ(R"('abc"abc')", escape_arg("abc\"abc"));
+ EXPECT_EQ(R"('abc\abc')", escape_arg("abc\\abc"));
+ EXPECT_EQ(R"('abc(abc')", escape_arg("abc(abc"));
+ EXPECT_EQ(R"('abc)abc')", escape_arg("abc)abc"));
+
+ EXPECT_EQ(R"('abc ')", escape_arg("abc "));
+ EXPECT_EQ(R"('abc"')", escape_arg("abc\""));
+ EXPECT_EQ(R"('abc\')", escape_arg("abc\\"));
+ EXPECT_EQ(R"('abc(')", escape_arg("abc("));
+ EXPECT_EQ(R"('abc)')", escape_arg("abc)"));
}
void test_mkdirs(const std::string& basepath) {
diff --git a/adb/benchmark_device.py b/adb/benchmark_device.py
new file mode 100755
index 0000000..00c2315
--- /dev/null
+++ b/adb/benchmark_device.py
@@ -0,0 +1,120 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2018 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.
+#
+
+import os
+import statistics
+import time
+
+import adb
+
+def lock_min(device):
+ device.shell_nocheck(["""
+ for x in /sys/devices/system/cpu/cpu?/cpufreq; do
+ echo userspace > $x/scaling_governor
+ cat $x/scaling_min_freq > $x/scaling_setspeed
+ done
+ """])
+
+def lock_max(device):
+ device.shell_nocheck(["""
+ for x in /sys/devices/system/cpu/cpu?/cpufreq; do
+ echo userspace > $x/scaling_governor
+ cat $x/scaling_max_freq > $x/scaling_setspeed
+ done
+ """])
+
+def unlock(device):
+ device.shell_nocheck(["""
+ for x in /sys/devices/system/cpu/cpu?/cpufreq; do
+ echo ondemand > $x/scaling_governor
+ echo sched > $x/scaling_governor
+ echo schedutil > $x/scaling_governor
+ done
+ """])
+
+def harmonic_mean(xs):
+ return 1.0 / statistics.mean([1.0 / x for x in xs])
+
+def analyze(name, speeds):
+ median = statistics.median(speeds)
+ mean = harmonic_mean(speeds)
+ stddev = statistics.stdev(speeds)
+ msg = "%s: %d runs: median %.2f MiB/s, mean %.2f MiB/s, stddev: %.2f MiB/s"
+ print(msg % (name, len(speeds), median, mean, stddev))
+
+def benchmark_push(device=None, file_size_mb=100):
+ if device == None:
+ device = adb.get_device()
+
+ lock_max(device)
+
+ remote_path = "/dev/null"
+ local_path = "/tmp/adb_benchmark_temp"
+
+ with open(local_path, "wb") as f:
+ f.truncate(file_size_mb * 1024 * 1024)
+
+ speeds = list()
+ for _ in range(0, 5):
+ begin = time.time()
+ device.push(local=local_path, remote=remote_path)
+ end = time.time()
+ speeds.append(file_size_mb / float(end - begin))
+
+ analyze("push %dMiB" % file_size_mb, speeds)
+
+def benchmark_pull(device=None, file_size_mb=100):
+ if device == None:
+ device = adb.get_device()
+
+ lock_max(device)
+
+ remote_path = "/data/local/tmp/adb_benchmark_temp"
+ local_path = "/tmp/adb_benchmark_temp"
+
+ device.shell(["dd", "if=/dev/zero", "of=" + remote_path, "bs=1m",
+ "count=" + str(file_size_mb)])
+ speeds = list()
+ for _ in range(0, 5):
+ begin = time.time()
+ device.pull(remote=remote_path, local=local_path)
+ end = time.time()
+ speeds.append(file_size_mb / float(end - begin))
+
+ analyze("pull %dMiB" % file_size_mb, speeds)
+
+def benchmark_shell(device=None, file_size_mb=100):
+ if device == None:
+ device = adb.get_device()
+
+ lock_max(device)
+
+ speeds = list()
+ for _ in range(0, 5):
+ begin = time.time()
+ device.shell(["dd", "if=/dev/zero", "bs=1m",
+ "count=" + str(file_size_mb)])
+ end = time.time()
+ speeds.append(file_size_mb / float(end - begin))
+
+ analyze("shell %dMiB" % file_size_mb, speeds)
+
+def main():
+ benchmark_pull()
+
+if __name__ == "__main__":
+ main()
diff --git a/adb/client/adb_client.cpp b/adb/client/adb_client.cpp
index 849a6e7..1959258 100644
--- a/adb/client/adb_client.cpp
+++ b/adb/client/adb_client.cpp
@@ -46,7 +46,7 @@
#include "sysdeps/chrono.h"
static TransportType __adb_transport = kTransportAny;
-static const char* __adb_serial = NULL;
+static const char* __adb_serial = nullptr;
static TransportId __adb_transport_id = 0;
static const char* __adb_server_socket_spec;
diff --git a/adb/client/auth.cpp b/adb/client/auth.cpp
index c3aef16..71c19b8 100644
--- a/adb/client/auth.cpp
+++ b/adb/client/auth.cpp
@@ -109,7 +109,7 @@
LOG(INFO) << "generate_key(" << file << ")...";
mode_t old_mask;
- FILE *f = NULL;
+ FILE *f = nullptr;
int ret = 0;
EVP_PKEY* pkey = EVP_PKEY_new();
@@ -121,7 +121,7 @@
}
BN_set_word(exponent, RSA_F4);
- RSA_generate_key_ex(rsa, 2048, exponent, NULL);
+ RSA_generate_key_ex(rsa, 2048, exponent, nullptr);
EVP_PKEY_set1_RSA(pkey, rsa);
old_mask = umask(077);
@@ -135,7 +135,7 @@
umask(old_mask);
- if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
+ if (!PEM_write_PrivateKey(f, pkey, nullptr, nullptr, 0, nullptr, nullptr)) {
D("Failed to write key");
goto out;
}
@@ -302,7 +302,7 @@
static std::string adb_auth_sign(RSA* key, const char* token, size_t token_size) {
if (token_size != TOKEN_SIZE) {
D("Unexpected token size %zd", token_size);
- return 0;
+ return nullptr;
}
std::string result;
@@ -454,10 +454,8 @@
p->msg.command = A_AUTH;
p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
- p->payload = std::move(key);
-
// adbd expects a null-terminated string.
- p->payload.push_back('\0');
+ p->payload.assign(key.data(), key.data() + key.size() + 1);
p->msg.data_length = p->payload.size();
send_packet(p, t);
}
@@ -466,6 +464,8 @@
std::shared_ptr<RSA> key = t->NextKey();
if (key == nullptr) {
// No more private keys to try, send the public key.
+ t->SetConnectionState(kCsUnauthorized);
+ t->SetConnectionEstablished(true);
send_auth_publickey(t);
return;
}
@@ -482,7 +482,7 @@
p->msg.command = A_AUTH;
p->msg.arg0 = ADB_AUTH_SIGNATURE;
- p->payload = std::move(result);
+ p->payload.assign(result.begin(), result.end());
p->msg.data_length = p->payload.size();
send_packet(p, t);
}
diff --git a/adb/client/bugreport.cpp b/adb/client/bugreport.cpp
index abef86a..fe98737 100644
--- a/adb/client/bugreport.cpp
+++ b/adb/client/bugreport.cpp
@@ -16,6 +16,8 @@
#define TRACE_TAG ADB
+#include "sysdeps.h"
+
#include "bugreport.h"
#include <string>
@@ -24,9 +26,8 @@
#include <android-base/file.h>
#include <android-base/strings.h>
-#include "sysdeps.h"
#include "adb_utils.h"
-#include "file_sync_service.h"
+#include "client/file_sync_client.h"
static constexpr char BUGZ_BEGIN_PREFIX[] = "BEGIN:";
static constexpr char BUGZ_PROGRESS_PREFIX[] = "PROGRESS:";
diff --git a/adb/client/commandline.cpp b/adb/client/commandline.cpp
index d126f52..41ac663 100644
--- a/adb/client/commandline.cpp
+++ b/adb/client/commandline.cpp
@@ -56,11 +56,12 @@
#include "adb_unique_fd.h"
#include "adb_utils.h"
#include "bugreport.h"
+#include "client/file_sync_client.h"
#include "commandline.h"
-#include "file_sync_service.h"
#include "services.h"
-#include "shell_service.h"
+#include "shell_protocol.h"
#include "sysdeps/chrono.h"
+#include "sysdeps/memory.h"
static int install_app(int argc, const char** argv);
static int install_multiple_app(int argc, const char** argv);
@@ -72,13 +73,13 @@
DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK(nullptr, nullptr);
-static std::string product_file(const char* file) {
+static std::string product_file(const std::string& file) {
const char* ANDROID_PRODUCT_OUT = getenv("ANDROID_PRODUCT_OUT");
if (ANDROID_PRODUCT_OUT == nullptr) {
fprintf(stderr, "adb: product directory not specified; set $ANDROID_PRODUCT_OUT\n");
exit(1);
}
- return android::base::StringPrintf("%s%s%s", ANDROID_PRODUCT_OUT, OS_PATH_SEPARATOR_STR, file);
+ return std::string{ANDROID_PRODUCT_OUT} + OS_PATH_SEPARATOR_STR + file;
}
static void help() {
@@ -133,7 +134,7 @@
" pull [-a] REMOTE... LOCAL\n"
" copy files/dirs from device\n"
" -a: preserve file timestamp and mode\n"
- " sync [system|vendor|oem|data|all]\n"
+ " sync [all|data|odm|oem|product|system|vendor]\n"
" sync a local build from $ANDROID_PRODUCT_OUT to the device (default all)\n"
" -l: list but don't copy\n"
"\n"
@@ -148,8 +149,8 @@
" emu COMMAND run emulator console command\n"
"\n"
"app installation:\n"
- " install [-lrtsdg] PACKAGE\n"
- " install-multiple [-lrtsdpg] PACKAGE...\n"
+ " install [-lrtsdg] [--instant] PACKAGE\n"
+ " install-multiple [-lrtsdpg] [--instant] PACKAGE...\n"
" push package(s) to the device and install them\n"
" -l: forward lock application\n"
" -r: replace existing application\n"
@@ -158,6 +159,7 @@
" -d: allow version code downgrade (debuggable packages only)\n"
" -p: partial application install (install-multiple only)\n"
" -g: grant all runtime permissions\n"
+ " --instant: cause the app to be installed as an ephemeral install app\n"
" uninstall [-k] PACKAGE\n"
" remove this app package from the device\n"
" '-k': keep the data and cache directories\n"
@@ -188,8 +190,7 @@
" get-state print offline | bootloader | device\n"
" get-serialno print <serial-number>\n"
" get-devpath print <device-path>\n"
- " remount\n"
- " remount /system, /vendor, and /oem partitions read-write\n"
+ " remount remount partitions read-write\n"
" reboot [bootloader|recovery|sideload|sideload-auto-reboot]\n"
" reboot the device; defaults to booting system image but\n"
" supports bootloader and recovery too. sideload reboots\n"
@@ -263,7 +264,7 @@
char raw_buffer[BUFSIZ];
char* buffer_ptr = raw_buffer;
if (use_shell_protocol) {
- protocol.reset(new ShellProtocol(fd));
+ protocol = std::make_unique<ShellProtocol>(fd);
if (!protocol) {
LOG(ERROR) << "failed to allocate memory for ShellProtocol object";
return 1;
@@ -361,9 +362,8 @@
}
static void copy_to_file(int inFd, int outFd) {
- const size_t BUFSIZE = 32 * 1024;
- char* buf = (char*) malloc(BUFSIZE);
- if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file");
+ constexpr size_t BUFSIZE = 32 * 1024;
+ std::vector<char> buf(BUFSIZE);
int len;
long total = 0;
int old_stdin_mode = -1;
@@ -375,9 +375,9 @@
while (true) {
if (inFd == STDIN_FILENO) {
- len = unix_read(inFd, buf, BUFSIZE);
+ len = unix_read(inFd, buf.data(), BUFSIZE);
} else {
- len = adb_read(inFd, buf, BUFSIZE);
+ len = adb_read(inFd, buf.data(), BUFSIZE);
}
if (len == 0) {
D("copy_to_file() : read 0 bytes; exiting");
@@ -388,10 +388,10 @@
break;
}
if (outFd == STDOUT_FILENO) {
- fwrite(buf, 1, len, stdout);
+ fwrite(buf.data(), 1, len, stdout);
fflush(stdout);
} else {
- adb_write(outFd, buf, len);
+ adb_write(outFd, buf.data(), len);
}
total += len;
}
@@ -399,7 +399,6 @@
stdinout_raw_epilogue(inFd, outFd, old_stdin_mode, old_stdout_mode);
D("copy_to_file() finished after %lu bytes", total);
- free(buf);
}
static void send_window_size_change(int fd, std::unique_ptr<ShellProtocol>& shell) {
@@ -630,7 +629,7 @@
args->raw_stdin = raw_stdin;
args->escape_char = escape_char;
if (use_shell_protocol) {
- args->protocol.reset(new ShellProtocol(args->write_fd));
+ args->protocol = std::make_unique<ShellProtocol>(args->write_fd);
}
if (raw_stdin) stdin_raw_init();
@@ -884,7 +883,7 @@
return 0;
}
- int block = strtol(buf, NULL, 10);
+ int block = strtol(buf, nullptr, 10);
size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
if (offset >= static_cast<size_t>(sb.st_size)) {
@@ -967,7 +966,7 @@
//argv[2] and beyond become ppp_args[1] and beyond
ppp_args[i - 1] = argv[i];
}
- ppp_args[i-1] = NULL;
+ ppp_args[i-1] = nullptr;
// child side
@@ -1135,31 +1134,28 @@
cmd += " " + escape_arg(*argv++);
}
- // No need for shell protocol with logcat, always disable for simplicity.
- return send_shell_command(cmd, true);
+ return send_shell_command(cmd);
}
static void write_zeros(int bytes, int fd) {
int old_stdin_mode = -1;
int old_stdout_mode = -1;
- char* buf = (char*) calloc(1, bytes);
- if (buf == nullptr) fatal("couldn't allocate buffer for write_zeros");
+ std::vector<char> buf(bytes);
D("write_zeros(%d) -> %d", bytes, fd);
stdinout_raw_prologue(-1, fd, old_stdin_mode, old_stdout_mode);
if (fd == STDOUT_FILENO) {
- fwrite(buf, 1, bytes, stdout);
+ fwrite(buf.data(), 1, bytes, stdout);
fflush(stdout);
} else {
- adb_write(fd, buf, bytes);
+ adb_write(fd, buf.data(), bytes);
}
stdinout_raw_prologue(-1, fd, old_stdin_mode, old_stdout_mode);
D("write_zeros() finished");
- free(buf);
}
static int backup(int argc, const char** argv) {
@@ -1174,7 +1170,7 @@
argv[i++] = argv[j++];
}
argc -= 2;
- argv[argc] = NULL;
+ argv[argc] = nullptr;
}
}
@@ -1330,9 +1326,9 @@
}
int adb_commandline(int argc, const char** argv) {
- int no_daemon = 0;
- int is_daemon = 0;
- int is_server = 0;
+ bool no_daemon = false;
+ bool is_daemon = false;
+ bool is_server = false;
int r;
TransportType transport_type = kTransportAny;
int ack_reply_fd = -1;
@@ -1352,12 +1348,12 @@
while (argc > 0) {
if (!strcmp(argv[0],"server")) {
- is_server = 1;
+ is_server = true;
} else if (!strcmp(argv[0],"nodaemon")) {
- no_daemon = 1;
+ no_daemon = true;
} else if (!strcmp(argv[0], "fork-server")) {
/* this is a special flag used only when the ADB client launches the ADB Server */
- is_daemon = 1;
+ is_daemon = true;
} else if (!strcmp(argv[0], "--reply-fd")) {
if (argc < 2) return syntax_error("--reply-fd requires an argument");
const char* reply_fd_str = argv[1];
@@ -1618,9 +1614,9 @@
return bugreport.DoIt(argc, argv);
} else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) {
bool reverse = !strcmp(argv[0], "reverse");
- ++argv;
--argc;
if (argc < 1) return syntax_error("%s requires an argument", argv[0]);
+ ++argv;
// Determine the <host-prefix> for this command.
std::string host_prefix;
@@ -1730,48 +1726,28 @@
std::string src;
bool list_only = false;
if (argc < 2) {
- // No local path was specified.
- src = "";
+ // No partition specified: sync all of them.
} else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
list_only = true;
- if (argc == 3) {
- src = argv[2];
- } else {
- src = "";
- }
+ if (argc == 3) src = argv[2];
} else if (argc == 2) {
- // A local path or "android"/"data" arg was specified.
src = argv[1];
} else {
return syntax_error("adb sync [-l] [PARTITION]");
}
- if (src == "all") src = "";
-
- if (src != "" &&
- src != "system" && src != "data" && src != "vendor" && src != "oem") {
- return syntax_error("don't know how to sync %s partition", src.c_str());
+ if (src.empty()) src = "all";
+ std::vector<std::string> partitions{"data", "odm", "oem", "product", "system", "vendor"};
+ bool found = false;
+ for (const auto& partition : partitions) {
+ if (src == "all" || src == partition) {
+ std::string src_dir{product_file(partition)};
+ if (!directory_exists(src_dir)) continue;
+ found = true;
+ if (!do_sync_sync(src_dir, "/" + partition, list_only)) return 1;
+ }
}
-
- std::string system_src_path = product_file("system");
- std::string data_src_path = product_file("data");
- std::string vendor_src_path = product_file("vendor");
- std::string oem_src_path = product_file("oem");
-
- bool okay = true;
- if (okay && (src.empty() || src == "system")) {
- okay = do_sync_sync(system_src_path, "/system", list_only);
- }
- if (okay && (src.empty() || src == "vendor") && directory_exists(vendor_src_path)) {
- okay = do_sync_sync(vendor_src_path, "/vendor", list_only);
- }
- if (okay && (src.empty() || src == "oem") && directory_exists(oem_src_path)) {
- okay = do_sync_sync(oem_src_path, "/oem", list_only);
- }
- if (okay && (src.empty() || src == "data")) {
- okay = do_sync_sync(data_src_path, "/data", list_only);
- }
- return okay ? 0 : 1;
+ return found ? 0 : syntax_error("don't know how to sync %s partition", src.c_str());
}
/* passthrough commands */
else if (!strcmp(argv[0],"get-state") ||
@@ -1815,6 +1791,11 @@
}
else if (!strcmp(argv[0], "track-devices")) {
return adb_connect_command("host:track-devices");
+ } else if (!strcmp(argv[0], "raw")) {
+ if (argc != 2) {
+ return syntax_error("adb raw SERVICE");
+ }
+ return adb_connect_command(argv[1]);
}
@@ -1880,7 +1861,7 @@
cmd += " " + escape_arg(*argv++);
}
- return send_shell_command(cmd, false);
+ return send_shell_command(cmd);
}
static int install_app(int argc, const char** argv) {
@@ -1944,7 +1925,8 @@
for (int i = argc - 1; i >= 0; i--) {
const char* file = argv[i];
- if (android::base::EndsWithIgnoreCase(file, ".apk")) {
+ if (android::base::EndsWithIgnoreCase(file, ".apk") ||
+ android::base::EndsWithIgnoreCase(file, ".dm")) {
struct stat sb;
if (stat(file, &sb) != -1) total_size += sb.st_size;
first_apk = i;
@@ -1984,7 +1966,7 @@
char* end = strrchr(buf, ']');
if (start && end) {
*end = '\0';
- session_id = strtol(start + 1, NULL, 10);
+ session_id = strtol(start + 1, nullptr, 10);
}
}
if (session_id < 0) {
@@ -2005,9 +1987,8 @@
}
std::string cmd = android::base::StringPrintf(
- "%s install-write -S %" PRIu64 " %d %d_%s -",
- install_cmd.c_str(), static_cast<uint64_t>(sb.st_size), session_id, i,
- android::base::Basename(file).c_str());
+ "%s install-write -S %" PRIu64 " %d %s -", install_cmd.c_str(),
+ static_cast<uint64_t>(sb.st_size), session_id, android::base::Basename(file).c_str());
int localFd = adb_open(file, O_RDONLY);
if (localFd < 0) {
@@ -2068,7 +2049,7 @@
cmd += " " + escape_arg(*argv++);
}
- return send_shell_command(cmd, false);
+ return send_shell_command(cmd);
}
static int uninstall_app_legacy(int argc, const char** argv) {
@@ -2092,7 +2073,7 @@
static int delete_file(const std::string& filename) {
std::string cmd = "rm -f " + escape_arg(filename);
- return send_shell_command(cmd, false);
+ return send_shell_command(cmd);
}
static int install_app_legacy(int argc, const char** argv) {
diff --git a/adb/client/commandline.h b/adb/client/commandline.h
index 36cd798..3aa03a7 100644
--- a/adb/client/commandline.h
+++ b/adb/client/commandline.h
@@ -83,6 +83,14 @@
DISALLOW_COPY_AND_ASSIGN(DefaultStandardStreamsCallback);
};
+class SilentStandardStreamsCallbackInterface : public StandardStreamsCallbackInterface {
+ public:
+ SilentStandardStreamsCallbackInterface() = default;
+ void OnStdout(const char*, int) override final {}
+ void OnStderr(const char*, int) override final {}
+ int Done(int status) override final { return status; }
+};
+
// Singleton.
extern DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK;
@@ -92,7 +100,7 @@
// resulting output.
// if |callback| is non-null, stdout/stderr output will be handled by it.
int send_shell_command(
- const std::string& command, bool disable_shell_protocol,
- StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);
+ const std::string& command, bool disable_shell_protocol = false,
+ StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);
#endif // COMMANDLINE_H
diff --git a/adb/client/file_sync_client.cpp b/adb/client/file_sync_client.cpp
index 26f8d83..cb9bcfa 100644
--- a/adb/client/file_sync_client.cpp
+++ b/adb/client/file_sync_client.cpp
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+#include "client/file_sync_client.h"
+
#include <dirent.h>
#include <inttypes.h>
#include <limits.h>
@@ -39,11 +41,13 @@
#include "adb_client.h"
#include "adb_io.h"
#include "adb_utils.h"
-#include "file_sync_service.h"
+#include "file_sync_protocol.h"
#include "line_printer.h"
#include "sysdeps/errno.h"
#include "sysdeps/stat.h"
+#include "client/commandline.h"
+
#include <android-base/file.h>
#include <android-base/strings.h>
#include <android-base/stringprintf.h>
@@ -202,12 +206,11 @@
max = SYNC_DATA_MAX; // TODO: decide at runtime.
std::string error;
- FeatureSet features;
- if (!adb_get_feature_set(&features, &error)) {
+ if (!adb_get_feature_set(&features_, &error)) {
fd = -1;
Error("failed to get feature set: %s", error.c_str());
} else {
- have_stat_v2_ = CanUseFeature(features, kFeatureStat2);
+ have_stat_v2_ = CanUseFeature(features_, kFeatureStat2);
fd = adb_connect("sync:", &error);
if (fd < 0) {
Error("connect failed: %s", error.c_str());
@@ -232,6 +235,8 @@
line_printer_.KeepInfoLine();
}
+ const FeatureSet& Features() const { return features_; }
+
bool IsValid() { return fd >= 0; }
bool ReceivedError(const char* from, const char* to) {
@@ -510,8 +515,7 @@
return false;
}
-
- void Printf(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
+ void Printf(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))) {
std::string s;
va_list ap;
@@ -522,7 +526,7 @@
line_printer_.Print(s, LinePrinter::INFO);
}
- void Println(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
+ void Println(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))) {
std::string s;
va_list ap;
@@ -534,7 +538,7 @@
line_printer_.KeepInfoLine();
}
- void Error(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
+ void Error(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))) {
std::string s = "adb: error: ";
va_list ap;
@@ -545,7 +549,7 @@
line_printer_.Print(s, LinePrinter::ERROR);
}
- void Warning(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
+ void Warning(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))) {
std::string s = "adb: warning: ";
va_list ap;
@@ -577,6 +581,7 @@
private:
bool expect_done_;
+ FeatureSet features_;
bool have_stat_v2_;
TransferLedger global_ledger_;
@@ -806,7 +811,7 @@
}
static bool local_build_list(SyncConnection& sc, std::vector<copyinfo>* file_list,
- const std::string& lpath,
+ std::vector<std::string>* directory_list, const std::string& lpath,
const std::string& rpath) {
std::vector<copyinfo> dirlist;
std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(lpath.c_str()), closedir);
@@ -849,21 +854,9 @@
// Close this directory and recurse.
dir.reset();
- // Add the current directory to the list if it was empty, to ensure that
- // it gets created.
- if (empty_dir) {
- // TODO(b/25566053): Make pushing empty directories work.
- // TODO(b/25457350): We don't preserve permissions on directories.
- sc.Warning("skipping empty directory '%s'", lpath.c_str());
- copyinfo ci(android::base::Dirname(lpath), android::base::Dirname(rpath),
- android::base::Basename(lpath), S_IFDIR);
- ci.skip = true;
- file_list->push_back(ci);
- return true;
- }
-
for (const copyinfo& ci : dirlist) {
- local_build_list(sc, file_list, ci.lpath, ci.rpath);
+ directory_list->push_back(ci.rpath);
+ local_build_list(sc, file_list, directory_list, ci.lpath, ci.rpath);
}
return true;
@@ -880,11 +873,54 @@
// Recursively build the list of files to copy.
std::vector<copyinfo> file_list;
+ std::vector<std::string> directory_list;
+
+ for (std::string dirpath = rpath; dirpath != "/"; dirpath = android::base::Dirname(dirpath)) {
+ directory_list.push_back(dirpath);
+ }
+ std::reverse(directory_list.begin(), directory_list.end());
+
int skipped = 0;
- if (!local_build_list(sc, &file_list, lpath, rpath)) {
+ if (!local_build_list(sc, &file_list, &directory_list, lpath, rpath)) {
return false;
}
+ // b/110953234:
+ // P shipped with a bug that causes directory creation as a side-effect of a push to fail.
+ // Work around this by explicitly doing a mkdir via shell.
+ //
+ // Devices that don't support shell_v2 are unhappy if we try to send a too-long packet to them,
+ // but they're not affected by this bug, so only apply the workaround if we have shell_v2.
+ //
+ // TODO(b/25457350): We don't preserve permissions on directories.
+ // TODO: Find all of the leaves and `mkdir -p` them instead?
+ if (CanUseFeature(sc.Features(), kFeatureShell2)) {
+ SilentStandardStreamsCallbackInterface cb;
+ std::string cmd = "mkdir";
+ for (const auto& dir : directory_list) {
+ std::string escaped_path = escape_arg(dir);
+ if (escaped_path.size() > 16384) {
+ // Somewhat arbitrarily limit that probably won't ever happen.
+ sc.Error("path too long: %s", escaped_path.c_str());
+ return false;
+ }
+
+ // The maximum should be 64kiB, but that's not including other stuff that gets tacked
+ // onto the command line, so let's be a bit conservative.
+ if (cmd.size() + escaped_path.size() > 32768) {
+ // Dispatch the command, ignoring failure (since the directory might already exist).
+ send_shell_command(cmd, false, &cb);
+ cmd = "mkdir";
+ }
+ cmd += " ";
+ cmd += escaped_path;
+ }
+
+ if (cmd != "mkdir") {
+ send_shell_command(cmd, false, &cb);
+ }
+ }
+
if (check_timestamps) {
for (const copyinfo& ci : file_list) {
if (!sc.SendLstat(ci.rpath.c_str())) {
diff --git a/adb/client/file_sync_client.h b/adb/client/file_sync_client.h
new file mode 100644
index 0000000..df7f14c
--- /dev/null
+++ b/adb/client/file_sync_client.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+bool do_sync_ls(const char* path);
+bool do_sync_push(const std::vector<const char*>& srcs, const char* dst, bool sync);
+bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs,
+ const char* name = nullptr);
+
+bool do_sync_sync(const std::string& lpath, const std::string& rpath, bool list_only);
diff --git a/adb/client/line_printer.cpp b/adb/client/line_printer.cpp
index 64d10b6..9758526 100644
--- a/adb/client/line_printer.cpp
+++ b/adb/client/line_printer.cpp
@@ -52,7 +52,7 @@
// MSDN says: "For some systems, [_IOLBF] provides line
// buffering. However, for Win32, the behavior is the same as _IOFBF
// - Full Buffering."
- setvbuf(stdout, NULL, _IONBF, 0);
+ setvbuf(stdout, nullptr, _IONBF, 0);
console_ = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
smart_terminal_ = GetConsoleScreenBufferInfo(console_, &csbi);
diff --git a/adb/client/main.cpp b/adb/client/main.cpp
index 31cb853..095ad98 100644
--- a/adb/client/main.cpp
+++ b/adb/client/main.cpp
@@ -89,10 +89,10 @@
// unbuffer stdout and stderr just like if we were run at the console.
// This also keeps stderr unbuffered when it is redirected to adb.log.
if (is_daemon) {
- if (setvbuf(stdout, NULL, _IONBF, 0) == -1) {
+ if (setvbuf(stdout, nullptr, _IONBF, 0) == -1) {
fatal("cannot make stdout unbuffered: %s", strerror(errno));
}
- if (setvbuf(stderr, NULL, _IONBF, 0) == -1) {
+ if (setvbuf(stderr, nullptr, _IONBF, 0) == -1) {
fatal("cannot make stderr unbuffered: %s", strerror(errno));
}
}
@@ -117,10 +117,21 @@
atexit(adb_server_cleanup);
init_transport_registration();
- init_mdns_transport_discovery();
+ init_reconnect_handler();
- usb_init();
- local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
+ if (!getenv("ADB_MDNS") || strcmp(getenv("ADB_MDNS"), "0") != 0) {
+ init_mdns_transport_discovery();
+ }
+
+ if (!getenv("ADB_USB") || strcmp(getenv("ADB_USB"), "0") != 0) {
+ usb_init();
+ } else {
+ adb_notify_device_scan_complete();
+ }
+
+ if (!getenv("ADB_EMU") || strcmp(getenv("ADB_EMU"), "0") != 0) {
+ local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
+ }
std::string error;
diff --git a/adb/client/transport_mdns.cpp b/adb/client/transport_mdns.cpp
index 3603f09..283fac5 100644
--- a/adb/client/transport_mdns.cpp
+++ b/adb/client/transport_mdns.cpp
@@ -35,7 +35,7 @@
#include "sysdeps.h"
static DNSServiceRef service_ref;
-static fdevent service_ref_fde;
+static fdevent* service_ref_fde;
// Use adb_DNSServiceRefSockFD() instead of calling DNSServiceRefSockFD()
// directly so that the socket is put through the appropriate compatibility
@@ -68,27 +68,26 @@
}
virtual ~AsyncServiceRef() {
- if (! initialized_) {
+ if (!initialized_) {
return;
}
DNSServiceRefDeallocate(sdRef_);
- fdevent_remove(&fde_);
+ fdevent_destroy(fde_);
}
protected:
DNSServiceRef sdRef_;
void Initialize() {
- fdevent_install(&fde_, adb_DNSServiceRefSockFD(sdRef_),
- pump_service_ref, &sdRef_);
- fdevent_set(&fde_, FDE_READ);
+ fde_ = fdevent_create(adb_DNSServiceRefSockFD(sdRef_), pump_service_ref, &sdRef_);
+ fdevent_set(fde_, FDE_READ);
initialized_ = true;
}
private:
- bool initialized_;
- fdevent fde_;
+ bool initialized_ = false;
+ fdevent* fde_;
};
class ResolvedService : public AsyncServiceRef {
@@ -252,14 +251,12 @@
if (errorCode != kDNSServiceErr_NoError) {
D("Got error %d during mDNS browse.", errorCode);
DNSServiceRefDeallocate(sdRef);
- fdevent_remove(&service_ref_fde);
+ fdevent_destroy(service_ref_fde);
return;
}
- auto discovered = new DiscoveredService(interfaceIndex, serviceName,
- regtype, domain);
-
- if (! discovered->Initialized()) {
+ auto discovered = new DiscoveredService(interfaceIndex, serviceName, regtype, domain);
+ if (!discovered->Initialized()) {
delete discovered;
}
}
@@ -274,9 +271,9 @@
}
fdevent_run_on_main_thread([]() {
- fdevent_install(&service_ref_fde, adb_DNSServiceRefSockFD(service_ref), pump_service_ref,
- &service_ref);
- fdevent_set(&service_ref_fde, FDE_READ);
+ service_ref_fde =
+ fdevent_create(adb_DNSServiceRefSockFD(service_ref), pump_service_ref, &service_ref);
+ fdevent_set(service_ref_fde, FDE_READ);
});
}
diff --git a/adb/client/usb_libusb.cpp b/adb/client/usb_libusb.cpp
index 46c3f58..10b6090 100644
--- a/adb/client/usb_libusb.cpp
+++ b/adb/client/usb_libusb.cpp
@@ -589,7 +589,7 @@
int rc = perform_usb_transfer(h, info, std::move(lock));
LOG(DEBUG) << "usb_write(" << len << ") = " << rc;
- return rc;
+ return info->transfer->actual_length;
}
int usb_read(usb_handle* h, void* d, int len) {
diff --git a/adb/client/usb_linux.cpp b/adb/client/usb_linux.cpp
index 1f376a4..869e858 100644
--- a/adb/client/usb_linux.cpp
+++ b/adb/client/usb_linux.cpp
@@ -128,7 +128,7 @@
if (!bus_dir) return;
dirent* de;
- while ((de = readdir(bus_dir.get())) != 0) {
+ while ((de = readdir(bus_dir.get())) != nullptr) {
if (contains_non_digit(de->d_name)) continue;
std::string bus_name = base + "/" + de->d_name;
@@ -418,11 +418,11 @@
if (h->zero_mask && !(len & h->zero_mask)) {
// If we need 0-markers and our transfer is an even multiple of the packet size,
// then send a zero marker.
- return usb_bulk_write(h, _data, 0);
+ return usb_bulk_write(h, _data, 0) == 0 ? n : -1;
}
D("-- usb_write --");
- return 0;
+ return n;
}
int usb_read(usb_handle *h, void *_data, int len)
diff --git a/adb/client/usb_osx.cpp b/adb/client/usb_osx.cpp
index b15d28a..49baf36 100644
--- a/adb/client/usb_osx.cpp
+++ b/adb/client/usb_osx.cpp
@@ -50,7 +50,7 @@
{
UInt8 bulkIn;
UInt8 bulkOut;
- IOUSBInterfaceInterface190** interface;
+ IOUSBInterfaceInterface550** interface;
unsigned int zero_mask;
size_t max_packet_size;
@@ -106,8 +106,8 @@
}
static void AndroidInterfaceAdded(io_iterator_t iterator);
-static std::unique_ptr<usb_handle> CheckInterface(IOUSBInterfaceInterface190 **iface,
- UInt16 vendor, UInt16 product);
+static std::unique_ptr<usb_handle> CheckInterface(IOUSBInterfaceInterface550** iface, UInt16 vendor,
+ UInt16 product);
static bool FindUSBDevices() {
// Create the matching dictionary to find the Android device's adb interface.
@@ -295,8 +295,8 @@
continue;
}
- std::unique_ptr<usb_handle> handle = CheckInterface((IOUSBInterfaceInterface190**)iface,
- vendor, product);
+ std::unique_ptr<usb_handle> handle =
+ CheckInterface((IOUSBInterfaceInterface550**)iface, vendor, product);
if (handle == nullptr) {
LOG(ERROR) << "Could not find device interface";
(*iface)->Release(iface);
@@ -315,7 +315,7 @@
// Used to clear both the endpoints before starting.
// When adb quits, we might clear the host endpoint but not the device.
// So we make sure both sides are clear before starting up.
-static bool ClearPipeStallBothEnds(IOUSBInterfaceInterface190** interface, UInt8 bulkEp) {
+static bool ClearPipeStallBothEnds(IOUSBInterfaceInterface550** interface, UInt8 bulkEp) {
IOReturn rc = (*interface)->ClearPipeStallBothEnds(interface, bulkEp);
if (rc != kIOReturnSuccess) {
LOG(ERROR) << "Could not clear pipe stall both ends: " << std::hex << rc;
@@ -326,9 +326,8 @@
//* TODO: simplify this further since we only register to get ADB interface
//* subclass+protocol events
-static std::unique_ptr<usb_handle>
-CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 product)
-{
+static std::unique_ptr<usb_handle> CheckInterface(IOUSBInterfaceInterface550** interface,
+ UInt16 vendor, UInt16 product) {
std::unique_ptr<usb_handle> handle;
IOReturn kr;
UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol;
@@ -376,9 +375,14 @@
UInt8 interval;
UInt8 number;
UInt8 direction;
+ UInt8 maxBurst;
+ UInt8 mult;
+ UInt16 bytesPerInterval;
- kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,
- &number, &transferType, &maxPacketSize, &interval);
+ kr = (*interface)
+ ->GetPipePropertiesV2(interface, endpoint, &direction, &number, &transferType,
+ &maxPacketSize, &interval, &maxBurst, &mult,
+ &bytesPerInterval);
if (kr != kIOReturnSuccess) {
LOG(ERROR) << "FindDeviceInterface - could not get pipe properties: "
<< std::hex << kr;
@@ -397,6 +401,13 @@
if (!ClearPipeStallBothEnds(interface, handle->bulkOut)) goto err_get_pipe_props;
}
+ if (maxBurst != 0)
+ // bMaxBurst is the number of additional packets in the burst.
+ maxPacketSize /= (maxBurst + 1);
+
+ // mult is only relevant for isochronous endpoints.
+ CHECK_EQ(0, mult);
+
handle->zero_mask = maxPacketSize - 1;
handle->max_packet_size = maxPacketSize;
}
@@ -486,8 +497,8 @@
}
}
- if (0 == result)
- return 0;
+ if (!result)
+ return len;
LOG(ERROR) << "usb_write failed with status: " << std::hex << result;
return -1;
diff --git a/adb/client/usb_windows.cpp b/adb/client/usb_windows.cpp
index 9751ebf..e928377 100644
--- a/adb/client/usb_windows.cpp
+++ b/adb/client/usb_windows.cpp
@@ -75,7 +75,7 @@
static const GUID usb_class_id = ANDROID_USB_CLASS_ID;
/// List of opened usb handles
-static std::vector<usb_handle*> handle_list;
+static std::vector<usb_handle*>& handle_list = *new std::vector<usb_handle*>();
/// Locker for the list of opened usb handles
static std::mutex& usb_lock = *new std::mutex();
@@ -126,11 +126,11 @@
int usb_close(usb_handle* handle);
int known_device_locked(const wchar_t* dev_name) {
- if (NULL != dev_name) {
+ if (nullptr != dev_name) {
// Iterate through the list looking for the name match.
for (usb_handle* usb : handle_list) {
// In Windows names are not case sensetive!
- if ((NULL != usb->interface_name) && (0 == wcsicmp(usb->interface_name, dev_name))) {
+ if ((nullptr != usb->interface_name) && (0 == wcsicmp(usb->interface_name, dev_name))) {
return 1;
}
}
@@ -142,7 +142,7 @@
int known_device(const wchar_t* dev_name) {
int ret = 0;
- if (NULL != dev_name) {
+ if (nullptr != dev_name) {
std::lock_guard<std::mutex> lock(usb_lock);
ret = known_device_locked(dev_name);
}
@@ -151,7 +151,7 @@
}
int register_new_device(usb_handle* handle) {
- if (NULL == handle) return 0;
+ if (nullptr == handle) return 0;
std::lock_guard<std::mutex> lock(usb_lock);
@@ -209,7 +209,7 @@
// Get the HINSTANCE corresponding to the module that _power_window_proc
// is in (the main module).
- const HINSTANCE instance = GetModuleHandleW(NULL);
+ const HINSTANCE instance = GetModuleHandleW(nullptr);
if (!instance) {
// This is such a common API call that this should never fail.
fatal("GetModuleHandleW failed: %s",
@@ -228,14 +228,14 @@
}
if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName,
- L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0, NULL, NULL,
- instance, NULL)) {
+ L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0, nullptr, nullptr,
+ instance, nullptr)) {
fatal("CreateWindowExW failed: %s",
android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
MSG msg;
- while (GetMessageW(&msg, NULL, 0, 0)) {
+ while (GetMessageW(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
@@ -259,14 +259,14 @@
// Allocate our handle
usb_handle* ret = (usb_handle*)calloc(1, sizeof(usb_handle));
- if (NULL == ret) {
+ if (nullptr == ret) {
D("Could not allocate %u bytes for usb_handle: %s", sizeof(usb_handle), strerror(errno));
goto fail;
}
// Create interface.
ret->adb_interface = AdbCreateInterfaceByName(interface_name);
- if (NULL == ret->adb_interface) {
+ if (nullptr == ret->adb_interface) {
D("AdbCreateInterfaceByName failed: %s",
android::base::SystemErrorCodeToString(GetLastError()).c_str());
goto fail;
@@ -275,7 +275,7 @@
// Open read pipe (endpoint)
ret->adb_read_pipe = AdbOpenDefaultBulkReadEndpoint(
ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite);
- if (NULL == ret->adb_read_pipe) {
+ if (nullptr == ret->adb_read_pipe) {
D("AdbOpenDefaultBulkReadEndpoint failed: %s",
android::base::SystemErrorCodeToString(GetLastError()).c_str());
goto fail;
@@ -284,7 +284,7 @@
// Open write pipe (endpoint)
ret->adb_write_pipe = AdbOpenDefaultBulkWriteEndpoint(
ret->adb_interface, AdbOpenAccessTypeReadWrite, AdbOpenSharingModeReadWrite);
- if (NULL == ret->adb_write_pipe) {
+ if (nullptr == ret->adb_write_pipe) {
D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
android::base::SystemErrorCodeToString(GetLastError()).c_str());
goto fail;
@@ -292,7 +292,7 @@
// Save interface name
// First get expected name length
- AdbGetInterfaceName(ret->adb_interface, NULL, &name_len, false);
+ AdbGetInterfaceName(ret->adb_interface, nullptr, &name_len, false);
if (0 == name_len) {
D("AdbGetInterfaceName returned name length of zero: %s",
android::base::SystemErrorCodeToString(GetLastError()).c_str());
@@ -300,7 +300,7 @@
}
ret->interface_name = (wchar_t*)malloc(name_len * sizeof(ret->interface_name[0]));
- if (NULL == ret->interface_name) {
+ if (nullptr == ret->interface_name) {
D("Could not allocate %lu characters for interface_name: %s", name_len, strerror(errno));
goto fail;
}
@@ -316,12 +316,12 @@
return ret;
fail:
- if (NULL != ret) {
+ if (nullptr != ret) {
usb_cleanup_handle(ret);
free(ret);
}
- return NULL;
+ return nullptr;
}
int usb_write(usb_handle* handle, const void* data, int len) {
@@ -330,7 +330,7 @@
int err = 0;
D("usb_write %d", len);
- if (NULL == handle) {
+ if (nullptr == handle) {
D("usb_write was passed NULL handle");
err = EINVAL;
goto fail;
@@ -365,12 +365,12 @@
}
}
- return 0;
+ return written;
fail:
// Any failure should cause us to kick the device instead of leaving it a
// zombie state with potential to hang.
- if (NULL != handle) {
+ if (nullptr != handle) {
D("Kicking device due to error in usb_write");
usb_kick(handle);
}
@@ -387,7 +387,7 @@
int orig_len = len;
D("usb_read %d", len);
- if (NULL == handle) {
+ if (nullptr == handle) {
D("usb_read was passed NULL handle");
err = EINVAL;
goto fail;
@@ -411,7 +411,7 @@
fail:
// Any failure should cause us to kick the device instead of leaving it a
// zombie state with potential to hang.
- if (NULL != handle) {
+ if (nullptr != handle) {
D("Kicking device due to error in usb_read");
usb_kick(handle);
}
@@ -431,19 +431,19 @@
void usb_cleanup_handle(usb_handle* handle) {
D("usb_cleanup_handle");
- if (NULL != handle) {
- if (NULL != handle->interface_name) free(handle->interface_name);
+ if (nullptr != handle) {
+ if (nullptr != handle->interface_name) free(handle->interface_name);
// AdbCloseHandle(pipe) will break any threads out of pending IO calls and
// wait until the pipe no longer uses the interface. Then we can
// AdbCloseHandle() the interface.
- if (NULL != handle->adb_write_pipe) _adb_close_handle(handle->adb_write_pipe);
- if (NULL != handle->adb_read_pipe) _adb_close_handle(handle->adb_read_pipe);
- if (NULL != handle->adb_interface) _adb_close_handle(handle->adb_interface);
+ if (nullptr != handle->adb_write_pipe) _adb_close_handle(handle->adb_write_pipe);
+ if (nullptr != handle->adb_read_pipe) _adb_close_handle(handle->adb_read_pipe);
+ if (nullptr != handle->adb_interface) _adb_close_handle(handle->adb_interface);
- handle->interface_name = NULL;
- handle->adb_write_pipe = NULL;
- handle->adb_read_pipe = NULL;
- handle->adb_interface = NULL;
+ handle->interface_name = nullptr;
+ handle->adb_write_pipe = nullptr;
+ handle->adb_read_pipe = nullptr;
+ handle->adb_interface = nullptr;
}
}
@@ -455,7 +455,7 @@
void usb_kick(usb_handle* handle) {
D("usb_kick");
- if (NULL != handle) {
+ if (nullptr != handle) {
std::lock_guard<std::mutex> lock(usb_lock);
usb_kick_locked(handle);
} else {
@@ -466,7 +466,7 @@
int usb_close(usb_handle* handle) {
D("usb_close");
- if (NULL != handle) {
+ if (nullptr != handle) {
// Remove handle from the list
{
std::lock_guard<std::mutex> lock(usb_lock);
@@ -487,7 +487,7 @@
}
int recognized_device(usb_handle* handle) {
- if (NULL == handle) return 0;
+ if (nullptr == handle) return 0;
// Check vendor and product id first
USB_DEVICE_DESCRIPTOR device_desc;
@@ -532,7 +532,7 @@
}
void find_devices() {
- usb_handle* handle = NULL;
+ usb_handle* handle = nullptr;
char entry_buffer[2048];
AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]);
unsigned long entry_buffer_size = sizeof(entry_buffer);
@@ -540,7 +540,7 @@
// Enumerate all present and active interfaces.
ADBAPIHANDLE enum_handle = AdbEnumInterfaces(usb_class_id, true, true, true);
- if (NULL == enum_handle) {
+ if (nullptr == enum_handle) {
D("AdbEnumInterfaces failed: %s",
android::base::SystemErrorCodeToString(GetLastError()).c_str());
return;
@@ -551,7 +551,7 @@
if (!known_device(next_interface->device_name)) {
// This seems to be a new device. Open it!
handle = do_usb_open(next_interface->device_name);
- if (NULL != handle) {
+ if (nullptr != handle) {
// Lets see if this interface (device) belongs to us
if (recognized_device(handle)) {
D("adding a new device %ls", next_interface->device_name);
@@ -569,7 +569,7 @@
true)) {
// Lets make sure that we don't duplicate this device
if (register_new_device(handle)) {
- register_usb_transport(handle, serial_number, NULL, 1);
+ register_usb_transport(handle, serial_number, nullptr, 1);
} else {
D("register_new_device failed for %ls", next_interface->device_name);
usb_cleanup_handle(handle);
diff --git a/adb/daemon/auth.cpp b/adb/daemon/auth.cpp
index 3fd2b31..180df8f 100644
--- a/adb/daemon/auth.cpp
+++ b/adb/daemon/auth.cpp
@@ -35,8 +35,8 @@
#include <openssl/rsa.h>
#include <openssl/sha.h>
-static fdevent listener_fde;
-static fdevent framework_fde;
+static fdevent* listener_fde = nullptr;
+static fdevent* framework_fde = nullptr;
static int framework_fd = -1;
static void usb_disconnected(void* unused, atransport* t);
@@ -100,14 +100,16 @@
static void usb_disconnected(void* unused, atransport* t) {
LOG(INFO) << "USB disconnect";
- usb_transport = NULL;
+ usb_transport = nullptr;
needs_retry = false;
}
static void framework_disconnected() {
LOG(INFO) << "Framework disconnect";
- fdevent_remove(&framework_fde);
- framework_fd = -1;
+ if (framework_fde) {
+ fdevent_destroy(framework_fde);
+ framework_fd = -1;
+ }
}
static void adbd_auth_event(int fd, unsigned events, void*) {
@@ -168,8 +170,8 @@
}
framework_fd = s;
- fdevent_install(&framework_fde, framework_fd, adbd_auth_event, nullptr);
- fdevent_add(&framework_fde, FDE_READ);
+ framework_fde = fdevent_create(framework_fd, adbd_auth_event, nullptr);
+ fdevent_add(framework_fde, FDE_READ);
if (needs_retry) {
needs_retry = false;
@@ -198,8 +200,8 @@
return;
}
- fdevent_install(&listener_fde, fd, adbd_auth_listener, NULL);
- fdevent_add(&listener_fde, FDE_READ);
+ listener_fde = fdevent_create(fd, adbd_auth_listener, nullptr);
+ fdevent_add(listener_fde, FDE_READ);
}
void send_auth_request(atransport* t) {
diff --git a/adb/daemon/file_sync_service.cpp b/adb/daemon/file_sync_service.cpp
index 9a87931..8c39a20 100644
--- a/adb/daemon/file_sync_service.cpp
+++ b/adb/daemon/file_sync_service.cpp
@@ -16,8 +16,9 @@
#define TRACE_TAG SYNC
+#include "daemon/file_sync_service.h"
+
#include "sysdeps.h"
-#include "file_sync_service.h"
#include <dirent.h>
#include <errno.h>
@@ -42,16 +43,15 @@
#include "adb_io.h"
#include "adb_trace.h"
#include "adb_utils.h"
+#include "file_sync_protocol.h"
#include "security_log_tags.h"
#include "sysdeps/errno.h"
using android::base::StringPrintf;
static bool should_use_fs_config(const std::string& path) {
- // TODO: use fs_config to configure permissions on /data.
- return android::base::StartsWith(path, "/system/") ||
- android::base::StartsWith(path, "/vendor/") ||
- android::base::StartsWith(path, "/oem/");
+ // TODO: use fs_config to configure permissions on /data too.
+ return !android::base::StartsWith(path, "/data/");
}
static bool update_capabilities(const char* path, uint64_t capabilities) {
@@ -71,17 +71,23 @@
}
static bool secure_mkdirs(const std::string& path) {
- uid_t uid = -1;
- gid_t gid = -1;
- unsigned int mode = 0775;
- uint64_t capabilities = 0;
-
if (path[0] != '/') return false;
std::vector<std::string> path_components = android::base::Split(path, "/");
std::string partial_path;
for (const auto& path_component : path_components) {
- if (partial_path.back() != OS_PATH_SEPARATOR) partial_path += OS_PATH_SEPARATOR;
+ uid_t uid = -1;
+ gid_t gid = -1;
+ unsigned int mode = 0775;
+ uint64_t capabilities = 0;
+
+ if (path_component.empty()) {
+ continue;
+ }
+
+ if (partial_path.empty() || partial_path.back() != OS_PATH_SEPARATOR) {
+ partial_path += OS_PATH_SEPARATOR;
+ }
partial_path += path_component;
if (should_use_fs_config(partial_path)) {
@@ -521,12 +527,11 @@
return true;
}
-void file_sync_service(int fd, void*) {
+void file_sync_service(unique_fd fd) {
std::vector<char> buffer(SYNC_DATA_MAX);
- while (handle_sync_command(fd, buffer)) {
+ while (handle_sync_command(fd.get(), buffer)) {
}
D("sync: done");
- adb_close(fd);
}
diff --git a/adb/remount_service.h b/adb/daemon/file_sync_service.h
similarity index 71%
copy from adb/remount_service.h
copy to adb/daemon/file_sync_service.h
index 7bda1be..f300e7b 100644
--- a/adb/remount_service.h
+++ b/adb/daemon/file_sync_service.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,12 +14,8 @@
* limitations under the License.
*/
-#ifndef _REMOUNT_SERVICE_H_
-#define _REMOUNT_SERVICE_H_
+#pragma once
-#include <string>
+#include "adb_unique_fd.h"
-bool make_block_device_writable(const std::string&);
-void remount_service(int, void*);
-
-#endif
+void file_sync_service(unique_fd fd);
diff --git a/adb/daemon/framebuffer_service.cpp b/adb/daemon/framebuffer_service.cpp
index 6c3a225..8d28c49 100644
--- a/adb/daemon/framebuffer_service.cpp
+++ b/adb/daemon/framebuffer_service.cpp
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+#include "framebuffer_service.h"
+
#include <errno.h>
#include <fcntl.h>
#include <linux/fb.h>
@@ -55,8 +57,7 @@
unsigned int alpha_length;
} __attribute__((packed));
-void framebuffer_service(int fd, void *cookie)
-{
+void framebuffer_service(unique_fd fd) {
struct fbinfo fbinfo;
unsigned int i, bsize;
char buf[640];
@@ -65,7 +66,7 @@
int fds[2];
pid_t pid;
- if (pipe2(fds, O_CLOEXEC) < 0) goto pipefail;
+ if (pipe2(fds, O_CLOEXEC) < 0) return;
pid = fork();
if (pid < 0) goto done;
@@ -75,7 +76,7 @@
adb_close(fds[0]);
adb_close(fds[1]);
const char* command = "screencap";
- const char *args[2] = {command, NULL};
+ const char *args[2] = {command, nullptr};
execvp(command, (char**)args);
exit(1);
}
@@ -168,7 +169,7 @@
}
/* write header */
- if(!WriteFdExactly(fd, &fbinfo, sizeof(fbinfo))) goto done;
+ if (!WriteFdExactly(fd.get(), &fbinfo, sizeof(fbinfo))) goto done;
/* write data */
for(i = 0; i < fbinfo.size; i += bsize) {
@@ -176,13 +177,11 @@
if (i + bsize > fbinfo.size)
bsize = fbinfo.size - i;
if(!ReadFdExactly(fd_screencap, buf, bsize)) goto done;
- if(!WriteFdExactly(fd, buf, bsize)) goto done;
+ if (!WriteFdExactly(fd.get(), buf, bsize)) goto done;
}
done:
adb_close(fds[0]);
- TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
-pipefail:
- adb_close(fd);
+ TEMP_FAILURE_RETRY(waitpid(pid, nullptr, 0));
}
diff --git a/adb/remount_service.h b/adb/daemon/framebuffer_service.h
similarity index 71%
copy from adb/remount_service.h
copy to adb/daemon/framebuffer_service.h
index 7bda1be..264da59 100644
--- a/adb/remount_service.h
+++ b/adb/daemon/framebuffer_service.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,12 +14,8 @@
* limitations under the License.
*/
-#ifndef _REMOUNT_SERVICE_H_
-#define _REMOUNT_SERVICE_H_
+#pragma once
-#include <string>
+#include "adb_unique_fd.h"
-bool make_block_device_writable(const std::string&);
-void remount_service(int, void*);
-
-#endif
+void framebuffer_service(unique_fd fd);
diff --git a/adb/daemon/usb.h b/adb/daemon/include/adbd/usb.h
similarity index 92%
rename from adb/daemon/usb.h
rename to adb/daemon/include/adbd/usb.h
index db1a6d6..7905d9d 100644
--- a/adb/daemon/usb.h
+++ b/adb/daemon/include/adbd/usb.h
@@ -19,6 +19,7 @@
#include <atomic>
#include <condition_variable>
#include <mutex>
+#include <vector>
#include <asyncio/AsyncIO.h>
@@ -55,6 +56,8 @@
struct aio_block read_aiob;
struct aio_block write_aiob;
- int max_rw;
+ bool reads_zero_packets;
+ size_t io_size;
};
+usb_handle *create_usb_handle(unsigned num_bufs, unsigned io_size);
diff --git a/adb/daemon/jdwp_service.cpp b/adb/daemon/jdwp_service.cpp
index 9761558..b40faee 100644
--- a/adb/daemon/jdwp_service.cpp
+++ b/adb/daemon/jdwp_service.cpp
@@ -139,8 +139,6 @@
fatal("could not create fdevent for new JDWP process");
}
- this->fde->state |= FDE_DONT_CLOSE;
-
/* start by waiting for the PID */
fdevent_add(this->fde, FDE_READ);
}
@@ -148,7 +146,6 @@
~JdwpProcess() {
if (this->socket >= 0) {
adb_shutdown(this->socket);
- adb_close(this->socket);
this->socket = -1;
}
@@ -267,7 +264,7 @@
iov.iov_base = &dummy;
iov.iov_len = 1;
- msg.msg_name = NULL;
+ msg.msg_name = nullptr;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
@@ -292,8 +289,6 @@
goto CloseProcess;
}
- adb_close(fd);
-
D("sent file descriptor %d to JDWP process %d", fd, proc->pid);
proc->out_fds.pop_back();
@@ -316,7 +311,7 @@
jdwp_process_list_updated();
}
-int create_jdwp_connection_fd(int pid) {
+unique_fd create_jdwp_connection_fd(int pid) {
D("looking for pid %d in JDWP process list", pid);
for (auto& proc : _jdwp_list) {
@@ -325,7 +320,7 @@
if (adb_socketpair(fds) < 0) {
D("%s: socket pair creation failed: %s", __FUNCTION__, strerror(errno));
- return -1;
+ return unique_fd{};
}
D("socketpair: (%d,%d)", fds[0], fds[1]);
@@ -334,11 +329,11 @@
fdevent_add(proc->fde, FDE_WRITE);
}
- return fds[0];
+ return unique_fd{fds[0]};
}
}
D("search failed !!");
- return -1;
+ return unique_fd{};
}
/** VM DEBUG CONTROL SOCKET
@@ -398,7 +393,7 @@
control->listen_socket = s;
control->fde = fdevent_create(s, jdwp_control_event, control);
- if (control->fde == NULL) {
+ if (control->fde == nullptr) {
D("could not create fdevent for jdwp control socket");
adb_close(s);
return -1;
@@ -459,7 +454,7 @@
delete s;
}
-static int jdwp_socket_enqueue(asocket* s, std::string) {
+static int jdwp_socket_enqueue(asocket* s, apacket::payload_type) {
/* you can't write to this asocket */
D("LS(%d): JDWP socket received data?", s->id);
s->peer->close(s->peer);
@@ -474,7 +469,7 @@
* on the second one, close the connection
*/
if (!jdwp->pass) {
- std::string data;
+ apacket::payload_type data;
data.resize(s->get_max_payload());
size_t len = jdwp_process_list(&data[0], data.size());
data.resize(len);
@@ -521,7 +516,8 @@
for (auto& t : _jdwp_trackers) {
if (t->peer) {
// The tracker might not have been connected yet.
- t->peer->enqueue(t->peer, data);
+ apacket::payload_type payload(data.begin(), data.end());
+ t->peer->enqueue(t->peer, std::move(payload));
}
}
}
@@ -547,7 +543,7 @@
JdwpTracker* t = (JdwpTracker*)s;
if (t->need_initial) {
- std::string data;
+ apacket::payload_type data;
data.resize(s->get_max_payload());
data.resize(jdwp_process_list_msg(&data[0], data.size()));
t->need_initial = false;
@@ -555,7 +551,7 @@
}
}
-static int jdwp_tracker_enqueue(asocket* s, std::string) {
+static int jdwp_tracker_enqueue(asocket* s, apacket::payload_type) {
/* you can't write to this socket */
D("LS(%d): JDWP tracker received data?", s->id);
s->peer->close(s->peer);
diff --git a/adb/daemon/main.cpp b/adb/daemon/main.cpp
index 9b25a07..f6f1acc 100644
--- a/adb/daemon/main.cpp
+++ b/adb/daemon/main.cpp
@@ -18,11 +18,14 @@
#include "sysdeps.h"
+#include <android/fdsan.h>
#include <errno.h>
+#include <getopt.h>
+#include <malloc.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
-#include <getopt.h>
+#include <sys/capability.h>
#include <sys/prctl.h>
#include <memory>
@@ -36,7 +39,6 @@
#include <scoped_minijail.h>
#include <private/android_filesystem_config.h>
-#include "debuggerd/handler.h"
#include "selinux/android.h"
#include "adb.h"
@@ -49,13 +51,13 @@
static const char* root_seclabel = nullptr;
-static void drop_capabilities_bounding_set_if_needed(struct minijail *j) {
+static bool should_drop_capabilities_bounding_set() {
#if defined(ALLOW_ADBD_ROOT)
if (__android_log_is_debuggable()) {
- return;
+ return false;
}
#endif
- minijail_capbset_drop(j, CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
+ return true;
}
static bool should_drop_privileges() {
@@ -116,13 +118,37 @@
// Don't listen on a port (default 5037) if running in secure mode.
// Don't run as root if running in secure mode.
if (should_drop_privileges()) {
- drop_capabilities_bounding_set_if_needed(jail.get());
+ const bool should_drop_caps = should_drop_capabilities_bounding_set();
+
+ if (should_drop_caps) {
+ minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
+ }
minijail_change_gid(jail.get(), AID_SHELL);
minijail_change_uid(jail.get(), AID_SHELL);
// minijail_enter() will abort if any priv-dropping step fails.
minijail_enter(jail.get());
+ // Whenever ambient capabilities are being used, minijail cannot
+ // simultaneously drop the bounding capability set to just
+ // CAP_SETUID|CAP_SETGID while clearing the inheritable, effective,
+ // and permitted sets. So we need to do that in two steps.
+ using ScopedCaps =
+ std::unique_ptr<std::remove_pointer<cap_t>::type, std::function<void(cap_t)>>;
+ ScopedCaps caps(cap_get_proc(), &cap_free);
+ if (cap_clear_flag(caps.get(), CAP_INHERITABLE) == -1) {
+ PLOG(FATAL) << "cap_clear_flag(INHERITABLE) failed";
+ }
+ if (cap_clear_flag(caps.get(), CAP_EFFECTIVE) == -1) {
+ PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
+ }
+ if (cap_clear_flag(caps.get(), CAP_PERMITTED) == -1) {
+ PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed";
+ }
+ if (cap_set_proc(caps.get()) != 0) {
+ PLOG(FATAL) << "cap_set_proc() failed";
+ }
+
D("Local port disabled");
} else {
// minijail_enter() will abort if any priv-dropping step fails.
@@ -152,6 +178,11 @@
signal(SIGPIPE, SIG_IGN);
+ auto fdsan_level = android_fdsan_get_error_level();
+ if (fdsan_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) {
+ android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
+ }
+
init_transport_registration();
// We need to call this even if auth isn't enabled because the file
@@ -159,7 +190,8 @@
adbd_cloexec_auth_socket();
#if defined(ALLOW_ADBD_NO_AUTH)
- auth_required = android::base::GetBoolProperty("ro.adb.secure", true);
+ // If ro.adb.secure is unset, default to no authentication required.
+ auth_required = android::base::GetBoolProperty("ro.adb.secure", false);
#endif
adbd_auth_init();
@@ -212,6 +244,9 @@
}
int main(int argc, char** argv) {
+ // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
+ mallopt(M_DECAY_TIME, 1);
+
while (true) {
static struct option opts[] = {
{"root_seclabel", required_argument, nullptr, 's'},
@@ -244,7 +279,6 @@
close_stdin();
- debuggerd_init(nullptr);
adb_trace_init(argv);
D("Handling main()");
diff --git a/adb/daemon/remount_service.cpp b/adb/daemon/remount_service.cpp
index a4c7a5a..f7017dd 100644
--- a/adb/daemon/remount_service.cpp
+++ b/adb/daemon/remount_service.cpp
@@ -21,35 +21,48 @@
#include <errno.h>
#include <fcntl.h>
#include <mntent.h>
+#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
+#include <sys/statvfs.h>
+#include <sys/vfs.h>
#include <unistd.h>
+#include <memory>
+#include <set>
#include <string>
+#include <vector>
#include <android-base/properties.h>
+#include <bootloader_message/bootloader_message.h>
+#include <cutils/android_reboot.h>
+#include <ext4_utils/ext4_utils.h>
+#include <fs_mgr.h>
+#include <fs_mgr_overlayfs.h>
#include "adb.h"
#include "adb_io.h"
+#include "adb_unique_fd.h"
#include "adb_utils.h"
-#include "fs_mgr.h"
+#include "set_verity_enable_state_service.h"
-// Returns the device used to mount a directory in /proc/mounts.
+// Returns the last device used to mount a directory in /proc/mounts.
+// This will find overlayfs entry where upperdir=lowerdir, to make sure
+// remount is associated with the correct directory.
static std::string find_proc_mount(const char* dir) {
std::unique_ptr<FILE, int(*)(FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
- if (!fp) {
- return "";
- }
+ std::string mnt_fsname;
+ if (!fp) return mnt_fsname;
mntent* e;
while ((e = getmntent(fp.get())) != nullptr) {
if (strcmp(dir, e->mnt_dir) == 0) {
- return e->mnt_fsname;
+ mnt_fsname = e->mnt_fsname;
}
}
- return "";
+ return mnt_fsname;
}
// Returns the device used to mount a directory in the fstab.
@@ -71,6 +84,7 @@
}
bool make_block_device_writable(const std::string& dev) {
+ if ((dev == "overlay") || (dev == "overlayfs")) return true;
int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
if (fd == -1) {
return false;
@@ -82,6 +96,73 @@
return result;
}
+static bool fs_has_shared_blocks(const std::string& mount_point, const std::string& device) {
+ std::string path = mount_point + "/lost+found";
+ struct statfs fs;
+ if (statfs(path.c_str(), &fs) == -1 || fs.f_type != EXT4_SUPER_MAGIC) {
+ return false;
+ }
+ unique_fd fd(unix_open(device.c_str(), O_RDONLY));
+ if (fd < 0) {
+ return false;
+ }
+ struct ext4_super_block sb;
+ if (lseek64(fd, 1024, SEEK_SET) < 0 || unix_read(fd, &sb, sizeof(sb)) < 0) {
+ return false;
+ }
+ struct fs_info info;
+ if (ext4_parse_sb(&sb, &info) < 0) {
+ return false;
+ }
+ return (info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SHARED_BLOCKS) != 0;
+}
+
+static bool can_unshare_blocks(int fd, const char* dev) {
+ const char* E2FSCK_BIN = "/system/bin/e2fsck";
+ if (access(E2FSCK_BIN, X_OK)) {
+ WriteFdFmt(fd, "e2fsck is not available, cannot undo deduplication on %s\n", dev);
+ return false;
+ }
+
+ pid_t child;
+ char* env[] = {nullptr};
+ const char* argv[] = {E2FSCK_BIN, "-n", "-E", "unshare_blocks", dev, nullptr};
+ if (posix_spawn(&child, E2FSCK_BIN, nullptr, nullptr, const_cast<char**>(argv), env)) {
+ WriteFdFmt(fd, "failed to e2fsck to check deduplication: %s\n", strerror(errno));
+ return false;
+ }
+ int status = 0;
+ int ret = TEMP_FAILURE_RETRY(waitpid(child, &status, 0));
+ if (ret < 0) {
+ WriteFdFmt(fd, "failed to get e2fsck status: %s\n", strerror(errno));
+ return false;
+ }
+ if (!WIFEXITED(status)) {
+ WriteFdFmt(fd, "e2fsck exited abnormally with status %d\n", status);
+ return false;
+ }
+ int rc = WEXITSTATUS(status);
+ if (rc != 0) {
+ WriteFdFmt(fd,
+ "%s is deduplicated, and an e2fsck check failed. It might not "
+ "have enough free-space to be remounted as writable.\n",
+ dev);
+ return false;
+ }
+ return true;
+}
+
+static unsigned long get_mount_flags(int fd, const char* dir) {
+ struct statvfs st_vfs;
+ if (statvfs(dir, &st_vfs) == -1) {
+ // Even though we could not get the original mount flags, assume that
+ // the mount was originally read-only.
+ WriteFdFmt(fd, "statvfs of the %s mount failed: %s.\n", dir, strerror(errno));
+ return MS_RDONLY;
+ }
+ return st_vfs.f_flag;
+}
+
static bool remount_partition(int fd, const char* dir) {
if (!directory_exists(dir)) {
return true;
@@ -100,7 +181,12 @@
dir, dev.c_str(), strerror(errno));
return false;
}
- if (mount(dev.c_str(), dir, "none", MS_REMOUNT | MS_BIND, nullptr) == -1) {
+
+ unsigned long remount_flags = get_mount_flags(fd, dir);
+ remount_flags &= ~MS_RDONLY;
+ remount_flags |= MS_REMOUNT;
+
+ if (mount(dev.c_str(), dir, "none", remount_flags | MS_BIND, nullptr) == -1) {
// This is useful for cases where the superblock is already marked as
// read-write, but the mount itself is read-only, such as containers
// where the remount with just MS_REMOUNT is forbidden by the kernel.
@@ -114,41 +200,123 @@
return true;
}
-void remount_service(int fd, void* cookie) {
+static void reboot_for_remount(int fd, bool need_fsck) {
+ std::string reboot_cmd = "reboot";
+ if (need_fsck) {
+ const std::vector<std::string> options = {"--fsck_unshare_blocks"};
+ std::string err;
+ if (!write_bootloader_message(options, &err)) {
+ WriteFdFmt(fd, "Failed to set bootloader message: %s\n", err.c_str());
+ return;
+ }
+
+ WriteFdExactly(fd,
+ "The device will now reboot to recovery and attempt "
+ "un-deduplication.\n");
+ reboot_cmd = "reboot,recovery";
+ }
+
+ sync();
+ android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd.c_str());
+}
+
+void remount_service(unique_fd fd, const std::string& cmd) {
+ bool user_requested_reboot = cmd == "-R";
+
if (getuid() != 0) {
- WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
- adb_close(fd);
+ WriteFdExactly(fd.get(), "Not running as root. Try \"adb root\" first.\n");
return;
}
bool system_verified = !(android::base::GetProperty("partition.system.verified", "").empty());
bool vendor_verified = !(android::base::GetProperty("partition.vendor.verified", "").empty());
- if (system_verified || vendor_verified) {
+ std::vector<std::string> partitions = {"/odm", "/oem", "/product", "/vendor"};
+ if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
+ partitions.push_back("/");
+ } else {
+ partitions.push_back("/system");
+ }
+
+ bool verity_enabled = (system_verified || vendor_verified);
+
+ // If we can use overlayfs, lets get it in place first
+ // before we struggle with determining deduplication operations.
+ if (!verity_enabled && fs_mgr_overlayfs_setup() && fs_mgr_overlayfs_mount_all()) {
+ WriteFdExactly(fd.get(), "overlayfs mounted\n");
+ }
+
+ // Find partitions that are deduplicated, and can be un-deduplicated.
+ std::set<std::string> dedup;
+ for (const auto& partition : partitions) {
+ std::string dev = find_mount(partition.c_str(), partition == "/");
+ if (dev.empty() || !fs_has_shared_blocks(partition, dev)) {
+ continue;
+ }
+ if (can_unshare_blocks(fd.get(), dev.c_str())) {
+ dedup.emplace(partition);
+ }
+ }
+
+ // Reboot now if the user requested it (and an operation needs a reboot).
+ if (user_requested_reboot) {
+ if (!dedup.empty() || verity_enabled) {
+ if (verity_enabled) {
+ set_verity_enabled_state_service(unique_fd(dup(fd.get())), false);
+ }
+ reboot_for_remount(fd.get(), !dedup.empty());
+ return;
+ }
+ WriteFdExactly(fd.get(), "No reboot needed, skipping -R.\n");
+ }
+
+ // If we need to disable-verity, but we also need to perform a recovery
+ // fsck for deduplicated partitions, hold off on warning about verity. We
+ // can handle both verity and the recovery fsck in the same reboot cycle.
+ if (verity_enabled && dedup.empty()) {
// Allow remount but warn of likely bad effects
bool both = system_verified && vendor_verified;
- WriteFdFmt(fd,
- "dm_verity is enabled on the %s%s%s partition%s.\n",
- system_verified ? "system" : "",
- both ? " and " : "",
- vendor_verified ? "vendor" : "",
- both ? "s" : "");
- WriteFdExactly(fd,
+ WriteFdFmt(fd.get(), "dm_verity is enabled on the %s%s%s partition%s.\n",
+ system_verified ? "system" : "", both ? " and " : "",
+ vendor_verified ? "vendor" : "", both ? "s" : "");
+ WriteFdExactly(fd.get(),
"Use \"adb disable-verity\" to disable verity.\n"
"If you do not, remount may succeed, however, you will still "
"not be able to write to these volumes.\n");
+ WriteFdExactly(fd.get(),
+ "Alternately, use \"adb remount -R\" to disable verity "
+ "and automatically reboot.\n");
}
bool success = true;
- if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
- success &= remount_partition(fd, "/");
- } else {
- success &= remount_partition(fd, "/system");
+ for (const auto& partition : partitions) {
+ // Don't try to remount partitions that need an fsck in recovery.
+ if (dedup.count(partition)) {
+ continue;
+ }
+ success &= remount_partition(fd.get(), partition.c_str());
}
- success &= remount_partition(fd, "/vendor");
- success &= remount_partition(fd, "/oem");
- WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n");
+ if (!dedup.empty()) {
+ WriteFdExactly(fd.get(),
+ "The following partitions are deduplicated and cannot "
+ "yet be remounted:\n");
+ for (const std::string& name : dedup) {
+ WriteFdFmt(fd.get(), " %s\n", name.c_str());
+ }
- adb_close(fd);
+ WriteFdExactly(fd.get(),
+ "To reboot and un-deduplicate the listed partitions, "
+ "please retry with adb remount -R.\n");
+ if (system_verified || vendor_verified) {
+ WriteFdExactly(fd.get(), "Note: verity will be automatically disabled after reboot.\n");
+ }
+ return;
+ }
+
+ if (!success) {
+ WriteFdExactly(fd.get(), "remount failed\n");
+ } else {
+ WriteFdExactly(fd.get(), "remount succeeded\n");
+ }
}
diff --git a/adb/remount_service.h b/adb/daemon/remount_service.h
similarity index 87%
rename from adb/remount_service.h
rename to adb/daemon/remount_service.h
index 7bda1be..e4e2550 100644
--- a/adb/remount_service.h
+++ b/adb/daemon/remount_service.h
@@ -14,12 +14,11 @@
* limitations under the License.
*/
-#ifndef _REMOUNT_SERVICE_H_
-#define _REMOUNT_SERVICE_H_
+#pragma once
#include <string>
-bool make_block_device_writable(const std::string&);
-void remount_service(int, void*);
+#include "adb_unique_fd.h"
-#endif
+bool make_block_device_writable(const std::string&);
+void remount_service(unique_fd, const std::string&);
diff --git a/adb/daemon/services.cpp b/adb/daemon/services.cpp
new file mode 100644
index 0000000..8417690
--- /dev/null
+++ b/adb/daemon/services.cpp
@@ -0,0 +1,257 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#define TRACE_TAG SERVICES
+
+#include "sysdeps.h"
+
+#include <errno.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+#include <thread>
+
+#include <android-base/file.h>
+#include <android-base/parsenetaddress.h>
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
+#include <bootloader_message/bootloader_message.h>
+#include <cutils/android_reboot.h>
+#include <cutils/sockets.h>
+#include <log/log_properties.h>
+
+#include "adb.h"
+#include "adb_io.h"
+#include "adb_unique_fd.h"
+#include "adb_utils.h"
+#include "services.h"
+#include "socket_spec.h"
+#include "sysdeps.h"
+#include "transport.h"
+
+#include "daemon/file_sync_service.h"
+#include "daemon/framebuffer_service.h"
+#include "daemon/remount_service.h"
+#include "daemon/set_verity_enable_state_service.h"
+#include "daemon/shell_service.h"
+
+void restart_root_service(unique_fd fd) {
+ if (getuid() == 0) {
+ WriteFdExactly(fd.get(), "adbd is already running as root\n");
+ return;
+ }
+ if (!__android_log_is_debuggable()) {
+ WriteFdExactly(fd.get(), "adbd cannot run as root in production builds\n");
+ return;
+ }
+
+ android::base::SetProperty("service.adb.root", "1");
+ WriteFdExactly(fd.get(), "restarting adbd as root\n");
+}
+
+void restart_unroot_service(unique_fd fd) {
+ if (getuid() != 0) {
+ WriteFdExactly(fd.get(), "adbd not running as root\n");
+ return;
+ }
+ android::base::SetProperty("service.adb.root", "0");
+ WriteFdExactly(fd.get(), "restarting adbd as non root\n");
+}
+
+void restart_tcp_service(unique_fd fd, int port) {
+ if (port <= 0) {
+ WriteFdFmt(fd.get(), "invalid port %d\n", port);
+ return;
+ }
+
+ android::base::SetProperty("service.adb.tcp.port", android::base::StringPrintf("%d", port));
+ WriteFdFmt(fd.get(), "restarting in TCP mode port: %d\n", port);
+}
+
+void restart_usb_service(unique_fd fd) {
+ android::base::SetProperty("service.adb.tcp.port", "0");
+ WriteFdExactly(fd.get(), "restarting in USB mode\n");
+}
+
+void reboot_service(unique_fd fd, const std::string& arg) {
+ std::string reboot_arg = arg;
+ sync();
+
+ if (reboot_arg.empty()) reboot_arg = "adb";
+ std::string reboot_string = android::base::StringPrintf("reboot,%s", reboot_arg.c_str());
+
+ if (reboot_arg == "fastboot" && access("/dev/socket/recovery", F_OK) == 0) {
+ LOG(INFO) << "Recovery specific reboot fastboot";
+ /*
+ * The socket is created to allow switching between recovery and
+ * fastboot.
+ */
+ android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
+ if (sock < 0) {
+ WriteFdFmt(fd, "reboot (%s) create\n", strerror(errno));
+ PLOG(ERROR) << "Creating recovery socket failed";
+ return;
+ }
+
+ sockaddr_un addr = {.sun_family = AF_UNIX};
+ strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
+ if (connect(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) {
+ WriteFdFmt(fd, "reboot (%s) connect\n", strerror(errno));
+ PLOG(ERROR) << "Couldn't connect to recovery socket";
+ return;
+ }
+ const char msg_switch_to_fastboot = 'f';
+ auto ret = adb_write(sock, &msg_switch_to_fastboot, sizeof(msg_switch_to_fastboot));
+ if (ret != sizeof(msg_switch_to_fastboot)) {
+ WriteFdFmt(fd, "reboot (%s) write\n", strerror(errno));
+ PLOG(ERROR) << "Couldn't write message to recovery socket to switch to fastboot";
+ return;
+ }
+ } else {
+ if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) {
+ WriteFdFmt(fd.get(), "reboot (%s) failed\n", reboot_string.c_str());
+ return;
+ }
+ }
+ // Don't return early. Give the reboot command time to take effect
+ // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
+ while (true) {
+ pause();
+ }
+}
+
+void reconnect_service(unique_fd fd, atransport* t) {
+ WriteFdExactly(fd.get(), "done");
+ kick_transport(t);
+}
+
+unique_fd reverse_service(const char* command, atransport* transport) {
+ int s[2];
+ if (adb_socketpair(s)) {
+ PLOG(ERROR) << "cannot create service socket pair.";
+ return unique_fd{};
+ }
+ VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1];
+ if (!handle_forward_request(command, transport, s[1])) {
+ SendFail(s[1], "not a reverse forwarding command");
+ }
+ adb_close(s[1]);
+ return unique_fd{s[0]};
+}
+
+// Shell service string can look like:
+// shell[,arg1,arg2,...]:[command]
+unique_fd ShellService(const std::string& args, const atransport* transport) {
+ size_t delimiter_index = args.find(':');
+ if (delimiter_index == std::string::npos) {
+ LOG(ERROR) << "No ':' found in shell service arguments: " << args;
+ return unique_fd{};
+ }
+
+ const std::string service_args = args.substr(0, delimiter_index);
+ const std::string command = args.substr(delimiter_index + 1);
+
+ // Defaults:
+ // PTY for interactive, raw for non-interactive.
+ // No protocol.
+ // $TERM set to "dumb".
+ SubprocessType type(command.empty() ? SubprocessType::kPty : SubprocessType::kRaw);
+ SubprocessProtocol protocol = SubprocessProtocol::kNone;
+ std::string terminal_type = "dumb";
+
+ for (const std::string& arg : android::base::Split(service_args, ",")) {
+ if (arg == kShellServiceArgRaw) {
+ type = SubprocessType::kRaw;
+ } else if (arg == kShellServiceArgPty) {
+ type = SubprocessType::kPty;
+ } else if (arg == kShellServiceArgShellProtocol) {
+ protocol = SubprocessProtocol::kShell;
+ } else if (android::base::StartsWith(arg, "TERM=")) {
+ terminal_type = arg.substr(5);
+ } else if (!arg.empty()) {
+ // This is not an error to allow for future expansion.
+ LOG(WARNING) << "Ignoring unknown shell service argument: " << arg;
+ }
+ }
+
+ return StartSubprocess(command.c_str(), terminal_type.c_str(), type, protocol);
+}
+
+unique_fd daemon_service_to_fd(const char* name, atransport* transport) {
+ if (!strncmp("dev:", name, 4)) {
+ return unique_fd{unix_open(name + 4, O_RDWR | O_CLOEXEC)};
+ } else if (!strncmp(name, "framebuffer:", 12)) {
+ return create_service_thread("fb", framebuffer_service);
+ } else if (!strncmp(name, "jdwp:", 5)) {
+ return create_jdwp_connection_fd(atoi(name + 5));
+ } else if (!strncmp(name, "shell", 5)) {
+ return ShellService(name + 5, transport);
+ } else if (!strncmp(name, "exec:", 5)) {
+ return StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
+ } else if (!strncmp(name, "sync:", 5)) {
+ return create_service_thread("sync", file_sync_service);
+ } else if (!strncmp(name, "remount:", 8)) {
+ std::string options(name + strlen("remount:"));
+ return create_service_thread("remount",
+ std::bind(remount_service, std::placeholders::_1, options));
+ } else if (!strncmp(name, "reboot:", 7)) {
+ std::string arg(name + strlen("reboot:"));
+ return create_service_thread("reboot",
+ std::bind(reboot_service, std::placeholders::_1, arg));
+ } else if (!strncmp(name, "root:", 5)) {
+ return create_service_thread("root", restart_root_service);
+ } else if (!strncmp(name, "unroot:", 7)) {
+ return create_service_thread("unroot", restart_unroot_service);
+ } else if (!strncmp(name, "backup:", 7)) {
+ return StartSubprocess(
+ android::base::StringPrintf("/system/bin/bu backup %s", (name + 7)).c_str(),
+ nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
+ } else if (!strncmp(name, "restore:", 8)) {
+ return StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw,
+ SubprocessProtocol::kNone);
+ } else if (!strncmp(name, "tcpip:", 6)) {
+ int port;
+ if (sscanf(name + 6, "%d", &port) != 1) {
+ return unique_fd{};
+ }
+ return create_service_thread("tcp",
+ std::bind(restart_tcp_service, std::placeholders::_1, port));
+ } else if (!strncmp(name, "usb:", 4)) {
+ return create_service_thread("usb", restart_usb_service);
+ } else if (!strncmp(name, "reverse:", 8)) {
+ return reverse_service(name + 8, transport);
+ } else if (!strncmp(name, "disable-verity:", 15)) {
+ return create_service_thread("verity-on", std::bind(set_verity_enabled_state_service,
+ std::placeholders::_1, false));
+ } else if (!strncmp(name, "enable-verity:", 15)) {
+ return create_service_thread("verity-off", std::bind(set_verity_enabled_state_service,
+ std::placeholders::_1, true));
+ } else if (!strcmp(name, "reconnect")) {
+ return create_service_thread(
+ "reconnect", std::bind(reconnect_service, std::placeholders::_1, transport));
+ }
+ return unique_fd{};
+}
diff --git a/adb/daemon/set_verity_enable_state_service.cpp b/adb/daemon/set_verity_enable_state_service.cpp
index 49e0363..3676de5 100644
--- a/adb/daemon/set_verity_enable_state_service.cpp
+++ b/adb/daemon/set_verity_enable_state_service.cpp
@@ -16,8 +16,10 @@
#define TRACE_TAG ADB
+#include "set_verity_enable_state_service.h"
#include "sysdeps.h"
+#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <libavb_user/libavb_user.h>
@@ -25,14 +27,16 @@
#include <stdio.h>
#include <sys/stat.h>
-#include "android-base/properties.h"
-#include "android-base/stringprintf.h"
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
+#include <fs_mgr.h>
+#include <fs_mgr_overlayfs.h>
+#include <fstab/fstab.h>
#include <log/log_properties.h>
#include "adb.h"
#include "adb_io.h"
#include "adb_unique_fd.h"
-#include "fs_mgr.h"
#include "remount_service.h"
#include "fec/io.h"
@@ -45,6 +49,10 @@
static const bool kAllowDisableVerity = false;
#endif
+void suggest_run_adb_root(int fd) {
+ if (getuid() != 0) WriteFdExactly(fd, "Maybe run adb root?\n");
+}
+
/* Turn verity on/off */
static bool set_verity_enabled_state(int fd, const char* block_device, const char* mount_point,
bool enable) {
@@ -58,14 +66,14 @@
if (!fh) {
WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno));
- WriteFdFmt(fd, "Maybe run adb root?\n");
+ suggest_run_adb_root(fd);
return false;
}
fec_verity_metadata metadata;
if (!fh.get_verity_metadata(metadata)) {
- WriteFdFmt(fd, "Couldn't find verity metadata!\n");
+ WriteFdExactly(fd, "Couldn't find verity metadata!\n");
return false;
}
@@ -86,6 +94,17 @@
return false;
}
+ auto change = false;
+ errno = 0;
+ if (enable ? fs_mgr_overlayfs_teardown(mount_point, &change)
+ : fs_mgr_overlayfs_setup(nullptr, mount_point, &change)) {
+ if (change) {
+ WriteFdFmt(fd, "%s overlayfs for %s\n", enable ? "disabling" : "using", mount_point);
+ }
+ } else if (errno) {
+ WriteFdFmt(fd, "Overlayfs %s for %s failed with error %s\n", enable ? "teardown" : "setup",
+ mount_point, strerror(errno));
+ }
WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point);
return true;
}
@@ -98,13 +117,38 @@
return android::base::GetProperty("ro.boot.slot_suffix", "");
}
+static bool is_avb_device_locked() {
+ return android::base::GetProperty("ro.boot.vbmeta.device_state", "") == "locked";
+}
+
+static bool overlayfs_setup(int fd, bool enable) {
+ auto change = false;
+ errno = 0;
+ if (enable ? fs_mgr_overlayfs_teardown(nullptr, &change)
+ : fs_mgr_overlayfs_setup(nullptr, nullptr, &change)) {
+ if (change) {
+ WriteFdFmt(fd, "%s overlayfs\n", enable ? "disabling" : "using");
+ }
+ } else if (errno) {
+ WriteFdFmt(fd, "Overlayfs %s failed with error %s\n", enable ? "teardown" : "setup",
+ strerror(errno));
+ suggest_run_adb_root(fd);
+ }
+ return change;
+}
+
/* Use AVB to turn verity on/off */
static bool set_avb_verity_enabled_state(int fd, AvbOps* ops, bool enable_verity) {
std::string ab_suffix = get_ab_suffix();
-
bool verity_enabled;
+
+ if (is_avb_device_locked()) {
+ WriteFdExactly(fd, "Device is locked. Please unlock the device first\n");
+ return false;
+ }
+
if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) {
- WriteFdFmt(fd, "Error getting verity state\n");
+ WriteFdExactly(fd, "Error getting verity state. Try adb root first?\n");
return false;
}
@@ -114,20 +158,18 @@
}
if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) {
- WriteFdFmt(fd, "Error setting verity\n");
+ WriteFdExactly(fd, "Error setting verity\n");
return false;
}
+ overlayfs_setup(fd, enable_verity);
WriteFdFmt(fd, "Successfully %s verity\n", enable_verity ? "enabled" : "disabled");
return true;
}
-void set_verity_enabled_state_service(int fd, void* cookie) {
- unique_fd closer(fd);
+void set_verity_enabled_state_service(unique_fd fd, bool enable) {
bool any_changed = false;
- bool enable = (cookie != NULL);
-
// Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
// contract, androidboot.vbmeta.digest is set by the bootloader
// when using AVB).
@@ -138,12 +180,13 @@
// VB1.0 dm-verity is only enabled on certain builds.
if (!using_avb) {
if (!kAllowDisableVerity) {
- WriteFdFmt(fd, "%s-verity only works for userdebug builds\n",
+ WriteFdFmt(fd.get(), "%s-verity only works for userdebug builds\n",
enable ? "enable" : "disable");
}
if (!android::base::GetBoolProperty("ro.secure", false)) {
- WriteFdFmt(fd, "verity not enabled - ENG build\n");
+ overlayfs_setup(fd, enable);
+ WriteFdExactly(fd.get(), "verity not enabled - ENG build\n");
return;
}
}
@@ -151,7 +194,7 @@
// Should never be possible to disable dm-verity on a USER build
// regardless of using AVB or VB1.0.
if (!__android_log_is_debuggable()) {
- WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n");
+ WriteFdExactly(fd.get(), "verity cannot be disabled/enabled - USER build\n");
return;
}
@@ -159,10 +202,10 @@
// Yep, the system is using AVB.
AvbOps* ops = avb_ops_user_new();
if (ops == nullptr) {
- WriteFdFmt(fd, "Error getting AVB ops\n");
+ WriteFdExactly(fd.get(), "Error getting AVB ops\n");
return;
}
- if (set_avb_verity_enabled_state(fd, ops, enable)) {
+ if (set_avb_verity_enabled_state(fd.get(), ops, enable)) {
any_changed = true;
}
avb_ops_user_free(ops);
@@ -170,24 +213,26 @@
// Not using AVB - assume VB1.0.
// read all fstab entries at once from all sources
- fstab = fs_mgr_read_fstab_default();
+ if (!fstab) fstab = fs_mgr_read_fstab_default();
if (!fstab) {
- WriteFdFmt(fd, "Failed to read fstab\nMaybe run adb root?\n");
+ WriteFdExactly(fd.get(), "Failed to read fstab\n");
+ suggest_run_adb_root(fd.get());
return;
}
- // Loop through entries looking for ones that vold manages.
+ // Loop through entries looking for ones that verity manages.
for (int i = 0; i < fstab->num_entries; i++) {
if (fs_mgr_is_verified(&fstab->recs[i])) {
- if (set_verity_enabled_state(fd, fstab->recs[i].blk_device,
+ if (set_verity_enabled_state(fd.get(), fstab->recs[i].blk_device,
fstab->recs[i].mount_point, enable)) {
any_changed = true;
}
}
}
}
+ if (!any_changed) any_changed = overlayfs_setup(fd, enable);
if (any_changed) {
- WriteFdFmt(fd, "Now reboot your device for settings to take effect\n");
+ WriteFdExactly(fd.get(), "Now reboot your device for settings to take effect\n");
}
}
diff --git a/adb/remount_service.h b/adb/daemon/set_verity_enable_state_service.h
similarity index 71%
copy from adb/remount_service.h
copy to adb/daemon/set_verity_enable_state_service.h
index 7bda1be..c1413c8 100644
--- a/adb/remount_service.h
+++ b/adb/daemon/set_verity_enable_state_service.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,12 +14,8 @@
* limitations under the License.
*/
-#ifndef _REMOUNT_SERVICE_H_
-#define _REMOUNT_SERVICE_H_
+#pragma once
-#include <string>
+#include "adb_unique_fd.h"
-bool make_block_device_writable(const std::string&);
-void remount_service(int, void*);
-
-#endif
+void set_verity_enabled_state_service(unique_fd fd, bool enable);
diff --git a/adb/daemon/shell_service.cpp b/adb/daemon/shell_service.cpp
index f9f80c0..01097ac 100644
--- a/adb/daemon/shell_service.cpp
+++ b/adb/daemon/shell_service.cpp
@@ -98,6 +98,7 @@
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <private/android_logger.h>
+#include <selinux/android.h>
#include "adb.h"
#include "adb_io.h"
@@ -105,6 +106,7 @@
#include "adb_unique_fd.h"
#include "adb_utils.h"
#include "security_log_tags.h"
+#include "shell_protocol.h"
namespace {
@@ -334,8 +336,36 @@
// processes, so we need to manually reset back to SIG_DFL here (http://b/35209888).
signal(SIGPIPE, SIG_DFL);
+ // Increase oom_score_adj from -1000, so that the child is visible to the OOM-killer.
+ // Don't treat failure as an error, because old Android kernels explicitly disabled this.
+ int oom_score_adj_fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
+ if (oom_score_adj_fd != -1) {
+ const char* oom_score_adj_value = "-950";
+ TEMP_FAILURE_RETRY(
+ adb_write(oom_score_adj_fd, oom_score_adj_value, strlen(oom_score_adj_value)));
+ }
+
+#ifdef __ANDROID_RECOVERY__
+ // Special routine for recovery. Switch to shell domain when adbd is
+ // is running with dropped privileged (i.e. not running as root) and
+ // is built for the recovery mode. This is required because recovery
+ // rootfs is not labeled and everything is labeled just as rootfs.
+ char* con = nullptr;
+ if (getcon(&con) == 0) {
+ if (!strcmp(con, "u:r:adbd:s0")) {
+ if (selinux_android_setcon("u:r:shell:s0") < 0) {
+ LOG(FATAL) << "Could not set SELinux context for subprocess";
+ }
+ }
+ freecon(con);
+ } else {
+ LOG(FATAL) << "Failed to get SELinux context";
+ }
+#endif
+
if (command_.empty()) {
- execle(_PATH_BSHELL, _PATH_BSHELL, "-", nullptr, cenv.data());
+ // Spawn a login shell if we don't have a command.
+ execle(_PATH_BSHELL, "-" _PATH_BSHELL, nullptr, cenv.data());
} else {
execle(_PATH_BSHELL, _PATH_BSHELL, "-c", command_.c_str(), nullptr, cenv.data());
}
@@ -372,8 +402,8 @@
}
D("protocol FD = %d", protocol_sfd_.get());
- input_.reset(new ShellProtocol(protocol_sfd_));
- output_.reset(new ShellProtocol(protocol_sfd_));
+ input_ = std::make_unique<ShellProtocol>(protocol_sfd_);
+ output_ = std::make_unique<ShellProtocol>(protocol_sfd_);
if (!input_ || !output_) {
*error = "failed to allocate shell protocol objects";
kill(pid_, SIGKILL);
@@ -681,59 +711,42 @@
}
protocol_sfd_.reset(-1);
}
-
- // Pass the local socket FD to the shell cleanup fdevent.
- if (SHELL_EXIT_NOTIFY_FD >= 0) {
- int fd = local_socket_sfd_;
- if (WriteFdExactly(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd))) {
- D("passed fd %d to SHELL_EXIT_NOTIFY_FD (%d) for pid %d",
- fd, SHELL_EXIT_NOTIFY_FD, pid_);
- // The shell exit fdevent now owns the FD and will close it once
- // the last bit of data flushes through.
- static_cast<void>(local_socket_sfd_.release());
- } else {
- PLOG(ERROR) << "failed to write fd " << fd
- << " to SHELL_EXIT_NOTIFY_FD (" << SHELL_EXIT_NOTIFY_FD
- << ") for pid " << pid_;
- }
- }
}
} // namespace
// Create a pipe containing the error.
-static int ReportError(SubprocessProtocol protocol, const std::string& message) {
- int pipefd[2];
- if (pipe(pipefd) != 0) {
- LOG(ERROR) << "failed to create pipe to report error";
- return -1;
+static unique_fd ReportError(SubprocessProtocol protocol, const std::string& message) {
+ unique_fd read, write;
+ if (!Pipe(&read, &write)) {
+ PLOG(ERROR) << "failed to create pipe to report error";
+ return unique_fd{};
}
std::string buf = android::base::StringPrintf("error: %s\n", message.c_str());
if (protocol == SubprocessProtocol::kShell) {
ShellProtocol::Id id = ShellProtocol::kIdStderr;
uint32_t length = buf.length();
- WriteFdExactly(pipefd[1], &id, sizeof(id));
- WriteFdExactly(pipefd[1], &length, sizeof(length));
+ WriteFdExactly(write.get(), &id, sizeof(id));
+ WriteFdExactly(write.get(), &length, sizeof(length));
}
- WriteFdExactly(pipefd[1], buf.data(), buf.length());
+ WriteFdExactly(write.get(), buf.data(), buf.length());
if (protocol == SubprocessProtocol::kShell) {
ShellProtocol::Id id = ShellProtocol::kIdExit;
uint32_t length = 1;
char exit_code = 126;
- WriteFdExactly(pipefd[1], &id, sizeof(id));
- WriteFdExactly(pipefd[1], &length, sizeof(length));
- WriteFdExactly(pipefd[1], &exit_code, sizeof(exit_code));
+ WriteFdExactly(write.get(), &id, sizeof(id));
+ WriteFdExactly(write.get(), &length, sizeof(length));
+ WriteFdExactly(write.get(), &exit_code, sizeof(exit_code));
}
- adb_close(pipefd[1]);
- return pipefd[0];
+ return read;
}
-int StartSubprocess(const char* name, const char* terminal_type,
- SubprocessType type, SubprocessProtocol protocol) {
+unique_fd StartSubprocess(const char* name, const char* terminal_type, SubprocessType type,
+ SubprocessProtocol protocol) {
D("starting %s subprocess (protocol=%s, TERM=%s): '%s'",
type == SubprocessType::kRaw ? "raw" : "PTY",
protocol == SubprocessProtocol::kNone ? "none" : "shell",
@@ -760,5 +773,5 @@
return ReportError(protocol, error);
}
- return local_socket.release();
+ return local_socket;
}
diff --git a/adb/daemon/shell_service.h b/adb/daemon/shell_service.h
new file mode 100644
index 0000000..2a48923
--- /dev/null
+++ b/adb/daemon/shell_service.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include "adb_unique_fd.h"
+
+enum class SubprocessType {
+ kPty,
+ kRaw,
+};
+
+enum class SubprocessProtocol {
+ kNone,
+ kShell,
+};
+
+// Forks and starts a new shell subprocess. If |name| is empty an interactive
+// shell is started, otherwise |name| is executed non-interactively.
+//
+// Returns an open FD connected to the subprocess or -1 on failure.
+unique_fd StartSubprocess(const char* name, const char* terminal_type, SubprocessType type,
+ SubprocessProtocol protocol);
diff --git a/adb/daemon/shell_service_test.cpp b/adb/daemon/shell_service_test.cpp
index 839284e..323bcec 100644
--- a/adb/daemon/shell_service_test.cpp
+++ b/adb/daemon/shell_service_test.cpp
@@ -27,6 +27,7 @@
#include "adb.h"
#include "adb_io.h"
+#include "shell_protocol.h"
#include "sysdeps.h"
class ShellServiceTest : public ::testing::Test {
@@ -54,42 +55,18 @@
static sighandler_t saved_sigpipe_handler_;
- int subprocess_fd_ = -1;
- int shell_exit_receiver_fd_ = -1, saved_shell_exit_fd_;
+ unique_fd subprocess_fd_;
};
sighandler_t ShellServiceTest::saved_sigpipe_handler_ = nullptr;
void ShellServiceTest::StartTestSubprocess(
const char* command, SubprocessType type, SubprocessProtocol protocol) {
- // We want to intercept the shell exit message to make sure it's sent.
- saved_shell_exit_fd_ = SHELL_EXIT_NOTIFY_FD;
- int fd[2];
- ASSERT_TRUE(adb_socketpair(fd) >= 0);
- SHELL_EXIT_NOTIFY_FD = fd[0];
- shell_exit_receiver_fd_ = fd[1];
-
subprocess_fd_ = StartSubprocess(command, nullptr, type, protocol);
ASSERT_TRUE(subprocess_fd_ >= 0);
}
void ShellServiceTest::CleanupTestSubprocess() {
- if (subprocess_fd_ >= 0) {
- // Subprocess should send its FD to SHELL_EXIT_NOTIFY_FD for cleanup.
- int notified_fd = -1;
- ASSERT_TRUE(ReadFdExactly(shell_exit_receiver_fd_, ¬ified_fd,
- sizeof(notified_fd)));
- ASSERT_EQ(notified_fd, subprocess_fd_);
-
- adb_close(subprocess_fd_);
- subprocess_fd_ = -1;
-
- // Restore SHELL_EXIT_NOTIFY_FD.
- adb_close(SHELL_EXIT_NOTIFY_FD);
- adb_close(shell_exit_receiver_fd_);
- shell_exit_receiver_fd_ = -1;
- SHELL_EXIT_NOTIFY_FD = saved_shell_exit_fd_;
- }
}
namespace {
diff --git a/adb/daemon/usb.cpp b/adb/daemon/usb.cpp
index 87ed3db..c79d6fc 100644
--- a/adb/daemon/usb.cpp
+++ b/adb/daemon/usb.cpp
@@ -41,7 +41,7 @@
#include <android-base/properties.h>
#include "adb.h"
-#include "daemon/usb.h"
+#include "adbd/usb.h"
#include "transport.h"
using namespace std::chrono_literals;
@@ -53,15 +53,11 @@
#define USB_FFS_BULK_SIZE 16384
// Number of buffers needed to fit MAX_PAYLOAD, with an extra for ZLPs.
-#define USB_FFS_NUM_BUFS ((MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1)
+#define USB_FFS_NUM_BUFS ((4 * MAX_PAYLOAD / USB_FFS_BULK_SIZE) + 1)
#define cpu_to_le16(x) htole16(x)
#define cpu_to_le32(x) htole32(x)
-#define FUNCTIONFS_ENDPOINT_ALLOC _IOR('g', 231, __u32)
-
-static constexpr size_t ENDPOINT_ALLOC_RETRIES = 10;
-
static int dummy_fd = -1;
struct func_desc {
@@ -230,14 +226,18 @@
},
};
-static void aio_block_init(aio_block* aiob) {
- aiob->iocb.resize(USB_FFS_NUM_BUFS);
- aiob->iocbs.resize(USB_FFS_NUM_BUFS);
- aiob->events.resize(USB_FFS_NUM_BUFS);
+static void aio_block_init(aio_block* aiob, unsigned num_bufs) {
+ aiob->iocb.resize(num_bufs);
+ aiob->iocbs.resize(num_bufs);
+ aiob->events.resize(num_bufs);
aiob->num_submitted = 0;
- for (unsigned i = 0; i < USB_FFS_NUM_BUFS; i++) {
+ for (unsigned i = 0; i < num_bufs; i++) {
aiob->iocbs[i] = &aiob->iocb[i];
}
+ memset(&aiob->ctx, 0, sizeof(aiob->ctx));
+ if (io_setup(num_bufs, &aiob->ctx)) {
+ D("[ aio: got error on io_setup (%d) ]", errno);
+ }
}
static int getMaxPacketSize(int ffs_fd) {
@@ -250,13 +250,12 @@
}
}
-bool init_functionfs(struct usb_handle* h) {
+static bool init_functionfs(struct usb_handle* h) {
LOG(INFO) << "initializing functionfs";
ssize_t ret;
struct desc_v1 v1_descriptor;
struct desc_v2 v2_descriptor;
- size_t retries = 0;
v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
@@ -274,7 +273,7 @@
if (h->control < 0) { // might have already done this before
LOG(INFO) << "opening control endpoint " << USB_FFS_ADB_EP0;
- h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
+ h->control = adb_open(USB_FFS_ADB_EP0, O_WRONLY);
if (h->control < 0) {
PLOG(ERROR) << "cannot open control endpoint " << USB_FFS_ADB_EP0;
goto err;
@@ -305,49 +304,21 @@
android::base::SetProperty("sys.usb.ffs.ready", "1");
}
- h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
+ h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDONLY);
if (h->bulk_out < 0) {
PLOG(ERROR) << "cannot open bulk-out endpoint " << USB_FFS_ADB_OUT;
goto err;
}
- h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
+ h->bulk_in = adb_open(USB_FFS_ADB_IN, O_WRONLY);
if (h->bulk_in < 0) {
PLOG(ERROR) << "cannot open bulk-in endpoint " << USB_FFS_ADB_IN;
goto err;
}
- if (io_setup(USB_FFS_NUM_BUFS, &h->read_aiob.ctx) ||
- io_setup(USB_FFS_NUM_BUFS, &h->write_aiob.ctx)) {
- D("[ aio: got error on io_setup (%d) ]", errno);
- }
-
h->read_aiob.fd = h->bulk_out;
h->write_aiob.fd = h->bulk_in;
-
- h->max_rw = MAX_PAYLOAD;
- while (h->max_rw >= USB_FFS_BULK_SIZE && retries < ENDPOINT_ALLOC_RETRIES) {
- int ret_in = ioctl(h->bulk_in, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(h->max_rw));
- int errno_in = errno;
- int ret_out = ioctl(h->bulk_out, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(h->max_rw));
- int errno_out = errno;
-
- if (ret_in || ret_out) {
- if (errno_in == ENODEV || errno_out == ENODEV) {
- std::this_thread::sleep_for(100ms);
- retries += 1;
- continue;
- }
- h->max_rw /= 2;
- } else {
- return true;
- }
- }
-
- D("[ adb: cannot call endpoint alloc: errno=%d ]", errno);
- // Kernel pre-allocation could have failed for recoverable reasons.
- // Continue running with a safe max rw size.
- h->max_rw = USB_FFS_BULK_SIZE;
+ h->reads_zero_packets = true;
return true;
err:
@@ -366,9 +337,7 @@
return false;
}
-static void usb_ffs_open_thread(void* x) {
- struct usb_handle* usb = (struct usb_handle*)x;
-
+static void usb_ffs_open_thread(usb_handle *usb) {
adb_thread_setname("usb ffs open");
while (true) {
@@ -389,7 +358,7 @@
}
LOG(INFO) << "registering usb transport";
- register_usb_transport(usb, 0, 0, 1);
+ register_usb_transport(usb, nullptr, nullptr, 1);
}
// never gets here
@@ -400,8 +369,9 @@
D("about to write (fd=%d, len=%d)", h->bulk_in, len);
const char* buf = static_cast<const char*>(data);
+ int orig_len = len;
while (len > 0) {
- int write_len = std::min(h->max_rw, len);
+ int write_len = std::min(USB_FFS_BULK_SIZE, len);
int n = adb_write(h->bulk_in, buf, write_len);
if (n < 0) {
D("ERROR: fd = %d, n = %d: %s", h->bulk_in, n, strerror(errno));
@@ -412,15 +382,16 @@
}
D("[ done fd=%d ]", h->bulk_in);
- return 0;
+ return orig_len;
}
static int usb_ffs_read(usb_handle* h, void* data, int len) {
D("about to read (fd=%d, len=%d)", h->bulk_out, len);
char* buf = static_cast<char*>(data);
+ int orig_len = len;
while (len > 0) {
- int read_len = std::min(h->max_rw, len);
+ int read_len = std::min(USB_FFS_BULK_SIZE, len);
int n = adb_read(h->bulk_out, buf, read_len);
if (n < 0) {
D("ERROR: fd = %d, n = %d: %s", h->bulk_out, n, strerror(errno));
@@ -431,14 +402,14 @@
}
D("[ done fd=%d ]", h->bulk_out);
- return 0;
+ return orig_len;
}
static int usb_ffs_do_aio(usb_handle* h, const void* data, int len, bool read) {
aio_block* aiob = read ? &h->read_aiob : &h->write_aiob;
bool zero_packet = false;
- int num_bufs = len / USB_FFS_BULK_SIZE + (len % USB_FFS_BULK_SIZE == 0 ? 0 : 1);
+ int num_bufs = len / h->io_size + (len % h->io_size == 0 ? 0 : 1);
const char* cur_data = reinterpret_cast<const char*>(data);
int packet_size = getMaxPacketSize(aiob->fd);
@@ -448,7 +419,7 @@
}
for (int i = 0; i < num_bufs; i++) {
- int buf_len = std::min(len, USB_FFS_BULK_SIZE);
+ int buf_len = std::min(len, static_cast<int>(h->io_size));
io_prep(&aiob->iocb[i], aiob->fd, cur_data, buf_len, 0, read);
len -= buf_len;
@@ -457,7 +428,7 @@
if (len == 0 && buf_len % packet_size == 0 && read) {
// adb does not expect the device to send a zero packet after data transfer,
// but the host *does* send a zero packet for the device to read.
- zero_packet = true;
+ zero_packet = h->reads_zero_packets;
}
}
if (zero_packet) {
@@ -466,23 +437,31 @@
num_bufs += 1;
}
- if (io_submit(aiob->ctx, num_bufs, aiob->iocbs.data()) < num_bufs) {
- D("[ aio: got error submitting %s (%d) ]", read ? "read" : "write", errno);
- return -1;
- }
- if (TEMP_FAILURE_RETRY(
- io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(), nullptr)) < num_bufs) {
- D("[ aio: got error waiting %s (%d) ]", read ? "read" : "write", errno);
- return -1;
- }
- for (int i = 0; i < num_bufs; i++) {
- if (aiob->events[i].res < 0) {
- errno = aiob->events[i].res;
- D("[ aio: got error event on %s (%d) ]", read ? "read" : "write", errno);
+ while (true) {
+ if (TEMP_FAILURE_RETRY(io_submit(aiob->ctx, num_bufs, aiob->iocbs.data())) < num_bufs) {
+ PLOG(ERROR) << "aio: got error submitting " << (read ? "read" : "write");
return -1;
}
+ if (TEMP_FAILURE_RETRY(io_getevents(aiob->ctx, num_bufs, num_bufs, aiob->events.data(),
+ nullptr)) < num_bufs) {
+ PLOG(ERROR) << "aio: got error waiting " << (read ? "read" : "write");
+ return -1;
+ }
+ if (num_bufs == 1 && aiob->events[0].res == -EINTR) {
+ continue;
+ }
+ int ret = 0;
+ for (int i = 0; i < num_bufs; i++) {
+ if (aiob->events[i].res < 0) {
+ errno = -aiob->events[i].res;
+ PLOG(ERROR) << "aio: got error event on " << (read ? "read" : "write")
+ << " total bufs " << num_bufs;
+ return -1;
+ }
+ ret += aiob->events[i].res;
+ }
+ return ret;
}
- return 0;
}
static int usb_ffs_aio_read(usb_handle* h, void* data, int len) {
@@ -521,8 +500,6 @@
h->kicked = false;
adb_close(h->bulk_out);
adb_close(h->bulk_in);
- io_destroy(h->read_aiob.ctx);
- io_destroy(h->write_aiob.ctx);
// Notify usb_adb_open_thread to open a new connection.
h->lock.lock();
@@ -531,9 +508,7 @@
h->notify.notify_one();
}
-static void usb_ffs_init() {
- D("[ usb_init - using FunctionFS ]");
-
+usb_handle *create_usb_handle(unsigned num_bufs, unsigned io_size) {
usb_handle* h = new usb_handle();
if (android::base::GetBoolProperty("sys.usb.ffs.aio_compat", false)) {
@@ -544,20 +519,21 @@
} else {
h->write = usb_ffs_aio_write;
h->read = usb_ffs_aio_read;
- aio_block_init(&h->read_aiob);
- aio_block_init(&h->write_aiob);
+ aio_block_init(&h->read_aiob, num_bufs);
+ aio_block_init(&h->write_aiob, num_bufs);
}
+ h->io_size = io_size;
h->kick = usb_ffs_kick;
h->close = usb_ffs_close;
-
- D("[ usb_init - starting thread ]");
- std::thread(usb_ffs_open_thread, h).detach();
+ return h;
}
void usb_init() {
+ D("[ usb_init - using FunctionFS ]");
dummy_fd = adb_open("/dev/null", O_WRONLY);
CHECK_NE(dummy_fd, -1);
- usb_ffs_init();
+
+ std::thread(usb_ffs_open_thread, create_usb_handle(USB_FFS_NUM_BUFS, USB_FFS_BULK_SIZE)).detach();
}
int usb_write(usb_handle* h, const void* data, int len) {
diff --git a/adb/fdevent.cpp b/adb/fdevent.cpp
index d285561..dee87bd 100644
--- a/adb/fdevent.cpp
+++ b/adb/fdevent.cpp
@@ -21,6 +21,8 @@
#include "fdevent.h"
#include <fcntl.h>
+#include <inttypes.h>
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -36,19 +38,13 @@
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/thread_annotations.h>
+#include <android-base/threads.h>
#include "adb_io.h"
#include "adb_trace.h"
#include "adb_unique_fd.h"
#include "adb_utils.h"
-#if !ADB_HOST
-// This socket is used when a subproc shell service exists.
-// It wakes up the fdevent_loop() and cause the correct handling
-// of the shell's pseudo-tty master. I.e. force close it.
-int SHELL_EXIT_NOTIFY_FD = -1;
-#endif // !ADB_HOST
-
#define FDE_EVENTMASK 0x00ff
#define FDE_STATEMASK 0xff00
@@ -62,7 +58,7 @@
explicit PollNode(fdevent* fde) : fde(fde) {
memset(&pollfd, 0, sizeof(pollfd));
- pollfd.fd = fde->fd;
+ pollfd.fd = fde->fd.get();
#if defined(__linux__)
// Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
@@ -78,21 +74,24 @@
static auto& g_pending_list = *new std::list<fdevent*>();
static std::atomic<bool> terminate_loop(false);
static bool main_thread_valid;
-static unsigned long main_thread_id;
+static uint64_t main_thread_id;
+static uint64_t fdevent_id;
+
+static bool run_needs_flush = false;
static auto& run_queue_notify_fd = *new unique_fd();
static auto& run_queue_mutex = *new std::mutex();
static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque<std::function<void()>>();
void check_main_thread() {
if (main_thread_valid) {
- CHECK_EQ(main_thread_id, adb_thread_id());
+ CHECK_EQ(main_thread_id, android::base::GetThreadId());
}
}
void set_main_thread() {
main_thread_valid = true;
- main_thread_id = adb_thread_id();
+ main_thread_id = android::base::GetThreadId();
}
static std::string dump_fde(const fdevent* fde) {
@@ -115,37 +114,24 @@
if (fde->state & FDE_ERROR) {
state += "E";
}
- if (fde->state & FDE_DONT_CLOSE) {
- state += "D";
- }
- return android::base::StringPrintf("(fdevent %d %s)", fde->fd, state.c_str());
-}
-
-fdevent* fdevent_create(int fd, fd_func func, void* arg) {
- check_main_thread();
- fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
- if(fde == 0) return 0;
- fdevent_install(fde, fd, func, arg);
- fde->state |= FDE_CREATED;
- return fde;
-}
-
-void fdevent_destroy(fdevent* fde) {
- check_main_thread();
- if(fde == 0) return;
- if(!(fde->state & FDE_CREATED)) {
- LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
- }
- fdevent_remove(fde);
- free(fde);
+ return android::base::StringPrintf("(fdevent %" PRIu64 ": fd %d %s)", fde->id, fde->fd.get(),
+ state.c_str());
}
void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
check_main_thread();
CHECK_GE(fd, 0);
memset(fde, 0, sizeof(fdevent));
+}
+
+fdevent* fdevent_create(int fd, fd_func func, void* arg) {
+ check_main_thread();
+ CHECK_GE(fd, 0);
+
+ fdevent* fde = new fdevent();
+ fde->id = fdevent_id++;
fde->state = FDE_ACTIVE;
- fde->fd = fd;
+ fde->fd.reset(fd);
fde->func = func;
fde->arg = arg;
if (!set_file_block_mode(fd, false)) {
@@ -154,30 +140,35 @@
// to handle it.
LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
}
- auto pair = g_poll_node_map.emplace(fde->fd, PollNode(fde));
+ auto pair = g_poll_node_map.emplace(fde->fd.get(), PollNode(fde));
CHECK(pair.second) << "install existing fd " << fd;
- D("fdevent_install %s", dump_fde(fde).c_str());
+
+ fde->state |= FDE_CREATED;
+ return fde;
}
-void fdevent_remove(fdevent* fde) {
+void fdevent_destroy(fdevent* fde) {
check_main_thread();
- D("fdevent_remove %s", dump_fde(fde).c_str());
+ if (fde == nullptr) return;
+ if (!(fde->state & FDE_CREATED)) {
+ LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
+ }
+
if (fde->state & FDE_ACTIVE) {
- g_poll_node_map.erase(fde->fd);
+ g_poll_node_map.erase(fde->fd.get());
if (fde->state & FDE_PENDING) {
g_pending_list.remove(fde);
}
- if (!(fde->state & FDE_DONT_CLOSE)) {
- adb_close(fde->fd);
- fde->fd = -1;
- }
+ fde->fd.reset();
fde->state = 0;
fde->events = 0;
}
+
+ delete fde;
}
static void fdevent_update(fdevent* fde, unsigned events) {
- auto it = g_poll_node_map.find(fde->fd);
+ auto it = g_poll_node_map.find(fde->fd.get());
CHECK(it != g_poll_node_map.end());
PollNode& node = it->second;
if (events & FDE_READ) {
@@ -276,7 +267,7 @@
auto it = g_poll_node_map.find(pollfd.fd);
CHECK(it != g_poll_node_map.end());
fdevent* fde = it->second.fde;
- CHECK_EQ(fde->fd, pollfd.fd);
+ CHECK_EQ(fde->fd.get(), pollfd.fd);
fde->events |= events;
D("%s got events %x", dump_fde(fde).c_str(), events);
fde->state |= FDE_PENDING;
@@ -291,75 +282,9 @@
CHECK(fde->state & FDE_PENDING);
fde->state &= (~FDE_PENDING);
D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
- fde->func(fde->fd, events, fde->arg);
+ fde->func(fde->fd.get(), events, fde->arg);
}
-#if !ADB_HOST
-
-#include <sys/ioctl.h>
-
-static void fdevent_subproc_event_func(int fd, unsigned ev, void* /* userdata */) {
- D("subproc handling on fd = %d, ev = %x", fd, ev);
-
- CHECK_GE(fd, 0);
-
- if (ev & FDE_READ) {
- int subproc_fd;
-
- if(!ReadFdExactly(fd, &subproc_fd, sizeof(subproc_fd))) {
- LOG(FATAL) << "Failed to read the subproc's fd from " << fd;
- }
- auto it = g_poll_node_map.find(subproc_fd);
- if (it == g_poll_node_map.end()) {
- D("subproc_fd %d cleared from fd_table", subproc_fd);
- adb_close(subproc_fd);
- return;
- }
- fdevent* subproc_fde = it->second.fde;
- if(subproc_fde->fd != subproc_fd) {
- // Already reallocated?
- LOG(FATAL) << "subproc_fd(" << subproc_fd << ") != subproc_fde->fd(" << subproc_fde->fd
- << ")";
- return;
- }
-
- subproc_fde->force_eof = 1;
-
- int rcount = 0;
- ioctl(subproc_fd, FIONREAD, &rcount);
- D("subproc with fd %d has rcount=%d, err=%d", subproc_fd, rcount, errno);
- if (rcount != 0) {
- // If there is data left, it will show up in the select().
- // This works because there is no other thread reading that
- // data when in this fd_func().
- return;
- }
-
- D("subproc_fde %s", dump_fde(subproc_fde).c_str());
- subproc_fde->events |= FDE_READ;
- if(subproc_fde->state & FDE_PENDING) {
- return;
- }
- subproc_fde->state |= FDE_PENDING;
- fdevent_call_fdfunc(subproc_fde);
- }
-}
-
-static void fdevent_subproc_setup() {
- int s[2];
-
- if(adb_socketpair(s)) {
- PLOG(FATAL) << "cannot create shell-exit socket-pair";
- }
- D("fdevent_subproc: socket pair (%d, %d)", s[0], s[1]);
-
- SHELL_EXIT_NOTIFY_FD = s[0];
- fdevent *fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
- CHECK(fde != nullptr) << "cannot create fdevent for shell-exit handler";
- fdevent_add(fde, FDE_READ);
-}
-#endif // !ADB_HOST
-
static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
// We need to be careful around reentrancy here, since a function we call can queue up another
// function.
@@ -388,7 +313,8 @@
PLOG(FATAL) << "failed to empty run queue notify fd";
}
- fdevent_run_flush();
+ // Mark that we need to flush, and then run it at the end of fdevent_loop.
+ run_needs_flush = true;
}
static void fdevent_run_setup() {
@@ -400,6 +326,10 @@
PLOG(FATAL) << "failed to create run queue notify socketpair";
}
+ if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) {
+ PLOG(FATAL) << "failed to make run queue notify socket nonblocking";
+ }
+
run_queue_notify_fd.reset(s[0]);
fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr);
CHECK(fde != nullptr);
@@ -416,7 +346,12 @@
// run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up.
// In that case, rely on the setup code to flush the queue without a notification being needed.
if (run_queue_notify_fd != -1) {
- if (adb_write(run_queue_notify_fd.get(), "", 1) != 1) {
+ int rc = adb_write(run_queue_notify_fd.get(), "", 1);
+
+ // It's possible that we get EAGAIN here, if lots of notifications came in while handling.
+ if (rc == 0) {
+ PLOG(FATAL) << "run queue notify fd was closed?";
+ } else if (rc == -1 && errno != EAGAIN) {
PLOG(FATAL) << "failed to write to run queue notify fd";
}
}
@@ -424,9 +359,6 @@
void fdevent_loop() {
set_main_thread();
-#if !ADB_HOST
- fdevent_subproc_setup();
-#endif // !ADB_HOST
fdevent_run_setup();
while (true) {
@@ -443,6 +375,11 @@
g_pending_list.pop_front();
fdevent_call_fdfunc(fde);
}
+
+ if (run_needs_flush) {
+ fdevent_run_flush();
+ run_needs_flush = false;
+ }
}
}
diff --git a/adb/fdevent.h b/adb/fdevent.h
index 896400a..d501b86 100644
--- a/adb/fdevent.h
+++ b/adb/fdevent.h
@@ -22,28 +22,26 @@
#include <functional>
+#include "adb_unique_fd.h"
+
/* events that may be observed */
#define FDE_READ 0x0001
#define FDE_WRITE 0x0002
#define FDE_ERROR 0x0004
-/* features that may be set (via the events set/add/del interface) */
-#define FDE_DONT_CLOSE 0x0080
-
typedef void (*fd_func)(int fd, unsigned events, void *userdata);
struct fdevent {
- fdevent *next;
- fdevent *prev;
+ uint64_t id;
- int fd;
- int force_eof;
+ unique_fd fd;
+ int force_eof = 0;
- uint16_t state;
- uint16_t events;
+ uint16_t state = 0;
+ uint16_t events = 0;
- fd_func func;
- void *arg;
+ fd_func func = nullptr;
+ void* arg = nullptr;
};
/* Allocate and initialize a new fdevent object
@@ -57,15 +55,6 @@
*/
void fdevent_destroy(fdevent *fde);
-/* Initialize an fdevent object that was externally allocated
-*/
-void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
-
-/* Uninitialize an fdevent object that was initialized by
-** fdevent_install()
-*/
-void fdevent_remove(fdevent *item);
-
/* Change which events should cause notifications
*/
void fdevent_set(fdevent *fde, unsigned events);
diff --git a/adb/fdevent_test.cpp b/adb/fdevent_test.cpp
index 63cc4d1..0cb2439 100644
--- a/adb/fdevent_test.cpp
+++ b/adb/fdevent_test.cpp
@@ -26,18 +26,19 @@
#include "adb_io.h"
#include "fdevent_test.h"
+#include "sysdeps/memory.h"
class FdHandler {
public:
FdHandler(int read_fd, int write_fd) : read_fd_(read_fd), write_fd_(write_fd) {
- fdevent_install(&read_fde_, read_fd_, FdEventCallback, this);
- fdevent_add(&read_fde_, FDE_READ);
- fdevent_install(&write_fde_, write_fd_, FdEventCallback, this);
+ read_fde_ = fdevent_create(read_fd_, FdEventCallback, this);
+ fdevent_add(read_fde_, FDE_READ);
+ write_fde_ = fdevent_create(write_fd_, FdEventCallback, this);
}
~FdHandler() {
- fdevent_remove(&read_fde_);
- fdevent_remove(&write_fde_);
+ fdevent_destroy(read_fde_);
+ fdevent_destroy(write_fde_);
}
private:
@@ -49,7 +50,7 @@
char c;
ASSERT_EQ(1, adb_read(fd, &c, 1));
handler->queue_.push(c);
- fdevent_add(&handler->write_fde_, FDE_WRITE);
+ fdevent_add(handler->write_fde_, FDE_WRITE);
}
if (events & FDE_WRITE) {
ASSERT_EQ(fd, handler->write_fd_);
@@ -58,7 +59,7 @@
handler->queue_.pop();
ASSERT_EQ(1, adb_write(fd, &c, 1));
if (handler->queue_.empty()) {
- fdevent_del(&handler->write_fde_, FDE_WRITE);
+ fdevent_del(handler->write_fde_, FDE_WRITE);
}
}
}
@@ -66,8 +67,8 @@
private:
const int read_fd_;
const int write_fd_;
- fdevent read_fde_;
- fdevent write_fde_;
+ fdevent* read_fde_;
+ fdevent* write_fde_;
std::queue<char> queue_;
};
@@ -79,30 +80,7 @@
TEST_F(FdeventTest, fdevent_terminate) {
PrepareThread();
-
- std::thread thread(fdevent_loop);
- TerminateThread(thread);
-}
-
-static void FdEventThreadFunc(ThreadArg* arg) {
- std::vector<int> read_fds;
- std::vector<int> write_fds;
-
- read_fds.push_back(arg->first_read_fd);
- for (size_t i = 0; i < arg->middle_pipe_count; ++i) {
- int fds[2];
- ASSERT_EQ(0, adb_socketpair(fds));
- read_fds.push_back(fds[0]);
- write_fds.push_back(fds[1]);
- }
- write_fds.push_back(arg->last_write_fd);
-
- std::vector<std::unique_ptr<FdHandler>> fd_handlers;
- for (size_t i = 0; i < read_fds.size(); ++i) {
- fd_handlers.push_back(std::unique_ptr<FdHandler>(new FdHandler(read_fds[i], write_fds[i])));
- }
-
- fdevent_loop();
+ TerminateThread();
}
TEST_F(FdeventTest, smoke) {
@@ -121,7 +99,26 @@
int reader = fd_pair2[0];
PrepareThread();
- std::thread thread(FdEventThreadFunc, &thread_arg);
+
+ std::vector<std::unique_ptr<FdHandler>> fd_handlers;
+ fdevent_run_on_main_thread([&thread_arg, &fd_handlers]() {
+ std::vector<int> read_fds;
+ std::vector<int> write_fds;
+
+ read_fds.push_back(thread_arg.first_read_fd);
+ for (size_t i = 0; i < thread_arg.middle_pipe_count; ++i) {
+ int fds[2];
+ ASSERT_EQ(0, adb_socketpair(fds));
+ read_fds.push_back(fds[0]);
+ write_fds.push_back(fds[1]);
+ }
+ write_fds.push_back(thread_arg.last_write_fd);
+
+ for (size_t i = 0; i < read_fds.size(); ++i) {
+ fd_handlers.push_back(std::make_unique<FdHandler>(read_fds[i], write_fds[i]));
+ }
+ });
+ WaitForFdeventLoop();
for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
std::string read_buffer = MESSAGE;
@@ -131,21 +128,24 @@
ASSERT_EQ(read_buffer, write_buffer);
}
- TerminateThread(thread);
+ fdevent_run_on_main_thread([&fd_handlers]() { fd_handlers.clear(); });
+ WaitForFdeventLoop();
+
+ TerminateThread();
ASSERT_EQ(0, adb_close(writer));
ASSERT_EQ(0, adb_close(reader));
}
struct InvalidFdArg {
- fdevent fde;
+ fdevent* fde;
unsigned expected_events;
size_t* happened_event_count;
};
-static void InvalidFdEventCallback(int fd, unsigned events, void* userdata) {
+static void InvalidFdEventCallback(int, unsigned events, void* userdata) {
InvalidFdArg* arg = reinterpret_cast<InvalidFdArg*>(userdata);
ASSERT_EQ(arg->expected_events, events);
- fdevent_remove(&arg->fde);
+ fdevent_destroy(arg->fde);
if (++*(arg->happened_event_count) == 2) {
fdevent_terminate_loop();
}
@@ -157,15 +157,15 @@
InvalidFdArg read_arg;
read_arg.expected_events = FDE_READ | FDE_ERROR;
read_arg.happened_event_count = &happened_event_count;
- fdevent_install(&read_arg.fde, INVALID_READ_FD, InvalidFdEventCallback, &read_arg);
- fdevent_add(&read_arg.fde, FDE_READ);
+ read_arg.fde = fdevent_create(INVALID_READ_FD, InvalidFdEventCallback, &read_arg);
+ fdevent_add(read_arg.fde, FDE_READ);
const int INVALID_WRITE_FD = std::numeric_limits<int>::max();
InvalidFdArg write_arg;
write_arg.expected_events = FDE_READ | FDE_ERROR;
write_arg.happened_event_count = &happened_event_count;
- fdevent_install(&write_arg.fde, INVALID_WRITE_FD, InvalidFdEventCallback, &write_arg);
- fdevent_add(&write_arg.fde, FDE_WRITE);
+ write_arg.fde = fdevent_create(INVALID_WRITE_FD, InvalidFdEventCallback, &write_arg);
+ fdevent_add(write_arg.fde, FDE_WRITE);
fdevent_loop();
}
@@ -178,19 +178,24 @@
std::vector<int> vec;
PrepareThread();
- std::thread thread(fdevent_loop);
- for (int i = 0; i < 100; ++i) {
+ // Block the main thread for a long time while we queue our callbacks.
+ fdevent_run_on_main_thread([]() {
+ check_main_thread();
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ });
+
+ for (int i = 0; i < 1000000; ++i) {
fdevent_run_on_main_thread([i, &vec]() {
check_main_thread();
vec.push_back(i);
});
}
- TerminateThread(thread);
+ TerminateThread();
- ASSERT_EQ(100u, vec.size());
- for (int i = 0; i < 100; ++i) {
+ ASSERT_EQ(1000000u, vec.size());
+ for (int i = 0; i < 1000000; ++i) {
ASSERT_EQ(i, vec[i]);
}
}
@@ -211,11 +216,8 @@
std::vector<int> vec;
PrepareThread();
- std::thread thread(fdevent_loop);
-
fdevent_run_on_main_thread(make_appender(&vec, 0));
-
- TerminateThread(thread);
+ TerminateThread();
ASSERT_EQ(100u, vec.size());
for (int i = 0; i < 100; ++i) {
diff --git a/adb/fdevent_test.h b/adb/fdevent_test.h
index 5ca49ac..5a417e0 100644
--- a/adb/fdevent_test.h
+++ b/adb/fdevent_test.h
@@ -16,10 +16,31 @@
#include <gtest/gtest.h>
+#include <condition_variable>
+#include <mutex>
#include <thread>
#include "socket.h"
#include "sysdeps.h"
+#include "sysdeps/chrono.h"
+
+static void WaitForFdeventLoop() {
+ // Sleep for a bit to make sure that network events have propagated.
+ std::this_thread::sleep_for(100ms);
+
+ // fdevent_run_on_main_thread has a guaranteed ordering, and is guaranteed to happen after
+ // socket events, so as soon as our function is called, we know that we've processed all
+ // previous events.
+ std::mutex mutex;
+ std::condition_variable cv;
+ std::unique_lock<std::mutex> lock(mutex);
+ fdevent_run_on_main_thread([&]() {
+ mutex.lock();
+ mutex.unlock();
+ cv.notify_one();
+ });
+ cv.wait(lock);
+}
class FdeventTest : public ::testing::Test {
protected:
@@ -49,22 +70,22 @@
}
dummy_socket->ready(dummy_socket);
dummy = dummy_fds[0];
+
+ thread_ = std::thread([]() { fdevent_loop(); });
+ WaitForFdeventLoop();
}
size_t GetAdditionalLocalSocketCount() {
-#if ADB_HOST
// dummy socket installed in PrepareThread() + fdevent_run_on_main_thread socket
return 2;
-#else
- // dummy socket + fdevent_run_on_main_thread + fdevent_subproc_setup() sockets
- return 3;
-#endif
}
- void TerminateThread(std::thread& thread) {
+ void TerminateThread() {
fdevent_terminate_loop();
ASSERT_TRUE(WriteFdExactly(dummy, "", 1));
- thread.join();
+ thread_.join();
ASSERT_EQ(0, adb_close(dummy));
}
+
+ std::thread thread_;
};
diff --git a/adb/file_sync_protocol.h b/adb/file_sync_protocol.h
new file mode 100644
index 0000000..108639a
--- /dev/null
+++ b/adb/file_sync_protocol.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2007 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.
+ */
+
+#pragma once
+
+#define MKID(a, b, c, d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
+
+#define ID_LSTAT_V1 MKID('S', 'T', 'A', 'T')
+#define ID_STAT_V2 MKID('S', 'T', 'A', '2')
+#define ID_LSTAT_V2 MKID('L', 'S', 'T', '2')
+#define ID_LIST MKID('L', 'I', 'S', 'T')
+#define ID_SEND MKID('S', 'E', 'N', 'D')
+#define ID_RECV MKID('R', 'E', 'C', 'V')
+#define ID_DENT MKID('D', 'E', 'N', 'T')
+#define ID_DONE MKID('D', 'O', 'N', 'E')
+#define ID_DATA MKID('D', 'A', 'T', 'A')
+#define ID_OKAY MKID('O', 'K', 'A', 'Y')
+#define ID_FAIL MKID('F', 'A', 'I', 'L')
+#define ID_QUIT MKID('Q', 'U', 'I', 'T')
+
+struct SyncRequest {
+ uint32_t id; // ID_STAT, et cetera.
+ uint32_t path_length; // <= 1024
+ // Followed by 'path_length' bytes of path (not NUL-terminated).
+} __attribute__((packed));
+
+union syncmsg {
+ struct __attribute__((packed)) {
+ uint32_t id;
+ uint32_t mode;
+ uint32_t size;
+ uint32_t time;
+ } stat_v1;
+ struct __attribute__((packed)) {
+ uint32_t id;
+ uint32_t error;
+ uint64_t dev;
+ uint64_t ino;
+ uint32_t mode;
+ uint32_t nlink;
+ uint32_t uid;
+ uint32_t gid;
+ uint64_t size;
+ int64_t atime;
+ int64_t mtime;
+ int64_t ctime;
+ } stat_v2;
+ struct __attribute__((packed)) {
+ uint32_t id;
+ uint32_t mode;
+ uint32_t size;
+ uint32_t time;
+ uint32_t namelen;
+ } dent;
+ struct __attribute__((packed)) {
+ uint32_t id;
+ uint32_t size;
+ } data;
+ struct __attribute__((packed)) {
+ uint32_t id;
+ uint32_t msglen;
+ } status;
+};
+
+#define SYNC_DATA_MAX (64 * 1024)
diff --git a/adb/file_sync_service.h b/adb/file_sync_service.h
deleted file mode 100644
index 6606efd..0000000
--- a/adb/file_sync_service.h
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright (C) 2007 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.
- */
-
-#ifndef _FILE_SYNC_SERVICE_H_
-#define _FILE_SYNC_SERVICE_H_
-
-#include <string>
-#include <vector>
-
-#define MKID(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
-
-#define ID_LSTAT_V1 MKID('S','T','A','T')
-#define ID_STAT_V2 MKID('S','T','A','2')
-#define ID_LSTAT_V2 MKID('L','S','T','2')
-#define ID_LIST MKID('L','I','S','T')
-#define ID_SEND MKID('S','E','N','D')
-#define ID_RECV MKID('R','E','C','V')
-#define ID_DENT MKID('D','E','N','T')
-#define ID_DONE MKID('D','O','N','E')
-#define ID_DATA MKID('D','A','T','A')
-#define ID_OKAY MKID('O','K','A','Y')
-#define ID_FAIL MKID('F','A','I','L')
-#define ID_QUIT MKID('Q','U','I','T')
-
-struct SyncRequest {
- uint32_t id; // ID_STAT, et cetera.
- uint32_t path_length; // <= 1024
- // Followed by 'path_length' bytes of path (not NUL-terminated).
-} __attribute__((packed)) ;
-
-union syncmsg {
- struct __attribute__((packed)) {
- uint32_t id;
- uint32_t mode;
- uint32_t size;
- uint32_t time;
- } stat_v1;
- struct __attribute__((packed)) {
- uint32_t id;
- uint32_t error;
- uint64_t dev;
- uint64_t ino;
- uint32_t mode;
- uint32_t nlink;
- uint32_t uid;
- uint32_t gid;
- uint64_t size;
- int64_t atime;
- int64_t mtime;
- int64_t ctime;
- } stat_v2;
- struct __attribute__((packed)) {
- uint32_t id;
- uint32_t mode;
- uint32_t size;
- uint32_t time;
- uint32_t namelen;
- } dent;
- struct __attribute__((packed)) {
- uint32_t id;
- uint32_t size;
- } data;
- struct __attribute__((packed)) {
- uint32_t id;
- uint32_t msglen;
- } status;
-};
-
-void file_sync_service(int fd, void* cookie);
-bool do_sync_ls(const char* path);
-bool do_sync_push(const std::vector<const char*>& srcs, const char* dst, bool sync);
-bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst,
- bool copy_attrs, const char* name=nullptr);
-
-bool do_sync_sync(const std::string& lpath, const std::string& rpath, bool list_only);
-
-#define SYNC_DATA_MAX (64*1024)
-
-#endif
diff --git a/adb/range.h b/adb/range.h
deleted file mode 100644
index 7a0b822..0000000
--- a/adb/range.h
+++ /dev/null
@@ -1,65 +0,0 @@
-#pragma once
-
-/*
- * Copyright (C) 2018 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 <string>
-
-#include <android-base/logging.h>
-
-struct Range {
- explicit Range(std::string data) : data_(std::move(data)) {}
-
- Range(const Range& copy) = delete;
- Range& operator=(const Range& copy) = delete;
-
- Range(Range&& move) = default;
- Range& operator=(Range&& move) = default;
-
- bool empty() const {
- return size() == 0;
- }
-
- size_t size() const {
- return data_.size() - begin_offset_ - end_offset_;
- };
-
- void drop_front(size_t n) {
- CHECK_GE(size(), n);
- begin_offset_ += n;
- }
-
- void drop_end(size_t n) {
- CHECK_GE(size(), n);
- end_offset_ += n;
- }
-
- char* data() {
- return &data_[0] + begin_offset_;
- }
-
- std::string::iterator begin() {
- return data_.begin() + begin_offset_;
- }
-
- std::string::iterator end() {
- return data_.end() - end_offset_;
- }
-
- std::string data_;
- size_t begin_offset_ = 0;
- size_t end_offset_ = 0;
-};
diff --git a/adb/services.cpp b/adb/services.cpp
index fe74eb6..4b033bd 100644
--- a/adb/services.cpp
+++ b/adb/services.cpp
@@ -24,228 +24,40 @@
#include <stdlib.h>
#include <string.h>
-#ifndef _WIN32
-#include <netdb.h>
-#include <netinet/in.h>
-#include <sys/ioctl.h>
-#include <unistd.h>
-#endif
-
#include <thread>
-
-#include <android-base/file.h>
-#include <android-base/parsenetaddress.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <cutils/sockets.h>
-#if !ADB_HOST
-#include <android-base/properties.h>
-#include <bootloader_message/bootloader_message.h>
-#include <cutils/android_reboot.h>
-#include <log/log_properties.h>
-#endif
-
#include "adb.h"
#include "adb_io.h"
+#include "adb_unique_fd.h"
#include "adb_utils.h"
-#include "file_sync_service.h"
-#include "remount_service.h"
#include "services.h"
-#include "shell_service.h"
#include "socket_spec.h"
#include "sysdeps.h"
#include "transport.h"
-struct stinfo {
- const char* service_name;
- void (*func)(int fd, void *cookie);
- int fd;
- void *cookie;
-};
+namespace {
-static void service_bootstrap_func(void* x) {
- stinfo* sti = reinterpret_cast<stinfo*>(x);
- adb_thread_setname(android::base::StringPrintf("%s svc %d", sti->service_name, sti->fd));
- sti->func(sti->fd, sti->cookie);
- free(sti);
+void service_bootstrap_func(std::string service_name, std::function<void(unique_fd)> func,
+ unique_fd fd) {
+ adb_thread_setname(android::base::StringPrintf("%s svc %d", service_name.c_str(), fd.get()));
+ func(std::move(fd));
}
-#if !ADB_HOST
+} // namespace
-void restart_root_service(int fd, void *cookie) {
- if (getuid() == 0) {
- WriteFdExactly(fd, "adbd is already running as root\n");
- adb_close(fd);
- } else {
- if (!__android_log_is_debuggable()) {
- WriteFdExactly(fd, "adbd cannot run as root in production builds\n");
- adb_close(fd);
- return;
- }
-
- android::base::SetProperty("service.adb.root", "1");
- WriteFdExactly(fd, "restarting adbd as root\n");
- adb_close(fd);
- }
-}
-
-void restart_unroot_service(int fd, void *cookie) {
- if (getuid() != 0) {
- WriteFdExactly(fd, "adbd not running as root\n");
- adb_close(fd);
- } else {
- android::base::SetProperty("service.adb.root", "0");
- WriteFdExactly(fd, "restarting adbd as non root\n");
- adb_close(fd);
- }
-}
-
-void restart_tcp_service(int fd, void *cookie) {
- int port = (int) (uintptr_t) cookie;
- if (port <= 0) {
- WriteFdFmt(fd, "invalid port %d\n", port);
- adb_close(fd);
- return;
- }
-
- android::base::SetProperty("service.adb.tcp.port", android::base::StringPrintf("%d", port));
- WriteFdFmt(fd, "restarting in TCP mode port: %d\n", port);
- adb_close(fd);
-}
-
-void restart_usb_service(int fd, void *cookie) {
- android::base::SetProperty("service.adb.tcp.port", "0");
- WriteFdExactly(fd, "restarting in USB mode\n");
- adb_close(fd);
-}
-
-static bool reboot_service_impl(int fd, const char* arg) {
- const char* reboot_arg = arg;
- bool auto_reboot = false;
-
- if (strcmp(reboot_arg, "sideload-auto-reboot") == 0) {
- auto_reboot = true;
- reboot_arg = "sideload";
- }
-
- // It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot"
- // in the command file.
- if (strcmp(reboot_arg, "sideload") == 0) {
- if (getuid() != 0) {
- WriteFdExactly(fd, "'adb root' is required for 'adb reboot sideload'.\n");
- return false;
- }
-
- const std::vector<std::string> options = {
- auto_reboot ? "--sideload_auto_reboot" : "--sideload"
- };
- std::string err;
- if (!write_bootloader_message(options, &err)) {
- D("Failed to set bootloader message: %s", err.c_str());
- return false;
- }
-
- reboot_arg = "recovery";
- }
-
- sync();
-
- if (!reboot_arg || !reboot_arg[0]) reboot_arg = "adb";
- std::string reboot_string = android::base::StringPrintf("reboot,%s", reboot_arg);
- if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) {
- WriteFdFmt(fd, "reboot (%s) failed\n", reboot_string.c_str());
- return false;
- }
-
- return true;
-}
-
-void reboot_service(int fd, void* arg) {
- if (reboot_service_impl(fd, static_cast<const char*>(arg))) {
- // Don't return early. Give the reboot command time to take effect
- // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
- while (true) {
- pause();
- }
- }
-
- free(arg);
- adb_close(fd);
-}
-
-static void reconnect_service(int fd, void* arg) {
- WriteFdExactly(fd, "done");
- adb_close(fd);
- atransport* t = static_cast<atransport*>(arg);
- kick_transport(t);
-}
-
-int reverse_service(const char* command) {
- int s[2];
- if (adb_socketpair(s)) {
- PLOG(ERROR) << "cannot create service socket pair.";
- return -1;
- }
- VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1];
- if (handle_forward_request(command, kTransportAny, nullptr, 0, s[1]) < 0) {
- SendFail(s[1], "not a reverse forwarding command");
- }
- adb_close(s[1]);
- return s[0];
-}
-
-// Shell service string can look like:
-// shell[,arg1,arg2,...]:[command]
-static int ShellService(const std::string& args, const atransport* transport) {
- size_t delimiter_index = args.find(':');
- if (delimiter_index == std::string::npos) {
- LOG(ERROR) << "No ':' found in shell service arguments: " << args;
- return -1;
- }
-
- const std::string service_args = args.substr(0, delimiter_index);
- const std::string command = args.substr(delimiter_index + 1);
-
- // Defaults:
- // PTY for interactive, raw for non-interactive.
- // No protocol.
- // $TERM set to "dumb".
- SubprocessType type(command.empty() ? SubprocessType::kPty
- : SubprocessType::kRaw);
- SubprocessProtocol protocol = SubprocessProtocol::kNone;
- std::string terminal_type = "dumb";
-
- for (const std::string& arg : android::base::Split(service_args, ",")) {
- if (arg == kShellServiceArgRaw) {
- type = SubprocessType::kRaw;
- } else if (arg == kShellServiceArgPty) {
- type = SubprocessType::kPty;
- } else if (arg == kShellServiceArgShellProtocol) {
- protocol = SubprocessProtocol::kShell;
- } else if (android::base::StartsWith(arg, "TERM=")) {
- terminal_type = arg.substr(5);
- } else if (!arg.empty()) {
- // This is not an error to allow for future expansion.
- LOG(WARNING) << "Ignoring unknown shell service argument: " << arg;
- }
- }
-
- return StartSubprocess(command.c_str(), terminal_type.c_str(), type, protocol);
-}
-
-#endif // !ADB_HOST
-
-static int create_service_thread(const char* service_name, void (*func)(int, void*), void* cookie) {
+unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func) {
int s[2];
if (adb_socketpair(s)) {
printf("cannot create service socket pair\n");
- return -1;
+ return unique_fd();
}
D("socketpair: (%d,%d)", s[0], s[1]);
#if !ADB_HOST
- if (func == &file_sync_service) {
+ if (strcmp(service_name, "sync") == 0) {
// Set file sync service socket to maximum size
int max_buf = LINUX_MAX_SOCKET_SIZE;
adb_setsockopt(s[0], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf));
@@ -253,22 +65,13 @@
}
#endif // !ADB_HOST
- stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
- if (sti == nullptr) {
- fatal("cannot allocate stinfo");
- }
- sti->service_name = service_name;
- sti->func = func;
- sti->cookie = cookie;
- sti->fd = s[1];
-
- std::thread(service_bootstrap_func, sti).detach();
+ std::thread(service_bootstrap_func, service_name, func, unique_fd(s[1])).detach();
D("service thread started, %d:%d",s[0], s[1]);
- return s[0];
+ return unique_fd(s[0]);
}
-int service_to_fd(const char* name, const atransport* transport) {
+int service_to_fd(const char* name, atransport* transport) {
int ret = -1;
if (is_socket_spec(name)) {
@@ -277,58 +80,12 @@
if (ret < 0) {
LOG(ERROR) << "failed to connect to socket '" << name << "': " << error;
}
+ } else {
#if !ADB_HOST
- } else if(!strncmp("dev:", name, 4)) {
- ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
- } else if(!strncmp(name, "framebuffer:", 12)) {
- ret = create_service_thread("fb", framebuffer_service, nullptr);
- } else if (!strncmp(name, "jdwp:", 5)) {
- ret = create_jdwp_connection_fd(atoi(name+5));
- } else if(!strncmp(name, "shell", 5)) {
- ret = ShellService(name + 5, transport);
- } else if(!strncmp(name, "exec:", 5)) {
- ret = StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
- } else if(!strncmp(name, "sync:", 5)) {
- ret = create_service_thread("sync", file_sync_service, nullptr);
- } else if(!strncmp(name, "remount:", 8)) {
- ret = create_service_thread("remount", remount_service, nullptr);
- } else if(!strncmp(name, "reboot:", 7)) {
- void* arg = strdup(name + 7);
- if (arg == NULL) return -1;
- ret = create_service_thread("reboot", reboot_service, arg);
- if (ret < 0) free(arg);
- } else if(!strncmp(name, "root:", 5)) {
- ret = create_service_thread("root", restart_root_service, nullptr);
- } else if(!strncmp(name, "unroot:", 7)) {
- ret = create_service_thread("unroot", restart_unroot_service, nullptr);
- } else if(!strncmp(name, "backup:", 7)) {
- ret = StartSubprocess(android::base::StringPrintf("/system/bin/bu backup %s",
- (name + 7)).c_str(),
- nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
- } else if(!strncmp(name, "restore:", 8)) {
- ret = StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw,
- SubprocessProtocol::kNone);
- } else if(!strncmp(name, "tcpip:", 6)) {
- int port;
- if (sscanf(name + 6, "%d", &port) != 1) {
- return -1;
- }
- ret = create_service_thread("tcp", restart_tcp_service, reinterpret_cast<void*>(port));
- } else if(!strncmp(name, "usb:", 4)) {
- ret = create_service_thread("usb", restart_usb_service, nullptr);
- } else if (!strncmp(name, "reverse:", 8)) {
- ret = reverse_service(name + 8);
- } else if(!strncmp(name, "disable-verity:", 15)) {
- ret = create_service_thread("verity-on", set_verity_enabled_state_service,
- reinterpret_cast<void*>(0));
- } else if(!strncmp(name, "enable-verity:", 15)) {
- ret = create_service_thread("verity-off", set_verity_enabled_state_service,
- reinterpret_cast<void*>(1));
- } else if (!strcmp(name, "reconnect")) {
- ret = create_service_thread("reconnect", reconnect_service,
- const_cast<atransport*>(transport));
+ ret = daemon_service_to_fd(name, transport).release();
#endif
}
+
if (ret >= 0) {
close_on_exec(ret);
}
@@ -351,7 +108,7 @@
while (true) {
bool is_ambiguous = false;
std::string error = "unknown error";
- const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : NULL;
+ const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : nullptr;
atransport* t = acquire_one_transport(sinfo->transport_type, serial, sinfo->transport_id,
&is_ambiguous, &error);
if (t != nullptr && (sinfo->state == kCsAny || sinfo->state == t->GetConnectionState())) {
@@ -388,8 +145,8 @@
return;
}
- int console_port = strtol(pieces[0].c_str(), NULL, 0);
- int adb_port = strtol(pieces[1].c_str(), NULL, 0);
+ int console_port = strtol(pieces[0].c_str(), nullptr, 0);
+ int adb_port = strtol(pieces[1].c_str(), nullptr, 0);
if (console_port <= 0 || adb_port <= 0) {
*response = android::base::StringPrintf("Invalid port numbers: %s", port_spec.c_str());
return;
@@ -419,19 +176,16 @@
}
}
-static void connect_service(int fd, void* data) {
- char* host = reinterpret_cast<char*>(data);
+static void connect_service(unique_fd fd, std::string host) {
std::string response;
- if (!strncmp(host, "emu:", 4)) {
- connect_emulator(host + 4, &response);
+ if (!strncmp(host.c_str(), "emu:", 4)) {
+ connect_emulator(host.c_str() + 4, &response);
} else {
- connect_device(host, &response);
+ connect_device(host.c_str(), &response);
}
- free(host);
// Send response for emulator and device
- SendProtocolString(fd, response);
- adb_close(fd);
+ SendProtocolString(fd.get(), response);
}
#endif
@@ -444,7 +198,7 @@
} else if (android::base::StartsWith(name, "wait-for-")) {
name += strlen("wait-for-");
- std::unique_ptr<state_info> sinfo(new state_info);
+ std::unique_ptr<state_info> sinfo = std::make_unique<state_info>();
if (sinfo == nullptr) {
fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno));
return nullptr;
@@ -480,19 +234,20 @@
return nullptr;
}
- int fd = create_service_thread("wait", wait_for_state, sinfo.get());
+ int fd = create_service_thread(
+ "wait", std::bind(wait_for_state, std::placeholders::_1, sinfo.get()))
+ .release();
if (fd != -1) {
sinfo.release();
}
return create_local_socket(fd);
} else if (!strncmp(name, "connect:", 8)) {
- char* host = strdup(name + 8);
- int fd = create_service_thread("connect", connect_service, host);
- if (fd == -1) {
- free(host);
- }
+ std::string host(name + strlen("connect:"));
+ int fd = create_service_thread("connect",
+ std::bind(connect_service, std::placeholders::_1, host))
+ .release();
return create_local_socket(fd);
}
- return NULL;
+ return nullptr;
}
#endif /* ADB_HOST */
diff --git a/adb/services.h b/adb/services.h
index 0428ca4..0ce25ba 100644
--- a/adb/services.h
+++ b/adb/services.h
@@ -17,8 +17,11 @@
#ifndef SERVICES_H_
#define SERVICES_H_
+#include "adb_unique_fd.h"
+
constexpr char kShellServiceArgRaw[] = "raw";
constexpr char kShellServiceArgPty[] = "pty";
constexpr char kShellServiceArgShellProtocol[] = "v2";
+unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func);
#endif // SERVICES_H_
diff --git a/adb/shell_service.h b/adb/shell_protocol.h
similarity index 79%
rename from adb/shell_service.h
rename to adb/shell_protocol.h
index e3d676a..2c82689 100644
--- a/adb/shell_service.h
+++ b/adb/shell_protocol.h
@@ -14,16 +14,7 @@
* limitations under the License.
*/
-// This file contains classes and functionality to launch shell subprocesses
-// in adbd and communicate between those subprocesses and the adb client.
-//
-// The main features exposed here are:
-// 1. A ShellPacket class to wrap data in a simple protocol. Both adbd and
-// the adb client use this class to transmit data between them.
-// 2. Functions to launch a subprocess on the adbd side.
-
-#ifndef SHELL_SERVICE_H_
-#define SHELL_SERVICE_H_
+#pragma once
#include <stdint.h>
@@ -124,26 +115,3 @@
DISALLOW_COPY_AND_ASSIGN(ShellProtocol);
};
-
-#if !ADB_HOST
-
-enum class SubprocessType {
- kPty,
- kRaw,
-};
-
-enum class SubprocessProtocol {
- kNone,
- kShell,
-};
-
-// Forks and starts a new shell subprocess. If |name| is empty an interactive
-// shell is started, otherwise |name| is executed non-interactively.
-//
-// Returns an open FD connected to the subprocess or -1 on failure.
-int StartSubprocess(const char* name, const char* terminal_type,
- SubprocessType type, SubprocessProtocol protocol);
-
-#endif // !ADB_HOST
-
-#endif // SHELL_SERVICE_H_
diff --git a/adb/shell_service_protocol.cpp b/adb/shell_service_protocol.cpp
index 623629c..13b66ec 100644
--- a/adb/shell_service_protocol.cpp
+++ b/adb/shell_service_protocol.cpp
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "shell_service.h"
+#include "shell_protocol.h"
#include <string.h>
diff --git a/adb/shell_service_protocol_test.cpp b/adb/shell_service_protocol_test.cpp
index b0fa3ed..a10b5c0 100644
--- a/adb/shell_service_protocol_test.cpp
+++ b/adb/shell_service_protocol_test.cpp
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "shell_service.h"
+#include "shell_protocol.h"
#include <gtest/gtest.h>
diff --git a/adb/socket.h b/adb/socket.h
index 2f09080..0905aab 100644
--- a/adb/socket.h
+++ b/adb/socket.h
@@ -24,9 +24,8 @@
#include <string>
#include "fdevent.h"
-#include "range.h"
+#include "types.h"
-struct apacket;
class atransport;
/* An asocket represents one half of a connection between a local and
@@ -59,11 +58,11 @@
* us to our fd event system. For remote asockets
* these fields are not used.
*/
- fdevent fde = {};
- int fd = 0;
+ fdevent* fde = nullptr;
+ int fd = -1;
// queue of data waiting to be written
- std::deque<Range> packet_queue;
+ IOVector packet_queue;
std::string smart_socket_data;
@@ -73,7 +72,7 @@
* peer->ready() when we once again are ready to
* receive data.
*/
- int (*enqueue)(asocket* s, std::string data) = nullptr;
+ int (*enqueue)(asocket* s, apacket::payload_type data) = nullptr;
/* ready is called by the peer when it is ready for
* us to send data via enqueue again
@@ -104,8 +103,7 @@
void close_all_sockets(atransport *t);
asocket *create_local_socket(int fd);
-asocket *create_local_service_socket(const char* destination,
- const atransport* transport);
+asocket* create_local_service_socket(const char* destination, atransport* transport);
asocket *create_remote_socket(unsigned id, atransport *t);
void connect_to_remote(asocket *s, const char *destination);
diff --git a/adb/socket_test.cpp b/adb/socket_test.cpp
index 04ad6f3..04214a2 100644
--- a/adb/socket_test.cpp
+++ b/adb/socket_test.cpp
@@ -42,8 +42,6 @@
class LocalSocketTest : public FdeventTest {};
-constexpr auto SLEEP_FOR_FDEVENT = 100ms;
-
TEST_F(LocalSocketTest, smoke) {
// Join two socketpairs with a chain of intermediate socketpairs.
int first[2];
@@ -84,7 +82,6 @@
connect(prev_tail, end);
PrepareThread();
- std::thread thread(fdevent_loop);
for (size_t i = 0; i < MESSAGE_LOOP_COUNT; ++i) {
std::string read_buffer = MESSAGE;
@@ -98,9 +95,9 @@
ASSERT_EQ(0, adb_close(last[1]));
// Wait until the local sockets are closed.
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+ WaitForFdeventLoop();
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
- TerminateThread(thread);
+ TerminateThread();
}
struct CloseWithPacketArg {
@@ -109,28 +106,39 @@
int cause_close_fd;
};
-static void CloseWithPacketThreadFunc(CloseWithPacketArg* arg) {
- asocket* s = create_local_socket(arg->socket_fd);
- ASSERT_TRUE(s != nullptr);
- arg->bytes_written = 0;
- while (true) {
- std::string data;
- data.resize(MAX_PAYLOAD);
- arg->bytes_written += data.size();
- int ret = s->enqueue(s, std::move(data));
- if (ret == 1) {
- // The writer has one packet waiting to send.
- break;
+static void CreateCloser(CloseWithPacketArg* arg) {
+ fdevent_run_on_main_thread([arg]() {
+ asocket* s = create_local_socket(arg->socket_fd);
+ ASSERT_TRUE(s != nullptr);
+ arg->bytes_written = 0;
+
+ // On platforms that implement sockets via underlying sockets (e.g. Wine),
+ // a socket can appear to be full, and then become available for writes
+ // again without read being called on the other end. Loop and sleep after
+ // each write to give the underlying implementation time to flush.
+ bool socket_filled = false;
+ for (int i = 0; i < 128; ++i) {
+ apacket::payload_type data;
+ data.resize(MAX_PAYLOAD);
+ arg->bytes_written += data.size();
+ int ret = s->enqueue(s, std::move(data));
+ if (ret == 1) {
+ socket_filled = true;
+ break;
+ }
+ ASSERT_NE(-1, ret);
+
+ std::this_thread::sleep_for(250ms);
}
- }
+ ASSERT_TRUE(socket_filled);
- asocket* cause_close_s = create_local_socket(arg->cause_close_fd);
- ASSERT_TRUE(cause_close_s != nullptr);
- cause_close_s->peer = s;
- s->peer = cause_close_s;
- cause_close_s->ready(cause_close_s);
-
- fdevent_loop();
+ asocket* cause_close_s = create_local_socket(arg->cause_close_fd);
+ ASSERT_TRUE(cause_close_s != nullptr);
+ cause_close_s->peer = s;
+ s->peer = cause_close_s;
+ cause_close_s->ready(cause_close_s);
+ });
+ WaitForFdeventLoop();
}
// This test checks if we can close local socket in the following situation:
@@ -147,16 +155,17 @@
arg.cause_close_fd = cause_close_fd[1];
PrepareThread();
- std::thread thread(CloseWithPacketThreadFunc, &arg);
- // Wait until the fdevent_loop() starts.
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+ CreateCloser(&arg);
+
ASSERT_EQ(0, adb_close(cause_close_fd[0]));
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+
+ WaitForFdeventLoop();
EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
ASSERT_EQ(0, adb_close(socket_fd[0]));
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+
+ WaitForFdeventLoop();
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
- TerminateThread(thread);
+ TerminateThread();
}
// This test checks if we can read packets from a closing local socket.
@@ -170,11 +179,12 @@
arg.cause_close_fd = cause_close_fd[1];
PrepareThread();
- std::thread thread(CloseWithPacketThreadFunc, &arg);
- // Wait until the fdevent_loop() starts.
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+ CreateCloser(&arg);
+
+ WaitForFdeventLoop();
ASSERT_EQ(0, adb_close(cause_close_fd[0]));
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+
+ WaitForFdeventLoop();
EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
// Verify if we can read successfully.
@@ -183,9 +193,9 @@
ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
ASSERT_EQ(0, adb_close(socket_fd[0]));
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+ WaitForFdeventLoop();
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
- TerminateThread(thread);
+ TerminateThread();
}
// This test checks if we can close local socket in the following situation:
@@ -202,15 +212,52 @@
arg.cause_close_fd = cause_close_fd[1];
PrepareThread();
- std::thread thread(CloseWithPacketThreadFunc, &arg);
- // Wait until the fdevent_loop() starts.
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+ CreateCloser(&arg);
+
+ WaitForFdeventLoop();
EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
ASSERT_EQ(0, adb_close(socket_fd[0]));
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+ WaitForFdeventLoop();
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
- TerminateThread(thread);
+ TerminateThread();
+}
+
+// Ensure that if we fail to write output to an fd, we will still flush data coming from it.
+TEST_F(LocalSocketTest, flush_after_shutdown) {
+ int head_fd[2];
+ int tail_fd[2];
+ ASSERT_EQ(0, adb_socketpair(head_fd));
+ ASSERT_EQ(0, adb_socketpair(tail_fd));
+
+ asocket* head = create_local_socket(head_fd[1]);
+ asocket* tail = create_local_socket(tail_fd[1]);
+
+ head->peer = tail;
+ head->ready(head);
+
+ tail->peer = head;
+ tail->ready(tail);
+
+ PrepareThread();
+
+ EXPECT_TRUE(WriteFdExactly(head_fd[0], "foo", 3));
+
+ EXPECT_EQ(0, adb_shutdown(head_fd[0], SHUT_RD));
+ const char* str = "write succeeds, but local_socket will fail to write";
+ EXPECT_TRUE(WriteFdExactly(tail_fd[0], str, strlen(str)));
+ EXPECT_TRUE(WriteFdExactly(head_fd[0], "bar", 3));
+
+ char buf[6];
+ EXPECT_TRUE(ReadFdExactly(tail_fd[0], buf, 6));
+ EXPECT_EQ(0, memcmp(buf, "foobar", 6));
+
+ adb_close(head_fd[0]);
+ adb_close(tail_fd[0]);
+
+ WaitForFdeventLoop();
+ ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
+ TerminateThread();
}
#if defined(__linux__)
@@ -219,21 +266,10 @@
std::string error;
int fd = network_loopback_client(5038, SOCK_STREAM, &error);
ASSERT_GE(fd, 0) << error;
- std::this_thread::sleep_for(200ms);
+ std::this_thread::sleep_for(1s);
ASSERT_EQ(0, adb_close(fd));
}
-struct CloseRdHupSocketArg {
- int socket_fd;
-};
-
-static void CloseRdHupSocketThreadFunc(CloseRdHupSocketArg* arg) {
- asocket* s = create_local_socket(arg->socket_fd);
- ASSERT_TRUE(s != nullptr);
-
- fdevent_loop();
-}
-
// This test checks if we can close sockets in CLOSE_WAIT state.
TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
std::string error;
@@ -244,22 +280,23 @@
int accept_fd = adb_socket_accept(listen_fd, nullptr, nullptr);
ASSERT_GE(accept_fd, 0);
- CloseRdHupSocketArg arg;
- arg.socket_fd = accept_fd;
PrepareThread();
- std::thread thread(CloseRdHupSocketThreadFunc, &arg);
- // Wait until the fdevent_loop() starts.
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+ fdevent_run_on_main_thread([accept_fd]() {
+ asocket* s = create_local_socket(accept_fd);
+ ASSERT_TRUE(s != nullptr);
+ });
+
+ WaitForFdeventLoop();
EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
// Wait until the client closes its socket.
client_thread.join();
- std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
+ WaitForFdeventLoop();
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
- TerminateThread(thread);
+ TerminateThread();
}
#endif // defined(__linux__)
diff --git a/adb/sockets.cpp b/adb/sockets.cpp
index 0007fed..1534792 100644
--- a/adb/sockets.cpp
+++ b/adb/sockets.cpp
@@ -37,8 +37,8 @@
#include "adb.h"
#include "adb_io.h"
-#include "range.h"
#include "transport.h"
+#include "types.h"
static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
static unsigned local_socket_next_id = 1;
@@ -106,68 +106,147 @@
}
}
-static int local_socket_enqueue(asocket* s, std::string data) {
- D("LS(%d): enqueue %zu", s->id, data.size());
+enum class SocketFlushResult {
+ Destroyed,
+ TryAgain,
+ Completed,
+};
- Range r(std::move(data));
-
- /* if there is already data queue'd, we will receive
- ** events when it's time to write. just add this to
- ** the tail
- */
+static SocketFlushResult local_socket_flush_incoming(asocket* s) {
if (!s->packet_queue.empty()) {
- goto enqueue;
+ std::vector<adb_iovec> iov = s->packet_queue.iovecs();
+ ssize_t rc = adb_writev(s->fd, iov.data(), iov.size());
+ if (rc > 0 && static_cast<size_t>(rc) == s->packet_queue.size()) {
+ s->packet_queue.clear();
+ } else if (rc > 0) {
+ // TODO: Implement a faster drop_front?
+ s->packet_queue.take_front(rc);
+ fdevent_add(s->fde, FDE_WRITE);
+ return SocketFlushResult::TryAgain;
+ } else if (rc == -1 && errno == EAGAIN) {
+ fdevent_add(s->fde, FDE_WRITE);
+ return SocketFlushResult::TryAgain;
+ } else {
+ // We failed to write, but it's possible that we can still read from the socket.
+ // Give that a try before giving up.
+ s->has_write_error = true;
+ }
}
- /* write as much as we can, until we
- ** would block or there is an error/eof
- */
- while (!r.empty()) {
- int rc = adb_write(s->fd, r.data(), r.size());
- if (rc > 0) {
- r.drop_front(rc);
+ // If we sent the last packet of a closing socket, we can now destroy it.
+ if (s->closing) {
+ s->close(s);
+ return SocketFlushResult::Destroyed;
+ }
+
+ fdevent_del(s->fde, FDE_WRITE);
+ return SocketFlushResult::Completed;
+}
+
+// Returns false if the socket has been closed and destroyed as a side-effect of this function.
+static bool local_socket_flush_outgoing(asocket* s) {
+ const size_t max_payload = s->get_max_payload();
+ apacket::payload_type data;
+ data.resize(max_payload);
+ char* x = &data[0];
+ size_t avail = max_payload;
+ int r = 0;
+ int is_eof = 0;
+
+ while (avail > 0) {
+ r = adb_read(s->fd, x, avail);
+ D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
+ r < 0 ? errno : 0, avail);
+ if (r == -1) {
+ if (errno == EAGAIN) {
+ break;
+ }
+ } else if (r > 0) {
+ avail -= r;
+ x += r;
continue;
}
- if (rc == 0 || errno != EAGAIN) {
- D("LS(%d): not ready, errno=%d: %s", s->id, errno, strerror(errno));
- s->has_write_error = true;
- s->close(s);
- return 1; /* not ready (error) */
- } else {
- // errno == EAGAIN
- break;
+ /* r = 0 or unhandled error */
+ is_eof = 1;
+ break;
+ }
+ D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d", s->id, s->fd, r, is_eof,
+ s->fde->force_eof);
+
+ if (avail != max_payload && s->peer) {
+ data.resize(max_payload - avail);
+
+ // s->peer->enqueue() may call s->close() and free s,
+ // so save variables for debug printing below.
+ unsigned saved_id = s->id;
+ int saved_fd = s->fd;
+ r = s->peer->enqueue(s->peer, std::move(data));
+ D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
+
+ if (r < 0) {
+ // Error return means they closed us as a side-effect and we must
+ // return immediately.
+ //
+ // Note that if we still have buffered packets, the socket will be
+ // placed on the closing socket list. This handler function will be
+ // called again to process FDE_WRITE events.
+ return false;
+ }
+
+ if (r > 0) {
+ /* if the remote cannot accept further events,
+ ** we disable notification of READs. They'll
+ ** be enabled again when we get a call to ready()
+ */
+ fdevent_del(s->fde, FDE_READ);
}
}
- if (r.empty()) {
- return 0; /* ready for more data */
+ // Don't allow a forced eof if data is still there.
+ if ((s->fde->force_eof && !r) || is_eof) {
+ D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde->force_eof);
+ s->close(s);
+ return false;
}
-enqueue:
- /* make sure we are notified when we can drain the queue */
- s->packet_queue.push_back(std::move(r));
- fdevent_add(&s->fde, FDE_WRITE);
+ return true;
+}
- return 1; /* not ready (backlog) */
+static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
+ D("LS(%d): enqueue %zu", s->id, data.size());
+
+ s->packet_queue.append(std::move(data));
+ switch (local_socket_flush_incoming(s)) {
+ case SocketFlushResult::Destroyed:
+ return -1;
+
+ case SocketFlushResult::TryAgain:
+ return 1;
+
+ case SocketFlushResult::Completed:
+ return 0;
+ }
+
+ return !s->packet_queue.empty();
}
static void local_socket_ready(asocket* s) {
/* far side is ready for data, pay attention to
readable events */
- fdevent_add(&s->fde, FDE_READ);
+ fdevent_add(s->fde, FDE_READ);
}
// be sure to hold the socket list lock when calling this
static void local_socket_destroy(asocket* s) {
int exit_on_close = s->exit_on_close;
- D("LS(%d): destroying fde.fd=%d", s->id, s->fde.fd);
+ D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
/* IMPORTANT: the remove closes the fd
** that belongs to this socket
*/
- fdevent_remove(&s->fde);
+ fdevent_destroy(s->fde);
remove_socket(s);
delete s;
@@ -209,11 +288,11 @@
*/
D("LS(%d): closing", s->id);
s->closing = 1;
- fdevent_del(&s->fde, FDE_READ);
+ fdevent_del(s->fde, FDE_READ);
remove_socket(s);
D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
local_socket_closing_list.push_back(s);
- CHECK_EQ(FDE_WRITE, s->fde.state & FDE_WRITE);
+ CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
}
static void local_socket_event_func(int fd, unsigned ev, void* _s) {
@@ -224,114 +303,21 @@
** in order to simplify the code.
*/
if (ev & FDE_WRITE) {
- while (!s->packet_queue.empty()) {
- Range& r = s->packet_queue.front();
- while (!r.empty()) {
- int rc = adb_write(fd, r.data(), r.size());
- if (rc == -1) {
- /* returning here is ok because FDE_READ will
- ** be processed in the next iteration loop
- */
- if (errno == EAGAIN) {
- return;
- }
- } else if (rc > 0) {
- r.drop_front(rc);
- continue;
- }
-
- D(" closing after write because rc=%d and errno is %d", rc, errno);
- s->has_write_error = true;
- s->close(s);
+ switch (local_socket_flush_incoming(s)) {
+ case SocketFlushResult::Destroyed:
return;
- }
- if (r.empty()) {
- s->packet_queue.pop_front();
- }
+ case SocketFlushResult::TryAgain:
+ break;
+
+ case SocketFlushResult::Completed:
+ s->peer->ready(s->peer);
+ break;
}
-
- /* if we sent the last packet of a closing socket,
- ** we can now destroy it.
- */
- if (s->closing) {
- D(" closing because 'closing' is set after write");
- s->close(s);
- return;
- }
-
- /* no more packets queued, so we can ignore
- ** writable events again and tell our peer
- ** to resume writing
- */
- fdevent_del(&s->fde, FDE_WRITE);
- s->peer->ready(s->peer);
}
if (ev & FDE_READ) {
- const size_t max_payload = s->get_max_payload();
- std::string data;
- data.resize(max_payload);
- char* x = &data[0];
- size_t avail = max_payload;
- int r = 0;
- int is_eof = 0;
-
- while (avail > 0) {
- r = adb_read(fd, x, avail);
- D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
- r < 0 ? errno : 0, avail);
- if (r == -1) {
- if (errno == EAGAIN) {
- break;
- }
- } else if (r > 0) {
- avail -= r;
- x += r;
- continue;
- }
-
- /* r = 0 or unhandled error */
- is_eof = 1;
- break;
- }
- D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d", s->id, s->fd, r, is_eof,
- s->fde.force_eof);
-
- if (avail != max_payload && s->peer) {
- data.resize(max_payload - avail);
-
- // s->peer->enqueue() may call s->close() and free s,
- // so save variables for debug printing below.
- unsigned saved_id = s->id;
- int saved_fd = s->fd;
- r = s->peer->enqueue(s->peer, std::move(data));
- D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
-
- if (r < 0) {
- /* error return means they closed us as a side-effect
- ** and we must return immediately.
- **
- ** note that if we still have buffered packets, the
- ** socket will be placed on the closing socket list.
- ** this handler function will be called again
- ** to process FDE_WRITE events.
- */
- return;
- }
-
- if (r > 0) {
- /* if the remote cannot accept further events,
- ** we disable notification of READs. They'll
- ** be enabled again when we get a call to ready()
- */
- fdevent_del(&s->fde, FDE_READ);
- }
- }
- /* Don't allow a forced eof if data is still there */
- if ((s->fde.force_eof && !r) || is_eof) {
- D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde.force_eof);
- s->close(s);
+ if (!local_socket_flush_outgoing(s)) {
return;
}
}
@@ -351,16 +337,16 @@
s->fd = fd;
s->enqueue = local_socket_enqueue;
s->ready = local_socket_ready;
- s->shutdown = NULL;
+ s->shutdown = nullptr;
s->close = local_socket_close;
install_local_socket(s);
- fdevent_install(&s->fde, fd, local_socket_event_func, s);
+ s->fde = fdevent_create(fd, local_socket_event_func, s);
D("LS(%d): created (fd=%d)", s->id, s->fd);
return s;
}
-asocket* create_local_service_socket(const char* name, const atransport* transport) {
+asocket* create_local_service_socket(const char* name, atransport* transport) {
#if !ADB_HOST
if (!strcmp(name, "jdwp")) {
return create_jdwp_service_socket();
@@ -397,7 +383,7 @@
s = host_service_to_socket(name, serial, transport_id);
- if (s != NULL) {
+ if (s != nullptr) {
D("LS(%d) bound to '%s'", s->id, name);
return s;
}
@@ -406,7 +392,7 @@
}
#endif /* ADB_HOST */
-static int remote_socket_enqueue(asocket* s, std::string data) {
+static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
apacket* p = get_apacket();
@@ -449,7 +435,7 @@
static void remote_socket_close(asocket* s) {
if (s->peer) {
- s->peer->peer = 0;
+ s->peer->peer = nullptr;
D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
s->peer->close(s->peer);
}
@@ -488,8 +474,7 @@
p->msg.arg0 = s->id;
// adbd expects a null-terminated string.
- p->payload = destination;
- p->payload.push_back('\0');
+ p->payload.assign(destination, destination + strlen(destination) + 1);
p->msg.data_length = p->payload.size();
if (p->msg.data_length > s->get_max_payload()) {
@@ -503,7 +488,7 @@
send the go-ahead message when they connect */
static void local_socket_ready_notify(asocket* s) {
s->ready = local_socket_ready;
- s->shutdown = NULL;
+ s->shutdown = nullptr;
s->close = local_socket_close;
SendOkay(s->fd);
s->ready(s);
@@ -514,7 +499,7 @@
connected (to avoid closing them without a status message) */
static void local_socket_close_notify(asocket* s) {
s->ready = local_socket_ready;
- s->shutdown = NULL;
+ s->shutdown = nullptr;
s->close = local_socket_close;
SendFail(s->fd, "closed");
s->close(s);
@@ -624,7 +609,7 @@
#endif // ADB_HOST
-static int smart_socket_enqueue(asocket* s, std::string data) {
+static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
#if ADB_HOST
char* service = nullptr;
char* serial = nullptr;
@@ -635,7 +620,8 @@
D("SS(%d): enqueue %zu", s->id, data.size());
if (s->smart_socket_data.empty()) {
- s->smart_socket_data = std::move(data);
+ // TODO: Make this an IOVector?
+ s->smart_socket_data.assign(data.begin(), data.end());
} else {
std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
}
@@ -699,13 +685,9 @@
if (service) {
asocket* s2;
- /* some requests are handled immediately -- in that
- ** case the handle_host_request() routine has sent
- ** the OKAY or FAIL message and all we have to do
- ** is clean up.
- */
- if (handle_host_request(service, type, serial, transport_id, s->peer->fd, s) == 0) {
- /* XXX fail message? */
+ // Some requests are handled immediately -- in that case the handle_host_request() routine
+ // has sent the OKAY or FAIL message and all we have to do is clean up.
+ if (handle_host_request(service, type, serial, transport_id, s->peer->fd, s)) {
D("SS(%d): handled host service '%s'", s->id, service);
goto fail;
}
@@ -720,7 +702,7 @@
** and tear down here.
*/
s2 = create_host_service_socket(service, serial, transport_id);
- if (s2 == 0) {
+ if (s2 == nullptr) {
D("SS(%d): couldn't create host service '%s'", s->id, service);
SendFail(s->peer->fd, "unknown host service");
goto fail;
@@ -740,7 +722,7 @@
s->peer->close = local_socket_close;
s->peer->peer = s2;
s2->peer = s->peer;
- s->peer = 0;
+ s->peer = nullptr;
D("SS(%d): okay", s->id);
s->close(s);
@@ -762,7 +744,7 @@
if (!s->transport) {
SendFail(s->peer->fd, "device offline (no transport)");
goto fail;
- } else if (s->transport->GetConnectionState() == kCsOffline) {
+ } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
/* if there's no remote we fail the connection
** right here and terminate it
*/
@@ -778,12 +760,12 @@
s->peer->ready = local_socket_ready_notify;
s->peer->shutdown = nullptr;
s->peer->close = local_socket_close_notify;
- s->peer->peer = 0;
+ s->peer->peer = nullptr;
/* give him our transport and upref it */
s->peer->transport = s->transport;
connect_to_remote(s->peer, s->smart_socket_data.data() + 4);
- s->peer = 0;
+ s->peer = nullptr;
s->close(s);
return 1;
@@ -803,9 +785,9 @@
static void smart_socket_close(asocket* s) {
D("SS(%d): closed", s->id);
if (s->peer) {
- s->peer->peer = 0;
+ s->peer->peer = nullptr;
s->peer->close(s->peer);
- s->peer = 0;
+ s->peer = nullptr;
}
delete s;
}
@@ -815,7 +797,7 @@
asocket* s = new asocket();
s->enqueue = smart_socket_enqueue;
s->ready = smart_socket_ready;
- s->shutdown = NULL;
+ s->shutdown = nullptr;
s->close = smart_socket_close;
D("SS(%d)", s->id);
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index 307be6d..f2911e0 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -39,19 +39,6 @@
#include "sysdeps/network.h"
#include "sysdeps/stat.h"
-// Some printf-like functions are implemented in terms of
-// android::base::StringAppendV, so they should use the same attribute for
-// compile-time format string checking. On Windows, if the mingw version of
-// vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd
-// and PRIu64 (and related) to be recognized by the compile-time checking.
-#define ADB_FORMAT_ARCHETYPE __printf__
-#ifdef __USE_MINGW_ANSI_STDIO
-#if __USE_MINGW_ANSI_STDIO
-#undef ADB_FORMAT_ARCHETYPE
-#define ADB_FORMAT_ARCHETYPE gnu_printf
-#endif
-#endif
-
#ifdef _WIN32
// Clang-only nullability specifiers
@@ -92,11 +79,6 @@
return 0;
}
-static __inline__ unsigned long adb_thread_id()
-{
- return GetCurrentThreadId();
-}
-
static __inline__ void close_on_exec(int fd)
{
/* nothing really */
@@ -111,14 +93,14 @@
#define mkdir ___xxx_mkdir
// See the comments for the !defined(_WIN32) versions of adb_*().
-extern int adb_open(const char* path, int options);
-extern int adb_creat(const char* path, int mode);
-extern int adb_read(int fd, void* buf, int len);
-extern int adb_write(int fd, const void* buf, int len);
-extern int adb_lseek(int fd, int pos, int where);
-extern int adb_shutdown(int fd);
-extern int adb_close(int fd);
-extern int adb_register_socket(SOCKET s);
+extern int adb_open(const char* path, int options);
+extern int adb_creat(const char* path, int mode);
+extern int adb_read(int fd, void* buf, int len);
+extern int adb_write(int fd, const void* buf, int len);
+extern int adb_lseek(int fd, int pos, int where);
+extern int adb_shutdown(int fd, int direction = SHUT_RDWR);
+extern int adb_close(int fd);
+extern int adb_register_socket(SOCKET s);
// See the comments for the !defined(_WIN32) version of unix_close().
static __inline__ int unix_close(int fd)
@@ -217,14 +199,12 @@
extern int adb_utime(const char *, struct utimbuf *);
extern int adb_chmod(const char *, int);
-extern int adb_vfprintf(FILE *stream, const char *format, va_list ap)
- __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0)));
-extern int adb_vprintf(const char *format, va_list ap)
- __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 0)));
-extern int adb_fprintf(FILE *stream, const char *format, ...)
- __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3)));
-extern int adb_printf(const char *format, ...)
- __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2)));
+extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
+ __attribute__((__format__(__printf__, 2, 0)));
+extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
+extern int adb_fprintf(FILE* stream, const char* format, ...)
+ __attribute__((__format__(__printf__, 2, 3)));
+extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
extern int adb_fputs(const char* buf, FILE* stream);
extern int adb_fputc(int ch, FILE* stream);
@@ -334,7 +314,6 @@
#else /* !_WIN32 a.k.a. Unix */
#include <cutils/sockets.h>
-#include <cutils/threads.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
@@ -419,14 +398,10 @@
#undef open
#define open ___xxx_open
-static __inline__ int adb_shutdown(int fd)
-{
- return shutdown(fd, SHUT_RDWR);
-}
-static __inline__ int adb_shutdown(int fd, int direction)
-{
+static __inline__ int adb_shutdown(int fd, int direction = SHUT_RDWR) {
return shutdown(fd, direction);
}
+
#undef shutdown
#define shutdown ____xxx_shutdown
@@ -625,11 +600,6 @@
return path[0] == '/';
}
-static __inline__ unsigned long adb_thread_id()
-{
- return (unsigned long)gettid();
-}
-
#endif /* !_WIN32 */
static inline void disable_tcp_nagle(int fd) {
diff --git a/adb/sysdeps/memory.h b/adb/sysdeps/memory.h
new file mode 100644
index 0000000..4108aff
--- /dev/null
+++ b/adb/sysdeps/memory.h
@@ -0,0 +1,65 @@
+#pragma once
+
+/*
+ * Copyright (C) 2018 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 <memory>
+#include <type_traits>
+
+#if defined(_WIN32)
+// We don't have C++14 on Windows yet.
+// Reimplement std::make_unique ourselves until we do.
+
+namespace internal {
+
+template <typename T>
+struct array_known_bounds;
+
+template <typename T>
+struct array_known_bounds<T[]> {
+ constexpr static bool value = false;
+};
+
+template <typename T, size_t N>
+struct array_known_bounds<T[N]> {
+ constexpr static bool value = true;
+};
+
+} // namespace internal
+
+namespace std {
+
+template <typename T, typename... Args>
+typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type make_unique(
+ Args&&... args) {
+ return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+}
+
+template <typename T>
+typename std::enable_if<std::is_array<T>::value && !internal::array_known_bounds<T>::value,
+ std::unique_ptr<T>>::type
+make_unique(std::size_t size) {
+ return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]());
+}
+
+template <typename T, typename... Args>
+typename std::enable_if<std::is_array<T>::value && internal::array_known_bounds<T>::value,
+ std::unique_ptr<T>>::type
+make_unique(Args&&... args) = delete;
+
+} // namespace std
+
+#endif
diff --git a/adb/sysdeps/posix/network.cpp b/adb/sysdeps/posix/network.cpp
index 45da5af..ecd1fd2 100644
--- a/adb/sysdeps/posix/network.cpp
+++ b/adb/sysdeps/posix/network.cpp
@@ -105,8 +105,7 @@
}
if (type == SOCK_STREAM || type == SOCK_SEQPACKET) {
- // Arbitrarily selected value, ported from libcutils.
- if (listen(s, 4) != 0) {
+ if (listen(s, SOMAXCONN) != 0) {
set_error(error);
return -1;
}
diff --git a/adb/sysdeps/uio.h b/adb/sysdeps/uio.h
new file mode 100644
index 0000000..d06ef89
--- /dev/null
+++ b/adb/sysdeps/uio.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include <sys/types.h>
+
+#if defined(_WIN32)
+
+// Layout of this struct must match struct WSABUF (verified via static assert in sysdeps_win32.cpp)
+struct adb_iovec {
+ size_t iov_len;
+ void* iov_base;
+};
+
+ssize_t adb_writev(int fd, const adb_iovec* iov, int iovcnt);
+
+#else
+
+#include <sys/uio.h>
+using adb_iovec = struct iovec;
+#define adb_writev writev
+
+#endif
+
+#pragma GCC poison writev
diff --git a/adb/sysdeps_test.cpp b/adb/sysdeps_test.cpp
index fd19882..79cebe6 100644
--- a/adb/sysdeps_test.cpp
+++ b/adb/sysdeps_test.cpp
@@ -121,10 +121,13 @@
adb_pollfd pfd[3] = {};
pfd[0].fd = fds[0];
pfd[0].events = POLLRDNORM;
+ pfd[0].revents = ~0;
pfd[1].fd = INT_MAX;
pfd[1].events = POLLRDNORM;
+ pfd[1].revents = ~0;
pfd[2].fd = fds[1];
pfd[2].events = POLLWRNORM;
+ pfd[2].revents = ~0;
ASSERT_TRUE(WriteFdExactly(fds[1], "foo", 4));
@@ -136,6 +139,17 @@
EXPECT_EQ(POLLRDNORM, pfd[0].revents);
EXPECT_EQ(POLLNVAL, pfd[1].revents);
EXPECT_EQ(POLLWRNORM, pfd[2].revents);
+
+ // Make sure that we return immediately if an invalid FD is given.
+ pfd[0].fd = fds[0];
+ pfd[0].events = POLLRDNORM;
+ pfd[0].revents = ~0;
+ pfd[1].fd = INT_MAX;
+ pfd[1].events = POLLRDNORM;
+ pfd[1].revents = ~0;
+ EXPECT_EQ(2, adb_poll(pfd, 2, -1));
+ EXPECT_EQ(POLLRDNORM, pfd[0].revents);
+ EXPECT_EQ(POLLNVAL, pfd[1].revents);
}
TEST_F(sysdeps_poll, duplicate_fd) {
diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp
index 5873b2b..c94d13f 100644
--- a/adb/sysdeps_win32.cpp
+++ b/adb/sysdeps_win32.cpp
@@ -36,6 +36,7 @@
#include <android-base/errors.h>
#include <android-base/logging.h>
+#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/utf8.h>
@@ -43,6 +44,8 @@
#include "adb.h"
#include "adb_utils.h"
+#include "sysdeps/uio.h"
+
extern void fatal(const char *fmt, ...);
/* forward declarations */
@@ -57,6 +60,7 @@
int (*_fh_lseek)(FH, int, int);
int (*_fh_read)(FH, void*, int);
int (*_fh_write)(FH, const void*, int);
+ int (*_fh_writev)(FH, const adb_iovec*, int);
} FHClassRec;
static void _fh_file_init(FH);
@@ -64,6 +68,7 @@
static int _fh_file_lseek(FH, int, int);
static int _fh_file_read(FH, void*, int);
static int _fh_file_write(FH, const void*, int);
+static int _fh_file_writev(FH, const adb_iovec*, int);
static const FHClassRec _fh_file_class = {
_fh_file_init,
@@ -71,6 +76,7 @@
_fh_file_lseek,
_fh_file_read,
_fh_file_write,
+ _fh_file_writev,
};
static void _fh_socket_init(FH);
@@ -78,6 +84,7 @@
static int _fh_socket_lseek(FH, int, int);
static int _fh_socket_read(FH, void*, int);
static int _fh_socket_write(FH, const void*, int);
+static int _fh_socket_writev(FH, const adb_iovec*, int);
static const FHClassRec _fh_socket_class = {
_fh_socket_init,
@@ -85,6 +92,7 @@
_fh_socket_lseek,
_fh_socket_read,
_fh_socket_write,
+ _fh_socket_writev,
};
#define assert(cond) \
@@ -150,7 +158,7 @@
D( "_fh_from_int: invalid fd %d passed to %s", fd + WIN32_FH_BASE,
func );
errno = EBADF;
- return NULL;
+ return nullptr;
}
f = &_win32_fhs[fd];
@@ -159,7 +167,7 @@
D( "_fh_from_int: invalid fd %d passed to %s", fd + WIN32_FH_BASE,
func );
errno = EBADF;
- return NULL;
+ return nullptr;
}
return f;
@@ -178,12 +186,12 @@
static FH
_fh_alloc( FHClass clazz )
{
- FH f = NULL;
+ FH f = nullptr;
std::lock_guard<std::mutex> lock(_win32_lock);
for (int i = _win32_fh_next; i < WIN32_MAX_FHS; ++i) {
- if (_win32_fhs[i].clazz == NULL) {
+ if (_win32_fhs[i].clazz == nullptr) {
f = &_win32_fhs[i];
_win32_fh_next = i + 1;
f->clazz = clazz;
@@ -218,7 +226,7 @@
f->name[0] = '\0';
f->eof = 0;
f->used = 0;
- f->clazz = NULL;
+ f->clazz = nullptr;
}
return 0;
}
@@ -248,57 +256,88 @@
/**************************************************************************/
/**************************************************************************/
-static void _fh_file_init( FH f ) {
+static void _fh_file_init(FH f) {
f->fh_handle = INVALID_HANDLE_VALUE;
}
-static int _fh_file_close( FH f ) {
- CloseHandle( f->fh_handle );
+static int _fh_file_close(FH f) {
+ CloseHandle(f->fh_handle);
f->fh_handle = INVALID_HANDLE_VALUE;
return 0;
}
-static int _fh_file_read( FH f, void* buf, int len ) {
- DWORD read_bytes;
+static int _fh_file_read(FH f, void* buf, int len) {
+ DWORD read_bytes;
- if ( !ReadFile( f->fh_handle, buf, (DWORD)len, &read_bytes, NULL ) ) {
- D( "adb_read: could not read %d bytes from %s", len, f->name );
+ if (!ReadFile(f->fh_handle, buf, (DWORD)len, &read_bytes, nullptr)) {
+ D("adb_read: could not read %d bytes from %s", len, f->name);
errno = EIO;
return -1;
} else if (read_bytes < (DWORD)len) {
f->eof = 1;
}
- return (int)read_bytes;
+ return read_bytes;
}
-static int _fh_file_write( FH f, const void* buf, int len ) {
- DWORD wrote_bytes;
+static int _fh_file_write(FH f, const void* buf, int len) {
+ DWORD wrote_bytes;
- if ( !WriteFile( f->fh_handle, buf, (DWORD)len, &wrote_bytes, NULL ) ) {
- D( "adb_file_write: could not write %d bytes from %s", len, f->name );
+ if (!WriteFile(f->fh_handle, buf, (DWORD)len, &wrote_bytes, nullptr)) {
+ D("adb_file_write: could not write %d bytes from %s", len, f->name);
errno = EIO;
return -1;
} else if (wrote_bytes < (DWORD)len) {
f->eof = 1;
}
- return (int)wrote_bytes;
+ return wrote_bytes;
}
-static int _fh_file_lseek( FH f, int pos, int origin ) {
- DWORD method;
- DWORD result;
+static int _fh_file_writev(FH f, const adb_iovec* iov, int iovcnt) {
+ if (iovcnt <= 0) {
+ errno = EINVAL;
+ return -1;
+ }
- switch (origin)
- {
- case SEEK_SET: method = FILE_BEGIN; break;
- case SEEK_CUR: method = FILE_CURRENT; break;
- case SEEK_END: method = FILE_END; break;
+ DWORD wrote_bytes = 0;
+
+ for (int i = 0; i < iovcnt; ++i) {
+ ssize_t rc = _fh_file_write(f, iov[i].iov_base, iov[i].iov_len);
+ if (rc == -1) {
+ return wrote_bytes > 0 ? wrote_bytes : -1;
+ } else if (rc == 0) {
+ return wrote_bytes;
+ }
+
+ wrote_bytes += rc;
+
+ if (static_cast<size_t>(rc) < iov[i].iov_len) {
+ return wrote_bytes;
+ }
+ }
+
+ return wrote_bytes;
+}
+
+static int _fh_file_lseek(FH f, int pos, int origin) {
+ DWORD method;
+ DWORD result;
+
+ switch (origin) {
+ case SEEK_SET:
+ method = FILE_BEGIN;
+ break;
+ case SEEK_CUR:
+ method = FILE_CURRENT;
+ break;
+ case SEEK_END:
+ method = FILE_END;
+ break;
default:
errno = EINVAL;
return -1;
}
- result = SetFilePointer( f->fh_handle, pos, NULL, method );
+ result = SetFilePointer(f->fh_handle, pos, nullptr, method);
if (result == INVALID_SET_FILE_POINTER) {
errno = EIO;
return -1;
@@ -308,7 +347,6 @@
return (int)result;
}
-
/**************************************************************************/
/**************************************************************************/
/***** *****/
@@ -317,12 +355,11 @@
/**************************************************************************/
/**************************************************************************/
-int adb_open(const char* path, int options)
-{
- FH f;
+int adb_open(const char* path, int options) {
+ FH f;
- DWORD desiredAccess = 0;
- DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
+ DWORD desiredAccess = 0;
+ DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
switch (options) {
case O_RDONLY:
@@ -340,8 +377,8 @@
return -1;
}
- f = _fh_alloc( &_fh_file_class );
- if ( !f ) {
+ f = _fh_alloc(&_fh_file_class);
+ if (!f) {
return -1;
}
@@ -349,21 +386,21 @@
if (!android::base::UTF8ToWide(path, &path_wide)) {
return -1;
}
- f->fh_handle = CreateFileW( path_wide.c_str(), desiredAccess, shareMode,
- NULL, OPEN_EXISTING, 0, NULL );
+ f->fh_handle =
+ CreateFileW(path_wide.c_str(), desiredAccess, shareMode, nullptr, OPEN_EXISTING, 0, nullptr);
- if ( f->fh_handle == INVALID_HANDLE_VALUE ) {
+ if (f->fh_handle == INVALID_HANDLE_VALUE) {
const DWORD err = GetLastError();
_fh_close(f);
- D( "adb_open: could not open '%s': ", path );
+ D("adb_open: could not open '%s': ", path);
switch (err) {
case ERROR_FILE_NOT_FOUND:
- D( "file not found" );
+ D("file not found");
errno = ENOENT;
return -1;
case ERROR_PATH_NOT_FOUND:
- D( "path not found" );
+ D("path not found");
errno = ENOTDIR;
return -1;
@@ -374,18 +411,17 @@
}
}
- snprintf( f->name, sizeof(f->name), "%d(%s)", _fh_to_int(f), path );
- D( "adb_open: '%s' => fd %d", path, _fh_to_int(f) );
+ snprintf(f->name, sizeof(f->name), "%d(%s)", _fh_to_int(f), path);
+ D("adb_open: '%s' => fd %d", path, _fh_to_int(f));
return _fh_to_int(f);
}
/* ignore mode on Win32 */
-int adb_creat(const char* path, int mode)
-{
- FH f;
+int adb_creat(const char* path, int mode) {
+ FH f;
- f = _fh_alloc( &_fh_file_class );
- if ( !f ) {
+ f = _fh_alloc(&_fh_file_class);
+ if (!f) {
return -1;
}
@@ -393,23 +429,21 @@
if (!android::base::UTF8ToWide(path, &path_wide)) {
return -1;
}
- f->fh_handle = CreateFileW( path_wide.c_str(), GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
- NULL );
+ f->fh_handle = CreateFileW(path_wide.c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
+ nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
- if ( f->fh_handle == INVALID_HANDLE_VALUE ) {
+ if (f->fh_handle == INVALID_HANDLE_VALUE) {
const DWORD err = GetLastError();
_fh_close(f);
- D( "adb_creat: could not open '%s': ", path );
+ D("adb_creat: could not open '%s': ", path);
switch (err) {
case ERROR_FILE_NOT_FOUND:
- D( "file not found" );
+ D("file not found");
errno = ENOENT;
return -1;
case ERROR_PATH_NOT_FOUND:
- D( "path not found" );
+ D("path not found");
errno = ENOTDIR;
return -1;
@@ -419,57 +453,64 @@
return -1;
}
}
- snprintf( f->name, sizeof(f->name), "%d(%s)", _fh_to_int(f), path );
- D( "adb_creat: '%s' => fd %d", path, _fh_to_int(f) );
+ snprintf(f->name, sizeof(f->name), "%d(%s)", _fh_to_int(f), path);
+ D("adb_creat: '%s' => fd %d", path, _fh_to_int(f));
return _fh_to_int(f);
}
+int adb_read(int fd, void* buf, int len) {
+ FH f = _fh_from_int(fd, __func__);
-int adb_read(int fd, void* buf, int len)
-{
- FH f = _fh_from_int(fd, __func__);
-
- if (f == NULL) {
+ if (f == nullptr) {
+ errno = EBADF;
return -1;
}
- return f->clazz->_fh_read( f, buf, len );
+ return f->clazz->_fh_read(f, buf, len);
}
+int adb_write(int fd, const void* buf, int len) {
+ FH f = _fh_from_int(fd, __func__);
-int adb_write(int fd, const void* buf, int len)
-{
- FH f = _fh_from_int(fd, __func__);
-
- if (f == NULL) {
+ if (f == nullptr) {
+ errno = EBADF;
return -1;
}
return f->clazz->_fh_write(f, buf, len);
}
+ssize_t adb_writev(int fd, const adb_iovec* iov, int iovcnt) {
+ FH f = _fh_from_int(fd, __func__);
-int adb_lseek(int fd, int pos, int where)
-{
- FH f = _fh_from_int(fd, __func__);
+ if (f == nullptr) {
+ errno = EBADF;
+ return -1;
+ }
+
+ return f->clazz->_fh_writev(f, iov, iovcnt);
+}
+
+int adb_lseek(int fd, int pos, int where) {
+ FH f = _fh_from_int(fd, __func__);
if (!f) {
+ errno = EBADF;
return -1;
}
return f->clazz->_fh_lseek(f, pos, where);
}
-
-int adb_close(int fd)
-{
- FH f = _fh_from_int(fd, __func__);
+int adb_close(int fd) {
+ FH f = _fh_from_int(fd, __func__);
if (!f) {
+ errno = EBADF;
return -1;
}
- D( "adb_close: %s", f->name);
+ D("adb_close: %s", f->name);
_fh_close(f);
return 0;
}
@@ -529,6 +570,7 @@
int skipped = 0;
std::vector<WSAPOLLFD> sockets;
std::vector<adb_pollfd*> original;
+
for (size_t i = 0; i < nfds; ++i) {
FH fh = _fh_from_int(fds[i].fd, __func__);
if (!fh || !fh->used || fh->clazz != &_fh_socket_class) {
@@ -549,6 +591,11 @@
return skipped;
}
+ // If we have any invalid FDs in our FD set, make sure to return immediately.
+ if (skipped > 0) {
+ timeout = 0;
+ }
+
int result = WSAPoll(sockets.data(), sockets.size(), timeout);
if (result == SOCKET_ERROR) {
_socket_set_errno(WSAGetLastError());
@@ -560,7 +607,7 @@
original[i]->revents = sockets[i].revents;
}
- // WSAPoll appears to return the number of unique FDs with avaiable events, instead of how many
+ // WSAPoll appears to return the number of unique FDs with available events, instead of how many
// of the pollfd elements have a non-zero revents field, which is what it and poll are specified
// to do. Ignore its result and calculate the proper return value.
result = 0;
@@ -576,7 +623,7 @@
f->fh_socket = INVALID_SOCKET;
}
-static int _fh_socket_close( FH f ) {
+static int _fh_socket_close(FH f) {
if (f->fh_socket != INVALID_SOCKET) {
/* gently tell any peer that we're closing the socket */
if (shutdown(f->fh_socket, SD_BOTH) == SOCKET_ERROR) {
@@ -597,13 +644,13 @@
return 0;
}
-static int _fh_socket_lseek( FH f, int pos, int origin ) {
+static int _fh_socket_lseek(FH f, int pos, int origin) {
errno = EPIPE;
return -1;
}
static int _fh_socket_read(FH f, void* buf, int len) {
- int result = recv(f->fh_socket, reinterpret_cast<char*>(buf), len, 0);
+ int result = recv(f->fh_socket, reinterpret_cast<char*>(buf), len, 0);
if (result == SOCKET_ERROR) {
const DWORD err = WSAGetLastError();
// WSAEWOULDBLOCK is normal with a non-blocking socket, so don't trace
@@ -615,11 +662,11 @@
_socket_set_errno(err);
result = -1;
}
- return result;
+ return result;
}
static int _fh_socket_write(FH f, const void* buf, int len) {
- int result = send(f->fh_socket, reinterpret_cast<const char*>(buf), len, 0);
+ int result = send(f->fh_socket, reinterpret_cast<const char*>(buf), len, 0);
if (result == SOCKET_ERROR) {
const DWORD err = WSAGetLastError();
// WSAEWOULDBLOCK is normal with a non-blocking socket, so don't trace
@@ -633,13 +680,44 @@
} else {
// According to https://code.google.com/p/chromium/issues/detail?id=27870
// Winsock Layered Service Providers may cause this.
- CHECK_LE(result, len) << "Tried to write " << len << " bytes to "
- << f->name << ", but " << result
- << " bytes reportedly written";
+ CHECK_LE(result, len) << "Tried to write " << len << " bytes to " << f->name << ", but "
+ << result << " bytes reportedly written";
}
return result;
}
+// Make sure that adb_iovec is compatible with WSABUF.
+static_assert(sizeof(adb_iovec) == sizeof(WSABUF), "");
+static_assert(SIZEOF_MEMBER(adb_iovec, iov_len) == SIZEOF_MEMBER(WSABUF, len), "");
+static_assert(offsetof(adb_iovec, iov_len) == offsetof(WSABUF, len), "");
+
+static_assert(SIZEOF_MEMBER(adb_iovec, iov_base) == SIZEOF_MEMBER(WSABUF, buf), "");
+static_assert(offsetof(adb_iovec, iov_base) == offsetof(WSABUF, buf), "");
+
+static int _fh_socket_writev(FH f, const adb_iovec* iov, int iovcnt) {
+ if (iovcnt <= 0) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ WSABUF* wsabuf = reinterpret_cast<WSABUF*>(const_cast<adb_iovec*>(iov));
+ DWORD bytes_written = 0;
+ int result = WSASend(f->fh_socket, wsabuf, iovcnt, &bytes_written, 0, nullptr, nullptr);
+ if (result == SOCKET_ERROR) {
+ const DWORD err = WSAGetLastError();
+ // WSAEWOULDBLOCK is normal with a non-blocking socket, so don't trace
+ // that to reduce spam and confusion.
+ if (err != WSAEWOULDBLOCK) {
+ D("send fd %d failed: %s", _fh_to_int(f),
+ android::base::SystemErrorCodeToString(err).c_str());
+ }
+ _socket_set_errno(err);
+ result = -1;
+ }
+ CHECK_GE(static_cast<DWORD>(std::numeric_limits<int>::max()), bytes_written);
+ return static_cast<int>(bytes_written);
+}
+
/**************************************************************************/
/**************************************************************************/
/***** *****/
@@ -648,23 +726,15 @@
/**************************************************************************/
/**************************************************************************/
-#include <winsock2.h>
-
-static int _winsock_init;
-
-static void
-_init_winsock( void )
-{
- // TODO: Multiple threads calling this may potentially cause multiple calls
- // to WSAStartup() which offers no real benefit.
- if (!_winsock_init) {
- WSADATA wsaData;
- int rc = WSAStartup( MAKEWORD(2,2), &wsaData);
+static int _init_winsock(void) {
+ static std::once_flag once;
+ std::call_once(once, []() {
+ WSADATA wsaData;
+ int rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rc != 0) {
fatal("adb: could not initialize Winsock: %s",
android::base::SystemErrorCodeToString(rc).c_str());
}
- _winsock_init = 1;
// Note that we do not call atexit() to register WSACleanup to be called
// at normal process termination because:
@@ -679,9 +749,12 @@
// setupapi.dll which tries to load wintrust.dll which tries to load
// crypt32.dll which calls atexit() which tries to acquire the C
// Runtime lock that the other thread holds.
- }
+ });
+ return 0;
}
+static int _winsock_init = _init_winsock();
+
// Map a socket type to an explicit socket protocol instead of using the socket
// protocol of 0. Explicit socket protocols are used by most apps and we should
// do the same to reduce the chance of exercising uncommon code-paths that might
@@ -709,8 +782,6 @@
return -1;
}
- if (!_winsock_init) _init_winsock();
-
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
@@ -747,8 +818,6 @@
return fd;
}
-#define LISTEN_BACKLOG 4
-
// interface_address is INADDR_LOOPBACK or INADDR_ANY.
static int _network_server(int port, int type, u_long interface_address, std::string* error) {
struct sockaddr_in addr;
@@ -761,8 +830,6 @@
return -1;
}
- if (!_winsock_init) _init_winsock();
-
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
@@ -805,7 +872,7 @@
return -1;
}
if (type == SOCK_STREAM) {
- if (listen(s, LISTEN_BACKLOG) == SOCKET_ERROR) {
+ if (listen(s, SOMAXCONN) == SOCKET_ERROR) {
const DWORD err = WSAGetLastError();
*error = android::base::StringPrintf(
"cannot listen on socket: %s", android::base::SystemErrorCodeToString(err).c_str());
@@ -839,8 +906,6 @@
return -1;
}
- if (!_winsock_init) _init_winsock();
-
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
@@ -908,52 +973,49 @@
return fd;
}
-int adb_register_socket(SOCKET s) {
- FH f = _fh_alloc( &_fh_socket_class );
+int adb_register_socket(SOCKET s) {
+ FH f = _fh_alloc(&_fh_socket_class);
f->fh_socket = s;
return _fh_to_int(f);
}
#undef accept
-int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
-{
- FH serverfh = _fh_from_int(serverfd, __func__);
+int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t* addrlen) {
+ FH serverfh = _fh_from_int(serverfd, __func__);
- if ( !serverfh || serverfh->clazz != &_fh_socket_class ) {
+ if (!serverfh || serverfh->clazz != &_fh_socket_class) {
D("adb_socket_accept: invalid fd %d", serverfd);
errno = EBADF;
return -1;
}
- unique_fh fh(_fh_alloc( &_fh_socket_class ));
+ unique_fh fh(_fh_alloc(&_fh_socket_class));
if (!fh) {
PLOG(ERROR) << "adb_socket_accept: failed to allocate accepted socket "
"descriptor";
return -1;
}
- fh->fh_socket = accept( serverfh->fh_socket, addr, addrlen );
+ fh->fh_socket = accept(serverfh->fh_socket, addr, addrlen);
if (fh->fh_socket == INVALID_SOCKET) {
const DWORD err = WSAGetLastError();
- LOG(ERROR) << "adb_socket_accept: accept on fd " << serverfd <<
- " failed: " + android::base::SystemErrorCodeToString(err);
- _socket_set_errno( err );
+ LOG(ERROR) << "adb_socket_accept: accept on fd " << serverfd
+ << " failed: " + android::base::SystemErrorCodeToString(err);
+ _socket_set_errno(err);
return -1;
}
const int fd = _fh_to_int(fh.get());
- snprintf( fh->name, sizeof(fh->name), "%d(accept:%s)", fd, serverfh->name );
- D( "adb_socket_accept on fd %d returns fd %d", serverfd, fd );
+ snprintf(fh->name, sizeof(fh->name), "%d(accept:%s)", fd, serverfh->name);
+ D("adb_socket_accept on fd %d returns fd %d", serverfd, fd);
fh.release();
- return fd;
+ return fd;
}
+int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen) {
+ FH fh = _fh_from_int(fd, __func__);
-int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
-{
- FH fh = _fh_from_int(fd, __func__);
-
- if ( !fh || fh->clazz != &_fh_socket_class ) {
+ if (!fh || fh->clazz != &_fh_socket_class) {
D("adb_setsockopt: invalid fd %d", fd);
errno = EBADF;
return -1;
@@ -963,13 +1025,13 @@
// to set SOL_SOCKET, SO_SNDBUF/SO_RCVBUF, ignore it since the OS has
// auto-tuning.
- int result = setsockopt( fh->fh_socket, level, optname,
- reinterpret_cast<const char*>(optval), optlen );
- if ( result == SOCKET_ERROR ) {
+ int result =
+ setsockopt(fh->fh_socket, level, optname, reinterpret_cast<const char*>(optval), optlen);
+ if (result == SOCKET_ERROR) {
const DWORD err = WSAGetLastError();
- D("adb_setsockopt: setsockopt on fd %d level %d optname %d failed: %s\n",
- fd, level, optname, android::base::SystemErrorCodeToString(err).c_str());
- _socket_set_errno( err );
+ D("adb_setsockopt: setsockopt on fd %d level %d optname %d failed: %s\n", fd, level,
+ optname, android::base::SystemErrorCodeToString(err).c_str());
+ _socket_set_errno(err);
result = -1;
}
return result;
@@ -1013,9 +1075,8 @@
return ntohs(reinterpret_cast<sockaddr_in*>(&addr_storage)->sin_port);
}
-int adb_shutdown(int fd)
-{
- FH f = _fh_from_int(fd, __func__);
+int adb_shutdown(int fd, int direction) {
+ FH f = _fh_from_int(fd, __func__);
if (!f || f->clazz != &_fh_socket_class) {
D("adb_shutdown: invalid fd %d", fd);
@@ -1023,8 +1084,8 @@
return -1;
}
- D( "adb_shutdown: %s", f->name);
- if (shutdown(f->fh_socket, SD_BOTH) == SOCKET_ERROR) {
+ D("adb_shutdown: %s", f->name);
+ if (shutdown(f->fh_socket, direction) == SOCKET_ERROR) {
const DWORD err = WSAGetLastError();
D("socket shutdown fd %d failed: %s", fd,
android::base::SystemErrorCodeToString(err).c_str());
@@ -1639,7 +1700,7 @@
// The following emulation code should write the output sequence to
// either seqstr or to seqbuf and seqbuflen.
- const char* seqstr = NULL; // NULL terminated C-string
+ const char* seqstr = nullptr; // NULL terminated C-string
// Enough space for max sequence string below, plus modifiers and/or
// escape prefix.
char seqbuf[16];
@@ -1937,7 +1998,7 @@
// * seqstr is set (and strlen can be used to determine the length).
// * seqbuf and seqbuflen are set
// Fallback to ch from Windows.
- if (seqstr != NULL) {
+ if (seqstr != nullptr) {
out = seqstr;
outlen = strlen(seqstr);
} else if (seqbuflen > 0) {
@@ -2007,9 +2068,9 @@
}
void stdin_raw_restore() {
- if (_console_handle != NULL) {
+ if (_console_handle != nullptr) {
const HANDLE in = _console_handle;
- _console_handle = NULL; // clear state
+ _console_handle = nullptr; // clear state
if (!SetConsoleMode(in, _old_console_mode)) {
// This really should not fail.
@@ -2021,7 +2082,7 @@
// Called by 'adb shell' and 'adb exec-in' (via unix_read()) to read from stdin.
int unix_read_interruptible(int fd, void* buf, size_t len) {
- if ((fd == STDIN_FILENO) && (_console_handle != NULL)) {
+ if ((fd == STDIN_FILENO) && (_console_handle != nullptr)) {
// If it is a request to read from stdin, and stdin_raw_init() has been
// called, and it successfully configured the console, then read from
// the console using Win32 console APIs and partially emulate a unix
@@ -2381,7 +2442,7 @@
// Write UTF-16 to the console.
DWORD written = 0;
- if (!WriteConsoleW(console, utf16.c_str(), utf16.length(), &written, NULL)) {
+ if (!WriteConsoleW(console, utf16.c_str(), utf16.length(), &written, nullptr)) {
errno = EIO;
return -1;
}
@@ -2394,9 +2455,8 @@
}
// Function prototype because attributes cannot be placed on func definitions.
-static int _console_vfprintf(const HANDLE console, FILE* stream,
- const char *format, va_list ap)
- __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 3, 0)));
+static int _console_vfprintf(const HANDLE console, FILE* stream, const char* format, va_list ap)
+ __attribute__((__format__(__printf__, 3, 0)));
// Internal function to format a UTF-8 string and write it to a Win32 console.
// Returns -1 on error.
@@ -2426,7 +2486,7 @@
// If there is an associated Win32 console, write to it specially,
// otherwise defer to the regular C Runtime, passing it UTF-8.
- if (console != NULL) {
+ if (console != nullptr) {
return _console_vfprintf(console, stream, format, ap);
} else {
// If vfprintf is a macro, undefine it, so we can call the real
@@ -2517,7 +2577,7 @@
// If there is an associated Win32 console, write to it specially,
// otherwise defer to the regular C Runtime, passing it UTF-8.
- if (console != NULL) {
+ if (console != nullptr) {
return _console_fwrite(ptr, size, nmemb, stream, console);
} else {
// If fwrite is a macro, undefine it, so we can call the real
diff --git a/adb/test_adb.py b/adb/test_adb.py
old mode 100644
new mode 100755
index 98c8a59..86c13d0
--- a/adb/test_adb.py
+++ b/adb/test_adb.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
#
# Copyright (C) 2015 The Android Open Source Project
#
@@ -19,65 +19,186 @@
This differs from things in test_device.py in that there is no API for these
things. Most of these tests involve specific error messages or the help text.
"""
-from __future__ import print_function
import contextlib
import os
import random
+import select
import socket
import struct
import subprocess
import threading
+import time
import unittest
-import adb
+
+@contextlib.contextmanager
+def fake_adbd(protocol=socket.AF_INET, port=0):
+ """Creates a fake ADB daemon that just replies with a CNXN packet."""
+
+ serversock = socket.socket(protocol, socket.SOCK_STREAM)
+ serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ if protocol == socket.AF_INET:
+ serversock.bind(("127.0.0.1", port))
+ else:
+ serversock.bind(("::1", port))
+ serversock.listen(1)
+
+ # A pipe that is used to signal the thread that it should terminate.
+ readsock, writesock = socket.socketpair()
+
+ def _adb_packet(command: bytes, arg0: int, arg1: int, data: bytes) -> bytes:
+ bin_command = struct.unpack("I", command)[0]
+ buf = struct.pack("IIIIII", bin_command, arg0, arg1, len(data), 0,
+ bin_command ^ 0xffffffff)
+ buf += data
+ return buf
+
+ def _handle(sock):
+ with contextlib.closing(sock) as serversock:
+ rlist = [readsock, serversock]
+ cnxn_sent = {}
+ while True:
+ read_ready, _, _ = select.select(rlist, [], [])
+ for ready in read_ready:
+ if ready == readsock:
+ # Closure pipe
+ for f in rlist:
+ f.close()
+ return
+ elif ready == serversock:
+ # Server socket
+ conn, _ = ready.accept()
+ rlist.append(conn)
+ else:
+ # Client socket
+ data = ready.recv(1024)
+ if not data or data.startswith(b"OPEN"):
+ if ready in cnxn_sent:
+ del cnxn_sent[ready]
+ ready.shutdown(socket.SHUT_RDWR)
+ ready.close()
+ rlist.remove(ready)
+ continue
+ if ready in cnxn_sent:
+ continue
+ cnxn_sent[ready] = True
+ ready.sendall(_adb_packet(b"CNXN", 0x01000001, 1024 * 1024,
+ b"device::ro.product.name=fakeadb"))
+
+ port = serversock.getsockname()[1]
+ server_thread = threading.Thread(target=_handle, args=(serversock,))
+ server_thread.start()
+
+ try:
+ yield port, writesock
+ finally:
+ writesock.close()
+ server_thread.join()
-class NonApiTest(unittest.TestCase):
- """Tests for ADB that aren't a part of the AndroidDevice API."""
+@contextlib.contextmanager
+def adb_connect(unittest, serial):
+ """Context manager for an ADB connection.
+
+ This automatically disconnects when done with the connection.
+ """
+
+ output = subprocess.check_output(["adb", "connect", serial])
+ unittest.assertEqual(output.strip(),
+ "connected to {}".format(serial).encode("utf8"))
+
+ try:
+ yield
+ finally:
+ # Perform best-effort disconnection. Discard the output.
+ subprocess.Popen(["adb", "disconnect", serial],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE).communicate()
+
+
+@contextlib.contextmanager
+def adb_server():
+ """Context manager for an ADB server.
+
+ This creates an ADB server and returns the port it's listening on.
+ """
+
+ port = 5038
+ # Kill any existing server on this non-default port.
+ subprocess.check_output(["adb", "-P", str(port), "kill-server"],
+ stderr=subprocess.STDOUT)
+ read_pipe, write_pipe = os.pipe()
+ os.set_inheritable(write_pipe, True)
+ proc = subprocess.Popen(["adb", "-L", "tcp:localhost:{}".format(port),
+ "fork-server", "server",
+ "--reply-fd", str(write_pipe)], close_fds=False)
+ try:
+ os.close(write_pipe)
+ greeting = os.read(read_pipe, 1024)
+ assert greeting == b"OK\n", repr(greeting)
+ yield port
+ finally:
+ proc.terminate()
+ proc.wait()
+
+
+class CommandlineTest(unittest.TestCase):
+ """Tests for the ADB commandline."""
def test_help(self):
"""Make sure we get _something_ out of help."""
out = subprocess.check_output(
- ['adb', 'help'], stderr=subprocess.STDOUT)
+ ["adb", "help"], stderr=subprocess.STDOUT)
self.assertGreater(len(out), 0)
def test_version(self):
"""Get a version number out of the output of adb."""
- lines = subprocess.check_output(['adb', 'version']).splitlines()
+ lines = subprocess.check_output(["adb", "version"]).splitlines()
version_line = lines[0]
- self.assertRegexpMatches(
- version_line, r'^Android Debug Bridge version \d+\.\d+\.\d+$')
+ self.assertRegex(
+ version_line, rb"^Android Debug Bridge version \d+\.\d+\.\d+$")
if len(lines) == 2:
# Newer versions of ADB have a second line of output for the
# version that includes a specific revision (git SHA).
revision_line = lines[1]
- self.assertRegexpMatches(
- revision_line, r'^Revision [0-9a-f]{12}-android$')
+ self.assertRegex(
+ revision_line, rb"^Revision [0-9a-f]{12}-android$")
def test_tcpip_error_messages(self):
- p = subprocess.Popen(['adb', 'tcpip'], stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
- out, _ = p.communicate()
- self.assertEqual(1, p.returncode)
- self.assertIn('requires an argument', out)
+ """Make sure 'adb tcpip' parsing is sane."""
+ proc = subprocess.Popen(["adb", "tcpip"],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ out, _ = proc.communicate()
+ self.assertEqual(1, proc.returncode)
+ self.assertIn(b"requires an argument", out)
- p = subprocess.Popen(['adb', 'tcpip', 'foo'], stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
- out, _ = p.communicate()
- self.assertEqual(1, p.returncode)
- self.assertIn('invalid port', out)
+ proc = subprocess.Popen(["adb", "tcpip", "foo"],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ out, _ = proc.communicate()
+ self.assertEqual(1, proc.returncode)
+ self.assertIn(b"invalid port", out)
- # Helper method that reads a pipe until it is closed, then sets the event.
- def _read_pipe_and_set_event(self, pipe, event):
- x = pipe.read()
+
+class ServerTest(unittest.TestCase):
+ """Tests for the ADB server."""
+
+ @staticmethod
+ def _read_pipe_and_set_event(pipe, event):
+ """Reads a pipe until it is closed, then sets the event."""
+ pipe.read()
event.set()
- # Test that launch_server() does not let the adb server inherit
- # stdin/stdout/stderr handles which can cause callers of adb.exe to hang.
- # This test also runs fine on unix even though the impetus is an issue
- # unique to Windows.
def test_handle_inheritance(self):
+ """Test that launch_server() does not inherit handles.
+
+ launch_server() should not let the adb server inherit
+ stdin/stdout/stderr handles, which can cause callers of adb.exe to hang.
+ This test also runs fine on unix even though the impetus is an issue
+ unique to Windows.
+ """
# This test takes 5 seconds to run on Windows: if there is no adb server
# running on the the port used below, adb kill-server tries to make a
# TCP connection to a closed port and that takes 1 second on Windows;
@@ -94,34 +215,33 @@
port = 5038
# Kill any existing server on this non-default port.
- subprocess.check_output(['adb', '-P', str(port), 'kill-server'],
+ subprocess.check_output(["adb", "-P", str(port), "kill-server"],
stderr=subprocess.STDOUT)
try:
# Run the adb client and have it start the adb server.
- p = subprocess.Popen(['adb', '-P', str(port), 'start-server'],
- stdin=subprocess.PIPE, stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
+ proc = subprocess.Popen(["adb", "-P", str(port), "start-server"],
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
# Start threads that set events when stdout/stderr are closed.
stdout_event = threading.Event()
stdout_thread = threading.Thread(
- target=self._read_pipe_and_set_event,
- args=(p.stdout, stdout_event))
- stdout_thread.daemon = True
+ target=ServerTest._read_pipe_and_set_event,
+ args=(proc.stdout, stdout_event))
stdout_thread.start()
stderr_event = threading.Event()
stderr_thread = threading.Thread(
- target=self._read_pipe_and_set_event,
- args=(p.stderr, stderr_event))
- stderr_thread.daemon = True
+ target=ServerTest._read_pipe_and_set_event,
+ args=(proc.stderr, stderr_event))
stderr_thread.start()
# Wait for the adb client to finish. Once that has occurred, if
# stdin/stderr/stdout are still open, it must be open in the adb
# server.
- p.wait()
+ proc.wait()
# Try to write to stdin which we expect is closed. If it isn't
# closed, we should get an IOError. If we don't get an IOError,
@@ -129,7 +249,8 @@
# probably letting the adb server inherit stdin which would be
# wrong.
with self.assertRaises(IOError):
- p.stdin.write('x')
+ proc.stdin.write(b"x")
+ proc.stdin.flush()
# Wait a few seconds for stdout/stderr to be closed (in the success
# case, this won't wait at all). If there is a timeout, that means
@@ -138,15 +259,21 @@
# inherit stdout/stderr which would be wrong.
self.assertTrue(stdout_event.wait(5), "adb stdout not closed")
self.assertTrue(stderr_event.wait(5), "adb stderr not closed")
+ stdout_thread.join()
+ stderr_thread.join()
finally:
# If we started a server, kill it.
- subprocess.check_output(['adb', '-P', str(port), 'kill-server'],
+ subprocess.check_output(["adb", "-P", str(port), "kill-server"],
stderr=subprocess.STDOUT)
- # Use SO_LINGER to cause TCP RST segment to be sent on socket close.
+
+class EmulatorTest(unittest.TestCase):
+ """Tests for the emulator connection."""
+
def _reset_socket_on_close(self, sock):
+ """Use SO_LINGER to cause TCP RST segment to be sent on socket close."""
# The linger structure is two shorts on Windows, but two ints on Unix.
- linger_format = 'hh' if os.name == 'nt' else 'ii'
+ linger_format = "hh" if os.name == "nt" else "ii"
l_onoff = 1
l_linger = 0
@@ -162,35 +289,38 @@
Bug: https://code.google.com/p/android/issues/detail?id=21021
"""
- port = 12345
-
with contextlib.closing(
- socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as listener:
+ socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as listener:
# Use SO_REUSEADDR so subsequent runs of the test can grab the port
# even if it is in TIME_WAIT.
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- listener.bind(('127.0.0.1', port))
+ listener.bind(("127.0.0.1", 0))
listener.listen(4)
+ port = listener.getsockname()[1]
# Now that listening has started, start adb emu kill, telling it to
# connect to our mock emulator.
- p = subprocess.Popen(
- ['adb', '-s', 'emulator-' + str(port), 'emu', 'kill'],
+ proc = subprocess.Popen(
+ ["adb", "-s", "emulator-" + str(port), "emu", "kill"],
stderr=subprocess.STDOUT)
accepted_connection, addr = listener.accept()
with contextlib.closing(accepted_connection) as conn:
# If WSAECONNABORTED (10053) is raised by any socket calls,
# then adb probably isn't reading the data that we sent it.
- conn.sendall('Android Console: type \'help\' for a list ' +
- 'of commands\r\n')
- conn.sendall('OK\r\n')
+ conn.sendall(("Android Console: type 'help' for a list "
+ "of commands\r\n").encode("utf8"))
+ conn.sendall(b"OK\r\n")
- with contextlib.closing(conn.makefile()) as f:
- self.assertEqual('kill\n', f.readline())
- self.assertEqual('quit\n', f.readline())
+ with contextlib.closing(conn.makefile()) as connf:
+ line = connf.readline()
+ if line.startswith("auth"):
+ # Ignore the first auth line.
+ line = connf.readline()
+ self.assertEqual("kill\n", line)
+ self.assertEqual("quit\n", connf.readline())
- conn.sendall('OK: killing emulator, bye bye\r\n')
+ conn.sendall(b"OK: killing emulator, bye bye\r\n")
# Use SO_LINGER to send TCP RST segment to test whether adb
# ignores WSAECONNRESET on Windows. This happens with the
@@ -201,43 +331,160 @@
self._reset_socket_on_close(conn)
# Wait for adb to finish, so we can check return code.
- p.communicate()
+ proc.communicate()
# If this fails, adb probably isn't ignoring WSAECONNRESET when
# reading the response from the adb emu kill command (on Windows).
- self.assertEqual(0, p.returncode)
+ self.assertEqual(0, proc.returncode)
+
+ def test_emulator_connect(self):
+ """Ensure that the emulator can connect.
+
+ Bug: http://b/78991667
+ """
+ with adb_server() as server_port:
+ with fake_adbd() as (port, _):
+ serial = "emulator-{}".format(port - 1)
+ # Ensure that the emulator is not there.
+ try:
+ subprocess.check_output(["adb", "-P", str(server_port),
+ "-s", serial, "get-state"],
+ stderr=subprocess.STDOUT)
+ self.fail("Device should not be available")
+ except subprocess.CalledProcessError as err:
+ self.assertEqual(
+ err.output.strip(),
+ "error: device '{}' not found".format(serial).encode("utf8"))
+
+ # Let the ADB server know that the emulator has started.
+ with contextlib.closing(
+ socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
+ sock.connect(("localhost", server_port))
+ command = "host:emulator:{}".format(port).encode("utf8")
+ sock.sendall(b"%04x%s" % (len(command), command))
+
+ # Ensure the emulator is there.
+ subprocess.check_call(["adb", "-P", str(server_port),
+ "-s", serial, "wait-for-device"])
+ output = subprocess.check_output(["adb", "-P", str(server_port),
+ "-s", serial, "get-state"])
+ self.assertEqual(output.strip(), b"device")
+
+
+class ConnectionTest(unittest.TestCase):
+ """Tests for adb connect."""
def test_connect_ipv4_ipv6(self):
"""Ensure that `adb connect localhost:1234` will try both IPv4 and IPv6.
Bug: http://b/30313466
"""
- ipv4 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- ipv4.bind(('127.0.0.1', 0))
- ipv4.listen(1)
+ for protocol in (socket.AF_INET, socket.AF_INET6):
+ try:
+ with fake_adbd(protocol=protocol) as (port, _):
+ serial = "localhost:{}".format(port)
+ with adb_connect(self, serial):
+ pass
+ except socket.error:
+ print("IPv6 not available, skipping")
+ continue
- ipv6 = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
- ipv6.bind(('::1', ipv4.getsockname()[1] + 1))
- ipv6.listen(1)
+ def test_already_connected(self):
+ """Ensure that an already-connected device stays connected."""
- for s in (ipv4, ipv6):
- port = s.getsockname()[1]
- output = subprocess.check_output(
- ['adb', 'connect', 'localhost:{}'.format(port)])
+ with fake_adbd() as (port, _):
+ serial = "localhost:{}".format(port)
+ with adb_connect(self, serial):
+ # b/31250450: this always returns 0 but probably shouldn't.
+ output = subprocess.check_output(["adb", "connect", serial])
+ self.assertEqual(
+ output.strip(),
+ "already connected to {}".format(serial).encode("utf8"))
- self.assertEqual(
- output.strip(), 'connected to localhost:{}'.format(port))
- s.close()
+ def test_reconnect(self):
+ """Ensure that a disconnected device reconnects."""
+
+ with fake_adbd() as (port, _):
+ serial = "localhost:{}".format(port)
+ with adb_connect(self, serial):
+ output = subprocess.check_output(["adb", "-s", serial,
+ "get-state"])
+ self.assertEqual(output.strip(), b"device")
+
+ # This will fail.
+ proc = subprocess.Popen(["adb", "-s", serial, "shell", "true"],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ output, _ = proc.communicate()
+ self.assertEqual(output.strip(), b"error: closed")
+
+ subprocess.check_call(["adb", "-s", serial, "wait-for-device"])
+
+ output = subprocess.check_output(["adb", "-s", serial,
+ "get-state"])
+ self.assertEqual(output.strip(), b"device")
+
+ # Once we explicitly kick a device, it won't attempt to
+ # reconnect.
+ output = subprocess.check_output(["adb", "disconnect", serial])
+ self.assertEqual(
+ output.strip(),
+ "disconnected {}".format(serial).encode("utf8"))
+ try:
+ subprocess.check_output(["adb", "-s", serial, "get-state"],
+ stderr=subprocess.STDOUT)
+ self.fail("Device should not be available")
+ except subprocess.CalledProcessError as err:
+ self.assertEqual(
+ err.output.strip(),
+ "error: device '{}' not found".format(serial).encode("utf8"))
+
+
+class DisconnectionTest(unittest.TestCase):
+ """Tests for adb disconnect."""
+
+ def test_disconnect(self):
+ """Ensure that `adb disconnect` takes effect immediately."""
+
+ def _devices(port):
+ output = subprocess.check_output(["adb", "-P", str(port), "devices"])
+ return [x.split("\t") for x in output.decode("utf8").strip().splitlines()[1:]]
+
+ with adb_server() as server_port:
+ with fake_adbd() as (port, sock):
+ device_name = "localhost:{}".format(port)
+ output = subprocess.check_output(["adb", "-P", str(server_port),
+ "connect", device_name])
+ self.assertEqual(output.strip(),
+ "connected to {}".format(device_name).encode("utf8"))
+
+
+ self.assertEqual(_devices(server_port), [[device_name, "device"]])
+
+ # Send a deliberately malformed packet to make the device go offline.
+ packet = struct.pack("IIIIII", 0, 0, 0, 0, 0, 0)
+ sock.sendall(packet)
+
+ # Wait a bit.
+ time.sleep(0.1)
+
+ self.assertEqual(_devices(server_port), [[device_name, "offline"]])
+
+ # Disconnect the device.
+ output = subprocess.check_output(["adb", "-P", str(server_port),
+ "disconnect", device_name])
+
+ # Wait a bit.
+ time.sleep(0.1)
+
+ self.assertEqual(_devices(server_port), [])
def main():
+ """Main entrypoint."""
random.seed(0)
- if len(adb.get_devices()) > 0:
- suite = unittest.TestLoader().loadTestsFromName(__name__)
- unittest.TextTestRunner(verbosity=3).run(suite)
- else:
- print('Test suite must be run with attached devices')
+ unittest.main(verbosity=3)
-if __name__ == '__main__':
+if __name__ == "__main__":
main()
diff --git a/adb/test_device.py b/adb/test_device.py
index 4cf2206..42aadc4 100644
--- a/adb/test_device.py
+++ b/adb/test_device.py
@@ -31,6 +31,7 @@
import subprocess
import sys
import tempfile
+import threading
import time
import unittest
@@ -187,8 +188,6 @@
finally:
self.device.reverse_remove_all()
- # Note: If you run this test when adb connect'd to a physical device over
- # TCP, it will fail in adb reverse due to https://code.google.com/p/android/issues/detail?id=189821
def test_forward_reverse_echo(self):
"""Send data through adb forward and read it back via adb reverse"""
forward_port = 12345
@@ -493,6 +492,29 @@
stdout, _ = self.device.shell(["cat", log_path])
self.assertEqual(stdout.strip(), "SIGHUP")
+ def test_exit_stress(self):
+ """Hammer `adb shell exit 42` with multiple threads."""
+ thread_count = 48
+ result = dict()
+ def hammer(thread_idx, thread_count, result):
+ success = True
+ for i in range(thread_idx, 240, thread_count):
+ ret = subprocess.call(['adb', 'shell', 'exit {}'.format(i)])
+ if ret != i % 256:
+ success = False
+ break
+ result[thread_idx] = success
+
+ threads = []
+ for i in range(thread_count):
+ thread = threading.Thread(target=hammer, args=(i, thread_count, result))
+ thread.start()
+ threads.append(thread)
+ for thread in threads:
+ thread.join()
+ for i, success in result.iteritems():
+ self.assertTrue(success)
+
class ArgumentEscapingTest(DeviceTest):
def test_shell_escaping(self):
@@ -728,7 +750,6 @@
if host_dir is not None:
shutil.rmtree(host_dir)
- @unittest.expectedFailure # b/25566053
def test_push_empty(self):
"""Push a directory containing an empty directory to the device."""
self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
@@ -741,9 +762,10 @@
os.chmod(host_dir, 0o700)
# Create an empty directory.
- os.mkdir(os.path.join(host_dir, 'empty'))
+ empty_dir_path = os.path.join(host_dir, 'empty')
+ os.mkdir(empty_dir_path);
- self.device.push(host_dir, self.DEVICE_TEMP_DIR)
+ self.device.push(empty_dir_path, self.DEVICE_TEMP_DIR)
test_empty_cmd = ['[', '-d',
os.path.join(self.DEVICE_TEMP_DIR, 'empty')]
@@ -844,6 +866,21 @@
self.assertTrue('Permission denied' in output or
'Read-only file system' in output)
+ @requires_non_root
+ def test_push_directory_creation(self):
+ """Regression test for directory creation.
+
+ Bug: http://b/110953234
+ """
+ with tempfile.NamedTemporaryFile() as tmp_file:
+ tmp_file.write('\0' * 1024 * 1024)
+ tmp_file.flush()
+ remote_path = self.DEVICE_TEMP_DIR + '/test_push_directory_creation'
+ self.device.shell(['rm', '-rf', remote_path])
+
+ remote_path += '/filename'
+ self.device.push(local=tmp_file.name, remote=remote_path)
+
def _test_pull(self, remote_file, checksum):
tmp_write = tempfile.NamedTemporaryFile(mode='wb', delete=False)
tmp_write.close()
@@ -996,7 +1033,8 @@
if host_dir is not None:
shutil.rmtree(host_dir)
- def test_pull_symlink_dir(self):
+ # selinux prevents adbd from accessing symlinks on /data/local/tmp.
+ def disabled_test_pull_symlink_dir(self):
"""Pull a symlink to a directory of symlinks to files."""
try:
host_dir = tempfile.mkdtemp()
@@ -1163,7 +1201,7 @@
# Verify that the device ended up with the expected UTF-8 path
output = self.device.shell(
['ls', '/data/local/tmp/adb-test-*'])[0].strip()
- self.assertEqual(remote_path.encode('utf-8'), output)
+ self.assertEqual(remote_path, output)
# pull.
self.device.pull(remote_path, tf.name)
@@ -1251,16 +1289,18 @@
"""
# The values that trigger things are 507 (512 - 5 bytes from shell protocol) + 1024*n
# Probe some surrounding values as well, for the hell of it.
- for length in [506, 507, 508, 1018, 1019, 1020, 1530, 1531, 1532]:
- cmd = ['dd', 'if=/dev/zero', 'bs={}'.format(length), 'count=1', '2>/dev/null;'
- 'echo', 'foo']
- rc, stdout, _ = self.device.shell_nocheck(cmd)
+ for base in [512] + range(1024, 1024 * 16, 1024):
+ for offset in [-6, -5, -4]:
+ length = base + offset
+ cmd = ['dd', 'if=/dev/zero', 'bs={}'.format(length), 'count=1', '2>/dev/null;'
+ 'echo', 'foo']
+ rc, stdout, _ = self.device.shell_nocheck(cmd)
- self.assertEqual(0, rc)
+ self.assertEqual(0, rc)
- # Output should be '\0' * length, followed by "foo\n"
- self.assertEqual(length, len(stdout) - 4)
- self.assertEqual(stdout, "\0" * length + "foo\n")
+ # Output should be '\0' * length, followed by "foo\n"
+ self.assertEqual(length, len(stdout) - 4)
+ self.assertEqual(stdout, "\0" * length + "foo\n")
def main():
diff --git a/adb/transport.cpp b/adb/transport.cpp
index 3329f0f..332e0f8 100644
--- a/adb/transport.cpp
+++ b/adb/transport.cpp
@@ -17,6 +17,8 @@
#define TRACE_TAG TRANSPORT
#include "sysdeps.h"
+#include "sysdeps/memory.h"
+
#include "transport.h"
#include <ctype.h>
@@ -31,6 +33,7 @@
#include <deque>
#include <list>
#include <mutex>
+#include <set>
#include <thread>
#include <android-base/logging.h>
@@ -47,8 +50,11 @@
#include "adb_trace.h"
#include "adb_utils.h"
#include "fdevent.h"
+#include "sysdeps/chrono.h"
-static void transport_unref(atransport *t);
+static void register_transport(atransport* transport);
+static void remove_transport(atransport* transport);
+static void transport_unref(atransport* transport);
// TODO: unordered_map<TransportId, atransport*>
static auto& transport_list = *new std::list<atransport*>();
@@ -62,6 +68,186 @@
const char* const kFeatureLibusb = "libusb";
const char* const kFeaturePushSync = "push_sync";
+namespace {
+
+// A class that helps the Clang Thread Safety Analysis deal with
+// std::unique_lock. Given that std::unique_lock is movable, and the analysis
+// can not currently perform alias analysis, it is not annotated. In order to
+// assert that the mutex is held, a ScopedAssumeLocked can be created just after
+// the std::unique_lock.
+class SCOPED_CAPABILITY ScopedAssumeLocked {
+ public:
+ ScopedAssumeLocked(std::mutex& mutex) ACQUIRE(mutex) {}
+ ~ScopedAssumeLocked() RELEASE() {}
+};
+
+#if ADB_HOST
+// Tracks and handles atransport*s that are attempting reconnection.
+class ReconnectHandler {
+ public:
+ ReconnectHandler() = default;
+ ~ReconnectHandler() = default;
+
+ // Starts the ReconnectHandler thread.
+ void Start();
+
+ // Requests the ReconnectHandler thread to stop.
+ void Stop();
+
+ // Adds the atransport* to the queue of reconnect attempts.
+ void TrackTransport(atransport* transport);
+
+ // Wake up the ReconnectHandler thread to have it check for kicked transports.
+ void CheckForKicked();
+
+ private:
+ // The main thread loop.
+ void Run();
+
+ // Tracks a reconnection attempt.
+ struct ReconnectAttempt {
+ atransport* transport;
+ std::chrono::steady_clock::time_point reconnect_time;
+ size_t attempts_left;
+
+ bool operator<(const ReconnectAttempt& rhs) const {
+ if (reconnect_time == rhs.reconnect_time) {
+ return reinterpret_cast<uintptr_t>(transport) <
+ reinterpret_cast<uintptr_t>(rhs.transport);
+ }
+ return reconnect_time < rhs.reconnect_time;
+ }
+ };
+
+ // Only retry for up to one minute.
+ static constexpr const std::chrono::seconds kDefaultTimeout = 10s;
+ static constexpr const size_t kMaxAttempts = 6;
+
+ // Protects all members.
+ std::mutex reconnect_mutex_;
+ bool running_ GUARDED_BY(reconnect_mutex_) = true;
+ std::thread handler_thread_;
+ std::condition_variable reconnect_cv_;
+ std::set<ReconnectAttempt> reconnect_queue_ GUARDED_BY(reconnect_mutex_);
+
+ DISALLOW_COPY_AND_ASSIGN(ReconnectHandler);
+};
+
+void ReconnectHandler::Start() {
+ check_main_thread();
+ handler_thread_ = std::thread(&ReconnectHandler::Run, this);
+}
+
+void ReconnectHandler::Stop() {
+ check_main_thread();
+ {
+ std::lock_guard<std::mutex> lock(reconnect_mutex_);
+ running_ = false;
+ }
+ reconnect_cv_.notify_one();
+ handler_thread_.join();
+
+ // Drain the queue to free all resources.
+ std::lock_guard<std::mutex> lock(reconnect_mutex_);
+ while (!reconnect_queue_.empty()) {
+ ReconnectAttempt attempt = *reconnect_queue_.begin();
+ reconnect_queue_.erase(reconnect_queue_.begin());
+ remove_transport(attempt.transport);
+ }
+}
+
+void ReconnectHandler::TrackTransport(atransport* transport) {
+ check_main_thread();
+ {
+ std::lock_guard<std::mutex> lock(reconnect_mutex_);
+ if (!running_) return;
+ // Arbitrary sleep to give adbd time to get ready, if we disconnected because it exited.
+ auto reconnect_time = std::chrono::steady_clock::now() + 250ms;
+ reconnect_queue_.emplace(
+ ReconnectAttempt{transport, reconnect_time, ReconnectHandler::kMaxAttempts});
+ }
+ reconnect_cv_.notify_one();
+}
+
+void ReconnectHandler::CheckForKicked() {
+ reconnect_cv_.notify_one();
+}
+
+void ReconnectHandler::Run() {
+ while (true) {
+ ReconnectAttempt attempt;
+ {
+ std::unique_lock<std::mutex> lock(reconnect_mutex_);
+ ScopedAssumeLocked assume_lock(reconnect_mutex_);
+
+ if (!reconnect_queue_.empty()) {
+ // FIXME: libstdc++ (used on Windows) implements condition_variable with
+ // system_clock as its clock, so we're probably hosed if the clock changes,
+ // even if we use steady_clock throughout. This problem goes away once we
+ // switch to libc++.
+ reconnect_cv_.wait_until(lock, reconnect_queue_.begin()->reconnect_time);
+ } else {
+ reconnect_cv_.wait(lock);
+ }
+
+ if (!running_) return;
+
+ // Scan the whole list for kicked transports, so that we immediately handle an explicit
+ // disconnect request.
+ bool kicked = false;
+ for (auto it = reconnect_queue_.begin(); it != reconnect_queue_.end();) {
+ if (it->transport->kicked()) {
+ D("transport %s was kicked. giving up on it.", it->transport->serial.c_str());
+ remove_transport(it->transport);
+ it = reconnect_queue_.erase(it);
+ } else {
+ ++it;
+ }
+ kicked = true;
+ }
+
+ if (reconnect_queue_.empty()) continue;
+
+ // Go back to sleep if we either woke up spuriously, or we were woken up to remove
+ // a kicked transport, and the first transport isn't ready for reconnection yet.
+ auto now = std::chrono::steady_clock::now();
+ if (reconnect_queue_.begin()->reconnect_time > now) {
+ continue;
+ }
+
+ attempt = *reconnect_queue_.begin();
+ reconnect_queue_.erase(reconnect_queue_.begin());
+ }
+ D("attempting to reconnect %s", attempt.transport->serial.c_str());
+
+ if (!attempt.transport->Reconnect()) {
+ D("attempting to reconnect %s failed.", attempt.transport->serial.c_str());
+ if (attempt.attempts_left == 0) {
+ D("transport %s exceeded the number of retry attempts. giving up on it.",
+ attempt.transport->serial.c_str());
+ remove_transport(attempt.transport);
+ continue;
+ }
+
+ std::lock_guard<std::mutex> lock(reconnect_mutex_);
+ reconnect_queue_.emplace(ReconnectAttempt{
+ attempt.transport,
+ std::chrono::steady_clock::now() + ReconnectHandler::kDefaultTimeout,
+ attempt.attempts_left - 1});
+ continue;
+ }
+
+ D("reconnection to %s succeeded.", attempt.transport->serial.c_str());
+ register_transport(attempt.transport);
+ }
+}
+
+static auto& reconnect_handler = *new ReconnectHandler();
+
+#endif
+
+} // namespace
+
TransportId NextTransportId() {
static std::atomic<TransportId> next(1);
return next++;
@@ -76,10 +262,16 @@
}
void BlockingConnectionAdapter::Start() {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (started_) {
+ LOG(FATAL) << "BlockingConnectionAdapter(" << this->transport_name_
+ << "): started multiple times";
+ }
+
read_thread_ = std::thread([this]() {
LOG(INFO) << this->transport_name_ << ": read thread spawning";
while (true) {
- std::unique_ptr<apacket> packet(new apacket());
+ auto packet = std::make_unique<apacket>();
if (!underlying_->Read(packet.get())) {
PLOG(INFO) << this->transport_name_ << ": read failed";
break;
@@ -93,7 +285,10 @@
LOG(INFO) << this->transport_name_ << ": write thread spawning";
while (true) {
std::unique_lock<std::mutex> lock(mutex_);
- cv_.wait(lock, [this]() { return this->stopped_ || !this->write_queue_.empty(); });
+ ScopedAssumeLocked assume_locked(mutex_);
+ cv_.wait(lock, [this]() REQUIRES(mutex_) {
+ return this->stopped_ || !this->write_queue_.empty();
+ });
if (this->stopped_) {
return;
@@ -109,25 +304,44 @@
}
std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "write failed"); });
});
+
+ started_ = true;
}
void BlockingConnectionAdapter::Stop() {
- std::unique_lock<std::mutex> lock(mutex_);
- if (stopped_) {
- LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): already stopped";
- return;
- }
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (!started_) {
+ LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): not started";
+ return;
+ }
- stopped_ = true;
- lock.unlock();
+ if (stopped_) {
+ LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_
+ << "): already stopped";
+ return;
+ }
+
+ stopped_ = true;
+ }
LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopping";
this->underlying_->Close();
-
this->cv_.notify_one();
- read_thread_.join();
- write_thread_.join();
+
+ // Move the threads out into locals with the lock taken, and then unlock to let them exit.
+ std::thread read_thread;
+ std::thread write_thread;
+
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ read_thread = std::move(read_thread_);
+ write_thread = std::move(write_thread_);
+ }
+
+ read_thread.join();
+ write_thread.join();
LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopped";
std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "requested stop"); });
@@ -135,7 +349,7 @@
bool BlockingConnectionAdapter::Write(std::unique_ptr<apacket> packet) {
{
- std::unique_lock<std::mutex> lock(this->mutex_);
+ std::lock_guard<std::mutex> lock(this->mutex_);
write_queue_.emplace_back(std::move(packet));
}
@@ -230,14 +444,14 @@
p->msg.data_check = calculate_apacket_checksum(p);
}
- VLOG(TRANSPORT) << dump_packet(t->serial, "to remote", p);
+ VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "to remote", p);
- if (t == NULL) {
+ if (t == nullptr) {
fatal("Transport is null");
}
if (t->Write(p) != 0) {
- D("%s: failed to enqueue packet, closing transport", t->serial);
+ D("%s: failed to enqueue packet, closing transport", t->serial.c_str());
t->Kick();
}
}
@@ -251,11 +465,15 @@
if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
t->Kick();
}
+
+#if ADB_HOST
+ reconnect_handler.CheckForKicked();
+#endif
}
static int transport_registration_send = -1;
static int transport_registration_recv = -1;
-static fdevent transport_registration_fde;
+static fdevent* transport_registration_fde;
#if ADB_HOST
@@ -295,14 +513,14 @@
D("device tracker %p removed", tracker);
if (peer) {
- peer->peer = NULL;
+ peer->peer = nullptr;
peer->close(peer);
}
device_tracker_remove(tracker);
delete tracker;
}
-static int device_tracker_enqueue(asocket* socket, std::string) {
+static int device_tracker_enqueue(asocket* socket, apacket::payload_type) {
/* you can't read from a device tracker, close immediately */
device_tracker_close(socket);
return -1;
@@ -311,7 +529,7 @@
static int device_tracker_send(device_tracker* tracker, const std::string& string) {
asocket* peer = tracker->socket.peer;
- std::string data;
+ apacket::payload_type data;
data.resize(4 + string.size());
char buf[5];
snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size()));
@@ -432,8 +650,6 @@
return 0;
}
-static void remove_transport(atransport*);
-
static void transport_registration_func(int _fd, unsigned ev, void*) {
tmsg m;
atransport* t;
@@ -449,19 +665,13 @@
t = m.transport;
if (m.action == 0) {
- D("transport: %s deleting", t->serial);
+ D("transport: %s deleting", t->serial.c_str());
{
std::lock_guard<std::recursive_mutex> lock(transport_lock);
transport_list.remove(t);
}
- if (t->product) free(t->product);
- if (t->serial) free(t->serial);
- if (t->model) free(t->model);
- if (t->device) free(t->device);
- if (t->devpath) free(t->devpath);
-
delete t;
update_transports();
@@ -470,31 +680,32 @@
/* don't create transport threads for inaccessible devices */
if (t->GetConnectionState() != kCsNoPerm) {
- /* initial references are the two threads */
- t->ref_count = 1;
- t->connection->SetTransportName(t->serial_name());
- t->connection->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) {
+ // The connection gets a reference to the atransport. It will release it
+ // upon a read/write error.
+ t->ref_count++;
+ t->connection()->SetTransportName(t->serial_name());
+ t->connection()->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) {
if (!check_header(p.get(), t)) {
- D("%s: remote read: bad header", t->serial);
+ D("%s: remote read: bad header", t->serial.c_str());
return false;
}
- VLOG(TRANSPORT) << dump_packet(t->serial, "from remote", p.get());
+ VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "from remote", p.get());
apacket* packet = p.release();
// TODO: Does this need to run on the main thread?
fdevent_run_on_main_thread([packet, t]() { handle_packet(packet, t); });
return true;
});
- t->connection->SetErrorCallback([t](Connection*, const std::string& error) {
- D("%s: connection terminated: %s", t->serial, error.c_str());
+ t->connection()->SetErrorCallback([t](Connection*, const std::string& error) {
+ D("%s: connection terminated: %s", t->serial.c_str(), error.c_str());
fdevent_run_on_main_thread([t]() {
handle_offline(t);
transport_unref(t);
});
});
- t->connection->Start();
+ t->connection()->Start();
#if ADB_HOST
send_connect(t);
#endif
@@ -502,13 +713,22 @@
{
std::lock_guard<std::recursive_mutex> lock(transport_lock);
- pending_list.remove(t);
- transport_list.push_front(t);
+ auto it = std::find(pending_list.begin(), pending_list.end(), t);
+ if (it != pending_list.end()) {
+ pending_list.remove(t);
+ transport_list.push_front(t);
+ }
}
update_transports();
}
+#if ADB_HOST
+void init_reconnect_handler(void) {
+ reconnect_handler.Start();
+}
+#endif
+
void init_transport_registration(void) {
int s[2];
@@ -520,13 +740,15 @@
transport_registration_send = s[0];
transport_registration_recv = s[1];
- fdevent_install(&transport_registration_fde, transport_registration_recv,
- transport_registration_func, 0);
-
- fdevent_set(&transport_registration_fde, FDE_READ);
+ transport_registration_fde =
+ fdevent_create(transport_registration_recv, transport_registration_func, nullptr);
+ fdevent_set(transport_registration_fde, FDE_READ);
}
void kick_all_transports() {
+#if ADB_HOST
+ reconnect_handler.Stop();
+#endif
// To avoid only writing part of a packet to a transport after exit, kick all transports.
std::lock_guard<std::recursive_mutex> lock(transport_lock);
for (auto t : transport_list) {
@@ -539,7 +761,7 @@
tmsg m;
m.transport = transport;
m.action = 1;
- D("transport: %s registered", transport->serial);
+ D("transport: %s registered", transport->serial.c_str());
if (transport_write_action(transport_registration_send, &m)) {
fatal_errno("cannot write transport registration socket\n");
}
@@ -549,48 +771,60 @@
tmsg m;
m.transport = transport;
m.action = 0;
- D("transport: %s removed", transport->serial);
+ D("transport: %s removed", transport->serial.c_str());
if (transport_write_action(transport_registration_send, &m)) {
fatal_errno("cannot write transport registration socket\n");
}
}
static void transport_unref(atransport* t) {
+ check_main_thread();
CHECK(t != nullptr);
std::lock_guard<std::recursive_mutex> lock(transport_lock);
CHECK_GT(t->ref_count, 0u);
t->ref_count--;
if (t->ref_count == 0) {
- D("transport: %s unref (kicking and closing)", t->serial);
- t->connection->Stop();
+ t->connection()->Stop();
+#if ADB_HOST
+ if (t->IsTcpDevice() && !t->kicked()) {
+ D("transport: %s unref (attempting reconnection)", t->serial.c_str());
+ reconnect_handler.TrackTransport(t);
+ } else {
+ D("transport: %s unref (kicking and closing)", t->serial.c_str());
+ remove_transport(t);
+ }
+#else
+ D("transport: %s unref (kicking and closing)", t->serial.c_str());
remove_transport(t);
+#endif
+
} else {
- D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
+ D("transport: %s unref (count=%zu)", t->serial.c_str(), t->ref_count);
}
}
-static int qual_match(const char* to_test, const char* prefix, const char* qual,
+static int qual_match(const std::string& to_test, const char* prefix, const std::string& qual,
bool sanitize_qual) {
- if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
- return !qual || !*qual;
+ if (to_test.empty()) /* Return true if both the qual and to_test are empty strings. */
+ return qual.empty();
- if (!qual) return 0;
+ if (qual.empty()) return 0;
+ const char* ptr = to_test.c_str();
if (prefix) {
while (*prefix) {
- if (*prefix++ != *to_test++) return 0;
+ if (*prefix++ != *ptr++) return 0;
}
}
- while (*qual) {
- char ch = *qual++;
+ for (char ch : qual) {
if (sanitize_qual && !isalnum(ch)) ch = '_';
- if (ch != *to_test++) return 0;
+ if (ch != *ptr++) return 0;
}
- /* Everything matched so far. Return true if *to_test is a NUL. */
- return !*to_test;
+ /* Everything matched so far. Return true if *ptr is a NUL. */
+ return !*ptr;
}
atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
@@ -663,22 +897,41 @@
}
lock.unlock();
- // Don't return unauthorized devices; the caller can't do anything with them.
- if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) {
- *error_out = "device unauthorized.\n";
- char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
- *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
- *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
- *error_out += "\n";
- *error_out += "Try 'adb kill-server' if that seems wrong.\n";
- *error_out += "Otherwise check for a confirmation dialog on your device.";
- result = nullptr;
- }
+ if (result && !accept_any_state) {
+ // The caller requires an active transport.
+ // Make sure that we're actually connected.
+ ConnectionState state = result->GetConnectionState();
+ switch (state) {
+ case kCsConnecting:
+ *error_out = "device still connecting";
+ result = nullptr;
+ break;
- // Don't return offline devices; the caller can't do anything with them.
- if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) {
- *error_out = "device offline";
- result = nullptr;
+ case kCsAuthorizing:
+ *error_out = "device still authorizing";
+ result = nullptr;
+ break;
+
+ case kCsUnauthorized: {
+ *error_out = "device unauthorized.\n";
+ char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
+ *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
+ *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
+ *error_out += "\n";
+ *error_out += "Try 'adb kill-server' if that seems wrong.\n";
+ *error_out += "Otherwise check for a confirmation dialog on your device.";
+ result = nullptr;
+ break;
+ }
+
+ case kCsOffline:
+ *error_out = "device offline";
+ result = nullptr;
+ break;
+
+ default:
+ break;
+ }
}
if (result) {
@@ -688,15 +941,38 @@
return result;
}
+bool ConnectionWaitable::WaitForConnection(std::chrono::milliseconds timeout) {
+ std::unique_lock<std::mutex> lock(mutex_);
+ ScopedAssumeLocked assume_locked(mutex_);
+ return cv_.wait_for(lock, timeout, [&]() REQUIRES(mutex_) {
+ return connection_established_ready_;
+ }) && connection_established_;
+}
+
+void ConnectionWaitable::SetConnectionEstablished(bool success) {
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (connection_established_ready_) return;
+ connection_established_ready_ = true;
+ connection_established_ = success;
+ D("connection established with %d", success);
+ }
+ cv_.notify_one();
+}
+
+atransport::~atransport() {
+ // If the connection callback had not been run before, run it now.
+ SetConnectionEstablished(false);
+}
+
int atransport::Write(apacket* p) {
- return this->connection->Write(std::unique_ptr<apacket>(p)) ? 0 : -1;
+ return this->connection()->Write(std::unique_ptr<apacket>(p)) ? 0 : -1;
}
void atransport::Kick() {
- if (!kicked_) {
- D("kicking transport %s", this->serial);
- kicked_ = true;
- this->connection->Stop();
+ if (!kicked_.exchange(true)) {
+ D("kicking transport %p %s", this, this->serial.c_str());
+ this->connection()->Stop();
}
}
@@ -709,6 +985,11 @@
connection_state_ = state;
}
+void atransport::SetConnection(std::unique_ptr<Connection> connection) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ connection_ = std::shared_ptr<Connection>(std::move(connection));
+}
+
std::string atransport::connection_state_name() const {
ConnectionState state = GetConnectionState();
switch (state) {
@@ -728,6 +1009,10 @@
return "sideload";
case kCsUnauthorized:
return "unauthorized";
+ case kCsAuthorizing:
+ return "authorizing";
+ case kCsConnecting:
+ return "connecting";
default:
return "unknown";
}
@@ -805,7 +1090,7 @@
}
bool atransport::MatchesTarget(const std::string& target) const {
- if (serial) {
+ if (!serial.empty()) {
if (target == serial) {
return true;
} else if (type == kTransportLocal) {
@@ -834,10 +1119,17 @@
}
}
- return (devpath && target == devpath) ||
- qual_match(target.c_str(), "product:", product, false) ||
- qual_match(target.c_str(), "model:", model, true) ||
- qual_match(target.c_str(), "device:", device, false);
+ return (target == devpath) || qual_match(target, "product:", product, false) ||
+ qual_match(target, "model:", model, true) ||
+ qual_match(target, "device:", device, false);
+}
+
+void atransport::SetConnectionEstablished(bool success) {
+ connection_waitable_->SetConnectionEstablished(success);
+}
+
+bool atransport::Reconnect() {
+ return reconnect_(this);
}
#if ADB_HOST
@@ -850,9 +1142,9 @@
return str;
}
-static void append_transport_info(std::string* result, const char* key, const char* value,
+static void append_transport_info(std::string* result, const char* key, const std::string& value,
bool alphanumeric) {
- if (value == nullptr || *value == '\0') {
+ if (value.empty()) {
return;
}
@@ -862,8 +1154,8 @@
}
static void append_transport(const atransport* t, std::string* result, bool long_listing) {
- const char* serial = t->serial;
- if (!serial || !serial[0]) {
+ std::string serial = t->serial;
+ if (serial.empty()) {
serial = "(no serial number)";
}
@@ -872,7 +1164,8 @@
*result += '\t';
*result += t->connection_state_name();
} else {
- android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
+ android::base::StringAppendF(result, "%-22s %s", serial.c_str(),
+ t->connection_state_name().c_str());
append_transport_info(result, "", t->devpath, false);
append_transport_info(result, "product:", t->product, false);
@@ -895,7 +1188,7 @@
if (x->type != y->type) {
return x->type < y->type;
}
- return strcmp(x->serial, y->serial) < 0;
+ return x->serial < y->serial;
});
std::string result;
@@ -920,47 +1213,62 @@
}
#endif // ADB_HOST
-int register_socket_transport(int s, const char* serial, int port, int local) {
- atransport* t = new atransport();
+bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
+ atransport::ReconnectCallback reconnect, int* error) {
+ atransport* t = new atransport(std::move(reconnect), kCsOffline);
- if (!serial) {
- char buf[32];
- snprintf(buf, sizeof(buf), "T-%p", t);
- serial = buf;
- }
-
- D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
- if (init_socket_transport(t, s, port, local) < 0) {
+ D("transport: %s init'ing for socket %d, on port %d", serial.c_str(), s.get(), port);
+ if (init_socket_transport(t, std::move(s), port, local) < 0) {
delete t;
- return -1;
+ if (error) *error = errno;
+ return false;
}
std::unique_lock<std::recursive_mutex> lock(transport_lock);
for (const auto& transport : pending_list) {
- if (transport->serial && strcmp(serial, transport->serial) == 0) {
+ if (serial == transport->serial) {
VLOG(TRANSPORT) << "socket transport " << transport->serial
<< " is already in pending_list and fails to register";
delete t;
- return -1;
+ if (error) *error = EALREADY;
+ return false;
}
}
for (const auto& transport : transport_list) {
- if (transport->serial && strcmp(serial, transport->serial) == 0) {
+ if (serial == transport->serial) {
VLOG(TRANSPORT) << "socket transport " << transport->serial
<< " is already in transport_list and fails to register";
delete t;
- return -1;
+ if (error) *error = EALREADY;
+ return false;
}
}
+ t->serial = std::move(serial);
pending_list.push_front(t);
- t->serial = strdup(serial);
lock.unlock();
+ auto waitable = t->connection_waitable();
register_transport(t);
- return 0;
+
+ if (local == 1) {
+ // Do not wait for emulator transports.
+ return true;
+ }
+
+ if (!waitable->WaitForConnection(std::chrono::seconds(10))) {
+ if (error) *error = ETIMEDOUT;
+ return false;
+ }
+
+ if (t->GetConnectionState() == kCsUnauthorized) {
+ if (error) *error = EPERM;
+ return false;
+ }
+
+ return true;
}
#if ADB_HOST
@@ -969,7 +1277,7 @@
std::lock_guard<std::recursive_mutex> lock(transport_lock);
for (auto& t : transport_list) {
- if (t->serial && strcmp(serial, t->serial) == 0) {
+ if (strcmp(serial, t->serial.c_str()) == 0) {
result = t;
break;
}
@@ -989,22 +1297,25 @@
t->Kick();
}
}
+#if ADB_HOST
+ reconnect_handler.CheckForKicked();
+#endif
}
#endif
void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
unsigned writeable) {
- atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm));
+ atransport* t = new atransport(writeable ? kCsOffline : kCsNoPerm);
D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
init_usb_transport(t, usb);
if (serial) {
- t->serial = strdup(serial);
+ t->serial = serial;
}
if (devpath) {
- t->devpath = strdup(devpath);
+ t->devpath = devpath;
}
{
@@ -1019,8 +1330,9 @@
void unregister_usb_transport(usb_handle* usb) {
std::lock_guard<std::recursive_mutex> lock(transport_lock);
transport_list.remove_if([usb](atransport* t) {
- if (auto connection = dynamic_cast<UsbConnection*>(t->connection.get())) {
- return connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm;
+ auto connection = t->connection();
+ if (auto usb_connection = dynamic_cast<UsbConnection*>(connection.get())) {
+ return usb_connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm;
}
return false;
});
diff --git a/adb/transport.h b/adb/transport.h
index 8b71e34..f362f24 100644
--- a/adb/transport.h
+++ b/adb/transport.h
@@ -20,6 +20,7 @@
#include <sys/types.h>
#include <atomic>
+#include <chrono>
#include <condition_variable>
#include <deque>
#include <functional>
@@ -30,6 +31,8 @@
#include <thread>
#include <unordered_set>
+#include <android-base/macros.h>
+#include <android-base/thread_annotations.h>
#include <openssl/rsa.h>
#include "adb.h"
@@ -89,6 +92,8 @@
std::string transport_name_;
ReadCallback read_callback_;
ErrorCallback error_callback_;
+
+ static std::unique_ptr<Connection> FromFd(unique_fd fd);
};
// Abstraction for a blocking packet transport.
@@ -121,13 +126,14 @@
virtual void Start() override final;
virtual void Stop() override final;
- bool stopped_ = false;
+ bool started_ GUARDED_BY(mutex_) = false;
+ bool stopped_ GUARDED_BY(mutex_) = false;
std::unique_ptr<BlockingConnection> underlying_;
- std::thread read_thread_;
- std::thread write_thread_;
+ std::thread read_thread_ GUARDED_BY(mutex_);
+ std::thread write_thread_ GUARDED_BY(mutex_);
- std::deque<std::unique_ptr<apacket>> write_queue_;
+ std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
std::mutex mutex_;
std::condition_variable cv_;
@@ -158,6 +164,35 @@
usb_handle* handle_;
};
+// Waits for a transport's connection to be not pending. This is a separate
+// object so that the transport can be destroyed and another thread can be
+// notified of it in a race-free way.
+class ConnectionWaitable {
+ public:
+ ConnectionWaitable() = default;
+ ~ConnectionWaitable() = default;
+
+ // Waits until the first CNXN packet has been received by the owning
+ // atransport, or the specified timeout has elapsed. Can be called from any
+ // thread.
+ //
+ // Returns true if the CNXN packet was received in a timely fashion, false
+ // otherwise.
+ bool WaitForConnection(std::chrono::milliseconds timeout);
+
+ // Can be called from any thread when the connection stops being pending.
+ // Only the first invocation will be acknowledged, the rest will be no-ops.
+ void SetConnectionEstablished(bool success);
+
+ private:
+ bool connection_established_ GUARDED_BY(mutex_) = false;
+ bool connection_established_ready_ GUARDED_BY(mutex_) = false;
+ std::mutex mutex_;
+ std::condition_variable cv_;
+
+ DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable);
+};
+
class atransport {
public:
// TODO(danalbert): We expose waaaaaaay too much stuff because this was
@@ -165,35 +200,49 @@
// class in one go is a very large change. Given how bad our testing is,
// it's better to do this piece by piece.
- atransport(ConnectionState state = kCsOffline)
- : id(NextTransportId()), connection_state_(state) {
+ using ReconnectCallback = std::function<bool(atransport*)>;
+
+ atransport(ReconnectCallback reconnect, ConnectionState state)
+ : id(NextTransportId()),
+ kicked_(false),
+ connection_state_(state),
+ connection_waitable_(std::make_shared<ConnectionWaitable>()),
+ connection_(nullptr),
+ reconnect_(std::move(reconnect)) {
// Initialize protocol to min version for compatibility with older versions.
// Version will be updated post-connect.
protocol_version = A_VERSION_MIN;
max_payload = MAX_PAYLOAD;
}
- virtual ~atransport() {}
+ atransport(ConnectionState state = kCsOffline)
+ : atransport([](atransport*) { return false; }, state) {}
+ virtual ~atransport();
int Write(apacket* p);
void Kick();
+ bool kicked() const { return kicked_; }
// ConnectionState can be read by all threads, but can only be written in the main thread.
ConnectionState GetConnectionState() const;
void SetConnectionState(ConnectionState state);
+ void SetConnection(std::unique_ptr<Connection> connection);
+ std::shared_ptr<Connection> connection() {
+ std::lock_guard<std::mutex> lock(mutex_);
+ return connection_;
+ }
+
const TransportId id;
size_t ref_count = 0;
bool online = false;
TransportType type = kTransportAny;
- std::unique_ptr<Connection> connection;
-
// Used to identify transports for clients.
- char* serial = nullptr;
- char* product = nullptr;
- char* model = nullptr;
- char* device = nullptr;
- char* devpath = nullptr;
+ std::string serial;
+ std::string product;
+ std::string model;
+ std::string device;
+ std::string devpath;
bool IsTcpDevice() const { return type == kTransportLocal; }
@@ -204,7 +253,7 @@
char token[TOKEN_SIZE] = {};
size_t failed_auth_attempts = 0;
- std::string serial_name() const { return serial ? serial : "<unknown>"; }
+ std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
std::string connection_state_name() const;
void update_version(int version, size_t payload);
@@ -239,8 +288,19 @@
// This is to make it easier to use the same network target for both fastboot and adb.
bool MatchesTarget(const std::string& target) const;
-private:
- bool kicked_ = false;
+ // Notifies that the atransport is no longer waiting for the connection
+ // being established.
+ void SetConnectionEstablished(bool success);
+
+ // Gets a shared reference to the ConnectionWaitable.
+ std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; }
+
+ // Attempts to reconnect with the underlying Connection. Returns true if the
+ // reconnection attempt succeeded.
+ bool Reconnect();
+
+ private:
+ std::atomic<bool> kicked_;
// A set of features transmitted in the banner with the initial connection.
// This is stored in the banner as 'features=feature0,feature1,etc'.
@@ -256,6 +316,18 @@
std::deque<std::shared_ptr<RSA>> keys_;
#endif
+ // A sharable object that can be used to wait for the atransport's
+ // connection to be established.
+ std::shared_ptr<ConnectionWaitable> connection_waitable_;
+
+ // The underlying connection object.
+ std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
+
+ // A callback that will be invoked when the atransport needs to reconnect.
+ ReconnectCallback reconnect_;
+
+ std::mutex mutex_;
+
DISALLOW_COPY_AND_ASSIGN(atransport);
};
@@ -277,6 +349,7 @@
// Stops iteration and returns false if fn returns false, otherwise returns true.
bool iterate_transports(std::function<bool(const atransport*)> fn);
+void init_reconnect_handler(void);
void init_transport_registration(void);
void init_mdns_transport_discovery(void);
std::string list_transports(bool long_listing);
@@ -291,7 +364,8 @@
void connect_device(const std::string& address, std::string* response);
/* cause new transports to be init'd and added to the list */
-int register_socket_transport(int s, const char* serial, int port, int local);
+bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
+ atransport::ReconnectCallback reconnect, int* error = nullptr);
// This should only be used for transports with connection_state == kCsNoPerm.
void unregister_usb_transport(usb_handle* usb);
diff --git a/adb/transport_benchmark.cpp b/adb/transport_benchmark.cpp
new file mode 100644
index 0000000..022808f
--- /dev/null
+++ b/adb/transport_benchmark.cpp
@@ -0,0 +1,194 @@
+/*
+ * Copyright (C) 2018 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 <malloc.h>
+#include <stdio.h>
+
+#include <android-base/logging.h>
+#include <benchmark/benchmark.h>
+
+#include "adb_trace.h"
+#include "sysdeps.h"
+#include "transport.h"
+
+#define ADB_CONNECTION_BENCHMARK(benchmark_name, ...) \
+ BENCHMARK_TEMPLATE(benchmark_name, FdConnection, ##__VA_ARGS__) \
+ ->Arg(1) \
+ ->Arg(16384) \
+ ->Arg(MAX_PAYLOAD) \
+ ->UseRealTime(); \
+ BENCHMARK_TEMPLATE(benchmark_name, NonblockingFdConnection, ##__VA_ARGS__) \
+ ->Arg(1) \
+ ->Arg(16384) \
+ ->Arg(MAX_PAYLOAD) \
+ ->UseRealTime()
+
+struct NonblockingFdConnection;
+template <typename ConnectionType>
+std::unique_ptr<Connection> MakeConnection(unique_fd fd);
+
+template <>
+std::unique_ptr<Connection> MakeConnection<FdConnection>(unique_fd fd) {
+ auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
+ return std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection));
+}
+
+template <>
+std::unique_ptr<Connection> MakeConnection<NonblockingFdConnection>(unique_fd fd) {
+ return Connection::FromFd(std::move(fd));
+}
+
+template <typename ConnectionType>
+void BM_Connection_Unidirectional(benchmark::State& state) {
+ int fds[2];
+ if (adb_socketpair(fds) != 0) {
+ LOG(FATAL) << "failed to create socketpair";
+ }
+
+ auto client = MakeConnection<ConnectionType>(unique_fd(fds[0]));
+ auto server = MakeConnection<ConnectionType>(unique_fd(fds[1]));
+
+ std::atomic<size_t> received_bytes;
+
+ client->SetReadCallback([](Connection*, std::unique_ptr<apacket>) -> bool { return true; });
+ server->SetReadCallback([&received_bytes](Connection*, std::unique_ptr<apacket> packet) -> bool {
+ received_bytes += packet->payload.size();
+ return true;
+ });
+
+ client->SetErrorCallback(
+ [](Connection*, const std::string& error) { LOG(INFO) << "client closed: " << error; });
+ server->SetErrorCallback(
+ [](Connection*, const std::string& error) { LOG(INFO) << "server closed: " << error; });
+
+ client->Start();
+ server->Start();
+
+ for (auto _ : state) {
+ size_t data_size = state.range(0);
+ std::unique_ptr<apacket> packet = std::make_unique<apacket>();
+ memset(&packet->msg, 0, sizeof(packet->msg));
+ packet->msg.command = A_WRTE;
+ packet->msg.data_length = data_size;
+ packet->payload.resize(data_size);
+
+ memset(&packet->payload[0], 0xff, data_size);
+
+ received_bytes = 0;
+ client->Write(std::move(packet));
+ while (received_bytes < data_size) {
+ continue;
+ }
+ }
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * state.range(0));
+
+ client->Stop();
+ server->Stop();
+}
+
+ADB_CONNECTION_BENCHMARK(BM_Connection_Unidirectional);
+
+enum class ThreadPolicy {
+ MainThread,
+ SameThread,
+};
+
+template <typename ConnectionType, enum ThreadPolicy Policy>
+void BM_Connection_Echo(benchmark::State& state) {
+ int fds[2];
+ if (adb_socketpair(fds) != 0) {
+ LOG(FATAL) << "failed to create socketpair";
+ }
+
+ auto client = MakeConnection<ConnectionType>(unique_fd(fds[0]));
+ auto server = MakeConnection<ConnectionType>(unique_fd(fds[1]));
+
+ std::atomic<size_t> received_bytes;
+
+ fdevent_reset();
+ std::thread fdevent_thread([]() { fdevent_loop(); });
+
+ client->SetReadCallback([&received_bytes](Connection*, std::unique_ptr<apacket> packet) -> bool {
+ received_bytes += packet->payload.size();
+ return true;
+ });
+
+ static const auto handle_packet = [](Connection* connection, std::unique_ptr<apacket> packet) {
+ connection->Write(std::move(packet));
+ };
+
+ server->SetReadCallback([](Connection* connection, std::unique_ptr<apacket> packet) -> bool {
+ if (Policy == ThreadPolicy::MainThread) {
+ auto raw_packet = packet.release();
+ fdevent_run_on_main_thread([connection, raw_packet]() {
+ std::unique_ptr<apacket> packet(raw_packet);
+ handle_packet(connection, std::move(packet));
+ });
+ } else {
+ handle_packet(connection, std::move(packet));
+ }
+ return true;
+ });
+
+ client->SetErrorCallback(
+ [](Connection*, const std::string& error) { LOG(INFO) << "client closed: " << error; });
+ server->SetErrorCallback(
+ [](Connection*, const std::string& error) { LOG(INFO) << "server closed: " << error; });
+
+ client->Start();
+ server->Start();
+
+ for (auto _ : state) {
+ size_t data_size = state.range(0);
+ std::unique_ptr<apacket> packet = std::make_unique<apacket>();
+ memset(&packet->msg, 0, sizeof(packet->msg));
+ packet->msg.command = A_WRTE;
+ packet->msg.data_length = data_size;
+ packet->payload.resize(data_size);
+
+ memset(&packet->payload[0], 0xff, data_size);
+
+ received_bytes = 0;
+ client->Write(std::move(packet));
+ while (received_bytes < data_size) {
+ continue;
+ }
+ }
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * state.range(0));
+
+ client->Stop();
+ server->Stop();
+
+ // TODO: Make it so that you don't need to poke the fdevent loop to make it terminate?
+ fdevent_terminate_loop();
+ fdevent_run_on_main_thread([]() {});
+
+ fdevent_thread.join();
+}
+
+ADB_CONNECTION_BENCHMARK(BM_Connection_Echo, ThreadPolicy::SameThread);
+ADB_CONNECTION_BENCHMARK(BM_Connection_Echo, ThreadPolicy::MainThread);
+
+int main(int argc, char** argv) {
+ // Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
+ mallopt(M_DECAY_TIME, 1);
+
+ android::base::SetMinimumLogSeverity(android::base::WARNING);
+ adb_trace_init(argv);
+ ::benchmark::Initialize(&argc, argv);
+ if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
+ ::benchmark::RunSpecifiedBenchmarks();
+}
diff --git a/adb/transport_fd.cpp b/adb/transport_fd.cpp
new file mode 100644
index 0000000..85f3c52
--- /dev/null
+++ b/adb/transport_fd.cpp
@@ -0,0 +1,239 @@
+/*
+ * Copyright (C) 2018 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 <stdint.h>
+
+#include <deque>
+#include <mutex>
+#include <string>
+#include <thread>
+
+#include <android-base/logging.h>
+#include <android-base/stringprintf.h>
+#include <android-base/thread_annotations.h>
+
+#include "adb_unique_fd.h"
+#include "adb_utils.h"
+#include "sysdeps.h"
+#include "sysdeps/memory.h"
+#include "transport.h"
+#include "types.h"
+
+static void CreateWakeFds(unique_fd* read, unique_fd* write) {
+ // TODO: eventfd on linux?
+ int wake_fds[2];
+ int rc = adb_socketpair(wake_fds);
+ set_file_block_mode(wake_fds[0], false);
+ set_file_block_mode(wake_fds[1], false);
+ CHECK_EQ(0, rc);
+ *read = unique_fd(wake_fds[0]);
+ *write = unique_fd(wake_fds[1]);
+}
+
+struct NonblockingFdConnection : public Connection {
+ NonblockingFdConnection(unique_fd fd) : started_(false), fd_(std::move(fd)) {
+ set_file_block_mode(fd_.get(), false);
+ CreateWakeFds(&wake_fd_read_, &wake_fd_write_);
+ }
+
+ void SetRunning(bool value) {
+ std::lock_guard<std::mutex> lock(run_mutex_);
+ running_ = value;
+ }
+
+ bool IsRunning() {
+ std::lock_guard<std::mutex> lock(run_mutex_);
+ return running_;
+ }
+
+ void Run(std::string* error) {
+ SetRunning(true);
+ while (IsRunning()) {
+ adb_pollfd pfds[2] = {
+ {.fd = fd_.get(), .events = POLLIN},
+ {.fd = wake_fd_read_.get(), .events = POLLIN},
+ };
+
+ {
+ std::lock_guard<std::mutex> lock(this->write_mutex_);
+ if (!writable_) {
+ pfds[0].events |= POLLOUT;
+ }
+ }
+
+ int rc = adb_poll(pfds, 2, -1);
+ if (rc == -1) {
+ *error = android::base::StringPrintf("poll failed: %s", strerror(errno));
+ return;
+ } else if (rc == 0) {
+ LOG(FATAL) << "poll timed out with an infinite timeout?";
+ }
+
+ if (pfds[0].revents) {
+ if ((pfds[0].revents & POLLOUT)) {
+ std::lock_guard<std::mutex> lock(this->write_mutex_);
+ WriteResult result = DispatchWrites();
+ switch (result) {
+ case WriteResult::Error:
+ *error = "write failed";
+ return;
+
+ case WriteResult::Completed:
+ writable_ = true;
+ break;
+
+ case WriteResult::TryAgain:
+ break;
+ }
+ }
+
+ if (pfds[0].revents & POLLIN) {
+ // TODO: Should we be getting blocks from a free list?
+ auto block = std::make_unique<IOVector::block_type>(MAX_PAYLOAD);
+ rc = adb_read(fd_.get(), &(*block)[0], block->size());
+ if (rc == -1) {
+ *error = std::string("read failed: ") + strerror(errno);
+ return;
+ } else if (rc == 0) {
+ *error = "read failed: EOF";
+ return;
+ }
+ block->resize(rc);
+ read_buffer_.append(std::move(block));
+
+ if (!read_header_ && read_buffer_.size() >= sizeof(amessage)) {
+ auto header_buf = read_buffer_.take_front(sizeof(amessage)).coalesce();
+ CHECK_EQ(sizeof(amessage), header_buf.size());
+ read_header_ = std::make_unique<amessage>();
+ memcpy(read_header_.get(), header_buf.data(), sizeof(amessage));
+ }
+
+ if (read_header_ && read_buffer_.size() >= read_header_->data_length) {
+ auto data_chain = read_buffer_.take_front(read_header_->data_length);
+
+ // TODO: Make apacket carry around a IOVector instead of coalescing.
+ auto payload = data_chain.coalesce<apacket::payload_type>();
+ auto packet = std::make_unique<apacket>();
+ packet->msg = *read_header_;
+ packet->payload = std::move(payload);
+ read_header_ = nullptr;
+ read_callback_(this, std::move(packet));
+ }
+ }
+ }
+
+ if (pfds[1].revents) {
+ uint64_t buf;
+ rc = adb_read(wake_fd_read_.get(), &buf, sizeof(buf));
+ CHECK_EQ(static_cast<int>(sizeof(buf)), rc);
+
+ // We were woken up either to add POLLOUT to our events, or to exit.
+ // Do nothing.
+ }
+ }
+ }
+
+ void Start() override final {
+ if (started_.exchange(true)) {
+ LOG(FATAL) << "Connection started multiple times?";
+ }
+
+ thread_ = std::thread([this]() {
+ std::string error = "connection closed";
+ Run(&error);
+ this->error_callback_(this, error);
+ });
+ }
+
+ void Stop() override final {
+ SetRunning(false);
+ WakeThread();
+ thread_.join();
+ }
+
+ void WakeThread() {
+ uint64_t buf = 0;
+ if (TEMP_FAILURE_RETRY(adb_write(wake_fd_write_.get(), &buf, sizeof(buf))) != sizeof(buf)) {
+ LOG(FATAL) << "failed to wake up thread";
+ }
+ }
+
+ enum class WriteResult {
+ Error,
+ Completed,
+ TryAgain,
+ };
+
+ WriteResult DispatchWrites() REQUIRES(write_mutex_) {
+ CHECK(!write_buffer_.empty());
+ if (!writable_) {
+ return WriteResult::TryAgain;
+ }
+
+ auto iovs = write_buffer_.iovecs();
+ ssize_t rc = adb_writev(fd_.get(), iovs.data(), iovs.size());
+ if (rc == -1) {
+ return WriteResult::Error;
+ } else if (rc == 0) {
+ errno = 0;
+ return WriteResult::Error;
+ }
+
+ // TODO: Implement a more efficient drop_front?
+ write_buffer_.take_front(rc);
+ if (write_buffer_.empty()) {
+ return WriteResult::Completed;
+ }
+
+ // There's data left in the range, which means our write returned early.
+ return WriteResult::TryAgain;
+ }
+
+ bool Write(std::unique_ptr<apacket> packet) final {
+ std::lock_guard<std::mutex> lock(write_mutex_);
+ const char* header_begin = reinterpret_cast<const char*>(&packet->msg);
+ const char* header_end = header_begin + sizeof(packet->msg);
+ auto header_block = std::make_unique<IOVector::block_type>(header_begin, header_end);
+ write_buffer_.append(std::move(header_block));
+ if (!packet->payload.empty()) {
+ write_buffer_.append(std::make_unique<IOVector::block_type>(std::move(packet->payload)));
+ }
+ return DispatchWrites() != WriteResult::Error;
+ }
+
+ std::thread thread_;
+
+ std::atomic<bool> started_;
+ std::mutex run_mutex_;
+ bool running_ GUARDED_BY(run_mutex_);
+
+ std::unique_ptr<amessage> read_header_;
+ IOVector read_buffer_;
+
+ unique_fd fd_;
+ unique_fd wake_fd_read_;
+ unique_fd wake_fd_write_;
+
+ std::mutex write_mutex_;
+ bool writable_ GUARDED_BY(write_mutex_) = true;
+ IOVector write_buffer_ GUARDED_BY(write_mutex_);
+
+ IOVector incoming_queue_;
+};
+
+std::unique_ptr<Connection> Connection::FromFd(unique_fd fd) {
+ return std::make_unique<NonblockingFdConnection>(std::move(fd));
+}
diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp
index ff395dc..1431252 100644
--- a/adb/transport_local.cpp
+++ b/adb/transport_local.cpp
@@ -45,6 +45,7 @@
#include "adb_unique_fd.h"
#include "adb_utils.h"
#include "sysdeps/chrono.h"
+#include "sysdeps/memory.h"
#if ADB_HOST
@@ -67,28 +68,24 @@
return local_connect_arbitrary_ports(port - 1, port, &dummy) == 0;
}
-void connect_device(const std::string& address, std::string* response) {
- if (address.empty()) {
- *response = "empty address";
- return;
- }
-
+std::tuple<unique_fd, int, std::string> tcp_connect(const std::string& address,
+ std::string* response) {
std::string serial;
std::string host;
int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
if (!android::base::ParseNetAddress(address, &host, &port, &serial, response)) {
- return;
+ return std::make_tuple(unique_fd(), port, serial);
}
std::string error;
- int fd = network_connect(host.c_str(), port, SOCK_STREAM, 10, &error);
+ unique_fd fd(network_connect(host.c_str(), port, SOCK_STREAM, 10, &error));
if (fd == -1) {
*response = android::base::StringPrintf("unable to connect to %s: %s",
serial.c_str(), error.c_str());
- return;
+ return std::make_tuple(std::move(fd), port, serial);
}
- D("client: connected %s remote on fd %d", serial.c_str(), fd);
+ D("client: connected %s remote on fd %d", serial.c_str(), fd.get());
close_on_exec(fd);
disable_tcp_nagle(fd);
@@ -97,10 +94,46 @@
D("warning: failed to configure TCP keepalives (%s)", strerror(errno));
}
- int ret = register_socket_transport(fd, serial.c_str(), port, 0);
- if (ret < 0) {
- adb_close(fd);
- *response = android::base::StringPrintf("already connected to %s", serial.c_str());
+ return std::make_tuple(std::move(fd), port, serial);
+}
+
+void connect_device(const std::string& address, std::string* response) {
+ if (address.empty()) {
+ *response = "empty address";
+ return;
+ }
+
+ unique_fd fd;
+ int port;
+ std::string serial;
+ std::tie(fd, port, serial) = tcp_connect(address, response);
+ auto reconnect = [address](atransport* t) {
+ std::string response;
+ unique_fd fd;
+ int port;
+ std::string serial;
+ std::tie(fd, port, serial) = tcp_connect(address, &response);
+ if (fd == -1) {
+ D("reconnect failed: %s", response.c_str());
+ return false;
+ }
+
+ // This invokes the part of register_socket_transport() that needs to be
+ // invoked if the atransport* has already been setup. This eventually
+ // calls atransport->SetConnection() with a newly created Connection*
+ // that will in turn send the CNXN packet.
+ return init_socket_transport(t, std::move(fd), port, 0) >= 0;
+ };
+
+ int error;
+ if (!register_socket_transport(std::move(fd), serial, port, 0, std::move(reconnect), &error)) {
+ if (error == EALREADY) {
+ *response = android::base::StringPrintf("already connected to %s", serial.c_str());
+ } else if (error == EPERM) {
+ *response = android::base::StringPrintf("failed to authenticate to %s", serial.c_str());
+ } else {
+ *response = android::base::StringPrintf("failed to connect to %s", serial.c_str());
+ }
} else {
*response = android::base::StringPrintf("connected to %s", serial.c_str());
}
@@ -108,7 +141,7 @@
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
- int fd = -1;
+ unique_fd fd;
#if ADB_HOST
if (find_emulator_transport_by_adb_port(adb_port) != nullptr ||
@@ -118,22 +151,22 @@
const char *host = getenv("ADBHOST");
if (host) {
- fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
+ fd.reset(network_connect(host, adb_port, SOCK_STREAM, 0, error));
}
#endif
if (fd < 0) {
- fd = network_loopback_client(adb_port, SOCK_STREAM, error);
+ fd.reset(network_loopback_client(adb_port, SOCK_STREAM, error));
}
if (fd >= 0) {
- D("client: connected on remote on fd %d", fd);
- close_on_exec(fd);
- disable_tcp_nagle(fd);
+ D("client: connected on remote on fd %d", fd.get());
+ close_on_exec(fd.get());
+ disable_tcp_nagle(fd.get());
std::string serial = getEmulatorSerialString(console_port);
- if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
+ if (register_socket_transport(std::move(fd), std::move(serial), adb_port, 1,
+ [](atransport*) { return false; })) {
return 0;
}
- adb_close(fd);
}
return -1;
}
@@ -210,33 +243,31 @@
#else // ADB_HOST
static void server_socket_thread(int port) {
- int serverfd, fd;
+ unique_fd serverfd;
adb_thread_setname("server socket");
D("transport: server_socket_thread() starting");
- serverfd = -1;
- for(;;) {
- if(serverfd == -1) {
- std::string error;
- serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
- if(serverfd < 0) {
- D("server: cannot bind socket yet: %s", error.c_str());
- std::this_thread::sleep_for(1s);
- continue;
- }
- close_on_exec(serverfd);
+ while (serverfd == -1) {
+ std::string error;
+ serverfd.reset(network_inaddr_any_server(port, SOCK_STREAM, &error));
+ if (serverfd < 0) {
+ D("server: cannot bind socket yet: %s", error.c_str());
+ std::this_thread::sleep_for(1s);
+ continue;
}
+ close_on_exec(serverfd.get());
+ }
+ while (true) {
D("server: trying to get new connection from %d", port);
- fd = adb_socket_accept(serverfd, nullptr, nullptr);
- if(fd >= 0) {
- D("server: new connection on fd %d", fd);
- close_on_exec(fd);
- disable_tcp_nagle(fd);
- std::string serial = android::base::StringPrintf("host-%d", fd);
- if (register_socket_transport(fd, serial.c_str(), port, 1) != 0) {
- adb_close(fd);
- }
+ unique_fd fd(adb_socket_accept(serverfd, nullptr, nullptr));
+ if (fd >= 0) {
+ D("server: new connection on fd %d", fd.get());
+ close_on_exec(fd.get());
+ disable_tcp_nagle(fd.get());
+ std::string serial = android::base::StringPrintf("host-%d", fd.get());
+ register_socket_transport(std::move(fd), std::move(serial), port, 1,
+ [](atransport*) { return false; });
}
}
D("transport: server_socket_thread() exiting");
@@ -297,7 +328,6 @@
/* 'ok' reply from the adb QEMUD service. */
static const char _ok_resp[] = "ok";
- int fd;
char tmp[256];
char con_name[32];
@@ -308,7 +338,7 @@
snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port);
/* Connect to the adb QEMUD service. */
- fd = qemu_pipe_open(con_name);
+ unique_fd fd(qemu_pipe_open(con_name));
if (fd < 0) {
/* This could be an older version of the emulator, that doesn't
* implement adb QEMUD service. Fall back to the old TCP way. */
@@ -317,30 +347,28 @@
return;
}
- for(;;) {
+ while (true) {
/*
* Wait till the host creates a new connection.
*/
/* Send the 'accept' request. */
- if (WriteFdExactly(fd, _accept_req, strlen(_accept_req))) {
+ if (WriteFdExactly(fd.get(), _accept_req, strlen(_accept_req))) {
/* Wait for the response. In the response we expect 'ok' on success,
* or 'ko' on failure. */
- if (!ReadFdExactly(fd, tmp, 2) || memcmp(tmp, _ok_resp, 2)) {
+ if (!ReadFdExactly(fd.get(), tmp, 2) || memcmp(tmp, _ok_resp, 2)) {
D("Accepting ADB host connection has failed.");
- adb_close(fd);
} else {
/* Host is connected. Register the transport, and start the
* exchange. */
- std::string serial = android::base::StringPrintf("host-%d", fd);
- if (register_socket_transport(fd, serial.c_str(), port, 1) != 0 ||
- !WriteFdExactly(fd, _start_req, strlen(_start_req))) {
- adb_close(fd);
- }
+ std::string serial = android::base::StringPrintf("host-%d", fd.get());
+ WriteFdExactly(fd.get(), _start_req, strlen(_start_req));
+ register_socket_transport(std::move(fd), std::move(serial), port, 1,
+ [](atransport*) { return false; });
}
/* Prepare for accepting of the next ADB host connection. */
- fd = qemu_pipe_open(con_name);
+ fd.reset(qemu_pipe_open(con_name));
if (fd < 0) {
D("adb service become unavailable.");
return;
@@ -427,10 +455,6 @@
return it->second;
}
-std::string getEmulatorSerialString(int console_port) {
- return android::base::StringPrintf("emulator-%d", console_port);
-}
-
atransport* find_emulator_transport_by_adb_port(int adb_port) {
std::lock_guard<std::mutex> lock(local_transports_lock);
return find_emulator_transport_by_adb_port_locked(adb_port);
@@ -441,21 +465,24 @@
}
#endif
-int init_socket_transport(atransport* t, int s, int adb_port, int local) {
+std::string getEmulatorSerialString(int console_port) {
+ return android::base::StringPrintf("emulator-%d", console_port);
+}
+
+int init_socket_transport(atransport* t, unique_fd fd, int adb_port, int local) {
int fail = 0;
- unique_fd fd(s);
t->type = kTransportLocal;
#if ADB_HOST
// Emulator connection.
if (local) {
- std::unique_ptr<BlockingConnection> emulator_connection(
- new EmulatorConnection(std::move(fd), adb_port));
- t->connection.reset(new BlockingConnectionAdapter(std::move(emulator_connection)));
+ auto emulator_connection = std::make_unique<EmulatorConnection>(std::move(fd), adb_port);
+ t->SetConnection(
+ std::make_unique<BlockingConnectionAdapter>(std::move(emulator_connection)));
std::lock_guard<std::mutex> lock(local_transports_lock);
atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port);
- if (existing_transport != NULL) {
+ if (existing_transport != nullptr) {
D("local transport for port %d already registered (%p)?", adb_port, existing_transport);
fail = -1;
} else if (local_transports.size() >= ADB_LOCAL_TRANSPORT_MAX) {
@@ -471,7 +498,7 @@
#endif
// Regular tcp connection.
- std::unique_ptr<BlockingConnection> fd_connection(new FdConnection(std::move(fd)));
- t->connection.reset(new BlockingConnectionAdapter(std::move(fd_connection)));
+ auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
+ t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection)));
return fail;
}
diff --git a/adb/transport_test.cpp b/adb/transport_test.cpp
index d987d4f..8c628d8 100644
--- a/adb/transport_test.cpp
+++ b/adb/transport_test.cpp
@@ -86,9 +86,9 @@
ASSERT_EQ(0U, t.features().size());
ASSERT_EQ(kCsHost, t.GetConnectionState());
- ASSERT_EQ(nullptr, t.product);
- ASSERT_EQ(nullptr, t.model);
- ASSERT_EQ(nullptr, t.device);
+ ASSERT_EQ(std::string(), t.product);
+ ASSERT_EQ(std::string(), t.model);
+ ASSERT_EQ(std::string(), t.device);
}
TEST(transport, parse_banner_product_features) {
diff --git a/adb/transport_usb.cpp b/adb/transport_usb.cpp
index 33e00a1..602970c 100644
--- a/adb/transport_usb.cpp
+++ b/adb/transport_usb.cpp
@@ -17,6 +17,7 @@
#define TRACE_TAG TRANSPORT
#include "sysdeps.h"
+#include "sysdeps/memory.h"
#include "transport.h"
#include <stdio.h>
@@ -121,7 +122,7 @@
// On Android devices, we rely on the kernel to provide buffered read.
// So we can recover automatically from EOVERFLOW.
static int remote_read(apacket* p, usb_handle* usb) {
- if (usb_read(usb, &p->msg, sizeof(amessage))) {
+ if (usb_read(usb, &p->msg, sizeof(amessage)) != sizeof(amessage)) {
PLOG(ERROR) << "remote usb: read terminated (message)";
return -1;
}
@@ -133,7 +134,8 @@
}
p->payload.resize(p->msg.data_length);
- if (usb_read(usb, &p->payload[0], p->payload.size())) {
+ if (usb_read(usb, &p->payload[0], p->payload.size())
+ != static_cast<int>(p->payload.size())) {
PLOG(ERROR) << "remote usb: terminated (data)";
return -1;
}
@@ -153,14 +155,14 @@
}
bool UsbConnection::Write(apacket* packet) {
- unsigned size = packet->msg.data_length;
+ int size = packet->msg.data_length;
- if (usb_write(handle_, &packet->msg, sizeof(packet->msg)) != 0) {
+ if (usb_write(handle_, &packet->msg, sizeof(packet->msg)) != sizeof(packet->msg)) {
PLOG(ERROR) << "remote usb: 1 - write terminated";
return false;
}
- if (packet->msg.data_length != 0 && usb_write(handle_, packet->payload.data(), size) != 0) {
+ if (packet->msg.data_length != 0 && usb_write(handle_, packet->payload.data(), size) != size) {
PLOG(ERROR) << "remote usb: 2 - write terminated";
return false;
}
@@ -174,8 +176,8 @@
void init_usb_transport(atransport* t, usb_handle* h) {
D("transport: usb");
- std::unique_ptr<BlockingConnection> connection(new UsbConnection(h));
- t->connection.reset(new BlockingConnectionAdapter(std::move(connection)));
+ auto connection = std::make_unique<UsbConnection>(h);
+ t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(connection)));
t->type = kTransportUsb;
}
diff --git a/adb/types.h b/adb/types.h
new file mode 100644
index 0000000..a3e5d48
--- /dev/null
+++ b/adb/types.h
@@ -0,0 +1,339 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include <algorithm>
+#include <deque>
+#include <memory>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include <android-base/logging.h>
+
+#include "sysdeps/memory.h"
+#include "sysdeps/uio.h"
+
+// Essentially std::vector<char>, except without zero initialization or reallocation.
+struct Block {
+ using iterator = char*;
+
+ Block() {}
+
+ explicit Block(size_t size) { allocate(size); }
+
+ template <typename Iterator>
+ Block(Iterator begin, Iterator end) : Block(end - begin) {
+ std::copy(begin, end, data_.get());
+ }
+
+ Block(const Block& copy) = delete;
+ Block(Block&& move) {
+ std::swap(data_, move.data_);
+ std::swap(capacity_, move.capacity_);
+ std::swap(size_, move.size_);
+ }
+
+ Block& operator=(const Block& copy) = delete;
+ Block& operator=(Block&& move) {
+ clear();
+
+ std::swap(data_, move.data_);
+ std::swap(capacity_, move.capacity_);
+ std::swap(size_, move.size_);
+
+ return *this;
+ }
+
+ ~Block() { clear(); }
+
+ void resize(size_t new_size) {
+ if (!data_) {
+ allocate(new_size);
+ } else {
+ CHECK_GE(capacity_, new_size);
+ size_ = new_size;
+ }
+ }
+
+ template <typename InputIt>
+ void assign(InputIt begin, InputIt end) {
+ clear();
+ allocate(end - begin);
+ std::copy(begin, end, data_.get());
+ }
+
+ void clear() {
+ data_.reset();
+ capacity_ = 0;
+ size_ = 0;
+ }
+
+ size_t capacity() const { return capacity_; }
+ size_t size() const { return size_; }
+ bool empty() const { return size() == 0; }
+
+ char* data() { return data_.get(); }
+ const char* data() const { return data_.get(); }
+
+ char* begin() { return data_.get(); }
+ const char* begin() const { return data_.get(); }
+
+ char* end() { return data() + size_; }
+ const char* end() const { return data() + size_; }
+
+ char& operator[](size_t idx) { return data()[idx]; }
+ const char& operator[](size_t idx) const { return data()[idx]; }
+
+ bool operator==(const Block& rhs) const {
+ return size() == rhs.size() && memcmp(data(), rhs.data(), size()) == 0;
+ }
+
+ private:
+ void allocate(size_t size) {
+ CHECK(data_ == nullptr);
+ CHECK_EQ(0ULL, capacity_);
+ CHECK_EQ(0ULL, size_);
+ if (size != 0) {
+ data_ = std::make_unique<char[]>(size);
+ capacity_ = size;
+ size_ = size;
+ }
+ }
+
+ std::unique_ptr<char[]> data_;
+ size_t capacity_ = 0;
+ size_t size_ = 0;
+};
+
+struct amessage {
+ uint32_t command; /* command identifier constant */
+ uint32_t arg0; /* first argument */
+ uint32_t arg1; /* second argument */
+ uint32_t data_length; /* length of payload (0 is allowed) */
+ uint32_t data_check; /* checksum of data payload */
+ uint32_t magic; /* command ^ 0xffffffff */
+};
+
+struct apacket {
+ using payload_type = Block;
+ amessage msg;
+ payload_type payload;
+};
+
+struct IOVector {
+ using value_type = char;
+ using block_type = Block;
+ using size_type = size_t;
+
+ IOVector() {}
+
+ explicit IOVector(std::unique_ptr<block_type> block) {
+ append(std::move(block));
+ }
+
+ IOVector(const IOVector& copy) = delete;
+ IOVector(IOVector&& move) : IOVector() {
+ *this = std::move(move);
+ }
+
+ IOVector& operator=(const IOVector& copy) = delete;
+ IOVector& operator=(IOVector&& move) {
+ chain_ = std::move(move.chain_);
+ chain_length_ = move.chain_length_;
+ begin_offset_ = move.begin_offset_;
+ end_offset_ = move.end_offset_;
+
+ move.chain_.clear();
+ move.chain_length_ = 0;
+ move.begin_offset_ = 0;
+ move.end_offset_ = 0;
+
+ return *this;
+ }
+
+ size_type size() const { return chain_length_ - begin_offset_ - end_offset_; }
+ bool empty() const { return size() == 0; }
+
+ void clear() {
+ chain_length_ = 0;
+ begin_offset_ = 0;
+ end_offset_ = 0;
+ chain_.clear();
+ }
+
+ // Split the first |len| bytes out of this chain into its own.
+ IOVector take_front(size_type len) {
+ IOVector head;
+
+ if (len == 0) {
+ return head;
+ }
+ CHECK_GE(size(), len);
+
+ std::shared_ptr<const block_type> first_block = chain_.front();
+ CHECK_GE(first_block->size(), begin_offset_);
+ head.append_shared(std::move(first_block));
+ head.begin_offset_ = begin_offset_;
+
+ while (head.size() < len) {
+ pop_front_block();
+ CHECK(!chain_.empty());
+
+ head.append_shared(chain_.front());
+ }
+
+ if (head.size() == len) {
+ // Head takes full ownership of the last block it took.
+ head.end_offset_ = 0;
+ begin_offset_ = 0;
+ pop_front_block();
+ } else {
+ // Head takes partial ownership of the last block it took.
+ size_t bytes_taken = head.size() - len;
+ head.end_offset_ = bytes_taken;
+ CHECK_GE(chain_.front()->size(), bytes_taken);
+ begin_offset_ = chain_.front()->size() - bytes_taken;
+ }
+
+ return head;
+ }
+
+ // Add a nonempty block to the chain.
+ // The end of the chain must be a complete block (i.e. end_offset_ == 0).
+ void append(std::unique_ptr<const block_type> block) {
+ CHECK_NE(0ULL, block->size());
+ CHECK_EQ(0ULL, end_offset_);
+ chain_length_ += block->size();
+ chain_.emplace_back(std::move(block));
+ }
+
+ void append(block_type&& block) { append(std::make_unique<block_type>(std::move(block))); }
+
+ void trim_front() {
+ if (begin_offset_ == 0) {
+ return;
+ }
+
+ const block_type* first_block = chain_.front().get();
+ auto copy = std::make_unique<block_type>(first_block->size() - begin_offset_);
+ memcpy(copy->data(), first_block->data() + begin_offset_, copy->size());
+ chain_.front() = std::move(copy);
+
+ chain_length_ -= begin_offset_;
+ begin_offset_ = 0;
+ }
+
+ private:
+ // append, except takes a shared_ptr.
+ // Private to prevent exterior mutation of blocks.
+ void append_shared(std::shared_ptr<const block_type> block) {
+ CHECK_NE(0ULL, block->size());
+ CHECK_EQ(0ULL, end_offset_);
+ chain_length_ += block->size();
+ chain_.emplace_back(std::move(block));
+ }
+
+ // Drop the front block from the chain, and update chain_length_ appropriately.
+ void pop_front_block() {
+ chain_length_ -= chain_.front()->size();
+ begin_offset_ = 0;
+ chain_.pop_front();
+ }
+
+ // Iterate over the blocks with a callback with an operator()(const char*, size_t).
+ template <typename Fn>
+ void iterate_blocks(Fn&& callback) const {
+ if (chain_.size() == 0) {
+ return;
+ }
+
+ for (size_t i = 0; i < chain_.size(); ++i) {
+ const std::shared_ptr<const block_type>& block = chain_.at(i);
+ const char* begin = block->data();
+ size_t length = block->size();
+
+ // Note that both of these conditions can be true if there's only one block.
+ if (i == 0) {
+ CHECK_GE(block->size(), begin_offset_);
+ begin += begin_offset_;
+ length -= begin_offset_;
+ }
+
+ if (i == chain_.size() - 1) {
+ CHECK_GE(length, end_offset_);
+ length -= end_offset_;
+ }
+
+ callback(begin, length);
+ }
+ }
+
+ public:
+ // Copy all of the blocks into a single block.
+ template <typename CollectionType = block_type>
+ CollectionType coalesce() const {
+ CollectionType result;
+ if (size() == 0) {
+ return result;
+ }
+
+ result.resize(size());
+
+ size_t offset = 0;
+ iterate_blocks([&offset, &result](const char* data, size_t len) {
+ memcpy(&result[offset], data, len);
+ offset += len;
+ });
+
+ return result;
+ }
+
+ template <typename FunctionType>
+ auto coalesced(FunctionType&& f) const ->
+ typename std::result_of<FunctionType(const char*, size_t)>::type {
+ if (chain_.size() == 1) {
+ // If we only have one block, we can use it directly.
+ return f(chain_.front()->data() + begin_offset_, size());
+ } else {
+ // Otherwise, copy to a single block.
+ auto data = coalesce();
+ return f(data.data(), data.size());
+ }
+ }
+
+ // Get a list of iovecs that can be used to write out all of the blocks.
+ std::vector<adb_iovec> iovecs() const {
+ std::vector<adb_iovec> result;
+ iterate_blocks([&result](const char* data, size_t len) {
+ adb_iovec iov;
+ iov.iov_base = const_cast<char*>(data);
+ iov.iov_len = len;
+ result.emplace_back(iov);
+ });
+
+ return result;
+ }
+
+ private:
+ // Total length of all of the blocks in the chain.
+ size_t chain_length_ = 0;
+
+ size_t begin_offset_ = 0;
+ size_t end_offset_ = 0;
+ std::deque<std::shared_ptr<const block_type>> chain_;
+};
diff --git a/adb/types_test.cpp b/adb/types_test.cpp
new file mode 100644
index 0000000..31ab90a
--- /dev/null
+++ b/adb/types_test.cpp
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2018 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 <gtest/gtest.h>
+
+#include "sysdeps/memory.h"
+#include "types.h"
+
+static std::unique_ptr<IOVector::block_type> create_block(const std::string& string) {
+ return std::make_unique<IOVector::block_type>(string.begin(), string.end());
+}
+
+static std::unique_ptr<IOVector::block_type> create_block(char value, size_t len) {
+ auto block = std::make_unique<IOVector::block_type>();
+ block->resize(len);
+ memset(&(*block)[0], value, len);
+ return block;
+}
+
+template <typename T>
+static std::unique_ptr<IOVector::block_type> copy_block(T&& block) {
+ auto copy = std::make_unique<IOVector::block_type>();
+ copy->assign(block->begin(), block->end());
+ return copy;
+}
+
+TEST(IOVector, empty) {
+ // Empty IOVector.
+ IOVector bc;
+ CHECK_EQ(0ULL, bc.coalesce().size());
+}
+
+TEST(IOVector, single_block) {
+ // A single block.
+ auto block = create_block('x', 100);
+ IOVector bc;
+ bc.append(copy_block(block));
+ ASSERT_EQ(100ULL, bc.size());
+ auto coalesced = bc.coalesce();
+ ASSERT_EQ(*block, coalesced);
+}
+
+TEST(IOVector, single_block_split) {
+ // One block split.
+ IOVector bc;
+ bc.append(create_block("foobar"));
+ IOVector foo = bc.take_front(3);
+ ASSERT_EQ(3ULL, foo.size());
+ ASSERT_EQ(3ULL, bc.size());
+ ASSERT_EQ(*create_block("foo"), foo.coalesce());
+ ASSERT_EQ(*create_block("bar"), bc.coalesce());
+}
+
+TEST(IOVector, aligned_split) {
+ IOVector bc;
+ bc.append(create_block("foo"));
+ bc.append(create_block("bar"));
+ bc.append(create_block("baz"));
+ ASSERT_EQ(9ULL, bc.size());
+
+ IOVector foo = bc.take_front(3);
+ ASSERT_EQ(3ULL, foo.size());
+ ASSERT_EQ(*create_block("foo"), foo.coalesce());
+
+ IOVector bar = bc.take_front(3);
+ ASSERT_EQ(3ULL, bar.size());
+ ASSERT_EQ(*create_block("bar"), bar.coalesce());
+
+ IOVector baz = bc.take_front(3);
+ ASSERT_EQ(3ULL, baz.size());
+ ASSERT_EQ(*create_block("baz"), baz.coalesce());
+
+ ASSERT_EQ(0ULL, bc.size());
+}
+
+TEST(IOVector, misaligned_split) {
+ IOVector bc;
+ bc.append(create_block("foo"));
+ bc.append(create_block("bar"));
+ bc.append(create_block("baz"));
+ bc.append(create_block("qux"));
+ bc.append(create_block("quux"));
+
+ // Aligned left, misaligned right, across multiple blocks.
+ IOVector foob = bc.take_front(4);
+ ASSERT_EQ(4ULL, foob.size());
+ ASSERT_EQ(*create_block("foob"), foob.coalesce());
+
+ // Misaligned left, misaligned right, in one block.
+ IOVector a = bc.take_front(1);
+ ASSERT_EQ(1ULL, a.size());
+ ASSERT_EQ(*create_block("a"), a.coalesce());
+
+ // Misaligned left, misaligned right, across two blocks.
+ IOVector rba = bc.take_front(3);
+ ASSERT_EQ(3ULL, rba.size());
+ ASSERT_EQ(*create_block("rba"), rba.coalesce());
+
+ // Misaligned left, misaligned right, across three blocks.
+ IOVector zquxquu = bc.take_front(7);
+ ASSERT_EQ(7ULL, zquxquu.size());
+ ASSERT_EQ(*create_block("zquxquu"), zquxquu.coalesce());
+
+ ASSERT_EQ(1ULL, bc.size());
+ ASSERT_EQ(*create_block("x"), bc.coalesce());
+}
diff --git a/adf/libadf/Android.bp b/adf/libadf/Android.bp
index 8eef2ea..49e3721 100644
--- a/adf/libadf/Android.bp
+++ b/adf/libadf/Android.bp
@@ -14,6 +14,7 @@
cc_library {
name: "libadf",
+ recovery_available: true,
vendor_available: true,
vndk: {
enabled: true,
diff --git a/adf/libadf/adf.cpp b/adf/libadf/adf.cpp
index 60d8ef0..fd9c208 100644
--- a/adf/libadf/adf.cpp
+++ b/adf/libadf/adf.cpp
@@ -132,8 +132,11 @@
void adf_free_device_data(struct adf_device_data *data)
{
delete [] data->attachments;
+ data->attachments = nullptr;
delete [] data->allowed_attachments;
+ data->allowed_attachments = nullptr;
delete [] static_cast<char *>(data->custom_data);
+ data->custom_data = nullptr;
}
int adf_device_post(struct adf_device *dev,
@@ -236,9 +239,10 @@
return err;
std::vector<adf_id_t> ids;
- for (size_t i = 0; i < data.n_allowed_attachments; i++)
- if (data.allowed_attachments[i].overlay_engine == overlay_engine)
- ids.push_back(data.allowed_attachments[i].interface);
+ if (data.allowed_attachments != nullptr)
+ for (size_t i = 0; i < data.n_allowed_attachments; i++)
+ if (data.allowed_attachments[i].overlay_engine == overlay_engine)
+ ids.push_back(data.allowed_attachments[i].interface);
adf_free_device_data(&data);
return adf_id_vector_to_array(ids, interfaces);
@@ -450,9 +454,10 @@
return err;
std::vector<adf_id_t> ids;
- for (size_t i = 0; i < data.n_allowed_attachments; i++)
- if (data.allowed_attachments[i].interface == interface)
- ids.push_back(data.allowed_attachments[i].overlay_engine);
+ if (data.allowed_attachments != nullptr)
+ for (size_t i = 0; i < data.n_allowed_attachments; i++)
+ if (data.allowed_attachments[i].interface == interface)
+ ids.push_back(data.allowed_attachments[i].overlay_engine);
return adf_id_vector_to_array(ids, overlay_engines);
}
@@ -551,7 +556,9 @@
void adf_free_overlay_engine_data(struct adf_overlay_engine_data *data)
{
delete [] data->supported_formats;
+ data->supported_formats = nullptr;
delete [] static_cast<char *>(data->custom_data);
+ data->custom_data = nullptr;
}
bool adf_overlay_engine_supports_format(int fd, __u32 format)
@@ -564,10 +571,12 @@
if (err < 0)
return false;
- for (i = 0; i < data.n_supported_formats; i++) {
- if (data.supported_formats[i] == format) {
- ret = true;
- break;
+ if (data.supported_formats != nullptr) {
+ for (i = 0; i < data.n_supported_formats; i++) {
+ if (data.supported_formats[i] == format) {
+ ret = true;
+ break;
+ }
}
}
@@ -638,18 +647,18 @@
const __u32 *formats, size_t n_formats,
adf_id_t interface, adf_id_t *overlay_engine)
{
- adf_id_t *engs;
+ adf_id_t *engs = nullptr;
ssize_t n_engs = adf_overlay_engines_for_interface(dev, interface, &engs);
- if (n_engs <= 0)
+ if (engs == nullptr)
return false;
- adf_id_t *filtered_engs;
+ adf_id_t *filtered_engs = nullptr;
ssize_t n_filtered_engs = adf_overlay_engines_filter_by_format(dev,
formats, n_formats, engs, n_engs, &filtered_engs);
free(engs);
- if (n_filtered_engs <= 0)
+ if (filtered_engs == nullptr)
return false;
*overlay_engine = filtered_engs[0];
@@ -700,17 +709,17 @@
if (n_intfs < 0)
return n_intfs;
- else if (!n_intfs)
+ else if (!intfs)
return -ENODEV;
- adf_id_t *primary_intfs;
+ adf_id_t *primary_intfs = nullptr;
ssize_t n_primary_intfs = adf_interfaces_filter_by_flag(dev,
ADF_INTF_FLAG_PRIMARY, intfs, n_intfs, &primary_intfs);
free(intfs);
if (n_primary_intfs < 0)
return n_primary_intfs;
- else if (!n_primary_intfs)
+ else if (!primary_intfs)
return -ENODEV;
if (!formats) {
diff --git a/base/Android.bp b/base/Android.bp
index 5d70d47..3d80d97 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -26,6 +26,7 @@
cc_library_headers {
name: "libbase_headers",
vendor_available: true,
+ recovery_available: true,
host_supported: true,
export_include_dirs: ["include"],
@@ -47,18 +48,17 @@
"file.cpp",
"logging.cpp",
"parsenetaddress.cpp",
+ "properties.cpp",
"quick_exit.cpp",
"stringprintf.cpp",
"strings.cpp",
+ "threads.cpp",
"test_utils.cpp",
],
shared_libs: ["liblog"],
target: {
android: {
- srcs: [
- "properties.cpp",
- ],
sanitize: {
misc_undefined: ["integer"],
},
@@ -93,6 +93,7 @@
name: "libbase",
defaults: ["libbase_defaults"],
vendor_available: true,
+ recovery_available: true,
host_supported: true,
vndk: {
enabled: true,
@@ -123,9 +124,11 @@
"errors_test.cpp",
"file_test.cpp",
"logging_test.cpp",
+ "macros_test.cpp",
"parsedouble_test.cpp",
"parseint_test.cpp",
"parsenetaddress_test.cpp",
+ "properties_test.cpp",
"quick_exit_test.cpp",
"scopeguard_test.cpp",
"stringprintf_test.cpp",
@@ -135,7 +138,6 @@
],
target: {
android: {
- srcs: ["properties_test.cpp"],
sanitize: {
misc_undefined: ["integer"],
},
@@ -160,4 +162,5 @@
suffix: "64",
},
},
+ test_suites: ["device-tests"],
}
diff --git a/base/file.cpp b/base/file.cpp
index 2f697a1..d6fe753 100644
--- a/base/file.cpp
+++ b/base/file.cpp
@@ -199,17 +199,23 @@
bool RemoveFileIfExists(const std::string& path, std::string* err) {
struct stat st;
#if defined(_WIN32)
- //TODO: Windows version can't handle symbol link correctly.
+ // TODO: Windows version can't handle symbolic links correctly.
int result = stat(path.c_str(), &st);
bool file_type_removable = (result == 0 && S_ISREG(st.st_mode));
#else
int result = lstat(path.c_str(), &st);
bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)));
#endif
+ if (result == -1) {
+ if (errno == ENOENT || errno == ENOTDIR) return true;
+ if (err != nullptr) *err = strerror(errno);
+ return false;
+ }
+
if (result == 0) {
if (!file_type_removable) {
if (err != nullptr) {
- *err = "is not a regular or symbol link file";
+ *err = "is not a regular file or symbolic link";
}
return false;
}
diff --git a/base/file_test.cpp b/base/file_test.cpp
index 02b431d..6794652 100644
--- a/base/file_test.cpp
+++ b/base/file_test.cpp
@@ -26,6 +26,10 @@
#include "android-base/test_utils.h"
+#if !defined(_WIN32)
+#include <pwd.h>
+#endif
+
TEST(file, ReadFileToString_ENOENT) {
std::string s("hello");
errno = 0;
@@ -115,7 +119,7 @@
ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
}
-TEST(file, RemoveFileIfExist) {
+TEST(file, RemoveFileIfExists) {
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
close(tf.fd);
@@ -126,9 +130,43 @@
TemporaryDir td;
ASSERT_FALSE(android::base::RemoveFileIfExists(td.path));
ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err));
- ASSERT_EQ("is not a regular or symbol link file", err);
+ ASSERT_EQ("is not a regular file or symbolic link", err);
}
+TEST(file, RemoveFileIfExists_ENOTDIR) {
+ TemporaryFile tf;
+ close(tf.fd);
+ tf.fd = -1;
+ std::string err{"xxx"};
+ ASSERT_TRUE(android::base::RemoveFileIfExists(std::string{tf.path} + "/abc", &err));
+ ASSERT_EQ("xxx", err);
+}
+
+#if !defined(_WIN32)
+TEST(file, RemoveFileIfExists_EACCES) {
+ // EACCES -- one of the directories in the path has no search permission
+ // root can bypass permission restrictions, so drop root.
+ if (getuid() == 0) {
+ passwd* shell = getpwnam("shell");
+ setgid(shell->pw_gid);
+ setuid(shell->pw_uid);
+ }
+
+ TemporaryDir td;
+ TemporaryFile tf(td.path);
+ close(tf.fd);
+ tf.fd = -1;
+ std::string err{"xxx"};
+ // Remove dir's search permission.
+ ASSERT_TRUE(chmod(td.path, S_IRUSR | S_IWUSR) == 0);
+ ASSERT_FALSE(android::base::RemoveFileIfExists(tf.path, &err));
+ ASSERT_EQ("Permission denied", err);
+ // Set dir's search permission again.
+ ASSERT_TRUE(chmod(td.path, S_IRWXU) == 0);
+ ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err));
+}
+#endif
+
TEST(file, Readlink) {
#if !defined(_WIN32)
// Linux doesn't allow empty symbolic links.
diff --git a/base/include/android-base/chrono_utils.h b/base/include/android-base/chrono_utils.h
index c3396ee..11fcf71 100644
--- a/base/include/android-base/chrono_utils.h
+++ b/base/include/android-base/chrono_utils.h
@@ -14,13 +14,12 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_CHRONO_UTILS_H
-#define ANDROID_BASE_CHRONO_UTILS_H
+#pragma once
#include <chrono>
#include <sstream>
-#if __cplusplus > 201103L // C++14
+#if __cplusplus > 201103L && !defined(__WIN32) // C++14
using namespace std::chrono_literals;
#endif
@@ -52,5 +51,3 @@
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_CHRONO_UTILS_H
diff --git a/base/include/android-base/endian.h b/base/include/android-base/endian.h
index 6eb677c..cbbd8c9 100644
--- a/base/include/android-base/endian.h
+++ b/base/include/android-base/endian.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_ENDIAN_H
-#define ANDROID_BASE_ENDIAN_H
+#pragma once
/* A cross-platform equivalent of bionic's <sys/endian.h>. */
@@ -86,5 +85,3 @@
#define le64toh(x) (x)
#endif
-
-#endif // ANDROID_BASE_ENDIAN_H
diff --git a/base/include/android-base/errors.h b/base/include/android-base/errors.h
index 04c299c..06f29fc 100644
--- a/base/include/android-base/errors.h
+++ b/base/include/android-base/errors.h
@@ -27,8 +27,7 @@
// special handling to get the error string. Refer to Microsoft documentation
// to determine which error code to check for each function.
-#ifndef ANDROID_BASE_ERRORS_H
-#define ANDROID_BASE_ERRORS_H
+#pragma once
#include <string>
@@ -42,5 +41,3 @@
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_ERRORS_H
diff --git a/base/include/android-base/file.h b/base/include/android-base/file.h
index 667d6fb..908690b 100644
--- a/base/include/android-base/file.h
+++ b/base/include/android-base/file.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_FILE_H
-#define ANDROID_BASE_FILE_H
+#pragma once
#include <sys/stat.h>
#include <sys/types.h>
@@ -78,5 +77,3 @@
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_FILE_H
diff --git a/base/include/android-base/logging.h b/base/include/android-base/logging.h
index afff2c9..f94cc25 100644
--- a/base/include/android-base/logging.h
+++ b/base/include/android-base/logging.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_LOGGING_H
-#define ANDROID_BASE_LOGGING_H
+#pragma once
//
// Google-style C++ logging.
@@ -100,11 +99,23 @@
unsigned int, const char*)>;
using AbortFunction = std::function<void(const char*)>;
+// Loggers for use with InitLogging/SetLogger.
+
+// Log to the kernel log (dmesg).
void KernelLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*);
+// Log to stderr in the full logcat format (with pid/tid/time/tag details).
void StderrLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*);
+// Log just the message to stdout/stderr (without pid/tid/time/tag details).
+// The choice of stdout versus stderr is based on the severity.
+// Errors are also prefixed by the program name (as with err(3)/error(3)).
+// Useful for replacing printf(3)/perror(3)/err(3)/error(3) in command-line tools.
+void StdioLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*);
void DefaultAborter(const char* abort_message);
+std::string GetDefaultTag();
+void SetDefaultTag(const std::string& tag);
+
#ifdef __ANDROID__
// We expose this even though it is the default because a user that wants to
// override the default log buffer will have to construct this themselves.
@@ -436,11 +447,6 @@
private:
const std::unique_ptr<LogMessageData> data_;
- // TODO(b/35361699): remove these symbols once all prebuilds stop using it.
- LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity, int error);
- static void LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
- const char* msg);
-
DISALLOW_COPY_AND_ASSIGN(LogMessage);
};
@@ -476,23 +482,14 @@
// Note: to print the pointer, use "<< static_cast<const void*>(string_pointer)" instead.
// Note: a not-recommended alternative is to let Clang ignore the warning by adding
// -Wno-user-defined-warnings to CPPFLAGS.
-#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgcc-compat"
#define OSTREAM_STRING_POINTER_USAGE_WARNING \
__attribute__((diagnose_if(true, "Unexpected logging of string pointer", "warning")))
-#else
-#define OSTREAM_STRING_POINTER_USAGE_WARNING /* empty */
-#endif
inline std::ostream& operator<<(std::ostream& stream, const std::string* string_pointer)
OSTREAM_STRING_POINTER_USAGE_WARNING {
return stream << static_cast<const void*>(string_pointer);
}
-#ifdef __clang__
#pragma clang diagnostic pop
-#endif
-#undef OSTREAM_STRING_POINTER_USAGE_WARNING
} // namespace std
-
-#endif // ANDROID_BASE_LOGGING_H
diff --git a/base/include/android-base/macros.h b/base/include/android-base/macros.h
index 25f2ff4..49cc0c9 100644
--- a/base/include/android-base/macros.h
+++ b/base/include/android-base/macros.h
@@ -14,12 +14,13 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_MACROS_H
-#define ANDROID_BASE_MACROS_H
+#pragma once
#include <stddef.h> // for size_t
#include <unistd.h> // for TEMP_FAILURE_RETRY
+#include <utility>
+
// bionic and glibc both have TEMP_FAILURE_RETRY, but eg Mac OS' libc doesn't.
#ifndef TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(exp) \
@@ -114,6 +115,8 @@
((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
+#define SIZEOF_MEMBER(t, f) sizeof(std::declval<t>().f)
+
// Changing this definition will cause you a lot of pain. A majority of
// vendor code defines LIKELY and UNLIKELY this way, and includes
// this header through an indirect path.
@@ -167,17 +170,7 @@
//
// In either case this macro has no effect on runtime behavior and performance
// of code.
-#if defined(__clang__) && defined(__has_warning)
-#if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
#define FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT
-#endif
-#endif
-
-#ifndef FALLTHROUGH_INTENDED
-#define FALLTHROUGH_INTENDED \
- do { \
- } while (0)
-#endif
// Current ABI string
#if defined(__arm__)
@@ -193,5 +186,3 @@
#elif defined(__mips__) && defined(__LP64__)
#define ABI_STRING "mips64"
#endif
-
-#endif // ANDROID_BASE_MACROS_H
diff --git a/base/include/android-base/memory.h b/base/include/android-base/memory.h
index 9971226..0277a03 100644
--- a/base/include/android-base/memory.h
+++ b/base/include/android-base/memory.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_MEMORY_H
-#define ANDROID_BASE_MEMORY_H
+#pragma once
namespace android {
namespace base {
@@ -37,5 +36,3 @@
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_MEMORY_H
diff --git a/base/include/android-base/parsedouble.h b/base/include/android-base/parsedouble.h
index daa6902..ccffba2 100644
--- a/base/include/android-base/parsedouble.h
+++ b/base/include/android-base/parsedouble.h
@@ -14,37 +14,64 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_PARSEDOUBLE_H
-#define ANDROID_BASE_PARSEDOUBLE_H
+#pragma once
#include <errno.h>
#include <stdlib.h>
#include <limits>
+#include <string>
namespace android {
namespace base {
-// Parse double value in the string 's' and sets 'out' to that value.
+// Parse floating value in the string 's' and sets 'out' to that value if it exists.
// Optionally allows the caller to define a 'min' and 'max' beyond which
// otherwise valid values will be rejected. Returns boolean success.
-static inline bool ParseDouble(const char* s, double* out,
- double min = std::numeric_limits<double>::lowest(),
- double max = std::numeric_limits<double>::max()) {
+template <typename T, T (*strtox)(const char* str, char** endptr)>
+static inline bool ParseFloatingPoint(const char* s, T* out, T min, T max) {
errno = 0;
char* end;
- double result = strtod(s, &end);
+ T result = strtox(s, &end);
if (errno != 0 || s == end || *end != '\0') {
return false;
}
if (result < min || max < result) {
return false;
}
- *out = result;
+ if (out != nullptr) {
+ *out = result;
+ }
return true;
}
+// Parse double value in the string 's' and sets 'out' to that value if it exists.
+// Optionally allows the caller to define a 'min' and 'max' beyond which
+// otherwise valid values will be rejected. Returns boolean success.
+static inline bool ParseDouble(const char* s, double* out,
+ double min = std::numeric_limits<double>::lowest(),
+ double max = std::numeric_limits<double>::max()) {
+ return ParseFloatingPoint<double, strtod>(s, out, min, max);
+}
+static inline bool ParseDouble(const std::string& s, double* out,
+ double min = std::numeric_limits<double>::lowest(),
+ double max = std::numeric_limits<double>::max()) {
+ return ParseFloatingPoint<double, strtod>(s.c_str(), out, min, max);
+}
+
+// Parse float value in the string 's' and sets 'out' to that value if it exists.
+// Optionally allows the caller to define a 'min' and 'max' beyond which
+// otherwise valid values will be rejected. Returns boolean success.
+static inline bool ParseFloat(const char* s, float* out,
+ float min = std::numeric_limits<float>::lowest(),
+ float max = std::numeric_limits<float>::max()) {
+ return ParseFloatingPoint<float, strtof>(s, out, min, max);
+}
+static inline bool ParseFloat(const std::string& s, float* out,
+ float min = std::numeric_limits<float>::lowest(),
+ float max = std::numeric_limits<float>::max()) {
+ return ParseFloatingPoint<float, strtof>(s.c_str(), out, min, max);
+}
+
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_PARSEDOUBLE_H
diff --git a/base/include/android-base/parseint.h b/base/include/android-base/parseint.h
index 2c8570e..55f1ed3 100644
--- a/base/include/android-base/parseint.h
+++ b/base/include/android-base/parseint.h
@@ -14,11 +14,11 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_PARSEINT_H
-#define ANDROID_BASE_PARSEINT_H
+#pragma once
#include <errno.h>
#include <stdlib.h>
+#include <string.h>
#include <limits>
#include <string>
@@ -26,38 +26,55 @@
namespace android {
namespace base {
-// Parses the unsigned decimal integer in the string 's' and sets 'out' to
-// that value. Optionally allows the caller to define a 'max' beyond which
-// otherwise valid values will be rejected. Returns boolean success; 'out'
-// is untouched if parsing fails.
+// Parses the unsigned decimal or hexadecimal integer in the string 's' and sets
+// 'out' to that value if it is specified. Optionally allows the caller to define
+// a 'max' beyond which otherwise valid values will be rejected. Returns boolean
+// success; 'out' is untouched if parsing fails.
template <typename T>
-bool ParseUint(const char* s, T* out,
- T max = std::numeric_limits<T>::max()) {
+bool ParseUint(const char* s, T* out, T max = std::numeric_limits<T>::max(),
+ bool allow_suffixes = false) {
int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10;
errno = 0;
char* end;
unsigned long long int result = strtoull(s, &end, base);
- if (errno != 0 || s == end || *end != '\0') {
- return false;
+ if (errno != 0 || end == s) return false;
+ if (*end != '\0') {
+ const char* suffixes = "bkmgtpe";
+ const char* suffix;
+ if (!allow_suffixes || (suffix = strchr(suffixes, tolower(*end))) == nullptr) return false;
+ if (__builtin_mul_overflow(result, 1ULL << (10 * (suffix - suffixes)), &result)) return false;
}
if (max < result) {
return false;
}
- *out = static_cast<T>(result);
+ if (out != nullptr) {
+ *out = static_cast<T>(result);
+ }
return true;
}
// TODO: string_view
template <typename T>
-bool ParseUint(const std::string& s, T* out,
- T max = std::numeric_limits<T>::max()) {
- return ParseUint(s.c_str(), out, max);
+bool ParseUint(const std::string& s, T* out, T max = std::numeric_limits<T>::max(),
+ bool allow_suffixes = false) {
+ return ParseUint(s.c_str(), out, max, allow_suffixes);
}
-// Parses the signed decimal integer in the string 's' and sets 'out' to
-// that value. Optionally allows the caller to define a 'min' and 'max
-// beyond which otherwise valid values will be rejected. Returns boolean
-// success; 'out' is untouched if parsing fails.
+template <typename T>
+bool ParseByteCount(const char* s, T* out, T max = std::numeric_limits<T>::max()) {
+ return ParseUint(s, out, max, true);
+}
+
+// TODO: string_view
+template <typename T>
+bool ParseByteCount(const std::string& s, T* out, T max = std::numeric_limits<T>::max()) {
+ return ParseByteCount(s.c_str(), out, max);
+}
+
+// Parses the signed decimal or hexadecimal integer in the string 's' and sets
+// 'out' to that value if it is specified. Optionally allows the caller to define
+// a 'min' and 'max' beyond which otherwise valid values will be rejected. Returns
+// boolean success; 'out' is untouched if parsing fails.
template <typename T>
bool ParseInt(const char* s, T* out,
T min = std::numeric_limits<T>::min(),
@@ -72,7 +89,9 @@
if (result < min || max < result) {
return false;
}
- *out = static_cast<T>(result);
+ if (out != nullptr) {
+ *out = static_cast<T>(result);
+ }
return true;
}
@@ -86,5 +105,3 @@
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_PARSEINT_H
diff --git a/base/include/android-base/parsenetaddress.h b/base/include/android-base/parsenetaddress.h
index b4ac025..47f8b5f 100644
--- a/base/include/android-base/parsenetaddress.h
+++ b/base/include/android-base/parsenetaddress.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_PARSENETADDRESS_H
-#define ANDROID_BASE_PARSENETADDRESS_H
+#pragma once
#include <string>
@@ -34,5 +33,3 @@
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_PARSENETADDRESS_H
diff --git a/base/include/android-base/properties.h b/base/include/android-base/properties.h
index 041586c..31e5273 100644
--- a/base/include/android-base/properties.h
+++ b/base/include/android-base/properties.h
@@ -14,15 +14,10 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_PROPERTIES_H
-#define ANDROID_BASE_PROPERTIES_H
+#pragma once
#include <sys/cdefs.h>
-#if !defined(__BIONIC__)
-#error Only bionic supports system properties.
-#endif
-
#include <chrono>
#include <limits>
#include <string>
@@ -62,16 +57,18 @@
// Waits for the system property `key` to have the value `expected_value`.
// Times out after `relative_timeout`.
// Returns true on success, false on timeout.
+#if defined(__BIONIC__)
bool WaitForProperty(const std::string& key, const std::string& expected_value,
std::chrono::milliseconds relative_timeout = std::chrono::milliseconds::max());
+#endif
// Waits for the system property `key` to be created.
// Times out after `relative_timeout`.
// Returns true on success, false on timeout.
+#if defined(__BIONIC__)
bool WaitForPropertyCreation(const std::string& key, std::chrono::milliseconds relative_timeout =
std::chrono::milliseconds::max());
+#endif
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_PROPERTIES_H
diff --git a/base/include/android-base/scopeguard.h b/base/include/android-base/scopeguard.h
index abcf4bc..e6a9d10 100644
--- a/base/include/android-base/scopeguard.h
+++ b/base/include/android-base/scopeguard.h
@@ -14,23 +14,29 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_SCOPEGUARD_H
-#define ANDROID_BASE_SCOPEGUARD_H
+#pragma once
-#include <utility> // for std::move
+#include <utility> // for std::move, std::forward
namespace android {
namespace base {
+// ScopeGuard ensures that the specified functor is executed no matter how the
+// current scope exits.
template <typename F>
class ScopeGuard {
public:
- ScopeGuard(F f) : f_(f), active_(true) {}
+ ScopeGuard(F&& f) : f_(std::forward<F>(f)), active_(true) {}
ScopeGuard(ScopeGuard&& that) : f_(std::move(that.f_)), active_(that.active_) {
that.active_ = false;
}
+ template <typename Functor>
+ ScopeGuard(ScopeGuard<Functor>&& that) : f_(std::move(that.f_)), active_(that.active_) {
+ that.active_ = false;
+ }
+
~ScopeGuard() {
if (active_) f_();
}
@@ -45,16 +51,17 @@
bool active() const { return active_; }
private:
+ template <typename Functor>
+ friend class ScopeGuard;
+
F f_;
bool active_;
};
-template <typename T>
-ScopeGuard<T> make_scope_guard(T f) {
- return ScopeGuard<T>(f);
+template <typename F>
+ScopeGuard<F> make_scope_guard(F&& f) {
+ return ScopeGuard<F>(std::forward<F>(f));
}
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_SCOPEGUARD_H
diff --git a/base/include/android-base/stringprintf.h b/base/include/android-base/stringprintf.h
index 1fd6297..93c56af 100644
--- a/base/include/android-base/stringprintf.h
+++ b/base/include/android-base/stringprintf.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_STRINGPRINTF_H
-#define ANDROID_BASE_STRINGPRINTF_H
+#pragma once
#include <stdarg.h>
#include <string>
@@ -24,33 +23,18 @@
namespace base {
// These printf-like functions are implemented in terms of vsnprintf, so they
-// use the same attribute for compile-time format string checking. On Windows,
-// if the mingw version of vsnprintf is used, use `gnu_printf' which allows z
-// in %zd and PRIu64 (and related) to be recognized by the compile-time
-// checking.
-#define ANDROID_BASE_FORMAT_ARCHETYPE __printf__
-#ifdef __USE_MINGW_ANSI_STDIO
-#if __USE_MINGW_ANSI_STDIO
-#undef ANDROID_BASE_FORMAT_ARCHETYPE
-#define ANDROID_BASE_FORMAT_ARCHETYPE gnu_printf
-#endif
-#endif
+// use the same attribute for compile-time format string checking.
// Returns a string corresponding to printf-like formatting of the arguments.
-std::string StringPrintf(const char* fmt, ...)
- __attribute__((__format__(ANDROID_BASE_FORMAT_ARCHETYPE, 1, 2)));
+std::string StringPrintf(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
// Appends a printf-like formatting of the arguments to 'dst'.
void StringAppendF(std::string* dst, const char* fmt, ...)
- __attribute__((__format__(ANDROID_BASE_FORMAT_ARCHETYPE, 2, 3)));
+ __attribute__((__format__(__printf__, 2, 3)));
// Appends a printf-like formatting of the arguments to 'dst'.
void StringAppendV(std::string* dst, const char* format, va_list ap)
- __attribute__((__format__(ANDROID_BASE_FORMAT_ARCHETYPE, 2, 0)));
-
-#undef ANDROID_BASE_FORMAT_ARCHETYPE
+ __attribute__((__format__(__printf__, 2, 0)));
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_STRINGPRINTF_H
diff --git a/base/include/android-base/strings.h b/base/include/android-base/strings.h
index 4d9fa34..9c35560 100644
--- a/base/include/android-base/strings.h
+++ b/base/include/android-base/strings.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_STRINGS_H
-#define ANDROID_BASE_STRINGS_H
+#pragma once
#include <sstream>
#include <string>
@@ -75,5 +74,3 @@
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_STRINGS_H
diff --git a/base/include/android-base/test_utils.h b/base/include/android-base/test_utils.h
index 2edafe3..9e2ea97 100644
--- a/base/include/android-base/test_utils.h
+++ b/base/include/android-base/test_utils.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_TEST_UTILS_H
-#define ANDROID_BASE_TEST_UTILS_H
+#pragma once
#include <regex>
#include <string>
@@ -31,6 +30,8 @@
// Release the ownership of fd, caller is reponsible for closing the
// fd or stream properly.
int release();
+ // Don't remove the temporary file in the destructor.
+ void DoNotRemove() { remove_file_ = false; }
int fd;
char path[1024];
@@ -38,6 +39,8 @@
private:
void init(const std::string& tmp_dir);
+ bool remove_file_ = true;
+
DISALLOW_COPY_AND_ASSIGN(TemporaryFile);
};
@@ -54,21 +57,33 @@
DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
};
-class CapturedStderr {
+class CapturedStdFd {
public:
- CapturedStderr();
- ~CapturedStderr();
+ CapturedStdFd(int std_fd);
+ ~CapturedStdFd();
int fd() const;
+ std::string str();
private:
- void init();
- void reset();
+ void Init();
+ void Reset();
TemporaryFile temp_file_;
- int old_stderr_;
+ int std_fd_;
+ int old_fd_;
- DISALLOW_COPY_AND_ASSIGN(CapturedStderr);
+ DISALLOW_COPY_AND_ASSIGN(CapturedStdFd);
+};
+
+class CapturedStderr : public CapturedStdFd {
+ public:
+ CapturedStderr() : CapturedStdFd(STDERR_FILENO) {}
+};
+
+class CapturedStdout : public CapturedStdFd {
+ public:
+ CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
};
#define ASSERT_MATCH(str, pattern) \
@@ -98,5 +113,3 @@
ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
} \
} while (0)
-
-#endif // ANDROID_BASE_TEST_UTILS_H
diff --git a/base/include/android-base/thread_annotations.h b/base/include/android-base/thread_annotations.h
index 1307f0e..5c55e63 100644
--- a/base/include/android-base/thread_annotations.h
+++ b/base/include/android-base/thread_annotations.h
@@ -14,14 +14,9 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_THREAD_ANNOTATIONS_H
-#define ANDROID_BASE_THREAD_ANNOTATIONS_H
+#pragma once
-#if defined(__SUPPORT_TS_ANNOTATION__) || defined(__clang__)
-#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
-#else
-#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
-#endif
+#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
#define CAPABILITY(x) \
THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
@@ -109,5 +104,3 @@
#define NO_THREAD_SAFETY_ANALYSIS \
THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
-
-#endif // ANDROID_BASE_THREAD_ANNOTATIONS_H
diff --git a/init/watchdogd.h b/base/include/android-base/threads.h
similarity index 69%
rename from init/watchdogd.h
rename to base/include/android-base/threads.h
index 73f77d5..dba1fc6 100644
--- a/init/watchdogd.h
+++ b/base/include/android-base/threads.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2012 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,15 +14,17 @@
* limitations under the License.
*/
-#ifndef _INIT_WATCHDOGD_H_
-#define _INIT_WATCHDOGD_H_
+#pragma once
+
+#include <stdint.h>
namespace android {
-namespace init {
-
-int watchdogd_main(int argc, char **argv);
-
-} // namespace init
+namespace base {
+uint64_t GetThreadId();
+}
} // namespace android
+#if defined(__GLIBC__)
+// bionic has this Linux-specifix call, but glibc doesn't.
+extern "C" int tgkill(int tgid, int tid, int sig);
#endif
diff --git a/base/include/android-base/unique_fd.h b/base/include/android-base/unique_fd.h
index 5d89271..c6936f1 100644
--- a/base/include/android-base/unique_fd.h
+++ b/base/include/android-base/unique_fd.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_UNIQUE_FD_H
-#define ANDROID_BASE_UNIQUE_FD_H
+#pragma once
#include <fcntl.h>
@@ -43,10 +42,35 @@
//
// unique_fd is also known as ScopedFd/ScopedFD/scoped_fd; mentioned here to help
// you find this class if you're searching for one of those names.
+
+#if defined(__BIONIC__)
+#include <android/fdsan.h>
+#endif
+
namespace android {
namespace base {
struct DefaultCloser {
+#if defined(__BIONIC__)
+ static void Tag(int fd, void* old_addr, void* new_addr) {
+ if (android_fdsan_exchange_owner_tag) {
+ uint64_t old_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
+ reinterpret_cast<uint64_t>(old_addr));
+ uint64_t new_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
+ reinterpret_cast<uint64_t>(new_addr));
+ android_fdsan_exchange_owner_tag(fd, old_tag, new_tag);
+ }
+ }
+ static void Close(int fd, void* addr) {
+ if (android_fdsan_close_with_tag) {
+ uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
+ reinterpret_cast<uint64_t>(addr));
+ android_fdsan_close_with_tag(fd, tag);
+ } else {
+ close(fd);
+ }
+ }
+#else
static void Close(int fd) {
// Even if close(2) fails with EINTR, the fd will have been closed.
// Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone
@@ -54,40 +78,75 @@
// http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
::close(fd);
}
+#endif
};
template <typename Closer>
class unique_fd_impl final {
public:
- unique_fd_impl() : value_(-1) {}
+ unique_fd_impl() {}
- explicit unique_fd_impl(int value) : value_(value) {}
+ explicit unique_fd_impl(int fd) { reset(fd); }
~unique_fd_impl() { reset(); }
- unique_fd_impl(unique_fd_impl&& other) : value_(other.release()) {}
+ unique_fd_impl(unique_fd_impl&& other) { reset(other.release()); }
unique_fd_impl& operator=(unique_fd_impl&& s) {
- reset(s.release());
+ int fd = s.fd_;
+ s.fd_ = -1;
+ reset(fd, &s);
return *this;
}
- void reset(int new_value = -1) {
- if (value_ != -1) {
- Closer::Close(value_);
- }
- value_ = new_value;
- }
+ void reset(int new_value = -1) { reset(new_value, nullptr); }
- int get() const { return value_; }
+ int get() const { return fd_; }
operator int() const { return get(); }
int release() __attribute__((warn_unused_result)) {
- int ret = value_;
- value_ = -1;
+ tag(fd_, this, nullptr);
+ int ret = fd_;
+ fd_ = -1;
return ret;
}
private:
- int value_;
+ void reset(int new_value, void* previous_tag) {
+ if (fd_ != -1) {
+ close(fd_, this);
+ }
+
+ fd_ = new_value;
+ if (new_value != -1) {
+ tag(new_value, previous_tag, this);
+ }
+ }
+
+ int fd_ = -1;
+
+ // Template magic to use Closer::Tag if available, and do nothing if not.
+ // If Closer::Tag exists, this implementation is preferred, because int is a better match.
+ // If not, this implementation is SFINAEd away, and the no-op below is the only one that exists.
+ template <typename T = Closer>
+ static auto tag(int fd, void* old_tag, void* new_tag)
+ -> decltype(T::Tag(fd, old_tag, new_tag), void()) {
+ T::Tag(fd, old_tag, new_tag);
+ }
+
+ template <typename T = Closer>
+ static void tag(long, void*, void*) {
+ // No-op.
+ }
+
+ // Same as above, to select between Closer::Close(int) and Closer::Close(int, void*).
+ template <typename T = Closer>
+ static auto close(int fd, void* tag_value) -> decltype(T::Close(fd, tag_value), void()) {
+ T::Close(fd, tag_value);
+ }
+
+ template <typename T = Closer>
+ static auto close(int fd, void*) -> decltype(T::Close(fd), void()) {
+ T::Close(fd);
+ }
unique_fd_impl(const unique_fd_impl&);
void operator=(const unique_fd_impl&);
@@ -98,7 +157,8 @@
#if !defined(_WIN32)
// Inline functions, so that they can be used header-only.
-inline bool Pipe(unique_fd* read, unique_fd* write) {
+template <typename Closer>
+inline bool Pipe(unique_fd_impl<Closer>* read, unique_fd_impl<Closer>* write) {
int pipefd[2];
#if defined(__linux__)
@@ -122,7 +182,9 @@
return true;
}
-inline bool Socketpair(int domain, int type, int protocol, unique_fd* left, unique_fd* right) {
+template <typename Closer>
+inline bool Socketpair(int domain, int type, int protocol, unique_fd_impl<Closer>* left,
+ unique_fd_impl<Closer>* right) {
int sockfd[2];
if (socketpair(domain, type, protocol, sockfd) != 0) {
return false;
@@ -132,7 +194,8 @@
return true;
}
-inline bool Socketpair(int type, unique_fd* left, unique_fd* right) {
+template <typename Closer>
+inline bool Socketpair(int type, unique_fd_impl<Closer>* left, unique_fd_impl<Closer>* right) {
return Socketpair(AF_UNIX, type, 0, left, right);
}
@@ -143,12 +206,4 @@
template <typename T>
int close(const android::base::unique_fd_impl<T>&)
-#if defined(__clang__)
- __attribute__((__unavailable__(
-#else
- __attribute__((__error__(
-#endif
- "close called on unique_fd"
- )));
-
-#endif // ANDROID_BASE_UNIQUE_FD_H
+ __attribute__((__unavailable__("close called on unique_fd")));
diff --git a/base/include/android-base/utf8.h b/base/include/android-base/utf8.h
old mode 100755
new mode 100644
index c9cc1ab..4b91623
--- a/base/include/android-base/utf8.h
+++ b/base/include/android-base/utf8.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef ANDROID_BASE_UTF8_H
-#define ANDROID_BASE_UTF8_H
+#pragma once
#ifdef _WIN32
#include <string>
@@ -102,5 +101,3 @@
} // namespace utf8
} // namespace base
} // namespace android
-
-#endif // ANDROID_BASE_UTF8_H
diff --git a/base/logging.cpp b/base/logging.cpp
index 1f7bc2a..d60d91d 100644
--- a/base/logging.cpp
+++ b/base/logging.cpp
@@ -21,6 +21,7 @@
#include "android-base/logging.h"
#include <fcntl.h>
+#include <inttypes.h>
#include <libgen.h>
#include <time.h>
@@ -53,50 +54,19 @@
#endif
#include <android-base/macros.h>
+#include <android-base/parseint.h>
#include <android-base/strings.h>
+#include <android-base/threads.h>
-// For gettid.
-#if defined(__APPLE__)
-#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
-#include <stdint.h>
-#include <stdlib.h>
-#include <sys/syscall.h>
-#include <sys/time.h>
-#include <unistd.h>
-#elif defined(__linux__) && !defined(__ANDROID__)
-#include <syscall.h>
-#include <unistd.h>
-#elif defined(_WIN32)
-#include <windows.h>
-#endif
+namespace android {
+namespace base {
-#if defined(_WIN32)
-typedef uint32_t thread_id;
-#else
-typedef pid_t thread_id;
-#endif
-
-static thread_id GetThreadId() {
-#if defined(__BIONIC__)
- return gettid();
-#elif defined(__APPLE__)
- uint64_t tid;
- pthread_threadid_np(NULL, &tid);
- return tid;
-#elif defined(__linux__)
- return syscall(__NR_gettid);
-#elif defined(_WIN32)
- return GetCurrentThreadId();
-#endif
-}
-
-namespace {
+// BSD-based systems like Android/macOS have getprogname(). Others need us to provide one.
+#if defined(__GLIBC__) || defined(_WIN32)
+static const char* getprogname() {
#if defined(__GLIBC__)
-const char* getprogname() {
return program_invocation_short_name;
-}
#elif defined(_WIN32)
-const char* getprogname() {
static bool first = true;
static char progname[MAX_PATH] = {};
@@ -113,12 +83,42 @@
}
return progname;
+#endif
}
#endif
-} // namespace
-namespace android {
-namespace base {
+static const char* GetFileBasename(const char* file) {
+ // We can't use basename(3) even on Unix because the Mac doesn't
+ // have a non-modifying basename.
+ const char* last_slash = strrchr(file, '/');
+ if (last_slash != nullptr) {
+ return last_slash + 1;
+ }
+#if defined(_WIN32)
+ const char* last_backslash = strrchr(file, '\\');
+ if (last_backslash != nullptr) {
+ return last_backslash + 1;
+ }
+#endif
+ return file;
+}
+
+#if defined(__linux__)
+static int OpenKmsg() {
+#if defined(__ANDROID__)
+ // pick up 'file w /dev/kmsg' environment from daemon's init rc file
+ const auto val = getenv("ANDROID_FILE__dev_kmsg");
+ if (val != nullptr) {
+ int fd;
+ if (android::base::ParseInt(val, &fd, 0)) {
+ auto flags = fcntl(fd, F_GETFL);
+ if ((flags != -1) && ((flags & O_ACCMODE) == O_WRONLY)) return fd;
+ }
+ }
+#endif
+ return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC));
+}
+#endif
static std::mutex& LoggingLock() {
static auto& logging_lock = *new std::mutex();
@@ -139,9 +139,27 @@
return aborter;
}
-static std::string& ProgramInvocationName() {
- static auto& programInvocationName = *new std::string(getprogname());
- return programInvocationName;
+static std::recursive_mutex& TagLock() {
+ static auto& tag_lock = *new std::recursive_mutex();
+ return tag_lock;
+}
+static std::string* gDefaultTag;
+std::string GetDefaultTag() {
+ std::lock_guard<std::recursive_mutex> lock(TagLock());
+ if (gDefaultTag == nullptr) {
+ return "";
+ }
+ return *gDefaultTag;
+}
+void SetDefaultTag(const std::string& tag) {
+ std::lock_guard<std::recursive_mutex> lock(TagLock());
+ if (gDefaultTag != nullptr) {
+ delete gDefaultTag;
+ gDefaultTag = nullptr;
+ }
+ if (!tag.empty()) {
+ gDefaultTag = new std::string(tag);
+ }
}
static bool gInitialized = false;
@@ -165,7 +183,7 @@
static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1,
"Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity");
- static int klog_fd = TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC));
+ static int klog_fd = OpenKmsg();
if (klog_fd == -1) return;
int level = kLogSeverityToKernelLogLevel[severity];
@@ -205,8 +223,18 @@
static_assert(arraysize(log_characters) - 1 == FATAL + 1,
"Mismatch in size of log_characters and values in LogSeverity");
char severity_char = log_characters[severity];
- fprintf(stderr, "%s %c %s %5d %5d %s:%u] %s\n", tag ? tag : "nullptr", severity_char, timestamp,
- getpid(), GetThreadId(), file, line, message);
+ fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s:%u] %s\n", tag ? tag : "nullptr", severity_char,
+ timestamp, getpid(), GetThreadId(), file, line, message);
+}
+
+void StdioLogger(LogId, LogSeverity severity, const char* /*tag*/, const char* /*file*/,
+ unsigned int /*line*/, const char* message) {
+ if (severity >= WARNING) {
+ fflush(stdout);
+ fprintf(stderr, "%s: %s\n", GetFileBasename(getprogname()), message);
+ } else {
+ fprintf(stdout, "%s\n", message);
+ }
}
void DefaultAborter(const char* abort_message) {
@@ -269,8 +297,7 @@
// Linux to recover this, but we don't have that luxury on the Mac/Windows,
// and there are a couple of argv[0] variants that are commonly used.
if (argv != nullptr) {
- std::lock_guard<std::mutex> lock(LoggingLock());
- ProgramInvocationName() = basename(argv[0]);
+ SetDefaultTag(basename(argv[0]));
}
const char* tags = getenv("ANDROID_LOG_TAGS");
@@ -324,22 +351,6 @@
Aborter() = std::move(aborter);
}
-static const char* GetFileBasename(const char* file) {
- // We can't use basename(3) even on Unix because the Mac doesn't
- // have a non-modifying basename.
- const char* last_slash = strrchr(file, '/');
- if (last_slash != nullptr) {
- return last_slash + 1;
- }
-#if defined(_WIN32)
- const char* last_backslash = strrchr(file, '\\');
- if (last_backslash != nullptr) {
- return last_backslash + 1;
- }
-#endif
- return file;
-}
-
// This indirection greatly reduces the stack impact of having lots of
// checks/logging in a function.
class LogMessageData {
@@ -399,10 +410,6 @@
const char* tag, int error)
: data_(new LogMessageData(file, line, id, severity, tag, error)) {}
-LogMessage::LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity,
- int error)
- : LogMessage(file, line, id, severity, nullptr, error) {}
-
LogMessage::~LogMessage() {
// Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM.
if (!WOULD_LOG(data_->GetSeverity())) {
@@ -448,13 +455,15 @@
void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
const char* tag, const char* message) {
- if (tag == nullptr) tag = ProgramInvocationName().c_str();
- Logger()(id, severity, tag, file, line, message);
-}
-
-void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
- const char* message) {
- LogLine(file, line, id, severity, nullptr, message);
+ if (tag == nullptr) {
+ std::lock_guard<std::recursive_mutex> lock(TagLock());
+ if (gDefaultTag == nullptr) {
+ gDefaultTag = new std::string(getprogname());
+ }
+ Logger()(id, severity, gDefaultTag->c_str(), file, line, message);
+ } else {
+ Logger()(id, severity, tag, file, line, message);
+ }
}
LogSeverity GetMinimumLogSeverity() {
diff --git a/base/logging_test.cpp b/base/logging_test.cpp
index 6f05d9b..75b4ea0 100644
--- a/base/logging_test.cpp
+++ b/base/logging_test.cpp
@@ -206,20 +206,27 @@
}
#endif
-static void CheckMessage(const CapturedStderr& cap,
- android::base::LogSeverity severity, const char* expected) {
- std::string output;
- ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
- android::base::ReadFdToString(cap.fd(), &output);
+static void CheckMessage(CapturedStderr& cap, android::base::LogSeverity severity,
+ const char* expected, const char* expected_tag = nullptr) {
+ std::string output = cap.str();
// We can't usefully check the output of any of these on Windows because we
// don't have std::regex, but we can at least make sure we printed at least as
// many characters are in the log message.
ASSERT_GT(output.length(), strlen(expected));
ASSERT_NE(nullptr, strstr(output.c_str(), expected)) << output;
+ if (expected_tag != nullptr) {
+ ASSERT_NE(nullptr, strstr(output.c_str(), expected_tag)) << output;
+ }
#if !defined(_WIN32)
- std::regex message_regex(make_log_pattern(severity, expected));
+ std::string regex_str;
+ if (expected_tag != nullptr) {
+ regex_str.append(expected_tag);
+ regex_str.append(" ");
+ }
+ regex_str.append(make_log_pattern(severity, expected));
+ std::regex message_regex(regex_str);
ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
#endif
}
@@ -600,3 +607,37 @@
__attribute__((constructor)) void TestLoggingInConstructor() {
LOG(ERROR) << "foobar";
}
+
+TEST(logging, SetDefaultTag) {
+ constexpr const char* expected_tag = "test_tag";
+ constexpr const char* expected_msg = "foobar";
+ CapturedStderr cap;
+ {
+ std::string old_default_tag = android::base::GetDefaultTag();
+ android::base::SetDefaultTag(expected_tag);
+ android::base::ScopedLogSeverity sls(android::base::LogSeverity::INFO);
+ LOG(INFO) << expected_msg;
+ android::base::SetDefaultTag(old_default_tag);
+ }
+ CheckMessage(cap, android::base::LogSeverity::INFO, expected_msg, expected_tag);
+}
+
+TEST(logging, StdioLogger) {
+ std::string err_str;
+ std::string out_str;
+ {
+ CapturedStderr cap_err;
+ CapturedStdout cap_out;
+ android::base::SetLogger(android::base::StdioLogger);
+ LOG(INFO) << "out";
+ LOG(ERROR) << "err";
+ err_str = cap_err.str();
+ out_str = cap_out.str();
+ }
+
+ // For INFO we expect just the literal "out\n".
+ ASSERT_EQ("out\n", out_str) << out_str;
+ // Whereas ERROR logging includes the program name.
+ ASSERT_EQ(android::base::Basename(android::base::GetExecutablePath()) + ": err\n", err_str)
+ << err_str;
+}
diff --git a/init/watchdogd.h b/base/macros_test.cpp
similarity index 65%
copy from init/watchdogd.h
copy to base/macros_test.cpp
index 73f77d5..2b522db 100644
--- a/init/watchdogd.h
+++ b/base/macros_test.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2012 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,15 +14,17 @@
* limitations under the License.
*/
-#ifndef _INIT_WATCHDOGD_H_
-#define _INIT_WATCHDOGD_H_
+#include "android-base/macros.h"
-namespace android {
-namespace init {
+#include <stdint.h>
-int watchdogd_main(int argc, char **argv);
+#include <gtest/gtest.h>
-} // namespace init
-} // namespace android
-
-#endif
+TEST(macros, SIZEOF_MEMBER_macro) {
+ struct S {
+ int32_t i32;
+ double d;
+ };
+ ASSERT_EQ(4U, SIZEOF_MEMBER(S, i32));
+ ASSERT_EQ(8U, SIZEOF_MEMBER(S, d));
+}
diff --git a/base/parsedouble_test.cpp b/base/parsedouble_test.cpp
index 8734c42..ec3c10c 100644
--- a/base/parsedouble_test.cpp
+++ b/base/parsedouble_test.cpp
@@ -18,7 +18,7 @@
#include <gtest/gtest.h>
-TEST(parsedouble, smoke) {
+TEST(parsedouble, double_smoke) {
double d;
ASSERT_FALSE(android::base::ParseDouble("", &d));
ASSERT_FALSE(android::base::ParseDouble("x", &d));
@@ -35,4 +35,33 @@
ASSERT_FALSE(android::base::ParseDouble("3.0", &d, -1.0, 2.0));
ASSERT_TRUE(android::base::ParseDouble("1.0", &d, 0.0, 2.0));
ASSERT_DOUBLE_EQ(1.0, d);
+
+ ASSERT_FALSE(android::base::ParseDouble("123.4x", nullptr));
+ ASSERT_TRUE(android::base::ParseDouble("-123.4", nullptr));
+ ASSERT_FALSE(android::base::ParseDouble("3.0", nullptr, -1.0, 2.0));
+ ASSERT_TRUE(android::base::ParseDouble("1.0", nullptr, 0.0, 2.0));
+}
+
+TEST(parsedouble, float_smoke) {
+ float f;
+ ASSERT_FALSE(android::base::ParseFloat("", &f));
+ ASSERT_FALSE(android::base::ParseFloat("x", &f));
+ ASSERT_FALSE(android::base::ParseFloat("123.4x", &f));
+
+ ASSERT_TRUE(android::base::ParseFloat("123.4", &f));
+ ASSERT_FLOAT_EQ(123.4, f);
+ ASSERT_TRUE(android::base::ParseFloat("-123.4", &f));
+ ASSERT_FLOAT_EQ(-123.4, f);
+
+ ASSERT_TRUE(android::base::ParseFloat("0", &f, 0.0));
+ ASSERT_FLOAT_EQ(0.0, f);
+ ASSERT_FALSE(android::base::ParseFloat("0", &f, 1e-9));
+ ASSERT_FALSE(android::base::ParseFloat("3.0", &f, -1.0, 2.0));
+ ASSERT_TRUE(android::base::ParseFloat("1.0", &f, 0.0, 2.0));
+ ASSERT_FLOAT_EQ(1.0, f);
+
+ ASSERT_FALSE(android::base::ParseFloat("123.4x", nullptr));
+ ASSERT_TRUE(android::base::ParseFloat("-123.4", nullptr));
+ ASSERT_FALSE(android::base::ParseFloat("3.0", nullptr, -1.0, 2.0));
+ ASSERT_TRUE(android::base::ParseFloat("1.0", nullptr, 0.0, 2.0));
}
diff --git a/base/parseint_test.cpp b/base/parseint_test.cpp
index 483b1d3..8f9ed77 100644
--- a/base/parseint_test.cpp
+++ b/base/parseint_test.cpp
@@ -36,6 +36,10 @@
ASSERT_EQ(12, i);
ASSERT_FALSE(android::base::ParseInt("-12", &i, 0, 15));
ASSERT_FALSE(android::base::ParseInt("16", &i, 0, 15));
+
+ ASSERT_FALSE(android::base::ParseInt<int>("x", nullptr));
+ ASSERT_FALSE(android::base::ParseInt<int>("123x", nullptr));
+ ASSERT_TRUE(android::base::ParseInt<int>("1234", nullptr));
}
TEST(parseint, unsigned_smoke) {
@@ -55,6 +59,10 @@
ASSERT_EQ(12u, i);
ASSERT_FALSE(android::base::ParseUint("-12", &i, 15u));
ASSERT_FALSE(android::base::ParseUint("16", &i, 15u));
+
+ ASSERT_FALSE(android::base::ParseUint<unsigned short>("x", nullptr));
+ ASSERT_FALSE(android::base::ParseUint<unsigned short>("123x", nullptr));
+ ASSERT_TRUE(android::base::ParseUint<unsigned short>("1234", nullptr));
}
TEST(parseint, no_implicit_octal) {
@@ -96,3 +104,44 @@
ASSERT_FALSE(android::base::ParseInt("456x", &u));
ASSERT_EQ(123u, u);
}
+
+TEST(parseint, ParseByteCount) {
+ uint64_t i = 0;
+ ASSERT_TRUE(android::base::ParseByteCount("123b", &i));
+ ASSERT_EQ(123ULL, i);
+
+ ASSERT_TRUE(android::base::ParseByteCount("8k", &i));
+ ASSERT_EQ(8ULL * 1024, i);
+
+ ASSERT_TRUE(android::base::ParseByteCount("8M", &i));
+ ASSERT_EQ(8ULL * 1024 * 1024, i);
+
+ ASSERT_TRUE(android::base::ParseByteCount("6g", &i));
+ ASSERT_EQ(6ULL * 1024 * 1024 * 1024, i);
+
+ ASSERT_TRUE(android::base::ParseByteCount("1T", &i));
+ ASSERT_EQ(1ULL * 1024 * 1024 * 1024 * 1024, i);
+
+ ASSERT_TRUE(android::base::ParseByteCount("2p", &i));
+ ASSERT_EQ(2ULL * 1024 * 1024 * 1024 * 1024 * 1024, i);
+
+ ASSERT_TRUE(android::base::ParseByteCount("4e", &i));
+ ASSERT_EQ(4ULL * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, i);
+}
+
+TEST(parseint, ParseByteCount_invalid_suffix) {
+ unsigned u;
+ ASSERT_FALSE(android::base::ParseByteCount("1x", &u));
+}
+
+TEST(parseint, ParseByteCount_overflow) {
+ uint64_t u64;
+ ASSERT_FALSE(android::base::ParseByteCount("4294967295E", &u64));
+
+ uint16_t u16;
+ ASSERT_TRUE(android::base::ParseByteCount("63k", &u16));
+ ASSERT_EQ(63U * 1024, u16);
+ ASSERT_TRUE(android::base::ParseByteCount("65535b", &u16));
+ ASSERT_EQ(65535U, u16);
+ ASSERT_FALSE(android::base::ParseByteCount("65k", &u16));
+}
diff --git a/base/properties.cpp b/base/properties.cpp
index 6cf43f9..d5a5918 100644
--- a/base/properties.cpp
+++ b/base/properties.cpp
@@ -14,16 +14,18 @@
* limitations under the License.
*/
-#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
-
#include "android-base/properties.h"
+#if defined(__BIONIC__)
+#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <sys/system_properties.h>
#include <sys/_system_properties.h>
+#endif
#include <algorithm>
#include <chrono>
#include <limits>
+#include <map>
#include <string>
#include <android-base/parseint.h>
@@ -31,24 +33,6 @@
namespace android {
namespace base {
-std::string GetProperty(const std::string& key, const std::string& default_value) {
- const prop_info* pi = __system_property_find(key.c_str());
- if (pi == nullptr) return default_value;
-
- std::string property_value;
- __system_property_read_callback(pi,
- [](void* cookie, const char*, const char* value, unsigned) {
- auto property_value = reinterpret_cast<std::string*>(cookie);
- *property_value = value;
- },
- &property_value);
-
- // If the property exists but is empty, also return the default value.
- // Since we can't remove system properties, "empty" is traditionally
- // the same as "missing" (this was true for cutils' property_get).
- return property_value.empty() ? default_value : property_value;
-}
-
bool GetBoolProperty(const std::string& key, bool default_value) {
std::string value = GetProperty(key, "");
if (value == "1" || value == "y" || value == "yes" || value == "on" || value == "true") {
@@ -85,10 +69,43 @@
template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t);
template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t);
+#if !defined(__BIONIC__)
+static std::map<std::string, std::string>& g_properties = *new std::map<std::string, std::string>;
+static int __system_property_set(const char* key, const char* value) {
+ g_properties[key] = value;
+ return 0;
+}
+#endif
+
+std::string GetProperty(const std::string& key, const std::string& default_value) {
+ std::string property_value;
+#if defined(__BIONIC__)
+ const prop_info* pi = __system_property_find(key.c_str());
+ if (pi == nullptr) return default_value;
+
+ __system_property_read_callback(pi,
+ [](void* cookie, const char*, const char* value, unsigned) {
+ auto property_value = reinterpret_cast<std::string*>(cookie);
+ *property_value = value;
+ },
+ &property_value);
+#else
+ auto it = g_properties.find(key);
+ if (it == g_properties.end()) return default_value;
+ property_value = it->second;
+#endif
+ // If the property exists but is empty, also return the default value.
+ // Since we can't remove system properties, "empty" is traditionally
+ // the same as "missing" (this was true for cutils' property_get).
+ return property_value.empty() ? default_value : property_value;
+}
+
bool SetProperty(const std::string& key, const std::string& value) {
return (__system_property_set(key.c_str(), value.c_str()) == 0);
}
+#if defined(__BIONIC__)
+
struct WaitForPropertyData {
bool done;
const std::string* expected_value;
@@ -175,5 +192,7 @@
return (WaitForPropertyCreation(key, relative_timeout, start_time) != nullptr);
}
+#endif
+
} // namespace base
} // namespace android
diff --git a/base/properties_test.cpp b/base/properties_test.cpp
index de5f3dc..e7d4880 100644
--- a/base/properties_test.cpp
+++ b/base/properties_test.cpp
@@ -23,7 +23,9 @@
#include <string>
#include <thread>
-using namespace std::chrono_literals;
+#if !defined(_WIN32)
+using namespace std::literals;
+#endif
TEST(properties, smoke) {
android::base::SetProperty("debug.libbase.property_test", "hello");
@@ -126,6 +128,7 @@
TEST(properties, GetUintProperty_uint64_t) { CheckGetUintProperty<uint64_t>(); }
TEST(properties, WaitForProperty) {
+#if defined(__BIONIC__)
std::atomic<bool> flag{false};
std::thread thread([&]() {
std::this_thread::sleep_for(100ms);
@@ -138,9 +141,13 @@
flag = true;
ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b", 1s));
thread.join();
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
TEST(properties, WaitForProperty_timeout) {
+#if defined(__BIONIC__)
auto t0 = std::chrono::steady_clock::now();
ASSERT_FALSE(android::base::WaitForProperty("debug.libbase.WaitForProperty_timeout_test", "a",
200ms));
@@ -149,9 +156,13 @@
ASSERT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 200ms);
// Upper bounds on timing are inherently flaky, but let's try...
ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 600ms);
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
TEST(properties, WaitForProperty_MaxTimeout) {
+#if defined(__BIONIC__)
std::atomic<bool> flag{false};
std::thread thread([&]() {
android::base::SetProperty("debug.libbase.WaitForProperty_test", "a");
@@ -165,9 +176,13 @@
// Test that this does not immediately return false due to overflow issues with the timeout.
ASSERT_TRUE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b"));
thread.join();
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
TEST(properties, WaitForProperty_NegativeTimeout) {
+#if defined(__BIONIC__)
std::atomic<bool> flag{false};
std::thread thread([&]() {
android::base::SetProperty("debug.libbase.WaitForProperty_test", "a");
@@ -181,9 +196,13 @@
// Assert that this immediately returns with a negative timeout
ASSERT_FALSE(android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b", -100ms));
thread.join();
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
TEST(properties, WaitForPropertyCreation) {
+#if defined(__BIONIC__)
std::thread thread([&]() {
std::this_thread::sleep_for(100ms);
android::base::SetProperty("debug.libbase.WaitForPropertyCreation_test", "a");
@@ -192,9 +211,13 @@
ASSERT_TRUE(android::base::WaitForPropertyCreation(
"debug.libbase.WaitForPropertyCreation_test", 1s));
thread.join();
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
TEST(properties, WaitForPropertyCreation_timeout) {
+#if defined(__BIONIC__)
auto t0 = std::chrono::steady_clock::now();
ASSERT_FALSE(android::base::WaitForPropertyCreation(
"debug.libbase.WaitForPropertyCreation_timeout_test", 200ms));
@@ -203,4 +226,7 @@
ASSERT_GE(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 200ms);
// Upper bounds on timing are inherently flaky, but let's try...
ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0), 600ms);
+#else
+ GTEST_LOG_(INFO) << "This test does nothing on the host.\n";
+#endif
}
diff --git a/base/scopeguard_test.cpp b/base/scopeguard_test.cpp
index e11154a..9236d7b 100644
--- a/base/scopeguard_test.cpp
+++ b/base/scopeguard_test.cpp
@@ -17,6 +17,7 @@
#include "android-base/scopeguard.h"
#include <utility>
+#include <vector>
#include <gtest/gtest.h>
@@ -44,3 +45,15 @@
EXPECT_FALSE(scopeguard.active());
ASSERT_FALSE(guarded_var);
}
+
+TEST(scopeguard, vector) {
+ int guarded_var = 0;
+ {
+ std::vector<android::base::ScopeGuard<std::function<void()>>> scopeguards;
+ scopeguards.emplace_back(android::base::make_scope_guard(
+ std::bind([](int& guarded_var) { guarded_var++; }, std::ref(guarded_var))));
+ scopeguards.emplace_back(android::base::make_scope_guard(
+ std::bind([](int& guarded_var) { guarded_var++; }, std::ref(guarded_var))));
+ }
+ ASSERT_EQ(guarded_var, 2);
+}
diff --git a/base/test_utils.cpp b/base/test_utils.cpp
index 9d8dfb2..5096369 100644
--- a/base/test_utils.cpp
+++ b/base/test_utils.cpp
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-#include "android-base/logging.h"
#include "android-base/test_utils.h"
#include <fcntl.h>
@@ -33,6 +32,9 @@
#include <string>
+#include <android-base/file.h>
+#include <android-base/logging.h>
+
#ifdef _WIN32
int mkstemp(char* template_name) {
if (_mktemp(template_name) == nullptr) {
@@ -92,7 +94,9 @@
if (fd != -1) {
close(fd);
}
- unlink(path);
+ if (remove_file_) {
+ unlink(path);
+ }
}
int TemporaryFile::release() {
@@ -121,31 +125,38 @@
return (mkdtemp(path) != nullptr);
}
-CapturedStderr::CapturedStderr() : old_stderr_(-1) {
- init();
+CapturedStdFd::CapturedStdFd(int std_fd) : std_fd_(std_fd), old_fd_(-1) {
+ Init();
}
-CapturedStderr::~CapturedStderr() {
- reset();
+CapturedStdFd::~CapturedStdFd() {
+ Reset();
}
-int CapturedStderr::fd() const {
+int CapturedStdFd::fd() const {
return temp_file_.fd;
}
-void CapturedStderr::init() {
+std::string CapturedStdFd::str() {
+ std::string result;
+ CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET)));
+ android::base::ReadFdToString(fd(), &result);
+ return result;
+}
+
+void CapturedStdFd::Init() {
#if defined(_WIN32)
// On Windows, stderr is often buffered, so make sure it is unbuffered so
// that we can immediately read back what was written to stderr.
- CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
+ if (std_fd_ == STDERR_FILENO) CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
#endif
- old_stderr_ = dup(STDERR_FILENO);
- CHECK_NE(-1, old_stderr_);
- CHECK_NE(-1, dup2(fd(), STDERR_FILENO));
+ old_fd_ = dup(std_fd_);
+ CHECK_NE(-1, old_fd_);
+ CHECK_NE(-1, dup2(fd(), std_fd_));
}
-void CapturedStderr::reset() {
- CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO));
- CHECK_EQ(0, close(old_stderr_));
+void CapturedStdFd::Reset() {
+ CHECK_NE(-1, dup2(old_fd_, std_fd_));
+ CHECK_EQ(0, close(old_fd_));
// Note: cannot restore prior setvbuf() setting.
}
diff --git a/base/threads.cpp b/base/threads.cpp
new file mode 100644
index 0000000..48f6197
--- /dev/null
+++ b/base/threads.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2018 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/threads.h>
+
+#include <stdint.h>
+#include <unistd.h>
+
+#if defined(__APPLE__)
+#include <pthread.h>
+#elif defined(__linux__) && !defined(__ANDROID__)
+#include <syscall.h>
+#elif defined(_WIN32)
+#include <windows.h>
+#endif
+
+namespace android {
+namespace base {
+
+uint64_t GetThreadId() {
+#if defined(__BIONIC__)
+ return gettid();
+#elif defined(__APPLE__)
+ uint64_t tid;
+ pthread_threadid_np(NULL, &tid);
+ return tid;
+#elif defined(__linux__)
+ return syscall(__NR_gettid);
+#elif defined(_WIN32)
+ return GetCurrentThreadId();
+#endif
+}
+
+} // namespace base
+} // namespace android
+
+#if defined(__GLIBC__)
+int tgkill(int tgid, int tid, int sig) {
+ return syscall(__NR_tgkill, tgid, tid, sig);
+}
+#endif
diff --git a/bootstat/Android.bp b/bootstat/Android.bp
index dd9ba88..5e2d171 100644
--- a/bootstat/Android.bp
+++ b/bootstat/Android.bp
@@ -63,7 +63,9 @@
name: "bootstat",
defaults: ["bootstat_defaults"],
static_libs: ["libbootstat"],
- shared_libs: ["liblogcat"],
+ shared_libs: [
+ "libstatslog"
+ ],
init_rc: ["bootstat.rc"],
product_variables: {
pdk: {
diff --git a/bootstat/boot_reason_test.sh b/bootstat/boot_reason_test.sh
index 79702a6..71d3ecb 100755
--- a/bootstat/boot_reason_test.sh
+++ b/bootstat/boot_reason_test.sh
@@ -24,6 +24,7 @@
NORMAL="${ESCAPE}[0m"
# Best guess to an average device's reboot time, refined as tests return
DURATION_DEFAULT=45
+STOP_ON_FAILURE=false
# Helper functions
@@ -50,11 +51,18 @@
fi
}
+[ "USAGE: get_property <prop>
+
+Returns the property value" ]
+get_property() {
+ adb shell getprop ${1} 2>&1 </dev/null
+}
+
[ "USAGE: isDebuggable
Returns: true if device is (likely) a debug build" ]
isDebuggable() {
- if inAdb && [ 1 -ne `adb shell getprop ro.debuggable` ]; then
+ if inAdb && [ 1 -ne `get_property ro.debuggable` ]; then
false
fi
}
@@ -93,7 +101,7 @@
return 1
fi
adb shell su root setprop persist.test.boot.reason "'${1}'" 2>/dev/null
- test_reason="`adb shell getprop persist.test.boot.reason 2>/dev/null`"
+ test_reason="`get_property persist.test.boot.reason`"
if [ X"${test_reason}" != X"${1}" ]; then
echo "ERROR: can not set persist.test.boot.reason to '${1}'." >&2
return 1
@@ -188,9 +196,9 @@
if [ 0 != ${counter} ]; then
adb wait-for-device </dev/null >/dev/null 2>/dev/null
fi
- if [ -n "`adb shell getprop sys.boot.reason </dev/null 2>/dev/null`" ]
+ if [ -n "`get_property sys.boot.reason`" ]
then
- vals=`adb shell getprop </dev/null 2>/dev/null |
+ vals=`get_property |
sed -n 's/[[]sys[.]\(boot_completed\|logbootcomplete\)[]]: [[]\([01]\)[]]$/\1=\2/p'`
if [ "${vals}" = "`echo boot_completed=1 ; echo logbootcomplete=1`" ]
then
@@ -223,15 +231,38 @@
rval="${2}"
shift 2
if ! ( echo X"${rval}" | grep '^X'"${lval}"'$' >/dev/null 2>/dev/null ); then
- echo "ERROR: expected \"${lval}\" got \"${rval}\"" >&2
- if [ -n "${*}" ] ; then
- echo " ${*}" >&2
+ if [ `echo ${lval}${rval}${*} | wc -c` -gt 50 -o "${rval}" != "${rval%
+*}" ]; then
+ echo "ERROR: expected \"${lval}\"" >&2
+ echo " got \"${rval}\"" |
+ sed ': again
+ N
+ s/\(\n\)\([^ ]\)/\1 \2/
+ t again' >&2
+ if [ -n "${*}" ] ; then
+ echo " ${*}" >&2
+ fi
+ else
+ echo "ERROR: expected \"${lval}\" got \"${rval}\" ${*}" >&2
fi
return 1
fi
if [ -n "${*}" ] ; then
if [ X"${lval}" != X"${rval}" ]; then
- echo "INFO: ok \"${lval}\"(=\"${rval}\") ${*}" >&2
+ if [ `echo ${lval}${rval}${*} | wc -c` -gt 60 -o "${rval}" != "${rval%
+*}" ]; then
+ echo "INFO: ok \"${lval}\"" >&2
+ echo " = \"${rval}\"" |
+ sed ': again
+ N
+ s/\(\n\)\([^ ]\)/\1 \2/
+ t again' >&2
+ if [ -n "${*}" ] ; then
+ echo " ${*}" >&2
+ fi
+ else
+ echo "INFO: ok \"${lval}\" = \"${rval}\" ${*}" >&2
+ fi
else
echo "INFO: ok \"${lval}\" ${*}" >&2
fi
@@ -239,6 +270,8 @@
return 0
}
+BAD_BOOTLOADER_REASON=
+
[ "USAGE: EXPECT_PROPERTY <prop> <value> [--allow_failure]
Returns true (0) if current return (regex) value is true and the result matches
@@ -248,10 +281,21 @@
property="${1}"
value="${2}"
shift 2
- val=`adb shell getprop ${property} 2>&1`
- EXPECT_EQ "${value}" "${val}" for Android property ${property} ||
- [ -n "${1}" ] ||
- save_ret=${?}
+ val=`get_property ${property}`
+ EXPECT_EQ "${value}" "${val}" for Android property ${property}
+ local_ret=${?}
+ if [ 0 != ${local_ret} -a "ro.boot.bootreason" = "${property}" ]; then
+ if [ -z "${BAD_BOOTLOADER_REASON}" ]; then
+ BAD_BOOTLOADER_REASON=${val}
+ elif [ X"${BAD_BOOTLOADER_REASON}" = X"${val}" ]; then
+ local_ret=0
+ fi
+ fi
+ if [ 0 != ${local_ret} ]; then
+ if [ -z "${1}" ] ; then
+ save_ret=${local_ret}
+ fi
+ fi
return ${save_ret}
}
@@ -287,6 +331,7 @@
bootstat: Service started: /system/bin/bootstat --record_boot_reason
bootstat: Service started: /system/bin/bootstat --record_time_since_factory_reset
bootstat: Service started: /system/bin/bootstat -l
+bootstat: Service started: /system/bin/bootstat --set_system_boot_reason --record_boot_complete --record_boot_reason --record_time_since_factory_reset -l
bootstat: Battery level at shutdown 100%
bootstat: Battery level at startup 100%
init : Parsing file /system/etc/init/bootstat.rc...
@@ -298,10 +343,12 @@
init : processing action (ro.boot.bootreason=*) from (/system/etc/init/bootstat.rc
init : processing action (sys.boot_completed=1 && sys.logbootcomplete=1) from (/system/etc/init/bootstat.rc
(/system/bin/bootstat --record_boot_complete --record_boot_reason --record_time_since_factory_reset -l)'
+ (/system/bin/bootstat --set_system_boot_reason --record_boot_complete --record_boot_reason --record_time_since_factory_reset -l)'
(/system/bin/bootstat -r post_decrypt_time_elapsed)'
init : Command 'exec - system log -- /system/bin/bootstat --record_boot_complete' action=sys.boot_completed=1 && sys.logbootcomplete=1 (/system/etc/init/bootstat.rc:
init : Command 'exec - system log -- /system/bin/bootstat --record_boot_reason' action=sys.boot_completed=1 && sys.logbootcomplete=1 (/system/etc/init/bootstat.rc:
init : Command 'exec - system log -- /system/bin/bootstat --record_time_since_factory_reset' action=sys.boot_completed=1 && sys.logbootcomplete=1 (/system/etc/init/bootstat.rc:
+init : Command 'exec_background - system log -- /system/bin/bootstat --set_system_boot_reason --record_boot_complete --record_boot_reason --record_time_since_factory_reset -l' action=sys.boot_completed=1 && sys.logbootcomplete=1 (/system/etc/init/bootstat.rc
(/system/bin/bootstat --record_boot_complete)'...
(/system/bin/bootstat --record_boot_complete)' (pid${SPACE}
(/system/bin/bootstat --record_boot_reason)'...
@@ -377,6 +424,9 @@
echo "${GREEN}[ OK ]${NORMAL} ${TEST} ${*}"
else
echo "${RED}[ FAILED ]${NORMAL} ${TEST} ${*}"
+ if ${STOP_ON_FAILURE}; then
+ exit ${save_ret}
+ fi
fi
return ${save_ret}
}
@@ -407,29 +457,35 @@
tr ' \f\t\r\n' '_____'`
case ${var} in
watchdog | watchdog,?* ) ;;
- kernel_panic | kernel_panic,?*) ;;
- recovery | recovery,?*) ;;
- bootloader | bootloader,?*) ;;
- cold | cold,?*) ;;
- hard | hard,?*) ;;
- warm | warm,?*) ;;
- shutdown | shutdown,?*) ;;
+ kernel_panic | kernel_panic,?* ) ;;
+ recovery | recovery,?* ) ;;
+ bootloader | bootloader,?* ) ;;
+ cold | cold,?* ) ;;
+ hard | hard,?* ) ;;
+ warm | warm,?* ) ;;
+ shutdown | shutdown,?* ) ;;
reboot,reboot | reboot,reboot,* ) var=${var#reboot,} ; var=${var%,} ;;
reboot,cold | reboot,cold,* ) var=${var#reboot,} ; var=${var%,} ;;
reboot,hard | reboot,hard,* ) var=${var#reboot,} ; var=${var%,} ;;
reboot,warm | reboot,warm,* ) var=${var#reboot,} ; var=${var%,} ;;
reboot,recovery | reboot,recovery,* ) var=${var#reboot,} ; var=${var%,} ;;
reboot,bootloader | reboot,bootloader,* ) var=${var#reboot,} ; var=${var%,} ;;
- reboot | reboot,?*) ;;
+ reboot | reboot,?* ) ;;
# Aliases and Heuristics
- *wdog* | *watchdog* ) var="watchdog" ;;
- *powerkey* ) var="cold,powerkey" ;;
- *panic* | *kernel_panic*) var="kernel_panic" ;;
- *thermal*) var="shutdown,thermal" ;;
- *s3_wakeup*) var="warm,s3_wakeup" ;;
- *hw_reset*) var="hard,hw_reset" ;;
- *bootloader*) var="bootloader" ;;
- *) var="reboot" ;;
+ *wdog* | *watchdog* ) var="watchdog" ;;
+ *powerkey* | *power_key* | *PowerKey* ) var="cold,powerkey" ;;
+ *panic* | *kernel_panic* ) var="kernel_panic" ;;
+ *thermal* ) var="shutdown,thermal" ;;
+ *s3_wakeup* ) var="warm,s3_wakeup" ;;
+ *hw_reset* ) var="hard,hw_reset" ;;
+ *usb* ) var="cold,charger" ;;
+ *rtc* ) var="cold,rtc" ;;
+ *2sec_reboot* ) var="cold,rtc,2sec" ;;
+ *wdt_by_pass_pwk* ) var="warm" ;;
+ wdt ) var="reboot" ;;
+ *tool_by_pass_pwk* ) var="reboot,tool" ;;
+ *bootloader* ) var="bootloader" ;;
+ * ) var="reboot" ;;
esac
echo ${var}
}
@@ -441,7 +497,7 @@
NB: must also roughly match heuristics in system/core/bootstat/bootstat.cpp" ]
validate_property() {
- val="`adb shell getprop ${1} 2>&1`"
+ val=`get_property ${1}`
ret=`validate_reason "${val}"`
if [ "reboot" = "${ret}" ]; then
ret=`validate_reason "reboot,${val}"`
@@ -449,6 +505,17 @@
echo ${ret}
}
+[ "USAGE: check_boilerblate_properties
+
+Check for common property values" ]
+check_boilerplate_properties() {
+ EXPECT_PROPERTY persist.sys.boot.reason ""
+ save_ret=${?}
+ reason=`validate_property sys.boot.reason`
+ ( exit ${save_ret} ) # because one can not just do ?=${save_ret}
+ EXPECT_PROPERTY persist.sys.boot.reason.history "${reason},[1-9][0-9]*\(\|[^0-9].*\)"
+}
+
#
# Actual test frames
#
@@ -459,18 +526,20 @@
- (wait until screen is up, boot has completed)
- adb shell getprop ro.boot.bootreason (bootloader reason)
- adb shell getprop persist.sys.boot.reason (last reason)
+- adb shell getprop sys.boot.reason.last (last last reason)
- adb shell getprop sys.boot.reason (system reason)
- NB: all should have a value that is compliant with our known set." ]
test_properties() {
duration_test 1
wait_for_screen
retval=0
- check_set="ro.boot.bootreason persist.sys.boot.reason sys.boot.reason"
+ # sys.boot.reason is last for a reason
+ check_set="ro.boot.bootreason sys.boot.reason.last sys.boot.reason"
bootloader=""
# NB: this test could fail if performed _after_ optional_factory_reset test
# and will report
# ERROR: expected "reboot" got ""
- # for Android property persist.sys.boot.reason
+ # for Android property sys.boot.reason.last
# following is mitigation for the persist.sys.boot.reason, skip it
if [ "reboot,factory_reset" = "`validate_property ro.boot_bootreason`" ]; then
check_set="ro.boot.bootreason sys.boot.reason"
@@ -480,7 +549,7 @@
reason=`validate_property ${prop}`
EXPECT_PROPERTY ${prop} ${reason} || retval=${?}
done
- # sys.boot.reason is last for a reason
+ check_boilerplate_properties || retval=${?}
report_bootstat_logs ${reason} ${bootloader}
return ${retval}
}
@@ -533,7 +602,8 @@
popd >&2
wait_for_screen
EXPECT_PROPERTY sys.boot.reason "\(reboot,ota\|bootloader\)"
- EXPECT_PROPERTY persist.sys.boot.reason bootloader
+ EXPECT_PROPERTY sys.boot.reason.last bootloader
+ check_boilerplate_properties
report_bootstat_logs reboot,ota bootloader
}
@@ -547,7 +617,8 @@
adb reboot ota
wait_for_screen
EXPECT_PROPERTY sys.boot.reason reboot,ota
- EXPECT_PROPERTY persist.sys.boot.reason reboot,ota
+ EXPECT_PROPERTY sys.boot.reason.last reboot,ota
+ check_boilerplate_properties
report_bootstat_logs reboot,ota
}
@@ -566,15 +637,21 @@
duration_test
case ${TEST} in
bootloader | recovery | cold | hard | warm ) reason=${TEST} ;;
- *) reason=reboot,${TEST} ;;
+ *) reason=reboot,${TEST#optional_} ;;
esac
- adb reboot ${TEST}
+ adb reboot ${TEST#optional_}
wait_for_screen
bootloader_reason=`validate_property ro.boot.bootreason`
EXPECT_PROPERTY ro.boot.bootreason ${bootloader_reason}
- EXPECT_PROPERTY sys.boot.reason ${reason}
- EXPECT_PROPERTY persist.sys.boot.reason ${reason}
- report_bootstat_logs ${reason}
+ # to make sys.boot.reason report user friendly
+ reasons=${reason}
+ if [ "${bootloader_reason}" != "${reason}" -a -n "${bootloader_reason}" ]; then
+ reasons="\(${reason}\|${bootloader_reason}\)"
+ fi
+ EXPECT_PROPERTY sys.boot.reason ${reasons}
+ EXPECT_PROPERTY sys.boot.reason.last ${reason}
+ check_boilerplate_properties
+ report_bootstat_logs ${reason} ${bootloader_reason}
}
[ "USAGE: test_cold
@@ -606,7 +683,8 @@
adb reboot >&2
wait_for_screen
EXPECT_PROPERTY sys.boot.reason reboot,factory_reset
- EXPECT_PROPERTY persist.sys.boot.reason "reboot,.*"
+ EXPECT_PROPERTY sys.boot.reason.last "reboot,.*"
+ check_boilerplate_properties
report_bootstat_logs reboot,factory_reset reboot, reboot,adb \
"-bootstat: Failed to read /data/misc/bootstat/build_date: No such file or directory" \
"-bootstat: Failed to parse boot time record: /data/misc/bootstat/build_date"
@@ -637,7 +715,8 @@
wait_for_screen
( exit ${save_ret} ) # because one can not just do ?=${save_ret}
EXPECT_PROPERTY sys.boot.reason reboot,factory_reset
- EXPECT_PROPERTY persist.sys.boot.reason ""
+ EXPECT_PROPERTY sys.boot.reason.last ""
+ check_boilerplate_properties
report_bootstat_logs reboot,factory_reset bootloader \
"-bootstat: Failed to read /data/misc/bootstat/last_boot_time_utc: No such file or directory" \
"-bootstat: Failed to parse boot time record: /data/misc/bootstat/last_boot_time_utc" \
@@ -710,7 +789,8 @@
)
EXPECT_PROPERTY sys.boot.reason shutdown,battery
- EXPECT_PROPERTY persist.sys.boot.reason cold
+ EXPECT_PROPERTY sys.boot.reason.last cold
+ check_boilerplate_properties
report_bootstat_logs shutdown,battery "-bootstat: Battery level at shutdown 2%"
exitPstore
}
@@ -731,7 +811,8 @@
echo -n "WARNING: Please power device back up, waiting ... " >&2
wait_for_screen -n >&2
EXPECT_PROPERTY sys.boot.reason shutdown,battery
- EXPECT_PROPERTY persist.sys.boot.reason shutdown,battery
+ EXPECT_PROPERTY sys.boot.reason.last shutdown,battery
+ check_boilerplate_properties
report_bootstat_logs shutdown,battery
}
@@ -751,7 +832,8 @@
echo -n "WARNING: Please power device back up, waiting ... " >&2
wait_for_screen -n >&2
EXPECT_PROPERTY sys.boot.reason shutdown,thermal,battery
- EXPECT_PROPERTY persist.sys.boot.reason shutdown,thermal,battery
+ EXPECT_PROPERTY sys.boot.reason.last shutdown,thermal,battery
+ check_boilerplate_properties
report_bootstat_logs shutdown,thermal,battery
}
@@ -787,11 +869,71 @@
echo c | adb shell su root tee /proc/sysrq-trigger >/dev/null
wait_for_screen
EXPECT_PROPERTY sys.boot.reason ${panic_msg}
- EXPECT_PROPERTY persist.sys.boot.reason ${panic_msg}
+ EXPECT_PROPERTY sys.boot.reason.last ${panic_msg}
+ check_boilerplate_properties
report_bootstat_logs kernel_panic,sysrq
exitPstore
}
+[ "USAGE: test_kernel_panic_subreason
+
+kernel_panic_subreason test:
+- echo SysRq : Trigger a crash : 'test' | adb shell su root tee /dev/kmsg
+- echo c | adb shell su root tee /proc/sysrq-trigger
+- (wait until screen is up, boot has completed)
+- adb shell getprop sys.boot.reason
+- NB: should report kernel_panic,sysrq,test" ]
+test_kernel_panic_subreason() {
+ checkDebugBuild || return
+ duration_test ">90"
+ panic_msg="kernel_panic,sysrq,test"
+ enterPstore
+ if [ ${?} != 0 ]; then
+ echo " or functional bootloader" >&2
+ panic_msg="\(kernel_panic,sysrq,test\|kernel_panic\)"
+ pstore_ok=true
+ fi
+ echo "SysRq : Trigger a crash : 'test'" | adb shell su root tee /dev/kmsg
+ echo c | adb shell su root tee /proc/sysrq-trigger >/dev/null
+ wait_for_screen
+ EXPECT_PROPERTY sys.boot.reason ${panic_msg}
+ EXPECT_PROPERTY sys.boot.reason.last ${panic_msg}
+ check_boilerplate_properties
+ report_bootstat_logs kernel_panic,sysrq,test \
+ "-bootstat: Unknown boot reason: kernel_panic,sysrq,test"
+ exitPstore
+}
+
+[ "USAGE: test_kernel_panic_hung
+
+kernel_panic_hung test:
+- echo Kernel panic - not synching: hung_task: blocked tasks |
+ adb shell su root tee /dev/kmsg
+- adb reboot warm
+- (wait until screen is up, boot has completed)
+- adb shell getprop sys.boot.reason
+- NB: should report kernel_panic,hung" ]
+test_kernel_panic_hung() {
+ checkDebugBuild || return
+ duration_test
+ panic_msg="kernel_panic,hung"
+ enterPstore
+ if [ ${?} != 0 ]; then
+ echo " or functional bootloader" >&2
+ panic_msg="\(kernel_panic,hung\|reboot,hung\)"
+ pstore_ok=true
+ fi
+ echo "Kernel panic - not syncing: hung_task: blocked tasks" |
+ adb shell su root tee /dev/kmsg
+ adb reboot warm
+ wait_for_screen
+ EXPECT_PROPERTY sys.boot.reason ${panic_msg}
+ EXPECT_PROPERTY sys.boot.reason.last ${panic_msg}
+ check_boilerplate_properties
+ report_bootstat_logs kernel_panic,hung
+ exitPstore
+}
+
[ "USAGE: test_warm
warm test
@@ -819,7 +961,8 @@
echo -n "WARNING: Please power device back up, waiting ... " >&2
wait_for_screen -n >&2
EXPECT_PROPERTY sys.boot.reason shutdown,thermal
- EXPECT_PROPERTY persist.sys.boot.reason shutdown,thermal
+ EXPECT_PROPERTY sys.boot.reason.last shutdown,thermal
+ check_boilerplate_properties
report_bootstat_logs shutdown,thermal
}
@@ -839,7 +982,8 @@
echo -n "WARNING: Please power device back up, waiting ... " >&2
wait_for_screen -n >&2
EXPECT_PROPERTY sys.boot.reason shutdown,userrequested
- EXPECT_PROPERTY persist.sys.boot.reason shutdown,userrequested
+ EXPECT_PROPERTY sys.boot.reason.last shutdown,userrequested
+ check_boilerplate_properties
report_bootstat_logs shutdown,userrequested
}
@@ -855,7 +999,8 @@
adb shell reboot
wait_for_screen
EXPECT_PROPERTY sys.boot.reason reboot,shell
- EXPECT_PROPERTY persist.sys.boot.reason reboot,shell
+ EXPECT_PROPERTY sys.boot.reason.last reboot,shell
+ check_boilerplate_properties
report_bootstat_logs reboot,shell
}
@@ -871,10 +1016,25 @@
adb reboot
wait_for_screen
EXPECT_PROPERTY sys.boot.reason reboot,adb
- EXPECT_PROPERTY persist.sys.boot.reason reboot,adb
+ EXPECT_PROPERTY sys.boot.reason.last reboot,adb
+ check_boilerplate_properties
report_bootstat_logs reboot,adb
}
+[ "USAGE: test_rescueparty
+
+rescueparty test
+- adb reboot rescueparty
+- (wait until screen is up, boot has completed)
+- adb shell getprop sys.boot.reason
+- adb shell getprop ro.boot.bootreason
+- NB: should report reboot,rescueparty" ]
+test_optional_rescueparty() {
+ blind_reboot_test
+ echo "WARNING: legacy devices are allowed to fail following ro.boot.bootreason result" >&2
+ EXPECT_PROPERTY ro.boot.bootreason reboot,rescueparty
+}
+
[ "USAGE: test_Its_Just_So_Hard_reboot
Its Just So Hard reboot test:
@@ -892,23 +1052,8 @@
adb shell 'reboot "Its Just So Hard"'
wait_for_screen
EXPECT_PROPERTY sys.boot.reason reboot,its_just_so_hard
- EXPECT_PROPERTY persist.sys.boot.reason "reboot,Its Just So Hard"
- # Do not leave this test with an illegal value in persist.sys.boot.reason
- save_ret=${?} # hold on to error code from above two lines
- if isDebuggable; then # can do this easy, or we can do this hard.
- adb shell su root setprop persist.sys.boot.reason reboot,its_just_so_hard
- ( exit ${save_ret} ) # because one can not just do ?=${save_ret}
- else
- report_bootstat_logs reboot,its_just_so_hard # report what we have so far
- # user build mitigation
- adb shell reboot its_just_so_hard
- wait_for_screen
- ( exit ${save_ret} ) # because one can not just do ?=${save_ret}
- EXPECT_PROPERTY sys.boot.reason reboot,its_just_so_hard
- fi
- # Ensure persist.sys.boot.reason now valid, failure here acts as a signal
- # that we could choke up following tests. For example test_properties.
- EXPECT_PROPERTY persist.sys.boot.reason reboot,its_just_so_hard ${flag}
+ EXPECT_PROPERTY sys.boot.reason.last reboot,its_just_so_hard
+ check_boilerplate_properties
report_bootstat_logs reboot,its_just_so_hard
}
@@ -957,7 +1102,8 @@
# Check values
EXPECT_PROPERTY ro.boot.bootreason "${bootloader_expected}"
EXPECT_PROPERTY sys.boot.reason "${sys_expected}"
- EXPECT_PROPERTY persist.sys.boot.reason "${last_expected}"
+ EXPECT_PROPERTY sys.boot.reason.last "${last_expected}"
+ check_boilerplate_properties
report_bootstat_logs "${sys_expected}"
}
@@ -1011,88 +1157,98 @@
shift 2
fi
-if [ X"--help" = X"${1}" -o X"-h" = X"${1}" -o X"-?" = X"${1}" ]; then
- echo "USAGE: ${0##*/} [-s SERIAL] [tests]"
- echo tests - `sed -n 's/^test_\([^ ()]*\)() {/\1/p' $0 </dev/null`
- exit 0
-fi
+# Helpful for debugging, allows us to import the functions.
+if [ X"--macros" != X"${1}" ]; then
-# Check if all conditions for the script are sane
+ if [ X"--help" = X"${1}" -o X"-h" = X"${1}" -o X"-?" = X"${1}" ]; then
+ echo "USAGE: ${0##*/} [-s SERIAL] [tests]"
+ echo tests - `sed -n 's/^test_\([^ ()]*\)() {/\1/p' $0 </dev/null`
+ exit 0
+ fi
-if [ -z "${ANDROID_SERIAL}" ]; then
- ndev=`(
- adb devices | grep -v 'List of devices attached'
- fastboot devices
- ) |
- grep -v "^[${SPACE}${TAB}]*\$" |
- wc -l`
- if [ ${ndev} -gt 1 ]; then
- echo "ERROR: no target device specified, ${ndev} connected" >&2
- echo "${RED}[ FAILED ]${NORMAL}"
- exit 1
+ if [ X"--stop" = X"${1}" ]; then
+ STOP_ON_FAILURE=true
+ shift
fi
- echo "WARNING: no target device specified" >&2
-fi
-ret=0
+ # Check if all conditions for the script are sane
-# Test Series
-if [ X"all" = X"${*}" ]; then
- # automagically pick up all test_<function>s.
- eval set nothing `sed -n 's/^test_\([^ ()]*\)() {/\1/p' $0 </dev/null`
- if [ X"nothing" = X"${1}" ]; then
- shift 1
- fi
-fi
-if [ -z "$*" ]; then
- # automagically pick up all test_<function>, except test_optional_<function>.
- eval set nothing `sed -n 's/^test_\([^ ()]*\)() {/\1/p' $0 </dev/null |
- grep -v '^optional_'`
- if [ -z "${2}" ]; then
- # Hard coded should shell fail to find them above (search/permission issues)
- eval set properties ota cold factory_reset hard battery unknown \
- kernel_panic warm thermal_shutdown userrequested_shutdown \
- shell_reboot adb_reboot Its_Just_So_Hard_reboot \
- bootloader_normal bootloader_watchdog bootloader_kernel_panic \
- bootloader_oem_powerkey bootloader_wdog_reset \
- bootloader_wdog_reset bootloader_wdog_reset bootloader_hard \
- bootloader_recovery
- fi
- if [ X"nothing" = X"${1}" ]; then
- shift 1
- fi
-fi
-echo "INFO: selected test(s): ${@}" >&2
-echo
-# Prepare device
-setBootloaderBootReason 2>/dev/null
-# Start pouring through the tests.
-failures=
-successes=
-for t in "${@}"; do
- wrap_test ${t}
- retval=${?}
- if [ 0 = ${retval} ]; then
- if [ -z "${successes}" ]; then
- successes=${t}
- else
- successes="${successes} ${t}"
+ if [ -z "${ANDROID_SERIAL}" ]; then
+ ndev=`(
+ adb devices | grep -v 'List of devices attached'
+ fastboot devices
+ ) |
+ grep -v "^[${SPACE}${TAB}]*\$" |
+ wc -l`
+ if [ ${ndev} -gt 1 ]; then
+ echo "ERROR: no target device specified, ${ndev} connected" >&2
+ echo "${RED}[ FAILED ]${NORMAL}"
+ exit 1
fi
- else
- ret=${retval}
- if [ -z "${failures}" ]; then
- failures=${t}
- else
- failures="${failures} ${t}"
+ echo "WARNING: no target device specified" >&2
+ fi
+
+ ret=0
+
+ # Test Series
+ if [ X"all" = X"${*}" ]; then
+ # automagically pick up all test_<function>s.
+ eval set nothing `sed -n 's/^test_\([^ ()]*\)() {/\1/p' $0 </dev/null`
+ if [ X"nothing" = X"${1}" ]; then
+ shift 1
fi
fi
+ if [ -z "$*" ]; then
+ # automagically pick up all test_<function>, except test_optional_<function>.
+ eval set nothing `sed -n 's/^test_\([^ ()]*\)() {/\1/p' $0 </dev/null |
+ grep -v '^optional_'`
+ if [ -z "${2}" ]; then
+ # Hard coded should shell fail to find them (search/permission issues)
+ eval set properties ota cold factory_reset hard battery unknown \
+ kernel_panic kernel_panic_subreason kernel_panic_hung warm \
+ thermal_shutdown userrequested_shutdown shell_reboot adb_reboot \
+ Its_Just_So_Hard_reboot bootloader_normal bootloader_watchdog \
+ bootloader_kernel_panic bootloader_oem_powerkey \
+ bootloader_wdog_reset bootloader_cold bootloader_warm \
+ bootloader_hard bootloader_recovery
+ fi
+ if [ X"nothing" = X"${1}" ]; then
+ shift 1
+ fi
+ fi
+ echo "INFO: selected test(s): ${@}" >&2
echo
-done
+ # Prepare device
+ setBootloaderBootReason 2>/dev/null
+ # Start pouring through the tests.
+ failures=
+ successes=
+ for t in "${@}"; do
+ wrap_test ${t}
+ retval=${?}
+ if [ 0 = ${retval} ]; then
+ if [ -z "${successes}" ]; then
+ successes=${t}
+ else
+ successes="${successes} ${t}"
+ fi
+ else
+ ret=${retval}
+ if [ -z "${failures}" ]; then
+ failures=${t}
+ else
+ failures="${failures} ${t}"
+ fi
+ fi
+ echo
+ done
-if [ -n "${successes}" ]; then
- echo "${GREEN}[ PASSED ]${NORMAL} ${successes}"
+ if [ -n "${successes}" ]; then
+ echo "${GREEN}[ PASSED ]${NORMAL} ${successes}"
+ fi
+ if [ -n "${failures}" ]; then
+ echo "${RED}[ FAILED ]${NORMAL} ${failures}"
+ fi
+ exit ${ret}
+
fi
-if [ -n "${failures}" ]; then
- echo "${RED}[ FAILED ]${NORMAL} ${failures}"
-fi
-exit ${ret}
diff --git a/bootstat/bootstat.cpp b/bootstat/bootstat.cpp
index a1fe6ed..c17e00f 100644
--- a/bootstat/bootstat.cpp
+++ b/bootstat/bootstat.cpp
@@ -27,21 +27,25 @@
#include <cstddef>
#include <cstdio>
#include <ctime>
+#include <iterator>
#include <map>
#include <memory>
+#include <regex>
#include <string>
+#include <utility>
#include <vector>
#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
+#include <android-base/properties.h>
#include <android-base/strings.h>
#include <android/log.h>
#include <cutils/android_reboot.h>
#include <cutils/properties.h>
-#include <log/logcat.h>
#include <metricslogger/metrics_logger.h>
+#include <statslog.h>
#include "boot_event_record_store.h"
@@ -121,12 +125,12 @@
return std::string(&temp[0], len);
}
-void SetProperty(const char* key, const std::string& val) {
- property_set(key, val.c_str());
+bool SetProperty(const char* key, const std::string& val) {
+ return property_set(key, val.c_str()) == 0;
}
-void SetProperty(const char* key, const char* val) {
- property_set(key, val);
+bool SetProperty(const char* key, const char* val) {
+ return property_set(key, val) == 0;
}
constexpr int32_t kEmptyBootReason = 0;
@@ -138,7 +142,7 @@
// values.
const std::map<std::string, int32_t> kBootReasonMap = {
{"empty", kEmptyBootReason},
- {"unknown", kUnknownBootReason},
+ {"__BOOTSTAT_UNKNOWN__", kUnknownBootReason},
{"normal", 2},
{"recovery", 3},
{"reboot", 4},
@@ -182,7 +186,7 @@
{"wdog_bark", 42},
{"wdog_bite", 43},
{"wdog_reset", 44},
- {"shutdown,", 45}, // Trailing comma is intentional.
+ {"shutdown,", 45}, // Trailing comma is intentional. Do NOT use.
{"shutdown,userrequested", 46},
{"reboot,bootloader", 47},
{"reboot,cold", 48},
@@ -191,12 +195,14 @@
{"s3_wakeup", 51},
{"kernel_panic,sysrq", 52},
{"kernel_panic,NULL", 53},
+ {"kernel_panic,null", 53},
{"kernel_panic,BUG", 54},
+ {"kernel_panic,bug", 54},
{"bootloader", 55},
{"cold", 56},
{"hard", 57},
{"warm", 58},
- {"recovery", 59},
+ {"reboot,kernel_power_off_charging__reboot_system", 59}, // Can not happen
{"thermal-shutdown", 60},
{"shutdown,thermal", 61},
{"shutdown,battery", 62},
@@ -223,11 +229,11 @@
{"2sec_reboot", 83},
{"reboot,by_key", 84},
{"reboot,longkey", 85},
- {"reboot,2sec", 86},
+ {"reboot,2sec", 86}, // Deprecate in two years, replaced with cold,rtc,2sec
{"shutdown,thermal,battery", 87},
{"reboot,its_just_so_hard", 88}, // produced by boot_reason_test
{"reboot,Its Just So Hard", 89}, // produced by boot_reason_test
- {"usb", 90},
+ {"reboot,rescueparty", 90},
{"charge", 91},
{"oem_tz_crash", 92},
{"uvlo", 93},
@@ -256,8 +262,8 @@
{"oem_rpm_undef_error", 116},
{"oem_crash_on_the_lk", 117},
{"oem_rpm_reset", 118},
- {"oem_lpass_cfg", 119},
- {"oem_xpu_ns_error", 120},
+ {"REUSE1", 119}, // Former dupe, can be re-used
+ {"REUSE2", 120}, // Former dupe, can be re-used
{"factory_cable", 121},
{"oem_ar6320_failed_to_powerup", 122},
{"watchdog_rpm_bite", 123},
@@ -285,6 +291,27 @@
{"oem_sdi_err_fatal", 145},
{"pmic_watchdog", 146},
{"software_master", 147},
+ {"cold,charger", 148},
+ {"cold,rtc", 149},
+ {"cold,rtc,2sec", 150},
+ {"reboot,tool", 151},
+ {"reboot,wdt", 152},
+ {"reboot,unknown", 153},
+ {"kernel_panic,audit", 154},
+ {"kernel_panic,atomic", 155},
+ {"kernel_panic,hung", 156},
+ {"kernel_panic,hung,rcu", 157},
+ {"kernel_panic,init", 158},
+ {"kernel_panic,oom", 159},
+ {"kernel_panic,stack", 160},
+ {"kernel_panic,sysrq,livelock,alarm", 161}, // llkd
+ {"kernel_panic,sysrq,livelock,driver", 162}, // llkd
+ {"kernel_panic,sysrq,livelock,zombie", 163}, // llkd
+ {"kernel_panic,modem", 164},
+ {"kernel_panic,adsp", 165},
+ {"kernel_panic,dsps", 166},
+ {"kernel_panic,wcnss", 167},
+ {"kernel_panic,_sde_encoder_phys_cmd_handle_ppdone_timeout", 168},
};
// Converts a string value representing the reason the system booted to an
@@ -461,11 +488,13 @@
}
return std::string::npos;
}
+
+ operator const std::string&() const { return console; }
};
// If bit error match to needle, correct it.
// Return true if any corrections were discovered and applied.
-bool correctForBer(std::string& reason, const std::string& needle) {
+bool correctForBitError(std::string& reason, const std::string& needle) {
bool corrected = false;
if (reason.length() < needle.length()) return corrected;
const pstoreConsole console(reason);
@@ -483,20 +512,219 @@
return corrected;
}
+// If bit error match to needle, correct it.
+// Return true if any corrections were discovered and applied.
+// Try again if we can replace underline with spaces.
+bool correctForBitErrorOrUnderline(std::string& reason, const std::string& needle) {
+ bool corrected = correctForBitError(reason, needle);
+ std::string _needle(needle);
+ std::transform(_needle.begin(), _needle.end(), _needle.begin(),
+ [](char c) { return (c == '_') ? ' ' : c; });
+ if (needle != _needle) {
+ corrected |= correctForBitError(reason, _needle);
+ }
+ return corrected;
+}
+
+// Converts a string value representing the reason the system booted to a
+// string complying with Android system standard reason.
+void transformReason(std::string& reason) {
+ std::transform(reason.begin(), reason.end(), reason.begin(), ::tolower);
+ std::transform(reason.begin(), reason.end(), reason.begin(),
+ [](char c) { return ::isblank(c) ? '_' : c; });
+ std::transform(reason.begin(), reason.end(), reason.begin(),
+ [](char c) { return ::isprint(c) ? c : '?'; });
+}
+
+// Check subreasons for reboot,<subreason> kernel_panic,sysrq,<subreason> or
+// kernel_panic,<subreason>.
+//
+// If quoted flag is set, pull out and correct single quoted ('), newline (\n)
+// or unprintable character terminated subreason, pos is supplied just beyond
+// first quote. if quoted false, pull out and correct newline (\n) or
+// unprintable character terminated subreason.
+//
+// Heuristics to find termination is painted into a corner:
+
+// single bit error for quote ' that we can block. It is acceptable for
+// the others 7, g in reason. 2/9 chance will miss the terminating quote,
+// but there is always the terminating newline that usually immediately
+// follows to fortify our chances.
+bool likely_single_quote(char c) {
+ switch (static_cast<uint8_t>(c)) {
+ case '\'': // '\''
+ case '\'' ^ 0x01: // '&'
+ case '\'' ^ 0x02: // '%'
+ case '\'' ^ 0x04: // '#'
+ case '\'' ^ 0x08: // '/'
+ return true;
+ case '\'' ^ 0x10: // '7'
+ break;
+ case '\'' ^ 0x20: // '\a' (unprintable)
+ return true;
+ case '\'' ^ 0x40: // 'g'
+ break;
+ case '\'' ^ 0x80: // 0xA7 (unprintable)
+ return true;
+ }
+ return false;
+}
+
+// ::isprint(c) and likely_space() will prevent us from being called for
+// fundamentally printable entries, except for '\r' and '\b'.
+//
+// Except for * and J, single bit errors for \n, all others are non-
+// printable so easy catch. It is _acceptable_ for *, J or j to exist in
+// the reason string, so 2/9 chance we will miss the terminating newline.
+//
+// NB: J might not be acceptable, except if at the beginning or preceded
+// with a space, '(' or any of the quotes and their BER aliases.
+// NB: * might not be acceptable, except if at the beginning or preceded
+// with a space, another *, or any of the quotes or their BER aliases.
+//
+// To reduce the chances to closer to 1/9 is too complicated for the gain.
+bool likely_newline(char c) {
+ switch (static_cast<uint8_t>(c)) {
+ case '\n': // '\n' (unprintable)
+ case '\n' ^ 0x01: // '\r' (unprintable)
+ case '\n' ^ 0x02: // '\b' (unprintable)
+ case '\n' ^ 0x04: // 0x0E (unprintable)
+ case '\n' ^ 0x08: // 0x02 (unprintable)
+ case '\n' ^ 0x10: // 0x1A (unprintable)
+ return true;
+ case '\n' ^ 0x20: // '*'
+ case '\n' ^ 0x40: // 'J'
+ break;
+ case '\n' ^ 0x80: // 0x8A (unprintable)
+ return true;
+ }
+ return false;
+}
+
+// ::isprint(c) will prevent us from being called for all the printable
+// matches below. If we let unprintables through because of this, they
+// get converted to underscore (_) by the validation phase.
+bool likely_space(char c) {
+ switch (static_cast<uint8_t>(c)) {
+ case ' ': // ' '
+ case ' ' ^ 0x01: // '!'
+ case ' ' ^ 0x02: // '"'
+ case ' ' ^ 0x04: // '$'
+ case ' ' ^ 0x08: // '('
+ case ' ' ^ 0x10: // '0'
+ case ' ' ^ 0x20: // '\0' (unprintable)
+ case ' ' ^ 0x40: // 'P'
+ case ' ' ^ 0x80: // 0xA0 (unprintable)
+ case '\t': // '\t'
+ case '\t' ^ 0x01: // '\b' (unprintable) (likely_newline counters)
+ case '\t' ^ 0x02: // '\v' (unprintable)
+ case '\t' ^ 0x04: // '\r' (unprintable) (likely_newline counters)
+ case '\t' ^ 0x08: // 0x01 (unprintable)
+ case '\t' ^ 0x10: // 0x19 (unprintable)
+ case '\t' ^ 0x20: // ')'
+ case '\t' ^ 0x40: // '1'
+ case '\t' ^ 0x80: // 0x89 (unprintable)
+ return true;
+ }
+ return false;
+}
+
+std::string getSubreason(const std::string& content, size_t pos, bool quoted) {
+ static constexpr size_t max_reason_length = 256;
+
+ std::string subReason(content.substr(pos, max_reason_length));
+ // Correct against any known strings that Bit Error Match
+ for (const auto& s : knownReasons) {
+ correctForBitErrorOrUnderline(subReason, s);
+ }
+ std::string terminator(quoted ? "'" : "");
+ for (const auto& m : kBootReasonMap) {
+ if (m.first.length() <= strlen("cold")) continue; // too short?
+ if (correctForBitErrorOrUnderline(subReason, m.first + terminator)) continue;
+ if (m.first.length() <= strlen("reboot,cold")) continue; // short?
+ if (android::base::StartsWith(m.first, "reboot,")) {
+ correctForBitErrorOrUnderline(subReason, m.first.substr(strlen("reboot,")) + terminator);
+ } else if (android::base::StartsWith(m.first, "kernel_panic,sysrq,")) {
+ correctForBitErrorOrUnderline(subReason,
+ m.first.substr(strlen("kernel_panic,sysrq,")) + terminator);
+ } else if (android::base::StartsWith(m.first, "kernel_panic,")) {
+ correctForBitErrorOrUnderline(subReason, m.first.substr(strlen("kernel_panic,")) + terminator);
+ }
+ }
+ for (pos = 0; pos < subReason.length(); ++pos) {
+ char c = subReason[pos];
+ if (!(::isprint(c) || likely_space(c)) || likely_newline(c) ||
+ (quoted && likely_single_quote(c))) {
+ subReason.erase(pos);
+ break;
+ }
+ }
+ transformReason(subReason);
+ return subReason;
+}
+
bool addKernelPanicSubReason(const pstoreConsole& console, std::string& ret) {
// Check for kernel panic types to refine information
- if (console.rfind("SysRq : Trigger a crash") != std::string::npos) {
- // Can not happen, except on userdebug, during testing/debugging.
+ if ((console.rfind("SysRq : Trigger a crash") != std::string::npos) ||
+ (console.rfind("PC is at sysrq_handle_crash+") != std::string::npos)) {
ret = "kernel_panic,sysrq";
+ // Invented for Android to allow daemons that specifically trigger sysrq
+ // to communicate more accurate boot subreasons via last console messages.
+ static constexpr char sysrqSubreason[] = "SysRq : Trigger a crash : '";
+ auto pos = console.rfind(sysrqSubreason);
+ if (pos != std::string::npos) {
+ ret += "," + getSubreason(console, pos + strlen(sysrqSubreason), /* quoted */ true);
+ }
return true;
}
if (console.rfind("Unable to handle kernel NULL pointer dereference at virtual address") !=
std::string::npos) {
- ret = "kernel_panic,NULL";
+ ret = "kernel_panic,null";
return true;
}
if (console.rfind("Kernel BUG at ") != std::string::npos) {
- ret = "kernel_panic,BUG";
+ ret = "kernel_panic,bug";
+ return true;
+ }
+
+ std::string panic("Kernel panic - not syncing: ");
+ auto pos = console.rfind(panic);
+ if (pos != std::string::npos) {
+ static const std::vector<std::pair<const std::string, const std::string>> panicReasons = {
+ {"Out of memory", "oom"},
+ {"out of memory", "oom"},
+ {"Oh boy, that early out of memory", "oom"}, // omg
+ {"BUG!", "bug"},
+ {"hung_task: blocked tasks", "hung"},
+ {"audit: ", "audit"},
+ {"scheduling while atomic", "atomic"},
+ {"Attempted to kill init!", "init"},
+ {"Requested init", "init"},
+ {"No working init", "init"},
+ {"Could not decompress init", "init"},
+ {"RCU Stall", "hung,rcu"},
+ {"stack-protector", "stack"},
+ {"kernel stack overflow", "stack"},
+ {"Corrupt kernel stack", "stack"},
+ {"low stack detected", "stack"},
+ {"corrupted stack end", "stack"},
+ {"subsys-restart: Resetting the SoC - modem crashed.", "modem"},
+ {"subsys-restart: Resetting the SoC - adsp crashed.", "adsp"},
+ {"subsys-restart: Resetting the SoC - dsps crashed.", "dsps"},
+ {"subsys-restart: Resetting the SoC - wcnss crashed.", "wcnss"},
+ };
+
+ ret = "kernel_panic";
+ for (auto& s : panicReasons) {
+ if (console.find(panic + s.first, pos) != std::string::npos) {
+ ret += "," + s.second;
+ return true;
+ }
+ }
+ auto reason = getSubreason(console, pos + panic.length(), /* newline */ false);
+ if (reason.length() > 3) {
+ ret += "," + reason;
+ }
return true;
}
return false;
@@ -506,32 +734,54 @@
return addKernelPanicSubReason(pstoreConsole(content), ret);
}
-// std::transform Helper callback functions:
-// Converts a string value representing the reason the system booted to a
-// string complying with Android system standard reason.
-char tounderline(char c) {
- return ::isblank(c) ? '_' : c;
-}
-
-char toprintable(char c) {
- return ::isprint(c) ? c : '?';
-}
-
-// Cleanup boot_reason regarding acceptable character set
-void transformReason(std::string& reason) {
- std::transform(reason.begin(), reason.end(), reason.begin(), ::tolower);
- std::transform(reason.begin(), reason.end(), reason.begin(), tounderline);
- std::transform(reason.begin(), reason.end(), reason.begin(), toprintable);
-}
-
const char system_reboot_reason_property[] = "sys.boot.reason";
const char last_reboot_reason_property[] = LAST_REBOOT_REASON_PROPERTY;
+const char last_last_reboot_reason_property[] = "sys.boot.reason.last";
+constexpr size_t history_reboot_reason_size = 4;
+const char history_reboot_reason_property[] = LAST_REBOOT_REASON_PROPERTY ".history";
const char bootloader_reboot_reason_property[] = "ro.boot.bootreason";
+// Land system_boot_reason into system_reboot_reason_property.
+// Shift system_boot_reason into history_reboot_reason_property.
+void BootReasonAddToHistory(const std::string& system_boot_reason) {
+ if (system_boot_reason.empty()) return;
+ LOG(INFO) << "Canonical boot reason: " << system_boot_reason;
+ auto old_system_boot_reason = GetProperty(system_reboot_reason_property);
+ if (!SetProperty(system_reboot_reason_property, system_boot_reason)) {
+ SetProperty(system_reboot_reason_property, system_boot_reason.substr(0, PROPERTY_VALUE_MAX - 1));
+ }
+ auto reason_history = android::base::Split(GetProperty(history_reboot_reason_property), "\n");
+ static auto mark = time(nullptr);
+ auto mark_str = std::string(",") + std::to_string(mark);
+ auto marked_system_boot_reason = system_boot_reason + mark_str;
+ if (!reason_history.empty()) {
+ // delete any entries that we just wrote in a previous
+ // call and leveraging duplicate line handling
+ auto last = old_system_boot_reason + mark_str;
+ // trim the list to (history_reboot_reason_size - 1)
+ ssize_t max = history_reboot_reason_size;
+ for (auto it = reason_history.begin(); it != reason_history.end();) {
+ if (it->empty() || (last == *it) || (marked_system_boot_reason == *it) || (--max <= 0)) {
+ it = reason_history.erase(it);
+ } else {
+ last = *it;
+ ++it;
+ }
+ }
+ }
+ // insert at the front, concatenating mark (<epoch time>) detail to the value.
+ reason_history.insert(reason_history.begin(), marked_system_boot_reason);
+ // If the property string is too long ( > PROPERTY_VALUE_MAX)
+ // we get an error, so trim out last entry and try again.
+ while (!(SetProperty(history_reboot_reason_property, android::base::Join(reason_history, '\n')))) {
+ auto it = std::prev(reason_history.end());
+ if (it == reason_history.end()) break;
+ reason_history.erase(it);
+ }
+}
+
// Scrub, Sanitize, Standardize and Enhance the boot reason string supplied.
std::string BootReasonStrToReason(const std::string& boot_reason) {
- static const size_t max_reason_length = 256;
-
std::string ret(GetProperty(system_reboot_reason_property));
std::string reason(boot_reason);
// If sys.boot.reason == ro.boot.bootreason, let's re-evaluate
@@ -566,26 +816,41 @@
// A series of checks to take some officially unsupported reasons
// reported by the bootloader and find some logical and canonical
// sense. In an ideal world, we would require those bootloaders
- // to behave and follow our standards.
+ // to behave and follow our CTS standards.
+ //
+ // first member is the output
+ // second member is an unanchored regex for an alias
+ //
+ // If output has a prefix of <bang> '!', we do not use it as a
+ // match needle (and drop the <bang> prefix when landing in output),
+ // otherwise look for it as well. This helps keep the scale of the
+ // following table smaller.
static const std::vector<std::pair<const std::string, const std::string>> aliasReasons = {
{"watchdog", "wdog"},
- {"cold,powerkey", "powerkey"},
+ {"cold,powerkey", "powerkey|power_key|PowerKey"},
{"kernel_panic", "panic"},
{"shutdown,thermal", "thermal"},
{"warm,s3_wakeup", "s3_wakeup"},
{"hard,hw_reset", "hw_reset"},
- {"reboot,2sec", "2sec_reboot"},
+ {"cold,charger", "usb"},
+ {"cold,rtc", "rtc"},
+ {"cold,rtc,2sec", "2sec_reboot"},
+ {"!warm", "wdt_by_pass_pwk"}, // change flavour of blunt
+ {"!reboot", "^wdt$"}, // change flavour of blunt
+ {"reboot,tool", "tool_by_pass_pwk"},
+ {"!reboot,longkey", "reboot_longkey"},
+ {"!reboot,longkey", "kpdpwr"},
{"bootloader", ""},
};
- // Either the primary or alias is found _somewhere_ in the reason string.
for (auto& s : aliasReasons) {
- if (reason.find(s.first) != std::string::npos) {
+ size_t firstHasNot = s.first[0] == '!';
+ if (!firstHasNot && (reason.find(s.first) != std::string::npos)) {
ret = s.first;
break;
}
- if (s.second.size() && (reason.find(s.second) != std::string::npos)) {
- ret = s.first;
+ if (s.second.size() && std::regex_search(reason, std::regex(s.second))) {
+ ret = s.first.substr(firstHasNot);
break;
}
}
@@ -624,28 +889,7 @@
static const char cmd[] = "reboot: Restarting system with command '";
size_t pos = console.rfind(cmd);
if (pos != std::string::npos) {
- pos += strlen(cmd);
- std::string subReason(content.substr(pos, max_reason_length));
- // Correct against any known strings that Bit Error Match
- for (const auto& s : knownReasons) {
- correctForBer(subReason, s);
- }
- for (const auto& m : kBootReasonMap) {
- if (m.first.length() <= strlen("cold")) continue; // too short?
- if (correctForBer(subReason, m.first + "'")) continue;
- if (m.first.length() <= strlen("reboot,cold")) continue; // short?
- if (!android::base::StartsWith(m.first, "reboot,")) continue;
- correctForBer(subReason, m.first.substr(strlen("reboot,")) + "'");
- }
- for (pos = 0; pos < subReason.length(); ++pos) {
- char c = subReason[pos];
- // #, &, %, / are common single bit error for ' that we can block
- if (!::isprint(c) || (c == '\'') || (c == '#') || (c == '&') || (c == '%') || (c == '/')) {
- subReason.erase(pos);
- break;
- }
- }
- transformReason(subReason);
+ std::string subReason(getSubreason(content, pos + strlen(cmd), /* quoted */ true));
if (subReason != "") { // Will not land "reboot" as that is too blunt.
if (isKernelRebootReason(subReason)) {
ret = "reboot," + subReason; // User space can't talk kernel reasons.
@@ -655,6 +899,10 @@
ret = "reboot," + subReason; // legitimize unknown reasons
}
}
+ // Some bootloaders shutdown results record in last kernel message.
+ if (!strcmp(ret.c_str(), "reboot,kernel_power_off_charging__reboot_system")) {
+ ret = "shutdown";
+ }
}
// Check for kernel panics, allowed to override reboot command.
@@ -666,103 +914,7 @@
}
}
- // The following battery test should migrate to a default system health HAL
-
- // Let us not worry if the reboot command was issued, for the cases of
- // reboot -p, reboot <no reason>, reboot cold, reboot warm and reboot hard.
- // Same for bootloader and ro.boot.bootreasons of this set, but a dead
- // battery could conceivably lead to these, so worthy of override.
- if (isBluntRebootReason(ret)) {
- // Heuristic to determine if shutdown possibly because of a dead battery?
- // Really a hail-mary pass to find it in last klog content ...
- static const int battery_dead_threshold = 2; // percent
- static const char battery[] = "healthd: battery l=";
- const pstoreConsole console(content);
- size_t pos = console.rfind(battery); // last one
- std::string digits;
- if (pos != std::string::npos) {
- digits = content.substr(pos + strlen(battery), strlen("100 "));
- // correct common errors
- correctForBer(digits, "100 ");
- if (digits[0] == '!') digits[0] = '1';
- if (digits[1] == '!') digits[1] = '1';
- }
- const char* endptr = digits.c_str();
- unsigned level = 0;
- while (::isdigit(*endptr)) {
- level *= 10;
- level += *endptr++ - '0';
- // make sure no leading zeros, except zero itself, and range check.
- if ((level == 0) || (level > 100)) break;
- }
- // example bit error rate issues for 10%
- // 'l=10 ' no bits in error
- // 'l=00 ' single bit error (fails above)
- // 'l=1 ' single bit error
- // 'l=0 ' double bit error
- // There are others, not typically critical because of 2%
- // battery_dead_threshold. KISS check, make sure second
- // character after digit sequence is not a space.
- if ((level <= 100) && (endptr != digits.c_str()) && (endptr[0] == ' ') && (endptr[1] != ' ')) {
- LOG(INFO) << "Battery level at shutdown " << level << "%";
- if (level <= battery_dead_threshold) {
- ret = "shutdown,battery";
- }
- } else { // Most likely
- digits = ""; // reset digits
-
- // Content buffer no longer will have console data. Beware if more
- // checks added below, that depend on parsing console content.
- content = "";
-
- LOG(DEBUG) << "Can not find last low battery in last console messages";
- android_logcat_context ctx = create_android_logcat();
- FILE* fp = android_logcat_popen(&ctx, "logcat -b kernel -v brief -d");
- if (fp != nullptr) {
- android::base::ReadFdToString(fileno(fp), &content);
- }
- android_logcat_pclose(&ctx, fp);
- static const char logcat_battery[] = "W/healthd ( 0): battery l=";
- const char* match = logcat_battery;
-
- if (content == "") {
- // Service logd.klog not running, go to smaller buffer in the kernel.
- int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
- if (rc > 0) {
- ssize_t len = rc + 1024; // 1K Margin should it grow between calls.
- std::unique_ptr<char[]> buf(new char[len]);
- rc = klogctl(KLOG_READ_ALL, buf.get(), len);
- if (rc < len) {
- len = rc + 1;
- }
- buf[--len] = '\0';
- content = buf.get();
- }
- match = battery;
- }
-
- pos = content.find(match); // The first one it finds.
- if (pos != std::string::npos) {
- digits = content.substr(pos + strlen(match), strlen("100 "));
- }
- endptr = digits.c_str();
- level = 0;
- while (::isdigit(*endptr)) {
- level *= 10;
- level += *endptr++ - '0';
- // make sure no leading zeros, except zero itself, and range check.
- if ((level == 0) || (level > 100)) break;
- }
- if ((level <= 100) && (endptr != digits.c_str()) && (*endptr == ' ')) {
- LOG(INFO) << "Battery level at startup " << level << "%";
- if (level <= battery_dead_threshold) {
- ret = "shutdown,battery";
- }
- } else {
- LOG(DEBUG) << "Can not find first battery level in dmesg or logcat";
- }
- }
- }
+ // TODO: use the HAL to get battery level (http://b/77725702).
// Is there a controlled shutdown hint in last_reboot_reason_property?
if (isBluntRebootReason(ret)) {
@@ -799,10 +951,6 @@
}
LOG(INFO) << "Canonical boot reason: " << ret;
- if (isKernelRebootReason(ret) && (GetProperty(last_reboot_reason_property) != "")) {
- // Rewrite as it must be old news, kernel reasons trump user space.
- SetProperty(last_reboot_reason_property, ret);
- }
return ret;
}
@@ -827,13 +975,11 @@
if (!boot_event_store.GetBootEvent(kBuildDateKey, &record)) {
boot_complete_prefix = "factory_reset_" + boot_complete_prefix;
boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
- LOG(INFO) << "Canonical boot reason: reboot,factory_reset";
- SetProperty(system_reboot_reason_property, "reboot,factory_reset");
+ BootReasonAddToHistory("reboot,factory_reset");
} else if (build_date != record.second) {
boot_complete_prefix = "ota_" + boot_complete_prefix;
boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
- LOG(INFO) << "Canonical boot reason: reboot,ota";
- SetProperty(system_reboot_reason_property, "reboot,ota");
+ BootReasonAddToHistory("reboot,ota");
}
return boot_complete_prefix;
@@ -881,6 +1027,16 @@
return timings;
}
+// Returns the total bootloader boot time from the ro.boot.boottime system property.
+int32_t GetBootloaderTime(const BootloaderTimingMap& bootloader_timings) {
+ int32_t total_time = 0;
+ for (const auto& timing : bootloader_timings) {
+ total_time += timing.second;
+ }
+
+ return total_time;
+}
+
// Parses and records the set of bootloader stages and associated boot times
// from the ro.boot.boottime system property.
void RecordBootloaderTimings(BootEventRecordStore* boot_event_store,
@@ -894,11 +1050,10 @@
boot_event_store->AddBootEventWithValue("boottime.bootloader.total", total_time);
}
-// Records the closest estimation to the absolute device boot time, i.e.,
+// Returns the closest estimation to the absolute device boot time, i.e.,
// from power on to boot_complete, including bootloader times.
-void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store,
- const BootloaderTimingMap& bootloader_timings,
- std::chrono::milliseconds uptime) {
+std::chrono::milliseconds GetAbsoluteBootTime(const BootloaderTimingMap& bootloader_timings,
+ std::chrono::milliseconds uptime) {
int32_t bootloader_time_ms = 0;
for (const auto& timing : bootloader_timings) {
@@ -908,9 +1063,66 @@
}
auto bootloader_duration = std::chrono::milliseconds(bootloader_time_ms);
- auto absolute_total =
- std::chrono::duration_cast<std::chrono::seconds>(bootloader_duration + uptime);
- boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total.count());
+ return bootloader_duration + uptime;
+}
+
+// Records the closest estimation to the absolute device boot time in seconds.
+// i.e. from power on to boot_complete, including bootloader times.
+void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store,
+ std::chrono::milliseconds absolute_total) {
+ auto absolute_total_sec = std::chrono::duration_cast<std::chrono::seconds>(absolute_total);
+ boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total_sec.count());
+}
+
+// Logs the total boot time and reason to statsd.
+void LogBootInfoToStatsd(std::chrono::milliseconds end_time,
+ std::chrono::milliseconds total_duration, int32_t bootloader_duration_ms,
+ double time_since_last_boot_sec) {
+ const std::string reason(GetProperty(bootloader_reboot_reason_property));
+
+ if (reason.empty()) {
+ android::util::stats_write(android::util::BOOT_SEQUENCE_REPORTED, "<EMPTY>", "<EMPTY>",
+ end_time.count(), total_duration.count(),
+ (int64_t)bootloader_duration_ms,
+ (int64_t)time_since_last_boot_sec * 1000);
+ return;
+ }
+
+ const std::string system_reason(GetProperty(system_reboot_reason_property));
+ android::util::stats_write(android::util::BOOT_SEQUENCE_REPORTED, reason.c_str(),
+ system_reason.c_str(), end_time.count(), total_duration.count(),
+ (int64_t)bootloader_duration_ms,
+ (int64_t)time_since_last_boot_sec * 1000);
+}
+
+void SetSystemBootReason() {
+ const std::string bootloader_boot_reason(GetProperty(bootloader_reboot_reason_property));
+ const std::string system_boot_reason(BootReasonStrToReason(bootloader_boot_reason));
+ // Record the scrubbed system_boot_reason to the property
+ BootReasonAddToHistory(system_boot_reason);
+ // Shift last_reboot_reason_property to last_last_reboot_reason_property
+ std::string last_boot_reason(GetProperty(last_reboot_reason_property));
+ if (last_boot_reason.empty() || isKernelRebootReason(system_boot_reason)) {
+ last_boot_reason = system_boot_reason;
+ } else {
+ transformReason(last_boot_reason);
+ }
+ SetProperty(last_last_reboot_reason_property, last_boot_reason);
+ SetProperty(last_reboot_reason_property, "");
+}
+
+// Gets the boot time offset. This is useful when Android is running in a
+// container, because the boot_clock is not reset when Android reboots.
+std::chrono::nanoseconds GetBootTimeOffset() {
+ static const int64_t boottime_offset =
+ android::base::GetIntProperty<int64_t>("ro.boot.boottime_offset", 0);
+ return std::chrono::nanoseconds(boottime_offset);
+}
+
+// Returns the current uptime, accounting for any offset in the CLOCK_BOOTTIME
+// clock.
+android::base::boot_clock::duration GetUptime() {
+ return android::base::boot_clock::now().time_since_epoch() - GetBootTimeOffset();
}
// Records several metrics related to the time it takes to boot the device,
@@ -919,13 +1131,14 @@
BootEventRecordStore boot_event_store;
BootEventRecordStore::BootEventRecord record;
- auto time_since_epoch = android::base::boot_clock::now().time_since_epoch();
- auto uptime = std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch);
+ auto uptime_ns = GetUptime();
+ auto uptime_s = std::chrono::duration_cast<std::chrono::seconds>(uptime_ns);
time_t current_time_utc = time(nullptr);
+ time_t time_since_last_boot = 0;
if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
time_t last_boot_time_utc = record.second;
- time_t time_since_last_boot = difftime(current_time_utc, last_boot_time_utc);
+ time_since_last_boot = difftime(current_time_utc, last_boot_time_utc);
boot_event_store.AddBootEventWithValue("time_since_last_boot", time_since_last_boot);
}
@@ -945,29 +1158,38 @@
// Log the amount of time elapsed until the device is decrypted, which
// includes the variable amount of time the user takes to enter the
// decryption password.
- boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count());
+ boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime_s.count());
// Subtract the decryption time to normalize the boot cycle timing.
- std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second);
+ std::chrono::seconds boot_complete = std::chrono::seconds(uptime_s.count() - record.second);
boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
boot_complete.count());
} else {
- boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption", uptime.count());
+ boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
+ uptime_s.count());
}
// Record the total time from device startup to boot complete, regardless of
// encryption state.
- boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count());
+ boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime_s.count());
RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init");
RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux");
RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait");
const BootloaderTimingMap bootloader_timings = GetBootLoaderTimings();
+ int32_t bootloader_boot_duration = GetBootloaderTime(bootloader_timings);
RecordBootloaderTimings(&boot_event_store, bootloader_timings);
- auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch);
- RecordAbsoluteBootTime(&boot_event_store, bootloader_timings, uptime_ms);
+ auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(uptime_ns);
+ auto absolute_boot_time = GetAbsoluteBootTime(bootloader_timings, uptime_ms);
+ RecordAbsoluteBootTime(&boot_event_store, absolute_boot_time);
+
+ auto boot_end_time_point = std::chrono::system_clock::now().time_since_epoch();
+ auto boot_end_time = std::chrono::duration_cast<std::chrono::milliseconds>(boot_end_time_point);
+
+ LogBootInfoToStatsd(boot_end_time, absolute_boot_time, bootloader_boot_duration,
+ time_since_last_boot);
}
// Records the boot_reason metric by querying the ro.boot.bootreason system
@@ -991,12 +1213,10 @@
boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
// Log the scrubbed system_boot_reason.
- const std::string system_reason(BootReasonStrToReason(reason));
+ const std::string system_reason(GetProperty(system_reboot_reason_property));
int32_t system_boot_reason = BootReasonStrToEnum(system_reason);
boot_event_store.AddBootEventWithValue("system_boot_reason", system_boot_reason);
- // Record the scrubbed system_boot_reason to the property
- SetProperty(system_reboot_reason_property, system_reason);
if (reason == "") {
SetProperty(bootloader_reboot_reason_property, system_reason);
}
@@ -1062,20 +1282,22 @@
int option_index = 0;
static const char value_str[] = "value";
+ static const char system_boot_reason_str[] = "set_system_boot_reason";
static const char boot_complete_str[] = "record_boot_complete";
static const char boot_reason_str[] = "record_boot_reason";
static const char factory_reset_str[] = "record_time_since_factory_reset";
static const struct option long_options[] = {
// clang-format off
- { "help", no_argument, NULL, 'h' },
- { "log", no_argument, NULL, 'l' },
- { "print", no_argument, NULL, 'p' },
- { "record", required_argument, NULL, 'r' },
- { value_str, required_argument, NULL, 0 },
- { boot_complete_str, no_argument, NULL, 0 },
- { boot_reason_str, no_argument, NULL, 0 },
- { factory_reset_str, no_argument, NULL, 0 },
- { NULL, 0, NULL, 0 }
+ { "help", no_argument, NULL, 'h' },
+ { "log", no_argument, NULL, 'l' },
+ { "print", no_argument, NULL, 'p' },
+ { "record", required_argument, NULL, 'r' },
+ { value_str, required_argument, NULL, 0 },
+ { system_boot_reason_str, no_argument, NULL, 0 },
+ { boot_complete_str, no_argument, NULL, 0 },
+ { boot_reason_str, no_argument, NULL, 0 },
+ { factory_reset_str, no_argument, NULL, 0 },
+ { NULL, 0, NULL, 0 }
// clang-format on
};
@@ -1091,6 +1313,8 @@
// |optarg| is an external variable set by getopt representing
// the option argument.
value = optarg;
+ } else if (option_name == system_boot_reason_str) {
+ SetSystemBootReason();
} else if (option_name == boot_complete_str) {
RecordBootComplete();
} else if (option_name == boot_reason_str) {
diff --git a/bootstat/bootstat.rc b/bootstat/bootstat.rc
index f06a38f..1300a27 100644
--- a/bootstat/bootstat.rc
+++ b/bootstat/bootstat.rc
@@ -68,8 +68,9 @@
# Record boot complete metrics.
on property:sys.boot_completed=1 && property:sys.logbootcomplete=1
+ # Converts bootloader boot reason to system boot reason
# Record boot_complete and related stats (decryption, etc).
# Record the boot reason.
# Record time since factory reset.
# Log all boot events.
- exec_background - system log -- /system/bin/bootstat --record_boot_complete --record_boot_reason --record_time_since_factory_reset -l
+ exec_background - system log -- /system/bin/bootstat --set_system_boot_reason --record_boot_complete --record_boot_reason --record_time_since_factory_reset -l
diff --git a/debuggerd/Android.bp b/debuggerd/Android.bp
index 7e6f24d..03b3287 100644
--- a/debuggerd/Android.bp
+++ b/debuggerd/Android.bp
@@ -17,6 +17,7 @@
cc_library_headers {
name: "libdebuggerd_common_headers",
export_include_dirs: ["common/include"],
+ recovery_available: true,
}
cc_library_shared {
@@ -42,10 +43,11 @@
export_include_dirs: ["tombstoned/include"],
}
-// Utility library to tombstoned and get an output fd.
+// Utility library to talk to tombstoned and get an output fd.
cc_library_static {
name: "libtombstoned_client_static",
defaults: ["debuggerd_defaults"],
+ recovery_available: true,
srcs: [
"tombstoned/tombstoned_client.cpp",
"util.cpp",
@@ -67,6 +69,7 @@
cc_library_static {
name: "libdebuggerd_handler_core",
defaults: ["debuggerd_defaults"],
+ recovery_available: true,
srcs: ["handler/debuggerd_handler.cpp"],
header_libs: [
@@ -101,6 +104,7 @@
cc_library_static {
name: "libdebuggerd_handler_fallback",
defaults: ["debuggerd_defaults"],
+ recovery_available: true,
srcs: [
"handler/debuggerd_fallback.cpp",
],
@@ -112,12 +116,17 @@
"libbase",
"libdebuggerd",
"libbacktrace",
- "libunwind",
"libunwindstack",
"libdexfile",
"liblzma",
"libcutils",
],
+ target: {
+ recovery: {
+ cflags: ["-DNO_LIBDEXFILE_SUPPORT"],
+ exclude_static_libs: ["libdexfile"],
+ },
+ },
export_include_dirs: ["include"],
}
@@ -144,6 +153,7 @@
cc_library_static {
name: "libdebuggerd",
defaults: ["debuggerd_defaults"],
+ recovery_available: true,
srcs: [
"libdebuggerd/backtrace.cpp",
@@ -156,9 +166,11 @@
local_include_dirs: ["libdebuggerd/include"],
export_include_dirs: ["libdebuggerd/include"],
+ // Needed for private/bionic_fdsan.h
+ include_dirs: ["bionic/libc"],
+
static_libs: [
"libbacktrace",
- "libunwind",
"libunwindstack",
"liblzma",
"libbase",
@@ -221,6 +233,8 @@
stem: "debuggerd_test64",
},
},
+
+ test_suites: ["device-tests"],
}
cc_benchmark {
diff --git a/debuggerd/crash_dump.cpp b/debuggerd/crash_dump.cpp
index a1f0211..93f7572 100644
--- a/debuggerd/crash_dump.cpp
+++ b/debuggerd/crash_dump.cpp
@@ -190,6 +190,19 @@
static unique_fd g_tombstoned_socket;
static unique_fd g_output_fd;
+static void DefuseSignalHandlers() {
+ // Don't try to dump ourselves.
+ struct sigaction action = {};
+ action.sa_handler = SIG_DFL;
+ debuggerd_register_handlers(&action);
+
+ sigset_t mask;
+ sigemptyset(&mask);
+ if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
+ PLOG(FATAL) << "failed to set signal mask";
+ }
+}
+
static void Initialize(char** argv) {
android::base::InitLogging(argv);
android::base::SetAborter([](const char* abort_msg) {
@@ -213,17 +226,6 @@
_exit(1);
});
-
- // Don't try to dump ourselves.
- struct sigaction action = {};
- action.sa_handler = SIG_DFL;
- debuggerd_register_handlers(&action);
-
- sigset_t mask;
- sigemptyset(&mask);
- if (sigprocmask(SIG_SETMASK, &mask, nullptr) != 0) {
- PLOG(FATAL) << "failed to set signal mask";
- }
}
static void ParseArgs(int argc, char** argv, pid_t* pseudothread_tid, DebuggerdDumpType* dump_type) {
@@ -247,24 +249,48 @@
}
static void ReadCrashInfo(unique_fd& fd, siginfo_t* siginfo,
- std::unique_ptr<unwindstack::Regs>* regs, uintptr_t* abort_address) {
+ std::unique_ptr<unwindstack::Regs>* regs, uintptr_t* abort_msg_address,
+ uintptr_t* fdsan_table_address) {
std::aligned_storage<sizeof(CrashInfo) + 1, alignof(CrashInfo)>::type buf;
+ CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &buf, sizeof(buf)));
if (rc == -1) {
PLOG(FATAL) << "failed to read target ucontext";
- } else if (rc != sizeof(CrashInfo)) {
- LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
- << sizeof(CrashInfo);
+ } else {
+ ssize_t expected_size = 0;
+ switch (crash_info->header.version) {
+ case 1:
+ expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV1);
+ break;
+
+ case 2:
+ expected_size = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV2);
+ break;
+
+ default:
+ LOG(FATAL) << "unexpected CrashInfo version: " << crash_info->header.version;
+ break;
+ };
+
+ if (rc != expected_size) {
+ LOG(FATAL) << "read " << rc << " bytes when reading target crash information, expected "
+ << expected_size;
+ }
}
- CrashInfo* crash_info = reinterpret_cast<CrashInfo*>(&buf);
- if (crash_info->version != 1) {
- LOG(FATAL) << "version mismatch, expected 1, received " << crash_info->version;
- }
+ *fdsan_table_address = 0;
+ switch (crash_info->header.version) {
+ case 2:
+ *fdsan_table_address = crash_info->data.v2.fdsan_table_address;
+ case 1:
+ *abort_msg_address = crash_info->data.v1.abort_msg_address;
+ *siginfo = crash_info->data.v1.siginfo;
+ regs->reset(Regs::CreateFromUcontext(Regs::CurrentArch(), &crash_info->data.v1.ucontext));
+ break;
- *siginfo = crash_info->siginfo;
- regs->reset(Regs::CreateFromUcontext(Regs::CurrentArch(), &crash_info->ucontext));
- *abort_address = crash_info->abort_msg_address;
+ default:
+ __builtin_unreachable();
+ }
}
// Wait for a process to clone and return the child's pid.
@@ -321,6 +347,8 @@
}
int main(int argc, char** argv) {
+ DefuseSignalHandlers();
+
atrace_begin(ATRACE_TAG, "before reparent");
pid_t target_process = getppid();
@@ -365,7 +393,8 @@
ATRACE_NAME("after reparent");
pid_t pseudothread_tid;
DebuggerdDumpType dump_type;
- uintptr_t abort_address = 0;
+ uintptr_t abort_msg_address = 0;
+ uintptr_t fdsan_table_address = 0;
Initialize(argv);
ParseArgs(argc, argv, &pseudothread_tid, &dump_type);
@@ -383,7 +412,7 @@
OpenFilesList open_files;
{
ATRACE_NAME("open files");
- populate_open_files_list(g_target_thread, &open_files);
+ populate_open_files_list(&open_files, g_target_thread);
}
// In order to reduce the duration that we pause the process for, we ptrace
@@ -425,7 +454,8 @@
if (thread == g_target_thread) {
// Read the thread's registers along with the rest of the crash info out of the pipe.
- ReadCrashInfo(input_pipe, &siginfo, &info.registers, &abort_address);
+ ReadCrashInfo(input_pipe, &siginfo, &info.registers, &abort_msg_address,
+ &fdsan_table_address);
info.siginfo = &siginfo;
info.signo = info.siginfo->si_signo;
} else {
@@ -500,8 +530,8 @@
g_output_fd = std::move(devnull);
}
- LOG(INFO) << "performing dump of process " << target_process << " (target tid = " << g_target_thread
- << ")";
+ LOG(INFO) << "performing dump of process " << target_process
+ << " (target tid = " << g_target_thread << ")";
int signo = siginfo.si_signo;
bool fatal_signal = signo != DEBUGGER_SIGNAL;
@@ -537,9 +567,16 @@
ATRACE_NAME("dump_backtrace");
dump_backtrace(std::move(g_output_fd), map.get(), thread_info, g_target_thread);
} else {
- ATRACE_NAME("engrave_tombstone");
- engrave_tombstone(std::move(g_output_fd), map.get(), process_memory.get(), thread_info,
- g_target_thread, abort_address, &open_files, &amfd_data);
+ {
+ ATRACE_NAME("fdsan table dump");
+ populate_fdsan_table(&open_files, process_memory, fdsan_table_address);
+ }
+
+ {
+ ATRACE_NAME("engrave_tombstone");
+ engrave_tombstone(std::move(g_output_fd), map.get(), process_memory.get(), thread_info,
+ g_target_thread, abort_msg_address, &open_files, &amfd_data);
+ }
}
if (fatal_signal) {
diff --git a/debuggerd/crasher/crasher.cpp b/debuggerd/crasher/crasher.cpp
index 4b32b9d..f0fe1d0 100644
--- a/debuggerd/crasher/crasher.cpp
+++ b/debuggerd/crasher/crasher.cpp
@@ -183,6 +183,8 @@
fprintf(stderr, " exit call exit(1)\n");
fprintf(stderr, "\n");
fprintf(stderr, " fortify fail a _FORTIFY_SOURCE check\n");
+ fprintf(stderr, " fdsan_file close a file descriptor that's owned by a FILE*\n");
+ fprintf(stderr, " fdsan_dir close a file descriptor that's owned by a DIR*\n");
fprintf(stderr, " seccomp fail a seccomp check\n");
#if defined(__arm__)
fprintf(stderr, " kuser_helper_version call kuser_helper_version\n");
@@ -197,6 +199,7 @@
fprintf(stderr, " LOG-FATAL call libbase LOG(FATAL)\n");
fprintf(stderr, "\n");
fprintf(stderr, " SIGFPE cause a SIGFPE\n");
+ fprintf(stderr, " SIGILL cause a SIGILL\n");
fprintf(stderr, " SIGSEGV cause a SIGSEGV at address 0x0 (synonym: crash)\n");
fprintf(stderr, " SIGSEGV-non-null cause a SIGSEGV at a non-zero address\n");
fprintf(stderr, " SIGSEGV-unmapped mmap/munmap a region of memory and then attempt to access it\n");
@@ -235,62 +238,78 @@
// Actions.
if (!strcasecmp(arg, "SIGSEGV-non-null")) {
- sigsegv_non_null();
+ sigsegv_non_null();
} else if (!strcasecmp(arg, "smash-stack")) {
- volatile int len = 128;
- return smash_stack(&len);
+ volatile int len = 128;
+ return smash_stack(&len);
} else if (!strcasecmp(arg, "stack-overflow")) {
- overflow_stack(nullptr);
+ overflow_stack(nullptr);
} else if (!strcasecmp(arg, "nostack")) {
- crashnostack();
+ crashnostack();
} else if (!strcasecmp(arg, "exit")) {
- exit(1);
+ exit(1);
} else if (!strcasecmp(arg, "call-null")) {
return crash_null();
} else if (!strcasecmp(arg, "crash") || !strcmp(arg, "SIGSEGV")) {
- return crash(42);
+ return crash(42);
} else if (!strcasecmp(arg, "abort")) {
- maybe_abort();
+ maybe_abort();
} else if (!strcasecmp(arg, "assert")) {
- __assert("some_file.c", 123, "false");
+ __assert("some_file.c", 123, "false");
} else if (!strcasecmp(arg, "assert2")) {
- __assert2("some_file.c", 123, "some_function", "false");
+ __assert2("some_file.c", 123, "some_function", "false");
} else if (!strcasecmp(arg, "fortify")) {
- char buf[10];
- __read_chk(-1, buf, 32, 10);
- while (true) pause();
+ char buf[10];
+ __read_chk(-1, buf, 32, 10);
+ while (true) pause();
+ } else if (!strcasecmp(arg, "fdsan_file")) {
+ FILE* f = fopen("/dev/null", "r");
+ close(fileno(f));
+ } else if (!strcasecmp(arg, "fdsan_dir")) {
+ DIR* d = opendir("/dev/");
+ close(dirfd(d));
} else if (!strcasecmp(arg, "LOG(FATAL)")) {
- LOG(FATAL) << "hello " << 123;
+ LOG(FATAL) << "hello " << 123;
} else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL")) {
- LOG_ALWAYS_FATAL("hello %s", "world");
+ LOG_ALWAYS_FATAL("hello %s", "world");
} else if (!strcasecmp(arg, "LOG_ALWAYS_FATAL_IF")) {
- LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
+ LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
} else if (!strcasecmp(arg, "SIGFPE")) {
- raise(SIGFPE);
- return EXIT_SUCCESS;
+ raise(SIGFPE);
+ return EXIT_SUCCESS;
+ } else if (!strcasecmp(arg, "SIGILL")) {
+#if defined(__aarch64__)
+ __asm__ volatile(".word 0\n");
+#elif defined(__arm__)
+ __asm__ volatile(".word 0xe7f0def0\n");
+#elif defined(__i386__) || defined(__x86_64__)
+ __asm__ volatile("ud2\n");
+#else
+#error
+#endif
} else if (!strcasecmp(arg, "SIGTRAP")) {
- raise(SIGTRAP);
- return EXIT_SUCCESS;
+ raise(SIGTRAP);
+ return EXIT_SUCCESS;
} else if (!strcasecmp(arg, "fprintf-NULL")) {
- fprintf_null();
+ fprintf_null();
} else if (!strcasecmp(arg, "readdir-NULL")) {
- readdir_null();
+ readdir_null();
} else if (!strcasecmp(arg, "strlen-NULL")) {
- return strlen_null();
+ return strlen_null();
} else if (!strcasecmp(arg, "pthread_join-NULL")) {
- return pthread_join(0, nullptr);
+ return pthread_join(0, nullptr);
} else if (!strcasecmp(arg, "heap-usage")) {
- abuse_heap();
+ abuse_heap();
} else if (!strcasecmp(arg, "leak")) {
- leak();
+ leak();
} else if (!strcasecmp(arg, "SIGSEGV-unmapped")) {
- char* map = reinterpret_cast<char*>(mmap(nullptr, sizeof(int), PROT_READ | PROT_WRITE,
- MAP_SHARED | MAP_ANONYMOUS, -1, 0));
- munmap(map, sizeof(int));
- map[0] = '8';
+ char* map = reinterpret_cast<char*>(
+ mmap(nullptr, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0));
+ munmap(map, sizeof(int));
+ map[0] = '8';
} else if (!strcasecmp(arg, "seccomp")) {
- set_system_seccomp_filter();
- syscall(99999);
+ set_system_seccomp_filter();
+ syscall(99999);
#if defined(__arm__)
} else if (!strcasecmp(arg, "kuser_helper_version")) {
return __kuser_helper_version;
diff --git a/debuggerd/debuggerd.cpp b/debuggerd/debuggerd.cpp
index b016e23..360ea95 100644
--- a/debuggerd/debuggerd.cpp
+++ b/debuggerd/debuggerd.cpp
@@ -20,6 +20,7 @@
#include <string.h>
#include <limits>
+#include <string_view>
#include <thread>
#include <android-base/file.h>
@@ -33,9 +34,10 @@
using android::base::unique_fd;
static void usage(int exit_code) {
- fprintf(stderr, "usage: debuggerd [-b] PID\n");
+ fprintf(stderr, "usage: debuggerd [-bj] PID\n");
fprintf(stderr, "\n");
fprintf(stderr, "-b, --backtrace just a backtrace rather than a full tombstone\n");
+ fprintf(stderr, "-j collect java traces\n");
_exit(exit_code);
}
@@ -58,8 +60,19 @@
int main(int argc, char* argv[]) {
if (argc <= 1) usage(0);
if (argc > 3) usage(1);
- if (argc == 3 && strcmp(argv[1], "-b") != 0 && strcmp(argv[1], "--backtrace") != 0) usage(1);
- bool backtrace_only = argc == 3;
+
+ DebuggerdDumpType dump_type = kDebuggerdTombstone;
+
+ if (argc == 3) {
+ std::string_view flag = argv[1];
+ if (flag == "-b" || flag == "--backtrace") {
+ dump_type = kDebuggerdNativeBacktrace;
+ } else if (flag == "-j") {
+ dump_type = kDebuggerdJavaBacktrace;
+ } else {
+ usage(1);
+ }
+ }
pid_t pid;
if (!android::base::ParseInt(argv[argc - 1], &pid, 1, std::numeric_limits<pid_t>::max())) {
@@ -90,8 +103,7 @@
}
std::thread redirect_thread = spawn_redirect_thread(std::move(piperead));
- if (!debuggerd_trigger_dump(pid, backtrace_only ? kDebuggerdNativeBacktrace : kDebuggerdTombstone,
- 0, std::move(pipewrite))) {
+ if (!debuggerd_trigger_dump(pid, dump_type, 0, std::move(pipewrite))) {
redirect_thread.join();
errx(1, "failed to dump process %d", pid);
}
diff --git a/debuggerd/debuggerd_test.cpp b/debuggerd/debuggerd_test.cpp
index 397ff2f..dfb7a6a 100644
--- a/debuggerd/debuggerd_test.cpp
+++ b/debuggerd/debuggerd_test.cpp
@@ -80,8 +80,13 @@
return value; \
}()
+// Backtrace frame dump could contain:
+// #01 pc 0001cded /data/tmp/debuggerd_test32 (raise_debugger_signal+80)
+// or
+// #01 pc 00022a09 /data/tmp/debuggerd_test32 (offset 0x12000) (raise_debugger_signal+80)
#define ASSERT_BACKTRACE_FRAME(result, frame_name) \
- ASSERT_MATCH(result, R"(#\d\d pc [0-9a-f]+\s+ \S+ \()" frame_name R"(\+)");
+ ASSERT_MATCH(result, \
+ R"(#\d\d pc [0-9a-f]+\s+ \S+ (\(offset 0x[0-9a-f]+\) )?\()" frame_name R"(\+)");
static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd,
InterceptStatus* status, DebuggerdDumpType intercept_type) {
@@ -346,7 +351,9 @@
std::string result;
ConsumeFd(std::move(output_fd), &result);
- ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER\), fault addr --------)");
+ ASSERT_MATCH(
+ result,
+ R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER from pid \d+, uid \d+\), fault addr --------)");
ASSERT_MATCH(result, R"(backtrace:)");
}
@@ -354,7 +361,14 @@
int intercept_result;
unique_fd output_fd;
StartProcess([]() {
- android_set_abort_message("abort message goes here");
+ // Arrived at experimentally;
+ // logd truncates at 4062.
+ // strlen("Abort message: ''") is 17.
+ // That's 4045, but we also want a NUL.
+ char buf[4045 + 1];
+ memset(buf, 'x', sizeof(buf));
+ buf[sizeof(buf) - 1] = '\0';
+ android_set_abort_message(buf);
abort();
});
StartIntercept(&output_fd);
@@ -366,7 +380,7 @@
std::string result;
ConsumeFd(std::move(output_fd), &result);
- ASSERT_MATCH(result, R"(Abort message: 'abort message goes here')");
+ ASSERT_MATCH(result, R"(Abort message: 'x{4045}')");
}
TEST_F(CrasherTest, abort_message_backtrace) {
diff --git a/debuggerd/handler/debuggerd_fallback.cpp b/debuggerd/handler/debuggerd_fallback.cpp
index dea2e17..079a574 100644
--- a/debuggerd/handler/debuggerd_fallback.cpp
+++ b/debuggerd/handler/debuggerd_fallback.cpp
@@ -304,7 +304,16 @@
crash_mutex.lock();
if (lock_count++ > 0) {
- async_safe_format_log(ANDROID_LOG_ERROR, "libc", "recursed signal handler call, exiting");
+ async_safe_format_log(ANDROID_LOG_ERROR, "libc", "recursed signal handler call, aborting");
+ signal(SIGABRT, SIG_DFL);
+ raise(SIGABRT);
+ sigset_t sigset;
+ sigemptyset(&sigset);
+ sigaddset(&sigset, SIGABRT);
+ sigprocmask(SIG_UNBLOCK, &sigset, nullptr);
+
+ // Just in case...
+ async_safe_format_log(ANDROID_LOG_ERROR, "libc", "abort didn't exit, exiting");
_exit(1);
}
diff --git a/debuggerd/handler/debuggerd_handler.cpp b/debuggerd/handler/debuggerd_handler.cpp
index 05e6efa..91e6f71 100644
--- a/debuggerd/handler/debuggerd_handler.cpp
+++ b/debuggerd/handler/debuggerd_handler.cpp
@@ -59,7 +59,16 @@
#include "protocol.h"
using android::base::Pipe;
-using android::base::unique_fd;
+
+// We muck with our fds in a 'thread' that doesn't share the same fd table.
+// Close fds in that thread with a raw close syscall instead of going through libc.
+struct FdsanBypassCloser {
+ static void Close(int fd) {
+ syscall(__NR_close, fd);
+ }
+};
+
+using unique_fd = android::base::unique_fd_impl<FdsanBypassCloser>;
// see man(2) prctl, specifically the section about PR_GET_NAME
#define MAX_TASK_NAME_LEN (16)
@@ -99,6 +108,7 @@
int saved_errno_;
};
+extern "C" void* android_fdsan_get_fd_table();
extern "C" void debuggerd_fallback_handler(siginfo_t*, ucontext_t*, void*);
static debuggerd_callbacks_t g_callbacks;
@@ -169,24 +179,26 @@
return;
}
- const char* signal_name = get_signame(info->si_signo);
- bool has_address = signal_has_si_addr(info->si_signo, info->si_code);
-
- // Many signals don't have an address.
+ // Many signals don't have an address or sender.
char addr_desc[32] = ""; // ", fault addr 0x1234"
- if (has_address) {
+ if (signal_has_si_addr(info)) {
async_safe_format_buffer(addr_desc, sizeof(addr_desc), ", fault addr %p", info->si_addr);
}
+ pid_t self_pid = __getpid();
+ char sender_desc[32] = {}; // " from pid 1234, uid 666"
+ if (signal_has_sender(info, self_pid)) {
+ get_signal_sender(sender_desc, sizeof(sender_desc), info);
+ }
char main_thread_name[MAX_TASK_NAME_LEN + 1];
if (!get_main_thread_name(main_thread_name, sizeof(main_thread_name))) {
strncpy(main_thread_name, "<unknown>", sizeof(main_thread_name));
}
- async_safe_format_log(
- ANDROID_LOG_FATAL, "libc", "Fatal signal %d (%s), code %d (%s)%s in tid %d (%s), pid %d (%s)",
- info->si_signo, signal_name, info->si_code, get_sigcode(info->si_signo, info->si_code),
- addr_desc, __gettid(), thread_name, __getpid(), main_thread_name);
+ async_safe_format_log(ANDROID_LOG_FATAL, "libc",
+ "Fatal signal %d (%s), code %d (%s%s)%s in tid %d (%s), pid %d (%s)",
+ info->si_signo, get_signame(info), info->si_code, get_sigcode(info),
+ sender_desc, addr_desc, __gettid(), thread_name, self_pid, main_thread_name);
}
/*
@@ -275,6 +287,7 @@
siginfo_t* siginfo;
void* ucontext;
uintptr_t abort_msg;
+ uintptr_t fdsan_table;
};
// Logging and contacting debuggerd requires free file descriptors, which we might not have.
@@ -297,7 +310,8 @@
debugger_thread_info* thread_info = static_cast<debugger_thread_info*>(arg);
for (int i = 0; i < 1024; ++i) {
- close(i);
+ // Don't use close to avoid bionic's file descriptor ownership checks.
+ syscall(__NR_close, i);
}
int devnull = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
@@ -318,23 +332,23 @@
}
// ucontext_t is absurdly large on AArch64, so piece it together manually with writev.
- uint32_t version = 1;
- constexpr size_t expected =
- sizeof(version) + sizeof(siginfo_t) + sizeof(ucontext_t) + sizeof(uintptr_t);
+ uint32_t version = 2;
+ constexpr size_t expected = sizeof(CrashInfoHeader) + sizeof(CrashInfoDataV2);
errno = 0;
if (fcntl(output_write.get(), F_SETPIPE_SZ, expected) < static_cast<int>(expected)) {
- fatal_errno("failed to set pipe bufer size");
+ fatal_errno("failed to set pipe buffer size");
}
- struct iovec iovs[4] = {
+ struct iovec iovs[5] = {
{.iov_base = &version, .iov_len = sizeof(version)},
{.iov_base = thread_info->siginfo, .iov_len = sizeof(siginfo_t)},
{.iov_base = thread_info->ucontext, .iov_len = sizeof(ucontext_t)},
{.iov_base = &thread_info->abort_msg, .iov_len = sizeof(uintptr_t)},
+ {.iov_base = &thread_info->fdsan_table, .iov_len = sizeof(uintptr_t)},
};
- ssize_t rc = TEMP_FAILURE_RETRY(writev(output_write.get(), iovs, 4));
+ ssize_t rc = TEMP_FAILURE_RETRY(writev(output_write.get(), iovs, 5));
if (rc == -1) {
fatal_errno("failed to write crash info");
} else if (rc != expected) {
@@ -492,6 +506,7 @@
.siginfo = info,
.ucontext = context,
.abort_msg = reinterpret_cast<uintptr_t>(abort_message),
+ .fdsan_table = reinterpret_cast<uintptr_t>(android_fdsan_get_fd_table()),
};
// Set PR_SET_DUMPABLE to 1, so that crash_dump can ptrace us.
diff --git a/debuggerd/libdebuggerd/include/libdebuggerd/open_files_list.h b/debuggerd/libdebuggerd/include/libdebuggerd/open_files_list.h
index 4727ca4..d47f2dd 100644
--- a/debuggerd/libdebuggerd/include/libdebuggerd/open_files_list.h
+++ b/debuggerd/libdebuggerd/include/libdebuggerd/open_files_list.h
@@ -14,23 +14,31 @@
* limitations under the License.
*/
-#ifndef _DEBUGGERD_OPEN_FILES_LIST_H
-#define _DEBUGGERD_OPEN_FILES_LIST_H
+#pragma once
+#include <stdint.h>
#include <sys/types.h>
+#include <map>
+#include <optional>
#include <string>
#include <utility>
-#include <vector>
#include "utility.h"
-typedef std::vector<std::pair<int, std::string>> OpenFilesList;
+struct FDInfo {
+ std::optional<std::string> path;
+ std::optional<uint64_t> fdsan_owner;
+};
-/* Populates the given list with open files for the given process. */
-void populate_open_files_list(pid_t pid, OpenFilesList* list);
+using OpenFilesList = std::map<int, FDInfo>;
-/* Dumps the open files list to the log. */
+// Populates the given list with open files for the given process.
+void populate_open_files_list(OpenFilesList* list, pid_t pid);
+
+// Populates the given list with the target process's fdsan table.
+void populate_fdsan_table(OpenFilesList* list, std::shared_ptr<unwindstack::Memory> memory,
+ uint64_t fdsan_table_address);
+
+// Dumps the open files list to the log.
void dump_open_files_list(log_t* log, const OpenFilesList& files, const char* prefix);
-
-#endif // _DEBUGGERD_OPEN_FILES_LIST_H
diff --git a/debuggerd/libdebuggerd/include/libdebuggerd/utility.h b/debuggerd/libdebuggerd/include/libdebuggerd/utility.h
index 7b04e71..7c5304e 100644
--- a/debuggerd/libdebuggerd/include/libdebuggerd/utility.h
+++ b/debuggerd/libdebuggerd/include/libdebuggerd/utility.h
@@ -74,8 +74,10 @@
void drop_capabilities();
-bool signal_has_si_addr(int si_signo, int si_code);
-const char* get_signame(int sig);
-const char* get_sigcode(int signo, int code);
+bool signal_has_sender(const siginfo_t*, pid_t caller_pid);
+bool signal_has_si_addr(const siginfo_t*);
+void get_signal_sender(char* buf, size_t n, const siginfo_t*);
+const char* get_signame(const siginfo_t*);
+const char* get_sigcode(const siginfo_t*);
#endif // _DEBUGGERD_UTILITY_H
diff --git a/debuggerd/libdebuggerd/open_files_list.cpp b/debuggerd/libdebuggerd/open_files_list.cpp
index b12703e..1fdf236 100644
--- a/debuggerd/libdebuggerd/open_files_list.cpp
+++ b/debuggerd/libdebuggerd/open_files_list.cpp
@@ -32,10 +32,12 @@
#include <android-base/file.h>
#include <log/log.h>
+#include <unwindstack/Memory.h>
#include "libdebuggerd/utility.h"
+#include "private/bionic_fdsan.h"
-void populate_open_files_list(pid_t pid, OpenFilesList* list) {
+void populate_open_files_list(OpenFilesList* list, pid_t pid) {
std::string fd_dir_name = "/proc/" + std::to_string(pid) + "/fd";
std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(fd_dir_name.c_str()), closedir);
if (dir == nullptr) {
@@ -53,17 +55,84 @@
std::string path = fd_dir_name + "/" + std::string(de->d_name);
std::string target;
if (android::base::Readlink(path, &target)) {
- list->emplace_back(fd, target);
+ (*list)[fd].path = target;
} else {
+ (*list)[fd].path = "???";
ALOGE("failed to readlink %s: %s", path.c_str(), strerror(errno));
- list->emplace_back(fd, "???");
}
}
}
+void populate_fdsan_table(OpenFilesList* list, std::shared_ptr<unwindstack::Memory> memory,
+ uint64_t fdsan_table_address) {
+ constexpr size_t inline_fds = sizeof(FdTable::entries) / sizeof(*FdTable::entries);
+ static_assert(inline_fds == 128);
+ size_t entry_offset = offsetof(FdTable, entries);
+ for (size_t i = 0; i < inline_fds; ++i) {
+ uint64_t address = fdsan_table_address + entry_offset + sizeof(FdEntry) * i;
+ FdEntry entry;
+ if (!memory->Read(address, &entry, sizeof(entry))) {
+ ALOGE("failed to read fdsan table entry %zu: %s", i, strerror(errno));
+ return;
+ }
+ ALOGE("fd %zu = %#" PRIx64, i, entry.close_tag.load());
+ if (entry.close_tag) {
+ (*list)[i].fdsan_owner = entry.close_tag.load();
+ }
+ }
+
+ size_t overflow_offset = offsetof(FdTable, overflow);
+ uintptr_t overflow = 0;
+ if (!memory->Read(fdsan_table_address + overflow_offset, &overflow, sizeof(overflow))) {
+ ALOGE("failed to read fdsan table overflow pointer: %s", strerror(errno));
+ return;
+ }
+
+ if (!overflow) {
+ return;
+ }
+
+ size_t overflow_length;
+ if (!memory->Read(overflow, &overflow_length, sizeof(overflow_length))) {
+ ALOGE("failed to read fdsan overflow table length: %s", strerror(errno));
+ return;
+ }
+
+ if (overflow_length > 131072) {
+ ALOGE("unreasonable large fdsan overflow table size %zu, bailing out", overflow_length);
+ return;
+ }
+
+ for (size_t i = 0; i < overflow_length; ++i) {
+ int fd = i + inline_fds;
+ uint64_t address = overflow + offsetof(FdTableOverflow, entries) + i * sizeof(FdEntry);
+ FdEntry entry;
+ if (!memory->Read(address, &entry, sizeof(entry))) {
+ ALOGE("failed to read fdsan overflow entry for fd %d: %s", fd, strerror(errno));
+ return;
+ }
+ if (entry.close_tag) {
+ (*list)[fd].fdsan_owner = entry.close_tag;
+ }
+ }
+ return;
+}
+
void dump_open_files_list(log_t* log, const OpenFilesList& files, const char* prefix) {
- for (auto& file : files) {
- _LOG(log, logtype::OPEN_FILES, "%sfd %i: %s\n", prefix, file.first, file.second.c_str());
+ for (auto& [fd, entry] : files) {
+ const std::optional<std::string>& path = entry.path;
+ const std::optional<uint64_t>& fdsan_owner = entry.fdsan_owner;
+ if (path && fdsan_owner) {
+ _LOG(log, logtype::OPEN_FILES, "%sfd %i: %s (owned by %#" PRIx64 ")\n", prefix, fd,
+ path->c_str(), *fdsan_owner);
+ } else if (path && !fdsan_owner) {
+ _LOG(log, logtype::OPEN_FILES, "%sfd %i: %s (unowned)\n", prefix, fd, path->c_str());
+ } else if (!path && fdsan_owner) {
+ _LOG(log, logtype::OPEN_FILES, "%sfd %i: <MISSING> (owned by %#" PRIx64 ")\n", prefix, fd,
+ *fdsan_owner);
+ } else {
+ ALOGE("OpenFilesList contains an entry (fd %d) with no path or owner", fd);
+ }
}
}
diff --git a/debuggerd/libdebuggerd/test/open_files_list_test.cpp b/debuggerd/libdebuggerd/test/open_files_list_test.cpp
index acac72c..d7036fd 100644
--- a/debuggerd/libdebuggerd/test/open_files_list_test.cpp
+++ b/debuggerd/libdebuggerd/test/open_files_list_test.cpp
@@ -34,13 +34,13 @@
// Get the list of open files for this process.
OpenFilesList list;
- populate_open_files_list(getpid(), &list);
+ populate_open_files_list(&list, getpid());
// Verify our open file is in the list.
bool found = false;
- for (auto& file : list) {
+ for (auto& file : list) {
if (file.first == tf.fd) {
- EXPECT_EQ(file.second, std::string(tf.path));
+ EXPECT_EQ(file.second.path.value_or(""), std::string(tf.path));
found = true;
break;
}
diff --git a/debuggerd/libdebuggerd/tombstone.cpp b/debuggerd/libdebuggerd/tombstone.cpp
index 140ef6d..0b8a936 100644
--- a/debuggerd/libdebuggerd/tombstone.cpp
+++ b/debuggerd/libdebuggerd/tombstone.cpp
@@ -102,18 +102,31 @@
if (!cause.empty()) _LOG(log, logtype::HEADER, "Cause: %s\n", cause.c_str());
}
-static void dump_signal_info(log_t* log, const siginfo_t* si) {
- char addr_desc[32]; // ", fault addr 0x1234"
- if (signal_has_si_addr(si->si_signo, si->si_code)) {
- snprintf(addr_desc, sizeof(addr_desc), "%p", si->si_addr);
+static void dump_signal_info(log_t* log, const ThreadInfo& thread_info, Memory* process_memory) {
+ char addr_desc[64]; // ", fault addr 0x1234"
+ if (signal_has_si_addr(thread_info.siginfo)) {
+ void* addr = thread_info.siginfo->si_addr;
+ if (thread_info.siginfo->si_signo == SIGILL) {
+ uint32_t instruction = {};
+ process_memory->Read(reinterpret_cast<uint64_t>(addr), &instruction, sizeof(instruction));
+ snprintf(addr_desc, sizeof(addr_desc), "%p (*pc=%#08x)", addr, instruction);
+ } else {
+ snprintf(addr_desc, sizeof(addr_desc), "%p", addr);
+ }
} else {
snprintf(addr_desc, sizeof(addr_desc), "--------");
}
- _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s), fault addr %s\n", si->si_signo,
- get_signame(si->si_signo), si->si_code, get_sigcode(si->si_signo, si->si_code), addr_desc);
+ char sender_desc[32] = {}; // " from pid 1234, uid 666"
+ if (signal_has_sender(thread_info.siginfo, thread_info.pid)) {
+ get_signal_sender(sender_desc, sizeof(sender_desc), thread_info.siginfo);
+ }
- dump_probable_cause(log, si);
+ _LOG(log, logtype::HEADER, "signal %d (%s), code %d (%s%s), fault addr %s\n",
+ thread_info.siginfo->si_signo, get_signame(thread_info.siginfo),
+ thread_info.siginfo->si_code, get_sigcode(thread_info.siginfo), sender_desc, addr_desc);
+
+ dump_probable_cause(log, thread_info.siginfo);
}
static void dump_thread_info(log_t* log, const ThreadInfo& thread_info) {
@@ -239,19 +252,22 @@
return;
}
- char msg[512];
- if (length >= sizeof(msg)) {
- _LOG(log, logtype::HEADER, "Abort message too long: claimed length = %zd\n", length);
+ // The length field includes the length of the length field itself.
+ if (length < sizeof(size_t)) {
+ _LOG(log, logtype::HEADER, "Abort message header malformed: claimed length = %zd\n", length);
return;
}
- if (!process_memory->ReadFully(address + sizeof(length), msg, length)) {
+ length -= sizeof(size_t);
+
+ // The abort message should be null terminated already, but reserve a spot for NUL just in case.
+ std::vector<char> msg(length + 1);
+ if (!process_memory->ReadFully(address + sizeof(length), &msg[0], length)) {
_LOG(log, logtype::HEADER, "Failed to read abort message: %s\n", strerror(errno));
return;
}
- msg[length] = '\0';
- _LOG(log, logtype::HEADER, "Abort message: '%s'\n", msg);
+ _LOG(log, logtype::HEADER, "Abort message: '%s'\n", &msg[0]);
}
static void dump_all_maps(log_t* log, BacktraceMap* map, Memory* process_memory, uint64_t addr) {
@@ -409,7 +425,7 @@
dump_thread_info(log, thread_info);
if (thread_info.siginfo) {
- dump_signal_info(log, thread_info.siginfo);
+ dump_signal_info(log, thread_info, process_memory);
}
if (primary_thread) {
@@ -418,8 +434,10 @@
dump_registers(log, thread_info.registers.get());
+ // Unwind will mutate the registers, so make a copy first.
+ std::unique_ptr<Regs> regs_copy(thread_info.registers->Clone());
std::vector<backtrace_frame_data_t> frames;
- if (!Backtrace::Unwind(thread_info.registers.get(), map, &frames, 0, nullptr)) {
+ if (!Backtrace::Unwind(regs_copy.get(), map, &frames, 0, nullptr)) {
_LOG(log, logtype::THREAD, "Failed to unwind");
return false;
}
@@ -437,7 +455,7 @@
if (map) {
uint64_t addr = 0;
siginfo_t* si = thread_info.siginfo;
- if (signal_has_si_addr(si->si_signo, si->si_code)) {
+ if (signal_has_si_addr(si)) {
addr = reinterpret_cast<uint64_t>(si->si_addr);
}
dump_all_maps(log, map, process_memory, addr);
@@ -456,7 +474,7 @@
static void dump_log_file(log_t* log, pid_t pid, const char* filename, unsigned int tail) {
bool first = true;
- struct logger_list* logger_list;
+ logger_list* logger_list;
if (!log->should_retrieve_logcat) {
return;
@@ -470,11 +488,9 @@
return;
}
- struct log_msg log_entry;
-
while (true) {
+ log_msg log_entry;
ssize_t actual = android_logger_list_read(logger_list, &log_entry);
- struct logger_entry* entry;
if (actual < 0) {
if (actual == -EINTR) {
@@ -497,8 +513,6 @@
// high-frequency debug diagnostics should just be written to
// the tombstone file.
- entry = &log_entry.entry_v1;
-
if (first) {
_LOG(log, logtype::LOGS, "--------- %slog %s\n",
tail ? "tail end of " : "", filename);
@@ -509,19 +523,8 @@
//
// We want to display it in the same format as "logcat -v threadtime"
// (although in this case the pid is redundant).
- static const char* kPrioChars = "!.VDIWEFS";
- unsigned hdr_size = log_entry.entry.hdr_size;
- if (!hdr_size) {
- hdr_size = sizeof(log_entry.entry_v1);
- }
- if ((hdr_size < sizeof(log_entry.entry_v1)) ||
- (hdr_size > sizeof(log_entry.entry))) {
- continue;
- }
- char* msg = reinterpret_cast<char*>(log_entry.buf) + hdr_size;
-
char timeBuf[32];
- time_t sec = static_cast<time_t>(entry->sec);
+ time_t sec = static_cast<time_t>(log_entry.entry.sec);
struct tm tmBuf;
struct tm* ptm;
ptm = localtime_r(&sec, &tmBuf);
@@ -529,17 +532,23 @@
if (log_entry.id() == LOG_ID_EVENTS) {
if (!g_eventTagMap) {
- g_eventTagMap = android_openEventTagMap(NULL);
+ g_eventTagMap = android_openEventTagMap(nullptr);
}
AndroidLogEntry e;
char buf[512];
- android_log_processBinaryLogBuffer(entry, &e, g_eventTagMap, buf, sizeof(buf));
- _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8.*s: %s\n",
- timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
- 'I', (int)e.tagLen, e.tag, e.message);
+ if (android_log_processBinaryLogBuffer(&log_entry.entry_v1, &e, g_eventTagMap, buf,
+ sizeof(buf)) == 0) {
+ _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8.*s: %s\n", timeBuf,
+ log_entry.entry.nsec / 1000000, log_entry.entry.pid, log_entry.entry.tid, 'I',
+ (int)e.tagLen, e.tag, e.message);
+ }
continue;
}
+ char* msg = log_entry.msg();
+ if (msg == nullptr) {
+ continue;
+ }
unsigned char prio = msg[0];
char* tag = msg + 1;
msg = tag + strlen(tag) + 1;
@@ -550,20 +559,21 @@
*nl-- = '\0';
}
+ static const char* kPrioChars = "!.VDIWEFS";
char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
// Look for line breaks ('\n') and display each text line
// on a separate line, prefixed with the header, like logcat does.
do {
nl = strchr(msg, '\n');
- if (nl) {
+ if (nl != nullptr) {
*nl = '\0';
++nl;
}
- _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n",
- timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
- prioChar, tag, msg);
+ _LOG(log, logtype::LOGS, "%s.%03d %5d %5d %c %-8s: %s\n", timeBuf,
+ log_entry.entry.nsec / 1000000, log_entry.entry.pid, log_entry.entry.tid, prioChar, tag,
+ msg);
} while ((msg = nl));
}
diff --git a/debuggerd/libdebuggerd/utility.cpp b/debuggerd/libdebuggerd/utility.cpp
index d153865..8bdc02f 100644
--- a/debuggerd/libdebuggerd/utility.cpp
+++ b/debuggerd/libdebuggerd/utility.cpp
@@ -74,25 +74,22 @@
&& (log->crashed_tid == log->current_tid);
static bool write_to_kmsg = should_write_to_kmsg();
- char buf[512];
+ std::string msg;
va_list ap;
va_start(ap, fmt);
- vsnprintf(buf, sizeof(buf), fmt, ap);
+ android::base::StringAppendV(&msg, fmt, ap);
va_end(ap);
- size_t len = strlen(buf);
- if (len <= 0) {
- return;
- }
+ if (msg.empty()) return;
if (write_to_tombstone) {
- TEMP_FAILURE_RETRY(write(log->tfd, buf, len));
+ TEMP_FAILURE_RETRY(write(log->tfd, msg.c_str(), msg.size()));
}
if (write_to_logcat) {
- __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, buf);
+ __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, msg.c_str());
if (log->amfd_data != nullptr) {
- *log->amfd_data += buf;
+ *log->amfd_data += msg;
}
if (write_to_kmsg) {
@@ -100,11 +97,11 @@
if (kmsg_fd.get() >= 0) {
// Our output might contain newlines which would otherwise be handled by the android logger.
// Split the lines up ourselves before sending to the kernel logger.
- if (buf[len - 1] == '\n') {
- buf[len - 1] = '\0';
+ if (msg.back() == '\n') {
+ msg.back() = '\0';
}
- std::vector<std::string> fragments = android::base::Split(buf, "\n");
+ std::vector<std::string> fragments = android::base::Split(msg, "\n");
for (const std::string& fragment : fragments) {
static constexpr char prefix[] = "<3>DEBUG: ";
struct iovec iov[3];
@@ -257,13 +254,13 @@
}
}
-bool signal_has_si_addr(int si_signo, int si_code) {
+bool signal_has_si_addr(const siginfo_t* si) {
// Manually sent signals won't have si_addr.
- if (si_code == SI_USER || si_code == SI_QUEUE || si_code == SI_TKILL) {
+ if (si->si_code == SI_USER || si->si_code == SI_QUEUE || si->si_code == SI_TKILL) {
return false;
}
- switch (si_signo) {
+ switch (si->si_signo) {
case SIGBUS:
case SIGFPE:
case SIGILL:
@@ -275,16 +272,22 @@
}
}
-const char* get_signame(int sig) {
- switch (sig) {
+bool signal_has_sender(const siginfo_t* si, pid_t caller_pid) {
+ return SI_FROMUSER(si) && (si->si_pid != 0) && (si->si_pid != caller_pid);
+}
+
+void get_signal_sender(char* buf, size_t n, const siginfo_t* si) {
+ snprintf(buf, n, " from pid %d, uid %d", si->si_pid, si->si_uid);
+}
+
+const char* get_signame(const siginfo_t* si) {
+ switch (si->si_signo) {
case SIGABRT: return "SIGABRT";
case SIGBUS: return "SIGBUS";
case SIGFPE: return "SIGFPE";
case SIGILL: return "SIGILL";
case SIGSEGV: return "SIGSEGV";
-#if defined(SIGSTKFLT)
case SIGSTKFLT: return "SIGSTKFLT";
-#endif
case SIGSTOP: return "SIGSTOP";
case SIGSYS: return "SIGSYS";
case SIGTRAP: return "SIGTRAP";
@@ -293,11 +296,11 @@
}
}
-const char* get_sigcode(int signo, int code) {
+const char* get_sigcode(const siginfo_t* si) {
// Try the signal-specific codes...
- switch (signo) {
+ switch (si->si_signo) {
case SIGILL:
- switch (code) {
+ switch (si->si_code) {
case ILL_ILLOPC: return "ILL_ILLOPC";
case ILL_ILLOPN: return "ILL_ILLOPN";
case ILL_ILLADR: return "ILL_ILLADR";
@@ -306,11 +309,17 @@
case ILL_PRVREG: return "ILL_PRVREG";
case ILL_COPROC: return "ILL_COPROC";
case ILL_BADSTK: return "ILL_BADSTK";
+ case ILL_BADIADDR:
+ return "ILL_BADIADDR";
+ case __ILL_BREAK:
+ return "ILL_BREAK";
+ case __ILL_BNDMOD:
+ return "ILL_BNDMOD";
}
- static_assert(NSIGILL == ILL_BADSTK, "missing ILL_* si_code");
+ static_assert(NSIGILL == __ILL_BNDMOD, "missing ILL_* si_code");
break;
case SIGBUS:
- switch (code) {
+ switch (si->si_code) {
case BUS_ADRALN: return "BUS_ADRALN";
case BUS_ADRERR: return "BUS_ADRERR";
case BUS_OBJERR: return "BUS_OBJERR";
@@ -320,7 +329,7 @@
static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
break;
case SIGFPE:
- switch (code) {
+ switch (si->si_code) {
case FPE_INTDIV: return "FPE_INTDIV";
case FPE_INTOVF: return "FPE_INTOVF";
case FPE_FLTDIV: return "FPE_FLTDIV";
@@ -329,45 +338,53 @@
case FPE_FLTRES: return "FPE_FLTRES";
case FPE_FLTINV: return "FPE_FLTINV";
case FPE_FLTSUB: return "FPE_FLTSUB";
+ case __FPE_DECOVF:
+ return "FPE_DECOVF";
+ case __FPE_DECDIV:
+ return "FPE_DECDIV";
+ case __FPE_DECERR:
+ return "FPE_DECERR";
+ case __FPE_INVASC:
+ return "FPE_INVASC";
+ case __FPE_INVDEC:
+ return "FPE_INVDEC";
+ case FPE_FLTUNK:
+ return "FPE_FLTUNK";
+ case FPE_CONDTRAP:
+ return "FPE_CONDTRAP";
}
- static_assert(NSIGFPE == FPE_FLTSUB, "missing FPE_* si_code");
+ static_assert(NSIGFPE == FPE_CONDTRAP, "missing FPE_* si_code");
break;
case SIGSEGV:
- switch (code) {
+ switch (si->si_code) {
case SEGV_MAPERR: return "SEGV_MAPERR";
case SEGV_ACCERR: return "SEGV_ACCERR";
-#if defined(SEGV_BNDERR)
case SEGV_BNDERR: return "SEGV_BNDERR";
-#endif
-#if defined(SEGV_PKUERR)
case SEGV_PKUERR: return "SEGV_PKUERR";
-#endif
+ case SEGV_ACCADI:
+ return "SEGV_ACCADI";
+ case SEGV_ADIDERR:
+ return "SEGV_ADIDERR";
+ case SEGV_ADIPERR:
+ return "SEGV_ADIPERR";
}
-#if defined(SEGV_PKUERR)
- static_assert(NSIGSEGV == SEGV_PKUERR, "missing SEGV_* si_code");
-#elif defined(SEGV_BNDERR)
- static_assert(NSIGSEGV == SEGV_BNDERR, "missing SEGV_* si_code");
-#else
- static_assert(NSIGSEGV == SEGV_ACCERR, "missing SEGV_* si_code");
-#endif
+ static_assert(NSIGSEGV == SEGV_ADIPERR, "missing SEGV_* si_code");
break;
-#if defined(SYS_SECCOMP) // Our glibc is too old, and we build this for the host too.
case SIGSYS:
- switch (code) {
+ switch (si->si_code) {
case SYS_SECCOMP: return "SYS_SECCOMP";
}
static_assert(NSIGSYS == SYS_SECCOMP, "missing SYS_* si_code");
break;
-#endif
case SIGTRAP:
- switch (code) {
+ switch (si->si_code) {
case TRAP_BRKPT: return "TRAP_BRKPT";
case TRAP_TRACE: return "TRAP_TRACE";
case TRAP_BRANCH: return "TRAP_BRANCH";
case TRAP_HWBKPT: return "TRAP_HWBKPT";
}
- if ((code & 0xff) == SIGTRAP) {
- switch ((code >> 8) & 0xff) {
+ if ((si->si_code & 0xff) == SIGTRAP) {
+ switch ((si->si_code >> 8) & 0xff) {
case PTRACE_EVENT_FORK:
return "PTRACE_EVENT_FORK";
case PTRACE_EVENT_VFORK:
@@ -390,7 +407,7 @@
break;
}
// Then the other codes...
- switch (code) {
+ switch (si->si_code) {
case SI_USER: return "SI_USER";
case SI_KERNEL: return "SI_KERNEL";
case SI_QUEUE: return "SI_QUEUE";
diff --git a/debuggerd/protocol.h b/debuggerd/protocol.h
index 6903b0e..bfd0fbb 100644
--- a/debuggerd/protocol.h
+++ b/debuggerd/protocol.h
@@ -81,9 +81,24 @@
};
// Sent from handler to crash_dump via pipe.
-struct __attribute__((__packed__)) CrashInfo {
- uint32_t version; // must be 1.
+struct __attribute__((__packed__)) CrashInfoHeader {
+ uint32_t version;
+};
+
+struct __attribute__((__packed__)) CrashInfoDataV1 {
siginfo_t siginfo;
ucontext_t ucontext;
uintptr_t abort_msg_address;
};
+
+struct __attribute__((__packed__)) CrashInfoDataV2 : public CrashInfoDataV1 {
+ uintptr_t fdsan_table_address;
+};
+
+struct __attribute__((__packed__)) CrashInfo {
+ CrashInfoHeader header;
+ union {
+ CrashInfoDataV1 v1;
+ CrashInfoDataV2 v2;
+ } data;
+};
diff --git a/debuggerd/tombstoned/tombstoned.cpp b/debuggerd/tombstoned/tombstoned.cpp
index 1bf8f14..15ae406 100644
--- a/debuggerd/tombstoned/tombstoned.cpp
+++ b/debuggerd/tombstoned/tombstoned.cpp
@@ -61,10 +61,11 @@
struct Crash {
~Crash() { event_free(crash_event); }
- unique_fd crash_fd;
+ std::string crash_tombstone_path;
+ unique_fd crash_tombstone_fd;
+ unique_fd crash_socket_fd;
pid_t crash_pid;
event* crash_event = nullptr;
- std::string crash_path;
DebuggerdDumpType crash_type;
};
@@ -109,24 +110,29 @@
return &queue;
}
- std::pair<unique_fd, std::string> get_output() {
- unique_fd result;
- std::string file_name = StringPrintf("%s%02d", file_name_prefix_.c_str(), next_artifact_);
-
- // Unlink and create the file, instead of using O_TRUNC, to avoid two processes
- // interleaving their output in case we ever get into that situation.
- if (unlinkat(dir_fd_, file_name.c_str(), 0) != 0 && errno != ENOENT) {
- PLOG(FATAL) << "failed to unlink tombstone at " << dir_path_ << "/" << file_name;
- }
-
- result.reset(openat(dir_fd_, file_name.c_str(),
- O_CREAT | O_EXCL | O_WRONLY | O_APPEND | O_CLOEXEC, 0640));
+ std::pair<std::string, unique_fd> get_output() {
+ std::string path;
+ unique_fd result(openat(dir_fd_, ".", O_WRONLY | O_APPEND | O_TMPFILE | O_CLOEXEC, 0640));
if (result == -1) {
- PLOG(FATAL) << "failed to create tombstone at " << dir_path_ << "/" << file_name;
- }
+ // We might not have O_TMPFILE. Try creating with an arbitrary filename instead.
+ static size_t counter = 0;
+ std::string tmp_filename = StringPrintf(".temporary%zu", counter++);
+ result.reset(openat(dir_fd_, tmp_filename.c_str(),
+ O_WRONLY | O_APPEND | O_CREAT | O_TRUNC | O_CLOEXEC, 0640));
+ if (result == -1) {
+ PLOG(FATAL) << "failed to create temporary tombstone in " << dir_path_;
+ }
+ path = StringPrintf("%s/%s", dir_path_.c_str(), tmp_filename.c_str());
+ }
+ return std::make_pair(std::move(path), std::move(result));
+ }
+
+ std::string get_next_artifact_path() {
+ std::string file_name =
+ StringPrintf("%s/%s%02d", dir_path_.c_str(), file_name_prefix_.c_str(), next_artifact_);
next_artifact_ = (next_artifact_ + 1) % max_artifacts_;
- return {std::move(result), dir_path_ + "/" + file_name};
+ return file_name;
}
bool maybe_enqueue_crash(Crash* crash) {
@@ -203,14 +209,17 @@
static void perform_request(Crash* crash) {
unique_fd output_fd;
- if (!intercept_manager->GetIntercept(crash->crash_pid, crash->crash_type, &output_fd)) {
- std::tie(output_fd, crash->crash_path) = CrashQueue::for_crash(crash)->get_output();
+ bool intercepted =
+ intercept_manager->GetIntercept(crash->crash_pid, crash->crash_type, &output_fd);
+ if (!intercepted) {
+ std::tie(crash->crash_tombstone_path, output_fd) = CrashQueue::for_crash(crash)->get_output();
+ crash->crash_tombstone_fd.reset(dup(output_fd.get()));
}
TombstonedCrashPacket response = {
.packet_type = CrashPacketType::kPerformDump
};
- ssize_t rc = send_fd(crash->crash_fd, &response, sizeof(response), std::move(output_fd));
+ ssize_t rc = send_fd(crash->crash_socket_fd, &response, sizeof(response), std::move(output_fd));
if (rc == -1) {
PLOG(WARNING) << "failed to send response to CrashRequest";
goto fail;
@@ -222,7 +231,7 @@
struct timeval timeout = { 10, 0 };
event_base* base = event_get_base(crash->crash_event);
- event_assign(crash->crash_event, base, crash->crash_fd, EV_TIMEOUT | EV_READ,
+ event_assign(crash->crash_event, base, crash->crash_socket_fd, EV_TIMEOUT | EV_READ,
crash_completed_cb, crash);
event_add(crash->crash_event, &timeout);
}
@@ -243,7 +252,7 @@
// and only native crashes on the native socket.
struct timeval timeout = { 1, 0 };
event* crash_event = event_new(base, sockfd, EV_TIMEOUT | EV_READ, crash_request_cb, crash);
- crash->crash_fd.reset(sockfd);
+ crash->crash_socket_fd.reset(sockfd);
crash->crash_event = crash_event;
event_add(crash_event, &timeout);
}
@@ -342,14 +351,37 @@
goto fail;
}
- if (!crash->crash_path.empty()) {
- if (crash->crash_type == kDebuggerdJavaBacktrace) {
- LOG(ERROR) << "Traces for pid " << crash->crash_pid << " written to: " << crash->crash_path;
+ if (crash->crash_tombstone_fd != -1) {
+ std::string fd_path = StringPrintf("/proc/self/fd/%d", crash->crash_tombstone_fd.get());
+ std::string tombstone_path = CrashQueue::for_crash(crash)->get_next_artifact_path();
+
+ // linkat doesn't let us replace a file, so we need to unlink first.
+ int rc = unlink(tombstone_path.c_str());
+ if (rc != 0 && errno != ENOENT) {
+ PLOG(ERROR) << "failed to unlink tombstone at " << tombstone_path;
+ goto fail;
+ }
+
+ rc = linkat(AT_FDCWD, fd_path.c_str(), AT_FDCWD, tombstone_path.c_str(), AT_SYMLINK_FOLLOW);
+ if (rc != 0) {
+ PLOG(ERROR) << "failed to link tombstone";
} else {
- // NOTE: Several tools parse this log message to figure out where the
- // tombstone associated with a given native crash was written. Any changes
- // to this message must be carefully considered.
- LOG(ERROR) << "Tombstone written to: " << crash->crash_path;
+ if (crash->crash_type == kDebuggerdJavaBacktrace) {
+ LOG(ERROR) << "Traces for pid " << crash->crash_pid << " written to: " << tombstone_path;
+ } else {
+ // NOTE: Several tools parse this log message to figure out where the
+ // tombstone associated with a given native crash was written. Any changes
+ // to this message must be carefully considered.
+ LOG(ERROR) << "Tombstone written to: " << tombstone_path;
+ }
+ }
+
+ // If we don't have O_TMPFILE, we need to clean up after ourselves.
+ if (!crash->crash_tombstone_path.empty()) {
+ rc = unlink(crash->crash_tombstone_path.c_str());
+ if (rc != 0) {
+ PLOG(ERROR) << "failed to unlink temporary tombstone at " << crash->crash_tombstone_path;
+ }
}
}
diff --git a/demangle/Android.bp b/demangle/Android.bp
index 8d5b135..fd79cf8 100644
--- a/demangle/Android.bp
+++ b/demangle/Android.bp
@@ -36,6 +36,7 @@
name: "libdemangle",
defaults: ["libdemangle_defaults"],
vendor_available: true,
+ recovery_available: true,
srcs: [
"Demangler.cpp",
@@ -78,4 +79,9 @@
shared_libs: [
"libdemangle",
],
+
+ test_suites: ["device-tests"],
+ required: [
+ "libdemangle",
+ ],
}
diff --git a/demangle/Android.mk b/demangle/Android.mk
index e3cfc2a..d8082a9 100644
--- a/demangle/Android.mk
+++ b/demangle/Android.mk
@@ -19,7 +19,6 @@
include $(CLEAR_VARS)
LOCAL_MODULE := demangle_fuzzer
-LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := \
Demangler.cpp \
demangle_fuzzer.cpp \
diff --git a/diagnose_usb/Android.bp b/diagnose_usb/Android.bp
index a7ecf37..6bee28c 100644
--- a/diagnose_usb/Android.bp
+++ b/diagnose_usb/Android.bp
@@ -2,6 +2,7 @@
name: "libdiagnose_usb",
cflags: ["-Wall", "-Wextra", "-Werror"],
host_supported: true,
+ recovery_available: true,
target: {
windows: {
enabled: true,
diff --git a/fastboot/Android.bp b/fastboot/Android.bp
new file mode 100644
index 0000000..3414d53
--- /dev/null
+++ b/fastboot/Android.bp
@@ -0,0 +1,124 @@
+// Copyright (C) 2018 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.
+
+cc_library_host_static {
+ name: "libfastboot2",
+
+ //host_supported: true,
+
+ compile_multilib: "first",
+ srcs: [
+ "bootimg_utils.cpp",
+ "fs.cpp",
+ "socket.cpp",
+ "tcp.cpp",
+ "udp.cpp",
+ "util.cpp",
+ "fastboot_driver.cpp",
+ ],
+
+ static_libs: [
+ "libziparchive",
+ "libsparse",
+ "libutils",
+ "liblog",
+ "libz",
+ "libdiagnose_usb",
+ "libbase",
+ "libcutils",
+ "libgtest",
+ "libgtest_main",
+ "libbase",
+ "libadb_host"
+ ],
+
+ header_libs: [
+ "bootimg_headers"
+ ],
+
+ export_header_lib_headers: [
+ "bootimg_headers"
+ ],
+
+
+ target: {
+ linux: {
+ srcs: ["usb_linux.cpp"],
+ },
+ },
+
+ cflags: [
+ "-Wall",
+ "-Wextra",
+ "-Werror",
+ "-Wunreachable-code",
+ ],
+
+ export_include_dirs: ["."],
+
+}
+
+cc_defaults {
+ name: "fastboot_defaults",
+
+ cflags: [
+ "-Wall",
+ "-Wextra",
+ "-Werror",
+ "-Wvla",
+ ],
+ rtti: true,
+
+ clang_cflags: [
+ "-Wthread-safety",
+ ],
+}
+
+cc_binary {
+ name: "fastbootd",
+ defaults: ["fastboot_defaults"],
+
+ recovery: true,
+
+ srcs: [
+ "device/commands.cpp",
+ "device/fastboot_device.cpp",
+ "device/flashing.cpp",
+ "device/main.cpp",
+ "device/usb_client.cpp",
+ "device/utility.cpp",
+ "device/variables.cpp",
+ ],
+
+ shared_libs: [
+ "android.hardware.boot@1.0",
+ "libadbd",
+ "libasyncio",
+ "libbase",
+ "libbootloader_message",
+ "libcutils",
+ "libext2_uuid",
+ "libext4_utils",
+ "libfs_mgr",
+ "libhidlbase",
+ "libhidltransport",
+ "libhwbinder",
+ "liblog",
+ "liblp",
+ "libsparse",
+ "libutils",
+ ],
+
+ cpp_std: "c++17",
+}
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index f5bcc26..a679143 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -16,48 +16,16 @@
include $(LOCAL_PATH)/../platform_tools_tool_version.mk
-include $(CLEAR_VARS)
-
-LOCAL_CFLAGS += -DFASTBOOT_VERSION="\"$(tool_version)\""
-
-LOCAL_C_INCLUDES := \
- $(LOCAL_PATH)/../adb \
- $(LOCAL_PATH)/../mkbootimg \
-
-LOCAL_SRC_FILES := \
- bootimg_utils.cpp \
- engine.cpp \
- fastboot.cpp \
- fs.cpp\
- protocol.cpp \
- socket.cpp \
- tcp.cpp \
- udp.cpp \
- util.cpp \
-
-LOCAL_MODULE := fastboot
-LOCAL_MODULE_TAGS := debug
-LOCAL_MODULE_HOST_OS := darwin linux windows
-LOCAL_CFLAGS += -Wall -Wextra -Werror -Wunreachable-code
-LOCAL_REQUIRED_MODULES := mke2fs make_f2fs
-
-LOCAL_SRC_FILES_linux := usb_linux.cpp
-LOCAL_STATIC_LIBRARIES_linux := libselinux
-LOCAL_REQUIRED_MODULES_linux := e2fsdroid mke2fs.conf sload_f2fs
-
-LOCAL_SRC_FILES_darwin := usb_osx.cpp
-LOCAL_STATIC_LIBRARIES_darwin := libselinux
-LOCAL_REQUIRED_MODULES_darwin := e2fsdroid mke2fs.conf sload_f2fs
-LOCAL_LDLIBS_darwin := -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
-LOCAL_CFLAGS_darwin := -Wno-unused-parameter
-
-LOCAL_SRC_FILES_windows := usb_windows.cpp
-LOCAL_SHARED_LIBRARIES_windows := AdbWinApi
-LOCAL_REQUIRED_MODULES_windows := AdbWinUsbApi
-LOCAL_LDLIBS_windows := -lws2_32
-LOCAL_C_INCLUDES_windows := development/host/windows/usb/api
-
-LOCAL_STATIC_LIBRARIES := \
+fastboot_cflags := -Wall -Wextra -Werror -Wunreachable-code
+fastboot_cflags += -DFASTBOOT_VERSION="\"$(tool_version)\""
+fastboot_cflags_darwin := -Wno-unused-parameter
+fastboot_ldlibs_darwin := -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
+fastboot_ldlibs_windows := -lws2_32
+# Don't add anything here, we don't want additional shared dependencies
+# on the host fastboot tool, and shared libraries that link against libc++
+# will violate ODR.
+fastboot_shared_libs :=
+fastboot_static_libs := \
libziparchive \
libsparse \
libutils \
@@ -68,59 +36,105 @@
libcutils \
libgtest_host \
-LOCAL_CXX_STL := libc++_static
+fastboot_stl := libc++_static
-# Don't add anything here, we don't want additional shared dependencies
-# on the host fastboot tool, and shared libraries that link against libc++
-# will violate ODR
-LOCAL_SHARED_LIBRARIES :=
+#
+# Build host libfastboot.
+#
-include $(BUILD_HOST_EXECUTABLE)
-
-my_dist_files := $(LOCAL_BUILT_MODULE)
-my_dist_files += $(HOST_OUT_EXECUTABLES)/mke2fs$(HOST_EXECUTABLE_SUFFIX)
-my_dist_files += $(HOST_OUT_EXECUTABLES)/e2fsdroid$(HOST_EXECUTABLE_SUFFIX)
-my_dist_files += $(HOST_OUT_EXECUTABLES)/make_f2fs$(HOST_EXECUTABLE_SUFFIX)
-my_dist_files += $(HOST_OUT_EXECUTABLES)/sload_f2fs$(HOST_EXECUTABLE_SUFFIX)
-$(call dist-for-goals,dist_files sdk win_sdk,$(my_dist_files))
-ifdef HOST_CROSS_OS
-# Archive fastboot.exe for win_sdk build.
-$(call dist-for-goals,win_sdk,$(ALL_MODULES.host_cross_fastboot.BUILT))
-endif
-my_dist_files :=
-
-ifeq ($(HOST_OS),linux)
include $(CLEAR_VARS)
-LOCAL_SRC_FILES := usbtest.cpp usb_linux.cpp util.cpp
-LOCAL_MODULE := usbtest
-LOCAL_CFLAGS := -Werror
-LOCAL_STATIC_LIBRARIES := libbase
-include $(BUILD_HOST_EXECUTABLE)
-endif
-
-# fastboot_test
-# =========================================================
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := fastboot_test
+LOCAL_MODULE := libfastboot
LOCAL_MODULE_HOST_OS := darwin linux windows
LOCAL_SRC_FILES := \
+ bootimg_utils.cpp \
+ engine.cpp \
+ fastboot.cpp \
+ fs.cpp \
socket.cpp \
+ tcp.cpp \
+ udp.cpp \
+ util.cpp \
+ fastboot_driver.cpp \
+
+LOCAL_SRC_FILES_darwin := usb_osx.cpp
+LOCAL_SRC_FILES_linux := usb_linux.cpp
+LOCAL_SRC_FILES_windows := usb_windows.cpp
+
+LOCAL_C_INCLUDES_windows := development/host/windows/usb/api
+LOCAL_CFLAGS := $(fastboot_cflags)
+LOCAL_CFLAGS_darwin := $(fastboot_cflags_darwin)
+LOCAL_CXX_STL := $(fastboot_stl)
+LOCAL_HEADER_LIBRARIES := bootimg_headers
+LOCAL_LDLIBS_darwin := $(fastboot_ldlibs_darwin)
+LOCAL_LDLIBS_windows := $(fastboot_ldlibs_windows)
+LOCAL_SHARED_LIBRARIES := $(fastboot_shared_libs)
+LOCAL_STATIC_LIBRARIES := $(fastboot_static_libs)
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+#
+# Build host fastboot / fastboot.exe
+#
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := fastboot
+LOCAL_MODULE_HOST_OS := darwin linux windows
+
+LOCAL_CFLAGS := $(fastboot_cflags)
+LOCAL_CFLAGS_darwin := $(fastboot_cflags_darwin)
+LOCAL_CPP_STD := c++17
+LOCAL_CXX_STL := $(fastboot_stl)
+LOCAL_HEADER_LIBRARIES := bootimg_headers
+LOCAL_LDLIBS_darwin := $(fastboot_ldlibs_darwin)
+LOCAL_LDLIBS_windows := $(fastboot_ldlibs_windows)
+LOCAL_REQUIRED_MODULES := mke2fs make_f2fs
+LOCAL_REQUIRED_MODULES_darwin := e2fsdroid mke2fs.conf sload_f2fs
+LOCAL_REQUIRED_MODULES_linux := e2fsdroid mke2fs.conf sload_f2fs
+LOCAL_REQUIRED_MODULES_windows := AdbWinUsbApi
+LOCAL_SRC_FILES := main.cpp
+LOCAL_SHARED_LIBRARIES := $(fastboot_shared_libs)
+LOCAL_SHARED_LIBRARIES_windows := AdbWinApi
+LOCAL_STATIC_LIBRARIES := libfastboot $(fastboot_static_libs)
+include $(BUILD_HOST_EXECUTABLE)
+
+#
+# Package fastboot-related executables.
+#
+
+my_dist_files := $(HOST_OUT_EXECUTABLES)/fastboot
+my_dist_files += $(HOST_OUT_EXECUTABLES)/mke2fs
+my_dist_files += $(HOST_OUT_EXECUTABLES)/e2fsdroid
+my_dist_files += $(HOST_OUT_EXECUTABLES)/make_f2fs
+my_dist_files += $(HOST_OUT_EXECUTABLES)/sload_f2fs
+$(call dist-for-goals,dist_files sdk win_sdk,$(my_dist_files))
+ifdef HOST_CROSS_OS
+$(call dist-for-goals,dist_files sdk win_sdk,$(ALL_MODULES.host_cross_fastboot.BUILT))
+endif
+my_dist_files :=
+
+#
+# Build host fastboot_test.
+#
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := fastboot_test
+LOCAL_MODULE_HOST_OS := darwin linux windows
+LOCAL_MODULE_HOST_CROSS_ARCH := x86 # Avoid trying to build for win64.
+
+LOCAL_SRC_FILES := \
+ fastboot_test.cpp \
socket_mock.cpp \
socket_test.cpp \
- tcp.cpp \
tcp_test.cpp \
- udp.cpp \
udp_test.cpp \
-LOCAL_STATIC_LIBRARIES := libbase libcutils
-
-LOCAL_CFLAGS += -Wall -Wextra -Werror -Wunreachable-code
-
-LOCAL_LDLIBS_darwin := -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
-LOCAL_CFLAGS_darwin := -Wno-unused-parameter
-
-LOCAL_LDLIBS_windows := -lws2_32
-
+LOCAL_CFLAGS := $(fastboot_cflags)
+LOCAL_CFLAGS_darwin := $(fastboot_cflags_darwin)
+LOCAL_CXX_STL := $(fastboot_stl)
+LOCAL_HEADER_LIBRARIES := bootimg_headers
+LOCAL_LDLIBS_darwin := $(fastboot_ldlibs_darwin)
+LOCAL_LDLIBS_windows := $(fastboot_ldlibs_windows)
+LOCAL_SHARED_LIBRARIES := $(fastboot_shared_libs)
+LOCAL_SHARED_LIBRARIES_windows := AdbWinApi
+LOCAL_STATIC_LIBRARIES := libfastboot $(fastboot_static_libs)
include $(BUILD_HOST_NATIVE_TEST)
diff --git a/fastboot/OWNERS b/fastboot/OWNERS
index 2d12d50..2088ae3 100644
--- a/fastboot/OWNERS
+++ b/fastboot/OWNERS
@@ -1,3 +1,4 @@
dpursell@google.com
enh@google.com
jmgao@google.com
+tomcherry@google.com
diff --git a/fastboot/bootimg_utils.cpp b/fastboot/bootimg_utils.cpp
index 62a26b3..1152007 100644
--- a/fastboot/bootimg_utils.cpp
+++ b/fastboot/bootimg_utils.cpp
@@ -28,52 +28,50 @@
#include "bootimg_utils.h"
-#include "fastboot.h"
+#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-void bootimg_set_cmdline(boot_img_hdr* h, const char* cmdline) {
- if (strlen(cmdline) >= sizeof(h->cmdline)) die("command line too large: %zu", strlen(cmdline));
- strcpy(reinterpret_cast<char*>(h->cmdline), cmdline);
+void bootimg_set_cmdline(boot_img_hdr_v1* h, const std::string& cmdline) {
+ if (cmdline.size() >= sizeof(h->cmdline)) die("command line too large: %zu", cmdline.size());
+ strcpy(reinterpret_cast<char*>(h->cmdline), cmdline.c_str());
}
-boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset,
- void* ramdisk, int64_t ramdisk_size, off_t ramdisk_offset,
- void* second, int64_t second_size, off_t second_offset,
- size_t page_size, size_t base, off_t tags_offset,
- int64_t* bootimg_size)
-{
- size_t page_mask = page_size - 1;
+boot_img_hdr_v1* mkbootimg(void* kernel, int64_t kernel_size, void* ramdisk, int64_t ramdisk_size,
+ void* second, int64_t second_size, size_t base,
+ const boot_img_hdr_v1& src, int64_t* bootimg_size) {
+ const size_t page_mask = src.page_size - 1;
+ int64_t header_actual = (sizeof(boot_img_hdr_v1) + page_mask) & (~page_mask);
int64_t kernel_actual = (kernel_size + page_mask) & (~page_mask);
int64_t ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask);
int64_t second_actual = (second_size + page_mask) & (~page_mask);
- *bootimg_size = page_size + kernel_actual + ramdisk_actual + second_actual;
+ *bootimg_size = header_actual + kernel_actual + ramdisk_actual + second_actual;
- boot_img_hdr* hdr = reinterpret_cast<boot_img_hdr*>(calloc(*bootimg_size, 1));
- if (hdr == nullptr) {
- return hdr;
- }
+ boot_img_hdr_v1* hdr = reinterpret_cast<boot_img_hdr_v1*>(calloc(*bootimg_size, 1));
+ if (hdr == nullptr) die("couldn't allocate boot image: %" PRId64 " bytes", *bootimg_size);
+ *hdr = src;
memcpy(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
hdr->kernel_size = kernel_size;
hdr->ramdisk_size = ramdisk_size;
hdr->second_size = second_size;
- hdr->kernel_addr = base + kernel_offset;
- hdr->ramdisk_addr = base + ramdisk_offset;
- hdr->second_addr = base + second_offset;
- hdr->tags_addr = base + tags_offset;
+ hdr->kernel_addr += base;
+ hdr->ramdisk_addr += base;
+ hdr->second_addr += base;
+ hdr->tags_addr += base;
- hdr->page_size = page_size;
+ if (hdr->header_version != 0) {
+ hdr->header_size = sizeof(boot_img_hdr_v1);
+ }
- memcpy(hdr->magic + page_size, kernel, kernel_size);
- memcpy(hdr->magic + page_size + kernel_actual, ramdisk, ramdisk_size);
- memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, second, second_size);
-
+ memcpy(hdr->magic + hdr->page_size, kernel, kernel_size);
+ memcpy(hdr->magic + hdr->page_size + kernel_actual, ramdisk, ramdisk_size);
+ memcpy(hdr->magic + hdr->page_size + kernel_actual + ramdisk_actual, second, second_size);
return hdr;
}
diff --git a/fastboot/bootimg_utils.h b/fastboot/bootimg_utils.h
index fcc8662..fe805b0 100644
--- a/fastboot/bootimg_utils.h
+++ b/fastboot/bootimg_utils.h
@@ -26,18 +26,15 @@
* SUCH DAMAGE.
*/
-#ifndef _FASTBOOT_BOOTIMG_UTILS_H_
-#define _FASTBOOT_BOOTIMG_UTILS_H_
+#pragma once
#include <bootimg.h>
#include <inttypes.h>
#include <sys/types.h>
-void bootimg_set_cmdline(boot_img_hdr* h, const char* cmdline);
-boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset,
- void* ramdisk, int64_t ramdisk_size, off_t ramdisk_offset,
- void* second, int64_t second_size, off_t second_offset,
- size_t page_size, size_t base, off_t tags_offset,
- int64_t* bootimg_size);
+#include <string>
-#endif
+boot_img_hdr_v1* mkbootimg(void* kernel, int64_t kernel_size, void* ramdisk, int64_t ramdisk_size,
+ void* second, int64_t second_size, size_t base,
+ const boot_img_hdr_v1& src, int64_t* bootimg_size);
+void bootimg_set_cmdline(boot_img_hdr_v1* h, const std::string& cmdline);
diff --git a/fastboot/constants.h b/fastboot/constants.h
new file mode 100644
index 0000000..43daab0
--- /dev/null
+++ b/fastboot/constants.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+#pragma once
+
+#define FB_CMD_GETVAR "getvar"
+#define FB_CMD_DOWNLOAD "download"
+#define FB_CMD_UPLOAD "upload"
+#define FB_CMD_VERIFY "verify"
+#define FB_CMD_FLASH "flash"
+#define FB_CMD_ERASE "erase"
+#define FB_CMD_BOOT "boot"
+#define FB_CMD_SET_ACTIVE "set_active"
+#define FB_CMD_CONTINUE "continue"
+#define FB_CMD_REBOOT "reboot"
+#define FB_CMD_SHUTDOWN "shutdown"
+#define FB_CMD_REBOOT_BOOTLOADER "reboot-bootloader"
+#define FB_CMD_REBOOT_RECOVERY "reboot-recovery"
+#define FB_CMD_REBOOT_FASTBOOT "reboot-fastboot"
+#define FB_CMD_POWERDOWN "powerdown"
+#define FB_CMD_CREATE_PARTITION "create-logical-partition"
+#define FB_CMD_DELETE_PARTITION "delete-logical-partition"
+#define FB_CMD_RESIZE_PARTITION "resize-logical-partition"
+
+#define RESPONSE_OKAY "OKAY"
+#define RESPONSE_FAIL "FAIL"
+#define RESPONSE_DATA "DATA"
+#define RESPONSE_INFO "INFO"
+
+#define FB_COMMAND_SZ 64
+#define FB_RESPONSE_SZ 64
+
+#define FB_VAR_VERSION "version"
+#define FB_VAR_VERSION_BOOTLOADER "version-bootloader"
+#define FB_VAR_VERSION_BASEBAND "version-baseband"
+#define FB_VAR_PRODUCT "product"
+#define FB_VAR_SERIALNO "serialno"
+#define FB_VAR_SECURE "secure"
+#define FB_VAR_UNLOCKED "unlocked"
+#define FB_VAR_CURRENT_SLOT "current-slot"
+#define FB_VAR_MAX_DOWNLOAD_SIZE "max-download-size"
+#define FB_VAR_HAS_SLOT "has-slot"
+#define FB_VAR_SLOT_COUNT "slot-count"
+#define FB_VAR_PARTITION_SIZE "partition-size"
+#define FB_VAR_SLOT_SUCCESSFUL "slot-successful"
+#define FB_VAR_SLOT_UNBOOTABLE "slot-unbootable"
+#define FB_VAR_IS_LOGICAL "is-logical"
+#define FB_VAR_IS_USERSPACE "is-userspace"
diff --git a/fastboot/device/commands.cpp b/fastboot/device/commands.cpp
new file mode 100644
index 0000000..690bfa8
--- /dev/null
+++ b/fastboot/device/commands.cpp
@@ -0,0 +1,338 @@
+/*
+ * Copyright (C) 2018 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 "commands.h"
+
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
+#include <cutils/android_reboot.h>
+#include <ext4_utils/wipe.h>
+#include <liblp/builder.h>
+#include <liblp/liblp.h>
+#include <uuid/uuid.h>
+
+#include "constants.h"
+#include "fastboot_device.h"
+#include "flashing.h"
+#include "utility.h"
+
+using ::android::hardware::hidl_string;
+using ::android::hardware::boot::V1_0::BoolResult;
+using ::android::hardware::boot::V1_0::CommandResult;
+using ::android::hardware::boot::V1_0::Slot;
+using namespace android::fs_mgr;
+
+bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) {
+ using VariableHandler = std::function<bool(FastbootDevice*, const std::vector<std::string>&)>;
+ const std::unordered_map<std::string, VariableHandler> kVariableMap = {
+ {FB_VAR_VERSION, GetVersion},
+ {FB_VAR_VERSION_BOOTLOADER, GetBootloaderVersion},
+ {FB_VAR_VERSION_BASEBAND, GetBasebandVersion},
+ {FB_VAR_PRODUCT, GetProduct},
+ {FB_VAR_SERIALNO, GetSerial},
+ {FB_VAR_SECURE, GetSecure},
+ {FB_VAR_UNLOCKED, GetUnlocked},
+ {FB_VAR_MAX_DOWNLOAD_SIZE, GetMaxDownloadSize},
+ {FB_VAR_CURRENT_SLOT, ::GetCurrentSlot},
+ {FB_VAR_SLOT_COUNT, GetSlotCount},
+ {FB_VAR_HAS_SLOT, GetHasSlot},
+ {FB_VAR_SLOT_SUCCESSFUL, GetSlotSuccessful},
+ {FB_VAR_SLOT_UNBOOTABLE, GetSlotUnbootable},
+ {FB_VAR_PARTITION_SIZE, GetPartitionSize},
+ {FB_VAR_IS_LOGICAL, GetPartitionIsLogical},
+ {FB_VAR_IS_USERSPACE, GetIsUserspace}};
+
+ // args[0] is command name, args[1] is variable.
+ auto found_variable = kVariableMap.find(args[1]);
+ if (found_variable == kVariableMap.end()) {
+ return device->WriteStatus(FastbootResult::FAIL, "Unknown variable");
+ }
+
+ std::vector<std::string> getvar_args(args.begin() + 2, args.end());
+ return found_variable->second(device, getvar_args);
+}
+
+bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.size() < 2) {
+ return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
+ }
+ PartitionHandle handle;
+ if (!OpenPartition(device, args[1], &handle)) {
+ return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
+ }
+ if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
+ return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
+ }
+ return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
+}
+
+bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.size() < 2) {
+ return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
+ }
+ // arg[0] is the command name, arg[1] contains size of data to be downloaded
+ unsigned int size;
+ if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) {
+ return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
+ }
+ device->download_data().resize(size);
+ if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
+ return false;
+ }
+
+ if (device->HandleData(true, &device->download_data())) {
+ return device->WriteStatus(FastbootResult::OKAY, "");
+ }
+
+ PLOG(ERROR) << "Couldn't download data";
+ return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
+}
+
+bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.size() < 2) {
+ return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
+ }
+ int ret = Flash(device, args[1]);
+ if (ret < 0) {
+ return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
+ }
+ return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
+}
+
+bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.size() < 2) {
+ return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
+ }
+
+ // Slot suffix needs to be between 'a' and 'z'.
+ Slot slot;
+ if (!GetSlotNumber(args[1], &slot)) {
+ return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
+ }
+
+ // Non-A/B devices will not have a boot control HAL.
+ auto boot_control_hal = device->boot_control_hal();
+ if (!boot_control_hal) {
+ return device->WriteStatus(FastbootResult::FAIL,
+ "Cannot set slot: boot control HAL absent");
+ }
+ if (slot >= boot_control_hal->getNumberSlots()) {
+ return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
+ }
+ CommandResult ret;
+ auto cb = [&ret](CommandResult result) { ret = result; };
+ auto result = boot_control_hal->setActiveBootSlot(slot, cb);
+ if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, "");
+ return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
+}
+
+bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
+ android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
+ device->CloseDevice();
+ TEMP_FAILURE_RETRY(pause());
+ return result;
+}
+
+bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
+ android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
+ device->CloseDevice();
+ TEMP_FAILURE_RETRY(pause());
+ return result;
+}
+
+bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
+ android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
+ device->CloseDevice();
+ TEMP_FAILURE_RETRY(pause());
+ return result;
+}
+
+bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
+ android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
+ device->CloseDevice();
+ TEMP_FAILURE_RETRY(pause());
+ return result;
+}
+
+static bool EnterRecovery() {
+ const char msg_switch_to_recovery = 'r';
+
+ android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
+ if (sock < 0) {
+ PLOG(ERROR) << "Couldn't create sock";
+ return false;
+ }
+
+ struct sockaddr_un addr = {.sun_family = AF_UNIX};
+ strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
+ if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
+ PLOG(ERROR) << "Couldn't connect to recovery";
+ return false;
+ }
+ // Switch to recovery will not update the boot reason since it does not
+ // require a reboot.
+ auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
+ if (ret != sizeof(msg_switch_to_recovery)) {
+ PLOG(ERROR) << "Couldn't write message to switch to recovery";
+ return false;
+ }
+
+ return true;
+}
+
+bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ auto status = true;
+ if (EnterRecovery()) {
+ status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
+ } else {
+ status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
+ }
+ device->CloseDevice();
+ TEMP_FAILURE_RETRY(pause());
+ return status;
+}
+
+// Helper class for opening a handle to a MetadataBuilder and writing the new
+// partition table to the same place it was read.
+class PartitionBuilder {
+ public:
+ explicit PartitionBuilder(FastbootDevice* device);
+
+ bool Write();
+ bool Valid() const { return !!builder_; }
+ MetadataBuilder* operator->() const { return builder_.get(); }
+
+ private:
+ std::string super_device_;
+ uint32_t slot_number_;
+ std::unique_ptr<MetadataBuilder> builder_;
+};
+
+PartitionBuilder::PartitionBuilder(FastbootDevice* device) {
+ auto super_device = FindPhysicalPartition(LP_METADATA_PARTITION_NAME);
+ if (!super_device) {
+ return;
+ }
+ super_device_ = *super_device;
+
+ std::string slot = device->GetCurrentSlot();
+ slot_number_ = SlotNumberForSlotSuffix(slot);
+ builder_ = MetadataBuilder::New(super_device_, slot_number_);
+}
+
+bool PartitionBuilder::Write() {
+ std::unique_ptr<LpMetadata> metadata = builder_->Export();
+ if (!metadata) {
+ return false;
+ }
+ return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_);
+}
+
+bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.size() < 3) {
+ return device->WriteFail("Invalid partition name and size");
+ }
+
+ uint64_t partition_size;
+ std::string partition_name = args[1];
+ if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
+ return device->WriteFail("Invalid partition size");
+ }
+
+ PartitionBuilder builder(device);
+ if (!builder.Valid()) {
+ return device->WriteFail("Could not open super partition");
+ }
+ // TODO(112433293) Disallow if the name is in the physical table as well.
+ if (builder->FindPartition(partition_name)) {
+ return device->WriteFail("Partition already exists");
+ }
+
+ // Make a random UUID, since they're not currently used.
+ uuid_t uuid;
+ char uuid_str[37];
+ uuid_generate_random(uuid);
+ uuid_unparse(uuid, uuid_str);
+
+ Partition* partition = builder->AddPartition(partition_name, uuid_str, 0);
+ if (!partition) {
+ return device->WriteFail("Failed to add partition");
+ }
+ if (!builder->ResizePartition(partition, partition_size)) {
+ builder->RemovePartition(partition_name);
+ return device->WriteFail("Not enough space for partition");
+ }
+ if (!builder.Write()) {
+ return device->WriteFail("Failed to write partition table");
+ }
+ return device->WriteOkay("Partition created");
+}
+
+bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.size() < 2) {
+ return device->WriteFail("Invalid partition name and size");
+ }
+
+ PartitionBuilder builder(device);
+ if (!builder.Valid()) {
+ return device->WriteFail("Could not open super partition");
+ }
+ builder->RemovePartition(args[1]);
+ if (!builder.Write()) {
+ return device->WriteFail("Failed to write partition table");
+ }
+ return device->WriteOkay("Partition deleted");
+}
+
+bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.size() < 3) {
+ return device->WriteFail("Invalid partition name and size");
+ }
+
+ uint64_t partition_size;
+ std::string partition_name = args[1];
+ if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
+ return device->WriteFail("Invalid partition size");
+ }
+
+ PartitionBuilder builder(device);
+ if (!builder.Valid()) {
+ return device->WriteFail("Could not open super partition");
+ }
+
+ Partition* partition = builder->FindPartition(partition_name);
+ if (!partition) {
+ return device->WriteFail("Partition does not exist");
+ }
+ if (!builder->ResizePartition(partition, partition_size)) {
+ return device->WriteFail("Not enough space to resize partition");
+ }
+ if (!builder.Write()) {
+ return device->WriteFail("Failed to write partition table");
+ }
+ return device->WriteOkay("Partition resized");
+}
diff --git a/fastboot/device/commands.h b/fastboot/device/commands.h
new file mode 100644
index 0000000..f67df91
--- /dev/null
+++ b/fastboot/device/commands.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+#pragma once
+
+#include <functional>
+#include <string>
+#include <vector>
+
+class FastbootDevice;
+
+enum class FastbootResult {
+ OKAY,
+ FAIL,
+ INFO,
+ DATA,
+};
+
+// Execute a command with the given arguments (possibly empty).
+using CommandHandler = std::function<bool(FastbootDevice*, const std::vector<std::string>&)>;
+
+bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args);
+bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args);
diff --git a/fastboot/device/fastboot_device.cpp b/fastboot/device/fastboot_device.cpp
new file mode 100644
index 0000000..c306e67
--- /dev/null
+++ b/fastboot/device/fastboot_device.cpp
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2018 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 "fastboot_device.h"
+
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+#include <android/hardware/boot/1.0/IBootControl.h>
+
+#include <algorithm>
+
+#include "constants.h"
+#include "flashing.h"
+#include "usb_client.h"
+
+using ::android::hardware::hidl_string;
+using ::android::hardware::boot::V1_0::IBootControl;
+using ::android::hardware::boot::V1_0::Slot;
+namespace sph = std::placeholders;
+
+FastbootDevice::FastbootDevice()
+ : kCommandMap({
+ {FB_CMD_SET_ACTIVE, SetActiveHandler},
+ {FB_CMD_DOWNLOAD, DownloadHandler},
+ {FB_CMD_GETVAR, GetVarHandler},
+ {FB_CMD_SHUTDOWN, ShutDownHandler},
+ {FB_CMD_REBOOT, RebootHandler},
+ {FB_CMD_REBOOT_BOOTLOADER, RebootBootloaderHandler},
+ {FB_CMD_REBOOT_FASTBOOT, RebootFastbootHandler},
+ {FB_CMD_REBOOT_RECOVERY, RebootRecoveryHandler},
+ {FB_CMD_ERASE, EraseHandler},
+ {FB_CMD_FLASH, FlashHandler},
+ {FB_CMD_CREATE_PARTITION, CreatePartitionHandler},
+ {FB_CMD_DELETE_PARTITION, DeletePartitionHandler},
+ {FB_CMD_RESIZE_PARTITION, ResizePartitionHandler},
+ }),
+ transport_(std::make_unique<ClientUsbTransport>()),
+ boot_control_hal_(IBootControl::getService()) {}
+
+FastbootDevice::~FastbootDevice() {
+ CloseDevice();
+}
+
+void FastbootDevice::CloseDevice() {
+ transport_->Close();
+}
+
+std::string FastbootDevice::GetCurrentSlot() {
+ // Non-A/B devices must not have boot control HALs.
+ if (!boot_control_hal_) {
+ return "";
+ }
+ std::string suffix;
+ auto cb = [&suffix](hidl_string s) { suffix = s; };
+ boot_control_hal_->getSuffix(boot_control_hal_->getCurrentSlot(), cb);
+ return suffix;
+}
+
+bool FastbootDevice::WriteStatus(FastbootResult result, const std::string& message) {
+ constexpr size_t kResponseReasonSize = 4;
+ constexpr size_t kNumResponseTypes = 4; // "FAIL", "OKAY", "INFO", "DATA"
+
+ char buf[FB_RESPONSE_SZ];
+ constexpr size_t kMaxMessageSize = sizeof(buf) - kResponseReasonSize;
+ size_t msg_len = std::min(kMaxMessageSize, message.size());
+
+ constexpr const char* kResultStrings[kNumResponseTypes] = {RESPONSE_OKAY, RESPONSE_FAIL,
+ RESPONSE_INFO, RESPONSE_DATA};
+
+ if (static_cast<size_t>(result) >= kNumResponseTypes) {
+ return false;
+ }
+
+ memcpy(buf, kResultStrings[static_cast<size_t>(result)], kResponseReasonSize);
+ memcpy(buf + kResponseReasonSize, message.c_str(), msg_len);
+
+ size_t response_len = kResponseReasonSize + msg_len;
+ auto write_ret = this->get_transport()->Write(buf, response_len);
+ if (write_ret != static_cast<ssize_t>(response_len)) {
+ PLOG(ERROR) << "Failed to write " << message;
+ return false;
+ }
+
+ return true;
+}
+
+bool FastbootDevice::HandleData(bool read, std::vector<char>* data) {
+ auto read_write_data_size = read ? this->get_transport()->Read(data->data(), data->size())
+ : this->get_transport()->Write(data->data(), data->size());
+ if (read_write_data_size == -1 || static_cast<size_t>(read_write_data_size) != data->size()) {
+ return false;
+ }
+ return true;
+}
+
+void FastbootDevice::ExecuteCommands() {
+ char command[FB_RESPONSE_SZ + 1];
+ for (;;) {
+ auto bytes_read = transport_->Read(command, FB_RESPONSE_SZ);
+ if (bytes_read == -1) {
+ PLOG(ERROR) << "Couldn't read command";
+ return;
+ }
+ command[bytes_read] = '\0';
+
+ LOG(INFO) << "Fastboot command: " << command;
+ auto args = android::base::Split(command, ":");
+ auto found_command = kCommandMap.find(args[0]);
+ if (found_command == kCommandMap.end()) {
+ WriteStatus(FastbootResult::FAIL, "Unrecognized command");
+ continue;
+ }
+ if (!found_command->second(this, args)) {
+ return;
+ }
+ }
+}
+
+bool FastbootDevice::WriteOkay(const std::string& message) {
+ return WriteStatus(FastbootResult::OKAY, message);
+}
+
+bool FastbootDevice::WriteFail(const std::string& message) {
+ return WriteStatus(FastbootResult::FAIL, message);
+}
diff --git a/fastboot/device/fastboot_device.h b/fastboot/device/fastboot_device.h
new file mode 100644
index 0000000..addc2ef
--- /dev/null
+++ b/fastboot/device/fastboot_device.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+#include <unordered_map>
+#include <utility>
+#include <vector>
+
+#include <android/hardware/boot/1.0/IBootControl.h>
+
+#include "commands.h"
+#include "transport.h"
+#include "variables.h"
+
+class FastbootDevice {
+ public:
+ FastbootDevice();
+ ~FastbootDevice();
+
+ void CloseDevice();
+ void ExecuteCommands();
+ bool WriteStatus(FastbootResult result, const std::string& message);
+ bool HandleData(bool read, std::vector<char>* data);
+ std::string GetCurrentSlot();
+
+ // Shortcuts for writing OKAY and FAIL status results.
+ bool WriteOkay(const std::string& message);
+ bool WriteFail(const std::string& message);
+
+ std::vector<char>& download_data() { return download_data_; }
+ Transport* get_transport() { return transport_.get(); }
+ android::sp<android::hardware::boot::V1_0::IBootControl> boot_control_hal() {
+ return boot_control_hal_;
+ }
+
+ private:
+ const std::unordered_map<std::string, CommandHandler> kCommandMap;
+
+ std::unique_ptr<Transport> transport_;
+ android::sp<android::hardware::boot::V1_0::IBootControl> boot_control_hal_;
+ std::vector<char> download_data_;
+};
diff --git a/fastboot/device/flashing.cpp b/fastboot/device/flashing.cpp
new file mode 100644
index 0000000..b5ed170
--- /dev/null
+++ b/fastboot/device/flashing.cpp
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2018 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 "flashing.h"
+
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <memory>
+
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+#include <ext4_utils/ext4_utils.h>
+#include <sparse/sparse.h>
+
+#include "fastboot_device.h"
+#include "utility.h"
+
+namespace {
+
+constexpr uint32_t SPARSE_HEADER_MAGIC = 0xed26ff3a;
+
+} // namespace
+
+int FlashRawDataChunk(int fd, const char* data, size_t len) {
+ size_t ret = 0;
+ while (ret < len) {
+ int this_len = std::min(static_cast<size_t>(1048576UL * 8), len - ret);
+ int this_ret = write(fd, data, this_len);
+ if (this_ret < 0) {
+ PLOG(ERROR) << "Failed to flash data of len " << len;
+ return -1;
+ }
+ data += this_ret;
+ ret += this_ret;
+ }
+ return 0;
+}
+
+int FlashRawData(int fd, const std::vector<char>& downloaded_data) {
+ int ret = FlashRawDataChunk(fd, downloaded_data.data(), downloaded_data.size());
+ if (ret < 0) {
+ return -errno;
+ }
+ return ret;
+}
+
+int WriteCallback(void* priv, const void* data, size_t len) {
+ int fd = reinterpret_cast<long long>(priv);
+ if (!data) {
+ return lseek64(fd, len, SEEK_CUR) >= 0 ? 0 : -errno;
+ }
+ return FlashRawDataChunk(fd, reinterpret_cast<const char*>(data), len);
+}
+
+int FlashSparseData(int fd, std::vector<char>& downloaded_data) {
+ struct sparse_file* file = sparse_file_import_buf(downloaded_data.data(), true, false);
+ if (!file) {
+ return -ENOENT;
+ }
+ return sparse_file_callback(file, false, false, WriteCallback, reinterpret_cast<void*>(fd));
+}
+
+int FlashBlockDevice(int fd, std::vector<char>& downloaded_data) {
+ lseek64(fd, 0, SEEK_SET);
+ if (downloaded_data.size() >= sizeof(SPARSE_HEADER_MAGIC) &&
+ *reinterpret_cast<uint32_t*>(downloaded_data.data()) == SPARSE_HEADER_MAGIC) {
+ return FlashSparseData(fd, downloaded_data);
+ } else {
+ return FlashRawData(fd, downloaded_data);
+ }
+}
+
+int Flash(FastbootDevice* device, const std::string& partition_name) {
+ PartitionHandle handle;
+ if (!OpenPartition(device, partition_name, &handle)) {
+ return -ENOENT;
+ }
+
+ std::vector<char> data = std::move(device->download_data());
+ if (data.size() == 0) {
+ return -EINVAL;
+ } else if (data.size() > get_block_device_size(handle.fd())) {
+ return -EOVERFLOW;
+ }
+ return FlashBlockDevice(handle.fd(), data);
+}
diff --git a/adb/remount_service.h b/fastboot/device/flashing.h
similarity index 73%
copy from adb/remount_service.h
copy to fastboot/device/flashing.h
index 7bda1be..206a407 100644
--- a/adb/remount_service.h
+++ b/fastboot/device/flashing.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,12 +14,11 @@
* limitations under the License.
*/
-#ifndef _REMOUNT_SERVICE_H_
-#define _REMOUNT_SERVICE_H_
+#pragma once
#include <string>
+#include <vector>
-bool make_block_device_writable(const std::string&);
-void remount_service(int, void*);
+class FastbootDevice;
-#endif
+int Flash(FastbootDevice* device, const std::string& partition_name);
diff --git a/init/watchdogd.h b/fastboot/device/main.cpp
similarity index 63%
copy from init/watchdogd.h
copy to fastboot/device/main.cpp
index 73f77d5..df9c900 100644
--- a/init/watchdogd.h
+++ b/fastboot/device/main.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2012 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,15 +14,15 @@
* limitations under the License.
*/
-#ifndef _INIT_WATCHDOGD_H_
-#define _INIT_WATCHDOGD_H_
+#include <android-base/logging.h>
-namespace android {
-namespace init {
+#include "fastboot_device.h"
-int watchdogd_main(int argc, char **argv);
+int main(int /*argc*/, char* argv[]) {
+ android::base::InitLogging(argv, &android::base::KernelLogger);
-} // namespace init
-} // namespace android
-
-#endif
+ while (true) {
+ FastbootDevice device;
+ device.ExecuteCommands();
+ }
+}
diff --git a/fastboot/device/usb_client.cpp b/fastboot/device/usb_client.cpp
new file mode 100644
index 0000000..215f99a
--- /dev/null
+++ b/fastboot/device/usb_client.cpp
@@ -0,0 +1,307 @@
+/*
+ * Copyright (C) 2018 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 "usb_client.h"
+
+#include <endian.h>
+#include <fcntl.h>
+#include <linux/usb/ch9.h>
+#include <linux/usb/functionfs.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <android-base/logging.h>
+#include <android-base/properties.h>
+
+constexpr int kMaxPacketSizeFs = 64;
+constexpr int kMaxPacketSizeHs = 512;
+constexpr int kMaxPacketsizeSs = 1024;
+
+constexpr size_t kFbFfsNumBufs = 16;
+constexpr size_t kFbFfsBufSize = 32768;
+
+constexpr const char* kUsbFfsFastbootEp0 = "/dev/usb-ffs/fastboot/ep0";
+constexpr const char* kUsbFfsFastbootOut = "/dev/usb-ffs/fastboot/ep1";
+constexpr const char* kUsbFfsFastbootIn = "/dev/usb-ffs/fastboot/ep2";
+
+struct FuncDesc {
+ struct usb_interface_descriptor intf;
+ struct usb_endpoint_descriptor_no_audio source;
+ struct usb_endpoint_descriptor_no_audio sink;
+} __attribute__((packed));
+
+struct SsFuncDesc {
+ struct usb_interface_descriptor intf;
+ struct usb_endpoint_descriptor_no_audio source;
+ struct usb_ss_ep_comp_descriptor source_comp;
+ struct usb_endpoint_descriptor_no_audio sink;
+ struct usb_ss_ep_comp_descriptor sink_comp;
+} __attribute__((packed));
+
+struct DescV2 {
+ struct usb_functionfs_descs_head_v2 header;
+ // The rest of the structure depends on the flags in the header.
+ __le32 fs_count;
+ __le32 hs_count;
+ __le32 ss_count;
+ struct FuncDesc fs_descs, hs_descs;
+ struct SsFuncDesc ss_descs;
+} __attribute__((packed));
+
+struct usb_interface_descriptor fastboot_interface = {
+ .bLength = USB_DT_INTERFACE_SIZE,
+ .bDescriptorType = USB_DT_INTERFACE,
+ .bInterfaceNumber = 0,
+ .bNumEndpoints = 2,
+ .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
+ .bInterfaceSubClass = 66,
+ .bInterfaceProtocol = 3,
+ .iInterface = 1, /* first string from the provided table */
+};
+
+static struct FuncDesc fs_descriptors = {
+ .intf = fastboot_interface,
+ .source =
+ {
+ .bLength = sizeof(fs_descriptors.source),
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 1 | USB_DIR_OUT,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = kMaxPacketSizeFs,
+ },
+ .sink =
+ {
+ .bLength = sizeof(fs_descriptors.sink),
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 1 | USB_DIR_IN,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = kMaxPacketSizeFs,
+ },
+};
+
+static struct FuncDesc hs_descriptors = {
+ .intf = fastboot_interface,
+ .source =
+ {
+ .bLength = sizeof(hs_descriptors.source),
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 1 | USB_DIR_OUT,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = kMaxPacketSizeHs,
+ },
+ .sink =
+ {
+ .bLength = sizeof(hs_descriptors.sink),
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 1 | USB_DIR_IN,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = kMaxPacketSizeHs,
+ },
+};
+
+static struct SsFuncDesc ss_descriptors = {
+ .intf = fastboot_interface,
+ .source =
+ {
+ .bLength = sizeof(ss_descriptors.source),
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 1 | USB_DIR_OUT,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = kMaxPacketsizeSs,
+ },
+ .source_comp =
+ {
+ .bLength = sizeof(ss_descriptors.source_comp),
+ .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
+ .bMaxBurst = 15,
+ },
+ .sink =
+ {
+ .bLength = sizeof(ss_descriptors.sink),
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 1 | USB_DIR_IN,
+ .bmAttributes = USB_ENDPOINT_XFER_BULK,
+ .wMaxPacketSize = kMaxPacketsizeSs,
+ },
+ .sink_comp =
+ {
+ .bLength = sizeof(ss_descriptors.sink_comp),
+ .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
+ .bMaxBurst = 15,
+ },
+};
+
+#define STR_INTERFACE_ "fastboot"
+
+static const struct {
+ struct usb_functionfs_strings_head header;
+ struct {
+ __le16 code;
+ const char str1[sizeof(STR_INTERFACE_)];
+ } __attribute__((packed)) lang0;
+} __attribute__((packed)) strings = {
+ .header =
+ {
+ .magic = htole32(FUNCTIONFS_STRINGS_MAGIC),
+ .length = htole32(sizeof(strings)),
+ .str_count = htole32(1),
+ .lang_count = htole32(1),
+ },
+ .lang0 =
+ {
+ htole16(0x0409), /* en-us */
+ STR_INTERFACE_,
+ },
+};
+
+static struct DescV2 v2_descriptor = {
+ .header =
+ {
+ .magic = htole32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
+ .length = htole32(sizeof(v2_descriptor)),
+ .flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
+ FUNCTIONFS_HAS_SS_DESC,
+ },
+ .fs_count = 3,
+ .hs_count = 3,
+ .ss_count = 5,
+ .fs_descs = fs_descriptors,
+ .hs_descs = hs_descriptors,
+ .ss_descs = ss_descriptors,
+};
+
+// Reimplementing since usb_ffs_close() does not close the control FD.
+static void CloseFunctionFs(usb_handle* h) {
+ if (h->bulk_in > 0) {
+ close(h->bulk_in);
+ h->bulk_in = -1;
+ }
+ if (h->bulk_out > 0) {
+ close(h->bulk_out);
+ h->bulk_out = -1;
+ }
+ if (h->control > 0) {
+ close(h->control);
+ h->control = -1;
+ }
+}
+
+static bool InitFunctionFs(usb_handle* h) {
+ LOG(INFO) << "initializing functionfs";
+
+ if (h->control < 0) { // might have already done this before
+ LOG(INFO) << "opening control endpoint " << kUsbFfsFastbootEp0;
+ h->control = open(kUsbFfsFastbootEp0, O_RDWR);
+ if (h->control < 0) {
+ PLOG(ERROR) << "cannot open control endpoint " << kUsbFfsFastbootEp0;
+ goto err;
+ }
+
+ auto ret = write(h->control, &v2_descriptor, sizeof(v2_descriptor));
+ if (ret < 0) {
+ PLOG(ERROR) << "cannot write descriptors " << kUsbFfsFastbootEp0;
+ goto err;
+ }
+
+ ret = write(h->control, &strings, sizeof(strings));
+ if (ret < 0) {
+ PLOG(ERROR) << "cannot write strings " << kUsbFfsFastbootEp0;
+ goto err;
+ }
+ // Signal only when writing the descriptors to ffs
+ android::base::SetProperty("sys.usb.ffs.ready", "1");
+ }
+
+ h->bulk_out = open(kUsbFfsFastbootOut, O_RDONLY);
+ if (h->bulk_out < 0) {
+ PLOG(ERROR) << "cannot open bulk-out endpoint " << kUsbFfsFastbootOut;
+ goto err;
+ }
+
+ h->bulk_in = open(kUsbFfsFastbootIn, O_WRONLY);
+ if (h->bulk_in < 0) {
+ PLOG(ERROR) << "cannot open bulk-in endpoint " << kUsbFfsFastbootIn;
+ goto err;
+ }
+
+ h->read_aiob.fd = h->bulk_out;
+ h->write_aiob.fd = h->bulk_in;
+ h->reads_zero_packets = false;
+ return true;
+
+err:
+ CloseFunctionFs(h);
+ return false;
+}
+
+ClientUsbTransport::ClientUsbTransport()
+ : handle_(std::unique_ptr<usb_handle>(create_usb_handle(kFbFfsNumBufs, kFbFfsBufSize))) {
+ if (!InitFunctionFs(handle_.get())) {
+ handle_.reset(nullptr);
+ }
+}
+
+ssize_t ClientUsbTransport::Read(void* data, size_t len) {
+ if (handle_ == nullptr || len > SSIZE_MAX) {
+ return -1;
+ }
+ char* char_data = static_cast<char*>(data);
+ size_t bytes_read_total = 0;
+ while (bytes_read_total < len) {
+ auto bytes_to_read = std::min(len - bytes_read_total, kFbFfsNumBufs * kFbFfsBufSize);
+ auto bytes_read_now = handle_->read(handle_.get(), char_data, bytes_to_read);
+ if (bytes_read_now < 0) {
+ return bytes_read_total;
+ }
+ bytes_read_total += bytes_read_now;
+ char_data += bytes_read_now;
+ if (static_cast<size_t>(bytes_read_now) < bytes_to_read) {
+ break;
+ }
+ }
+ return bytes_read_total;
+}
+
+ssize_t ClientUsbTransport::Write(const void* data, size_t len) {
+ if (handle_ == nullptr || len > SSIZE_MAX) {
+ return -1;
+ }
+ const char* char_data = reinterpret_cast<const char*>(data);
+ size_t bytes_written_total = 0;
+ while (bytes_written_total < len) {
+ auto bytes_to_write = std::min(len - bytes_written_total, kFbFfsNumBufs * kFbFfsBufSize);
+ auto bytes_written_now = handle_->write(handle_.get(), data, bytes_to_write);
+ if (bytes_written_now < 0) {
+ return bytes_written_total;
+ }
+ bytes_written_total += bytes_written_now;
+ char_data += bytes_written_now;
+ if (static_cast<size_t>(bytes_written_now) < bytes_to_write) {
+ break;
+ }
+ }
+ return bytes_written_total;
+}
+
+int ClientUsbTransport::Close() {
+ if (handle_ == nullptr) {
+ return -1;
+ }
+ CloseFunctionFs(handle_.get());
+ return 0;
+}
diff --git a/fastboot/device/usb_client.h b/fastboot/device/usb_client.h
new file mode 100644
index 0000000..3694f9a
--- /dev/null
+++ b/fastboot/device/usb_client.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+#pragma once
+
+#include <memory>
+
+#include <adbd/usb.h>
+
+#include "transport.h"
+
+class ClientUsbTransport : public Transport {
+ public:
+ ClientUsbTransport();
+ ~ClientUsbTransport() override = default;
+
+ ssize_t Read(void* data, size_t len) override;
+ ssize_t Write(const void* data, size_t len) override;
+ int Close() override;
+
+ private:
+ std::unique_ptr<usb_handle> handle_;
+
+ DISALLOW_COPY_AND_ASSIGN(ClientUsbTransport);
+};
diff --git a/fastboot/device/utility.cpp b/fastboot/device/utility.cpp
new file mode 100644
index 0000000..ec84576
--- /dev/null
+++ b/fastboot/device/utility.cpp
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2018 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 "utility.h"
+
+#include <android-base/logging.h>
+#include <fs_mgr_dm_linear.h>
+#include <liblp/liblp.h>
+
+#include "fastboot_device.h"
+
+using namespace android::fs_mgr;
+using android::base::unique_fd;
+using android::hardware::boot::V1_0::Slot;
+
+static bool OpenPhysicalPartition(const std::string& name, PartitionHandle* handle) {
+ std::optional<std::string> path = FindPhysicalPartition(name);
+ if (!path) {
+ return false;
+ }
+ *handle = PartitionHandle(*path);
+ return true;
+}
+
+static bool OpenLogicalPartition(const std::string& name, const std::string& slot,
+ PartitionHandle* handle) {
+ std::optional<std::string> path = FindPhysicalPartition(LP_METADATA_PARTITION_NAME);
+ if (!path) {
+ return false;
+ }
+ uint32_t slot_number = SlotNumberForSlotSuffix(slot);
+ std::string dm_path;
+ if (!CreateLogicalPartition(path->c_str(), slot_number, name, true, &dm_path)) {
+ LOG(ERROR) << "Could not map partition: " << name;
+ return false;
+ }
+ auto closer = [name]() -> void { DestroyLogicalPartition(name); };
+ *handle = PartitionHandle(dm_path, std::move(closer));
+ return true;
+}
+
+bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle) {
+ // We prioritize logical partitions over physical ones, and do this
+ // consistently for other partition operations (like getvar:partition-size).
+ if (LogicalPartitionExists(name, device->GetCurrentSlot())) {
+ if (!OpenLogicalPartition(name, device->GetCurrentSlot(), handle)) {
+ return false;
+ }
+ } else if (!OpenPhysicalPartition(name, handle)) {
+ LOG(ERROR) << "No such partition: " << name;
+ return false;
+ }
+
+ unique_fd fd(TEMP_FAILURE_RETRY(open(handle->path().c_str(), O_WRONLY | O_EXCL)));
+ if (fd < 0) {
+ PLOG(ERROR) << "Failed to open block device: " << handle->path();
+ return false;
+ }
+ handle->set_fd(std::move(fd));
+ return true;
+}
+
+std::optional<std::string> FindPhysicalPartition(const std::string& name) {
+ std::string path = "/dev/block/by-name/" + name;
+ if (access(path.c_str(), R_OK | W_OK) < 0) {
+ return {};
+ }
+ return path;
+}
+
+static const LpMetadataPartition* FindLogicalPartition(const LpMetadata& metadata,
+ const std::string& name) {
+ for (const auto& partition : metadata.partitions) {
+ if (GetPartitionName(partition) == name) {
+ return &partition;
+ }
+ }
+ return nullptr;
+}
+
+bool LogicalPartitionExists(const std::string& name, const std::string& slot_suffix,
+ bool* is_zero_length) {
+ auto path = FindPhysicalPartition(LP_METADATA_PARTITION_NAME);
+ if (!path) {
+ return false;
+ }
+
+ uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
+ std::unique_ptr<LpMetadata> metadata = ReadMetadata(path->c_str(), slot_number);
+ if (!metadata) {
+ return false;
+ }
+ const LpMetadataPartition* partition = FindLogicalPartition(*metadata.get(), name);
+ if (!partition) {
+ return false;
+ }
+ if (is_zero_length) {
+ *is_zero_length = (partition->num_extents == 0);
+ }
+ return true;
+}
+
+bool GetSlotNumber(const std::string& slot, Slot* number) {
+ if (slot.size() != 1) {
+ return false;
+ }
+ if (slot[0] < 'a' || slot[0] > 'z') {
+ return false;
+ }
+ *number = slot[0] - 'a';
+ return true;
+}
diff --git a/fastboot/device/utility.h b/fastboot/device/utility.h
new file mode 100644
index 0000000..0931fc3
--- /dev/null
+++ b/fastboot/device/utility.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+#pragma once
+
+#include <optional>
+#include <string>
+
+#include <android-base/unique_fd.h>
+#include <android/hardware/boot/1.0/IBootControl.h>
+
+// Logical partitions are only mapped to a block device as needed, and
+// immediately unmapped when no longer needed. In order to enforce this we
+// require accessing partitions through a Handle abstraction, which may perform
+// additional operations after closing its file descriptor.
+class PartitionHandle {
+ public:
+ PartitionHandle() {}
+ explicit PartitionHandle(const std::string& path) : path_(path) {}
+ PartitionHandle(const std::string& path, std::function<void()>&& closer)
+ : path_(path), closer_(std::move(closer)) {}
+ PartitionHandle(PartitionHandle&& other) = default;
+ PartitionHandle& operator=(PartitionHandle&& other) = default;
+ ~PartitionHandle() {
+ if (closer_) {
+ // Make sure the device is closed first.
+ fd_ = {};
+ closer_();
+ }
+ }
+ const std::string& path() const { return path_; }
+ int fd() const { return fd_.get(); }
+ void set_fd(android::base::unique_fd&& fd) { fd_ = std::move(fd); }
+
+ private:
+ std::string path_;
+ android::base::unique_fd fd_;
+ std::function<void()> closer_;
+};
+
+class FastbootDevice;
+
+std::optional<std::string> FindPhysicalPartition(const std::string& name);
+bool LogicalPartitionExists(const std::string& name, const std::string& slot_suffix,
+ bool* is_zero_length = nullptr);
+bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle);
+
+bool GetSlotNumber(const std::string& slot, android::hardware::boot::V1_0::Slot* number);
diff --git a/fastboot/device/variables.cpp b/fastboot/device/variables.cpp
new file mode 100644
index 0000000..65cfea3
--- /dev/null
+++ b/fastboot/device/variables.cpp
@@ -0,0 +1,175 @@
+/*
+ * Copyright (C) 2018 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 "variables.h"
+
+#include <inttypes.h>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+#include <ext4_utils/ext4_utils.h>
+
+#include "fastboot_device.h"
+#include "flashing.h"
+#include "utility.h"
+
+using ::android::hardware::boot::V1_0::BoolResult;
+using ::android::hardware::boot::V1_0::Slot;
+
+constexpr int kMaxDownloadSizeDefault = 0x20000000;
+constexpr char kFastbootProtocolVersion[] = "0.4";
+
+bool GetVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ return device->WriteOkay(kFastbootProtocolVersion);
+}
+
+bool GetBootloaderVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ return device->WriteOkay(android::base::GetProperty("ro.bootloader", ""));
+}
+
+bool GetBasebandVersion(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ return device->WriteOkay(android::base::GetProperty("ro.build.expect.baseband", ""));
+}
+
+bool GetProduct(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ return device->WriteOkay(android::base::GetProperty("ro.product.device", ""));
+}
+
+bool GetSerial(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ return device->WriteOkay(android::base::GetProperty("ro.serialno", ""));
+}
+
+bool GetSecure(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ return device->WriteOkay(android::base::GetBoolProperty("ro.secure", "") ? "yes" : "no");
+}
+
+bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ std::string suffix = device->GetCurrentSlot();
+ std::string slot = suffix.size() == 2 ? suffix.substr(1) : suffix;
+ return device->WriteOkay(slot);
+}
+
+bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ auto boot_control_hal = device->boot_control_hal();
+ if (!boot_control_hal) {
+ return "0";
+ }
+ return device->WriteOkay(std::to_string(boot_control_hal->getNumberSlots()));
+}
+
+bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.empty()) {
+ return device->WriteFail("Missing argument");
+ }
+ Slot slot;
+ if (!GetSlotNumber(args[0], &slot)) {
+ return device->WriteFail("Invalid slot");
+ }
+ auto boot_control_hal = device->boot_control_hal();
+ if (!boot_control_hal) {
+ return device->WriteFail("Device has no slots");
+ }
+ if (boot_control_hal->isSlotMarkedSuccessful(slot) != BoolResult::TRUE) {
+ return device->WriteOkay("no");
+ }
+ return device->WriteOkay("yes");
+}
+
+bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.empty()) {
+ return device->WriteFail("Missing argument");
+ }
+ Slot slot;
+ if (!GetSlotNumber(args[0], &slot)) {
+ return device->WriteFail("Invalid slot");
+ }
+ auto boot_control_hal = device->boot_control_hal();
+ if (!boot_control_hal) {
+ return device->WriteFail("Device has no slots");
+ }
+ if (boot_control_hal->isSlotBootable(slot) != BoolResult::TRUE) {
+ return device->WriteOkay("yes");
+ }
+ return device->WriteOkay("no");
+}
+
+bool GetMaxDownloadSize(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ return device->WriteOkay(std::to_string(kMaxDownloadSizeDefault));
+}
+
+bool GetUnlocked(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ return device->WriteOkay("yes");
+}
+
+bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.empty()) {
+ return device->WriteFail("Missing argument");
+ }
+ std::string slot_suffix = device->GetCurrentSlot();
+ if (slot_suffix.empty()) {
+ return device->WriteOkay("no");
+ }
+ std::string partition_name = args[0] + slot_suffix;
+ if (FindPhysicalPartition(partition_name) ||
+ LogicalPartitionExists(partition_name, slot_suffix)) {
+ return device->WriteOkay("yes");
+ }
+ return device->WriteOkay("no");
+}
+
+bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.size() < 1) {
+ return device->WriteFail("Missing argument");
+ }
+ // Zero-length partitions cannot be created through device-mapper, so we
+ // special case them here.
+ bool is_zero_length;
+ if (LogicalPartitionExists(args[0], device->GetCurrentSlot(), &is_zero_length) &&
+ is_zero_length) {
+ return device->WriteOkay("0");
+ }
+ // Otherwise, open the partition as normal.
+ PartitionHandle handle;
+ if (!OpenPartition(device, args[0], &handle)) {
+ return device->WriteFail("Could not open partition");
+ }
+ uint64_t size = get_block_device_size(handle.fd());
+ return device->WriteOkay(android::base::StringPrintf("%" PRIX64, size));
+}
+
+bool GetPartitionIsLogical(FastbootDevice* device, const std::vector<std::string>& args) {
+ if (args.size() < 1) {
+ return device->WriteFail("Missing argument");
+ }
+ // Note: if a partition name is in both the GPT and the super partition, we
+ // return "true", to be consistent with prefering to flash logical partitions
+ // over physical ones.
+ std::string partition_name = args[0];
+ if (LogicalPartitionExists(partition_name, device->GetCurrentSlot())) {
+ return device->WriteOkay("yes");
+ }
+ if (FindPhysicalPartition(partition_name)) {
+ return device->WriteOkay("no");
+ }
+ return device->WriteFail("Partition not found");
+}
+
+bool GetIsUserspace(FastbootDevice* device, const std::vector<std::string>& /* args */) {
+ return device->WriteOkay("yes");
+}
diff --git a/fastboot/device/variables.h b/fastboot/device/variables.h
new file mode 100644
index 0000000..554a080
--- /dev/null
+++ b/fastboot/device/variables.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+#pragma once
+
+#include <functional>
+#include <string>
+#include <vector>
+
+class FastbootDevice;
+
+bool GetVersion(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetBootloaderVersion(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetBasebandVersion(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetProduct(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetSerial(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetSecure(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetCurrentSlot(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetSlotCount(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetSlotSuccessful(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetSlotUnbootable(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetMaxDownloadSize(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetUnlocked(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetHasSlot(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetPartitionSize(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetPartitionIsLogical(FastbootDevice* device, const std::vector<std::string>& args);
+bool GetIsUserspace(FastbootDevice* device, const std::vector<std::string>& args);
diff --git a/fastboot/engine.cpp b/fastboot/engine.cpp
index 271b792..6890643 100644
--- a/fastboot/engine.cpp
+++ b/fastboot/engine.cpp
@@ -25,9 +25,7 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
-
-#include "fastboot.h"
-#include "fs.h"
+#include "engine.h"
#include <errno.h>
#include <stdarg.h>
@@ -38,150 +36,138 @@
#include <sys/types.h>
#include <unistd.h>
-#define OP_DOWNLOAD 1
-#define OP_COMMAND 2
-#define OP_QUERY 3
-#define OP_NOTICE 4
-#define OP_DOWNLOAD_SPARSE 5
-#define OP_WAIT_FOR_DISCONNECT 6
-#define OP_DOWNLOAD_FD 7
-#define OP_UPLOAD 8
+#include <memory>
+#include <vector>
-typedef struct Action Action;
+#include <android-base/stringprintf.h>
-#define CMD_SIZE 64
+#include "constants.h"
+#include "transport.h"
-struct Action {
- unsigned op;
- Action* next;
-
- char cmd[CMD_SIZE];
- const char* prod;
- void* data;
- int fd;
-
- // The protocol only supports 32-bit sizes, so you'll have to break
- // anything larger into chunks.
- uint32_t size;
-
- const char *msg;
- int (*func)(Action* a, int status, const char* resp);
-
- double start;
+enum Op {
+ OP_DOWNLOAD,
+ OP_COMMAND,
+ OP_QUERY,
+ OP_NOTICE,
+ OP_DOWNLOAD_SPARSE,
+ OP_WAIT_FOR_DISCONNECT,
+ OP_DOWNLOAD_FD,
+ OP_UPLOAD,
};
-static Action *action_list = 0;
-static Action *action_last = 0;
+struct Action {
+ Action(Op op, const std::string& cmd) : op(op), cmd(cmd) {}
+ Op op;
+ std::string cmd;
+ std::string msg;
+ std::string product;
+ void* data = nullptr;
+ // The protocol only supports 32-bit sizes, so you'll have to break
+ // anything larger into multiple chunks.
+ uint32_t size = 0;
-bool fb_getvar(Transport* transport, const std::string& key, std::string* value) {
- std::string cmd = "getvar:";
- cmd += key;
+ int fd = -1;
- char buf[FB_RESPONSE_SZ + 1];
- memset(buf, 0, sizeof(buf));
- if (fb_command_response(transport, cmd.c_str(), buf)) {
- return false;
- }
- *value = buf;
- return true;
+ int (*func)(Action& a, int status, const char* resp) = nullptr;
+
+ double start = -1;
+};
+
+static std::vector<std::unique_ptr<Action>> action_list;
+static fastboot::FastBootDriver* fb = nullptr;
+
+void fb_init(fastboot::FastBootDriver& fbi) {
+ fb = &fbi;
+ auto cb = [](std::string& info) { fprintf(stderr, "(bootloader) %s\n", info.c_str()); };
+ fb->SetInfoCallback(cb);
}
-static int cb_default(Action* a, int status, const char* resp) {
+const std::string fb_get_error() {
+ return fb->Error();
+}
+
+bool fb_getvar(const std::string& key, std::string* value) {
+ return !fb->GetVar(key, value);
+}
+
+static int cb_default(Action& a, int status, const char* resp) {
if (status) {
fprintf(stderr,"FAILED (%s)\n", resp);
} else {
double split = now();
- fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
- a->start = split;
+ fprintf(stderr, "OKAY [%7.3fs]\n", (split - a.start));
+ a.start = split;
}
return status;
}
-static Action *queue_action(unsigned op, const char *fmt, ...)
-{
- va_list ap;
- size_t cmdsize;
-
- Action* a = reinterpret_cast<Action*>(calloc(1, sizeof(Action)));
- if (a == nullptr) die("out of memory");
-
- va_start(ap, fmt);
- cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
- va_end(ap);
-
- if (cmdsize >= sizeof(a->cmd)) {
- free(a);
- die("Command length (%zu) exceeds maximum size (%zu)", cmdsize, sizeof(a->cmd));
- }
-
- if (action_last) {
- action_last->next = a;
- } else {
- action_list = a;
- }
- action_last = a;
- a->op = op;
+static Action& queue_action(Op op, const std::string& cmd) {
+ std::unique_ptr<Action> a{new Action(op, cmd)};
a->func = cb_default;
- a->start = -1;
-
- return a;
+ action_list.push_back(std::move(a));
+ return *action_list.back();
}
-void fb_set_active(const char *slot)
-{
- Action *a;
- a = queue_action(OP_COMMAND, "set_active:%s", slot);
- a->msg = mkmsg("Setting current slot to '%s'", slot);
+void fb_set_active(const std::string& slot) {
+ Action& a = queue_action(OP_COMMAND, FB_CMD_SET_ACTIVE ":" + slot);
+ a.msg = "Setting current slot to '" + slot + "'";
}
-void fb_queue_erase(const char *ptn)
-{
- Action *a;
- a = queue_action(OP_COMMAND, "erase:%s", ptn);
- a->msg = mkmsg("erasing '%s'", ptn);
+void fb_queue_erase(const std::string& partition) {
+ Action& a = queue_action(OP_COMMAND, FB_CMD_ERASE ":" + partition);
+ a.msg = "Erasing '" + partition + "'";
}
-void fb_queue_flash_fd(const char *ptn, int fd, uint32_t sz)
-{
- Action *a;
+void fb_queue_flash_fd(const std::string& partition, int fd, uint32_t sz) {
+ Action& a = queue_action(OP_DOWNLOAD_FD, "");
+ a.fd = fd;
+ a.size = sz;
+ a.msg = android::base::StringPrintf("Sending '%s' (%u KB)", partition.c_str(), sz / 1024);
- a = queue_action(OP_DOWNLOAD_FD, "");
- a->fd = fd;
- a->size = sz;
- a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
-
- a = queue_action(OP_COMMAND, "flash:%s", ptn);
- a->msg = mkmsg("writing '%s'", ptn);
+ Action& b = queue_action(OP_COMMAND, FB_CMD_FLASH ":" + partition);
+ b.msg = "Writing '" + partition + "'";
}
-void fb_queue_flash(const char *ptn, void *data, uint32_t sz)
-{
- Action *a;
+void fb_queue_flash(const std::string& partition, void* data, uint32_t sz) {
+ Action& a = queue_action(OP_DOWNLOAD, "");
+ a.data = data;
+ a.size = sz;
+ a.msg = android::base::StringPrintf("Sending '%s' (%u KB)", partition.c_str(), sz / 1024);
- a = queue_action(OP_DOWNLOAD, "");
- a->data = data;
- a->size = sz;
- a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
-
- a = queue_action(OP_COMMAND, "flash:%s", ptn);
- a->msg = mkmsg("writing '%s'", ptn);
+ Action& b = queue_action(OP_COMMAND, FB_CMD_FLASH ":" + partition);
+ b.msg = "Writing '" + partition + "'";
}
-void fb_queue_flash_sparse(const char* ptn, struct sparse_file* s, uint32_t sz, size_t current,
- size_t total) {
- Action *a;
+void fb_queue_flash_sparse(const std::string& partition, struct sparse_file* s, uint32_t sz,
+ size_t current, size_t total) {
+ Action& a = queue_action(OP_DOWNLOAD_SPARSE, "");
+ a.data = s;
+ a.size = 0;
+ a.msg = android::base::StringPrintf("Sending sparse '%s' %zu/%zu (%u KB)", partition.c_str(),
+ current, total, sz / 1024);
- a = queue_action(OP_DOWNLOAD_SPARSE, "");
- a->data = s;
- a->size = 0;
- a->msg = mkmsg("sending sparse '%s' %zu/%zu (%d KB)", ptn, current, total, sz / 1024);
+ Action& b = queue_action(OP_COMMAND, FB_CMD_FLASH ":" + partition);
+ b.msg = android::base::StringPrintf("Writing sparse '%s' %zu/%zu", partition.c_str(), current,
+ total);
+}
- a = queue_action(OP_COMMAND, "flash:%s", ptn);
- a->msg = mkmsg("writing '%s' %zu/%zu", ptn, current, total);
+void fb_queue_create_partition(const std::string& partition, const std::string& size) {
+ Action& a = queue_action(OP_COMMAND, FB_CMD_CREATE_PARTITION ":" + partition + ":" + size);
+ a.msg = "Creating '" + partition + "'";
+}
+
+void fb_queue_delete_partition(const std::string& partition) {
+ Action& a = queue_action(OP_COMMAND, FB_CMD_DELETE_PARTITION ":" + partition);
+ a.msg = "Deleting '" + partition + "'";
+}
+
+void fb_queue_resize_partition(const std::string& partition, const std::string& size) {
+ Action& a = queue_action(OP_COMMAND, FB_CMD_RESIZE_PARTITION ":" + partition + ":" + size);
+ a.msg = "Resizing '" + partition + "'";
}
static int match(const char* str, const char** value, unsigned count) {
@@ -205,212 +191,184 @@
return 0;
}
-
-
-static int cb_check(Action* a, int status, const char* resp, int invert)
-{
- const char** value = reinterpret_cast<const char**>(a->data);
- unsigned count = a->size;
+static int cb_check(Action& a, int status, const char* resp, int invert) {
+ const char** value = reinterpret_cast<const char**>(a.data);
+ unsigned count = a.size;
unsigned n;
- int yes;
if (status) {
fprintf(stderr,"FAILED (%s)\n", resp);
return status;
}
- if (a->prod) {
- if (strcmp(a->prod, cur_product) != 0) {
+ if (!a.product.empty()) {
+ if (a.product != cur_product) {
double split = now();
- fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n",
- cur_product, a->prod, (split - a->start));
- a->start = split;
+ fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n", cur_product,
+ a.product.c_str(), (split - a.start));
+ a.start = split;
return 0;
}
}
- yes = match(resp, value, count);
+ int yes = match(resp, value, count);
if (invert) yes = !yes;
if (yes) {
double split = now();
- fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
- a->start = split;
+ fprintf(stderr, "OKAY [%7.3fs]\n", (split - a.start));
+ a.start = split;
return 0;
}
- fprintf(stderr,"FAILED\n\n");
- fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp);
- fprintf(stderr,"Update %s '%s'",
- invert ? "rejects" : "requires", value[0]);
+ fprintf(stderr, "FAILED\n\n");
+ fprintf(stderr, "Device %s is '%s'.\n", a.cmd.c_str() + 7, resp);
+ fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", value[0]);
for (n = 1; n < count; n++) {
- fprintf(stderr," or '%s'", value[n]);
+ fprintf(stderr, " or '%s'", value[n]);
}
- fprintf(stderr,".\n\n");
+ fprintf(stderr, ".\n\n");
return -1;
}
-static int cb_require(Action*a, int status, const char* resp) {
+static int cb_require(Action& a, int status, const char* resp) {
return cb_check(a, status, resp, 0);
}
-static int cb_reject(Action* a, int status, const char* resp) {
+static int cb_reject(Action& a, int status, const char* resp) {
return cb_check(a, status, resp, 1);
}
-static char* xstrdup(const char* s) {
- char* result = strdup(s);
- if (!result) die("out of memory");
- return result;
+void fb_queue_require(const std::string& product, const std::string& var, bool invert,
+ size_t nvalues, const char** values) {
+ Action& a = queue_action(OP_QUERY, FB_CMD_GETVAR ":" + var);
+ a.product = product;
+ a.data = values;
+ a.size = nvalues;
+ a.msg = "Checking " + var;
+ a.func = invert ? cb_reject : cb_require;
+ if (a.data == nullptr) die("out of memory");
}
-void fb_queue_require(const char *prod, const char *var,
- bool invert, size_t nvalues, const char **value)
-{
- Action *a;
- a = queue_action(OP_QUERY, "getvar:%s", var);
- a->prod = prod;
- a->data = value;
- a->size = nvalues;
- a->msg = mkmsg("checking %s", var);
- a->func = invert ? cb_reject : cb_require;
- if (a->data == nullptr) die("out of memory");
-}
-
-static int cb_display(Action* a, int status, const char* resp) {
+static int cb_display(Action& a, int status, const char* resp) {
if (status) {
- fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
+ fprintf(stderr, "%s FAILED (%s)\n", a.cmd.c_str(), resp);
return status;
}
- fprintf(stderr, "%s: %s\n", static_cast<const char*>(a->data), resp);
- free(static_cast<char*>(a->data));
+ fprintf(stderr, "%s: %s\n", static_cast<const char*>(a.data), resp);
+ free(static_cast<char*>(a.data));
return 0;
}
-void fb_queue_display(const char* var, const char* prettyname) {
- Action* a = queue_action(OP_QUERY, "getvar:%s", var);
- a->data = xstrdup(prettyname);
- a->func = cb_display;
+void fb_queue_display(const std::string& label, const std::string& var) {
+ Action& a = queue_action(OP_QUERY, FB_CMD_GETVAR ":" + var);
+ a.data = xstrdup(label.c_str());
+ a.func = cb_display;
}
-static int cb_save(Action* a, int status, const char* resp) {
+static int cb_save(Action& a, int status, const char* resp) {
if (status) {
- fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
+ fprintf(stderr, "%s FAILED (%s)\n", a.cmd.c_str(), resp);
return status;
}
- strncpy(reinterpret_cast<char*>(a->data), resp, a->size);
+ strncpy(reinterpret_cast<char*>(a.data), resp, a.size);
return 0;
}
-void fb_queue_query_save(const char* var, char* dest, uint32_t dest_size) {
- Action* a = queue_action(OP_QUERY, "getvar:%s", var);
- a->data = dest;
- a->size = dest_size;
- a->func = cb_save;
+void fb_queue_query_save(const std::string& var, char* dest, uint32_t dest_size) {
+ Action& a = queue_action(OP_QUERY, FB_CMD_GETVAR ":" + var);
+ a.data = dest;
+ a.size = dest_size;
+ a.func = cb_save;
}
-static int cb_do_nothing(Action*, int , const char*) {
- fprintf(stderr,"\n");
+static int cb_do_nothing(Action&, int, const char*) {
+ fprintf(stderr, "\n");
return 0;
}
-void fb_queue_reboot(void)
-{
- Action *a = queue_action(OP_COMMAND, "reboot");
- a->func = cb_do_nothing;
- a->msg = "rebooting";
+void fb_queue_reboot() {
+ Action& a = queue_action(OP_COMMAND, FB_CMD_REBOOT);
+ a.func = cb_do_nothing;
+ a.msg = "Rebooting";
}
-void fb_queue_command(const char *cmd, const char *msg)
-{
- Action *a = queue_action(OP_COMMAND, cmd);
- a->msg = msg;
+void fb_queue_command(const std::string& cmd, const std::string& msg) {
+ Action& a = queue_action(OP_COMMAND, cmd);
+ a.msg = msg;
}
-void fb_queue_download(const char *name, void *data, uint32_t size)
-{
- Action *a = queue_action(OP_DOWNLOAD, "");
- a->data = data;
- a->size = size;
- a->msg = mkmsg("downloading '%s'", name);
+void fb_queue_download(const std::string& name, void* data, uint32_t size) {
+ Action& a = queue_action(OP_DOWNLOAD, "");
+ a.data = data;
+ a.size = size;
+ a.msg = "Downloading '" + name + "'";
}
-void fb_queue_download_fd(const char *name, int fd, uint32_t sz)
-{
- Action *a;
- a = queue_action(OP_DOWNLOAD_FD, "");
- a->fd = fd;
- a->size = sz;
- a->msg = mkmsg("sending '%s' (%d KB)", name, sz / 1024);
+void fb_queue_download_fd(const std::string& name, int fd, uint32_t sz) {
+ Action& a = queue_action(OP_DOWNLOAD_FD, "");
+ a.fd = fd;
+ a.size = sz;
+ a.msg = android::base::StringPrintf("Sending '%s' (%u KB)", name.c_str(), sz / 1024);
}
-void fb_queue_upload(const char* outfile) {
- Action* a = queue_action(OP_UPLOAD, "");
- a->data = xstrdup(outfile);
- a->msg = mkmsg("uploading '%s'", outfile);
+void fb_queue_upload(const std::string& outfile) {
+ Action& a = queue_action(OP_UPLOAD, "");
+ a.data = xstrdup(outfile.c_str());
+ a.msg = "Uploading '" + outfile + "'";
}
-void fb_queue_notice(const char* notice) {
- Action *a = queue_action(OP_NOTICE, "");
- a->data = (void*) notice;
+void fb_queue_notice(const std::string& notice) {
+ Action& a = queue_action(OP_NOTICE, "");
+ a.msg = notice;
}
-void fb_queue_wait_for_disconnect(void)
-{
+void fb_queue_wait_for_disconnect() {
queue_action(OP_WAIT_FOR_DISCONNECT, "");
}
-int64_t fb_execute_queue(Transport* transport)
-{
- Action *a;
- char resp[FB_RESPONSE_SZ+1];
+int64_t fb_execute_queue() {
int64_t status = 0;
-
- a = action_list;
- if (!a)
- return status;
- resp[FB_RESPONSE_SZ] = 0;
-
- double start = -1;
- for (a = action_list; a; a = a->next) {
+ for (auto& a : action_list) {
a->start = now();
- if (start < 0) start = a->start;
- if (a->msg) {
- // fprintf(stderr,"%30s... ",a->msg);
- fprintf(stderr,"%s...\n",a->msg);
+ if (!a->msg.empty()) {
+ fprintf(stderr, "%-50s ", a->msg.c_str());
+ verbose("\n");
}
if (a->op == OP_DOWNLOAD) {
- status = fb_download_data(transport, a->data, a->size);
- status = a->func(a, status, status ? fb_get_error().c_str() : "");
+ char* cbuf = static_cast<char*>(a->data);
+ status = fb->Download(cbuf, a->size);
+ status = a->func(*a, status, status ? fb_get_error().c_str() : "");
if (status) break;
} else if (a->op == OP_DOWNLOAD_FD) {
- status = fb_download_data_fd(transport, a->fd, a->size);
- status = a->func(a, status, status ? fb_get_error().c_str() : "");
+ status = fb->Download(a->fd, a->size);
+ status = a->func(*a, status, status ? fb_get_error().c_str() : "");
if (status) break;
} else if (a->op == OP_COMMAND) {
- status = fb_command(transport, a->cmd);
- status = a->func(a, status, status ? fb_get_error().c_str() : "");
+ status = fb->RawCommand(a->cmd);
+ status = a->func(*a, status, status ? fb_get_error().c_str() : "");
if (status) break;
} else if (a->op == OP_QUERY) {
- status = fb_command_response(transport, a->cmd, resp);
- status = a->func(a, status, status ? fb_get_error().c_str() : resp);
+ std::string resp;
+ status = fb->RawCommand(a->cmd, &resp);
+ status = a->func(*a, status, status ? fb_get_error().c_str() : resp.c_str());
if (status) break;
} else if (a->op == OP_NOTICE) {
- fprintf(stderr,"%s\n",(char*)a->data);
+ // We already showed the notice because it's in `Action::msg`.
+ fprintf(stderr, "\n");
} else if (a->op == OP_DOWNLOAD_SPARSE) {
- status = fb_download_data_sparse(transport, reinterpret_cast<sparse_file*>(a->data));
- status = a->func(a, status, status ? fb_get_error().c_str() : "");
+ status = fb->Download(reinterpret_cast<sparse_file*>(a->data));
+ status = a->func(*a, status, status ? fb_get_error().c_str() : "");
if (status) break;
} else if (a->op == OP_WAIT_FOR_DISCONNECT) {
- transport->WaitForDisconnect();
+ fb->WaitForDisconnect();
} else if (a->op == OP_UPLOAD) {
- status = fb_upload_data(transport, reinterpret_cast<char*>(a->data));
- status = a->func(a, status, status ? fb_get_error().c_str() : "");
+ status = fb->Upload(reinterpret_cast<const char*>(a->data));
+ status = a->func(*a, status, status ? fb_get_error().c_str() : "");
} else {
- die("bogus action");
+ die("unknown action: %d", a->op);
}
}
-
- fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
+ action_list.clear();
return status;
}
diff --git a/fastboot/engine.h b/fastboot/engine.h
new file mode 100644
index 0000000..8aebdd7
--- /dev/null
+++ b/fastboot/engine.h
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <inttypes.h>
+#include <stdlib.h>
+
+#include <string>
+
+#include <bootimg.h>
+#include "fastboot_driver.h"
+#include "util.h"
+
+#include "constants.h"
+
+class Transport;
+struct sparse_file;
+
+const std::string fb_get_error();
+
+//#define FB_COMMAND_SZ (fastboot::FB_COMMAND_SZ)
+//#define FB_RESPONSE_SZ (fastboot::FB_RESPONSE_SZ)
+
+/* engine.c - high level command queue engine */
+
+void fb_init(fastboot::FastBootDriver& fbi);
+
+bool fb_getvar(const std::string& key, std::string* value);
+void fb_queue_flash(const std::string& partition, void* data, uint32_t sz);
+void fb_queue_flash_fd(const std::string& partition, int fd, uint32_t sz);
+void fb_queue_flash_sparse(const std::string& partition, struct sparse_file* s, uint32_t sz,
+ size_t current, size_t total);
+void fb_queue_erase(const std::string& partition);
+void fb_queue_format(const std::string& partition, int skip_if_not_supported, int32_t max_chunk_sz);
+void fb_queue_require(const std::string& prod, const std::string& var, bool invert, size_t nvalues,
+ const char** values);
+void fb_queue_display(const std::string& label, const std::string& var);
+void fb_queue_query_save(const std::string& var, char* dest, uint32_t dest_size);
+void fb_queue_reboot(void);
+void fb_queue_command(const std::string& cmd, const std::string& msg);
+void fb_queue_download(const std::string& name, void* data, uint32_t size);
+void fb_queue_download_fd(const std::string& name, int fd, uint32_t sz);
+void fb_queue_upload(const std::string& outfile);
+void fb_queue_notice(const std::string& notice);
+void fb_queue_wait_for_disconnect(void);
+void fb_queue_create_partition(const std::string& partition, const std::string& size);
+void fb_queue_delete_partition(const std::string& partition);
+void fb_queue_resize_partition(const std::string& partition, const std::string& size);
+int64_t fb_execute_queue();
+void fb_set_active(const std::string& slot);
+
+/* Current product */
+extern char cur_product[FB_RESPONSE_SZ + 1];
+
+class FastBootTool {
+ public:
+ int Main(int argc, char* argv[]);
+
+ void ParseOsPatchLevel(boot_img_hdr_v1*, const char*);
+ void ParseOsVersion(boot_img_hdr_v1*, const char*);
+};
diff --git a/fastboot/fastboot.bash b/fastboot/fastboot.bash
new file mode 100644
index 0000000..cb1d354
--- /dev/null
+++ b/fastboot/fastboot.bash
@@ -0,0 +1,182 @@
+# /* vim: set ai ts=4 ft=sh: */
+#
+# Copyright 2017, 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.
+#
+
+_fastboot() {
+ if ! check_type "$1" >/dev/null; then
+ return
+ fi
+
+ if check_type _init_completion >/dev/null; then
+ _init_completion || return
+ fi
+
+ local where i cur serial
+ COMPREPLY=()
+
+ serial="${ANDROID_SERIAL:-none}"
+ where=OPTIONS
+ for ((i=1; i <= COMP_CWORD; i++)); do
+ cur="${COMP_WORDS[i]}"
+ case "${cur}" in
+ -s)
+ where=OPT_SERIAL
+ ;;
+ --slot)
+ where=OPT_SLOT
+ ;;
+ -*)
+ where=OPTIONS
+ ;;
+ *)
+ if [[ $where == OPT_SERIAL ]]; then
+ where=OPT_SERIAL_ARG
+ serial=${cur}
+ elif [[ $where == OPT_SLOT ]]; then
+ where=OPT_SLOT_ARG
+ else
+ where=COMMAND
+ break
+ fi
+ ;;
+ esac
+ done
+
+ if [[ $where == COMMAND && $i -ge $COMP_CWORD ]]; then
+ where=OPTIONS
+ fi
+
+ OPTIONS="-a -c --disable-verification --disable-verity -h --help -s --set-active --skip-secondary --skip-reboot --slot -u --version -w"
+ COMMAND="continue devices erase flash flashall flashing format getvar get_staged help oem reboot stage update"
+
+ case $where in
+ OPTIONS|OPT_SERIAL)
+ COMPREPLY=( $(compgen -W "$OPTIONS $COMMAND" -- "$cur") )
+ ;;
+ OPT_SERIAL_ARG)
+ local devices=$(command fastboot devices 2> /dev/null | awk '{ print $1 }')
+ COMPREPLY=( $(compgen -W "${devices}" -- ${cur}) )
+ ;;
+ OPT_SLOT_ARG)
+ local slots="a all b other"
+ COMPREPLY=( $(compgen -W "${slots}" -- ${cur}) )
+ ;;
+ COMMAND)
+ if [[ $i -eq $COMP_CWORD ]]; then
+ COMPREPLY=( $(compgen -W "$COMMAND" -- "$cur") )
+ else
+ i=$((i+1))
+ case "${cur}" in
+ flash)
+ _fastboot_cmd_flash "$serial" $i
+ ;;
+ reboot)
+ if [[ $COMP_CWORD == $i ]]; then
+ args="bootloader"
+ COMPREPLY=( $(compgen -W "${args}" -- "${COMP_WORDS[i]}") )
+ fi
+ ;;
+ update)
+ _fastboot_cmd_update "$serial" $i
+ ;;
+ esac
+ fi
+ ;;
+ esac
+
+ return 0
+}
+
+_fastboot_cmd_flash() {
+ local serial i cur
+ local partitions
+
+ serial=$1
+ i=$2
+
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ if [[ $i -eq $COMP_CWORD ]]; then
+ partitions="boot bootloader dtbo modem odm oem product radio recovery system vbmeta vendor"
+ COMPREPLY=( $(compgen -W "$partitions" -- $cur) )
+ else
+ _fastboot_util_complete_local_file "${cur}" '!*.img'
+ fi
+}
+
+_fastboot_cmd_update() {
+ local serial i cur
+
+ serial=$1
+ i=$2
+
+ cur="${COMP_WORDS[COMP_CWORD]}"
+
+ _fastboot_util_complete_local_file "${cur}" '!*.zip'
+}
+
+_fastboot_util_complete_local_file() {
+ local file xspec i j IFS=$'\n'
+ local -a dirs files
+
+ file=$1
+ xspec=$2
+
+ # Since we're probably doing file completion here, don't add a space after.
+ if [[ $(check_type compopt) == "builtin" ]]; then
+ compopt -o plusdirs
+ if [[ "${xspec}" == "" ]]; then
+ COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -f -- "${cur}") )
+ else
+ compopt +o filenames
+ COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -f -X "${xspec}" -- "${cur}") )
+ fi
+ else
+ # Work-around for shells with no compopt
+
+ dirs=( $(compgen -d -- "${cur}" ) )
+
+ if [[ "${xspec}" == "" ]]; then
+ files=( ${COMPREPLY[@]:-} $(compgen -f -- "${cur}") )
+ else
+ files=( ${COMPREPLY[@]:-} $(compgen -f -X "${xspec}" -- "${cur}") )
+ fi
+
+ COMPREPLY=( $(
+ for i in "${files[@]}"; do
+ local skip=
+ for j in "${dirs[@]}"; do
+ if [[ $i == $j ]]; then
+ skip=1
+ break
+ fi
+ done
+ [[ -n $skip ]] || printf "%s\n" "$i"
+ done
+ ))
+
+ COMPREPLY=( ${COMPREPLY[@]:-} $(
+ for i in "${dirs[@]}"; do
+ printf "%s/\n" "$i"
+ done
+ ))
+ fi
+}
+
+if [[ $(check_type compopt) == "builtin" ]]; then
+ complete -F _fastboot fastboot
+else
+ complete -o nospace -F _fastboot fastboot
+fi
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp
index 536d64e..dc94952 100644
--- a/fastboot/fastboot.cpp
+++ b/fastboot/fastboot.cpp
@@ -60,7 +60,7 @@
#include "bootimg_utils.h"
#include "diagnose_usb.h"
-#include "fastboot.h"
+#include "engine.h"
#include "fs.h"
#include "tcp.h"
#include "transport.h"
@@ -76,22 +76,18 @@
char cur_product[FB_RESPONSE_SZ + 1];
static const char* serial = nullptr;
-static const char* cmdline = nullptr;
-static unsigned short vendor_id = 0;
-static int long_listing = 0;
+
+static bool g_long_listing = false;
// Don't resparse files in too-big chunks.
// libsparse will support INT_MAX, but this results in large allocations, so
// let's keep it at 1GB to avoid memory pressure on the host.
static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
-static int64_t sparse_limit = -1;
+static uint64_t sparse_limit = 0;
static int64_t target_sparse_limit = -1;
-static unsigned page_size = 2048;
-static unsigned base_addr = 0x10000000;
-static unsigned kernel_offset = 0x00008000;
-static unsigned ramdisk_offset = 0x01000000;
-static unsigned second_offset = 0x00f00000;
-static unsigned tags_offset = 0x00000100;
+static unsigned g_base_addr = 0x10000000;
+static boot_img_hdr_v1 g_boot_img_hdr = {};
+static std::string g_cmdline;
static bool g_disable_verity = false;
static bool g_disable_verification = false;
@@ -115,21 +111,30 @@
const char* img_name;
const char* sig_name;
const char* part_name;
- bool is_optional;
- bool is_secondary;
+ bool optional_if_no_image;
+ bool optional_if_no_partition;
+ bool IsSecondary() const { return nickname == nullptr; }
} images[] = {
- // clang-format off
+ // clang-format off
{ "boot", "boot.img", "boot.sig", "boot", false, false },
- { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
+ { nullptr, "boot_other.img", "boot.sig", "boot", true, false },
{ "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
{ "dts", "dt.img", "dt.sig", "dts", true, false },
+ { "odm", "odm.img", "odm.sig", "odm", true, false },
+ { "product", "product.img", "product.sig", "product", true, false },
+ { "product-services",
+ "product-services.img",
+ "product-services.sig",
+ "product_services",
+ true, true },
{ "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
- { "system", "system.img", "system.sig", "system", false, false },
- { nullptr, "system_other.img", "system.sig", "system", true, true },
+ { "super", "super.img", "super.sig", "super", true, true },
+ { "system", "system.img", "system.sig", "system", false, true },
+ { nullptr, "system_other.img", "system.sig", "system", true, false },
{ "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
- { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
- { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
- // clang-format on
+ { "vendor", "vendor.img", "vendor.sig", "vendor", true, true },
+ { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, false },
+ // clang-format on
};
static std::string find_item_given_name(const char* img_name) {
@@ -191,11 +196,6 @@
}
static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
- // Require a matching vendor id if the user specified one with -i.
- if (vendor_id != 0 && info->dev_vendor != vendor_id) {
- return -1;
- }
-
if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
return -1;
}
@@ -221,7 +221,7 @@
serial = "????????????";
}
// output compatible with "adb devices"
- if (!long_listing) {
+ if (!g_long_listing) {
printf("%s\tfastboot", serial.c_str());
} else {
printf("%-22s fastboot", serial.c_str());
@@ -236,7 +236,7 @@
// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
// a specific device, otherwise the first USB device found will be used.
//
-// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
+// If |serial| is non-null but invalid, this exits.
// Otherwise it blocks until the target is available.
//
// The returned Transport is a singleton, so multiple calls to this function will return the same
@@ -268,9 +268,7 @@
if (net_address != nullptr) {
std::string error;
if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
- fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
- error.c_str());
- return nullptr;
+ die("invalid network address '%s': %s\n", net_address, error.c_str());
}
}
}
@@ -325,133 +323,101 @@
static int show_help() {
// clang-format off
fprintf(stdout,
-/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
- "usage: fastboot [ <option> ] <command>\n"
+// 1 2 3 4 5 6 7 8
+// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
+ "usage: fastboot [OPTION...] COMMAND...\n"
"\n"
- "commands:\n"
- " update <filename> Reflash device from update.zip.\n"
- " Sets the flashed slot as active.\n"
- " flashall Flash boot, system, vendor, and --\n"
- " if found -- recovery. If the device\n"
- " supports slots, the slot that has\n"
- " been flashed to is set as active.\n"
- " Secondary images may be flashed to\n"
- " an inactive slot.\n"
- " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
- " flashing lock Locks the device. Prevents flashing.\n"
- " flashing unlock Unlocks the device. Allows flashing\n"
- " any partition except\n"
- " bootloader-related partitions.\n"
- " flashing lock_critical Prevents flashing bootloader-related\n"
- " partitions.\n"
- " flashing unlock_critical Enables flashing bootloader-related\n"
- " partitions.\n"
- " flashing get_unlock_ability Queries bootloader to see if the\n"
- " device is unlocked.\n"
- " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
- " unlock nonce.\n"
- " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
- " flashing lock_bootloader Locks the bootloader to prevent\n"
- " bootloader version rollback.\n"
- " erase <partition> Erase a flash partition.\n"
- " format[:[<fs type>][:[<size>]] <partition>\n"
- " Format a flash partition. Can\n"
- " override the fs type and/or size\n"
- " the bootloader reports.\n"
- " getvar <variable> Display a bootloader variable.\n"
- " set_active <slot> Sets the active slot. If slots are\n"
- " not supported, this does nothing.\n"
- " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
- " flash:raw <bootable-partition> <kernel> [ <ramdisk> [ <second> ] ]\n"
- " Create bootimage and flash it.\n"
- " devices [-l] List all connected devices [with\n"
- " device paths].\n"
- " continue Continue with autoboot.\n"
- " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
- " reboot-bootloader Reboot device into bootloader.\n"
- " oem <parameter1> ... <parameterN> Executes oem specific command.\n"
- " stage <infile> Sends contents of <infile> to stage for\n"
- " the next command. Supported only on\n"
- " Android Things devices.\n"
- " get_staged <outfile> Receives data to <outfile> staged by the\n"
- " last command. Supported only on Android\n"
- " Things devices.\n"
- " help Show this help message.\n"
+ "flashing:\n"
+ " update ZIP Flash all partitions from an update.zip package.\n"
+ " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
+ " On A/B devices, flashed slot is set as active.\n"
+ " Secondary images may be flashed to inactive slot.\n"
+ " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
+ " $ANDROID_PRODUCT_OUT if no filename is given.\n"
+ "\n"
+ "basics:\n"
+ " devices [-l] List devices in bootloader (-l: with device paths).\n"
+ " getvar NAME Display given bootloader variable.\n"
+ " reboot [bootloader] Reboot device.\n"
+ "\n"
+ "locking/unlocking:\n"
+ " flashing lock|unlock Lock/unlock partitions for flashing\n"
+ " flashing lock_critical|unlock_critical\n"
+ " Lock/unlock 'critical' bootloader partitions.\n"
+ " flashing get_unlock_ability\n"
+ " Check whether unlocking is allowed (1) or not(0).\n"
+ "\n"
+ "advanced:\n"
+ " erase PARTITION Erase a flash partition.\n"
+ " format[:FS_TYPE[:SIZE]] PARTITION\n"
+ " Format a flash partition.\n"
+ " set_active SLOT Set the active slot.\n"
+ " oem [COMMAND...] Execute OEM-specific command.\n"
+ "\n"
+ "boot image:\n"
+ " boot KERNEL [RAMDISK [SECOND]]\n"
+ " Download and boot kernel from RAM.\n"
+ " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
+ " Create boot image and flash it.\n"
+ " --cmdline CMDLINE Override kernel command line.\n"
+ " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
+ " --kernel-offset Set kernel offset (default: 0x00008000).\n"
+ " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
+ " --tags-offset Set tags offset (default: 0x00000100).\n"
+ " --page-size BYTES Set flash page size (default: 2048).\n"
+ " --header-version VERSION Set boot image header version.\n"
+ " --os-version MAJOR[.MINOR[.PATCH]]\n"
+ " Set boot image OS version (default: 0.0.0).\n"
+ " --os-patch-level YYYY-MM-DD\n"
+ " Set boot image OS security patch level.\n"
+ // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
+ "\n"
+ // TODO: what device(s) used this? is there any documentation?
+ //" continue Continue with autoboot.\n"
+ //"\n"
+ "Android Things:\n"
+ " stage IN_FILE Sends given file to stage for the next command.\n"
+ " get_staged OUT_FILE Writes data staged by the last command to a file.\n"
"\n"
"options:\n"
- " -w Erase userdata and cache (and format\n"
- " if supported by partition type).\n"
- " -u Do not erase partition before\n"
- " formatting.\n"
- " -s <specific device> Specify a device. For USB, provide either\n"
- " a serial number or path to device port.\n"
- " For ethernet, provide an address in the\n"
- " form <protocol>:<hostname>[:port] where\n"
- " <protocol> is either tcp or udp.\n"
- " -c <cmdline> Override kernel commandline.\n"
- " -i <vendor id> Specify a custom USB vendor id.\n"
- " -b, --base <base_addr> Specify a custom kernel base\n"
- " address (default: 0x10000000).\n"
- " --kernel-offset Specify a custom kernel offset.\n"
- " (default: 0x00008000)\n"
- " --ramdisk-offset Specify a custom ramdisk offset.\n"
- " (default: 0x01000000)\n"
- " --tags-offset Specify a custom tags offset.\n"
- " (default: 0x00000100)\n"
- " -n, --page-size <page size> Specify the nand page size\n"
- " (default: 2048).\n"
- " -S <size>[K|M|G] Automatically sparse files greater\n"
- " than 'size'. 0 to disable.\n"
- " --slot <slot> Specify slot name to be used if the\n"
- " device supports slots. All operations\n"
- " on partitions that support slots will\n"
- " be done on the slot specified.\n"
- " 'all' can be given to refer to all slots.\n"
- " 'other' can be given to refer to a\n"
- " non-current slot. If this flag is not\n"
- " used, slotted partitions will default\n"
- " to the current active slot.\n"
- " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
- " provided, this will default to the value\n"
- " given by --slot. If slots are not\n"
- " supported, this does nothing. This will\n"
- " run after all non-reboot commands.\n"
- " --skip-secondary Will not flash secondary slots when\n"
- " performing a flashall or update. This\n"
- " will preserve data on other slots.\n"
- " --skip-reboot Will not reboot the device when\n"
- " performing commands that normally\n"
- " trigger a reboot.\n"
- " --disable-verity Set the disable-verity flag in the\n"
- " the vbmeta image being flashed.\n"
- " --disable-verification Set the disable-verification flag in"
- " the vbmeta image being flashed.\n"
+ " -w Wipe userdata.\n"
+ " -s SERIAL Specify a USB device.\n"
+ " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
+ " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
+ " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
+ " non-current slot (default: current active slot).\n"
+ " --set-active[=SLOT] Sets the active slot before rebooting.\n"
+ " --skip-secondary Don't flash secondary slots in flashall/update.\n"
+ " --skip-reboot Don't reboot device after flashing.\n"
+ " --disable-verity Sets disable-verity when flashing vbmeta.\n"
+ " --disable-verification Sets disable-verification when flashing vbmeta.\n"
#if !defined(_WIN32)
- " --wipe-and-use-fbe On devices which support it,\n"
- " erase userdata and cache, and\n"
- " enable file-based encryption\n"
+ " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
#endif
- " --unbuffered Do not buffer input or output.\n"
- " --version Display version.\n"
- " -h, --help show this message.\n"
+ // TODO: remove --unbuffered?
+ " --unbuffered Don't buffer input or output.\n"
+ " --verbose, -v Verbose output.\n"
+ " --version Display version.\n"
+ " --help, -h Show this message.\n"
);
// clang-format off
return 0;
}
static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
- const std::string& second_stage, int64_t* sz,
- const char* cmdline) {
+ const std::string& second_stage, int64_t* sz) {
int64_t ksize;
void* kdata = load_file(kernel.c_str(), &ksize);
if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
// Is this actually a boot image?
- if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr))) {
+ if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
die("cannot load '%s': too short", kernel.c_str());
}
if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
- if (cmdline) bootimg_set_cmdline(reinterpret_cast<boot_img_hdr*>(kdata), cmdline);
+ if (!g_cmdline.empty()) {
+ bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
+ }
if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
@@ -474,16 +440,12 @@
}
fprintf(stderr,"creating boot image...\n");
- int64_t bsize = 0;
- void* bdata = mkbootimg(kdata, ksize, kernel_offset,
- rdata, rsize, ramdisk_offset,
- sdata, ssize, second_offset,
- page_size, base_addr, tags_offset, &bsize);
+ boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
+ g_base_addr, g_boot_img_hdr, sz);
if (bdata == nullptr) die("failed to create boot.img");
- if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
- fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
- *sz = bsize;
+ if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
+ fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
return bdata;
}
@@ -537,7 +499,7 @@
die("make_temporary_directory not supported under Windows, sorry!");
}
-static int make_temporary_fd() {
+static int make_temporary_fd(const char* /*what*/) {
// TODO: reimplement to avoid leaking a FILE*.
return fileno(tmpfile());
}
@@ -553,18 +515,18 @@
static std::string make_temporary_directory() {
std::string result(make_temporary_template());
if (mkdtemp(&result[0]) == nullptr) {
- fprintf(stderr, "Unable to create temporary directory: %s\n", strerror(errno));
- return "";
+ die("unable to create temporary directory with template %s: %s",
+ result.c_str(), strerror(errno));
}
return result;
}
-static int make_temporary_fd() {
+static int make_temporary_fd(const char* what) {
std::string path_template(make_temporary_template());
int fd = mkstemp(&path_template[0]);
if (fd == -1) {
- fprintf(stderr, "Unable to create temporary file: %s\n", strerror(errno));
- return -1;
+ die("failed to create temporary file for %s with template %s: %s\n",
+ path_template.c_str(), what, strerror(errno));
}
unlink(path_template.c_str());
return fd;
@@ -574,16 +536,11 @@
static std::string create_fbemarker_tmpdir() {
std::string dir = make_temporary_directory();
- if (dir.empty()) {
- fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
- return "";
- }
std::string marker_file = dir + "/" + convert_fbe_marker_filename;
int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
if (fd == -1) {
- fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
- marker_file.c_str(), errno, strerror(errno));
- return "";
+ die("unable to create FBE marker file %s locally: %s",
+ marker_file.c_str(), strerror(errno));
}
close(fd);
return dir;
@@ -604,10 +561,7 @@
}
static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
- unique_fd fd(make_temporary_fd());
- if (fd == -1) {
- die("failed to create temporary file for '%s': %s", entry_name, strerror(errno));
- }
+ unique_fd fd(make_temporary_fd(entry_name));
ZipString zip_entry_name(entry_name);
ZipEntry zip_entry;
@@ -633,27 +587,31 @@
return fd.release();
}
-static char *strip(char *s)
-{
- int n;
- while(*s && isspace(*s)) s++;
- n = strlen(s);
- while(n-- > 0) {
- if(!isspace(s[n])) break;
+static char* strip(char* s) {
+ while (*s && isspace(*s)) s++;
+
+ int n = strlen(s);
+ while (n-- > 0) {
+ if (!isspace(s[n])) break;
s[n] = 0;
}
return s;
}
#define MAX_OPTIONS 32
-static int setup_requirement_line(char *name)
-{
+static void check_requirement(char* line) {
char *val[MAX_OPTIONS];
- char *prod = nullptr;
- unsigned n, count;
+ unsigned count;
char *x;
int invert = 0;
+ // "require product=alpha|beta|gamma"
+ // "require version-bootloader=1234"
+ // "require-for-product:gamma version-bootloader=istanbul|constantinople"
+ // "require partition-exists=vendor"
+
+ char* name = line;
+ const char* product = "";
if (!strncmp(name, "reject ", 7)) {
name += 7;
invert = 1;
@@ -662,19 +620,48 @@
invert = 0;
} else if (!strncmp(name, "require-for-product:", 20)) {
// Get the product and point name past it
- prod = name + 20;
+ product = name + 20;
name = strchr(name, ' ');
- if (!name) return -1;
+ if (!name) die("android-info.txt syntax error: %s", line);
*name = 0;
name += 1;
invert = 0;
}
x = strchr(name, '=');
- if (x == 0) return 0;
+ if (x == 0) return;
*x = 0;
val[0] = x + 1;
+ name = strip(name);
+
+ // "require partition-exists=x" is a special case, added because of the trouble we had when
+ // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
+ // missing out new partitions. A device with new partitions can use "partition-exists" to
+ // override the fields `optional_if_no_image` and 'optional_if_no_partition' in the `images`
+ // array.
+ if (!strcmp(name, "partition-exists")) {
+ const char* partition_name = val[0];
+ std::string has_slot;
+ if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
+ (has_slot != "yes" && has_slot != "no")) {
+ die("device doesn't have required partition %s!", partition_name);
+ }
+ bool known_partition = false;
+ for (size_t i = 0; i < arraysize(images); ++i) {
+ if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
+ images[i].optional_if_no_image = false;
+ images[i].optional_if_no_partition = false;
+ known_partition = true;
+ }
+ }
+ if (!known_partition) {
+ die("device requires partition %s which is not known to this version of fastboot",
+ partition_name);
+ }
+ return;
+ }
+
for(count = 1; count < MAX_OPTIONS; count++) {
x = strchr(val[count - 1],'|');
if (x == 0) break;
@@ -682,61 +669,50 @@
val[count] = x + 1;
}
- name = strip(name);
- for(n = 0; n < count; n++) val[n] = strip(val[n]);
-
- name = strip(name);
- if (name == 0) return -1;
-
- const char* var = name;
// Work around an unfortunate name mismatch.
- if (!strcmp(name,"board")) var = "product";
+ const char* var = name;
+ if (!strcmp(name, "board")) var = "product";
const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
- if (out == 0) return -1;
+ if (out == nullptr) die("out of memory");
- for(n = 0; n < count; n++) {
- out[n] = strdup(strip(val[n]));
- if (out[n] == 0) {
- for(size_t i = 0; i < n; ++i) {
- free((char*) out[i]);
- }
- free(out);
- return -1;
- }
+ for (size_t i = 0; i < count; ++i) {
+ out[i] = xstrdup(strip(val[i]));
}
- fb_queue_require(prod, var, invert, n, out);
- return 0;
+ fb_queue_require(product, var, invert, count, out);
}
-static void setup_requirements(char* data, int64_t sz) {
+static void check_requirements(char* data, int64_t sz) {
char* s = data;
while (sz-- > 0) {
if (*s == '\n') {
*s++ = 0;
- if (setup_requirement_line(data)) {
- die("out of memory");
- }
+ check_requirement(data);
data = s;
} else {
s++;
}
}
+ if (fb_execute_queue()) die("requirements not met!");
}
static void queue_info_dump() {
fb_queue_notice("--------------------------------------------");
- fb_queue_display("version-bootloader", "Bootloader Version...");
- fb_queue_display("version-baseband", "Baseband Version.....");
- fb_queue_display("serialno", "Serial Number........");
+ fb_queue_display("Bootloader Version...", "version-bootloader");
+ fb_queue_display("Baseband Version.....", "version-baseband");
+ fb_queue_display("Serial Number........", "serialno");
fb_queue_notice("--------------------------------------------");
}
-static struct sparse_file** load_sparse_files(int fd, int max_size) {
+static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
struct sparse_file* s = sparse_file_import_auto(fd, false, true);
if (!s) die("cannot sparse read file");
+ if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
+ die("invalid max size %" PRId64, max_size);
+ }
+
int files = sparse_file_resparse(s, max_size, nullptr, 0);
if (files < 0) die("Failed to resparse");
@@ -749,11 +725,11 @@
return out_s;
}
-static int64_t get_target_sparse_limit(Transport* transport) {
+static int64_t get_target_sparse_limit() {
std::string max_download_size;
- if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
- max_download_size.empty()) {
- fprintf(stderr, "target didn't report max-download-size\n");
+ if (!fb_getvar("max-download-size", &max_download_size) ||
+ max_download_size.empty()) {
+ verbose("target didn't report max-download-size");
return 0;
}
@@ -765,22 +741,17 @@
fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
return 0;
}
- if (limit > 0) {
- fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
- }
+ if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
return limit;
}
-static int64_t get_sparse_limit(Transport* transport, int64_t size) {
- int64_t limit;
-
- if (sparse_limit == 0) {
- return 0;
- } else if (sparse_limit > 0) {
- limit = sparse_limit;
- } else {
+static int64_t get_sparse_limit(int64_t size) {
+ int64_t limit = sparse_limit;
+ if (limit == 0) {
+ // Unlimited, so see what the target device's limit is.
+ // TODO: shouldn't we apply this limit even if you've used -S?
if (target_sparse_limit == -1) {
- target_sparse_limit = get_target_sparse_limit(transport);
+ target_sparse_limit = get_target_sparse_limit();
}
if (target_sparse_limit > 0) {
limit = target_sparse_limit;
@@ -796,25 +767,14 @@
return 0;
}
-// Until we get lazy inode table init working in make_ext4fs, we need to
-// erase partitions of type ext4 before flashing a filesystem so no stale
-// inodes are left lying around. Otherwise, e2fsck gets very upset.
-static bool needs_erase(Transport* transport, const char* partition) {
- std::string partition_type;
- if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
- return false;
- }
- return partition_type == "ext4";
-}
-
-static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
+static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
int64_t sz = get_file_size(fd);
if (sz == -1) {
return false;
}
lseek64(fd, 0, SEEK_SET);
- int64_t limit = get_sparse_limit(transport, sz);
+ int64_t limit = get_sparse_limit(sz);
if (limit) {
sparse_file** s = load_sparse_files(fd, limit);
if (s == nullptr) {
@@ -832,7 +792,7 @@
return true;
}
-static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
+static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
if (fd == -1) {
@@ -848,7 +808,7 @@
return false;
}
- return load_buf_fd(transport, fd.release(), buf);
+ return load_buf_fd(fd.release(), buf);
}
static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
@@ -858,10 +818,7 @@
return;
}
- int fd = make_temporary_fd();
- if (fd == -1) {
- die("Failed to create temporary file for vbmeta rewriting");
- }
+ int fd = make_temporary_fd("vbmeta rewriting");
std::string data;
if (!android::base::ReadFdToString(buf->fd, &data)) {
@@ -889,14 +846,13 @@
lseek(fd, 0, SEEK_SET);
}
-static void flash_buf(const char *pname, struct fastboot_buffer *buf)
+static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
{
sparse_file** s;
// Rewrite vbmeta if that's what we're flashing and modification has been requested.
if ((g_disable_verity || g_disable_verification) &&
- (strcmp(pname, "vbmeta") == 0 || strcmp(pname, "vbmeta_a") == 0 ||
- strcmp(pname, "vbmeta_b") == 0)) {
+ (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
rewrite_vbmeta_buffer(buf);
}
@@ -912,63 +868,35 @@
for (size_t i = 0; i < sparse_files.size(); ++i) {
const auto& pair = sparse_files[i];
- fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
+ fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
}
break;
}
case FB_BUFFER_FD:
- fb_queue_flash_fd(pname, buf->fd, buf->sz);
+ fb_queue_flash_fd(partition, buf->fd, buf->sz);
break;
default:
die("unknown buffer type: %d", buf->type);
}
}
-static std::string get_current_slot(Transport* transport)
-{
+static std::string get_current_slot() {
std::string current_slot;
- if (fb_getvar(transport, "current-slot", ¤t_slot)) {
- if (current_slot == "_a") return "a"; // Legacy support
- if (current_slot == "_b") return "b"; // Legacy support
- return current_slot;
- }
- return "";
+ if (!fb_getvar("current-slot", ¤t_slot)) return "";
+ return current_slot;
}
-// Legacy support
-static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
- std::vector<std::string> suffixes;
- std::string suffix_list;
- if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
- return suffixes;
- }
- suffixes = android::base::Split(suffix_list, ",");
- // Unfortunately some devices will return an error message in the
- // guise of a valid value. If we only see only one suffix, it's probably
- // not real.
- if (suffixes.size() == 1) {
- suffixes.clear();
- }
- return suffixes;
-}
-
-// Legacy support
-static bool supports_AB_obsolete(Transport* transport) {
- return !get_suffixes_obsolete(transport).empty();
-}
-
-static int get_slot_count(Transport* transport) {
+static int get_slot_count() {
std::string var;
- int count;
- if (!fb_getvar(transport, "slot-count", &var)) {
- if (supports_AB_obsolete(transport)) return 2; // Legacy support
+ int count = 0;
+ if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
+ return 0;
}
- if (!android::base::ParseInt(var, &count)) return 0;
return count;
}
-static bool supports_AB(Transport* transport) {
- return get_slot_count(transport) >= 2;
+static bool supports_AB() {
+ return get_slot_count() >= 2;
}
// Given a current slot, this returns what the 'other' slot is.
@@ -979,27 +907,25 @@
return std::string(1, next);
}
-static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
- return get_other_slot(current_slot, get_slot_count(transport));
+static std::string get_other_slot(const std::string& current_slot) {
+ return get_other_slot(current_slot, get_slot_count());
}
-static std::string get_other_slot(Transport* transport, int count) {
- return get_other_slot(get_current_slot(transport), count);
+static std::string get_other_slot(int count) {
+ return get_other_slot(get_current_slot(), count);
}
-static std::string get_other_slot(Transport* transport) {
- return get_other_slot(get_current_slot(transport), get_slot_count(transport));
+static std::string get_other_slot() {
+ return get_other_slot(get_current_slot(), get_slot_count());
}
-static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
+static std::string verify_slot(const std::string& slot_name, bool allow_all) {
std::string slot = slot_name;
- if (slot == "_a") slot = "a"; // Legacy support
- if (slot == "_b") slot = "b"; // Legacy support
if (slot == "all") {
if (allow_all) {
return "all";
} else {
- int count = get_slot_count(transport);
+ int count = get_slot_count();
if (count > 0) {
return "a";
} else {
@@ -1008,11 +934,11 @@
}
}
- int count = get_slot_count(transport);
+ int count = get_slot_count();
if (count == 0) die("Device does not support slots");
if (slot == "other") {
- std::string other = get_other_slot(transport, count);
+ std::string other = get_other_slot( count);
if (other == "") {
die("No known slots");
}
@@ -1029,22 +955,22 @@
exit(1);
}
-static std::string verify_slot(Transport* transport, const std::string& slot) {
- return verify_slot(transport, slot, true);
+static std::string verify_slot(const std::string& slot) {
+ return verify_slot(slot, true);
}
-static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
+static void do_for_partition(const std::string& part, const std::string& slot,
const std::function<void(const std::string&)>& func, bool force_slot) {
std::string has_slot;
std::string current_slot;
- if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
+ if (!fb_getvar("has-slot:" + part, &has_slot)) {
/* If has-slot is not supported, the answer is no. */
has_slot = "no";
}
if (has_slot == "yes") {
if (slot == "") {
- current_slot = get_current_slot(transport);
+ current_slot = get_current_slot();
if (current_slot == "") {
die("Failed to identify current slot");
}
@@ -1066,30 +992,30 @@
* partition names. If force_slot is true, it will fail if a slot is specified, and the given
* partition does not support slots.
*/
-static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
+static 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 has_slot;
if (slot == "all") {
- if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
+ if (!fb_getvar("has-slot:" + part, &has_slot)) {
die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
}
if (has_slot == "yes") {
- for (int i=0; i < get_slot_count(transport); i++) {
- do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
+ for (int i=0; i < get_slot_count(); i++) {
+ do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
}
} else {
- do_for_partition(transport, part, "", func, force_slot);
+ do_for_partition(part, "", func, force_slot);
}
} else {
- do_for_partition(transport, part, slot, func, force_slot);
+ do_for_partition(part, slot, func, force_slot);
}
}
-static void do_flash(Transport* transport, const char* pname, const char* fname) {
+static void do_flash(const char* pname, const char* fname) {
struct fastboot_buffer buf;
- if (!load_buf(transport, fname, &buf)) {
+ if (!load_buf(fname, &buf)) {
die("cannot load '%s': %s", fname, strerror(errno));
}
flash_buf(pname, &buf);
@@ -1105,26 +1031,39 @@
// Sets slot_override as the active slot. If slot_override is blank,
// set current slot as active instead. This clears slot-unbootable.
-static void set_active(Transport* transport, const std::string& slot_override) {
- std::string separator = "";
- if (!supports_AB(transport)) {
- if (supports_AB_obsolete(transport)) {
- separator = "_"; // Legacy support
- } else {
- return;
- }
- }
+static void set_active(const std::string& slot_override) {
+ if (!supports_AB()) return;
+
if (slot_override != "") {
- fb_set_active((separator + slot_override).c_str());
+ fb_set_active(slot_override);
} else {
- std::string current_slot = get_current_slot(transport);
+ std::string current_slot = get_current_slot();
if (current_slot != "") {
- fb_set_active((separator + current_slot).c_str());
+ fb_set_active(current_slot);
}
}
}
-static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool erase_first, bool skip_secondary) {
+static bool if_partition_exists(const std::string& partition, const std::string& slot) {
+ std::string has_slot;
+ std::string partition_name = partition;
+
+ if (fb_getvar("has-slot:" + partition, &has_slot) && has_slot == "yes") {
+ if (slot == "") {
+ std::string current_slot = get_current_slot();
+ if (current_slot == "") {
+ die("Failed to identify current slot");
+ }
+ partition_name += "_" + current_slot;
+ } else {
+ partition_name += "_" + slot;
+ }
+ }
+ std::string partition_size;
+ return fb_getvar("partition-size:" + partition_name, &partition_size);
+}
+
+static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
queue_info_dump();
fb_queue_query_save("product", cur_product, sizeof(cur_product));
@@ -1141,17 +1080,17 @@
die("update package '%s' has no android-info.txt", filename);
}
- setup_requirements(reinterpret_cast<char*>(data), sz);
+ check_requirements(reinterpret_cast<char*>(data), sz);
std::string secondary;
if (!skip_secondary) {
if (slot_override != "") {
- secondary = get_other_slot(transport, slot_override);
+ secondary = get_other_slot(slot_override);
} else {
- secondary = get_other_slot(transport);
+ secondary = get_other_slot();
}
if (secondary == "") {
- if (supports_AB(transport)) {
+ if (supports_AB()) {
fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
}
skip_secondary = true;
@@ -1159,7 +1098,7 @@
}
for (size_t i = 0; i < arraysize(images); ++i) {
const char* slot = slot_override.c_str();
- if (images[i].is_secondary) {
+ if (images[i].IsSecondary()) {
if (!skip_secondary) {
slot = secondary.c_str();
} else {
@@ -1169,35 +1108,37 @@
int fd = unzip_to_file(zip, images[i].img_name);
if (fd == -1) {
- if (images[i].is_optional) {
+ if (images[i].optional_if_no_image) {
continue; // An optional file is missing, so ignore it.
}
die("non-optional file %s missing", images[i].img_name);
}
+ if (images[i].optional_if_no_partition &&
+ !if_partition_exists(images[i].part_name, slot)) {
+ continue;
+ }
+
fastboot_buffer buf;
- if (!load_buf_fd(transport, fd, &buf)) {
+ if (!load_buf_fd(fd, &buf)) {
die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
}
auto update = [&](const std::string& partition) {
do_update_signature(zip, images[i].sig_name);
- if (erase_first && needs_erase(transport, partition.c_str())) {
- fb_queue_erase(partition.c_str());
- }
flash_buf(partition.c_str(), &buf);
/* not closing the fd here since the sparse code keeps the fd around
* but hasn't mmaped data yet. The temporary file will get cleaned up when the
* program exits.
*/
};
- do_for_partitions(transport, images[i].part_name, slot, update, false);
+ do_for_partitions(images[i].part_name, slot, update, false);
}
if (slot_override == "all") {
- set_active(transport, "a");
+ set_active("a");
} else {
- set_active(transport, slot_override);
+ set_active(slot_override);
}
CloseArchive(zip);
@@ -1217,7 +1158,7 @@
fb_queue_command("signature", "installing signature");
}
-static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
+static void do_flashall(const std::string& slot_override, bool skip_secondary) {
std::string fname;
queue_info_dump();
@@ -1230,17 +1171,17 @@
void* data = load_file(fname.c_str(), &sz);
if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
- setup_requirements(reinterpret_cast<char*>(data), sz);
+ check_requirements(reinterpret_cast<char*>(data), sz);
std::string secondary;
if (!skip_secondary) {
if (slot_override != "") {
- secondary = get_other_slot(transport, slot_override);
+ secondary = get_other_slot(slot_override);
} else {
- secondary = get_other_slot(transport);
+ secondary = get_other_slot();
}
if (secondary == "") {
- if (supports_AB(transport)) {
+ if (supports_AB()) {
fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
}
skip_secondary = true;
@@ -1249,7 +1190,7 @@
for (size_t i = 0; i < arraysize(images); i++) {
const char* slot = NULL;
- if (images[i].is_secondary) {
+ if (images[i].IsSecondary()) {
if (!skip_secondary) slot = secondary.c_str();
} else {
slot = slot_override.c_str();
@@ -1257,25 +1198,25 @@
if (!slot) continue;
fname = find_item_given_name(images[i].img_name);
fastboot_buffer buf;
- if (!load_buf(transport, fname.c_str(), &buf)) {
- if (images[i].is_optional) continue;
+ if (!load_buf(fname.c_str(), &buf)) {
+ if (images[i].optional_if_no_image) continue;
die("could not load '%s': %s", images[i].img_name, strerror(errno));
}
-
+ if (images[i].optional_if_no_partition &&
+ !if_partition_exists(images[i].part_name, slot)) {
+ continue;
+ }
auto flashall = [&](const std::string &partition) {
do_send_signature(fname.c_str());
- if (erase_first && needs_erase(transport, partition.c_str())) {
- fb_queue_erase(partition.c_str());
- }
flash_buf(partition.c_str(), &buf);
};
- do_for_partitions(transport, images[i].part_name, slot, flashall, false);
+ do_for_partitions(images[i].part_name, slot, flashall, false);
}
if (slot_override == "all") {
- set_active(transport, "a");
+ set_active("a");
} else {
- set_active(transport, slot_override);
+ set_active(slot_override);
}
}
@@ -1286,18 +1227,6 @@
return result;
}
-static void do_bypass_unlock_command(std::vector<std::string>* args) {
- if (args->empty()) syntax_error("missing unlock_bootloader request");
-
- std::string filename = next_arg(args);
-
- int64_t sz;
- void* data = load_file(filename.c_str(), &sz);
- if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
- fb_queue_download("unlock_message", data, sz);
- fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
-}
-
static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
if (args->empty()) syntax_error("empty oem command");
@@ -1305,48 +1234,7 @@
while (!args->empty()) {
command += " " + next_arg(args);
}
- fb_queue_command(command.c_str(), "");
-}
-
-static int64_t parse_num(const char *arg)
-{
- char *endptr;
- unsigned long long num;
-
- num = strtoull(arg, &endptr, 0);
- if (endptr == arg) {
- return -1;
- }
-
- if (*endptr == 'k' || *endptr == 'K') {
- if (num >= (-1ULL) / 1024) {
- return -1;
- }
- num *= 1024LL;
- endptr++;
- } else if (*endptr == 'm' || *endptr == 'M') {
- if (num >= (-1ULL) / (1024 * 1024)) {
- return -1;
- }
- num *= 1024LL * 1024LL;
- endptr++;
- } else if (*endptr == 'g' || *endptr == 'G') {
- if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
- return -1;
- }
- num *= 1024LL * 1024LL * 1024LL;
- endptr++;
- }
-
- if (*endptr != '\0') {
- return -1;
- }
-
- if (num > INT64_MAX) {
- return -1;
- }
-
- return num;
+ fb_queue_command(command, "");
}
static std::string fb_fix_numeric_var(std::string var) {
@@ -1358,10 +1246,10 @@
return var;
}
-static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
+static unsigned fb_get_flash_block_size(std::string name) {
std::string sizeString;
- if (!fb_getvar(transport, name.c_str(), &sizeString) || sizeString.empty()) {
- /* This device does not report flash block sizes, so return 0 */
+ if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
+ // This device does not report flash block sizes, so return 0.
return 0;
}
sizeString = fb_fix_numeric_var(sizeString);
@@ -1378,8 +1266,8 @@
return size;
}
-static void fb_perform_format(Transport* transport,
- const char* partition, int skip_if_not_supported,
+static void fb_perform_format(
+ const std::string& partition, int skip_if_not_supported,
const std::string& type_override, const std::string& size_override,
const std::string& initial_dir) {
std::string partition_type, partition_size;
@@ -1398,26 +1286,26 @@
limit = sparse_limit;
}
- if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
+ if (!fb_getvar("partition-type:" + partition, &partition_type)) {
errMsg = "Can't determine partition type.\n";
goto failed;
}
if (!type_override.empty()) {
if (partition_type != type_override) {
fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
- partition, partition_type.c_str(), type_override.c_str());
+ partition.c_str(), partition_type.c_str(), type_override.c_str());
}
partition_type = type_override;
}
- if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
+ if (!fb_getvar("partition-size:" + partition, &partition_size)) {
errMsg = "Unable to get partition size\n";
goto failed;
}
if (!size_override.empty()) {
if (partition_size != size_override) {
fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
- partition, partition_size.c_str(), size_override.c_str());
+ partition.c_str(), partition_size.c_str(), size_override.c_str());
}
partition_size = size_override;
}
@@ -1442,12 +1330,12 @@
}
unsigned eraseBlkSize, logicalBlkSize;
- eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
- logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
+ eraseBlkSize = fb_get_flash_block_size("erase-block-size");
+ logicalBlkSize = fb_get_flash_block_size("logical-block-size");
if (fs_generator_generate(gen, output.path, size, initial_dir,
eraseBlkSize, logicalBlkSize)) {
- die("Cannot generate image for %s", partition);
+ die("Cannot generate image for %s", partition.c_str());
return;
}
@@ -1456,7 +1344,7 @@
fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
return;
}
- if (!load_buf_fd(transport, fd.release(), &buf)) {
+ if (!load_buf_fd(fd.release(), &buf)) {
fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
return;
}
@@ -1471,16 +1359,15 @@
fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
}
-int main(int argc, char **argv)
-{
+int FastBootTool::Main(int argc, char* argv[]) {
bool wants_wipe = false;
bool wants_reboot = false;
bool wants_reboot_bootloader = false;
- bool wants_reboot_emergency = false;
+ bool wants_reboot_recovery = false;
+ bool wants_reboot_fastboot = false;
bool skip_reboot = false;
bool wants_set_active = false;
bool skip_secondary = false;
- bool erase_first = true;
bool set_fbe_marker = false;
void *data;
int64_t sz;
@@ -1488,26 +1375,32 @@
std::string slot_override;
std::string next_active;
+ g_boot_img_hdr.kernel_addr = 0x00008000;
+ g_boot_img_hdr.ramdisk_addr = 0x01000000;
+ g_boot_img_hdr.second_addr = 0x00f00000;
+ g_boot_img_hdr.tags_addr = 0x00000100;
+ g_boot_img_hdr.page_size = 2048;
+
const struct option longopts[] = {
- {"base", required_argument, 0, 'b'},
- {"kernel_offset", required_argument, 0, 'k'},
- {"kernel-offset", required_argument, 0, 'k'},
- {"page_size", required_argument, 0, 'n'},
- {"page-size", required_argument, 0, 'n'},
- {"ramdisk_offset", required_argument, 0, 'r'},
- {"ramdisk-offset", required_argument, 0, 'r'},
- {"tags_offset", required_argument, 0, 't'},
- {"tags-offset", required_argument, 0, 't'},
- {"help", no_argument, 0, 'h'},
- {"unbuffered", no_argument, 0, 0},
- {"version", no_argument, 0, 0},
- {"slot", required_argument, 0, 0},
- {"set_active", optional_argument, 0, 'a'},
- {"set-active", optional_argument, 0, 'a'},
- {"skip-secondary", no_argument, 0, 0},
- {"skip-reboot", no_argument, 0, 0},
- {"disable-verity", no_argument, 0, 0},
+ {"base", required_argument, 0, 0},
+ {"cmdline", required_argument, 0, 0},
{"disable-verification", no_argument, 0, 0},
+ {"disable-verity", no_argument, 0, 0},
+ {"header-version", required_argument, 0, 0},
+ {"help", no_argument, 0, 'h'},
+ {"kernel-offset", required_argument, 0, 0},
+ {"os-patch-level", required_argument, 0, 0},
+ {"os-version", required_argument, 0, 0},
+ {"page-size", required_argument, 0, 0},
+ {"ramdisk-offset", required_argument, 0, 0},
+ {"set-active", optional_argument, 0, 'a'},
+ {"skip-reboot", no_argument, 0, 0},
+ {"skip-secondary", no_argument, 0, 0},
+ {"slot", required_argument, 0, 0},
+ {"tags-offset", required_argument, 0, 0},
+ {"unbuffered", no_argument, 0, 0},
+ {"verbose", no_argument, 0, 'v'},
+ {"version", no_argument, 0, 0},
#if !defined(_WIN32)
{"wipe-and-use-fbe", no_argument, 0, 0},
#endif
@@ -1516,98 +1409,84 @@
serial = getenv("ANDROID_SERIAL");
- while (1) {
- int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lc:i:m:ha::", longopts, &longindex);
- if (c < 0) {
- break;
- }
- /* Alphabetical cases */
- switch (c) {
- case 'a':
- wants_set_active = true;
- if (optarg)
- next_active = optarg;
- break;
- case 'b':
- base_addr = strtoul(optarg, 0, 16);
- break;
- case 'c':
- cmdline = optarg;
- break;
- case 'h':
- return show_help();
- case 'i': {
- char *endptr = nullptr;
- unsigned long val;
-
- val = strtoul(optarg, &endptr, 0);
- if (!endptr || *endptr != '\0' || (val & ~0xffff))
- die("invalid vendor id '%s'", optarg);
- vendor_id = (unsigned short)val;
- break;
- }
- case 'k':
- kernel_offset = strtoul(optarg, 0, 16);
- break;
- case 'l':
- long_listing = 1;
- break;
- case 'n':
- page_size = (unsigned)strtoul(optarg, nullptr, 0);
- if (!page_size) die("invalid page size");
- break;
- case 'r':
- ramdisk_offset = strtoul(optarg, 0, 16);
- break;
- case 't':
- tags_offset = strtoul(optarg, 0, 16);
- break;
- case 's':
- serial = optarg;
- break;
- case 'S':
- sparse_limit = parse_num(optarg);
- if (sparse_limit < 0) die("invalid sparse limit");
- break;
- case 'u':
- erase_first = false;
- break;
- case 'w':
- wants_wipe = true;
- break;
- case '?':
- return 1;
- case 0:
- if (strcmp("unbuffered", longopts[longindex].name) == 0) {
+ int c;
+ while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
+ if (c == 0) {
+ std::string name{longopts[longindex].name};
+ if (name == "base") {
+ g_base_addr = strtoul(optarg, 0, 16);
+ } else if (name == "cmdline") {
+ g_cmdline = optarg;
+ } else if (name == "disable-verification") {
+ g_disable_verification = true;
+ } else if (name == "disable-verity") {
+ g_disable_verity = true;
+ } else if (name == "header-version") {
+ g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
+ } else if (name == "kernel-offset") {
+ g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
+ } else if (name == "os-patch-level") {
+ ParseOsPatchLevel(&g_boot_img_hdr, optarg);
+ } else if (name == "os-version") {
+ ParseOsVersion(&g_boot_img_hdr, optarg);
+ } else if (name == "page-size") {
+ g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
+ if (g_boot_img_hdr.page_size == 0) die("invalid page size");
+ } else if (name == "ramdisk-offset") {
+ g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
+ } else if (name == "skip-reboot") {
+ skip_reboot = true;
+ } else if (name == "skip-secondary") {
+ skip_secondary = true;
+ } else if (name == "slot") {
+ slot_override = optarg;
+ } else if (name == "tags-offset") {
+ g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
+ } else if (name == "unbuffered") {
setvbuf(stdout, nullptr, _IONBF, 0);
setvbuf(stderr, nullptr, _IONBF, 0);
- } else if (strcmp("version", longopts[longindex].name) == 0) {
+ } else if (name == "version") {
fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
return 0;
- } else if (strcmp("slot", longopts[longindex].name) == 0) {
- slot_override = std::string(optarg);
- } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
- skip_secondary = true;
- } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
- skip_reboot = true;
- } else if (strcmp("disable-verity", longopts[longindex].name) == 0 ) {
- g_disable_verity = true;
- } else if (strcmp("disable-verification", longopts[longindex].name) == 0 ) {
- g_disable_verification = true;
#if !defined(_WIN32)
- } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
+ } else if (name == "wipe-and-use-fbe") {
wants_wipe = true;
set_fbe_marker = true;
#endif
} else {
- fprintf(stderr, "Internal error in options processing for %s\n",
- longopts[longindex].name);
- return 1;
+ die("unknown option %s", longopts[longindex].name);
}
- break;
- default:
- abort();
+ } else {
+ switch (c) {
+ case 'a':
+ wants_set_active = true;
+ if (optarg) next_active = optarg;
+ break;
+ case 'h':
+ return show_help();
+ case 'l':
+ g_long_listing = true;
+ break;
+ case 's':
+ serial = optarg;
+ break;
+ case 'S':
+ if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
+ die("invalid sparse limit %s", optarg);
+ }
+ break;
+ case 'v':
+ set_verbose();
+ break;
+ case 'w':
+ wants_wipe = true;
+ break;
+ case '?':
+ return 1;
+ default:
+ abort();
+ }
}
}
@@ -1629,24 +1508,25 @@
if (transport == nullptr) {
return 1;
}
+ fastboot::FastBootDriver fb(transport);
+ fb_init(fb);
- if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
- fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
- }
- if (slot_override != "") slot_override = verify_slot(transport, slot_override);
- if (next_active != "") next_active = verify_slot(transport, next_active, false);
+ const double start = now();
+
+ if (slot_override != "") slot_override = verify_slot(slot_override);
+ if (next_active != "") next_active = verify_slot(next_active, false);
if (wants_set_active) {
if (next_active == "") {
if (slot_override == "") {
std::string current_slot;
- if (fb_getvar(transport, "current-slot", ¤t_slot)) {
- next_active = verify_slot(transport, current_slot, false);
+ if (fb_getvar("current-slot", ¤t_slot)) {
+ next_active = verify_slot(current_slot, false);
} else {
wants_set_active = false;
}
} else {
- next_active = verify_slot(transport, slot_override, false);
+ next_active = verify_slot(slot_override, false);
}
}
}
@@ -1657,21 +1537,21 @@
if (command == "getvar") {
std::string variable = next_arg(&args);
- fb_queue_display(variable.c_str(), variable.c_str());
+ fb_queue_display(variable, variable);
} else if (command == "erase") {
std::string partition = next_arg(&args);
auto erase = [&](const std::string& partition) {
std::string partition_type;
- if (fb_getvar(transport, std::string("partition-type:") + partition,
+ if (fb_getvar(std::string("partition-type:") + partition,
&partition_type) &&
fs_get_generator(partition_type) != nullptr) {
fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
partition_type.c_str());
}
- fb_queue_erase(partition.c_str());
+ fb_queue_erase(partition);
};
- do_for_partitions(transport, partition, slot_override, erase, true);
+ do_for_partitions(partition, slot_override, erase, true);
} else if (android::base::StartsWith(command, "format")) {
// Parsing for: "format[:[type][:[size]]]"
// Some valid things:
@@ -1689,13 +1569,9 @@
std::string partition = next_arg(&args);
auto format = [&](const std::string& partition) {
- if (erase_first && needs_erase(transport, partition.c_str())) {
- fb_queue_erase(partition.c_str());
- }
- fb_perform_format(transport, partition.c_str(), 0, type_override, size_override,
- "");
+ fb_perform_format(partition, 0, type_override, size_override, "");
};
- do_for_partitions(transport, partition.c_str(), slot_override, format, true);
+ do_for_partitions(partition.c_str(), slot_override, format, true);
} else if (command == "signature") {
std::string filename = next_arg(&args);
data = load_file(filename.c_str(), &sz);
@@ -1711,9 +1587,12 @@
if (what == "bootloader") {
wants_reboot = false;
wants_reboot_bootloader = true;
- } else if (what == "emergency") {
+ } else if (what == "recovery") {
wants_reboot = false;
- wants_reboot_emergency = true;
+ wants_reboot_recovery = true;
+ } else if (what == "fastboot") {
+ wants_reboot = false;
+ wants_reboot_fastboot = true;
} else {
syntax_error("unknown reboot target %s", what.c_str());
}
@@ -1722,6 +1601,10 @@
if (!args.empty()) syntax_error("junk after reboot command");
} else if (command == "reboot-bootloader") {
wants_reboot_bootloader = true;
+ } else if (command == "reboot-recovery") {
+ wants_reboot_recovery = true;
+ } else if (command == "reboot-fastboot") {
+ wants_reboot_fastboot = true;
} else if (command == "continue") {
fb_queue_command("continue", "resuming boot");
} else if (command == "boot") {
@@ -1731,7 +1614,7 @@
std::string second_stage;
if (!args.empty()) second_stage = next_arg(&args);
- data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
+ data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
fb_queue_download("boot.img", data, sz);
fb_queue_command("boot", "booting");
} else if (command == "flash") {
@@ -1746,12 +1629,9 @@
if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
auto flash = [&](const std::string &partition) {
- if (erase_first && needs_erase(transport, partition.c_str())) {
- fb_queue_erase(partition.c_str());
- }
- do_flash(transport, partition.c_str(), fname.c_str());
+ do_flash(partition.c_str(), fname.c_str());
};
- do_for_partitions(transport, pname.c_str(), slot_override, flash, true);
+ do_for_partitions(pname.c_str(), slot_override, flash, true);
} else if (command == "flash:raw") {
std::string partition = next_arg(&args);
std::string kernel = next_arg(&args);
@@ -1760,17 +1640,17 @@
std::string second_stage;
if (!args.empty()) second_stage = next_arg(&args);
- data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
+ data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
auto flashraw = [&](const std::string& partition) {
- fb_queue_flash(partition.c_str(), data, sz);
+ fb_queue_flash(partition, data, sz);
};
- do_for_partitions(transport, partition, slot_override, flashraw, true);
+ do_for_partitions(partition, slot_override, flashraw, true);
} else if (command == "flashall") {
if (slot_override == "all") {
fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
- do_flashall(transport, slot_override, erase_first, true);
+ do_flashall(slot_override, true);
} else {
- do_flashall(transport, slot_override, erase_first, skip_secondary);
+ do_flashall(slot_override, skip_secondary);
}
wants_reboot = true;
} else if (command == "update") {
@@ -1782,32 +1662,22 @@
if (!args.empty()) {
filename = next_arg(&args);
}
- do_update(transport, filename.c_str(), slot_override, erase_first,
- skip_secondary || slot_all);
+ do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
wants_reboot = true;
} else if (command == "set_active") {
- std::string slot = verify_slot(transport, next_arg(&args), false);
-
- // Legacy support: verify_slot() removes leading underscores, we need to put them back
- // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
- // do have slot-suffixes.
- std::string var;
- if (!fb_getvar(transport, "slot-count", &var) &&
- fb_getvar(transport, "slot-suffixes", &var)) {
- slot = "_" + slot;
- }
- fb_set_active(slot.c_str());
+ std::string slot = verify_slot(next_arg(&args), false);
+ fb_set_active(slot);
} else if (command == "stage") {
std::string filename = next_arg(&args);
struct fastboot_buffer buf;
- if (!load_buf(transport, filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
+ if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
die("cannot load '%s'", filename.c_str());
}
- fb_queue_download_fd(filename.c_str(), buf.fd, buf.sz);
+ fb_queue_download_fd(filename, buf.fd, buf.sz);
} else if (command == "get_staged") {
std::string filename = next_arg(&args);
- fb_queue_upload(filename.c_str());
+ fb_queue_upload(filename);
} else if (command == "oem") {
do_oem_command("oem", &args);
} else if (command == "flashing") {
@@ -1816,44 +1686,46 @@
} else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
args[0] == "unlock_critical" ||
args[0] == "lock_critical" ||
- args[0] == "get_unlock_ability" ||
- args[0] == "get_unlock_bootloader_nonce" ||
- args[0] == "lock_bootloader")) {
+ args[0] == "get_unlock_ability")) {
do_oem_command("flashing", &args);
- } else if (args.size() == 2 && args[0] == "unlock_bootloader") {
- do_bypass_unlock_command(&args);
} else {
syntax_error("unknown 'flashing' command %s", args[0].c_str());
}
+ } else if (command == "create-logical-partition") {
+ std::string partition = next_arg(&args);
+ std::string size = next_arg(&args);
+ fb_queue_create_partition(partition, size);
+ } else if (command == "delete-logical-partition") {
+ std::string partition = next_arg(&args);
+ fb_queue_delete_partition(partition);
+ } else if (command == "resize-logical-partition") {
+ std::string partition = next_arg(&args);
+ std::string size = next_arg(&args);
+ fb_queue_resize_partition(partition, size);
} else {
syntax_error("unknown command %s", command.c_str());
}
}
if (wants_wipe) {
- fprintf(stderr, "wiping userdata...\n");
- fb_queue_erase("userdata");
- if (set_fbe_marker) {
- fprintf(stderr, "setting FBE marker...\n");
- std::string initial_userdata_dir = create_fbemarker_tmpdir();
- if (initial_userdata_dir.empty()) {
- return 1;
+ std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
+ for (const auto& partition : partitions) {
+ std::string partition_type;
+ if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
+ if (partition_type.empty()) continue;
+ fb_queue_erase(partition);
+ if (partition == "userdata" && set_fbe_marker) {
+ fprintf(stderr, "setting FBE marker on initial userdata...\n");
+ std::string initial_userdata_dir = create_fbemarker_tmpdir();
+ fb_perform_format(partition, 1, "", "", initial_userdata_dir);
+ delete_fbemarker_tmpdir(initial_userdata_dir);
+ } else {
+ fb_perform_format(partition, 1, "", "", "");
}
- fb_perform_format(transport, "userdata", 1, "", "", initial_userdata_dir);
- delete_fbemarker_tmpdir(initial_userdata_dir);
- } else {
- fb_perform_format(transport, "userdata", 1, "", "", "");
- }
-
- std::string cache_type;
- if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
- fprintf(stderr, "wiping cache...\n");
- fb_queue_erase("cache");
- fb_perform_format(transport, "cache", 1, "", "", "");
}
}
if (wants_set_active) {
- fb_set_active(next_active.c_str());
+ fb_set_active(next_active);
}
if (wants_reboot && !skip_reboot) {
fb_queue_reboot();
@@ -1861,10 +1733,38 @@
} else if (wants_reboot_bootloader) {
fb_queue_command("reboot-bootloader", "rebooting into bootloader");
fb_queue_wait_for_disconnect();
- } else if (wants_reboot_emergency) {
- fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
+ } else if (wants_reboot_recovery) {
+ fb_queue_command("reboot-recovery", "rebooting into recovery");
+ fb_queue_wait_for_disconnect();
+ } else if (wants_reboot_fastboot) {
+ fb_queue_command("reboot-fastboot", "rebooting into fastboot");
fb_queue_wait_for_disconnect();
}
- return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
+ int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
+ fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
+ return status;
+}
+
+void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
+ unsigned year, month, day;
+ if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
+ syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
+ }
+ if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
+ if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
+ hdr->SetOsPatchLevel(year, month);
+}
+
+void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
+ unsigned major = 0, minor = 0, patch = 0;
+ std::vector<std::string> versions = android::base::Split(arg, ".");
+ if (versions.size() < 1 || versions.size() > 3 ||
+ (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
+ (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
+ (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
+ (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
+ syntax_error("bad OS version: %s", arg);
+ }
+ hdr->SetOsVersion(major, minor, patch);
}
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
deleted file mode 100644
index f4faa21..0000000
--- a/fastboot/fastboot.h
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _FASTBOOT_H_
-#define _FASTBOOT_H_
-
-#include <inttypes.h>
-#include <stdlib.h>
-
-#include <string>
-
-#include "transport.h"
-
-struct sparse_file;
-
-/* protocol.c - fastboot protocol */
-int fb_command(Transport* transport, const char* cmd);
-int fb_command_response(Transport* transport, const char* cmd, char* response);
-int64_t fb_download_data(Transport* transport, const void* data, uint32_t size);
-int64_t fb_download_data_fd(Transport* transport, int fd, uint32_t size);
-int fb_download_data_sparse(Transport* transport, struct sparse_file* s);
-int64_t fb_upload_data(Transport* transport, const char* outfile);
-const std::string fb_get_error();
-
-#define FB_COMMAND_SZ 64
-#define FB_RESPONSE_SZ 64
-
-/* engine.c - high level command queue engine */
-bool fb_getvar(Transport* transport, const std::string& key, std::string* value);
-void fb_queue_flash(const char *ptn, void *data, uint32_t sz);
-void fb_queue_flash_fd(const char *ptn, int fd, uint32_t sz);
-void fb_queue_flash_sparse(const char* ptn, struct sparse_file* s, uint32_t sz, size_t current,
- size_t total);
-void fb_queue_erase(const char *ptn);
-void fb_queue_format(const char *ptn, int skip_if_not_supported, int32_t max_chunk_sz);
-void fb_queue_require(const char *prod, const char *var, bool invert,
- size_t nvalues, const char **value);
-void fb_queue_display(const char *var, const char *prettyname);
-void fb_queue_query_save(const char *var, char *dest, uint32_t dest_size);
-void fb_queue_reboot(void);
-void fb_queue_command(const char *cmd, const char *msg);
-void fb_queue_download(const char *name, void *data, uint32_t size);
-void fb_queue_download_fd(const char *name, int fd, uint32_t sz);
-void fb_queue_upload(const char* outfile);
-void fb_queue_notice(const char *notice);
-void fb_queue_wait_for_disconnect(void);
-int64_t fb_execute_queue(Transport* transport);
-void fb_set_active(const char *slot);
-
-/* util stuff */
-double now();
-char *mkmsg(const char *fmt, ...);
-
-// These printf-like functions are implemented in terms of vsnprintf, so they
-// use the same attribute for compile-time format string checking. On Windows,
-// if the mingw version of vsnprintf is used, use `gnu_printf' which allows z
-// in %zd and PRIu64 (and related) to be recognized by the compile-time
-// checking.
-#define FASTBOOT_FORMAT_ARCHETYPE __printf__
-#ifdef __USE_MINGW_ANSI_STDIO
-#if __USE_MINGW_ANSI_STDIO
-#undef FASTBOOT_FORMAT_ARCHETYPE
-#define FASTBOOT_FORMAT_ARCHETYPE gnu_printf
-#endif
-#endif
-void die(const char* fmt, ...) __attribute__((__noreturn__))
-__attribute__((__format__(FASTBOOT_FORMAT_ARCHETYPE, 1, 2)));
-#undef FASTBOOT_FORMAT_ARCHETYPE
-
-/* Current product */
-extern char cur_product[FB_RESPONSE_SZ + 1];
-
-#endif
diff --git a/fastboot/fastboot_driver.cpp b/fastboot/fastboot_driver.cpp
new file mode 100644
index 0000000..55ca65d
--- /dev/null
+++ b/fastboot/fastboot_driver.cpp
@@ -0,0 +1,542 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include "fastboot_driver.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <algorithm>
+#include <chrono>
+#include <fstream>
+#include <memory>
+#include <regex>
+#include <vector>
+
+#include <android-base/file.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
+#include <utils/FileMap.h>
+#include "fastboot_driver.h"
+#include "transport.h"
+
+namespace fastboot {
+
+/*************************** PUBLIC *******************************/
+FastBootDriver::FastBootDriver(Transport* transport, std::function<void(std::string&)> info,
+ bool no_checks)
+ : transport(transport) {
+ info_cb_ = info;
+ disable_checks_ = no_checks;
+}
+
+RetCode FastBootDriver::Boot(std::string* response, std::vector<std::string>* info) {
+ return RawCommand(Commands::BOOT, response, info);
+}
+
+RetCode FastBootDriver::Continue(std::string* response, std::vector<std::string>* info) {
+ return RawCommand(Commands::CONTINUE, response, info);
+}
+
+RetCode FastBootDriver::Erase(const std::string& part, std::string* response,
+ std::vector<std::string>* info) {
+ return RawCommand(Commands::ERASE + part, response, info);
+}
+
+RetCode FastBootDriver::Flash(const std::string& part, std::string* response,
+ std::vector<std::string>* info) {
+ return RawCommand(Commands::FLASH + part, response, info);
+}
+
+RetCode FastBootDriver::GetVar(const std::string& key, std::string* val,
+ std::vector<std::string>* info) {
+ return RawCommand(Commands::GET_VAR + key, val, info);
+}
+
+RetCode FastBootDriver::GetVarAll(std::vector<std::string>* response) {
+ std::string tmp;
+ return GetVar("all", &tmp, response);
+}
+
+RetCode FastBootDriver::Powerdown(std::string* response, std::vector<std::string>* info) {
+ return RawCommand(Commands::POWERDOWN, response, info);
+}
+
+RetCode FastBootDriver::Reboot(std::string* response, std::vector<std::string>* info) {
+ return RawCommand(Commands::REBOOT, response, info);
+}
+
+RetCode FastBootDriver::SetActive(const std::string& part, std::string* response,
+ std::vector<std::string>* info) {
+ return RawCommand(Commands::SET_ACTIVE + part, response, info);
+}
+
+RetCode FastBootDriver::Verify(uint32_t num, std::string* response, std::vector<std::string>* info) {
+ std::string cmd = android::base::StringPrintf("%s%08" PRIx32, Commands::VERIFY.c_str(), num);
+ return RawCommand(cmd, response, info);
+}
+
+RetCode FastBootDriver::FlashPartition(const std::string& part, const std::vector<char>& data) {
+ RetCode ret;
+ if ((ret = Download(data))) {
+ return ret;
+ }
+ return RawCommand(Commands::FLASH + part);
+}
+
+RetCode FastBootDriver::FlashPartition(const std::string& part, int fd, uint32_t sz) {
+ RetCode ret;
+ if ((ret = Download(fd, sz))) {
+ return ret;
+ }
+ return RawCommand(Commands::FLASH + part);
+}
+
+RetCode FastBootDriver::FlashPartition(const std::string& part, sparse_file* s) {
+ RetCode ret;
+ if ((ret = Download(s))) {
+ return ret;
+ }
+ return RawCommand(Commands::FLASH + part);
+}
+
+RetCode FastBootDriver::Partitions(std::vector<std::tuple<std::string, uint32_t>>* parts) {
+ std::vector<std::string> all;
+ RetCode ret;
+ if ((ret = GetVarAll(&all))) {
+ return ret;
+ }
+
+ std::regex reg("partition-size[[:s:]]*:[[:s:]]*([[:w:]]+)[[:s:]]*:[[:s:]]*0x([[:xdigit:]]+)");
+ std::smatch sm;
+
+ for (auto& s : all) {
+ if (std::regex_match(s, sm, reg)) {
+ std::string m1(sm[1]);
+ std::string m2(sm[2]);
+ uint32_t tmp = strtol(m2.c_str(), 0, 16);
+ parts->push_back(std::make_tuple(m1, tmp));
+ }
+ }
+ return SUCCESS;
+}
+
+RetCode FastBootDriver::Require(const std::string& var, const std::vector<std::string>& allowed,
+ bool* reqmet, bool invert) {
+ *reqmet = invert;
+ RetCode ret;
+ std::string response;
+ if ((ret = GetVar(var, &response))) {
+ return ret;
+ }
+
+ // Now check if we have a match
+ for (const auto s : allowed) {
+ // If it ends in *, and starting substring match
+ if (response == s || (s.length() && s.back() == '*' &&
+ !response.compare(0, s.length() - 1, s, 0, s.length() - 1))) {
+ *reqmet = !invert;
+ break;
+ }
+ }
+
+ return SUCCESS;
+}
+
+RetCode FastBootDriver::Download(int fd, size_t size, std::string* response,
+ std::vector<std::string>* info) {
+ RetCode ret;
+
+ if ((size <= 0 || size > MAX_DOWNLOAD_SIZE) && !disable_checks_) {
+ error_ = "File is too large to download";
+ return BAD_ARG;
+ }
+
+ uint32_t u32size = static_cast<uint32_t>(size);
+ if ((ret = DownloadCommand(u32size, response, info))) {
+ return ret;
+ }
+
+ // Write the buffer
+ if ((ret = SendBuffer(fd, size))) {
+ return ret;
+ }
+
+ // Wait for response
+ return HandleResponse(response, info);
+}
+
+RetCode FastBootDriver::Download(const std::vector<char>& buf, std::string* response,
+ std::vector<std::string>* info) {
+ return Download(buf.data(), buf.size(), response, info);
+}
+
+RetCode FastBootDriver::Download(const char* buf, uint32_t size, std::string* response,
+ std::vector<std::string>* info) {
+ RetCode ret;
+ error_ = "";
+ if ((size == 0 || size > MAX_DOWNLOAD_SIZE) && !disable_checks_) {
+ error_ = "Buffer is too large or 0 bytes";
+ return BAD_ARG;
+ }
+
+ if ((ret = DownloadCommand(size, response, info))) {
+ return ret;
+ }
+
+ // Write the buffer
+ if ((ret = SendBuffer(buf, size))) {
+ return ret;
+ }
+
+ // Wait for response
+ return HandleResponse(response, info);
+}
+
+RetCode FastBootDriver::Download(sparse_file* s, std::string* response,
+ std::vector<std::string>* info) {
+ error_ = "";
+ int64_t size = sparse_file_len(s, true, false);
+ if (size <= 0 || size > MAX_DOWNLOAD_SIZE) {
+ error_ = "Sparse file is too large or invalid";
+ return BAD_ARG;
+ }
+
+ RetCode ret;
+ uint32_t u32size = static_cast<uint32_t>(size);
+ if ((ret = DownloadCommand(u32size, response, info))) {
+ return ret;
+ }
+
+ struct SparseCBPrivate {
+ FastBootDriver* self;
+ std::vector<char> tpbuf;
+ } cb_priv;
+ cb_priv.self = this;
+
+ auto cb = [](void* priv, const void* buf, size_t len) -> int {
+ SparseCBPrivate* data = static_cast<SparseCBPrivate*>(priv);
+ const char* cbuf = static_cast<const char*>(buf);
+ return data->self->SparseWriteCallback(data->tpbuf, cbuf, len);
+ };
+
+ if (sparse_file_callback(s, true, false, cb, &cb_priv) < 0) {
+ error_ = "Error reading sparse file";
+ return IO_ERROR;
+ }
+
+ // Now flush
+ if (cb_priv.tpbuf.size() && (ret = SendBuffer(cb_priv.tpbuf))) {
+ return ret;
+ }
+
+ return HandleResponse(response, info);
+}
+
+RetCode FastBootDriver::Upload(const std::string& outfile, std::string* response,
+ std::vector<std::string>* info) {
+ RetCode ret;
+ int dsize;
+ if ((ret = RawCommand(Commands::UPLOAD, response, info, &dsize))) {
+ error_ = "Upload request failed: " + error_;
+ return ret;
+ }
+
+ if (!dsize) {
+ error_ = "Upload request failed, device reports 0 bytes available";
+ return BAD_DEV_RESP;
+ }
+
+ std::vector<char> data;
+ data.resize(dsize);
+
+ if ((ret = ReadBuffer(data))) {
+ return ret;
+ }
+
+ std::ofstream ofs;
+ ofs.open(outfile, std::ofstream::out | std::ofstream::binary);
+ if (ofs.fail()) {
+ error_ = android::base::StringPrintf("Failed to open '%s'", outfile.c_str());
+ return IO_ERROR;
+ }
+ ofs.write(data.data(), data.size());
+ if (ofs.fail() || ofs.bad()) {
+ error_ = android::base::StringPrintf("Writing to '%s' failed", outfile.c_str());
+ return IO_ERROR;
+ }
+ ofs.close();
+
+ return HandleResponse(response, info);
+}
+
+// Helpers
+void FastBootDriver::SetInfoCallback(std::function<void(std::string&)> info) {
+ info_cb_ = info;
+}
+
+const std::string FastBootDriver::RCString(RetCode rc) {
+ switch (rc) {
+ case SUCCESS:
+ return std::string("Success");
+
+ case BAD_ARG:
+ return std::string("Invalid Argument");
+
+ case IO_ERROR:
+ return std::string("I/O Error");
+
+ case BAD_DEV_RESP:
+ return std::string("Invalid Device Response");
+
+ case DEVICE_FAIL:
+ return std::string("Device Error");
+
+ case TIMEOUT:
+ return std::string("Timeout");
+
+ default:
+ return std::string("Unknown Error");
+ }
+}
+
+std::string FastBootDriver::Error() {
+ return error_;
+}
+
+RetCode FastBootDriver::WaitForDisconnect() {
+ return transport->WaitForDisconnect() ? IO_ERROR : SUCCESS;
+}
+
+/****************************** PROTECTED *************************************/
+RetCode FastBootDriver::RawCommand(const std::string& cmd, std::string* response,
+ std::vector<std::string>* info, int* dsize) {
+ error_ = ""; // Clear any pending error
+ if (cmd.size() > FB_COMMAND_SZ && !disable_checks_) {
+ error_ = "Command length to RawCommand() is too long";
+ return BAD_ARG;
+ }
+
+ if (transport->Write(cmd.c_str(), cmd.size()) != static_cast<int>(cmd.size())) {
+ error_ = ErrnoStr("Write to device failed");
+ return IO_ERROR;
+ }
+
+ // Read the response
+ return HandleResponse(response, info, dsize);
+}
+
+RetCode FastBootDriver::DownloadCommand(uint32_t size, std::string* response,
+ std::vector<std::string>* info) {
+ std::string cmd(android::base::StringPrintf("%s%08" PRIx32, Commands::DOWNLOAD.c_str(), size));
+ RetCode ret;
+ if ((ret = RawCommand(cmd, response, info))) {
+ return ret;
+ }
+ return SUCCESS;
+}
+
+RetCode FastBootDriver::HandleResponse(std::string* response, std::vector<std::string>* info,
+ int* dsize) {
+ char status[FB_RESPONSE_SZ + 1];
+ auto start = std::chrono::system_clock::now();
+
+ auto set_response = [response](std::string s) {
+ if (response) *response = std::move(s);
+ };
+ auto add_info = [info](std::string s) {
+ if (info) info->push_back(std::move(s));
+ };
+
+ // erase response
+ set_response("");
+ while ((std::chrono::system_clock::now() - start) < std::chrono::seconds(RESP_TIMEOUT)) {
+ int r = transport->Read(status, FB_RESPONSE_SZ);
+ if (r < 0) {
+ error_ = ErrnoStr("Status read failed");
+ return IO_ERROR;
+ }
+
+ status[r] = '\0'; // Need the null terminator
+ std::string input(status);
+ if (android::base::StartsWith(input, "INFO")) {
+ std::string tmp = input.substr(strlen("INFO"));
+ info_cb_(tmp);
+ add_info(std::move(tmp));
+ } else if (android::base::StartsWith(input, "OKAY")) {
+ set_response(input.substr(strlen("OKAY")));
+ return SUCCESS;
+ } else if (android::base::StartsWith(input, "FAIL")) {
+ error_ = android::base::StringPrintf("remote: '%s'", status + strlen("FAIL"));
+ set_response(input.substr(strlen("FAIL")));
+ return DEVICE_FAIL;
+ } else if (android::base::StartsWith(input, "DATA")) {
+ std::string tmp = input.substr(strlen("DATA"));
+ uint32_t num = strtol(tmp.c_str(), 0, 16);
+ if (num > MAX_DOWNLOAD_SIZE) {
+ error_ = android::base::StringPrintf("Data size too large (%d)", num);
+ return BAD_DEV_RESP;
+ }
+ if (dsize) *dsize = num;
+ set_response(std::move(tmp));
+ return SUCCESS;
+ } else {
+ error_ = android::base::StringPrintf("Device sent unknown status code: %s", status);
+ return BAD_DEV_RESP;
+ }
+
+ } // End of while loop
+
+ return TIMEOUT;
+}
+
+std::string FastBootDriver::ErrnoStr(const std::string& msg) {
+ return android::base::StringPrintf("%s (%s)", msg.c_str(), strerror(errno));
+}
+
+const std::string FastBootDriver::Commands::BOOT = "boot";
+const std::string FastBootDriver::Commands::CONTINUE = "continue";
+const std::string FastBootDriver::Commands::DOWNLOAD = "download:";
+const std::string FastBootDriver::Commands::ERASE = "erase:";
+const std::string FastBootDriver::Commands::FLASH = "flash:";
+const std::string FastBootDriver::Commands::GET_VAR = "getvar:";
+const std::string FastBootDriver::Commands::POWERDOWN = "powerdown";
+const std::string FastBootDriver::Commands::REBOOT = "reboot";
+const std::string FastBootDriver::Commands::SET_ACTIVE = "set_active:";
+const std::string FastBootDriver::Commands::UPLOAD = "upload";
+const std::string FastBootDriver::Commands::VERIFY = "verify:";
+
+/******************************* PRIVATE **************************************/
+RetCode FastBootDriver::SendBuffer(int fd, size_t size) {
+ static constexpr uint32_t MAX_MAP_SIZE = 512 * 1024 * 1024;
+ off64_t offset = 0;
+ uint32_t remaining = size;
+ RetCode ret;
+
+ while (remaining) {
+ // Memory map the file
+ android::FileMap filemap;
+ size_t len = std::min(remaining, MAX_MAP_SIZE);
+
+ if (!filemap.create(NULL, fd, offset, len, true)) {
+ error_ = "Creating filemap failed";
+ return IO_ERROR;
+ }
+
+ if ((ret = SendBuffer(filemap.getDataPtr(), len))) {
+ return ret;
+ }
+
+ remaining -= len;
+ offset += len;
+ }
+
+ return SUCCESS;
+}
+
+RetCode FastBootDriver::SendBuffer(const std::vector<char>& buf) {
+ // Write the buffer
+ return SendBuffer(buf.data(), buf.size());
+}
+
+RetCode FastBootDriver::SendBuffer(const void* buf, size_t size) {
+ // ioctl on 0-length buffer causes freezing
+ if (!size) {
+ return BAD_ARG;
+ }
+ // Write the buffer
+ ssize_t tmp = transport->Write(buf, size);
+
+ if (tmp < 0) {
+ error_ = ErrnoStr("Write to device failed in SendBuffer()");
+ return IO_ERROR;
+ } else if (static_cast<size_t>(tmp) != size) {
+ error_ = android::base::StringPrintf("Failed to write all %zu bytes", size);
+
+ return IO_ERROR;
+ }
+
+ return SUCCESS;
+}
+
+RetCode FastBootDriver::ReadBuffer(std::vector<char>& buf) {
+ // Read the buffer
+ return ReadBuffer(buf.data(), buf.size());
+}
+
+RetCode FastBootDriver::ReadBuffer(void* buf, size_t size) {
+ // Read the buffer
+ ssize_t tmp = transport->Read(buf, size);
+
+ if (tmp < 0) {
+ error_ = ErrnoStr("Read from device failed in ReadBuffer()");
+ return IO_ERROR;
+ } else if (static_cast<size_t>(tmp) != size) {
+ error_ = android::base::StringPrintf("Failed to read all %zu bytes", size);
+ return IO_ERROR;
+ }
+
+ return SUCCESS;
+}
+
+int FastBootDriver::SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len) {
+ size_t total = 0;
+ size_t to_write = std::min(TRANSPORT_CHUNK_SIZE - tpbuf.size(), len);
+
+ // Handle the residual
+ tpbuf.insert(tpbuf.end(), data, data + to_write);
+ if (tpbuf.size() < TRANSPORT_CHUNK_SIZE) { // Nothing enough to send rn
+ return 0;
+ }
+
+ if (SendBuffer(tpbuf)) {
+ error_ = ErrnoStr("Send failed in SparseWriteCallback()");
+ return -1;
+ }
+ tpbuf.clear();
+ total += to_write;
+
+ // Now we need to send a multiple of chunk size
+ size_t nchunks = (len - total) / TRANSPORT_CHUNK_SIZE;
+ size_t nbytes = TRANSPORT_CHUNK_SIZE * nchunks;
+ if (nbytes && SendBuffer(data + total, nbytes)) { // Don't send a ZLP
+ error_ = ErrnoStr("Send failed in SparseWriteCallback()");
+ return -1;
+ }
+ total += nbytes;
+
+ if (len - total > 0) { // We have residual data to save for next time
+ tpbuf.assign(data + total, data + len);
+ }
+
+ return 0;
+}
+
+} // End namespace fastboot
diff --git a/fastboot/fastboot_driver.h b/fastboot/fastboot_driver.h
new file mode 100644
index 0000000..dd199c0
--- /dev/null
+++ b/fastboot/fastboot_driver.h
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#pragma once
+#include <cstdlib>
+#include <deque>
+#include <limits>
+#include <string>
+#include <vector>
+
+#include <android-base/logging.h>
+#include <android-base/stringprintf.h>
+#include <bootimg.h>
+#include <inttypes.h>
+#include <sparse/sparse.h>
+#include "transport.h"
+
+class Transport;
+
+namespace fastboot {
+
+static constexpr int FB_COMMAND_SZ = 64;
+static constexpr int FB_RESPONSE_SZ = 64;
+
+enum RetCode : int {
+ SUCCESS = 0,
+ BAD_ARG,
+ IO_ERROR,
+ BAD_DEV_RESP,
+ DEVICE_FAIL,
+ TIMEOUT,
+};
+
+class FastBootDriver {
+ public:
+ static constexpr int RESP_TIMEOUT = 30; // 30 seconds
+ static constexpr uint32_t MAX_DOWNLOAD_SIZE = std::numeric_limits<uint32_t>::max();
+ static constexpr size_t TRANSPORT_CHUNK_SIZE = 1024;
+
+ FastBootDriver(Transport* transport,
+ std::function<void(std::string&)> info = [](std::string&) {},
+ bool no_checks = false);
+
+ RetCode Boot(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
+ RetCode Continue(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
+ RetCode Download(int fd, size_t size, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr);
+ RetCode Download(const std::vector<char>& buf, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr);
+ // This will be removed after fastboot is modified to use a vector
+ RetCode Download(const char* buf, uint32_t size, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr);
+ RetCode Download(sparse_file* s, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr);
+ RetCode Erase(const std::string& part, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr);
+ RetCode Flash(const std::string& part, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr);
+ RetCode GetVar(const std::string& key, std::string* val,
+ std::vector<std::string>* info = nullptr);
+ RetCode GetVarAll(std::vector<std::string>* response);
+ RetCode Powerdown(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
+ RetCode Reboot(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
+ RetCode SetActive(const std::string& part, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr);
+ RetCode Upload(const std::string& outfile, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr);
+ RetCode Verify(uint32_t num, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr);
+
+ /* HIGHER LEVEL COMMANDS -- Composed of the commands above */
+ RetCode FlashPartition(const std::string& part, const std::vector<char>& data);
+ RetCode FlashPartition(const std::string& part, int fd, uint32_t sz);
+ RetCode FlashPartition(const std::string& part, sparse_file* s);
+
+ RetCode Partitions(std::vector<std::tuple<std::string, uint32_t>>* parts);
+ RetCode Require(const std::string& var, const std::vector<std::string>& allowed, bool* reqmet,
+ bool invert = false);
+
+ /* HELPERS */
+ void SetInfoCallback(std::function<void(std::string&)> info);
+ static const std::string RCString(RetCode rc);
+ std::string Error();
+ RetCode WaitForDisconnect();
+
+ // This is temporarily public for engine.cpp
+ RetCode RawCommand(const std::string& cmd, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr, int* dsize = nullptr);
+
+ protected:
+ RetCode DownloadCommand(uint32_t size, std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr);
+ RetCode HandleResponse(std::string* response = nullptr,
+ std::vector<std::string>* info = nullptr, int* dsize = nullptr);
+
+ std::string ErrnoStr(const std::string& msg);
+
+ // More like a namespace...
+ struct Commands {
+ static const std::string BOOT;
+ static const std::string CONTINUE;
+ static const std::string DOWNLOAD;
+ static const std::string ERASE;
+ static const std::string FLASH;
+ static const std::string GET_VAR;
+ static const std::string POWERDOWN;
+ static const std::string REBOOT;
+ static const std::string SET_ACTIVE;
+ static const std::string UPLOAD;
+ static const std::string VERIFY;
+ };
+
+ Transport* const transport;
+
+ private:
+ RetCode SendBuffer(int fd, size_t size);
+ RetCode SendBuffer(const std::vector<char>& buf);
+ RetCode SendBuffer(const void* buf, size_t size);
+
+ RetCode ReadBuffer(std::vector<char>& buf);
+ RetCode ReadBuffer(void* buf, size_t size);
+
+ int SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len);
+
+ std::string error_;
+ std::function<void(std::string&)> info_cb_;
+ bool disable_checks_;
+};
+
+} // namespace fastboot
diff --git a/fastboot/fastboot_test.cpp b/fastboot/fastboot_test.cpp
new file mode 100644
index 0000000..43201fa
--- /dev/null
+++ b/fastboot/fastboot_test.cpp
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2018 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 "engine.h"
+
+#include <gtest/gtest.h>
+
+TEST(FastBoot, ParseOsPatchLevel) {
+ FastBootTool fb;
+ boot_img_hdr_v1 hdr;
+
+ hdr = {};
+ fb.ParseOsPatchLevel(&hdr, "2018-01-05");
+ ASSERT_EQ(2018U, 2000U + ((hdr.os_version >> 4) & 0x7f));
+ ASSERT_EQ(1U, ((hdr.os_version >> 0) & 0xf));
+
+ EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018"), "should be YYYY-MM-DD");
+ EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018-01"), "should be YYYY-MM-DD");
+ EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2128-01-05"), "year out of range");
+ EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018-13-05"), "month out of range");
+}
+
+TEST(FastBoot, ParseOsVersion) {
+ FastBootTool fb;
+ boot_img_hdr_v1 hdr;
+
+ hdr = {};
+ fb.ParseOsVersion(&hdr, "1.2.3");
+ ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f));
+ ASSERT_EQ(2U, ((hdr.os_version >> 18) & 0x7f));
+ ASSERT_EQ(3U, ((hdr.os_version >> 11) & 0x7f));
+
+ fb.ParseOsVersion(&hdr, "1.2");
+ ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f));
+ ASSERT_EQ(2U, ((hdr.os_version >> 18) & 0x7f));
+ ASSERT_EQ(0U, ((hdr.os_version >> 11) & 0x7f));
+
+ fb.ParseOsVersion(&hdr, "1");
+ ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f));
+ ASSERT_EQ(0U, ((hdr.os_version >> 18) & 0x7f));
+ ASSERT_EQ(0U, ((hdr.os_version >> 11) & 0x7f));
+
+ EXPECT_DEATH(fb.ParseOsVersion(&hdr, ""), "bad OS version");
+ EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.2.3.4"), "bad OS version");
+ EXPECT_DEATH(fb.ParseOsVersion(&hdr, "128.2.3"), "bad OS version");
+ EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.128.3"), "bad OS version");
+ EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.2.128"), "bad OS version");
+}
diff --git a/fastboot/fs.cpp b/fastboot/fs.cpp
index a1e1677..b0ac2ef 100644
--- a/fastboot/fs.cpp
+++ b/fastboot/fs.cpp
@@ -1,6 +1,5 @@
#include "fs.h"
-#include "fastboot.h"
#include <errno.h>
#include <fcntl.h>
@@ -20,6 +19,7 @@
#include <android-base/errors.h>
#include <android-base/file.h>
+#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>
@@ -177,6 +177,8 @@
mkf2fs_args.push_back("encrypt");
mkf2fs_args.push_back("-O");
mkf2fs_args.push_back("quota");
+ mkf2fs_args.push_back("-O");
+ mkf2fs_args.push_back("verity");
mkf2fs_args.push_back(fileName);
mkf2fs_args.push_back(nullptr);
diff --git a/fastboot/fs.h b/fastboot/fs.h
index c6baa7f..331100d 100644
--- a/fastboot/fs.h
+++ b/fastboot/fs.h
@@ -1,5 +1,4 @@
-#ifndef _FS_H_
-#define _FS_H_
+#pragma once
#include <string>
#include <stdint.h>
@@ -9,5 +8,3 @@
const struct fs_generator* fs_get_generator(const std::string& fs_type);
int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize,
const std::string& initial_dir, unsigned eraseBlkSize = 0, unsigned logicalBlkSize = 0);
-
-#endif
diff --git a/fastboot/main.cpp b/fastboot/main.cpp
new file mode 100644
index 0000000..c3683f7
--- /dev/null
+++ b/fastboot/main.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "engine.h"
+
+int main(int argc, char* argv[]) {
+ FastBootTool fb;
+ return fb.Main(argc, argv);
+}
diff --git a/fastboot/protocol.cpp b/fastboot/protocol.cpp
deleted file mode 100644
index dcdf8f0..0000000
--- a/fastboot/protocol.cpp
+++ /dev/null
@@ -1,358 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#define round_down(a, b) \
- ({ typeof(a) _a = (a); typeof(b) _b = (b); _a - (_a % _b); })
-
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-#include <algorithm>
-#include <vector>
-
-#include <android-base/file.h>
-#include <android-base/stringprintf.h>
-#include <android-base/unique_fd.h>
-#include <sparse/sparse.h>
-#include <utils/FileMap.h>
-
-#include "fastboot.h"
-#include "transport.h"
-
-static std::string g_error;
-
-using android::base::unique_fd;
-using android::base::WriteStringToFile;
-
-const std::string fb_get_error() {
- return g_error;
-}
-
-static int64_t check_response(Transport* transport, uint32_t size, char* response) {
- char status[65];
-
- while (true) {
- int r = transport->Read(status, 64);
- if (r < 0) {
- g_error = android::base::StringPrintf("status read failed (%s)", strerror(errno));
- transport->Close();
- return -1;
- }
- status[r] = 0;
-
- if (r < 4) {
- g_error = android::base::StringPrintf("status malformed (%d bytes)", r);
- transport->Close();
- return -1;
- }
-
- if (!memcmp(status, "INFO", 4)) {
- fprintf(stderr,"(bootloader) %s\n", status + 4);
- continue;
- }
-
- if (!memcmp(status, "OKAY", 4)) {
- if (response) {
- strcpy(response, (char*) status + 4);
- }
- return 0;
- }
-
- if (!memcmp(status, "FAIL", 4)) {
- if (r > 4) {
- g_error = android::base::StringPrintf("remote: %s", status + 4);
- } else {
- g_error = "remote failure";
- }
- return -1;
- }
-
- if (!memcmp(status, "DATA", 4) && size > 0){
- uint32_t dsize = strtol(status + 4, 0, 16);
- if (dsize > size) {
- g_error = android::base::StringPrintf("data size too large (%d)", dsize);
- transport->Close();
- return -1;
- }
- return dsize;
- }
-
- g_error = "unknown status code";
- transport->Close();
- break;
- }
-
- return -1;
-}
-
-static int64_t _command_start(Transport* transport, const char* cmd, uint32_t size, char* response) {
- size_t cmdsize = strlen(cmd);
- if (cmdsize > 64) {
- g_error = android::base::StringPrintf("command too large (%zu)", cmdsize);
- return -1;
- }
-
- if (response) {
- response[0] = 0;
- }
-
- if (transport->Write(cmd, cmdsize) != static_cast<int>(cmdsize)) {
- g_error = android::base::StringPrintf("command write failed (%s)", strerror(errno));
- transport->Close();
- return -1;
- }
-
- return check_response(transport, size, response);
-}
-
-static int64_t _command_write_data(Transport* transport, const void* data, uint32_t size) {
- int64_t r = transport->Write(data, size);
- if (r < 0) {
- g_error = android::base::StringPrintf("data write failure (%s)", strerror(errno));
- transport->Close();
- return -1;
- }
- if (r != static_cast<int64_t>(size)) {
- g_error = "data write failure (short transfer)";
- transport->Close();
- return -1;
- }
- return r;
-}
-
-static int64_t _command_read_data(Transport* transport, void* data, uint32_t size) {
- int64_t r = transport->Read(data, size);
- if (r < 0) {
- g_error = android::base::StringPrintf("data read failure (%s)", strerror(errno));
- transport->Close();
- return -1;
- }
- if (r != (static_cast<int64_t>(size))) {
- g_error = "data read failure (short transfer)";
- transport->Close();
- return -1;
- }
- return r;
-}
-
-static int64_t _command_end(Transport* transport) {
- return check_response(transport, 0, 0) < 0 ? -1 : 0;
-}
-
-static int64_t _command_send(Transport* transport, const char* cmd, const void* data, uint32_t size,
- char* response) {
- if (size == 0) {
- return -1;
- }
-
- int64_t r = _command_start(transport, cmd, size, response);
- if (r < 0) {
- return -1;
- }
- r = _command_write_data(transport, data, size);
- if (r < 0) {
- return -1;
- }
-
- r = _command_end(transport);
- if (r < 0) {
- return -1;
- }
-
- return size;
-}
-
-static int64_t _command_send_fd(Transport* transport, const char* cmd, int fd, uint32_t size,
- char* response) {
- static constexpr uint32_t MAX_MAP_SIZE = 512 * 1024 * 1024;
- off64_t offset = 0;
- uint32_t remaining = size;
-
- if (_command_start(transport, cmd, size, response) < 0) {
- return -1;
- }
-
- while (remaining) {
- android::FileMap filemap;
- size_t len = std::min(remaining, MAX_MAP_SIZE);
-
- if (!filemap.create(NULL, fd, offset, len, true)) {
- return -1;
- }
-
- if (_command_write_data(transport, filemap.getDataPtr(), len) < 0) {
- return -1;
- }
-
- remaining -= len;
- offset += len;
- }
-
- if (_command_end(transport) < 0) {
- return -1;
- }
-
- return size;
-}
-
-static int _command_send_no_data(Transport* transport, const char* cmd, char* response) {
- return _command_start(transport, cmd, 0, response);
-}
-
-int fb_command(Transport* transport, const char* cmd) {
- return _command_send_no_data(transport, cmd, 0);
-}
-
-int fb_command_response(Transport* transport, const char* cmd, char* response) {
- return _command_send_no_data(transport, cmd, response);
-}
-
-int64_t fb_download_data(Transport* transport, const void* data, uint32_t size) {
- std::string cmd(android::base::StringPrintf("download:%08x", size));
- return _command_send(transport, cmd.c_str(), data, size, 0) < 0 ? -1 : 0;
-}
-
-int64_t fb_download_data_fd(Transport* transport, int fd, uint32_t size) {
- std::string cmd(android::base::StringPrintf("download:%08x", size));
- return _command_send_fd(transport, cmd.c_str(), fd, size, 0) < 0 ? -1 : 0;
-}
-
-int64_t fb_upload_data(Transport* transport, const char* outfile) {
- // positive return value is the upload size sent by the device
- int64_t r = _command_start(transport, "upload", std::numeric_limits<int32_t>::max(), nullptr);
- if (r <= 0) {
- g_error = android::base::StringPrintf("command start failed (%s)", strerror(errno));
- return r;
- }
-
- std::string data;
- data.resize(r);
- if ((r = _command_read_data(transport, &data[0], data.size())) == -1) {
- return r;
- }
-
- if (!WriteStringToFile(data, outfile, true)) {
- g_error = android::base::StringPrintf("write to '%s' failed", outfile);
- return -1;
- }
-
- return _command_end(transport);
-}
-
-#define TRANSPORT_BUF_SIZE 1024
-static char transport_buf[TRANSPORT_BUF_SIZE];
-static int transport_buf_len;
-
-static int fb_download_data_sparse_write(void *priv, const void *data, int len)
-{
- int r;
- Transport* transport = reinterpret_cast<Transport*>(priv);
- int to_write;
- const char* ptr = reinterpret_cast<const char*>(data);
-
- if (transport_buf_len) {
- to_write = std::min(TRANSPORT_BUF_SIZE - transport_buf_len, len);
-
- memcpy(transport_buf + transport_buf_len, ptr, to_write);
- transport_buf_len += to_write;
- ptr += to_write;
- len -= to_write;
- }
-
- if (transport_buf_len == TRANSPORT_BUF_SIZE) {
- r = _command_write_data(transport, transport_buf, TRANSPORT_BUF_SIZE);
- if (r != TRANSPORT_BUF_SIZE) {
- return -1;
- }
- transport_buf_len = 0;
- }
-
- if (len > TRANSPORT_BUF_SIZE) {
- if (transport_buf_len > 0) {
- g_error = "internal error: transport_buf not empty";
- return -1;
- }
- to_write = round_down(len, TRANSPORT_BUF_SIZE);
- r = _command_write_data(transport, ptr, to_write);
- if (r != to_write) {
- return -1;
- }
- ptr += to_write;
- len -= to_write;
- }
-
- if (len > 0) {
- if (len > TRANSPORT_BUF_SIZE) {
- g_error = "internal error: too much left for transport_buf";
- return -1;
- }
- memcpy(transport_buf, ptr, len);
- transport_buf_len = len;
- }
-
- return 0;
-}
-
-static int fb_download_data_sparse_flush(Transport* transport) {
- if (transport_buf_len > 0) {
- int64_t r = _command_write_data(transport, transport_buf, transport_buf_len);
- if (r != static_cast<int64_t>(transport_buf_len)) {
- return -1;
- }
- transport_buf_len = 0;
- }
- return 0;
-}
-
-int fb_download_data_sparse(Transport* transport, struct sparse_file* s) {
- int size = sparse_file_len(s, true, false);
- if (size <= 0) {
- return -1;
- }
-
- std::string cmd(android::base::StringPrintf("download:%08x", size));
- int r = _command_start(transport, cmd.c_str(), size, 0);
- if (r < 0) {
- return -1;
- }
-
- r = sparse_file_callback(s, true, false, fb_download_data_sparse_write, transport);
- if (r < 0) {
- return -1;
- }
-
- r = fb_download_data_sparse_flush(transport);
- if (r < 0) {
- return -1;
- }
-
- return _command_end(transport);
-}
diff --git a/fastboot/socket.h b/fastboot/socket.h
index 7eaa0ab..e791f2c 100644
--- a/fastboot/socket.h
+++ b/fastboot/socket.h
@@ -30,8 +30,7 @@
// engine should not be using this interface directly, but instead should use a higher-level
// interface that enforces the fastboot protocol.
-#ifndef SOCKET_H_
-#define SOCKET_H_
+#pragma once
#include <functional>
#include <memory>
@@ -125,5 +124,3 @@
DISALLOW_COPY_AND_ASSIGN(Socket);
};
-
-#endif // SOCKET_H_
diff --git a/fastboot/socket_mock.h b/fastboot/socket_mock.h
index eacd6bb..6e95b16 100644
--- a/fastboot/socket_mock.h
+++ b/fastboot/socket_mock.h
@@ -26,8 +26,7 @@
* SUCH DAMAGE.
*/
-#ifndef SOCKET_MOCK_H_
-#define SOCKET_MOCK_H_
+#pragma once
#include <memory>
#include <queue>
@@ -97,5 +96,3 @@
DISALLOW_COPY_AND_ASSIGN(SocketMock);
};
-
-#endif // SOCKET_MOCK_H_
diff --git a/fastboot/tcp.h b/fastboot/tcp.h
index aa3ef13..8b638a4 100644
--- a/fastboot/tcp.h
+++ b/fastboot/tcp.h
@@ -26,8 +26,7 @@
* SUCH DAMAGE.
*/
-#ifndef TCP_H_
-#define TCP_H_
+#pragma once
#include <memory>
#include <string>
@@ -55,5 +54,3 @@
} // namespace internal
} // namespace tcp
-
-#endif // TCP_H_
diff --git a/fastboot/transport.h b/fastboot/transport.h
index 67d01f9..96b90d2 100644
--- a/fastboot/transport.h
+++ b/fastboot/transport.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef TRANSPORT_H_
-#define TRANSPORT_H_
+#pragma once
#include <android-base/macros.h>
@@ -44,5 +43,3 @@
private:
DISALLOW_COPY_AND_ASSIGN(Transport);
};
-
-#endif // TRANSPORT_H_
diff --git a/fastboot/udp.h b/fastboot/udp.h
index 14f5b35..8d37b84 100644
--- a/fastboot/udp.h
+++ b/fastboot/udp.h
@@ -26,8 +26,7 @@
* SUCH DAMAGE.
*/
-#ifndef UDP_H_
-#define UDP_H_
+#pragma once
#include <memory>
#include <string>
@@ -77,5 +76,3 @@
} // namespace internal
} // namespace udp
-
-#endif // UDP_H_
diff --git a/fastboot/usb.h b/fastboot/usb.h
index 4acf12d..7ca44c4 100644
--- a/fastboot/usb.h
+++ b/fastboot/usb.h
@@ -26,8 +26,7 @@
* SUCH DAMAGE.
*/
-#ifndef _USB_H_
-#define _USB_H_
+#pragma once
#include "transport.h"
@@ -53,8 +52,14 @@
char device_path[256];
};
+class UsbTransport : public Transport {
+ // Resets the underlying transport. Returns 0 on success.
+ // This effectively simulates unplugging and replugging
+ public:
+ virtual int Reset() = 0;
+};
+
typedef int (*ifc_match_func)(usb_ifc_info *ifc);
-Transport* usb_open(ifc_match_func callback);
-
-#endif
+// 0 is non blocking
+UsbTransport* usb_open(ifc_match_func callback, uint32_t timeout_ms = 0);
diff --git a/fastboot/usb_linux.cpp b/fastboot/usb_linux.cpp
index cdab4f1..9b779dd 100644
--- a/fastboot/usb_linux.cpp
+++ b/fastboot/usb_linux.cpp
@@ -47,12 +47,12 @@
#include <memory>
#include <thread>
-#include "fastboot.h"
#include "usb.h"
+#include "util.h"
using namespace std::chrono_literals;
-#define MAX_RETRIES 5
+#define MAX_RETRIES 2
/* Timeout in seconds for usb_wait_for_disconnect.
* It doesn't usually take long for a device to disconnect (almost always
@@ -91,18 +91,21 @@
unsigned char ep_out;
};
-class LinuxUsbTransport : public Transport {
+class LinuxUsbTransport : public UsbTransport {
public:
- explicit LinuxUsbTransport(std::unique_ptr<usb_handle> handle) : handle_(std::move(handle)) {}
+ explicit LinuxUsbTransport(std::unique_ptr<usb_handle> handle, uint32_t ms_timeout = 0)
+ : handle_(std::move(handle)), ms_timeout_(ms_timeout) {}
~LinuxUsbTransport() override = default;
ssize_t Read(void* data, size_t len) override;
ssize_t Write(const void* data, size_t len) override;
int Close() override;
+ int Reset() override;
int WaitForDisconnect() override;
private:
std::unique_ptr<usb_handle> handle_;
+ const uint32_t ms_timeout_;
DISALLOW_COPY_AND_ASSIGN(LinuxUsbTransport);
};
@@ -402,7 +405,7 @@
bulk.ep = handle_->ep_out;
bulk.len = xfer;
bulk.data = data;
- bulk.timeout = 0;
+ bulk.timeout = ms_timeout_;
n = ioctl(handle_->desc, USBDEVFS_BULK, &bulk);
if(n != xfer) {
@@ -436,7 +439,7 @@
bulk.ep = handle_->ep_in;
bulk.len = xfer;
bulk.data = data;
- bulk.timeout = 0;
+ bulk.timeout = ms_timeout_;
retry = 0;
do {
@@ -447,7 +450,7 @@
if (n < 0) {
DBG1("ERROR: n = %d, errno = %d (%s)\n",n, errno, strerror(errno));
if (++retry > MAX_RETRIES) return -1;
- std::this_thread::sleep_for(1s);
+ std::this_thread::sleep_for(100ms);
}
} while (n < 0);
@@ -477,10 +480,19 @@
return 0;
}
-Transport* usb_open(ifc_match_func callback)
-{
+int LinuxUsbTransport::Reset() {
+ int ret = 0;
+ // We reset the USB connection
+ if ((ret = ioctl(handle_->desc, USBDEVFS_RESET, 0))) {
+ return ret;
+ }
+
+ return 0;
+}
+
+UsbTransport* usb_open(ifc_match_func callback, uint32_t timeout_ms) {
std::unique_ptr<usb_handle> handle = find_usb_device("/sys/bus/usb/devices", callback);
- return handle ? new LinuxUsbTransport(std::move(handle)) : nullptr;
+ return handle ? new LinuxUsbTransport(std::move(handle), timeout_ms) : nullptr;
}
/* Wait for the system to notice the device is gone, so that a subsequent
diff --git a/fastboot/usb_osx.cpp b/fastboot/usb_osx.cpp
index e95b049..4d48f6e 100644
--- a/fastboot/usb_osx.cpp
+++ b/fastboot/usb_osx.cpp
@@ -65,17 +65,21 @@
unsigned int zero_mask;
};
-class OsxUsbTransport : public Transport {
+class OsxUsbTransport : public UsbTransport {
public:
- OsxUsbTransport(std::unique_ptr<usb_handle> handle) : handle_(std::move(handle)) {}
+ // A timeout of 0 is blocking
+ OsxUsbTransport(std::unique_ptr<usb_handle> handle, uint32_t ms_timeout = 0)
+ : handle_(std::move(handle)), ms_timeout_(ms_timeout) {}
~OsxUsbTransport() override = default;
ssize_t Read(void* data, size_t len) override;
ssize_t Write(const void* data, size_t len) override;
int Close() override;
+ int Reset() override;
private:
std::unique_ptr<usb_handle> handle_;
+ const uint32_t ms_timeout_;
DISALLOW_COPY_AND_ASSIGN(OsxUsbTransport);
};
@@ -456,7 +460,7 @@
* Definitions of this file's public functions.
*/
-Transport* usb_open(ifc_match_func callback) {
+UsbTransport* usb_open(ifc_match_func callback, uint32_t timeout_ms) {
std::unique_ptr<usb_handle> handle;
if (init_usb(callback, &handle) < 0) {
@@ -464,7 +468,7 @@
return nullptr;
}
- return new OsxUsbTransport(std::move(handle));
+ return new OsxUsbTransport(std::move(handle), timeout_ms);
}
int OsxUsbTransport::Close() {
@@ -472,6 +476,19 @@
return 0;
}
+/*
+ TODO: this SHOULD be easy to do with ResetDevice() from IOUSBDeviceInterface.
+ However to perform operations that manipulate the state of the device, you must
+ claim ownership of the device with USBDeviceOpenSeize(). However, this operation
+ always fails with kIOReturnExclusiveAccess.
+ It seems that the kext com.apple.driver.usb.AppleUSBHostCompositeDevice
+ always loads and claims ownership of the device and refuses to give it up.
+*/
+int OsxUsbTransport::Reset() {
+ ERR("USB reset is currently unsupported on osx\n");
+ return -1;
+}
+
ssize_t OsxUsbTransport::Read(void* data, size_t len) {
IOReturn result;
UInt32 numBytes = len;
@@ -494,7 +511,14 @@
return -1;
}
- result = (*handle_->interface)->ReadPipe(handle_->interface, handle_->bulkIn, data, &numBytes);
+ if (!ms_timeout_) {
+ result = (*handle_->interface)
+ ->ReadPipe(handle_->interface, handle_->bulkIn, data, &numBytes);
+ } else {
+ result = (*handle_->interface)
+ ->ReadPipeTO(handle_->interface, handle_->bulkIn, data, &numBytes,
+ ms_timeout_, ms_timeout_);
+ }
if (result == 0) {
return (int) numBytes;
@@ -541,8 +565,16 @@
int lenToSend = lenRemaining > maxLenToSend
? maxLenToSend : lenRemaining;
- result = (*handle_->interface)->WritePipe(
- handle_->interface, handle_->bulkOut, (void *)data, lenToSend);
+ if (!ms_timeout_) { // blocking
+ result = (*handle_->interface)
+ ->WritePipe(handle_->interface, handle_->bulkOut, (void*)data,
+ lenToSend);
+ } else {
+ result = (*handle_->interface)
+ ->WritePipeTO(handle_->interface, handle_->bulkOut, (void*)data,
+ lenToSend, ms_timeout_, ms_timeout_);
+ }
+
if (result != 0) break;
lenRemaining -= lenToSend;
diff --git a/fastboot/usb_windows.cpp b/fastboot/usb_windows.cpp
index 3dab5ac..8c60a71 100644
--- a/fastboot/usb_windows.cpp
+++ b/fastboot/usb_windows.cpp
@@ -66,7 +66,7 @@
std::string interface_name;
};
-class WindowsUsbTransport : public Transport {
+class WindowsUsbTransport : public UsbTransport {
public:
WindowsUsbTransport(std::unique_ptr<usb_handle> handle) : handle_(std::move(handle)) {}
~WindowsUsbTransport() override = default;
@@ -74,6 +74,7 @@
ssize_t Read(void* data, size_t len) override;
ssize_t Write(const void* data, size_t len) override;
int Close() override;
+ int Reset() override;
private:
std::unique_ptr<usb_handle> handle_;
@@ -106,6 +107,7 @@
if (nullptr == ret->adb_interface) {
errno = GetLastError();
+ DBG("failed to open interface %S\n", interface_name);
return nullptr;
}
@@ -157,7 +159,7 @@
unsigned count = 0;
int ret;
- DBG("usb_write %d\n", len);
+ DBG("usb_write %zu\n", len);
if (nullptr != handle_) {
// Perform write
while(len > 0) {
@@ -195,7 +197,7 @@
unsigned long read = 0;
int ret;
- DBG("usb_read %d\n", len);
+ DBG("usb_read %zu\n", len);
if (nullptr != handle_) {
while (1) {
int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len;
@@ -260,6 +262,12 @@
return 0;
}
+int WindowsUsbTransport::Reset() {
+ DBG("usb_reset currently unsupported\n\n");
+ // TODO, this is a bit complicated since it is using ADB
+ return -1;
+}
+
int recognized_device(usb_handle* handle, ifc_match_func callback) {
struct usb_ifc_info info;
USB_DEVICE_DESCRIPTOR device_desc;
@@ -269,19 +277,22 @@
return 0;
// Check vendor and product id first
- if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
- &device_desc)) {
+ if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, &device_desc)) {
+ DBG("skipping device %x:%x\n", device_desc.idVendor, device_desc.idProduct);
return 0;
}
// Then check interface properties
- if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
- &interf_desc)) {
+ if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, &interf_desc)) {
+ DBG("skipping device %x:%x, failed to find interface\n", device_desc.idVendor,
+ device_desc.idProduct);
return 0;
}
// Must have two endpoints
if (2 != interf_desc.bNumEndpoints) {
+ DBG("skipping device %x:%x, incorrect number of endpoints\n", device_desc.idVendor,
+ device_desc.idProduct);
return 0;
}
@@ -305,9 +316,13 @@
info.device_path[0] = 0;
if (callback(&info) == 0) {
+ DBG("skipping device %x:%x, not selected by callback\n", device_desc.idVendor,
+ device_desc.idProduct);
return 1;
}
+ DBG("found device %x:%x (%s)\n", device_desc.idVendor, device_desc.idProduct,
+ info.serial_number);
return 0;
}
@@ -338,6 +353,7 @@
}
*copy_name = '\0';
+ DBG("attempting to open interface %S\n", next_interface->device_name);
handle = do_usb_open(next_interface->device_name);
if (NULL != handle) {
// Lets see if this interface (device) belongs to us
@@ -357,8 +373,7 @@
return handle;
}
-Transport* usb_open(ifc_match_func callback)
-{
+UsbTransport* usb_open(ifc_match_func callback, uint32_t) {
std::unique_ptr<usb_handle> handle = find_usb_device(callback);
return handle ? new WindowsUsbTransport(std::move(handle)) : nullptr;
}
diff --git a/fastboot/usbtest.cpp b/fastboot/usbtest.cpp
deleted file mode 100644
index 9423c6d..0000000
--- a/fastboot/usbtest.cpp
+++ /dev/null
@@ -1,212 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-#include <sys/time.h>
-
-#include "usb.h"
-
-static unsigned arg_size = 4096;
-static unsigned arg_count = 4096;
-
-long long NOW(void)
-{
- struct timeval tv;
- gettimeofday(&tv, 0);
-
- return (((long long) tv.tv_sec) * ((long long) 1000000)) +
- (((long long) tv.tv_usec));
-}
-
-int printifc(usb_ifc_info *info)
-{
- printf("dev: csp=%02x/%02x/%02x v=%04x p=%04x ",
- info->dev_class, info->dev_subclass, info->dev_protocol,
- info->dev_vendor, info->dev_product);
- printf("ifc: csp=%02x/%02x/%02x%s%s\n",
- info->ifc_class, info->ifc_subclass, info->ifc_protocol,
- info->has_bulk_in ? " in" : "",
- info->has_bulk_out ? " out" : "");
- return -1;
-}
-
-int match_null(usb_ifc_info *info)
-{
- if(info->dev_vendor != 0x18d1) return -1;
- if(info->ifc_class != 0xff) return -1;
- if(info->ifc_subclass != 0xfe) return -1;
- if(info->ifc_protocol != 0x01) return -1;
- return 0;
-}
-
-int match_zero(usb_ifc_info *info)
-{
- if(info->dev_vendor != 0x18d1) return -1;
- if(info->ifc_class != 0xff) return -1;
- if(info->ifc_subclass != 0xfe) return -1;
- if(info->ifc_protocol != 0x02) return -1;
- return 0;
-}
-
-int match_loop(usb_ifc_info *info)
-{
- if(info->dev_vendor != 0x18d1) return -1;
- if(info->ifc_class != 0xff) return -1;
- if(info->ifc_subclass != 0xfe) return -1;
- if(info->ifc_protocol != 0x03) return -1;
- return 0;
-}
-
-int test_null(Transport* usb)
-{
- unsigned i;
- unsigned char buf[4096];
- memset(buf, 0xee, 4096);
- long long t0, t1;
-
- t0 = NOW();
- for (i = 0; i < arg_count; i++) {
- if (usb->Write(buf, arg_size) != static_cast<int>(arg_size)) {
- fprintf(stderr,"write failed (%s)\n", strerror(errno));
- return -1;
- }
- }
- t1 = NOW();
- fprintf(stderr,"%d bytes in %lld uS\n", arg_count * arg_size, (t1 - t0));
- return 0;
-}
-
-int test_zero(Transport* usb)
-{
- unsigned i;
- unsigned char buf[4096];
- long long t0, t1;
-
- t0 = NOW();
- for (i = 0; i < arg_count; i++) {
- if (usb->Read(buf, arg_size) != static_cast<int>(arg_size)) {
- fprintf(stderr,"read failed (%s)\n", strerror(errno));
- return -1;
- }
- }
- t1 = NOW();
- fprintf(stderr,"%d bytes in %lld uS\n", arg_count * arg_size, (t1 - t0));
- return 0;
-}
-
-struct
-{
- const char *cmd;
- ifc_match_func match;
- int (*test)(Transport* usb);
- const char *help;
-} tests[] = {
- { "list", printifc, NULL, "list interfaces" },
- { "send", match_null, test_null, "send to null interface" },
- { "recv", match_zero, test_zero, "recv from zero interface" },
- { "loop", match_loop, NULL, "exercise loopback interface" },
- { NULL, NULL, NULL, NULL },
-};
-
-int usage(void)
-{
- int i;
-
- fprintf(stderr,"usage: usbtest <testname>\n\navailable tests:\n");
- for(i = 0; tests[i].cmd; i++) {
- fprintf(stderr," %-8s %s\n", tests[i].cmd, tests[i].help);
- }
- return -1;
-}
-
-int process_args(int argc, char **argv)
-{
- while(argc-- > 0) {
- char *arg = *argv++;
- if(!strncmp(arg,"count=",6)) {
- arg_count = atoi(arg + 6);
- } else if(!strncmp(arg,"size=",5)) {
- arg_size = atoi(arg + 5);
- } else {
- fprintf(stderr,"unknown argument: %s\n", arg);
- return -1;
- }
- }
-
- if(arg_count == 0) {
- fprintf(stderr,"count may not be zero\n");
- return -1;
- }
-
- if(arg_size > 4096) {
- fprintf(stderr,"size may not be greater than 4096\n");
- return -1;
- }
-
- return 0;
-}
-
-int main(int argc, char **argv)
-{
- Transport* usb;
- int i;
-
- if(argc < 2)
- return usage();
-
- if(argc > 2) {
- if(process_args(argc - 2, argv + 2))
- return -1;
- }
-
- for(i = 0; tests[i].cmd; i++) {
- if(!strcmp(argv[1], tests[i].cmd)) {
- usb = usb_open(tests[i].match);
- if(tests[i].test) {
- if(usb == 0) {
- fprintf(stderr,"usbtest: %s: could not find interface\n",
- tests[i].cmd);
- return -1;
- }
- if(tests[i].test(usb)) {
- fprintf(stderr,"usbtest: %s: FAIL\n", tests[i].cmd);
- return -1;
- } else {
- fprintf(stderr,"usbtest: %s: OKAY\n", tests[i].cmd);
- }
- }
- return 0;
- }
- }
-
- return usage();
-}
diff --git a/fastboot/util.cpp b/fastboot/util.cpp
index f2bbd34..8f6e52a 100644
--- a/fastboot/util.cpp
+++ b/fastboot/util.cpp
@@ -33,37 +33,45 @@
#include <sys/time.h>
-#include "fastboot.h"
+#include "util.h"
-double now()
-{
+static bool g_verbose = false;
+
+double now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
}
-char *mkmsg(const char *fmt, ...)
-{
- char buf[256];
- char *s;
+void die(const char* fmt, ...) {
va_list ap;
-
va_start(ap, fmt);
- vsprintf(buf, fmt, ap);
+ fprintf(stderr, "fastboot: error: ");
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, "\n");
va_end(ap);
-
- s = strdup(buf);
- if (s == 0) die("out of memory");
- return s;
+ exit(EXIT_FAILURE);
}
-void die(const char *fmt, ...)
-{
- va_list ap;
- va_start(ap, fmt);
- fprintf(stderr,"error: ");
- vfprintf(stderr, fmt, ap);
- fprintf(stderr,"\n");
- va_end(ap);
- exit(1);
+void set_verbose() {
+ g_verbose = true;
+}
+
+void verbose(const char* fmt, ...) {
+ if (!g_verbose) return;
+
+ if (*fmt != '\n') {
+ va_list ap;
+ va_start(ap, fmt);
+ fprintf(stderr, "fastboot: verbose: ");
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ }
+ fprintf(stderr, "\n");
+}
+
+char* xstrdup(const char* s) {
+ char* result = strdup(s);
+ if (!result) die("out of memory");
+ return result;
}
diff --git a/fastboot/util.h b/fastboot/util.h
new file mode 100644
index 0000000..9033f93
--- /dev/null
+++ b/fastboot/util.h
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <inttypes.h>
+#include <stdlib.h>
+
+#include <string>
+
+#include <bootimg.h>
+
+/* util stuff */
+double now();
+char* xstrdup(const char*);
+void set_verbose();
+
+// These printf-like functions are implemented in terms of vsnprintf, so they
+// use the same attribute for compile-time format string checking.
+void die(const char* fmt, ...) __attribute__((__noreturn__))
+__attribute__((__format__(__printf__, 1, 2)));
+void verbose(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
diff --git a/fs_mgr/Android.bp b/fs_mgr/Android.bp
index f23150d..3cce0e8 100644
--- a/fs_mgr/Android.bp
+++ b/fs_mgr/Android.bp
@@ -30,36 +30,49 @@
],
}
-cc_library_static {
+cc_library {
+ // Do not ever allow this library to be vendor_available as a shared library.
+ // It does not have a stable interface.
name: "libfs_mgr",
defaults: ["fs_mgr_defaults"],
+ recovery_available: true,
export_include_dirs: ["include"],
include_dirs: ["system/vold"],
srcs: [
"fs_mgr.cpp",
- "fs_mgr_dm_ioctl.cpp",
"fs_mgr_format.cpp",
"fs_mgr_verity.cpp",
"fs_mgr_avb.cpp",
"fs_mgr_avb_ops.cpp",
+ "fs_mgr_dm_linear.cpp",
+ "fs_mgr_overlayfs.cpp",
+ ],
+ shared_libs: [
+ "libbase",
+ "libcrypto",
+ "libcrypto_utils",
+ "libcutils",
+ "libext4_utils",
+ "libfec",
+ "liblog",
+ "liblp",
+ "libselinux",
],
static_libs: [
- "libfec",
- "libfec_rs",
- "libbase",
- "libcrypto_utils",
- "libcrypto",
- "libext4_utils",
- "libsquashfs_utils",
- "libselinux",
"libavb",
"libfstab",
+ "libdm",
],
export_static_lib_headers: [
"libfstab",
+ "libdm",
+ ],
+ export_shared_lib_headers: [
+ "liblp",
],
whole_static_libs: [
"liblogwrap",
+ "libdm",
"libfstab",
],
cppflags: [
@@ -76,8 +89,11 @@
}
cc_library_static {
+ // Do not ever make this a shared library as long as it is vendor_available.
+ // It does not have a stable interface.
name: "libfstab",
vendor_available: true,
+ recovery_available: true,
defaults: ["fs_mgr_defaults"],
srcs: [
"fs_mgr_fstab.cpp",
diff --git a/fs_mgr/fs_mgr.cpp b/fs_mgr/fs_mgr.cpp
index 9aab0ba..99f2df8 100644
--- a/fs_mgr/fs_mgr.cpp
+++ b/fs_mgr/fs_mgr.cpp
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+#include "fs_mgr.h"
+
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
@@ -31,6 +33,7 @@
#include <time.h>
#include <unistd.h>
+#include <functional>
#include <memory>
#include <string>
#include <thread>
@@ -50,16 +53,16 @@
#include <ext4_utils/ext4_sb.h>
#include <ext4_utils/ext4_utils.h>
#include <ext4_utils/wipe.h>
+#include <fs_mgr_overlayfs.h>
+#include <libdm/dm.h>
#include <linux/fs.h>
#include <linux/loop.h>
#include <linux/magic.h>
#include <log/log_properties.h>
#include <logwrap/logwrap.h>
-#include "fs_mgr.h"
#include "fs_mgr_avb.h"
#include "fs_mgr_priv.h"
-#include "fs_mgr_priv_dm_ioctl.h"
#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
#define KEY_IN_FOOTER "footer"
@@ -76,6 +79,8 @@
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
+using DeviceMapper = android::dm::DeviceMapper;
+
// record fs stat
enum FsStatFlags {
FS_STAT_IS_EXT4 = 0x0001,
@@ -794,6 +799,24 @@
return true;
}
+bool fs_mgr_update_logical_partition(struct fstab_rec* rec) {
+ // Logical partitions are specified with a named partition rather than a
+ // block device, so if the block device is a path, then it has already
+ // been updated.
+ if (rec->blk_device[0] == '/') {
+ return true;
+ }
+
+ DeviceMapper& dm = DeviceMapper::Instance();
+ std::string device_name;
+ if (!dm.GetDmDevicePathByName(rec->blk_device, &device_name)) {
+ return false;
+ }
+ free(rec->blk_device);
+ rec->blk_device = strdup(device_name.c_str());
+ return true;
+}
+
/* When multiple fstab records share the same mount_point, it will
* try to mount each one in turn, and ignore any duplicates after a
* first successful mount.
@@ -845,6 +868,13 @@
}
}
+ if ((fstab->recs[i].fs_mgr_flags & MF_LOGICAL)) {
+ if (!fs_mgr_update_logical_partition(&fstab->recs[i])) {
+ LERROR << "Could not set up logical partition, skipping!";
+ continue;
+ }
+ }
+
if (fstab->recs[i].fs_mgr_flags & MF_WAIT &&
!fs_mgr_wait_for_file(fstab->recs[i].blk_device, 20s)) {
LERROR << "Skipping '" << fstab->recs[i].blk_device << "' during mount_all";
@@ -1007,6 +1037,10 @@
}
}
+#if ALLOW_ADBD_DISABLE_VERITY == 1 // "userdebug" build
+ fs_mgr_overlayfs_mount_all();
+#endif
+
if (error_count) {
return FS_MGR_MNTALL_FAIL;
} else {
@@ -1024,6 +1058,9 @@
return FS_MGR_DOMNT_FAILED;
}
+ // Run fsck if needed
+ prepare_fs_for_mount(rec->blk_device, rec);
+
int ret = __mount(rec->blk_device, rec->mount_point, rec);
if (ret) {
ret = (errno == EBUSY) ? FS_MGR_DOMNT_BUSY : FS_MGR_DOMNT_FAILED;
@@ -1065,6 +1102,13 @@
return FS_MGR_DOMNT_FAILED;
}
+ if ((fstab->recs[i].fs_mgr_flags & MF_LOGICAL)) {
+ if (!fs_mgr_update_logical_partition(&fstab->recs[i])) {
+ LERROR << "Could not set up logical partition, skipping!";
+ continue;
+ }
+ }
+
/* First check the filesystem if requested */
if (fstab->recs[i].fs_mgr_flags & MF_WAIT && !fs_mgr_wait_for_file(n_blk_device, 20s)) {
LERROR << "Skipping mounting '" << n_blk_device << "'";
@@ -1142,8 +1186,8 @@
{
int ret;
- ret = mount("tmpfs", n_name, "tmpfs",
- MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS);
+ ret = mount("tmpfs", n_name, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV | MS_NOEXEC,
+ CRYPTO_TMPFS_OPTIONS);
if (ret < 0) {
LERROR << "Cannot mount tmpfs filesystem at " << n_name;
return -1;
@@ -1153,27 +1197,6 @@
return 0;
}
-int fs_mgr_unmount_all(struct fstab *fstab)
-{
- int i = 0;
- int ret = 0;
-
- if (!fstab) {
- return -1;
- }
-
- while (fstab->recs[i].blk_device) {
- if (umount(fstab->recs[i].mount_point)) {
- LERROR << "Cannot unmount filesystem at "
- << fstab->recs[i].mount_point;
- ret = -1;
- }
- i++;
- }
-
- return ret;
-}
-
/* This must be called after mount_all, because the mkswap command needs to be
* available.
*/
@@ -1343,7 +1366,7 @@
return true;
}
-bool fs_mgr_update_verity_state(fs_mgr_verity_state_callback callback) {
+bool fs_mgr_update_verity_state(std::function<fs_mgr_verity_state_callback> callback) {
if (!callback) {
return false;
}
@@ -1353,12 +1376,6 @@
return false;
}
- android::base::unique_fd fd(TEMP_FAILURE_RETRY(open("/dev/device-mapper", O_RDWR | O_CLOEXEC)));
- if (fd == -1) {
- PERROR << "Error opening device mapper";
- return false;
- }
-
std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
fs_mgr_free_fstab);
if (!fstab) {
@@ -1366,8 +1383,8 @@
return false;
}
- alignas(dm_ioctl) char buffer[DM_BUF_SIZE];
- struct dm_ioctl* io = (struct dm_ioctl*)buffer;
+ DeviceMapper& dm = DeviceMapper::Instance();
+
bool system_root = android::base::GetProperty("ro.build.system_root_image", "") == "true";
for (int i = 0; i < fstab->num_entries; i++) {
@@ -1383,20 +1400,20 @@
mount_point = basename(fstab->recs[i].mount_point);
}
- fs_mgr_verity_ioctl_init(io, mount_point, 0);
+ const char* status = nullptr;
- const char* status;
- if (ioctl(fd, DM_TABLE_STATUS, io)) {
+ std::vector<DeviceMapper::TargetInfo> table;
+ if (!dm.GetTableStatus(mount_point, &table) || table.empty() || table[0].data.empty()) {
if (fstab->recs[i].fs_mgr_flags & MF_VERIFYATBOOT) {
status = "V";
} else {
PERROR << "Failed to query DM_TABLE_STATUS for " << mount_point.c_str();
continue;
}
+ } else {
+ status = table[0].data.c_str();
}
- status = &buffer[io->data_start + sizeof(struct dm_target_spec)];
-
// To be consistent in vboot 1.0 and vboot 2.0 (AVB), change the mount_point
// back to 'system' for the callback. So it has property [partition.system.verified]
// instead of [partition.vroot.verified].
diff --git a/fs_mgr/fs_mgr_avb.cpp b/fs_mgr/fs_mgr_avb.cpp
index 7824cfa..7c6093e 100644
--- a/fs_mgr/fs_mgr_avb.cpp
+++ b/fs_mgr/fs_mgr_avb.cpp
@@ -33,11 +33,11 @@
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <libavb/libavb.h>
+#include <libdm/dm.h>
#include "fs_mgr.h"
#include "fs_mgr_priv.h"
#include "fs_mgr_priv_avb_ops.h"
-#include "fs_mgr_priv_dm_ioctl.h"
#include "fs_mgr_priv_sha.h"
static inline bool nibble_value(const char& c, uint8_t* value) {
@@ -139,38 +139,23 @@
};
std::unique_ptr<FsManagerAvbVerifier> FsManagerAvbVerifier::Create() {
- std::string cmdline;
- if (!android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
- PERROR << "Failed to read /proc/cmdline";
- return nullptr;
- }
-
std::unique_ptr<FsManagerAvbVerifier> avb_verifier(new FsManagerAvbVerifier());
if (!avb_verifier) {
LERROR << "Failed to create unique_ptr<FsManagerAvbVerifier>";
return nullptr;
}
- std::string digest;
- std::string hash_alg;
- for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
- std::vector<std::string> pieces = android::base::Split(entry, "=");
- const std::string& key = pieces[0];
- const std::string& value = pieces[1];
-
- if (key == "androidboot.vbmeta.hash_alg") {
- hash_alg = value;
- } else if (key == "androidboot.vbmeta.size") {
- if (!android::base::ParseUint(value.c_str(), &avb_verifier->vbmeta_size_)) {
- return nullptr;
- }
- } else if (key == "androidboot.vbmeta.digest") {
- digest = value;
- }
+ std::string value;
+ if (!fs_mgr_get_boot_config("vbmeta.size", &value) ||
+ !android::base::ParseUint(value.c_str(), &avb_verifier->vbmeta_size_)) {
+ LERROR << "Invalid hash size: " << value.c_str();
+ return nullptr;
}
// Reads hash algorithm.
size_t expected_digest_size = 0;
+ std::string hash_alg;
+ fs_mgr_get_boot_config("vbmeta.hash_alg", &hash_alg);
if (hash_alg == "sha256") {
expected_digest_size = SHA256_DIGEST_LENGTH * 2;
avb_verifier->hash_alg_ = kSHA256;
@@ -183,6 +168,8 @@
}
// Reads digest.
+ std::string digest;
+ fs_mgr_get_boot_config("vbmeta.digest", &digest);
if (digest.size() != expected_digest_size) {
LERROR << "Unexpected digest size: " << digest.size()
<< " (expected: " << expected_digest_size << ")";
@@ -231,9 +218,9 @@
// Constructs dm-verity arguments for sending DM_TABLE_LOAD ioctl to kernel.
// See the following link for more details:
// https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity
-static std::string construct_verity_table(const AvbHashtreeDescriptor& hashtree_desc,
- const std::string& salt, const std::string& root_digest,
- const std::string& blk_device) {
+static bool construct_verity_table(const AvbHashtreeDescriptor& hashtree_desc,
+ const std::string& salt, const std::string& root_digest,
+ const std::string& blk_device, android::dm::DmTable* table) {
// Loads androidboot.veritymode from kernel cmdline.
std::string verity_mode;
if (!fs_mgr_get_boot_config("veritymode", &verity_mode)) {
@@ -248,144 +235,56 @@
dm_verity_mode = "ignore_corruption";
} else if (verity_mode != "eio") { // Default dm_verity_mode is eio.
LERROR << "Unknown androidboot.veritymode: " << verity_mode;
- return "";
+ return false;
}
- // dm-verity construction parameters:
- // <version> <dev> <hash_dev>
- // <data_block_size> <hash_block_size>
- // <num_data_blocks> <hash_start_block>
- // <algorithm> <digest> <salt>
- // [<#opt_params> <opt_params>]
- std::ostringstream verity_table;
- verity_table << hashtree_desc.dm_verity_version << " " << blk_device << " " << blk_device << " "
- << hashtree_desc.data_block_size << " " << hashtree_desc.hash_block_size << " "
- << hashtree_desc.image_size / hashtree_desc.data_block_size << " "
- << hashtree_desc.tree_offset / hashtree_desc.hash_block_size << " "
- << hashtree_desc.hash_algorithm << " " << root_digest << " " << salt;
+ std::ostringstream hash_algorithm;
+ hash_algorithm << hashtree_desc.hash_algorithm;
- // Continued from the above optional parameters:
- // [<#opt_params> <opt_params>]
- int optional_argc = 0;
- std::ostringstream optional_args;
-
- // dm-verity optional parameters for FEC (forward error correction):
- // use_fec_from_device <fec_dev>
- // fec_roots <num>
- // fec_blocks <num>
- // fec_start <offset>
+ android::dm::DmTargetVerity target(0, hashtree_desc.image_size / 512,
+ hashtree_desc.dm_verity_version, blk_device, blk_device,
+ hashtree_desc.data_block_size, hashtree_desc.hash_block_size,
+ hashtree_desc.image_size / hashtree_desc.data_block_size,
+ hashtree_desc.tree_offset / hashtree_desc.hash_block_size,
+ hash_algorithm.str(), root_digest, salt);
if (hashtree_desc.fec_size > 0) {
- // Note that fec_blocks is the size that FEC covers, *NOT* the
- // size of the FEC data. Since we use FEC for everything up until
- // the FEC data, it's the same as the offset (fec_start).
- optional_argc += 8;
- // clang-format off
- optional_args << "use_fec_from_device " << blk_device
- << " fec_roots " << hashtree_desc.fec_num_roots
- << " fec_blocks " << hashtree_desc.fec_offset / hashtree_desc.data_block_size
- << " fec_start " << hashtree_desc.fec_offset / hashtree_desc.data_block_size
- << " ";
- // clang-format on
+ target.UseFec(blk_device, hashtree_desc.fec_num_roots,
+ hashtree_desc.fec_offset / hashtree_desc.data_block_size,
+ hashtree_desc.fec_offset / hashtree_desc.data_block_size);
}
-
if (!dm_verity_mode.empty()) {
- optional_argc += 1;
- optional_args << dm_verity_mode << " ";
+ target.SetVerityMode(dm_verity_mode);
}
-
// Always use ignore_zero_blocks.
- optional_argc += 1;
- optional_args << "ignore_zero_blocks";
+ target.IgnoreZeroBlocks();
- verity_table << " " << optional_argc << " " << optional_args.str();
- return verity_table.str();
-}
+ LINFO << "Built verity table: '" << target.GetParameterString() << "'";
-static bool load_verity_table(struct dm_ioctl* io, const std::string& dm_device_name, int fd,
- uint64_t image_size, const std::string& verity_table) {
- fs_mgr_verity_ioctl_init(io, dm_device_name, DM_STATUS_TABLE_FLAG);
-
- // The buffer consists of [dm_ioctl][dm_target_spec][verity_params].
- char* buffer = (char*)io;
-
- // Builds the dm_target_spec arguments.
- struct dm_target_spec* dm_target = (struct dm_target_spec*)&buffer[sizeof(struct dm_ioctl)];
- io->target_count = 1;
- dm_target->status = 0;
- dm_target->sector_start = 0;
- dm_target->length = image_size / 512;
- strcpy(dm_target->target_type, "verity");
-
- // Builds the verity params.
- char* verity_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
- size_t bufsize = DM_BUF_SIZE - (verity_params - buffer);
-
- LINFO << "Loading verity table: '" << verity_table << "'";
-
- // Copies verity_table to verity_params (including the terminating null byte).
- if (verity_table.size() > bufsize - 1) {
- LERROR << "Verity table size too large: " << verity_table.size()
- << " (max allowable size: " << bufsize - 1 << ")";
- return false;
- }
- memcpy(verity_params, verity_table.c_str(), verity_table.size() + 1);
-
- // Sets ext target boundary.
- verity_params += verity_table.size() + 1;
- verity_params = (char*)(((unsigned long)verity_params + 7) & ~7);
- dm_target->next = verity_params - buffer;
-
- // Sends the ioctl to load the verity table.
- if (ioctl(fd, DM_TABLE_LOAD, io)) {
- PERROR << "Error loading verity table";
- return false;
- }
-
- return true;
+ return table->AddTarget(std::make_unique<android::dm::DmTargetVerity>(target));
}
static bool hashtree_dm_verity_setup(struct fstab_rec* fstab_entry,
const AvbHashtreeDescriptor& hashtree_desc,
const std::string& salt, const std::string& root_digest,
bool wait_for_verity_dev) {
- // Gets the device mapper fd.
- android::base::unique_fd fd(open("/dev/device-mapper", O_RDWR));
- if (fd < 0) {
- PERROR << "Error opening device mapper";
+ android::dm::DmTable table;
+ if (!construct_verity_table(hashtree_desc, salt, root_digest, fstab_entry->blk_device, &table) ||
+ !table.valid()) {
+ LERROR << "Failed to construct verity table.";
return false;
}
+ table.set_readonly(true);
- // Creates the device.
- alignas(dm_ioctl) char buffer[DM_BUF_SIZE];
- struct dm_ioctl* io = (struct dm_ioctl*)buffer;
const std::string mount_point(basename(fstab_entry->mount_point));
- if (!fs_mgr_create_verity_device(io, mount_point, fd)) {
+ android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance();
+ if (!dm.CreateDevice(mount_point, table)) {
LERROR << "Couldn't create verity device!";
return false;
}
- // Gets the name of the device file.
- std::string verity_blk_name;
- if (!fs_mgr_get_verity_device_name(io, mount_point, fd, &verity_blk_name)) {
- LERROR << "Couldn't get verity device number!";
- return false;
- }
-
- std::string verity_table =
- construct_verity_table(hashtree_desc, salt, root_digest, fstab_entry->blk_device);
- if (verity_table.empty()) {
- LERROR << "Failed to construct verity table.";
- return false;
- }
-
- // Loads the verity mapping table.
- if (!load_verity_table(io, mount_point, fd, hashtree_desc.image_size, verity_table)) {
- LERROR << "Couldn't load verity table!";
- return false;
- }
-
- // Activates the device.
- if (!fs_mgr_resume_verity_table(io, mount_point, fd)) {
+ std::string dev_path;
+ if (!dm.GetDmDevicePathByName(mount_point, &dev_path)) {
+ LERROR << "Couldn't get verity device path!";
return false;
}
@@ -394,10 +293,10 @@
// Updates fstab_rec->blk_device to verity device name.
free(fstab_entry->blk_device);
- fstab_entry->blk_device = strdup(verity_blk_name.c_str());
+ fstab_entry->blk_device = strdup(dev_path.c_str());
// Makes sure we've set everything up properly.
- if (wait_for_verity_dev && !fs_mgr_wait_for_file(verity_blk_name, 1s)) {
+ if (wait_for_verity_dev && !fs_mgr_wait_for_file(dev_path, 1s)) {
return false;
}
@@ -584,7 +483,13 @@
// Derives partition_name from blk_device to query the corresponding AVB HASHTREE descriptor
// to setup dm-verity. The partition_names in AVB descriptors are without A/B suffix.
- std::string partition_name(basename(fstab_entry->blk_device));
+ std::string partition_name;
+ if (fstab_entry->fs_mgr_flags & MF_LOGICAL) {
+ partition_name = fstab_entry->logical_partition_name;
+ } else {
+ partition_name = basename(fstab_entry->blk_device);
+ }
+
if (fstab_entry->fs_mgr_flags & MF_SLOTSELECT) {
auto ab_suffix = partition_name.rfind(fs_mgr_get_slot_suffix());
if (ab_suffix != std::string::npos) {
diff --git a/fs_mgr/fs_mgr_boot_config.cpp b/fs_mgr/fs_mgr_boot_config.cpp
index 9c5d3f3..733ad55 100644
--- a/fs_mgr/fs_mgr_boot_config.cpp
+++ b/fs_mgr/fs_mgr_boot_config.cpp
@@ -14,7 +14,10 @@
* limitations under the License.
*/
+#include <algorithm>
+#include <iterator>
#include <string>
+#include <vector>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
@@ -23,46 +26,71 @@
#include "fs_mgr_priv.h"
-// Tries to get the given boot config value from kernel cmdline.
-// Returns true if successfully found, false otherwise.
-bool fs_mgr_get_boot_config_from_kernel_cmdline(const std::string& key, std::string* out_val) {
+std::vector<std::pair<std::string, std::string>> fs_mgr_parse_boot_config(const std::string& cmdline) {
+ static constexpr char quote = '"';
+
+ std::vector<std::pair<std::string, std::string>> result;
+ size_t base = 0;
+ while (true) {
+ // skip quoted spans
+ auto found = base;
+ while (((found = cmdline.find_first_of(" \"", found)) != cmdline.npos) &&
+ (cmdline[found] == quote)) {
+ // unbalanced quote is ok
+ if ((found = cmdline.find(quote, found + 1)) == cmdline.npos) break;
+ ++found;
+ }
+ std::string piece;
+ auto source = cmdline.substr(base, found - base);
+ std::remove_copy(source.begin(), source.end(),
+ std::back_insert_iterator<std::string>(piece), quote);
+ auto equal_sign = piece.find('=');
+ if (equal_sign == piece.npos) {
+ if (!piece.empty()) {
+ // no difference between <key> and <key>=
+ result.emplace_back(std::move(piece), "");
+ }
+ } else {
+ result.emplace_back(piece.substr(0, equal_sign), piece.substr(equal_sign + 1));
+ }
+ if (found == cmdline.npos) break;
+ base = found + 1;
+ }
+
+ return result;
+}
+
+bool fs_mgr_get_boot_config_from_kernel(const std::string& cmdline, const std::string& android_key,
+ std::string* out_val) {
FS_MGR_CHECK(out_val != nullptr);
- std::string cmdline;
- std::string cmdline_key("androidboot." + key);
- if (android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
- for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
- std::vector<std::string> pieces = android::base::Split(entry, "=");
- if (pieces.size() == 2) {
- if (pieces[0] == cmdline_key) {
- *out_val = pieces[1];
- return true;
- }
- }
+ const std::string cmdline_key("androidboot." + android_key);
+ for (const auto& [key, value] : fs_mgr_parse_boot_config(cmdline)) {
+ if (key == cmdline_key) {
+ *out_val = value;
+ return true;
}
}
+ *out_val = "";
return false;
}
-// Tries to get the boot config value in properties, kernel cmdline and
-// device tree (in that order). returns 'true' if successfully found, 'false'
-// otherwise
+// Tries to get the given boot config value from kernel cmdline.
+// Returns true if successfully found, false otherwise.
+bool fs_mgr_get_boot_config_from_kernel_cmdline(const std::string& key, std::string* out_val) {
+ std::string cmdline;
+ if (!android::base::ReadFileToString("/proc/cmdline", &cmdline)) return false;
+ return fs_mgr_get_boot_config_from_kernel(cmdline, key, out_val);
+}
+
+// Tries to get the boot config value in device tree, properties and
+// kernel cmdline (in that order). Returns 'true' if successfully
+// found, 'false' otherwise.
bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val) {
FS_MGR_CHECK(out_val != nullptr);
- // first check if we have "ro.boot" property already
- *out_val = android::base::GetProperty("ro.boot." + key, "");
- if (!out_val->empty()) {
- return true;
- }
-
- // fallback to kernel cmdline, properties may not be ready yet
- if (fs_mgr_get_boot_config_from_kernel_cmdline(key, out_val)) {
- return true;
- }
-
- // lastly, check the device tree
+ // firstly, check the device tree
if (is_dt_compatible()) {
std::string file_name = get_android_dt_dir() + "/" + key;
if (android::base::ReadFileToString(file_name, out_val)) {
@@ -73,5 +101,16 @@
}
}
+ // next, check if we have "ro.boot" property already
+ *out_val = android::base::GetProperty("ro.boot." + key, "");
+ if (!out_val->empty()) {
+ return true;
+ }
+
+ // finally, fallback to kernel cmdline, properties may not be ready yet
+ if (fs_mgr_get_boot_config_from_kernel_cmdline(key, out_val)) {
+ return true;
+ }
+
return false;
}
diff --git a/fs_mgr/fs_mgr_dm_ioctl.cpp b/fs_mgr/fs_mgr_dm_ioctl.cpp
deleted file mode 100644
index 4cbd5a8..0000000
--- a/fs_mgr/fs_mgr_dm_ioctl.cpp
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright (C) 2016 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 <string.h>
-
-#include <android-base/logging.h>
-#include <sys/ioctl.h>
-
-#include "fs_mgr_priv.h"
-#include "fs_mgr_priv_dm_ioctl.h"
-
-void fs_mgr_verity_ioctl_init(struct dm_ioctl* io, const std::string& name, unsigned flags) {
- memset(io, 0, DM_BUF_SIZE);
- io->data_size = DM_BUF_SIZE;
- io->data_start = sizeof(struct dm_ioctl);
- io->version[0] = 4;
- io->version[1] = 0;
- io->version[2] = 0;
- io->flags = flags | DM_READONLY_FLAG;
- if (!name.empty()) {
- strlcpy(io->name, name.c_str(), sizeof(io->name));
- }
-}
-
-bool fs_mgr_create_verity_device(struct dm_ioctl* io, const std::string& name, int fd) {
- fs_mgr_verity_ioctl_init(io, name, 1);
- if (ioctl(fd, DM_DEV_CREATE, io)) {
- PERROR << "Error creating device mapping";
- return false;
- }
- return true;
-}
-
-bool fs_mgr_destroy_verity_device(struct dm_ioctl* io, const std::string& name, int fd) {
- fs_mgr_verity_ioctl_init(io, name, 0);
- if (ioctl(fd, DM_DEV_REMOVE, io)) {
- PERROR << "Error removing device mapping";
- return false;
- }
- return true;
-}
-
-bool fs_mgr_get_verity_device_name(struct dm_ioctl* io, const std::string& name, int fd,
- std::string* out_dev_name) {
- FS_MGR_CHECK(out_dev_name != nullptr);
-
- fs_mgr_verity_ioctl_init(io, name, 0);
- if (ioctl(fd, DM_DEV_STATUS, io)) {
- PERROR << "Error fetching verity device number";
- return false;
- }
-
- int dev_num = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
- *out_dev_name = "/dev/block/dm-" + std::to_string(dev_num);
-
- return true;
-}
-
-bool fs_mgr_resume_verity_table(struct dm_ioctl* io, const std::string& name, int fd) {
- fs_mgr_verity_ioctl_init(io, name, 0);
- if (ioctl(fd, DM_DEV_SUSPEND, io)) {
- PERROR << "Error activating verity device";
- return false;
- }
- return true;
-}
diff --git a/fs_mgr/fs_mgr_dm_linear.cpp b/fs_mgr/fs_mgr_dm_linear.cpp
new file mode 100644
index 0000000..a91e92e
--- /dev/null
+++ b/fs_mgr/fs_mgr_dm_linear.cpp
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "fs_mgr_dm_linear.h"
+
+#include <inttypes.h>
+#include <linux/dm-ioctl.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <sstream>
+
+#include <android-base/logging.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
+#include <liblp/reader.h>
+
+#include "fs_mgr_priv.h"
+
+namespace android {
+namespace fs_mgr {
+
+using DeviceMapper = android::dm::DeviceMapper;
+using DmTable = android::dm::DmTable;
+using DmTarget = android::dm::DmTarget;
+using DmTargetZero = android::dm::DmTargetZero;
+using DmTargetLinear = android::dm::DmTargetLinear;
+
+static bool CreateDmTable(const std::string& block_device, const LpMetadata& metadata,
+ const LpMetadataPartition& partition, DmTable* table) {
+ uint64_t sector = 0;
+ for (size_t i = 0; i < partition.num_extents; i++) {
+ const auto& extent = metadata.extents[partition.first_extent_index + i];
+ std::unique_ptr<DmTarget> target;
+ switch (extent.target_type) {
+ case LP_TARGET_TYPE_ZERO:
+ target = std::make_unique<DmTargetZero>(sector, extent.num_sectors);
+ break;
+ case LP_TARGET_TYPE_LINEAR:
+ target = std::make_unique<DmTargetLinear>(sector, extent.num_sectors, block_device,
+ extent.target_data);
+ break;
+ default:
+ LOG(ERROR) << "Unknown target type in metadata: " << extent.target_type;
+ return false;
+ }
+ if (!table->AddTarget(std::move(target))) {
+ return false;
+ }
+ sector += extent.num_sectors;
+ }
+ if (partition.attributes & LP_PARTITION_ATTR_READONLY) {
+ table->set_readonly(true);
+ }
+ return true;
+}
+
+static bool CreateLogicalPartition(const std::string& block_device, const LpMetadata& metadata,
+ const LpMetadataPartition& partition, bool force_writable,
+ std::string* path) {
+ DeviceMapper& dm = DeviceMapper::Instance();
+
+ DmTable table;
+ if (!CreateDmTable(block_device, metadata, partition, &table)) {
+ return false;
+ }
+ if (force_writable) {
+ table.set_readonly(false);
+ }
+ std::string name = GetPartitionName(partition);
+ if (!dm.CreateDevice(name, table)) {
+ return false;
+ }
+ if (!dm.GetDmDevicePathByName(name, path)) {
+ return false;
+ }
+ LINFO << "Created logical partition " << name << " on device " << *path;
+ return true;
+}
+
+bool CreateLogicalPartitions(const std::string& block_device) {
+ uint32_t slot = SlotNumberForSlotSuffix(fs_mgr_get_slot_suffix());
+ auto metadata = ReadMetadata(block_device.c_str(), slot);
+ if (!metadata) {
+ LOG(ERROR) << "Could not read partition table.";
+ return true;
+ }
+ for (const auto& partition : metadata->partitions) {
+ if (!partition.num_extents) {
+ LINFO << "Skipping zero-length logical partition: " << GetPartitionName(partition);
+ continue;
+ }
+ std::string path;
+ if (!CreateLogicalPartition(block_device, *metadata.get(), partition, false, &path)) {
+ LERROR << "Could not create logical partition: " << GetPartitionName(partition);
+ return false;
+ }
+ }
+ return true;
+}
+
+bool CreateLogicalPartition(const std::string& block_device, uint32_t metadata_slot,
+ const std::string& partition_name, bool force_writable,
+ std::string* path) {
+ auto metadata = ReadMetadata(block_device.c_str(), metadata_slot);
+ if (!metadata) {
+ LOG(ERROR) << "Could not read partition table.";
+ return true;
+ }
+ for (const auto& partition : metadata->partitions) {
+ if (GetPartitionName(partition) == partition_name) {
+ return CreateLogicalPartition(block_device, *metadata.get(), partition, force_writable,
+ path);
+ }
+ }
+ LERROR << "Could not find any partition with name: " << partition_name;
+ return false;
+}
+
+bool DestroyLogicalPartition(const std::string& name) {
+ DeviceMapper& dm = DeviceMapper::Instance();
+ if (!dm.DeleteDevice(name)) {
+ return false;
+ }
+ LINFO << "Unmapped logical partition " << name;
+ return true;
+}
+
+} // namespace fs_mgr
+} // namespace android
diff --git a/fs_mgr/fs_mgr_format.cpp b/fs_mgr/fs_mgr_format.cpp
index 85a593f..845cca9 100644
--- a/fs_mgr/fs_mgr_format.cpp
+++ b/fs_mgr/fs_mgr_format.cpp
@@ -114,8 +114,20 @@
}
std::string size_str = std::to_string(dev_sz / 4096);
+ // clang-format off
const char* const args[] = {
- "/system/bin/make_f2fs", "-f", "-O", "encrypt", fs_blkdev, size_str.c_str(), nullptr};
+ "/system/bin/make_f2fs",
+ "-d1",
+ "-f",
+ "-O", "encrypt",
+ "-O", "quota",
+ "-O", "verity",
+ "-w", "4096",
+ fs_blkdev,
+ size_str.c_str(),
+ nullptr
+ };
+ // clang-format on
return android_fork_execvp_ext(arraysize(args), const_cast<char**>(args), NULL, true,
LOG_KLOG, true, nullptr, nullptr, 0);
diff --git a/fs_mgr/fs_mgr_fstab.cpp b/fs_mgr/fs_mgr_fstab.cpp
index 9b7405a..f87a3b1 100644
--- a/fs_mgr/fs_mgr_fstab.cpp
+++ b/fs_mgr/fs_mgr_fstab.cpp
@@ -33,6 +33,8 @@
#include "fs_mgr_priv.h"
+using android::base::StartsWith;
+
const std::string kDefaultAndroidDtDir("/proc/device-tree/firmware/android");
struct fs_mgr_flag_values {
@@ -107,6 +109,7 @@
{"logicalblk=", MF_LOGICALBLKSIZE},
{"sysfs_path=", MF_SYSFS},
{"defaults", 0},
+ {"logical", MF_LOGICAL},
{0, 0},
};
@@ -228,41 +231,46 @@
* If not found, the loop exits with fl[i].name being null.
*/
for (i = 0; fl[i].name; i++) {
- if (!strncmp(p, fl[i].name, strlen(fl[i].name))) {
+ auto name = fl[i].name;
+ auto len = strlen(name);
+ auto end = len;
+ if (name[end - 1] == '=') --end;
+ if (!strncmp(p, name, len) && (p[end] == name[end])) {
f |= fl[i].flag;
- if ((fl[i].flag == MF_CRYPT) && flag_vals) {
+ if (!flag_vals) break;
+ if (p[end] != '=') break;
+ char* arg = p + end + 1;
+ auto flag = fl[i].flag;
+ if (flag == MF_CRYPT) {
/* The encryptable flag is followed by an = and the
* location of the keys. Get it and return it.
*/
- flag_vals->key_loc = strdup(strchr(p, '=') + 1);
- } else if ((fl[i].flag == MF_VERIFY) && flag_vals) {
+ flag_vals->key_loc = strdup(arg);
+ } else if (flag == MF_VERIFY) {
/* If the verify flag is followed by an = and the
* location for the verity state, get it and return it.
*/
- char *start = strchr(p, '=');
- if (start) {
- flag_vals->verity_loc = strdup(start + 1);
- }
- } else if ((fl[i].flag == MF_FORCECRYPT) && flag_vals) {
+ flag_vals->verity_loc = strdup(arg);
+ } else if (flag == MF_FORCECRYPT) {
/* The forceencrypt flag is followed by an = and the
* location of the keys. Get it and return it.
*/
- flag_vals->key_loc = strdup(strchr(p, '=') + 1);
- } else if ((fl[i].flag == MF_FORCEFDEORFBE) && flag_vals) {
+ flag_vals->key_loc = strdup(arg);
+ } else if (flag == MF_FORCEFDEORFBE) {
/* The forcefdeorfbe flag is followed by an = and the
* location of the keys. Get it and return it.
*/
- flag_vals->key_loc = strdup(strchr(p, '=') + 1);
+ flag_vals->key_loc = strdup(arg);
flag_vals->file_contents_mode = EM_AES_256_XTS;
flag_vals->file_names_mode = EM_AES_256_CTS;
- } else if ((fl[i].flag == MF_FILEENCRYPTION) && flag_vals) {
+ } else if (flag == MF_FILEENCRYPTION) {
/* The fileencryption flag is followed by an = and
* the mode of contents encryption, then optionally a
* : and the mode of filenames encryption (defaults
* to aes-256-cts). Get it and return it.
*/
- char *mode = strchr(p, '=') + 1;
- char *colon = strchr(mode, ':');
+ auto mode = arg;
+ auto colon = strchr(mode, ':');
if (colon) {
*colon = '\0';
}
@@ -276,33 +284,30 @@
} else {
flag_vals->file_names_mode = EM_AES_256_CTS;
}
- } else if ((fl[i].flag == MF_KEYDIRECTORY) && flag_vals) {
+ } else if (flag == MF_KEYDIRECTORY) {
/* The metadata flag is followed by an = and the
* directory for the keys. Get it and return it.
*/
- flag_vals->key_dir = strdup(strchr(p, '=') + 1);
- } else if ((fl[i].flag == MF_LENGTH) && flag_vals) {
+ flag_vals->key_dir = strdup(arg);
+ } else if (flag == MF_LENGTH) {
/* The length flag is followed by an = and the
* size of the partition. Get it and return it.
*/
- flag_vals->part_length = strtoll(strchr(p, '=') + 1, NULL, 0);
- } else if ((fl[i].flag == MF_VOLDMANAGED) && flag_vals) {
+ flag_vals->part_length = strtoll(arg, NULL, 0);
+ } else if (flag == MF_VOLDMANAGED) {
/* The voldmanaged flag is followed by an = and the
* label, a colon and the partition number or the
* word "auto", e.g.
* voldmanaged=sdcard:3
* Get and return them.
*/
- char *label_start;
- char *label_end;
- char *part_start;
+ auto label_start = arg;
+ auto label_end = strchr(label_start, ':');
- label_start = strchr(p, '=') + 1;
- label_end = strchr(p, ':');
if (label_end) {
flag_vals->label = strndup(label_start,
(int) (label_end - label_start));
- part_start = strchr(p, ':') + 1;
+ auto part_start = label_end + 1;
if (!strcmp(part_start, "auto")) {
flag_vals->partnum = -1;
} else {
@@ -311,41 +316,41 @@
} else {
LERROR << "Warning: voldmanaged= flag malformed";
}
- } else if ((fl[i].flag == MF_SWAPPRIO) && flag_vals) {
- flag_vals->swap_prio = strtoll(strchr(p, '=') + 1, NULL, 0);
- } else if ((fl[i].flag == MF_MAX_COMP_STREAMS) && flag_vals) {
- flag_vals->max_comp_streams = strtoll(strchr(p, '=') + 1, NULL, 0);
- } else if ((fl[i].flag == MF_ZRAMSIZE) && flag_vals) {
- int is_percent = !!strrchr(p, '%');
- unsigned int val = strtoll(strchr(p, '=') + 1, NULL, 0);
+ } else if (flag == MF_SWAPPRIO) {
+ flag_vals->swap_prio = strtoll(arg, NULL, 0);
+ } else if (flag == MF_MAX_COMP_STREAMS) {
+ flag_vals->max_comp_streams = strtoll(arg, NULL, 0);
+ } else if (flag == MF_ZRAMSIZE) {
+ auto is_percent = !!strrchr(arg, '%');
+ auto val = strtoll(arg, NULL, 0);
if (is_percent)
flag_vals->zram_size = calculate_zram_size(val);
else
flag_vals->zram_size = val;
- } else if ((fl[i].flag == MF_RESERVEDSIZE) && flag_vals) {
+ } else if (flag == MF_RESERVEDSIZE) {
/* The reserved flag is followed by an = and the
* reserved size of the partition. Get it and return it.
*/
- flag_vals->reserved_size = parse_size(strchr(p, '=') + 1);
- } else if ((fl[i].flag == MF_ERASEBLKSIZE) && flag_vals) {
+ flag_vals->reserved_size = parse_size(arg);
+ } else if (flag == MF_ERASEBLKSIZE) {
/* The erase block size flag is followed by an = and the flash
* erase block size. Get it, check that it is a power of 2 and
* at least 4096, and return it.
*/
- unsigned int val = strtoul(strchr(p, '=') + 1, NULL, 0);
+ auto val = strtoul(arg, NULL, 0);
if (val >= 4096 && (val & (val - 1)) == 0)
flag_vals->erase_blk_size = val;
- } else if ((fl[i].flag == MF_LOGICALBLKSIZE) && flag_vals) {
+ } else if (flag == MF_LOGICALBLKSIZE) {
/* The logical block size flag is followed by an = and the flash
* logical block size. Get it, check that it is a power of 2 and
* at least 4096, and return it.
*/
- unsigned int val = strtoul(strchr(p, '=') + 1, NULL, 0);
+ auto val = strtoul(arg, NULL, 0);
if (val >= 4096 && (val & (val - 1)) == 0)
flag_vals->logical_blk_size = val;
- } else if ((fl[i].flag == MF_SYSFS) && flag_vals) {
+ } else if (flag == MF_SYSFS) {
/* The path to trigger device gc by idle-maint of vold. */
- flag_vals->sysfs_path = strdup(strchr(p, '=') + 1);
+ flag_vals->sysfs_path = strdup(arg);
}
break;
}
@@ -499,8 +504,7 @@
return false;
}
-static struct fstab *fs_mgr_read_fstab_file(FILE *fstab_file)
-{
+static struct fstab* fs_mgr_read_fstab_file(FILE* fstab_file, bool proc_mounts) {
int cnt, entries;
ssize_t len;
size_t alloc_len = 0;
@@ -600,7 +604,10 @@
fstab->recs[cnt].fs_options = NULL;
}
- if (!(p = strtok_r(NULL, delim, &save_ptr))) {
+ // For /proc/mounts, ignore everything after mnt_freq and mnt_passno
+ if (proc_mounts) {
+ p += strlen(p);
+ } else if (!(p = strtok_r(NULL, delim, &save_ptr))) {
LERROR << "Error parsing fs_mgr_options";
goto err;
}
@@ -621,6 +628,10 @@
fstab->recs[cnt].erase_blk_size = flag_vals.erase_blk_size;
fstab->recs[cnt].logical_blk_size = flag_vals.logical_blk_size;
fstab->recs[cnt].sysfs_path = flag_vals.sysfs_path;
+ if (fstab->recs[cnt].fs_mgr_flags & MF_LOGICAL) {
+ fstab->recs[cnt].logical_partition_name = strdup(fstab->recs[cnt].blk_device);
+ }
+
cnt++;
}
/* If an A/B partition, modify block device to be the real block device */
@@ -668,13 +679,55 @@
// We can't call fs_mgr_free_fstab because a->recs still references the
// memory allocated by strdup.
free(b->recs);
- free(b->fstab_filename);
free(b);
a->num_entries = total_entries;
return a;
}
+/* Extracts <device>s from the by-name symlinks specified in a fstab:
+ * /dev/block/<type>/<device>/by-name/<partition>
+ *
+ * <type> can be: platform, pci or vbd.
+ *
+ * For example, given the following entries in the input fstab:
+ * /dev/block/platform/soc/1da4000.ufshc/by-name/system
+ * /dev/block/pci/soc.0/f9824900.sdhci/by-name/vendor
+ * it returns a set { "soc/1da4000.ufshc", "soc.0/f9824900.sdhci" }.
+ */
+static std::set<std::string> extract_boot_devices(const fstab& fstab) {
+ std::set<std::string> boot_devices;
+
+ for (int i = 0; i < fstab.num_entries; i++) {
+ std::string blk_device(fstab.recs[i].blk_device);
+ // Skips blk_device that doesn't conform to the format.
+ if (!android::base::StartsWith(blk_device, "/dev/block") ||
+ android::base::StartsWith(blk_device, "/dev/block/by-name") ||
+ android::base::StartsWith(blk_device, "/dev/block/bootdevice/by-name")) {
+ continue;
+ }
+ // Skips non-by_name blk_device.
+ // /dev/block/<type>/<device>/by-name/<partition>
+ // ^ slash_by_name
+ auto slash_by_name = blk_device.find("/by-name");
+ if (slash_by_name == std::string::npos) continue;
+ blk_device.erase(slash_by_name); // erases /by-name/<partition>
+
+ // Erases /dev/block/, now we have <type>/<device>
+ blk_device.erase(0, std::string("/dev/block/").size());
+
+ // <type>/<device>
+ // ^ first_slash
+ auto first_slash = blk_device.find('/');
+ if (first_slash == std::string::npos) continue;
+
+ auto boot_device = blk_device.substr(first_slash + 1);
+ if (!boot_device.empty()) boot_devices.insert(std::move(boot_device));
+ }
+
+ return boot_devices;
+}
+
struct fstab *fs_mgr_read_fstab(const char *fstab_path)
{
FILE *fstab_file;
@@ -686,10 +739,8 @@
return nullptr;
}
- fstab = fs_mgr_read_fstab_file(fstab_file);
- if (fstab) {
- fstab->fstab_filename = strdup(fstab_path);
- } else {
+ fstab = fs_mgr_read_fstab_file(fstab_file, !strcmp("/proc/mounts", fstab_path));
+ if (!fstab) {
LERROR << __FUNCTION__ << "(): failed to load fstab from : '" << fstab_path << "'";
}
@@ -716,7 +767,7 @@
return nullptr;
}
- struct fstab *fstab = fs_mgr_read_fstab_file(fstab_file.get());
+ struct fstab* fstab = fs_mgr_read_fstab_file(fstab_file.get(), false);
if (!fstab) {
LERROR << __FUNCTION__ << "(): failed to load fstab from kernel:"
<< std::endl << fstab_buf;
@@ -756,7 +807,7 @@
std::string default_fstab;
// Use different fstab paths for normal boot and recovery boot, respectively
- if (access("/sbin/recovery", F_OK) == 0) {
+ if (access("/system/bin/recovery", F_OK) == 0) {
default_fstab = "/etc/recovery.fstab";
} else { // normal boot
default_fstab = get_fstab_path();
@@ -787,6 +838,7 @@
for (i = 0; i < fstab->num_entries; i++) {
/* Free the pointers return by strdup(3) */
free(fstab->recs[i].blk_device);
+ free(fstab->recs[i].logical_partition_name);
free(fstab->recs[i].mount_point);
free(fstab->recs[i].fs_type);
free(fstab->recs[i].fs_options);
@@ -799,9 +851,6 @@
/* Free the fstab_recs array created by calloc(3) */
free(fstab->recs);
- /* Free the fstab filename */
- free(fstab->fstab_filename);
-
/* Free fstab */
free(fstab);
}
@@ -851,6 +900,23 @@
return nullptr;
}
+std::set<std::string> fs_mgr_get_boot_devices() {
+ // boot_devices can be specified in device tree.
+ std::string dt_value;
+ std::string file_name = get_android_dt_dir() + "/boot_devices";
+ if (read_dt_file(file_name, &dt_value)) {
+ auto boot_devices = android::base::Split(dt_value, ",");
+ return std::set<std::string>(boot_devices.begin(), boot_devices.end());
+ }
+
+ // Fallback to extract boot devices from fstab.
+ std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
+ fs_mgr_free_fstab);
+ if (fstab) return extract_boot_devices(*fstab);
+
+ return {};
+}
+
int fs_mgr_is_voldmanaged(const struct fstab_rec *fstab)
{
return fstab->fs_mgr_flags & MF_VOLDMANAGED;
@@ -934,3 +1000,7 @@
{
return fstab->fs_mgr_flags & MF_SYSFS;
}
+
+int fs_mgr_is_logical(const struct fstab_rec* fstab) {
+ return fstab->fs_mgr_flags & MF_LOGICAL;
+}
diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp
new file mode 100644
index 0000000..78151d5
--- /dev/null
+++ b/fs_mgr/fs_mgr_overlayfs.cpp
@@ -0,0 +1,479 @@
+/*
+ * Copyright (C) 2018 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 <dirent.h>
+#include <errno.h>
+#include <linux/fs.h>
+#include <selinux/selinux.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mount.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <map>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <android-base/file.h>
+#include <android-base/macros.h>
+#include <android-base/properties.h>
+#include <android-base/strings.h>
+#include <fs_mgr_overlayfs.h>
+#include <fstab/fstab.h>
+
+#include "fs_mgr_priv.h"
+
+using namespace std::literals;
+
+#if ALLOW_ADBD_DISABLE_VERITY == 0 // If we are a user build, provide stubs
+
+bool fs_mgr_overlayfs_mount_all() {
+ return false;
+}
+
+bool fs_mgr_overlayfs_setup(const char*, const char*, bool* change) {
+ if (change) *change = false;
+ return false;
+}
+
+bool fs_mgr_overlayfs_teardown(const char*, bool* change) {
+ if (change) *change = false;
+ return false;
+}
+
+#else // ALLOW_ADBD_DISABLE_VERITY == 0
+
+namespace {
+
+// acceptable overlayfs backing storage
+const auto kOverlayMountPoint = "/cache"s;
+
+// Return true if everything is mounted, but before adb is started. Right
+// after 'trigger load_persist_props_action' is done.
+bool fs_mgr_boot_completed() {
+ return android::base::GetBoolProperty("ro.persistent_properties.ready", false);
+}
+
+bool fs_mgr_is_dir(const std::string& path) {
+ struct stat st;
+ return !stat(path.c_str(), &st) && S_ISDIR(st.st_mode);
+}
+
+// Similar test as overlayfs workdir= validation in the kernel for read-write
+// validation, except we use fs_mgr_work. Covers space and storage issues.
+bool fs_mgr_dir_is_writable(const std::string& path) {
+ auto test_directory = path + "/fs_mgr_work";
+ rmdir(test_directory.c_str());
+ auto ret = !mkdir(test_directory.c_str(), 0700);
+ return ret | !rmdir(test_directory.c_str());
+}
+
+std::string fs_mgr_get_context(const std::string& mount_point) {
+ char* ctx = nullptr;
+ auto len = getfilecon(mount_point.c_str(), &ctx);
+ if ((len > 0) && ctx) {
+ std::string context(ctx, len);
+ free(ctx);
+ return context;
+ }
+ return "";
+}
+
+bool fs_mgr_overlayfs_enabled(const struct fstab_rec* fsrec) {
+ // readonly filesystem, can not be mount -o remount,rw
+ return "squashfs"s == fsrec->fs_type;
+}
+
+constexpr char upper_name[] = "upper";
+constexpr char work_name[] = "work";
+
+std::string fs_mgr_get_overlayfs_candidate(const std::string& mount_point) {
+ if (!fs_mgr_is_dir(mount_point)) return "";
+ auto dir = kOverlayMountPoint + "/overlay/" + android::base::Basename(mount_point) + "/";
+ auto upper = dir + upper_name;
+ if (!fs_mgr_is_dir(upper)) return "";
+ auto work = dir + work_name;
+ if (!fs_mgr_is_dir(work)) return "";
+ if (!fs_mgr_dir_is_writable(work)) return "";
+ return dir;
+}
+
+constexpr char lowerdir_option[] = "lowerdir=";
+constexpr char upperdir_option[] = "upperdir=";
+
+// default options for mount_point, returns empty string for none available.
+std::string fs_mgr_get_overlayfs_options(const char* mount_point) {
+ auto fsrec_mount_point = std::string(mount_point);
+ auto candidate = fs_mgr_get_overlayfs_candidate(fsrec_mount_point);
+ if (candidate.empty()) return "";
+
+ auto context = fs_mgr_get_context(fsrec_mount_point);
+ if (!context.empty()) context = ",rootcontext="s + context;
+ return "override_creds=off,"s + lowerdir_option + fsrec_mount_point + "," + upperdir_option +
+ candidate + upper_name + ",workdir=" + candidate + work_name + context;
+}
+
+bool fs_mgr_system_root_image(const fstab* fstab) {
+ if (!fstab) { // can not happen?
+ // This will return empty on init first_stage_mount,
+ // hence why we prefer checking the fstab instead.
+ return android::base::GetBoolProperty("ro.build.system_root_image", false);
+ }
+ for (auto i = 0; i < fstab->num_entries; i++) {
+ const auto fsrec = &fstab->recs[i];
+ auto fsrec_mount_point = fsrec->mount_point;
+ if (!fsrec_mount_point) continue;
+ if ("/system"s == fsrec_mount_point) return false;
+ }
+ return true;
+}
+
+std::string fs_mgr_get_overlayfs_options(const fstab* fstab, const char* mount_point) {
+ if (fs_mgr_system_root_image(fstab) && ("/"s == mount_point)) mount_point = "/system";
+
+ return fs_mgr_get_overlayfs_options(mount_point);
+}
+
+// return true if system supports overlayfs
+bool fs_mgr_wants_overlayfs() {
+ // This will return empty on init first_stage_mount, so speculative
+ // determination, empty (unset) _or_ "1" is true which differs from the
+ // official ro.debuggable policy. ALLOW_ADBD_DISABLE_VERITY == 0 should
+ // protect us from false in any case, so this is insurance.
+ auto debuggable = android::base::GetProperty("ro.debuggable", "1");
+ if (debuggable != "1") return false;
+
+ // Overlayfs available in the kernel, and patched for override_creds?
+ static signed char overlayfs_in_kernel = -1; // cache for constant condition
+ if (overlayfs_in_kernel == -1) {
+ auto save_errno = errno;
+ overlayfs_in_kernel = !access("/sys/module/overlay/parameters/override_creds", F_OK);
+ errno = save_errno;
+ }
+ return overlayfs_in_kernel;
+}
+
+bool fs_mgr_wants_overlayfs(const fstab_rec* fsrec) {
+ if (!fsrec) return false;
+
+ auto fsrec_mount_point = fsrec->mount_point;
+ if (!fsrec_mount_point) return false;
+
+ if (!fsrec->fs_type) return false;
+
+ // Don't check entries that are managed by vold.
+ if (fsrec->fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) return false;
+
+ // Only concerned with readonly partitions.
+ if (!(fsrec->flags & MS_RDONLY)) return false;
+
+ // If unbindable, do not allow overlayfs as this could expose us to
+ // security issues. On Android, this could also be used to turn off
+ // the ability to overlay an otherwise acceptable filesystem since
+ // /system and /vendor are never bound(sic) to.
+ if (fsrec->flags & MS_UNBINDABLE) return false;
+
+ if (!fs_mgr_overlayfs_enabled(fsrec)) return false;
+
+ // Verity enabled?
+ const auto basename_mount_point(android::base::Basename(fsrec_mount_point));
+ auto found = false;
+ fs_mgr_update_verity_state(
+ [&basename_mount_point, &found](fstab_rec*, const char* mount_point, int, int) {
+ if (mount_point && (basename_mount_point == mount_point)) found = true;
+ });
+ return !found;
+}
+
+bool fs_mgr_rm_all(const std::string& path, bool* change = nullptr) {
+ auto save_errno = errno;
+ std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
+ if (!dir) {
+ if (errno == ENOENT) {
+ errno = save_errno;
+ return true;
+ }
+ PERROR << "overlayfs open " << path;
+ return false;
+ }
+ dirent* entry;
+ auto ret = true;
+ while ((entry = readdir(dir.get()))) {
+ if (("."s == entry->d_name) || (".."s == entry->d_name)) continue;
+ auto file = path + "/" + entry->d_name;
+ if (entry->d_type == DT_UNKNOWN) {
+ struct stat st;
+ if (!lstat(file.c_str(), &st) && (st.st_mode & S_IFDIR)) entry->d_type = DT_DIR;
+ }
+ if (entry->d_type == DT_DIR) {
+ ret &= fs_mgr_rm_all(file, change);
+ if (!rmdir(file.c_str())) {
+ if (change) *change = true;
+ } else {
+ ret = false;
+ PERROR << "overlayfs rmdir " << file;
+ }
+ continue;
+ }
+ if (!unlink(file.c_str())) {
+ if (change) *change = true;
+ } else {
+ ret = false;
+ PERROR << "overlayfs rm " << file;
+ }
+ }
+ return ret;
+}
+
+bool fs_mgr_overlayfs_setup_one(const std::string& overlay, const std::string& mount_point,
+ bool* change) {
+ auto ret = true;
+ auto fsrec_mount_point = overlay + android::base::Basename(mount_point) + "/";
+ auto save_errno = errno;
+ if (!mkdir(fsrec_mount_point.c_str(), 0755)) {
+ if (change) *change = true;
+ } else if (errno != EEXIST) {
+ ret = false;
+ PERROR << "overlayfs mkdir " << fsrec_mount_point;
+ } else {
+ errno = save_errno;
+ }
+
+ save_errno = errno;
+ if (!mkdir((fsrec_mount_point + work_name).c_str(), 0755)) {
+ if (change) *change = true;
+ } else if (errno != EEXIST) {
+ ret = false;
+ PERROR << "overlayfs mkdir " << fsrec_mount_point << work_name;
+ } else {
+ errno = save_errno;
+ }
+
+ auto new_context = fs_mgr_get_context(mount_point);
+ if (!new_context.empty() && setfscreatecon(new_context.c_str())) {
+ ret = false;
+ PERROR << "overlayfs setfscreatecon " << new_context;
+ }
+ auto upper = fsrec_mount_point + upper_name;
+ save_errno = errno;
+ if (!mkdir(upper.c_str(), 0755)) {
+ if (change) *change = true;
+ } else if (errno != EEXIST) {
+ ret = false;
+ PERROR << "overlayfs mkdir " << upper;
+ } else {
+ errno = save_errno;
+ }
+ if (!new_context.empty()) setfscreatecon(nullptr);
+
+ return ret;
+}
+
+bool fs_mgr_overlayfs_mount(const fstab* fstab, const fstab_rec* fsrec) {
+ if (!fs_mgr_wants_overlayfs(fsrec)) return false;
+ auto fsrec_mount_point = fsrec->mount_point;
+ if (!fsrec_mount_point || !fsrec_mount_point[0]) return false;
+ auto options = fs_mgr_get_overlayfs_options(fstab, fsrec_mount_point);
+ if (options.empty()) return false;
+
+ // hijack __mount() report format to help triage
+ auto report = "__mount(source=overlay,target="s + fsrec_mount_point + ",type=overlay";
+ const auto opt_list = android::base::Split(options, ",");
+ for (const auto opt : opt_list) {
+ if (android::base::StartsWith(opt, upperdir_option)) {
+ report = report + "," + opt;
+ break;
+ }
+ }
+ report = report + ")=";
+
+ auto ret = mount("overlay", fsrec_mount_point, "overlay", MS_RDONLY | MS_RELATIME,
+ options.c_str());
+ if (ret) {
+ PERROR << report << ret;
+ return false;
+ } else {
+ LINFO << report << ret;
+ return true;
+ }
+}
+
+bool fs_mgr_overlayfs_already_mounted(const char* mount_point) {
+ if (!mount_point) return false;
+ std::unique_ptr<struct fstab, decltype(&fs_mgr_free_fstab)> fstab(
+ fs_mgr_read_fstab("/proc/mounts"), fs_mgr_free_fstab);
+ if (!fstab) return false;
+ const auto lowerdir = std::string(lowerdir_option) + mount_point;
+ for (auto i = 0; i < fstab->num_entries; ++i) {
+ const auto fsrec = &fstab->recs[i];
+ const auto fs_type = fsrec->fs_type;
+ if (!fs_type) continue;
+ if (("overlay"s != fs_type) && ("overlayfs"s != fs_type)) continue;
+ auto fsrec_mount_point = fsrec->mount_point;
+ if (!fsrec_mount_point) continue;
+ if (strcmp(fsrec_mount_point, mount_point)) continue;
+ const auto fs_options = fsrec->fs_options;
+ if (!fs_options) continue;
+ const auto options = android::base::Split(fs_options, ",");
+ for (const auto opt : options) {
+ if (opt == lowerdir) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+} // namespace
+
+bool fs_mgr_overlayfs_mount_all() {
+ auto ret = false;
+
+ if (!fs_mgr_wants_overlayfs()) return ret;
+
+ std::unique_ptr<struct fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
+ fs_mgr_free_fstab);
+ if (!fstab) return ret;
+
+ for (auto i = 0; i < fstab->num_entries; i++) {
+ const auto fsrec = &fstab->recs[i];
+ auto fsrec_mount_point = fsrec->mount_point;
+ if (!fsrec_mount_point) continue;
+ if (fs_mgr_overlayfs_already_mounted(fsrec_mount_point)) continue;
+
+ if (fs_mgr_overlayfs_mount(fstab.get(), fsrec)) ret = true;
+ }
+ return ret;
+}
+
+// Returns false if setup not permitted, errno set to last error.
+// If something is altered, set *change.
+bool fs_mgr_overlayfs_setup(const char* backing, const char* mount_point, bool* change) {
+ if (change) *change = false;
+ auto ret = false;
+ if (backing && (kOverlayMountPoint != backing)) {
+ errno = EINVAL;
+ return ret;
+ }
+ if (!fs_mgr_wants_overlayfs()) return ret;
+ if (!fs_mgr_boot_completed()) {
+ errno = EBUSY;
+ PERROR << "overlayfs setup";
+ return ret;
+ }
+
+ std::unique_ptr<struct fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
+ fs_mgr_free_fstab);
+ std::vector<std::string> mounts;
+ if (fstab) {
+ if (!fs_mgr_get_entry_for_mount_point(fstab.get(), kOverlayMountPoint)) return ret;
+ for (auto i = 0; i < fstab->num_entries; i++) {
+ const auto fsrec = &fstab->recs[i];
+ auto fsrec_mount_point = fsrec->mount_point;
+ if (!fsrec_mount_point) continue;
+ if (mount_point && strcmp(fsrec_mount_point, mount_point)) continue;
+ if (!fs_mgr_wants_overlayfs(fsrec)) continue;
+ mounts.emplace_back(fsrec_mount_point);
+ }
+ if (mounts.empty()) return ret;
+ }
+
+ if (mount_point && ("/"s == mount_point) && fs_mgr_system_root_image(fstab.get())) {
+ mount_point = "/system";
+ }
+ auto overlay = kOverlayMountPoint + "/overlay/";
+ auto save_errno = errno;
+ if (!mkdir(overlay.c_str(), 0755)) {
+ if (change) *change = true;
+ } else if (errno != EEXIST) {
+ PERROR << "overlayfs mkdir " << overlay;
+ } else {
+ errno = save_errno;
+ }
+ if (!fstab && mount_point && fs_mgr_overlayfs_setup_one(overlay, mount_point, change)) {
+ ret = true;
+ }
+ for (const auto& fsrec_mount_point : mounts) {
+ ret |= fs_mgr_overlayfs_setup_one(overlay, fsrec_mount_point, change);
+ }
+ return ret;
+}
+
+// Returns false if teardown not permitted, errno set to last error.
+// If something is altered, set *change.
+bool fs_mgr_overlayfs_teardown(const char* mount_point, bool* change) {
+ if (change) *change = false;
+ if (mount_point && ("/"s == mount_point)) {
+ std::unique_ptr<struct fstab, decltype(&fs_mgr_free_fstab)> fstab(
+ fs_mgr_read_fstab_default(), fs_mgr_free_fstab);
+ if (fs_mgr_system_root_image(fstab.get())) mount_point = "/system";
+ }
+ auto ret = true;
+ const auto overlay = kOverlayMountPoint + "/overlay";
+ const auto oldpath = overlay + (mount_point ?: "");
+ const auto newpath = oldpath + ".teardown";
+ ret &= fs_mgr_rm_all(newpath);
+ auto save_errno = errno;
+ if (!rename(oldpath.c_str(), newpath.c_str())) {
+ if (change) *change = true;
+ } else if (errno != ENOENT) {
+ ret = false;
+ PERROR << "overlayfs mv " << oldpath << " " << newpath;
+ } else {
+ errno = save_errno;
+ }
+ ret &= fs_mgr_rm_all(newpath, change);
+ save_errno = errno;
+ if (!rmdir(newpath.c_str())) {
+ if (change) *change = true;
+ } else if (errno != ENOENT) {
+ ret = false;
+ PERROR << "overlayfs rmdir " << newpath;
+ } else {
+ errno = save_errno;
+ }
+ if (mount_point) {
+ save_errno = errno;
+ if (!rmdir(overlay.c_str())) {
+ if (change) *change = true;
+ } else if ((errno != ENOENT) && (errno != ENOTEMPTY)) {
+ ret = false;
+ PERROR << "overlayfs rmdir " << overlay;
+ } else {
+ errno = save_errno;
+ }
+ }
+ if (!fs_mgr_wants_overlayfs()) {
+ // After obligatory teardown to make sure everything is clean, but if
+ // we didn't want overlayfs in the the first place, we do not want to
+ // waste time on a reboot (or reboot request message).
+ if (change) *change = false;
+ }
+ // And now that we did what we could, lets inform
+ // caller that there may still be more to do.
+ if (!fs_mgr_boot_completed()) {
+ errno = EBUSY;
+ PERROR << "overlayfs teardown";
+ ret = false;
+ }
+ return ret;
+}
+
+#endif // ALLOW_ADBD_DISABLE_VERITY != 0
diff --git a/fs_mgr/fs_mgr_priv.h b/fs_mgr/fs_mgr_priv.h
index ade0cc4..a347faf 100644
--- a/fs_mgr/fs_mgr_priv.h
+++ b/fs_mgr/fs_mgr_priv.h
@@ -45,7 +45,7 @@
#define PWARNING PLOG(WARNING) << FS_MGR_TAG
#define PERROR PLOG(ERROR) << FS_MGR_TAG
-#define CRYPTO_TMPFS_OPTIONS "size=256m,mode=0771,uid=1000,gid=1000"
+#define CRYPTO_TMPFS_OPTIONS "size=512m,mode=0771,uid=1000,gid=1000"
/* fstab has the following format:
*
@@ -82,6 +82,7 @@
*
*/
+// clang-format off
#define MF_WAIT 0x1
#define MF_CHECK 0x2
#define MF_CRYPT 0x4
@@ -111,6 +112,8 @@
#define MF_AVB 0X2000000
#define MF_KEYDIRECTORY 0X4000000
#define MF_SYSFS 0X8000000
+#define MF_LOGICAL 0x10000000
+// clang-format on
#define DM_BUF_SIZE 4096
diff --git a/fs_mgr/fs_mgr_priv_boot_config.h b/fs_mgr/fs_mgr_priv_boot_config.h
index d98dc02..417fb38 100644
--- a/fs_mgr/fs_mgr_priv_boot_config.h
+++ b/fs_mgr/fs_mgr_priv_boot_config.h
@@ -19,7 +19,13 @@
#include <sys/cdefs.h>
#include <string>
+#include <utility>
+#include <vector>
+std::vector<std::pair<std::string, std::string>> fs_mgr_parse_boot_config(const std::string& cmdline);
+
+bool fs_mgr_get_boot_config_from_kernel(const std::string& cmdline, const std::string& key,
+ std::string* out_val);
bool fs_mgr_get_boot_config_from_kernel_cmdline(const std::string& key, std::string* out_val);
bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val);
diff --git a/fs_mgr/fs_mgr_priv_dm_ioctl.h b/fs_mgr/fs_mgr_priv_dm_ioctl.h
deleted file mode 100644
index a00a9c1..0000000
--- a/fs_mgr/fs_mgr_priv_dm_ioctl.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2016 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.
- */
-
-#ifndef __CORE_FS_MGR_PRIV_DM_IOCTL_H
-#define __CORE_FS_MGR_PRIV_DM_IOCTL_H
-
-#include <linux/dm-ioctl.h>
-#include <string>
-
-void fs_mgr_verity_ioctl_init(struct dm_ioctl* io, const std::string& name, unsigned flags);
-
-bool fs_mgr_create_verity_device(struct dm_ioctl* io, const std::string& name, int fd);
-
-bool fs_mgr_destroy_verity_device(struct dm_ioctl* io, const std::string& name, int fd);
-
-bool fs_mgr_get_verity_device_name(struct dm_ioctl* io, const std::string& name, int fd,
- std::string* out_dev_name);
-
-bool fs_mgr_resume_verity_table(struct dm_ioctl* io, const std::string& name, int fd);
-
-#endif /* __CORE_FS_MGR_PRIV_DM_IOCTL_H */
diff --git a/fs_mgr/fs_mgr_slotselect.cpp b/fs_mgr/fs_mgr_slotselect.cpp
index 0a113b4..3b01d0e 100644
--- a/fs_mgr/fs_mgr_slotselect.cpp
+++ b/fs_mgr/fs_mgr_slotselect.cpp
@@ -36,19 +36,30 @@
std::string ab_suffix;
for (n = 0; n < fstab->num_entries; n++) {
- if (fstab->recs[n].fs_mgr_flags & MF_SLOTSELECT) {
- char *tmp;
+ fstab_rec& record = fstab->recs[n];
+ if (record.fs_mgr_flags & MF_SLOTSELECT) {
if (ab_suffix.empty()) {
ab_suffix = fs_mgr_get_slot_suffix();
// Return false if failed to get ab_suffix when MF_SLOTSELECT is specified.
if (ab_suffix.empty()) return false;
}
- if (asprintf(&tmp, "%s%s", fstab->recs[n].blk_device, ab_suffix.c_str()) > 0) {
- free(fstab->recs[n].blk_device);
- fstab->recs[n].blk_device = tmp;
- } else {
+
+ char* new_blk_device;
+ if (asprintf(&new_blk_device, "%s%s", record.blk_device, ab_suffix.c_str()) <= 0) {
return false;
}
+ free(record.blk_device);
+ record.blk_device = new_blk_device;
+
+ char* new_partition_name;
+ if (record.logical_partition_name) {
+ if (asprintf(&new_partition_name, "%s%s", record.logical_partition_name,
+ ab_suffix.c_str()) <= 0) {
+ return false;
+ }
+ free(record.logical_partition_name);
+ record.logical_partition_name = new_partition_name;
+ }
}
}
return true;
diff --git a/fs_mgr/fs_mgr_verity.cpp b/fs_mgr/fs_mgr_verity.cpp
index 896b603..5fb4ebb 100644
--- a/fs_mgr/fs_mgr_verity.cpp
+++ b/fs_mgr/fs_mgr_verity.cpp
@@ -35,6 +35,7 @@
#include <android-base/unique_fd.h>
#include <crypto_utils/android_pubkey.h>
#include <cutils/properties.h>
+#include <libdm/dm.h>
#include <logwrap/logwrap.h>
#include <openssl/obj_mac.h>
#include <openssl/rsa.h>
@@ -44,7 +45,6 @@
#include "fs_mgr.h"
#include "fs_mgr_priv.h"
-#include "fs_mgr_priv_dm_ioctl.h"
#define VERITY_TABLE_RSA_KEY "/verity_key"
#define VERITY_TABLE_HASH_IDX 8
@@ -250,47 +250,27 @@
return true;
}
-static int load_verity_table(struct dm_ioctl *io, const std::string &name,
- uint64_t device_size, int fd,
- const struct verity_table_params *params, format_verity_table_func format)
-{
- char *verity_params;
- char *buffer = (char*) io;
- size_t bufsize;
+static int load_verity_table(android::dm::DeviceMapper& dm, const std::string& name,
+ uint64_t device_size, const struct verity_table_params* params,
+ format_verity_table_func format) {
+ android::dm::DmTable table;
+ table.set_readonly(true);
- fs_mgr_verity_ioctl_init(io, name, DM_STATUS_TABLE_FLAG);
-
- struct dm_target_spec *tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
-
- // set tgt arguments
- io->target_count = 1;
- tgt->status = 0;
- tgt->sector_start = 0;
- tgt->length = device_size / 512;
- strcpy(tgt->target_type, "verity");
-
- // build the verity params
- verity_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
- bufsize = DM_BUF_SIZE - (verity_params - buffer);
-
- if (!format(verity_params, bufsize, params)) {
+ char buffer[DM_BUF_SIZE];
+ if (!format(buffer, sizeof(buffer), params)) {
LERROR << "Failed to format verity parameters";
return -1;
}
- LINFO << "loading verity table: '" << verity_params << "'";
-
- // set next target boundary
- verity_params += strlen(verity_params) + 1;
- verity_params = (char*)(((uintptr_t)verity_params + 7) & ~7);
- tgt->next = verity_params - buffer;
-
- // send the ioctl to load the verity table
- if (ioctl(fd, DM_TABLE_LOAD, io)) {
- PERROR << "Error loading verity table";
+ android::dm::DmTargetVerityString target(0, device_size / 512, buffer);
+ if (!table.AddTarget(std::make_unique<decltype(target)>(target))) {
+ LERROR << "Failed to add verity target";
return -1;
}
-
+ if (!dm.CreateDevice(name, table)) {
+ LERROR << "Failed to create verity device \"" << name << "\"";
+ return -1;
+ }
return 0;
}
@@ -760,11 +740,11 @@
struct fec_verity_metadata verity;
struct verity_table_params params = { .table = NULL };
- alignas(dm_ioctl) char buffer[DM_BUF_SIZE];
- struct dm_ioctl *io = (struct dm_ioctl *) buffer;
const std::string mount_point(basename(fstab->mount_point));
bool verified_at_boot = false;
+ android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance();
+
if (fec_open(&f, fstab->blk_device, O_RDONLY, FEC_VERITY_DISABLE,
FEC_DEFAULT_ROOTS) < 0) {
PERROR << "Failed to open '" << fstab->blk_device << "'";
@@ -797,24 +777,6 @@
params.ecc_dev = fstab->blk_device;
- // get the device mapper fd
- if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
- PERROR << "Error opening device mapper";
- goto out;
- }
-
- // create the device
- if (!fs_mgr_create_verity_device(io, mount_point, fd)) {
- LERROR << "Couldn't create verity device!";
- goto out;
- }
-
- // get the name of the device file
- if (!fs_mgr_get_verity_device_name(io, mount_point, fd, &verity_blk_name)) {
- LERROR << "Couldn't get verity device number!";
- goto out;
- }
-
if (load_verity_state(fstab, ¶ms.mode) < 0) {
/* if accessing or updating the state failed, switch to the default
* safe mode. This makes sure the device won't end up in an endless
@@ -860,8 +822,7 @@
fstab->fs_mgr_flags & MF_SLOTSELECT);
// load the verity mapping table
- if (load_verity_table(io, mount_point, verity.data_size, fd, ¶ms,
- format_verity_table) == 0) {
+ if (load_verity_table(dm, mount_point, verity.data_size, ¶ms, format_verity_table) == 0) {
goto loaded;
}
@@ -870,15 +831,14 @@
LINFO << "Disabling error correction for " << mount_point.c_str();
params.ecc.valid = false;
- if (load_verity_table(io, mount_point, verity.data_size, fd, ¶ms,
- format_verity_table) == 0) {
+ if (load_verity_table(dm, mount_point, verity.data_size, ¶ms, format_verity_table) == 0) {
goto loaded;
}
}
// try the legacy format for backwards compatibility
- if (load_verity_table(io, mount_point, verity.data_size, fd, ¶ms,
- format_legacy_verity_table) == 0) {
+ if (load_verity_table(dm, mount_point, verity.data_size, ¶ms, format_legacy_verity_table) ==
+ 0) {
goto loaded;
}
@@ -887,8 +847,8 @@
LINFO << "Falling back to EIO mode for " << mount_point.c_str();
params.mode = VERITY_MODE_EIO;
- if (load_verity_table(io, mount_point, verity.data_size, fd, ¶ms,
- format_legacy_verity_table) == 0) {
+ if (load_verity_table(dm, mount_point, verity.data_size, ¶ms,
+ format_legacy_verity_table) == 0) {
goto loaded;
}
}
@@ -897,9 +857,8 @@
goto out;
loaded:
-
- // activate the device
- if (!fs_mgr_resume_verity_table(io, mount_point, fd)) {
+ if (!dm.GetDmDevicePathByName(mount_point, &verity_blk_name)) {
+ LERROR << "Couldn't get verity device number!";
goto out;
}
@@ -922,7 +881,7 @@
if (!verified_at_boot) {
free(fstab->blk_device);
fstab->blk_device = strdup(verity_blk_name.c_str());
- } else if (!fs_mgr_destroy_verity_device(io, mount_point, fd)) {
+ } else if (!dm.DeleteDevice(mount_point)) {
LERROR << "Failed to remove verity device " << mount_point.c_str();
goto out;
}
diff --git a/fs_mgr/include/fs_mgr.h b/fs_mgr/include/fs_mgr.h
index 653d8fa..1049fb6 100644
--- a/fs_mgr/include/fs_mgr.h
+++ b/fs_mgr/include/fs_mgr.h
@@ -22,6 +22,8 @@
#include <stdbool.h>
#include <linux/dm-ioctl.h>
+#include <functional>
+
#include <fstab/fstab.h>
// Magic number at start of verity metadata
@@ -48,8 +50,8 @@
};
// Callback function for verity status
-typedef void (*fs_mgr_verity_state_callback)(struct fstab_rec *fstab,
- const char *mount_point, int mode, int status);
+typedef void fs_mgr_verity_state_callback(struct fstab_rec* fstab, const char* mount_point,
+ int mode, int status);
#define FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED 7
#define FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION 6
@@ -70,12 +72,12 @@
char *tmp_mount_point);
int fs_mgr_do_mount_one(struct fstab_rec *rec);
int fs_mgr_do_tmpfs_mount(const char *n_name);
-int fs_mgr_unmount_all(struct fstab *fstab);
struct fstab_rec const* fs_mgr_get_crypt_entry(struct fstab const* fstab);
void fs_mgr_get_crypt_info(struct fstab* fstab, char* key_loc, char* real_blk_device, size_t size);
bool fs_mgr_load_verity_state(int* mode);
-bool fs_mgr_update_verity_state(fs_mgr_verity_state_callback callback);
+bool fs_mgr_update_verity_state(std::function<fs_mgr_verity_state_callback> callback);
int fs_mgr_swapon_all(struct fstab *fstab);
+bool fs_mgr_update_logical_partition(struct fstab_rec* rec);
int fs_mgr_do_format(struct fstab_rec *fstab, bool reserve_footer);
diff --git a/fs_mgr/include/fs_mgr_dm_linear.h b/fs_mgr/include/fs_mgr_dm_linear.h
new file mode 100644
index 0000000..f15c450
--- /dev/null
+++ b/fs_mgr/include/fs_mgr_dm_linear.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __CORE_FS_MGR_DM_LINEAR_H
+#define __CORE_FS_MGR_DM_LINEAR_H
+
+#include <stdint.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <libdm/dm.h>
+#include <liblp/metadata_format.h>
+
+namespace android {
+namespace fs_mgr {
+
+bool CreateLogicalPartitions(const std::string& block_device);
+
+// Create a block device for a single logical partition, given metadata and
+// the partition name. On success, a path to the partition's block device is
+// returned. If |force_writable| is true, the "readonly" flag will be ignored
+// so the partition can be flashed.
+bool CreateLogicalPartition(const std::string& block_device, uint32_t metadata_slot,
+ const std::string& partition_name, bool force_writable,
+ std::string* path);
+
+// Destroy the block device for a logical partition, by name.
+bool DestroyLogicalPartition(const std::string& name);
+
+} // namespace fs_mgr
+} // namespace android
+
+#endif // __CORE_FS_MGR_DM_LINEAR_H
diff --git a/libcutils/include/cutils/open_memstream.h b/fs_mgr/include/fs_mgr_overlayfs.h
similarity index 60%
rename from libcutils/include/cutils/open_memstream.h
rename to fs_mgr/include/fs_mgr_overlayfs.h
index c1a81eb..1d2ff03 100644
--- a/libcutils/include/cutils/open_memstream.h
+++ b/fs_mgr/include/fs_mgr_overlayfs.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,23 +14,11 @@
* limitations under the License.
*/
-#ifndef __CUTILS_OPEN_MEMSTREAM_H__
-#define __CUTILS_OPEN_MEMSTREAM_H__
+#pragma once
-#include <stdio.h>
+#include <fstab/fstab.h>
-#if defined(__APPLE__)
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-FILE* open_memstream(char** bufp, size_t* sizep);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __APPLE__ */
-
-#endif /*__CUTILS_OPEN_MEMSTREAM_H__*/
+bool fs_mgr_overlayfs_mount_all();
+bool fs_mgr_overlayfs_setup(const char* backing = nullptr, const char* mount_point = nullptr,
+ bool* change = nullptr);
+bool fs_mgr_overlayfs_teardown(const char* mount_point = nullptr, bool* change = nullptr);
diff --git a/fs_mgr/include_fstab/fstab/fstab.h b/fs_mgr/include_fstab/fstab/fstab.h
index 8c585dd..b1ee328 100644
--- a/fs_mgr/include_fstab/fstab/fstab.h
+++ b/fs_mgr/include_fstab/fstab/fstab.h
@@ -22,6 +22,7 @@
#include <stdint.h>
#include <stdio.h>
+#include <set>
#include <string>
/*
@@ -32,11 +33,11 @@
struct fstab {
int num_entries;
struct fstab_rec* recs;
- char* fstab_filename;
};
struct fstab_rec {
char* blk_device;
+ char* logical_partition_name;
char* mount_point;
char* fs_type;
unsigned long flags;
@@ -84,8 +85,10 @@
int fs_mgr_is_nofail(const struct fstab_rec* fstab);
int fs_mgr_is_latemount(const struct fstab_rec* fstab);
int fs_mgr_is_quota(const struct fstab_rec* fstab);
+int fs_mgr_is_logical(const struct fstab_rec* fstab);
int fs_mgr_has_sysfs_path(const struct fstab_rec* fstab);
std::string fs_mgr_get_slot_suffix();
+std::set<std::string> fs_mgr_get_boot_devices();
#endif /* __CORE_FS_TAB_H */
diff --git a/fs_mgr/libdm/Android.bp b/fs_mgr/libdm/Android.bp
new file mode 100644
index 0000000..22af123
--- /dev/null
+++ b/fs_mgr/libdm/Android.bp
@@ -0,0 +1,54 @@
+//
+// Copyright (C) 2018 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.
+//
+
+cc_library_static {
+ name: "libdm",
+ defaults: ["fs_mgr_defaults"],
+ recovery_available: true,
+
+ export_include_dirs: ["include"],
+ cflags: [
+ // TODO(b/110035986): Allows us to create a skeleton of required classes
+ "-Wno-unused-private-field",
+ ],
+
+ srcs: [
+ "dm_table.cpp",
+ "dm_target.cpp",
+ "dm.cpp",
+ "loop_control.cpp",
+ ],
+
+ header_libs: [
+ "libbase_headers",
+ "liblog_headers",
+ ],
+}
+
+cc_test {
+ name: "libdm_test",
+ defaults: ["fs_mgr_defaults"],
+ static_libs: [
+ "libdm",
+ "libbase",
+ "liblog",
+ ],
+ srcs: [
+ "dm_test.cpp",
+ "loop_control_test.cpp",
+ "test_util.cpp",
+ ]
+}
diff --git a/fs_mgr/libdm/dm.cpp b/fs_mgr/libdm/dm.cpp
new file mode 100644
index 0000000..ad3b6f4
--- /dev/null
+++ b/fs_mgr/libdm/dm.cpp
@@ -0,0 +1,331 @@
+/*
+ * Copyright (C) 2018 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 "libdm/dm.h"
+
+#include <sys/ioctl.h>
+#include <sys/sysmacros.h>
+#include <sys/types.h>
+
+#include <android-base/macros.h>
+
+namespace android {
+namespace dm {
+
+DeviceMapper& DeviceMapper::Instance() {
+ static DeviceMapper instance;
+ return instance;
+}
+// Creates a new device mapper device
+bool DeviceMapper::CreateDevice(const std::string& name) {
+ if (name.empty()) {
+ LOG(ERROR) << "Unnamed device mapper device creation is not supported";
+ return false;
+ }
+
+ if (name.size() >= DM_NAME_LEN) {
+ LOG(ERROR) << "[" << name << "] is too long to be device mapper name";
+ return false;
+ }
+
+ struct dm_ioctl io;
+ InitIo(&io, name);
+
+ if (ioctl(fd_, DM_DEV_CREATE, &io)) {
+ PLOG(ERROR) << "DM_DEV_CREATE failed for [" << name << "]";
+ return false;
+ }
+
+ // Check to make sure the newly created device doesn't already have targets
+ // added or opened by someone
+ CHECK(io.target_count == 0) << "Unexpected targets for newly created [" << name << "] device";
+ CHECK(io.open_count == 0) << "Unexpected opens for newly created [" << name << "] device";
+
+ // Creates a new device mapper device with the name passed in
+ return true;
+}
+
+bool DeviceMapper::DeleteDevice(const std::string& name) {
+ if (name.empty()) {
+ LOG(ERROR) << "Unnamed device mapper device creation is not supported";
+ return false;
+ }
+
+ if (name.size() >= DM_NAME_LEN) {
+ LOG(ERROR) << "[" << name << "] is too long to be device mapper name";
+ return false;
+ }
+
+ struct dm_ioctl io;
+ InitIo(&io, name);
+
+ if (ioctl(fd_, DM_DEV_REMOVE, &io)) {
+ PLOG(ERROR) << "DM_DEV_REMOVE failed for [" << name << "]";
+ return false;
+ }
+
+ // Check to make sure appropriate uevent is generated so ueventd will
+ // do the right thing and remove the corresponding device node and symlinks.
+ CHECK(io.flags & DM_UEVENT_GENERATED_FLAG)
+ << "Didn't generate uevent for [" << name << "] removal";
+
+ return true;
+}
+
+const std::unique_ptr<DmTable> DeviceMapper::table(const std::string& /* name */) const {
+ // TODO(b/110035986): Return the table, as read from the kernel instead
+ return nullptr;
+}
+
+DmDeviceState DeviceMapper::state(const std::string& /* name */) const {
+ // TODO(b/110035986): Return the state, as read from the kernel instead
+ return DmDeviceState::INVALID;
+}
+
+bool DeviceMapper::CreateDevice(const std::string& name, const DmTable& table) {
+ if (!CreateDevice(name)) {
+ return false;
+ }
+ if (!LoadTableAndActivate(name, table)) {
+ DeleteDevice(name);
+ return false;
+ }
+ return true;
+}
+
+bool DeviceMapper::LoadTableAndActivate(const std::string& name, const DmTable& table) {
+ std::string ioctl_buffer(sizeof(struct dm_ioctl), 0);
+ ioctl_buffer += table.Serialize();
+
+ struct dm_ioctl* io = reinterpret_cast<struct dm_ioctl*>(&ioctl_buffer[0]);
+ InitIo(io, name);
+ io->data_size = ioctl_buffer.size();
+ io->data_start = sizeof(struct dm_ioctl);
+ io->target_count = static_cast<uint32_t>(table.num_targets());
+ if (table.readonly()) {
+ io->flags |= DM_READONLY_FLAG;
+ }
+ if (ioctl(fd_, DM_TABLE_LOAD, io)) {
+ PLOG(ERROR) << "DM_TABLE_LOAD failed";
+ return false;
+ }
+
+ InitIo(io, name);
+ if (ioctl(fd_, DM_DEV_SUSPEND, io)) {
+ PLOG(ERROR) << "DM_TABLE_SUSPEND resume failed";
+ return false;
+ }
+ return true;
+}
+
+// Reads all the available device mapper targets and their corresponding
+// versions from the kernel and returns in a vector
+bool DeviceMapper::GetAvailableTargets(std::vector<DmTargetTypeInfo>* targets) {
+ targets->clear();
+
+ // calculate the space needed to read a maximum of kMaxPossibleDmTargets
+ uint32_t payload_size = sizeof(struct dm_target_versions);
+ payload_size += DM_MAX_TYPE_NAME;
+ // device mapper wants every target spec to be aligned at 8-byte boundary
+ payload_size = DM_ALIGN(payload_size);
+ payload_size *= kMaxPossibleDmTargets;
+
+ uint32_t data_size = sizeof(struct dm_ioctl) + payload_size;
+ auto buffer = std::unique_ptr<void, void (*)(void*)>(calloc(1, data_size), free);
+ if (buffer == nullptr) {
+ LOG(ERROR) << "failed to allocate memory";
+ return false;
+ }
+
+ // Sets appropriate data size and data_start to make sure we tell kernel
+ // about the total size of the buffer we are passing and where to start
+ // writing the list of targets.
+ struct dm_ioctl* io = reinterpret_cast<struct dm_ioctl*>(buffer.get());
+ InitIo(io);
+ io->data_size = data_size;
+ io->data_start = sizeof(*io);
+
+ if (ioctl(fd_, DM_LIST_VERSIONS, io)) {
+ PLOG(ERROR) << "DM_LIST_VERSIONS failed";
+ return false;
+ }
+
+ // If the provided buffer wasn't enough to list all targets, note that
+ // any data beyond sizeof(*io) must not be read in this case
+ if (io->flags & DM_BUFFER_FULL_FLAG) {
+ LOG(INFO) << data_size << " is not enough memory to list all dm targets";
+ return false;
+ }
+
+ // if there are no targets registered, return success with empty vector
+ if (io->data_size == sizeof(*io)) {
+ return true;
+ }
+
+ // Parse each target and list the name and version
+ // TODO(b/110035986): Templatize this
+ uint32_t next = sizeof(*io);
+ data_size = io->data_size - next;
+ struct dm_target_versions* vers =
+ reinterpret_cast<struct dm_target_versions*>(static_cast<char*>(buffer.get()) + next);
+ while (next && data_size) {
+ targets->emplace_back(vers);
+ if (vers->next == 0) {
+ break;
+ }
+ next += vers->next;
+ data_size -= vers->next;
+ vers = reinterpret_cast<struct dm_target_versions*>(static_cast<char*>(buffer.get()) + next);
+ }
+
+ return true;
+}
+
+bool DeviceMapper::GetAvailableDevices(std::vector<DmBlockDevice>* devices) {
+ devices->clear();
+
+ // calculate the space needed to read a maximum of 256 targets, each with
+ // name with maximum length of 16 bytes
+ uint32_t payload_size = sizeof(struct dm_name_list);
+ // 128-bytes for the name
+ payload_size += DM_NAME_LEN;
+ // dm wants every device spec to be aligned at 8-byte boundary
+ payload_size = DM_ALIGN(payload_size);
+ payload_size *= kMaxPossibleDmDevices;
+ uint32_t data_size = sizeof(struct dm_ioctl) + payload_size;
+ auto buffer = std::unique_ptr<void, void (*)(void*)>(calloc(1, data_size), free);
+ if (buffer == nullptr) {
+ LOG(ERROR) << "failed to allocate memory";
+ return false;
+ }
+
+ // Sets appropriate data size and data_start to make sure we tell kernel
+ // about the total size of the buffer we are passing and where to start
+ // writing the list of targets.
+ struct dm_ioctl* io = reinterpret_cast<struct dm_ioctl*>(buffer.get());
+ InitIo(io);
+ io->data_size = data_size;
+ io->data_start = sizeof(*io);
+
+ if (ioctl(fd_, DM_LIST_DEVICES, io)) {
+ PLOG(ERROR) << "DM_LIST_DEVICES failed";
+ return false;
+ }
+
+ // If the provided buffer wasn't enough to list all devices any data
+ // beyond sizeof(*io) must not be read.
+ if (io->flags & DM_BUFFER_FULL_FLAG) {
+ LOG(INFO) << data_size << " is not enough memory to list all dm devices";
+ return false;
+ }
+
+ // if there are no devices created yet, return success with empty vector
+ if (io->data_size == sizeof(*io)) {
+ return true;
+ }
+
+ // Parse each device and add a new DmBlockDevice to the vector
+ // created from the kernel data.
+ uint32_t next = sizeof(*io);
+ data_size = io->data_size - next;
+ struct dm_name_list* dm_dev =
+ reinterpret_cast<struct dm_name_list*>(static_cast<char*>(buffer.get()) + next);
+
+ while (next && data_size) {
+ devices->emplace_back((dm_dev));
+ if (dm_dev->next == 0) {
+ break;
+ }
+ next += dm_dev->next;
+ data_size -= dm_dev->next;
+ dm_dev = reinterpret_cast<struct dm_name_list*>(static_cast<char*>(buffer.get()) + next);
+ }
+
+ return true;
+}
+
+// Accepts a device mapper device name (like system_a, vendor_b etc) and
+// returns the path to it's device node (or symlink to the device node)
+bool DeviceMapper::GetDmDevicePathByName(const std::string& name, std::string* path) {
+ struct dm_ioctl io;
+ InitIo(&io, name);
+ if (ioctl(fd_, DM_DEV_STATUS, &io) < 0) {
+ PLOG(ERROR) << "DM_DEV_STATUS failed for " << name;
+ return false;
+ }
+
+ uint32_t dev_num = minor(io.dev);
+ *path = "/dev/block/dm-" + std::to_string(dev_num);
+ return true;
+}
+
+bool DeviceMapper::GetTableStatus(const std::string& name, std::vector<TargetInfo>* table) {
+ char buffer[4096];
+ struct dm_ioctl* io = reinterpret_cast<struct dm_ioctl*>(buffer);
+
+ InitIo(io, name);
+ io->data_size = sizeof(buffer);
+ io->data_start = sizeof(*io);
+ if (ioctl(fd_, DM_TABLE_STATUS, io) < 0) {
+ PLOG(ERROR) << "DM_TABLE_STATUS failed for " << name;
+ return false;
+ }
+ if (io->flags & DM_BUFFER_FULL_FLAG) {
+ PLOG(ERROR) << "DM_TABLE_STATUS result for " << name << " was too large";
+ return false;
+ }
+
+ uint32_t cursor = io->data_start;
+ uint32_t data_end = std::min(io->data_size, uint32_t(sizeof(buffer)));
+ for (uint32_t i = 0; i < io->target_count; i++) {
+ if (cursor + sizeof(struct dm_target_spec) > data_end) {
+ break;
+ }
+ // After each dm_target_spec is a status string. spec->next is an
+ // offset from |io->data_start|, and we clamp it to the size of our
+ // buffer.
+ struct dm_target_spec* spec = reinterpret_cast<struct dm_target_spec*>(buffer + cursor);
+ uint32_t data_offset = cursor + sizeof(dm_target_spec);
+ uint32_t next_cursor = std::min(io->data_start + spec->next, data_end);
+
+ std::string data;
+ if (next_cursor > data_offset) {
+ // Note: we use c_str() to eliminate any extra trailing 0s.
+ data = std::string(buffer + data_offset, next_cursor - data_offset).c_str();
+ }
+ table->emplace_back(*spec, data);
+ cursor = next_cursor;
+ }
+ return true;
+}
+
+// private methods of DeviceMapper
+void DeviceMapper::InitIo(struct dm_ioctl* io, const std::string& name) const {
+ CHECK(io != nullptr) << "nullptr passed to dm_ioctl initialization";
+ memset(io, 0, sizeof(*io));
+
+ io->version[0] = DM_VERSION0;
+ io->version[1] = DM_VERSION1;
+ io->version[2] = DM_VERSION2;
+ io->data_size = sizeof(*io);
+ io->data_start = 0;
+ if (!name.empty()) {
+ strlcpy(io->name, name.c_str(), sizeof(io->name));
+ }
+}
+
+} // namespace dm
+} // namespace android
diff --git a/fs_mgr/libdm/dm_table.cpp b/fs_mgr/libdm/dm_table.cpp
new file mode 100644
index 0000000..15c7ce1
--- /dev/null
+++ b/fs_mgr/libdm/dm_table.cpp
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2018 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 "libdm/dm_table.h"
+
+#include <android-base/logging.h>
+#include <android-base/macros.h>
+
+namespace android {
+namespace dm {
+
+bool DmTable::AddTarget(std::unique_ptr<DmTarget>&& target) {
+ if (!target->Valid()) {
+ return false;
+ }
+ targets_.push_back(std::move(target));
+ return true;
+}
+
+bool DmTable::RemoveTarget(std::unique_ptr<DmTarget>&& /* target */) {
+ return true;
+}
+
+bool DmTable::valid() const {
+ if (targets_.empty()) {
+ LOG(ERROR) << "Device-mapper table must have at least one target.";
+ return "";
+ }
+ if (targets_[0]->start() != 0) {
+ LOG(ERROR) << "Device-mapper table must start at logical sector 0.";
+ return "";
+ }
+ return true;
+}
+
+uint64_t DmTable::num_sectors() const {
+ return valid() ? num_sectors_ : 0;
+}
+
+// Returns a string representation of the table that is ready to be passed
+// down to the kernel for loading.
+//
+// Implementation must verify there are no gaps in the table, table starts
+// with sector == 0, and iterate over each target to get its table
+// serialized.
+std::string DmTable::Serialize() const {
+ if (!valid()) {
+ return "";
+ }
+
+ std::string table;
+ for (const auto& target : targets_) {
+ table += target->Serialize();
+ }
+ return table;
+}
+
+} // namespace dm
+} // namespace android
diff --git a/fs_mgr/libdm/dm_target.cpp b/fs_mgr/libdm/dm_target.cpp
new file mode 100644
index 0000000..7c18267
--- /dev/null
+++ b/fs_mgr/libdm/dm_target.cpp
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2018 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 "libdm/dm_target.h"
+
+#include <android-base/logging.h>
+#include <android-base/macros.h>
+#include <android-base/strings.h>
+
+#include <libdm/dm.h>
+
+namespace android {
+namespace dm {
+
+std::string DmTarget::Serialize() const {
+ // Create a string containing a dm_target_spec, parameter data, and an
+ // explicit null terminator.
+ std::string data(sizeof(dm_target_spec), '\0');
+ data += GetParameterString();
+ data.push_back('\0');
+
+ // The kernel expects each target to be 8-byte aligned.
+ size_t padding = DM_ALIGN(data.size()) - data.size();
+ for (size_t i = 0; i < padding; i++) {
+ data.push_back('\0');
+ }
+
+ // Finally fill in the dm_target_spec.
+ struct dm_target_spec* spec = reinterpret_cast<struct dm_target_spec*>(&data[0]);
+ spec->sector_start = start();
+ spec->length = size();
+ strlcpy(spec->target_type, name().c_str(), sizeof(spec->target_type));
+ spec->next = (uint32_t)data.size();
+ return data;
+}
+
+std::string DmTargetZero::GetParameterString() const {
+ // The zero target type has no additional parameters.
+ return "";
+}
+
+std::string DmTargetLinear::GetParameterString() const {
+ return block_device_ + " " + std::to_string(physical_sector_);
+}
+
+DmTargetVerity::DmTargetVerity(uint64_t start, uint64_t length, uint32_t version,
+ const std::string& block_device, const std::string& hash_device,
+ uint32_t data_block_size, uint32_t hash_block_size,
+ uint32_t num_data_blocks, uint32_t hash_start_block,
+ const std::string& hash_algorithm, const std::string& root_digest,
+ const std::string& salt)
+ : DmTarget(start, length), valid_(true) {
+ base_args_ = {
+ std::to_string(version),
+ block_device,
+ hash_device,
+ std::to_string(data_block_size),
+ std::to_string(hash_block_size),
+ std::to_string(num_data_blocks),
+ std::to_string(hash_start_block),
+ hash_algorithm,
+ root_digest,
+ salt,
+ };
+}
+
+void DmTargetVerity::UseFec(const std::string& device, uint32_t num_roots, uint32_t num_blocks,
+ uint32_t start) {
+ optional_args_.emplace_back("use_fec_from_device");
+ optional_args_.emplace_back(device);
+ optional_args_.emplace_back("fec_roots");
+ optional_args_.emplace_back(std::to_string(num_roots));
+ optional_args_.emplace_back("fec_blocks");
+ optional_args_.emplace_back(std::to_string(num_blocks));
+ optional_args_.emplace_back("fec_start");
+ optional_args_.emplace_back(std::to_string(start));
+}
+
+void DmTargetVerity::SetVerityMode(const std::string& mode) {
+ if (mode != "restart_on_corruption" && mode != "ignore_corruption") {
+ LOG(ERROR) << "Unknown verity mode: " << mode;
+ valid_ = false;
+ return;
+ }
+ optional_args_.emplace_back(mode);
+}
+
+void DmTargetVerity::IgnoreZeroBlocks() {
+ optional_args_.emplace_back("ignore_zero_blocks");
+}
+
+std::string DmTargetVerity::GetParameterString() const {
+ std::string base = android::base::Join(base_args_, " ");
+ if (optional_args_.empty()) {
+ return base;
+ }
+ std::string optional = android::base::Join(optional_args_, " ");
+ return base + " " + std::to_string(optional_args_.size()) + " " + optional;
+}
+
+std::string DmTargetAndroidVerity::GetParameterString() const {
+ return keyid_ + " " + block_device_;
+}
+
+} // namespace dm
+} // namespace android
diff --git a/fs_mgr/libdm/dm_test.cpp b/fs_mgr/libdm/dm_test.cpp
new file mode 100644
index 0000000..cc61917
--- /dev/null
+++ b/fs_mgr/libdm/dm_test.cpp
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2018 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 <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <chrono>
+#include <ctime>
+#include <map>
+#include <thread>
+
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+#include <gtest/gtest.h>
+#include <libdm/dm.h>
+#include <libdm/loop_control.h>
+#include "test_util.h"
+
+using namespace std;
+using namespace android::dm;
+using unique_fd = android::base::unique_fd;
+
+TEST(libdm, HasMinimumTargets) {
+ DeviceMapper& dm = DeviceMapper::Instance();
+ vector<DmTargetTypeInfo> targets;
+ ASSERT_TRUE(dm.GetAvailableTargets(&targets));
+
+ map<string, DmTargetTypeInfo> by_name;
+ for (const auto& target : targets) {
+ by_name[target.name()] = target;
+ }
+
+ auto iter = by_name.find("linear");
+ EXPECT_NE(iter, by_name.end());
+}
+
+// Helper to ensure that device mapper devices are released.
+class TempDevice {
+ public:
+ TempDevice(const std::string& name, const DmTable& table)
+ : dm_(DeviceMapper::Instance()), name_(name), valid_(false) {
+ valid_ = dm_.CreateDevice(name, table);
+ }
+ TempDevice(TempDevice&& other) : dm_(other.dm_), name_(other.name_), valid_(other.valid_) {
+ other.valid_ = false;
+ }
+ ~TempDevice() {
+ if (valid_) {
+ dm_.DeleteDevice(name_);
+ }
+ }
+ bool Destroy() {
+ if (!valid_) {
+ return false;
+ }
+ valid_ = false;
+ return dm_.DeleteDevice(name_);
+ }
+ bool WaitForUdev() const {
+ auto start_time = std::chrono::steady_clock::now();
+ while (true) {
+ if (!access(path().c_str(), F_OK)) {
+ return true;
+ }
+ if (errno != ENOENT) {
+ return false;
+ }
+ std::this_thread::sleep_for(50ms);
+ std::chrono::duration elapsed = std::chrono::steady_clock::now() - start_time;
+ if (elapsed >= 5s) {
+ return false;
+ }
+ }
+ }
+ std::string path() const {
+ std::string device_path;
+ if (!dm_.GetDmDevicePathByName(name_, &device_path)) {
+ return "";
+ }
+ return device_path;
+ }
+ const std::string& name() const { return name_; }
+ bool valid() const { return valid_; }
+
+ TempDevice(const TempDevice&) = delete;
+ TempDevice& operator=(const TempDevice&) = delete;
+
+ TempDevice& operator=(TempDevice&& other) {
+ name_ = other.name_;
+ valid_ = other.valid_;
+ other.valid_ = false;
+ return *this;
+ }
+
+ private:
+ DeviceMapper& dm_;
+ std::string name_;
+ bool valid_;
+};
+
+TEST(libdm, DmLinear) {
+ unique_fd tmp1(CreateTempFile("file_1", 4096));
+ ASSERT_GE(tmp1, 0);
+ unique_fd tmp2(CreateTempFile("file_2", 4096));
+ ASSERT_GE(tmp2, 0);
+
+ // Create two different files. These will back two separate loop devices.
+ const char message1[] = "Hello! This is sector 1.";
+ const char message2[] = "Goodbye. This is sector 2.";
+ ASSERT_TRUE(android::base::WriteFully(tmp1, message1, sizeof(message1)));
+ ASSERT_TRUE(android::base::WriteFully(tmp2, message2, sizeof(message2)));
+
+ LoopDevice loop_a(tmp1);
+ ASSERT_TRUE(loop_a.valid());
+ LoopDevice loop_b(tmp2);
+ ASSERT_TRUE(loop_b.valid());
+
+ // Define a 2-sector device, with each sector mapping to the first sector
+ // of one of our loop devices.
+ DmTable table;
+ ASSERT_TRUE(table.AddTarget(make_unique<DmTargetLinear>(0, 1, loop_a.device(), 0)));
+ ASSERT_TRUE(table.AddTarget(make_unique<DmTargetLinear>(1, 1, loop_b.device(), 0)));
+ ASSERT_TRUE(table.valid());
+
+ TempDevice dev("libdm-test-dm-linear", table);
+ ASSERT_TRUE(dev.valid());
+ ASSERT_FALSE(dev.path().empty());
+ ASSERT_TRUE(dev.WaitForUdev());
+
+ // Note: a scope is needed to ensure that there are no open descriptors
+ // when we go to close the device.
+ {
+ unique_fd dev_fd(open(dev.path().c_str(), O_RDWR));
+ ASSERT_GE(dev_fd, 0);
+
+ // Test that each sector of our device is correctly mapped to each loop
+ // device.
+ char sector[512];
+ ASSERT_TRUE(android::base::ReadFully(dev_fd, sector, sizeof(sector)));
+ ASSERT_EQ(strncmp(sector, message1, sizeof(message1)), 0);
+ ASSERT_TRUE(android::base::ReadFully(dev_fd, sector, sizeof(sector)));
+ ASSERT_EQ(strncmp(sector, message2, sizeof(message2)), 0);
+ }
+
+ // Test GetTableStatus.
+ DeviceMapper& dm = DeviceMapper::Instance();
+ vector<DeviceMapper::TargetInfo> targets;
+ ASSERT_TRUE(dm.GetTableStatus(dev.name(), &targets));
+ ASSERT_EQ(targets.size(), 2);
+ EXPECT_EQ(strcmp(targets[0].spec.target_type, "linear"), 0);
+ EXPECT_TRUE(targets[0].data.empty());
+ EXPECT_EQ(targets[0].spec.sector_start, 0);
+ EXPECT_EQ(targets[0].spec.length, 1);
+ EXPECT_EQ(strcmp(targets[1].spec.target_type, "linear"), 0);
+ EXPECT_TRUE(targets[1].data.empty());
+ EXPECT_EQ(targets[1].spec.sector_start, 1);
+ EXPECT_EQ(targets[1].spec.length, 1);
+
+ // Normally the TestDevice destructor would delete this, but at least one
+ // test should ensure that device deletion works.
+ ASSERT_TRUE(dev.Destroy());
+}
+
+TEST(libdm, DmVerityArgsAvb2) {
+ std::string device = "/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a";
+ std::string algorithm = "sha1";
+ std::string digest = "4be7e823b8c40f7bd5c8ccd5123f0722c5baca21";
+ std::string salt = "cc99f81ecb9484220a003b0719ee59dcf9be7e5d";
+
+ DmTargetVerity target(0, 10000, 1, device, device, 4096, 4096, 125961, 125961, algorithm,
+ digest, salt);
+ target.UseFec(device, 2, 126955, 126955);
+ target.SetVerityMode("restart_on_corruption");
+ target.IgnoreZeroBlocks();
+
+ // Verity table from a walleye build.
+ std::string expected =
+ "1 /dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a "
+ "/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a 4096 4096 125961 125961 sha1 "
+ "4be7e823b8c40f7bd5c8ccd5123f0722c5baca21 cc99f81ecb9484220a003b0719ee59dcf9be7e5d 10 "
+ "use_fec_from_device /dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a fec_roots "
+ "2 fec_blocks 126955 fec_start 126955 restart_on_corruption ignore_zero_blocks";
+ EXPECT_EQ(target.GetParameterString(), expected);
+}
diff --git a/fs_mgr/libdm/include/libdm/dm.h b/fs_mgr/libdm/include/libdm/dm.h
new file mode 100644
index 0000000..e2bc729
--- /dev/null
+++ b/fs_mgr/libdm/include/libdm/dm.h
@@ -0,0 +1,171 @@
+/*
+ * Copyright 2018 Google, Inc
+ *
+ * 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.
+ */
+
+#ifndef _LIBDM_DM_H_
+#define _LIBDM_DM_H_
+
+#include <fcntl.h>
+#include <linux/dm-ioctl.h>
+#include <linux/kdev_t.h>
+#include <stdint.h>
+#include <sys/sysmacros.h>
+#include <unistd.h>
+
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include <android-base/logging.h>
+
+#include "dm_table.h"
+
+// The minimum expected device mapper major.minor version
+#define DM_VERSION0 (4)
+#define DM_VERSION1 (0)
+#define DM_VERSION2 (0)
+
+#define DM_ALIGN_MASK (7)
+#define DM_ALIGN(x) ((x + DM_ALIGN_MASK) & ~DM_ALIGN_MASK)
+
+namespace android {
+namespace dm {
+
+enum class DmDeviceState { INVALID, SUSPENDED, ACTIVE };
+
+class DeviceMapper final {
+ public:
+ class DmBlockDevice final {
+ public:
+ // only allow creating this with dm_name_list
+ DmBlockDevice() = delete;
+
+ explicit DmBlockDevice(struct dm_name_list* d) : name_(d->name), dev_(d->dev){};
+
+ // Returs device mapper name associated with the block device
+ const std::string& name() const { return name_; }
+
+ // Return major number for the block device
+ uint32_t Major() const { return major(dev_); }
+
+ // Return minor number for the block device
+ uint32_t Minor() const { return minor(dev_); }
+ ~DmBlockDevice() = default;
+
+ private:
+ std::string name_;
+ uint64_t dev_;
+ };
+
+ // Removes a device mapper device with the given name.
+ // Returns 'true' on success, false otherwise.
+ bool DeleteDevice(const std::string& name);
+
+ // Reads the device mapper table from the device with given anme and
+ // returns it in a DmTable object.
+ const std::unique_ptr<DmTable> table(const std::string& name) const;
+
+ // Returns the current state of the underlying device mapper device
+ // with given name.
+ // One of INVALID, SUSPENDED or ACTIVE.
+ DmDeviceState state(const std::string& name) const;
+
+ // Creates a device, loads the given table, and activates it. If the device
+ // is not able to be activated, it is destroyed, and false is returned.
+ bool CreateDevice(const std::string& name, const DmTable& table);
+
+ // Loads the device mapper table from parameter into the underlying device
+ // mapper device with given name and activate / resumes the device in the
+ // process. A device with the given name must already exist.
+ //
+ // Returns 'true' on success, false otherwise.
+ bool LoadTableAndActivate(const std::string& name, const DmTable& table);
+
+ // Returns true if a list of available device mapper targets registered in the kernel was
+ // successfully read and stored in 'targets'. Returns 'false' otherwise.
+ bool GetAvailableTargets(std::vector<DmTargetTypeInfo>* targets);
+
+ // Return 'true' if it can successfully read the list of device mapper block devices
+ // currently created. 'devices' will be empty if the kernel interactions
+ // were successful and there are no block devices at the moment. Returns
+ // 'false' in case of any failure along the way.
+ bool GetAvailableDevices(std::vector<DmBlockDevice>* devices);
+
+ // Returns the path to the device mapper device node in '/dev' corresponding to
+ // 'name'. If the device does not exist, false is returned, and the path
+ // parameter is not set.
+ bool GetDmDevicePathByName(const std::string& name, std::string* path);
+
+ // The only way to create a DeviceMapper object.
+ static DeviceMapper& Instance();
+
+ ~DeviceMapper() {
+ if (fd_ != -1) {
+ ::close(fd_);
+ }
+ }
+
+ // Query the status of a table, given a device name. The output vector will
+ // contain one TargetInfo for each target in the table. If the device does
+ // not exist, or there were too many targets, the call will fail and return
+ // false.
+ struct TargetInfo {
+ struct dm_target_spec spec;
+ std::string data;
+ TargetInfo(const struct dm_target_spec& spec, const std::string& data)
+ : spec(spec), data(data) {}
+ };
+ bool GetTableStatus(const std::string& name, std::vector<TargetInfo>* table);
+
+ private:
+ // Maximum possible device mapper targets registered in the kernel.
+ // This is only used to read the list of targets from kernel so we allocate
+ // a finite amount of memory. This limit is in no way enforced by the kernel.
+ static constexpr uint32_t kMaxPossibleDmTargets = 256;
+
+ // Maximum possible device mapper created block devices. Note that this is restricted by
+ // the minor numbers (that used to be 8 bits) that can be range from 0 to 2^20-1 in newer
+ // kernels. In Android systems however, we never expect these to grow beyond the artificial
+ // limit we are imposing here of 256.
+ static constexpr uint32_t kMaxPossibleDmDevices = 256;
+
+ void InitIo(struct dm_ioctl* io, const std::string& name = std::string()) const;
+
+ DeviceMapper() : fd_(-1) {
+ fd_ = TEMP_FAILURE_RETRY(open("/dev/device-mapper", O_RDWR | O_CLOEXEC));
+ if (fd_ < 0) {
+ PLOG(ERROR) << "Failed to open device-mapper";
+ }
+ }
+
+ // Creates a device mapper device with given name.
+ // Return 'true' on success and 'false' on failure to
+ // create OR if a device mapper device with the same name already
+ // exists.
+ bool CreateDevice(const std::string& name);
+
+ int fd_;
+ // Non-copyable & Non-movable
+ DeviceMapper(const DeviceMapper&) = delete;
+ DeviceMapper& operator=(const DeviceMapper&) = delete;
+ DeviceMapper& operator=(DeviceMapper&&) = delete;
+ DeviceMapper(DeviceMapper&&) = delete;
+};
+
+} // namespace dm
+} // namespace android
+
+#endif /* _LIBDM_DM_H_ */
diff --git a/fs_mgr/libdm/include/libdm/dm_table.h b/fs_mgr/libdm/include/libdm/dm_table.h
new file mode 100644
index 0000000..5c639be
--- /dev/null
+++ b/fs_mgr/libdm/include/libdm/dm_table.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2018 Google, Inc
+ *
+ * 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.
+ */
+
+#ifndef _LIBDM_DMTABLE_H_
+#define _LIBDM_DMTABLE_H_
+
+#include <stdint.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "dm_target.h"
+
+namespace android {
+namespace dm {
+
+class DmTable {
+ public:
+ DmTable() : num_sectors_(0), readonly_(false) {}
+
+ // Adds a target to the device mapper table for a range specified in the target object.
+ // The function will return 'true' if the target was successfully added and doesn't overlap with
+ // any of the existing targets in the table. Gaps are allowed. The final check, including
+ // overlaps and gaps are done before loading the table. Returns 'false' on failure.
+ bool AddTarget(std::unique_ptr<DmTarget>&& target);
+
+ // Removes a target from the table for the range specified in the target object. Returns 'false'
+ // if the target name doesn't match with the one in the table. Returns 'true' if target is
+ // successfully removed.
+ bool RemoveTarget(std::unique_ptr<DmTarget>&& target);
+
+ // Checks the table to make sure it is valid. i.e. Checks for range overlaps, range gaps
+ // and returns 'true' if the table is ready to be loaded into kernel. Returns 'false' if the
+ // table is malformed.
+ bool valid() const;
+
+ // Returns the toatl number of targets.
+ size_t num_targets() const { return targets_.size(); }
+
+ // Returns the total size represented by the table in terms of number of 512-byte sectors.
+ // NOTE: This function will overlook if there are any gaps in the targets added in the table.
+ uint64_t num_sectors() const;
+
+ // Returns the string represntation of the table that is ready to be passed into the kernel
+ // as part of the DM_TABLE_LOAD ioctl.
+ std::string Serialize() const;
+
+ void set_readonly(bool readonly) { readonly_ = readonly; }
+ bool readonly() const { return readonly_; }
+
+ ~DmTable() = default;
+
+ private:
+ // list of targets defined in this table sorted by
+ // their start and end sectors.
+ // Note: Overlapping targets MUST never be added in this list.
+ std::vector<std::unique_ptr<DmTarget>> targets_;
+
+ // Total size in terms of # of sectors, as calculated by looking at the last and the first
+ // target in 'target_'.
+ uint64_t num_sectors_;
+
+ // True if the device should be read-only; false otherwise.
+ bool readonly_;
+};
+
+} // namespace dm
+} // namespace android
+
+#endif /* _LIBDM_DMTABLE_H_ */
diff --git a/fs_mgr/libdm/include/libdm/dm_target.h b/fs_mgr/libdm/include/libdm/dm_target.h
new file mode 100644
index 0000000..31863c8
--- /dev/null
+++ b/fs_mgr/libdm/include/libdm/dm_target.h
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2018 Google, Inc
+ *
+ * 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.
+ */
+
+#ifndef _LIBDM_DMTARGET_H_
+#define _LIBDM_DMTARGET_H_
+
+#include <linux/dm-ioctl.h>
+#include <stdint.h>
+
+#include <string>
+#include <vector>
+
+#include <android-base/logging.h>
+
+namespace android {
+namespace dm {
+
+class DmTargetTypeInfo {
+ public:
+ DmTargetTypeInfo() : major_(0), minor_(0), patch_(0) {}
+ DmTargetTypeInfo(const struct dm_target_versions* info)
+ : name_(info->name),
+ major_(info->version[0]),
+ minor_(info->version[1]),
+ patch_(info->version[2]) {}
+
+ const std::string& name() const { return name_; }
+ std::string version() const {
+ return std::to_string(major_) + "." + std::to_string(minor_) + "." + std::to_string(patch_);
+ }
+
+ private:
+ std::string name_;
+ uint32_t major_;
+ uint32_t minor_;
+ uint32_t patch_;
+};
+
+class DmTarget {
+ public:
+ DmTarget(uint64_t start, uint64_t length) : start_(start), length_(length) {}
+
+ virtual ~DmTarget() = default;
+
+ // Returns name of the target.
+ virtual std::string name() const = 0;
+
+ // Return the first logical sector represented by this target.
+ uint64_t start() const { return start_; }
+
+ // Returns size in number of sectors when this target is part of
+ // a DmTable, return 0 otherwise.
+ uint64_t size() const { return length_; }
+
+ // Function that converts this object to a string of arguments that can
+ // be passed to the kernel for adding this target in a table. Each target (e.g. verity, linear)
+ // must implement this, for it to be used on a device.
+ std::string Serialize() const;
+
+ virtual bool Valid() const { return true; }
+
+ protected:
+ // Get the parameter string that is passed to the end of the dm_target_spec
+ // for this target type.
+ virtual std::string GetParameterString() const = 0;
+
+ private:
+ // logical sector number start and total length (in terms of 512-byte sectors) represented
+ // by this target within a DmTable.
+ uint64_t start_, length_;
+};
+
+class DmTargetZero final : public DmTarget {
+ public:
+ DmTargetZero(uint64_t start, uint64_t length) : DmTarget(start, length) {}
+
+ std::string name() const override { return "zero"; }
+ std::string GetParameterString() const override;
+};
+
+class DmTargetLinear final : public DmTarget {
+ public:
+ DmTargetLinear(uint64_t start, uint64_t length, const std::string& block_device,
+ uint64_t physical_sector)
+ : DmTarget(start, length), block_device_(block_device), physical_sector_(physical_sector) {}
+
+ std::string name() const override { return "linear"; }
+ std::string GetParameterString() const override;
+ const std::string& block_device() const { return block_device_; }
+
+ private:
+ std::string block_device_;
+ uint64_t physical_sector_;
+};
+
+class DmTargetVerity final : public DmTarget {
+ public:
+ DmTargetVerity(uint64_t start, uint64_t length, uint32_t version,
+ const std::string& block_device, const std::string& hash_device,
+ uint32_t data_block_size, uint32_t hash_block_size, uint32_t num_data_blocks,
+ uint32_t hash_start_block, const std::string& hash_algorithm,
+ const std::string& root_digest, const std::string& salt);
+
+ void UseFec(const std::string& device, uint32_t num_roots, uint32_t num_blocks, uint32_t start);
+ void SetVerityMode(const std::string& mode);
+ void IgnoreZeroBlocks();
+
+ std::string name() const override { return "verity"; }
+ std::string GetParameterString() const override;
+ bool Valid() const override { return valid_; }
+
+ private:
+ std::vector<std::string> base_args_;
+ std::vector<std::string> optional_args_;
+ bool valid_;
+};
+
+class DmTargetAndroidVerity final : public DmTarget {
+ public:
+ DmTargetAndroidVerity(uint64_t start, uint64_t length, const std::string& block_device,
+ const std::string& keyid)
+ : DmTarget(start, length), keyid_(keyid), block_device_(block_device) {}
+
+ std::string name() const override { return "android-verity"; }
+ std::string GetParameterString() const override;
+
+ private:
+ std::string keyid_;
+ std::string block_device_;
+};
+
+// This is the same as DmTargetVerity, but the table may be specified as a raw
+// string. This code exists only for fs_mgr_verity and should be avoided. Use
+// DmTargetVerity for new code instead.
+class DmTargetVerityString final : public DmTarget {
+ public:
+ DmTargetVerityString(uint64_t start, uint64_t length, const std::string& target_string)
+ : DmTarget(start, length), target_string_(target_string) {}
+
+ std::string name() const override { return "verity"; }
+ std::string GetParameterString() const override { return target_string_; }
+ bool Valid() const override { return true; }
+
+ private:
+ std::string target_string_;
+};
+
+} // namespace dm
+} // namespace android
+
+#endif /* _LIBDM_DMTARGET_H_ */
diff --git a/fs_mgr/libdm/include/libdm/loop_control.h b/fs_mgr/libdm/include/libdm/loop_control.h
new file mode 100644
index 0000000..e6e83f4
--- /dev/null
+++ b/fs_mgr/libdm/include/libdm/loop_control.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2018 Google, Inc
+ *
+ * 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.
+ */
+
+#ifndef _LIBDM_LOOP_CONTROL_H_
+#define _LIBDM_LOOP_CONTROL_H_
+
+#include <string>
+
+#include <android-base/unique_fd.h>
+
+namespace android {
+namespace dm {
+
+class LoopControl final {
+ public:
+ LoopControl();
+
+ // Attaches the file specified by 'file_fd' to the loop device specified
+ // by 'loopdev'
+ bool Attach(int file_fd, std::string* loopdev) const;
+
+ // Detach the loop device given by 'loopdev' from the attached backing file.
+ bool Detach(const std::string& loopdev) const;
+
+ LoopControl(const LoopControl&) = delete;
+ LoopControl& operator=(const LoopControl&) = delete;
+ LoopControl& operator=(LoopControl&&) = default;
+ LoopControl(LoopControl&&) = default;
+
+ private:
+ bool FindFreeLoopDevice(std::string* loopdev) const;
+
+ static constexpr const char* kLoopControlDevice = "/dev/loop-control";
+
+ android::base::unique_fd control_fd_;
+};
+
+// Create a temporary loop device around a file descriptor or path.
+class LoopDevice {
+ public:
+ // Create a loop device for the given file descriptor. It is closed when
+ // LoopDevice is destroyed only if auto_close is true.
+ LoopDevice(int fd, bool auto_close = false);
+ // Create a loop device for the given file path. It will be opened for
+ // reading and writing and closed when the loop device is detached.
+ explicit LoopDevice(const std::string& path);
+ ~LoopDevice();
+
+ bool valid() const { return fd_ != -1 && !device_.empty(); }
+ const std::string& device() const { return device_; }
+
+ LoopDevice(const LoopDevice&) = delete;
+ LoopDevice& operator=(const LoopDevice&) = delete;
+ LoopDevice& operator=(LoopDevice&&) = default;
+ LoopDevice(LoopDevice&&) = default;
+
+ private:
+ void Init();
+
+ android::base::unique_fd fd_;
+ bool owns_fd_;
+ std::string device_;
+ LoopControl control_;
+};
+
+} // namespace dm
+} // namespace android
+
+#endif /* _LIBDM_LOOP_CONTROL_H_ */
diff --git a/fs_mgr/libdm/loop_control.cpp b/fs_mgr/libdm/loop_control.cpp
new file mode 100644
index 0000000..0beb1a6
--- /dev/null
+++ b/fs_mgr/libdm/loop_control.cpp
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2018 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 "libdm/loop_control.h"
+
+#include <fcntl.h>
+#include <linux/loop.h>
+#include <stdint.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <android-base/logging.h>
+#include <android-base/stringprintf.h>
+#include <android-base/unique_fd.h>
+
+namespace android {
+namespace dm {
+
+LoopControl::LoopControl() : control_fd_(-1) {
+ control_fd_.reset(TEMP_FAILURE_RETRY(open(kLoopControlDevice, O_RDWR | O_CLOEXEC)));
+ if (control_fd_ < 0) {
+ PLOG(ERROR) << "Failed to open loop-control";
+ }
+}
+
+bool LoopControl::Attach(int file_fd, std::string* loopdev) const {
+ if (!FindFreeLoopDevice(loopdev)) {
+ LOG(ERROR) << "Failed to attach, no free loop devices";
+ return false;
+ }
+
+ android::base::unique_fd loop_fd(TEMP_FAILURE_RETRY(open(loopdev->c_str(), O_RDWR | O_CLOEXEC)));
+ if (loop_fd < 0) {
+ PLOG(ERROR) << "Failed to open: " << *loopdev;
+ return false;
+ }
+
+ int rc = ioctl(loop_fd, LOOP_SET_FD, file_fd);
+ if (rc < 0) {
+ PLOG(ERROR) << "Failed LOOP_SET_FD";
+ return false;
+ }
+ return true;
+}
+
+bool LoopControl::Detach(const std::string& loopdev) const {
+ if (loopdev.empty()) {
+ LOG(ERROR) << "Must provide a loop device";
+ return false;
+ }
+
+ android::base::unique_fd loop_fd(TEMP_FAILURE_RETRY(open(loopdev.c_str(), O_RDWR | O_CLOEXEC)));
+ if (loop_fd < 0) {
+ PLOG(ERROR) << "Failed to open: " << loopdev;
+ return false;
+ }
+
+ int rc = ioctl(loop_fd, LOOP_CLR_FD, 0);
+ if (rc) {
+ PLOG(ERROR) << "Failed LOOP_CLR_FD for '" << loopdev << "'";
+ return false;
+ }
+ return true;
+}
+
+bool LoopControl::FindFreeLoopDevice(std::string* loopdev) const {
+ int rc = ioctl(control_fd_, LOOP_CTL_GET_FREE);
+ if (rc < 0) {
+ PLOG(ERROR) << "Failed to get free loop device";
+ return false;
+ }
+
+ // Ueventd on android creates all loop devices as /dev/block/loopX
+ // The total number of available devices is determined by 'loop.max_part'
+ // kernel command line argument.
+ *loopdev = ::android::base::StringPrintf("/dev/block/loop%d", rc);
+ return true;
+}
+
+LoopDevice::LoopDevice(int fd, bool auto_close) : fd_(fd), owns_fd_(auto_close) {
+ Init();
+}
+
+LoopDevice::LoopDevice(const std::string& path) : fd_(-1), owns_fd_(true) {
+ fd_.reset(open(path.c_str(), O_RDWR | O_CLOEXEC));
+ if (fd_ < -1) {
+ PLOG(ERROR) << "open failed for " << path;
+ return;
+ }
+ Init();
+}
+
+LoopDevice::~LoopDevice() {
+ if (valid()) {
+ control_.Detach(device_);
+ }
+ if (!owns_fd_) {
+ (void)fd_.release();
+ }
+}
+
+void LoopDevice::Init() {
+ control_.Attach(fd_, &device_);
+}
+
+} // namespace dm
+} // namespace android
diff --git a/fs_mgr/libdm/loop_control_test.cpp b/fs_mgr/libdm/loop_control_test.cpp
new file mode 100644
index 0000000..08bdc00
--- /dev/null
+++ b/fs_mgr/libdm/loop_control_test.cpp
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2018 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 "libdm/loop_control.h"
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+#include <gtest/gtest.h>
+#include "test_util.h"
+
+using namespace std;
+using namespace android::dm;
+using unique_fd = android::base::unique_fd;
+
+static unique_fd TempFile() {
+ // A loop device needs to be at least one sector to actually work, so fill
+ // up the file with a message.
+ unique_fd fd(CreateTempFile("temp", 0));
+ if (fd < 0) {
+ return {};
+ }
+ char buffer[] = "Hello";
+ for (size_t i = 0; i < 1000; i++) {
+ if (!android::base::WriteFully(fd, buffer, sizeof(buffer))) {
+ perror("write");
+ return {};
+ }
+ }
+ return fd;
+}
+
+TEST(libdm, LoopControl) {
+ unique_fd fd = TempFile();
+ ASSERT_GE(fd, 0);
+
+ LoopDevice loop(fd);
+ ASSERT_TRUE(loop.valid());
+
+ char buffer[6];
+ unique_fd loop_fd(open(loop.device().c_str(), O_RDWR));
+ ASSERT_GE(loop_fd, 0);
+ ASSERT_TRUE(android::base::ReadFully(loop_fd, buffer, sizeof(buffer)));
+ ASSERT_EQ(memcmp(buffer, "Hello", 6), 0);
+}
diff --git a/fs_mgr/libdm/test_util.cpp b/fs_mgr/libdm/test_util.cpp
new file mode 100644
index 0000000..307251c
--- /dev/null
+++ b/fs_mgr/libdm/test_util.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2018 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 <fcntl.h>
+#include <linux/memfd.h>
+#include <stdio.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "test_util.h"
+
+namespace android {
+namespace dm {
+
+using unique_fd = android::base::unique_fd;
+
+// Create a temporary in-memory file. If size is non-zero, the file will be
+// created with a fixed size.
+unique_fd CreateTempFile(const std::string& name, size_t size) {
+ unique_fd fd(syscall(__NR_memfd_create, name.c_str(), MFD_ALLOW_SEALING));
+ if (fd < 0) {
+ return {};
+ }
+ if (size) {
+ if (ftruncate(fd, size) < 0) {
+ perror("ftruncate");
+ return {};
+ }
+ if (fcntl(fd, F_ADD_SEALS, F_SEAL_GROW | F_SEAL_SHRINK) < 0) {
+ perror("fcntl");
+ return {};
+ }
+ }
+ return fd;
+}
+
+} // namespace dm
+} // namespace android
diff --git a/fs_mgr/libdm/test_util.h b/fs_mgr/libdm/test_util.h
new file mode 100644
index 0000000..96b051c
--- /dev/null
+++ b/fs_mgr/libdm/test_util.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef _LIBDM_TEST_UTILS_H_
+#define _LIBDM_TEST_UTILS_H_
+
+#include <android-base/unique_fd.h>
+#include <stddef.h>
+
+#include <string>
+
+namespace android {
+namespace dm {
+
+// Create a temporary in-memory file. If size is non-zero, the file will be
+// created with a fixed size.
+android::base::unique_fd CreateTempFile(const std::string& name, size_t size);
+
+} // namespace dm
+} // namespace android
+
+#endif // _LIBDM_TEST_UTILS_H_
diff --git a/fs_mgr/liblp/Android.bp b/fs_mgr/liblp/Android.bp
new file mode 100644
index 0000000..89282db
--- /dev/null
+++ b/fs_mgr/liblp/Android.bp
@@ -0,0 +1,61 @@
+//
+// Copyright (C) 2018 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.
+//
+
+cc_library {
+ name: "liblp",
+ host_supported: true,
+ recovery_available: true,
+ defaults: ["fs_mgr_defaults"],
+ cppflags: [
+ "-D_FILE_OFFSET_BITS=64",
+ ],
+ srcs: [
+ "builder.cpp",
+ "images.cpp",
+ "reader.cpp",
+ "utility.cpp",
+ "writer.cpp",
+ ],
+ shared_libs: [
+ "libbase",
+ "liblog",
+ "libcrypto",
+ "libcrypto_utils",
+ "libsparse",
+ "libext2_uuid",
+ "libext4_utils",
+ "libz",
+ ],
+ export_include_dirs: ["include"],
+}
+
+cc_test {
+ name: "liblp_test",
+ defaults: ["fs_mgr_defaults"],
+ cppflags: [
+ "-Wno-unused-parameter",
+ ],
+ shared_libs: [
+ "liblp",
+ "libbase",
+ "libfs_mgr",
+ ],
+ srcs: [
+ "builder_test.cpp",
+ "io_test.cpp",
+ "utility_test.cpp",
+ ],
+}
diff --git a/fs_mgr/liblp/builder.cpp b/fs_mgr/liblp/builder.cpp
new file mode 100644
index 0000000..2015e4d
--- /dev/null
+++ b/fs_mgr/liblp/builder.cpp
@@ -0,0 +1,530 @@
+/*
+ * Copyright (C) 2018 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 "liblp/builder.h"
+
+#if defined(__linux__)
+#include <linux/fs.h>
+#endif
+#include <string.h>
+#include <sys/ioctl.h>
+
+#include <algorithm>
+
+#include <android-base/unique_fd.h>
+#include <uuid/uuid.h>
+
+#include "liblp/liblp.h"
+#include "reader.h"
+#include "utility.h"
+
+namespace android {
+namespace fs_mgr {
+
+bool GetBlockDeviceInfo(const std::string& block_device, BlockDeviceInfo* device_info) {
+#if defined(__linux__)
+ android::base::unique_fd fd(open(block_device.c_str(), O_RDONLY));
+ if (fd < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "open '" << block_device << "' failed";
+ return false;
+ }
+ if (!GetDescriptorSize(fd, &device_info->size)) {
+ return false;
+ }
+ if (ioctl(fd, BLKIOMIN, &device_info->alignment) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "BLKIOMIN failed";
+ return false;
+ }
+
+ int alignment_offset;
+ if (ioctl(fd, BLKALIGNOFF, &alignment_offset) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "BLKIOMIN failed";
+ return false;
+ }
+ int logical_block_size;
+ if (ioctl(fd, BLKSSZGET, &logical_block_size) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "BLKSSZGET failed";
+ return false;
+ }
+
+ device_info->alignment_offset = static_cast<uint32_t>(alignment_offset);
+ device_info->logical_block_size = static_cast<uint32_t>(logical_block_size);
+ return true;
+#else
+ (void)block_device;
+ (void)device_info;
+ LERROR << __PRETTY_FUNCTION__ << ": Not supported on this operating system.";
+ return false;
+#endif
+}
+
+void LinearExtent::AddTo(LpMetadata* out) const {
+ out->extents.push_back(LpMetadataExtent{num_sectors_, LP_TARGET_TYPE_LINEAR, physical_sector_});
+}
+
+void ZeroExtent::AddTo(LpMetadata* out) const {
+ out->extents.push_back(LpMetadataExtent{num_sectors_, LP_TARGET_TYPE_ZERO, 0});
+}
+
+Partition::Partition(const std::string& name, const std::string& guid, uint32_t attributes)
+ : name_(name), guid_(guid), attributes_(attributes), size_(0) {}
+
+void Partition::AddExtent(std::unique_ptr<Extent>&& extent) {
+ size_ += extent->num_sectors() * LP_SECTOR_SIZE;
+
+ if (LinearExtent* new_extent = extent->AsLinearExtent()) {
+ if (!extents_.empty() && extents_.back()->AsLinearExtent() &&
+ extents_.back()->AsLinearExtent()->end_sector() == new_extent->physical_sector()) {
+ // If the previous extent can be merged into this new one, do so
+ // to avoid creating unnecessary extents.
+ LinearExtent* prev_extent = extents_.back()->AsLinearExtent();
+ extent = std::make_unique<LinearExtent>(
+ prev_extent->num_sectors() + new_extent->num_sectors(),
+ prev_extent->physical_sector());
+ extents_.pop_back();
+ }
+ }
+ extents_.push_back(std::move(extent));
+}
+
+void Partition::RemoveExtents() {
+ size_ = 0;
+ extents_.clear();
+}
+
+void Partition::ShrinkTo(uint64_t aligned_size) {
+ if (aligned_size == 0) {
+ RemoveExtents();
+ return;
+ }
+
+ // Remove or shrink extents of any kind until the total partition size is
+ // equal to the requested size.
+ uint64_t sectors_to_remove = (size_ - aligned_size) / LP_SECTOR_SIZE;
+ while (sectors_to_remove) {
+ Extent* extent = extents_.back().get();
+ if (extent->num_sectors() > sectors_to_remove) {
+ size_ -= sectors_to_remove * LP_SECTOR_SIZE;
+ extent->set_num_sectors(extent->num_sectors() - sectors_to_remove);
+ break;
+ }
+ size_ -= (extent->num_sectors() * LP_SECTOR_SIZE);
+ sectors_to_remove -= extent->num_sectors();
+ extents_.pop_back();
+ }
+ DCHECK(size_ == aligned_size);
+}
+
+std::unique_ptr<MetadataBuilder> MetadataBuilder::New(const std::string& block_device,
+ uint32_t slot_number) {
+ std::unique_ptr<LpMetadata> metadata = ReadMetadata(block_device.c_str(), slot_number);
+ if (!metadata) {
+ return nullptr;
+ }
+ std::unique_ptr<MetadataBuilder> builder = New(*metadata.get());
+ if (!builder) {
+ return nullptr;
+ }
+ BlockDeviceInfo device_info;
+ if (fs_mgr::GetBlockDeviceInfo(block_device, &device_info)) {
+ builder->set_block_device_info(device_info);
+ }
+ return builder;
+}
+
+std::unique_ptr<MetadataBuilder> MetadataBuilder::New(const BlockDeviceInfo& device_info,
+ uint32_t metadata_max_size,
+ uint32_t metadata_slot_count) {
+ std::unique_ptr<MetadataBuilder> builder(new MetadataBuilder());
+ if (!builder->Init(device_info, metadata_max_size, metadata_slot_count)) {
+ return nullptr;
+ }
+ return builder;
+}
+
+std::unique_ptr<MetadataBuilder> MetadataBuilder::New(const LpMetadata& metadata) {
+ std::unique_ptr<MetadataBuilder> builder(new MetadataBuilder());
+ if (!builder->Init(metadata)) {
+ return nullptr;
+ }
+ return builder;
+}
+
+MetadataBuilder::MetadataBuilder() {
+ memset(&geometry_, 0, sizeof(geometry_));
+ geometry_.magic = LP_METADATA_GEOMETRY_MAGIC;
+ geometry_.struct_size = sizeof(geometry_);
+
+ memset(&header_, 0, sizeof(header_));
+ header_.magic = LP_METADATA_HEADER_MAGIC;
+ header_.major_version = LP_METADATA_MAJOR_VERSION;
+ header_.minor_version = LP_METADATA_MINOR_VERSION;
+ header_.header_size = sizeof(header_);
+ header_.partitions.entry_size = sizeof(LpMetadataPartition);
+ header_.extents.entry_size = sizeof(LpMetadataExtent);
+}
+
+bool MetadataBuilder::Init(const LpMetadata& metadata) {
+ geometry_ = metadata.geometry;
+
+ for (const auto& partition : metadata.partitions) {
+ Partition* builder = AddPartition(GetPartitionName(partition), GetPartitionGuid(partition),
+ partition.attributes);
+ if (!builder) {
+ return false;
+ }
+
+ for (size_t i = 0; i < partition.num_extents; i++) {
+ const LpMetadataExtent& extent = metadata.extents[partition.first_extent_index + i];
+ if (extent.target_type == LP_TARGET_TYPE_LINEAR) {
+ auto copy = std::make_unique<LinearExtent>(extent.num_sectors, extent.target_data);
+ builder->AddExtent(std::move(copy));
+ } else if (extent.target_type == LP_TARGET_TYPE_ZERO) {
+ auto copy = std::make_unique<ZeroExtent>(extent.num_sectors);
+ builder->AddExtent(std::move(copy));
+ }
+ }
+ }
+
+ device_info_.alignment = geometry_.alignment;
+ device_info_.alignment_offset = geometry_.alignment_offset;
+ device_info_.logical_block_size = geometry_.logical_block_size;
+ return true;
+}
+
+bool MetadataBuilder::Init(const BlockDeviceInfo& device_info, uint32_t metadata_max_size,
+ uint32_t metadata_slot_count) {
+ if (metadata_max_size < sizeof(LpMetadataHeader)) {
+ LERROR << "Invalid metadata maximum size.";
+ return false;
+ }
+ if (metadata_slot_count == 0) {
+ LERROR << "Invalid metadata slot count.";
+ return false;
+ }
+
+ // Align the metadata size up to the nearest sector.
+ metadata_max_size = AlignTo(metadata_max_size, LP_SECTOR_SIZE);
+
+ // Check that device properties are sane.
+ device_info_ = device_info;
+ if (device_info_.size % LP_SECTOR_SIZE != 0) {
+ LERROR << "Block device size must be a multiple of 512.";
+ return false;
+ }
+ if (device_info_.logical_block_size % LP_SECTOR_SIZE != 0) {
+ LERROR << "Logical block size must be a multiple of 512.";
+ return false;
+ }
+ if (device_info_.alignment_offset % LP_SECTOR_SIZE != 0) {
+ LERROR << "Alignment offset is not sector-aligned.";
+ return false;
+ }
+ if (device_info_.alignment % LP_SECTOR_SIZE != 0) {
+ LERROR << "Partition alignment is not sector-aligned.";
+ return false;
+ }
+ if (device_info_.alignment_offset > device_info_.alignment) {
+ LERROR << "Partition alignment offset is greater than its alignment.";
+ return false;
+ }
+
+ // We reserve a geometry block (4KB) plus space for each copy of the
+ // maximum size of a metadata blob. Then, we double that space since
+ // we store a backup copy of everything.
+ uint64_t reserved =
+ LP_METADATA_GEOMETRY_SIZE + (uint64_t(metadata_max_size) * metadata_slot_count);
+ uint64_t total_reserved = reserved * 2;
+ if (device_info_.size < total_reserved) {
+ LERROR << "Attempting to create metadata on a block device that is too small.";
+ return false;
+ }
+
+ // Compute the first free sector, factoring in alignment.
+ uint64_t free_area = AlignTo(reserved, device_info_.alignment, device_info_.alignment_offset);
+ uint64_t first_sector = free_area / LP_SECTOR_SIZE;
+
+ // Compute the last free sector, which is inclusive. We subtract 1 to make
+ // sure that logical partitions won't overlap with the same sector as the
+ // backup metadata, which could happen if the block device was not aligned
+ // to LP_SECTOR_SIZE.
+ uint64_t last_sector = ((device_info_.size - reserved) / LP_SECTOR_SIZE) - 1;
+
+ // If this check fails, it means either (1) we did not have free space to
+ // allocate a single sector, or (2) we did, but the alignment was high
+ // enough to bump the first sector out of range. Either way, we cannot
+ // continue.
+ if (first_sector > last_sector) {
+ LERROR << "Not enough space to allocate any partition tables.";
+ return false;
+ }
+
+ // Finally, the size of the allocatable space must be a multiple of the
+ // logical block size. If we have no more free space after this
+ // computation, then we abort. Note that the last sector is inclusive,
+ // so we have to account for that.
+ uint64_t num_free_sectors = last_sector - first_sector + 1;
+ uint64_t sectors_per_block = device_info_.logical_block_size / LP_SECTOR_SIZE;
+ if (num_free_sectors < sectors_per_block) {
+ LERROR << "Not enough space to allocate any partition tables.";
+ return false;
+ }
+ last_sector = first_sector + (num_free_sectors / sectors_per_block) * sectors_per_block - 1;
+
+ geometry_.first_logical_sector = first_sector;
+ geometry_.last_logical_sector = last_sector;
+ geometry_.metadata_max_size = metadata_max_size;
+ geometry_.metadata_slot_count = metadata_slot_count;
+ geometry_.alignment = device_info_.alignment;
+ geometry_.alignment_offset = device_info_.alignment_offset;
+ geometry_.block_device_size = device_info_.size;
+ geometry_.logical_block_size = device_info.logical_block_size;
+ return true;
+}
+
+Partition* MetadataBuilder::AddPartition(const std::string& name, const std::string& guid,
+ uint32_t attributes) {
+ if (name.empty()) {
+ LERROR << "Partition must have a non-empty name.";
+ return nullptr;
+ }
+ if (FindPartition(name)) {
+ LERROR << "Attempting to create duplication partition with name: " << name;
+ return nullptr;
+ }
+ partitions_.push_back(std::make_unique<Partition>(name, guid, attributes));
+ return partitions_.back().get();
+}
+
+Partition* MetadataBuilder::FindPartition(const std::string& name) {
+ for (const auto& partition : partitions_) {
+ if (partition->name() == name) {
+ return partition.get();
+ }
+ }
+ return nullptr;
+}
+
+void MetadataBuilder::RemovePartition(const std::string& name) {
+ for (auto iter = partitions_.begin(); iter != partitions_.end(); iter++) {
+ if ((*iter)->name() == name) {
+ partitions_.erase(iter);
+ return;
+ }
+ }
+}
+
+bool MetadataBuilder::GrowPartition(Partition* partition, uint64_t aligned_size) {
+ // Figure out how much we need to allocate.
+ uint64_t space_needed = aligned_size - partition->size();
+ uint64_t sectors_needed = space_needed / LP_SECTOR_SIZE;
+ DCHECK(sectors_needed * LP_SECTOR_SIZE == space_needed);
+
+ struct Interval {
+ uint64_t start;
+ uint64_t end;
+
+ Interval(uint64_t start, uint64_t end) : start(start), end(end) {}
+ uint64_t length() const { return end - start; }
+ bool operator<(const Interval& other) const { return start < other.start; }
+ };
+
+ // Collect all extents in the partition table, then sort them by starting
+ // sector.
+ std::vector<Interval> extents;
+ for (const auto& partition : partitions_) {
+ for (const auto& extent : partition->extents()) {
+ LinearExtent* linear = extent->AsLinearExtent();
+ if (!linear) {
+ continue;
+ }
+ extents.emplace_back(linear->physical_sector(),
+ linear->physical_sector() + extent->num_sectors());
+ }
+ }
+ std::sort(extents.begin(), extents.end());
+
+ // Convert the extent list into a list of gaps between the extents; i.e.,
+ // the list of ranges that are free on the disk.
+ std::vector<Interval> free_regions;
+ for (size_t i = 1; i < extents.size(); i++) {
+ const Interval& previous = extents[i - 1];
+ const Interval& current = extents[i];
+
+ uint64_t aligned = AlignSector(previous.end);
+ if (aligned >= current.start) {
+ // There is no gap between these two extents, try the next one.
+ // Note that we check with >= instead of >, since alignment may
+ // bump the ending sector past the beginning of the next extent.
+ continue;
+ }
+
+ // The new interval represents the free space starting at the end of
+ // the previous interval, and ending at the start of the next interval.
+ free_regions.emplace_back(aligned, current.start);
+ }
+
+ // Add a final interval representing the remainder of the free space.
+ uint64_t last_free_extent_start =
+ extents.empty() ? geometry_.first_logical_sector : extents.back().end;
+ last_free_extent_start = AlignSector(last_free_extent_start);
+ if (last_free_extent_start <= geometry_.last_logical_sector) {
+ free_regions.emplace_back(last_free_extent_start, geometry_.last_logical_sector + 1);
+ }
+
+ const uint64_t sectors_per_block = device_info_.logical_block_size / LP_SECTOR_SIZE;
+ CHECK(sectors_needed % sectors_per_block == 0);
+
+ // Find gaps that we can use for new extents. Note we store new extents in a
+ // temporary vector, and only commit them if we are guaranteed enough free
+ // space.
+ std::vector<std::unique_ptr<LinearExtent>> new_extents;
+ for (auto& region : free_regions) {
+ if (region.length() % sectors_per_block != 0) {
+ // This should never happen, because it would imply that we
+ // once allocated an extent that was not a multiple of the
+ // block size. That extent would be rejected by DM_TABLE_LOAD.
+ LERROR << "Region " << region.start << ".." << region.end
+ << " is not a multiple of the block size, " << sectors_per_block;
+
+ // If for some reason the final region is mis-sized we still want
+ // to be able to grow partitions. So just to be safe, round the
+ // region down to the nearest block.
+ region.end = region.start + (region.length() / sectors_per_block) * sectors_per_block;
+ if (!region.length()) {
+ continue;
+ }
+ }
+
+ uint64_t sectors = std::min(sectors_needed, region.length());
+ CHECK(sectors % sectors_per_block == 0);
+
+ auto extent = std::make_unique<LinearExtent>(sectors, region.start);
+ new_extents.push_back(std::move(extent));
+ sectors_needed -= sectors;
+ if (!sectors_needed) {
+ break;
+ }
+ }
+ if (sectors_needed) {
+ LERROR << "Not enough free space to expand partition: " << partition->name();
+ return false;
+ }
+
+ // Everything succeeded, so commit the new extents.
+ for (auto& extent : new_extents) {
+ partition->AddExtent(std::move(extent));
+ }
+ return true;
+}
+
+void MetadataBuilder::ShrinkPartition(Partition* partition, uint64_t aligned_size) {
+ partition->ShrinkTo(aligned_size);
+}
+
+std::unique_ptr<LpMetadata> MetadataBuilder::Export() {
+ std::unique_ptr<LpMetadata> metadata = std::make_unique<LpMetadata>();
+ metadata->header = header_;
+ metadata->geometry = geometry_;
+
+ // Flatten the partition and extent structures into an LpMetadata, which
+ // makes it very easy to validate, serialize, or pass on to device-mapper.
+ for (const auto& partition : partitions_) {
+ LpMetadataPartition part;
+ memset(&part, 0, sizeof(part));
+
+ if (partition->name().size() > sizeof(part.name)) {
+ LERROR << "Partition name is too long: " << partition->name();
+ return nullptr;
+ }
+ if (partition->attributes() & ~(LP_PARTITION_ATTRIBUTE_MASK)) {
+ LERROR << "Partition " << partition->name() << " has unsupported attribute.";
+ return nullptr;
+ }
+
+ strncpy(part.name, partition->name().c_str(), sizeof(part.name));
+ if (uuid_parse(partition->guid().c_str(), part.guid) != 0) {
+ LERROR << "Could not parse guid " << partition->guid() << " for partition "
+ << partition->name();
+ return nullptr;
+ }
+
+ part.first_extent_index = static_cast<uint32_t>(metadata->extents.size());
+ part.num_extents = static_cast<uint32_t>(partition->extents().size());
+ part.attributes = partition->attributes();
+
+ for (const auto& extent : partition->extents()) {
+ extent->AddTo(metadata.get());
+ }
+ metadata->partitions.push_back(part);
+ }
+
+ metadata->header.partitions.num_entries = static_cast<uint32_t>(metadata->partitions.size());
+ metadata->header.extents.num_entries = static_cast<uint32_t>(metadata->extents.size());
+ return metadata;
+}
+
+uint64_t MetadataBuilder::AllocatableSpace() const {
+ return (geometry_.last_logical_sector - geometry_.first_logical_sector + 1) * LP_SECTOR_SIZE;
+}
+
+uint64_t MetadataBuilder::AlignSector(uint64_t sector) {
+ // Note: when reading alignment info from the Kernel, we don't assume it
+ // is aligned to the sector size, so we round up to the nearest sector.
+ uint64_t lba = sector * LP_SECTOR_SIZE;
+ uint64_t aligned = AlignTo(lba, device_info_.alignment, device_info_.alignment_offset);
+ return AlignTo(aligned, LP_SECTOR_SIZE) / LP_SECTOR_SIZE;
+}
+
+void MetadataBuilder::set_block_device_info(const BlockDeviceInfo& device_info) {
+ device_info_.size = device_info.size;
+
+ // Note that if the logical block size changes, we're probably in trouble:
+ // we could have already built extents that will only work on the previous
+ // size.
+ DCHECK(partitions_.empty() ||
+ device_info_.logical_block_size == device_info.logical_block_size);
+
+ // The kernel does not guarantee these values are present, so we only
+ // replace existing values if the new values are non-zero.
+ if (device_info.alignment) {
+ device_info_.alignment = device_info.alignment;
+ }
+ if (device_info.alignment_offset) {
+ device_info_.alignment_offset = device_info.alignment_offset;
+ }
+}
+
+bool MetadataBuilder::ResizePartition(Partition* partition, uint64_t requested_size) {
+ // Align the space needed up to the nearest sector.
+ uint64_t aligned_size = AlignTo(requested_size, device_info_.logical_block_size);
+ uint64_t old_size = partition->size();
+
+ if (aligned_size > old_size) {
+ if (!GrowPartition(partition, aligned_size)) {
+ return false;
+ }
+ } else if (aligned_size < partition->size()) {
+ ShrinkPartition(partition, aligned_size);
+ }
+
+ LINFO << "Partition " << partition->name() << " will resize from " << old_size << " bytes to "
+ << aligned_size << " bytes";
+ return true;
+}
+
+} // namespace fs_mgr
+} // namespace android
diff --git a/fs_mgr/liblp/builder_test.cpp b/fs_mgr/liblp/builder_test.cpp
new file mode 100644
index 0000000..da9c8f3
--- /dev/null
+++ b/fs_mgr/liblp/builder_test.cpp
@@ -0,0 +1,483 @@
+/*
+ * Copyright (C) 2018 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 <gtest/gtest.h>
+#include <liblp/builder.h>
+#include "fs_mgr.h"
+#include "utility.h"
+
+using namespace std;
+using namespace android::fs_mgr;
+
+static const char* TEST_GUID = "A799D1D6-669F-41D8-A3F0-EBB7572D8302";
+static const char* TEST_GUID2 = "A799D1D6-669F-41D8-A3F0-EBB7572D8303";
+
+TEST(liblp, BuildBasic) {
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
+
+ Partition* partition = builder->AddPartition("system", TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ ASSERT_NE(partition, nullptr);
+ EXPECT_EQ(partition->name(), "system");
+ EXPECT_EQ(partition->guid(), TEST_GUID);
+ EXPECT_EQ(partition->attributes(), LP_PARTITION_ATTR_READONLY);
+ EXPECT_EQ(partition->size(), 0);
+ EXPECT_EQ(builder->FindPartition("system"), partition);
+
+ builder->RemovePartition("system");
+ EXPECT_EQ(builder->FindPartition("system"), nullptr);
+}
+
+TEST(liblp, ResizePartition) {
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
+
+ Partition* system = builder->AddPartition("system", TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ ASSERT_NE(system, nullptr);
+ EXPECT_EQ(builder->ResizePartition(system, 65536), true);
+ EXPECT_EQ(system->size(), 65536);
+ ASSERT_EQ(system->extents().size(), 1);
+
+ LinearExtent* extent = system->extents()[0]->AsLinearExtent();
+ ASSERT_NE(extent, nullptr);
+ EXPECT_EQ(extent->num_sectors(), 65536 / LP_SECTOR_SIZE);
+ // The first logical sector will be (4096+1024*2)/512 = 12.
+ EXPECT_EQ(extent->physical_sector(), 12);
+
+ // Test resizing to the same size.
+ EXPECT_EQ(builder->ResizePartition(system, 65536), true);
+ EXPECT_EQ(system->size(), 65536);
+ EXPECT_EQ(system->extents().size(), 1);
+ EXPECT_EQ(system->extents()[0]->num_sectors(), 65536 / LP_SECTOR_SIZE);
+ // Test resizing to a smaller size.
+ EXPECT_EQ(builder->ResizePartition(system, 0), true);
+ EXPECT_EQ(system->size(), 0);
+ EXPECT_EQ(system->extents().size(), 0);
+ // Test resizing to a greater size.
+ builder->ResizePartition(system, 131072);
+ EXPECT_EQ(system->size(), 131072);
+ EXPECT_EQ(system->extents().size(), 1);
+ EXPECT_EQ(system->extents()[0]->num_sectors(), 131072 / LP_SECTOR_SIZE);
+ // Test resizing again, that the extents are merged together.
+ builder->ResizePartition(system, 1024 * 256);
+ EXPECT_EQ(system->size(), 1024 * 256);
+ EXPECT_EQ(system->extents().size(), 1);
+ EXPECT_EQ(system->extents()[0]->num_sectors(), (1024 * 256) / LP_SECTOR_SIZE);
+
+ // Test shrinking within the same extent.
+ builder->ResizePartition(system, 32768);
+ EXPECT_EQ(system->size(), 32768);
+ EXPECT_EQ(system->extents().size(), 1);
+ extent = system->extents()[0]->AsLinearExtent();
+ ASSERT_NE(extent, nullptr);
+ EXPECT_EQ(extent->num_sectors(), 32768 / LP_SECTOR_SIZE);
+ EXPECT_EQ(extent->physical_sector(), 12);
+
+ // Test shrinking to 0.
+ builder->ResizePartition(system, 0);
+ EXPECT_EQ(system->size(), 0);
+ EXPECT_EQ(system->extents().size(), 0);
+}
+
+TEST(liblp, PartitionAlignment) {
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
+
+ // Test that we align up to one sector.
+ Partition* system = builder->AddPartition("system", TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ ASSERT_NE(system, nullptr);
+ EXPECT_EQ(builder->ResizePartition(system, 10000), true);
+ EXPECT_EQ(system->size(), 12288);
+ EXPECT_EQ(system->extents().size(), 1);
+
+ builder->ResizePartition(system, 7000);
+ EXPECT_EQ(system->size(), 8192);
+ EXPECT_EQ(system->extents().size(), 1);
+}
+
+TEST(liblp, DiskAlignment) {
+ static const uint64_t kDiskSize = 1000000;
+ static const uint32_t kMetadataSize = 1024;
+ static const uint32_t kMetadataSlots = 2;
+
+ unique_ptr<MetadataBuilder> builder =
+ MetadataBuilder::New(kDiskSize, kMetadataSize, kMetadataSlots);
+ ASSERT_EQ(builder, nullptr);
+}
+
+TEST(liblp, MetadataAlignment) {
+ // Make sure metadata sizes get aligned up.
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1000, 2);
+ unique_ptr<LpMetadata> exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+ EXPECT_EQ(exported->geometry.metadata_max_size, 1024);
+}
+
+TEST(liblp, InternalAlignment) {
+ // Test the metadata fitting within alignment.
+ BlockDeviceInfo device_info(1024 * 1024, 768 * 1024, 0, 4096);
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(device_info, 1024, 2);
+ ASSERT_NE(builder, nullptr);
+ unique_ptr<LpMetadata> exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+ EXPECT_EQ(exported->geometry.first_logical_sector, 1536);
+ EXPECT_EQ(exported->geometry.last_logical_sector, 2031);
+
+ // Test a large alignment offset thrown in.
+ device_info.alignment_offset = 753664;
+ builder = MetadataBuilder::New(device_info, 1024, 2);
+ ASSERT_NE(builder, nullptr);
+ exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+ EXPECT_EQ(exported->geometry.first_logical_sector, 1472);
+ EXPECT_EQ(exported->geometry.last_logical_sector, 2031);
+
+ // Alignment offset without alignment doesn't mean anything.
+ device_info.alignment = 0;
+ builder = MetadataBuilder::New(device_info, 1024, 2);
+ ASSERT_EQ(builder, nullptr);
+
+ // Test a small alignment with an alignment offset.
+ device_info.alignment = 12 * 1024;
+ device_info.alignment_offset = 3 * 1024;
+ builder = MetadataBuilder::New(device_info, 16 * 1024, 2);
+ ASSERT_NE(builder, nullptr);
+ exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+ EXPECT_EQ(exported->geometry.first_logical_sector, 78);
+ EXPECT_EQ(exported->geometry.last_logical_sector, 1973);
+
+ // Test a small alignment with no alignment offset.
+ device_info.alignment = 11 * 1024;
+ builder = MetadataBuilder::New(device_info, 16 * 1024, 2);
+ ASSERT_NE(builder, nullptr);
+ exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+ EXPECT_EQ(exported->geometry.first_logical_sector, 72);
+ EXPECT_EQ(exported->geometry.last_logical_sector, 1975);
+}
+
+TEST(liblp, InternalPartitionAlignment) {
+ BlockDeviceInfo device_info(512 * 1024 * 1024, 768 * 1024, 753664, 4096);
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(device_info, 32 * 1024, 2);
+
+ Partition* a = builder->AddPartition("a", TEST_GUID, 0);
+ ASSERT_NE(a, nullptr);
+ Partition* b = builder->AddPartition("b", TEST_GUID2, 0);
+ ASSERT_NE(b, nullptr);
+
+ // Add a bunch of small extents to each, interleaving.
+ for (size_t i = 0; i < 10; i++) {
+ ASSERT_TRUE(builder->ResizePartition(a, a->size() + 4096));
+ ASSERT_TRUE(builder->ResizePartition(b, b->size() + 4096));
+ }
+ EXPECT_EQ(a->size(), 40960);
+ EXPECT_EQ(b->size(), 40960);
+
+ unique_ptr<LpMetadata> exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+
+ // Check that each starting sector is aligned.
+ for (const auto& extent : exported->extents) {
+ ASSERT_EQ(extent.target_type, LP_TARGET_TYPE_LINEAR);
+ EXPECT_EQ(extent.num_sectors, 8);
+
+ uint64_t lba = extent.target_data * LP_SECTOR_SIZE;
+ uint64_t aligned_lba = AlignTo(lba, device_info.alignment, device_info.alignment_offset);
+ EXPECT_EQ(lba, aligned_lba);
+ }
+
+ // Sanity check one extent.
+ EXPECT_EQ(exported->extents.back().target_data, 30656);
+}
+
+TEST(liblp, UseAllDiskSpace) {
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
+ EXPECT_EQ(builder->AllocatableSpace(), 1036288);
+
+ Partition* system = builder->AddPartition("system", TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ ASSERT_NE(system, nullptr);
+ EXPECT_EQ(builder->ResizePartition(system, 1036288), true);
+ EXPECT_EQ(system->size(), 1036288);
+ EXPECT_EQ(builder->ResizePartition(system, 1036289), false);
+}
+
+TEST(liblp, BuildComplex) {
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
+
+ Partition* system = builder->AddPartition("system", TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ Partition* vendor = builder->AddPartition("vendor", TEST_GUID2, LP_PARTITION_ATTR_READONLY);
+ ASSERT_NE(system, nullptr);
+ ASSERT_NE(vendor, nullptr);
+ EXPECT_EQ(builder->ResizePartition(system, 65536), true);
+ EXPECT_EQ(builder->ResizePartition(vendor, 32768), true);
+ EXPECT_EQ(builder->ResizePartition(system, 98304), true);
+ EXPECT_EQ(system->size(), 98304);
+ EXPECT_EQ(vendor->size(), 32768);
+
+ // We now expect to have 3 extents total: 2 for system, 1 for vendor, since
+ // our allocation strategy is greedy/first-fit.
+ ASSERT_EQ(system->extents().size(), 2);
+ ASSERT_EQ(vendor->extents().size(), 1);
+
+ LinearExtent* system1 = system->extents()[0]->AsLinearExtent();
+ LinearExtent* system2 = system->extents()[1]->AsLinearExtent();
+ LinearExtent* vendor1 = vendor->extents()[0]->AsLinearExtent();
+ ASSERT_NE(system1, nullptr);
+ ASSERT_NE(system2, nullptr);
+ ASSERT_NE(vendor1, nullptr);
+ EXPECT_EQ(system1->num_sectors(), 65536 / LP_SECTOR_SIZE);
+ EXPECT_EQ(system1->physical_sector(), 12);
+ EXPECT_EQ(system2->num_sectors(), 32768 / LP_SECTOR_SIZE);
+ EXPECT_EQ(system2->physical_sector(), 204);
+ EXPECT_EQ(vendor1->num_sectors(), 32768 / LP_SECTOR_SIZE);
+ EXPECT_EQ(vendor1->physical_sector(), 140);
+ EXPECT_EQ(system1->physical_sector() + system1->num_sectors(), vendor1->physical_sector());
+ EXPECT_EQ(vendor1->physical_sector() + vendor1->num_sectors(), system2->physical_sector());
+}
+
+TEST(liblp, AddInvalidPartition) {
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
+
+ Partition* partition = builder->AddPartition("system", TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ ASSERT_NE(partition, nullptr);
+
+ // Duplicate name.
+ partition = builder->AddPartition("system", TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ EXPECT_EQ(partition, nullptr);
+
+ // Empty name.
+ partition = builder->AddPartition("", TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ EXPECT_EQ(partition, nullptr);
+}
+
+TEST(liblp, BuilderExport) {
+ static const uint64_t kDiskSize = 1024 * 1024;
+ static const uint32_t kMetadataSize = 1024;
+ static const uint32_t kMetadataSlots = 2;
+ unique_ptr<MetadataBuilder> builder =
+ MetadataBuilder::New(kDiskSize, kMetadataSize, kMetadataSlots);
+
+ Partition* system = builder->AddPartition("system", TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ Partition* vendor = builder->AddPartition("vendor", TEST_GUID2, LP_PARTITION_ATTR_READONLY);
+ ASSERT_NE(system, nullptr);
+ ASSERT_NE(vendor, nullptr);
+ EXPECT_EQ(builder->ResizePartition(system, 65536), true);
+ EXPECT_EQ(builder->ResizePartition(vendor, 32768), true);
+ EXPECT_EQ(builder->ResizePartition(system, 98304), true);
+
+ unique_ptr<LpMetadata> exported = builder->Export();
+ EXPECT_NE(exported, nullptr);
+
+ // Verify geometry. Some details of this may change if we change the
+ // metadata structures. So in addition to checking the exact values, we
+ // also check that they are internally consistent after.
+ const LpMetadataGeometry& geometry = exported->geometry;
+ EXPECT_EQ(geometry.magic, LP_METADATA_GEOMETRY_MAGIC);
+ EXPECT_EQ(geometry.struct_size, sizeof(geometry));
+ EXPECT_EQ(geometry.metadata_max_size, 1024);
+ EXPECT_EQ(geometry.metadata_slot_count, 2);
+ EXPECT_EQ(geometry.first_logical_sector, 12);
+ EXPECT_EQ(geometry.last_logical_sector, 2035);
+
+ static const size_t kMetadataSpace =
+ (kMetadataSize * kMetadataSlots) + LP_METADATA_GEOMETRY_SIZE;
+ uint64_t space_at_end = kDiskSize - (geometry.last_logical_sector + 1) * LP_SECTOR_SIZE;
+ EXPECT_GE(space_at_end, kMetadataSpace);
+ EXPECT_GE(geometry.first_logical_sector * LP_SECTOR_SIZE, kMetadataSpace);
+
+ // Verify header.
+ const LpMetadataHeader& header = exported->header;
+ EXPECT_EQ(header.magic, LP_METADATA_HEADER_MAGIC);
+ EXPECT_EQ(header.major_version, LP_METADATA_MAJOR_VERSION);
+ EXPECT_EQ(header.minor_version, LP_METADATA_MINOR_VERSION);
+
+ ASSERT_EQ(exported->partitions.size(), 2);
+ ASSERT_EQ(exported->extents.size(), 3);
+
+ for (const auto& partition : exported->partitions) {
+ Partition* original = builder->FindPartition(GetPartitionName(partition));
+ ASSERT_NE(original, nullptr);
+ EXPECT_EQ(original->guid(), GetPartitionGuid(partition));
+ for (size_t i = 0; i < partition.num_extents; i++) {
+ const auto& extent = exported->extents[partition.first_extent_index + i];
+ LinearExtent* original_extent = original->extents()[i]->AsLinearExtent();
+ EXPECT_EQ(extent.num_sectors, original_extent->num_sectors());
+ EXPECT_EQ(extent.target_type, LP_TARGET_TYPE_LINEAR);
+ EXPECT_EQ(extent.target_data, original_extent->physical_sector());
+ }
+ EXPECT_EQ(partition.attributes, original->attributes());
+ }
+}
+
+TEST(liblp, BuilderImport) {
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
+
+ Partition* system = builder->AddPartition("system", TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ Partition* vendor = builder->AddPartition("vendor", TEST_GUID2, LP_PARTITION_ATTR_READONLY);
+ ASSERT_NE(system, nullptr);
+ ASSERT_NE(vendor, nullptr);
+ EXPECT_EQ(builder->ResizePartition(system, 65536), true);
+ EXPECT_EQ(builder->ResizePartition(vendor, 32768), true);
+ EXPECT_EQ(builder->ResizePartition(system, 98304), true);
+
+ unique_ptr<LpMetadata> exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+
+ builder = MetadataBuilder::New(*exported.get());
+ ASSERT_NE(builder, nullptr);
+ system = builder->FindPartition("system");
+ ASSERT_NE(system, nullptr);
+ vendor = builder->FindPartition("vendor");
+ ASSERT_NE(vendor, nullptr);
+
+ EXPECT_EQ(system->size(), 98304);
+ ASSERT_EQ(system->extents().size(), 2);
+ EXPECT_EQ(system->guid(), TEST_GUID);
+ EXPECT_EQ(system->attributes(), LP_PARTITION_ATTR_READONLY);
+ EXPECT_EQ(vendor->size(), 32768);
+ ASSERT_EQ(vendor->extents().size(), 1);
+ EXPECT_EQ(vendor->guid(), TEST_GUID2);
+ EXPECT_EQ(vendor->attributes(), LP_PARTITION_ATTR_READONLY);
+
+ LinearExtent* system1 = system->extents()[0]->AsLinearExtent();
+ LinearExtent* system2 = system->extents()[1]->AsLinearExtent();
+ LinearExtent* vendor1 = vendor->extents()[0]->AsLinearExtent();
+ EXPECT_EQ(system1->num_sectors(), 65536 / LP_SECTOR_SIZE);
+ EXPECT_EQ(system1->physical_sector(), 12);
+ EXPECT_EQ(system2->num_sectors(), 32768 / LP_SECTOR_SIZE);
+ EXPECT_EQ(system2->physical_sector(), 204);
+ EXPECT_EQ(vendor1->num_sectors(), 32768 / LP_SECTOR_SIZE);
+}
+
+TEST(liblp, ExportNameTooLong) {
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
+
+ std::string name = "abcdefghijklmnopqrstuvwxyz0123456789";
+ Partition* system = builder->AddPartition(name + name, TEST_GUID, LP_PARTITION_ATTR_READONLY);
+ EXPECT_NE(system, nullptr);
+
+ unique_ptr<LpMetadata> exported = builder->Export();
+ EXPECT_EQ(exported, nullptr);
+}
+
+TEST(liblp, ExportInvalidGuid) {
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
+
+ Partition* system = builder->AddPartition("system", "bad", LP_PARTITION_ATTR_READONLY);
+ EXPECT_NE(system, nullptr);
+
+ unique_ptr<LpMetadata> exported = builder->Export();
+ EXPECT_EQ(exported, nullptr);
+}
+
+TEST(liblp, MetadataTooLarge) {
+ static const size_t kDiskSize = 128 * 1024;
+ static const size_t kMetadataSize = 64 * 1024;
+
+ // No space to store metadata + geometry.
+ BlockDeviceInfo device_info(kDiskSize, 0, 0, 4096);
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(device_info, kMetadataSize, 1);
+ EXPECT_EQ(builder, nullptr);
+
+ // No space to store metadata + geometry + one free sector.
+ device_info.size += LP_METADATA_GEOMETRY_SIZE * 2;
+ builder = MetadataBuilder::New(device_info, kMetadataSize, 1);
+ EXPECT_EQ(builder, nullptr);
+
+ // Space for metadata + geometry + one free block.
+ device_info.size += device_info.logical_block_size;
+ builder = MetadataBuilder::New(device_info, kMetadataSize, 1);
+ EXPECT_NE(builder, nullptr);
+
+ // Test with alignment.
+ device_info.alignment = 131072;
+ builder = MetadataBuilder::New(device_info, kMetadataSize, 1);
+ EXPECT_EQ(builder, nullptr);
+
+ device_info.alignment = 0;
+ device_info.alignment_offset = 32768 - LP_SECTOR_SIZE;
+ builder = MetadataBuilder::New(device_info, kMetadataSize, 1);
+ EXPECT_EQ(builder, nullptr);
+}
+
+TEST(liblp, block_device_info) {
+ std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
+ fs_mgr_free_fstab);
+ ASSERT_NE(fstab, nullptr);
+
+ // This should read from the "super" partition once we have a well-defined
+ // way to access it.
+ struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab.get(), "/data");
+ ASSERT_NE(rec, nullptr);
+
+ BlockDeviceInfo device_info;
+ ASSERT_TRUE(GetBlockDeviceInfo(rec->blk_device, &device_info));
+
+ // Sanity check that the device doesn't give us some weird inefficient
+ // alignment.
+ ASSERT_EQ(device_info.alignment % LP_SECTOR_SIZE, 0);
+ ASSERT_EQ(device_info.alignment_offset % LP_SECTOR_SIZE, 0);
+ ASSERT_LE(device_info.alignment_offset, INT_MAX);
+ ASSERT_EQ(device_info.logical_block_size % LP_SECTOR_SIZE, 0);
+
+ // Having an alignment offset > alignment doesn't really make sense.
+ ASSERT_LT(device_info.alignment_offset, device_info.alignment);
+}
+
+TEST(liblp, UpdateBlockDeviceInfo) {
+ BlockDeviceInfo device_info(1024 * 1024, 4096, 1024, 4096);
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(device_info, 1024, 1);
+ ASSERT_NE(builder, nullptr);
+
+ EXPECT_EQ(builder->block_device_info().size, device_info.size);
+ EXPECT_EQ(builder->block_device_info().alignment, device_info.alignment);
+ EXPECT_EQ(builder->block_device_info().alignment_offset, device_info.alignment_offset);
+ EXPECT_EQ(builder->block_device_info().logical_block_size, device_info.logical_block_size);
+
+ device_info.alignment = 0;
+ device_info.alignment_offset = 2048;
+ builder->set_block_device_info(device_info);
+ EXPECT_EQ(builder->block_device_info().alignment, 4096);
+ EXPECT_EQ(builder->block_device_info().alignment_offset, device_info.alignment_offset);
+
+ device_info.alignment = 8192;
+ device_info.alignment_offset = 0;
+ builder->set_block_device_info(device_info);
+ EXPECT_EQ(builder->block_device_info().alignment, 8192);
+ EXPECT_EQ(builder->block_device_info().alignment_offset, 2048);
+}
+
+TEST(liblp, InvalidBlockSize) {
+ BlockDeviceInfo device_info(1024 * 1024, 0, 0, 513);
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(device_info, 1024, 1);
+ EXPECT_EQ(builder, nullptr);
+}
+
+TEST(liblp, AlignedExtentSize) {
+ BlockDeviceInfo device_info(1024 * 1024, 0, 0, 4096);
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(device_info, 1024, 1);
+ ASSERT_NE(builder, nullptr);
+
+ Partition* partition = builder->AddPartition("system", TEST_GUID, 0);
+ ASSERT_NE(partition, nullptr);
+ ASSERT_TRUE(builder->ResizePartition(partition, 512));
+ EXPECT_EQ(partition->size(), 4096);
+}
+
+TEST(liblp, AlignedFreeSpace) {
+ // Only one sector free - at least one block is required.
+ BlockDeviceInfo device_info(10240, 0, 0, 4096);
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(device_info, 512, 1);
+ ASSERT_EQ(builder, nullptr);
+}
diff --git a/fs_mgr/liblp/images.cpp b/fs_mgr/liblp/images.cpp
new file mode 100644
index 0000000..a361a5d
--- /dev/null
+++ b/fs_mgr/liblp/images.cpp
@@ -0,0 +1,355 @@
+/*
+ * Copyright (C) 2018 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 "images.h"
+
+#include <limits.h>
+
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+#include <sparse/sparse.h>
+
+#include "reader.h"
+#include "utility.h"
+#include "writer.h"
+
+namespace android {
+namespace fs_mgr {
+
+std::unique_ptr<LpMetadata> ReadFromImageFile(int fd) {
+ LpMetadataGeometry geometry;
+ if (!ReadLogicalPartitionGeometry(fd, &geometry)) {
+ return nullptr;
+ }
+ if (SeekFile64(fd, LP_METADATA_GEOMETRY_SIZE, SEEK_SET) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << LP_METADATA_GEOMETRY_SIZE;
+ return nullptr;
+ }
+ std::unique_ptr<LpMetadata> metadata = ParseMetadata(fd);
+ if (!metadata) {
+ return nullptr;
+ }
+ metadata->geometry = geometry;
+ return metadata;
+}
+
+std::unique_ptr<LpMetadata> ReadFromImageFile(const char* file) {
+ android::base::unique_fd fd(open(file, O_RDONLY));
+ if (fd < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "open failed: " << file;
+ return nullptr;
+ }
+ return ReadFromImageFile(fd);
+}
+
+bool WriteToImageFile(int fd, const LpMetadata& input) {
+ std::string geometry = SerializeGeometry(input.geometry);
+ std::string metadata = SerializeMetadata(input);
+
+ std::string everything = geometry + metadata;
+
+ if (!android::base::WriteFully(fd, everything.data(), everything.size())) {
+ PERROR << __PRETTY_FUNCTION__ << "write " << everything.size() << " bytes failed";
+ return false;
+ }
+ return true;
+}
+
+bool WriteToImageFile(const char* file, const LpMetadata& input) {
+ android::base::unique_fd fd(open(file, O_CREAT | O_RDWR | O_TRUNC, 0644));
+ if (fd < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "open failed: " << file;
+ return false;
+ }
+ return WriteToImageFile(fd, input);
+}
+
+// We use an object to build the sparse file since it requires that data
+// pointers be held alive until the sparse file is destroyed. It's easier
+// to do this when the data pointers are all in one place.
+class SparseBuilder {
+ public:
+ SparseBuilder(const LpMetadata& metadata, uint32_t block_size,
+ const std::map<std::string, std::string>& images);
+
+ bool Build();
+ bool Export(const char* file);
+ bool IsValid() const { return file_ != nullptr; }
+
+ private:
+ bool AddData(const std::string& blob, uint64_t sector);
+ bool AddPartitionImage(const LpMetadataPartition& partition, const std::string& file);
+ int OpenImageFile(const std::string& file);
+ bool SectorToBlock(uint64_t sector, uint32_t* block);
+
+ const LpMetadata& metadata_;
+ const LpMetadataGeometry& geometry_;
+ uint32_t block_size_;
+ std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)> file_;
+ std::string primary_blob_;
+ std::string backup_blob_;
+ std::map<std::string, std::string> images_;
+ std::vector<android::base::unique_fd> temp_fds_;
+};
+
+SparseBuilder::SparseBuilder(const LpMetadata& metadata, uint32_t block_size,
+ const std::map<std::string, std::string>& images)
+ : metadata_(metadata),
+ geometry_(metadata.geometry),
+ block_size_(block_size),
+ file_(sparse_file_new(block_size_, geometry_.block_device_size), sparse_file_destroy),
+ images_(images) {}
+
+bool SparseBuilder::Export(const char* file) {
+ android::base::unique_fd fd(open(file, O_CREAT | O_RDWR | O_TRUNC, 0644));
+ if (fd < 0) {
+ PERROR << "open failed: " << file;
+ return false;
+ }
+ // No gzip compression; sparseify; no checksum.
+ int ret = sparse_file_write(file_.get(), fd, false, true, false);
+ if (ret != 0) {
+ LERROR << "sparse_file_write failed (error code " << ret << ")";
+ return false;
+ }
+ return true;
+}
+
+bool SparseBuilder::AddData(const std::string& blob, uint64_t sector) {
+ uint32_t block;
+ if (!SectorToBlock(sector, &block)) {
+ return false;
+ }
+ void* data = const_cast<char*>(blob.data());
+ int ret = sparse_file_add_data(file_.get(), data, blob.size(), block);
+ if (ret != 0) {
+ LERROR << "sparse_file_add_data failed (error code " << ret << ")";
+ return false;
+ }
+ return true;
+}
+
+bool SparseBuilder::SectorToBlock(uint64_t sector, uint32_t* block) {
+ // The caller must ensure that the metadata has an alignment that is a
+ // multiple of the block size. liblp will take care of the rest, ensuring
+ // that all partitions are on an aligned boundary. Therefore all writes
+ // should be block-aligned, and if they are not, the table was misconfigured.
+ // Note that the default alignment is 1MiB, which is a multiple of the
+ // default block size (4096).
+ if ((sector * LP_SECTOR_SIZE) % block_size_ != 0) {
+ LERROR << "sector " << sector << " is not aligned to block size " << block_size_;
+ return false;
+ }
+ *block = (sector * LP_SECTOR_SIZE) / block_size_;
+ return true;
+}
+
+bool SparseBuilder::Build() {
+ std::string geometry_blob = SerializeGeometry(geometry_);
+ std::string metadata_blob = SerializeMetadata(metadata_);
+ metadata_blob.resize(geometry_.metadata_max_size);
+
+ std::string all_metadata;
+ for (size_t i = 0; i < geometry_.metadata_slot_count; i++) {
+ all_metadata += metadata_blob;
+ }
+
+ // Metadata immediately follows geometry, and we write the same metadata
+ // to all slots. Note that we don't bother trying to write skip chunks
+ // here since it's a small amount of data.
+ primary_blob_ = geometry_blob + all_metadata;
+ if (!AddData(primary_blob_, 0)) {
+ return false;
+ }
+
+ for (const auto& partition : metadata_.partitions) {
+ auto iter = images_.find(GetPartitionName(partition));
+ if (iter == images_.end()) {
+ continue;
+ }
+ if (!AddPartitionImage(partition, iter->second)) {
+ return false;
+ }
+ images_.erase(iter);
+ }
+
+ if (!images_.empty()) {
+ LERROR << "Partition image was specified but no partition was found.";
+ return false;
+ }
+
+ // The backup area contains all metadata slots, and then geometry. Similar
+ // to before we write the metadata to every slot.
+ int64_t backup_offset = GetBackupMetadataOffset(geometry_, 0);
+ uint64_t backups_start = geometry_.block_device_size + backup_offset;
+ uint64_t backup_sector = backups_start / LP_SECTOR_SIZE;
+
+ backup_blob_ = all_metadata + geometry_blob;
+ if (!AddData(backup_blob_, backup_sector)) {
+ return false;
+ }
+ return true;
+}
+
+static inline bool HasFillValue(uint32_t* buffer, size_t count) {
+ uint32_t fill_value = buffer[0];
+ for (size_t i = 1; i < count; i++) {
+ if (fill_value != buffer[i]) {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool SparseBuilder::AddPartitionImage(const LpMetadataPartition& partition,
+ const std::string& file) {
+ if (partition.num_extents != 1) {
+ LERROR << "Partition for new tables should not have more than one extent: "
+ << GetPartitionName(partition);
+ return false;
+ }
+
+ const LpMetadataExtent& extent = metadata_.extents[partition.first_extent_index];
+ if (extent.target_type != LP_TARGET_TYPE_LINEAR) {
+ LERROR << "Partition should only have linear extents: " << GetPartitionName(partition);
+ return false;
+ }
+
+ int fd = OpenImageFile(file);
+ if (fd < 0) {
+ LERROR << "Could not open image for partition: " << GetPartitionName(partition);
+ return false;
+ }
+
+ // Make sure the image does not exceed the partition size.
+ uint64_t file_length;
+ if (!GetDescriptorSize(fd, &file_length)) {
+ LERROR << "Could not compute image size";
+ return false;
+ }
+ if (file_length > extent.num_sectors * LP_SECTOR_SIZE) {
+ LERROR << "Image for partition '" << GetPartitionName(partition)
+ << "' is greater than its size";
+ return false;
+ }
+ if (SeekFile64(fd, 0, SEEK_SET)) {
+ PERROR << "lseek failed";
+ return false;
+ }
+
+ uint32_t output_block;
+ if (!SectorToBlock(extent.target_data, &output_block)) {
+ return false;
+ }
+
+ uint64_t pos = 0;
+ uint64_t remaining = file_length;
+ while (remaining) {
+ uint32_t buffer[block_size_ / sizeof(uint32_t)];
+ size_t read_size = remaining >= sizeof(buffer) ? sizeof(buffer) : size_t(remaining);
+ if (!android::base::ReadFully(fd, buffer, sizeof(buffer))) {
+ PERROR << "read failed";
+ return false;
+ }
+ if (read_size != sizeof(buffer) || !HasFillValue(buffer, read_size / sizeof(uint32_t))) {
+ int rv = sparse_file_add_fd(file_.get(), fd, pos, read_size, output_block);
+ if (rv) {
+ LERROR << "sparse_file_add_fd failed with code: " << rv;
+ return false;
+ }
+ } else {
+ int rv = sparse_file_add_fill(file_.get(), buffer[0], read_size, output_block);
+ if (rv) {
+ LERROR << "sparse_file_add_fill failed with code: " << rv;
+ return false;
+ }
+ }
+ pos += read_size;
+ remaining -= read_size;
+ output_block++;
+ }
+
+ return true;
+}
+
+int SparseBuilder::OpenImageFile(const std::string& file) {
+ android::base::unique_fd source_fd(open(file.c_str(), O_RDONLY));
+ if (source_fd < 0) {
+ PERROR << "open image file failed: " << file;
+ return -1;
+ }
+
+ std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)> source(
+ sparse_file_import(source_fd, true, true), sparse_file_destroy);
+ if (!source) {
+ int fd = source_fd.get();
+ temp_fds_.push_back(std::move(source_fd));
+ return fd;
+ }
+
+ char temp_file[PATH_MAX];
+ snprintf(temp_file, sizeof(temp_file), "%s/imageXXXXXX", P_tmpdir);
+ android::base::unique_fd temp_fd(mkstemp(temp_file));
+ if (temp_fd < 0) {
+ PERROR << "mkstemp failed";
+ return -1;
+ }
+ if (unlink(temp_file) < 0) {
+ PERROR << "unlink failed";
+ return -1;
+ }
+
+ // We temporarily unsparse the file, rather than try to merge its chunks.
+ int rv = sparse_file_write(source.get(), temp_fd, false, false, false);
+ if (rv) {
+ LERROR << "sparse_file_write failed with code: " << rv;
+ return -1;
+ }
+ temp_fds_.push_back(std::move(temp_fd));
+ return temp_fds_.back().get();
+}
+
+bool WriteToSparseFile(const char* file, const LpMetadata& metadata, uint32_t block_size,
+ const std::map<std::string, std::string>& images) {
+ if (block_size % LP_SECTOR_SIZE != 0) {
+ LERROR << "Block size must be a multiple of the sector size, " << LP_SECTOR_SIZE;
+ return false;
+ }
+ if (metadata.geometry.block_device_size % block_size != 0) {
+ LERROR << "Device size must be a multiple of the block size, " << block_size;
+ return false;
+ }
+ uint64_t num_blocks = metadata.geometry.block_device_size % block_size;
+ if (num_blocks >= UINT_MAX) {
+ // libsparse counts blocks in unsigned 32-bit integers, so we check to
+ // make sure we're not going to overflow.
+ LERROR << "Block device is too large to encode with libsparse.";
+ return false;
+ }
+
+ SparseBuilder builder(metadata, block_size, images);
+ if (!builder.IsValid()) {
+ LERROR << "Could not allocate sparse file of size " << metadata.geometry.block_device_size;
+ return false;
+ }
+ if (!builder.Build()) {
+ return false;
+ }
+ return builder.Export(file);
+}
+
+} // namespace fs_mgr
+} // namespace android
diff --git a/fs_mgr/liblp/images.h b/fs_mgr/liblp/images.h
new file mode 100644
index 0000000..3a999b8
--- /dev/null
+++ b/fs_mgr/liblp/images.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2018 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 <liblp/liblp.h>
+
+namespace android {
+namespace fs_mgr {
+
+// Helper function to serialize geometry and metadata to a normal file, for
+// flashing or debugging.
+std::unique_ptr<LpMetadata> ReadFromImageFile(int fd);
+bool WriteToImageFile(const char* file, const LpMetadata& metadata);
+bool WriteToImageFile(int fd, const LpMetadata& metadata);
+
+} // namespace fs_mgr
+} // namespace android
diff --git a/fs_mgr/liblp/include/liblp/builder.h b/fs_mgr/liblp/include/liblp/builder.h
new file mode 100644
index 0000000..a35cf8e
--- /dev/null
+++ b/fs_mgr/liblp/include/liblp/builder.h
@@ -0,0 +1,214 @@
+//
+// Copyright (C) 2018 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.
+//
+
+#ifndef LIBLP_METADATA_BUILDER_H
+#define LIBLP_METADATA_BUILDER_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <map>
+#include <memory>
+
+#include "liblp.h"
+
+namespace android {
+namespace fs_mgr {
+
+class LinearExtent;
+
+// By default, partitions are aligned on a 1MiB boundary.
+static const uint32_t kDefaultPartitionAlignment = 1024 * 1024;
+static const uint32_t kDefaultBlockSize = 4096;
+
+struct BlockDeviceInfo {
+ BlockDeviceInfo() : size(0), alignment(0), alignment_offset(0), logical_block_size(0) {}
+ BlockDeviceInfo(uint64_t size, uint32_t alignment, uint32_t alignment_offset,
+ uint32_t logical_block_size)
+ : size(size),
+ alignment(alignment),
+ alignment_offset(alignment_offset),
+ logical_block_size(logical_block_size) {}
+ // Size of the block device, in bytes.
+ uint64_t size;
+ // Optimal target alignment, in bytes. Partition extents will be aligned to
+ // this value by default. This value must be 0 or a multiple of 512.
+ uint32_t alignment;
+ // Alignment offset to parent device (if any), in bytes. The sector at
+ // |alignment_offset| on the target device is correctly aligned on its
+ // parent device. This value must be 0 or a multiple of 512.
+ uint32_t alignment_offset;
+ // Block size, for aligning extent sizes and partition sizes.
+ uint32_t logical_block_size;
+};
+
+// Abstraction around dm-targets that can be encoded into logical partition tables.
+class Extent {
+ public:
+ explicit Extent(uint64_t num_sectors) : num_sectors_(num_sectors) {}
+ virtual ~Extent() {}
+
+ virtual void AddTo(LpMetadata* out) const = 0;
+ virtual LinearExtent* AsLinearExtent() { return nullptr; }
+
+ uint64_t num_sectors() const { return num_sectors_; }
+ void set_num_sectors(uint64_t num_sectors) { num_sectors_ = num_sectors; }
+
+ protected:
+ uint64_t num_sectors_;
+};
+
+// This corresponds to a dm-linear target.
+class LinearExtent final : public Extent {
+ public:
+ LinearExtent(uint64_t num_sectors, uint64_t physical_sector)
+ : Extent(num_sectors), physical_sector_(physical_sector) {}
+
+ void AddTo(LpMetadata* metadata) const override;
+ LinearExtent* AsLinearExtent() override { return this; }
+
+ uint64_t physical_sector() const { return physical_sector_; }
+ uint64_t end_sector() const { return physical_sector_ + num_sectors_; }
+
+ private:
+ uint64_t physical_sector_;
+};
+
+// This corresponds to a dm-zero target.
+class ZeroExtent final : public Extent {
+ public:
+ explicit ZeroExtent(uint64_t num_sectors) : Extent(num_sectors) {}
+
+ void AddTo(LpMetadata* out) const override;
+};
+
+class Partition final {
+ friend class MetadataBuilder;
+
+ public:
+ Partition(const std::string& name, const std::string& guid, uint32_t attributes);
+
+ // Add a raw extent.
+ void AddExtent(std::unique_ptr<Extent>&& extent);
+
+ // Remove all extents from this partition.
+ void RemoveExtents();
+
+ const std::string& name() const { return name_; }
+ uint32_t attributes() const { return attributes_; }
+ const std::string& guid() const { return guid_; }
+ const std::vector<std::unique_ptr<Extent>>& extents() const { return extents_; }
+ uint64_t size() const { return size_; }
+
+ private:
+ void ShrinkTo(uint64_t aligned_size);
+
+ std::string name_;
+ std::string guid_;
+ std::vector<std::unique_ptr<Extent>> extents_;
+ uint32_t attributes_;
+ uint64_t size_;
+};
+
+class MetadataBuilder {
+ public:
+ // Construct an empty logical partition table builder. The block device size
+ // and maximum metadata size must be specified, as this will determine which
+ // areas of the physical partition can be flashed for metadata vs for logical
+ // partitions.
+ //
+ // If the parameters would yield invalid metadata, nullptr is returned. This
+ // could happen if the block device size is too small to store the metadata
+ // and backup copies.
+ static std::unique_ptr<MetadataBuilder> New(const BlockDeviceInfo& device_info,
+ uint32_t metadata_max_size,
+ uint32_t metadata_slot_count);
+
+ // Import an existing table for modification. This reads metadata off the
+ // given block device and imports it. It also adjusts alignment information
+ // based on run-time values in the operating system.
+ static std::unique_ptr<MetadataBuilder> New(const std::string& block_device,
+ uint32_t slot_number);
+
+ // Import an existing table for modification. If the table is not valid, for
+ // example it contains duplicate partition names, then nullptr is returned.
+ // This method is for testing or changing off-line tables.
+ static std::unique_ptr<MetadataBuilder> New(const LpMetadata& metadata);
+
+ // Wrapper around New() with a BlockDeviceInfo that only specifies a device
+ // size. This is a convenience method for tests.
+ static std::unique_ptr<MetadataBuilder> New(uint64_t blockdev_size, uint32_t metadata_max_size,
+ uint32_t metadata_slot_count) {
+ BlockDeviceInfo device_info(blockdev_size, 0, 0, kDefaultBlockSize);
+ return New(device_info, metadata_max_size, metadata_slot_count);
+ }
+
+ // Export metadata so it can be serialized to an image, to disk, or mounted
+ // via device-mapper.
+ std::unique_ptr<LpMetadata> Export();
+
+ // Add a partition, returning a handle so it can be sized as needed. If a
+ // partition with the given name already exists, nullptr is returned.
+ Partition* AddPartition(const std::string& name, const std::string& guid, uint32_t attributes);
+
+ // Delete a partition by name if it exists.
+ void RemovePartition(const std::string& name);
+
+ // Find a partition by name. If no partition is found, nullptr is returned.
+ Partition* FindPartition(const std::string& name);
+
+ // Grow or shrink a partition to the requested size. This size will be
+ // rounded UP to the nearest block (512 bytes).
+ //
+ // When growing a partition, a greedy algorithm is used to find free gaps
+ // in the partition table and allocate them. If not enough space can be
+ // allocated, false is returned, and the parition table will not be
+ // modified.
+ //
+ // Note, this is an in-memory operation, and it does not alter the
+ // underlying filesystem or contents of the partition on disk.
+ bool ResizePartition(Partition* partition, uint64_t requested_size);
+
+ // Amount of space that can be allocated to logical partitions.
+ uint64_t AllocatableSpace() const;
+
+ // Merge new block device information into previous values. Alignment values
+ // are only overwritten if the new values are non-zero.
+ void set_block_device_info(const BlockDeviceInfo& device_info);
+ const BlockDeviceInfo& block_device_info() const { return device_info_; }
+
+ private:
+ MetadataBuilder();
+ bool Init(const BlockDeviceInfo& info, uint32_t metadata_max_size, uint32_t metadata_slot_count);
+ bool Init(const LpMetadata& metadata);
+ bool GrowPartition(Partition* partition, uint64_t aligned_size);
+ void ShrinkPartition(Partition* partition, uint64_t aligned_size);
+ uint64_t AlignSector(uint64_t sector);
+
+ LpMetadataGeometry geometry_;
+ LpMetadataHeader header_;
+ std::vector<std::unique_ptr<Partition>> partitions_;
+ BlockDeviceInfo device_info_;
+};
+
+// Read BlockDeviceInfo for a given block device. This always returns false
+// for non-Linux operating systems.
+bool GetBlockDeviceInfo(const std::string& block_device, BlockDeviceInfo* device_info);
+
+} // namespace fs_mgr
+} // namespace android
+
+#endif /* LIBLP_METADATA_BUILDER_H */
diff --git a/fs_mgr/liblp/include/liblp/liblp.h b/fs_mgr/liblp/include/liblp/liblp.h
new file mode 100644
index 0000000..627aa8c
--- /dev/null
+++ b/fs_mgr/liblp/include/liblp/liblp.h
@@ -0,0 +1,78 @@
+//
+// Copyright (C) 2018 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.
+//
+
+#ifndef LIBLP_LIBLP_H
+#define LIBLP_LIBLP_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <map>
+#include <memory>
+#include <string>
+
+#include "metadata_format.h"
+
+namespace android {
+namespace fs_mgr {
+
+// Helper structure for easily interpreting deserialized metadata, or
+// re-serializing metadata.
+struct LpMetadata {
+ LpMetadataGeometry geometry;
+ LpMetadataHeader header;
+ std::vector<LpMetadataPartition> partitions;
+ std::vector<LpMetadataExtent> extents;
+};
+
+// Place an initial partition table on the device. This will overwrite the
+// existing geometry, and should not be used for normal partition table
+// updates. False can be returned if the geometry is incompatible with the
+// block device or an I/O error occurs.
+bool FlashPartitionTable(const std::string& block_device, const LpMetadata& metadata,
+ uint32_t slot_number);
+
+// Update the partition table for a given metadata slot number. False is
+// returned if an error occurs, which can include:
+// - Invalid slot number.
+// - I/O error.
+// - Corrupt or missing metadata geometry on disk.
+// - Incompatible geometry.
+bool UpdatePartitionTable(const std::string& block_device, const LpMetadata& metadata,
+ uint32_t slot_number);
+
+// Read logical partition metadata from its predetermined location on a block
+// device. If readback fails, we also attempt to load from a backup copy.
+std::unique_ptr<LpMetadata> ReadMetadata(const char* block_device, uint32_t slot_number);
+
+// Read/Write logical partition metadata to an image file, for diagnostics or
+// flashing.
+bool WriteToSparseFile(const char* file, const LpMetadata& metadata, uint32_t block_size,
+ const std::map<std::string, std::string>& images);
+bool WriteToImageFile(const char* file, const LpMetadata& metadata);
+std::unique_ptr<LpMetadata> ReadFromImageFile(const char* file);
+
+// Helper to extract safe C++ strings from partition info.
+std::string GetPartitionName(const LpMetadataPartition& partition);
+std::string GetPartitionGuid(const LpMetadataPartition& partition);
+
+// Helper to return a slot number for a slot suffix.
+uint32_t SlotNumberForSlotSuffix(const std::string& suffix);
+
+} // namespace fs_mgr
+} // namespace android
+
+#endif // LIBLP_LIBLP_H
diff --git a/fs_mgr/liblp/include/liblp/metadata_format.h b/fs_mgr/liblp/include/liblp/metadata_format.h
new file mode 100644
index 0000000..52c80f7
--- /dev/null
+++ b/fs_mgr/liblp/include/liblp/metadata_format.h
@@ -0,0 +1,278 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef LOGICAL_PARTITION_METADATA_FORMAT_H_
+#define LOGICAL_PARTITION_METADATA_FORMAT_H_
+
+#ifdef __cplusplus
+#include <string>
+#include <vector>
+#endif
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Magic signature for LpMetadataGeometry. */
+#define LP_METADATA_GEOMETRY_MAGIC 0x616c4467
+
+/* Space reserved for geometry information. */
+#define LP_METADATA_GEOMETRY_SIZE 4096
+
+/* Magic signature for LpMetadataHeader. */
+#define LP_METADATA_HEADER_MAGIC 0x414C5030
+
+/* Current metadata version. */
+#define LP_METADATA_MAJOR_VERSION 1
+#define LP_METADATA_MINOR_VERSION 0
+
+/* Attributes for the LpMetadataPartition::attributes field.
+ *
+ * READONLY - The partition should not be considered writable. When used with
+ * device mapper, the block device will be created as read-only.
+ */
+#define LP_PARTITION_ATTR_NONE 0x0
+#define LP_PARTITION_ATTR_READONLY 0x1
+
+/* Mask that defines all valid attributes. */
+#define LP_PARTITION_ATTRIBUTE_MASK (LP_PARTITION_ATTR_READONLY)
+
+/* Default name of the physical partition that holds logical partition entries.
+ * The layout of this partition will look like:
+ *
+ * +--------------------+
+ * | Disk Geometry |
+ * +--------------------+
+ * | Metadata |
+ * +--------------------+
+ * | Logical Partitions |
+ * +--------------------+
+ * | Backup Metadata |
+ * +--------------------+
+ * | Geometry Backup |
+ * +--------------------+
+ */
+#define LP_METADATA_PARTITION_NAME "super"
+
+/* Size of a sector is always 512 bytes for compatibility with the Linux kernel. */
+#define LP_SECTOR_SIZE 512
+
+/* This structure is stored at sector 0 in the first 4096 bytes of the
+ * partition, and again in the very last 4096 bytes. It is never modified and
+ * describes how logical partition information can be located.
+ */
+typedef struct LpMetadataGeometry {
+ /* 0: Magic signature (LP_METADATA_GEOMETRY_MAGIC). */
+ uint32_t magic;
+
+ /* 4: Size of the LpMetadataGeometry struct. */
+ uint32_t struct_size;
+
+ /* 8: SHA256 checksum of this struct, with this field set to 0. */
+ uint8_t checksum[32];
+
+ /* 40: Maximum amount of space a single copy of the metadata can use. This
+ * must be a multiple of LP_SECTOR_SIZE.
+ */
+ uint32_t metadata_max_size;
+
+ /* 44: Number of copies of the metadata to keep. For A/B devices, this
+ * will be 2. For an A/B/C device, it would be 3, et cetera. For Non-A/B
+ * it will be 1. A backup copy of each slot is kept, so if this is "2",
+ * there will be four copies total.
+ */
+ uint32_t metadata_slot_count;
+
+ /* 48: First usable sector for allocating logical partitions. this will be
+ * the first sector after the initial 4096 geometry block, followed by the
+ * space consumed by metadata_max_size*metadata_slot_count.
+ */
+ uint64_t first_logical_sector;
+
+ /* 56: Last usable sector, inclusive, for allocating logical partitions.
+ * At the end of this sector will follow backup metadata slots and the
+ * backup geometry block at the very end.
+ */
+ uint64_t last_logical_sector;
+
+ /* 64: Alignment for defining partitions or partition extents. For example,
+ * an alignment of 1MiB will require that all partitions have a size evenly
+ * divisible by 1MiB, and that the smallest unit the partition can grow by
+ * is 1MiB.
+ *
+ * Alignment is normally determined at runtime when growing or adding
+ * partitions. If for some reason the alignment cannot be determined, then
+ * this predefined alignment in the geometry is used instead. By default
+ * it is set to 1MiB.
+ */
+ uint32_t alignment;
+
+ /* 68: Alignment offset for "stacked" devices. For example, if the "super"
+ * partition itself is not aligned within the parent block device's
+ * partition table, then we adjust for this in deciding where to place
+ * |first_logical_sector|.
+ *
+ * Similar to |alignment|, this will be derived from the operating system.
+ * If it cannot be determined, it is assumed to be 0.
+ */
+ uint32_t alignment_offset;
+
+ /* 72: Block device size, as specified when the metadata was created. This
+ * can be used to verify the geometry against a target device.
+ */
+ uint64_t block_device_size;
+
+ /* 76: Logical block size of the super partition block device. This is the
+ * minimal alignment for partition and extent sizes, and it must be a
+ * multiple of LP_SECTOR_SIZE.
+ */
+ uint32_t logical_block_size;
+} __attribute__((packed)) LpMetadataGeometry;
+
+/* The logical partition metadata has a number of tables; they are described
+ * in the header via the following structure.
+ *
+ * The size of the table can be computed by multiplying entry_size by
+ * num_entries, and the result must not overflow a 32-bit signed integer.
+ */
+typedef struct LpMetadataTableDescriptor {
+ /* 0: Location of the table, relative to the metadata header. */
+ uint32_t offset;
+ /* 4: Number of entries in the table. */
+ uint32_t num_entries;
+ /* 8: Size of each entry in the table, in bytes. */
+ uint32_t entry_size;
+} __attribute__((packed)) LpMetadataTableDescriptor;
+
+/* Binary format for the header of the logical partition metadata format.
+ *
+ * The format has three sections. The header must occur first, and the
+ * proceeding tables may be placed in any order after.
+ *
+ * +-----------------------------------------+
+ * | Header data - fixed size |
+ * +-----------------------------------------+
+ * | Partition table - variable size |
+ * +-----------------------------------------+
+ * | Partition table extents - variable size |
+ * +-----------------------------------------+
+ *
+ * The "Header" portion is described by LpMetadataHeader. It will always
+ * precede the other three blocks.
+ *
+ * All fields are stored in little-endian byte order when serialized.
+ *
+ * This struct is versioned; see the |major_version| and |minor_version|
+ * fields.
+ */
+typedef struct LpMetadataHeader {
+ /* 0: Four bytes equal to LP_METADATA_HEADER_MAGIC. */
+ uint32_t magic;
+
+ /* 4: Version number required to read this metadata. If the version is not
+ * equal to the library version, the metadata should be considered
+ * incompatible.
+ */
+ uint16_t major_version;
+
+ /* 6: Minor version. A library supporting newer features should be able to
+ * read metadata with an older minor version. However, an older library
+ * should not support reading metadata if its minor version is higher.
+ */
+ uint16_t minor_version;
+
+ /* 8: The size of this header struct. */
+ uint32_t header_size;
+
+ /* 12: SHA256 checksum of the header, up to |header_size| bytes, computed as
+ * if this field were set to 0.
+ */
+ uint8_t header_checksum[32];
+
+ /* 44: The total size of all tables. This size is contiguous; tables may not
+ * have gaps in between, and they immediately follow the header.
+ */
+ uint32_t tables_size;
+
+ /* 48: SHA256 checksum of all table contents. */
+ uint8_t tables_checksum[32];
+
+ /* 80: Partition table descriptor. */
+ LpMetadataTableDescriptor partitions;
+ /* 92: Extent table descriptor. */
+ LpMetadataTableDescriptor extents;
+} __attribute__((packed)) LpMetadataHeader;
+
+/* This struct defines a logical partition entry, similar to what would be
+ * present in a GUID Partition Table.
+ */
+typedef struct LpMetadataPartition {
+ /* 0: Name of this partition in ASCII characters. Any unused characters in
+ * the buffer must be set to 0. Characters may only be alphanumeric or _.
+ * The name must include at least one ASCII character, and it must be unique
+ * across all partition names. The length (36) is the same as the maximum
+ * length of a GPT partition name.
+ */
+ char name[36];
+
+ /* 36: Globally unique identifier (GUID) of this partition. */
+ uint8_t guid[16];
+
+ /* 52: Attributes for the partition (see LP_PARTITION_ATTR_* flags above). */
+ uint32_t attributes;
+
+ /* 56: Index of the first extent owned by this partition. The extent will
+ * start at logical sector 0. Gaps between extents are not allowed.
+ */
+ uint32_t first_extent_index;
+
+ /* 60: Number of extents in the partition. Every partition must have at
+ * least one extent.
+ */
+ uint32_t num_extents;
+} __attribute__((packed)) LpMetadataPartition;
+
+/* This extent is a dm-linear target, and the index is an index into the
+ * LinearExtent table.
+ */
+#define LP_TARGET_TYPE_LINEAR 0
+
+/* This extent is a dm-zero target. The index is ignored and must be 0. */
+#define LP_TARGET_TYPE_ZERO 1
+
+/* This struct defines an extent entry in the extent table block. */
+typedef struct LpMetadataExtent {
+ /* 0: Length of this extent, in 512-byte sectors. */
+ uint64_t num_sectors;
+
+ /* 8: Target type for device-mapper (see LP_TARGET_TYPE_* values). */
+ uint32_t target_type;
+
+ /* 12: Contents depends on target_type.
+ *
+ * LINEAR: The sector on the physical partition that this extent maps onto.
+ * ZERO: This field must be 0.
+ */
+ uint64_t target_data;
+} __attribute__((packed)) LpMetadataExtent;
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* LOGICAL_PARTITION_METADATA_FORMAT_H_ */
diff --git a/fs_mgr/liblp/io_test.cpp b/fs_mgr/liblp/io_test.cpp
new file mode 100644
index 0000000..638f4b3
--- /dev/null
+++ b/fs_mgr/liblp/io_test.cpp
@@ -0,0 +1,523 @@
+/*
+ * Copyright (C) 2018 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 <fcntl.h>
+#include <linux/memfd.h>
+#include <stdio.h>
+#include <sys/syscall.h>
+
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+#include <gtest/gtest.h>
+#include <liblp/builder.h>
+
+#include "images.h"
+#include "reader.h"
+#include "utility.h"
+#include "writer.h"
+
+using namespace std;
+using namespace android::fs_mgr;
+using unique_fd = android::base::unique_fd;
+
+// Our tests assume a 128KiB disk with two 512 byte metadata slots.
+static const size_t kDiskSize = 131072;
+static const size_t kMetadataSize = 512;
+static const size_t kMetadataSlots = 2;
+static const char* TEST_GUID_BASE = "A799D1D6-669F-41D8-A3F0-EBB7572D830";
+static const char* TEST_GUID = "A799D1D6-669F-41D8-A3F0-EBB7572D8302";
+
+// Helper function for creating an in-memory file descriptor. This lets us
+// simulate read/writing logical partition metadata as if we had a block device
+// for a physical partition.
+static unique_fd CreateFakeDisk(off_t size) {
+ unique_fd fd(syscall(__NR_memfd_create, "fake_disk", MFD_ALLOW_SEALING));
+ if (fd < 0) {
+ perror("memfd_create");
+ return {};
+ }
+ if (ftruncate(fd, size) < 0) {
+ perror("ftruncate");
+ return {};
+ }
+ // Prevent anything from accidentally growing/shrinking the file, as it
+ // would not be allowed on an actual partition.
+ if (fcntl(fd, F_ADD_SEALS, F_SEAL_GROW | F_SEAL_SHRINK) < 0) {
+ perror("fcntl");
+ return {};
+ }
+ // Write garbage to the "disk" so we can tell what has been zeroed or not.
+ unique_ptr<uint8_t[]> buffer = make_unique<uint8_t[]>(size);
+ memset(buffer.get(), 0xcc, size);
+ if (!android::base::WriteFully(fd, buffer.get(), size)) {
+ return {};
+ }
+ return fd;
+}
+
+// Create a disk of the default size.
+static unique_fd CreateFakeDisk() {
+ return CreateFakeDisk(kDiskSize);
+}
+
+// Create a MetadataBuilder around some default sizes.
+static unique_ptr<MetadataBuilder> CreateDefaultBuilder() {
+ unique_ptr<MetadataBuilder> builder =
+ MetadataBuilder::New(kDiskSize, kMetadataSize, kMetadataSlots);
+ return builder;
+}
+
+static bool AddDefaultPartitions(MetadataBuilder* builder) {
+ Partition* system = builder->AddPartition("system", TEST_GUID, LP_PARTITION_ATTR_NONE);
+ if (!system) {
+ return false;
+ }
+ return builder->ResizePartition(system, 24 * 1024);
+}
+
+// Create a temporary disk and flash it with the default partition setup.
+static unique_fd CreateFlashedDisk() {
+ unique_ptr<MetadataBuilder> builder = CreateDefaultBuilder();
+ if (!builder || !AddDefaultPartitions(builder.get())) {
+ return {};
+ }
+ unique_fd fd = CreateFakeDisk();
+ if (fd < 0) {
+ return {};
+ }
+ // Export and flash.
+ unique_ptr<LpMetadata> exported = builder->Export();
+ if (!exported) {
+ return {};
+ }
+ if (!FlashPartitionTable(fd, *exported.get(), 0)) {
+ return {};
+ }
+ return fd;
+}
+
+// Test that our CreateFakeDisk() function works.
+TEST(liblp, CreateFakeDisk) {
+ unique_fd fd = CreateFakeDisk();
+ ASSERT_GE(fd, 0);
+
+ uint64_t size;
+ ASSERT_TRUE(GetDescriptorSize(fd, &size));
+ ASSERT_EQ(size, kDiskSize);
+}
+
+// Flashing metadata should not work if the metadata was created for a larger
+// disk than the destination disk.
+TEST(liblp, ExportDiskTooSmall) {
+ unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(kDiskSize + 1024, 512, 2);
+ ASSERT_NE(builder, nullptr);
+ unique_ptr<LpMetadata> exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+
+ // A larger geometry should fail to flash, since there won't be enough
+ // space to store the logical partition range that was specified.
+ unique_fd fd = CreateFakeDisk();
+ ASSERT_GE(fd, 0);
+
+ EXPECT_FALSE(FlashPartitionTable(fd, *exported.get(), 0));
+}
+
+// Test the basics of flashing a partition and reading it back.
+TEST(liblp, FlashAndReadback) {
+ unique_ptr<MetadataBuilder> builder = CreateDefaultBuilder();
+ ASSERT_NE(builder, nullptr);
+ ASSERT_TRUE(AddDefaultPartitions(builder.get()));
+
+ unique_fd fd = CreateFakeDisk();
+ ASSERT_GE(fd, 0);
+
+ // Export and flash.
+ unique_ptr<LpMetadata> exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+ ASSERT_TRUE(FlashPartitionTable(fd, *exported.get(), 0));
+
+ // Read back. Note that some fields are only filled in during
+ // serialization, so exported and imported will not be identical. For
+ // example, table sizes and checksums are computed in WritePartitionTable.
+ // Therefore we check on a field-by-field basis.
+ unique_ptr<LpMetadata> imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+
+ // Check geometry and header.
+ EXPECT_EQ(exported->geometry.metadata_max_size, imported->geometry.metadata_max_size);
+ EXPECT_EQ(exported->geometry.metadata_slot_count, imported->geometry.metadata_slot_count);
+ EXPECT_EQ(exported->geometry.first_logical_sector, imported->geometry.first_logical_sector);
+ EXPECT_EQ(exported->geometry.last_logical_sector, imported->geometry.last_logical_sector);
+ EXPECT_EQ(exported->header.major_version, imported->header.major_version);
+ EXPECT_EQ(exported->header.minor_version, imported->header.minor_version);
+ EXPECT_EQ(exported->header.header_size, imported->header.header_size);
+
+ // Check partition tables.
+ ASSERT_EQ(exported->partitions.size(), imported->partitions.size());
+ EXPECT_EQ(GetPartitionName(exported->partitions[0]), GetPartitionName(imported->partitions[0]));
+ EXPECT_EQ(GetPartitionGuid(exported->partitions[0]), GetPartitionGuid(imported->partitions[0]));
+ EXPECT_EQ(exported->partitions[0].attributes, imported->partitions[0].attributes);
+ EXPECT_EQ(exported->partitions[0].first_extent_index,
+ imported->partitions[0].first_extent_index);
+ EXPECT_EQ(exported->partitions[0].num_extents, imported->partitions[0].num_extents);
+
+ // Check extent tables.
+ ASSERT_EQ(exported->extents.size(), imported->extents.size());
+ EXPECT_EQ(exported->extents[0].num_sectors, imported->extents[0].num_sectors);
+ EXPECT_EQ(exported->extents[0].target_type, imported->extents[0].target_type);
+ EXPECT_EQ(exported->extents[0].target_data, imported->extents[0].target_data);
+}
+
+// Test that we can update metadata slots without disturbing others.
+TEST(liblp, UpdateAnyMetadataSlot) {
+ unique_fd fd = CreateFlashedDisk();
+ ASSERT_GE(fd, 0);
+
+ unique_ptr<LpMetadata> imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+ ASSERT_EQ(imported->partitions.size(), 1);
+ EXPECT_EQ(GetPartitionName(imported->partitions[0]), "system");
+
+ // Verify that we can't read unwritten metadata.
+ ASSERT_EQ(ReadMetadata(fd, 1), nullptr);
+
+ // Change the name before writing to the next slot.
+ strncpy(imported->partitions[0].name, "vendor", sizeof(imported->partitions[0].name));
+ ASSERT_TRUE(UpdatePartitionTable(fd, *imported.get(), 1));
+
+ // Read back the original slot, make sure it hasn't changed.
+ imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+ ASSERT_EQ(imported->partitions.size(), 1);
+ EXPECT_EQ(GetPartitionName(imported->partitions[0]), "system");
+
+ // Now read back the new slot, and verify that it has a different name.
+ imported = ReadMetadata(fd, 1);
+ ASSERT_NE(imported, nullptr);
+ ASSERT_EQ(imported->partitions.size(), 1);
+ EXPECT_EQ(GetPartitionName(imported->partitions[0]), "vendor");
+
+ // Verify that we didn't overwrite anything in the logical paritition area.
+ // We expect the disk to be filled with 0xcc on creation so we can read
+ // this back and compare it.
+ char expected[LP_SECTOR_SIZE];
+ memset(expected, 0xcc, sizeof(expected));
+ for (uint64_t i = imported->geometry.first_logical_sector;
+ i <= imported->geometry.last_logical_sector; i++) {
+ char buffer[LP_SECTOR_SIZE];
+ ASSERT_GE(lseek(fd, i * LP_SECTOR_SIZE, SEEK_SET), 0);
+ ASSERT_TRUE(android::base::ReadFully(fd, buffer, sizeof(buffer)));
+ ASSERT_EQ(memcmp(expected, buffer, LP_SECTOR_SIZE), 0);
+ }
+}
+
+TEST(liblp, InvalidMetadataSlot) {
+ unique_fd fd = CreateFlashedDisk();
+ ASSERT_GE(fd, 0);
+
+ // Make sure all slots are filled.
+ unique_ptr<LpMetadata> metadata = ReadMetadata(fd, 0);
+ ASSERT_NE(metadata, nullptr);
+ for (uint32_t i = 1; i < kMetadataSlots; i++) {
+ ASSERT_TRUE(UpdatePartitionTable(fd, *metadata.get(), i));
+ }
+
+ // Verify that we can't read unavailable slots.
+ EXPECT_EQ(ReadMetadata(fd, kMetadataSlots), nullptr);
+}
+
+// Test that updating a metadata slot does not allow it to be computed based
+// on mismatching geometry.
+TEST(liblp, NoChangingGeometry) {
+ unique_fd fd = CreateFlashedDisk();
+ ASSERT_GE(fd, 0);
+
+ unique_ptr<LpMetadata> imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+ ASSERT_TRUE(UpdatePartitionTable(fd, *imported.get(), 1));
+
+ imported->geometry.metadata_max_size += LP_SECTOR_SIZE;
+ ASSERT_FALSE(UpdatePartitionTable(fd, *imported.get(), 1));
+
+ imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+ imported->geometry.metadata_slot_count++;
+ ASSERT_FALSE(UpdatePartitionTable(fd, *imported.get(), 1));
+
+ imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+ imported->geometry.first_logical_sector++;
+ ASSERT_FALSE(UpdatePartitionTable(fd, *imported.get(), 1));
+
+ imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+ imported->geometry.last_logical_sector--;
+ ASSERT_FALSE(UpdatePartitionTable(fd, *imported.get(), 1));
+}
+
+// Test that changing one bit of metadata is enough to break the checksum.
+TEST(liblp, BitFlipGeometry) {
+ unique_fd fd = CreateFlashedDisk();
+ ASSERT_GE(fd, 0);
+
+ LpMetadataGeometry geometry;
+ ASSERT_GE(lseek(fd, 0, SEEK_SET), 0);
+ ASSERT_TRUE(android::base::ReadFully(fd, &geometry, sizeof(geometry)));
+
+ LpMetadataGeometry bad_geometry = geometry;
+ bad_geometry.metadata_slot_count++;
+ ASSERT_TRUE(android::base::WriteFully(fd, &bad_geometry, sizeof(bad_geometry)));
+
+ unique_ptr<LpMetadata> metadata = ReadMetadata(fd, 0);
+ ASSERT_NE(metadata, nullptr);
+ EXPECT_EQ(metadata->geometry.metadata_slot_count, 2);
+}
+
+TEST(liblp, ReadBackupGeometry) {
+ unique_fd fd = CreateFlashedDisk();
+ ASSERT_GE(fd, 0);
+
+ char corruption[LP_METADATA_GEOMETRY_SIZE];
+ memset(corruption, 0xff, sizeof(corruption));
+
+ // Corrupt the first 4096 bytes of the disk.
+ ASSERT_GE(lseek(fd, 0, SEEK_SET), 0);
+ ASSERT_TRUE(android::base::WriteFully(fd, corruption, sizeof(corruption)));
+ EXPECT_NE(ReadMetadata(fd, 0), nullptr);
+
+ // Corrupt the last 4096 bytes too.
+ ASSERT_GE(lseek(fd, -LP_METADATA_GEOMETRY_SIZE, SEEK_END), 0);
+ ASSERT_TRUE(android::base::WriteFully(fd, corruption, sizeof(corruption)));
+ EXPECT_EQ(ReadMetadata(fd, 0), nullptr);
+}
+
+TEST(liblp, ReadBackupMetadata) {
+ unique_fd fd = CreateFlashedDisk();
+ ASSERT_GE(fd, 0);
+
+ unique_ptr<LpMetadata> metadata = ReadMetadata(fd, 0);
+
+ char corruption[kMetadataSize];
+ memset(corruption, 0xff, sizeof(corruption));
+
+ ASSERT_GE(lseek(fd, LP_METADATA_GEOMETRY_SIZE, SEEK_SET), 0);
+ ASSERT_TRUE(android::base::WriteFully(fd, corruption, sizeof(corruption)));
+ EXPECT_NE(ReadMetadata(fd, 0), nullptr);
+
+ off_t offset = LP_METADATA_GEOMETRY_SIZE + kMetadataSize * 2;
+
+ // Corrupt the backup metadata.
+ ASSERT_GE(lseek(fd, -offset, SEEK_END), 0);
+ ASSERT_TRUE(android::base::WriteFully(fd, corruption, sizeof(corruption)));
+ EXPECT_EQ(ReadMetadata(fd, 0), nullptr);
+}
+
+// Test that we don't attempt to write metadata if it would overflow its
+// reserved space.
+TEST(liblp, TooManyPartitions) {
+ unique_ptr<MetadataBuilder> builder = CreateDefaultBuilder();
+ ASSERT_NE(builder, nullptr);
+
+ // Compute the maximum number of partitions we can fit in 1024 bytes of metadata.
+ size_t max_partitions = (kMetadataSize - sizeof(LpMetadataHeader)) / sizeof(LpMetadataPartition);
+ EXPECT_LT(max_partitions, 10);
+
+ // Add this number of partitions.
+ Partition* partition = nullptr;
+ for (size_t i = 0; i < max_partitions; i++) {
+ std::string guid = std::string(TEST_GUID) + to_string(i);
+ partition = builder->AddPartition(to_string(i), TEST_GUID, LP_PARTITION_ATTR_NONE);
+ ASSERT_NE(partition, nullptr);
+ }
+ ASSERT_NE(partition, nullptr);
+ // Add one extent to any partition to fill up more space - we're at 508
+ // bytes after this, out of 512.
+ ASSERT_TRUE(builder->ResizePartition(partition, 1024));
+
+ unique_ptr<LpMetadata> exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+
+ unique_fd fd = CreateFakeDisk();
+ ASSERT_GE(fd, 0);
+
+ // Check that we are able to write our table.
+ ASSERT_TRUE(FlashPartitionTable(fd, *exported.get(), 0));
+ ASSERT_TRUE(UpdatePartitionTable(fd, *exported.get(), 1));
+
+ // Check that adding one more partition overflows the metadata allotment.
+ partition = builder->AddPartition("final", TEST_GUID, LP_PARTITION_ATTR_NONE);
+ EXPECT_NE(partition, nullptr);
+
+ exported = builder->Export();
+ ASSERT_NE(exported, nullptr);
+
+ // The new table should be too large to be written.
+ ASSERT_FALSE(UpdatePartitionTable(fd, *exported.get(), 1));
+
+ // Check that the first and last logical sectors weren't touched when we
+ // wrote this almost-full metadata.
+ char expected[LP_SECTOR_SIZE];
+ memset(expected, 0xcc, sizeof(expected));
+ char buffer[LP_SECTOR_SIZE];
+ ASSERT_GE(lseek(fd, exported->geometry.first_logical_sector * LP_SECTOR_SIZE, SEEK_SET), 0);
+ ASSERT_TRUE(android::base::ReadFully(fd, buffer, sizeof(buffer)));
+ EXPECT_EQ(memcmp(expected, buffer, LP_SECTOR_SIZE), 0);
+ ASSERT_GE(lseek(fd, exported->geometry.last_logical_sector * LP_SECTOR_SIZE, SEEK_SET), 0);
+ ASSERT_TRUE(android::base::ReadFully(fd, buffer, sizeof(buffer)));
+ EXPECT_EQ(memcmp(expected, buffer, LP_SECTOR_SIZE), 0);
+}
+
+// Test that we can read and write image files.
+TEST(liblp, ImageFiles) {
+ unique_ptr<MetadataBuilder> builder = CreateDefaultBuilder();
+ ASSERT_NE(builder, nullptr);
+ ASSERT_TRUE(AddDefaultPartitions(builder.get()));
+ unique_ptr<LpMetadata> exported = builder->Export();
+
+ unique_fd fd(syscall(__NR_memfd_create, "image_file", 0));
+ ASSERT_GE(fd, 0);
+ ASSERT_TRUE(WriteToImageFile(fd, *exported.get()));
+
+ unique_ptr<LpMetadata> imported = ReadFromImageFile(fd);
+ ASSERT_NE(imported, nullptr);
+}
+
+class BadWriter {
+ public:
+ // When requested, write garbage instead of the requested bytes, then
+ // return false.
+ bool operator()(int fd, const std::string& blob) {
+ write_count_++;
+ if (write_count_ == fail_on_write_) {
+ std::unique_ptr<char[]> new_data = std::make_unique<char[]>(blob.size());
+ memset(new_data.get(), 0xe5, blob.size());
+ EXPECT_TRUE(android::base::WriteFully(fd, new_data.get(), blob.size()));
+ return false;
+ } else {
+ if (!android::base::WriteFully(fd, blob.data(), blob.size())) {
+ return false;
+ }
+ return fail_after_write_ != write_count_;
+ }
+ }
+ void Reset() {
+ fail_on_write_ = 0;
+ fail_after_write_ = 0;
+ write_count_ = 0;
+ }
+ void FailOnWrite(int number) {
+ Reset();
+ fail_on_write_ = number;
+ }
+ void FailAfterWrite(int number) {
+ Reset();
+ fail_after_write_ = number;
+ }
+
+ private:
+ int fail_on_write_ = 0;
+ int fail_after_write_ = 0;
+ int write_count_ = 0;
+};
+
+// Test that an interrupted flash operation on the "primary" copy of metadata
+// is not fatal.
+TEST(liblp, UpdatePrimaryMetadataFailure) {
+ unique_fd fd = CreateFlashedDisk();
+ ASSERT_GE(fd, 0);
+
+ BadWriter writer;
+
+ // Read and write it back.
+ writer.FailOnWrite(1);
+ unique_ptr<LpMetadata> imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+ ASSERT_FALSE(UpdatePartitionTable(fd, *imported.get(), 0, writer));
+
+ // We should still be able to read the backup copy.
+ imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+
+ // Flash again, this time fail the backup copy. We should still be able
+ // to read the primary.
+ writer.FailOnWrite(3);
+ ASSERT_FALSE(UpdatePartitionTable(fd, *imported.get(), 0, writer));
+ imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+}
+
+// Test that an interrupted flash operation on the "backup" copy of metadata
+// is not fatal.
+TEST(liblp, UpdateBackupMetadataFailure) {
+ unique_fd fd = CreateFlashedDisk();
+ ASSERT_GE(fd, 0);
+
+ BadWriter writer;
+
+ // Read and write it back.
+ writer.FailOnWrite(2);
+ unique_ptr<LpMetadata> imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+ ASSERT_FALSE(UpdatePartitionTable(fd, *imported.get(), 0, writer));
+
+ // We should still be able to read the primary copy.
+ imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+
+ // Flash again, this time fail the primary copy. We should still be able
+ // to read the primary.
+ writer.FailOnWrite(2);
+ ASSERT_FALSE(UpdatePartitionTable(fd, *imported.get(), 0, writer));
+ imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+}
+
+// Test that an interrupted write *in between* writing metadata will read
+// the correct metadata copy. The primary is always considered newer than
+// the backup.
+TEST(liblp, UpdateMetadataCleanFailure) {
+ unique_fd fd = CreateFlashedDisk();
+ ASSERT_GE(fd, 0);
+
+ BadWriter writer;
+
+ // Change the name of the existing partition.
+ unique_ptr<LpMetadata> new_table = ReadMetadata(fd, 0);
+ ASSERT_NE(new_table, nullptr);
+ ASSERT_GE(new_table->partitions.size(), 1);
+ new_table->partitions[0].name[0]++;
+
+ // Flash it, but fail to write the backup copy.
+ writer.FailAfterWrite(2);
+ ASSERT_FALSE(UpdatePartitionTable(fd, *new_table.get(), 0, writer));
+
+ // When we read back, we should get the updated primary copy.
+ unique_ptr<LpMetadata> imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+ ASSERT_GE(new_table->partitions.size(), 1);
+ ASSERT_EQ(GetPartitionName(new_table->partitions[0]), GetPartitionName(imported->partitions[0]));
+
+ // Flash again. After, the backup and primary copy should be coherent.
+ // Note that the sync step should have used the primary to sync, not
+ // the backup.
+ writer.Reset();
+ ASSERT_TRUE(UpdatePartitionTable(fd, *new_table.get(), 0, writer));
+
+ imported = ReadMetadata(fd, 0);
+ ASSERT_NE(imported, nullptr);
+ ASSERT_GE(new_table->partitions.size(), 1);
+ ASSERT_EQ(GetPartitionName(new_table->partitions[0]), GetPartitionName(imported->partitions[0]));
+}
diff --git a/fs_mgr/liblp/reader.cpp b/fs_mgr/liblp/reader.cpp
new file mode 100644
index 0000000..117da59
--- /dev/null
+++ b/fs_mgr/liblp/reader.cpp
@@ -0,0 +1,304 @@
+/*
+ * Copyright (C) 2018 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 "reader.h"
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <functional>
+
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+
+#include "utility.h"
+
+namespace android {
+namespace fs_mgr {
+
+// Parse an LpMetadataGeometry from a buffer. The buffer must be at least
+// LP_METADATA_GEOMETRY_SIZE bytes in size.
+static bool ParseGeometry(const void* buffer, LpMetadataGeometry* geometry) {
+ static_assert(sizeof(*geometry) <= LP_METADATA_GEOMETRY_SIZE);
+ memcpy(geometry, buffer, sizeof(*geometry));
+
+ // Check the magic signature.
+ if (geometry->magic != LP_METADATA_GEOMETRY_MAGIC) {
+ LERROR << "Logical partition metadata has invalid geometry magic signature.";
+ return false;
+ }
+ // Reject if the struct size is larger than what we compiled. This is so we
+ // can compute a checksum with the |struct_size| field rather than using
+ // sizeof.
+ if (geometry->struct_size > sizeof(LpMetadataGeometry)) {
+ LERROR << "Logical partition metadata has unrecognized fields.";
+ return false;
+ }
+ // Recompute and check the CRC32.
+ {
+ LpMetadataGeometry temp = *geometry;
+ memset(&temp.checksum, 0, sizeof(temp.checksum));
+ SHA256(&temp, temp.struct_size, temp.checksum);
+ if (memcmp(temp.checksum, geometry->checksum, sizeof(temp.checksum)) != 0) {
+ LERROR << "Logical partition metadata has invalid geometry checksum.";
+ return false;
+ }
+ }
+ // Check that the struct size is equal (this will have to change if we ever
+ // change the struct size in a release).
+ if (geometry->struct_size != sizeof(LpMetadataGeometry)) {
+ LERROR << "Logical partition metadata has invalid struct size.";
+ return false;
+ }
+ if (geometry->metadata_slot_count == 0) {
+ LERROR << "Logical partition metadata has invalid slot count.";
+ return false;
+ }
+ if (geometry->metadata_max_size % LP_SECTOR_SIZE != 0) {
+ LERROR << "Metadata max size is not sector-aligned.";
+ return false;
+ }
+
+ // Check that the metadata area and logical partition areas don't overlap.
+ int64_t end_of_metadata =
+ GetPrimaryMetadataOffset(*geometry, geometry->metadata_slot_count - 1) +
+ geometry->metadata_max_size;
+ if (uint64_t(end_of_metadata) > geometry->first_logical_sector * LP_SECTOR_SIZE) {
+ LERROR << "Logical partition metadata overlaps with logical partition contents.";
+ return false;
+ }
+ return true;
+}
+
+// Read and validate geometry information from a block device that holds
+// logical partitions. If the information is corrupted, this will attempt
+// to read it from a secondary backup location.
+bool ReadLogicalPartitionGeometry(int fd, LpMetadataGeometry* geometry) {
+ // Read the first 4096 bytes.
+ std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(LP_METADATA_GEOMETRY_SIZE);
+ if (SeekFile64(fd, 0, SEEK_SET) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "lseek failed";
+ return false;
+ }
+ if (!android::base::ReadFully(fd, buffer.get(), LP_METADATA_GEOMETRY_SIZE)) {
+ PERROR << __PRETTY_FUNCTION__ << "read " << LP_METADATA_GEOMETRY_SIZE << " bytes failed";
+ return false;
+ }
+ if (ParseGeometry(buffer.get(), geometry)) {
+ return true;
+ }
+
+ // Try the backup copy in the last 4096 bytes.
+ if (SeekFile64(fd, -LP_METADATA_GEOMETRY_SIZE, SEEK_END) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "lseek failed, offset " << -LP_METADATA_GEOMETRY_SIZE;
+ return false;
+ }
+ if (!android::base::ReadFully(fd, buffer.get(), LP_METADATA_GEOMETRY_SIZE)) {
+ PERROR << __PRETTY_FUNCTION__ << "backup read " << LP_METADATA_GEOMETRY_SIZE
+ << " bytes failed";
+ return false;
+ }
+ return ParseGeometry(buffer.get(), geometry);
+}
+
+static bool ValidateTableBounds(const LpMetadataHeader& header,
+ const LpMetadataTableDescriptor& table) {
+ if (table.offset > header.tables_size) {
+ return false;
+ }
+ uint64_t table_size = uint64_t(table.num_entries) * table.entry_size;
+ if (header.tables_size - table.offset < table_size) {
+ return false;
+ }
+ return true;
+}
+
+static bool ValidateMetadataHeader(const LpMetadataHeader& header) {
+ // To compute the header's checksum, we have to temporarily set its checksum
+ // field to 0.
+ {
+ LpMetadataHeader temp = header;
+ memset(&temp.header_checksum, 0, sizeof(temp.header_checksum));
+ SHA256(&temp, sizeof(temp), temp.header_checksum);
+ if (memcmp(temp.header_checksum, header.header_checksum, sizeof(temp.header_checksum)) != 0) {
+ LERROR << "Logical partition metadata has invalid checksum.";
+ return false;
+ }
+ }
+
+ // Do basic validation of key metadata bits.
+ if (header.magic != LP_METADATA_HEADER_MAGIC) {
+ LERROR << "Logical partition metadata has invalid magic value.";
+ return false;
+ }
+ // Check that the version is compatible.
+ if (header.major_version != LP_METADATA_MAJOR_VERSION ||
+ header.minor_version > LP_METADATA_MINOR_VERSION) {
+ LERROR << "Logical partition metadata has incompatible version.";
+ return false;
+ }
+ if (!ValidateTableBounds(header, header.partitions) ||
+ !ValidateTableBounds(header, header.extents)) {
+ LERROR << "Logical partition metadata has invalid table bounds.";
+ return false;
+ }
+ // Check that table entry sizes can accomodate their respective structs. If
+ // table sizes change, these checks will have to be adjusted.
+ if (header.partitions.entry_size != sizeof(LpMetadataPartition)) {
+ LERROR << "Logical partition metadata has invalid partition table entry size.";
+ return false;
+ }
+ if (header.extents.entry_size != sizeof(LpMetadataExtent)) {
+ LERROR << "Logical partition metadata has invalid extent table entry size.";
+ return false;
+ }
+ return true;
+}
+
+// Parse and validate all metadata at the current position in the given file
+// descriptor.
+std::unique_ptr<LpMetadata> ParseMetadata(int fd) {
+ // First read and validate the header.
+ std::unique_ptr<LpMetadata> metadata = std::make_unique<LpMetadata>();
+ if (!android::base::ReadFully(fd, &metadata->header, sizeof(metadata->header))) {
+ PERROR << __PRETTY_FUNCTION__ << "read " << sizeof(metadata->header) << "bytes failed";
+ return nullptr;
+ }
+ if (!ValidateMetadataHeader(metadata->header)) {
+ return nullptr;
+ }
+
+ LpMetadataHeader& header = metadata->header;
+
+ // Read the metadata payload. Allocation is fallible in case the metadata is
+ // corrupt and has some huge value.
+ std::unique_ptr<uint8_t[]> buffer(new (std::nothrow) uint8_t[header.tables_size]);
+ if (!buffer) {
+ LERROR << "Out of memory reading logical partition tables.";
+ return nullptr;
+ }
+ if (!android::base::ReadFully(fd, buffer.get(), header.tables_size)) {
+ PERROR << __PRETTY_FUNCTION__ << "read " << header.tables_size << "bytes failed";
+ return nullptr;
+ }
+
+ uint8_t checksum[32];
+ SHA256(buffer.get(), header.tables_size, checksum);
+ if (memcmp(checksum, header.tables_checksum, sizeof(checksum)) != 0) {
+ LERROR << "Logical partition metadata has invalid table checksum.";
+ return nullptr;
+ }
+
+ // ValidateTableSize ensured that |cursor| is valid for the number of
+ // entries in the table.
+ uint8_t* cursor = buffer.get() + header.partitions.offset;
+ for (size_t i = 0; i < header.partitions.num_entries; i++) {
+ LpMetadataPartition partition;
+ memcpy(&partition, cursor, sizeof(partition));
+ cursor += header.partitions.entry_size;
+
+ if (partition.attributes & ~LP_PARTITION_ATTRIBUTE_MASK) {
+ LERROR << "Logical partition has invalid attribute set.";
+ return nullptr;
+ }
+ if (partition.first_extent_index + partition.num_extents > header.extents.num_entries) {
+ LERROR << "Logical partition has invalid extent list.";
+ return nullptr;
+ }
+
+ metadata->partitions.push_back(partition);
+ }
+
+ cursor = buffer.get() + header.extents.offset;
+ for (size_t i = 0; i < header.extents.num_entries; i++) {
+ LpMetadataExtent extent;
+ memcpy(&extent, cursor, sizeof(extent));
+ cursor += header.extents.entry_size;
+
+ metadata->extents.push_back(extent);
+ }
+
+ return metadata;
+}
+
+std::unique_ptr<LpMetadata> ReadPrimaryMetadata(int fd, const LpMetadataGeometry& geometry,
+ uint32_t slot_number) {
+ int64_t offset = GetPrimaryMetadataOffset(geometry, slot_number);
+ if (SeekFile64(fd, offset, SEEK_SET) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << offset;
+ return nullptr;
+ }
+ return ParseMetadata(fd);
+}
+
+std::unique_ptr<LpMetadata> ReadBackupMetadata(int fd, const LpMetadataGeometry& geometry,
+ uint32_t slot_number) {
+ int64_t offset = GetBackupMetadataOffset(geometry, slot_number);
+ if (SeekFile64(fd, offset, SEEK_END) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << offset;
+ return nullptr;
+ }
+ return ParseMetadata(fd);
+}
+
+std::unique_ptr<LpMetadata> ReadMetadata(int fd, uint32_t slot_number) {
+ LpMetadataGeometry geometry;
+ if (!ReadLogicalPartitionGeometry(fd, &geometry)) {
+ return nullptr;
+ }
+
+ if (slot_number >= geometry.metadata_slot_count) {
+ LERROR << __PRETTY_FUNCTION__ << "invalid metadata slot number";
+ return nullptr;
+ }
+
+ // Read the priamry copy, and if that fails, try the backup.
+ std::unique_ptr<LpMetadata> metadata = ReadPrimaryMetadata(fd, geometry, slot_number);
+ if (!metadata) {
+ metadata = ReadBackupMetadata(fd, geometry, slot_number);
+ }
+ if (metadata) {
+ metadata->geometry = geometry;
+ }
+ return metadata;
+}
+
+std::unique_ptr<LpMetadata> ReadMetadata(const char* block_device, uint32_t slot_number) {
+ android::base::unique_fd fd(open(block_device, O_RDONLY));
+ if (fd < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "open failed: " << block_device;
+ return nullptr;
+ }
+ return ReadMetadata(fd, slot_number);
+}
+
+static std::string NameFromFixedArray(const char* name, size_t buffer_size) {
+ // If the end of the buffer has a null character, it's safe to assume the
+ // buffer is null terminated. Otherwise, we cap the string to the input
+ // buffer size.
+ if (name[buffer_size - 1] == '\0') {
+ return std::string(name);
+ }
+ return std::string(name, buffer_size);
+}
+
+std::string GetPartitionName(const LpMetadataPartition& partition) {
+ return NameFromFixedArray(partition.name, sizeof(partition.name));
+}
+
+} // namespace fs_mgr
+} // namespace android
diff --git a/fs_mgr/liblp/reader.h b/fs_mgr/liblp/reader.h
new file mode 100644
index 0000000..843b2f2
--- /dev/null
+++ b/fs_mgr/liblp/reader.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef LIBLP_READER_H_
+#define LIBLP_READER_H_
+
+#include <stddef.h>
+
+#include <memory>
+
+#include <liblp/liblp.h>
+
+namespace android {
+namespace fs_mgr {
+
+std::unique_ptr<LpMetadata> ReadMetadata(int fd, uint32_t slot_number);
+
+// Helper functions for manually reading geometry and metadata.
+bool ReadLogicalPartitionGeometry(int fd, LpMetadataGeometry* geometry);
+std::unique_ptr<LpMetadata> ParseMetadata(int fd);
+
+// These functions assume a valid geometry and slot number.
+std::unique_ptr<LpMetadata> ReadPrimaryMetadata(int fd, const LpMetadataGeometry& geometry,
+ uint32_t slot_number);
+std::unique_ptr<LpMetadata> ReadBackupMetadata(int fd, const LpMetadataGeometry& geometry,
+ uint32_t slot_number);
+
+} // namespace fs_mgr
+} // namespace android
+
+#endif /* LIBLP_READER_H_ */
diff --git a/fs_mgr/liblp/utility.cpp b/fs_mgr/liblp/utility.cpp
new file mode 100644
index 0000000..a590037
--- /dev/null
+++ b/fs_mgr/liblp/utility.cpp
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2018 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 <fcntl.h>
+#include <stdint.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <android-base/file.h>
+#include <ext4_utils/ext4_utils.h>
+#include <openssl/sha.h>
+#include <uuid/uuid.h>
+
+#include "utility.h"
+
+namespace android {
+namespace fs_mgr {
+
+bool GetDescriptorSize(int fd, uint64_t* size) {
+ struct stat s;
+ if (fstat(fd, &s) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "fstat failed";
+ return false;
+ }
+
+ if (S_ISBLK(s.st_mode)) {
+ *size = get_block_device_size(fd);
+ return *size != 0;
+ }
+
+ int64_t result = SeekFile64(fd, 0, SEEK_END);
+ if (result == -1) {
+ PERROR << __PRETTY_FUNCTION__ << "lseek failed";
+ return false;
+ }
+
+ *size = result;
+ return true;
+}
+
+int64_t SeekFile64(int fd, int64_t offset, int whence) {
+ static_assert(sizeof(off_t) == sizeof(int64_t), "Need 64-bit lseek");
+ return lseek(fd, offset, whence);
+}
+
+int64_t GetPrimaryMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number) {
+ CHECK(slot_number < geometry.metadata_slot_count);
+
+ int64_t offset = LP_METADATA_GEOMETRY_SIZE + geometry.metadata_max_size * slot_number;
+ CHECK(offset + geometry.metadata_max_size <=
+ int64_t(geometry.first_logical_sector * LP_SECTOR_SIZE));
+ return offset;
+}
+
+int64_t GetBackupMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number) {
+ CHECK(slot_number < geometry.metadata_slot_count);
+ int64_t start = int64_t(-LP_METADATA_GEOMETRY_SIZE) -
+ int64_t(geometry.metadata_max_size) * geometry.metadata_slot_count;
+ return start + int64_t(geometry.metadata_max_size * slot_number);
+}
+
+void SHA256(const void* data, size_t length, uint8_t out[32]) {
+ SHA256_CTX c;
+ SHA256_Init(&c);
+ SHA256_Update(&c, data, length);
+ SHA256_Final(out, &c);
+}
+
+std::string GetPartitionGuid(const LpMetadataPartition& partition) {
+ // 32 hex characters, four hyphens. Unfortunately libext2_uuid provides no
+ // macro to assist with buffer sizing.
+ static const size_t kGuidLen = 36;
+ char buffer[kGuidLen + 1];
+ uuid_unparse_upper(partition.guid, buffer);
+ return buffer;
+}
+
+uint32_t SlotNumberForSlotSuffix(const std::string& suffix) {
+ if (suffix.empty()) {
+ return 0;
+ }
+ if (suffix.size() != 2 || suffix[0] != '_' || suffix[1] < 'a') {
+ LERROR << __PRETTY_FUNCTION__ << "slot '" << suffix
+ << "' does not have a recognized format.";
+ return 0;
+ }
+ return suffix[1] - 'a';
+}
+
+} // namespace fs_mgr
+} // namespace android
diff --git a/fs_mgr/liblp/utility.h b/fs_mgr/liblp/utility.h
new file mode 100644
index 0000000..6ef5124
--- /dev/null
+++ b/fs_mgr/liblp/utility.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef LIBLP_UTILITY_H
+#define LIBLP_UTILITY_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+#include <android-base/logging.h>
+
+#include "liblp/metadata_format.h"
+
+#define LP_TAG "[liblp]"
+#define LWARN LOG(WARNING) << LP_TAG
+#define LINFO LOG(INFO) << LP_TAG
+#define LERROR LOG(ERROR) << LP_TAG
+#define PERROR PLOG(ERROR) << LP_TAG
+
+namespace android {
+namespace fs_mgr {
+
+// Determine the size of a block device (or file). Logs and returns false on
+// error. After calling this, the position of |fd| may have changed.
+bool GetDescriptorSize(int fd, uint64_t* size);
+
+// Return the offset of a primary metadata slot, relative to the start of the
+// device.
+int64_t GetPrimaryMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number);
+
+// Return the offset of a backup metadata slot, relative to the end of the
+// device.
+int64_t GetBackupMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number);
+
+// Cross-platform helper for lseek64().
+int64_t SeekFile64(int fd, int64_t offset, int whence);
+
+// Compute a SHA256 hash.
+void SHA256(const void* data, size_t length, uint8_t out[32]);
+
+// Align |base| such that it is evenly divisible by |alignment|, which does not
+// have to be a power of two.
+constexpr uint64_t AlignTo(uint64_t base, uint32_t alignment) {
+ if (!alignment) {
+ return base;
+ }
+ uint64_t remainder = base % alignment;
+ if (remainder == 0) {
+ return base;
+ }
+ return base + (alignment - remainder);
+}
+
+// Same as the above |AlignTo|, except that |base| is only aligned when added to
+// |alignment_offset|.
+constexpr uint64_t AlignTo(uint64_t base, uint32_t alignment, uint32_t alignment_offset) {
+ uint64_t aligned = AlignTo(base, alignment) + alignment_offset;
+ if (aligned - alignment >= base) {
+ // We overaligned (base < alignment_offset).
+ return aligned - alignment;
+ }
+ return aligned;
+}
+
+} // namespace fs_mgr
+} // namespace android
+
+#endif // LIBLP_UTILITY_H
diff --git a/fs_mgr/liblp/utility_test.cpp b/fs_mgr/liblp/utility_test.cpp
new file mode 100644
index 0000000..7bf42ae
--- /dev/null
+++ b/fs_mgr/liblp/utility_test.cpp
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2018 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 <gtest/gtest.h>
+#include <liblp/liblp.h>
+
+#include "utility.h"
+
+using namespace android;
+using namespace android::fs_mgr;
+
+TEST(liblp, SlotNumberForSlotSuffix) {
+ EXPECT_EQ(SlotNumberForSlotSuffix(""), 0);
+ EXPECT_EQ(SlotNumberForSlotSuffix("_a"), 0);
+ EXPECT_EQ(SlotNumberForSlotSuffix("_b"), 1);
+ EXPECT_EQ(SlotNumberForSlotSuffix("_c"), 2);
+ EXPECT_EQ(SlotNumberForSlotSuffix("_d"), 3);
+}
+
+TEST(liblp, GetMetadataOffset) {
+ LpMetadataGeometry geometry = {LP_METADATA_GEOMETRY_MAGIC,
+ sizeof(geometry),
+ {0},
+ 16384,
+ 4,
+ 10000,
+ 80000,
+ 0,
+ 0,
+ 1024 * 1024,
+ 4096};
+ EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 0), 4096);
+ EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 1), 4096 + 16384);
+ EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 2), 4096 + 16384 * 2);
+ EXPECT_EQ(GetPrimaryMetadataOffset(geometry, 3), 4096 + 16384 * 3);
+
+ EXPECT_EQ(GetBackupMetadataOffset(geometry, 3), -4096 - 16384 * 1);
+ EXPECT_EQ(GetBackupMetadataOffset(geometry, 2), -4096 - 16384 * 2);
+ EXPECT_EQ(GetBackupMetadataOffset(geometry, 1), -4096 - 16384 * 3);
+ EXPECT_EQ(GetBackupMetadataOffset(geometry, 0), -4096 - 16384 * 4);
+}
+
+TEST(liblp, AlignTo) {
+ EXPECT_EQ(AlignTo(37, 0), 37);
+ EXPECT_EQ(AlignTo(1024, 1024), 1024);
+ EXPECT_EQ(AlignTo(555, 1024), 1024);
+ EXPECT_EQ(AlignTo(555, 1000), 1000);
+ EXPECT_EQ(AlignTo(0, 1024), 0);
+ EXPECT_EQ(AlignTo(54, 32, 30), 62);
+ EXPECT_EQ(AlignTo(32, 32, 30), 62);
+ EXPECT_EQ(AlignTo(17, 32, 30), 30);
+}
diff --git a/fs_mgr/liblp/writer.cpp b/fs_mgr/liblp/writer.cpp
new file mode 100644
index 0000000..fc9d83f
--- /dev/null
+++ b/fs_mgr/liblp/writer.cpp
@@ -0,0 +1,334 @@
+/*
+ * Copyright (C) 2007 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 "writer.h"
+
+#include <inttypes.h>
+#include <unistd.h>
+
+#include <string>
+
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+
+#include "reader.h"
+#include "utility.h"
+
+namespace android {
+namespace fs_mgr {
+
+std::string SerializeGeometry(const LpMetadataGeometry& input) {
+ LpMetadataGeometry geometry = input;
+ memset(geometry.checksum, 0, sizeof(geometry.checksum));
+ SHA256(&geometry, sizeof(geometry), geometry.checksum);
+
+ std::string blob(reinterpret_cast<const char*>(&geometry), sizeof(geometry));
+ blob.resize(LP_METADATA_GEOMETRY_SIZE);
+ return blob;
+}
+
+static bool CompareGeometry(const LpMetadataGeometry& g1, const LpMetadataGeometry& g2) {
+ return g1.metadata_max_size == g2.metadata_max_size &&
+ g1.metadata_slot_count == g2.metadata_slot_count &&
+ g1.first_logical_sector == g2.first_logical_sector &&
+ g1.last_logical_sector == g2.last_logical_sector;
+}
+
+std::string SerializeMetadata(const LpMetadata& input) {
+ LpMetadata metadata = input;
+ LpMetadataHeader& header = metadata.header;
+
+ // Serialize individual tables.
+ std::string partitions(reinterpret_cast<const char*>(metadata.partitions.data()),
+ metadata.partitions.size() * sizeof(LpMetadataPartition));
+ std::string extents(reinterpret_cast<const char*>(metadata.extents.data()),
+ metadata.extents.size() * sizeof(LpMetadataExtent));
+
+ // Compute positions of tables.
+ header.partitions.offset = 0;
+ header.extents.offset = header.partitions.offset + partitions.size();
+ header.tables_size = header.extents.offset + extents.size();
+
+ // Compute payload checksum.
+ std::string tables = partitions + extents;
+ SHA256(tables.data(), tables.size(), header.tables_checksum);
+
+ // Compute header checksum.
+ memset(header.header_checksum, 0, sizeof(header.header_checksum));
+ SHA256(&header, sizeof(header), header.header_checksum);
+
+ std::string header_blob =
+ std::string(reinterpret_cast<const char*>(&metadata.header), sizeof(metadata.header));
+ return header_blob + tables;
+}
+
+// Perform sanity checks so we don't accidentally overwrite valid metadata
+// with potentially invalid metadata, or random partition data with metadata.
+static bool ValidateAndSerializeMetadata(int fd, const LpMetadata& metadata, std::string* blob) {
+ uint64_t blockdevice_size;
+ if (!GetDescriptorSize(fd, &blockdevice_size)) {
+ return false;
+ }
+
+ *blob = SerializeMetadata(metadata);
+
+ const LpMetadataHeader& header = metadata.header;
+ const LpMetadataGeometry& geometry = metadata.geometry;
+ // Validate the usable sector range.
+ if (geometry.first_logical_sector > geometry.last_logical_sector) {
+ LERROR << "Logical partition metadata has invalid sector range.";
+ return false;
+ }
+ // Make sure we're writing within the space reserved.
+ if (blob->size() > geometry.metadata_max_size) {
+ LERROR << "Logical partition metadata is too large.";
+ return false;
+ }
+
+ // Make sure the device has enough space to store two backup copies of the
+ // metadata.
+ uint64_t reserved_size = LP_METADATA_GEOMETRY_SIZE +
+ uint64_t(geometry.metadata_max_size) * geometry.metadata_slot_count;
+ if (reserved_size > blockdevice_size ||
+ reserved_size > geometry.first_logical_sector * LP_SECTOR_SIZE) {
+ LERROR << "Not enough space to store all logical partition metadata slots.";
+ return false;
+ }
+ if (blockdevice_size - reserved_size < (geometry.last_logical_sector + 1) * LP_SECTOR_SIZE) {
+ LERROR << "Not enough space to backup all logical partition metadata slots.";
+ return false;
+ }
+ if (blockdevice_size != metadata.geometry.block_device_size) {
+ LERROR << "Block device size " << blockdevice_size
+ << " does not match metadata requested size " << metadata.geometry.block_device_size;
+ return false;
+ }
+
+ // Make sure all partition entries reference valid extents.
+ for (const auto& partition : metadata.partitions) {
+ if (partition.first_extent_index + partition.num_extents > metadata.extents.size()) {
+ LERROR << "Partition references invalid extent.";
+ return false;
+ }
+ }
+
+ // Make sure all linear extents have a valid range.
+ for (const auto& extent : metadata.extents) {
+ if (extent.target_type == LP_TARGET_TYPE_LINEAR) {
+ uint64_t physical_sector = extent.target_data;
+ if (physical_sector < geometry.first_logical_sector ||
+ physical_sector + extent.num_sectors > geometry.last_logical_sector) {
+ LERROR << "Extent table entry is out of bounds.";
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+static bool WritePrimaryMetadata(int fd, const LpMetadataGeometry& geometry, uint32_t slot_number,
+ const std::string& blob,
+ const std::function<bool(int, const std::string&)>& writer) {
+ int64_t primary_offset = GetPrimaryMetadataOffset(geometry, slot_number);
+ if (SeekFile64(fd, primary_offset, SEEK_SET) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << primary_offset;
+ return false;
+ }
+ if (!writer(fd, blob)) {
+ PERROR << __PRETTY_FUNCTION__ << "write " << blob.size() << " bytes failed";
+ return false;
+ }
+ return true;
+}
+
+static bool WriteBackupMetadata(int fd, const LpMetadataGeometry& geometry, uint32_t slot_number,
+ const std::string& blob,
+ const std::function<bool(int, const std::string&)>& writer) {
+ int64_t backup_offset = GetBackupMetadataOffset(geometry, slot_number);
+ int64_t abs_offset = SeekFile64(fd, backup_offset, SEEK_END);
+ if (abs_offset == (int64_t)-1) {
+ PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << backup_offset;
+ return false;
+ }
+ if (abs_offset < int64_t((geometry.last_logical_sector + 1) * LP_SECTOR_SIZE)) {
+ PERROR << __PRETTY_FUNCTION__ << "backup offset " << abs_offset
+ << " is within logical partition bounds, sector " << geometry.last_logical_sector;
+ return false;
+ }
+ if (!writer(fd, blob)) {
+ PERROR << __PRETTY_FUNCTION__ << "backup write " << blob.size() << " bytes failed";
+ return false;
+ }
+ return true;
+}
+
+static bool WriteMetadata(int fd, const LpMetadataGeometry& geometry, uint32_t slot_number,
+ const std::string& blob,
+ const std::function<bool(int, const std::string&)>& writer) {
+ // Make sure we're writing to a valid metadata slot.
+ if (slot_number >= geometry.metadata_slot_count) {
+ LERROR << "Invalid logical partition metadata slot number.";
+ return false;
+ }
+ if (!WritePrimaryMetadata(fd, geometry, slot_number, blob, writer)) {
+ return false;
+ }
+ if (!WriteBackupMetadata(fd, geometry, slot_number, blob, writer)) {
+ return false;
+ }
+ return true;
+}
+
+static bool DefaultWriter(int fd, const std::string& blob) {
+ return android::base::WriteFully(fd, blob.data(), blob.size());
+}
+
+bool FlashPartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number) {
+ // Before writing geometry and/or logical partition tables, perform some
+ // basic checks that the geometry and tables are coherent, and will fit
+ // on the given block device.
+ std::string metadata_blob;
+ if (!ValidateAndSerializeMetadata(fd, metadata, &metadata_blob)) {
+ return false;
+ }
+
+ // Write geometry to the first and last 4096 bytes of the device.
+ std::string blob = SerializeGeometry(metadata.geometry);
+ if (SeekFile64(fd, 0, SEEK_SET) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset 0";
+ return false;
+ }
+ if (!android::base::WriteFully(fd, blob.data(), blob.size())) {
+ PERROR << __PRETTY_FUNCTION__ << "write " << blob.size() << " bytes failed";
+ return false;
+ }
+ if (SeekFile64(fd, -LP_METADATA_GEOMETRY_SIZE, SEEK_END) < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << -LP_METADATA_GEOMETRY_SIZE;
+ return false;
+ }
+ if (!android::base::WriteFully(fd, blob.data(), blob.size())) {
+ PERROR << __PRETTY_FUNCTION__ << "backup write " << blob.size() << " bytes failed";
+ return false;
+ }
+
+ // Write metadata to the correct slot, now that geometry is in place.
+ return WriteMetadata(fd, metadata.geometry, slot_number, metadata_blob, DefaultWriter);
+}
+
+static bool CompareMetadata(const LpMetadata& a, const LpMetadata& b) {
+ return !memcmp(a.header.header_checksum, b.header.header_checksum,
+ sizeof(a.header.header_checksum));
+}
+
+bool UpdatePartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number,
+ const std::function<bool(int, const std::string&)>& writer) {
+ // Before writing geometry and/or logical partition tables, perform some
+ // basic checks that the geometry and tables are coherent, and will fit
+ // on the given block device.
+ std::string blob;
+ if (!ValidateAndSerializeMetadata(fd, metadata, &blob)) {
+ return false;
+ }
+
+ // Verify that the old geometry is identical. If it's not, then we might be
+ // writing a table that was built for a different device, so we must reject
+ // it.
+ const LpMetadataGeometry& geometry = metadata.geometry;
+ LpMetadataGeometry old_geometry;
+ if (!ReadLogicalPartitionGeometry(fd, &old_geometry)) {
+ return false;
+ }
+ if (!CompareGeometry(geometry, old_geometry)) {
+ LERROR << "Incompatible geometry in new logical partition metadata";
+ return false;
+ }
+
+ // Validate the slot number now, before we call Read*Metadata.
+ if (slot_number >= geometry.metadata_slot_count) {
+ LERROR << "Invalid logical partition metadata slot number.";
+ return false;
+ }
+
+ // Try to read both existing copies of the metadata, if any.
+ std::unique_ptr<LpMetadata> primary = ReadPrimaryMetadata(fd, geometry, slot_number);
+ std::unique_ptr<LpMetadata> backup = ReadBackupMetadata(fd, geometry, slot_number);
+
+ if (primary && (!backup || !CompareMetadata(*primary.get(), *backup.get()))) {
+ // If the backup copy does not match the primary copy, we first
+ // synchronize the backup copy. This guarantees that a partial write
+ // still leaves one copy intact.
+ std::string old_blob;
+ if (!ValidateAndSerializeMetadata(fd, *primary.get(), &old_blob)) {
+ LERROR << "Error serializing primary metadata to repair corrupted backup";
+ return false;
+ }
+ if (!WriteBackupMetadata(fd, geometry, slot_number, old_blob, writer)) {
+ LERROR << "Error writing primary metadata to repair corrupted backup";
+ return false;
+ }
+ } else if (backup && !primary) {
+ // The backup copy is coherent, and the primary is not. Sync it for
+ // safety.
+ std::string old_blob;
+ if (!ValidateAndSerializeMetadata(fd, *backup.get(), &old_blob)) {
+ LERROR << "Error serializing primary metadata to repair corrupted backup";
+ return false;
+ }
+ if (!WritePrimaryMetadata(fd, geometry, slot_number, old_blob, writer)) {
+ LERROR << "Error writing primary metadata to repair corrupted backup";
+ return false;
+ }
+ }
+
+ // Both copies should now be in sync, so we can continue the update.
+ return WriteMetadata(fd, geometry, slot_number, blob, writer);
+}
+
+bool FlashPartitionTable(const std::string& block_device, const LpMetadata& metadata,
+ uint32_t slot_number) {
+ android::base::unique_fd fd(open(block_device.c_str(), O_RDWR | O_SYNC));
+ if (fd < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "open failed: " << block_device;
+ return false;
+ }
+ if (!FlashPartitionTable(fd, metadata, slot_number)) {
+ return false;
+ }
+ LWARN << "Flashed new logical partition geometry to " << block_device;
+ return true;
+}
+
+bool UpdatePartitionTable(const std::string& block_device, const LpMetadata& metadata,
+ uint32_t slot_number) {
+ android::base::unique_fd fd(open(block_device.c_str(), O_RDWR | O_SYNC));
+ if (fd < 0) {
+ PERROR << __PRETTY_FUNCTION__ << "open failed: " << block_device;
+ return false;
+ }
+ if (!UpdatePartitionTable(fd, metadata, slot_number)) {
+ return false;
+ }
+ LINFO << "Updated logical partition table at slot " << slot_number << " on device "
+ << block_device;
+ return true;
+}
+
+bool UpdatePartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number) {
+ return UpdatePartitionTable(fd, metadata, slot_number, DefaultWriter);
+}
+
+} // namespace fs_mgr
+} // namespace android
diff --git a/fs_mgr/liblp/writer.h b/fs_mgr/liblp/writer.h
new file mode 100644
index 0000000..adbbebf
--- /dev/null
+++ b/fs_mgr/liblp/writer.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef LIBLP_WRITER_H
+#define LIBLP_WRITER_H
+
+#include <functional>
+#include <string>
+
+#include <liblp/liblp.h>
+
+namespace android {
+namespace fs_mgr {
+
+std::string SerializeGeometry(const LpMetadataGeometry& input);
+std::string SerializeMetadata(const LpMetadata& input);
+
+// These variants are for testing only. The path-based functions should be used
+// for actual operation, so that open() is called with the correct flags.
+bool FlashPartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number);
+bool UpdatePartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number);
+
+bool UpdatePartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number,
+ const std::function<bool(int, const std::string&)>& writer);
+
+} // namespace fs_mgr
+} // namespace android
+
+#endif /* LIBLP_WRITER_H */
diff --git a/fs_mgr/tests/Android.bp b/fs_mgr/tests/Android.bp
new file mode 100644
index 0000000..5497223
--- /dev/null
+++ b/fs_mgr/tests/Android.bp
@@ -0,0 +1,36 @@
+// Copyright (C) 2018 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.
+
+cc_test {
+ name: "fs_mgr_unit_test",
+
+ shared_libs: [
+ "libbase",
+ "liblog",
+ ],
+ static_libs: [
+ "libfs_mgr",
+ "libfstab",
+ ],
+
+ srcs: [
+ "fs_mgr_test.cpp",
+ ],
+
+ cflags: [
+ "-Wall",
+ "-Wextra",
+ "-Werror",
+ ],
+}
diff --git a/fs_mgr/tests/fs_mgr_test.cpp b/fs_mgr/tests/fs_mgr_test.cpp
new file mode 100644
index 0000000..8b1c55a
--- /dev/null
+++ b/fs_mgr/tests/fs_mgr_test.cpp
@@ -0,0 +1,203 @@
+/*
+ * Copyright (C) 2018 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 <linux/fs.h>
+#include <mntent.h>
+
+#include <algorithm>
+#include <iterator>
+#include <set>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include <android-base/strings.h>
+#include <fstab/fstab.h>
+#include <gtest/gtest.h>
+
+#include "../fs_mgr_priv_boot_config.h"
+
+namespace {
+
+const std::string cmdline =
+ "rcupdate.rcu_expedited=1 rootwait ro "
+ "init=/init androidboot.bootdevice=1d84000.ufshc "
+ "androidboot.baseband=sdy androidboot.keymaster=1 skip_initramfs "
+ "androidboot.serialno=BLAHBLAHBLAH androidboot.slot_suffix=_a "
+ "androidboot.hardware.platform=sdw813 androidboot.hardware=foo "
+ "androidboot.revision=EVT1.0 androidboot.bootloader=burp-0.1-7521 "
+ "androidboot.hardware.sku=mary androidboot.hardware.radio.subtype=0 "
+ "androidboot.dtbo_idx=2 androidboot.mode=normal "
+ "androidboot.hardware.ddr=1GB,combuchi,LPDDR4X "
+ "androidboot.ddr_info=combuchiandroidboot.ddr_size=2GB "
+ "androidboot.hardware.ufs=2GB,combushi "
+ "androidboot.boottime=0BLE:58,1BLL:22,1BLE:571,2BLL:105,ODT:0,AVB:123 "
+ "androidboot.ramdump=disabled "
+ "dm=\"1 vroot none ro 1,0 10416 verity 1 624684 fec_start 624684\" "
+ "root=/dev/dm-0 "
+ "androidboot.vbmeta.device=PARTUUID=aa08f1a4-c7c9-402e-9a66-9707cafa9ceb "
+ "androidboot.vbmeta.avb_version=\"1.1\" "
+ "androidboot.vbmeta.device_state=unlocked "
+ "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=5248 "
+ "androidboot.vbmeta.digest="
+ "ac13147e959861c20f2a6da97d25fe79e60e902c022a371c5c039d31e7c68860 "
+ "androidboot.vbmeta.invalidate_on_error=yes "
+ "androidboot.veritymode=enforcing androidboot.verifiedbootstate=orange "
+ "androidboot.space=\"sha256 5248 androidboot.nospace=nope\" "
+ "printk.devkmsg=on msm_rtb.filter=0x237 ehci-hcd.park=3 "
+ "\"string =\"\"string '\" "
+ "service_locator.enable=1 firmware_class.path=/vendor/firmware "
+ "cgroup.memory=nokmem lpm_levels.sleep_disabled=1 "
+ "buildvariant=userdebug console=null "
+ "terminator=\"truncated";
+
+const std::vector<std::pair<std::string, std::string>> result_space = {
+ {"rcupdate.rcu_expedited", "1"},
+ {"rootwait", ""},
+ {"ro", ""},
+ {"init", "/init"},
+ {"androidboot.bootdevice", "1d84000.ufshc"},
+ {"androidboot.baseband", "sdy"},
+ {"androidboot.keymaster", "1"},
+ {"skip_initramfs", ""},
+ {"androidboot.serialno", "BLAHBLAHBLAH"},
+ {"androidboot.slot_suffix", "_a"},
+ {"androidboot.hardware.platform", "sdw813"},
+ {"androidboot.hardware", "foo"},
+ {"androidboot.revision", "EVT1.0"},
+ {"androidboot.bootloader", "burp-0.1-7521"},
+ {"androidboot.hardware.sku", "mary"},
+ {"androidboot.hardware.radio.subtype", "0"},
+ {"androidboot.dtbo_idx", "2"},
+ {"androidboot.mode", "normal"},
+ {"androidboot.hardware.ddr", "1GB,combuchi,LPDDR4X"},
+ {"androidboot.ddr_info", "combuchiandroidboot.ddr_size=2GB"},
+ {"androidboot.hardware.ufs", "2GB,combushi"},
+ {"androidboot.boottime", "0BLE:58,1BLL:22,1BLE:571,2BLL:105,ODT:0,AVB:123"},
+ {"androidboot.ramdump", "disabled"},
+ {"dm", "1 vroot none ro 1,0 10416 verity 1 624684 fec_start 624684"},
+ {"root", "/dev/dm-0"},
+ {"androidboot.vbmeta.device", "PARTUUID=aa08f1a4-c7c9-402e-9a66-9707cafa9ceb"},
+ {"androidboot.vbmeta.avb_version", "1.1"},
+ {"androidboot.vbmeta.device_state", "unlocked"},
+ {"androidboot.vbmeta.hash_alg", "sha256"},
+ {"androidboot.vbmeta.size", "5248"},
+ {"androidboot.vbmeta.digest",
+ "ac13147e959861c20f2a6da97d25fe79e60e902c022a371c5c039d31e7c68860"},
+ {"androidboot.vbmeta.invalidate_on_error", "yes"},
+ {"androidboot.veritymode", "enforcing"},
+ {"androidboot.verifiedbootstate", "orange"},
+ {"androidboot.space", "sha256 5248 androidboot.nospace=nope"},
+ {"printk.devkmsg", "on"},
+ {"msm_rtb.filter", "0x237"},
+ {"ehci-hcd.park", "3"},
+ {"string ", "string '"},
+ {"service_locator.enable", "1"},
+ {"firmware_class.path", "/vendor/firmware"},
+ {"cgroup.memory", "nokmem"},
+ {"lpm_levels.sleep_disabled", "1"},
+ {"buildvariant", "userdebug"},
+ {"console", "null"},
+ {"terminator", "truncated"},
+};
+
+} // namespace
+
+TEST(fs_mgr, fs_mgr_parse_boot_config) {
+ EXPECT_EQ(result_space, fs_mgr_parse_boot_config(cmdline));
+}
+
+TEST(fs_mgr, fs_mgr_get_boot_config_from_kernel_cmdline) {
+ std::string content;
+ for (const auto& entry : result_space) {
+ static constexpr char androidboot[] = "androidboot.";
+ if (!android::base::StartsWith(entry.first, androidboot)) continue;
+ auto key = entry.first.substr(strlen(androidboot));
+ EXPECT_TRUE(fs_mgr_get_boot_config_from_kernel(cmdline, key, &content)) << " for " << key;
+ EXPECT_EQ(entry.second, content);
+ }
+ EXPECT_FALSE(fs_mgr_get_boot_config_from_kernel(cmdline, "vbmeta.avb_versio", &content));
+ EXPECT_TRUE(content.empty()) << content;
+ EXPECT_FALSE(fs_mgr_get_boot_config_from_kernel(cmdline, "nospace", &content));
+ EXPECT_TRUE(content.empty()) << content;
+}
+
+TEST(fs_mgr, fs_mgr_read_fstab_file_proc_mounts) {
+ auto fstab = fs_mgr_read_fstab("/proc/mounts");
+ ASSERT_NE(fstab, nullptr);
+
+ std::unique_ptr<std::FILE, int (*)(std::FILE*)> mounts(setmntent("/proc/mounts", "r"),
+ endmntent);
+ ASSERT_NE(mounts, nullptr);
+
+ mntent* mentry;
+ int i = 0;
+ while ((mentry = getmntent(mounts.get())) != nullptr) {
+ ASSERT_LT(i, fstab->num_entries);
+ auto fsrec = &fstab->recs[i];
+
+ std::string mnt_fsname(mentry->mnt_fsname ?: "nullptr");
+ std::string blk_device(fsrec->blk_device ?: "nullptr");
+ EXPECT_EQ(mnt_fsname, blk_device);
+
+ std::string mnt_dir(mentry->mnt_dir ?: "nullptr");
+ std::string mount_point(fsrec->mount_point ?: "nullptr");
+ EXPECT_EQ(mnt_dir, mount_point);
+
+ std::string mnt_type(mentry->mnt_type ?: "nullptr");
+ std::string fs_type(fsrec->fs_type ?: "nullptr");
+ EXPECT_EQ(mnt_type, fs_type);
+
+ std::set<std::string> mnt_opts;
+ for (auto& s : android::base::Split(mentry->mnt_opts ?: "nullptr", ",")) {
+ mnt_opts.emplace(s);
+ }
+ std::set<std::string> fs_options;
+ for (auto& s : android::base::Split(fsrec->fs_options ?: "nullptr", ",")) {
+ fs_options.emplace(s);
+ }
+ // matches private content in fs_mgr_fstab.c
+ static struct flag_list {
+ const char* name;
+ unsigned int flag;
+ } mount_flags[] = {
+ {"noatime", MS_NOATIME},
+ {"noexec", MS_NOEXEC},
+ {"nosuid", MS_NOSUID},
+ {"nodev", MS_NODEV},
+ {"nodiratime", MS_NODIRATIME},
+ {"ro", MS_RDONLY},
+ {"rw", 0},
+ {"remount", MS_REMOUNT},
+ {"bind", MS_BIND},
+ {"rec", MS_REC},
+ {"unbindable", MS_UNBINDABLE},
+ {"private", MS_PRIVATE},
+ {"slave", MS_SLAVE},
+ {"shared", MS_SHARED},
+ {"defaults", 0},
+ {0, 0},
+ };
+ for (auto f = 0; mount_flags[f].name; ++f) {
+ if (mount_flags[f].flag & fsrec->flags) {
+ fs_options.emplace(mount_flags[f].name);
+ }
+ }
+ if (!(fsrec->flags & MS_RDONLY)) fs_options.emplace("rw");
+ EXPECT_EQ(mnt_opts, fs_options);
+ ++i;
+ }
+}
diff --git a/fs_mgr/tools/Android.bp b/fs_mgr/tools/Android.bp
new file mode 100644
index 0000000..4d4aae4
--- /dev/null
+++ b/fs_mgr/tools/Android.bp
@@ -0,0 +1,31 @@
+//
+// Copyright (C) 2018 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.
+//
+
+cc_binary {
+ name: "dmctl",
+ srcs: ["dmctl.cpp"],
+
+ static_libs: [
+ "libfs_mgr",
+ ],
+
+ shared_libs: [
+ "libbase",
+ "liblog",
+ ],
+
+ cflags: ["-Werror"],
+}
diff --git a/fs_mgr/tools/dmctl.cpp b/fs_mgr/tools/dmctl.cpp
new file mode 100644
index 0000000..45a81af
--- /dev/null
+++ b/fs_mgr/tools/dmctl.cpp
@@ -0,0 +1,322 @@
+/*
+ * Copyright (C) 2018 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 <fcntl.h>
+#include <getopt.h>
+#include <linux/dm-ioctl.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <android-base/parseint.h>
+#include <android-base/unique_fd.h>
+#include <libdm/dm.h>
+
+#include <functional>
+#include <iomanip>
+#include <ios>
+#include <iostream>
+#include <map>
+#include <sstream>
+#include <string>
+#include <vector>
+
+using DeviceMapper = ::android::dm::DeviceMapper;
+using DmTable = ::android::dm::DmTable;
+using DmTarget = ::android::dm::DmTarget;
+using DmTargetLinear = ::android::dm::DmTargetLinear;
+using DmTargetZero = ::android::dm::DmTargetZero;
+using DmTargetAndroidVerity = ::android::dm::DmTargetAndroidVerity;
+using DmTargetTypeInfo = ::android::dm::DmTargetTypeInfo;
+using DmBlockDevice = ::android::dm::DeviceMapper::DmBlockDevice;
+
+static int Usage(void) {
+ std::cerr << "usage: dmctl <command> [command options]" << std::endl;
+ std::cerr << "commands:" << std::endl;
+ std::cerr << " create <dm-name> [-ro] <targets...>" << std::endl;
+ std::cerr << " delete <dm-name>" << std::endl;
+ std::cerr << " list <devices | targets>" << std::endl;
+ std::cerr << " getpath <dm-name>" << std::endl;
+ std::cerr << " table <dm-name>" << std::endl;
+ std::cerr << " help" << std::endl;
+ std::cerr << std::endl;
+ std::cerr << "Target syntax:" << std::endl;
+ std::cerr << " <target_type> <start_sector> <num_sectors> [target_data]" << std::endl;
+ return -EINVAL;
+}
+
+class TargetParser final {
+ public:
+ TargetParser(int argc, char** argv) : arg_index_(0), argc_(argc), argv_(argv) {}
+
+ bool More() const { return arg_index_ < argc_; }
+ std::unique_ptr<DmTarget> Next() {
+ if (!HasArgs(3)) {
+ std::cerr << "Expected <target_type> <start_sector> <num_sectors>" << std::endl;
+ return nullptr;
+ }
+
+ std::string target_type = NextArg();
+ uint64_t start_sector, num_sectors;
+ if (!android::base::ParseUint(NextArg(), &start_sector)) {
+ std::cerr << "Expected start sector, got: " << PreviousArg() << std::endl;
+ return nullptr;
+ }
+ if (!android::base::ParseUint(NextArg(), &num_sectors) || !num_sectors) {
+ std::cerr << "Expected non-zero sector count, got: " << PreviousArg() << std::endl;
+ return nullptr;
+ }
+
+ if (target_type == "zero") {
+ return std::make_unique<DmTargetZero>(start_sector, num_sectors);
+ } else if (target_type == "linear") {
+ if (!HasArgs(2)) {
+ std::cerr << "Expected \"linear\" <block_device> <sector>" << std::endl;
+ return nullptr;
+ }
+
+ std::string block_device = NextArg();
+ uint64_t physical_sector;
+ if (!android::base::ParseUint(NextArg(), &physical_sector)) {
+ std::cerr << "Expected sector, got: \"" << PreviousArg() << "\"" << std::endl;
+ return nullptr;
+ }
+ return std::make_unique<DmTargetLinear>(start_sector, num_sectors, block_device,
+ physical_sector);
+ } else if (target_type == "android-verity") {
+ if (!HasArgs(2)) {
+ std::cerr << "Expected \"android-verity\" <public-key-id> <block_device>"
+ << std::endl;
+ return nullptr;
+ }
+ std::string keyid = NextArg();
+ std::string block_device = NextArg();
+ return std::make_unique<DmTargetAndroidVerity>(start_sector, num_sectors, keyid,
+ block_device);
+ } else {
+ std::cerr << "Unrecognized target type: " << target_type << std::endl;
+ return nullptr;
+ }
+ }
+
+ private:
+ bool HasArgs(int count) { return arg_index_ + count <= argc_; }
+ const char* NextArg() {
+ CHECK(arg_index_ < argc_);
+ return argv_[arg_index_++];
+ }
+ const char* PreviousArg() {
+ CHECK(arg_index_ >= 0);
+ return argv_[arg_index_ - 1];
+ }
+
+ private:
+ int arg_index_;
+ int argc_;
+ char** argv_;
+};
+
+static int DmCreateCmdHandler(int argc, char** argv) {
+ if (argc < 1) {
+ std::cerr << "Usage: dmctl create <dm-name> [-ro] <targets...>" << std::endl;
+ return -EINVAL;
+ }
+ std::string name = argv[0];
+
+ // Parse extended options first.
+ DmTable table;
+ int arg_index = 1;
+ while (arg_index < argc && argv[arg_index][0] == '-') {
+ if (strcmp(argv[arg_index], "-ro") == 0) {
+ table.set_readonly(true);
+ arg_index++;
+ } else {
+ std::cerr << "Unrecognized option: " << argv[arg_index] << std::endl;
+ return -EINVAL;
+ }
+ }
+
+ // Parse everything else as target information.
+ TargetParser parser(argc - arg_index, argv + arg_index);
+ while (parser.More()) {
+ std::unique_ptr<DmTarget> target = parser.Next();
+ if (!target || !table.AddTarget(std::move(target))) {
+ return -EINVAL;
+ }
+ }
+
+ if (table.num_targets() == 0) {
+ std::cerr << "Must define at least one target." << std::endl;
+ return -EINVAL;
+ }
+
+ DeviceMapper& dm = DeviceMapper::Instance();
+ if (!dm.CreateDevice(name, table)) {
+ std::cerr << "Failed to create device-mapper device with name: " << name << std::endl;
+ return -EIO;
+ }
+ return 0;
+}
+
+static int DmDeleteCmdHandler(int argc, char** argv) {
+ if (argc < 1) {
+ std::cerr << "Usage: dmctl delete <name>" << std::endl;
+ return -EINVAL;
+ }
+
+ std::string name = argv[0];
+ DeviceMapper& dm = DeviceMapper::Instance();
+ if (!dm.DeleteDevice(name)) {
+ std::cerr << "Failed to delete [" << name << "]" << std::endl;
+ return -EIO;
+ }
+
+ return 0;
+}
+
+static int DmListTargets(DeviceMapper& dm) {
+ std::vector<DmTargetTypeInfo> targets;
+ if (!dm.GetAvailableTargets(&targets)) {
+ std::cerr << "Failed to read available device mapper targets" << std::endl;
+ return -errno;
+ }
+
+ std::cout << "Available Device Mapper Targets:" << std::endl;
+ if (targets.empty()) {
+ std::cout << " <empty>" << std::endl;
+ return 0;
+ }
+
+ for (const auto& target : targets) {
+ std::cout << std::left << std::setw(20) << target.name() << " : " << target.version()
+ << std::endl;
+ }
+
+ return 0;
+}
+
+static int DmListDevices(DeviceMapper& dm) {
+ std::vector<DmBlockDevice> devices;
+ if (!dm.GetAvailableDevices(&devices)) {
+ std::cerr << "Failed to read available device mapper devices" << std::endl;
+ return -errno;
+ }
+ std::cout << "Available Device Mapper Devices:" << std::endl;
+ if (devices.empty()) {
+ std::cout << " <empty>" << std::endl;
+ return 0;
+ }
+
+ for (const auto& dev : devices) {
+ std::cout << std::left << std::setw(20) << dev.name() << " : " << dev.Major() << ":"
+ << dev.Minor() << std::endl;
+ }
+
+ return 0;
+}
+
+static const std::map<std::string, std::function<int(DeviceMapper&)>> listmap = {
+ {"targets", DmListTargets},
+ {"devices", DmListDevices},
+};
+
+static int DmListCmdHandler(int argc, char** argv) {
+ if (argc < 1) {
+ std::cerr << "Invalid arguments, see \'dmctl help\'" << std::endl;
+ return -EINVAL;
+ }
+
+ DeviceMapper& dm = DeviceMapper::Instance();
+ for (const auto& l : listmap) {
+ if (l.first == argv[0]) return l.second(dm);
+ }
+
+ std::cerr << "Invalid argument to \'dmctl list\': " << argv[0] << std::endl;
+ return -EINVAL;
+}
+
+static int HelpCmdHandler(int /* argc */, char** /* argv */) {
+ Usage();
+ return 0;
+}
+
+static int GetPathCmdHandler(int argc, char** argv) {
+ if (argc != 1) {
+ std::cerr << "Invalid arguments, see \'dmctl help\'" << std::endl;
+ return -EINVAL;
+ }
+
+ DeviceMapper& dm = DeviceMapper::Instance();
+ std::string path;
+ if (!dm.GetDmDevicePathByName(argv[0], &path)) {
+ std::cerr << "Could not query path of device \"" << argv[0] << "\"." << std::endl;
+ return -EINVAL;
+ }
+ std::cout << path << std::endl;
+ return 0;
+}
+
+static int TableCmdHandler(int argc, char** argv) {
+ if (argc != 1) {
+ std::cerr << "Invalid arguments, see \'dmctl help\'" << std::endl;
+ return -EINVAL;
+ }
+
+ DeviceMapper& dm = DeviceMapper::Instance();
+ std::vector<DeviceMapper::TargetInfo> table;
+ if (!dm.GetTableStatus(argv[0], &table)) {
+ std::cerr << "Could not query table status of device \"" << argv[0] << "\"." << std::endl;
+ return -EINVAL;
+ }
+ std::cout << "Targets in the device-mapper table for " << argv[0] << ":" << std::endl;
+ for (const auto& target : table) {
+ std::cout << target.spec.sector_start << "-"
+ << (target.spec.sector_start + target.spec.length) << ": "
+ << target.spec.target_type;
+ if (!target.data.empty()) {
+ std::cout << ", " << target.data;
+ }
+ std::cout << std::endl;
+ }
+ return 0;
+}
+
+static std::map<std::string, std::function<int(int, char**)>> cmdmap = {
+ // clang-format off
+ {"create", DmCreateCmdHandler},
+ {"delete", DmDeleteCmdHandler},
+ {"list", DmListCmdHandler},
+ {"help", HelpCmdHandler},
+ {"getpath", GetPathCmdHandler},
+ {"table", TableCmdHandler},
+ // clang-format on
+};
+
+int main(int argc, char** argv) {
+ android::base::InitLogging(argv, &android::base::StderrLogger);
+ if (argc < 2) {
+ return Usage();
+ }
+
+ for (const auto& cmd : cmdmap) {
+ if (cmd.first == argv[1]) {
+ return cmd.second(argc - 2, argv + 2);
+ }
+ }
+
+ return Usage();
+}
diff --git a/gatekeeperd/Android.mk b/gatekeeperd/Android.mk
index 28f0b07..6d5d1ea 100644
--- a/gatekeeperd/Android.mk
+++ b/gatekeeperd/Android.mk
@@ -32,6 +32,7 @@
libbase \
libutils \
libcrypto \
+ libkeystore_aidl \
libkeystore_binder \
libhidlbase \
libhidltransport \
diff --git a/gatekeeperd/OWNERS b/gatekeeperd/OWNERS
new file mode 100644
index 0000000..9c99c6e
--- /dev/null
+++ b/gatekeeperd/OWNERS
@@ -0,0 +1,2 @@
+swillden@google.com
+jdanis@google.com
diff --git a/gatekeeperd/gatekeeperd.cpp b/gatekeeperd/gatekeeperd.cpp
index 61c8804..5f3ce36 100644
--- a/gatekeeperd/gatekeeperd.cpp
+++ b/gatekeeperd/gatekeeperd.cpp
@@ -25,14 +25,15 @@
#include <unistd.h>
#include <memory>
+#include <android/security/IKeystoreService.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/PermissionCache.h>
#include <gatekeeper/password_handle.h> // for password_handle_t
#include <hardware/gatekeeper.h>
#include <hardware/hw_auth_token.h>
-#include <keystore/IKeystoreService.h>
#include <keystore/keystore.h> // For error code
+#include <keystore/keystore_return_types.h>
#include <log/log.h>
#include <utils/Log.h>
#include <utils/String16.h>
@@ -234,11 +235,13 @@
virtual int verify(uint32_t uid,
const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length,
const uint8_t *provided_password, uint32_t provided_password_length, bool *request_reenroll) {
- uint8_t *auth_token;
+ uint8_t *auth_token = nullptr;
uint32_t auth_token_length;
- return verifyChallenge(uid, 0, enrolled_password_handle, enrolled_password_handle_length,
+ int ret = verifyChallenge(uid, 0, enrolled_password_handle, enrolled_password_handle_length,
provided_password, provided_password_length,
&auth_token, &auth_token_length, request_reenroll);
+ delete [] auth_token;
+ return ret;
}
virtual int verifyChallenge(uint32_t uid, uint64_t challenge,
@@ -315,11 +318,15 @@
// TODO: cache service?
sp<IServiceManager> sm = defaultServiceManager();
sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
- sp<IKeystoreService> service = interface_cast<IKeystoreService>(binder);
+ sp<security::IKeystoreService> service =
+ interface_cast<security::IKeystoreService>(binder);
if (service != NULL) {
- auto ret = service->addAuthToken(*auth_token, *auth_token_length);
- if (!ret.isOk()) {
- ALOGE("Failure sending auth token to KeyStore: %" PRId32, int32_t(ret));
+ std::vector<uint8_t> auth_token_vector(*auth_token,
+ (*auth_token) + *auth_token_length);
+ int result = 0;
+ auto binder_result = service->addAuthToken(auth_token_vector, &result);
+ if (!binder_result.isOk() || !keystore::KeyStoreServiceReturnCode(result).isOk()) {
+ ALOGE("Failure sending auth token to KeyStore: %" PRId32, result);
}
} else {
ALOGE("Unable to communicate with KeyStore");
diff --git a/healthd/Android.bp b/healthd/Android.bp
index 56f5148..6b00f81 100644
--- a/healthd/Android.bp
+++ b/healthd/Android.bp
@@ -1,7 +1,86 @@
cc_library_headers {
name: "libhealthd_headers",
vendor_available: true,
+ recovery_available: true,
export_include_dirs: ["include"],
header_libs: ["libbatteryservice_headers"],
export_header_lib_headers: ["libbatteryservice_headers"],
}
+
+cc_library_static {
+ name: "libbatterymonitor",
+ srcs: ["BatteryMonitor.cpp"],
+ cflags: ["-Wall", "-Werror"],
+ vendor_available: true,
+ recovery_available: true,
+ export_include_dirs: ["include"],
+ shared_libs: [
+ "libutils",
+ "libbase",
+ ],
+ header_libs: ["libhealthd_headers"],
+ export_header_lib_headers: ["libhealthd_headers"],
+}
+
+cc_defaults {
+ name: "android.hardware.health@2.0-service_defaults",
+
+ cflags: [
+ "-Wall",
+ "-Werror",
+ ],
+
+ static_libs: [
+ "android.hardware.health@2.0-impl",
+ "android.hardware.health@1.0-convert",
+ "libhealthservice",
+ "libhealthstoragedefault",
+ "libbatterymonitor",
+ ],
+
+ shared_libs: [
+ "libbase",
+ "libcutils",
+ "libhidlbase",
+ "libhidltransport",
+ "libhwbinder",
+ "liblog",
+ "libutils",
+ "android.hardware.health@2.0",
+ ],
+}
+
+cc_binary {
+ name: "android.hardware.health@2.0-service",
+ defaults: ["android.hardware.health@2.0-service_defaults"],
+
+ vendor: true,
+ relative_install_path: "hw",
+ init_rc: ["android.hardware.health@2.0-service.rc"],
+ srcs: [
+ "HealthServiceDefault.cpp",
+ ],
+
+ overrides: [
+ "healthd",
+ ]
+}
+
+cc_binary {
+ name: "healthd",
+ defaults: ["android.hardware.health@2.0-service_defaults"],
+
+ init_rc: ["healthd.rc"],
+ srcs: [
+ "HealthServiceHealthd.cpp",
+ ],
+ local_include_dirs: ["include"],
+
+ shared_libs: [
+ "android.hardware.health@1.0",
+ ],
+
+ vintf_fragments: [
+ "manifest_healthd.xml"
+ ],
+}
diff --git a/healthd/Android.mk b/healthd/Android.mk
index 6b14289..f7214c6 100644
--- a/healthd/Android.mk
+++ b/healthd/Android.mk
@@ -3,35 +3,6 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
-LOCAL_SRC_FILES := BatteryMonitor.cpp
-LOCAL_MODULE := libbatterymonitor
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
-LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
-LOCAL_STATIC_LIBRARIES := libutils libbase libbinder
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := \
- healthd_mode_android.cpp \
- BatteryPropertiesRegistrar.cpp
-
-LOCAL_MODULE := libhealthd_android
-LOCAL_EXPORT_C_INCLUDE_DIRS := \
- $(LOCAL_PATH) \
- $(LOCAL_PATH)/include
-
-LOCAL_STATIC_LIBRARIES := \
- libbatterymonitor \
- libbatteryservice \
- libutils \
- libbase \
- libcutils \
- liblog \
- libc \
-
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
LOCAL_MODULE := libhealthd_draw
@@ -53,6 +24,8 @@
LOCAL_CFLAGS += -DHEALTHD_DRAW_SPLIT_OFFSET=0
endif
+LOCAL_HEADER_LIBRARIES := libbatteryservice_headers
+
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
@@ -76,6 +49,11 @@
$(LOCAL_PATH)/include
LOCAL_STATIC_LIBRARIES := \
+ android.hardware.health@2.0 \
+ android.hardware.health@2.0-impl \
+ android.hardware.health@1.0 \
+ android.hardware.health@1.0-convert \
+ libhealthstoragedefault \
libminui \
libpng \
libz \
@@ -98,12 +76,8 @@
ifeq ($(strip $(BOARD_CHARGER_NO_UI)),true)
LOCAL_CHARGER_NO_UI := true
endif
-ifdef BRILLO
-LOCAL_CHARGER_NO_UI := true
-endif
LOCAL_SRC_FILES := \
- healthd_common.cpp \
charger.cpp \
LOCAL_MODULE := charger
@@ -117,14 +91,17 @@
ifeq ($(strip $(LOCAL_CHARGER_NO_UI)),true)
LOCAL_CFLAGS += -DCHARGER_NO_UI
endif
-ifneq ($(BOARD_PERIODIC_CHORES_INTERVAL_FAST),)
-LOCAL_CFLAGS += -DBOARD_PERIODIC_CHORES_INTERVAL_FAST=$(BOARD_PERIODIC_CHORES_INTERVAL_FAST)
-endif
-ifneq ($(BOARD_PERIODIC_CHORES_INTERVAL_SLOW),)
-LOCAL_CFLAGS += -DBOARD_PERIODIC_CHORES_INTERVAL_SLOW=$(BOARD_PERIODIC_CHORES_INTERVAL_SLOW)
-endif
-LOCAL_STATIC_LIBRARIES := \
+CHARGER_STATIC_LIBRARIES := \
+ android.hardware.health@2.0-impl \
+ android.hardware.health@2.0 \
+ android.hardware.health@1.0 \
+ android.hardware.health@1.0-convert \
+ libhidltransport \
+ libhidlbase \
+ libhwbinder_noltopgo \
+ libhealthstoragedefault \
+ libvndksupport \
libhealthd_charger \
libhealthd_draw \
libbatterymonitor \
@@ -135,6 +112,8 @@
libm \
libc \
+LOCAL_STATIC_LIBRARIES := $(CHARGER_STATIC_LIBRARIES)
+
ifneq ($(strip $(LOCAL_CHARGER_NO_UI)),true)
LOCAL_STATIC_LIBRARIES += \
libminui \
@@ -155,6 +134,21 @@
include $(BUILD_EXECUTABLE)
+include $(CLEAR_VARS)
+LOCAL_MODULE := charger_test
+LOCAL_MODULE_TAGS := optional
+LOCAL_FORCE_STATIC_EXECUTABLE := true
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+LOCAL_CFLAGS := -Wall -Werror -DCHARGER_TEST -DCHARGER_NO_UI
+LOCAL_STATIC_LIBRARIES := $(CHARGER_STATIC_LIBRARIES)
+LOCAL_SRC_FILES := \
+ charger.cpp \
+ charger_test.cpp \
+
+include $(BUILD_EXECUTABLE)
+
+CHARGER_STATIC_LIBRARIES :=
+
ifneq ($(strip $(LOCAL_CHARGER_NO_UI)),true)
define _add-charger-image
include $$(CLEAR_VARS)
@@ -182,41 +176,3 @@
_add-charger-image :=
_img_modules :=
endif # LOCAL_CHARGER_NO_UI
-
-### healthd ###
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := \
- healthd_common.cpp \
- healthd.cpp \
-
-LOCAL_MODULE := healthd
-LOCAL_MODULE_TAGS := optional
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
-
-ifneq ($(BOARD_PERIODIC_CHORES_INTERVAL_FAST),)
-LOCAL_CFLAGS += -DBOARD_PERIODIC_CHORES_INTERVAL_FAST=$(BOARD_PERIODIC_CHORES_INTERVAL_FAST)
-endif
-ifneq ($(BOARD_PERIODIC_CHORES_INTERVAL_SLOW),)
-LOCAL_CFLAGS += -DBOARD_PERIODIC_CHORES_INTERVAL_SLOW=$(BOARD_PERIODIC_CHORES_INTERVAL_SLOW)
-endif
-
-LOCAL_STATIC_LIBRARIES := \
- libhealthd_android \
- libbatterymonitor \
- libbatteryservice \
- android.hardware.health@1.0-convert \
-
-LOCAL_SHARED_LIBRARIES := \
- libbinder \
- libbase \
- libutils \
- libcutils \
- liblog \
- libm \
- libc \
- libhidlbase \
- libhidltransport \
- android.hardware.health@1.0 \
-
-include $(BUILD_EXECUTABLE)
diff --git a/healthd/BatteryMonitor.cpp b/healthd/BatteryMonitor.cpp
index fa79d0b..80c5afe 100644
--- a/healthd/BatteryMonitor.cpp
+++ b/healthd/BatteryMonitor.cpp
@@ -88,6 +88,10 @@
initBatteryProperties(&props);
}
+struct BatteryProperties getBatteryProperties(BatteryMonitor* batteryMonitor) {
+ return batteryMonitor->props;
+}
+
int BatteryMonitor::getBatteryStatus(const char* status) {
int ret;
struct sysfsStringEnumMap batteryStatusMap[] = {
@@ -531,12 +535,6 @@
POWER_SUPPLY_SYSFS_PATH, name);
if (access(path, R_OK) == 0) {
mHealthdConfig->batteryVoltagePath = path;
- } else {
- path.clear();
- path.appendFormat("%s/%s/batt_vol",
- POWER_SUPPLY_SYSFS_PATH, name);
- if (access(path, R_OK) == 0)
- mHealthdConfig->batteryVoltagePath = path;
}
}
@@ -586,12 +584,6 @@
name);
if (access(path, R_OK) == 0) {
mHealthdConfig->batteryTemperaturePath = path;
- } else {
- path.clear();
- path.appendFormat("%s/%s/batt_temp",
- POWER_SUPPLY_SYSFS_PATH, name);
- if (access(path, R_OK) == 0)
- mHealthdConfig->batteryTemperaturePath = path;
}
}
diff --git a/healthd/BatteryPropertiesRegistrar.cpp b/healthd/BatteryPropertiesRegistrar.cpp
deleted file mode 100644
index e51a06d..0000000
--- a/healthd/BatteryPropertiesRegistrar.cpp
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * Copyright (C) 2013 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 "BatteryPropertiesRegistrar.h"
-#include <batteryservice/BatteryService.h>
-#include <batteryservice/IBatteryPropertiesListener.h>
-#include <batteryservice/IBatteryPropertiesRegistrar.h>
-#include <binder/IPCThreadState.h>
-#include <binder/IServiceManager.h>
-#include <binder/PermissionCache.h>
-#include <private/android_filesystem_config.h>
-#include <utils/Errors.h>
-#include <utils/Mutex.h>
-#include <utils/String16.h>
-
-#include <healthd/healthd.h>
-
-namespace android {
-
-void BatteryPropertiesRegistrar::publish(
- const sp<BatteryPropertiesRegistrar>& service) {
- defaultServiceManager()->addService(String16("batteryproperties"), service);
-}
-
-void BatteryPropertiesRegistrar::notifyListeners(const struct BatteryProperties& props) {
- Vector<sp<IBatteryPropertiesListener> > listenersCopy;
-
- // Binder currently may service an incoming oneway transaction whenever an
- // outbound oneway call is made (if there is already a pending incoming
- // oneway call waiting). This is considered a bug and may change in the
- // future. For now, avoid recursive mutex lock while making outbound
- // calls by making a local copy of the current list of listeners.
- {
- Mutex::Autolock _l(mRegistrationLock);
- listenersCopy = mListeners;
- }
- for (size_t i = 0; i < listenersCopy.size(); i++) {
- listenersCopy[i]->batteryPropertiesChanged(props);
- }
-}
-
-void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) {
- {
- if (listener == NULL)
- return;
- Mutex::Autolock _l(mRegistrationLock);
- // check whether this is a duplicate
- for (size_t i = 0; i < mListeners.size(); i++) {
- if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
- return;
- }
- }
-
- mListeners.add(listener);
- IInterface::asBinder(listener)->linkToDeath(this);
- }
- healthd_battery_update();
-}
-
-void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
- if (listener == NULL)
- return;
- Mutex::Autolock _l(mRegistrationLock);
- for (size_t i = 0; i < mListeners.size(); i++) {
- if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
- IInterface::asBinder(mListeners[i])->unlinkToDeath(this);
- mListeners.removeAt(i);
- break;
- }
- }
-}
-
-status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) {
- return healthd_get_property(id, val);
-}
-
-void BatteryPropertiesRegistrar::scheduleUpdate() {
- healthd_battery_update();
-}
-
-status_t BatteryPropertiesRegistrar::dump(int fd, const Vector<String16>& /*args*/) {
- IPCThreadState* self = IPCThreadState::self();
- const int pid = self->getCallingPid();
- const int uid = self->getCallingUid();
- if ((uid != AID_SHELL) &&
- !PermissionCache::checkPermission(
- String16("android.permission.DUMP"), pid, uid))
- return PERMISSION_DENIED;
-
- healthd_dump_battery_state(fd);
- return OK;
-}
-
-void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
- Mutex::Autolock _l(mRegistrationLock);
-
- for (size_t i = 0; i < mListeners.size(); i++) {
- if (IInterface::asBinder(mListeners[i]) == who) {
- mListeners.removeAt(i);
- break;
- }
- }
-}
-
-} // namespace android
diff --git a/healthd/BatteryPropertiesRegistrar.h b/healthd/BatteryPropertiesRegistrar.h
deleted file mode 100644
index 14e9145..0000000
--- a/healthd/BatteryPropertiesRegistrar.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#ifndef HEALTHD_BATTERYPROPERTIES_REGISTRAR_H
-#define HEALTHD_BATTERYPROPERTIES_REGISTRAR_H
-
-#include <binder/IBinder.h>
-#include <utils/Mutex.h>
-#include <utils/String16.h>
-#include <utils/Vector.h>
-#include <batteryservice/BatteryService.h>
-#include <batteryservice/IBatteryPropertiesListener.h>
-#include <batteryservice/IBatteryPropertiesRegistrar.h>
-
-namespace android {
-
-class BatteryPropertiesRegistrar : public BnBatteryPropertiesRegistrar,
- public IBinder::DeathRecipient {
-public:
- void publish(const sp<BatteryPropertiesRegistrar>& service);
- void notifyListeners(const struct BatteryProperties& props);
- void scheduleUpdate();
-
-private:
- Mutex mRegistrationLock;
- Vector<sp<IBatteryPropertiesListener> > mListeners;
-
- void registerListener(const sp<IBatteryPropertiesListener>& listener);
- void unregisterListener(const sp<IBatteryPropertiesListener>& listener);
- status_t getProperty(int id, struct BatteryProperty *val);
- status_t dump(int fd, const Vector<String16>& args);
- void binderDied(const wp<IBinder>& who);
-};
-
-}; // namespace android
-
-#endif // HEALTHD_BATTERYPROPERTIES_REGISTRAR_H
diff --git a/healthd/HealthServiceDefault.cpp b/healthd/HealthServiceDefault.cpp
new file mode 100644
index 0000000..89ecc2f
--- /dev/null
+++ b/healthd/HealthServiceDefault.cpp
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2017 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 <health2/service.h>
+#include <healthd/healthd.h>
+
+void healthd_board_init(struct healthd_config*) {
+ // Implementation-defined init logic goes here.
+ // 1. config->periodic_chores_interval_* variables
+ // 2. config->battery*Path variables
+ // 3. config->energyCounter. In this implementation, energyCounter is not defined.
+
+ // use defaults
+}
+
+int healthd_board_battery_update(struct android::BatteryProperties*) {
+ // Implementation-defined update logic goes here. An implementation
+ // can make modifications to prop before broadcasting it to all callbacks.
+
+ // return 0 to log periodic polled battery status to kernel log
+ return 0;
+}
+
+int main() {
+ return health_service_main();
+}
diff --git a/healthd/HealthServiceHealthd.cpp b/healthd/HealthServiceHealthd.cpp
new file mode 100644
index 0000000..5fd2597
--- /dev/null
+++ b/healthd/HealthServiceHealthd.cpp
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#define LOG_TAG "healthd"
+#include <android-base/logging.h>
+
+#include <android/hardware/health/1.0/IHealth.h>
+#include <android/hardware/health/1.0/types.h>
+#include <hal_conversion.h>
+#include <health2/service.h>
+#include <healthd/healthd.h>
+#include <hidl/HidlTransportSupport.h>
+
+using android::OK;
+using android::NAME_NOT_FOUND;
+using android::hardware::health::V1_0::HealthConfig;
+using android::hardware::health::V1_0::HealthInfo;
+using android::hardware::health::V1_0::Result;
+using android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
+using android::hardware::health::V1_0::hal_conversion::convertToHealthConfig;
+using android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
+using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
+
+using IHealthLegacy = android::hardware::health::V1_0::IHealth;
+
+static android::sp<IHealthLegacy> gHealth_1_0;
+
+static int healthd_board_get_energy_counter(int64_t* energy) {
+ if (gHealth_1_0 == nullptr) {
+ return NAME_NOT_FOUND;
+ }
+
+ Result result = Result::NOT_SUPPORTED;
+ gHealth_1_0->energyCounter([energy, &result](Result ret, int64_t energyOut) {
+ result = ret;
+ *energy = energyOut;
+ });
+
+ return result == Result::SUCCESS ? OK : NAME_NOT_FOUND;
+}
+
+void healthd_board_init(struct healthd_config* config) {
+ gHealth_1_0 = IHealthLegacy::getService();
+
+ if (gHealth_1_0 == nullptr) {
+ return;
+ }
+
+ HealthConfig halConfig{};
+ convertToHealthConfig(config, halConfig);
+ gHealth_1_0->init(halConfig, [config](const auto& halConfigOut) {
+ convertFromHealthConfig(halConfigOut, config);
+ // always redirect energy counter queries
+ config->energyCounter = healthd_board_get_energy_counter;
+ });
+ LOG(INFO) << LOG_TAG << ": redirecting calls to 1.0 health HAL";
+}
+
+// TODO(b/68724651): Move this function into healthd_mode_service_2_0_battery_update
+// with logthis returned.
+int healthd_board_battery_update(struct android::BatteryProperties* props) {
+ int logthis = 0;
+
+ if (gHealth_1_0 == nullptr) {
+ return logthis;
+ }
+
+ HealthInfo info;
+ convertToHealthInfo(props, info);
+ gHealth_1_0->update(info, [props, &logthis](int32_t ret, const auto& infoOut) {
+ logthis = ret;
+ convertFromHealthInfo(infoOut, props);
+ });
+
+ return logthis;
+}
+
+int main() {
+ return health_service_main("backup");
+}
diff --git a/healthd/OWNERS b/healthd/OWNERS
index 2375f7c..00df08a 100644
--- a/healthd/OWNERS
+++ b/healthd/OWNERS
@@ -1 +1,2 @@
+elsk@google.com
toddpoynor@google.com
diff --git a/healthd/android.hardware.health@2.0-service.rc b/healthd/android.hardware.health@2.0-service.rc
new file mode 100644
index 0000000..dca0ccc
--- /dev/null
+++ b/healthd/android.hardware.health@2.0-service.rc
@@ -0,0 +1,5 @@
+service health-hal-2-0 /vendor/bin/hw/android.hardware.health@2.0-service
+ class hal
+ user system
+ group system
+ file /dev/kmsg w
diff --git a/healthd/charger.cpp b/healthd/charger.cpp
index 5a8fe1a..43e7fd5 100644
--- a/healthd/charger.cpp
+++ b/healthd/charger.cpp
@@ -17,6 +17,7 @@
#define LOG_TAG "charger"
#define KLOG_LEVEL 6
+#include <health2/Health.h>
#include <healthd/healthd.h>
#include <stdlib.h>
@@ -62,7 +63,9 @@
};
#endif
-static void healthd_mode_nop_init(struct healthd_config* /*config*/) {
+static void healthd_mode_nop_init(struct healthd_config* config) {
+ using android::hardware::health::V2_0::implementation::Health;
+ Health::initInstance(config);
}
static int healthd_mode_nop_preparetowait(void) {
@@ -76,7 +79,7 @@
struct android::BatteryProperties* /*props*/) {
}
-int main(int argc, char **argv) {
+int healthd_charger_main(int argc, char** argv) {
int ch;
healthd_mode_ops = &charger_ops;
@@ -100,3 +103,9 @@
return healthd_main();
}
+
+#ifndef CHARGER_TEST
+int main(int argc, char** argv) {
+ return healthd_charger_main(argc, argv);
+}
+#endif
diff --git a/healthd/charger_test.cpp b/healthd/charger_test.cpp
new file mode 100644
index 0000000..a7e2161
--- /dev/null
+++ b/healthd/charger_test.cpp
@@ -0,0 +1,181 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#define LOG_TAG "charger_test"
+#include <android/log.h>
+
+#include <chrono>
+#include <condition_variable>
+#include <fstream>
+#include <iostream>
+#include <mutex>
+#include <streambuf>
+#include <string>
+#include <thread>
+#include <vector>
+
+#include <health2/Health.h>
+
+#define LOG_THIS(fmt, ...) \
+ ALOGE(fmt, ##__VA_ARGS__); \
+ printf(fmt "\n", ##__VA_ARGS__);
+
+template <typename T>
+class Atomic {
+ public:
+ Atomic(T&& init) : mValue(std::move(init)) {}
+ void set(T&& newVal) {
+ {
+ std::lock_guard<std::mutex> lock(mMutex);
+ mValue = std::move(newVal);
+ }
+ mChanged.notify_all();
+ }
+ bool waitFor(long ms, const T& expectVal) {
+ std::unique_lock<std::mutex> lock(mMutex);
+ return mChanged.wait_for(lock, std::chrono::milliseconds(ms),
+ [this, &expectVal] { return mValue == expectVal; });
+ }
+ private:
+ std::mutex mMutex;
+ std::condition_variable mChanged;
+ T mValue;
+};
+
+Atomic<bool>& getUpdateNotifier() {
+ static Atomic<bool> val(false);
+ return val;
+}
+
+int energyCounter(int64_t* counter) {
+ *counter = 0xEC12345;
+ return 0;
+}
+
+const char* createFile(const char* path, const char* content) {
+ std::ofstream stream(path);
+ if (!stream.is_open()) {
+ LOG_THIS("Cannot create file %s", path);
+ return NULL;
+ }
+ stream << content << std::endl;
+ stream.close();
+ return path;
+}
+
+std::string openToString(const char* path) {
+ std::ifstream stream(path);
+ if (!stream.is_open()) {
+ LOG_THIS("Cannot open file %s", path);
+ return "";
+ }
+ return std::string(std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>());
+}
+
+int expectContains(const std::string& content, const std::vector<std::string>& fields) {
+ int status = 0;
+ for (const auto& field : fields) {
+ auto pos = content.find(field);
+ if (pos == std::string::npos) {
+ LOG_THIS("Cannot find substr '%s'", field.c_str());
+ status = 1;
+ }
+ }
+ return status;
+}
+
+::android::hardware::hidl_handle createHidlHandle(const char* filepath) {
+ int fd = creat(filepath, S_IRUSR | S_IWUSR);
+ if (fd < 0) return {};
+ native_handle_t* nativeHandle = native_handle_create(1, 0);
+ nativeHandle->data[0] = fd;
+ ::android::hardware::hidl_handle handle;
+ handle.setTo(nativeHandle, true /* shouldOwn */);
+ return handle;
+}
+
+void healthd_board_init(struct healthd_config* config) {
+ config->periodic_chores_interval_fast = 60;
+ config->periodic_chores_interval_slow = 600;
+
+ config->batteryStatusPath = createFile("/data/local/tmp/batteryStatus", "Not charging");
+ config->batteryHealthPath = createFile("/data/local/tmp/batteryHealth", "Unspecified failure");
+ config->batteryPresentPath = createFile("/data/local/tmp/batteryPresent", "1");
+ config->batteryCapacityPath = createFile("/data/local/tmp/batteryCapacity", "47");
+ config->batteryVoltagePath = createFile("/data/local/tmp/batteryVoltage", "45000");
+ config->batteryTemperaturePath = createFile("/data/local/tmp/batteryTemperature", "987");
+ config->batteryTechnologyPath = createFile("/data/local/tmp/batteryTechnology", "NiCd");
+ config->batteryCurrentNowPath = createFile("/data/local/tmp/batteryCurrentNow", "99000");
+ config->batteryCurrentAvgPath = createFile("/data/local/tmp/batteryCurrentAvg", "98000");
+ config->batteryChargeCounterPath = createFile("/data/local/tmp/batteryChargeCounter", "600");
+ config->batteryFullChargePath = createFile("/data/local/tmp/batteryFullCharge", "3515547");
+ config->batteryCycleCountPath = createFile("/data/local/tmp/batteryCycleCount", "77");
+
+ config->energyCounter = energyCounter;
+ config->boot_min_cap = 50;
+ config->screen_on = NULL;
+}
+
+int healthd_board_battery_update(struct android::BatteryProperties*) {
+ getUpdateNotifier().set(true /* updated */);
+
+ // return 0 to log periodic polled battery status to kernel log
+ return 0;
+}
+
+extern int healthd_charger_main(int argc, char** argv);
+
+int main(int argc, char** argv) {
+ using android::hardware::health::V2_0::implementation::Health;
+
+ const char* dumpFile = "/data/local/tmp/dump.txt";
+
+ std::thread bgThread([=] {
+ healthd_charger_main(argc, argv);
+ });
+
+ // wait for healthd_init to finish
+ if (!getUpdateNotifier().waitFor(1000 /* wait ms */, true /* updated */)) {
+ LOG_THIS("Time out.");
+ exit(1);
+ }
+
+ Health::getImplementation()->debug(createHidlHandle(dumpFile), {} /* options */);
+
+ std::string content = openToString(dumpFile);
+ int status = expectContains(content, {
+ "status: 4",
+ "health: 6",
+ "present: 1",
+ "level: 47",
+ "voltage: 45",
+ "temp: 987",
+ "current now: 99000",
+ "current avg: 98000",
+ "charge counter: 600",
+ "current now: 99",
+ "cycle count: 77",
+ "Full charge: 3515547"
+ });
+
+ if (status == 0) {
+ LOG_THIS("Test success.");
+ } else {
+ LOG_THIS("Actual dump:\n%s", content.c_str());
+ }
+
+ exit(status); // force bgThread to exit
+}
diff --git a/healthd/healthd.cpp b/healthd/healthd.cpp
deleted file mode 100644
index ed1971a..0000000
--- a/healthd/healthd.cpp
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * Copyright (C) 2016 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.
- */
-
-#define LOG_TAG "healthd"
-#define KLOG_LEVEL 6
-
-#include <healthd/healthd.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <cutils/klog.h>
-
-#include <android/hardware/health/1.0/IHealth.h>
-#include <android/hardware/health/1.0/types.h>
-#include <hal_conversion.h>
-
-using namespace android;
-
-using IHealth = ::android::hardware::health::V1_0::IHealth;
-using Result = ::android::hardware::health::V1_0::Result;
-using HealthConfig = ::android::hardware::health::V1_0::HealthConfig;
-using HealthInfo = ::android::hardware::health::V1_0::HealthInfo;
-
-using ::android::hardware::health::V1_0::hal_conversion::convertToHealthConfig;
-using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
-using ::android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
-using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
-
-// device specific hal interface;
-static sp<IHealth> gHealth;
-
-// main healthd loop
-extern int healthd_main(void);
-
-// Android mode
-extern void healthd_mode_android_init(struct healthd_config *config);
-extern int healthd_mode_android_preparetowait(void);
-extern void healthd_mode_android_heartbeat(void);
-extern void healthd_mode_android_battery_update(
- struct android::BatteryProperties *props);
-
-static struct healthd_mode_ops android_ops = {
- .init = healthd_mode_android_init,
- .preparetowait = healthd_mode_android_preparetowait,
- .heartbeat = healthd_mode_android_heartbeat,
- .battery_update = healthd_mode_android_battery_update,
-};
-
-// default energy counter property redirect to talk to device
-// HAL
-static int healthd_board_get_energy_counter(int64_t *energy) {
-
- if (gHealth == nullptr) {
- return NAME_NOT_FOUND;
- }
-
- Result result = Result::NOT_SUPPORTED;
- gHealth->energyCounter([=, &result] (Result ret, int64_t energyOut) {
- result = ret;
- *energy = energyOut;
- });
-
- return result == Result::SUCCESS ? OK : NAME_NOT_FOUND;
-}
-
-void healthd_board_init(struct healthd_config *config) {
-
- // Initialize the board HAL - Equivalent of healthd_board_init(config)
- // in charger/recovery mode.
-
- gHealth = IHealth::getService();
- if (gHealth == nullptr) {
- KLOG_WARNING(LOG_TAG, "unable to get HAL interface, using defaults\n");
- return;
- }
-
- HealthConfig halConfig;
- convertToHealthConfig(config, halConfig);
- gHealth->init(halConfig, [=] (const auto &halConfigOut) {
- convertFromHealthConfig(halConfigOut, config);
- // always redirect energy counter queries
- config->energyCounter = healthd_board_get_energy_counter;
- });
-}
-
-int healthd_board_battery_update(struct android::BatteryProperties *props) {
- int logthis = 0;
-
- if (gHealth == nullptr) {
- return logthis;
- }
-
- HealthInfo info;
- convertToHealthInfo(props, info);
- gHealth->update(info,
- [=, &logthis] (int32_t ret, const auto &infoOut) {
- logthis = ret;
- convertFromHealthInfo(infoOut, props);
- });
-
- return logthis;
-}
-
-int main(int /*argc*/, char ** /*argv*/) {
-
- healthd_mode_ops = &android_ops;
-
- return healthd_main();
-}
diff --git a/healthd/healthd.rc b/healthd/healthd.rc
new file mode 100644
index 0000000..8e2ebb6
--- /dev/null
+++ b/healthd/healthd.rc
@@ -0,0 +1,4 @@
+service healthd /system/bin/healthd
+ class hal
+ critical
+ group root system wakelock
diff --git a/healthd/healthd_common.cpp b/healthd/healthd_common.cpp
deleted file mode 100644
index 6599919..0000000
--- a/healthd/healthd_common.cpp
+++ /dev/null
@@ -1,304 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#define LOG_TAG "healthd-common"
-#define KLOG_LEVEL 6
-
-#include <healthd/healthd.h>
-#include <healthd/BatteryMonitor.h>
-
-#include <errno.h>
-#include <libgen.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <batteryservice/BatteryService.h>
-#include <cutils/klog.h>
-#include <cutils/uevent.h>
-#include <sys/epoll.h>
-#include <sys/timerfd.h>
-#include <utils/Errors.h>
-
-using namespace android;
-
-#ifndef BOARD_PERIODIC_CHORES_INTERVAL_FAST
- // Periodic chores fast interval in seconds
- #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1)
-#else
- #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (BOARD_PERIODIC_CHORES_INTERVAL_FAST)
-#endif
-
-#ifndef BOARD_PERIODIC_CHORES_INTERVAL_SLOW
- // Periodic chores fast interval in seconds
- #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10)
-#else
- #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (BOARD_PERIODIC_CHORES_INTERVAL_SLOW)
-#endif
-
-static struct healthd_config healthd_config = {
- .periodic_chores_interval_fast = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST,
- .periodic_chores_interval_slow = DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW,
- .batteryStatusPath = String8(String8::kEmptyString),
- .batteryHealthPath = String8(String8::kEmptyString),
- .batteryPresentPath = String8(String8::kEmptyString),
- .batteryCapacityPath = String8(String8::kEmptyString),
- .batteryVoltagePath = String8(String8::kEmptyString),
- .batteryTemperaturePath = String8(String8::kEmptyString),
- .batteryTechnologyPath = String8(String8::kEmptyString),
- .batteryCurrentNowPath = String8(String8::kEmptyString),
- .batteryCurrentAvgPath = String8(String8::kEmptyString),
- .batteryChargeCounterPath = String8(String8::kEmptyString),
- .batteryFullChargePath = String8(String8::kEmptyString),
- .batteryCycleCountPath = String8(String8::kEmptyString),
- .energyCounter = NULL,
- .boot_min_cap = 0,
- .screen_on = NULL,
-};
-
-static int eventct;
-static int epollfd;
-
-#define POWER_SUPPLY_SUBSYSTEM "power_supply"
-
-// epoll_create() parameter is actually unused
-#define MAX_EPOLL_EVENTS 40
-static int uevent_fd;
-static int wakealarm_fd;
-
-// -1 for no epoll timeout
-static int awake_poll_interval = -1;
-
-static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
-
-static BatteryMonitor* gBatteryMonitor;
-
-struct healthd_mode_ops *healthd_mode_ops;
-
-int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup) {
- struct epoll_event ev;
-
- ev.events = EPOLLIN;
-
- if (wakeup == EVENT_WAKEUP_FD)
- ev.events |= EPOLLWAKEUP;
-
- ev.data.ptr = (void *)handler;
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
- KLOG_ERROR(LOG_TAG,
- "epoll_ctl failed; errno=%d\n", errno);
- return -1;
- }
-
- eventct++;
- return 0;
-}
-
-static void wakealarm_set_interval(int interval) {
- struct itimerspec itval;
-
- if (wakealarm_fd == -1)
- return;
-
- wakealarm_wake_interval = interval;
-
- if (interval == -1)
- interval = 0;
-
- itval.it_interval.tv_sec = interval;
- itval.it_interval.tv_nsec = 0;
- itval.it_value.tv_sec = interval;
- itval.it_value.tv_nsec = 0;
-
- if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
- KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
-}
-
-status_t healthd_get_property(int id, struct BatteryProperty *val) {
- return gBatteryMonitor->getProperty(id, val);
-}
-
-void healthd_battery_update(void) {
- // Fast wake interval when on charger (watch for overheat);
- // slow wake interval when on battery (watch for drained battery).
-
- int new_wake_interval = gBatteryMonitor->update() ?
- healthd_config.periodic_chores_interval_fast :
- healthd_config.periodic_chores_interval_slow;
-
- if (new_wake_interval != wakealarm_wake_interval)
- wakealarm_set_interval(new_wake_interval);
-
- // During awake periods poll at fast rate. If wake alarm is set at fast
- // rate then just use the alarm; if wake alarm is set at slow rate then
- // poll at fast rate while awake and let alarm wake up at slow rate when
- // asleep.
-
- if (healthd_config.periodic_chores_interval_fast == -1)
- awake_poll_interval = -1;
- else
- awake_poll_interval =
- new_wake_interval == healthd_config.periodic_chores_interval_fast ?
- -1 : healthd_config.periodic_chores_interval_fast * 1000;
-}
-
-void healthd_dump_battery_state(int fd) {
- gBatteryMonitor->dumpState(fd);
- fsync(fd);
-}
-
-static void periodic_chores() {
- healthd_battery_update();
-}
-
-#define UEVENT_MSG_LEN 2048
-static void uevent_event(uint32_t /*epevents*/) {
- char msg[UEVENT_MSG_LEN+2];
- char *cp;
- int n;
-
- n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
- if (n <= 0)
- return;
- if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
- return;
-
- msg[n] = '\0';
- msg[n+1] = '\0';
- cp = msg;
-
- while (*cp) {
- if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
- healthd_battery_update();
- break;
- }
-
- /* advance to after the next \0 */
- while (*cp++)
- ;
- }
-}
-
-static void uevent_init(void) {
- uevent_fd = uevent_open_socket(64*1024, true);
-
- if (uevent_fd < 0) {
- KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
- return;
- }
-
- fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
- if (healthd_register_event(uevent_fd, uevent_event, EVENT_WAKEUP_FD))
- KLOG_ERROR(LOG_TAG,
- "register for uevent events failed\n");
-}
-
-static void wakealarm_event(uint32_t /*epevents*/) {
- unsigned long long wakeups;
-
- if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
- KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n");
- return;
- }
-
- periodic_chores();
-}
-
-static void wakealarm_init(void) {
- wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
- if (wakealarm_fd == -1) {
- KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
- return;
- }
-
- if (healthd_register_event(wakealarm_fd, wakealarm_event, EVENT_WAKEUP_FD))
- KLOG_ERROR(LOG_TAG,
- "Registration of wakealarm event failed\n");
-
- wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
-}
-
-static void healthd_mainloop(void) {
- int nevents = 0;
- while (1) {
- struct epoll_event events[eventct];
- int timeout = awake_poll_interval;
- int mode_timeout;
-
- /* Don't wait for first timer timeout to run periodic chores */
- if (!nevents)
- periodic_chores();
-
- healthd_mode_ops->heartbeat();
-
- mode_timeout = healthd_mode_ops->preparetowait();
- if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout))
- timeout = mode_timeout;
- nevents = epoll_wait(epollfd, events, eventct, timeout);
- if (nevents == -1) {
- if (errno == EINTR)
- continue;
- KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
- break;
- }
-
- for (int n = 0; n < nevents; ++n) {
- if (events[n].data.ptr)
- (*(void (*)(int))events[n].data.ptr)(events[n].events);
- }
- }
-
- return;
-}
-
-static int healthd_init() {
- epollfd = epoll_create(MAX_EPOLL_EVENTS);
- if (epollfd == -1) {
- KLOG_ERROR(LOG_TAG,
- "epoll_create failed; errno=%d\n",
- errno);
- return -1;
- }
-
- healthd_board_init(&healthd_config);
- healthd_mode_ops->init(&healthd_config);
- wakealarm_init();
- uevent_init();
- gBatteryMonitor = new BatteryMonitor();
- gBatteryMonitor->init(&healthd_config);
- return 0;
-}
-
-int healthd_main() {
- int ret;
-
- klog_set_level(KLOG_LEVEL);
-
- if (!healthd_mode_ops) {
- KLOG_ERROR("healthd ops not set, exiting\n");
- exit(1);
- }
-
- ret = healthd_init();
- if (ret) {
- KLOG_ERROR("Initialization failed, exiting\n");
- exit(2);
- }
-
- healthd_mainloop();
- KLOG_ERROR("Main loop terminated, exiting\n");
- return 3;
-}
diff --git a/healthd/healthd_draw.cpp b/healthd/healthd_draw.cpp
index ea3d991..706dc80 100644
--- a/healthd/healthd_draw.cpp
+++ b/healthd/healthd_draw.cpp
@@ -21,77 +21,96 @@
#include "healthd_draw.h"
#define LOGE(x...) KLOG_ERROR("charger", x);
+#define LOGW(x...) KLOG_WARNING("charger", x);
#define LOGV(x...) KLOG_DEBUG("charger", x);
HealthdDraw::HealthdDraw(animation* anim)
: kSplitScreen(HEALTHD_DRAW_SPLIT_SCREEN),
kSplitOffset(HEALTHD_DRAW_SPLIT_OFFSET) {
- gr_init();
- gr_font_size(gr_sys_font(), &char_width_, &char_height_);
+ int ret = gr_init();
- screen_width_ = gr_fb_width() / (kSplitScreen ? 2 : 1);
- screen_height_ = gr_fb_height();
+ if (ret < 0) {
+ LOGE("gr_init failed\n");
+ graphics_available = false;
+ return;
+ }
- int res;
- if (!anim->text_clock.font_file.empty() &&
- (res = gr_init_font(anim->text_clock.font_file.c_str(),
- &anim->text_clock.font)) < 0) {
- LOGE("Could not load time font (%d)\n", res);
- }
- if (!anim->text_percent.font_file.empty() &&
- (res = gr_init_font(anim->text_percent.font_file.c_str(),
- &anim->text_percent.font)) < 0) {
- LOGE("Could not load percent font (%d)\n", res);
- }
+ graphics_available = true;
+ sys_font = gr_sys_font();
+ if (sys_font == nullptr) {
+ LOGW("No system font, screen fallback text not available\n");
+ } else {
+ gr_font_size(sys_font, &char_width_, &char_height_);
+ }
+
+ screen_width_ = gr_fb_width() / (kSplitScreen ? 2 : 1);
+ screen_height_ = gr_fb_height();
+
+ int res;
+ if (!anim->text_clock.font_file.empty() &&
+ (res = gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) {
+ LOGE("Could not load time font (%d)\n", res);
+ }
+ if (!anim->text_percent.font_file.empty() &&
+ (res = gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) {
+ LOGE("Could not load percent font (%d)\n", res);
+ }
}
HealthdDraw::~HealthdDraw() {}
void HealthdDraw::redraw_screen(const animation* batt_anim, GRSurface* surf_unknown) {
- clear_screen();
+ if (!graphics_available) return;
+ clear_screen();
- /* try to display *something* */
- if (batt_anim->cur_level < 0 || batt_anim->num_frames == 0)
- draw_unknown(surf_unknown);
- else
- draw_battery(batt_anim);
- gr_flip();
+ /* try to display *something* */
+ if (batt_anim->cur_level < 0 || batt_anim->num_frames == 0)
+ draw_unknown(surf_unknown);
+ else
+ draw_battery(batt_anim);
+ gr_flip();
}
-void HealthdDraw::blank_screen(bool blank) { gr_fb_blank(blank); }
+void HealthdDraw::blank_screen(bool blank) {
+ if (!graphics_available) return;
+ gr_fb_blank(blank);
+}
void HealthdDraw::clear_screen(void) {
- gr_color(0, 0, 0, 255);
- gr_clear();
+ if (!graphics_available) return;
+ gr_color(0, 0, 0, 255);
+ gr_clear();
}
int HealthdDraw::draw_surface_centered(GRSurface* surface) {
- int w = gr_get_width(surface);
- int h = gr_get_height(surface);
- int x = (screen_width_ - w) / 2 + kSplitOffset;
- int y = (screen_height_ - h) / 2;
+ if (!graphics_available) return 0;
- LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
- gr_blit(surface, 0, 0, w, h, x, y);
- if (kSplitScreen) {
- x += screen_width_ - 2 * kSplitOffset;
+ int w = gr_get_width(surface);
+ int h = gr_get_height(surface);
+ int x = (screen_width_ - w) / 2 + kSplitOffset;
+ int y = (screen_height_ - h) / 2;
+
LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
gr_blit(surface, 0, 0, w, h, x, y);
- }
+ if (kSplitScreen) {
+ x += screen_width_ - 2 * kSplitOffset;
+ LOGV("drawing surface %dx%d+%d+%d\n", w, h, x, y);
+ gr_blit(surface, 0, 0, w, h, x, y);
+ }
- return y + h;
+ return y + h;
}
int HealthdDraw::draw_text(const GRFont* font, int x, int y, const char* str) {
- int str_len_px = gr_measure(font, str);
+ if (!graphics_available) return 0;
+ int str_len_px = gr_measure(font, str);
- if (x < 0) x = (screen_width_ - str_len_px) / 2;
- if (y < 0) y = (screen_height_ - char_height_) / 2;
- gr_text(font, x + kSplitOffset, y, str, false /* bold */);
- if (kSplitScreen)
- gr_text(font, x - kSplitOffset + screen_width_, y, str, false /* bold */);
+ if (x < 0) x = (screen_width_ - str_len_px) / 2;
+ if (y < 0) y = (screen_height_ - char_height_) / 2;
+ gr_text(font, x + kSplitOffset, y, str, false /* bold */);
+ if (kSplitScreen) gr_text(font, x - kSplitOffset + screen_width_, y, str, false /* bold */);
- return y + char_height_;
+ return y + char_height_;
}
void HealthdDraw::determine_xy(const animation::text_field& field,
@@ -119,77 +138,80 @@
}
void HealthdDraw::draw_clock(const animation* anim) {
- static constexpr char CLOCK_FORMAT[] = "%H:%M";
- static constexpr int CLOCK_LENGTH = 6;
+ static constexpr char CLOCK_FORMAT[] = "%H:%M";
+ static constexpr int CLOCK_LENGTH = 6;
- const animation::text_field& field = anim->text_clock;
+ const animation::text_field& field = anim->text_clock;
- if (field.font == nullptr || field.font->char_width == 0 ||
- field.font->char_height == 0)
- return;
+ if (!graphics_available || field.font == nullptr || field.font->char_width == 0 ||
+ field.font->char_height == 0)
+ return;
- time_t rawtime;
- time(&rawtime);
- tm* time_info = localtime(&rawtime);
+ time_t rawtime;
+ time(&rawtime);
+ tm* time_info = localtime(&rawtime);
- char clock_str[CLOCK_LENGTH];
- size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info);
- if (length != CLOCK_LENGTH - 1) {
- LOGE("Could not format time\n");
- return;
- }
+ char clock_str[CLOCK_LENGTH];
+ size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info);
+ if (length != CLOCK_LENGTH - 1) {
+ LOGE("Could not format time\n");
+ return;
+ }
- int x, y;
- determine_xy(field, length, &x, &y);
+ int x, y;
+ determine_xy(field, length, &x, &y);
- LOGV("drawing clock %s %d %d\n", clock_str, x, y);
- gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
- draw_text(field.font, x, y, clock_str);
+ LOGV("drawing clock %s %d %d\n", clock_str, x, y);
+ gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
+ draw_text(field.font, x, y, clock_str);
}
void HealthdDraw::draw_percent(const animation* anim) {
- int cur_level = anim->cur_level;
- if (anim->cur_status == BATTERY_STATUS_FULL) {
- cur_level = 100;
- }
+ if (!graphics_available) return;
+ int cur_level = anim->cur_level;
+ if (anim->cur_status == BATTERY_STATUS_FULL) {
+ cur_level = 100;
+ }
- if (cur_level <= 0) return;
+ if (cur_level < 0) return;
- const animation::text_field& field = anim->text_percent;
- if (field.font == nullptr || field.font->char_width == 0 ||
- field.font->char_height == 0) {
- return;
- }
+ const animation::text_field& field = anim->text_percent;
+ if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) {
+ return;
+ }
- std::string str = base::StringPrintf("%d%%", cur_level);
+ std::string str = base::StringPrintf("%d%%", cur_level);
- int x, y;
- determine_xy(field, str.size(), &x, &y);
+ int x, y;
+ determine_xy(field, str.size(), &x, &y);
- LOGV("drawing percent %s %d %d\n", str.c_str(), x, y);
- gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
- draw_text(field.font, x, y, str.c_str());
+ LOGV("drawing percent %s %d %d\n", str.c_str(), x, y);
+ gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
+ draw_text(field.font, x, y, str.c_str());
}
void HealthdDraw::draw_battery(const animation* anim) {
- const animation::frame& frame = anim->frames[anim->cur_frame];
+ if (!graphics_available) return;
+ const animation::frame& frame = anim->frames[anim->cur_frame];
- if (anim->num_frames != 0) {
- draw_surface_centered(frame.surface);
- LOGV("drawing frame #%d min_cap=%d time=%d\n", anim->cur_frame,
- frame.min_level, frame.disp_time);
- }
- draw_clock(anim);
- draw_percent(anim);
+ if (anim->num_frames != 0) {
+ draw_surface_centered(frame.surface);
+ LOGV("drawing frame #%d min_cap=%d time=%d\n", anim->cur_frame, frame.min_level,
+ frame.disp_time);
+ }
+ draw_clock(anim);
+ draw_percent(anim);
}
void HealthdDraw::draw_unknown(GRSurface* surf_unknown) {
int y;
if (surf_unknown) {
- draw_surface_centered(surf_unknown);
+ draw_surface_centered(surf_unknown);
+ } else if (sys_font) {
+ gr_color(0xa4, 0xc6, 0x39, 255);
+ y = draw_text(sys_font, -1, -1, "Charging!");
+ draw_text(sys_font, -1, y + 25, "?\?/100");
} else {
- gr_color(0xa4, 0xc6, 0x39, 255);
- y = draw_text(gr_sys_font(), -1, -1, "Charging!");
- draw_text(gr_sys_font(), -1, y + 25, "?\?/100");
+ LOGW("Charging, level unknown\n");
}
}
diff --git a/healthd/healthd_draw.h b/healthd/healthd_draw.h
index 6a6ba76..7c847bd 100644
--- a/healthd/healthd_draw.h
+++ b/healthd/healthd_draw.h
@@ -70,6 +70,12 @@
const bool kSplitScreen;
// Pixels to offset graphics towards center split.
const int kSplitOffset;
+
+ // system text font, may be nullptr
+ const GRFont* sys_font;
+
+ // true if minui init'ed OK, false if minui init failed
+ bool graphics_available;
};
#endif // HEALTHD_DRAW_H
diff --git a/healthd/healthd_mode_android.cpp b/healthd/healthd_mode_android.cpp
deleted file mode 100644
index c612313..0000000
--- a/healthd/healthd_mode_android.cpp
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#define LOG_TAG "healthd-android"
-
-#include <healthd/healthd.h>
-#include "BatteryPropertiesRegistrar.h"
-
-#include <binder/IPCThreadState.h>
-#include <binder/ProcessState.h>
-#include <cutils/klog.h>
-#include <sys/epoll.h>
-
-using namespace android;
-
-static int gBinderFd;
-static sp<BatteryPropertiesRegistrar> gBatteryPropertiesRegistrar;
-
-void healthd_mode_android_battery_update(
- struct android::BatteryProperties *props) {
- if (gBatteryPropertiesRegistrar != NULL)
- gBatteryPropertiesRegistrar->notifyListeners(*props);
-
- return;
-}
-
-int healthd_mode_android_preparetowait(void) {
- IPCThreadState::self()->flushCommands();
- return -1;
-}
-
-void healthd_mode_android_heartbeat(void) {
-}
-
-static void binder_event(uint32_t /*epevents*/) {
- IPCThreadState::self()->handlePolledCommands();
-}
-
-void healthd_mode_android_init(struct healthd_config* /*config*/) {
- ProcessState::self()->setThreadPoolMaxThreadCount(0);
- IPCThreadState::self()->disableBackgroundScheduling(true);
- IPCThreadState::self()->setupPolling(&gBinderFd);
-
- if (gBinderFd >= 0) {
- if (healthd_register_event(gBinderFd, binder_event))
- KLOG_ERROR(LOG_TAG,
- "Register for binder events failed\n");
- }
-
- gBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar();
- gBatteryPropertiesRegistrar->publish(gBatteryPropertiesRegistrar);
-}
diff --git a/healthd/healthd_mode_charger.cpp b/healthd/healthd_mode_charger.cpp
index 4f77e7a..2eb5497 100644
--- a/healthd/healthd_mode_charger.cpp
+++ b/healthd/healthd_mode_charger.cpp
@@ -49,6 +49,7 @@
#include "AnimationParser.h"
#include "healthd_draw.h"
+#include <health2/Health.h>
#include <healthd/healthd.h>
using namespace android;
@@ -71,6 +72,7 @@
#define BATTERY_UNKNOWN_TIME (2 * MSEC_PER_SEC)
#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 LAST_KMSG_MAX_SZ (32 * 1024)
@@ -90,6 +92,7 @@
struct charger {
bool have_battery_state;
bool charger_connected;
+ bool screen_blanked;
int64_t next_screen_transition;
int64_t next_key_check;
int64_t next_pwr_check;
@@ -292,6 +295,7 @@
#ifndef CHARGER_DISABLE_INIT_BLANK
healthd_draw->blank_screen(true);
+ charger->screen_blanked = true;
#endif
}
@@ -300,6 +304,7 @@
reset_animation(batt_anim);
charger->next_screen_transition = -1;
healthd_draw->blank_screen(true);
+ charger->screen_blanked = true;
LOGV("[%" PRId64 "] animation done\n", now);
if (charger->charger_connected) request_suspend(true);
return;
@@ -307,6 +312,11 @@
disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time;
+ if (charger->screen_blanked) {
+ healthd_draw->blank_screen(false);
+ charger->screen_blanked = false;
+ }
+
/* animation starting, set up the animation */
if (batt_anim->cur_frame == 0) {
LOGV("[%" PRId64 "] animation starting\n", now);
@@ -323,16 +333,19 @@
}
}
- // repeat the first frame first_frame_repeats times
- disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time *
- batt_anim->first_frame_repeats;
+ if (charger->charger_connected) {
+ // repeat the first frame first_frame_repeats times
+ disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time *
+ batt_anim->first_frame_repeats;
+ } else {
+ disp_time = UNPLUGGED_DISPLAY_TIME / batt_anim->num_cycles;
+ }
+
+ LOGV("cur_frame=%d disp_time=%d\n", batt_anim->cur_frame, disp_time);
}
}
}
- /* unblank the screen on first cycle */
- if (batt_anim->cur_cycle == 0) healthd_draw->blank_screen(false);
-
/* draw the new frame (@ cur_frame) */
healthd_draw->redraw_screen(charger->batt_anim, charger->surf_unknown);
@@ -347,7 +360,7 @@
}
/* schedule next screen transition */
- charger->next_screen_transition = now + disp_time;
+ charger->next_screen_transition = curr_time_ms() + disp_time;
/* advance frame cntr to the next valid frame only if we are charging
* if necessary, advance cycle cntr, and reset frame cntr
@@ -457,6 +470,7 @@
/* if the power key got released, force screen state cycle */
if (key->pending) {
kick_animation(charger->batt_anim);
+ request_suspend(false);
}
}
}
@@ -475,12 +489,18 @@
if (!charger->have_battery_state) return;
if (!charger->charger_connected) {
- /* Last cycle would have stopped at the extreme top of battery-icon
- * Need to show the correct level corresponding to capacity.
- */
- kick_animation(charger->batt_anim);
request_suspend(false);
if (charger->next_pwr_check == -1) {
+ /* Last cycle would have stopped at the extreme top of battery-icon
+ * Need to show the correct level corresponding to capacity.
+ *
+ * Reset next_screen_transition to update screen immediately.
+ * Reset & kick animation to show complete animation cycles
+ * when charger disconnected.
+ */
+ charger->next_screen_transition = now - 1;
+ reset_animation(charger->batt_anim);
+ kick_animation(charger->batt_anim);
charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
LOGW("[%" PRId64 "] device unplugged: shutting down in %" PRId64 " (@ %" PRId64 ")\n",
now, (int64_t)UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
@@ -493,8 +513,15 @@
} else {
/* online supply present, reset shutdown timer if set */
if (charger->next_pwr_check != -1) {
- LOGW("[%" PRId64 "] device plugged in: shutdown cancelled\n", now);
+ /* Reset next_screen_transition to update screen immediately.
+ * Reset & kick animation to show complete animation cycles
+ * when charger connected again.
+ */
+ request_suspend(false);
+ charger->next_screen_transition = now - 1;
+ reset_animation(charger->batt_anim);
kick_animation(charger->batt_anim);
+ LOGW("[%" PRId64 "] device plugged in: shutdown cancelled\n", now);
}
charger->next_pwr_check = -1;
}
@@ -522,6 +549,7 @@
if (!charger->have_battery_state) {
charger->have_battery_state = true;
charger->next_screen_transition = curr_time_ms() - 1;
+ request_suspend(false);
reset_animation(charger->batt_anim);
kick_animation(charger->batt_anim);
}
@@ -612,6 +640,8 @@
}
void healthd_mode_charger_init(struct healthd_config* config) {
+ using android::hardware::health::V2_0::implementation::Health;
+
int ret;
charger* charger = &charger_state;
int i;
@@ -632,7 +662,7 @@
ret = res_create_display_surface(anim->fail_file.c_str(), &charger->surf_unknown);
if (ret < 0) {
- LOGE("Cannot load custom battery_fail image. Reverting to built in.\n");
+ LOGE("Cannot load custom battery_fail image. Reverting to built in: %d\n", ret);
ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
if (ret < 0) {
LOGE("Cannot load built in battery_fail image\n");
@@ -666,6 +696,10 @@
charger->next_screen_transition = -1;
charger->next_key_check = -1;
charger->next_pwr_check = -1;
+
+ // Initialize Health implementation (which initializes the internal BatteryMonitor).
+ Health::initInstance(config);
+
healthd_config = config;
charger->boot_min_cap = config->boot_min_cap;
}
diff --git a/healthd/include/healthd/BatteryMonitor.h b/healthd/include/healthd/BatteryMonitor.h
index 194e667..4d1d53f 100644
--- a/healthd/include/healthd/BatteryMonitor.h
+++ b/healthd/include/healthd/BatteryMonitor.h
@@ -18,7 +18,6 @@
#define HEALTHD_BATTERYMONITOR_H
#include <batteryservice/BatteryService.h>
-#include <binder/IInterface.h>
#include <utils/String8.h>
#include <utils/Vector.h>
@@ -43,6 +42,7 @@
int getChargeStatus();
status_t getProperty(int id, struct BatteryProperty *val);
void dumpState(int fd);
+ friend struct BatteryProperties getBatteryProperties(BatteryMonitor* batteryMonitor);
private:
struct healthd_config *mHealthdConfig;
diff --git a/healthd/include/healthd/healthd.h b/healthd/include/healthd/healthd.h
index 17efbd6..c01e8d7 100644
--- a/healthd/include/healthd/healthd.h
+++ b/healthd/include/healthd/healthd.h
@@ -81,10 +81,6 @@
// Global helper functions
int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup = EVENT_NO_WAKEUP_FD);
-void healthd_battery_update();
-android::status_t healthd_get_property(int id,
- struct android::BatteryProperty *val);
-void healthd_dump_battery_state(int fd);
struct healthd_mode_ops {
void (*init)(struct healthd_config *config);
diff --git a/healthd/manifest_healthd.xml b/healthd/manifest_healthd.xml
new file mode 100644
index 0000000..097a7d8
--- /dev/null
+++ b/healthd/manifest_healthd.xml
@@ -0,0 +1,11 @@
+<manifest version="1.0" type="framework">
+ <hal>
+ <name>android.hardware.health</name>
+ <transport>hwbinder</transport>
+ <version>2.0</version>
+ <interface>
+ <name>IHealth</name>
+ <instance>backup</instance>
+ </interface>
+ </hal>
+</manifest>
diff --git a/init/Android.bp b/init/Android.bp
index 31c8efb..84a78e2 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -67,18 +67,9 @@
"libsquashfs_utils",
"liblogwrap",
"libext4_utils",
- "libcutils",
- "libbase",
- "libc",
"libseccomp_policy",
- "libselinux",
- "liblog",
"libcrypto_utils",
- "libcrypto",
- "libc++_static",
- "libdl",
"libsparse",
- "libz",
"libprocessgroup",
"libavb",
"libkeyutils",
@@ -86,10 +77,22 @@
"libpropertyinfoserializer",
"libpropertyinfoparser",
],
+ shared_libs: [
+ "libcutils",
+ "libbase",
+ "libc",
+ "liblog",
+ "libcrypto",
+ "libc++",
+ "libdl",
+ "libz",
+ "libselinux",
+ ],
}
cc_library_static {
name: "libinit",
+ recovery_available: true,
defaults: ["init_defaults"],
srcs: [
"action.cpp",
@@ -100,18 +103,20 @@
"capabilities.cpp",
"descriptors.cpp",
"devices.cpp",
+ "epoll.cpp",
"firmware_handler.cpp",
+ "first_stage_mount.cpp",
"import_parser.cpp",
"init.cpp",
- "init_first_stage.cpp",
"keychords.cpp",
- "log.cpp",
+ "modalias_handler.cpp",
"parser.cpp",
"persistent_properties.cpp",
"persistent_properties.proto",
"property_service.cpp",
"property_type.cpp",
"reboot.cpp",
+ "reboot_utils.cpp",
"security.cpp",
"selinux.cpp",
"service.cpp",
@@ -124,42 +129,30 @@
"ueventd.cpp",
"ueventd_parser.cpp",
"util.cpp",
- "watchdogd.cpp",
],
whole_static_libs: ["libcap"],
- include_dirs: [
- "system/core/mkbootimg",
- ],
+ header_libs: ["bootimg_headers"],
proto: {
type: "lite",
export_proto_headers: true,
},
}
-/*
-This is not yet ready, see the below TODOs for what is missing
-
cc_binary {
- // TODO: Missing,
- //LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)
- //LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
-
- name: "init",
+ name: "init_second_stage",
+ recovery_available: true,
+ stem: "init",
defaults: ["init_defaults"],
+ static_libs: ["libinit"],
required: [
"e2fsdroid",
"mke2fs",
"sload_f2fs",
"make_f2fs",
],
- static_executable: true,
srcs: ["main.cpp"],
- symlinks: [
- "sbin/ueventd",
- "sbin/watchdogd",
- ],
+ symlinks: ["ueventd"],
}
-*/
// Tests
// ------------------------------------------------------------------------------
@@ -167,10 +160,10 @@
cc_test {
name: "init_tests",
defaults: ["init_defaults"],
- static_executable: true,
srcs: [
"devices_test.cpp",
"init_test.cpp",
+ "keychords_test.cpp",
"persistent_properties_test.cpp",
"property_service_test.cpp",
"property_type_test.cpp",
@@ -178,15 +171,17 @@
"rlimit_parser_test.cpp",
"service_test.cpp",
"subcontext_test.cpp",
+ "tokenizer_test.cpp",
+ "ueventd_parser_test.cpp",
"ueventd_test.cpp",
"util_test.cpp",
],
static_libs: ["libinit"],
+ test_suites: ["device-tests"],
}
cc_benchmark {
name: "init_benchmarks",
- static_executable: true,
defaults: ["init_defaults"],
srcs: [
"subcontext_benchmark.cpp",
@@ -232,8 +227,11 @@
"action_parser.cpp",
"capabilities.cpp",
"descriptors.cpp",
+ "epoll.cpp",
+ "keychords.cpp",
"import_parser.cpp",
- "host_init_parser.cpp",
+ "host_import_parser.cpp",
+ "host_init_verifier.cpp",
"host_init_stubs.cpp",
"parser.cpp",
"rlimit_parser.cpp",
@@ -246,7 +244,10 @@
proto: {
type: "lite",
},
- generated_headers: ["generated_stub_builtin_function_map"],
+ generated_headers: [
+ "generated_stub_builtin_function_map",
+ "generated_android_ids"
+ ],
target: {
android: {
enabled: false,
diff --git a/init/Android.mk b/init/Android.mk
index c4a6a50..ada87b8 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -41,53 +41,46 @@
include $(CLEAR_VARS)
LOCAL_CPPFLAGS := $(init_cflags)
-LOCAL_SRC_FILES := main.cpp
+LOCAL_SRC_FILES := \
+ devices.cpp \
+ first_stage_mount.cpp \
+ init_first_stage.cpp \
+ reboot_utils.cpp \
+ selinux.cpp \
+ uevent_listener.cpp \
+ util.cpp \
-LOCAL_MODULE:= init
+LOCAL_MODULE := init
LOCAL_FORCE_STATIC_EXECUTABLE := true
+
LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
LOCAL_STATIC_LIBRARIES := \
- libinit \
- libbootloader_message \
libfs_mgr \
libfec \
libfec_rs \
- libhidl-gen-utils \
libsquashfs_utils \
liblogwrap \
libext4_utils \
- libcutils \
- libbase \
- libc \
libseccomp_policy \
- libselinux \
- liblog \
libcrypto_utils \
- libcrypto \
- libc++_static \
- libdl \
libsparse \
- libz \
- libprocessgroup \
libavb \
libkeyutils \
- libprotobuf-cpp-lite \
- libpropertyinfoserializer \
- libpropertyinfoparser \
+ liblp \
+ libcutils \
+ libbase \
+ liblog \
+ libcrypto \
+ libdl \
+ libz \
+ libselinux \
+ libcap \
LOCAL_REQUIRED_MODULES := \
- e2fsdroid \
- mke2fs \
- sload_f2fs \
- make_f2fs \
-
-# Create symlinks.
-LOCAL_POST_INSTALL_CMD := $(hide) mkdir -p $(TARGET_ROOT_OUT)/sbin; \
- ln -sf ../init $(TARGET_ROOT_OUT)/sbin/ueventd; \
- ln -sf ../init $(TARGET_ROOT_OUT)/sbin/watchdogd
+ init_second_stage \
LOCAL_SANITIZE := signed-integer-overflow
include $(BUILD_EXECUTABLE)
diff --git a/init/README.md b/init/README.md
index 5c2352b..b0a73b9 100644
--- a/init/README.md
+++ b/init/README.md
@@ -161,6 +161,25 @@
Options are modifiers to services. They affect how and when init
runs the service.
+`capabilities <capability> [ <capability>\* ]`
+> Set capabilities when exec'ing this service. 'capability' should be a Linux
+ capability without the "CAP\_" prefix, like "NET\_ADMIN" or "SETPCAP". See
+ http://man7.org/linux/man-pages/man7/capabilities.7.html for a list of Linux
+ capabilities.
+
+`class <name> [ <name>\* ]`
+> Specify class names for the service. All services in a
+ named class may be started or stopped together. A service
+ is in the class "default" if one is not specified via the
+ class option. Additional classnames beyond the (required) first
+ one are used to group services.
+ The `animation` class should include all services necessary for both
+ boot animation and shutdown animation. As these services can be
+ launched very early during bootup and can run until the last stage
+ of shutdown, access to /data partition is not guaranteed. These
+ services can check files under /data but it should not keep files opened
+ and should work when /data is not available.
+
`console [<console>]`
> This service needs a console. The optional second parameter chooses a
specific console instead of the default. The default "/dev/console" can
@@ -174,11 +193,103 @@
`disabled`
> This service will not automatically start with its class.
- It must be explicitly started by name.
+ It must be explicitly started by name or by interface name.
+
+`enter_namespace <type> <path>`
+> Enters the namespace of type _type_ located at _path_. Only network namespaces are supported with
+ _type_ set to "net". Note that only one namespace of a given _type_ may be entered.
+
+`file <path> <type>`
+> Open a file path and pass its fd to the launched process. _type_ must be
+ "r", "w" or "rw". For native executables see libcutils
+ android\_get\_control\_file().
+
+`group <groupname> [ <groupname>\* ]`
+> Change to 'groupname' before exec'ing this service. Additional
+ groupnames beyond the (required) first one are used to set the
+ supplemental groups of the process (via setgroups()).
+ Currently defaults to root. (??? probably should default to nobody)
+
+`interface <interface name> <instance name>`
+> Associates this service with a list of the HIDL services that it provides. The interface name
+ must be a fully-qualified name and not a value name. This is used to allow hwservicemanager to
+ lazily start services.
+ For example: interface vendor.foo.bar@1.0::IBaz default
+
+`ioprio <class> <priority>`
+> Sets the IO priority and IO priority class for this service via the SYS_ioprio_set syscall.
+ _class_ must be one of "rt", "be", or "idle". _priority_ must be an integer in the range 0 - 7.
+
+`keycodes <keycode> [ <keycode>\* ]`
+> Sets the keycodes that will trigger this service. If all of the keys corresponding to the passed
+ keycodes are pressed at once, the service will start. This is typically used to start the
+ bugreport service.
+
+`memcg.limit_in_bytes <value>`
+> Sets the child's memory.limit_in_bytes to the specified value (only if memcg is mounted),
+ which must be equal or greater than 0.
+
+`memcg.soft_limit_in_bytes <value>`
+> Sets the child's memory.soft_limit_in_bytes to the specified value (only if memcg is mounted),
+ which must be equal or greater than 0.
+
+`memcg.swappiness <value>`
+> Sets the child's memory.swappiness to the specified value (only if memcg is mounted),
+ which must be equal or greater than 0.
+
+`namespace <pid|mnt>`
+> Enter a new PID or mount namespace when forking the service.
+
+`oneshot`
+> Do not restart the service when it exits.
+
+`onrestart`
+> Execute a Command (see below) when service restarts.
+
+`oom_score_adjust <value>`
+> Sets the child's /proc/self/oom\_score\_adj to the specified value,
+ which must range from -1000 to 1000.
+
+`override`
+> Indicates that this service definition is meant to override a previous definition for a service
+ with the same name. This is typically meant for services on /odm to override those defined on
+ /vendor. The last service definition that init parses with this keyword is the service definition
+ will use for this service. Pay close attention to the order in which init.rc files are parsed,
+ since it has some peculiarities for backwards compatibility reasons. The 'imports' section of
+ this file has more details on the order.
+
+`priority <priority>`
+> Scheduling priority of the service process. This value has to be in range
+ -20 to 19. Default priority is 0. Priority is set via setpriority().
+
+`rlimit <resource> <cur> <max>`
+> This applies the given rlimit to the service. rlimits are inherited by child
+ processes, so this effectively applies the given rlimit to the process tree
+ started by this service.
+ It is parsed similarly to the setrlimit command specified below.
+
+`seclabel <seclabel>`
+> Change to 'seclabel' before exec'ing this service.
+ Primarily for use by services run from the rootfs, e.g. ueventd, adbd.
+ Services on the system partition can instead use policy-defined transitions
+ based on their file security context.
+ If not specified and no transition is defined in policy, defaults to the init context.
`setenv <name> <value>`
> Set the environment variable _name_ to _value_ in the launched process.
+`shutdown <shutdown_behavior>`
+> Set shutdown behavior of the service process. When this is not specified,
+ the service is killed during shutdown process by using SIGTERM and SIGKILL.
+ The service with shutdown_behavior of "critical" is not killed during shutdown
+ until shutdown times out. When shutdown times out, even services tagged with
+ "shutdown critical" will be killed. When the service tagged with "shutdown critical"
+ is not running when shut down starts, it will be started.
+
+`sigstop`
+> Send SIGSTOP to the service immediately before exec is called. This is intended for debugging.
+ See the below section on debugging for how this can be used.
+
`socket <name> <type> <perm> [ <user> [ <group> [ <seclabel> ] ] ]`
> Create a unix domain socket named /dev/socket/_name_ and pass its fd to the
launched process. _type_ must be "dgram", "stream" or "seqpacket". User and
@@ -187,11 +298,6 @@
seclabel or computed based on the service executable file security context.
For native executables see libcutils android\_get\_control\_socket().
-`file <path> <type>`
-> Open a file path and pass its fd to the launched process. _type_ must be
- "r", "w" or "rw". For native executables see libcutils
- android\_get\_control\_file().
-
`user <username>`
> Change to 'username' before exec'ing this service.
Currently defaults to root. (??? probably should default to nobody)
@@ -208,88 +314,12 @@
As of Android O, processes can also request capabilities directly in their .rc
files. See the "capabilities" option below.
-`group <groupname> [ <groupname>\* ]`
-> Change to 'groupname' before exec'ing this service. Additional
- groupnames beyond the (required) first one are used to set the
- supplemental groups of the process (via setgroups()).
- Currently defaults to root. (??? probably should default to nobody)
-
-`capabilities <capability> [ <capability>\* ]`
-> Set capabilities when exec'ing this service. 'capability' should be a Linux
- capability without the "CAP\_" prefix, like "NET\_ADMIN" or "SETPCAP". See
- http://man7.org/linux/man-pages/man7/capabilities.7.html for a list of Linux
- capabilities.
-
-`setrlimit <resource> <cur> <max>`
-> This applies the given rlimit to the service. rlimits are inherited by child
- processes, so this effectively applies the given rlimit to the process tree
- started by this service.
- It is parsed similarly to the setrlimit command specified below.
-
-`seclabel <seclabel>`
-> Change to 'seclabel' before exec'ing this service.
- Primarily for use by services run from the rootfs, e.g. ueventd, adbd.
- Services on the system partition can instead use policy-defined transitions
- based on their file security context.
- If not specified and no transition is defined in policy, defaults to the init context.
-
-`oneshot`
-> Do not restart the service when it exits.
-
-`class <name> [ <name>\* ]`
-> Specify class names for the service. All services in a
- named class may be started or stopped together. A service
- is in the class "default" if one is not specified via the
- class option. Additional classnames beyond the (required) first
- one are used to group services.
-`animation class`
-> 'animation' class should include all services necessary for both
- boot animation and shutdown animation. As these services can be
- launched very early during bootup and can run until the last stage
- of shutdown, access to /data partition is not guaranteed. These
- services can check files under /data but it should not keep files opened
- and should work when /data is not available.
-
-`onrestart`
-> Execute a Command (see below) when service restarts.
-
`writepid <file> [ <file>\* ]`
> Write the child's pid to the given files when it forks. Meant for
cgroup/cpuset usage. If no files under /dev/cpuset/ are specified, but the
system property 'ro.cpuset.default' is set to a non-empty cpuset name (e.g.
'/foreground'), then the pid is written to file /dev/cpuset/_cpuset\_name_/tasks.
-`priority <priority>`
-> Scheduling priority of the service process. This value has to be in range
- -20 to 19. Default priority is 0. Priority is set via setpriority().
-
-`namespace <pid|mnt>`
-> Enter a new PID or mount namespace when forking the service.
-
-`oom_score_adjust <value>`
-> Sets the child's /proc/self/oom\_score\_adj to the specified value,
- which must range from -1000 to 1000.
-
-`memcg.swappiness <value>`
-> Sets the child's memory.swappiness to the specified value (only if memcg is mounted),
- which must be equal or greater than 0.
-
-`memcg.soft_limit_in_bytes <value>`
-> Sets the child's memory.soft_limit_in_bytes to the specified value (only if memcg is mounted),
- which must be equal or greater than 0.
-
-`memcg.limit_in_bytes <value>`
-> Sets the child's memory.limit_in_bytes to the specified value (only if memcg is mounted),
- which must be equal or greater than 0.
-
-`shutdown <shutdown_behavior>`
-> Set shutdown behavior of the service process. When this is not specified,
- the service is killed during shutdown process by using SIGTERM and SIGKILL.
- The service with shutdown_behavior of "critical" is not killed during shutdown
- until shutdown times out. When shutdown times out, even services tagged with
- "shutdown critical" will be killed. When the service tagged with "shutdown critical"
- is not running when shut down starts, it will be started.
-
Triggers
--------
@@ -686,23 +716,58 @@
Debugging init
--------------
-By default, programs executed by init will drop stdout and stderr into
-/dev/null. To help with debugging, you can execute your program via the
-Android program logwrapper. This will redirect stdout/stderr into the
-Android logging system (accessed via logcat).
+Launching init services without init is not recommended as init sets up a significant amount of
+environment (user, groups, security label, capabilities, etc) that is hard to replicate manually.
-For example
-service akmd /system/bin/logwrapper /sbin/akmd
+If it is required to debug a service from its very start, the `sigstop` service option is added.
+This option will send SIGSTOP to a service immediately before calling exec. This gives a window
+where developers can attach a debugger, strace, etc before continuing the service with SIGCONT.
-For quicker turnaround when working on init itself, use:
+This flag can also be dynamically controled via the ctl.sigstop_on and ctl.sigstop_off properties.
- mm -j &&
- m ramdisk-nodeps &&
- m bootimage-nodeps &&
- adb reboot bootloader &&
- fastboot boot $ANDROID_PRODUCT_OUT/boot.img
+Below is an example of dynamically debugging logd via the above:
-Alternatively, use the emulator:
+ stop logd
+ setprop ctl.sigstop_on logd
+ start logd
+ ps -e | grep logd
+ > logd 4343 1 18156 1684 do_signal_stop 538280 T init
+ gdbclient.py -p 4343
+ b main
+ c
+ c
+ c
+ > Breakpoint 1, main (argc=1, argv=0x7ff8c9a488) at system/core/logd/main.cpp:427
- emulator -partition-size 1024 \
- -verbose -show-kernel -no-window
+Below is an example of doing the same but with strace
+
+ stop logd
+ setprop ctl.sigstop_on logd
+ start logd
+ ps -e | grep logd
+ > logd 4343 1 18156 1684 do_signal_stop 538280 T init
+ strace -p 4343
+
+ (From a different shell)
+ kill -SIGCONT 4343
+
+ > strace runs
+
+Host Init Script Verification
+-----------------------------
+
+Init scripts are checked for correctness during build time. Specifically the below is checked.
+
+1) Well formatted action, service and import sections, e.g. no actions without a preceding 'on'
+line, and no extraneous lines after an 'import' statement.
+2) All commands map to a valid keyword and the argument count is within the correct range.
+3) All service options are valid. This is stricter than how commands are checked as the service
+options' arguments are fully parsed, e.g. UIDs and GIDs must resolve.
+
+There are other parts of init scripts that are only parsed at runtime and therefore not checked
+during build time, among them are the below.
+
+1) The validity of the arguments of commands, e.g. no checking if file paths actually exist, if
+SELinux would permit the operation, or if the UIDs and GIDs resolve.
+2) No checking if a service exists or has a valid SELinux domain defined
+3) No checking if a service has not been previously defined in a different init script.
diff --git a/init/action.cpp b/init/action.cpp
index f782b51..11335ca 100644
--- a/init/action.cpp
+++ b/init/action.cpp
@@ -18,16 +18,11 @@
#include <android-base/chrono_utils.h>
#include <android-base/logging.h>
+#include <android-base/properties.h>
#include <android-base/strings.h>
#include "util.h"
-#if defined(__ANDROID__)
-#include <android-base/properties.h>
-#else
-#include "host_init_stubs.h"
-#endif
-
using android::base::Join;
namespace android {
diff --git a/init/action_parser.cpp b/init/action_parser.cpp
index a2c9671..1481162 100644
--- a/init/action_parser.cpp
+++ b/init/action_parser.cpp
@@ -16,16 +16,11 @@
#include "action_parser.h"
+#include <android-base/properties.h>
#include <android-base/strings.h>
#include "stable_properties.h"
-#if defined(__ANDROID__)
-#include <android-base/properties.h>
-#else
-#include "host_init_stubs.h"
-#endif
-
using android::base::GetBoolProperty;
using android::base::StartsWith;
@@ -65,7 +60,7 @@
prop_name.erase(equal_pos);
if (!IsActionableProperty(subcontext, prop_name)) {
- return Error() << "unexported property tigger found: " << prop_name;
+ return Error() << "unexported property trigger found: " << prop_name;
}
if (auto [it, inserted] = property_triggers->emplace(prop_name, prop_value); !inserted) {
diff --git a/init/bootchart.cpp b/init/bootchart.cpp
index 379b4fa..c2cf573 100644
--- a/init/bootchart.cpp
+++ b/init/bootchart.cpp
@@ -32,12 +32,14 @@
#include <mutex>
#include <thread>
+#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
using android::base::StringPrintf;
+using android::base::boot_clock;
using namespace std::chrono_literals;
namespace android {
@@ -50,9 +52,9 @@
static bool g_bootcharting_finished;
static long long get_uptime_jiffies() {
- std::string uptime;
- if (!android::base::ReadFileToString("/proc/uptime", &uptime)) return 0;
- return 100LL * strtod(uptime.c_str(), NULL);
+ constexpr int64_t kNanosecondsPerJiffy = 10000000;
+ boot_clock::time_point uptime = boot_clock::now();
+ return uptime.time_since_epoch().count() / kNanosecondsPerJiffy;
}
static std::unique_ptr<FILE, decltype(&fclose)> fopen_unique(const char* filename,
diff --git a/init/builtins.cpp b/init/builtins.cpp
index fc74dda..17d34e1 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -82,6 +82,7 @@
static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
static Result<Success> reboot_into_recovery(const std::vector<std::string>& options) {
+ LOG(ERROR) << "Rebooting into recovery";
std::string err;
if (!write_bootloader_message(options, &err)) {
return Error() << "Failed to set bootloader message: " << err;
@@ -239,6 +240,29 @@
return Success();
}
+static Result<Success> do_interface_restart(const BuiltinArguments& args) {
+ Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
+ if (!svc) return Error() << "interface " << args[1] << " not found";
+ svc->Restart();
+ return Success();
+}
+
+static Result<Success> do_interface_start(const BuiltinArguments& args) {
+ Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
+ if (!svc) return Error() << "interface " << args[1] << " not found";
+ if (auto result = svc->Start(); !result) {
+ return Error() << "Could not start interface: " << result.error();
+ }
+ return Success();
+}
+
+static Result<Success> do_interface_stop(const BuiltinArguments& args) {
+ Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
+ if (!svc) return Error() << "interface " << args[1] << " not found";
+ svc->Stop();
+ return Success();
+}
+
// mkdir <path> [mode] [owner] [group]
static Result<Success> do_mkdir(const BuiltinArguments& args) {
mode_t mode = 0755;
@@ -285,11 +309,8 @@
if (e4crypt_is_native()) {
if (e4crypt_set_directory_policy(args[1].c_str())) {
- const std::vector<std::string> options = {
- "--prompt_and_wipe_data",
- "--reason=set_policy_failed:"s + args[1]};
- reboot_into_recovery(options);
- return Success();
+ return reboot_into_recovery(
+ {"--prompt_and_wipe_data", "--reason=set_policy_failed:"s + args[1]});
}
}
return Success();
@@ -493,8 +514,7 @@
/* Setup a wipe via recovery, and reboot into recovery */
PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
- reboot_into_recovery(options);
- return Success();
+ return reboot_into_recovery(options);
/* If reboot worked, there is no return. */
} else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
if (e4crypt_install_keyring()) {
@@ -522,6 +542,7 @@
if (e4crypt_install_keyring()) {
return Error() << "e4crypt_install_keyring() failed";
}
+ property_set("ro.crypto.state", "encrypted");
property_set("ro.crypto.type", "file");
// Although encrypted, vold has already set the device up, so we do not need to
@@ -987,6 +1008,29 @@
return android::base::GetProperty("ro.crypto.type", "") == "file";
}
+static Result<Success> ExecWithRebootOnFailure(const std::string& reboot_reason,
+ const BuiltinArguments& args) {
+ auto service = Service::MakeTemporaryOneshotService(args.args);
+ if (!service) {
+ return Error() << "Could not create exec service";
+ }
+ service->AddReapCallback([reboot_reason](const siginfo_t& siginfo) {
+ if (siginfo.si_code != CLD_EXITED || siginfo.si_status != 0) {
+ if (e4crypt_is_native()) {
+ LOG(ERROR) << "Rebooting into recovery, reason: " << reboot_reason;
+ reboot_into_recovery({"--prompt_and_wipe_data", "--reason="s + reboot_reason});
+ } else {
+ LOG(ERROR) << "Failure (reboot suppressed): " << reboot_reason;
+ }
+ }
+ });
+ if (auto result = service->ExecStart(); !result) {
+ return Error() << "Could not start exec service: " << result.error();
+ }
+ ServiceList::GetInstance().AddService(std::move(service));
+ return Success();
+}
+
static Result<Success> do_installkey(const BuiltinArguments& args) {
if (!is_file_crypto()) return Success();
@@ -994,15 +1038,15 @@
if (!make_dir(unencrypted_dir, 0700) && errno != EEXIST) {
return ErrnoError() << "Failed to create " << unencrypted_dir;
}
- std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
- "enablefilecrypto"};
- return do_exec({std::move(exec_args), args.context});
+ return ExecWithRebootOnFailure(
+ "enablefilecrypto_failed",
+ {{"exec", "/system/bin/vdc", "--wait", "cryptfs", "enablefilecrypto"}, args.context});
}
static Result<Success> do_init_user0(const BuiltinArguments& args) {
- std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
- "init_user0"};
- return do_exec({std::move(exec_args), args.context});
+ return ExecWithRebootOnFailure(
+ "init_user0_failed",
+ {{"exec", "/system/bin/vdc", "--wait", "cryptfs", "init_user0"}, args.context});
}
// Builtin-function-map start
@@ -1029,6 +1073,9 @@
{"init_user0", {0, 0, {false, do_init_user0}}},
{"insmod", {1, kMax, {true, do_insmod}}},
{"installkey", {1, 1, {false, do_installkey}}},
+ {"interface_restart", {1, 1, {false, do_interface_restart}}},
+ {"interface_start", {1, 1, {false, do_interface_start}}},
+ {"interface_stop", {1, 1, {false, do_interface_stop}}},
{"load_persist_props", {0, 0, {false, do_load_persist_props}}},
{"load_system_props", {0, 0, {false, do_load_system_props}}},
{"loglevel", {1, 1, {false, do_loglevel}}},
diff --git a/init/devices.cpp b/init/devices.cpp
index 8d27f4f..58c8b2e 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -31,11 +31,10 @@
#include <selinux/selinux.h>
#include "selinux.h"
-#include "ueventd.h"
#include "util.h"
#ifdef _INIT_INIT_H
-#error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals"
+#error "Do not include init.h in files used by ueventd; it will expose init's globals"
#endif
using android::base::Basename;
@@ -329,10 +328,10 @@
<< partition_name_sanitized << "'";
}
links.emplace_back(link_path + "/by-name/" + partition_name_sanitized);
- }
-
- if (uevent.partition_num >= 0) {
- links.emplace_back(link_path + "/by-num/p" + std::to_string(uevent.partition_num));
+ // Adds symlink: /dev/block/by-name/<partition_name>.
+ if (boot_devices_.find(device) != boot_devices_.end()) {
+ links.emplace_back("/dev/block/by-name/" + partition_name_sanitized);
+ }
}
auto last_slash = uevent.path.rfind('/');
@@ -350,8 +349,14 @@
PLOG(ERROR) << "Failed to create directory " << Dirname(link);
}
- if (symlink(devpath.c_str(), link.c_str()) && errno != EEXIST) {
- PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link;
+ if (symlink(devpath.c_str(), link.c_str())) {
+ if (errno != EEXIST) {
+ PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link;
+ } else if (std::string link_path;
+ Readlink(link, &link_path) && link_path != devpath) {
+ PLOG(ERROR) << "Failed to symlink " << devpath << " to " << link
+ << ", which already links to: " << link_path;
+ }
}
}
}
@@ -367,7 +372,7 @@
}
}
-void DeviceHandler::HandleDeviceEvent(const Uevent& uevent) {
+void DeviceHandler::HandleUevent(const Uevent& uevent) {
if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") {
FixupSysPermissions(uevent.path, uevent.subsystem);
}
@@ -413,18 +418,24 @@
HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links);
}
+void DeviceHandler::ColdbootDone() {
+ skip_restorecon_ = true;
+}
+
DeviceHandler::DeviceHandler(std::vector<Permissions> dev_permissions,
std::vector<SysfsPermissions> sysfs_permissions,
- std::vector<Subsystem> subsystems, bool skip_restorecon)
+ std::vector<Subsystem> subsystems, std::set<std::string> boot_devices,
+ bool skip_restorecon)
: dev_permissions_(std::move(dev_permissions)),
sysfs_permissions_(std::move(sysfs_permissions)),
subsystems_(std::move(subsystems)),
+ boot_devices_(std::move(boot_devices)),
skip_restorecon_(skip_restorecon),
sysfs_mount_point_("/sys") {}
DeviceHandler::DeviceHandler()
: DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},
- std::vector<Subsystem>{}, false) {}
+ std::vector<Subsystem>{}, std::set<std::string>{}, false) {}
} // namespace init
} // namespace android
diff --git a/init/devices.h b/init/devices.h
index 1f8f1e8..9d39eaa 100644
--- a/init/devices.h
+++ b/init/devices.h
@@ -21,6 +21,7 @@
#include <sys/types.h>
#include <algorithm>
+#include <set>
#include <string>
#include <vector>
@@ -28,12 +29,15 @@
#include <selinux/label.h>
#include "uevent.h"
+#include "uevent_handler.h"
namespace android {
namespace init {
class Permissions {
public:
+ friend void TestPermissions(const Permissions& expected, const Permissions& test);
+
Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid);
bool Match(const std::string& path) const;
@@ -56,6 +60,8 @@
class SysfsPermissions : public Permissions {
public:
+ friend void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test);
+
SysfsPermissions(const std::string& name, const std::string& attribute, mode_t perm, uid_t uid,
gid_t gid)
: Permissions(name, perm, uid, gid), attribute_(attribute) {}
@@ -70,16 +76,24 @@
class Subsystem {
public:
friend class SubsystemParser;
+ friend void TestSubsystems(const Subsystem& expected, const Subsystem& test);
+
+ enum DevnameSource {
+ DEVNAME_UEVENT_DEVNAME,
+ DEVNAME_UEVENT_DEVPATH,
+ };
Subsystem() {}
- Subsystem(std::string name) : name_(std::move(name)) {}
+ Subsystem(const std::string& name) : name_(name) {}
+ Subsystem(const std::string& name, DevnameSource source, const std::string& dir_name)
+ : name_(name), devname_source_(source), dir_name_(dir_name) {}
// Returns the full path for a uevent of a device that is a member of this subsystem,
// according to the rules parsed from ueventd.rc
std::string ParseDevPath(const Uevent& uevent) const {
- std::string devname = devname_source_ == DevnameSource::DEVNAME_UEVENT_DEVNAME
- ? uevent.device_name
- : android::base::Basename(uevent.path);
+ std::string devname = devname_source_ == DEVNAME_UEVENT_DEVNAME
+ ? uevent.device_name
+ : android::base::Basename(uevent.path);
return dir_name_ + "/" + devname;
}
@@ -87,30 +101,25 @@
bool operator==(const std::string& string_name) const { return name_ == string_name; }
private:
- enum class DevnameSource {
- DEVNAME_UEVENT_DEVNAME,
- DEVNAME_UEVENT_DEVPATH,
- };
-
std::string name_;
+ DevnameSource devname_source_ = DEVNAME_UEVENT_DEVNAME;
std::string dir_name_ = "/dev";
- DevnameSource devname_source_;
};
-class DeviceHandler {
+class DeviceHandler : public UeventHandler {
public:
friend class DeviceHandlerTester;
DeviceHandler();
DeviceHandler(std::vector<Permissions> dev_permissions,
- std::vector<SysfsPermissions> sysfs_permissions,
- std::vector<Subsystem> subsystems, bool skip_restorecon);
- ~DeviceHandler(){};
+ std::vector<SysfsPermissions> sysfs_permissions, std::vector<Subsystem> subsystems,
+ std::set<std::string> boot_devices, bool skip_restorecon);
+ virtual ~DeviceHandler() = default;
- void HandleDeviceEvent(const Uevent& uevent);
+ void HandleUevent(const Uevent& uevent) override;
+ void ColdbootDone() override;
std::vector<std::string> GetBlockDeviceSymlinks(const Uevent& uevent) const;
- void set_skip_restorecon(bool value) { skip_restorecon_ = value; }
private:
bool FindPlatformDevice(std::string path, std::string* platform_device_path) const;
@@ -125,6 +134,7 @@
std::vector<Permissions> dev_permissions_;
std::vector<SysfsPermissions> sysfs_permissions_;
std::vector<Subsystem> subsystems_;
+ std::set<std::string> boot_devices_;
bool skip_restorecon_;
std::string sysfs_mount_point_;
};
diff --git a/init/devices_test.cpp b/init/devices_test.cpp
index eba00cb..d658f4d 100644
--- a/init/devices_test.cpp
+++ b/init/devices_test.cpp
@@ -84,7 +84,6 @@
};
std::vector<std::string> expected_result{
"/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem",
- "/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1",
"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1",
};
@@ -100,7 +99,6 @@
.partition_num = 1,
};
std::vector<std::string> expected_result{
- "/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1",
"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1",
};
diff --git a/init/epoll.cpp b/init/epoll.cpp
new file mode 100644
index 0000000..4bca09e
--- /dev/null
+++ b/init/epoll.cpp
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2018 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 "epoll.h"
+
+#include <sys/epoll.h>
+
+#include <chrono>
+#include <functional>
+#include <map>
+
+namespace android {
+namespace init {
+
+Epoll::Epoll() {}
+
+Result<Success> Epoll::Open() {
+ if (epoll_fd_ >= 0) return Success();
+ epoll_fd_.reset(epoll_create1(EPOLL_CLOEXEC));
+
+ if (epoll_fd_ == -1) {
+ return ErrnoError() << "epoll_create1 failed";
+ }
+ return Success();
+}
+
+Result<Success> Epoll::RegisterHandler(int fd, std::function<void()> handler) {
+ auto [it, inserted] = epoll_handlers_.emplace(fd, std::move(handler));
+ if (!inserted) {
+ return Error() << "Cannot specify two epoll handlers for a given FD";
+ }
+ epoll_event ev;
+ ev.events = EPOLLIN;
+ // std::map's iterators do not get invalidated until erased, so we use the
+ // pointer to the std::function in the map directly for epoll_ctl.
+ ev.data.ptr = reinterpret_cast<void*>(&it->second);
+ if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &ev) == -1) {
+ Result<Success> result = ErrnoError() << "epoll_ctl failed to add fd";
+ epoll_handlers_.erase(fd);
+ return result;
+ }
+ return Success();
+}
+
+Result<Success> Epoll::UnregisterHandler(int fd) {
+ if (epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, nullptr) == -1) {
+ return ErrnoError() << "epoll_ctl failed to remove fd";
+ }
+ if (epoll_handlers_.erase(fd) != 1) {
+ return Error() << "Attempting to remove epoll handler for FD without an existing handler";
+ }
+ return Success();
+}
+
+Result<Success> Epoll::Wait(std::optional<std::chrono::milliseconds> timeout) {
+ int timeout_ms = -1;
+ if (timeout && timeout->count() < INT_MAX) {
+ timeout_ms = timeout->count();
+ }
+ epoll_event ev;
+ auto nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd_, &ev, 1, timeout_ms));
+ if (nr == -1) {
+ return ErrnoError() << "epoll_wait failed";
+ } else if (nr == 1) {
+ std::invoke(*reinterpret_cast<std::function<void()>*>(ev.data.ptr));
+ }
+ return Success();
+}
+
+} // namespace init
+} // namespace android
diff --git a/init/epoll.h b/init/epoll.h
new file mode 100644
index 0000000..85a791c
--- /dev/null
+++ b/init/epoll.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef _INIT_EPOLL_H
+#define _INIT_EPOLL_H
+
+#include <chrono>
+#include <functional>
+#include <map>
+#include <optional>
+
+#include <android-base/unique_fd.h>
+
+#include "result.h"
+
+namespace android {
+namespace init {
+
+class Epoll {
+ public:
+ Epoll();
+
+ Result<Success> Open();
+ Result<Success> RegisterHandler(int fd, std::function<void()> handler);
+ Result<Success> UnregisterHandler(int fd);
+ Result<Success> Wait(std::optional<std::chrono::milliseconds> timeout);
+
+ private:
+ android::base::unique_fd epoll_fd_;
+ std::map<int, std::function<void()>> epoll_handlers_;
+};
+
+} // namespace init
+} // namespace android
+
+#endif
diff --git a/init/firmware_handler.cpp b/init/firmware_handler.cpp
index 8c8d9f2..740e82c 100644
--- a/init/firmware_handler.cpp
+++ b/init/firmware_handler.cpp
@@ -21,7 +21,6 @@
#include <sys/wait.h>
#include <unistd.h>
-#include <string>
#include <thread>
#include <android-base/chrono_utils.h>
@@ -57,7 +56,10 @@
return access("/dev/.booting", F_OK) == 0;
}
-static void ProcessFirmwareEvent(const Uevent& uevent) {
+FirmwareHandler::FirmwareHandler(std::vector<std::string> firmware_directories)
+ : firmware_directories_(std::move(firmware_directories)) {}
+
+void FirmwareHandler::ProcessFirmwareEvent(const Uevent& uevent) {
int booting = IsBooting();
LOG(INFO) << "firmware: loading '" << uevent.firmware << "' for '" << uevent.path << "'";
@@ -78,12 +80,9 @@
return;
}
- static const char* firmware_dirs[] = {"/etc/firmware/", "/odm/firmware/",
- "/vendor/firmware/", "/firmware/image/"};
-
try_loading_again:
- for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
- std::string file = firmware_dirs[i] + uevent.firmware;
+ for (const auto& firmware_directory : firmware_directories_) {
+ std::string file = firmware_directory + uevent.firmware;
unique_fd fw_fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));
struct stat sb;
if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
@@ -106,7 +105,7 @@
write(loading_fd, "-1", 2);
}
-void HandleFirmwareEvent(const Uevent& uevent) {
+void FirmwareHandler::HandleUevent(const Uevent& uevent) {
if (uevent.subsystem != "firmware" || uevent.action != "add") return;
// Loading the firmware in a child means we can do that in parallel...
diff --git a/init/firmware_handler.h b/init/firmware_handler.h
index e456ac4..3996096 100644
--- a/init/firmware_handler.h
+++ b/init/firmware_handler.h
@@ -17,12 +17,27 @@
#ifndef _INIT_FIRMWARE_HANDLER_H
#define _INIT_FIRMWARE_HANDLER_H
+#include <string>
+#include <vector>
+
#include "uevent.h"
+#include "uevent_handler.h"
namespace android {
namespace init {
-void HandleFirmwareEvent(const Uevent& uevent);
+class FirmwareHandler : public UeventHandler {
+ public:
+ explicit FirmwareHandler(std::vector<std::string> firmware_directories);
+ virtual ~FirmwareHandler() = default;
+
+ void HandleUevent(const Uevent& uevent) override;
+
+ private:
+ void ProcessFirmwareEvent(const Uevent& uevent);
+
+ std::vector<std::string> firmware_directories_;
+};
} // namespace init
} // namespace android
diff --git a/init/first_stage_mount.cpp b/init/first_stage_mount.cpp
new file mode 100644
index 0000000..0c5cf76
--- /dev/null
+++ b/init/first_stage_mount.cpp
@@ -0,0 +1,600 @@
+/*
+ * Copyright (C) 2017 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 "first_stage_mount.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <chrono>
+#include <memory>
+#include <set>
+#include <string>
+#include <vector>
+
+#include <android-base/chrono_utils.h>
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+#include <liblp/metadata_format.h>
+
+#include "devices.h"
+#include "fs_mgr.h"
+#include "fs_mgr_avb.h"
+#include "fs_mgr_dm_linear.h"
+#include "fs_mgr_overlayfs.h"
+#include "uevent.h"
+#include "uevent_listener.h"
+#include "util.h"
+
+using android::base::Timer;
+
+namespace android {
+namespace init {
+
+// Class Declarations
+// ------------------
+class FirstStageMount {
+ public:
+ FirstStageMount();
+ virtual ~FirstStageMount() = default;
+
+ // The factory method to create either FirstStageMountVBootV1 or FirstStageMountVBootV2
+ // based on device tree configurations.
+ static std::unique_ptr<FirstStageMount> Create();
+ bool DoFirstStageMount(); // Mounts fstab entries read from device tree.
+ bool InitDevices();
+
+ protected:
+ ListenerAction HandleBlockDevice(const std::string& name, const Uevent&);
+ bool InitRequiredDevices();
+ bool InitMappedDevice(const std::string& verity_device);
+ bool CreateLogicalPartitions();
+ bool MountPartitions();
+ bool GetBackingDmLinearDevices();
+
+ virtual ListenerAction UeventCallback(const Uevent& uevent);
+
+ // Pure virtual functions.
+ virtual bool GetDmVerityDevices() = 0;
+ virtual bool SetUpDmVerity(fstab_rec* fstab_rec) = 0;
+
+ bool need_dm_verity_;
+
+ std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> device_tree_fstab_;
+ std::string lp_metadata_partition_;
+ std::vector<fstab_rec*> mount_fstab_recs_;
+ std::set<std::string> required_devices_partition_names_;
+ std::unique_ptr<DeviceHandler> device_handler_;
+ UeventListener uevent_listener_;
+};
+
+class FirstStageMountVBootV1 : public FirstStageMount {
+ public:
+ FirstStageMountVBootV1() = default;
+ ~FirstStageMountVBootV1() override = default;
+
+ protected:
+ bool GetDmVerityDevices() override;
+ bool SetUpDmVerity(fstab_rec* fstab_rec) override;
+};
+
+class FirstStageMountVBootV2 : public FirstStageMount {
+ public:
+ friend void SetInitAvbVersionInRecovery();
+
+ FirstStageMountVBootV2();
+ ~FirstStageMountVBootV2() override = default;
+
+ protected:
+ ListenerAction UeventCallback(const Uevent& uevent) override;
+ bool GetDmVerityDevices() override;
+ bool SetUpDmVerity(fstab_rec* fstab_rec) override;
+ bool InitAvbHandle();
+
+ std::string device_tree_vbmeta_parts_;
+ FsManagerAvbUniquePtr avb_handle_;
+ ByNameSymlinkMap by_name_symlink_map_;
+};
+
+// Static Functions
+// ----------------
+static inline bool IsDtVbmetaCompatible() {
+ return is_android_dt_value_expected("vbmeta/compatible", "android,vbmeta");
+}
+
+static bool inline IsRecoveryMode() {
+ return access("/system/bin/recovery", F_OK) == 0;
+}
+
+static inline bool IsDmLinearEnabled() {
+ static bool checked = false;
+ static bool enabled = false;
+ if (checked) {
+ return enabled;
+ }
+ import_kernel_cmdline(false,
+ [](const std::string& key, const std::string& value, bool in_qemu) {
+ if (key == "androidboot.logical_partitions" && value == "1") {
+ enabled = true;
+ }
+ });
+ checked = true;
+ return enabled;
+}
+
+// Class Definitions
+// -----------------
+FirstStageMount::FirstStageMount()
+ : need_dm_verity_(false), device_tree_fstab_(fs_mgr_read_fstab_dt(), fs_mgr_free_fstab) {
+ if (device_tree_fstab_) {
+ // Stores device_tree_fstab_->recs[] into mount_fstab_recs_ (vector<fstab_rec*>)
+ // for easier manipulation later, e.g., range-base for loop.
+ for (int i = 0; i < device_tree_fstab_->num_entries; i++) {
+ mount_fstab_recs_.push_back(&device_tree_fstab_->recs[i]);
+ }
+ } else {
+ LOG(INFO) << "Failed to read fstab from device tree";
+ }
+
+ auto boot_devices = fs_mgr_get_boot_devices();
+ device_handler_ = std::make_unique<DeviceHandler>(
+ std::vector<Permissions>{}, std::vector<SysfsPermissions>{}, std::vector<Subsystem>{},
+ std::move(boot_devices), false);
+}
+
+std::unique_ptr<FirstStageMount> FirstStageMount::Create() {
+ if (IsDtVbmetaCompatible()) {
+ return std::make_unique<FirstStageMountVBootV2>();
+ } else {
+ return std::make_unique<FirstStageMountVBootV1>();
+ }
+}
+
+bool FirstStageMount::DoFirstStageMount() {
+ if (!IsDmLinearEnabled() && mount_fstab_recs_.empty()) {
+ // Nothing to mount.
+ LOG(INFO) << "First stage mount skipped (missing/incompatible/empty fstab in device tree)";
+ return true;
+ }
+
+ if (!InitDevices()) return false;
+
+ if (!CreateLogicalPartitions()) return false;
+
+ if (!MountPartitions()) return false;
+
+ return true;
+}
+
+bool FirstStageMount::InitDevices() {
+ return GetBackingDmLinearDevices() && GetDmVerityDevices() && InitRequiredDevices();
+}
+
+bool FirstStageMount::GetBackingDmLinearDevices() {
+ // Add any additional devices required for dm-linear mappings.
+ if (!IsDmLinearEnabled()) {
+ return true;
+ }
+
+ required_devices_partition_names_.emplace(LP_METADATA_PARTITION_NAME);
+ return true;
+}
+
+// Creates devices with uevent->partition_name matching one in the member variable
+// required_devices_partition_names_. Found partitions will then be removed from it
+// for the subsequent member function to check which devices are NOT created.
+bool FirstStageMount::InitRequiredDevices() {
+ if (required_devices_partition_names_.empty()) {
+ return true;
+ }
+
+ if (IsDmLinearEnabled() || need_dm_verity_) {
+ const std::string dm_path = "/devices/virtual/misc/device-mapper";
+ bool found = false;
+ auto dm_callback = [this, &dm_path, &found](const Uevent& uevent) {
+ if (uevent.path == dm_path) {
+ device_handler_->HandleUevent(uevent);
+ found = true;
+ return ListenerAction::kStop;
+ }
+ return ListenerAction::kContinue;
+ };
+ uevent_listener_.RegenerateUeventsForPath("/sys" + dm_path, dm_callback);
+ if (!found) {
+ LOG(INFO) << "device-mapper device not found in /sys, waiting for its uevent";
+ Timer t;
+ uevent_listener_.Poll(dm_callback, 10s);
+ LOG(INFO) << "Wait for device-mapper returned after " << t;
+ }
+ if (!found) {
+ LOG(ERROR) << "device-mapper device not found after polling timeout";
+ return false;
+ }
+ }
+
+ auto uevent_callback = [this](const Uevent& uevent) { return UeventCallback(uevent); };
+ uevent_listener_.RegenerateUevents(uevent_callback);
+
+ // UeventCallback() will remove found partitions from required_devices_partition_names_.
+ // So if it isn't empty here, it means some partitions are not found.
+ if (!required_devices_partition_names_.empty()) {
+ LOG(INFO) << __PRETTY_FUNCTION__
+ << ": partition(s) not found in /sys, waiting for their uevent(s): "
+ << android::base::Join(required_devices_partition_names_, ", ");
+ Timer t;
+ uevent_listener_.Poll(uevent_callback, 10s);
+ LOG(INFO) << "Wait for partitions returned after " << t;
+ }
+
+ if (!required_devices_partition_names_.empty()) {
+ LOG(ERROR) << __PRETTY_FUNCTION__ << ": partition(s) not found after polling timeout: "
+ << android::base::Join(required_devices_partition_names_, ", ");
+ return false;
+ }
+
+ return true;
+}
+
+bool FirstStageMount::CreateLogicalPartitions() {
+ if (!IsDmLinearEnabled()) {
+ return true;
+ }
+
+ if (lp_metadata_partition_.empty()) {
+ LOG(ERROR) << "Could not locate logical partition tables in partition "
+ << LP_METADATA_PARTITION_NAME;
+ return false;
+ }
+ return android::fs_mgr::CreateLogicalPartitions(lp_metadata_partition_);
+}
+
+ListenerAction FirstStageMount::HandleBlockDevice(const std::string& name, const Uevent& uevent) {
+ // Matches partition name to create device nodes.
+ // Both required_devices_partition_names_ and uevent->partition_name have A/B
+ // suffix when A/B is used.
+ auto iter = required_devices_partition_names_.find(name);
+ if (iter != required_devices_partition_names_.end()) {
+ LOG(VERBOSE) << __PRETTY_FUNCTION__ << ": found partition: " << *iter;
+ if (IsDmLinearEnabled() && name == LP_METADATA_PARTITION_NAME) {
+ std::vector<std::string> links = device_handler_->GetBlockDeviceSymlinks(uevent);
+ lp_metadata_partition_ = links[0];
+ }
+ required_devices_partition_names_.erase(iter);
+ device_handler_->HandleUevent(uevent);
+ if (required_devices_partition_names_.empty()) {
+ return ListenerAction::kStop;
+ } else {
+ return ListenerAction::kContinue;
+ }
+ }
+ return ListenerAction::kContinue;
+}
+
+ListenerAction FirstStageMount::UeventCallback(const Uevent& uevent) {
+ // Ignores everything that is not a block device.
+ if (uevent.subsystem != "block") {
+ return ListenerAction::kContinue;
+ }
+
+ if (!uevent.partition_name.empty()) {
+ return HandleBlockDevice(uevent.partition_name, uevent);
+ } else {
+ size_t base_idx = uevent.path.rfind('/');
+ if (base_idx != std::string::npos) {
+ return HandleBlockDevice(uevent.path.substr(base_idx + 1), uevent);
+ }
+ }
+ // Not found a partition or find an unneeded partition, continue to find others.
+ return ListenerAction::kContinue;
+}
+
+// Creates "/dev/block/dm-XX" for dm-verity by running coldboot on /sys/block/dm-XX.
+bool FirstStageMount::InitMappedDevice(const std::string& dm_device) {
+ const std::string device_name(basename(dm_device.c_str()));
+ const std::string syspath = "/sys/block/" + device_name;
+ bool found = false;
+
+ auto verity_callback = [&device_name, &dm_device, this, &found](const Uevent& uevent) {
+ if (uevent.device_name == device_name) {
+ LOG(VERBOSE) << "Creating device-mapper device : " << dm_device;
+ device_handler_->HandleUevent(uevent);
+ found = true;
+ return ListenerAction::kStop;
+ }
+ return ListenerAction::kContinue;
+ };
+
+ uevent_listener_.RegenerateUeventsForPath(syspath, verity_callback);
+ if (!found) {
+ LOG(INFO) << "dm-verity device not found in /sys, waiting for its uevent";
+ Timer t;
+ uevent_listener_.Poll(verity_callback, 10s);
+ LOG(INFO) << "wait for dm-verity device returned after " << t;
+ }
+ if (!found) {
+ LOG(ERROR) << "dm-verity device not found after polling timeout";
+ return false;
+ }
+
+ return true;
+}
+
+bool FirstStageMount::MountPartitions() {
+ for (auto fstab_rec : mount_fstab_recs_) {
+ if (fs_mgr_is_logical(fstab_rec)) {
+ if (!fs_mgr_update_logical_partition(fstab_rec)) {
+ return false;
+ }
+ if (!InitMappedDevice(fstab_rec->blk_device)) {
+ return false;
+ }
+ }
+ if (!SetUpDmVerity(fstab_rec)) {
+ PLOG(ERROR) << "Failed to setup verity for '" << fstab_rec->mount_point << "'";
+ return false;
+ }
+ if (fs_mgr_do_mount_one(fstab_rec)) {
+ PLOG(ERROR) << "Failed to mount '" << fstab_rec->mount_point << "'";
+ return false;
+ }
+ }
+ fs_mgr_overlayfs_mount_all();
+ return true;
+}
+
+bool FirstStageMountVBootV1::GetDmVerityDevices() {
+ std::string verity_loc_device;
+ need_dm_verity_ = false;
+
+ for (auto fstab_rec : mount_fstab_recs_) {
+ // Don't allow verifyatboot in the first stage.
+ if (fs_mgr_is_verifyatboot(fstab_rec)) {
+ LOG(ERROR) << "Partitions can't be verified at boot";
+ return false;
+ }
+ // Checks for verified partitions.
+ if (fs_mgr_is_verified(fstab_rec)) {
+ need_dm_verity_ = true;
+ }
+ // Checks if verity metadata is on a separate partition. Note that it is
+ // not partition specific, so there must be only one additional partition
+ // that carries verity state.
+ if (fstab_rec->verity_loc) {
+ if (verity_loc_device.empty()) {
+ verity_loc_device = fstab_rec->verity_loc;
+ } else if (verity_loc_device != fstab_rec->verity_loc) {
+ LOG(ERROR) << "More than one verity_loc found: " << verity_loc_device << ", "
+ << fstab_rec->verity_loc;
+ return false;
+ }
+ }
+ }
+
+ // Includes the partition names of fstab records and verity_loc_device (if any).
+ // Notes that fstab_rec->blk_device has A/B suffix updated by fs_mgr when A/B is used.
+ for (auto fstab_rec : mount_fstab_recs_) {
+ required_devices_partition_names_.emplace(basename(fstab_rec->blk_device));
+ }
+
+ if (!verity_loc_device.empty()) {
+ required_devices_partition_names_.emplace(basename(verity_loc_device.c_str()));
+ }
+
+ return true;
+}
+
+bool FirstStageMountVBootV1::SetUpDmVerity(fstab_rec* fstab_rec) {
+ if (fs_mgr_is_verified(fstab_rec)) {
+ int ret = fs_mgr_setup_verity(fstab_rec, false /* wait_for_verity_dev */);
+ switch (ret) {
+ case FS_MGR_SETUP_VERITY_SKIPPED:
+ case FS_MGR_SETUP_VERITY_DISABLED:
+ LOG(INFO) << "Verity disabled/skipped for '" << fstab_rec->mount_point << "'";
+ return true;
+ case FS_MGR_SETUP_VERITY_SUCCESS:
+ // The exact block device name (fstab_rec->blk_device) is changed to
+ // "/dev/block/dm-XX". Needs to create it because ueventd isn't started in init
+ // first stage.
+ return InitMappedDevice(fstab_rec->blk_device);
+ default:
+ return false;
+ }
+ }
+ return true; // Returns true to mount the partition.
+}
+
+// FirstStageMountVBootV2 constructor.
+// Gets the vbmeta partitions from device tree.
+// /{
+// firmware {
+// android {
+// vbmeta {
+// compatible = "android,vbmeta";
+// parts = "vbmeta,boot,system,vendor"
+// };
+// };
+// };
+// }
+FirstStageMountVBootV2::FirstStageMountVBootV2() : avb_handle_(nullptr) {
+ if (!read_android_dt_file("vbmeta/parts", &device_tree_vbmeta_parts_)) {
+ PLOG(ERROR) << "Failed to read vbmeta/parts from device tree";
+ return;
+ }
+}
+
+bool FirstStageMountVBootV2::GetDmVerityDevices() {
+ need_dm_verity_ = false;
+
+ std::set<std::string> logical_partitions;
+
+ // fstab_rec->blk_device has A/B suffix.
+ for (auto fstab_rec : mount_fstab_recs_) {
+ if (fs_mgr_is_avb(fstab_rec)) {
+ need_dm_verity_ = true;
+ }
+ if (fs_mgr_is_logical(fstab_rec)) {
+ // Don't try to find logical partitions via uevent regeneration.
+ logical_partitions.emplace(basename(fstab_rec->blk_device));
+ } else {
+ required_devices_partition_names_.emplace(basename(fstab_rec->blk_device));
+ }
+ }
+
+ // libavb verifies AVB metadata on all verified partitions at once.
+ // e.g., The device_tree_vbmeta_parts_ will be "vbmeta,boot,system,vendor"
+ // for libavb to verify metadata, even if there is only /vendor in the
+ // above mount_fstab_recs_.
+ if (need_dm_verity_) {
+ if (device_tree_vbmeta_parts_.empty()) {
+ LOG(ERROR) << "Missing vbmeta parts in device tree";
+ return false;
+ }
+ std::vector<std::string> partitions = android::base::Split(device_tree_vbmeta_parts_, ",");
+ std::string ab_suffix = fs_mgr_get_slot_suffix();
+ for (const auto& partition : partitions) {
+ std::string partition_name = partition + ab_suffix;
+ if (logical_partitions.count(partition_name)) {
+ continue;
+ }
+ // required_devices_partition_names_ is of type std::set so it's not an issue
+ // to emplace a partition twice. e.g., /vendor might be in both places:
+ // - device_tree_vbmeta_parts_ = "vbmeta,boot,system,vendor"
+ // - mount_fstab_recs_: /vendor_a
+ required_devices_partition_names_.emplace(partition_name);
+ }
+ }
+ return true;
+}
+
+ListenerAction FirstStageMountVBootV2::UeventCallback(const Uevent& uevent) {
+ // Check if this uevent corresponds to one of the required partitions and store its symlinks if
+ // so, in order to create FsManagerAvbHandle later.
+ // Note that the parent callback removes partitions from the list of required partitions
+ // as it finds them, so this must happen first.
+ if (!uevent.partition_name.empty() &&
+ required_devices_partition_names_.find(uevent.partition_name) !=
+ required_devices_partition_names_.end()) {
+ // GetBlockDeviceSymlinks() will return three symlinks at most, depending on
+ // the content of uevent. by-name symlink will be at [0] if uevent->partition_name
+ // is not empty. e.g.,
+ // - /dev/block/platform/soc.0/f9824900.sdhci/by-name/modem
+ // - /dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1
+ std::vector<std::string> links = device_handler_->GetBlockDeviceSymlinks(uevent);
+ if (!links.empty()) {
+ auto [it, inserted] = by_name_symlink_map_.emplace(uevent.partition_name, links[0]);
+ if (!inserted) {
+ LOG(ERROR) << "Partition '" << uevent.partition_name
+ << "' already existed in the by-name symlink map with a value of '"
+ << it->second << "', new value '" << links[0] << "' will be ignored.";
+ }
+ }
+ }
+
+ return FirstStageMount::UeventCallback(uevent);
+}
+
+bool FirstStageMountVBootV2::SetUpDmVerity(fstab_rec* fstab_rec) {
+ if (fs_mgr_is_avb(fstab_rec)) {
+ if (!InitAvbHandle()) return false;
+ SetUpAvbHashtreeResult hashtree_result =
+ avb_handle_->SetUpAvbHashtree(fstab_rec, false /* wait_for_verity_dev */);
+ switch (hashtree_result) {
+ case SetUpAvbHashtreeResult::kDisabled:
+ return true; // Returns true to mount the partition.
+ case SetUpAvbHashtreeResult::kSuccess:
+ // The exact block device name (fstab_rec->blk_device) is changed to
+ // "/dev/block/dm-XX". Needs to create it because ueventd isn't started in init
+ // first stage.
+ return InitMappedDevice(fstab_rec->blk_device);
+ default:
+ return false;
+ }
+ }
+ return true; // Returns true to mount the partition.
+}
+
+bool FirstStageMountVBootV2::InitAvbHandle() {
+ if (avb_handle_) return true; // Returns true if the handle is already initialized.
+
+ if (by_name_symlink_map_.empty()) {
+ LOG(ERROR) << "by_name_symlink_map_ is empty";
+ return false;
+ }
+
+ avb_handle_ = FsManagerAvbHandle::Open(std::move(by_name_symlink_map_));
+ by_name_symlink_map_.clear(); // Removes all elements after the above std::move().
+
+ if (!avb_handle_) {
+ PLOG(ERROR) << "Failed to open FsManagerAvbHandle";
+ return false;
+ }
+ // Sets INIT_AVB_VERSION here for init to set ro.boot.avb_version in the second stage.
+ setenv("INIT_AVB_VERSION", avb_handle_->avb_version().c_str(), 1);
+ return true;
+}
+
+// Public functions
+// ----------------
+// Mounts partitions specified by fstab in device tree.
+bool DoFirstStageMount() {
+ // Skips first stage mount if we're in recovery mode.
+ if (IsRecoveryMode()) {
+ LOG(INFO) << "First stage mount skipped (recovery mode)";
+ return true;
+ }
+
+ std::unique_ptr<FirstStageMount> handle = FirstStageMount::Create();
+ if (!handle) {
+ LOG(ERROR) << "Failed to create FirstStageMount";
+ return false;
+ }
+ return handle->DoFirstStageMount();
+}
+
+void SetInitAvbVersionInRecovery() {
+ if (!IsRecoveryMode()) {
+ LOG(INFO) << "Skipped setting INIT_AVB_VERSION (not in recovery mode)";
+ return;
+ }
+
+ if (!IsDtVbmetaCompatible()) {
+ LOG(INFO) << "Skipped setting INIT_AVB_VERSION (not vbmeta compatible)";
+ return;
+ }
+
+ // Initializes required devices for the subsequent FsManagerAvbHandle::Open()
+ // to verify AVB metadata on all partitions in the verified chain.
+ // We only set INIT_AVB_VERSION when the AVB verification succeeds, i.e., the
+ // Open() function returns a valid handle.
+ // We don't need to mount partitions here in recovery mode.
+ FirstStageMountVBootV2 avb_first_mount;
+ if (!avb_first_mount.InitDevices()) {
+ LOG(ERROR) << "Failed to init devices for INIT_AVB_VERSION";
+ return;
+ }
+
+ FsManagerAvbUniquePtr avb_handle =
+ FsManagerAvbHandle::Open(std::move(avb_first_mount.by_name_symlink_map_));
+ if (!avb_handle) {
+ PLOG(ERROR) << "Failed to open FsManagerAvbHandle for INIT_AVB_VERSION";
+ return;
+ }
+ setenv("INIT_AVB_VERSION", avb_handle->avb_version().c_str(), 1);
+}
+
+} // namespace init
+} // namespace android
diff --git a/init/init_first_stage.h b/init/first_stage_mount.h
similarity index 91%
rename from init/init_first_stage.h
rename to init/first_stage_mount.h
index c7a3867..21d87fd 100644
--- a/init/init_first_stage.h
+++ b/init/first_stage_mount.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef _INIT_FIRST_STAGE_H
-#define _INIT_FIRST_STAGE_H
+#pragma once
namespace android {
namespace init {
@@ -25,5 +24,3 @@
} // namespace init
} // namespace android
-
-#endif
diff --git a/init/host_import_parser.cpp b/init/host_import_parser.cpp
new file mode 100644
index 0000000..93e363f
--- /dev/null
+++ b/init/host_import_parser.cpp
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2018 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 "host_import_parser.h"
+
+#include <android-base/strings.h>
+
+using android::base::StartsWith;
+
+namespace android {
+namespace init {
+
+Result<Success> HostImportParser::ParseSection(std::vector<std::string>&& args, const std::string&,
+ int) {
+ if (args.size() != 2) {
+ return Error() << "single argument needed for import\n";
+ }
+
+ return Success();
+}
+
+Result<Success> HostImportParser::ParseLineSection(std::vector<std::string>&&, int) {
+ return Error() << "Unexpected line found after import statement";
+}
+
+} // namespace init
+} // namespace android
diff --git a/init/log.h b/init/host_import_parser.h
similarity index 62%
copy from init/log.h
copy to init/host_import_parser.h
index 5a4eba6..52b8891 100644
--- a/init/log.h
+++ b/init/host_import_parser.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,19 +14,22 @@
* limitations under the License.
*/
-#ifndef _INIT_LOG_H_
-#define _INIT_LOG_H_
+#pragma once
-#include <sys/cdefs.h>
+#include <string>
+#include <vector>
+
+#include "parser.h"
namespace android {
namespace init {
-void InitKernelLogging(char* argv[]);
-
-int selinux_klog_callback(int level, const char* fmt, ...) __printflike(2, 3);
+class HostImportParser : public SectionParser {
+ public:
+ HostImportParser() {}
+ Result<Success> ParseSection(std::vector<std::string>&& args, const std::string&, int) override;
+ Result<Success> ParseLineSection(std::vector<std::string>&&, int) override;
+};
} // namespace init
} // namespace android
-
-#endif
diff --git a/init/host_init_parser.cpp b/init/host_init_parser.cpp
deleted file mode 100644
index 5232b7e..0000000
--- a/init/host_init_parser.cpp
+++ /dev/null
@@ -1,82 +0,0 @@
-//
-// Copyright (C) 2018 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 <pwd.h>
-
-#include <android-base/logging.h>
-
-#include "action.h"
-#include "action_manager.h"
-#include "action_parser.h"
-#include "parser.h"
-#include "result.h"
-#include "service.h"
-
-// The host passwd file won't have the Android entries, so we fake success here.
-passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
- char dummy_buf[] = "dummy";
- static passwd dummy_passwd = {
- .pw_name = dummy_buf,
- .pw_dir = dummy_buf,
- .pw_shell = dummy_buf,
- .pw_uid = 123,
- .pw_gid = 123,
- };
- return &dummy_passwd;
-}
-
-namespace android {
-namespace init {
-
-static Result<Success> do_stub(const BuiltinArguments& args) {
- return Success();
-}
-
-#include "generated_stub_builtin_function_map.h"
-
-int main(int argc, char** argv) {
- android::base::InitLogging(argv, &android::base::StderrLogger);
- if (argc != 2) {
- LOG(ERROR) << "Usage: " << argv[0] << " <init file to parse>";
- return -1;
- }
- const BuiltinFunctionMap function_map;
- Action::set_function_map(&function_map);
- ActionManager& am = ActionManager::GetInstance();
- ServiceList& sl = ServiceList::GetInstance();
- Parser parser;
- parser.AddSectionParser("service", std::make_unique<ServiceParser>(&sl, nullptr));
- parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
-
- size_t num_errors = 0;
- if (!parser.ParseConfig(argv[1], &num_errors)) {
- LOG(ERROR) << "Failed to find script";
- return -1;
- }
- if (num_errors > 0) {
- LOG(ERROR) << "Parse failed with " << num_errors << " errors";
- return -1;
- }
- LOG(INFO) << "Parse success!";
- return 0;
-}
-
-} // namespace init
-} // namespace android
-
-int main(int argc, char** argv) {
- android::init::main(argc, argv);
-}
diff --git a/init/host_init_stubs.cpp b/init/host_init_stubs.cpp
index e6cc08a..8866bdc 100644
--- a/init/host_init_stubs.cpp
+++ b/init/host_init_stubs.cpp
@@ -16,39 +16,34 @@
#include "host_init_stubs.h"
+#include <android-base/properties.h>
+
// unistd.h
int setgroups(size_t __size, const gid_t* __list) {
return 0;
}
namespace android {
-namespace base {
-
-std::string GetProperty(const std::string&, const std::string& default_value) {
- return default_value;
-}
-
-bool GetBoolProperty(const std::string&, bool default_value) {
- return default_value;
-}
-
-} // namespace base
-} // namespace android
-
-namespace android {
namespace init {
// init.h
std::string default_console = "/dev/console";
// property_service.h
-uint32_t (*property_set)(const std::string& name, const std::string& value) = nullptr;
+uint32_t SetProperty(const std::string& key, const std::string& value) {
+ android::base::SetProperty(key, value);
+ return 0;
+}
+uint32_t (*property_set)(const std::string& name, const std::string& value) = SetProperty;
uint32_t HandlePropertySet(const std::string&, const std::string&, const std::string&, const ucred&,
std::string*) {
return 0;
}
// selinux.h
+int SelinuxGetVendorAndroidVersion() {
+ return 10000;
+}
void SelabelInitialize() {}
bool SelabelLookupFileContext(const std::string& key, int type, std::string* result) {
diff --git a/init/host_init_stubs.h b/init/host_init_stubs.h
index f31ece6..0af11f6 100644
--- a/init/host_init_stubs.h
+++ b/init/host_init_stubs.h
@@ -23,22 +23,15 @@
#include <string>
+// android/api-level.h
+#define __ANDROID_API_P__ 28
+
// sys/system_properties.h
#define PROP_VALUE_MAX 92
// unistd.h
int setgroups(size_t __size, const gid_t* __list);
-// android-base/properties.h
-namespace android {
-namespace base {
-
-std::string GetProperty(const std::string& key, const std::string& default_value);
-bool GetBoolProperty(const std::string& key, bool default_value);
-
-} // namespace base
-} // namespace android
-
namespace android {
namespace init {
@@ -51,6 +44,7 @@
const std::string& source_context, const ucred& cr, std::string* error);
// selinux.h
+int SelinuxGetVendorAndroidVersion();
void SelabelInitialize();
bool SelabelLookupFileContext(const std::string& key, int type, std::string* result);
diff --git a/init/host_init_verifier.cpp b/init/host_init_verifier.cpp
new file mode 100644
index 0000000..8407729
--- /dev/null
+++ b/init/host_init_verifier.cpp
@@ -0,0 +1,166 @@
+//
+// Copyright (C) 2018 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 <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
+#include <android-base/strings.h>
+
+#include "action.h"
+#include "action_manager.h"
+#include "action_parser.h"
+#include "host_import_parser.h"
+#include "host_init_stubs.h"
+#include "parser.h"
+#include "result.h"
+#include "service.h"
+
+#define EXCLUDE_FS_CONFIG_STRUCTURES
+#include "generated_android_ids.h"
+
+using namespace std::literals;
+
+using android::base::ParseInt;
+using android::base::ReadFileToString;
+using android::base::Split;
+
+static std::string passwd_file;
+
+static std::vector<std::pair<std::string, int>> GetVendorPasswd() {
+ std::string passwd;
+ if (!ReadFileToString(passwd_file, &passwd)) {
+ return {};
+ }
+
+ std::vector<std::pair<std::string, int>> result;
+ auto passwd_lines = Split(passwd, "\n");
+ for (const auto& line : passwd_lines) {
+ auto split_line = Split(line, ":");
+ if (split_line.size() < 3) {
+ continue;
+ }
+ int uid = 0;
+ if (!ParseInt(split_line[2], &uid)) {
+ continue;
+ }
+ result.emplace_back(split_line[0], uid);
+ }
+ return result;
+}
+
+passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
+ // This isn't thread safe, but that's okay for our purposes.
+ static char static_name[32] = "";
+ static char static_dir[32] = "/";
+ static char static_shell[32] = "/system/bin/sh";
+ static passwd static_passwd = {
+ .pw_name = static_name,
+ .pw_dir = static_dir,
+ .pw_shell = static_shell,
+ .pw_uid = 0,
+ .pw_gid = 0,
+ };
+
+ for (size_t n = 0; n < android_id_count; ++n) {
+ if (!strcmp(android_ids[n].name, login)) {
+ snprintf(static_name, sizeof(static_name), "%s", android_ids[n].name);
+ static_passwd.pw_uid = android_ids[n].aid;
+ static_passwd.pw_gid = android_ids[n].aid;
+ return &static_passwd;
+ }
+ }
+
+ static const auto vendor_passwd = GetVendorPasswd();
+
+ for (const auto& [name, uid] : vendor_passwd) {
+ if (name == login) {
+ snprintf(static_name, sizeof(static_name), "%s", name.c_str());
+ static_passwd.pw_uid = uid;
+ static_passwd.pw_gid = uid;
+ return &static_passwd;
+ }
+ }
+
+ unsigned int oem_uid;
+ if (sscanf(login, "oem_%u", &oem_uid) == 1) {
+ snprintf(static_name, sizeof(static_name), "%s", login);
+ static_passwd.pw_uid = oem_uid;
+ static_passwd.pw_gid = oem_uid;
+ return &static_passwd;
+ }
+
+ errno = ENOENT;
+ return nullptr;
+}
+
+namespace android {
+namespace init {
+
+static Result<Success> do_stub(const BuiltinArguments& args) {
+ return Success();
+}
+
+#include "generated_stub_builtin_function_map.h"
+
+int main(int argc, char** argv) {
+ android::base::InitLogging(argv, &android::base::StdioLogger);
+ android::base::SetMinimumLogSeverity(android::base::ERROR);
+
+ if (argc != 2 && argc != 3) {
+ LOG(ERROR) << "Usage: " << argv[0] << " <init rc file> [passwd file]";
+ return EXIT_FAILURE;
+ }
+
+ if (argc == 3) {
+ passwd_file = argv[2];
+ }
+
+ const BuiltinFunctionMap function_map;
+ Action::set_function_map(&function_map);
+ ActionManager& am = ActionManager::GetInstance();
+ ServiceList& sl = ServiceList::GetInstance();
+ Parser parser;
+ parser.AddSectionParser("service", std::make_unique<ServiceParser>(&sl, nullptr));
+ parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
+ parser.AddSectionParser("import", std::make_unique<HostImportParser>());
+
+ if (!parser.ParseConfigFileInsecure(argv[1])) {
+ LOG(ERROR) << "Failed to open init rc script '" << argv[1] << "'";
+ return EXIT_FAILURE;
+ }
+ if (parser.parse_error_count() > 0) {
+ LOG(ERROR) << "Failed to parse init script '" << argv[1] << "' with "
+ << parser.parse_error_count() << " errors";
+ return EXIT_FAILURE;
+ }
+ return EXIT_SUCCESS;
+}
+
+} // namespace init
+} // namespace android
+
+int main(int argc, char** argv) {
+ return android::init::main(argc, argv);
+}
diff --git a/init/import_parser.cpp b/init/import_parser.cpp
index e335fd1..fb3185e 100644
--- a/init/import_parser.cpp
+++ b/init/import_parser.cpp
@@ -41,14 +41,15 @@
return Success();
}
+Result<Success> ImportParser::ParseLineSection(std::vector<std::string>&&, int) {
+ return Error() << "Unexpected line found after import statement";
+}
+
void ImportParser::EndFile() {
auto current_imports = std::move(imports_);
imports_.clear();
for (const auto& [import, line_num] : current_imports) {
- if (!parser_->ParseConfig(import)) {
- PLOG(ERROR) << filename_ << ": " << line_num << ": Could not import file '" << import
- << "'";
- }
+ parser_->ParseConfig(import);
}
}
diff --git a/init/import_parser.h b/init/import_parser.h
index 5a2f894..7bc72e6 100644
--- a/init/import_parser.h
+++ b/init/import_parser.h
@@ -30,6 +30,7 @@
ImportParser(Parser* parser) : parser_(parser) {}
Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
int line) override;
+ Result<Success> ParseLineSection(std::vector<std::string>&&, int) override;
void EndFile() override;
private:
diff --git a/init/init.cpp b/init/init.cpp
index efb9c1d..b550f1b 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -18,19 +18,19 @@
#include <dirent.h>
#include <fcntl.h>
-#include <paths.h>
#include <pthread.h>
-#include <seccomp_policy.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/epoll.h>
#include <sys/mount.h>
#include <sys/signalfd.h>
-#include <sys/sysmacros.h>
#include <sys/types.h>
#include <unistd.h>
+#include <map>
+#include <memory>
+#include <optional>
+
#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/logging.h>
@@ -40,26 +40,22 @@
#include <cutils/android_reboot.h>
#include <keyutils.h>
#include <libavb/libavb.h>
-#include <private/android_filesystem_config.h>
-#include <selinux/android.h>
-
-#include <memory>
-#include <optional>
#include "action_parser.h"
+#include "epoll.h"
+#include "first_stage_mount.h"
#include "import_parser.h"
-#include "init_first_stage.h"
#include "keychords.h"
-#include "log.h"
#include "property_service.h"
#include "reboot.h"
+#include "reboot_utils.h"
#include "security.h"
#include "selinux.h"
#include "sigchld_handler.h"
#include "ueventd.h"
#include "util.h"
-#include "watchdogd.h"
+using namespace std::chrono_literals;
using namespace std::string_literals;
using android::base::boot_clock;
@@ -78,8 +74,7 @@
std::string default_console = "/dev/console";
-static int epoll_fd = -1;
-static int sigterm_signal_fd = -1;
+static int signal_fd = -1;
static std::unique_ptr<Timer> waiting_for_prop(nullptr);
static std::string wait_prop_name;
@@ -119,6 +114,9 @@
if (!parser.ParseConfig("/product/etc/init")) {
late_import_paths.emplace_back("/product/etc/init");
}
+ if (!parser.ParseConfig("/product-services/etc/init")) {
+ late_import_paths.emplace_back("/product-services/etc/init");
+ }
if (!parser.ParseConfig("/odm/etc/init")) {
late_import_paths.emplace_back("/odm/etc/init");
}
@@ -130,15 +128,6 @@
}
}
-void register_epoll_handler(int fd, void (*fn)()) {
- epoll_event ev;
- ev.events = EPOLLIN;
- ev.data.ptr = reinterpret_cast<void*>(fn);
- if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
- PLOG(ERROR) << "epoll_ctl failed";
- }
-}
-
bool start_waiting_for_property(const char *name, const char *value)
{
if (waiting_for_prop) {
@@ -238,6 +227,10 @@
static const std::map<std::string, ControlMessageFunction>& get_control_message_map() {
// clang-format off
static const std::map<std::string, ControlMessageFunction> control_message_functions = {
+ {"sigstop_on", {ControlTarget::SERVICE,
+ [](auto* service) { service->set_sigstop(true); return Success(); }}},
+ {"sigstop_off", {ControlTarget::SERVICE,
+ [](auto* service) { service->set_sigstop(false); return Success(); }}},
{"start", {ControlTarget::SERVICE, DoControlStart}},
{"stop", {ControlTarget::SERVICE, DoControlStop}},
{"restart", {ControlTarget::SERVICE, DoControlRestart}},
@@ -273,40 +266,29 @@
const ControlMessageFunction& function = it->second;
- if (function.target == ControlTarget::SERVICE) {
- Service* svc = ServiceList::GetInstance().FindService(name);
- if (svc == nullptr) {
- LOG(ERROR) << "No such service '" << name << "' for ctl." << msg;
- return;
- }
- if (auto result = function.action(svc); !result) {
- LOG(ERROR) << "Could not ctl." << msg << " for service " << name << ": "
- << result.error();
- }
+ Service* svc = nullptr;
+ switch (function.target) {
+ case ControlTarget::SERVICE:
+ svc = ServiceList::GetInstance().FindService(name);
+ break;
+ case ControlTarget::INTERFACE:
+ svc = ServiceList::GetInstance().FindInterface(name);
+ break;
+ default:
+ LOG(ERROR) << "Invalid function target from static map key '" << msg << "': "
+ << static_cast<std::underlying_type<ControlTarget>::type>(function.target);
+ return;
+ }
+
+ if (svc == nullptr) {
+ LOG(ERROR) << "Could not find '" << name << "' for ctl." << msg;
return;
}
- if (function.target == ControlTarget::INTERFACE) {
- for (const auto& svc : ServiceList::GetInstance()) {
- if (svc->interfaces().count(name) == 0) {
- continue;
- }
-
- if (auto result = function.action(svc.get()); !result) {
- LOG(ERROR) << "Could not handle ctl." << msg << " for service " << svc->name()
- << " with interface " << name << ": " << result.error();
- }
-
- return;
- }
-
- LOG(ERROR) << "Could not find service hosting interface " << name;
- return;
+ if (auto result = function.action(svc); !result) {
+ LOG(ERROR) << "Could not ctl." << msg << " for '" << name << "': " << result.error();
}
-
- LOG(ERROR) << "Invalid function target from static map key '" << msg
- << "': " << static_cast<std::underlying_type<ControlTarget>::type>(function.target);
}
static Result<Success> wait_for_coldboot_done_action(const BuiltinArguments& args) {
@@ -330,11 +312,6 @@
return Success();
}
-static Result<Success> keychord_init_action(const BuiltinArguments& args) {
- keychord_init();
- return Success();
-}
-
static Result<Success> console_init_action(const BuiltinArguments& args) {
std::string console = GetProperty("ro.boot.console", "");
if (!console.empty()) {
@@ -372,21 +349,23 @@
}
static void export_kernel_boot_props() {
+ constexpr const char* UNSET = "";
struct {
const char *src_prop;
const char *dst_prop;
const char *default_value;
} prop_map[] = {
- { "ro.boot.serialno", "ro.serialno", "", },
+ { "ro.boot.serialno", "ro.serialno", UNSET, },
{ "ro.boot.mode", "ro.bootmode", "unknown", },
{ "ro.boot.baseband", "ro.baseband", "unknown", },
{ "ro.boot.bootloader", "ro.bootloader", "unknown", },
{ "ro.boot.hardware", "ro.hardware", "unknown", },
{ "ro.boot.revision", "ro.revision", "0", },
};
- for (size_t i = 0; i < arraysize(prop_map); i++) {
- std::string value = GetProperty(prop_map[i].src_prop, "");
- property_set(prop_map[i].dst_prop, (!value.empty()) ? value : prop_map[i].default_value);
+ for (const auto& prop : prop_map) {
+ std::string value = GetProperty(prop.src_prop, prop.default_value);
+ if (value != UNSET)
+ property_set(prop.dst_prop, value);
}
}
@@ -434,14 +413,6 @@
return Success();
}
-static void global_seccomp() {
- import_kernel_cmdline(false, [](const std::string& key, const std::string& value, bool in_qemu) {
- if (key == "androidboot.seccomp" && value == "global" && !set_global_seccomp_filter()) {
- LOG(FATAL) << "Failed to globally enable seccomp!";
- }
- });
-}
-
// Set the UDC controller for the ConfigFS USB Gadgets.
// Read the UDC controller in use from "/sys/class/udc".
// In case of multiple UDC controllers select the first one.
@@ -458,48 +429,7 @@
}
}
-static void InstallRebootSignalHandlers() {
- // Instead of panic'ing the kernel as is the default behavior when init crashes,
- // we prefer to reboot to bootloader on development builds, as this will prevent
- // boot looping bad configurations and allow both developers and test farms to easily
- // recover.
- struct sigaction action;
- memset(&action, 0, sizeof(action));
- sigfillset(&action.sa_mask);
- action.sa_handler = [](int signal) {
- // These signal handlers are also caught for processes forked from init, however we do not
- // want them to trigger reboot, so we directly call _exit() for children processes here.
- if (getpid() != 1) {
- _exit(signal);
- }
-
- // Calling DoReboot() or LOG(FATAL) is not a good option as this is a signal handler.
- // RebootSystem uses syscall() which isn't actually async-signal-safe, but our only option
- // and probably good enough given this is already an error case and only enabled for
- // development builds.
- RebootSystem(ANDROID_RB_RESTART2, "bootloader");
- };
- action.sa_flags = SA_RESTART;
- sigaction(SIGABRT, &action, nullptr);
- sigaction(SIGBUS, &action, nullptr);
- sigaction(SIGFPE, &action, nullptr);
- sigaction(SIGILL, &action, nullptr);
- sigaction(SIGSEGV, &action, nullptr);
-#if defined(SIGSTKFLT)
- sigaction(SIGSTKFLT, &action, nullptr);
-#endif
- sigaction(SIGSYS, &action, nullptr);
- sigaction(SIGTRAP, &action, nullptr);
-}
-
-static void HandleSigtermSignal() {
- signalfd_siginfo siginfo;
- ssize_t bytes_read = TEMP_FAILURE_RETRY(read(sigterm_signal_fd, &siginfo, sizeof(siginfo)));
- if (bytes_read != sizeof(siginfo)) {
- PLOG(ERROR) << "Failed to read siginfo from sigterm_signal_fd";
- return;
- }
-
+static void HandleSigtermSignal(const signalfd_siginfo& siginfo) {
if (siginfo.ssi_pid != 0) {
// Drop any userspace SIGTERM requests.
LOG(DEBUG) << "Ignoring SIGTERM from pid " << siginfo.ssi_pid;
@@ -509,37 +439,130 @@
HandlePowerctlMessage("shutdown,container");
}
-static void UnblockSigterm() {
- sigset_t mask;
- sigemptyset(&mask);
- sigaddset(&mask, SIGTERM);
+static void HandleSignalFd() {
+ signalfd_siginfo siginfo;
+ ssize_t bytes_read = TEMP_FAILURE_RETRY(read(signal_fd, &siginfo, sizeof(siginfo)));
+ if (bytes_read != sizeof(siginfo)) {
+ PLOG(ERROR) << "Failed to read siginfo from signal_fd";
+ return;
+ }
- if (sigprocmask(SIG_UNBLOCK, &mask, nullptr) == -1) {
- PLOG(FATAL) << "failed to unblock SIGTERM for PID " << getpid();
+ switch (siginfo.ssi_signo) {
+ case SIGCHLD:
+ ReapAnyOutstandingChildren();
+ break;
+ case SIGTERM:
+ HandleSigtermSignal(siginfo);
+ break;
+ default:
+ PLOG(ERROR) << "signal_fd: received unexpected signal " << siginfo.ssi_signo;
+ break;
}
}
-static void InstallSigtermHandler() {
+static void UnblockSignals() {
+ const struct sigaction act { .sa_handler = SIG_DFL };
+ sigaction(SIGCHLD, &act, nullptr);
+
sigset_t mask;
sigemptyset(&mask);
+ sigaddset(&mask, SIGCHLD);
sigaddset(&mask, SIGTERM);
- if (sigprocmask(SIG_BLOCK, &mask, nullptr) == -1) {
- PLOG(FATAL) << "failed to block SIGTERM";
+ if (sigprocmask(SIG_UNBLOCK, &mask, nullptr) == -1) {
+ PLOG(FATAL) << "failed to unblock signals for PID " << getpid();
+ }
+}
+
+static void InstallSignalFdHandler(Epoll* epoll) {
+ // Applying SA_NOCLDSTOP to a defaulted SIGCHLD handler prevents the signalfd from receiving
+ // SIGCHLD when a child process stops or continues (b/77867680#comment9).
+ const struct sigaction act { .sa_handler = SIG_DFL, .sa_flags = SA_NOCLDSTOP };
+ sigaction(SIGCHLD, &act, nullptr);
+
+ sigset_t mask;
+ sigemptyset(&mask);
+ sigaddset(&mask, SIGCHLD);
+
+ if (!IsRebootCapable()) {
+ // If init does not have the CAP_SYS_BOOT capability, it is running in a container.
+ // In that case, receiving SIGTERM will cause the system to shut down.
+ sigaddset(&mask, SIGTERM);
}
- // Register a handler to unblock SIGTERM in the child processes.
- const int result = pthread_atfork(nullptr, nullptr, &UnblockSigterm);
+ if (sigprocmask(SIG_BLOCK, &mask, nullptr) == -1) {
+ PLOG(FATAL) << "failed to block signals";
+ }
+
+ // Register a handler to unblock signals in the child processes.
+ const int result = pthread_atfork(nullptr, nullptr, &UnblockSignals);
if (result != 0) {
LOG(FATAL) << "Failed to register a fork handler: " << strerror(result);
}
- sigterm_signal_fd = signalfd(-1, &mask, SFD_CLOEXEC);
- if (sigterm_signal_fd == -1) {
- PLOG(FATAL) << "failed to create signalfd for SIGTERM";
+ signal_fd = signalfd(-1, &mask, SFD_CLOEXEC);
+ if (signal_fd == -1) {
+ PLOG(FATAL) << "failed to create signalfd";
}
- register_epoll_handler(sigterm_signal_fd, HandleSigtermSignal);
+ if (auto result = epoll->RegisterHandler(signal_fd, HandleSignalFd); !result) {
+ LOG(FATAL) << result.error();
+ }
+}
+
+void HandleKeychord(const std::vector<int>& keycodes) {
+ // Only handle keychords if adb is enabled.
+ std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
+ if (adb_enabled != "running") {
+ LOG(WARNING) << "Not starting service for keychord " << android::base::Join(keycodes, ' ')
+ << " because ADB is disabled";
+ return;
+ }
+
+ auto found = false;
+ for (const auto& service : ServiceList::GetInstance()) {
+ auto svc = service.get();
+ if (svc->keycodes() == keycodes) {
+ found = true;
+ LOG(INFO) << "Starting service '" << svc->name() << "' from keychord "
+ << android::base::Join(keycodes, ' ');
+ if (auto result = svc->Start(); !result) {
+ LOG(ERROR) << "Could not start service '" << svc->name() << "' from keychord "
+ << android::base::Join(keycodes, ' ') << ": " << result.error();
+ }
+ }
+ }
+ if (!found) {
+ LOG(ERROR) << "Service for keychord " << android::base::Join(keycodes, ' ') << " not found";
+ }
+}
+
+static void InitAborter(const char* abort_message) {
+ // When init forks, it continues to use this aborter for LOG(FATAL), but we want children to
+ // simply abort instead of trying to reboot the system.
+ if (getpid() != 1) {
+ android::base::DefaultAborter(abort_message);
+ return;
+ }
+
+ RebootSystem(ANDROID_RB_RESTART2, "bootloader");
+}
+
+static void InitKernelLogging(char* argv[]) {
+ // Make stdin/stdout/stderr all point to /dev/null.
+ int fd = open("/sys/fs/selinux/null", O_RDWR);
+ if (fd == -1) {
+ int saved_errno = errno;
+ android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
+ errno = saved_errno;
+ PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
+ }
+ dup2(fd, 0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ if (fd > 2) close(fd);
+
+ android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
}
int main(int argc, char** argv) {
@@ -547,12 +570,8 @@
return ueventd_main(argc, argv);
}
- if (!strcmp(basename(argv[0]), "watchdogd")) {
- return watchdogd_main(argc, argv);
- }
-
if (argc > 1 && !strcmp(argv[1], "subcontext")) {
- InitKernelLogging(argv);
+ android::base::InitLogging(argv, &android::base::KernelLogger);
const BuiltinFunctionMap function_map;
return SubcontextMain(argc, argv, &function_map);
}
@@ -561,81 +580,6 @@
InstallRebootSignalHandlers();
}
- bool is_first_stage = (getenv("INIT_SECOND_STAGE") == nullptr);
-
- if (is_first_stage) {
- boot_clock::time_point start_time = boot_clock::now();
-
- // Clear the umask.
- umask(0);
-
- clearenv();
- setenv("PATH", _PATH_DEFPATH, 1);
- // Get the basic filesystem setup we need put together in the initramdisk
- // on / and then we'll let the rc file figure out the rest.
- mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
- mkdir("/dev/pts", 0755);
- mkdir("/dev/socket", 0755);
- mount("devpts", "/dev/pts", "devpts", 0, NULL);
- #define MAKE_STR(x) __STRING(x)
- mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC));
- // Don't expose the raw commandline to unprivileged processes.
- chmod("/proc/cmdline", 0440);
- gid_t groups[] = { AID_READPROC };
- setgroups(arraysize(groups), groups);
- mount("sysfs", "/sys", "sysfs", 0, NULL);
- mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL);
-
- mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11));
-
- if constexpr (WORLD_WRITABLE_KMSG) {
- mknod("/dev/kmsg_debug", S_IFCHR | 0622, makedev(1, 11));
- }
-
- mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8));
- mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9));
-
- // Now that tmpfs is mounted on /dev and we have /dev/kmsg, we can actually
- // talk to the outside world...
- InitKernelLogging(argv);
-
- LOG(INFO) << "init first stage started!";
-
- if (!DoFirstStageMount()) {
- LOG(FATAL) << "Failed to mount required partitions early ...";
- }
-
- SetInitAvbVersionInRecovery();
-
- // Enable seccomp if global boot option was passed (otherwise it is enabled in zygote).
- global_seccomp();
-
- // Set up SELinux, loading the SELinux policy.
- SelinuxSetupKernelLogging();
- SelinuxInitialize();
-
- // We're in the kernel domain, so re-exec init to transition to the init domain now
- // that the SELinux policy has been loaded.
- if (selinux_android_restorecon("/init", 0) == -1) {
- PLOG(FATAL) << "restorecon failed of /init failed";
- }
-
- setenv("INIT_SECOND_STAGE", "true", 1);
-
- static constexpr uint32_t kNanosecondsPerMillisecond = 1e6;
- uint64_t start_ms = start_time.time_since_epoch().count() / kNanosecondsPerMillisecond;
- setenv("INIT_STARTED_AT", std::to_string(start_ms).c_str(), 1);
-
- char* path = argv[0];
- char* args[] = { path, nullptr };
- execv(path, args);
-
- // execv() only returns if an error happened, in which case we
- // panic and never fall through this conditional.
- PLOG(FATAL) << "execv(\"" << path << "\") failed";
- }
-
- // At this point we're in the second stage of init.
InitKernelLogging(argv);
LOG(INFO) << "init second stage started!";
@@ -667,7 +611,6 @@
if (avb_version) property_set("ro.boot.avb_version", avb_version);
// Clean up our environment.
- unsetenv("INIT_SECOND_STAGE");
unsetenv("INIT_STARTED_AT");
unsetenv("INIT_SELINUX_TOOK");
unsetenv("INIT_AVB_VERSION");
@@ -677,22 +620,16 @@
SelabelInitialize();
SelinuxRestoreContext();
- epoll_fd = epoll_create1(EPOLL_CLOEXEC);
- if (epoll_fd == -1) {
- PLOG(FATAL) << "epoll_create1 failed";
+ Epoll epoll;
+ if (auto result = epoll.Open(); !result) {
+ PLOG(FATAL) << result.error();
}
- sigchld_handler_init();
-
- if (!IsRebootCapable()) {
- // If init does not have the CAP_SYS_BOOT capability, it is running in a container.
- // In that case, receiving SIGTERM will cause the system to shut down.
- InstallSigtermHandler();
- }
+ InstallSignalFdHandler(&epoll);
property_load_boot_defaults();
export_oem_lock_status();
- start_property_service();
+ StartPropertyService(&epoll);
set_usb_controller();
const BuiltinFunctionMap function_map;
@@ -717,7 +654,16 @@
am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");
am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");
am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict");
- am.QueueBuiltinAction(keychord_init_action, "keychord_init");
+ Keychords keychords;
+ am.QueueBuiltinAction(
+ [&epoll, &keychords](const BuiltinArguments& args) -> Result<Success> {
+ for (const auto& svc : ServiceList::GetInstance()) {
+ keychords.Register(svc->keycodes());
+ }
+ keychords.Start(&epoll, HandleKeychord);
+ return Success();
+ },
+ "KeychordInit");
am.QueueBuiltinAction(console_init_action, "console_init");
// Trigger all the boot actions to get us started.
@@ -740,7 +686,7 @@
while (true) {
// By default, sleep until something happens.
- int epoll_timeout_ms = -1;
+ auto epoll_timeout = std::optional<std::chrono::milliseconds>{};
if (do_shutdown && !shutting_down) {
do_shutdown = false;
@@ -758,23 +704,18 @@
// If there's a process that needs restarting, wake up in time for that.
if (next_process_restart_time) {
- epoll_timeout_ms = std::chrono::ceil<std::chrono::milliseconds>(
- *next_process_restart_time - boot_clock::now())
- .count();
- if (epoll_timeout_ms < 0) epoll_timeout_ms = 0;
+ epoll_timeout = std::chrono::ceil<std::chrono::milliseconds>(
+ *next_process_restart_time - boot_clock::now());
+ if (*epoll_timeout < 0ms) epoll_timeout = 0ms;
}
}
// If there's more work to do, wake up again immediately.
- if (am.HasMoreCommands()) epoll_timeout_ms = 0;
+ if (am.HasMoreCommands()) epoll_timeout = 0ms;
}
- epoll_event ev;
- int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, epoll_timeout_ms));
- if (nr == -1) {
- PLOG(ERROR) << "epoll_wait failed";
- } else if (nr == 1) {
- ((void (*)()) ev.data.ptr)();
+ if (auto result = epoll.Wait(epoll_timeout); !result) {
+ LOG(ERROR) << result.error();
}
}
diff --git a/init/init.h b/init/init.h
index d4a0e96..f244ad7 100644
--- a/init/init.h
+++ b/init/init.h
@@ -19,6 +19,7 @@
#include <sys/types.h>
+#include <functional>
#include <string>
#include <vector>
@@ -30,8 +31,8 @@
namespace android {
namespace init {
-// Note: These globals are *only* valid in init, so they should not be used in ueventd,
-// watchdogd, or any files that may be included in those, such as devices.cpp and util.cpp.
+// Note: These globals are *only* valid in init, so they should not be used in ueventd
+// or any files that may be included in ueventd, such as devices.cpp and util.cpp.
// TODO: Have an Init class and remove all globals.
extern std::string default_console;
extern std::vector<std::string> late_import_paths;
@@ -42,8 +43,6 @@
void property_changed(const std::string& name, const std::string& value);
-void register_epoll_handler(int fd, void (*fn)());
-
bool start_waiting_for_property(const char *name, const char *value);
void DumpState();
diff --git a/init/init_first_stage.cpp b/init/init_first_stage.cpp
index 0f4cc68..466cde3 100644
--- a/init/init_first_stage.cpp
+++ b/init/init_first_stage.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2017 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,518 +14,156 @@
* limitations under the License.
*/
-#include "init_first_stage.h"
-
+#include <paths.h>
+#include <seccomp_policy.h>
#include <stdlib.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include <sys/types.h>
#include <unistd.h>
-#include <chrono>
-#include <memory>
-#include <set>
#include <string>
#include <vector>
#include <android-base/chrono_utils.h>
-#include <android-base/file.h>
#include <android-base/logging.h>
-#include <android-base/strings.h>
+#include <cutils/android_reboot.h>
+#include <private/android_filesystem_config.h>
+#include <selinux/android.h>
-#include "devices.h"
-#include "fs_mgr.h"
-#include "fs_mgr_avb.h"
-#include "uevent.h"
-#include "uevent_listener.h"
+#include "first_stage_mount.h"
+#include "reboot_utils.h"
+#include "selinux.h"
#include "util.h"
-using android::base::Timer;
+using android::base::boot_clock;
namespace android {
namespace init {
-// Class Declarations
-// ------------------
-class FirstStageMount {
- public:
- FirstStageMount();
- virtual ~FirstStageMount() = default;
-
- // The factory method to create either FirstStageMountVBootV1 or FirstStageMountVBootV2
- // based on device tree configurations.
- static std::unique_ptr<FirstStageMount> Create();
- bool DoFirstStageMount(); // Mounts fstab entries read from device tree.
- bool InitDevices();
-
- protected:
- ListenerAction HandleBlockDevice(const std::string& name, const Uevent&);
- bool InitRequiredDevices();
- bool InitVerityDevice(const std::string& verity_device);
- bool MountPartitions();
-
- virtual ListenerAction UeventCallback(const Uevent& uevent);
-
- // Pure virtual functions.
- virtual bool GetRequiredDevices() = 0;
- virtual bool SetUpDmVerity(fstab_rec* fstab_rec) = 0;
-
- bool need_dm_verity_;
-
- std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> device_tree_fstab_;
- std::vector<fstab_rec*> mount_fstab_recs_;
- std::set<std::string> required_devices_partition_names_;
- DeviceHandler device_handler_;
- UeventListener uevent_listener_;
-};
-
-class FirstStageMountVBootV1 : public FirstStageMount {
- public:
- FirstStageMountVBootV1() = default;
- ~FirstStageMountVBootV1() override = default;
-
- protected:
- bool GetRequiredDevices() override;
- bool SetUpDmVerity(fstab_rec* fstab_rec) override;
-};
-
-class FirstStageMountVBootV2 : public FirstStageMount {
- public:
- friend void SetInitAvbVersionInRecovery();
-
- FirstStageMountVBootV2();
- ~FirstStageMountVBootV2() override = default;
-
- protected:
- ListenerAction UeventCallback(const Uevent& uevent) override;
- bool GetRequiredDevices() override;
- bool SetUpDmVerity(fstab_rec* fstab_rec) override;
- bool InitAvbHandle();
-
- std::string device_tree_vbmeta_parts_;
- FsManagerAvbUniquePtr avb_handle_;
- ByNameSymlinkMap by_name_symlink_map_;
-};
-
-// Static Functions
-// ----------------
-static inline bool IsDtVbmetaCompatible() {
- return is_android_dt_value_expected("vbmeta/compatible", "android,vbmeta");
-}
-
-static bool inline IsRecoveryMode() {
- return access("/sbin/recovery", F_OK) == 0;
-}
-
-// Class Definitions
-// -----------------
-FirstStageMount::FirstStageMount()
- : need_dm_verity_(false), device_tree_fstab_(fs_mgr_read_fstab_dt(), fs_mgr_free_fstab) {
- if (!device_tree_fstab_) {
- // The client of FirstStageMount should check the existence of fstab in device-tree
- // in advance, without parsing it. Reaching here means there is a FATAL error when
- // parsing the fstab. So stop here to expose the failure.
- LOG(FATAL) << "Failed to read fstab from device tree";
- return;
- }
- // Stores device_tree_fstab_->recs[] into mount_fstab_recs_ (vector<fstab_rec*>)
- // for easier manipulation later, e.g., range-base for loop.
- for (int i = 0; i < device_tree_fstab_->num_entries; i++) {
- mount_fstab_recs_.push_back(&device_tree_fstab_->recs[i]);
- }
-}
-
-std::unique_ptr<FirstStageMount> FirstStageMount::Create() {
- if (IsDtVbmetaCompatible()) {
- return std::make_unique<FirstStageMountVBootV2>();
- } else {
- return std::make_unique<FirstStageMountVBootV1>();
- }
-}
-
-bool FirstStageMount::DoFirstStageMount() {
- // Nothing to mount.
- if (mount_fstab_recs_.empty()) return true;
-
- if (!InitDevices()) return false;
-
- if (!MountPartitions()) return false;
-
- return true;
-}
-
-bool FirstStageMount::InitDevices() {
- return GetRequiredDevices() && InitRequiredDevices();
-}
-
-// Creates devices with uevent->partition_name matching one in the member variable
-// required_devices_partition_names_. Found partitions will then be removed from it
-// for the subsequent member function to check which devices are NOT created.
-bool FirstStageMount::InitRequiredDevices() {
- if (required_devices_partition_names_.empty()) {
- return true;
- }
-
- if (need_dm_verity_) {
- const std::string dm_path = "/devices/virtual/misc/device-mapper";
- bool found = false;
- auto dm_callback = [this, &dm_path, &found](const Uevent& uevent) {
- if (uevent.path == dm_path) {
- device_handler_.HandleDeviceEvent(uevent);
- found = true;
- return ListenerAction::kStop;
- }
- return ListenerAction::kContinue;
- };
- uevent_listener_.RegenerateUeventsForPath("/sys" + dm_path, dm_callback);
- if (!found) {
- LOG(INFO) << "device-mapper device not found in /sys, waiting for its uevent";
- Timer t;
- uevent_listener_.Poll(dm_callback, 10s);
- LOG(INFO) << "Wait for device-mapper returned after " << t;
+static void GlobalSeccomp() {
+ import_kernel_cmdline(false, [](const std::string& key, const std::string& value,
+ bool in_qemu) {
+ if (key == "androidboot.seccomp" && value == "global" && !set_global_seccomp_filter()) {
+ LOG(FATAL) << "Failed to globally enable seccomp!";
}
- if (!found) {
- LOG(ERROR) << "device-mapper device not found after polling timeout";
- return false;
- }
- }
-
- auto uevent_callback = [this](const Uevent& uevent) { return UeventCallback(uevent); };
- uevent_listener_.RegenerateUevents(uevent_callback);
-
- // UeventCallback() will remove found partitions from required_devices_partition_names_.
- // So if it isn't empty here, it means some partitions are not found.
- if (!required_devices_partition_names_.empty()) {
- LOG(INFO) << __PRETTY_FUNCTION__
- << ": partition(s) not found in /sys, waiting for their uevent(s): "
- << android::base::Join(required_devices_partition_names_, ", ");
- Timer t;
- uevent_listener_.Poll(uevent_callback, 10s);
- LOG(INFO) << "Wait for partitions returned after " << t;
- }
-
- if (!required_devices_partition_names_.empty()) {
- LOG(ERROR) << __PRETTY_FUNCTION__ << ": partition(s) not found after polling timeout: "
- << android::base::Join(required_devices_partition_names_, ", ");
- return false;
- }
-
- return true;
+ });
}
-ListenerAction FirstStageMount::HandleBlockDevice(const std::string& name, const Uevent& uevent) {
- // Matches partition name to create device nodes.
- // Both required_devices_partition_names_ and uevent->partition_name have A/B
- // suffix when A/B is used.
- auto iter = required_devices_partition_names_.find(name);
- if (iter != required_devices_partition_names_.end()) {
- LOG(VERBOSE) << __PRETTY_FUNCTION__ << ": found partition: " << *iter;
- required_devices_partition_names_.erase(iter);
- device_handler_.HandleDeviceEvent(uevent);
- if (required_devices_partition_names_.empty()) {
- return ListenerAction::kStop;
- } else {
- return ListenerAction::kContinue;
+int main(int argc, char** argv) {
+ if (REBOOT_BOOTLOADER_ON_PANIC) {
+ InstallRebootSignalHandlers();
+ }
+
+ boot_clock::time_point start_time = boot_clock::now();
+
+ std::vector<std::pair<std::string, int>> errors;
+#define CHECKCALL(x) \
+ if (x != 0) errors.emplace_back(#x " failed", errno);
+
+ // Clear the umask.
+ umask(0);
+
+ CHECKCALL(clearenv());
+ CHECKCALL(setenv("PATH", _PATH_DEFPATH, 1));
+ // Get the basic filesystem setup we need put together in the initramdisk
+ // on / and then we'll let the rc file figure out the rest.
+ CHECKCALL(mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"));
+ CHECKCALL(mkdir("/dev/pts", 0755));
+ CHECKCALL(mkdir("/dev/socket", 0755));
+ CHECKCALL(mount("devpts", "/dev/pts", "devpts", 0, NULL));
+#define MAKE_STR(x) __STRING(x)
+ CHECKCALL(mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC)));
+#undef MAKE_STR
+ // Don't expose the raw commandline to unprivileged processes.
+ CHECKCALL(chmod("/proc/cmdline", 0440));
+ gid_t groups[] = {AID_READPROC};
+ CHECKCALL(setgroups(arraysize(groups), groups));
+ CHECKCALL(mount("sysfs", "/sys", "sysfs", 0, NULL));
+ CHECKCALL(mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL));
+
+ CHECKCALL(mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11)));
+
+ if constexpr (WORLD_WRITABLE_KMSG) {
+ CHECKCALL(mknod("/dev/kmsg_debug", S_IFCHR | 0622, makedev(1, 11)));
+ }
+
+ CHECKCALL(mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8)));
+ CHECKCALL(mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9)));
+
+ // This is needed for log wrapper, which gets called before ueventd runs.
+ CHECKCALL(mknod("/dev/ptmx", S_IFCHR | 0666, makedev(5, 2)));
+ CHECKCALL(mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3)));
+
+ // Mount staging areas for devices managed by vold
+ // See storage config details at http://source.android.com/devices/storage/
+ CHECKCALL(mount("tmpfs", "/mnt", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
+ "mode=0755,uid=0,gid=1000"));
+ // /mnt/vendor is used to mount vendor-specific partitions that can not be
+ // part of the vendor partition, e.g. because they are mounted read-write.
+ CHECKCALL(mkdir("/mnt/vendor", 0755));
+ // /mnt/product is used to mount product-specific partitions that can not be
+ // part of the product partition, e.g. because they are mounted read-write.
+ CHECKCALL(mkdir("/mnt/product", 0755));
+
+#undef CHECKCALL
+
+ // Now that tmpfs is mounted on /dev and we have /dev/kmsg, we can actually
+ // talk to the outside world...
+ android::base::InitLogging(argv, &android::base::KernelLogger, [](const char*) {
+ RebootSystem(ANDROID_RB_RESTART2, "bootloader");
+ });
+
+ if (!errors.empty()) {
+ for (const auto& [error_string, error_errno] : errors) {
+ LOG(ERROR) << error_string << " " << strerror(error_errno);
}
- }
- return ListenerAction::kContinue;
-}
-
-ListenerAction FirstStageMount::UeventCallback(const Uevent& uevent) {
- // Ignores everything that is not a block device.
- if (uevent.subsystem != "block") {
- return ListenerAction::kContinue;
+ LOG(FATAL) << "Init encountered errors starting first stage, aborting";
}
- if (!uevent.partition_name.empty()) {
- return HandleBlockDevice(uevent.partition_name, uevent);
- } else {
- size_t base_idx = uevent.path.rfind('/');
- if (base_idx != std::string::npos) {
- return HandleBlockDevice(uevent.path.substr(base_idx + 1), uevent);
- }
- }
- // Not found a partition or find an unneeded partition, continue to find others.
- return ListenerAction::kContinue;
-}
+ LOG(INFO) << "init first stage started!";
-// Creates "/dev/block/dm-XX" for dm-verity by running coldboot on /sys/block/dm-XX.
-bool FirstStageMount::InitVerityDevice(const std::string& verity_device) {
- const std::string device_name(basename(verity_device.c_str()));
- const std::string syspath = "/sys/block/" + device_name;
- bool found = false;
-
- auto verity_callback = [&device_name, &verity_device, this, &found](const Uevent& uevent) {
- if (uevent.device_name == device_name) {
- LOG(VERBOSE) << "Creating dm-verity device : " << verity_device;
- device_handler_.HandleDeviceEvent(uevent);
- found = true;
- return ListenerAction::kStop;
- }
- return ListenerAction::kContinue;
- };
-
- uevent_listener_.RegenerateUeventsForPath(syspath, verity_callback);
- if (!found) {
- LOG(INFO) << "dm-verity device not found in /sys, waiting for its uevent";
- Timer t;
- uevent_listener_.Poll(verity_callback, 10s);
- LOG(INFO) << "wait for dm-verity device returned after " << t;
- }
- if (!found) {
- LOG(ERROR) << "dm-verity device not found after polling timeout";
- return false;
+ if (!DoFirstStageMount()) {
+ LOG(FATAL) << "Failed to mount required partitions early ...";
}
- return true;
-}
+ SetInitAvbVersionInRecovery();
-bool FirstStageMount::MountPartitions() {
- for (auto fstab_rec : mount_fstab_recs_) {
- if (!SetUpDmVerity(fstab_rec)) {
- PLOG(ERROR) << "Failed to setup verity for '" << fstab_rec->mount_point << "'";
- return false;
- }
- if (fs_mgr_do_mount_one(fstab_rec)) {
- PLOG(ERROR) << "Failed to mount '" << fstab_rec->mount_point << "'";
- return false;
- }
- }
- return true;
-}
+ // Does this need to be done in first stage init or can it be done later?
+ // Enable seccomp if global boot option was passed (otherwise it is enabled in zygote).
+ GlobalSeccomp();
-bool FirstStageMountVBootV1::GetRequiredDevices() {
- std::string verity_loc_device;
- need_dm_verity_ = false;
+ // Set up SELinux, loading the SELinux policy.
+ SelinuxSetupKernelLogging();
+ SelinuxInitialize();
- for (auto fstab_rec : mount_fstab_recs_) {
- // Don't allow verifyatboot in the first stage.
- if (fs_mgr_is_verifyatboot(fstab_rec)) {
- LOG(ERROR) << "Partitions can't be verified at boot";
- return false;
- }
- // Checks for verified partitions.
- if (fs_mgr_is_verified(fstab_rec)) {
- need_dm_verity_ = true;
- }
- // Checks if verity metadata is on a separate partition. Note that it is
- // not partition specific, so there must be only one additional partition
- // that carries verity state.
- if (fstab_rec->verity_loc) {
- if (verity_loc_device.empty()) {
- verity_loc_device = fstab_rec->verity_loc;
- } else if (verity_loc_device != fstab_rec->verity_loc) {
- LOG(ERROR) << "More than one verity_loc found: " << verity_loc_device << ", "
- << fstab_rec->verity_loc;
- return false;
- }
- }
+ // We're in the kernel domain and want to transition to the init domain when we exec second
+ // stage init. File systems that store SELabels in their xattrs, such as ext4 do not need an
+ // explicit restorecon here, but other file systems do. In particular, this is needed for
+ // ramdisks such as the recovery image for A/B devices.
+ if (selinux_android_restorecon("/system/bin/init", 0) == -1) {
+ PLOG(FATAL) << "restorecon failed of /system/bin/init failed";
}
- // Includes the partition names of fstab records and verity_loc_device (if any).
- // Notes that fstab_rec->blk_device has A/B suffix updated by fs_mgr when A/B is used.
- for (auto fstab_rec : mount_fstab_recs_) {
- required_devices_partition_names_.emplace(basename(fstab_rec->blk_device));
- }
+ static constexpr uint32_t kNanosecondsPerMillisecond = 1e6;
+ uint64_t start_ms = start_time.time_since_epoch().count() / kNanosecondsPerMillisecond;
+ setenv("INIT_STARTED_AT", std::to_string(start_ms).c_str(), 1);
- if (!verity_loc_device.empty()) {
- required_devices_partition_names_.emplace(basename(verity_loc_device.c_str()));
- }
+ const char* path = "/system/bin/init";
+ const char* args[] = {path, nullptr};
+ execv(path, const_cast<char**>(args));
- return true;
-}
+ // execv() only returns if an error happened, in which case we
+ // panic and never fall through this conditional.
+ PLOG(FATAL) << "execv(\"" << path << "\") failed";
-bool FirstStageMountVBootV1::SetUpDmVerity(fstab_rec* fstab_rec) {
- if (fs_mgr_is_verified(fstab_rec)) {
- int ret = fs_mgr_setup_verity(fstab_rec, false /* wait_for_verity_dev */);
- switch (ret) {
- case FS_MGR_SETUP_VERITY_SKIPPED:
- case FS_MGR_SETUP_VERITY_DISABLED:
- LOG(INFO) << "Verity disabled/skipped for '" << fstab_rec->mount_point << "'";
- return true;
- case FS_MGR_SETUP_VERITY_SUCCESS:
- // The exact block device name (fstab_rec->blk_device) is changed to
- // "/dev/block/dm-XX". Needs to create it because ueventd isn't started in init
- // first stage.
- return InitVerityDevice(fstab_rec->blk_device);
- default:
- return false;
- }
- }
- return true; // Returns true to mount the partition.
-}
-
-// FirstStageMountVBootV2 constructor.
-// Gets the vbmeta partitions from device tree.
-// /{
-// firmware {
-// android {
-// vbmeta {
-// compatible = "android,vbmeta";
-// parts = "vbmeta,boot,system,vendor"
-// };
-// };
-// };
-// }
-FirstStageMountVBootV2::FirstStageMountVBootV2() : avb_handle_(nullptr) {
- if (!read_android_dt_file("vbmeta/parts", &device_tree_vbmeta_parts_)) {
- PLOG(ERROR) << "Failed to read vbmeta/parts from device tree";
- return;
- }
-}
-
-bool FirstStageMountVBootV2::GetRequiredDevices() {
- need_dm_verity_ = false;
-
- // fstab_rec->blk_device has A/B suffix.
- for (auto fstab_rec : mount_fstab_recs_) {
- if (fs_mgr_is_avb(fstab_rec)) {
- need_dm_verity_ = true;
- }
- required_devices_partition_names_.emplace(basename(fstab_rec->blk_device));
- }
-
- // libavb verifies AVB metadata on all verified partitions at once.
- // e.g., The device_tree_vbmeta_parts_ will be "vbmeta,boot,system,vendor"
- // for libavb to verify metadata, even if there is only /vendor in the
- // above mount_fstab_recs_.
- if (need_dm_verity_) {
- if (device_tree_vbmeta_parts_.empty()) {
- LOG(ERROR) << "Missing vbmeta parts in device tree";
- return false;
- }
- std::vector<std::string> partitions = android::base::Split(device_tree_vbmeta_parts_, ",");
- std::string ab_suffix = fs_mgr_get_slot_suffix();
- for (const auto& partition : partitions) {
- // required_devices_partition_names_ is of type std::set so it's not an issue
- // to emplace a partition twice. e.g., /vendor might be in both places:
- // - device_tree_vbmeta_parts_ = "vbmeta,boot,system,vendor"
- // - mount_fstab_recs_: /vendor_a
- required_devices_partition_names_.emplace(partition + ab_suffix);
- }
- }
- return true;
-}
-
-ListenerAction FirstStageMountVBootV2::UeventCallback(const Uevent& uevent) {
- // Check if this uevent corresponds to one of the required partitions and store its symlinks if
- // so, in order to create FsManagerAvbHandle later.
- // Note that the parent callback removes partitions from the list of required partitions
- // as it finds them, so this must happen first.
- if (!uevent.partition_name.empty() &&
- required_devices_partition_names_.find(uevent.partition_name) !=
- required_devices_partition_names_.end()) {
- // GetBlockDeviceSymlinks() will return three symlinks at most, depending on
- // the content of uevent. by-name symlink will be at [0] if uevent->partition_name
- // is not empty. e.g.,
- // - /dev/block/platform/soc.0/f9824900.sdhci/by-name/modem
- // - /dev/block/platform/soc.0/f9824900.sdhci/by-num/p1
- // - /dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1
- std::vector<std::string> links = device_handler_.GetBlockDeviceSymlinks(uevent);
- if (!links.empty()) {
- auto[it, inserted] = by_name_symlink_map_.emplace(uevent.partition_name, links[0]);
- if (!inserted) {
- LOG(ERROR) << "Partition '" << uevent.partition_name
- << "' already existed in the by-name symlink map with a value of '"
- << it->second << "', new value '" << links[0] << "' will be ignored.";
- }
- }
- }
-
- return FirstStageMount::UeventCallback(uevent);
-}
-
-bool FirstStageMountVBootV2::SetUpDmVerity(fstab_rec* fstab_rec) {
- if (fs_mgr_is_avb(fstab_rec)) {
- if (!InitAvbHandle()) return false;
- SetUpAvbHashtreeResult hashtree_result =
- avb_handle_->SetUpAvbHashtree(fstab_rec, false /* wait_for_verity_dev */);
- switch (hashtree_result) {
- case SetUpAvbHashtreeResult::kDisabled:
- return true; // Returns true to mount the partition.
- case SetUpAvbHashtreeResult::kSuccess:
- // The exact block device name (fstab_rec->blk_device) is changed to
- // "/dev/block/dm-XX". Needs to create it because ueventd isn't started in init
- // first stage.
- return InitVerityDevice(fstab_rec->blk_device);
- default:
- return false;
- }
- }
- return true; // Returns true to mount the partition.
-}
-
-bool FirstStageMountVBootV2::InitAvbHandle() {
- if (avb_handle_) return true; // Returns true if the handle is already initialized.
-
- if (by_name_symlink_map_.empty()) {
- LOG(ERROR) << "by_name_symlink_map_ is empty";
- return false;
- }
-
- avb_handle_ = FsManagerAvbHandle::Open(std::move(by_name_symlink_map_));
- by_name_symlink_map_.clear(); // Removes all elements after the above std::move().
-
- if (!avb_handle_) {
- PLOG(ERROR) << "Failed to open FsManagerAvbHandle";
- return false;
- }
- // Sets INIT_AVB_VERSION here for init to set ro.boot.avb_version in the second stage.
- setenv("INIT_AVB_VERSION", avb_handle_->avb_version().c_str(), 1);
- return true;
-}
-
-// Public functions
-// ----------------
-// Mounts partitions specified by fstab in device tree.
-bool DoFirstStageMount() {
- // Skips first stage mount if we're in recovery mode.
- if (IsRecoveryMode()) {
- LOG(INFO) << "First stage mount skipped (recovery mode)";
- return true;
- }
-
- // Firstly checks if device tree fstab entries are compatible.
- if (!is_android_dt_value_expected("fstab/compatible", "android,fstab")) {
- LOG(INFO) << "First stage mount skipped (missing/incompatible fstab in device tree)";
- return true;
- }
-
- std::unique_ptr<FirstStageMount> handle = FirstStageMount::Create();
- if (!handle) {
- LOG(ERROR) << "Failed to create FirstStageMount";
- return false;
- }
- return handle->DoFirstStageMount();
-}
-
-void SetInitAvbVersionInRecovery() {
- if (!IsRecoveryMode()) {
- LOG(INFO) << "Skipped setting INIT_AVB_VERSION (not in recovery mode)";
- return;
- }
-
- if (!IsDtVbmetaCompatible()) {
- LOG(INFO) << "Skipped setting INIT_AVB_VERSION (not vbmeta compatible)";
- return;
- }
-
- // Initializes required devices for the subsequent FsManagerAvbHandle::Open()
- // to verify AVB metadata on all partitions in the verified chain.
- // We only set INIT_AVB_VERSION when the AVB verification succeeds, i.e., the
- // Open() function returns a valid handle.
- // We don't need to mount partitions here in recovery mode.
- FirstStageMountVBootV2 avb_first_mount;
- if (!avb_first_mount.InitDevices()) {
- LOG(ERROR) << "Failed to init devices for INIT_AVB_VERSION";
- return;
- }
-
- FsManagerAvbUniquePtr avb_handle =
- FsManagerAvbHandle::Open(std::move(avb_first_mount.by_name_symlink_map_));
- if (!avb_handle) {
- PLOG(ERROR) << "Failed to open FsManagerAvbHandle for INIT_AVB_VERSION";
- return;
- }
- setenv("INIT_AVB_VERSION", avb_handle->avb_version().c_str(), 1);
+ return 1;
}
} // namespace init
} // namespace android
+
+int main(int argc, char** argv) {
+ return android::init::main(argc, argv);
+}
diff --git a/init/keychords.cpp b/init/keychords.cpp
index e686ce1..1af06dd 100644
--- a/init/keychords.cpp
+++ b/init/keychords.cpp
@@ -16,110 +16,269 @@
#include "keychords.h"
+#include <dirent.h>
#include <fcntl.h>
-#include <stdlib.h>
-#include <sys/stat.h>
+#include <linux/input.h>
+#include <sys/cdefs.h>
+#include <sys/inotify.h>
+#include <sys/ioctl.h>
#include <sys/types.h>
-#include <linux/keychord.h>
#include <unistd.h>
-#include <android-base/logging.h>
-#include <android-base/properties.h>
+#include <algorithm>
+#include <functional>
+#include <map>
+#include <memory>
+#include <string>
+#include <vector>
-#include "init.h"
+#include <android-base/logging.h>
namespace android {
namespace init {
-static struct input_keychord *keychords = 0;
-static int keychords_count = 0;
-static int keychords_length = 0;
-static int keychord_fd = -1;
+Keychords::Keychords() : epoll_(nullptr), inotify_fd_(-1) {}
-void add_service_keycodes(Service* svc)
-{
- struct input_keychord *keychord;
- size_t i, size;
-
- if (!svc->keycodes().empty()) {
- /* add a new keychord to the list */
- size = sizeof(*keychord) + svc->keycodes().size() * sizeof(keychord->keycodes[0]);
- keychords = (input_keychord*) realloc(keychords, keychords_length + size);
- if (!keychords) {
- PLOG(ERROR) << "could not allocate keychords";
- keychords_length = 0;
- keychords_count = 0;
- return;
- }
-
- keychord = (struct input_keychord *)((char *)keychords + keychords_length);
- keychord->version = KEYCHORD_VERSION;
- keychord->id = keychords_count + 1;
- keychord->count = svc->keycodes().size();
- svc->set_keychord_id(keychord->id);
-
- for (i = 0; i < svc->keycodes().size(); i++) {
- keychord->keycodes[i] = svc->keycodes()[i];
- }
- keychords_count++;
- keychords_length += size;
+Keychords::~Keychords() noexcept {
+ if (inotify_fd_ >= 0) {
+ epoll_->UnregisterHandler(inotify_fd_);
+ ::close(inotify_fd_);
}
+ while (!registration_.empty()) GeteventCloseDevice(registration_.begin()->first);
}
-static void handle_keychord() {
- int ret;
- __u16 id;
+Keychords::Mask::Mask(size_t bit) : bits_((bit + sizeof(mask_t) - 1) / sizeof(mask_t), 0) {}
- ret = read(keychord_fd, &id, sizeof(id));
- if (ret != sizeof(id)) {
- PLOG(ERROR) << "could not read keychord id";
- return;
- }
-
- // Only handle keychords if adb is enabled.
- std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
- if (adb_enabled == "running") {
- Service* svc = ServiceList::GetInstance().FindService(id, &Service::keychord_id);
- if (svc) {
- LOG(INFO) << "Starting service '" << svc->name() << "' from keychord " << id;
- if (auto result = svc->Start(); !result) {
- LOG(ERROR) << "Could not start service '" << svc->name() << "' from keychord " << id
- << ": " << result.error();
- }
- } else {
- LOG(ERROR) << "Service for keychord " << id << " not found";
- }
+void Keychords::Mask::SetBit(size_t bit, bool value) {
+ auto idx = bit / (kBitsPerByte * sizeof(mask_t));
+ if (idx >= bits_.size()) return;
+ if (value) {
+ bits_[idx] |= mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t)));
} else {
- LOG(WARNING) << "Not starting service for keychord " << id << " because ADB is disabled";
+ bits_[idx] &= ~(mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t))));
}
}
-void keychord_init() {
- for (const auto& service : ServiceList::GetInstance()) {
- add_service_keycodes(service.get());
+bool Keychords::Mask::GetBit(size_t bit) const {
+ auto idx = bit / (kBitsPerByte * sizeof(mask_t));
+ return bits_[idx] & (mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t))));
+}
+
+size_t Keychords::Mask::bytesize() const {
+ return bits_.size() * sizeof(mask_t);
+}
+
+void* Keychords::Mask::data() {
+ return bits_.data();
+}
+
+size_t Keychords::Mask::size() const {
+ return bits_.size() * sizeof(mask_t) * kBitsPerByte;
+}
+
+void Keychords::Mask::resize(size_t bit) {
+ auto idx = bit / (kBitsPerByte * sizeof(mask_t));
+ if (idx >= bits_.size()) {
+ bits_.resize(idx + 1, 0);
+ }
+}
+
+Keychords::Mask::operator bool() const {
+ for (size_t i = 0; i < bits_.size(); ++i) {
+ if (bits_[i]) return true;
+ }
+ return false;
+}
+
+Keychords::Mask Keychords::Mask::operator&(const Keychords::Mask& rval) const {
+ auto len = std::min(bits_.size(), rval.bits_.size());
+ Keychords::Mask ret;
+ ret.bits_.resize(len);
+ for (size_t i = 0; i < len; ++i) {
+ ret.bits_[i] = bits_[i] & rval.bits_[i];
+ }
+ return ret;
+}
+
+void Keychords::Mask::operator|=(const Keychords::Mask& rval) {
+ auto len = rval.bits_.size();
+ bits_.resize(len);
+ for (size_t i = 0; i < len; ++i) {
+ bits_[i] |= rval.bits_[i];
+ }
+}
+
+Keychords::Entry::Entry() : notified(false) {}
+
+void Keychords::LambdaCheck() {
+ for (auto& [keycodes, entry] : entries_) {
+ auto found = true;
+ for (auto& code : keycodes) {
+ if (!current_.GetBit(code)) {
+ entry.notified = false;
+ found = false;
+ break;
+ }
+ }
+ if (!found) continue;
+ if (entry.notified) continue;
+ entry.notified = true;
+ handler_(keycodes);
+ }
+}
+
+void Keychords::LambdaHandler(int fd) {
+ input_event event;
+ auto res = TEMP_FAILURE_RETRY(::read(fd, &event, sizeof(event)));
+ if ((res != sizeof(event)) || (event.type != EV_KEY)) return;
+ current_.SetBit(event.code, event.value);
+ LambdaCheck();
+}
+
+bool Keychords::GeteventEnable(int fd) {
+ // Make sure it is an event channel, should pass this ioctl call
+ int version;
+ if (::ioctl(fd, EVIOCGVERSION, &version)) return false;
+
+#ifdef EVIOCSMASK
+ static auto EviocsmaskSupported = true;
+ if (EviocsmaskSupported) {
+ Keychords::Mask mask(EV_KEY);
+ mask.SetBit(EV_KEY);
+ input_mask msg = {};
+ msg.type = EV_SYN;
+ msg.codes_size = mask.bytesize();
+ msg.codes_ptr = reinterpret_cast<uintptr_t>(mask.data());
+ if (::ioctl(fd, EVIOCSMASK, &msg) == -1) {
+ PLOG(WARNING) << "EVIOCSMASK not supported";
+ EviocsmaskSupported = false;
+ }
+ }
+#endif
+
+ Keychords::Mask mask;
+ for (auto& [keycodes, entry] : entries_) {
+ for (auto& code : keycodes) {
+ mask.resize(code);
+ mask.SetBit(code);
+ }
}
- // Nothing to do if no services require keychords.
- if (!keychords) {
+ current_.resize(mask.size());
+ Keychords::Mask available(mask.size());
+ auto res = ::ioctl(fd, EVIOCGBIT(EV_KEY, available.bytesize()), available.data());
+ if (res == -1) return false;
+ if (!(available & mask)) return false;
+
+#ifdef EVIOCSMASK
+ if (EviocsmaskSupported) {
+ input_mask msg = {};
+ msg.type = EV_KEY;
+ msg.codes_size = mask.bytesize();
+ msg.codes_ptr = reinterpret_cast<uintptr_t>(mask.data());
+ ::ioctl(fd, EVIOCSMASK, &msg);
+ }
+#endif
+
+ Keychords::Mask set(mask.size());
+ res = ::ioctl(fd, EVIOCGKEY(res), set.data());
+ if (res > 0) {
+ current_ |= mask & available & set;
+ LambdaCheck();
+ }
+ epoll_->RegisterHandler(fd, [this, fd]() { this->LambdaHandler(fd); });
+ return true;
+}
+
+void Keychords::GeteventOpenDevice(const std::string& device) {
+ if (registration_.count(device)) return;
+ auto fd = TEMP_FAILURE_RETRY(::open(device.c_str(), O_RDWR | O_CLOEXEC));
+ if (fd == -1) {
+ PLOG(ERROR) << "Can not open " << device;
+ return;
+ }
+ if (!GeteventEnable(fd)) {
+ ::close(fd);
+ } else {
+ registration_.emplace(device, fd);
+ }
+}
+
+void Keychords::GeteventCloseDevice(const std::string& device) {
+ auto it = registration_.find(device);
+ if (it == registration_.end()) return;
+ auto fd = (*it).second;
+ epoll_->UnregisterHandler(fd);
+ registration_.erase(it);
+ ::close(fd);
+}
+
+void Keychords::InotifyHandler() {
+ unsigned char buf[512]; // History shows 32-64 bytes typical
+
+ auto res = TEMP_FAILURE_RETRY(::read(inotify_fd_, buf, sizeof(buf)));
+ if (res < 0) {
+ PLOG(WARNING) << "could not get event";
return;
}
- keychord_fd = TEMP_FAILURE_RETRY(open("/dev/keychord", O_RDWR | O_CLOEXEC));
- if (keychord_fd == -1) {
- PLOG(ERROR) << "could not open /dev/keychord";
- return;
+ auto event_buf = buf;
+ while (static_cast<size_t>(res) >= sizeof(inotify_event)) {
+ auto event = reinterpret_cast<inotify_event*>(event_buf);
+ auto event_size = sizeof(inotify_event) + event->len;
+ if (static_cast<size_t>(res) < event_size) break;
+ if (event->len) {
+ std::string devname(kDevicePath);
+ devname += '/';
+ devname += event->name;
+ if (event->mask & IN_CREATE) {
+ GeteventOpenDevice(devname);
+ } else {
+ GeteventCloseDevice(devname);
+ }
+ }
+ res -= event_size;
+ event_buf += event_size;
+ }
+}
+
+void Keychords::GeteventOpenDevice() {
+ inotify_fd_ = ::inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
+ if (inotify_fd_ < 0) {
+ PLOG(WARNING) << "Could not instantiate inotify for " << kDevicePath;
+ } else if (::inotify_add_watch(inotify_fd_, kDevicePath, IN_DELETE | IN_CREATE | IN_ONLYDIR) <
+ 0) {
+ PLOG(WARNING) << "Could not add watch for " << kDevicePath;
+ ::close(inotify_fd_);
+ inotify_fd_ = -1;
}
- int ret = write(keychord_fd, keychords, keychords_length);
- if (ret != keychords_length) {
- PLOG(ERROR) << "could not configure /dev/keychord " << ret;
- close(keychord_fd);
+ std::unique_ptr<DIR, decltype(&closedir)> device(opendir(kDevicePath), closedir);
+ if (device) {
+ dirent* entry;
+ while ((entry = readdir(device.get()))) {
+ if (entry->d_name[0] == '.') continue;
+ std::string devname(kDevicePath);
+ devname += '/';
+ devname += entry->d_name;
+ GeteventOpenDevice(devname);
+ }
}
- free(keychords);
- keychords = nullptr;
+ if (inotify_fd_ >= 0) {
+ epoll_->RegisterHandler(inotify_fd_, [this]() { this->InotifyHandler(); });
+ }
+}
- register_epoll_handler(keychord_fd, handle_keychord);
+void Keychords::Register(const std::vector<int>& keycodes) {
+ if (keycodes.empty()) return;
+ entries_.try_emplace(keycodes, Entry());
+}
+
+void Keychords::Start(Epoll* epoll, std::function<void(const std::vector<int>&)> handler) {
+ epoll_ = epoll;
+ handler_ = handler;
+ if (entries_.size()) GeteventOpenDevice();
}
} // namespace init
diff --git a/init/keychords.h b/init/keychords.h
index 1c34098..00ed205 100644
--- a/init/keychords.h
+++ b/init/keychords.h
@@ -17,13 +17,81 @@
#ifndef _INIT_KEYCHORDS_H_
#define _INIT_KEYCHORDS_H_
-#include "service.h"
+#include <functional>
+#include <map>
+#include <string>
+#include <vector>
+
+#include "epoll.h"
namespace android {
namespace init {
-void add_service_keycodes(Service* svc);
-void keychord_init();
+class Keychords {
+ public:
+ Keychords();
+ Keychords(const Keychords&) = delete;
+ Keychords(Keychords&&) = delete;
+ Keychords& operator=(const Keychords&) = delete;
+ Keychords& operator=(Keychords&&) = delete;
+ ~Keychords() noexcept;
+
+ void Register(const std::vector<int>& keycodes);
+ void Start(Epoll* epoll, std::function<void(const std::vector<int>&)> handler);
+
+ private:
+ // Bit management
+ class Mask {
+ public:
+ explicit Mask(size_t bit = 0);
+
+ void SetBit(size_t bit, bool value = true);
+ bool GetBit(size_t bit) const;
+
+ size_t bytesize() const;
+ void* data();
+ size_t size() const;
+ void resize(size_t bit);
+
+ operator bool() const;
+ Mask operator&(const Mask& rval) const;
+ void operator|=(const Mask& rval);
+
+ private:
+ typedef unsigned int mask_t;
+ static constexpr size_t kBitsPerByte = 8;
+
+ std::vector<mask_t> bits_;
+ };
+
+ struct Entry {
+ Entry();
+
+ bool notified;
+ };
+
+ static constexpr char kDevicePath[] = "/dev/input";
+
+ void LambdaCheck();
+ void LambdaHandler(int fd);
+ void InotifyHandler();
+
+ bool GeteventEnable(int fd);
+ void GeteventOpenDevice(const std::string& device);
+ void GeteventOpenDevice();
+ void GeteventCloseDevice(const std::string& device);
+
+ Epoll* epoll_;
+ std::function<void(const std::vector<int>&)> handler_;
+
+ std::map<std::string, int> registration_;
+
+ std::map<const std::vector<int>, Entry> entries_;
+
+ Mask current_;
+
+ int inotify_fd_;
+};
} // namespace init
} // namespace android
diff --git a/init/keychords_test.cpp b/init/keychords_test.cpp
new file mode 100644
index 0000000..c8c47a8
--- /dev/null
+++ b/init/keychords_test.cpp
@@ -0,0 +1,348 @@
+/*
+ * Copyright (C) 2018 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 "keychords.h"
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <linux/input.h>
+#include <linux/uinput.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+#include <chrono>
+#include <set>
+#include <string>
+#include <vector>
+
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+#include <gtest/gtest.h>
+
+#include "epoll.h"
+
+using namespace std::chrono_literals;
+
+namespace android {
+namespace init {
+
+namespace {
+
+// This class is used to inject keys.
+class EventHandler {
+ public:
+ EventHandler();
+ EventHandler(const EventHandler&) = delete;
+ EventHandler(EventHandler&&);
+ EventHandler& operator=(const EventHandler&) = delete;
+ EventHandler& operator=(EventHandler&&);
+ ~EventHandler() noexcept;
+
+ bool init();
+
+ bool send(struct input_event& e);
+ bool send(uint16_t type, uint16_t code, uint16_t value);
+ bool send(uint16_t code, bool value);
+
+ private:
+ int fd_;
+};
+
+EventHandler::EventHandler() : fd_(-1) {}
+
+EventHandler::EventHandler(EventHandler&& rval) : fd_(rval.fd_) {
+ rval.fd_ = -1;
+}
+
+EventHandler& EventHandler::operator=(EventHandler&& rval) {
+ fd_ = rval.fd_;
+ rval.fd_ = -1;
+ return *this;
+}
+
+EventHandler::~EventHandler() {
+ if (fd_ == -1) return;
+ ::ioctl(fd_, UI_DEV_DESTROY);
+ ::close(fd_);
+}
+
+bool EventHandler::init() {
+ if (fd_ != -1) return true;
+ auto fd = TEMP_FAILURE_RETRY(::open("/dev/uinput", O_WRONLY | O_NONBLOCK | O_CLOEXEC));
+ if (fd == -1) return false;
+ if (::ioctl(fd, UI_SET_EVBIT, EV_KEY) == -1) {
+ ::close(fd);
+ return false;
+ }
+
+ static const struct uinput_user_dev u = {
+ .name = "com.google.android.init.test",
+ .id.bustype = BUS_VIRTUAL,
+ .id.vendor = 0x1AE0, // Google
+ .id.product = 0x494E, // IN
+ .id.version = 1,
+ };
+ if (TEMP_FAILURE_RETRY(::write(fd, &u, sizeof(u))) != sizeof(u)) {
+ ::close(fd);
+ return false;
+ }
+
+ // all keys
+ for (uint16_t i = 0; i < KEY_MAX; ++i) {
+ if (::ioctl(fd, UI_SET_KEYBIT, i) == -1) {
+ ::close(fd);
+ return false;
+ }
+ }
+ if (::ioctl(fd, UI_DEV_CREATE) == -1) {
+ ::close(fd);
+ return false;
+ }
+ fd_ = fd;
+ return true;
+}
+
+bool EventHandler::send(struct input_event& e) {
+ gettimeofday(&e.time, nullptr);
+ return TEMP_FAILURE_RETRY(::write(fd_, &e, sizeof(e))) == sizeof(e);
+}
+
+bool EventHandler::send(uint16_t type, uint16_t code, uint16_t value) {
+ struct input_event e = {.type = type, .code = code, .value = value};
+ return send(e);
+}
+
+bool EventHandler::send(uint16_t code, bool value) {
+ return (code < KEY_MAX) && init() && send(EV_KEY, code, value) && send(EV_SYN, SYN_REPORT, 0);
+}
+
+std::string InitFds(const char* prefix, pid_t pid = getpid()) {
+ std::string ret;
+
+ std::string init_fds("/proc/");
+ init_fds += std::to_string(pid) + "/fd";
+ std::unique_ptr<DIR, decltype(&closedir)> fds(opendir(init_fds.c_str()), closedir);
+ if (!fds) return ret;
+
+ dirent* entry;
+ while ((entry = readdir(fds.get()))) {
+ if (entry->d_name[0] == '.') continue;
+ std::string devname = init_fds + '/' + entry->d_name;
+ char buf[256];
+ auto retval = readlink(devname.c_str(), buf, sizeof(buf) - 1);
+ if ((retval < 0) || (size_t(retval) >= (sizeof(buf) - 1))) continue;
+ buf[retval] = '\0';
+ if (!android::base::StartsWith(buf, prefix)) continue;
+ if (ret.size() != 0) ret += ",";
+ ret += buf;
+ }
+ return ret;
+}
+
+std::string InitInputFds() {
+ return InitFds("/dev/input/");
+}
+
+std::string InitInotifyFds() {
+ return InitFds("anon_inode:inotify");
+}
+
+// NB: caller (this series of tests, or conversely the service parser in init)
+// is responsible for validation, sorting and uniqueness of the chords, so no
+// fuzzing is advised.
+
+const std::vector<int> escape_chord = {KEY_ESC};
+const std::vector<int> triple1_chord = {KEY_BACKSPACE, KEY_VOLUMEDOWN, KEY_VOLUMEUP};
+const std::vector<int> triple2_chord = {KEY_VOLUMEDOWN, KEY_VOLUMEUP, KEY_BACK};
+
+const std::vector<const std::vector<int>> empty_chords;
+const std::vector<const std::vector<int>> chords = {
+ escape_chord,
+ triple1_chord,
+ triple2_chord,
+};
+
+class TestFrame {
+ public:
+ TestFrame(const std::vector<const std::vector<int>>& chords, EventHandler* ev = nullptr);
+
+ void RelaxForMs(std::chrono::milliseconds wait = 1ms);
+
+ void SetChord(int key, bool value = true);
+ void SetChords(const std::vector<int>& chord, bool value = true);
+ void ClrChord(int key);
+ void ClrChords(const std::vector<int>& chord);
+
+ bool IsOnlyChord(const std::vector<int>& chord) const;
+ bool IsNoChord() const;
+ bool IsChord(const std::vector<int>& chord) const;
+ void WaitForChord(const std::vector<int>& chord);
+
+ std::string Format() const;
+
+ private:
+ static std::string Format(const std::vector<const std::vector<int>>& chords);
+
+ Epoll epoll_;
+ Keychords keychords_;
+ std::vector<const std::vector<int>> keycodes_;
+ EventHandler* ev_;
+};
+
+TestFrame::TestFrame(const std::vector<const std::vector<int>>& chords, EventHandler* ev)
+ : ev_(ev) {
+ if (!epoll_.Open()) return;
+ for (const auto& keycodes : chords) keychords_.Register(keycodes);
+ keychords_.Start(&epoll_, [this](const std::vector<int>& keycodes) {
+ this->keycodes_.emplace_back(keycodes);
+ });
+}
+
+void TestFrame::RelaxForMs(std::chrono::milliseconds wait) {
+ epoll_.Wait(wait);
+}
+
+void TestFrame::SetChord(int key, bool value) {
+ ASSERT_TRUE(!!ev_);
+ RelaxForMs();
+ EXPECT_TRUE(ev_->send(key, value));
+}
+
+void TestFrame::SetChords(const std::vector<int>& chord, bool value) {
+ ASSERT_TRUE(!!ev_);
+ for (auto& key : chord) SetChord(key, value);
+ RelaxForMs();
+}
+
+void TestFrame::ClrChord(int key) {
+ ASSERT_TRUE(!!ev_);
+ SetChord(key, false);
+}
+
+void TestFrame::ClrChords(const std::vector<int>& chord) {
+ ASSERT_TRUE(!!ev_);
+ SetChords(chord, false);
+}
+
+bool TestFrame::IsOnlyChord(const std::vector<int>& chord) const {
+ auto ret = false;
+ for (const auto& keycode : keycodes_) {
+ if (keycode != chord) return false;
+ ret = true;
+ }
+ return ret;
+}
+
+bool TestFrame::IsNoChord() const {
+ return keycodes_.empty();
+}
+
+bool TestFrame::IsChord(const std::vector<int>& chord) const {
+ for (const auto& keycode : keycodes_) {
+ if (keycode == chord) return true;
+ }
+ return false;
+}
+
+void TestFrame::WaitForChord(const std::vector<int>& chord) {
+ for (int retry = 1000; retry && !IsChord(chord); --retry) RelaxForMs();
+}
+
+std::string TestFrame::Format(const std::vector<const std::vector<int>>& chords) {
+ std::string ret("{");
+ if (!chords.empty()) {
+ ret += android::base::Join(chords.front(), ' ');
+ for (auto it = std::next(chords.begin()); it != chords.end(); ++it) {
+ ret += ',';
+ ret += android::base::Join(*it, ' ');
+ }
+ }
+ return ret + '}';
+}
+
+std::string TestFrame::Format() const {
+ return Format(keycodes_);
+}
+
+} // namespace
+
+TEST(keychords, not_instantiated) {
+ TestFrame test_frame(empty_chords);
+ EXPECT_TRUE(InitInotifyFds().size() == 0);
+}
+
+TEST(keychords, instantiated) {
+ // Test if a valid set of chords results in proper instantiation of the
+ // underlying mechanisms for /dev/input/ attachment.
+ TestFrame test_frame(chords);
+ EXPECT_TRUE(InitInotifyFds().size() != 0);
+}
+
+TEST(keychords, init_inotify) {
+ std::string before(InitInputFds());
+
+ TestFrame test_frame(chords);
+
+ EventHandler ev;
+ EXPECT_TRUE(ev.init());
+
+ for (int retry = 1000; retry && before == InitInputFds(); --retry) test_frame.RelaxForMs();
+ std::string after(InitInputFds());
+ EXPECT_NE(before, after);
+}
+
+TEST(keychords, key) {
+ EventHandler ev;
+ EXPECT_TRUE(ev.init());
+ TestFrame test_frame(chords, &ev);
+
+ test_frame.SetChords(escape_chord);
+ test_frame.WaitForChord(escape_chord);
+ test_frame.ClrChords(escape_chord);
+ EXPECT_TRUE(test_frame.IsOnlyChord(escape_chord))
+ << "expected only " << android::base::Join(escape_chord, ' ') << " got "
+ << test_frame.Format();
+}
+
+TEST(keychords, keys_in_series) {
+ EventHandler ev;
+ EXPECT_TRUE(ev.init());
+ TestFrame test_frame(chords, &ev);
+
+ for (auto& key : triple1_chord) {
+ test_frame.SetChord(key);
+ test_frame.ClrChord(key);
+ }
+ test_frame.WaitForChord(triple1_chord);
+ EXPECT_TRUE(test_frame.IsNoChord()) << "expected nothing got " << test_frame.Format();
+}
+
+TEST(keychords, keys_in_parallel) {
+ EventHandler ev;
+ EXPECT_TRUE(ev.init());
+ TestFrame test_frame(chords, &ev);
+
+ test_frame.SetChords(triple2_chord);
+ test_frame.WaitForChord(triple2_chord);
+ test_frame.ClrChords(triple2_chord);
+ EXPECT_TRUE(test_frame.IsOnlyChord(triple2_chord))
+ << "expected only " << android::base::Join(triple2_chord, ' ') << " got "
+ << test_frame.Format();
+}
+
+} // namespace init
+} // namespace android
diff --git a/init/log.cpp b/init/log.cpp
deleted file mode 100644
index 6198fc2..0000000
--- a/init/log.cpp
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Copyright (C) 2015 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 "log.h"
-
-#include <fcntl.h>
-#include <linux/audit.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <android-base/logging.h>
-#include <cutils/android_reboot.h>
-#include <selinux/selinux.h>
-
-#include "reboot.h"
-
-namespace android {
-namespace init {
-
-static void InitAborter(const char* abort_message) {
- // When init forks, it continues to use this aborter for LOG(FATAL), but we want children to
- // simply abort instead of trying to reboot the system.
- if (getpid() != 1) {
- android::base::DefaultAborter(abort_message);
- return;
- }
-
- // DoReboot() does a lot to try to shutdown the system cleanly. If something happens to call
- // LOG(FATAL) in the shutdown path, we want to catch this and immediately use the syscall to
- // reboot instead of recursing here.
- static bool has_aborted = false;
- if (!has_aborted) {
- has_aborted = true;
- // Do not queue "shutdown" trigger since we want to shutdown immediately and it's not likely
- // that we can even run the ActionQueue at this point.
- DoReboot(ANDROID_RB_RESTART2, "reboot", "bootloader", false);
- } else {
- RebootSystem(ANDROID_RB_RESTART2, "bootloader");
- }
-}
-
-void InitKernelLogging(char* argv[]) {
- // Make stdin/stdout/stderr all point to /dev/null.
- int fd = open("/sys/fs/selinux/null", O_RDWR);
- if (fd == -1) {
- int saved_errno = errno;
- android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
- errno = saved_errno;
- PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
- }
- dup2(fd, 0);
- dup2(fd, 1);
- dup2(fd, 2);
- if (fd > 2) close(fd);
-
- android::base::InitLogging(argv, &android::base::KernelLogger, InitAborter);
-}
-
-int selinux_klog_callback(int type, const char *fmt, ...) {
- android::base::LogSeverity severity = android::base::ERROR;
- if (type == SELINUX_WARNING) {
- severity = android::base::WARNING;
- } else if (type == SELINUX_INFO) {
- severity = android::base::INFO;
- }
- char buf[1024];
- va_list ap;
- va_start(ap, fmt);
- vsnprintf(buf, sizeof(buf), fmt, ap);
- va_end(ap);
- android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
- return 0;
-}
-
-} // namespace init
-} // namespace android
diff --git a/init/modalias_handler.cpp b/init/modalias_handler.cpp
new file mode 100644
index 0000000..1e0db57
--- /dev/null
+++ b/init/modalias_handler.cpp
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2018 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 "modalias_handler.h"
+
+#include <fnmatch.h>
+#include <sys/syscall.h>
+
+#include <algorithm>
+#include <functional>
+#include <string>
+#include <vector>
+
+#include <android-base/chrono_utils.h>
+#include <android-base/logging.h>
+#include <android-base/unique_fd.h>
+
+#include "parser.h"
+
+namespace android {
+namespace init {
+
+Result<Success> ModaliasHandler::ParseDepCallback(std::vector<std::string>&& args) {
+ std::vector<std::string> deps;
+
+ // Set first item as our modules path
+ std::string::size_type pos = args[0].find(':');
+ if (pos != std::string::npos) {
+ deps.emplace_back(args[0].substr(0, pos));
+ } else {
+ return Error() << "dependency lines must start with name followed by ':'";
+ }
+
+ // Remaining items are dependencies of our module
+ for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
+ deps.push_back(*arg);
+ }
+
+ // Key is striped module name to match names in alias file
+ std::size_t start = args[0].find_last_of("/");
+ std::size_t end = args[0].find(".ko:");
+ if ((end - start) <= 1) return Error() << "malformed dependency line";
+ auto mod_name = args[0].substr(start + 1, (end - start) - 1);
+ // module names can have '-', but their file names will have '_'
+ std::replace(mod_name.begin(), mod_name.end(), '-', '_');
+ this->module_deps_[mod_name] = deps;
+
+ return Success();
+}
+
+Result<Success> ModaliasHandler::ParseAliasCallback(std::vector<std::string>&& args) {
+ auto it = args.begin();
+ const std::string& type = *it++;
+
+ if (type != "alias") {
+ return Error() << "we only handle alias lines, got: " << type;
+ }
+
+ if (args.size() != 3) {
+ return Error() << "alias lines must have 3 entries";
+ }
+
+ std::string& alias = *it++;
+ std::string& module_name = *it++;
+ this->module_aliases_.emplace_back(alias, module_name);
+
+ return Success();
+}
+
+ModaliasHandler::ModaliasHandler() {
+ using namespace std::placeholders;
+
+ static const std::string base_paths[] = {
+ "/vendor/lib/modules/",
+ "/lib/modules/",
+ "/odm/lib/modules/",
+ };
+
+ Parser alias_parser;
+ auto alias_callback = std::bind(&ModaliasHandler::ParseAliasCallback, this, _1);
+ alias_parser.AddSingleLineParser("alias", alias_callback);
+ for (const auto& base_path : base_paths) alias_parser.ParseConfig(base_path + "modules.alias");
+
+ Parser dep_parser;
+ auto dep_callback = std::bind(&ModaliasHandler::ParseDepCallback, this, _1);
+ dep_parser.AddSingleLineParser("", dep_callback);
+ for (const auto& base_path : base_paths) dep_parser.ParseConfig(base_path + "modules.dep");
+}
+
+Result<Success> ModaliasHandler::Insmod(const std::string& path_name, const std::string& args) {
+ base::unique_fd fd(
+ TEMP_FAILURE_RETRY(open(path_name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
+ if (fd == -1) return ErrnoError() << "Could not open module '" << path_name << "'";
+
+ int ret = syscall(__NR_finit_module, fd.get(), args.c_str(), 0);
+ if (ret != 0) {
+ if (errno == EEXIST) {
+ // Module already loaded
+ return Success();
+ }
+ return ErrnoError() << "Failed to insmod '" << path_name << "' with args '" << args << "'";
+ }
+
+ LOG(INFO) << "Loaded kernel module " << path_name;
+ return Success();
+}
+
+Result<Success> ModaliasHandler::InsmodWithDeps(const std::string& module_name,
+ const std::string& args) {
+ if (module_name.empty()) {
+ return Error() << "Need valid module name";
+ }
+
+ auto it = module_deps_.find(module_name);
+ if (it == module_deps_.end()) {
+ return Error() << "Module '" << module_name << "' not in dependency file";
+ }
+ auto& dependencies = it->second;
+
+ // load module dependencies in reverse order
+ for (auto dep = dependencies.rbegin(); dep != dependencies.rend() - 1; ++dep) {
+ if (auto result = Insmod(*dep, ""); !result) return result;
+ }
+
+ // load target module itself with args
+ return Insmod(dependencies[0], args);
+}
+
+void ModaliasHandler::HandleUevent(const Uevent& uevent) {
+ if (uevent.modalias.empty()) return;
+
+ for (const auto& [alias, module] : module_aliases_) {
+ if (fnmatch(alias.c_str(), uevent.modalias.c_str(), 0) != 0) continue; // Keep looking
+
+ LOG(DEBUG) << "Loading kernel module '" << module << "' for alias '" << uevent.modalias
+ << "'";
+
+ if (auto result = InsmodWithDeps(module, ""); !result) {
+ LOG(ERROR) << "Cannot load module: " << result.error();
+ // try another one since there may be another match
+ continue;
+ }
+
+ // loading was successful
+ return;
+ }
+}
+
+} // namespace init
+} // namespace android
diff --git a/init/modalias_handler.h b/init/modalias_handler.h
new file mode 100644
index 0000000..3247c86
--- /dev/null
+++ b/init/modalias_handler.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "result.h"
+#include "uevent.h"
+#include "uevent_handler.h"
+
+namespace android {
+namespace init {
+
+class ModaliasHandler : public UeventHandler {
+ public:
+ ModaliasHandler();
+ virtual ~ModaliasHandler() = default;
+
+ void HandleUevent(const Uevent& uevent) override;
+
+ private:
+ Result<Success> InsmodWithDeps(const std::string& module_name, const std::string& args);
+ Result<Success> Insmod(const std::string& path_name, const std::string& args);
+
+ Result<Success> ParseDepCallback(std::vector<std::string>&& args);
+ Result<Success> ParseAliasCallback(std::vector<std::string>&& args);
+
+ std::vector<std::pair<std::string, std::string>> module_aliases_;
+ std::unordered_map<std::string, std::vector<std::string>> module_deps_;
+};
+
+} // namespace init
+} // namespace android
diff --git a/init/parser.cpp b/init/parser.cpp
index 4453aaa..fa0fd11 100644
--- a/init/parser.cpp
+++ b/init/parser.cpp
@@ -19,6 +19,7 @@
#include <dirent.h>
#include <android-base/chrono_utils.h>
+#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
@@ -39,14 +40,13 @@
line_callbacks_.emplace_back(prefix, callback);
}
-void Parser::ParseData(const std::string& filename, const std::string& data, size_t* parse_errors) {
- // TODO: Use a parser with const input and remove this copy
- std::vector<char> data_copy(data.begin(), data.end());
- data_copy.push_back('\0');
+void Parser::ParseData(const std::string& filename, std::string* data) {
+ data->push_back('\n'); // TODO: fix tokenizer
+ data->push_back('\0');
parse_state state;
state.line = 0;
- state.ptr = &data_copy[0];
+ state.ptr = data->data();
state.nexttoken = 0;
SectionParser* section_parser = nullptr;
@@ -57,7 +57,7 @@
if (section_parser == nullptr) return;
if (auto result = section_parser->EndSection(); !result) {
- (*parse_errors)++;
+ parse_error_count_++;
LOG(ERROR) << filename << ": " << section_start_line << ": " << result.error();
}
@@ -69,44 +69,53 @@
switch (next_token(&state)) {
case T_EOF:
end_section();
+
+ for (const auto& [section_name, section_parser] : section_parsers_) {
+ section_parser->EndFile();
+ }
+
return;
- case T_NEWLINE:
+ case T_NEWLINE: {
state.line++;
if (args.empty()) break;
// If we have a line matching a prefix we recognize, call its callback and unset any
// current section parsers. This is meant for /sys/ and /dev/ line entries for
// uevent.
- for (const auto& [prefix, callback] : line_callbacks_) {
- if (android::base::StartsWith(args[0], prefix)) {
- end_section();
+ auto line_callback = std::find_if(
+ line_callbacks_.begin(), line_callbacks_.end(),
+ [&args](const auto& c) { return android::base::StartsWith(args[0], c.first); });
+ if (line_callback != line_callbacks_.end()) {
+ end_section();
- if (auto result = callback(std::move(args)); !result) {
- (*parse_errors)++;
- LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
- }
- break;
+ if (auto result = line_callback->second(std::move(args)); !result) {
+ parse_error_count_++;
+ LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
}
- }
- if (section_parsers_.count(args[0])) {
+ } else if (section_parsers_.count(args[0])) {
end_section();
section_parser = section_parsers_[args[0]].get();
section_start_line = state.line;
if (auto result =
section_parser->ParseSection(std::move(args), filename, state.line);
!result) {
- (*parse_errors)++;
+ parse_error_count_++;
LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
section_parser = nullptr;
}
} else if (section_parser) {
if (auto result = section_parser->ParseLineSection(std::move(args), state.line);
!result) {
- (*parse_errors)++;
+ parse_error_count_++;
LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
}
+ } else {
+ parse_error_count_++;
+ LOG(ERROR) << filename << ": " << state.line
+ << ": Invalid section keyword found";
}
args.clear();
break;
+ }
case T_TEXT:
args.emplace_back(state.text);
break;
@@ -114,30 +123,36 @@
}
}
-bool Parser::ParseConfigFile(const std::string& path, size_t* parse_errors) {
+bool Parser::ParseConfigFileInsecure(const std::string& path) {
+ std::string config_contents;
+ if (!android::base::ReadFileToString(path, &config_contents)) {
+ return false;
+ }
+
+ ParseData(path, &config_contents);
+ return true;
+}
+
+bool Parser::ParseConfigFile(const std::string& path) {
LOG(INFO) << "Parsing file " << path << "...";
android::base::Timer t;
auto config_contents = ReadFile(path);
if (!config_contents) {
- LOG(ERROR) << "Unable to read config file '" << path << "': " << config_contents.error();
+ LOG(INFO) << "Unable to read config file '" << path << "': " << config_contents.error();
return false;
}
- config_contents->push_back('\n'); // TODO: fix parse_config.
- ParseData(path, *config_contents, parse_errors);
- for (const auto& [section_name, section_parser] : section_parsers_) {
- section_parser->EndFile();
- }
+ ParseData(path, &config_contents.value());
LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
return true;
}
-bool Parser::ParseConfigDir(const std::string& path, size_t* parse_errors) {
+bool Parser::ParseConfigDir(const std::string& path) {
LOG(INFO) << "Parsing directory " << path << "...";
std::unique_ptr<DIR, decltype(&closedir)> config_dir(opendir(path.c_str()), closedir);
if (!config_dir) {
- PLOG(ERROR) << "Could not import directory '" << path << "'";
+ PLOG(INFO) << "Could not import directory '" << path << "'";
return false;
}
dirent* current_file;
@@ -153,7 +168,7 @@
// Sort first so we load files in a consistent order (bug 31996208)
std::sort(files.begin(), files.end());
for (const auto& file : files) {
- if (!ParseConfigFile(file, parse_errors)) {
+ if (!ParseConfigFile(file)) {
LOG(ERROR) << "could not import file '" << file << "'";
}
}
@@ -161,16 +176,10 @@
}
bool Parser::ParseConfig(const std::string& path) {
- size_t parse_errors;
- return ParseConfig(path, &parse_errors);
-}
-
-bool Parser::ParseConfig(const std::string& path, size_t* parse_errors) {
- *parse_errors = 0;
if (is_dir(path.c_str())) {
- return ParseConfigDir(path, parse_errors);
+ return ParseConfigDir(path);
}
- return ParseConfigFile(path, parse_errors);
+ return ParseConfigFile(path);
}
} // namespace init
diff --git a/init/parser.h b/init/parser.h
index f6e237f..2454b6a 100644
--- a/init/parser.h
+++ b/init/parser.h
@@ -72,17 +72,22 @@
Parser();
bool ParseConfig(const std::string& path);
- bool ParseConfig(const std::string& path, size_t* parse_errors);
void AddSectionParser(const std::string& name, std::unique_ptr<SectionParser> parser);
void AddSingleLineParser(const std::string& prefix, LineCallback callback);
+ // Host init verifier check file permissions.
+ bool ParseConfigFileInsecure(const std::string& path);
+
+ size_t parse_error_count() const { return parse_error_count_; }
+
private:
- void ParseData(const std::string& filename, const std::string& data, size_t* parse_errors);
- bool ParseConfigFile(const std::string& path, size_t* parse_errors);
- bool ParseConfigDir(const std::string& path, size_t* parse_errors);
+ void ParseData(const std::string& filename, std::string* data);
+ bool ParseConfigFile(const std::string& path);
+ bool ParseConfigDir(const std::string& path);
std::map<std::string, std::unique_ptr<SectionParser>> section_parsers_;
std::vector<std::pair<std::string, LineCallback>> line_callbacks_;
+ size_t parse_error_count_ = 0;
};
} // namespace init
diff --git a/init/property_service.cpp b/init/property_service.cpp
index 95ef35c..cd2f630 100644
--- a/init/property_service.cpp
+++ b/init/property_service.cpp
@@ -16,6 +16,7 @@
#include "property_service.h"
+#include <android/api-level.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
@@ -56,9 +57,11 @@
#include <selinux/label.h>
#include <selinux/selinux.h>
+#include "epoll.h"
#include "init.h"
#include "persistent_properties.h"
#include "property_type.h"
+#include "selinux.h"
#include "subcontext.h"
#include "util.h"
@@ -93,6 +96,11 @@
void CreateSerializedPropertyInfo();
+struct PropertyAuditData {
+ const ucred* cr;
+ const char* name;
+};
+
void property_init() {
mkdir("/dev/__properties__", S_IRWXU | S_IXGRP | S_IXOTH);
CreateSerializedPropertyInfo();
@@ -109,7 +117,7 @@
return false;
}
- property_audit_data audit_data;
+ PropertyAuditData audit_data;
audit_data.name = name.c_str();
audit_data.cr = &cr;
@@ -370,6 +378,7 @@
int result = TEMP_FAILURE_RETRY(recv(socket_, data, bytes_left, MSG_DONTWAIT));
if (result <= 0) {
+ PLOG(ERROR) << "sys_prop: recv error";
return false;
}
@@ -377,6 +386,10 @@
data += result;
}
+ if (bytes_left != 0) {
+ LOG(ERROR) << "sys_prop: recv data is not properly obtained.";
+ }
+
return bytes_left == 0;
}
@@ -386,6 +399,35 @@
DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
};
+bool CheckControlPropertyPerms(const std::string& name, const std::string& value,
+ const std::string& source_context, const ucred& cr) {
+ // We check the legacy method first but these properties are dontaudit, so we only log an audit
+ // if the newer method fails as well. We only do this with the legacy ctl. properties.
+ if (name == "ctl.start" || name == "ctl.stop" || name == "ctl.restart") {
+ // The legacy permissions model is that ctl. properties have their name ctl.<action> and
+ // their value is the name of the service to apply that action to. Permissions for these
+ // actions are based on the service, so we must create a fake name of ctl.<service> to
+ // check permissions.
+ auto control_string_legacy = "ctl." + value;
+ const char* target_context_legacy = nullptr;
+ const char* type_legacy = nullptr;
+ property_info_area->GetPropertyInfo(control_string_legacy.c_str(), &target_context_legacy,
+ &type_legacy);
+
+ if (CheckMacPerms(control_string_legacy, target_context_legacy, source_context.c_str(), cr)) {
+ return true;
+ }
+ }
+
+ auto control_string_full = name + "$" + value;
+ const char* target_context_full = nullptr;
+ const char* type_full = nullptr;
+ property_info_area->GetPropertyInfo(control_string_full.c_str(), &target_context_full,
+ &type_full);
+
+ return CheckMacPerms(control_string_full, target_context_full, source_context.c_str(), cr);
+}
+
// This returns one of the enum of PROP_SUCCESS or PROP_ERROR*.
uint32_t HandlePropertySet(const std::string& name, const std::string& value,
const std::string& source_context, const ucred& cr, std::string* error) {
@@ -395,15 +437,9 @@
}
if (StartsWith(name, "ctl.")) {
- // ctl. properties have their name ctl.<action> and their value is the name of the service
- // to apply that action to. Permissions for these actions are based on the service, so we
- // must create a fake name of ctl.<service> to check permissions.
- auto control_string = "ctl." + value;
- const char* target_context = nullptr;
- const char* type = nullptr;
- property_info_area->GetPropertyInfo(control_string.c_str(), &target_context, &type);
- if (!CheckMacPerms(control_string, target_context, source_context.c_str(), cr)) {
- *error = StringPrintf("Unable to '%s' service %s", name.c_str() + 4, value.c_str());
+ if (!CheckControlPropertyPerms(name, value, source_context, cr)) {
+ *error = StringPrintf("Invalid permissions to perform '%s' on '%s'", name.c_str() + 4,
+ value.c_str());
return PROP_ERROR_HANDLE_CONTROL_MESSAGE;
}
@@ -541,9 +577,11 @@
size_t flen = 0;
const char* context = kInitContext.c_str();
- for (const auto& [path_prefix, secontext] : paths_and_secontexts) {
- if (StartsWith(filename, path_prefix)) {
- context = secontext;
+ if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
+ for (const auto& [path_prefix, secontext] : paths_and_secontexts) {
+ if (StartsWith(filename, path_prefix)) {
+ context = secontext;
+ }
}
}
@@ -654,6 +692,7 @@
}
}
load_properties_from_file("/product/build.prop", NULL);
+ load_properties_from_file("/product-services/build.prop", NULL);
load_properties_from_file("/odm/default.prop", NULL);
load_properties_from_file("/vendor/default.prop", NULL);
@@ -733,7 +772,7 @@
}
static int SelinuxAuditCallback(void* data, security_class_t /*cls*/, char* buf, size_t len) {
- property_audit_data* d = reinterpret_cast<property_audit_data*>(data);
+ auto* d = reinterpret_cast<PropertyAuditData*>(data);
if (!d || !d->name || !d->cr) {
LOG(ERROR) << "AuditCallback invoked with null data arguments!";
@@ -805,7 +844,7 @@
selinux_android_restorecon(kPropertyInfosPath, 0);
}
-void start_property_service() {
+void StartPropertyService(Epoll* epoll) {
selinux_callback cb;
cb.func_audit = SelinuxAuditCallback;
selinux_set_callback(SELINUX_CB_AUDIT, cb);
@@ -820,7 +859,9 @@
listen(property_set_fd, 8);
- register_epoll_handler(property_set_fd, handle_property_set_fd);
+ if (auto result = epoll->RegisterHandler(property_set_fd, handle_property_set_fd); !result) {
+ PLOG(FATAL) << result.error();
+ }
}
} // namespace init
diff --git a/init/property_service.h b/init/property_service.h
index 29eaaa9..cacd987 100644
--- a/init/property_service.h
+++ b/init/property_service.h
@@ -21,14 +21,11 @@
#include <string>
+#include "epoll.h"
+
namespace android {
namespace init {
-struct property_audit_data {
- const ucred* cr;
- const char* name;
-};
-
extern uint32_t (*property_set)(const std::string& name, const std::string& value);
uint32_t HandlePropertySet(const std::string& name, const std::string& value,
@@ -40,7 +37,7 @@
void property_load_boot_defaults(void);
void load_persist_props(void);
void load_system_props(void);
-void start_property_service(void);
+void StartPropertyService(Epoll* epoll);
} // namespace init
} // namespace android
diff --git a/init/reboot.cpp b/init/reboot.cpp
index 03ed55a..2f88121 100644
--- a/init/reboot.cpp
+++ b/init/reboot.cpp
@@ -20,12 +20,9 @@
#include <fcntl.h>
#include <linux/fs.h>
#include <mntent.h>
-#include <semaphore.h>
-#include <sys/capability.h>
#include <sys/cdefs.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
-#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
@@ -52,9 +49,9 @@
#include <selinux/selinux.h>
#include "action_manager.h"
-#include "capabilities.h"
#include "init.h"
#include "property_service.h"
+#include "reboot_utils.h"
#include "service.h"
#include "sigchld_handler.h"
@@ -160,54 +157,6 @@
<< stat;
}
-bool IsRebootCapable() {
- if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) {
- PLOG(WARNING) << "CAP_SYS_BOOT is not supported";
- return true;
- }
-
- ScopedCaps caps(cap_get_proc());
- if (!caps) {
- PLOG(WARNING) << "cap_get_proc() failed";
- return true;
- }
-
- cap_flag_value_t value = CAP_SET;
- if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) {
- PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed";
- return true;
- }
- return value == CAP_SET;
-}
-
-void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
- LOG(INFO) << "Reboot ending, jumping to kernel";
-
- if (!IsRebootCapable()) {
- // On systems where init does not have the capability of rebooting the
- // device, just exit cleanly.
- exit(0);
- }
-
- switch (cmd) {
- case ANDROID_RB_POWEROFF:
- reboot(RB_POWER_OFF);
- break;
-
- case ANDROID_RB_RESTART2:
- syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
- LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
- break;
-
- case ANDROID_RB_THERMOFF:
- reboot(RB_POWER_OFF);
- break;
- }
- // In normal case, reboot should not return.
- PLOG(ERROR) << "reboot call returned";
- abort();
-}
-
/* Find all read+write block devices and emulated devices in /proc/mounts
* and add them to correpsponding list.
*/
@@ -330,9 +279,46 @@
return stat;
}
-void RebootThread(unsigned int cmd, std::chrono::milliseconds shutdown_timeout, bool runFsck,
- sem_t* reboot_semaphore) {
+//* Reboot / shutdown the system.
+// cmd ANDROID_RB_* as defined in android_reboot.h
+// reason Reason string like "reboot", "shutdown,userrequested"
+// rebootTarget Reboot target string like "bootloader". Otherwise, it should be an
+// empty string.
+// runFsck Whether to run fsck after umount is done.
+//
+static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
+ bool runFsck) {
Timer t;
+ LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
+
+ // Ensure last reboot reason is reduced to canonical
+ // alias reported in bootloader or system boot reason.
+ size_t skip = 0;
+ std::vector<std::string> reasons = Split(reason, ",");
+ if (reasons.size() >= 2 && reasons[0] == "reboot" &&
+ (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
+ reasons[1] == "hard" || reasons[1] == "warm")) {
+ skip = strlen("reboot,");
+ }
+ property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip);
+ sync();
+
+ bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
+
+ auto shutdown_timeout = 0ms;
+ if (!SHUTDOWN_ZERO_TIMEOUT) {
+ if (is_thermal_shutdown) {
+ constexpr unsigned int thermal_shutdown_timeout = 1;
+ shutdown_timeout = std::chrono::seconds(thermal_shutdown_timeout);
+ } else {
+ constexpr unsigned int shutdown_timeout_default = 6;
+ auto shutdown_timeout_property = android::base::GetUintProperty(
+ "ro.build.shutdown_timeout", shutdown_timeout_default);
+ shutdown_timeout = std::chrono::seconds(shutdown_timeout_property);
+ }
+ }
+ LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
+
// keep debugging tools until non critical ones are all gone.
const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
// watchdogd is a vendor specific component but should be alive to complete shutdown safely.
@@ -356,7 +342,7 @@
}
// remaining operations (specifically fsck) may take a substantial duration
- if (cmd == ANDROID_RB_POWEROFF || cmd == ANDROID_RB_THERMOFF) {
+ if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) {
TurnOffBacklight();
}
@@ -414,6 +400,7 @@
for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
if (!s->IsShutdownCritical()) s->Stop();
}
+ SubcontextTerminate();
ReapAnyOutstandingChildren();
// 3. send volume shutdown to vold
@@ -429,81 +416,22 @@
if (kill_after_apps.count(s->name())) s->Stop();
}
// 4. sync, try umount, and optionally run fsck for user shutdown
- sync();
+ {
+ Timer sync_timer;
+ LOG(INFO) << "sync() before umount...";
+ sync();
+ LOG(INFO) << "sync() before umount took" << sync_timer;
+ }
UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration());
// Follow what linux shutdown is doing: one more sync with little bit delay
- sync();
- if (cmd != ANDROID_RB_THERMOFF) std::this_thread::sleep_for(100ms);
+ {
+ Timer sync_timer;
+ LOG(INFO) << "sync() after umount...";
+ sync();
+ LOG(INFO) << "sync() after umount took" << sync_timer;
+ }
+ if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
LogShutdownTime(stat, &t);
-
- if (reboot_semaphore != nullptr) {
- sem_post(reboot_semaphore);
- }
-}
-
-void RunRebootThread(unsigned int cmd, std::chrono::milliseconds shutdown_timeout) {
- sem_t reboot_semaphore;
- timespec shutdown_timeout_timespec;
-
- if (sem_init(&reboot_semaphore, false, 0) == -1 ||
- clock_gettime(CLOCK_REALTIME, &shutdown_timeout_timespec) == -1) {
- // These should never fail, but if they do, skip the graceful reboot and reboot immediately.
- return;
- }
-
- std::thread reboot_thread(&RebootThread, cmd, shutdown_timeout, false, &reboot_semaphore);
- reboot_thread.detach();
-
- // One extra second than the timeout passed to the thread as there is a final Umount pass
- // after the timeout is reached.
- shutdown_timeout_timespec.tv_sec += 1 + shutdown_timeout.count() / 1000;
-
- int sem_return = 0;
- while ((sem_return = sem_timedwait(&reboot_semaphore, &shutdown_timeout_timespec)) == -1 &&
- errno == EINTR) {
- }
-
- if (sem_return == -1) {
- LOG(ERROR) << "Reboot thread timed out";
- }
-}
-
-void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
- bool runFsck) {
- LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
-
- // Ensure last reboot reason is reduced to canonical
- // alias reported in bootloader or system boot reason.
- size_t skip = 0;
- std::vector<std::string> reasons = Split(reason, ",");
- if (reasons.size() >= 2 && reasons[0] == "reboot" &&
- (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
- reasons[1] == "hard" || reasons[1] == "warm")) {
- skip = strlen("reboot,");
- }
- property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip);
- sync();
-
- auto shutdown_timeout = 0ms;
- if (!SHUTDOWN_ZERO_TIMEOUT) {
- if (cmd == ANDROID_RB_THERMOFF) {
- constexpr auto kThermalShutdownTimeout = 1s;
- shutdown_timeout = kThermalShutdownTimeout;
- } else {
- constexpr unsigned int kShutdownTimeoutDefault = 6;
- auto shutdown_timeout_property = android::base::GetUintProperty(
- "ro.build.shutdown_timeout", kShutdownTimeoutDefault);
- shutdown_timeout = std::chrono::seconds(shutdown_timeout_property);
- }
- }
- LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
-
- if (runFsck) {
- RebootThread(cmd, shutdown_timeout, true, nullptr);
- } else {
- RunRebootThread(cmd, shutdown_timeout);
- }
-
// Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
RebootSystem(cmd, rebootTarget);
abort();
@@ -545,7 +473,21 @@
"bootloader_message: "
<< err;
}
+ } else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" ||
+ reboot_target == "fastboot") {
+ std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot"
+ : reboot_target;
+ const std::vector<std::string> options = {
+ "--" + arg,
+ };
+ std::string err;
+ if (!write_bootloader_message(options, &err)) {
+ LOG(ERROR) << "Failed to set bootloader message: " << err;
+ return false;
+ }
+ reboot_target = "recovery";
}
+
// If there is an additional parameter, pass it along
if ((cmd_params.size() == 3) && cmd_params[2].size()) {
reboot_target += "," + cmd_params[2];
diff --git a/init/reboot.h b/init/reboot.h
index 1c58bd1..07dcb6e 100644
--- a/init/reboot.h
+++ b/init/reboot.h
@@ -22,26 +22,9 @@
namespace android {
namespace init {
-// This is a wrapper around the actual reboot calls. DoReboot() should be preferred in most cases.
-void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget);
-
-/* Reboot / shutdown the system.
- * cmd ANDROID_RB_* as defined in android_reboot.h
- * reason Reason string like "reboot", "shutdown,userrequested"
- * rebootTarget Reboot target string like "bootloader". Otherwise, it should be an
- * empty string.
- * runFsck Whether to run fsck after umount is done.
- */
-void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
- bool runFsck) __attribute__((__noreturn__));
-
// Parses and handles a setprop sys.powerctl message.
bool HandlePowerctlMessage(const std::string& command);
-// Determines whether the system is capable of rebooting. This is conservative,
-// so if any of the attempts to determine this fail, it will still return true.
-bool IsRebootCapable();
-
} // namespace init
} // namespace android
diff --git a/init/reboot_utils.cpp b/init/reboot_utils.cpp
new file mode 100644
index 0000000..9610304
--- /dev/null
+++ b/init/reboot_utils.cpp
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2018 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 <sys/capability.h>
+#include <sys/reboot.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include <android-base/logging.h>
+#include <cutils/android_reboot.h>
+
+#include "capabilities.h"
+
+namespace android {
+namespace init {
+
+bool IsRebootCapable() {
+ if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) {
+ PLOG(WARNING) << "CAP_SYS_BOOT is not supported";
+ return true;
+ }
+
+ ScopedCaps caps(cap_get_proc());
+ if (!caps) {
+ PLOG(WARNING) << "cap_get_proc() failed";
+ return true;
+ }
+
+ cap_flag_value_t value = CAP_SET;
+ if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) {
+ PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed";
+ return true;
+ }
+ return value == CAP_SET;
+}
+
+void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
+ LOG(INFO) << "Reboot ending, jumping to kernel";
+
+ if (!IsRebootCapable()) {
+ // On systems where init does not have the capability of rebooting the
+ // device, just exit cleanly.
+ exit(0);
+ }
+
+ switch (cmd) {
+ case ANDROID_RB_POWEROFF:
+ reboot(RB_POWER_OFF);
+ break;
+
+ case ANDROID_RB_RESTART2:
+ syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
+ LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
+ break;
+
+ case ANDROID_RB_THERMOFF:
+ reboot(RB_POWER_OFF);
+ break;
+ }
+ // In normal case, reboot should not return.
+ PLOG(ERROR) << "reboot call returned";
+ abort();
+}
+
+void InstallRebootSignalHandlers() {
+ // Instead of panic'ing the kernel as is the default behavior when init crashes,
+ // we prefer to reboot to bootloader on development builds, as this will prevent
+ // boot looping bad configurations and allow both developers and test farms to easily
+ // recover.
+ struct sigaction action;
+ memset(&action, 0, sizeof(action));
+ sigfillset(&action.sa_mask);
+ action.sa_handler = [](int signal) {
+ // These signal handlers are also caught for processes forked from init, however we do not
+ // want them to trigger reboot, so we directly call _exit() for children processes here.
+ if (getpid() != 1) {
+ _exit(signal);
+ }
+
+ // Calling DoReboot() or LOG(FATAL) is not a good option as this is a signal handler.
+ // RebootSystem uses syscall() which isn't actually async-signal-safe, but our only option
+ // and probably good enough given this is already an error case and only enabled for
+ // development builds.
+ RebootSystem(ANDROID_RB_RESTART2, "bootloader");
+ };
+ action.sa_flags = SA_RESTART;
+ sigaction(SIGABRT, &action, nullptr);
+ sigaction(SIGBUS, &action, nullptr);
+ sigaction(SIGFPE, &action, nullptr);
+ sigaction(SIGILL, &action, nullptr);
+ sigaction(SIGSEGV, &action, nullptr);
+#if defined(SIGSTKFLT)
+ sigaction(SIGSTKFLT, &action, nullptr);
+#endif
+ sigaction(SIGSYS, &action, nullptr);
+ sigaction(SIGTRAP, &action, nullptr);
+}
+
+} // namespace init
+} // namespace android
diff --git a/init/log.h b/init/reboot_utils.h
similarity index 63%
copy from init/log.h
copy to init/reboot_utils.h
index 5a4eba6..073a16a 100644
--- a/init/log.h
+++ b/init/reboot_utils.h
@@ -14,19 +14,19 @@
* limitations under the License.
*/
-#ifndef _INIT_LOG_H_
-#define _INIT_LOG_H_
+#pragma once
-#include <sys/cdefs.h>
+#include <string>
namespace android {
namespace init {
-void InitKernelLogging(char* argv[]);
-
-int selinux_klog_callback(int level, const char* fmt, ...) __printflike(2, 3);
+// Determines whether the system is capable of rebooting. This is conservative,
+// so if any of the attempts to determine this fail, it will still return true.
+bool IsRebootCapable();
+// This is a wrapper around the actual reboot calls.
+void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& reboot_target);
+void InstallRebootSignalHandlers();
} // namespace init
} // namespace android
-
-#endif
diff --git a/init/selinux.cpp b/init/selinux.cpp
index 6aba9c1..fd7e86f 100644
--- a/init/selinux.cpp
+++ b/init/selinux.cpp
@@ -47,6 +47,7 @@
#include "selinux.h"
+#include <android/api-level.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
@@ -55,12 +56,13 @@
#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/logging.h>
+#include <android-base/parseint.h>
#include <android-base/unique_fd.h>
#include <selinux/android.h>
-#include "log.h"
#include "util.h"
+using android::base::ParseInt;
using android::base::Timer;
using android::base::unique_fd;
@@ -413,6 +415,8 @@
if constexpr (WORLD_WRITABLE_KMSG) {
selinux_android_restorecon("/dev/kmsg_debug", 0);
}
+ selinux_android_restorecon("/dev/null", 0);
+ selinux_android_restorecon("/dev/ptmx", 0);
selinux_android_restorecon("/dev/socket", 0);
selinux_android_restorecon("/dev/random", 0);
selinux_android_restorecon("/dev/urandom", 0);
@@ -446,13 +450,52 @@
selinux_android_restorecon("/sbin/sload.f2fs", 0);
}
+int SelinuxKlogCallback(int type, const char* fmt, ...) {
+ android::base::LogSeverity severity = android::base::ERROR;
+ if (type == SELINUX_WARNING) {
+ severity = android::base::WARNING;
+ } else if (type == SELINUX_INFO) {
+ severity = android::base::INFO;
+ }
+ char buf[1024];
+ va_list ap;
+ va_start(ap, fmt);
+ vsnprintf(buf, sizeof(buf), fmt, ap);
+ va_end(ap);
+ android::base::KernelLogger(android::base::MAIN, severity, "selinux", nullptr, 0, buf);
+ return 0;
+}
+
// This function sets up SELinux logging to be written to kmsg, to match init's logging.
void SelinuxSetupKernelLogging() {
selinux_callback cb;
- cb.func_log = selinux_klog_callback;
+ cb.func_log = SelinuxKlogCallback;
selinux_set_callback(SELINUX_CB_LOG, cb);
}
+// This function returns the Android version with which the vendor SEPolicy was compiled.
+// It is used for version checks such as whether or not vendor_init should be used
+int SelinuxGetVendorAndroidVersion() {
+ if (!IsSplitPolicyDevice()) {
+ // If this device does not split sepolicy files, it's not a Treble device and therefore,
+ // we assume it's always on the latest platform.
+ return __ANDROID_API_FUTURE__;
+ }
+
+ std::string version;
+ if (!GetVendorMappingVersion(&version)) {
+ LOG(FATAL) << "Could not read vendor SELinux version";
+ }
+
+ int major_version;
+ std::string major_version_str(version, 0, version.find('.'));
+ if (!ParseInt(major_version_str, &major_version)) {
+ PLOG(FATAL) << "Failed to parse the vendor sepolicy major version " << major_version_str;
+ }
+
+ return major_version;
+}
+
// selinux_android_file_context_handle() takes on the order of 10+ms to run, so we want to cache
// its value. selinux_android_restorecon() also needs an sehandle for file context look up. It
// will create and store its own copy, but selinux_android_set_sehandle() can be used to provide
diff --git a/init/selinux.h b/init/selinux.h
index 7b880ec..c41d7f0 100644
--- a/init/selinux.h
+++ b/init/selinux.h
@@ -27,6 +27,7 @@
void SelinuxRestoreContext();
void SelinuxSetupKernelLogging();
+int SelinuxGetVendorAndroidVersion();
void SelabelInitialize();
bool SelabelLookupFileContext(const std::string& key, int type, std::string* result);
diff --git a/init/service.cpp b/init/service.cpp
index 8130e73..d20e90a 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -18,6 +18,7 @@
#include <fcntl.h>
#include <inttypes.h>
+#include <linux/input.h>
#include <linux/securebits.h>
#include <sched.h>
#include <sys/mount.h>
@@ -32,8 +33,10 @@
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
+#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
#include <hidl-util/FQName.h>
#include <processgroup/processgroup.h>
#include <selinux/selinux.h>
@@ -43,12 +46,12 @@
#include "util.h"
#if defined(__ANDROID__)
+#include <android/api-level.h>
#include <sys/system_properties.h>
-#include <android-base/properties.h>
-
#include "init.h"
#include "property_service.h"
+#include "selinux.h"
#else
#include "host_init_stubs.h"
#endif
@@ -59,13 +62,13 @@
using android::base::ParseInt;
using android::base::StartsWith;
using android::base::StringPrintf;
+using android::base::unique_fd;
using android::base::WriteStringToFile;
namespace android {
namespace init {
-static Result<std::string> ComputeContextFromExecutable(std::string& service_name,
- const std::string& service_path) {
+static Result<std::string> ComputeContextFromExecutable(const std::string& service_path) {
std::string computed_context;
char* raw_con = nullptr;
@@ -101,36 +104,49 @@
return computed_context;
}
-static void SetUpPidNamespace(const std::string& service_name) {
+Result<Success> Service::SetUpMountNamespace() const {
constexpr unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
- // It's OK to LOG(FATAL) in this function since it's running in the first
- // child process.
-
// Recursively remount / as slave like zygote does so unmounting and mounting /proc
// doesn't interfere with the parent namespace's /proc mount. This will also
// prevent any other mounts/unmounts initiated by the service from interfering
// with the parent namespace but will still allow mount events from the parent
// namespace to propagate to the child.
if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) {
- PLOG(FATAL) << "couldn't remount(/) recursively as slave for " << service_name;
- }
- // umount() then mount() /proc.
- // Note that it is not sufficient to mount with MS_REMOUNT.
- if (umount("/proc") == -1) {
- PLOG(FATAL) << "couldn't umount(/proc) for " << service_name;
- }
- if (mount("", "/proc", "proc", kSafeFlags, "") == -1) {
- PLOG(FATAL) << "couldn't mount(/proc) for " << service_name;
+ return ErrnoError() << "Could not remount(/) recursively as slave";
}
- if (prctl(PR_SET_NAME, service_name.c_str()) == -1) {
- PLOG(FATAL) << "couldn't set name for " << service_name;
+ // umount() then mount() /proc and/or /sys
+ // Note that it is not sufficient to mount with MS_REMOUNT.
+ if (namespace_flags_ & CLONE_NEWPID) {
+ if (umount("/proc") == -1) {
+ return ErrnoError() << "Could not umount(/proc)";
+ }
+ if (mount("", "/proc", "proc", kSafeFlags, "") == -1) {
+ return ErrnoError() << "Could not mount(/proc)";
+ }
+ }
+ bool remount_sys = std::any_of(namespaces_to_enter_.begin(), namespaces_to_enter_.end(),
+ [](const auto& entry) { return entry.first == CLONE_NEWNET; });
+ if (remount_sys) {
+ if (umount2("/sys", MNT_DETACH) == -1) {
+ return ErrnoError() << "Could not umount(/sys)";
+ }
+ if (mount("", "/sys", "sysfs", kSafeFlags, "") == -1) {
+ return ErrnoError() << "Could not mount(/sys)";
+ }
+ }
+ return Success();
+}
+
+Result<Success> Service::SetUpPidNamespace() const {
+ if (prctl(PR_SET_NAME, name_.c_str()) == -1) {
+ return ErrnoError() << "Could not set name";
}
pid_t child_pid = fork();
if (child_pid == -1) {
- PLOG(FATAL) << "couldn't fork init inside the PID namespace for " << service_name;
+ return ErrnoError() << "Could not fork init inside the PID namespace";
}
if (child_pid > 0) {
@@ -153,9 +169,23 @@
}
_exit(WEXITSTATUS(init_exitstatus));
}
+ return Success();
}
-static bool ExpandArgsAndExecv(const std::vector<std::string>& args) {
+Result<Success> Service::EnterNamespaces() const {
+ for (const auto& [nstype, path] : namespaces_to_enter_) {
+ auto fd = unique_fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)};
+ if (!fd) {
+ return ErrnoError() << "Could not open namespace at " << path;
+ }
+ if (setns(fd, nstype) == -1) {
+ return ErrnoError() << "Could not setns() namespace at " << path;
+ }
+ }
+ return Success();
+}
+
+static bool ExpandArgsAndExecv(const std::vector<std::string>& args, bool sigstop) {
std::vector<std::string> expanded_args;
std::vector<char*> c_strings;
@@ -169,6 +199,10 @@
}
c_strings.push_back(nullptr);
+ if (sigstop) {
+ kill(getpid(), SIGSTOP);
+ }
+
return execv(c_strings[0], c_strings.data()) == 0;
}
@@ -196,7 +230,6 @@
seclabel_(seclabel),
onrestart_(false, subcontext_for_restart_commands, "<Service '" + name + "' onrestart>", 0,
"onrestart", {}),
- keychord_id_(0),
ioprio_class_(IoSchedClass_NONE),
ioprio_pri_(0),
priority_(0),
@@ -303,7 +336,7 @@
}
}
-void Service::Reap() {
+void Service::Reap(const siginfo_t& siginfo) {
if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) {
KillProcessGroup(SIGKILL);
}
@@ -312,6 +345,10 @@
std::for_each(descriptors_.begin(), descriptors_.end(),
std::bind(&DescriptorInfo::Clean, std::placeholders::_1));
+ for (const auto& f : reap_callbacks_) {
+ f(siginfo);
+ }
+
if (flags_ & SVC_EXEC) UnSetExec();
if (flags_ & SVC_TEMPORARY) return;
@@ -414,6 +451,20 @@
return Success();
}
+Result<Success> Service::ParseEnterNamespace(const std::vector<std::string>& args) {
+ if (args[1] != "net") {
+ return Error() << "Init only supports entering network namespaces";
+ }
+ if (!namespaces_to_enter_.empty()) {
+ return Error() << "Only one network namespace may be entered";
+ }
+ // Network namespaces require that /sys is remounted, otherwise the old adapters will still be
+ // present. Therefore, they also require mount namespaces.
+ namespace_flags_ |= CLONE_NEWNS;
+ namespaces_to_enter_.emplace_back(CLONE_NEWNET, args[2]);
+ return Success();
+}
+
Result<Success> Service::ParseGroup(const std::vector<std::string>& args) {
auto gid = DecodeUid(args[1]);
if (!gid) {
@@ -446,8 +497,8 @@
const std::string& interface_name = args[1];
const std::string& instance_name = args[2];
- const FQName fq_name = FQName(interface_name);
- if (!fq_name.isValid()) {
+ FQName fq_name;
+ if (!FQName::parse(interface_name, &fq_name)) {
return Error() << "Invalid fully-qualified name for interface '" << interface_name << "'";
}
@@ -494,10 +545,13 @@
Result<Success> Service::ParseKeycodes(const std::vector<std::string>& args) {
for (std::size_t i = 1; i < args.size(); i++) {
int code;
- if (ParseInt(args[i], &code)) {
- keycodes_.emplace_back(code);
+ if (ParseInt(args[i], &code, 0, KEY_MAX)) {
+ for (auto& key : keycodes_) {
+ if (key == code) return Error() << "duplicate keycode: " << args[i];
+ }
+ keycodes_.insert(std::upper_bound(keycodes_.begin(), keycodes_.end(), code), code);
} else {
- LOG(WARNING) << "ignoring invalid keycode: " << args[i];
+ return Error() << "invalid keycode: " << args[i];
}
}
return Success();
@@ -578,6 +632,11 @@
return Success();
}
+Result<Success> Service::ParseSigstop(const std::vector<std::string>& args) {
+ sigstop_ = true;
+ return Success();
+}
+
Result<Success> Service::ParseSetenv(const std::vector<std::string>& args) {
environment_vars_.emplace_back(args[1], args[2]);
return Success();
@@ -678,29 +737,32 @@
{"console", {0, 1, &Service::ParseConsole}},
{"critical", {0, 0, &Service::ParseCritical}},
{"disabled", {0, 0, &Service::ParseDisabled}},
+ {"enter_namespace",
+ {2, 2, &Service::ParseEnterNamespace}},
+ {"file", {2, 2, &Service::ParseFile}},
{"group", {1, NR_SVC_SUPP_GIDS + 1, &Service::ParseGroup}},
{"interface", {2, 2, &Service::ParseInterface}},
{"ioprio", {2, 2, &Service::ParseIoprio}},
- {"priority", {1, 1, &Service::ParsePriority}},
{"keycodes", {1, kMax, &Service::ParseKeycodes}},
- {"oneshot", {0, 0, &Service::ParseOneshot}},
- {"onrestart", {1, kMax, &Service::ParseOnrestart}},
- {"override", {0, 0, &Service::ParseOverride}},
- {"oom_score_adjust",
- {1, 1, &Service::ParseOomScoreAdjust}},
- {"memcg.swappiness",
- {1, 1, &Service::ParseMemcgSwappiness}},
- {"memcg.soft_limit_in_bytes",
- {1, 1, &Service::ParseMemcgSoftLimitInBytes}},
{"memcg.limit_in_bytes",
{1, 1, &Service::ParseMemcgLimitInBytes}},
+ {"memcg.soft_limit_in_bytes",
+ {1, 1, &Service::ParseMemcgSoftLimitInBytes}},
+ {"memcg.swappiness",
+ {1, 1, &Service::ParseMemcgSwappiness}},
{"namespace", {1, 2, &Service::ParseNamespace}},
+ {"oneshot", {0, 0, &Service::ParseOneshot}},
+ {"onrestart", {1, kMax, &Service::ParseOnrestart}},
+ {"oom_score_adjust",
+ {1, 1, &Service::ParseOomScoreAdjust}},
+ {"override", {0, 0, &Service::ParseOverride}},
+ {"priority", {1, 1, &Service::ParsePriority}},
{"rlimit", {3, 3, &Service::ParseProcessRlimit}},
{"seclabel", {1, 1, &Service::ParseSeclabel}},
{"setenv", {2, 2, &Service::ParseSetenv}},
{"shutdown", {1, 1, &Service::ParseShutdown}},
+ {"sigstop", {0, 0, &Service::ParseSigstop}},
{"socket", {3, 6, &Service::ParseSocket}},
- {"file", {2, 2, &Service::ParseFile}},
{"user", {1, 1, &Service::ParseUser}},
{"writepid", {1, kMax, &Service::ParseWritepid}},
};
@@ -727,9 +789,9 @@
flags_ |= SVC_EXEC;
is_exec_service_running_ = true;
- LOG(INFO) << "SVC_EXEC pid " << pid_ << " (uid " << uid_ << " gid " << gid_ << "+"
- << supp_gids_.size() << " context " << (!seclabel_.empty() ? seclabel_ : "default")
- << ") started; waiting...";
+ LOG(INFO) << "SVC_EXEC service '" << name_ << "' pid " << pid_ << " (uid " << uid_ << " gid "
+ << gid_ << "+" << supp_gids_.size() << " context "
+ << (!seclabel_.empty() ? seclabel_ : "default") << ") started; waiting...";
return Success();
}
@@ -779,7 +841,7 @@
if (!seclabel_.empty()) {
scon = seclabel_;
} else {
- auto result = ComputeContextFromExecutable(name_, args_[0]);
+ auto result = ComputeContextFromExecutable(args_[0]);
if (!result) {
return result.error();
}
@@ -798,10 +860,24 @@
if (pid == 0) {
umask(077);
+ if (auto result = EnterNamespaces(); !result) {
+ LOG(FATAL) << "Service '" << name_ << "' could not enter namespaces: " << result.error();
+ }
+
+ if (namespace_flags_ & CLONE_NEWNS) {
+ if (auto result = SetUpMountNamespace(); !result) {
+ LOG(FATAL) << "Service '" << name_
+ << "' could not set up mount namespace: " << result.error();
+ }
+ }
+
if (namespace_flags_ & CLONE_NEWPID) {
// This will fork again to run an init process inside the PID
// namespace.
- SetUpPidNamespace(name_);
+ if (auto result = SetUpPidNamespace(); !result) {
+ LOG(FATAL) << "Service '" << name_
+ << "' could not set up PID namespace: " << result.error();
+ }
}
for (const auto& [key, value] : environment_vars_) {
@@ -858,7 +934,7 @@
// priority. Aborts on failure.
SetProcessAttributes();
- if (!ExpandArgsAndExecv(args_)) {
+ if (!ExpandArgsAndExecv(args_, sigstop_)) {
PLOG(ERROR) << "cannot execve('" << args_[0] << "')";
}
@@ -1137,6 +1213,13 @@
}
std::vector<std::string> str_args(args.begin() + 2, args.end());
+
+ if (SelinuxGetVendorAndroidVersion() <= __ANDROID_API_P__) {
+ if (str_args[0] == "/sbin/watchdogd") {
+ str_args[0] = "/system/bin/watchdogd";
+ }
+ }
+
service_ = std::make_unique<Service>(name, restart_action_subcontext, str_args);
return Success();
}
diff --git a/init/service.h b/init/service.h
index d46a413..ea79a07 100644
--- a/init/service.h
+++ b/init/service.h
@@ -17,6 +17,7 @@
#ifndef _INIT_SERVICE_H
#define _INIT_SERVICE_H
+#include <signal.h>
#include <sys/resource.h>
#include <sys/types.h>
@@ -81,7 +82,7 @@
void Stop();
void Terminate();
void Restart();
- void Reap();
+ void Reap(const siginfo_t& siginfo);
void DumpState() const;
void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; }
bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; }
@@ -89,6 +90,9 @@
is_exec_service_running_ = false;
flags_ &= ~SVC_EXEC;
}
+ void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) {
+ reap_callbacks_.emplace_back(std::move(callback));
+ }
static bool is_exec_service_running() { return is_exec_service_running_; }
@@ -104,8 +108,6 @@
const std::vector<gid_t>& supp_gids() const { return supp_gids_; }
const std::string& seclabel() const { return seclabel_; }
const std::vector<int>& keycodes() const { return keycodes_; }
- int keychord_id() const { return keychord_id_; }
- void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; }
IoSchedClass ioprio_class() const { return ioprio_class_; }
int ioprio_pri() const { return ioprio_pri_; }
const std::set<std::string>& interfaces() const { return interfaces_; }
@@ -114,12 +116,16 @@
bool is_override() const { return override_; }
bool process_cgroup_empty() const { return process_cgroup_empty_; }
unsigned long start_order() const { return start_order_; }
+ void set_sigstop(bool value) { sigstop_ = value; }
const std::vector<std::string>& args() const { return args_; }
private:
using OptionParser = Result<Success> (Service::*)(const std::vector<std::string>& args);
class OptionParserMap;
+ Result<Success> SetUpMountNamespace() const;
+ Result<Success> SetUpPidNamespace() const;
+ Result<Success> EnterNamespaces() const;
void NotifyStateChange(const std::string& new_state) const;
void StopOrReset(int how);
void ZapStdio() const;
@@ -132,6 +138,7 @@
Result<Success> ParseConsole(const std::vector<std::string>& args);
Result<Success> ParseCritical(const std::vector<std::string>& args);
Result<Success> ParseDisabled(const std::vector<std::string>& args);
+ Result<Success> ParseEnterNamespace(const std::vector<std::string>& args);
Result<Success> ParseGroup(const std::vector<std::string>& args);
Result<Success> ParsePriority(const std::vector<std::string>& args);
Result<Success> ParseInterface(const std::vector<std::string>& args);
@@ -149,6 +156,7 @@
Result<Success> ParseSeclabel(const std::vector<std::string>& args);
Result<Success> ParseSetenv(const std::vector<std::string>& args);
Result<Success> ParseShutdown(const std::vector<std::string>& args);
+ Result<Success> ParseSigstop(const std::vector<std::string>& args);
Result<Success> ParseSocket(const std::vector<std::string>& args);
Result<Success> ParseFile(const std::vector<std::string>& args);
Result<Success> ParseUser(const std::vector<std::string>& args);
@@ -175,6 +183,8 @@
std::vector<gid_t> supp_gids_;
CapSet capabilities_;
unsigned namespace_flags_;
+ // Pair of namespace type, path to namespace.
+ std::vector<std::pair<int, std::string>> namespaces_to_enter_;
std::string seclabel_;
@@ -187,9 +197,8 @@
std::set<std::string> interfaces_; // e.g. some.package.foo@1.0::IBaz/instance-name
- // keycodes for triggering this service via /dev/keychord
+ // keycodes for triggering this service via /dev/input/input*
std::vector<int> keycodes_;
- int keychord_id_;
IoSchedClass ioprio_class_;
int ioprio_pri_;
@@ -209,7 +218,11 @@
std::vector<std::pair<int, rlimit>> rlimits_;
+ bool sigstop_ = false;
+
std::vector<std::string> args_;
+
+ std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_;
};
class ServiceList {
@@ -234,6 +247,16 @@
return nullptr;
}
+ Service* FindInterface(const std::string& interface_name) {
+ for (const auto& svc : services_) {
+ if (svc->interfaces().count(interface_name) > 0) {
+ return svc.get();
+ }
+ }
+
+ return nullptr;
+ }
+
void DumpState() const;
auto begin() const { return services_.begin(); }
diff --git a/init/service_test.cpp b/init/service_test.cpp
index b43c2e9..194aa2b 100644
--- a/init/service_test.cpp
+++ b/init/service_test.cpp
@@ -46,7 +46,6 @@
EXPECT_EQ(0U, service_in_old_memory->uid());
EXPECT_EQ(0U, service_in_old_memory->gid());
EXPECT_EQ(0U, service_in_old_memory->namespace_flags());
- EXPECT_EQ(0, service_in_old_memory->keychord_id());
EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory->ioprio_class());
EXPECT_EQ(0, service_in_old_memory->ioprio_pri());
EXPECT_EQ(0, service_in_old_memory->priority());
@@ -66,7 +65,6 @@
EXPECT_EQ(0U, service_in_old_memory2->uid());
EXPECT_EQ(0U, service_in_old_memory2->gid());
EXPECT_EQ(0U, service_in_old_memory2->namespace_flags());
- EXPECT_EQ(0, service_in_old_memory2->keychord_id());
EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory2->ioprio_class());
EXPECT_EQ(0, service_in_old_memory2->ioprio_pri());
EXPECT_EQ(0, service_in_old_memory2->priority());
diff --git a/init/sigchld_handler.cpp b/init/sigchld_handler.cpp
index 072a0fb..0b03324 100644
--- a/init/sigchld_handler.cpp
+++ b/init/sigchld_handler.cpp
@@ -39,9 +39,6 @@
namespace android {
namespace init {
-static int signal_write_fd = -1;
-static int signal_read_fd = -1;
-
static bool ReapOneProcess() {
siginfo_t siginfo = {};
// This returns a zombie pid or informs us that there are no zombies left to be reaped.
@@ -84,16 +81,15 @@
}
}
- auto status = siginfo.si_status;
- if (WIFEXITED(status)) {
- LOG(INFO) << name << " exited with status " << WEXITSTATUS(status) << wait_string;
- } else if (WIFSIGNALED(status)) {
- LOG(INFO) << name << " killed by signal " << WTERMSIG(status) << wait_string;
+ if (siginfo.si_code == CLD_EXITED) {
+ LOG(INFO) << name << " exited with status " << siginfo.si_status << wait_string;
+ } else {
+ LOG(INFO) << name << " received signal " << siginfo.si_status << wait_string;
}
if (!service) return true;
- service->Reap();
+ service->Reap(siginfo);
if (service->flags() & SVC_TEMPORARY) {
ServiceList::GetInstance().RemoveService(*service);
@@ -102,46 +98,10 @@
return true;
}
-static void handle_signal() {
- // Clear outstanding requests.
- char buf[32];
- read(signal_read_fd, buf, sizeof(buf));
-
- ReapAnyOutstandingChildren();
-}
-
-static void SIGCHLD_handler(int) {
- if (TEMP_FAILURE_RETRY(write(signal_write_fd, "1", 1)) == -1) {
- PLOG(ERROR) << "write(signal_write_fd) failed";
- }
-}
-
void ReapAnyOutstandingChildren() {
while (ReapOneProcess()) {
}
}
-void sigchld_handler_init() {
- // Create a signalling mechanism for SIGCHLD.
- int s[2];
- if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == -1) {
- PLOG(FATAL) << "socketpair failed in sigchld_handler_init";
- }
-
- signal_write_fd = s[0];
- signal_read_fd = s[1];
-
- // Write to signal_write_fd if we catch SIGCHLD.
- struct sigaction act;
- memset(&act, 0, sizeof(act));
- act.sa_handler = SIGCHLD_handler;
- act.sa_flags = SA_NOCLDSTOP;
- sigaction(SIGCHLD, &act, 0);
-
- ReapAnyOutstandingChildren();
-
- register_epoll_handler(signal_read_fd, handle_signal);
-}
-
} // namespace init
} // namespace android
diff --git a/init/sigchld_handler.h b/init/sigchld_handler.h
index c86dc8d..30063f2 100644
--- a/init/sigchld_handler.h
+++ b/init/sigchld_handler.h
@@ -22,8 +22,6 @@
void ReapAnyOutstandingChildren();
-void sigchld_handler_init(void);
-
} // namespace init
} // namespace android
diff --git a/init/stable_properties.h b/init/stable_properties.h
index be35457..4972d10 100644
--- a/init/stable_properties.h
+++ b/init/stable_properties.h
@@ -29,14 +29,23 @@
};
static const std::set<std::string> kExportedActionableProperties = {
+ "dev.bootcomplete",
+ "init.svc.console",
"init.svc.mediadrm",
+ "init.svc.surfaceflinger",
"init.svc.zygote",
"persist.bluetooth.btsnoopenable",
"persist.sys.crash_rcu",
+ "persist.sys.usb.usbradio.config",
"persist.sys.zram_enabled",
+ "ro.board.platform",
"ro.bootmode",
"ro.build.type",
+ "ro.crypto.state",
+ "ro.crypto.type",
+ "ro.debuggable",
"sys.boot_completed",
+ "sys.boot_from_charger_mode",
"sys.retaildemo.enabled",
"sys.shutdown.requested",
"sys.usb.config",
@@ -45,7 +54,10 @@
"sys.usb.ffs.ready",
"sys.user.0.ce_available",
"sys.vdso",
+ "vold.decrypt",
+ "vold.post_fs_data_done",
"vts.native_server.on",
+ "wlan.driver.status",
};
} // namespace init
diff --git a/init/subcontext.cpp b/init/subcontext.cpp
index c1846f7..c2a21d4 100644
--- a/init/subcontext.cpp
+++ b/init/subcontext.cpp
@@ -30,6 +30,7 @@
#include "util.h"
#if defined(__ANDROID__)
+#include <android/api-level.h>
#include "property_service.h"
#include "selinux.h"
#else
@@ -352,10 +353,13 @@
}
static std::vector<Subcontext> subcontexts;
+static bool shutting_down;
std::vector<Subcontext>* InitializeSubcontexts() {
- for (const auto& [path_prefix, secontext] : paths_and_secontexts) {
- subcontexts.emplace_back(path_prefix, secontext);
+ if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
+ for (const auto& [path_prefix, secontext] : paths_and_secontexts) {
+ subcontexts.emplace_back(path_prefix, secontext);
+ }
}
return &subcontexts;
}
@@ -363,12 +367,21 @@
bool SubcontextChildReap(pid_t pid) {
for (auto& subcontext : subcontexts) {
if (subcontext.pid() == pid) {
- subcontext.Restart();
+ if (!shutting_down) {
+ subcontext.Restart();
+ }
return true;
}
}
return false;
}
+void SubcontextTerminate() {
+ shutting_down = true;
+ for (auto& subcontext : subcontexts) {
+ kill(subcontext.pid(), SIGTERM);
+ }
+}
+
} // namespace init
} // namespace android
diff --git a/init/subcontext.h b/init/subcontext.h
index 22d7d43..628fd50 100644
--- a/init/subcontext.h
+++ b/init/subcontext.h
@@ -63,6 +63,7 @@
int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map);
std::vector<Subcontext>* InitializeSubcontexts();
bool SubcontextChildReap(pid_t pid);
+void SubcontextTerminate();
} // namespace init
} // namespace android
diff --git a/init/tokenizer.cpp b/init/tokenizer.cpp
index f8d9b6b..bb143f1 100644
--- a/init/tokenizer.cpp
+++ b/init/tokenizer.cpp
@@ -85,15 +85,19 @@
goto textdone;
case 'n':
*s++ = '\n';
+ x++;
break;
case 'r':
*s++ = '\r';
+ x++;
break;
case 't':
*s++ = '\t';
+ x++;
break;
case '\\':
*s++ = '\\';
+ x++;
break;
case '\r':
/* \ <cr> <lf> -> line continuation */
@@ -101,6 +105,7 @@
x++;
continue;
}
+ x++;
case '\n':
/* \ <lf> -> line continuation */
state->line++;
diff --git a/init/tokenizer_test.cpp b/init/tokenizer_test.cpp
new file mode 100644
index 0000000..acfc7c7
--- /dev/null
+++ b/init/tokenizer_test.cpp
@@ -0,0 +1,163 @@
+//
+// Copyright (C) 2018 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 "tokenizer.h"
+
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+namespace android {
+namespace init {
+
+namespace {
+
+void RunTest(const std::string& data, const std::vector<std::vector<std::string>>& expected_tokens) {
+ auto data_copy = std::string{data};
+ data_copy.push_back('\n'); // TODO: fix tokenizer
+ data_copy.push_back('\0');
+
+ parse_state state;
+ state.line = 0;
+ state.ptr = data_copy.data();
+ state.nexttoken = 0;
+
+ std::vector<std::string> current_line;
+ std::vector<std::vector<std::string>> tokens;
+
+ while (true) {
+ switch (next_token(&state)) {
+ case T_EOF:
+ EXPECT_EQ(expected_tokens, tokens) << data;
+ return;
+ case T_NEWLINE:
+ tokens.emplace_back(std::move(current_line));
+ break;
+ case T_TEXT:
+ current_line.emplace_back(state.text);
+ break;
+ }
+ }
+}
+
+} // namespace
+
+TEST(tokenizer, null) {
+ RunTest("", {{}});
+}
+
+TEST(tokenizer, simple_oneline) {
+ RunTest("one two\tthree\rfour", {{"one", "two", "three", "four"}});
+}
+
+TEST(tokenizer, simple_multiline) {
+ RunTest("1 2 3\n4 5 6\n7 8 9", {{"1", "2", "3"}, {"4", "5", "6"}, {"7", "8", "9"}});
+}
+
+TEST(tokenizer, preceding_space) {
+ // Preceding spaces are ignored.
+ RunTest(" 1 2 3\n\t\t\t\t4 5 6\n\r\r\r\r7 8 9",
+ {{"1", "2", "3"}, {"4", "5", "6"}, {"7", "8", "9"}});
+}
+
+TEST(tokenizer, comments) {
+ // Entirely commented lines still produce a T_NEWLINE token for tracking line count.
+ RunTest("1 2 3\n#4 5 6\n7 8 9", {{"1", "2", "3"}, {}, {"7", "8", "9"}});
+
+ RunTest("#1 2 3\n4 5 6\n7 8 9", {{}, {"4", "5", "6"}, {"7", "8", "9"}});
+
+ RunTest("1 2 3\n4 5 6\n#7 8 9", {{"1", "2", "3"}, {"4", "5", "6"}, {}});
+
+ RunTest("1 2 #3\n4 #5 6\n#7 8 9", {{"1", "2"}, {"4"}, {}});
+}
+
+TEST(tokenizer, control_chars) {
+ // Literal \n, \r, \t, and \\ produce the control characters \n, \r, \t, and \\ respectively.
+ // Literal \? produces ? for all other character '?'
+
+ RunTest(R"(1 token\ntoken 2)", {{"1", "token\ntoken", "2"}});
+ RunTest(R"(1 token\rtoken 2)", {{"1", "token\rtoken", "2"}});
+ RunTest(R"(1 token\ttoken 2)", {{"1", "token\ttoken", "2"}});
+ RunTest(R"(1 token\\token 2)", {{"1", "token\\token", "2"}});
+ RunTest(R"(1 token\btoken 2)", {{"1", "tokenbtoken", "2"}});
+
+ RunTest(R"(1 token\n 2)", {{"1", "token\n", "2"}});
+ RunTest(R"(1 token\r 2)", {{"1", "token\r", "2"}});
+ RunTest(R"(1 token\t 2)", {{"1", "token\t", "2"}});
+ RunTest(R"(1 token\\ 2)", {{"1", "token\\", "2"}});
+ RunTest(R"(1 token\b 2)", {{"1", "tokenb", "2"}});
+
+ RunTest(R"(1 \ntoken 2)", {{"1", "\ntoken", "2"}});
+ RunTest(R"(1 \rtoken 2)", {{"1", "\rtoken", "2"}});
+ RunTest(R"(1 \ttoken 2)", {{"1", "\ttoken", "2"}});
+ RunTest(R"(1 \\token 2)", {{"1", "\\token", "2"}});
+ RunTest(R"(1 \btoken 2)", {{"1", "btoken", "2"}});
+
+ RunTest(R"(1 \n 2)", {{"1", "\n", "2"}});
+ RunTest(R"(1 \r 2)", {{"1", "\r", "2"}});
+ RunTest(R"(1 \t 2)", {{"1", "\t", "2"}});
+ RunTest(R"(1 \\ 2)", {{"1", "\\", "2"}});
+ RunTest(R"(1 \b 2)", {{"1", "b", "2"}});
+}
+
+TEST(tokenizer, cr_lf) {
+ // \ before \n, \r, or \r\n is interpreted as a line continuation
+ // Extra whitespace on the next line is eaten, except \r unlike in the above tests.
+
+ RunTest("lf\\\ncont", {{"lfcont"}});
+ RunTest("lf\\\n \t\t\t\tcont", {{"lfcont"}});
+
+ RunTest("crlf\\\r\ncont", {{"crlfcont"}});
+ RunTest("crlf\\\r\n \t\t\t\tcont", {{"crlfcont"}});
+
+ RunTest("cr\\\rcont", {{"crcont"}});
+
+ RunTest("lfspace \\\ncont", {{"lfspace", "cont"}});
+ RunTest("lfspace \\\n \t\t\t\tcont", {{"lfspace", "cont"}});
+
+ RunTest("crlfspace \\\r\ncont", {{"crlfspace", "cont"}});
+ RunTest("crlfspace \\\r\n \t\t\t\tcont", {{"crlfspace", "cont"}});
+
+ RunTest("crspace \\\rcont", {{"crspace", "cont"}});
+}
+
+TEST(tokenizer, quoted) {
+ RunTest("\"quoted simple string\"", {{"quoted simple string"}});
+
+ // Unterminated quotes just return T_EOF without any T_NEWLINE.
+ RunTest("\"unterminated quoted string", {});
+
+ RunTest("\"1 2 3\"\n \"unterminated quoted string", {{"1 2 3"}});
+
+ // Escaping quotes is not allowed and are treated as an unterminated quoted string.
+ RunTest("\"quoted escaped quote\\\"\"", {});
+ RunTest("\"quoted escaped\\\" quote\"", {});
+ RunTest("\"\\\"quoted escaped quote\"", {});
+
+ RunTest("\"quoted control characters \\n \\r \\t \\\\ \\b \\\r \\\n \r \n\"",
+ {{"quoted control characters \\n \\r \\t \\\\ \\b \\\r \\\n \r \n"}});
+
+ RunTest("\"quoted simple string\" \"second quoted string\"",
+ {{"quoted simple string", "second quoted string"}});
+
+ RunTest("\"# comment quoted string\"", {{"# comment quoted string"}});
+
+ RunTest("\"Adjacent \"\"quoted strings\"", {{"Adjacent quoted strings"}});
+}
+
+} // namespace init
+} // namespace android
diff --git a/init/uevent.h b/init/uevent.h
index c4fd945..dc35fd9 100644
--- a/init/uevent.h
+++ b/init/uevent.h
@@ -29,6 +29,7 @@
std::string firmware;
std::string partition_name;
std::string device_name;
+ std::string modalias;
int partition_num;
int major;
int minor;
diff --git a/init/log.h b/init/uevent_handler.h
similarity index 71%
rename from init/log.h
rename to init/uevent_handler.h
index 5a4eba6..75d1990 100644
--- a/init/log.h
+++ b/init/uevent_handler.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,19 +14,21 @@
* limitations under the License.
*/
-#ifndef _INIT_LOG_H_
-#define _INIT_LOG_H_
+#pragma once
-#include <sys/cdefs.h>
+#include "uevent.h"
namespace android {
namespace init {
-void InitKernelLogging(char* argv[]);
+class UeventHandler {
+ public:
+ virtual ~UeventHandler() = default;
-int selinux_klog_callback(int level, const char* fmt, ...) __printflike(2, 3);
+ virtual void HandleUevent(const Uevent& uevent) = 0;
+
+ virtual void ColdbootDone() {}
+};
} // namespace init
} // namespace android
-
-#endif
diff --git a/init/uevent_listener.cpp b/init/uevent_listener.cpp
index ac1d7c7..8cf2128 100644
--- a/init/uevent_listener.cpp
+++ b/init/uevent_listener.cpp
@@ -39,6 +39,7 @@
uevent->firmware.clear();
uevent->partition_name.clear();
uevent->device_name.clear();
+ uevent->modalias.clear();
// currently ignoring SEQNUM
while (*msg) {
if (!strncmp(msg, "ACTION=", 7)) {
@@ -68,6 +69,9 @@
} else if (!strncmp(msg, "DEVNAME=", 8)) {
msg += 8;
uevent->device_name = msg;
+ } else if (!strncmp(msg, "MODALIAS=", 9)) {
+ msg += 9;
+ uevent->modalias = msg;
}
// advance to after the next \0
@@ -83,8 +87,8 @@
}
UeventListener::UeventListener() {
- // is 256K enough? udev uses 16MB!
- device_fd_.reset(uevent_open_socket(256 * 1024, true));
+ // is 2MB enough? udev uses 128MB!
+ device_fd_.reset(uevent_open_socket(2 * 1024 * 1024, true));
if (device_fd_ == -1) {
LOG(FATAL) << "Could not open uevent socket";
}
diff --git a/init/ueventd.cpp b/init/ueventd.cpp
index 1435d82..95be6af 100644
--- a/init/ueventd.cpp
+++ b/init/ueventd.cpp
@@ -30,13 +30,15 @@
#include <android-base/chrono_utils.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
+#include <fstab/fstab.h>
#include <selinux/android.h>
#include <selinux/selinux.h>
#include "devices.h"
#include "firmware_handler.h"
-#include "log.h"
+#include "modalias_handler.h"
#include "selinux.h"
+#include "uevent_handler.h"
#include "uevent_listener.h"
#include "ueventd_parser.h"
#include "util.h"
@@ -106,9 +108,10 @@
class ColdBoot {
public:
- ColdBoot(UeventListener& uevent_listener, DeviceHandler& device_handler)
+ ColdBoot(UeventListener& uevent_listener,
+ std::vector<std::unique_ptr<UeventHandler>>& uevent_handlers)
: uevent_listener_(uevent_listener),
- device_handler_(device_handler),
+ uevent_handlers_(uevent_handlers),
num_handler_subprocesses_(std::thread::hardware_concurrency() ?: 4) {}
void Run();
@@ -121,7 +124,7 @@
void WaitForSubProcesses();
UeventListener& uevent_listener_;
- DeviceHandler& device_handler_;
+ std::vector<std::unique_ptr<UeventHandler>>& uevent_handlers_;
unsigned int num_handler_subprocesses_;
std::vector<Uevent> uevent_queue_;
@@ -132,15 +135,16 @@
void ColdBoot::UeventHandlerMain(unsigned int process_num, unsigned int total_processes) {
for (unsigned int i = process_num; i < uevent_queue_.size(); i += total_processes) {
auto& uevent = uevent_queue_[i];
- device_handler_.HandleDeviceEvent(uevent);
+
+ for (auto& uevent_handler : uevent_handlers_) {
+ uevent_handler->HandleUevent(uevent);
+ }
}
_exit(EXIT_SUCCESS);
}
void ColdBoot::RegenerateUevents() {
uevent_listener_.RegenerateUevents([this](const Uevent& uevent) {
- HandleFirmwareEvent(uevent);
-
uevent_queue_.emplace_back(std::move(uevent));
return ListenerAction::kContinue;
});
@@ -163,7 +167,6 @@
void ColdBoot::DoRestoreCon() {
selinux_android_restorecon("/sys", SELINUX_ANDROID_RESTORECON_RECURSE);
- device_handler_.set_skip_restorecon(false);
}
void ColdBoot::WaitForSubProcesses() {
@@ -214,38 +217,6 @@
LOG(INFO) << "Coldboot took " << cold_boot_timer.duration().count() / 1000.0f << " seconds";
}
-DeviceHandler CreateDeviceHandler() {
- Parser parser;
-
- std::vector<Subsystem> subsystems;
- parser.AddSectionParser("subsystem", std::make_unique<SubsystemParser>(&subsystems));
-
- using namespace std::placeholders;
- std::vector<SysfsPermissions> sysfs_permissions;
- std::vector<Permissions> dev_permissions;
- parser.AddSingleLineParser("/sys/",
- std::bind(ParsePermissionsLine, _1, &sysfs_permissions, nullptr));
- parser.AddSingleLineParser("/dev/",
- std::bind(ParsePermissionsLine, _1, nullptr, &dev_permissions));
-
- parser.ParseConfig("/ueventd.rc");
- parser.ParseConfig("/vendor/ueventd.rc");
- parser.ParseConfig("/odm/ueventd.rc");
-
- /*
- * keep the current product name base configuration so
- * we remain backwards compatible and allow it to override
- * everything
- * TODO: cleanup platform ueventd.rc to remove vendor specific
- * device node entries (b/34968103)
- */
- std::string hardware = android::base::GetProperty("ro.hardware", "");
- parser.ParseConfig("/ueventd." + hardware + ".rc");
-
- return DeviceHandler(std::move(dev_permissions), std::move(sysfs_permissions),
- std::move(subsystems), true);
-}
-
int ueventd_main(int argc, char** argv) {
/*
* init sets the umask to 077 for forked processes. We need to
@@ -254,21 +225,47 @@
*/
umask(000);
- InitKernelLogging(argv);
+ android::base::InitLogging(argv, &android::base::KernelLogger);
LOG(INFO) << "ueventd started!";
SelinuxSetupKernelLogging();
SelabelInitialize();
- DeviceHandler device_handler = CreateDeviceHandler();
+ std::vector<std::unique_ptr<UeventHandler>> uevent_handlers;
UeventListener uevent_listener;
+ {
+ // Keep the current product name base configuration so we remain backwards compatible and
+ // allow it to override everything.
+ // TODO: cleanup platform ueventd.rc to remove vendor specific device node entries (b/34968103)
+ auto hardware = android::base::GetProperty("ro.hardware", "");
+
+ auto ueventd_configuration =
+ ParseConfig({"/ueventd.rc", "/vendor/ueventd.rc", "/odm/ueventd.rc",
+ "/ueventd." + hardware + ".rc"});
+
+ uevent_handlers.emplace_back(std::make_unique<DeviceHandler>(
+ std::move(ueventd_configuration.dev_permissions),
+ std::move(ueventd_configuration.sysfs_permissions),
+ std::move(ueventd_configuration.subsystems), fs_mgr_get_boot_devices(), true));
+ uevent_handlers.emplace_back(std::make_unique<FirmwareHandler>(
+ std::move(ueventd_configuration.firmware_directories)));
+
+ if (ueventd_configuration.enable_modalias_handling) {
+ uevent_handlers.emplace_back(std::make_unique<ModaliasHandler>());
+ }
+ }
+
if (access(COLDBOOT_DONE, F_OK) != 0) {
- ColdBoot cold_boot(uevent_listener, device_handler);
+ ColdBoot cold_boot(uevent_listener, uevent_handlers);
cold_boot.Run();
}
+ for (auto& uevent_handler : uevent_handlers) {
+ uevent_handler->ColdbootDone();
+ }
+
// We use waitpid() in ColdBoot, so we can't ignore SIGCHLD until now.
signal(SIGCHLD, SIG_IGN);
// Reap and pending children that exited between the last call to waitpid() and setting SIG_IGN
@@ -276,9 +273,10 @@
while (waitpid(-1, nullptr, WNOHANG) > 0) {
}
- uevent_listener.Poll([&device_handler](const Uevent& uevent) {
- HandleFirmwareEvent(uevent);
- device_handler.HandleDeviceEvent(uevent);
+ uevent_listener.Poll([&uevent_handlers](const Uevent& uevent) {
+ for (auto& uevent_handler : uevent_handlers) {
+ uevent_handler->HandleUevent(uevent);
+ }
return ListenerAction::kContinue;
});
diff --git a/init/ueventd_parser.cpp b/init/ueventd_parser.cpp
index f74c878..677938e 100644
--- a/init/ueventd_parser.cpp
+++ b/init/ueventd_parser.cpp
@@ -20,6 +20,7 @@
#include <pwd.h>
#include "keyword_map.h"
+#include "parser.h"
namespace android {
namespace init {
@@ -72,6 +73,50 @@
return Success();
}
+Result<Success> ParseFirmwareDirectoriesLine(std::vector<std::string>&& args,
+ std::vector<std::string>* firmware_directories) {
+ if (args.size() < 2) {
+ return Error() << "firmware_directories must have at least 1 entry";
+ }
+
+ std::move(std::next(args.begin()), args.end(), std::back_inserter(*firmware_directories));
+
+ return Success();
+}
+
+Result<Success> ParseModaliasHandlingLine(std::vector<std::string>&& args,
+ bool* enable_modalias_handling) {
+ if (args.size() != 2) {
+ return Error() << "modalias_handling lines take exactly one parameter";
+ }
+
+ if (args[1] == "enabled") {
+ *enable_modalias_handling = true;
+ } else if (args[1] == "disabled") {
+ *enable_modalias_handling = false;
+ } else {
+ return Error() << "modalias_handling takes either 'enabled' or 'disabled' as a parameter";
+ }
+
+ return Success();
+}
+
+class SubsystemParser : public SectionParser {
+ public:
+ SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
+ Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
+ int line) override;
+ Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override;
+ Result<Success> EndSection() override;
+
+ private:
+ Result<Success> ParseDevName(std::vector<std::string>&& args);
+ Result<Success> ParseDirName(std::vector<std::string>&& args);
+
+ Subsystem subsystem_;
+ std::vector<Subsystem>* subsystems_;
+};
+
Result<Success> SubsystemParser::ParseSection(std::vector<std::string>&& args,
const std::string& filename, int line) {
if (args.size() != 2) {
@@ -89,11 +134,11 @@
Result<Success> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
if (args[1] == "uevent_devname") {
- subsystem_.devname_source_ = Subsystem::DevnameSource::DEVNAME_UEVENT_DEVNAME;
+ subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
return Success();
}
if (args[1] == "uevent_devpath") {
- subsystem_.devname_source_ = Subsystem::DevnameSource::DEVNAME_UEVENT_DEVPATH;
+ subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
return Success();
}
@@ -138,5 +183,32 @@
return Success();
}
+UeventdConfiguration ParseConfig(const std::vector<std::string>& configs) {
+ Parser parser;
+ UeventdConfiguration ueventd_configuration;
+
+ parser.AddSectionParser("subsystem",
+ std::make_unique<SubsystemParser>(&ueventd_configuration.subsystems));
+
+ using namespace std::placeholders;
+ parser.AddSingleLineParser(
+ "/sys/",
+ std::bind(ParsePermissionsLine, _1, &ueventd_configuration.sysfs_permissions, nullptr));
+ parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, nullptr,
+ &ueventd_configuration.dev_permissions));
+ parser.AddSingleLineParser("firmware_directories",
+ std::bind(ParseFirmwareDirectoriesLine, _1,
+ &ueventd_configuration.firmware_directories));
+ parser.AddSingleLineParser("modalias_handling",
+ std::bind(ParseModaliasHandlingLine, _1,
+ &ueventd_configuration.enable_modalias_handling));
+
+ for (const auto& config : configs) {
+ parser.ParseConfig(config);
+ }
+
+ return ueventd_configuration;
+}
+
} // namespace init
} // namespace android
diff --git a/init/ueventd_parser.h b/init/ueventd_parser.h
index 83684f3..7d30edf 100644
--- a/init/ueventd_parser.h
+++ b/init/ueventd_parser.h
@@ -21,30 +21,19 @@
#include <vector>
#include "devices.h"
-#include "parser.h"
namespace android {
namespace init {
-class SubsystemParser : public SectionParser {
- public:
- SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
- Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
- int line) override;
- Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override;
- Result<Success> EndSection() override;
-
- private:
- Result<Success> ParseDevName(std::vector<std::string>&& args);
- Result<Success> ParseDirName(std::vector<std::string>&& args);
-
- Subsystem subsystem_;
- std::vector<Subsystem>* subsystems_;
+struct UeventdConfiguration {
+ std::vector<Subsystem> subsystems;
+ std::vector<SysfsPermissions> sysfs_permissions;
+ std::vector<Permissions> dev_permissions;
+ std::vector<std::string> firmware_directories;
+ bool enable_modalias_handling = false;
};
-Result<Success> ParsePermissionsLine(std::vector<std::string>&& args,
- std::vector<SysfsPermissions>* out_sysfs_permissions,
- std::vector<Permissions>* out_dev_permissions);
+UeventdConfiguration ParseConfig(const std::vector<std::string>& configs);
} // namespace init
} // namespace android
diff --git a/init/ueventd_parser_test.cpp b/init/ueventd_parser_test.cpp
new file mode 100644
index 0000000..31208b9
--- /dev/null
+++ b/init/ueventd_parser_test.cpp
@@ -0,0 +1,224 @@
+/*
+ * Copyright (C) 2018 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 "ueventd_parser.h"
+
+#include <android-base/test_utils.h>
+#include <gtest/gtest.h>
+#include <private/android_filesystem_config.h>
+
+namespace android {
+namespace init {
+
+void TestSubsystems(const Subsystem& expected, const Subsystem& test) {
+ EXPECT_EQ(expected.name_, test.name_);
+ EXPECT_EQ(expected.devname_source_, test.devname_source_) << expected.name_;
+ EXPECT_EQ(expected.dir_name_, test.dir_name_) << expected.name_;
+}
+
+void TestPermissions(const Permissions& expected, const Permissions& test) {
+ EXPECT_EQ(expected.name_, test.name_);
+ EXPECT_EQ(expected.perm_, test.perm_) << expected.name_;
+ EXPECT_EQ(expected.uid_, test.uid_) << expected.name_;
+ EXPECT_EQ(expected.gid_, test.gid_) << expected.name_;
+ EXPECT_EQ(expected.prefix_, test.prefix_) << expected.name_;
+ EXPECT_EQ(expected.wildcard_, test.wildcard_) << expected.name_;
+}
+
+void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test) {
+ TestPermissions(expected, test);
+ EXPECT_EQ(expected.attribute_, test.attribute_);
+}
+
+template <typename T, typename F>
+void TestVector(const T& expected, const T& test, F function) {
+ ASSERT_EQ(expected.size(), test.size());
+ auto expected_it = expected.begin();
+ auto test_it = test.begin();
+
+ for (; expected_it != expected.end(); ++expected_it, ++test_it) {
+ function(*expected_it, *test_it);
+ }
+}
+
+void TestUeventdFile(const std::string& content, const UeventdConfiguration& expected) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ ASSERT_TRUE(android::base::WriteStringToFd(content, tf.fd));
+
+ auto result = ParseConfig({tf.path});
+
+ TestVector(expected.subsystems, result.subsystems, TestSubsystems);
+ TestVector(expected.sysfs_permissions, result.sysfs_permissions, TestSysfsPermissions);
+ TestVector(expected.dev_permissions, result.dev_permissions, TestPermissions);
+ EXPECT_EQ(expected.firmware_directories, result.firmware_directories);
+}
+
+TEST(ueventd_parser, EmptyFile) {
+ TestUeventdFile("", {});
+}
+
+TEST(ueventd_parser, Subsystems) {
+ auto ueventd_file = R"(
+subsystem test_devname
+ devname uevent_devname
+
+subsystem test_devpath_no_dirname
+ devname uevent_devpath
+
+subsystem test_devname2
+ devname uevent_devname
+
+subsystem test_devpath_dirname
+ devname uevent_devpath
+ dirname /dev/graphics
+)";
+
+ auto subsystems = std::vector<Subsystem>{
+ {"test_devname", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
+ {"test_devpath_no_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev"},
+ {"test_devname2", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
+ {"test_devpath_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev/graphics"}};
+
+ TestUeventdFile(ueventd_file, {subsystems, {}, {}, {}});
+}
+
+TEST(ueventd_parser, Permissions) {
+ auto ueventd_file = R"(
+/dev/rtc0 0640 system system
+/dev/graphics/* 0660 root graphics
+/dev/*/test 0660 root system
+
+/sys/devices/platform/trusty.* trusty_version 0440 root log
+/sys/devices/virtual/input/input enable 0660 root input
+/sys/devices/virtual/*/input poll_delay 0660 root input
+)";
+
+ auto permissions = std::vector<Permissions>{
+ {"/dev/rtc0", 0640, AID_SYSTEM, AID_SYSTEM},
+ {"/dev/graphics/*", 0660, AID_ROOT, AID_GRAPHICS},
+ {"/dev/*/test", 0660, AID_ROOT, AID_SYSTEM},
+ };
+
+ auto sysfs_permissions = std::vector<SysfsPermissions>{
+ {"/sys/devices/platform/trusty.*", "trusty_version", 0440, AID_ROOT, AID_LOG},
+ {"/sys/devices/virtual/input/input", "enable", 0660, AID_ROOT, AID_INPUT},
+ {"/sys/devices/virtual/*/input", "poll_delay", 0660, AID_ROOT, AID_INPUT},
+ };
+
+ TestUeventdFile(ueventd_file, {{}, sysfs_permissions, permissions, {}});
+}
+
+TEST(ueventd_parser, FirmwareDirectories) {
+ auto ueventd_file = R"(
+firmware_directories /first/ /second /third
+firmware_directories /more
+)";
+
+ auto firmware_directories = std::vector<std::string>{
+ "/first/",
+ "/second",
+ "/third",
+ "/more",
+ };
+
+ TestUeventdFile(ueventd_file, {{}, {}, {}, firmware_directories});
+}
+
+TEST(ueventd_parser, AllTogether) {
+ auto ueventd_file = R"(
+
+/dev/rtc0 0640 system system
+firmware_directories /first/ /second /third
+/sys/devices/platform/trusty.* trusty_version 0440 root log
+
+subsystem test_devname
+ devname uevent_devname
+
+/dev/graphics/* 0660 root graphics
+
+subsystem test_devpath_no_dirname
+ devname uevent_devpath
+
+/sys/devices/virtual/input/input enable 0660 root input
+
+## this is a comment
+
+subsystem test_devname2
+## another comment
+ devname uevent_devname
+
+subsystem test_devpath_dirname
+ devname uevent_devpath
+ dirname /dev/graphics
+
+/dev/*/test 0660 root system
+/sys/devices/virtual/*/input poll_delay 0660 root input
+firmware_directories /more
+
+#ending comment
+)";
+
+ auto subsystems = std::vector<Subsystem>{
+ {"test_devname", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
+ {"test_devpath_no_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev"},
+ {"test_devname2", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
+ {"test_devpath_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev/graphics"}};
+
+ auto permissions = std::vector<Permissions>{
+ {"/dev/rtc0", 0640, AID_SYSTEM, AID_SYSTEM},
+ {"/dev/graphics/*", 0660, AID_ROOT, AID_GRAPHICS},
+ {"/dev/*/test", 0660, AID_ROOT, AID_SYSTEM},
+ };
+
+ auto sysfs_permissions = std::vector<SysfsPermissions>{
+ {"/sys/devices/platform/trusty.*", "trusty_version", 0440, AID_ROOT, AID_LOG},
+ {"/sys/devices/virtual/input/input", "enable", 0660, AID_ROOT, AID_INPUT},
+ {"/sys/devices/virtual/*/input", "poll_delay", 0660, AID_ROOT, AID_INPUT},
+ };
+
+ auto firmware_directories = std::vector<std::string>{
+ "/first/",
+ "/second",
+ "/third",
+ "/more",
+ };
+
+ TestUeventdFile(ueventd_file,
+ {subsystems, sysfs_permissions, permissions, firmware_directories});
+}
+
+// All of these lines are ill-formed, so test that there is 0 output.
+TEST(ueventd_parser, ParseErrors) {
+ auto ueventd_file = R"(
+
+/dev/rtc0 badmode baduidbad system
+/dev/rtc0 0640 baduidbad system
+/dev/rtc0 0640 system baduidbad
+firmware_directories #no directory listed
+/sys/devices/platform/trusty.* trusty_version badmode root log
+/sys/devices/platform/trusty.* trusty_version 0440 baduidbad log
+/sys/devices/platform/trusty.* trusty_version 0440 root baduidbad
+
+subsystem #no name
+
+)";
+
+ TestUeventdFile(ueventd_file, {});
+}
+
+} // namespace init
+} // namespace android
diff --git a/init/util.cpp b/init/util.cpp
index 4455b2e..105ac87 100644
--- a/init/util.cpp
+++ b/init/util.cpp
@@ -33,25 +33,21 @@
#include <android-base/file.h>
#include <android-base/logging.h>
+#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
-#include <cutils/android_reboot.h>
#include <cutils/sockets.h>
#include <selinux/android.h>
-#include "reboot.h"
-
#if defined(__ANDROID__)
-#include <android-base/properties.h>
-
#include "selinux.h"
#else
#include "host_init_stubs.h"
#endif
#ifdef _INIT_INIT_H
-#error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals"
+#error "Do not include init.h in files used by ueventd; it will expose init's globals"
#endif
using android::base::boot_clock;
diff --git a/libappfuse/FuseBuffer.cc b/libappfuse/FuseBuffer.cc
index 1eab46c..1915f22 100644
--- a/libappfuse/FuseBuffer.cc
+++ b/libappfuse/FuseBuffer.cc
@@ -149,8 +149,8 @@
}
constexpr int kMaxMessageSize = sizeof(FuseBuffer);
- if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUFFORCE, &kMaxMessageSize, sizeof(int)) != 0 ||
- setsockopt(fds[1], SOL_SOCKET, SO_SNDBUFFORCE, &kMaxMessageSize, sizeof(int)) != 0) {
+ if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &kMaxMessageSize, sizeof(int)) != 0 ||
+ setsockopt(fds[1], SOL_SOCKET, SO_SNDBUF, &kMaxMessageSize, sizeof(int)) != 0) {
PLOG(ERROR) << "Failed to update buffer size for socket";
return false;
}
diff --git a/libappfuse/include/libappfuse/FuseBuffer.h b/libappfuse/include/libappfuse/FuseBuffer.h
index 7a70bf3..3063815 100644
--- a/libappfuse/include/libappfuse/FuseBuffer.h
+++ b/libappfuse/include/libappfuse/FuseBuffer.h
@@ -25,7 +25,7 @@
// The numbers came from sdcard.c.
// Maximum number of bytes to write/read in one request/one reply.
-constexpr size_t kFuseMaxWrite = 256 * 1024;
+constexpr size_t kFuseMaxWrite = 128 * 1024;
constexpr size_t kFuseMaxRead = 128 * 1024;
constexpr int32_t kFuseSuccess = 0;
diff --git a/libasyncio/Android.bp b/libasyncio/Android.bp
index 8a2afea..4ab439d 100644
--- a/libasyncio/Android.bp
+++ b/libasyncio/Android.bp
@@ -27,6 +27,7 @@
name: "libasyncio",
defaults: ["libasyncio_defaults"],
vendor_available: true,
+ recovery_available: true,
host_supported: true,
srcs: [
"AsyncIO.cpp",
diff --git a/libasyncio/AsyncIO.cpp b/libasyncio/AsyncIO.cpp
index 7430bc8..6149f09 100644
--- a/libasyncio/AsyncIO.cpp
+++ b/libasyncio/AsyncIO.cpp
@@ -17,9 +17,10 @@
#include <asyncio/AsyncIO.h>
#include <sys/syscall.h>
#include <unistd.h>
+#include <cstdint>
+#include <cstring>
int io_setup(unsigned nr, aio_context_t* ctxp) {
- memset(ctxp, 0, sizeof(*ctxp));
return syscall(__NR_io_setup, nr, ctxp);
}
@@ -48,3 +49,11 @@
iocb->aio_nbytes = count;
iocb->aio_offset = offset;
}
+
+void io_prep_pread(struct iocb* iocb, int fd, void* buf, size_t count, long long offset) {
+ io_prep(iocb, fd, buf, count, offset, true);
+}
+
+void io_prep_pwrite(struct iocb* iocb, int fd, void* buf, size_t count, long long offset) {
+ io_prep(iocb, fd, buf, count, offset, false);
+}
diff --git a/libasyncio/include/asyncio/AsyncIO.h b/libasyncio/include/asyncio/AsyncIO.h
index e3fb93a..9620d2a 100644
--- a/libasyncio/include/asyncio/AsyncIO.h
+++ b/libasyncio/include/asyncio/AsyncIO.h
@@ -17,9 +17,9 @@
#ifndef _ASYNCIO_H
#define _ASYNCIO_H
-#include <cstring>
-#include <cstdint>
#include <linux/aio_abi.h>
+#include <stdbool.h>
+#include <stdint.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <time.h>
@@ -35,10 +35,14 @@
int io_setup(unsigned nr, aio_context_t* ctxp);
int io_destroy(aio_context_t ctx);
-int io_submit(aio_context_t ctx, long nr, iocb** iocbpp);
-int io_getevents(aio_context_t ctx, long min_nr, long max_nr, io_event* events, timespec* timeout);
-int io_cancel(aio_context_t ctx, iocb*, io_event* result);
-void io_prep(iocb* iocb, int fd, const void* buf, uint64_t count, int64_t offset, bool read);
+int io_submit(aio_context_t ctx, long nr, struct iocb** iocbpp);
+int io_getevents(aio_context_t ctx, long min_nr, long max_nr, struct io_event* events,
+ struct timespec* timeout);
+int io_cancel(aio_context_t ctx, struct iocb*, struct io_event* result);
+
+void io_prep_pread(struct iocb* iocb, int fd, void* buf, size_t count, long long offset);
+void io_prep_pwrite(struct iocb* iocb, int fd, void* buf, size_t count, long long offset);
+void io_prep(struct iocb* iocb, int fd, const void* buf, uint64_t count, int64_t offset, bool read);
#ifdef __cplusplus
};
diff --git a/libbacktrace/Android.bp b/libbacktrace/Android.bp
index 4bd01d2..a10e636 100644
--- a/libbacktrace/Android.bp
+++ b/libbacktrace/Android.bp
@@ -22,11 +22,6 @@
"-Werror",
],
- // The latest clang (r230699) does not allow SP/PC to be declared in inline asm lists.
- clang_cflags: ["-Wno-inline-asm"],
-
- include_dirs: ["external/libunwind/include/tdep"],
-
target: {
darwin: {
enabled: false,
@@ -47,7 +42,6 @@
"Backtrace.cpp",
"BacktraceCurrent.cpp",
"BacktracePtrace.cpp",
- "thread_utils.c",
"ThreadEntry.cpp",
"UnwindStack.cpp",
"UnwindStackMap.cpp",
@@ -56,12 +50,14 @@
cc_library_headers {
name: "libbacktrace_headers",
vendor_available: true,
+ recovery_available: true,
export_include_dirs: ["include"],
}
cc_library {
name: "libbacktrace",
vendor_available: false,
+ recovery_available: true,
vndk: {
enabled: true,
support_system_process: true,
@@ -92,20 +88,19 @@
shared_libs: [
"libbase",
"liblog",
- "libunwind",
"libunwindstack",
"libdexfile",
],
- static_libs: ["libcutils"],
+ static_libs: [
+ "libprocinfo",
+ ],
// libdexfile will eventually properly export headers, for now
// include these directly.
include_dirs: [
"art/runtime",
],
-
- header_libs: ["jni_headers"],
},
android: {
static_libs: ["libasync_safe"],
@@ -114,6 +109,10 @@
cflags: ["-DNO_LIBDEXFILE_SUPPORT"],
exclude_shared_libs: ["libdexfile"],
},
+ recovery: {
+ cflags: ["-DNO_LIBDEXFILE_SUPPORT"],
+ exclude_shared_libs: ["libdexfile"],
+ },
},
whole_static_libs: ["libdemangle"],
}
@@ -144,7 +143,6 @@
"backtrace_offline_test.cpp",
"backtrace_test.cpp",
"GetPss.cpp",
- "thread_utils.c",
],
cflags: [
@@ -158,7 +156,6 @@
"libbacktrace",
"libdexfile",
"libbase",
- "libcutils",
"liblog",
"libunwindstack",
],
@@ -183,12 +180,16 @@
"art/runtime",
],
+ test_suites: ["device-tests"],
data: [
"testdata/arm/*",
"testdata/arm64/*",
"testdata/x86/*",
"testdata/x86_64/*",
],
+ required: [
+ "libbacktrace_test",
+ ],
}
cc_benchmark {
diff --git a/libbacktrace/Backtrace.cpp b/libbacktrace/Backtrace.cpp
index 6445a7c..6bec63c 100644
--- a/libbacktrace/Backtrace.cpp
+++ b/libbacktrace/Backtrace.cpp
@@ -23,6 +23,7 @@
#include <string>
#include <android-base/stringprintf.h>
+#include <android-base/threads.h>
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
@@ -31,7 +32,6 @@
#include "BacktraceLog.h"
#include "UnwindStack.h"
-#include "thread_utils.h"
using android::base::StringPrintf;
@@ -124,7 +124,7 @@
if (pid == BACKTRACE_CURRENT_PROCESS) {
pid = getpid();
if (tid == BACKTRACE_CURRENT_THREAD) {
- tid = gettid();
+ tid = android::base::GetThreadId();
}
} else if (tid == BACKTRACE_CURRENT_THREAD) {
tid = pid;
diff --git a/libbacktrace/BacktraceCurrent.cpp b/libbacktrace/BacktraceCurrent.cpp
index f6f4423..39cb995 100644
--- a/libbacktrace/BacktraceCurrent.cpp
+++ b/libbacktrace/BacktraceCurrent.cpp
@@ -28,13 +28,13 @@
#include <string>
+#include <android-base/threads.h>
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
#include "BacktraceAsyncSafeLog.h"
#include "BacktraceCurrent.h"
#include "ThreadEntry.h"
-#include "thread_utils.h"
bool BacktraceCurrent::ReadWord(uint64_t ptr, word_t* out_value) {
if (!VerifyReadWordArgs(ptr, out_value)) {
@@ -76,7 +76,7 @@
return UnwindFromContext(num_ignore_frames, ucontext);
}
- if (Tid() != gettid()) {
+ if (Tid() != android::base::GetThreadId()) {
return UnwindThread(num_ignore_frames);
}
@@ -114,16 +114,17 @@
static void SignalLogOnly(int, siginfo_t*, void*) {
ErrnoRestorer restore;
- BACK_ASYNC_SAFE_LOGE("pid %d, tid %d: Received a spurious signal %d\n", getpid(), gettid(),
- THREAD_SIGNAL);
+ BACK_ASYNC_SAFE_LOGE("pid %d, tid %d: Received a spurious signal %d\n", getpid(),
+ static_cast<int>(android::base::GetThreadId()), THREAD_SIGNAL);
}
static void SignalHandler(int, siginfo_t*, void* sigcontext) {
ErrnoRestorer restore;
- ThreadEntry* entry = ThreadEntry::Get(getpid(), gettid(), false);
+ ThreadEntry* entry = ThreadEntry::Get(getpid(), android::base::GetThreadId(), false);
if (!entry) {
- BACK_ASYNC_SAFE_LOGE("pid %d, tid %d entry not found", getpid(), gettid());
+ BACK_ASYNC_SAFE_LOGE("pid %d, tid %d entry not found", getpid(),
+ static_cast<int>(android::base::GetThreadId()));
return;
}
diff --git a/libbacktrace/BacktraceMap.cpp b/libbacktrace/BacktraceMap.cpp
index bdae140..6a967f7 100644
--- a/libbacktrace/BacktraceMap.cpp
+++ b/libbacktrace/BacktraceMap.cpp
@@ -28,8 +28,9 @@
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
#include <backtrace/backtrace_constants.h>
-
-#include "thread_utils.h"
+#if defined(__linux__)
+#include <procinfo/process_map.h>
+#endif
using android::base::StringPrintf;
@@ -60,27 +61,19 @@
*map = {};
}
-bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
+#if defined(__APPLE__)
+static bool ParseLine(const char* line, backtrace_map_t* map) {
uint64_t start;
uint64_t end;
char permissions[5];
int name_pos;
-#if defined(__APPLE__)
// Mac OS vmmap(1) output:
// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
// 012345678901234567890123456789012345678901234567890123456789
// 0 1 2 3 4 5
if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c %n",
&start, &end, permissions, &name_pos) != 3) {
-#else
-// Linux /proc/<pid>/maps lines:
-// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n
-// 012345678901234567890123456789012345678901234567890123456789
-// 0 1 2 3 4 5
- if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %4s %*x %*x:%*x %*d %n",
- &start, &end, permissions, &name_pos) != 3) {
-#endif
return false;
}
@@ -107,24 +100,15 @@
map->flags, map->name.c_str());
return true;
}
+#endif // defined(__APPLE__)
bool BacktraceMap::Build() {
#if defined(__APPLE__)
char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
-#else
- char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1];
-#endif
char line[1024];
-
-#if defined(__APPLE__)
// cmd is guaranteed to always be big enough to hold this string.
snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
FILE* fp = popen(cmd, "r");
-#else
- // path is guaranteed to always be big enough to hold this string.
- snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
- FILE* fp = fopen(path, "r");
-#endif
if (fp == nullptr) {
return false;
}
@@ -135,13 +119,19 @@
maps_.push_back(map);
}
}
-#if defined(__APPLE__)
pclose(fp);
-#else
- fclose(fp);
-#endif
-
return true;
+#else
+ return android::procinfo::ReadProcessMaps(
+ pid_, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t, const char* name) {
+ maps_.resize(maps_.size() + 1);
+ backtrace_map_t& map = maps_.back();
+ map.start = start;
+ map.end = end;
+ map.flags = flags;
+ map.name = name;
+ });
+#endif
}
#if defined(__APPLE__)
diff --git a/libbacktrace/BacktracePtrace.cpp b/libbacktrace/BacktracePtrace.cpp
index bf6b16f..9da457d 100644
--- a/libbacktrace/BacktracePtrace.cpp
+++ b/libbacktrace/BacktracePtrace.cpp
@@ -28,7 +28,6 @@
#include "BacktraceLog.h"
#include "BacktracePtrace.h"
-#include "thread_utils.h"
#if !defined(__APPLE__)
static bool PtraceRead(pid_t tid, uint64_t addr, word_t* out_value) {
diff --git a/libbacktrace/UnwindStack.cpp b/libbacktrace/UnwindStack.cpp
index 711a12a..4e7f761 100644
--- a/libbacktrace/UnwindStack.cpp
+++ b/libbacktrace/UnwindStack.cpp
@@ -23,10 +23,6 @@
#include <set>
#include <string>
-#if !defined(__ANDROID__)
-#include <cutils/threads.h>
-#endif
-
#include <backtrace/Backtrace.h>
#include <demangle.h>
#include <unwindstack/Elf.h>
@@ -163,6 +159,9 @@
}
std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
+ if (!skip_frames_) {
+ skip_names.clear();
+ }
return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
}
diff --git a/libbacktrace/UnwindStackMap.cpp b/libbacktrace/UnwindStackMap.cpp
index 9c6fed4..d2d6ab8 100644
--- a/libbacktrace/UnwindStackMap.cpp
+++ b/libbacktrace/UnwindStackMap.cpp
@@ -149,13 +149,12 @@
}
// Create the process memory from the stack data.
- uint64_t size = stack.end - stack.start;
- unwindstack::MemoryBuffer* memory = new unwindstack::MemoryBuffer;
- memory->Resize(size);
- memcpy(memory->GetPtr(0), stack.data, size);
- std::shared_ptr<unwindstack::Memory> shared_memory(memory);
-
- process_memory_.reset(new unwindstack::MemoryRange(shared_memory, 0, size, stack.start));
+ if (memory_ == nullptr) {
+ memory_ = new unwindstack::MemoryOfflineBuffer(stack.data, stack.start, stack.end);
+ process_memory_.reset(memory_);
+ } else {
+ memory_->Reset(stack.data, stack.start, stack.end);
+ }
return true;
}
diff --git a/libbacktrace/UnwindStackMap.h b/libbacktrace/UnwindStackMap.h
index ec0d9c1..039f4a2 100644
--- a/libbacktrace/UnwindStackMap.h
+++ b/libbacktrace/UnwindStackMap.h
@@ -79,6 +79,9 @@
bool Build(const std::vector<backtrace_map_t>& maps);
bool CreateProcessMemory(const backtrace_stackinfo_t& stack);
+
+ private:
+ unwindstack::MemoryOfflineBuffer* memory_ = nullptr;
};
#endif // _LIBBACKTRACE_UNWINDSTACK_MAP_H
diff --git a/libbacktrace/backtrace_benchmarks.cpp b/libbacktrace/backtrace_benchmarks.cpp
index a23e3b4..099ac60 100644
--- a/libbacktrace/backtrace_benchmarks.cpp
+++ b/libbacktrace/backtrace_benchmarks.cpp
@@ -27,6 +27,7 @@
#include <string>
#include <android-base/file.h>
+#include <android-base/threads.h>
#include <benchmark/benchmark.h>
@@ -154,7 +155,7 @@
static void CreateBacktrace(benchmark::State& state, BacktraceMap* map, BacktraceCreateFn fn) {
while (state.KeepRunning()) {
- std::unique_ptr<Backtrace> backtrace(fn(getpid(), gettid(), map));
+ std::unique_ptr<Backtrace> backtrace(fn(getpid(), android::base::GetThreadId(), map));
backtrace->Unwind(0);
}
}
diff --git a/libbacktrace/backtrace_offline_test.cpp b/libbacktrace/backtrace_offline_test.cpp
index 9877f29..7d1027e 100644
--- a/libbacktrace/backtrace_offline_test.cpp
+++ b/libbacktrace/backtrace_offline_test.cpp
@@ -31,9 +31,9 @@
#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
+#include <android-base/threads.h>
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
-#include <cutils/threads.h>
#include <gtest/gtest.h>
@@ -99,7 +99,7 @@
static void* OfflineThreadFunc(void* arg) {
OfflineThreadArg* fn_arg = reinterpret_cast<OfflineThreadArg*>(arg);
- fn_arg->tid = gettid();
+ fn_arg->tid = android::base::GetThreadId();
test_get_context_and_wait(&fn_arg->ucontext, &fn_arg->exit_flag);
return nullptr;
}
diff --git a/libbacktrace/backtrace_test.cpp b/libbacktrace/backtrace_test.cpp
index aab6db9..06a32c7 100644
--- a/libbacktrace/backtrace_test.cpp
+++ b/libbacktrace/backtrace_test.cpp
@@ -46,16 +46,16 @@
#include <android-base/macros.h>
#include <android-base/stringprintf.h>
+#include <android-base/test_utils.h>
+#include <android-base/threads.h>
#include <android-base/unique_fd.h>
#include <cutils/atomic.h>
-#include <cutils/threads.h>
#include <gtest/gtest.h>
// For the THREAD_SIGNAL definition.
#include "BacktraceCurrent.h"
#include "backtrace_testlib.h"
-#include "thread_utils.h"
// Number of microseconds per milliseconds.
#define US_PER_MSEC 1000
@@ -69,6 +69,9 @@
// Number of simultaneous threads running in our forked process.
#define NUM_PTRACE_THREADS 5
+// The list of shared libaries that make up the backtrace library.
+static std::vector<std::string> kBacktraceLibs{"libunwindstack.so", "libbacktrace.so"};
+
struct thread_t {
pid_t tid;
int32_t state;
@@ -256,16 +259,49 @@
VERIFY_NO_ERROR(backtrace->GetError().error_code);
ASSERT_TRUE(backtrace->NumFrames() != 0);
+ // None of the frames should be in the backtrace libraries.
for (const auto& frame : *backtrace ) {
if (BacktraceMap::IsValid(frame.map)) {
const std::string name = basename(frame.map.name.c_str());
- ASSERT_TRUE(name != "libunwind.so" && name != "libbacktrace.so")
- << DumpFrames(backtrace.get());
+ for (const auto& lib : kBacktraceLibs) {
+ ASSERT_TRUE(name != lib) << DumpFrames(backtrace.get());
+ }
}
- break;
}
}
+TEST(libbacktrace, local_unwind_frames) {
+ // Verify that a local unwind with the skip frames disabled does include
+ // frames within the backtrace libraries.
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), getpid()));
+ ASSERT_TRUE(backtrace.get() != nullptr);
+ backtrace->SetSkipFrames(false);
+ ASSERT_TRUE(backtrace->Unwind(0));
+ VERIFY_NO_ERROR(backtrace->GetError().error_code);
+
+ ASSERT_TRUE(backtrace->NumFrames() != 0);
+ size_t first_frame_non_backtrace_lib = 0;
+ for (const auto& frame : *backtrace) {
+ if (BacktraceMap::IsValid(frame.map)) {
+ const std::string name = basename(frame.map.name.c_str());
+ bool found = false;
+ for (const auto& lib : kBacktraceLibs) {
+ if (name == lib) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ first_frame_non_backtrace_lib = frame.num;
+ break;
+ }
+ }
+ }
+
+ ASSERT_NE(0U, first_frame_non_backtrace_lib) << "No frames found in backtrace libraries:\n"
+ << DumpFrames(backtrace.get());
+}
+
TEST(libbacktrace, local_trace) {
ASSERT_NE(test_level_one(1, 2, 3, 4, VerifyLevelBacktrace, nullptr), 0);
}
@@ -488,7 +524,7 @@
}
void VerifyLevelThread(void*) {
- std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), android::base::GetThreadId()));
ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
VERIFY_NO_ERROR(backtrace->GetError().error_code);
@@ -501,7 +537,7 @@
}
static void VerifyMaxThread(void*) {
- std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), gettid()));
+ std::unique_ptr<Backtrace> backtrace(Backtrace::Create(getpid(), android::base::GetThreadId()));
ASSERT_TRUE(backtrace.get() != nullptr);
ASSERT_TRUE(backtrace->Unwind(0));
ASSERT_EQ(BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT, backtrace->GetError().error_code);
@@ -516,7 +552,7 @@
static void* ThreadLevelRun(void* data) {
thread_t* thread = reinterpret_cast<thread_t*>(data);
- thread->tid = gettid();
+ thread->tid = android::base::GetThreadId();
EXPECT_NE(test_level_one(1, 2, 3, 4, ThreadSetState, data), 0);
return nullptr;
}
@@ -607,7 +643,7 @@
static void* ThreadMaxRun(void* data) {
thread_t* thread = reinterpret_cast<thread_t*>(data);
- thread->tid = gettid();
+ thread->tid = android::base::GetThreadId();
EXPECT_NE(test_recursive_call(MAX_BACKTRACE_FRAMES+10, ThreadSetState, data), 0);
return nullptr;
}
@@ -957,7 +993,7 @@
static void* ThreadReadTest(void* data) {
thread_t* thread_data = reinterpret_cast<thread_t*>(data);
- thread_data->tid = gettid();
+ thread_data->tid = android::base::GetThreadId();
// Create two map pages.
// Mark the second page as not-readable.
@@ -1150,49 +1186,45 @@
ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library.";
}
-static const char* CopySharedLibrary() {
-#if defined(__LP64__)
- const char* lib_name = "lib64";
-#else
- const char* lib_name = "lib";
-#endif
+static void CopySharedLibrary(const char* tmp_dir, std::string* tmp_so_name) {
+ std::string system_dir;
#if defined(__BIONIC__)
- const char* tmp_so_name = "/data/local/tmp/libbacktrace_test.so";
- std::string cp_cmd = android::base::StringPrintf("cp /system/%s/libbacktrace_test.so %s",
- lib_name, tmp_so_name);
+ system_dir = "/system/lib";
#else
- const char* tmp_so_name = "/tmp/libbacktrace_test.so";
- if (getenv("ANDROID_HOST_OUT") == NULL) {
- fprintf(stderr, "ANDROID_HOST_OUT not set, make sure you run lunch.");
- return nullptr;
- }
- std::string cp_cmd = android::base::StringPrintf("cp %s/%s/libbacktrace_test.so %s",
- getenv("ANDROID_HOST_OUT"), lib_name,
- tmp_so_name);
+ const char* host_out_env = getenv("ANDROID_HOST_OUT");
+ ASSERT_TRUE(host_out_env != nullptr);
+ system_dir = std::string(host_out_env) + "/lib";
#endif
- // Copy the shared so to a tempory directory.
- system(cp_cmd.c_str());
+#if defined(__LP64__)
+ system_dir += "64";
+#endif
- return tmp_so_name;
+ *tmp_so_name = std::string(tmp_dir) + "/libbacktrace_test.so";
+ std::string cp_cmd =
+ android::base::StringPrintf("cp %s/libbacktrace_test.so %s", system_dir.c_str(), tmp_dir);
+
+ // Copy the shared so to a tempory directory.
+ ASSERT_EQ(0, system(cp_cmd.c_str()));
}
TEST(libbacktrace, check_unreadable_elf_local) {
- const char* tmp_so_name = CopySharedLibrary();
- ASSERT_TRUE(tmp_so_name != nullptr);
+ TemporaryDir td;
+ std::string tmp_so_name;
+ ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
struct stat buf;
- ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
+ ASSERT_TRUE(stat(tmp_so_name.c_str(), &buf) != -1);
uint64_t map_size = buf.st_size;
- int fd = open(tmp_so_name, O_RDONLY);
+ int fd = open(tmp_so_name.c_str(), O_RDONLY);
ASSERT_TRUE(fd != -1);
void* map = mmap(nullptr, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
ASSERT_TRUE(map != MAP_FAILED);
close(fd);
- ASSERT_TRUE(unlink(tmp_so_name) != -1);
+ ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
std::vector<std::string> found_functions;
std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
@@ -1220,32 +1252,33 @@
}
TEST(libbacktrace, check_unreadable_elf_remote) {
- const char* tmp_so_name = CopySharedLibrary();
- ASSERT_TRUE(tmp_so_name != nullptr);
+ TemporaryDir td;
+ std::string tmp_so_name;
+ ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
g_ready = 0;
struct stat buf;
- ASSERT_TRUE(stat(tmp_so_name, &buf) != -1);
+ ASSERT_TRUE(stat(tmp_so_name.c_str(), &buf) != -1);
uint64_t map_size = buf.st_size;
pid_t pid;
if ((pid = fork()) == 0) {
- int fd = open(tmp_so_name, O_RDONLY);
+ int fd = open(tmp_so_name.c_str(), O_RDONLY);
if (fd == -1) {
- fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name, strerror(errno));
- unlink(tmp_so_name);
+ fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name.c_str(), strerror(errno));
+ unlink(tmp_so_name.c_str());
exit(0);
}
void* map = mmap(nullptr, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
if (map == MAP_FAILED) {
fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno));
- unlink(tmp_so_name);
+ unlink(tmp_so_name.c_str());
exit(0);
}
close(fd);
- if (unlink(tmp_so_name) == -1) {
+ if (unlink(tmp_so_name.c_str()) == -1) {
fprintf(stderr, "Failed to unlink: %s\n", strerror(errno));
exit(0);
}
@@ -1358,11 +1391,13 @@
typedef int (*test_func_t)(int, int, int, int, void (*)(void*), void*);
TEST(libbacktrace, unwind_through_unreadable_elf_local) {
- const char* tmp_so_name = CopySharedLibrary();
- ASSERT_TRUE(tmp_so_name != nullptr);
- void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
+ TemporaryDir td;
+ std::string tmp_so_name;
+ ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
+
+ void* lib_handle = dlopen(tmp_so_name.c_str(), RTLD_NOW);
ASSERT_TRUE(lib_handle != nullptr);
- ASSERT_TRUE(unlink(tmp_so_name) != -1);
+ ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
test_func_t test_func;
test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
@@ -1375,11 +1410,13 @@
}
TEST(libbacktrace, unwind_through_unreadable_elf_remote) {
- const char* tmp_so_name = CopySharedLibrary();
- ASSERT_TRUE(tmp_so_name != nullptr);
- void* lib_handle = dlopen(tmp_so_name, RTLD_NOW);
+ TemporaryDir td;
+ std::string tmp_so_name;
+ ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
+
+ void* lib_handle = dlopen(tmp_so_name.c_str(), RTLD_NOW);
ASSERT_TRUE(lib_handle != nullptr);
- ASSERT_TRUE(unlink(tmp_so_name) != -1);
+ ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
test_func_t test_func;
test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
@@ -1408,7 +1445,8 @@
size_t frame_num;
if (FindFuncFrameInBacktrace(backtrace.get(), reinterpret_cast<uint64_t>(test_func),
- &frame_num)) {
+ &frame_num) &&
+ frame_num != 0) {
VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uint64_t>(test_func), frame_num);
done = true;
}
@@ -1777,7 +1815,8 @@
static void TestFrameSkipNumbering(create_func_t create_func, map_create_func_t map_create_func) {
std::unique_ptr<BacktraceMap> map(map_create_func(getpid(), false));
- std::unique_ptr<Backtrace> backtrace(create_func(getpid(), gettid(), map.get()));
+ std::unique_ptr<Backtrace> backtrace(
+ create_func(getpid(), android::base::GetThreadId(), map.get()));
backtrace->Unwind(1);
ASSERT_NE(0U, backtrace->NumFrames());
ASSERT_EQ(0U, backtrace->GetFrame(0)->num);
diff --git a/libbacktrace/include/backtrace/Backtrace.h b/libbacktrace/include/backtrace/Backtrace.h
index a088207..10e790b 100644
--- a/libbacktrace/include/backtrace/Backtrace.h
+++ b/libbacktrace/include/backtrace/Backtrace.h
@@ -122,7 +122,7 @@
// Tracing a thread in a different process is not supported.
// If map is NULL, then create the map and manage it internally.
// If map is not NULL, the map is still owned by the caller.
- static Backtrace* Create(pid_t pid, pid_t tid, BacktraceMap* map = NULL);
+ static Backtrace* Create(pid_t pid, pid_t tid, BacktraceMap* map = nullptr);
// Create an offline Backtrace object that can be used to do an unwind without a process
// that is still running. By default, information is only cached in the map
@@ -145,7 +145,7 @@
virtual ~Backtrace();
// Get the current stack trace and store in the backtrace_ structure.
- virtual bool Unwind(size_t num_ignore_frames, void* context = NULL) = 0;
+ virtual bool Unwind(size_t num_ignore_frames, void* context = nullptr) = 0;
static bool Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
@@ -160,7 +160,7 @@
// If the string is empty, then no valid function name was found,
// or the pc is not in any valid map.
virtual std::string GetFunctionName(uint64_t pc, uint64_t* offset,
- const backtrace_map_t* map = NULL);
+ const backtrace_map_t* map = nullptr);
// Fill in the map data associated with the given pc.
virtual void FillInMap(uint64_t pc, backtrace_map_t* map);
@@ -185,7 +185,7 @@
const backtrace_frame_data_t* GetFrame(size_t frame_num) {
if (frame_num >= frames_.size()) {
- return NULL;
+ return nullptr;
}
return &frames_[frame_num];
}
@@ -204,6 +204,9 @@
std::string GetErrorString(BacktraceUnwindError error);
+ // Set whether to skip frames in libbacktrace/libunwindstack when doing a local unwind.
+ void SetSkipFrames(bool skip_frames) { skip_frames_ = skip_frames; }
+
protected:
Backtrace(pid_t pid, pid_t tid, BacktraceMap* map);
@@ -223,6 +226,9 @@
std::vector<backtrace_frame_data_t> frames_;
+ // Skip frames in libbacktrace/libunwindstack when doing a local unwind.
+ bool skip_frames_ = true;
+
BacktraceUnwindError error_;
};
diff --git a/libbacktrace/include/backtrace/BacktraceMap.h b/libbacktrace/include/backtrace/BacktraceMap.h
index 473d195..c564271 100644
--- a/libbacktrace/include/backtrace/BacktraceMap.h
+++ b/libbacktrace/include/backtrace/BacktraceMap.h
@@ -31,6 +31,7 @@
#include <deque>
#include <iterator>
+#include <memory>
#include <string>
#include <vector>
@@ -40,6 +41,10 @@
// Special flag to indicate a map is in /dev/. However, a map in
// /dev/ashmem/... does not set this flag.
static constexpr int PROT_DEVICE_MAP = 0x8000;
+// Special flag to indicate that this map represents an elf file
+// created by ART for use with the gdb jit debug interface.
+// This should only ever appear in offline maps data.
+static constexpr int PROT_JIT_SYMFILE_MAP = 0x4000;
struct backtrace_map_t {
uint64_t start = 0;
@@ -165,8 +170,6 @@
virtual uint64_t GetLoadBias(size_t /* index */) { return 0; }
- virtual bool ParseLine(const char* line, backtrace_map_t* map);
-
pid_t pid_;
std::deque<backtrace_map_t> maps_;
std::vector<std::string> suffixes_to_ignore_;
diff --git a/libbacktrace/thread_utils.c b/libbacktrace/thread_utils.c
deleted file mode 100644
index e75f56e..0000000
--- a/libbacktrace/thread_utils.c
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (C) 2013 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 "thread_utils.h"
-
-#if !defined(__BIONIC__)
-
-// glibc doesn't implement or export tgkill.
-#include <unistd.h>
-#include <sys/syscall.h>
-
-int tgkill(int tgid, int tid, int sig) {
- return syscall(__NR_tgkill, tgid, tid, sig);
-}
-
-#endif
diff --git a/libbacktrace/thread_utils.h b/libbacktrace/thread_utils.h
deleted file mode 100644
index 9590657..0000000
--- a/libbacktrace/thread_utils.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#ifndef _LIBBACKTRACE_THREAD_UTILS_H
-#define _LIBBACKTRACE_THREAD_UTILS_H
-
-#include <unistd.h>
-
-#if !defined(__ANDROID__)
-#include <cutils/threads.h>
-#endif
-
-__BEGIN_DECLS
-
-int tgkill(int tgid, int tid, int sig);
-
-__END_DECLS
-
-#endif /* _LIBBACKTRACE_THREAD_UTILS_H */
diff --git a/libbinderwrapper/Android.bp b/libbinderwrapper/Android.bp
index 6fac0d8..d2487e2 100644
--- a/libbinderwrapper/Android.bp
+++ b/libbinderwrapper/Android.bp
@@ -38,6 +38,7 @@
cc_library_shared {
name: "libbinderwrapper",
defaults: ["libbinderwrapper_defaults"],
+ vendor_available: true,
srcs: [
"binder_wrapper.cc",
diff --git a/libcrypto_utils/Android.bp b/libcrypto_utils/Android.bp
index 47de12a..e47560f 100644
--- a/libcrypto_utils/Android.bp
+++ b/libcrypto_utils/Android.bp
@@ -17,6 +17,7 @@
cc_library {
name: "libcrypto_utils",
vendor_available: true,
+ recovery_available: true,
vndk: {
enabled: true,
},
diff --git a/libcutils/Android.bp b/libcutils/Android.bp
index 6d00dc6..37afb98 100644
--- a/libcutils/Android.bp
+++ b/libcutils/Android.bp
@@ -20,6 +20,7 @@
libcutils_nonwindows_sources = [
"android_get_control_file.cpp",
"fs.cpp",
+ "hashmap.cpp",
"multiuser.cpp",
"socket_inaddr_any_server_unix.cpp",
"socket_local_client_unix.cpp",
@@ -32,6 +33,7 @@
cc_library_headers {
name: "libcutils_headers",
vendor_available: true,
+ recovery_available: true,
host_supported: true,
export_include_dirs: ["include"],
target: {
@@ -54,16 +56,15 @@
enabled: true,
support_system_process: true,
},
+ recovery_available: true,
host_supported: true,
srcs: [
"config_utils.cpp",
"fs_config.cpp",
"canned_fs_config.cpp",
- "hashmap.cpp",
"iosched_policy.cpp",
"load_file.cpp",
"native_handle.cpp",
- "open_memstream.c",
"record_stream.cpp",
"sched_policy.cpp",
"sockets.cpp",
@@ -160,10 +161,20 @@
misc_undefined: ["integer"],
},
},
+
+ vendor: {
+ exclude_srcs: [
+ // qtaguid.cpp loads libnetd_client.so with dlopen(). Since
+ // the interface of libnetd_client.so may vary between AOSP
+ // releases, exclude qtaguid.cpp from the VNDK-SP variant.
+ "qtaguid.cpp",
+ ],
+ }
},
shared_libs: ["liblog"],
header_libs: [
+ "libbase_headers",
"libcutils_headers",
"libutils_headers",
],
diff --git a/libcutils/OWNERS b/libcutils/OWNERS
new file mode 100644
index 0000000..c18ed51
--- /dev/null
+++ b/libcutils/OWNERS
@@ -0,0 +1,4 @@
+cferris@google.com
+enh@google.com
+jmgao@google.com
+tomcherry@google.com
diff --git a/libcutils/ashmem-dev.cpp b/libcutils/ashmem-dev.cpp
index 15ace0e..0cc4fc0 100644
--- a/libcutils/ashmem-dev.cpp
+++ b/libcutils/ashmem-dev.cpp
@@ -90,7 +90,7 @@
dev_t rdev;
struct stat st;
- if (TEMP_FAILURE_RETRY(fstat(fd, &st)) < 0) {
+ if (fstat(fd, &st) < 0) {
return -1;
}
@@ -135,6 +135,12 @@
return -1;
}
+static int __ashmem_check_failure(int fd, int result)
+{
+ if (result == -1 && errno == ENOTTY) __ashmem_is_ashmem(fd, 1);
+ return result;
+}
+
int ashmem_valid(int fd)
{
return __ashmem_is_ashmem(fd, 0) >= 0;
@@ -182,12 +188,7 @@
int ashmem_set_prot_region(int fd, int prot)
{
- int ret = __ashmem_is_ashmem(fd, 1);
- if (ret < 0) {
- return ret;
- }
-
- return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot));
+ return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)));
}
int ashmem_pin_region(int fd, size_t offset, size_t len)
@@ -195,12 +196,7 @@
// TODO: should LP64 reject too-large offset/len?
ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
- int ret = __ashmem_is_ashmem(fd, 1);
- if (ret < 0) {
- return ret;
- }
-
- return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin));
+ return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin)));
}
int ashmem_unpin_region(int fd, size_t offset, size_t len)
@@ -208,20 +204,10 @@
// TODO: should LP64 reject too-large offset/len?
ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
- int ret = __ashmem_is_ashmem(fd, 1);
- if (ret < 0) {
- return ret;
- }
-
- return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin));
+ return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin)));
}
int ashmem_get_size_region(int fd)
{
- int ret = __ashmem_is_ashmem(fd, 1);
- if (ret < 0) {
- return ret;
- }
-
- return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL));
+ return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL)));
}
diff --git a/libcutils/ashmem-host.cpp b/libcutils/ashmem-host.cpp
index b2bec99..bb990d5 100644
--- a/libcutils/ashmem-host.cpp
+++ b/libcutils/ashmem-host.cpp
@@ -24,7 +24,6 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
-#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
diff --git a/libcutils/canned_fs_config.cpp b/libcutils/canned_fs_config.cpp
index 6b5763b..2772ef0 100644
--- a/libcutils/canned_fs_config.cpp
+++ b/libcutils/canned_fs_config.cpp
@@ -21,7 +21,6 @@
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
-#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
diff --git a/libcutils/fs_config.cpp b/libcutils/fs_config.cpp
index 5ea981f..af8f0a2 100644
--- a/libcutils/fs_config.cpp
+++ b/libcutils/fs_config.cpp
@@ -62,7 +62,7 @@
static const struct fs_path_config android_dirs[] = {
// clang-format off
{ 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
- { 00500, AID_ROOT, AID_ROOT, 0, "config" },
+ { 00555, AID_ROOT, AID_ROOT, 0, "config" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral" },
@@ -80,6 +80,7 @@
{ 00775, AID_ROOT, AID_ROOT, 0, "data/preloads" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
{ 00755, AID_ROOT, AID_SYSTEM, 0, "mnt" },
+ { 00755, AID_ROOT, AID_SHELL, 0, "product/bin" },
{ 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
{ 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
{ 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" },
@@ -122,8 +123,14 @@
{odm_conf_file, odm_conf_dir},
};
+// Do not use android_files to grant Linux capabilities. Use ambient capabilities in their
+// associated init.rc file instead. See https://source.android.com/devices/tech/config/ambient.
+
+// Do not place any new vendor/, data/vendor/, etc entries in android_files.
+// Vendor entries should be done via a vendor or device specific config.fs.
+// See https://source.android.com/devices/tech/config/filesystem#using-file-system-capabilities
static const struct fs_path_config android_files[] = {
- // clang-format off
+ // clang-format off
{ 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
{ 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral/*" },
{ 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
@@ -142,11 +149,13 @@
{ 00444, AID_ROOT, AID_ROOT, 0, oem_conf_dir + 1 },
{ 00444, AID_ROOT, AID_ROOT, 0, oem_conf_file + 1 },
{ 00600, AID_ROOT, AID_ROOT, 0, "product/build.prop" },
+ { 00600, AID_ROOT, AID_ROOT, 0, "product-services/build.prop" },
{ 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump32" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump64" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/bin/debuggerd" },
{ 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
+ { 00550, AID_LOGD, AID_LOGD, 0, "system/bin/logd" },
{ 00700, AID_ROOT, AID_ROOT, 0, "system/bin/secilc" },
{ 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
{ 00600, AID_ROOT, AID_ROOT, 0, "system/build.prop" },
@@ -172,12 +181,6 @@
// in user builds.
{ 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND),
"system/bin/inputflinger" },
- { 00550, AID_LOGD, AID_LOGD, CAP_MASK_LONG(CAP_SYSLOG) |
- CAP_MASK_LONG(CAP_AUDIT_CONTROL) |
- CAP_MASK_LONG(CAP_SETGID),
- "system/bin/logd" },
- { 00550, AID_SYSTEM, AID_LOG, CAP_MASK_LONG(CAP_SYSLOG),
- "system/bin/bootstat" },
{ 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
CAP_MASK_LONG(CAP_SETGID),
"system/bin/run-as" },
@@ -185,27 +188,11 @@
// Support FIFO scheduling mode in SurfaceFlinger.
{ 00755, AID_SYSTEM, AID_GRAPHICS, CAP_MASK_LONG(CAP_SYS_NICE),
"system/bin/surfaceflinger" },
-
- // Support hostapd administering a network interface.
- { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
- CAP_MASK_LONG(CAP_NET_RAW),
- "vendor/bin/hostapd" },
-
- // Support Bluetooth legacy hal accessing /sys/class/rfkill
- // Support RT scheduling in Bluetooth
- { 00700, AID_BLUETOOTH, AID_BLUETOOTH, CAP_MASK_LONG(CAP_NET_ADMIN) |
- CAP_MASK_LONG(CAP_SYS_NICE),
- "vendor/bin/hw/android.hardware.bluetooth@1.0-service" },
-
- // Support wifi_hal_legacy administering a network interface.
- { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
- CAP_MASK_LONG(CAP_NET_RAW),
- "vendor/bin/hw/android.hardware.wifi@1.0-service" },
-
// generic defaults
{ 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
{ 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
{ 00750, AID_ROOT, AID_SHELL, 0, "init*" },
+ { 00755, AID_ROOT, AID_SHELL, 0, "product/bin/*" },
{ 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
{ 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
@@ -214,7 +201,7 @@
{ 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
{ 00755, AID_ROOT, AID_SHELL, 0, "vendor/xbin/*" },
{ 00644, AID_ROOT, AID_ROOT, 0, 0 },
- // clang-format on
+ // clang-format on
};
#ifndef __ANDROID_VNDK__
auto __for_testing_only__android_files = android_files;
@@ -248,9 +235,10 @@
return fd;
}
-// if path is "vendor/<stuff>", "oem/<stuff>" or "odm/<stuff>"
+// if path is "odm/<stuff>", "oem/<stuff>", "product/<stuff>" or
+// "vendor/<stuff>"
static bool is_partition(const char* path, size_t len) {
- static const char* partitions[] = {"vendor/", "oem/", "odm/"};
+ static const char* partitions[] = {"odm/", "oem/", "product/", "vendor/"};
for (size_t i = 0; i < (sizeof(partitions) / sizeof(partitions[0])); ++i) {
size_t plen = strlen(partitions[i]);
if (len <= plen) continue;
diff --git a/libcutils/hashmap.cpp b/libcutils/hashmap.cpp
index 65b6ab1..57d6006 100644
--- a/libcutils/hashmap.cpp
+++ b/libcutils/hashmap.cpp
@@ -18,10 +18,9 @@
#include <assert.h>
#include <errno.h>
-#include <cutils/threads.h>
+#include <pthread.h>
#include <stdlib.h>
#include <string.h>
-#include <stdbool.h>
#include <sys/types.h>
typedef struct Entry Entry;
@@ -37,7 +36,7 @@
size_t bucketCount;
int (*hash)(void* key);
bool (*equals)(void* keyA, void* keyB);
- mutex_t lock;
+ pthread_mutex_t lock;
size_t size;
};
@@ -45,18 +44,18 @@
int (*hash)(void* key), bool (*equals)(void* keyA, void* keyB)) {
assert(hash != NULL);
assert(equals != NULL);
-
+
Hashmap* map = static_cast<Hashmap*>(malloc(sizeof(Hashmap)));
if (map == NULL) {
return NULL;
}
-
+
// 0.75 load factor.
size_t minimumBucketCount = initialCapacity * 4 / 3;
map->bucketCount = 1;
while (map->bucketCount <= minimumBucketCount) {
// Bucket count must be power of 2.
- map->bucketCount <<= 1;
+ map->bucketCount <<= 1;
}
map->buckets = static_cast<Entry**>(calloc(map->bucketCount, sizeof(Entry*)));
@@ -64,14 +63,14 @@
free(map);
return NULL;
}
-
+
map->size = 0;
map->hash = hash;
map->equals = equals;
-
- mutex_init(&map->lock);
-
+
+ pthread_mutex_init(&map->lock, nullptr);
+
return map;
}
@@ -90,12 +89,8 @@
h ^= (((unsigned int) h) >> 14);
h += (h << 4);
h ^= (((unsigned int) h) >> 10);
-
- return h;
-}
-size_t hashmapSize(Hashmap* map) {
- return map->size;
+ return h;
}
static inline size_t calculateIndex(size_t bucketCount, int hash) {
@@ -112,7 +107,7 @@
// Abort expansion.
return;
}
-
+
// Move over existing entries.
size_t i;
for (i = 0; i < map->bucketCount; i++) {
@@ -134,11 +129,11 @@
}
void hashmapLock(Hashmap* map) {
- mutex_lock(&map->lock);
+ pthread_mutex_lock(&map->lock);
}
void hashmapUnlock(Hashmap* map) {
- mutex_unlock(&map->lock);
+ pthread_mutex_unlock(&map->lock);
}
void hashmapFree(Hashmap* map) {
@@ -152,7 +147,7 @@
}
}
free(map->buckets);
- mutex_destroy(&map->lock);
+ pthread_mutex_destroy(&map->lock);
free(map);
}
@@ -241,54 +236,6 @@
return NULL;
}
-bool hashmapContainsKey(Hashmap* map, void* key) {
- int hash = hashKey(map, key);
- size_t index = calculateIndex(map->bucketCount, hash);
-
- Entry* entry = map->buckets[index];
- while (entry != NULL) {
- if (equalKeys(entry->key, entry->hash, key, hash, map->equals)) {
- return true;
- }
- entry = entry->next;
- }
-
- return false;
-}
-
-void* hashmapMemoize(Hashmap* map, void* key,
- void* (*initialValue)(void* key, void* context), void* context) {
- int hash = hashKey(map, key);
- size_t index = calculateIndex(map->bucketCount, hash);
-
- Entry** p = &(map->buckets[index]);
- while (true) {
- Entry* current = *p;
-
- // Add a new entry.
- if (current == NULL) {
- *p = createEntry(key, hash, NULL);
- if (*p == NULL) {
- errno = ENOMEM;
- return NULL;
- }
- void* value = initialValue(key, context);
- (*p)->value = value;
- map->size++;
- expandIfNecessary(map);
- return value;
- }
-
- // Return existing value.
- if (equalKeys(current->key, current->hash, key, hash, map->equals)) {
- return current->value;
- }
-
- // Move to next entry.
- p = ¤t->next;
- }
-}
-
void* hashmapRemove(Hashmap* map, void* key) {
int hash = hashKey(map, key);
size_t index = calculateIndex(map->bucketCount, hash);
@@ -311,9 +258,8 @@
return NULL;
}
-void hashmapForEach(Hashmap* map,
- bool (*callback)(void* key, void* value, void* context),
- void* context) {
+void hashmapForEach(Hashmap* map, bool (*callback)(void* key, void* value, void* context),
+ void* context) {
size_t i;
for (i = 0; i < map->bucketCount; i++) {
Entry* entry = map->buckets[i];
@@ -326,34 +272,3 @@
}
}
}
-
-size_t hashmapCurrentCapacity(Hashmap* map) {
- size_t bucketCount = map->bucketCount;
- return bucketCount * 3 / 4;
-}
-
-size_t hashmapCountCollisions(Hashmap* map) {
- size_t collisions = 0;
- size_t i;
- for (i = 0; i < map->bucketCount; i++) {
- Entry* entry = map->buckets[i];
- while (entry != NULL) {
- if (entry->next != NULL) {
- collisions++;
- }
- entry = entry->next;
- }
- }
- return collisions;
-}
-
-int hashmapIntHash(void* key) {
- // Return the key value itself.
- return *((int*) key);
-}
-
-bool hashmapIntEquals(void* keyA, void* keyB) {
- int a = *((int*) keyA);
- int b = *((int*) keyB);
- return a == b;
-}
diff --git a/libcutils/include/cutils/hashmap.h b/libcutils/include/cutils/hashmap.h
index 5cb344c..9cfd669 100644
--- a/libcutils/include/cutils/hashmap.h
+++ b/libcutils/include/cutils/hashmap.h
@@ -16,6 +16,9 @@
/**
* Hash map.
+ *
+ * Use std::map or std::unordered_map instead.
+ * https://en.cppreference.com/w/cpp/container
*/
#ifndef __HASHMAP_H
@@ -68,38 +71,17 @@
void* hashmapGet(Hashmap* map, void* key);
/**
- * Returns true if the map contains an entry for the given key.
- */
-bool hashmapContainsKey(Hashmap* map, void* key);
-
-/**
- * Gets the value for a key. If a value is not found, this function gets a
- * value and creates an entry using the given callback.
- *
- * If memory allocation fails, the callback is not called, this function
- * returns NULL, and errno is set to ENOMEM.
- */
-void* hashmapMemoize(Hashmap* map, void* key,
- void* (*initialValue)(void* key, void* context), void* context);
-
-/**
* Removes an entry from the map. Returns the removed value or NULL if no
* entry was present.
*/
void* hashmapRemove(Hashmap* map, void* key);
/**
- * Gets the number of entries in this map.
- */
-size_t hashmapSize(Hashmap* map);
-
-/**
* Invokes the given callback on each entry in the map. Stops iterating if
* the callback returns false.
*/
-void hashmapForEach(Hashmap* map,
- bool (*callback)(void* key, void* value, void* context),
- void* context);
+void hashmapForEach(Hashmap* map, bool (*callback)(void* key, void* value, void* context),
+ void* context);
/**
* Concurrency support.
@@ -115,36 +97,8 @@
*/
void hashmapUnlock(Hashmap* map);
-/**
- * Key utilities.
- */
-
-/**
- * Hashes int keys. 'key' is a pointer to int.
- */
-int hashmapIntHash(void* key);
-
-/**
- * Compares two int keys for equality.
- */
-bool hashmapIntEquals(void* keyA, void* keyB);
-
-/**
- * For debugging.
- */
-
-/**
- * Gets current capacity.
- */
-size_t hashmapCurrentCapacity(Hashmap* map);
-
-/**
- * Counts the number of entry collisions.
- */
-size_t hashmapCountCollisions(Hashmap* map);
-
#ifdef __cplusplus
}
#endif
-#endif /* __HASHMAP_H */
+#endif /* __HASHMAP_H */
diff --git a/libcutils/include/cutils/sched_policy.h b/libcutils/include/cutils/sched_policy.h
index 4c1113b..cf91b76 100644
--- a/libcutils/include/cutils/sched_policy.h
+++ b/libcutils/include/cutils/sched_policy.h
@@ -40,16 +40,17 @@
/* Keep in sync with THREAD_GROUP_* in frameworks/base/core/java/android/os/Process.java */
typedef enum {
- SP_DEFAULT = -1,
+ SP_DEFAULT = -1,
SP_BACKGROUND = 0,
SP_FOREGROUND = 1,
- SP_SYSTEM = 2, // can't be used with set_sched_policy()
- SP_AUDIO_APP = 3,
- SP_AUDIO_SYS = 4,
- SP_TOP_APP = 5,
- SP_RT_APP = 6,
+ SP_SYSTEM = 2, // can't be used with set_sched_policy()
+ SP_AUDIO_APP = 3,
+ SP_AUDIO_SYS = 4,
+ SP_TOP_APP = 5,
+ SP_RT_APP = 6,
+ SP_RESTRICTED = 7,
SP_CNT,
- SP_MAX = SP_CNT - 1,
+ SP_MAX = SP_CNT - 1,
SP_SYSTEM_DEFAULT = SP_FOREGROUND,
} SchedPolicy;
diff --git a/libcutils/include/cutils/threads.h b/libcutils/include/cutils/threads.h
index 5727494..ba4846e 100644
--- a/libcutils/include/cutils/threads.h
+++ b/libcutils/include/cutils/threads.h
@@ -29,16 +29,16 @@
extern "C" {
#endif
-/***********************************************************************/
-/***********************************************************************/
-/***** *****/
-/***** local thread storage *****/
-/***** *****/
-/***********************************************************************/
-/***********************************************************************/
+//
+// Deprecated: use android::base::GetThreadId instead, which doesn't truncate on Mac/Windows.
+//
extern pid_t gettid();
+//
+// Deprecated: use `_Thread_local` in C or `thread_local` in C++.
+//
+
#if !defined(_WIN32)
typedef struct {
@@ -70,77 +70,6 @@
void* value,
thread_store_destruct_t destroy);
-/***********************************************************************/
-/***********************************************************************/
-/***** *****/
-/***** mutexes *****/
-/***** *****/
-/***********************************************************************/
-/***********************************************************************/
-
-#if !defined(_WIN32)
-
-typedef pthread_mutex_t mutex_t;
-
-#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
-
-static __inline__ void mutex_lock(mutex_t* lock)
-{
- pthread_mutex_lock(lock);
-}
-static __inline__ void mutex_unlock(mutex_t* lock)
-{
- pthread_mutex_unlock(lock);
-}
-static __inline__ int mutex_init(mutex_t* lock)
-{
- return pthread_mutex_init(lock, NULL);
-}
-static __inline__ void mutex_destroy(mutex_t* lock)
-{
- pthread_mutex_destroy(lock);
-}
-
-#else // !defined(_WIN32)
-
-typedef struct {
- int init;
- CRITICAL_SECTION lock[1];
-} mutex_t;
-
-#define MUTEX_INITIALIZER { 0, {{ NULL, 0, 0, NULL, NULL, 0 }} }
-
-static __inline__ void mutex_lock(mutex_t* lock)
-{
- if (!lock->init) {
- lock->init = 1;
- InitializeCriticalSection( lock->lock );
- lock->init = 2;
- } else while (lock->init != 2)
- Sleep(10);
-
- EnterCriticalSection(lock->lock);
-}
-
-static __inline__ void mutex_unlock(mutex_t* lock)
-{
- LeaveCriticalSection(lock->lock);
-}
-static __inline__ int mutex_init(mutex_t* lock)
-{
- InitializeCriticalSection(lock->lock);
- lock->init = 2;
- return 0;
-}
-static __inline__ void mutex_destroy(mutex_t* lock)
-{
- if (lock->init) {
- lock->init = 0;
- DeleteCriticalSection(lock->lock);
- }
-}
-#endif // !defined(_WIN32)
-
#ifdef __cplusplus
}
#endif
diff --git a/libcutils/include/cutils/trace.h b/libcutils/include/cutils/trace.h
index b2779b2..58b9f09 100644
--- a/libcutils/include/cutils/trace.h
+++ b/libcutils/include/cutils/trace.h
@@ -73,7 +73,9 @@
#define ATRACE_TAG_NETWORK (1<<21)
#define ATRACE_TAG_ADB (1<<22)
#define ATRACE_TAG_VIBRATOR (1<<23)
-#define ATRACE_TAG_LAST ATRACE_TAG_VIBRATOR
+#define ATRACE_TAG_AIDL (1<<24)
+#define ATRACE_TAG_NNAPI (1<<25)
+#define ATRACE_TAG_LAST ATRACE_TAG_NNAPI
// Reserved for initialization.
#define ATRACE_TAG_NOT_READY (1ULL<<63)
diff --git a/libcutils/include/private/android_filesystem_config.h b/libcutils/include/private/android_filesystem_config.h
index 5d17698..3be8ad0 100644
--- a/libcutils/include/private/android_filesystem_config.h
+++ b/libcutils/include/private/android_filesystem_config.h
@@ -129,6 +129,8 @@
#define AID_STATSD 1066 /* statsd daemon */
#define AID_INCIDENTD 1067 /* incidentd daemon */
#define AID_SECURE_ELEMENT 1068 /* secure element subsystem */
+#define AID_LMKD 1069 /* low memory killer daemon */
+#define AID_LLKD 1070 /* live lock daemon */
/* Changes to this file must be made in AOSP, *not* in internal branches. */
#define AID_SHELL 2000 /* adb and debug shell user */
diff --git a/libcutils/include_vndk/cutils/open_memstream.h b/libcutils/include_vndk/cutils/open_memstream.h
deleted file mode 120000
index c894084..0000000
--- a/libcutils/include_vndk/cutils/open_memstream.h
+++ /dev/null
@@ -1 +0,0 @@
-../../include/cutils/open_memstream.h
\ No newline at end of file
diff --git a/libcutils/open_memstream.c b/libcutils/open_memstream.c
deleted file mode 100644
index 9183266..0000000
--- a/libcutils/open_memstream.c
+++ /dev/null
@@ -1,373 +0,0 @@
-/*
- * Copyright (C) 2010 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.
- */
-
-#if defined(__APPLE__)
-
-/*
- * Implementation of the POSIX open_memstream() function, which Linux has
- * but BSD lacks.
- *
- * Summary:
- * - Works like a file-backed FILE* opened with fopen(name, "w"), but the
- * backing is a chunk of memory rather than a file.
- * - The buffer expands as you write more data. Seeking past the end
- * of the file and then writing to it zero-fills the gap.
- * - The values at "*bufp" and "*sizep" should be considered read-only,
- * and are only valid immediately after an fflush() or fclose().
- * - A '\0' is maintained just past the end of the file. This is not included
- * in "*sizep". (The behavior w.r.t. fseek() is not clearly defined.
- * The spec says the null byte is written when a write() advances EOF,
- * but it looks like glibc ensures the null byte is always found at EOF,
- * even if you just seeked backwards. The example on the opengroup.org
- * page suggests that this is the expected behavior. The null must be
- * present after a no-op fflush(), which we can't see, so we have to save
- * and restore it. Annoying, but allows file truncation.)
- * - After fclose(), the caller must eventually free(*bufp).
- *
- * This is built out of funopen(), which BSD has but Linux lacks. There is
- * no flush() operator, so we need to keep the user pointers up to date
- * after each operation.
- *
- * I don't think Windows has any of the above, but we don't need to use
- * them there, so we just supply a stub.
- */
-#include <cutils/open_memstream.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include <assert.h>
-
-#if 0
-# define DBUG(x) printf x
-#else
-# define DBUG(x) ((void)0)
-#endif
-
-/*
- * Definition of a seekable, write-only memory stream.
- */
-typedef struct {
- char** bufp; /* pointer to buffer pointer */
- size_t* sizep; /* pointer to eof */
-
- size_t allocSize; /* size of buffer */
- size_t eof; /* furthest point we've written to */
- size_t offset; /* current write offset */
- char saved; /* required by NUL handling */
-} MemStream;
-
-#define kInitialSize 1024
-
-/*
- * Ensure that we have enough storage to write "size" bytes at the
- * current offset. We also have to take into account the extra '\0'
- * that we maintain just past EOF.
- *
- * Returns 0 on success.
- */
-static int ensureCapacity(MemStream* stream, int writeSize)
-{
- DBUG(("+++ ensureCap off=%d size=%d\n", stream->offset, writeSize));
-
- size_t neededSize = stream->offset + writeSize + 1;
- if (neededSize <= stream->allocSize)
- return 0;
-
- size_t newSize;
-
- if (stream->allocSize == 0) {
- newSize = kInitialSize;
- } else {
- newSize = stream->allocSize;
- newSize += newSize / 2; /* expand by 3/2 */
- }
-
- if (newSize < neededSize)
- newSize = neededSize;
- DBUG(("+++ realloc %p->%p to size=%d\n",
- stream->bufp, *stream->bufp, newSize));
- char* newBuf = (char*) realloc(*stream->bufp, newSize);
- if (newBuf == NULL)
- return -1;
-
- *stream->bufp = newBuf;
- stream->allocSize = newSize;
- return 0;
-}
-
-/*
- * Write data to a memstream, expanding the buffer if necessary.
- *
- * If we previously seeked beyond EOF, zero-fill the gap.
- *
- * Returns the number of bytes written.
- */
-static int write_memstream(void* cookie, const char* buf, int size)
-{
- MemStream* stream = (MemStream*) cookie;
-
- if (ensureCapacity(stream, size) < 0)
- return -1;
-
- /* seeked past EOF earlier? */
- if (stream->eof < stream->offset) {
- DBUG(("+++ zero-fill gap from %d to %d\n",
- stream->eof, stream->offset-1));
- memset(*stream->bufp + stream->eof, '\0',
- stream->offset - stream->eof);
- }
-
- /* copy data, advance write pointer */
- memcpy(*stream->bufp + stream->offset, buf, size);
- stream->offset += size;
-
- if (stream->offset > stream->eof) {
- /* EOF has advanced, update it and append null byte */
- DBUG(("+++ EOF advanced to %d, appending nul\n", stream->offset));
- assert(stream->offset < stream->allocSize);
- stream->eof = stream->offset;
- } else {
- /* within previously-written area; save char we're about to stomp */
- DBUG(("+++ within written area, saving '%c' at %d\n",
- *(*stream->bufp + stream->offset), stream->offset));
- stream->saved = *(*stream->bufp + stream->offset);
- }
- *(*stream->bufp + stream->offset) = '\0';
- *stream->sizep = stream->offset;
-
- return size;
-}
-
-/*
- * Seek within a memstream.
- *
- * Returns the new offset, or -1 on failure.
- */
-static fpos_t seek_memstream(void* cookie, fpos_t offset, int whence)
-{
- MemStream* stream = (MemStream*) cookie;
- off_t newPosn = (off_t) offset;
-
- if (whence == SEEK_CUR) {
- newPosn += stream->offset;
- } else if (whence == SEEK_END) {
- newPosn += stream->eof;
- }
-
- if (newPosn < 0 || ((fpos_t)((size_t) newPosn)) != newPosn) {
- /* bad offset - negative or huge */
- DBUG(("+++ bogus seek offset %ld\n", (long) newPosn));
- errno = EINVAL;
- return (fpos_t) -1;
- }
-
- if (stream->offset < stream->eof) {
- /*
- * We were pointing to an area we'd already written to, which means
- * we stomped on a character and must now restore it.
- */
- DBUG(("+++ restoring char '%c' at %d\n",
- stream->saved, stream->offset));
- *(*stream->bufp + stream->offset) = stream->saved;
- }
-
- stream->offset = (size_t) newPosn;
-
- if (stream->offset < stream->eof) {
- /*
- * We're seeked backward into the stream. Preserve the character
- * at EOF and stomp it with a NUL.
- */
- stream->saved = *(*stream->bufp + stream->offset);
- *(*stream->bufp + stream->offset) = '\0';
- *stream->sizep = stream->offset;
- } else {
- /*
- * We're positioned at, or possibly beyond, the EOF. We want to
- * publish the current EOF, not the current position.
- */
- *stream->sizep = stream->eof;
- }
-
- return newPosn;
-}
-
-/*
- * Close the memstream. We free everything but the data buffer.
- */
-static int close_memstream(void* cookie)
-{
- free(cookie);
- return 0;
-}
-
-/*
- * Prepare a memstream.
- */
-FILE* open_memstream(char** bufp, size_t* sizep)
-{
- FILE* fp;
- MemStream* stream;
-
- if (bufp == NULL || sizep == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- stream = (MemStream*) calloc(1, sizeof(MemStream));
- if (stream == NULL)
- return NULL;
-
- fp = funopen(stream,
- NULL, write_memstream, seek_memstream, close_memstream);
- if (fp == NULL) {
- free(stream);
- return NULL;
- }
-
- *sizep = 0;
- *bufp = NULL;
- stream->bufp = bufp;
- stream->sizep = sizep;
-
- return fp;
-}
-
-
-
-
-#if 0
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-/*
- * Simple regression test.
- *
- * To test on desktop Linux with valgrind, it's possible to make a simple
- * change to open_memstream() to use fopencookie instead:
- *
- * cookie_io_functions_t iofuncs =
- * { NULL, write_memstream, seek_memstream, close_memstream };
- * fp = fopencookie(stream, "w", iofuncs);
- *
- * (Some tweaks to seek_memstream are also required, as that takes a
- * pointer to an offset rather than an offset, and returns 0 or -1.)
- */
-int testMemStream(void)
-{
- FILE *stream;
- char *buf;
- size_t len;
- off_t eob;
-
- printf("Test1\n");
-
- /* std example */
- stream = open_memstream(&buf, &len);
- fprintf(stream, "hello my world");
- fflush(stream);
- printf("buf=%s, len=%zu\n", buf, len);
- eob = ftello(stream);
- fseeko(stream, 0, SEEK_SET);
- fprintf(stream, "good-bye");
- fseeko(stream, eob, SEEK_SET);
- fclose(stream);
- printf("buf=%s, len=%zu\n", buf, len);
- free(buf);
-
- printf("Test2\n");
-
- /* std example without final seek-to-end */
- stream = open_memstream(&buf, &len);
- fprintf(stream, "hello my world");
- fflush(stream);
- printf("buf=%s, len=%zu\n", buf, len);
- eob = ftello(stream);
- fseeko(stream, 0, SEEK_SET);
- fprintf(stream, "good-bye");
- //fseeko(stream, eob, SEEK_SET);
- fclose(stream);
- printf("buf=%s, len=%zu\n", buf, len);
- free(buf);
-
- printf("Test3\n");
-
- /* fancy example; should expand buffer with writes */
- static const int kCmpLen = 1024 + 128;
- char* cmp = malloc(kCmpLen);
- memset(cmp, 0, 1024);
- memset(cmp+1024, 0xff, kCmpLen-1024);
- sprintf(cmp, "This-is-a-tes1234");
- sprintf(cmp + 1022, "abcdef");
-
- stream = open_memstream (&buf, &len);
- setvbuf(stream, NULL, _IONBF, 0); /* note: crashes in glibc with this */
- fprintf(stream, "This-is-a-test");
- fseek(stream, -1, SEEK_CUR); /* broken in glibc; can use {13,SEEK_SET} */
- fprintf(stream, "1234");
- fseek(stream, 1022, SEEK_SET);
- fputc('a', stream);
- fputc('b', stream);
- fputc('c', stream);
- fputc('d', stream);
- fputc('e', stream);
- fputc('f', stream);
- fflush(stream);
-
- if (memcmp(buf, cmp, len+1) != 0) {
- printf("mismatch\n");
- } else {
- printf("match\n");
- }
-
- printf("Test4\n");
- stream = open_memstream (&buf, &len);
- fseek(stream, 5000, SEEK_SET);
- fseek(stream, 4096, SEEK_SET);
- fseek(stream, -1, SEEK_SET); /* should have no effect */
- fputc('x', stream);
- if (ftell(stream) == 4097)
- printf("good\n");
- else
- printf("BAD: offset is %ld\n", ftell(stream));
-
- printf("DONE\n");
-
- return 0;
-}
-
-/* expected output:
-Test1
-buf=hello my world, len=14
-buf=good-bye world, len=14
-Test2
-buf=hello my world, len=14
-buf=good-bye, len=8
-Test3
-match
-Test4
-good
-DONE
-*/
-
-#endif
-
-#endif /* __APPLE__ */
diff --git a/libcutils/sched_policy.cpp b/libcutils/sched_policy.cpp
index f5ce82f..3fa548f 100644
--- a/libcutils/sched_policy.cpp
+++ b/libcutils/sched_policy.cpp
@@ -25,6 +25,7 @@
#include <string.h>
#include <unistd.h>
+#include <android-base/macros.h>
#include <log/log.h>
/* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged.
@@ -57,6 +58,7 @@
static int bg_cpuset_fd = -1;
static int fg_cpuset_fd = -1;
static int ta_cpuset_fd = -1; // special cpuset for top app
+static int rs_cpuset_fd = -1; // special cpuset for screen off restrictions
// File descriptors open to /dev/stune/../tasks, setup by initialize, or -1 on error
static int bg_schedboost_fd = -1;
@@ -151,6 +153,8 @@
system_bg_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC);
filename = "/dev/cpuset/top-app/tasks";
ta_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC);
+ filename = "/dev/cpuset/restricted/tasks";
+ rs_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC);
if (schedboost_enabled()) {
filename = "/dev/stune/top-app/tasks";
@@ -308,6 +312,9 @@
case SP_SYSTEM:
fd = system_bg_cpuset_fd;
break;
+ case SP_RESTRICTED:
+ fd = rs_cpuset_fd;
+ break;
default:
boost_fd = fd = -1;
break;
@@ -454,20 +461,16 @@
#endif
-const char *get_sched_policy_name(SchedPolicy policy)
-{
+const char* get_sched_policy_name(SchedPolicy policy) {
policy = _policy(policy);
- static const char * const strings[SP_CNT] = {
- [SP_BACKGROUND] = "bg",
- [SP_FOREGROUND] = "fg",
- [SP_SYSTEM] = " ",
- [SP_AUDIO_APP] = "aa",
- [SP_AUDIO_SYS] = "as",
- [SP_TOP_APP] = "ta",
- [SP_RT_APP] = "rt",
+ static const char* const kSchedPolicyNames[] = {
+ [SP_BACKGROUND] = "bg", [SP_FOREGROUND] = "fg", [SP_SYSTEM] = " ",
+ [SP_AUDIO_APP] = "aa", [SP_AUDIO_SYS] = "as", [SP_TOP_APP] = "ta",
+ [SP_RT_APP] = "rt", [SP_RESTRICTED] = "rs",
};
- if ((policy < SP_CNT) && (strings[policy] != NULL))
- return strings[policy];
- else
+ static_assert(arraysize(kSchedPolicyNames) == SP_CNT, "missing name");
+ if (policy < SP_BACKGROUND || policy >= SP_CNT) {
return "error";
+ }
+ return kSchedPolicyNames[policy];
}
diff --git a/libcutils/sockets_unix.cpp b/libcutils/sockets_unix.cpp
index 2849aa8..0cb8a4d 100644
--- a/libcutils/sockets_unix.cpp
+++ b/libcutils/sockets_unix.cpp
@@ -32,10 +32,6 @@
#include "android_get_control_env.h"
-#ifndef TEMP_FAILURE_RETRY
-#define TEMP_FAILURE_RETRY(exp) (exp) // KISS implementation
-#endif
-
#if defined(__ANDROID__)
/* For the socket trust (credentials) check */
#include <private/android_filesystem_config.h>
@@ -102,15 +98,15 @@
// Compare to UNIX domain socket name, must match!
struct sockaddr_un addr;
socklen_t addrlen = sizeof(addr);
- int ret = TEMP_FAILURE_RETRY(getsockname(fd, (struct sockaddr *)&addr, &addrlen));
+ int ret = getsockname(fd, (struct sockaddr*)&addr, &addrlen);
if (ret < 0) return -1;
- char *path = NULL;
- if (asprintf(&path, ANDROID_SOCKET_DIR "/%s", name) < 0) return -1;
- if (!path) return -1;
- int cmp = strcmp(addr.sun_path, path);
- free(path);
- if (cmp != 0) return -1;
- // It is what we think it is
- return fd;
+ constexpr char prefix[] = ANDROID_SOCKET_DIR "/";
+ constexpr size_t prefix_size = sizeof(prefix) - sizeof('\0');
+ if ((strncmp(addr.sun_path, prefix, prefix_size) == 0) &&
+ (strcmp(addr.sun_path + prefix_size, name) == 0)) {
+ // It is what we think it is
+ return fd;
+ }
+ return -1;
}
diff --git a/libcutils/str_parms.cpp b/libcutils/str_parms.cpp
index f5a52a7..d818c51 100644
--- a/libcutils/str_parms.cpp
+++ b/libcutils/str_parms.cpp
@@ -354,12 +354,8 @@
char *str_parms_to_str(struct str_parms *str_parms)
{
char *str = NULL;
-
- if (hashmapSize(str_parms->map) > 0)
- hashmapForEach(str_parms->map, combine_strings, &str);
- else
- str = strdup("");
- return str;
+ hashmapForEach(str_parms->map, combine_strings, &str);
+ return (str != NULL) ? str : strdup("");
}
static bool dump_entry(void* key, void* value, void* /*context*/) {
diff --git a/libcutils/tests/sched_policy_test.cpp b/libcutils/tests/sched_policy_test.cpp
index 173174a..5942ee5 100644
--- a/libcutils/tests/sched_policy_test.cpp
+++ b/libcutils/tests/sched_policy_test.cpp
@@ -60,6 +60,12 @@
return sleepTimes[median];
}
+static void AssertPolicy(SchedPolicy expected_policy) {
+ SchedPolicy current_policy;
+ ASSERT_EQ(0, get_sched_policy(0, ¤t_policy));
+ EXPECT_EQ(expected_policy, current_policy);
+}
+
TEST(SchedPolicy, set_sched_policy) {
if (!hasCapSysNice()) {
GTEST_LOG_(INFO) << "skipping test that requires CAP_SYS_NICE";
@@ -76,23 +82,17 @@
const unsigned int BG_FG_SLACK_FACTOR = 100;
ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND));
+ AssertPolicy(SP_BACKGROUND);
auto bgSleepTime = medianSleepTime();
ASSERT_EQ(0, set_sched_policy(0, SP_FOREGROUND));
+ AssertPolicy(SP_FOREGROUND);
auto fgSleepTime = medianSleepTime();
ASSERT_GT(bgSleepTime, fgSleepTime * BG_FG_SLACK_FACTOR);
}
-TEST(SchedPolicy, get_sched_policy) {
- SchedPolicy policy;
- ASSERT_EQ(0, get_sched_policy(0, &policy));
-
- const char *policyName = get_sched_policy_name(policy);
- EXPECT_NE(nullptr, policyName);
- EXPECT_STRNE("error", policyName);
-
- ASSERT_EQ(0, set_sched_policy(0, SP_BACKGROUND));
- SchedPolicy newPolicy;
- ASSERT_EQ(0, get_sched_policy(0, &newPolicy));
- EXPECT_EQ(SP_BACKGROUND, newPolicy);
+TEST(SchedPolicy, get_sched_policy_name) {
+ EXPECT_STREQ("bg", get_sched_policy_name(SP_BACKGROUND));
+ EXPECT_STREQ("error", get_sched_policy_name(SchedPolicy(-2)));
+ EXPECT_STREQ("error", get_sched_policy_name(SP_CNT));
}
diff --git a/libcutils/trace-dev.inc b/libcutils/trace-dev.inc
index f95c6c5..c9580af 100644
--- a/libcutils/trace-dev.inc
+++ b/libcutils/trace-dev.inc
@@ -24,7 +24,6 @@
#include <limits.h>
#include <pthread.h>
#include <stdatomic.h>
-#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
diff --git a/libion/ion.c b/libion/ion.c
index 5836128..b8de5a4 100644
--- a/libion/ion.c
+++ b/libion/ion.c
@@ -55,7 +55,7 @@
int ion_open() {
int fd = open("/dev/ion", O_RDONLY | O_CLOEXEC);
- if (fd < 0) ALOGE("open /dev/ion failed!\n");
+ if (fd < 0) ALOGE("open /dev/ion failed: %s", strerror(errno));
return fd;
}
@@ -69,7 +69,7 @@
static int ion_ioctl(int fd, int req, void* arg) {
int ret = ioctl(fd, req, arg);
if (ret < 0) {
- ALOGE("ioctl %x failed with code %d: %s\n", req, ret, strerror(errno));
+ ALOGE("ioctl %x failed with code %d: %s", req, ret, strerror(errno));
return -errno;
}
return ret;
@@ -115,12 +115,12 @@
ret = ion_ioctl(fd, ION_IOC_MAP, &data);
if (ret < 0) return ret;
if (data.fd < 0) {
- ALOGE("map ioctl returned negative fd\n");
+ ALOGE("map ioctl returned negative fd");
return -EINVAL;
}
tmp_ptr = mmap(NULL, length, prot, flags, data.fd, offset);
if (tmp_ptr == MAP_FAILED) {
- ALOGE("mmap failed: %s\n", strerror(errno));
+ ALOGE("mmap failed: %s", strerror(errno));
return -errno;
}
*map_fd = data.fd;
@@ -140,7 +140,7 @@
ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
if (ret < 0) return ret;
if (data.fd < 0) {
- ALOGE("share ioctl returned negative fd\n");
+ ALOGE("share ioctl returned negative fd");
return -EINVAL;
}
*share_fd = data.fd;
diff --git a/libkeyutils/Android.bp b/libkeyutils/Android.bp
index 0285259..b388e95 100644
--- a/libkeyutils/Android.bp
+++ b/libkeyutils/Android.bp
@@ -2,6 +2,7 @@
name: "libkeyutils",
cflags: ["-Werror"],
defaults: ["linux_bionic_supported"],
+ recovery_available: true,
export_include_dirs: ["include/"],
local_include_dirs: ["include/"],
srcs: ["keyutils.cpp"],
@@ -13,4 +14,5 @@
cflags: ["-Werror"],
shared_libs: ["libkeyutils"],
srcs: ["keyutils_test.cpp"],
+ test_suites: ["device-tests"],
}
diff --git a/liblog/Android.bp b/liblog/Android.bp
index 7d9e306..4a165a0 100644
--- a/liblog/Android.bp
+++ b/liblog/Android.bp
@@ -46,6 +46,7 @@
name: "liblog_headers",
host_supported: true,
vendor_available: true,
+ recovery_available: true,
export_include_dirs: ["include"],
target: {
windows: {
@@ -65,7 +66,7 @@
cc_library {
name: "liblog",
host_supported: true,
-
+ recovery_available: true,
srcs: liblog_sources,
target: {
@@ -82,6 +83,7 @@
},
android_arm: {
// TODO: This is to work around b/24465209. Remove after root cause is fixed
+ pack_relocations: false,
ldflags: ["-Wl,--hash-style=both"],
},
windows: {
diff --git a/liblog/include/android/log.h b/liblog/include/android/log.h
index 28c87e4..ee9220d 100644
--- a/liblog/include/android/log.h
+++ b/liblog/include/android/log.h
@@ -35,6 +35,11 @@
*/
/**
+ * @addtogroup Logging
+ * @{
+ */
+
+/**
* \file
*
* Support routines to send messages to the Android log buffer,
@@ -110,16 +115,8 @@
*/
int __android_log_print(int prio, const char* tag, const char* fmt, ...)
#if defined(__GNUC__)
-#ifdef __USE_MINGW_ANSI_STDIO
-#if __USE_MINGW_ANSI_STDIO
- __attribute__((__format__(gnu_printf, 3, 4)))
-#else
__attribute__((__format__(printf, 3, 4)))
#endif
-#else
- __attribute__((__format__(printf, 3, 4)))
-#endif
-#endif
;
/**
@@ -128,16 +125,8 @@
*/
int __android_log_vprint(int prio, const char* tag, const char* fmt, va_list ap)
#if defined(__GNUC__)
-#ifdef __USE_MINGW_ANSI_STDIO
-#if __USE_MINGW_ANSI_STDIO
- __attribute__((__format__(gnu_printf, 3, 0)))
-#else
__attribute__((__format__(printf, 3, 0)))
#endif
-#else
- __attribute__((__format__(printf, 3, 0)))
-#endif
-#endif
;
/**
@@ -159,16 +148,8 @@
...)
#if defined(__GNUC__)
__attribute__((__noreturn__))
-#ifdef __USE_MINGW_ANSI_STDIO
-#if __USE_MINGW_ANSI_STDIO
- __attribute__((__format__(gnu_printf, 3, 4)))
-#else
__attribute__((__format__(printf, 3, 4)))
#endif
-#else
- __attribute__((__format__(printf, 3, 4)))
-#endif
-#endif
;
#ifndef log_id_t_defined
@@ -205,4 +186,6 @@
}
#endif
+/** @} */
+
#endif /* _ANDROID_LOG_H */
diff --git a/liblog/include/log/log_event_list.h b/liblog/include/log/log_event_list.h
index bb1ce34..1b7c377 100644
--- a/liblog/include/log/log_event_list.h
+++ b/liblog/include/log/log_event_list.h
@@ -108,6 +108,13 @@
android_log_list_element android_log_read_next(android_log_context ctx);
android_log_list_element android_log_peek_next(android_log_context ctx);
+/* Reset writer context */
+int android_log_reset(android_log_context ctx);
+
+/* Reset reader context */
+int android_log_parser_reset(android_log_context ctx,
+ const char* msg, size_t len);
+
/* Finished with reader or writer context */
int android_log_destroy(android_log_context* ctx);
diff --git a/liblog/include/log/log_main.h b/liblog/include/log/log_main.h
index 21fc7cc..e7b1728 100644
--- a/liblog/include/log/log_main.h
+++ b/liblog/include/log/log_main.h
@@ -53,6 +53,19 @@
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif
+/*
+ * Use __VA_ARGS__ if running a static analyzer,
+ * to avoid warnings of unused variables in __VA_ARGS__.
+ * __FAKE_USE_VA_ARGS is undefined at link time,
+ * so don't link with __clang_analyzer__ defined.
+ */
+#ifdef __clang_analyzer__
+extern void __fake_use_va_args(int, ...);
+#define __FAKE_USE_VA_ARGS(...) __fake_use_va_args(0, ##__VA_ARGS__)
+#else
+#define __FAKE_USE_VA_ARGS(...) ((void)(0))
+#endif
+
#ifndef __predict_false
#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
#endif
@@ -112,7 +125,7 @@
#define LOG_ALWAYS_FATAL_IF(cond, ...) \
((__predict_false(cond)) \
? ((void)android_printAssert(#cond, LOG_TAG, ##__VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
#ifndef LOG_ALWAYS_FATAL
@@ -128,10 +141,10 @@
#if LOG_NDEBUG
#ifndef LOG_FATAL_IF
-#define LOG_FATAL_IF(cond, ...) ((void)0)
+#define LOG_FATAL_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
#endif
#ifndef LOG_FATAL
-#define LOG_FATAL(...) ((void)0)
+#define LOG_FATAL(...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
#endif
#else
@@ -175,11 +188,12 @@
#ifndef ALOGV
#define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
#if LOG_NDEBUG
-#define ALOGV(...) \
- do { \
- if (false) { \
- __ALOGV(__VA_ARGS__); \
- } \
+#define ALOGV(...) \
+ do { \
+ __FAKE_USE_VA_ARGS(__VA_ARGS__); \
+ if (false) { \
+ __ALOGV(__VA_ARGS__); \
+ } \
} while (false)
#else
#define ALOGV(...) __ALOGV(__VA_ARGS__)
@@ -188,11 +202,11 @@
#ifndef ALOGV_IF
#if LOG_NDEBUG
-#define ALOGV_IF(cond, ...) ((void)0)
+#define ALOGV_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
#else
#define ALOGV_IF(cond, ...) \
((__predict_false(cond)) ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
#endif
@@ -206,7 +220,7 @@
#ifndef ALOGD_IF
#define ALOGD_IF(cond, ...) \
((__predict_false(cond)) ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
/*
@@ -219,7 +233,7 @@
#ifndef ALOGI_IF
#define ALOGI_IF(cond, ...) \
((__predict_false(cond)) ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
/*
@@ -232,7 +246,7 @@
#ifndef ALOGW_IF
#define ALOGW_IF(cond, ...) \
((__predict_false(cond)) ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
/*
@@ -245,7 +259,7 @@
#ifndef ALOGE_IF
#define ALOGE_IF(cond, ...) \
((__predict_false(cond)) ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
- : (void)0)
+ : __FAKE_USE_VA_ARGS(__VA_ARGS__))
#endif
/* --------------------------------------------------------------------- */
diff --git a/liblog/include/log/log_read.h b/liblog/include/log/log_read.h
index d118563..93b9d4e 100644
--- a/liblog/include/log/log_read.h
+++ b/liblog/include/log/log_read.h
@@ -184,7 +184,7 @@
hdr_size = sizeof(entry_v1);
}
if ((hdr_size < sizeof(entry_v1)) || (hdr_size > sizeof(entry))) {
- return NULL;
+ return nullptr;
}
return reinterpret_cast<char*>(buf) + hdr_size;
}
diff --git a/liblog/include/private/android_logger.h b/liblog/include/private/android_logger.h
index 965de37..b927b46 100644
--- a/liblog/include/private/android_logger.h
+++ b/liblog/include/private/android_logger.h
@@ -173,7 +173,7 @@
#if defined(_USING_LIBCXX)
operator std::string() {
if (ret) return std::string("");
- const char* cp = NULL;
+ const char* cp = nullptr;
ssize_t len = android_log_write_list_buffer(ctx, &cp);
if (len < 0) ret = len;
if (!cp || (len <= 0)) return std::string("");
diff --git a/liblog/include_vndk/log/log_event_list.h b/liblog/include_vndk/log/log_event_list.h
index cbd3091..9f74534 100644
--- a/liblog/include_vndk/log/log_event_list.h
+++ b/liblog/include_vndk/log/log_event_list.h
@@ -63,6 +63,13 @@
/* NB: LOG_ID_EVENTS and LOG_ID_SECURITY only valid binary buffers */
int android_log_write_list(android_log_context ctx, log_id_t id);
+/* Reset writer context */
+int android_log_reset(android_log_context ctx);
+
+/* Reset reader context */
+int android_log_parser_reset(android_log_context ctx,
+ const char* msg, size_t len);
+
/* Finished with reader or writer context */
int android_log_destroy(android_log_context* ctx);
diff --git a/liblog/liblog.map.txt b/liblog/liblog.map.txt
index 66670fe..015c9cb 100644
--- a/liblog/liblog.map.txt
+++ b/liblog/liblog.map.txt
@@ -53,3 +53,9 @@
__android_log_is_loggable_len;
__android_log_is_debuggable; # vndk
};
+
+LIBLOG_Q {
+ global:
+ android_log_reset; #vndk
+ android_log_parser_reset; #vndk
+};
diff --git a/liblog/log_event_list.c b/liblog/log_event_list.c
index a59cb87..14002ce 100644
--- a/liblog/log_event_list.c
+++ b/liblog/log_event_list.c
@@ -45,14 +45,9 @@
uint8_t storage[LOGGER_ENTRY_MAX_PAYLOAD];
} android_log_context_internal;
-LIBLOG_ABI_PUBLIC android_log_context create_android_logger(uint32_t tag) {
- size_t needed, i;
- android_log_context_internal* context;
+static void init_context(android_log_context_internal* context, uint32_t tag) {
+ size_t needed;
- context = calloc(1, sizeof(android_log_context_internal));
- if (!context) {
- return NULL;
- }
context->tag = tag;
context->read_write_flag = kAndroidLoggerWrite;
needed = sizeof(uint8_t) + sizeof(uint8_t);
@@ -63,6 +58,24 @@
context->storage[context->pos + 0] = EVENT_TYPE_LIST;
context->list[0] = context->pos + 1;
context->pos += needed;
+}
+
+static void init_parser_context(android_log_context_internal* context,
+ const char* msg, size_t len) {
+ len = (len <= MAX_EVENT_PAYLOAD) ? len : MAX_EVENT_PAYLOAD;
+ context->len = len;
+ memcpy(context->storage, msg, len);
+ context->read_write_flag = kAndroidLoggerRead;
+}
+
+LIBLOG_ABI_PUBLIC android_log_context create_android_logger(uint32_t tag) {
+ android_log_context_internal* context;
+
+ context = calloc(1, sizeof(android_log_context_internal));
+ if (!context) {
+ return NULL;
+ }
+ init_context(context, tag);
return (android_log_context)context;
}
@@ -76,10 +89,7 @@
if (!context) {
return NULL;
}
- len = (len <= MAX_EVENT_PAYLOAD) ? len : MAX_EVENT_PAYLOAD;
- context->len = len;
- memcpy(context->storage, msg, len);
- context->read_write_flag = kAndroidLoggerRead;
+ init_parser_context(context, msg, len);
return (android_log_context)context;
}
@@ -97,6 +107,38 @@
return 0;
}
+LIBLOG_ABI_PUBLIC int android_log_reset(android_log_context ctx) {
+ android_log_context_internal* context;
+ uint32_t tag;
+
+ context = (android_log_context_internal*)ctx;
+ if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
+ return -EBADF;
+ }
+
+ tag = context->tag;
+ memset(context, 0, sizeof(*context));
+ init_context(context, tag);
+
+ return 0;
+}
+
+LIBLOG_ABI_PUBLIC int android_log_parser_reset(android_log_context ctx,
+ const char* msg, size_t len) {
+ android_log_context_internal* context;
+
+ context = (android_log_context_internal*)ctx;
+ if (!context || (kAndroidLoggerRead != context->read_write_flag)) {
+ return -EBADF;
+ }
+
+ memset(context, 0, sizeof(*context));
+ init_parser_context(context, msg, len);
+
+ return 0;
+}
+
+
LIBLOG_ABI_PUBLIC int android_log_write_list_begin(android_log_context ctx) {
size_t needed;
android_log_context_internal* context;
diff --git a/liblog/logger_write.c b/liblog/logger_write.c
index d03a2b6..2754e6e 100644
--- a/liblog/logger_write.c
+++ b/liblog/logger_write.c
@@ -243,7 +243,7 @@
static int __write_to_log_daemon(log_id_t log_id, struct iovec* vec, size_t nr) {
struct android_log_transport_write* node;
- int ret;
+ int ret, save_errno;
struct timespec ts;
size_t len, i;
@@ -254,20 +254,24 @@
return -EINVAL;
}
+ save_errno = errno;
#if defined(__ANDROID__)
clock_gettime(android_log_clockid(), &ts);
if (log_id == LOG_ID_SECURITY) {
if (vec[0].iov_len < 4) {
+ errno = save_errno;
return -EINVAL;
}
ret = check_log_uid_permissions();
if (ret < 0) {
+ errno = save_errno;
return ret;
}
if (!__android_log_security()) {
/* If only we could reset downstream logd counter */
+ errno = save_errno;
return -EPERM;
}
} else if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
@@ -276,6 +280,7 @@
EventTagMap *m, *f;
if (vec[0].iov_len < 4) {
+ errno = save_errno;
return -EINVAL;
}
@@ -311,6 +316,7 @@
android_closeEventTagMap(f);
}
if (!ret) {
+ errno = save_errno;
return -EPERM;
}
} else {
@@ -340,6 +346,7 @@
}
if (!__android_log_is_loggable_len(prio, tag, len - 1, ANDROID_LOG_VERBOSE)) {
+ errno = save_errno;
return -EPERM;
}
}
@@ -371,21 +378,23 @@
}
}
+ errno = save_errno;
return ret;
}
static int __write_to_log_init(log_id_t log_id, struct iovec* vec, size_t nr) {
+ int ret, save_errno = errno;
+
__android_log_lock();
if (write_to_log == __write_to_log_init) {
- int ret;
-
ret = __write_to_log_initialize();
if (ret < 0) {
__android_log_unlock();
if (!list_empty(&__android_log_persist_write)) {
__write_to_log_daemon(log_id, vec, nr);
}
+ errno = save_errno;
return ret;
}
@@ -394,7 +403,9 @@
__android_log_unlock();
- return write_to_log(log_id, vec, nr);
+ ret = write_to_log(log_id, vec, nr);
+ errno = save_errno;
+ return ret;
}
LIBLOG_ABI_PUBLIC int __android_log_write(int prio, const char* tag,
diff --git a/liblog/logprint.c b/liblog/logprint.c
index a2839bf..7937cb1 100644
--- a/liblog/logprint.c
+++ b/liblog/logprint.c
@@ -1632,8 +1632,10 @@
prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm",
colorFromPri(entry->priority));
prefixLen = MIN(prefixLen, sizeof(prefixBuf));
- suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m");
- suffixLen = MIN(suffixLen, sizeof(suffixBuf));
+
+ const char suffixContents[] = "\x1B[0m";
+ strcpy(suffixBuf, suffixContents);
+ suffixLen = strlen(suffixContents);
}
char uid[16];
diff --git a/liblog/tests/libc_test.cpp b/liblog/tests/libc_test.cpp
index 329ba85..6d78ed6 100644
--- a/liblog/tests/libc_test.cpp
+++ b/liblog/tests/libc_test.cpp
@@ -21,6 +21,7 @@
TEST(libc, __pstore_append) {
#ifdef __ANDROID__
+#ifndef NO_PSTORE
FILE* fp;
ASSERT_TRUE(NULL != (fp = fopen("/dev/pmsg0", "a")));
static const char message[] = "libc.__pstore_append\n";
@@ -43,6 +44,9 @@
"Reboot, ensure string libc.__pstore_append is in "
"/sys/fs/pstore/pmsg-ramoops-0\n");
}
+#else /* NO_PSTORE */
+ GTEST_LOG_(INFO) << "This test does nothing because of NO_PSTORE.\n";
+#endif /* NO_PSTORE */
#else
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif
diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp
index 597a6bb..a8a9a12 100644
--- a/liblog/tests/liblog_test.cpp
+++ b/liblog/tests/liblog_test.cpp
@@ -3180,113 +3180,6 @@
}
#endif // USING_LOGGER_DEFAULT
-#ifdef USING_LOGGER_DEFAULT // Do not retest event mapping functionality
-#ifdef __ANDROID__
-// must be: '<needle:> 0 kB'
-static bool isZero(const std::string& content, std::string::size_type pos,
- const char* needle) {
- std::string::size_type offset = content.find(needle, pos);
- return (offset != std::string::npos) &&
- ((offset = content.find_first_not_of(" \t", offset + strlen(needle))) !=
- std::string::npos) &&
- (content.find_first_not_of('0', offset) != offset);
-}
-
-// must not be: '<needle:> 0 kB'
-static bool isNotZero(const std::string& content, std::string::size_type pos,
- const char* needle) {
- std::string::size_type offset = content.find(needle, pos);
- return (offset != std::string::npos) &&
- ((offset = content.find_first_not_of(" \t", offset + strlen(needle))) !=
- std::string::npos) &&
- (content.find_first_not_of("123456789", offset) != offset);
-}
-
-static void event_log_tags_test_smap(pid_t pid) {
- std::string filename = android::base::StringPrintf("/proc/%d/smaps", pid);
-
- std::string content;
- if (!android::base::ReadFileToString(filename, &content)) return;
-
- bool shared_ok = false;
- bool private_ok = false;
- bool anonymous_ok = false;
- bool pass_ok = false;
-
- static const char event_log_tags[] = "event-log-tags";
- std::string::size_type pos = 0;
- while ((pos = content.find(event_log_tags, pos)) != std::string::npos) {
- pos += strlen(event_log_tags);
-
- // must not be: 'Shared_Clean: 0 kB'
- bool ok =
- isNotZero(content, pos, "Shared_Clean:") ||
- // If not /etc/event-log-tags, thus r/w, then half points
- // back for not 'Shared_Dirty: 0 kB'
- ((content.substr(pos - 5 - strlen(event_log_tags), 5) != "/etc/") &&
- isNotZero(content, pos, "Shared_Dirty:"));
- if (ok && !pass_ok) {
- shared_ok = true;
- } else if (!ok) {
- shared_ok = false;
- }
-
- // must be: 'Private_Dirty: 0 kB' and 'Private_Clean: 0 kB'
- ok = isZero(content, pos, "Private_Dirty:") ||
- isZero(content, pos, "Private_Clean:");
- if (ok && !pass_ok) {
- private_ok = true;
- } else if (!ok) {
- private_ok = false;
- }
-
- // must be: 'Anonymous: 0 kB'
- ok = isZero(content, pos, "Anonymous:");
- if (ok && !pass_ok) {
- anonymous_ok = true;
- } else if (!ok) {
- anonymous_ok = false;
- }
-
- pass_ok = true;
- }
- content = "";
-
- if (!pass_ok) return;
- if (shared_ok && anonymous_ok && private_ok) return;
-
- filename = android::base::StringPrintf("/proc/%d/comm", pid);
- android::base::ReadFileToString(filename, &content);
- content = android::base::StringPrintf(
- "%d:%s", pid, content.substr(0, content.find('\n')).c_str());
-
- EXPECT_TRUE(IsOk(shared_ok, content));
- EXPECT_TRUE(IsOk(private_ok, content));
- EXPECT_TRUE(IsOk(anonymous_ok, content));
-}
-#endif // __ANDROID__
-
-TEST(liblog, event_log_tags) {
-#ifdef __ANDROID__
- std::unique_ptr<DIR, int (*)(DIR*)> proc_dir(opendir("/proc"), closedir);
- ASSERT_FALSE(!proc_dir);
-
- dirent* e;
- while ((e = readdir(proc_dir.get()))) {
- if (e->d_type != DT_DIR) continue;
- if (!isdigit(e->d_name[0])) continue;
- long long id = atoll(e->d_name);
- if (id <= 0) continue;
- pid_t pid = id;
- if (id != pid) continue;
- event_log_tags_test_smap(pid);
- }
-#else
- GTEST_LOG_(INFO) << "This test does nothing.\n";
-#endif
-}
-#endif // USING_LOGGER_DEFAULT
-
#ifdef USING_LOGGER_DEFAULT // Do not retest ratelimit
TEST(liblog, __android_log_ratelimit) {
time_t state = 0;
diff --git a/liblog/tests/log_read_test.cpp b/liblog/tests/log_read_test.cpp
index 444a5ac..443c3ea 100644
--- a/liblog/tests/log_read_test.cpp
+++ b/liblog/tests/log_read_test.cpp
@@ -27,6 +27,7 @@
// Test the APIs in this standalone include file
#include <log/log_read.h>
// Do not use anything in log/log_time.h despite side effects of the above.
+#include <private/android_logger.h>
TEST(liblog, __android_log_write__android_logger_list_read) {
#ifdef __ANDROID__
@@ -105,7 +106,10 @@
// framework (b/68266385).
EXPECT_LE( // boolean 1 or 0 depending on expected content or empty
!!((strcmp("crash", name) != 0) &&
- ((strcmp("kernel", name) != 0) || __android_log_is_debuggable()) &&
+ ((strcmp("kernel", name) != 0) ||
+ __android_logger_property_get_bool(
+ "ro.logd.kernel", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG |
+ BOOL_DEFAULT_FLAG_SVELTE)) &&
(strcmp("stats", name) != 0)),
android_logger_get_log_readable_size(logger));
} else {
diff --git a/libmemunreachable/Android.bp b/libmemunreachable/Android.bp
index caca377..248a9d2 100644
--- a/libmemunreachable/Android.bp
+++ b/libmemunreachable/Android.bp
@@ -29,7 +29,6 @@
"HeapWalker.cpp",
"LeakFolding.cpp",
"LeakPipe.cpp",
- "LineBuffer.cpp",
"MemUnreachable.cpp",
"ProcessMappings.cpp",
"PtracerThread.cpp",
@@ -38,6 +37,7 @@
static_libs: [
"libc_malloc_debug_backtrace",
+ "libprocinfo",
],
// Only need this for arm since libc++ uses its own unwind code that
// doesn't mix with the other default unwind code.
@@ -46,6 +46,12 @@
static_libs: ["libunwind_llvm"],
},
},
+
+ // TODO(b/78118944), clang lld link flags do not work with special link
+ // rules for libunwind_llvm yet. Linked aosp_arm-eng image failed to
+ // boot up in the emulator.
+ use_clang_lld: false,
+
export_include_dirs: ["include"],
local_include_dirs: ["include"],
}
@@ -83,12 +89,13 @@
enabled: false,
},
},
+
+ test_suites: ["device-tests"],
}
cc_test {
name: "memunreachable_binder_test",
defaults: ["libmemunreachable_defaults"],
- test_suites: ["vts"],
srcs: [
"tests/Binder_test.cpp",
],
@@ -98,4 +105,5 @@
"libhwbinder",
"libutils",
],
+ test_suites: ["device-tests"],
}
diff --git a/libmemunreachable/LineBuffer.cpp b/libmemunreachable/LineBuffer.cpp
deleted file mode 100644
index 4ea0542..0000000
--- a/libmemunreachable/LineBuffer.cpp
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright (C) 2015 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.
- */
-
-// Copied from system/extras/memory_replay/LineBuffer.cpp
-// TODO(ccross): find a way to share between libmemunreachable and memory_replay?
-
-#include <errno.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "LineBuffer.h"
-
-namespace android {
-
-LineBuffer::LineBuffer(int fd, char* buffer, size_t buffer_len)
- : fd_(fd), buffer_(buffer), buffer_len_(buffer_len) {}
-
-bool LineBuffer::GetLine(char** line, size_t* line_len) {
- while (true) {
- if (bytes_ > 0) {
- char* newline = reinterpret_cast<char*>(memchr(buffer_ + start_, '\n', bytes_));
- if (newline != nullptr) {
- *newline = '\0';
- *line = buffer_ + start_;
- start_ = newline - buffer_ + 1;
- bytes_ -= newline - *line + 1;
- *line_len = newline - *line;
- return true;
- }
- }
- if (start_ > 0) {
- // Didn't find anything, copy the current to the front of the buffer.
- memmove(buffer_, buffer_ + start_, bytes_);
- start_ = 0;
- }
- ssize_t bytes = TEMP_FAILURE_RETRY(read(fd_, buffer_ + bytes_, buffer_len_ - bytes_ - 1));
- if (bytes <= 0) {
- if (bytes_ > 0) {
- // The read data might not contain a nul terminator, so add one.
- buffer_[bytes_] = '\0';
- *line = buffer_ + start_;
- *line_len = bytes_;
- bytes_ = 0;
- start_ = 0;
- return true;
- }
- return false;
- }
- bytes_ += bytes;
- }
-}
-
-} // namespace android
diff --git a/libmemunreachable/LineBuffer.h b/libmemunreachable/LineBuffer.h
deleted file mode 100644
index cc6cd0c..0000000
--- a/libmemunreachable/LineBuffer.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2015 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.
- */
-
-#ifndef _LIBMEMUNREACHABLE_LINE_BUFFER_H
-#define _LIBMEMUNREACHABLE_LINE_BUFFER_H
-
-#include <stdint.h>
-
-namespace android {
-
-class LineBuffer {
- public:
- LineBuffer(int fd, char* buffer, size_t buffer_len);
-
- bool GetLine(char** line, size_t* line_len);
-
- private:
- int fd_;
- char* buffer_ = nullptr;
- size_t buffer_len_ = 0;
- size_t start_ = 0;
- size_t bytes_ = 0;
-};
-
-} // namespace android
-
-#endif // _LIBMEMUNREACHABLE_LINE_BUFFER_H
diff --git a/libmemunreachable/MemUnreachable.cpp b/libmemunreachable/MemUnreachable.cpp
index 24fdc7f..529a043 100644
--- a/libmemunreachable/MemUnreachable.cpp
+++ b/libmemunreachable/MemUnreachable.cpp
@@ -495,6 +495,21 @@
return oss.str();
}
+UnreachableMemoryInfo::~UnreachableMemoryInfo() {
+ // Clear the memory that holds the leaks, otherwise the next attempt to
+ // detect leaks may find the old data (for example in the jemalloc tcache)
+ // and consider all the leaks to be referenced.
+ memset(leaks.data(), 0, leaks.capacity() * sizeof(Leak));
+
+ std::vector<Leak> tmp;
+ leaks.swap(tmp);
+
+ // Disable and re-enable malloc to flush the jemalloc tcache to make sure
+ // there are no copies of the leaked pointer addresses there.
+ malloc_disable();
+ malloc_enable();
+}
+
std::string GetUnreachableMemoryString(bool log_contents, size_t limit) {
UnreachableMemoryInfo info;
if (!GetUnreachableMemory(info, limit)) {
diff --git a/libmemunreachable/ProcessMappings.cpp b/libmemunreachable/ProcessMappings.cpp
index 9a06870..701ce16 100644
--- a/libmemunreachable/ProcessMappings.cpp
+++ b/libmemunreachable/ProcessMappings.cpp
@@ -14,21 +14,30 @@
* limitations under the License.
*/
+#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <android-base/unique_fd.h>
+#include <procinfo/process_map.h>
-#include "LineBuffer.h"
#include "ProcessMappings.h"
-#include "log.h"
namespace android {
-// This function is not re-entrant since it uses a static buffer for
-// the line data.
+struct ReadMapCallback {
+ ReadMapCallback(allocator::vector<Mapping>& mappings) : mappings_(mappings) {}
+
+ void operator()(uint64_t start, uint64_t end, uint16_t flags, uint64_t, const char* name) const {
+ mappings_.emplace_back(start, end, flags & PROT_READ, flags & PROT_WRITE, flags & PROT_EXEC,
+ name);
+ }
+
+ allocator::vector<Mapping>& mappings_;
+};
+
bool ProcessMappings(pid_t pid, allocator::vector<Mapping>& mappings) {
char map_buffer[1024];
snprintf(map_buffer, sizeof(map_buffer), "/proc/%d/maps", pid);
@@ -36,35 +45,13 @@
if (fd == -1) {
return false;
}
-
- LineBuffer line_buf(fd, map_buffer, sizeof(map_buffer));
- char* line;
- size_t line_len;
- while (line_buf.GetLine(&line, &line_len)) {
- int name_pos;
- char perms[5];
- Mapping mapping{};
- if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %*x %*x:%*x %*d %n", &mapping.begin,
- &mapping.end, perms, &name_pos) == 3) {
- if (perms[0] == 'r') {
- mapping.read = true;
- }
- if (perms[1] == 'w') {
- mapping.write = true;
- }
- if (perms[2] == 'x') {
- mapping.execute = true;
- }
- if (perms[3] == 'p') {
- mapping.priv = true;
- }
- if ((size_t)name_pos < line_len) {
- strlcpy(mapping.name, line + name_pos, sizeof(mapping.name));
- }
- mappings.emplace_back(mapping);
- }
+ allocator::string content(mappings.get_allocator());
+ ssize_t n;
+ while ((n = TEMP_FAILURE_RETRY(read(fd, map_buffer, sizeof(map_buffer)))) > 0) {
+ content.append(map_buffer, n);
}
- return true;
+ ReadMapCallback callback(mappings);
+ return android::procinfo::ReadMapFileContent(&content[0], callback);
}
} // namespace android
diff --git a/libmemunreachable/ProcessMappings.h b/libmemunreachable/ProcessMappings.h
index a0e97e9..94da69b 100644
--- a/libmemunreachable/ProcessMappings.h
+++ b/libmemunreachable/ProcessMappings.h
@@ -17,6 +17,8 @@
#ifndef LIBMEMUNREACHABLE_PROCESS_MAPPING_H_
#define LIBMEMUNREACHABLE_PROCESS_MAPPING_H_
+#include <string.h>
+
#include "Allocator.h"
namespace android {
@@ -27,8 +29,13 @@
bool read;
bool write;
bool execute;
- bool priv;
char name[96];
+
+ Mapping() {}
+ Mapping(uintptr_t begin, uintptr_t end, bool read, bool write, bool execute, const char* name)
+ : begin(begin), end(end), read(read), write(write), execute(execute) {
+ strlcpy(this->name, name, sizeof(this->name));
+ }
};
// This function is not re-entrant since it uses a static buffer for
diff --git a/libmemunreachable/README.md b/libmemunreachable/README.md
index ae8fa94..9cc0c9b 100644
--- a/libmemunreachable/README.md
+++ b/libmemunreachable/README.md
@@ -12,6 +12,27 @@
Usage
-------
+### In Android apps ###
+
+libmemunreachble is loaded by zygote and can be triggered with `dumpsys -t 600 meminfo --unreachable [process]`.
+
+To enable malloc\_debug backtraces on allocations for a single app process on a userdebug device, use:
+```
+adb root
+adb shell setprop libc.debug.malloc.program app_process
+adb shell setprop wrap.[process] "\$\@"
+adb shell setprop libc.debug.malloc.options backtrace=4
+```
+
+Kill and restart the app, trigger the leak, and then run `dumpsys -t 600 meminfo --unreachable [process]`.
+
+To disable malloc\_debug:
+```
+adb shell setprop libc.debug.malloc.options "''"
+adb shell setprop libc.debug.malloc.program "''"
+adb shell setprop wrap.[process] "''"
+```
+
### C interface ###
#### `bool LogUnreachableMemory(bool log_contents, size_t limit)` ####
@@ -23,7 +44,7 @@
### C++ interface ###
-####`bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit = 100)`####
+#### `bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit = 100)` ####
Updates an `UnreachableMemoryInfo` object with information on leaks, including details on up to `limit` leaks. Returns true if leak detection succeeded.
#### `std::string GetUnreachableMemoryString(bool log_contents = false, size_t limit = 100)` ####
diff --git a/libmemunreachable/include/memunreachable/memunreachable.h b/libmemunreachable/include/memunreachable/memunreachable.h
index 438fcaf..c028eab 100644
--- a/libmemunreachable/include/memunreachable/memunreachable.h
+++ b/libmemunreachable/include/memunreachable/memunreachable.h
@@ -62,12 +62,7 @@
size_t allocation_bytes;
UnreachableMemoryInfo() {}
- ~UnreachableMemoryInfo() {
- // Clear the memory that holds the leaks, otherwise the next attempt to
- // detect leaks may find the old data (for example in the jemalloc tcache)
- // and consider all the leaks to be referenced.
- memset(leaks.data(), 0, leaks.capacity() * sizeof(Leak));
- }
+ ~UnreachableMemoryInfo();
std::string ToString(bool log_contents) const;
};
diff --git a/libmemunreachable/tests/MemUnreachable_test.cpp b/libmemunreachable/tests/MemUnreachable_test.cpp
index 87417f1..bba0c6d 100644
--- a/libmemunreachable/tests/MemUnreachable_test.cpp
+++ b/libmemunreachable/tests/MemUnreachable_test.cpp
@@ -23,6 +23,8 @@
#include <memunreachable/memunreachable.h>
+#include "bionic.h"
+
namespace android {
class HiddenPointer {
@@ -48,7 +50,35 @@
write(0, ptr, 0);
}
-TEST(MemunreachableTest, clean) {
+class MemunreachableTest : public ::testing::Test {
+ protected:
+ virtual void SetUp() {
+ CleanStack(8192);
+ CleanTcache();
+ }
+
+ virtual void TearDown() {
+ CleanStack(8192);
+ CleanTcache();
+ }
+
+ // Allocate a buffer on the stack and zero it to make sure there are no
+ // stray pointers from old test runs.
+ void __attribute__((noinline)) CleanStack(size_t size) {
+ void* buf = alloca(size);
+ memset(buf, 0, size);
+ Ref(&buf);
+ }
+
+ // Disable and re-enable malloc to flush the jemalloc tcache to make sure
+ // there are stray pointers from old test runs there.
+ void CleanTcache() {
+ malloc_disable();
+ malloc_enable();
+ }
+};
+
+TEST_F(MemunreachableTest, clean) {
UnreachableMemoryInfo info;
ASSERT_TRUE(LogUnreachableMemory(true, 100));
@@ -57,7 +87,7 @@
ASSERT_EQ(0U, info.leaks.size());
}
-TEST(MemunreachableTest, stack) {
+TEST_F(MemunreachableTest, stack) {
HiddenPointer hidden_ptr;
{
@@ -91,7 +121,7 @@
void* g_ptr;
-TEST(MemunreachableTest, global) {
+TEST_F(MemunreachableTest, global) {
HiddenPointer hidden_ptr;
g_ptr = hidden_ptr.Get();
@@ -122,7 +152,7 @@
}
}
-TEST(MemunreachableTest, tls) {
+TEST_F(MemunreachableTest, tls) {
HiddenPointer hidden_ptr;
pthread_key_t key;
pthread_key_create(&key, nullptr);
@@ -157,10 +187,22 @@
pthread_key_delete(key);
}
-TEST(MemunreachableTest, twice) {
+TEST_F(MemunreachableTest, twice) {
HiddenPointer hidden_ptr;
{
+ void* ptr = hidden_ptr.Get();
+ Ref(&ptr);
+
+ UnreachableMemoryInfo info;
+
+ ASSERT_TRUE(GetUnreachableMemory(info));
+ ASSERT_EQ(0U, info.leaks.size());
+
+ ptr = nullptr;
+ }
+
+ {
UnreachableMemoryInfo info;
ASSERT_TRUE(GetUnreachableMemory(info));
@@ -184,7 +226,7 @@
}
}
-TEST(MemunreachableTest, log) {
+TEST_F(MemunreachableTest, log) {
HiddenPointer hidden_ptr;
ASSERT_TRUE(LogUnreachableMemory(true, 100));
@@ -199,17 +241,23 @@
}
}
-TEST(MemunreachableTest, notdumpable) {
+TEST_F(MemunreachableTest, notdumpable) {
+ if (getuid() == 0) {
+ // TODO(ccross): make this a skipped test when gtest supports them
+ printf("[ SKIP ] Not testable when running as root\n");
+ return;
+ }
+
ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 0));
HiddenPointer hidden_ptr;
- ASSERT_TRUE(LogUnreachableMemory(true, 100));
+ EXPECT_FALSE(LogUnreachableMemory(true, 100));
ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 1));
}
-TEST(MemunreachableTest, leak_lots) {
+TEST_F(MemunreachableTest, leak_lots) {
std::vector<HiddenPointer> hidden_ptrs;
hidden_ptrs.resize(1024);
diff --git a/libmemunreachable/tests/ThreadCapture_test.cpp b/libmemunreachable/tests/ThreadCapture_test.cpp
index 4fbf729..933d65a 100644
--- a/libmemunreachable/tests/ThreadCapture_test.cpp
+++ b/libmemunreachable/tests/ThreadCapture_test.cpp
@@ -32,6 +32,8 @@
#include "ScopedDisableMalloc.h"
#include "ScopedPipe.h"
+#include <android-base/threads.h>
+
using namespace std::chrono_literals;
namespace android {
@@ -260,7 +262,7 @@
ThreadCapture thread_capture(ret, heap);
thread_capture.InjectTestFunc([&](pid_t tid) {
- syscall(SYS_tgkill, ret, tid, SIGKILL);
+ tgkill(ret, tid, SIGKILL);
usleep(10000);
});
auto list_tids = allocator::vector<pid_t>(heap);
@@ -319,7 +321,7 @@
ThreadCapture thread_capture(child, heap);
thread_capture.InjectTestFunc([&](pid_t tid) {
- syscall(SYS_tgkill, child, tid, sig);
+ tgkill(child, tid, sig);
usleep(10000);
});
auto list_tids = allocator::vector<pid_t>(heap);
diff --git a/libmetricslogger/Android.bp b/libmetricslogger/Android.bp
index 6549b8d..e6e17ce 100644
--- a/libmetricslogger/Android.bp
+++ b/libmetricslogger/Android.bp
@@ -29,6 +29,13 @@
defaults: ["metricslogger_defaults"],
}
+// static version of libmetricslogger, needed by a few art static binaries
+cc_library_static {
+ name: "libmetricslogger_static",
+ srcs: metricslogger_lib_src_files,
+ defaults: ["metricslogger_defaults"],
+}
+
// metricslogger shared library, debug
// -----------------------------------------------------------------------------
cc_library_shared {
diff --git a/libmetricslogger/OWNERS b/libmetricslogger/OWNERS
index 7fe0443..6a6fba2 100644
--- a/libmetricslogger/OWNERS
+++ b/libmetricslogger/OWNERS
@@ -1 +1,2 @@
+cwren@google.com
jhawkins@google.com
diff --git a/libmetricslogger/include/metricslogger/metrics_logger.h b/libmetricslogger/include/metricslogger/metrics_logger.h
index 189bc4b..c305db2 100644
--- a/libmetricslogger/include/metricslogger/metrics_logger.h
+++ b/libmetricslogger/include/metricslogger/metrics_logger.h
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <log/log_event_list.h>
#include <cstdint>
#include <string>
@@ -32,6 +33,36 @@
// |value| in the field |field|.
void LogMultiAction(int32_t category, int32_t field, const std::string& value);
+// Logs a Tron complex event.
+//
+// A complex event can include data in a structure not suppored by the other
+// log event types above.
+//
+// Note that instances of this class are single use. You must call Record()
+// to write the event to the event log.
+class ComplexEventLogger {
+ private:
+ android_log_event_list logger;
+
+ public:
+ // Create a complex event with category|category|.
+ explicit ComplexEventLogger(int category);
+ // Set the package name that this event originates from.
+ void SetPackageName(const std::string& package_name);
+ // Add tagged data to the event, with the given tag and integer value.
+ void AddTaggedData(int tag, int32_t value);
+ // Add tagged data to the event, with the given tag and string value.
+ void AddTaggedData(int tag, const std::string& value);
+ // Add tagged data to the event, with the given tag and integer value.
+ void AddTaggedData(int tag, int64_t value);
+ // Add tagged data to the event, with the given tag and float value.
+ void AddTaggedData(int tag, float value);
+ // Record this event. This method can only be used once per instance
+ // of ComplexEventLogger. Do not made any subsequent calls to AddTaggedData
+ // after recording an event.
+ void Record();
+};
+
// TODO: replace these with the metric_logger.proto definitions
enum {
LOGBUILDER_CATEGORY = 757,
@@ -41,14 +72,86 @@
LOGBUILDER_VALUE = 802,
LOGBUILDER_COUNTER = 803,
LOGBUILDER_HISTOGRAM = 804,
+ LOGBUILDER_PACKAGENAME = 806,
ACTION_BOOT = 1098,
FIELD_PLATFORM_REASON = 1099,
+
+ FIELD_DURATION_MILLIS = 1304,
+
+ FIELD_END_BATTERY_PERCENT = 1308,
+
+ ACTION_HIDDEN_API_ACCESSED = 1391,
+ FIELD_HIDDEN_API_ACCESS_METHOD = 1392,
+ FIELD_HIDDEN_API_ACCESS_DENIED = 1393,
+ FIELD_HIDDEN_API_SIGNATURE = 1394,
+
+ ACTION_USB_CONNECTOR_CONNECTED = 1422,
+ ACTION_USB_CONNECTOR_DISCONNECTED = 1423,
+ ACTION_USB_AUDIO_CONNECTED = 1424,
+ FIELD_USB_AUDIO_VIDPID = 1425,
+ ACTION_USB_AUDIO_DISCONNECTED = 1426,
+ ACTION_HARDWARE_FAILED = 1427,
+ FIELD_HARDWARE_TYPE = 1428,
+ FIELD_HARDWARE_FAILURE_CODE = 1429,
+ ACTION_PHYSICAL_DROP = 1430,
+ FIELD_CONFIDENCE_PERCENT = 1431,
+ FIELD_ACCEL_MILLI_G = 1432,
+ ACTION_BATTERY_HEALTH = 1433,
+ FIELD_BATTERY_HEALTH_SNAPSHOT_TYPE = 1434,
+ FIELD_BATTERY_TEMPERATURE_DECI_C = 1435,
+ FIELD_BATTERY_VOLTAGE_UV = 1436,
+ FIELD_BATTERY_OPEN_CIRCUIT_VOLTAGE_UV = 1437,
+ ACTION_BATTERY_CHARGE_CYCLES = 1438,
+ FIELD_BATTERY_CHARGE_CYCLES = 1439,
+
+ ACTION_SLOW_IO = 1442,
+ FIELD_IO_OPERATION_TYPE = 1443,
+ FIELD_IO_OPERATION_COUNT = 1444,
+ ACTION_SPEAKER_IMPEDANCE = 1445,
+ FIELD_SPEAKER_IMPEDANCE_MILLIOHMS = 1446,
+ FIELD_SPEAKER_LOCATION = 1447,
+ FIELD_BATTERY_RESISTANCE_UOHMS = 1448,
+ FIELD_BATTERY_CURRENT_UA = 1449,
+ FIELD_HARDWARE_LOCATION = 1450,
+ ACTION_BATTERY_CAUSED_SHUTDOWN = 1441,
};
enum {
TYPE_ACTION = 4,
};
+enum {
+ ACCESS_METHOD_NONE = 0,
+ ACCESS_METHOD_REFLECTION = 1,
+ ACCESS_METHOD_JNI = 2,
+ ACCESS_METHOD_LINKING = 3,
+};
+
+enum HardwareType {
+ HARDWARE_UNKNOWN = 0,
+ HARDWARE_MICROPHONE = 1,
+ HARDWARE_CODEC = 2,
+ HARDWARE_SPEAKER = 3,
+ HARDWARE_FINGERPRINT = 4,
+};
+
+enum HardwareFailureCode {
+ HARDWARE_FAILURE_UNKNOWN = 0,
+ HARDWARE_FAILURE_COMPLETE = 1,
+ HARDWARE_FAILURE_SPEAKER_HIGH_Z = 2,
+ HARDWARE_FAILURE_SPEAKER_SHORT = 3,
+ HARDWARE_FAILURE_FINGERPRINT_SENSOR_BROKEN = 4,
+ HARDWARE_FAILURE_FINGERPRINT_TOO_MANY_DEAD_PIXELS = 5,
+};
+
+enum IoOperation {
+ IOOP_UNKNOWN = 0,
+ IOOP_READ = 1,
+ IOOP_WRITE = 2,
+ IOOP_UNMAP = 3,
+ IOOP_SYNC = 4,
+};
+
} // namespace metricslogger
} // namespace android
diff --git a/libmetricslogger/metrics_logger.cpp b/libmetricslogger/metrics_logger.cpp
index fdc4407..6a32153 100644
--- a/libmetricslogger/metrics_logger.cpp
+++ b/libmetricslogger/metrics_logger.cpp
@@ -23,9 +23,14 @@
namespace {
+#ifdef __ANDROID__
EventTagMap* kEventTagMap = android_openEventTagMap(nullptr);
const int kSysuiMultiActionTag = android_lookupEventTagNum(
kEventTagMap, "sysui_multi_action", "(content|4)", ANDROID_LOG_UNKNOWN);
+#else
+// android_openEventTagMap does not work on host builds.
+const int kSysuiMultiActionTag = 0;
+#endif
} // namespace
@@ -53,5 +58,33 @@
<< field << value << LOG_ID_EVENTS;
}
+ComplexEventLogger::ComplexEventLogger(int category) : logger(kSysuiMultiActionTag) {
+ logger << LOGBUILDER_CATEGORY << category;
+}
+
+void ComplexEventLogger::SetPackageName(const std::string& package_name) {
+ logger << LOGBUILDER_PACKAGENAME << package_name;
+}
+
+void ComplexEventLogger::AddTaggedData(int tag, int32_t value) {
+ logger << tag << value;
+}
+
+void ComplexEventLogger::AddTaggedData(int tag, const std::string& value) {
+ logger << tag << value;
+}
+
+void ComplexEventLogger::AddTaggedData(int tag, int64_t value) {
+ logger << tag << value;
+}
+
+void ComplexEventLogger::AddTaggedData(int tag, float value) {
+ logger << tag << value;
+}
+
+void ComplexEventLogger::Record() {
+ logger << LOG_ID_EVENTS;
+}
+
} // namespace metricslogger
} // namespace android
diff --git a/libnativeloader/OWNERS b/libnativeloader/OWNERS
index f2cc942..5c28a9f 100644
--- a/libnativeloader/OWNERS
+++ b/libnativeloader/OWNERS
@@ -1 +1,2 @@
dimitry@google.com
+jiyong@google.com
diff --git a/libnativeloader/include/nativeloader/native_loader.h b/libnativeloader/include/nativeloader/native_loader.h
index 3563fc1..19a1783 100644
--- a/libnativeloader/include/nativeloader/native_loader.h
+++ b/libnativeloader/include/nativeloader/native_loader.h
@@ -53,8 +53,21 @@
#if defined(__ANDROID__)
// Look up linker namespace by class_loader. Returns nullptr if
// there is no namespace associated with the class_loader.
+// TODO(b/79940628): move users to FindNativeLoaderNamespaceByClassLoader and remove this function.
__attribute__((visibility("default")))
android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader);
+// That version works with native bridge namespaces, but requires use of OpenNativeLibrary.
+class NativeLoaderNamespace;
+__attribute__((visibility("default")))
+NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(
+ JNIEnv* env, jobject class_loader);
+// Load library. Unlinke OpenNativeLibrary above couldn't create namespace on demand, but does
+// not require access to JNIEnv either.
+__attribute__((visibility("default")))
+void* OpenNativeLibrary(NativeLoaderNamespace* ns,
+ const char* path,
+ bool* needs_native_bridge,
+ std::string* error_msg);
#endif
__attribute__((visibility("default")))
diff --git a/libnativeloader/native_loader.cpp b/libnativeloader/native_loader.cpp
index 0ebb226..67c1c10 100644
--- a/libnativeloader/native_loader.cpp
+++ b/libnativeloader/native_loader.cpp
@@ -29,6 +29,7 @@
#include "nativebridge/native_bridge.h"
#include <algorithm>
+#include <list>
#include <memory>
#include <mutex>
#include <string>
@@ -46,6 +47,8 @@
"%s:%d: %s CHECK '" #predicate "' failed.",\
__FILE__, __LINE__, __FUNCTION__)
+using namespace std::string_literals;
+
namespace android {
#if defined(__ANDROID__)
@@ -148,15 +151,14 @@
public:
LibraryNamespaces() : initialized_(false) { }
- bool Create(JNIEnv* env,
- uint32_t target_sdk_version,
- jobject class_loader,
- bool is_shared,
- bool is_for_vendor,
- jstring java_library_path,
- jstring java_permitted_path,
- NativeLoaderNamespace* ns,
- std::string* error_msg) {
+ NativeLoaderNamespace* Create(JNIEnv* env,
+ uint32_t target_sdk_version,
+ jobject class_loader,
+ bool is_shared,
+ bool is_for_vendor,
+ jstring java_library_path,
+ jstring java_permitted_path,
+ std::string* error_msg) {
std::string library_path; // empty string by default.
if (java_library_path != nullptr) {
@@ -180,10 +182,10 @@
}
if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) {
- return false;
+ return nullptr;
}
- bool found = FindNamespaceByClassLoader(env, class_loader, nullptr);
+ bool found = FindNamespaceByClassLoader(env, class_loader);
LOG_ALWAYS_FATAL_IF(found,
"There is already a namespace associated with this classloader");
@@ -197,13 +199,12 @@
namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED;
}
- NativeLoaderNamespace parent_ns;
- bool found_parent_namespace = FindParentNamespaceByClassLoader(env, class_loader, &parent_ns);
+ NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
bool is_native_bridge = false;
- if (found_parent_namespace) {
- is_native_bridge = !parent_ns.is_android_namespace();
+ if (parent_ns != nullptr) {
+ is_native_bridge = !parent_ns->is_android_namespace();
} else if (!library_path.empty()) {
is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str());
}
@@ -236,23 +237,30 @@
// Different name is useful for debugging
namespace_name = kVendorClassloaderNamespaceName;
ALOGD("classloader namespace configured for unbundled vendor apk. library_path=%s", library_path.c_str());
- } else if (!oem_public_libraries_.empty()) {
- // oem_public_libraries are NOT available to vendor apks, otherwise it
+ } else {
+ // oem and product public libraries are NOT available to vendor apks, otherwise it
// would be system->vendor violation.
- system_exposed_libraries = system_exposed_libraries + ":" + oem_public_libraries_.c_str();
+ if (!oem_public_libraries_.empty()) {
+ system_exposed_libraries = system_exposed_libraries + ':' + oem_public_libraries_;
+ }
+ if (!product_public_libraries_.empty()) {
+ system_exposed_libraries = system_exposed_libraries + ':' + product_public_libraries_;
+ }
}
NativeLoaderNamespace native_loader_ns;
if (!is_native_bridge) {
+ android_namespace_t* android_parent_ns =
+ parent_ns == nullptr ? nullptr : parent_ns->get_android_ns();
android_namespace_t* ns = android_create_namespace(namespace_name,
nullptr,
library_path.c_str(),
namespace_type,
permitted_path.c_str(),
- parent_ns.get_android_ns());
+ android_parent_ns);
if (ns == nullptr) {
*error_msg = dlerror();
- return false;
+ return nullptr;
}
// Note that when vendor_ns is not configured this function will return nullptr
@@ -262,49 +270,50 @@
if (!android_link_namespaces(ns, nullptr, system_exposed_libraries.c_str())) {
*error_msg = dlerror();
- return false;
+ return nullptr;
}
if (vndk_ns != nullptr && !system_vndksp_libraries_.empty()) {
// vendor apks are allowed to use VNDK-SP libraries.
if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries_.c_str())) {
*error_msg = dlerror();
- return false;
+ return nullptr;
}
}
if (!vendor_public_libraries_.empty()) {
if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
*error_msg = dlerror();
- return false;
+ return nullptr;
}
}
native_loader_ns = NativeLoaderNamespace(ns);
} else {
+ native_bridge_namespace_t* native_bridge_parent_namespace =
+ parent_ns == nullptr ? nullptr : parent_ns->get_native_bridge_ns();
native_bridge_namespace_t* ns = NativeBridgeCreateNamespace(namespace_name,
nullptr,
library_path.c_str(),
namespace_type,
permitted_path.c_str(),
- parent_ns.get_native_bridge_ns());
-
+ native_bridge_parent_namespace);
if (ns == nullptr) {
*error_msg = NativeBridgeGetError();
- return false;
+ return nullptr;
}
native_bridge_namespace_t* vendor_ns = NativeBridgeGetVendorNamespace();
if (!NativeBridgeLinkNamespaces(ns, nullptr, system_exposed_libraries.c_str())) {
*error_msg = NativeBridgeGetError();
- return false;
+ return nullptr;
}
if (!vendor_public_libraries_.empty()) {
if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
*error_msg = NativeBridgeGetError();
- return false;
+ return nullptr;
}
}
@@ -313,24 +322,19 @@
namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns));
- *ns = native_loader_ns;
- return true;
+ return &(namespaces_.back().second);
}
- bool FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader, NativeLoaderNamespace* ns) {
+ NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
[&](const std::pair<jweak, NativeLoaderNamespace>& value) {
return env->IsSameObject(value.first, class_loader);
});
if (it != namespaces_.end()) {
- if (ns != nullptr) {
- *ns = it->second;
- }
-
- return true;
+ return &it->second;
}
- return false;
+ return nullptr;
}
void Initialize() {
@@ -351,6 +355,8 @@
std::string vndksp_native_libraries_system_config =
root_dir + kVndkspNativeLibrariesSystemConfigPathFromRoot;
+ std::string product_public_native_libraries_dir = "/product/etc";
+
std::string error_msg;
LOG_ALWAYS_FATAL_IF(
!ReadConfig(public_native_libraries_system_config, &sonames, always_true, &error_msg),
@@ -373,7 +379,7 @@
//
// TODO(dimitry): this is a bit misleading since we do not know
// if the vendor public library is going to be opened from /vendor/lib
- // we might as well end up loading them from /system/lib
+ // we might as well end up loading them from /system/lib or /product/lib
// For now we rely on CTS test to catch things like this but
// it should probably be addressed in the future.
for (const auto& soname : sonames) {
@@ -387,48 +393,15 @@
// system libs that are exposed to apps. The libs in the txt files must be
// named as lib<name>.<companyname>.so.
sonames.clear();
- std::string dirname = base::Dirname(public_native_libraries_system_config);
- std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname.c_str()), closedir);
- if (dir != nullptr) {
- // Failing to opening the dir is not an error, which can happen in
- // webview_zygote.
- struct dirent* ent;
- while ((ent = readdir(dir.get())) != nullptr) {
- if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
- continue;
- }
- const std::string filename(ent->d_name);
- if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) &&
- android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) {
- const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen;
- const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen;
- const std::string company_name = filename.substr(start, end - start);
- const std::string config_file_path = dirname + "/" + filename;
- LOG_ALWAYS_FATAL_IF(
- company_name.empty(),
- "Error extracting company name from public native library list file path \"%s\"",
- config_file_path.c_str());
- LOG_ALWAYS_FATAL_IF(
- !ReadConfig(
- config_file_path, &sonames,
- [&company_name](const std::string& soname, std::string* error_msg) {
- if (android::base::StartsWith(soname, "lib") &&
- android::base::EndsWith(soname, "." + company_name + ".so")) {
- return true;
- } else {
- *error_msg = "Library name \"" + soname +
- "\" does not end with the company name: " + company_name + ".";
- return false;
- }
- },
- &error_msg),
- "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
- error_msg.c_str());
- }
- }
- }
+ ReadExtensionLibraries(base::Dirname(public_native_libraries_system_config).c_str(), &sonames);
oem_public_libraries_ = base::Join(sonames, ':');
+ // read /product/etc/public.libraries-<companyname>.txt which contain partner defined
+ // product libs that are exposed to apps.
+ sonames.clear();
+ ReadExtensionLibraries(product_public_native_libraries_dir.c_str(), &sonames);
+ product_public_libraries_ = base::Join(sonames, ':');
+
// Insert VNDK version to llndk and vndksp config file names.
insert_vndk_version_str(&llndk_native_libraries_system_config);
insert_vndk_version_str(&vndksp_native_libraries_system_config);
@@ -448,11 +421,54 @@
vendor_public_libraries_ = base::Join(sonames, ':');
}
- void Reset() {
- namespaces_.clear();
- }
+ void Reset() { namespaces_.clear(); }
private:
+ void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
+ std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
+ if (dir != nullptr) {
+ // Failing to opening the dir is not an error, which can happen in
+ // webview_zygote.
+ while (struct dirent* ent = readdir(dir.get())) {
+ if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
+ continue;
+ }
+ const std::string filename(ent->d_name);
+ if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) &&
+ android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) {
+ const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen;
+ const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen;
+ const std::string company_name = filename.substr(start, end - start);
+ const std::string config_file_path = dirname + "/"s + filename;
+ LOG_ALWAYS_FATAL_IF(
+ company_name.empty(),
+ "Error extracting company name from public native library list file path \"%s\"",
+ config_file_path.c_str());
+
+ std::string error_msg;
+
+ LOG_ALWAYS_FATAL_IF(
+ !ReadConfig(
+ config_file_path, sonames,
+ [&company_name](const std::string& soname, std::string* error_msg) {
+ if (android::base::StartsWith(soname, "lib") &&
+ android::base::EndsWith(soname, "." + company_name + ".so")) {
+ return true;
+ } else {
+ *error_msg = "Library name \"" + soname +
+ "\" does not end with the company name: " + company_name + ".";
+ return false;
+ }
+ },
+ &error_msg),
+ "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
+ error_msg.c_str());
+ }
+ }
+ }
+ }
+
+
bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
const std::function<bool(const std::string& /* soname */,
std::string* /* error_msg */)>& check_soname,
@@ -538,27 +554,27 @@
return env->CallObjectMethod(class_loader, get_parent);
}
- bool FindParentNamespaceByClassLoader(JNIEnv* env,
- jobject class_loader,
- NativeLoaderNamespace* ns) {
+ NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
jobject parent_class_loader = GetParentClassLoader(env, class_loader);
while (parent_class_loader != nullptr) {
- if (FindNamespaceByClassLoader(env, parent_class_loader, ns)) {
- return true;
+ NativeLoaderNamespace* ns;
+ if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
+ return ns;
}
parent_class_loader = GetParentClassLoader(env, parent_class_loader);
}
- return false;
+ return nullptr;
}
bool initialized_;
- std::vector<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
+ std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
std::string system_public_libraries_;
std::string vendor_public_libraries_;
std::string oem_public_libraries_;
+ std::string product_public_libraries_;
std::string system_llndk_libraries_;
std::string system_vndksp_libraries_;
@@ -594,7 +610,6 @@
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
std::string error_msg;
- NativeLoaderNamespace ns;
bool success = g_namespaces->Create(env,
target_sdk_version,
class_loader,
@@ -602,8 +617,7 @@
is_for_vendor,
library_path,
permitted_path,
- &ns,
- &error_msg);
+ &error_msg) != nullptr;
if (!success) {
return env->NewStringUTF(error_msg.c_str());
}
@@ -629,43 +643,24 @@
}
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
- NativeLoaderNamespace ns;
+ NativeLoaderNamespace* ns;
- if (!g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) {
+ if ((ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader)) == nullptr) {
// This is the case where the classloader was not created by ApplicationLoaders
// In this case we create an isolated not-shared namespace for it.
- if (!g_namespaces->Create(env,
- target_sdk_version,
- class_loader,
- false /* is_shared */,
- false /* is_for_vendor */,
- library_path,
- nullptr,
- &ns,
- error_msg)) {
+ if ((ns = g_namespaces->Create(env,
+ target_sdk_version,
+ class_loader,
+ false /* is_shared */,
+ false /* is_for_vendor */,
+ library_path,
+ nullptr,
+ error_msg)) == nullptr) {
return nullptr;
}
}
- if (ns.is_android_namespace()) {
- android_dlextinfo extinfo;
- extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
- extinfo.library_namespace = ns.get_android_ns();
-
- void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo);
- if (handle == nullptr) {
- *error_msg = dlerror();
- }
- *needs_native_bridge = false;
- return handle;
- } else {
- void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns.get_native_bridge_ns());
- if (handle == nullptr) {
- *error_msg = NativeBridgeGetError();
- }
- *needs_native_bridge = true;
- return handle;
- }
+ return OpenNativeLibrary(ns, path, needs_native_bridge, error_msg);
#else
UNUSED(env, target_sdk_version, class_loader);
@@ -721,18 +716,45 @@
}
#if defined(__ANDROID__)
+void* OpenNativeLibrary(NativeLoaderNamespace* ns, const char* path, bool* needs_native_bridge,
+ std::string* error_msg) {
+ if (ns->is_android_namespace()) {
+ android_dlextinfo extinfo;
+ extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
+ extinfo.library_namespace = ns->get_android_ns();
+
+ void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo);
+ if (handle == nullptr) {
+ *error_msg = dlerror();
+ }
+ *needs_native_bridge = false;
+ return handle;
+ } else {
+ void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns->get_native_bridge_ns());
+ if (handle == nullptr) {
+ *error_msg = NativeBridgeGetError();
+ }
+ *needs_native_bridge = true;
+ return handle;
+ }
+}
+
// native_bridge_namespaces are not supported for callers of this function.
// This function will return nullptr in the case when application is running
// on native bridge.
android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
- NativeLoaderNamespace ns;
- if (g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) {
- return ns.is_android_namespace() ? ns.get_android_ns() : nullptr;
+ NativeLoaderNamespace* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
+ if (ns != nullptr) {
+ return ns->is_android_namespace() ? ns->get_android_ns() : nullptr;
}
return nullptr;
}
+NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
+ std::lock_guard<std::mutex> guard(g_namespaces_mutex);
+ return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
+}
#endif
}; // android namespace
diff --git a/libnativeloader/test/Android.bp b/libnativeloader/test/Android.bp
index 5cf88b0..d528f30 100644
--- a/libnativeloader/test/Android.bp
+++ b/libnativeloader/test/Android.bp
@@ -49,3 +49,23 @@
"libbase",
],
}
+
+cc_library {
+ name: "libfoo.product1",
+ srcs: ["test.cpp"],
+ cflags: ["-DLIBNAME=\"libfoo.product1.so\""],
+ product_specific: true,
+ shared_libs: [
+ "libbase",
+ ],
+}
+
+cc_library {
+ name: "libbar.product1",
+ srcs: ["test.cpp"],
+ cflags: ["-DLIBNAME=\"libbar.product1.so\""],
+ product_specific: true,
+ shared_libs: [
+ "libbase",
+ ],
+}
diff --git a/libnativeloader/test/Android.mk b/libnativeloader/test/Android.mk
index e625454..65e7b09 100644
--- a/libnativeloader/test/Android.mk
+++ b/libnativeloader/test/Android.mk
@@ -30,6 +30,13 @@
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
+LOCAL_MODULE := public.libraries-product1.txt
+LOCAL_SRC_FILES:= $(LOCAL_MODULE)
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_PATH := $(TARGET_OUT_PRODUCT_ETC)
+include $(BUILD_PREBUILT)
+
+include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := oemlibrarytest-system
LOCAL_MODULE_TAGS := tests
LOCAL_MANIFEST_FILE := system/AndroidManifest.xml
diff --git a/libnativeloader/test/public.libraries-product1.txt b/libnativeloader/test/public.libraries-product1.txt
new file mode 100644
index 0000000..358154c
--- /dev/null
+++ b/libnativeloader/test/public.libraries-product1.txt
@@ -0,0 +1,2 @@
+libfoo.product1.so
+libbar.product1.so
diff --git a/libnativeloader/test/src/android/test/app/TestActivity.java b/libnativeloader/test/src/android/test/app/TestActivity.java
index 214892d..a7a455d 100644
--- a/libnativeloader/test/src/android/test/app/TestActivity.java
+++ b/libnativeloader/test/src/android/test/app/TestActivity.java
@@ -29,6 +29,8 @@
tryLoadingLib("bar.oem1");
tryLoadingLib("foo.oem2");
tryLoadingLib("bar.oem2");
+ tryLoadingLib("foo.product1");
+ tryLoadingLib("bar.product1");
}
private void tryLoadingLib(String name) {
diff --git a/libnetutils/packet.c b/libnetutils/packet.c
index e53a4c8..9ecdd4f 100644
--- a/libnetutils/packet.c
+++ b/libnetutils/packet.c
@@ -218,6 +218,20 @@
* to construct the pseudo header used in the checksum calculation.
*/
dhcp_size = ntohs(packet.udp.len) - sizeof(packet.udp);
+ /*
+ * check validity of dhcp_size.
+ * 1) cannot be negative or zero.
+ * 2) src buffer contains enough bytes to copy
+ * 3) cannot exceed destination buffer
+ */
+ if ((dhcp_size <= 0) ||
+ ((int)(nread - sizeof(struct iphdr) - sizeof(struct udphdr)) < dhcp_size) ||
+ ((int)sizeof(struct dhcp_msg) < dhcp_size)) {
+#if VERBOSE
+ ALOGD("Malformed Packet");
+#endif
+ return -1;
+ }
saddr = packet.ip.saddr;
daddr = packet.ip.daddr;
nread = ntohs(packet.ip.tot_len);
diff --git a/libpackagelistparser/Android.bp b/libpackagelistparser/Android.bp
index 27693b3..c38594a 100644
--- a/libpackagelistparser/Android.bp
+++ b/libpackagelistparser/Android.bp
@@ -1,6 +1,7 @@
cc_library {
name: "libpackagelistparser",
+ recovery_available: true,
srcs: ["packagelistparser.c"],
cflags: [
"-Wall",
diff --git a/libpixelflinger/fixed.cpp b/libpixelflinger/fixed.cpp
index 5094537..de6b479 100644
--- a/libpixelflinger/fixed.cpp
+++ b/libpixelflinger/fixed.cpp
@@ -70,17 +70,6 @@
// ------------------------------------------------------------------------
-GGLfixed gglFastDivx(GGLfixed n, GGLfixed d)
-{
- if ((d>>24) && ((d>>24)+1)) {
- n >>= 8;
- d >>= 8;
- }
- return gglMulx(n, gglRecip(d));
-}
-
-// ------------------------------------------------------------------------
-
static const GGLfixed ggl_sqrt_reciproc_approx_tab[8] = {
// 1/sqrt(x) with x = 1-N/16, N=[8...1]
0x16A09, 0x15555, 0x143D1, 0x134BF, 0x1279A, 0x11C01, 0x111AC, 0x10865
diff --git a/libpixelflinger/include/private/pixelflinger/ggl_fixed.h b/libpixelflinger/include/private/pixelflinger/ggl_fixed.h
index 51e9e26..7f39e9b 100644
--- a/libpixelflinger/include/private/pixelflinger/ggl_fixed.h
+++ b/libpixelflinger/include/private/pixelflinger/ggl_fixed.h
@@ -86,7 +86,6 @@
GGLfixed gglPowx(GGLfixed x, GGLfixed y) CONST;
GGLfixed gglSqrtx(GGLfixed a) CONST;
GGLfixed gglSqrtRecipx(GGLfixed x) CONST;
-GGLfixed gglFastDivx(GGLfixed n, GGLfixed d) CONST;
int32_t gglMulDivi(int32_t a, int32_t b, int32_t c);
int32_t gglRecipQNormalized(int32_t x, int* exponent);
diff --git a/libprocessgroup/Android.bp b/libprocessgroup/Android.bp
index b0bc497..c38279d 100644
--- a/libprocessgroup/Android.bp
+++ b/libprocessgroup/Android.bp
@@ -2,6 +2,7 @@
srcs: ["processgroup.cpp"],
name: "libprocessgroup",
host_supported: true,
+ recovery_available: true,
shared_libs: ["libbase"],
export_include_dirs: ["include"],
cflags: [
diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp
index 6dfa697..1cebb5d 100644
--- a/libprocessgroup/processgroup.cpp
+++ b/libprocessgroup/processgroup.cpp
@@ -39,12 +39,14 @@
#include <android-base/file.h>
#include <android-base/logging.h>
+#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <private/android_filesystem_config.h>
#include <processgroup/processgroup.h>
+using android::base::GetBoolProperty;
using android::base::StartsWith;
using android::base::StringPrintf;
using android::base::WriteStringToFile;
@@ -62,12 +64,20 @@
static const std::string& GetCgroupRootPath() {
static std::string cgroup_root_path;
std::call_once(init_path_flag, [&]() {
- // Check if mem cgroup is mounted, only then check for write-access to avoid
- // SELinux denials
+ // low-ram devices use per-app memcg by default, unlike high-end ones
+ bool low_ram_device = GetBoolProperty("ro.config.low_ram", false);
+ bool per_app_memcg =
+ GetBoolProperty("ro.config.per_app_memcg", low_ram_device);
+ if (per_app_memcg) {
+ // Check if mem cgroup is mounted, only then check for
+ // write-access to avoid SELinux denials
cgroup_root_path =
- (access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ? ACCT_CGROUP_PATH
- : MEM_CGROUP_PATH);
- });
+ (access(MEM_CGROUP_TASKS, F_OK) || access(MEM_CGROUP_PATH, W_OK) ?
+ ACCT_CGROUP_PATH : MEM_CGROUP_PATH);
+ } else {
+ cgroup_root_path = ACCT_CGROUP_PATH;
+ }
+ });
return cgroup_root_path;
}
diff --git a/libprocinfo/Android.bp b/libprocinfo/Android.bp
index 83b0a7f..15f03d0 100644
--- a/libprocinfo/Android.bp
+++ b/libprocinfo/Android.bp
@@ -27,6 +27,7 @@
name: "libprocinfo",
defaults: ["libprocinfo_defaults"],
vendor_available: true,
+ recovery_available: true,
vndk: {
enabled: true,
},
@@ -59,6 +60,7 @@
host_supported: true,
srcs: [
"process_test.cpp",
+ "process_map_test.cpp",
],
target: {
darwin: {
@@ -83,4 +85,37 @@
suffix: "64",
},
},
+
+ data: [
+ "testdata/*",
+ ],
+
+ test_suites: ["device-tests"],
+}
+
+cc_benchmark {
+ name: "libprocinfo_benchmark",
+ defaults: ["libprocinfo_defaults"],
+ srcs: [
+ "process_map_benchmark.cpp",
+ ],
+ shared_libs: [
+ "libbacktrace",
+ "libbase",
+ "libprocinfo",
+ "libunwindstack",
+ ],
+ compile_multilib: "both",
+ multilib: {
+ lib32: {
+ suffix: "32",
+ },
+ lib64: {
+ suffix: "64",
+ },
+ },
+
+ data: [
+ "testdata/*",
+ ],
}
diff --git a/libprocinfo/include/procinfo/process.h b/libprocinfo/include/procinfo/process.h
index db56fc1..9278e18 100644
--- a/libprocinfo/include/procinfo/process.h
+++ b/libprocinfo/include/procinfo/process.h
@@ -56,23 +56,25 @@
};
// Parse the contents of /proc/<tid>/status into |process_info|.
-bool GetProcessInfo(pid_t tid, ProcessInfo* process_info);
+bool GetProcessInfo(pid_t tid, ProcessInfo* process_info, std::string* error = nullptr);
// Parse the contents of <fd>/status into |process_info|.
// |fd| should be an fd pointing at a /proc/<pid> directory.
-bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info);
+bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info, std::string* error = nullptr);
// Fetch the list of threads from a given process's /proc/<pid> directory.
// |fd| should be an fd pointing at a /proc/<pid> directory.
template <typename Collection>
-auto GetProcessTidsFromProcPidFd(int fd, Collection* out) ->
+auto GetProcessTidsFromProcPidFd(int fd, Collection* out, std::string* error = nullptr) ->
typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type {
out->clear();
int task_fd = openat(fd, "task", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
std::unique_ptr<DIR, int (*)(DIR*)> dir(fdopendir(task_fd), closedir);
if (!dir) {
- PLOG(ERROR) << "failed to open task directory";
+ if (error != nullptr) {
+ *error = "failed to open task directory";
+ }
return false;
}
@@ -81,7 +83,9 @@
if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0) {
pid_t tid;
if (!android::base::ParseInt(dent->d_name, &tid, 1, std::numeric_limits<pid_t>::max())) {
- LOG(ERROR) << "failed to parse task id: " << dent->d_name;
+ if (error != nullptr) {
+ *error = std::string("failed to parse task id: ") + dent->d_name;
+ }
return false;
}
@@ -93,21 +97,25 @@
}
template <typename Collection>
-auto GetProcessTids(pid_t pid, Collection* out) ->
+auto GetProcessTids(pid_t pid, Collection* out, std::string* error = nullptr) ->
typename std::enable_if<sizeof(typename Collection::value_type) >= sizeof(pid_t), bool>::type {
char task_path[PATH_MAX];
if (snprintf(task_path, PATH_MAX, "/proc/%d", pid) >= PATH_MAX) {
- LOG(ERROR) << "task path overflow (pid = " << pid << ")";
+ if (error != nullptr) {
+ *error = "task path overflow (pid = " + std::to_string(pid) + ")";
+ }
return false;
}
android::base::unique_fd fd(open(task_path, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
if (fd == -1) {
- PLOG(ERROR) << "failed to open " << task_path;
+ if (error != nullptr) {
+ *error = std::string("failed to open ") + task_path;
+ }
return false;
}
- return GetProcessTidsFromProcPidFd(fd.get(), out);
+ return GetProcessTidsFromProcPidFd(fd.get(), out, error);
}
#endif
diff --git a/libprocinfo/include/procinfo/process_map.h b/libprocinfo/include/procinfo/process_map.h
new file mode 100644
index 0000000..3771f9f
--- /dev/null
+++ b/libprocinfo/include/procinfo/process_map.h
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include <functional>
+#include <string>
+
+#include <android-base/file.h>
+
+namespace android {
+namespace procinfo {
+
+template <class CallbackType>
+bool ReadMapFileContent(char* content, const CallbackType& callback) {
+ uint64_t start_addr;
+ uint64_t end_addr;
+ uint16_t flags;
+ uint64_t pgoff;
+ char* next_line = content;
+ char* p;
+
+ auto pass_space = [&]() {
+ if (*p != ' ') {
+ return false;
+ }
+ while (*p == ' ') {
+ p++;
+ }
+ return true;
+ };
+
+ auto pass_xdigit = [&]() {
+ if (!isxdigit(*p)) {
+ return false;
+ }
+ do {
+ p++;
+ } while (isxdigit(*p));
+ return true;
+ };
+
+ while (next_line != nullptr && *next_line != '\0') {
+ p = next_line;
+ next_line = strchr(next_line, '\n');
+ if (next_line != nullptr) {
+ *next_line = '\0';
+ next_line++;
+ }
+ // Parse line like: 00400000-00409000 r-xp 00000000 fc:00 426998 /usr/lib/gvfs/gvfsd-http
+ char* end;
+ // start_addr
+ start_addr = strtoull(p, &end, 16);
+ if (end == p || *end != '-') {
+ return false;
+ }
+ p = end + 1;
+ // end_addr
+ end_addr = strtoull(p, &end, 16);
+ if (end == p) {
+ return false;
+ }
+ p = end;
+ if (!pass_space()) {
+ return false;
+ }
+ // flags
+ flags = 0;
+ if (*p == 'r') {
+ flags |= PROT_READ;
+ } else if (*p != '-') {
+ return false;
+ }
+ p++;
+ if (*p == 'w') {
+ flags |= PROT_WRITE;
+ } else if (*p != '-') {
+ return false;
+ }
+ p++;
+ if (*p == 'x') {
+ flags |= PROT_EXEC;
+ } else if (*p != '-') {
+ return false;
+ }
+ p++;
+ if (*p != 'p' && *p != 's') {
+ return false;
+ }
+ p++;
+ if (!pass_space()) {
+ return false;
+ }
+ // pgoff
+ pgoff = strtoull(p, &end, 16);
+ if (end == p) {
+ return false;
+ }
+ p = end;
+ if (!pass_space()) {
+ return false;
+ }
+ // major:minor
+ if (!pass_xdigit() || *p++ != ':' || !pass_xdigit() || !pass_space()) {
+ return false;
+ }
+ // inode
+ if (!pass_xdigit() || (*p != '\0' && !pass_space())) {
+ return false;
+ }
+ // filename
+ callback(start_addr, end_addr, flags, pgoff, p);
+ }
+ return true;
+}
+
+inline bool ReadMapFile(
+ const std::string& map_file,
+ const std::function<void(uint64_t, uint64_t, uint16_t, uint64_t, const char*)>& callback) {
+ std::string content;
+ if (!android::base::ReadFileToString(map_file, &content)) {
+ return false;
+ }
+ return ReadMapFileContent(&content[0], callback);
+}
+
+inline bool ReadProcessMaps(
+ pid_t pid,
+ const std::function<void(uint64_t, uint64_t, uint16_t, uint64_t, const char*)>& callback) {
+ return ReadMapFile("/proc/" + std::to_string(pid) + "/maps", callback);
+}
+
+} /* namespace procinfo */
+} /* namespace android */
diff --git a/libprocinfo/process.cpp b/libprocinfo/process.cpp
index 6e5be6e..9194cf3 100644
--- a/libprocinfo/process.cpp
+++ b/libprocinfo/process.cpp
@@ -31,17 +31,19 @@
namespace android {
namespace procinfo {
-bool GetProcessInfo(pid_t tid, ProcessInfo* process_info) {
+bool GetProcessInfo(pid_t tid, ProcessInfo* process_info, std::string* error) {
char path[PATH_MAX];
snprintf(path, sizeof(path), "/proc/%d", tid);
unique_fd dirfd(open(path, O_DIRECTORY | O_RDONLY));
if (dirfd == -1) {
- PLOG(ERROR) << "failed to open " << path;
+ if (error != nullptr) {
+ *error = std::string("failed to open ") + path;
+ }
return false;
}
- return GetProcessInfoFromProcPidFd(dirfd.get(), process_info);
+ return GetProcessInfoFromProcPidFd(dirfd.get(), process_info, error);
}
static ProcessState parse_state(const char* state) {
@@ -62,17 +64,21 @@
}
}
-bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info) {
+bool GetProcessInfoFromProcPidFd(int fd, ProcessInfo* process_info, std::string* error) {
int status_fd = openat(fd, "status", O_RDONLY | O_CLOEXEC);
if (status_fd == -1) {
- PLOG(ERROR) << "failed to open status fd in GetProcessInfoFromProcPidFd";
+ if (error != nullptr) {
+ *error = "failed to open status fd in GetProcessInfoFromProcPidFd";
+ }
return false;
}
std::unique_ptr<FILE, decltype(&fclose)> fp(fdopen(status_fd, "r"), fclose);
if (!fp) {
- PLOG(ERROR) << "failed to open status file in GetProcessInfoFromProcPidFd";
+ if (error != nullptr) {
+ *error = "failed to open status file in GetProcessInfoFromProcPidFd";
+ }
close(status_fd);
return false;
}
diff --git a/libprocinfo/process_map_benchmark.cpp b/libprocinfo/process_map_benchmark.cpp
new file mode 100644
index 0000000..d9e8a4d
--- /dev/null
+++ b/libprocinfo/process_map_benchmark.cpp
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2018 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 <procinfo/process_map.h>
+
+#include <string.h>
+
+#include <string>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <backtrace/BacktraceMap.h>
+#include <unwindstack/Maps.h>
+
+#include <benchmark/benchmark.h>
+
+struct MapInfo {
+ uint64_t start;
+ uint64_t end;
+ uint16_t flags;
+ uint64_t pgoff;
+ const std::string name;
+
+ MapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name)
+ : start(start), end(end), flags(flags), pgoff(pgoff), name(name) {}
+};
+
+static void BM_ReadMapFile(benchmark::State& state) {
+ std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
+ for (auto _ : state) {
+ std::vector<MapInfo> maps;
+ android::procinfo::ReadMapFile(
+ map_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
+ const char* name) { maps.emplace_back(start, end, flags, pgoff, name); });
+ CHECK_EQ(maps.size(), 2043u);
+ }
+}
+BENCHMARK(BM_ReadMapFile);
+
+static void BM_unwindstack_FileMaps(benchmark::State& state) {
+ std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
+ for (auto _ : state) {
+ unwindstack::FileMaps maps(map_file);
+ maps.Parse();
+ CHECK_EQ(maps.Total(), 2043u);
+ }
+}
+BENCHMARK(BM_unwindstack_FileMaps);
+
+static void BM_unwindstack_BufferMaps(benchmark::State& state) {
+ std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
+ std::string content;
+ CHECK(android::base::ReadFileToString(map_file, &content));
+ for (auto _ : state) {
+ unwindstack::BufferMaps maps(content.c_str());
+ maps.Parse();
+ CHECK_EQ(maps.Total(), 2043u);
+ }
+}
+BENCHMARK(BM_unwindstack_BufferMaps);
+
+static void BM_backtrace_BacktraceMap(benchmark::State& state) {
+ pid_t pid = getpid();
+ for (auto _ : state) {
+ BacktraceMap* map = BacktraceMap::Create(pid, true);
+ CHECK(map != nullptr);
+ delete map;
+ }
+}
+BENCHMARK(BM_backtrace_BacktraceMap);
+
+BENCHMARK_MAIN();
diff --git a/libprocinfo/process_map_test.cpp b/libprocinfo/process_map_test.cpp
new file mode 100644
index 0000000..900fd85
--- /dev/null
+++ b/libprocinfo/process_map_test.cpp
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2018 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 <procinfo/process_map.h>
+
+#include <string>
+
+#include <android-base/file.h>
+
+#include <gtest/gtest.h>
+
+struct MapInfo {
+ uint64_t start;
+ uint64_t end;
+ uint16_t flags;
+ uint64_t pgoff;
+ const std::string name;
+
+ MapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name)
+ : start(start), end(end), flags(flags), pgoff(pgoff), name(name) {}
+};
+
+TEST(process_map, smoke) {
+ std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
+ std::vector<MapInfo> maps;
+ ASSERT_TRUE(android::procinfo::ReadMapFile(
+ map_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
+ const char* name) { maps.emplace_back(start, end, flags, pgoff, name); }));
+ ASSERT_EQ(2043u, maps.size());
+ ASSERT_EQ(maps[0].start, 0x12c00000ULL);
+ ASSERT_EQ(maps[0].end, 0x2ac00000ULL);
+ ASSERT_EQ(maps[0].flags, PROT_READ | PROT_WRITE);
+ ASSERT_EQ(maps[0].pgoff, 0ULL);
+ ASSERT_EQ(maps[0].name, "/dev/ashmem/dalvik-main space (region space) (deleted)");
+ ASSERT_EQ(maps[876].start, 0x70e6c4f000ULL);
+ ASSERT_EQ(maps[876].end, 0x70e6c6b000ULL);
+ ASSERT_EQ(maps[876].flags, PROT_READ | PROT_EXEC);
+ ASSERT_EQ(maps[876].pgoff, 0ULL);
+ ASSERT_EQ(maps[876].name, "/system/lib64/libutils.so");
+ ASSERT_EQ(maps[1260].start, 0x70e96fa000ULL);
+ ASSERT_EQ(maps[1260].end, 0x70e96fb000ULL);
+ ASSERT_EQ(maps[1260].flags, PROT_READ);
+ ASSERT_EQ(maps[1260].pgoff, 0ULL);
+ ASSERT_EQ(maps[1260].name,
+ "/dev/ashmem/dalvik-classes.dex extracted in memory from "
+ "/data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/base.apk (deleted)");
+}
diff --git a/libprocinfo/testdata/maps b/libprocinfo/testdata/maps
new file mode 100644
index 0000000..3b312e3
--- /dev/null
+++ b/libprocinfo/testdata/maps
@@ -0,0 +1,2043 @@
+12c00000-2ac00000 rw-p 00000000 00:05 10267643 /dev/ashmem/dalvik-main space (region space) (deleted)
+6fb5d000-6fd6e000 rw-p 00000000 103:1d 639511 /data/dalvik-cache/arm64/system@framework@boot.art
+6fd6e000-6fd82000 r--p 00211000 103:1d 639511 /data/dalvik-cache/arm64/system@framework@boot.art
+6fd82000-6fe47000 rw-p 00000000 103:1d 639514 /data/dalvik-cache/arm64/system@framework@boot-core-libart.art
+6fe47000-6fe52000 r--p 000c5000 103:1d 639514 /data/dalvik-cache/arm64/system@framework@boot-core-libart.art
+6fe52000-6fe84000 rw-p 00000000 103:1d 639517 /data/dalvik-cache/arm64/system@framework@boot-conscrypt.art
+6fe84000-6fe87000 r--p 00032000 103:1d 639517 /data/dalvik-cache/arm64/system@framework@boot-conscrypt.art
+6fe87000-6feb2000 rw-p 00000000 103:1d 639520 /data/dalvik-cache/arm64/system@framework@boot-okhttp.art
+6feb2000-6feb5000 r--p 0002b000 103:1d 639520 /data/dalvik-cache/arm64/system@framework@boot-okhttp.art
+6feb5000-6fef4000 rw-p 00000000 103:1d 639523 /data/dalvik-cache/arm64/system@framework@boot-bouncycastle.art
+6fef4000-6fefb000 r--p 0003f000 103:1d 639523 /data/dalvik-cache/arm64/system@framework@boot-bouncycastle.art
+6fefb000-6ff3f000 rw-p 00000000 103:1d 639526 /data/dalvik-cache/arm64/system@framework@boot-apache-xml.art
+6ff3f000-6ff45000 r--p 00044000 103:1d 639526 /data/dalvik-cache/arm64/system@framework@boot-apache-xml.art
+6ff45000-6ff7a000 rw-p 00000000 103:1d 639529 /data/dalvik-cache/arm64/system@framework@boot-ext.art
+6ff7a000-6ff85000 r--p 00035000 103:1d 639529 /data/dalvik-cache/arm64/system@framework@boot-ext.art
+6ff85000-70594000 rw-p 00000000 103:1d 639532 /data/dalvik-cache/arm64/system@framework@boot-framework.art
+70594000-705cb000 r--p 0060f000 103:1d 639532 /data/dalvik-cache/arm64/system@framework@boot-framework.art
+705cb000-7061f000 rw-p 00000000 103:1d 639535 /data/dalvik-cache/arm64/system@framework@boot-telephony-common.art
+7061f000-70629000 r--p 00054000 103:1d 639535 /data/dalvik-cache/arm64/system@framework@boot-telephony-common.art
+70629000-70635000 rw-p 00000000 103:1d 639538 /data/dalvik-cache/arm64/system@framework@boot-voip-common.art
+70635000-70636000 r--p 0000c000 103:1d 639538 /data/dalvik-cache/arm64/system@framework@boot-voip-common.art
+70636000-70644000 rw-p 00000000 103:1d 639541 /data/dalvik-cache/arm64/system@framework@boot-ims-common.art
+70644000-70645000 r--p 0000e000 103:1d 639541 /data/dalvik-cache/arm64/system@framework@boot-ims-common.art
+70645000-70648000 rw-p 00000000 103:1d 639544 /data/dalvik-cache/arm64/system@framework@boot-android.hidl.base-V1.0-java.art
+70648000-7064c000 rw-p 00000000 103:1d 639547 /data/dalvik-cache/arm64/system@framework@boot-android.hidl.manager-V1.0-java.art
+7064c000-7064d000 r--p 00004000 103:1d 639547 /data/dalvik-cache/arm64/system@framework@boot-android.hidl.manager-V1.0-java.art
+7064d000-7064e000 rw-p 00000000 103:1d 639550 /data/dalvik-cache/arm64/system@framework@boot-framework-oahl-backward-compatibility.art
+7064e000-70652000 rw-p 00000000 103:1d 639553 /data/dalvik-cache/arm64/system@framework@boot-android.test.base.art
+70652000-70653000 r--p 00004000 103:1d 639553 /data/dalvik-cache/arm64/system@framework@boot-android.test.base.art
+70653000-70654000 rw-p 00000000 103:1d 639556 /data/dalvik-cache/arm64/system@framework@boot-com.google.vr.platform.art
+70654000-70655000 r--p 00001000 103:1d 639556 /data/dalvik-cache/arm64/system@framework@boot-com.google.vr.platform.art
+70655000-70731000 r--p 00000000 fc:00 940 /system/framework/arm64/boot.oat
+70731000-709ca000 r-xp 000dc000 fc:00 940 /system/framework/arm64/boot.oat
+709ca000-709cb000 rw-p 00000000 00:00 0 [anon:.bss]
+709cb000-70e4c000 r--s 00000000 fc:00 961 /system/framework/boot.vdex
+70e4c000-70e4d000 r--p 00375000 fc:00 940 /system/framework/arm64/boot.oat
+70e4d000-70e4e000 rw-p 00376000 fc:00 940 /system/framework/arm64/boot.oat
+70e4e000-70eab000 r--p 00000000 fc:00 916 /system/framework/arm64/boot-core-libart.oat
+70eab000-70fad000 r-xp 0005d000 fc:00 916 /system/framework/arm64/boot-core-libart.oat
+70fad000-70fae000 rw-p 00000000 00:00 0 [anon:.bss]
+70fae000-712a9000 r--s 00000000 fc:00 702 /system/framework/boot-core-libart.vdex
+712a9000-712aa000 r--p 0015f000 fc:00 916 /system/framework/arm64/boot-core-libart.oat
+712aa000-712ab000 rw-p 00160000 fc:00 916 /system/framework/arm64/boot-core-libart.oat
+712ab000-712bb000 r--p 00000000 fc:00 941 /system/framework/arm64/boot-conscrypt.oat
+712bb000-712e4000 r-xp 00010000 fc:00 941 /system/framework/arm64/boot-conscrypt.oat
+712e4000-712e5000 rw-p 00000000 00:00 0 [anon:.bss]
+712e5000-71346000 r--s 00000000 fc:00 970 /system/framework/boot-conscrypt.vdex
+71346000-71347000 r--p 00039000 fc:00 941 /system/framework/arm64/boot-conscrypt.oat
+71347000-71348000 rw-p 0003a000 fc:00 941 /system/framework/arm64/boot-conscrypt.oat
+71348000-71361000 r--p 00000000 fc:00 908 /system/framework/arm64/boot-okhttp.oat
+71361000-713a3000 r-xp 00019000 fc:00 908 /system/framework/arm64/boot-okhttp.oat
+713a3000-713a4000 rw-p 00000000 00:00 0 [anon:.bss]
+713a4000-71403000 r--s 00000000 fc:00 886 /system/framework/boot-okhttp.vdex
+71403000-71404000 r--p 0005b000 fc:00 908 /system/framework/arm64/boot-okhttp.oat
+71404000-71405000 rw-p 0005c000 fc:00 908 /system/framework/arm64/boot-okhttp.oat
+71405000-71415000 r--p 00000000 fc:00 936 /system/framework/arm64/boot-bouncycastle.oat
+71415000-71437000 r-xp 00010000 fc:00 936 /system/framework/arm64/boot-bouncycastle.oat
+71437000-71438000 rw-p 00000000 00:00 0 [anon:.bss]
+71438000-7157b000 r--s 00000000 fc:00 1006 /system/framework/boot-bouncycastle.vdex
+7157b000-7157c000 r--p 00032000 fc:00 936 /system/framework/arm64/boot-bouncycastle.oat
+7157c000-7157d000 rw-p 00033000 fc:00 936 /system/framework/arm64/boot-bouncycastle.oat
+7157d000-71583000 r--p 00000000 fc:00 932 /system/framework/arm64/boot-apache-xml.oat
+71583000-71584000 r-xp 00006000 fc:00 932 /system/framework/arm64/boot-apache-xml.oat
+71584000-716a7000 r--s 00000000 fc:00 883 /system/framework/boot-apache-xml.vdex
+716a7000-716a8000 r--p 00007000 fc:00 932 /system/framework/arm64/boot-apache-xml.oat
+716a8000-716a9000 rw-p 00008000 fc:00 932 /system/framework/arm64/boot-apache-xml.oat
+716a9000-716b5000 r--p 00000000 fc:00 891 /system/framework/arm64/boot-ext.oat
+716b5000-716cc000 r-xp 0000c000 fc:00 891 /system/framework/arm64/boot-ext.oat
+716cc000-716cd000 rw-p 00000000 00:00 0 [anon:.bss]
+716cd000-717b8000 r--s 00000000 fc:00 879 /system/framework/boot-ext.vdex
+717b8000-717b9000 r--p 00023000 fc:00 891 /system/framework/arm64/boot-ext.oat
+717b9000-717ba000 rw-p 00024000 fc:00 891 /system/framework/arm64/boot-ext.oat
+717ba000-71aeb000 r--p 00000000 fc:00 943 /system/framework/arm64/boot-framework.oat
+71aeb000-72390000 r-xp 00331000 fc:00 943 /system/framework/arm64/boot-framework.oat
+72390000-72396000 rw-p 00000000 00:00 0 [anon:.bss]
+72396000-73746000 r--s 00000000 fc:00 985 /system/framework/boot-framework.vdex
+73746000-73747000 r--p 00bd6000 fc:00 943 /system/framework/arm64/boot-framework.oat
+73747000-73748000 rw-p 00bd7000 fc:00 943 /system/framework/arm64/boot-framework.oat
+73748000-73780000 r--p 00000000 fc:00 893 /system/framework/arm64/boot-telephony-common.oat
+73780000-73818000 r-xp 00038000 fc:00 893 /system/framework/arm64/boot-telephony-common.oat
+73818000-7381a000 rw-p 00000000 00:00 0 [anon:.bss]
+7381a000-73af0000 r--s 00000000 fc:00 697 /system/framework/boot-telephony-common.vdex
+73af0000-73af1000 r--p 000d0000 fc:00 893 /system/framework/arm64/boot-telephony-common.oat
+73af1000-73af2000 rw-p 000d1000 fc:00 893 /system/framework/arm64/boot-telephony-common.oat
+73af2000-73af6000 r--p 00000000 fc:00 922 /system/framework/arm64/boot-voip-common.oat
+73af6000-73af8000 r-xp 00004000 fc:00 922 /system/framework/arm64/boot-voip-common.oat
+73af8000-73af9000 rw-p 00000000 00:00 0 [anon:.bss]
+73af9000-73b1e000 r--s 00000000 fc:00 959 /system/framework/boot-voip-common.vdex
+73b1e000-73b1f000 r--p 00006000 fc:00 922 /system/framework/arm64/boot-voip-common.oat
+73b1f000-73b20000 rw-p 00007000 fc:00 922 /system/framework/arm64/boot-voip-common.oat
+73b20000-73b23000 r--p 00000000 fc:00 918 /system/framework/arm64/boot-ims-common.oat
+73b23000-73b25000 r-xp 00003000 fc:00 918 /system/framework/arm64/boot-ims-common.oat
+73b25000-73b26000 rw-p 00000000 00:00 0 [anon:.bss]
+73b26000-73b48000 r--s 00000000 fc:00 957 /system/framework/boot-ims-common.vdex
+73b48000-73b49000 r--p 00005000 fc:00 918 /system/framework/arm64/boot-ims-common.oat
+73b49000-73b4a000 rw-p 00006000 fc:00 918 /system/framework/arm64/boot-ims-common.oat
+73b4a000-73b4d000 r--p 00000000 fc:00 909 /system/framework/arm64/boot-android.hidl.base-V1.0-java.oat
+73b4d000-73b4e000 r-xp 00003000 fc:00 909 /system/framework/arm64/boot-android.hidl.base-V1.0-java.oat
+73b4e000-73b55000 r--s 00000000 fc:00 972 /system/framework/boot-android.hidl.base-V1.0-java.vdex
+73b55000-73b56000 r--p 00004000 fc:00 909 /system/framework/arm64/boot-android.hidl.base-V1.0-java.oat
+73b56000-73b57000 rw-p 00005000 fc:00 909 /system/framework/arm64/boot-android.hidl.base-V1.0-java.oat
+73b57000-73b5a000 r--p 00000000 fc:00 954 /system/framework/arm64/boot-android.hidl.manager-V1.0-java.oat
+73b5a000-73b5c000 r-xp 00003000 fc:00 954 /system/framework/arm64/boot-android.hidl.manager-V1.0-java.oat
+73b5c000-73b5d000 rw-p 00000000 00:00 0 [anon:.bss]
+73b5d000-73b68000 r--s 00000000 fc:00 704 /system/framework/boot-android.hidl.manager-V1.0-java.vdex
+73b68000-73b69000 r--p 00005000 fc:00 954 /system/framework/arm64/boot-android.hidl.manager-V1.0-java.oat
+73b69000-73b6a000 rw-p 00006000 fc:00 954 /system/framework/arm64/boot-android.hidl.manager-V1.0-java.oat
+73b6a000-73b6d000 r--p 00000000 fc:00 904 /system/framework/arm64/boot-framework-oahl-backward-compatibility.oat
+73b6d000-73b6e000 r-xp 00003000 fc:00 904 /system/framework/arm64/boot-framework-oahl-backward-compatibility.oat
+73b6e000-73b6f000 r--s 00000000 fc:00 994 /system/framework/boot-framework-oahl-backward-compatibility.vdex
+73b6f000-73b70000 r--p 00004000 fc:00 904 /system/framework/arm64/boot-framework-oahl-backward-compatibility.oat
+73b70000-73b71000 rw-p 00005000 fc:00 904 /system/framework/arm64/boot-framework-oahl-backward-compatibility.oat
+73b71000-73b75000 r--p 00000000 fc:00 896 /system/framework/arm64/boot-android.test.base.oat
+73b75000-73b79000 r-xp 00004000 fc:00 896 /system/framework/arm64/boot-android.test.base.oat
+73b79000-73b7a000 rw-p 00000000 00:00 0 [anon:.bss]
+73b7a000-73b82000 r--s 00000000 fc:00 706 /system/framework/boot-android.test.base.vdex
+73b82000-73b83000 r--p 00008000 fc:00 896 /system/framework/arm64/boot-android.test.base.oat
+73b83000-73b84000 rw-p 00009000 fc:00 896 /system/framework/arm64/boot-android.test.base.oat
+73b84000-73b87000 r--p 00000000 fc:00 899 /system/framework/arm64/boot-com.google.vr.platform.oat
+73b87000-73b88000 r-xp 00003000 fc:00 899 /system/framework/arm64/boot-com.google.vr.platform.oat
+73b88000-73b89000 r--s 00000000 fc:00 884 /system/framework/boot-com.google.vr.platform.vdex
+73b89000-73b8a000 r--p 00004000 fc:00 899 /system/framework/arm64/boot-com.google.vr.platform.oat
+73b8a000-73b8b000 rw-p 00005000 fc:00 899 /system/framework/arm64/boot-com.google.vr.platform.oat
+73b8b000-73b93000 rw-p 00000000 00:05 10267640 /dev/ashmem/dalvik-non moving space (deleted)
+73b93000-77b8b000 ---p 00008000 00:05 10267640 /dev/ashmem/dalvik-non moving space (deleted)
+77b8b000-97b8b000 rw-p 00000000 00:05 10267645 /dev/ashmem/dalvik-free list large object space (deleted)
+97b8b000-99b8b000 rw-p 00000000 00:05 10270989 /dev/ashmem/dalvik-data-code-cache (deleted)
+99b8b000-9bb8b000 r-xp 00000000 00:05 10270990 /dev/ashmem/dalvik-jit-code-cache (deleted)
+ebad6000-ebad7000 ---p 00000000 00:05 10269717 /dev/ashmem/dalvik-Sentinel fault page (deleted)
+7ffb6e000-7ffb76000 rw-s 000e5000 00:10 20630 /dev/kgsl-3d0
+7ffb76000-7ffb78000 rw-s 000e0000 00:10 20630 /dev/kgsl-3d0
+7ffbc3000-7ffbc4000 rw-s 000e8000 00:10 20630 /dev/kgsl-3d0
+7ffbc4000-7ffbc5000 rw-s 000e7000 00:10 20630 /dev/kgsl-3d0
+7ffbc6000-7ffbce000 rw-s 000e4000 00:10 20630 /dev/kgsl-3d0
+7ffbd0000-7ffbd2000 rw-s 000df000 00:10 20630 /dev/kgsl-3d0
+7ffbd2000-7ffbd4000 rw-s 000de000 00:10 20630 /dev/kgsl-3d0
+7ffbd4000-7ffbd6000 rw-s 000dd000 00:10 20630 /dev/kgsl-3d0
+7ffbd6000-7ffbd8000 rw-s 000dc000 00:10 20630 /dev/kgsl-3d0
+7ffbd8000-7ffbda000 rw-s 000db000 00:10 20630 /dev/kgsl-3d0
+7ffbda000-7ffbdc000 rw-s 000da000 00:10 20630 /dev/kgsl-3d0
+7ffbdd000-7ffbde000 rw-s 000ec000 00:10 20630 /dev/kgsl-3d0
+7ffbde000-7ffbe0000 rw-s 000d8000 00:10 20630 /dev/kgsl-3d0
+7ffce1000-7ffce2000 rw-s 000e6000 00:10 20630 /dev/kgsl-3d0
+7ffce2000-7ffce4000 rw-s 000d9000 00:10 20630 /dev/kgsl-3d0
+7ffce4000-7ffce8000 rw-s 000d4000 00:10 20630 /dev/kgsl-3d0
+7ffce8000-7ffcf8000 rw-s 000d2000 00:10 20630 /dev/kgsl-3d0
+7ffcf8000-7ffd08000 rw-s 000d1000 00:10 20630 /dev/kgsl-3d0
+7ffd08000-7ffd10000 rw-s 000d0000 00:10 20630 /dev/kgsl-3d0
+7ffd14000-7ffd18000 rw-s 000cd000 00:10 20630 /dev/kgsl-3d0
+7ffd18000-7ffd28000 rw-s 000cc000 00:10 20630 /dev/kgsl-3d0
+7ffd28000-7ffd38000 rw-s 000cb000 00:10 20630 /dev/kgsl-3d0
+7ffd38000-7ffd48000 rw-s 000ca000 00:10 20630 /dev/kgsl-3d0
+7ffd48000-7ffd58000 rw-s 000c9000 00:10 20630 /dev/kgsl-3d0
+7ffd58000-7ffd68000 rw-s 000c8000 00:10 20630 /dev/kgsl-3d0
+7ffd68000-7ffd6c000 rw-s 000c7000 00:10 20630 /dev/kgsl-3d0
+7ffdb1000-7ffdb2000 rw-s 000e3000 00:10 20630 /dev/kgsl-3d0
+7ffdb4000-7ffdb5000 rw-s 000e2000 00:10 20630 /dev/kgsl-3d0
+7ffdb5000-7ffdb7000 rw-s 000d7000 00:10 20630 /dev/kgsl-3d0
+7ffdb7000-7ffdb8000 rw-s 000c2000 00:10 20630 /dev/kgsl-3d0
+7ffdb8000-7ffdbc000 rw-s 000c0000 00:10 20630 /dev/kgsl-3d0
+7ffdbc000-7ffdc0000 rw-s 000be000 00:10 20630 /dev/kgsl-3d0
+7ffdc0000-7ffe00000 rw-s 000bb000 00:10 20630 /dev/kgsl-3d0
+7ffe00000-7ffe20000 rw-s 000ba000 00:10 20630 /dev/kgsl-3d0
+7ffe20000-7ffee0000 rw-s 000b9000 00:10 20630 /dev/kgsl-3d0
+7ffee1000-7ffee3000 rw-s 000c1000 00:10 20630 /dev/kgsl-3d0
+7ffee3000-7ffee4000 rw-s 000bf000 00:10 20630 /dev/kgsl-3d0
+7ffee4000-7ffee8000 rw-s 000bd000 00:10 20630 /dev/kgsl-3d0
+7ffee8000-7ffee9000 rw-s 000bc000 00:10 20630 /dev/kgsl-3d0
+7ffeea000-7ffeeb000 rw-s 000e1000 00:10 20630 /dev/kgsl-3d0
+7ffeeb000-7ffeec000 rw-s 000b6000 00:10 20630 /dev/kgsl-3d0
+7ffeec000-7ffeed000 rw-s 000b5000 00:10 20630 /dev/kgsl-3d0
+7ffeed000-7ffeee000 rw-s 000b4000 00:10 20630 /dev/kgsl-3d0
+7ffeee000-7ffeef000 rw-s 000b3000 00:10 20630 /dev/kgsl-3d0
+7ffeef000-7ffef0000 rw-s 000b2000 00:10 20630 /dev/kgsl-3d0
+7ffef0000-7ffef1000 rw-s 000b1000 00:10 20630 /dev/kgsl-3d0
+7ffef1000-7ffef2000 rw-s 000b0000 00:10 20630 /dev/kgsl-3d0
+7ffef2000-7ffef3000 rw-s 000af000 00:10 20630 /dev/kgsl-3d0
+7ffef3000-7ffef4000 rw-s 000ae000 00:10 20630 /dev/kgsl-3d0
+7ffef4000-7ffef5000 rw-s 000ad000 00:10 20630 /dev/kgsl-3d0
+7ffef5000-7ffef6000 rw-s 000ac000 00:10 20630 /dev/kgsl-3d0
+7ffef6000-7ffef7000 rw-s 000ab000 00:10 20630 /dev/kgsl-3d0
+7ffef7000-7ffef8000 rw-s 000aa000 00:10 20630 /dev/kgsl-3d0
+7ffef8000-7ffef9000 rw-s 000a9000 00:10 20630 /dev/kgsl-3d0
+7ffef9000-7ffefa000 rw-s 000a8000 00:10 20630 /dev/kgsl-3d0
+7ffefa000-7ffefb000 rw-s 000a7000 00:10 20630 /dev/kgsl-3d0
+7ffefb000-7ffefc000 rw-s 000a6000 00:10 20630 /dev/kgsl-3d0
+7ffefc000-7ffefd000 rw-s 000a5000 00:10 20630 /dev/kgsl-3d0
+7ffefd000-7ffefe000 rw-s 000a4000 00:10 20630 /dev/kgsl-3d0
+7ffefe000-7ffeff000 rw-s 000a3000 00:10 20630 /dev/kgsl-3d0
+7ffeff000-7fff00000 rw-s 000a2000 00:10 20630 /dev/kgsl-3d0
+7fff00000-7fff01000 rw-s 000a1000 00:10 20630 /dev/kgsl-3d0
+7fff01000-7fff02000 rw-s 000a0000 00:10 20630 /dev/kgsl-3d0
+7fff02000-7fff03000 rw-s 0009f000 00:10 20630 /dev/kgsl-3d0
+7fff03000-7fff04000 rw-s 0009e000 00:10 20630 /dev/kgsl-3d0
+7fff04000-7fff05000 rw-s 0009d000 00:10 20630 /dev/kgsl-3d0
+7fff05000-7fff06000 rw-s 0009c000 00:10 20630 /dev/kgsl-3d0
+7fff06000-7fff07000 rw-s 0009b000 00:10 20630 /dev/kgsl-3d0
+7fff07000-7fff08000 rw-s 0009a000 00:10 20630 /dev/kgsl-3d0
+7fff08000-7fff09000 rw-s 00099000 00:10 20630 /dev/kgsl-3d0
+7fff09000-7fff0a000 rw-s 00098000 00:10 20630 /dev/kgsl-3d0
+7fff0a000-7fff0b000 rw-s 00097000 00:10 20630 /dev/kgsl-3d0
+7fff0b000-7fff0c000 rw-s 00096000 00:10 20630 /dev/kgsl-3d0
+7fff0c000-7fff0d000 rw-s 00095000 00:10 20630 /dev/kgsl-3d0
+7fff0d000-7fff0e000 rw-s 00094000 00:10 20630 /dev/kgsl-3d0
+7fff0e000-7fff0f000 rw-s 00093000 00:10 20630 /dev/kgsl-3d0
+7fff0f000-7fff10000 rw-s 00092000 00:10 20630 /dev/kgsl-3d0
+7fff10000-7fff11000 rw-s 00091000 00:10 20630 /dev/kgsl-3d0
+7fff11000-7fff12000 rw-s 00090000 00:10 20630 /dev/kgsl-3d0
+7fff12000-7fff13000 rw-s 0008f000 00:10 20630 /dev/kgsl-3d0
+7fff13000-7fff14000 rw-s 0008e000 00:10 20630 /dev/kgsl-3d0
+7fff14000-7fff15000 rw-s 0008d000 00:10 20630 /dev/kgsl-3d0
+7fff15000-7fff16000 rw-s 0008c000 00:10 20630 /dev/kgsl-3d0
+7fff16000-7fff17000 rw-s 0008b000 00:10 20630 /dev/kgsl-3d0
+7fff17000-7fff18000 rw-s 0008a000 00:10 20630 /dev/kgsl-3d0
+7fff18000-7fff19000 rw-s 00089000 00:10 20630 /dev/kgsl-3d0
+7fff19000-7fff1a000 rw-s 00088000 00:10 20630 /dev/kgsl-3d0
+7fff1a000-7fff1b000 rw-s 00087000 00:10 20630 /dev/kgsl-3d0
+7fff1b000-7fff1c000 rw-s 00086000 00:10 20630 /dev/kgsl-3d0
+7fff1c000-7fff1d000 rw-s 00085000 00:10 20630 /dev/kgsl-3d0
+7fff1d000-7fff1e000 rw-s 00084000 00:10 20630 /dev/kgsl-3d0
+7fff1e000-7fff1f000 rw-s 00083000 00:10 20630 /dev/kgsl-3d0
+7fff1f000-7fff20000 rw-s 00082000 00:10 20630 /dev/kgsl-3d0
+7fff20000-7fff21000 rw-s 00081000 00:10 20630 /dev/kgsl-3d0
+7fff21000-7fff22000 rw-s 00080000 00:10 20630 /dev/kgsl-3d0
+7fff22000-7fff23000 rw-s 0007f000 00:10 20630 /dev/kgsl-3d0
+7fff23000-7fff24000 rw-s 0007e000 00:10 20630 /dev/kgsl-3d0
+7fff24000-7fff25000 rw-s 0007d000 00:10 20630 /dev/kgsl-3d0
+7fff25000-7fff26000 rw-s 0007c000 00:10 20630 /dev/kgsl-3d0
+7fff26000-7fff27000 rw-s 0007b000 00:10 20630 /dev/kgsl-3d0
+7fff27000-7fff28000 rw-s 0007a000 00:10 20630 /dev/kgsl-3d0
+7fff28000-7fff29000 rw-s 00079000 00:10 20630 /dev/kgsl-3d0
+7fff29000-7fff2a000 rw-s 00078000 00:10 20630 /dev/kgsl-3d0
+7fff2a000-7fff2b000 rw-s 00077000 00:10 20630 /dev/kgsl-3d0
+7fff2b000-7fff2c000 rw-s 00076000 00:10 20630 /dev/kgsl-3d0
+7fff2c000-7fff2d000 rw-s 00075000 00:10 20630 /dev/kgsl-3d0
+7fff2d000-7fff2e000 rw-s 00074000 00:10 20630 /dev/kgsl-3d0
+7fff2e000-7fff2f000 rw-s 00073000 00:10 20630 /dev/kgsl-3d0
+7fff2f000-7fff30000 rw-s 00072000 00:10 20630 /dev/kgsl-3d0
+7fff30000-7fff31000 rw-s 00071000 00:10 20630 /dev/kgsl-3d0
+7fff31000-7fff32000 rw-s 00070000 00:10 20630 /dev/kgsl-3d0
+7fff32000-7fff33000 rw-s 0006f000 00:10 20630 /dev/kgsl-3d0
+7fff33000-7fff34000 rw-s 0006e000 00:10 20630 /dev/kgsl-3d0
+7fff34000-7fff35000 rw-s 0006d000 00:10 20630 /dev/kgsl-3d0
+7fff35000-7fff36000 rw-s 0006c000 00:10 20630 /dev/kgsl-3d0
+7fff36000-7fff37000 rw-s 0006b000 00:10 20630 /dev/kgsl-3d0
+7fff37000-7fff38000 rw-s 0006a000 00:10 20630 /dev/kgsl-3d0
+7fff38000-7fff39000 rw-s 00069000 00:10 20630 /dev/kgsl-3d0
+7fff39000-7fff3a000 rw-s 00068000 00:10 20630 /dev/kgsl-3d0
+7fff3a000-7fff3b000 rw-s 00067000 00:10 20630 /dev/kgsl-3d0
+7fff3b000-7fff3c000 rw-s 00066000 00:10 20630 /dev/kgsl-3d0
+7fff3c000-7fff3d000 rw-s 00065000 00:10 20630 /dev/kgsl-3d0
+7fff3d000-7fff3e000 rw-s 00064000 00:10 20630 /dev/kgsl-3d0
+7fff3e000-7fff3f000 rw-s 00063000 00:10 20630 /dev/kgsl-3d0
+7fff3f000-7fff40000 rw-s 00062000 00:10 20630 /dev/kgsl-3d0
+7fff40000-7fff41000 rw-s 00061000 00:10 20630 /dev/kgsl-3d0
+7fff41000-7fff42000 rw-s 00060000 00:10 20630 /dev/kgsl-3d0
+7fff42000-7fff43000 rw-s 0005f000 00:10 20630 /dev/kgsl-3d0
+7fff43000-7fff44000 rw-s 0005e000 00:10 20630 /dev/kgsl-3d0
+7fff44000-7fff45000 rw-s 0005d000 00:10 20630 /dev/kgsl-3d0
+7fff45000-7fff46000 rw-s 0005c000 00:10 20630 /dev/kgsl-3d0
+7fff46000-7fff47000 rw-s 0005b000 00:10 20630 /dev/kgsl-3d0
+7fff47000-7fff48000 rw-s 0005a000 00:10 20630 /dev/kgsl-3d0
+7fff48000-7fff49000 rw-s 00059000 00:10 20630 /dev/kgsl-3d0
+7fff49000-7fff4a000 rw-s 00058000 00:10 20630 /dev/kgsl-3d0
+7fff4a000-7fff4b000 rw-s 00057000 00:10 20630 /dev/kgsl-3d0
+7fff4b000-7fff4c000 rw-s 00056000 00:10 20630 /dev/kgsl-3d0
+7fff4c000-7fff4d000 rw-s 00055000 00:10 20630 /dev/kgsl-3d0
+7fff4d000-7fff4e000 rw-s 00054000 00:10 20630 /dev/kgsl-3d0
+7fff4e000-7fff4f000 rw-s 00053000 00:10 20630 /dev/kgsl-3d0
+7fff4f000-7fff50000 rw-s 00052000 00:10 20630 /dev/kgsl-3d0
+7fff50000-7fff51000 rw-s 00051000 00:10 20630 /dev/kgsl-3d0
+7fff51000-7fff52000 rw-s 00050000 00:10 20630 /dev/kgsl-3d0
+7fff52000-7fff53000 rw-s 0004f000 00:10 20630 /dev/kgsl-3d0
+7fff53000-7fff54000 rw-s 0004e000 00:10 20630 /dev/kgsl-3d0
+7fff54000-7fff55000 rw-s 0004d000 00:10 20630 /dev/kgsl-3d0
+7fff55000-7fff56000 rw-s 0004c000 00:10 20630 /dev/kgsl-3d0
+7fff56000-7fff57000 rw-s 0004b000 00:10 20630 /dev/kgsl-3d0
+7fff57000-7fff58000 rw-s 0004a000 00:10 20630 /dev/kgsl-3d0
+7fff58000-7fff59000 rw-s 00049000 00:10 20630 /dev/kgsl-3d0
+7fff59000-7fff5a000 rw-s 00048000 00:10 20630 /dev/kgsl-3d0
+7fff5a000-7fff5b000 rw-s 00047000 00:10 20630 /dev/kgsl-3d0
+7fff5b000-7fff5c000 rw-s 00046000 00:10 20630 /dev/kgsl-3d0
+7fff5c000-7fff5d000 rw-s 00045000 00:10 20630 /dev/kgsl-3d0
+7fff5d000-7fff5e000 rw-s 00044000 00:10 20630 /dev/kgsl-3d0
+7fff5e000-7fff5f000 rw-s 00043000 00:10 20630 /dev/kgsl-3d0
+7fff5f000-7fff60000 rw-s 00042000 00:10 20630 /dev/kgsl-3d0
+7fff60000-7fff61000 rw-s 00041000 00:10 20630 /dev/kgsl-3d0
+7fff61000-7fff62000 rw-s 00040000 00:10 20630 /dev/kgsl-3d0
+7fff62000-7fff63000 rw-s 0003f000 00:10 20630 /dev/kgsl-3d0
+7fff63000-7fff64000 rw-s 0003e000 00:10 20630 /dev/kgsl-3d0
+7fff64000-7fff65000 rw-s 0003d000 00:10 20630 /dev/kgsl-3d0
+7fff65000-7fff66000 rw-s 0003c000 00:10 20630 /dev/kgsl-3d0
+7fff66000-7fff67000 rw-s 0003b000 00:10 20630 /dev/kgsl-3d0
+7fff67000-7fff68000 rw-s 0003a000 00:10 20630 /dev/kgsl-3d0
+7fff68000-7fff69000 rw-s 00039000 00:10 20630 /dev/kgsl-3d0
+7fff69000-7fff6a000 rw-s 00038000 00:10 20630 /dev/kgsl-3d0
+7fff6a000-7fff6b000 rw-s 00037000 00:10 20630 /dev/kgsl-3d0
+7fff6b000-7fff6c000 rw-s 00036000 00:10 20630 /dev/kgsl-3d0
+7fff6c000-7fff6d000 rw-s 00035000 00:10 20630 /dev/kgsl-3d0
+7fff6d000-7fff6e000 rw-s 00034000 00:10 20630 /dev/kgsl-3d0
+7fff6e000-7fff6f000 rw-s 00033000 00:10 20630 /dev/kgsl-3d0
+7fff6f000-7fff70000 rw-s 00032000 00:10 20630 /dev/kgsl-3d0
+7fff70000-7fff71000 rw-s 00031000 00:10 20630 /dev/kgsl-3d0
+7fff71000-7fff72000 rw-s 00030000 00:10 20630 /dev/kgsl-3d0
+7fff72000-7fff73000 rw-s 0002f000 00:10 20630 /dev/kgsl-3d0
+7fff73000-7fff74000 rw-s 0002e000 00:10 20630 /dev/kgsl-3d0
+7fff74000-7fff75000 rw-s 0002d000 00:10 20630 /dev/kgsl-3d0
+7fff75000-7fff76000 rw-s 0002c000 00:10 20630 /dev/kgsl-3d0
+7fff76000-7fff77000 rw-s 0002b000 00:10 20630 /dev/kgsl-3d0
+7fff77000-7fff78000 rw-s 0002a000 00:10 20630 /dev/kgsl-3d0
+7fff78000-7fff79000 rw-s 00029000 00:10 20630 /dev/kgsl-3d0
+7fff79000-7fff7a000 rw-s 00028000 00:10 20630 /dev/kgsl-3d0
+7fff7a000-7fff7b000 rw-s 00027000 00:10 20630 /dev/kgsl-3d0
+7fff7b000-7fff7c000 rw-s 00026000 00:10 20630 /dev/kgsl-3d0
+7fff7c000-7fff7d000 rw-s 00025000 00:10 20630 /dev/kgsl-3d0
+7fff7d000-7fff7e000 rw-s 00024000 00:10 20630 /dev/kgsl-3d0
+7fff7e000-7fff7f000 rw-s 00023000 00:10 20630 /dev/kgsl-3d0
+7fff7f000-7fff80000 rw-s 00022000 00:10 20630 /dev/kgsl-3d0
+7fff80000-7fff90000 rw-s 00019000 00:10 20630 /dev/kgsl-3d0
+7fff90000-7fffb0000 rw-s 00018000 00:10 20630 /dev/kgsl-3d0
+7fffb1000-7fffb2000 rw-s 00021000 00:10 20630 /dev/kgsl-3d0
+7fffb2000-7fffb3000 rw-s 00020000 00:10 20630 /dev/kgsl-3d0
+7fffb3000-7fffb4000 rw-s 0001f000 00:10 20630 /dev/kgsl-3d0
+7fffba000-7fffbe000 rw-s 0001b000 00:10 20630 /dev/kgsl-3d0
+7fffbe000-7fffbf000 rw-s 0001a000 00:10 20630 /dev/kgsl-3d0
+7fffbf000-7fffc0000 rw-s 00017000 00:10 20630 /dev/kgsl-3d0
+7fffc0000-7fffe0000 rw-s 00016000 00:10 20630 /dev/kgsl-3d0
+7fffe0000-7fffe1000 rw-s 00014000 00:10 20630 /dev/kgsl-3d0
+7fffe1000-7fffe5000 rw-s 00013000 00:10 20630 /dev/kgsl-3d0
+7fffe5000-7fffe6000 rw-s 00012000 00:10 20630 /dev/kgsl-3d0
+7fffe6000-7fffe7000 rw-s 00011000 00:10 20630 /dev/kgsl-3d0
+7fffe7000-7fffe8000 rw-s 00010000 00:10 20630 /dev/kgsl-3d0
+7fffe8000-7fffe9000 rw-s 0000f000 00:10 20630 /dev/kgsl-3d0
+7fffe9000-7fffea000 rw-s 0000e000 00:10 20630 /dev/kgsl-3d0
+7fffea000-7fffeb000 rw-s 0000d000 00:10 20630 /dev/kgsl-3d0
+7fffeb000-7fffec000 rw-s 0000c000 00:10 20630 /dev/kgsl-3d0
+7fffec000-7ffff0000 rw-s 0000b000 00:10 20630 /dev/kgsl-3d0
+7ffff0000-7ffff1000 rw-s 0000a000 00:10 20630 /dev/kgsl-3d0
+7ffff1000-7ffff5000 rw-s 00009000 00:10 20630 /dev/kgsl-3d0
+7ffff5000-7ffff6000 rw-s 00008000 00:10 20630 /dev/kgsl-3d0
+7ffff6000-7ffff7000 rw-s 00007000 00:10 20630 /dev/kgsl-3d0
+7ffff7000-7ffff8000 rw-s 00006000 00:10 20630 /dev/kgsl-3d0
+7ffff8000-7ffff9000 rw-s 00005000 00:10 20630 /dev/kgsl-3d0
+7ffff9000-7ffffa000 rw-s 00004000 00:10 20630 /dev/kgsl-3d0
+7ffffa000-7ffffb000 rw-s 00003000 00:10 20630 /dev/kgsl-3d0
+7ffffb000-7ffffc000 rw-s 00002000 00:10 20630 /dev/kgsl-3d0
+7ffffc000-800000000 rw-s 00001000 00:10 20630 /dev/kgsl-3d0
+5ff1d4f000-5ff1d54000 r-xp 00000000 fc:00 3419 /system/bin/app_process64
+5ff1d6e000-5ff1d6f000 r--p 0000f000 fc:00 3419 /system/bin/app_process64
+5ff1d6f000-5ff1d71000 rw-p 00000000 00:00 0
+704defa000-704defb000 ---p 00000000 00:00 0 [anon:thread stack guard]
+704defb000-704defc000 ---p 00000000 00:00 0
+704defc000-704e000000 rw-p 00000000 00:00 0
+704e000000-704e400000 rw-p 00000000 00:00 0 [anon:libc_malloc]
+704e455000-704e456000 ---p 00000000 00:00 0 [anon:thread stack guard]
+704e456000-704e457000 ---p 00000000 00:00 0
+704e457000-704e553000 rw-p 00000000 00:00 0
+704e553000-704e651000 r--p 00000000 00:10 16029 /dev/hwbinder
+704e651000-704e65f000 r-xp 00000000 fc:01 1040 /vendor/lib64/egl/eglSubDriverAndroid.so
+704e65f000-704e660000 r--p 0000e000 fc:01 1040 /vendor/lib64/egl/eglSubDriverAndroid.so
+704e660000-704e661000 rw-p 0000f000 fc:01 1040 /vendor/lib64/egl/eglSubDriverAndroid.so
+704e69d000-704e69e000 ---p 00000000 00:00 0 [anon:thread stack guard]
+704e69e000-704e79b000 rw-p 00000000 00:00 0
+704e79b000-704f79b000 rw-s 00000000 00:05 10271021 /dev/ashmem/AudioFlinger::Client(29312) (deleted)
+704f79b000-704f79c000 ---p 00000000 00:00 0 [anon:thread stack guard]
+704f79c000-704f899000 rw-p 00000000 00:00 0
+704f899000-704f89a000 ---p 00000000 00:00 0 [anon:thread stack guard]
+704f89a000-704f89b000 ---p 00000000 00:00 0
+704f89b000-704f997000 rw-p 00000000 00:00 0
+704f997000-704f9ee000 r-xp 00000000 103:1d 1737338 /data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/lib/arm64/libgame.so
+704f9ee000-704f9fd000 ---p 00000000 00:00 0
+704f9fd000-704fa00000 r--p 00056000 103:1d 1737338 /data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/lib/arm64/libgame.so
+704fa00000-704fa01000 rw-p 00059000 103:1d 1737338 /data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/lib/arm64/libgame.so
+704fa01000-704fa19000 rw-p 00000000 00:00 0 [anon:.bss]
+704fa40000-70507e7000 r-xp 00000000 fc:01 1026 /vendor/lib64/libllvm-glnext.so
+70507e7000-70507fc000 ---p 00000000 00:00 0
+70507fc000-7050835000 r--p 00da7000 fc:01 1026 /vendor/lib64/libllvm-glnext.so
+7050835000-705083a000 rw-p 00de0000 fc:01 1026 /vendor/lib64/libllvm-glnext.so
+705083a000-7050855000 rw-p 00000000 00:00 0 [anon:.bss]
+705089b000-7050f19000 r-xp 00000000 fc:01 1039 /vendor/lib64/egl/libGLESv2_adreno.so
+7050f19000-7050f22000 r--p 0067e000 fc:01 1039 /vendor/lib64/egl/libGLESv2_adreno.so
+7050f22000-7050f29000 rw-p 00687000 fc:01 1039 /vendor/lib64/egl/libGLESv2_adreno.so
+7050f29000-7050f2c000 rw-p 00000000 00:00 0 [anon:.bss]
+7050f83000-7050fbc000 r-xp 00000000 fc:01 1041 /vendor/lib64/egl/libGLESv1_CM_adreno.so
+7050fbc000-7050fbd000 r--p 00039000 fc:01 1041 /vendor/lib64/egl/libGLESv1_CM_adreno.so
+7050fbd000-7050fbe000 rw-p 0003a000 fc:01 1041 /vendor/lib64/egl/libGLESv1_CM_adreno.so
+7050fbe000-7050fbf000 rw-p 00000000 00:00 0 [anon:.bss]
+7050fc6000-705111d000 r-xp 00000000 fc:01 865 /vendor/lib64/libgsl.so
+705111d000-705111e000 r--p 00157000 fc:01 865 /vendor/lib64/libgsl.so
+705111e000-705111f000 rw-p 00158000 fc:01 865 /vendor/lib64/libgsl.so
+705111f000-7051120000 rw-p 00000000 00:00 0 [anon:.bss]
+7051146000-705115d000 r-xp 00000000 fc:00 2587 /system/lib64/vndk-sp-28/libz.so
+705115d000-7051175000 ---p 00000000 00:00 0
+7051175000-7051176000 r--p 0001f000 fc:00 2587 /system/lib64/vndk-sp-28/libz.so
+7051176000-7051177000 rw-p 00020000 fc:00 2587 /system/lib64/vndk-sp-28/libz.so
+705119f000-70511ac000 r-xp 00000000 fc:01 886 /vendor/lib64/libadreno_utils.so
+70511ac000-70511ad000 r--p 0000d000 fc:01 886 /vendor/lib64/libadreno_utils.so
+70511ad000-70511ae000 rw-p 0000e000 fc:01 886 /vendor/lib64/libadreno_utils.so
+70511ae000-70511b0000 rw-p 00000000 00:00 0 [anon:.bss]
+70511c0000-70511d7000 r-xp 00000000 fc:01 1044 /vendor/lib64/egl/libEGL_adreno.so
+70511d7000-70511d8000 r--p 00017000 fc:01 1044 /vendor/lib64/egl/libEGL_adreno.so
+70511d8000-70511d9000 rw-p 00018000 fc:01 1044 /vendor/lib64/egl/libEGL_adreno.so
+70511d9000-70511da000 rw-p 00000000 00:00 0 [anon:.bss]
+705120a000-705120d000 r-xp 00000000 fc:01 972 /vendor/lib64/libdrmutils.so
+705120d000-7051229000 ---p 00000000 00:00 0
+7051229000-705122a000 r--p 0000f000 fc:01 972 /vendor/lib64/libdrmutils.so
+705122a000-705122b000 rw-p 00010000 fc:01 972 /vendor/lib64/libdrmutils.so
+705125a000-705125c000 r-xp 00000000 fc:01 1046 /vendor/lib64/libqdMetaData.so
+705125c000-7051279000 ---p 00000000 00:00 0
+7051279000-705127a000 r--p 0000f000 fc:01 1046 /vendor/lib64/libqdMetaData.so
+705127a000-705127b000 rw-p 00010000 fc:01 1046 /vendor/lib64/libqdMetaData.so
+7051286000-7051297000 r-xp 00000000 fc:01 1024 /vendor/lib64/libdrm.so
+7051297000-70512b5000 ---p 00000000 00:00 0
+70512b5000-70512b6000 r--p 0001f000 fc:01 1024 /vendor/lib64/libdrm.so
+70512b6000-70512b7000 rw-p 00020000 fc:01 1024 /vendor/lib64/libdrm.so
+70512cb000-70512de000 r-xp 00000000 fc:01 1008 /vendor/lib64/hw/gralloc.msm8998.so
+70512de000-70512fa000 ---p 00000000 00:00 0
+70512fa000-70512fb000 r--p 0001f000 fc:01 1008 /vendor/lib64/hw/gralloc.msm8998.so
+70512fb000-70512fc000 rw-p 00020000 fc:01 1008 /vendor/lib64/hw/gralloc.msm8998.so
+7051326000-7051327000 ---p 00000000 00:00 0 [anon:thread stack guard]
+7051327000-7051328000 ---p 00000000 00:00 0
+7051328000-7051424000 rw-p 00000000 00:00 0
+7051424000-705143d000 r--p 00000000 fc:00 739 /system/framework/oat/arm64/org.apache.http.legacy.boot.odex
+705143d000-7051480000 r-xp 00019000 fc:00 739 /system/framework/oat/arm64/org.apache.http.legacy.boot.odex
+7051480000-7051494000 r--p 00211000 103:1d 639511 /data/dalvik-cache/arm64/system@framework@boot.art
+7051494000-705149f000 r--p 000c5000 103:1d 639514 /data/dalvik-cache/arm64/system@framework@boot-core-libart.art
+705149f000-70514a2000 r--p 00032000 103:1d 639517 /data/dalvik-cache/arm64/system@framework@boot-conscrypt.art
+70514a2000-70514a5000 r--p 0002b000 103:1d 639520 /data/dalvik-cache/arm64/system@framework@boot-okhttp.art
+70514a5000-70514ac000 r--p 0003f000 103:1d 639523 /data/dalvik-cache/arm64/system@framework@boot-bouncycastle.art
+70514ac000-70514b2000 r--p 00044000 103:1d 639526 /data/dalvik-cache/arm64/system@framework@boot-apache-xml.art
+70514b2000-70514bd000 r--p 00035000 103:1d 639529 /data/dalvik-cache/arm64/system@framework@boot-ext.art
+70514bd000-70514f4000 r--p 0060f000 103:1d 639532 /data/dalvik-cache/arm64/system@framework@boot-framework.art
+70514f4000-70514fe000 r--p 00054000 103:1d 639535 /data/dalvik-cache/arm64/system@framework@boot-telephony-common.art
+70514fe000-70514ff000 r--p 0000c000 103:1d 639538 /data/dalvik-cache/arm64/system@framework@boot-voip-common.art
+70514ff000-7051500000 r--p 0000e000 103:1d 639541 /data/dalvik-cache/arm64/system@framework@boot-ims-common.art
+7051500000-7051501000 r--p 00004000 103:1d 639547 /data/dalvik-cache/arm64/system@framework@boot-android.hidl.manager-V1.0-java.art
+7051501000-7051502000 r--p 00004000 103:1d 639553 /data/dalvik-cache/arm64/system@framework@boot-android.test.base.art
+7051502000-7051503000 r--p 00001000 103:1d 639556 /data/dalvik-cache/arm64/system@framework@boot-com.google.vr.platform.art
+7051503000-7051504000 rw-p 00000000 00:00 0 [anon:.bss]
+7051504000-7051579000 r--s 00000000 fc:00 790 /system/framework/oat/arm64/org.apache.http.legacy.boot.vdex
+7051579000-705157a000 r--p 0005c000 fc:00 739 /system/framework/oat/arm64/org.apache.http.legacy.boot.odex
+705157a000-705157b000 rw-p 0005d000 fc:00 739 /system/framework/oat/arm64/org.apache.http.legacy.boot.odex
+705158b000-7057f4d000 ---p 00000000 00:00 0
+7057f4d000-7057f4f000 r-xp 00000000 fc:00 2646 /system/lib64/libwebviewchromium_loader.so
+7057f4f000-7057f6c000 ---p 00000000 00:00 0
+7057f6c000-7057f6d000 r--p 0000f000 fc:00 2646 /system/lib64/libwebviewchromium_loader.so
+7057f6d000-7057f6e000 rw-p 00010000 fc:00 2646 /system/lib64/libwebviewchromium_loader.so
+7057f76000-7057f96000 r--s 00000000 00:10 16615 /dev/__properties__/u:object_r:hwservicemanager_prop:s0
+7057f96000-7057fb6000 r--s 00000000 00:10 16639 /dev/__properties__/u:object_r:public_vendor_default_prop:s0
+7057fb6000-7058004000 r--s 00000000 fc:00 1112 /system/usr/hyphen-data/hyph-hu.hyb
+7058004000-7058024000 r-xp 00000000 fc:00 2354 /system/lib64/libcompiler_rt.so
+7058024000-7058043000 ---p 00000000 00:00 0
+7058043000-7058044000 r--p 0002f000 fc:00 2354 /system/lib64/libcompiler_rt.so
+7058044000-7058045000 rw-p 00030000 fc:00 2354 /system/lib64/libcompiler_rt.so
+7058045000-70580b2000 rw-p 00000000 00:00 0 [anon:.bss]
+70580bd000-70580dd000 rw-p 00000000 00:05 10265386 /dev/ashmem/dalvik-LinearAlloc (deleted)
+70580dd000-70580df000 r-xp 00000000 fc:00 2597 /system/lib64/vndk-sp-28/libhardware.so
+70580df000-70580fc000 ---p 00000000 00:00 0
+70580fc000-70580fd000 r--p 0000f000 fc:00 2597 /system/lib64/vndk-sp-28/libhardware.so
+70580fd000-70580fe000 rw-p 00010000 fc:00 2597 /system/lib64/vndk-sp-28/libhardware.so
+705810e000-705811f000 r-xp 00000000 fc:00 2589 /system/lib64/vndk-sp-28/libbase.so
+705811f000-705813d000 ---p 00000000 00:00 0
+705813d000-705813e000 r--p 0001f000 fc:00 2589 /system/lib64/vndk-sp-28/libbase.so
+705813e000-705813f000 rw-p 00020000 fc:00 2589 /system/lib64/vndk-sp-28/libbase.so
+7058140000-7058167000 r-xp 00000000 fc:00 2572 /system/lib64/vndk-sp-28/libhwbinder.so
+7058167000-705817d000 ---p 00000000 00:00 0
+705817d000-705817f000 r--p 0002e000 fc:00 2572 /system/lib64/vndk-sp-28/libhwbinder.so
+705817f000-7058180000 rw-p 00030000 fc:00 2572 /system/lib64/vndk-sp-28/libhwbinder.so
+705818c000-705818d000 r-xp 00000000 fc:00 2584 /system/lib64/vndk-sp-28/android.hardware.graphics.common@1.0.so
+705818d000-70581ab000 ---p 00000000 00:00 0
+70581ab000-70581ac000 r--p 0000f000 fc:00 2584 /system/lib64/vndk-sp-28/android.hardware.graphics.common@1.0.so
+70581ac000-70581ad000 rw-p 00010000 fc:00 2584 /system/lib64/vndk-sp-28/android.hardware.graphics.common@1.0.so
+70581b7000-70581d7000 r--s 00000000 00:10 16619 /dev/__properties__/u:object_r:log_prop:s0
+70581d7000-7058237000 r-xp 00000000 fc:00 2574 /system/lib64/vndk-sp-28/libhidltransport.so
+7058237000-7058255000 ---p 00000000 00:00 0
+7058255000-705825d000 r--p 00068000 fc:00 2574 /system/lib64/vndk-sp-28/libhidltransport.so
+705825d000-705825e000 rw-p 00070000 fc:00 2574 /system/lib64/vndk-sp-28/libhidltransport.so
+7058260000-7058284000 r--s 00000000 fc:00 1138 /system/usr/hyphen-data/hyph-nn.hyb
+7058284000-70582a0000 r-xp 00000000 fc:00 2576 /system/lib64/vndk-sp-28/libutils.so
+70582a0000-70582b3000 ---p 00000000 00:00 0
+70582b3000-70582b4000 r--p 0001f000 fc:00 2576 /system/lib64/vndk-sp-28/libutils.so
+70582b4000-70582b5000 rw-p 00020000 fc:00 2576 /system/lib64/vndk-sp-28/libutils.so
+70582c4000-7058391000 r-xp 00000000 fc:00 2568 /system/lib64/vndk-sp-28/libc++.so
+7058391000-70583ad000 ---p 00000000 00:00 0
+70583ad000-70583b7000 r--p 000d6000 fc:00 2568 /system/lib64/vndk-sp-28/libc++.so
+70583b7000-70583b8000 rw-p 000e0000 fc:00 2568 /system/lib64/vndk-sp-28/libc++.so
+70583b8000-70583bb000 rw-p 00000000 00:00 0 [anon:.bss]
+70583cd000-70583e4000 r-xp 00000000 fc:00 2580 /system/lib64/vndk-sp-28/android.hardware.graphics.mapper@2.0.so
+70583e4000-70583f9000 ---p 00000000 00:00 0
+70583f9000-70583fb000 r--p 0001e000 fc:00 2580 /system/lib64/vndk-sp-28/android.hardware.graphics.mapper@2.0.so
+70583fb000-70583fc000 rw-p 00020000 fc:00 2580 /system/lib64/vndk-sp-28/android.hardware.graphics.mapper@2.0.so
+705841b000-7058421000 r-xp 00000000 fc:01 1001 /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so
+7058421000-705843a000 ---p 00000000 00:00 0
+705843a000-705843b000 r--p 0000f000 fc:01 1001 /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so
+705843b000-705843c000 rw-p 00010000 fc:01 1001 /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so
+705844f000-7058473000 r--s 00000000 fc:00 1150 /system/usr/hyphen-data/hyph-nb.hyb
+7058473000-7058495000 r-xp 00000000 fc:00 2582 /system/lib64/vndk-sp-28/libhidlbase.so
+7058495000-70584b1000 ---p 00000000 00:00 0
+70584b1000-70584b3000 r--p 0002e000 fc:00 2582 /system/lib64/vndk-sp-28/libhidlbase.so
+70584b3000-70584b4000 rw-p 00030000 fc:00 2582 /system/lib64/vndk-sp-28/libhidlbase.so
+70584cd000-70584df000 r-xp 00000000 fc:00 2595 /system/lib64/vndk-sp-28/libcutils.so
+70584df000-70584fb000 ---p 00000000 00:00 0
+70584fb000-70584fd000 r--p 0001e000 fc:00 2595 /system/lib64/vndk-sp-28/libcutils.so
+70584fd000-70584fe000 rw-p 00020000 fc:00 2595 /system/lib64/vndk-sp-28/libcutils.so
+7058519000-7058537000 r--s 00000000 fc:00 1124 /system/usr/hyphen-data/hyph-de-ch-1901.hyb
+7058537000-7059fd1000 r--s 0070b000 fc:00 989 /system/framework/framework-res.apk
+7059fd1000-705a013000 r-xp 00000000 fc:00 2610 /system/lib64/libjavacrypto.so
+705a013000-705a02b000 ---p 00000000 00:00 0
+705a02b000-705a02d000 r--p 0004e000 fc:00 2610 /system/lib64/libjavacrypto.so
+705a02d000-705a02f000 rw-p 00050000 fc:00 2610 /system/lib64/libjavacrypto.so
+705a041000-705a05f000 r--s 00000000 fc:00 1128 /system/usr/hyphen-data/hyph-de-1996.hyb
+705a05f000-705a06a000 r-xp 00000000 fc:00 2917 /system/lib64/libsoundpool.so
+705a06a000-705a07e000 ---p 00000000 00:00 0
+705a07e000-705a07f000 r--p 0000f000 fc:00 2917 /system/lib64/libsoundpool.so
+705a07f000-705a080000 rw-p 00010000 fc:00 2917 /system/lib64/libsoundpool.so
+705a087000-705a102000 r--s 00000000 fc:00 1246 /system/usr/share/zoneinfo/tzdata
+705a102000-705a863000 r--s 00000000 fc:00 101 /system/fonts/NotoColorEmoji.ttf
+705a863000-705c000000 r--s 00000000 fc:00 251 /system/fonts/NotoSerifCJK-Regular.ttc
+705c000000-705c200000 rw-p 00000000 00:00 0 [anon:libc_malloc]
+705c209000-705c227000 r--s 00000000 fc:00 1077 /system/usr/hyphen-data/hyph-de-1901.hyb
+705c227000-705c26e000 r--s 02284000 fc:00 989 /system/framework/framework-res.apk
+705c26e000-705d43e000 r--s 00000000 fc:00 95 /system/fonts/NotoSansCJK-Regular.ttc
+705d43e000-705d4ec000 r--s 00000000 fc:00 278 /system/fonts/NotoSansSymbols-Regular-Subsetted.ttf
+705d4ec000-705d548000 r--s 00000000 fc:00 233 /system/fonts/NotoSansTibetan-Bold.ttf
+705d548000-705d5ab000 r--s 00000000 fc:00 177 /system/fonts/NotoSansTibetan-Regular.ttf
+705d5ab000-705d627000 r--s 00000000 fc:00 197 /system/fonts/NotoSansEgyptianHieroglyphs-Regular.ttf
+705d627000-705d6a2000 r--s 00000000 fc:00 76 /system/fonts/NotoSansCuneiform-Regular.ttf
+705d6a2000-705d6f3000 r--s 00000000 fc:00 67 /system/fonts/RobotoCondensed-BoldItalic.ttf
+705d6f3000-705d73e000 r--s 00000000 fc:00 199 /system/fonts/RobotoCondensed-Bold.ttf
+705d73e000-705d78f000 r--s 00000000 fc:00 230 /system/fonts/RobotoCondensed-MediumItalic.ttf
+705d78f000-705d7da000 r--s 00000000 fc:00 92 /system/fonts/RobotoCondensed-Medium.ttf
+705d7da000-705d82b000 r--s 00000000 fc:00 128 /system/fonts/RobotoCondensed-Italic.ttf
+705d82b000-705d875000 r--s 00000000 fc:00 164 /system/fonts/RobotoCondensed-Regular.ttf
+705d875000-705d8c7000 r--s 00000000 fc:00 292 /system/fonts/RobotoCondensed-LightItalic.ttf
+705d8c7000-705d919000 r--s 00000000 fc:00 85 /system/fonts/Roboto-BoldItalic.ttf
+705d919000-705d964000 r--s 00000000 fc:00 175 /system/fonts/Roboto-Bold.ttf
+705d964000-705d9b5000 r--s 00000000 fc:00 266 /system/fonts/Roboto-BlackItalic.ttf
+705d9b5000-705da00000 r--s 00000000 fc:00 187 /system/fonts/Roboto-Black.ttf
+705da00000-705dc00000 rw-p 00000000 00:00 0 [anon:libc_malloc]
+705dc1d000-705dc6e000 r--s 00000000 fc:00 148 /system/fonts/Roboto-MediumItalic.ttf
+705dc6e000-705dcb9000 r--s 00000000 fc:00 284 /system/fonts/Roboto-Medium.ttf
+705dcb9000-705dd0a000 r--s 00000000 fc:00 105 /system/fonts/Roboto-Italic.ttf
+705dd0a000-705dd55000 r--s 00000000 fc:00 156 /system/fonts/Roboto-Regular.ttf
+705dd55000-705dda7000 r--s 00000000 fc:00 217 /system/fonts/Roboto-LightItalic.ttf
+705dda7000-705ddf8000 r--s 00000000 fc:00 166 /system/fonts/Roboto-ThinItalic.ttf
+705ddf8000-705ddf9000 ---p 00000000 00:00 0 [anon:thread stack guard]
+705ddf9000-705ddfa000 ---p 00000000 00:00 0
+705ddfa000-705def6000 rw-p 00000000 00:00 0
+705def6000-705f5ec000 r--s 00000000 fc:00 1350 /system/usr/icu/icudt60l.dat
+705f5ec000-705f5ed000 ---p 00000000 00:00 0 [anon:thread stack guard]
+705f5ed000-705f5ee000 ---p 00000000 00:00 0
+705f5ee000-705f6ea000 rw-p 00000000 00:00 0
+705f6ea000-705f7e8000 r--p 00000000 00:10 20636 /dev/binder
+705f7e8000-705f7e9000 ---p 00000000 00:00 0 [anon:thread stack guard]
+705f7e9000-705f7ea000 ---p 00000000 00:00 0
+705f7ea000-705f8ee000 rw-p 00000000 00:00 0
+705f8ee000-705f8ef000 ---p 00000000 00:00 0 [anon:thread stack guard]
+705f8ef000-705f8f0000 ---p 00000000 00:00 0
+705f8f0000-705f9f4000 rw-p 00000000 00:00 0
+705f9f4000-705f9f5000 ---p 00000000 00:00 0 [anon:thread stack guard]
+705f9f5000-705f9f6000 ---p 00000000 00:00 0
+705f9f6000-705fafa000 rw-p 00000000 00:00 0
+705fafa000-705fafb000 ---p 00000000 00:00 0 [anon:thread stack guard]
+705fafb000-705fafc000 ---p 00000000 00:00 0
+705fafc000-705fc00000 rw-p 00000000 00:00 0
+705fc00000-705fe00000 rw-p 00000000 00:00 0 [anon:libc_malloc]
+705fe01000-705fe4c000 r--s 00000000 fc:00 97 /system/fonts/Roboto-Light.ttf
+705fe4c000-705fe4d000 ---p 00000000 00:00 0 [anon:thread stack guard]
+705fe4d000-705fe4e000 ---p 00000000 00:00 0
+705fe4e000-705ff4a000 rw-p 00000000 00:00 0
+705ff4a000-705ff4b000 ---p 00000000 00:05 10270991 /dev/ashmem/dalvik-Jit thread pool worker thread 0 (deleted)
+705ff4b000-705ff4c000 ---p 00001000 00:05 10270991 /dev/ashmem/dalvik-Jit thread pool worker thread 0 (deleted)
+705ff4c000-706004b000 rw-p 00002000 00:05 10270991 /dev/ashmem/dalvik-Jit thread pool worker thread 0 (deleted)
+706004b000-706010f000 r-xp 00000000 fc:00 2390 /system/lib64/libvixl-arm64.so
+706010f000-7060120000 ---p 00000000 00:00 0
+7060120000-7060125000 r--p 000cb000 fc:00 2390 /system/lib64/libvixl-arm64.so
+7060125000-7060126000 rw-p 000d0000 fc:00 2390 /system/lib64/libvixl-arm64.so
+7060126000-706012d000 rw-p 00000000 00:00 0 [anon:.bss]
+7060135000-7060151000 r--s 00000000 fc:01 1180 /vendor/overlay/framework-res__auto_generated_rro.apk
+7060151000-7060263000 r-xp 00000000 fc:00 2669 /system/lib64/libvixl-arm.so
+7060263000-7060275000 ---p 00000000 00:00 0
+7060275000-706027a000 r--p 0011b000 fc:00 2669 /system/lib64/libvixl-arm.so
+706027a000-706027b000 rw-p 00120000 fc:00 2669 /system/lib64/libvixl-arm.so
+706028b000-706056c000 r-xp 00000000 fc:00 2972 /system/lib64/libart-compiler.so
+706056c000-7060580000 ---p 00000000 00:00 0
+7060580000-7060598000 r--p 002e8000 fc:00 2972 /system/lib64/libart-compiler.so
+7060598000-7060599000 rw-p 00300000 fc:00 2972 /system/lib64/libart-compiler.so
+7060599000-70605a0000 rw-p 00000000 00:00 0 [anon:.bss]
+70605b0000-70605d0000 r--s 00000000 00:10 16571 /dev/__properties__/u:object_r:config_prop:s0
+70605d0000-7060619000 r-xp 00000000 fc:00 2702 /system/lib64/libssl.so
+7060619000-706062d000 ---p 00000000 00:00 0
+706062d000-7060630000 r--p 0004d000 fc:00 2702 /system/lib64/libssl.so
+7060630000-7060631000 rw-p 00050000 fc:00 2702 /system/lib64/libssl.so
+7060647000-7060667000 r--s 00000000 00:10 16595 /dev/__properties__/u:object_r:exported3_radio_prop:s0
+7060667000-706069d000 r-xp 00000000 fc:00 2371 /system/lib64/libopenjdk.so
+706069d000-70606b2000 ---p 00000000 00:00 0
+70606b2000-70606b4000 r--p 0003e000 fc:00 2371 /system/lib64/libopenjdk.so
+70606b4000-70606b6000 rw-p 00040000 fc:00 2371 /system/lib64/libopenjdk.so
+70606bb000-70606db000 r--s 00000000 00:10 16608 /dev/__properties__/u:object_r:exported_system_prop:s0
+70606db000-70606e3000 r-xp 00000000 fc:00 2538 /system/lib64/libopenjdkjvm.so
+70606e3000-70606fa000 ---p 00000000 00:00 0
+70606fa000-70606fb000 r--p 0000f000 fc:00 2538 /system/lib64/libopenjdkjvm.so
+70606fb000-70606fc000 rw-p 00010000 fc:00 2538 /system/lib64/libopenjdkjvm.so
+7060701000-7060722000 r--s 00000000 fc:00 227 /system/fonts/NotoSansAnatolianHieroglyphs-Regular.otf
+7060722000-7061e18000 r--s 00000000 fc:00 1350 /system/usr/icu/icudt60l.dat
+7061e18000-7061e5d000 r-xp 00000000 fc:00 2368 /system/lib64/libjavacore.so
+7061e5d000-7061e71000 ---p 00000000 00:00 0
+7061e71000-7061e73000 r--p 0004e000 fc:00 2368 /system/lib64/libjavacore.so
+7061e73000-7061e75000 rw-p 00050000 fc:00 2368 /system/lib64/libjavacore.so
+7061e75000-7061e76000 rw-p 00000000 00:00 0 [anon:.bss]
+7061e77000-7061e96000 r--s 00000000 fc:00 186 /system/fonts/NotoSansYi-Regular.ttf
+7061e96000-7061e99000 r-xp 00000000 fc:00 2953 /system/lib64/libwebviewchromium_plat_support.so
+7061e99000-7061eb5000 ---p 00000000 00:00 0
+7061eb5000-7061eb6000 r--p 0000f000 fc:00 2953 /system/lib64/libwebviewchromium_plat_support.so
+7061eb6000-7061eb7000 rw-p 00010000 fc:00 2953 /system/lib64/libwebviewchromium_plat_support.so
+7061ebc000-7061edd000 r--s 00000000 fc:00 100 /system/fonts/NotoSansBamum-Regular.ttf
+7061edd000-7061eed000 r-xp 00000000 fc:00 2945 /system/lib64/libRS.so
+7061eed000-7061efc000 ---p 00000000 00:00 0
+7061efc000-7061efd000 r--p 0000f000 fc:00 2945 /system/lib64/libRS.so
+7061efd000-7061efe000 rw-p 00010000 fc:00 2945 /system/lib64/libRS.so
+7061f05000-7061f6b000 r-xp 00000000 fc:00 2423 /system/lib64/android.hardware.renderscript@1.0.so
+7061f6b000-7061f7a000 ---p 00000000 00:00 0
+7061f7a000-7061f7f000 r--p 0006b000 fc:00 2423 /system/lib64/android.hardware.renderscript@1.0.so
+7061f7f000-7061f80000 rw-p 00070000 fc:00 2423 /system/lib64/android.hardware.renderscript@1.0.so
+7061f99000-7061f9b000 r-xp 00000000 fc:00 2614 /system/lib64/libOpenSLES.so
+7061f9b000-7061fb8000 ---p 00000000 00:00 0
+7061fb8000-7061fb9000 r--p 0000f000 fc:00 2614 /system/lib64/libOpenSLES.so
+7061fb9000-7061fba000 rw-p 00010000 fc:00 2614 /system/lib64/libOpenSLES.so
+7061fc6000-7061fc8000 r-xp 00000000 fc:00 2963 /system/lib64/libOpenMAXAL.so
+7061fc8000-7061fe5000 ---p 00000000 00:00 0
+7061fe5000-7061fe6000 r--p 0000f000 fc:00 2963 /system/lib64/libOpenMAXAL.so
+7061fe6000-7061fe7000 rw-p 00010000 fc:00 2963 /system/lib64/libOpenMAXAL.so
+7061fe7000-7062000000 r--s 00000000 fc:00 143 /system/fonts/NotoSansBhaiksuki-Regular.otf
+7062000000-7062003000 r-xp 00000000 fc:00 2447 /system/lib64/libtextclassifier_hash.so
+7062003000-706201f000 ---p 00000000 00:00 0
+706201f000-7062020000 r--p 0000f000 fc:00 2447 /system/lib64/libtextclassifier_hash.so
+7062020000-7062021000 rw-p 00010000 fc:00 2447 /system/lib64/libtextclassifier_hash.so
+7062022000-7062042000 rw-p 00000000 00:05 10269731 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+7062042000-7062077000 r-xp 00000000 fc:00 2372 /system/lib64/android.hardware.neuralnetworks@1.0.so
+7062077000-7062095000 ---p 00000000 00:00 0
+7062095000-706209b000 r--p 0003a000 fc:00 2372 /system/lib64/android.hardware.neuralnetworks@1.0.so
+706209b000-706209c000 rw-p 00040000 fc:00 2372 /system/lib64/android.hardware.neuralnetworks@1.0.so
+70620a9000-70620c9000 rw-p 00000000 00:05 10269730 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70620c9000-70620e3000 r-xp 00000000 fc:00 2956 /system/lib64/android.hardware.neuralnetworks@1.1.so
+70620e3000-70620f4000 ---p 00000000 00:00 0
+70620f4000-70620f7000 r--p 0001d000 fc:00 2956 /system/lib64/android.hardware.neuralnetworks@1.1.so
+70620f7000-70620f8000 rw-p 00020000 fc:00 2956 /system/lib64/android.hardware.neuralnetworks@1.1.so
+706210b000-70621d0000 r-xp 00000000 fc:00 2387 /system/lib64/libneuralnetworks.so
+70621d0000-70621e3000 ---p 00000000 00:00 0
+70621e3000-70621e5000 r--p 000ce000 fc:00 2387 /system/lib64/libneuralnetworks.so
+70621e5000-70621e7000 rw-p 000d0000 fc:00 2387 /system/lib64/libneuralnetworks.so
+70621e7000-7062372000 rw-p 00000000 00:00 0 [anon:.bss]
+7062373000-7062395000 r--s 00000000 fc:00 274 /system/fonts/NotoSerifMyanmar-Bold.otf
+7062395000-7062398000 r-xp 00000000 fc:00 2937 /system/lib64/libjnigraphics.so
+7062398000-70623b4000 ---p 00000000 00:00 0
+70623b4000-70623b5000 r--p 0000f000 fc:00 2937 /system/lib64/libjnigraphics.so
+70623b5000-70623b6000 rw-p 00010000 fc:00 2937 /system/lib64/libjnigraphics.so
+70623c8000-70623e0000 r-xp 00000000 fc:00 2662 /system/lib64/libGLESv3.so
+70623e0000-70623f7000 ---p 00000000 00:00 0
+70623f7000-70623f8000 r--p 0001f000 fc:00 2662 /system/lib64/libGLESv3.so
+70623f8000-70623f9000 rw-p 00020000 fc:00 2662 /system/lib64/libGLESv3.so
+70623fc000-706241c000 rw-p 00000000 00:05 10269729 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+706241c000-7062444000 r-xp 00000000 fc:00 2603 /system/lib64/libexif.so
+7062444000-706245f000 ---p 00000000 00:00 0
+706245f000-7062472000 r--p 0002d000 fc:00 2603 /system/lib64/libexif.so
+7062472000-7062473000 rw-p 00040000 fc:00 2603 /system/lib64/libexif.so
+7062474000-7062490000 r--s 00000000 fc:00 286 /system/fonts/NotoSansMongolian-Regular.ttf
+7062490000-7062491000 r-xp 00000000 fc:00 2357 /system/lib64/libasyncio.so
+7062491000-70624af000 ---p 00000000 00:00 0
+70624af000-70624b0000 r--p 0000f000 fc:00 2357 /system/lib64/libasyncio.so
+70624b0000-70624b1000 rw-p 00010000 fc:00 2357 /system/lib64/libasyncio.so
+70624b5000-70624cf000 r--s 00000000 fc:00 221 /system/fonts/NotoSansMyanmarUI-Bold.ttf
+70624cf000-7062508000 r-xp 00000000 fc:00 2401 /system/lib64/libmtp.so
+7062508000-7062522000 ---p 00000000 00:00 0
+7062522000-7062525000 r--p 0003d000 fc:00 2401 /system/lib64/libmtp.so
+7062525000-706252c000 rw-p 00040000 fc:00 2401 /system/lib64/libmtp.so
+7062530000-7062550000 rw-p 00000000 00:05 10269728 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+7062550000-7062572000 r--s 00000000 fc:00 234 /system/fonts/NotoSerifMyanmar-Regular.otf
+7062572000-706259e000 r-xp 00000000 fc:00 2620 /system/lib64/libmediandk.so
+706259e000-70625b9000 ---p 00000000 00:00 0
+70625b9000-70625bc000 r--p 0002d000 fc:00 2620 /system/lib64/libmediandk.so
+70625bc000-70625c0000 rw-p 00030000 fc:00 2620 /system/lib64/libmediandk.so
+70625c2000-70625d1000 r-xp 00000000 fc:00 2613 /system/lib64/libmidi.so
+70625d1000-70625ef000 ---p 00000000 00:00 0
+70625ef000-70625f1000 r--p 0000e000 fc:00 2613 /system/lib64/libmidi.so
+70625f1000-70625f2000 rw-p 00010000 fc:00 2613 /system/lib64/libmidi.so
+7062600000-7062621000 r-xp 00000000 fc:00 2366 /system/lib64/libmediadrmmetrics_lite.so
+7062621000-706263d000 ---p 00000000 00:00 0
+706263d000-706263f000 r--p 0002e000 fc:00 2366 /system/lib64/libmediadrmmetrics_lite.so
+706263f000-7062640000 rw-p 00030000 fc:00 2366 /system/lib64/libmediadrmmetrics_lite.so
+706264b000-706266b000 rw-p 00000000 00:05 10269727 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+706266b000-70626d4000 r-xp 00000000 fc:00 2727 /system/lib64/libmedia_jni.so
+70626d4000-70626eb000 ---p 00000000 00:00 0
+70626eb000-70626f2000 r--p 00069000 fc:00 2727 /system/lib64/libmedia_jni.so
+70626f2000-70626f3000 rw-p 00070000 fc:00 2727 /system/lib64/libmedia_jni.so
+7062703000-7062732000 r-xp 00000000 fc:00 2399 /system/lib64/libcamera2ndk.so
+7062732000-7062748000 ---p 00000000 00:00 0
+7062748000-706274b000 r--p 0003d000 fc:00 2399 /system/lib64/libcamera2ndk.so
+706274b000-7062750000 rw-p 00040000 fc:00 2399 /system/lib64/libcamera2ndk.so
+7062768000-7062788000 rw-p 00000000 00:05 10269726 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+7062788000-70627ee000 r-xp 00000000 fc:00 2974 /system/lib64/android.hardware.drm@1.0.so
+70627ee000-7062805000 ---p 00000000 00:00 0
+7062805000-706280d000 r--p 00068000 fc:00 2974 /system/lib64/android.hardware.drm@1.0.so
+706280d000-706280e000 rw-p 00070000 fc:00 2974 /system/lib64/android.hardware.drm@1.0.so
+706281a000-706281b000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+706281b000-706281f000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+706281f000-7062843000 r--s 00000000 fc:00 142 /system/fonts/NotoSansKhmer-VF.ttf
+7062843000-7062886000 r-xp 00000000 fc:00 2637 /system/lib64/android.hardware.drm@1.1.so
+7062886000-70628a5000 ---p 00000000 00:00 0
+70628a5000-70628ab000 r--p 0004a000 fc:00 2637 /system/lib64/android.hardware.drm@1.1.so
+70628ab000-70628ac000 rw-p 00050000 fc:00 2637 /system/lib64/android.hardware.drm@1.1.so
+70628b0000-70628b1000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70628b1000-70628b5000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70628b5000-70628db000 r--s 00000000 fc:00 137 /system/fonts/NotoSansSinhala-Bold.ttf
+70628db000-7062907000 r-xp 00000000 fc:00 2478 /system/lib64/libmediadrm.so
+7062907000-7062918000 ---p 00000000 00:00 0
+7062918000-7062920000 r--p 00038000 fc:00 2478 /system/lib64/libmediadrm.so
+7062920000-7062921000 rw-p 00040000 fc:00 2478 /system/lib64/libmediadrm.so
+7062922000-7062929000 rw-p 00000000 fc:00 583 /system/etc/event-log-tags
+7062929000-7062951000 r--s 00000000 fc:00 296 /system/fonts/NotoSansSinhala-Regular.ttf
+7062951000-7062997000 r-xp 00000000 fc:00 2448 /system/lib64/libaaudio.so
+7062997000-70629ac000 ---p 00000000 00:00 0
+70629ac000-70629b2000 r--p 0004a000 fc:00 2448 /system/lib64/libaaudio.so
+70629b2000-70629ba000 rw-p 00050000 fc:00 2448 /system/lib64/libaaudio.so
+70629ba000-70629bb000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70629bb000-70629bf000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70629bf000-70629c0000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70629c0000-70629c3000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70629c3000-70629c4000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70629c4000-70629c5000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70629c5000-70629c9000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70629c9000-70629e3000 r-xp 00000000 fc:00 2940 /system/lib64/libandroid.so
+70629e3000-70629f3000 ---p 00000000 00:00 0
+70629f3000-70629f6000 r--p 0001d000 fc:00 2940 /system/lib64/libandroid.so
+70629f6000-70629f7000 rw-p 00020000 fc:00 2940 /system/lib64/libandroid.so
+70629f8000-70629f9000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70629f9000-70629fc000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70629fc000-70629fd000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70629fd000-7062a3e000 r--s 00000000 fc:00 216 /system/fonts/NotoSerif-BoldItalic.ttf
+7062a3e000-7062b06000 rw-p 00000000 00:05 10270984 /dev/ashmem/dalvik-indirect ref table (deleted)
+7062b06000-7062bce000 rw-p 00000000 00:05 10270983 /dev/ashmem/dalvik-indirect ref table (deleted)
+7062bce000-7062dce000 rw-p 00000000 00:05 10270726 /dev/ashmem/dalvik-rb copying gc mark stack (deleted)
+7062dce000-70635ce000 rw-p 00000000 00:05 10270725 /dev/ashmem/dalvik-concurrent copying gc mark stack (deleted)
+70635ce000-7063dcf000 rw-p 00000000 00:05 10270724 /dev/ashmem/dalvik-live stack (deleted)
+7063dcf000-70645d0000 rw-p 00000000 00:05 10270723 /dev/ashmem/dalvik-allocation stack (deleted)
+70645d0000-70649d1000 rw-p 00000000 00:05 10270721 /dev/ashmem/dalvik-card table (deleted)
+70649d1000-7064ad1000 rw-p 00000000 00:05 10267648 /dev/ashmem/dalvik-large object free list space allocation info map (deleted)
+7064ad1000-7065ad1000 rw-p 00000000 00:05 10267644 /dev/ashmem/dalvik-region space live bitmap (deleted)
+7065ad1000-7065bd1000 rw-p 00000000 00:05 10267642 /dev/ashmem/dalvik-allocspace zygote / non moving space mark-bitmap 0 (deleted)
+7065bd1000-7065cd1000 rw-p 00000000 00:05 10267641 /dev/ashmem/dalvik-allocspace zygote / non moving space live-bitmap 0 (deleted)
+7065cd1000-7065cd2000 r-xp 00000000 fc:00 2946 /system/lib64/libsigchain.so
+7065cd2000-7065cf0000 ---p 00000000 00:00 0
+7065cf0000-7065cf1000 r--p 0000f000 fc:00 2946 /system/lib64/libsigchain.so
+7065cf1000-7065cf2000 rw-p 00010000 fc:00 2946 /system/lib64/libsigchain.so
+7065cf4000-7065d0f000 r--s 00000000 fc:00 190 /system/fonts/NotoSansMyanmar-Bold.ttf
+7065d0f000-7065d22000 r-xp 00000000 fc:00 2405 /system/lib64/liblz4.so
+7065d22000-7065d3e000 ---p 00000000 00:00 0
+7065d3e000-7065d3f000 r--p 0001f000 fc:00 2405 /system/lib64/liblz4.so
+7065d3f000-7065d40000 rw-p 00020000 fc:00 2405 /system/lib64/liblz4.so
+7065d40000-7065d5a000 r--s 00000000 fc:00 222 /system/fonts/NotoSansMyanmarUI-Regular.ttf
+7065d5a000-7065d5e000 r-xp 00000000 fc:00 2609 /system/lib64/libtombstoned_client.so
+7065d5e000-7065d79000 ---p 00000000 00:00 0
+7065d79000-7065d7a000 r--p 0000f000 fc:00 2609 /system/lib64/libtombstoned_client.so
+7065d7a000-7065d7b000 rw-p 00010000 fc:00 2609 /system/lib64/libtombstoned_client.so
+7065d7f000-7065d80000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+7065d80000-7065d84000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+7065d84000-706636e000 r-xp 00000000 fc:00 2671 /system/lib64/libart.so
+706636e000-706638d000 ---p 00000000 00:00 0
+706638d000-706639e000 r--p 005ef000 fc:00 2671 /system/lib64/libart.so
+706639e000-70663a1000 rw-p 00600000 fc:00 2671 /system/lib64/libart.so
+70663a1000-70663a4000 rw-p 00000000 00:00 0 [anon:.bss]
+70663a6000-70663c6000 rw-p 00000000 00:05 10269725 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70663c6000-70663c8000 r-xp 00000000 fc:00 2673 /system/lib64/libmetricslogger.so
+70663c8000-70663e5000 ---p 00000000 00:00 0
+70663e5000-70663e6000 r--p 0000f000 fc:00 2673 /system/lib64/libmetricslogger.so
+70663e6000-70663e7000 rw-p 00010000 fc:00 2673 /system/lib64/libmetricslogger.so
+70663e7000-7066400000 r--s 00000000 fc:00 110 /system/fonts/NotoSansLepcha-Regular.ttf
+7066400000-7066800000 rw-p 00000000 00:00 0 [anon:libc_malloc]
+7066803000-706681e000 r--s 00000000 fc:00 297 /system/fonts/NotoSansMyanmar-Regular.ttf
+706681e000-7066821000 r--p 00000000 00:00 0 [anon:cfi shadow]
+7066821000-7066822000 r--p 00000000 00:00 0 [anon:cfi shadow]
+7066822000-7066b1d000 r--p 00000000 00:00 0 [anon:cfi shadow]
+7066b1d000-7066b1e000 r--p 00000000 00:00 0 [anon:cfi shadow]
+7066b1e000-7066ba0000 r--p 00000000 00:00 0 [anon:cfi shadow]
+7066ba0000-7066ba1000 r--p 00000000 00:00 0 [anon:cfi shadow]
+7066ba1000-7066ba2000 r--p 00000000 00:00 0 [anon:cfi shadow]
+7066ba2000-7066ba5000 r--p 00000000 00:00 0 [anon:cfi shadow]
+7066ba5000-7066ba6000 r--p 00000000 00:00 0 [anon:cfi shadow]
+7066ba6000-70e681e000 r--p 00000000 00:00 0 [anon:cfi shadow]
+70e681e000-70e6854000 r-xp 00000000 fc:00 2431 /system/lib64/libstagefright_foundation.so
+70e6854000-70e6865000 ---p 00000000 00:00 0
+70e6865000-70e6867000 r--p 0003e000 fc:00 2431 /system/lib64/libstagefright_foundation.so
+70e6867000-70e686c000 rw-p 00040000 fc:00 2431 /system/lib64/libstagefright_foundation.so
+70e686d000-70e686e000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70e686e000-70e6871000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70e6871000-70e6873000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70e6873000-70e6876000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70e6876000-70e6877000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70e6877000-70e688c000 r--s 00000000 fc:00 301 /system/fonts/NotoSansSinhalaUI-Bold.otf
+70e688c000-70e688e000 r-xp 00000000 fc:00 2943 /system/lib64/libion.so
+70e688e000-70e68ab000 ---p 00000000 00:00 0
+70e68ab000-70e68ac000 r--p 0000f000 fc:00 2943 /system/lib64/libion.so
+70e68ac000-70e68ad000 rw-p 00010000 fc:00 2943 /system/lib64/libion.so
+70e68ad000-70e68af000 rw-p 00000000 00:05 10282496 /dev/ashmem/dalvik-indirect ref table (deleted)
+70e68af000-70e68b1000 rw-p 00000000 00:05 10282493 /dev/ashmem/dalvik-indirect ref table (deleted)
+70e68b1000-70e68ee000 r--s 00000000 fc:00 256 /system/fonts/NotoSerif-Italic.ttf
+70e68ee000-70e6910000 r-xp 00000000 fc:00 2502 /system/lib64/libhidlbase.so
+70e6910000-70e692c000 ---p 00000000 00:00 0
+70e692c000-70e692e000 r--p 0002e000 fc:00 2502 /system/lib64/libhidlbase.so
+70e692e000-70e692f000 rw-p 00030000 fc:00 2502 /system/lib64/libhidlbase.so
+70e6930000-70e693f000 r--s 00000000 fc:00 1082 /system/usr/hyphen-data/hyph-en-us.hyb
+70e693f000-70e6954000 r--s 00000000 fc:00 138 /system/fonts/NotoSansSinhalaUI-Regular.otf
+70e6954000-70e6978000 r-xp 00000000 fc:00 2482 /system/lib64/libui.so
+70e6978000-70e6992000 ---p 00000000 00:00 0
+70e6992000-70e6994000 r--p 0002e000 fc:00 2482 /system/lib64/libui.so
+70e6994000-70e6995000 rw-p 00030000 fc:00 2482 /system/lib64/libui.so
+70e6996000-70e69a2000 r--s 00000000 fc:00 1117 /system/usr/hyphen-data/hyph-en-gb.hyb
+70e69a2000-70e69b7000 r--s 00000000 fc:00 202 /system/fonts/NotoSerifSinhala-Bold.otf
+70e69b7000-70e69cb000 r--s 00000000 fc:00 124 /system/fonts/NotoSansOriyaUI-Bold.ttf
+70e69cb000-70e69e1000 r-xp 00000000 fc:00 2537 /system/lib64/liblog.so
+70e69e1000-70e69fa000 ---p 00000000 00:00 0
+70e69fa000-70e69fb000 r--p 0001f000 fc:00 2537 /system/lib64/liblog.so
+70e69fb000-70e69fc000 rw-p 00020000 fc:00 2537 /system/lib64/liblog.so
+70e69fc000-70e69fe000 rw-p 00000000 00:05 10266158 /dev/ashmem/dalvik-indirect ref table (deleted)
+70e69fe000-70e69ff000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70e69ff000-70e6a02000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70e6a02000-70e6a03000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70e6a03000-70e6a05000 r-xp 00000000 fc:00 2489 /system/lib64/android.hidl.token@1.0-utils.so
+70e6a05000-70e6a22000 ---p 00000000 00:00 0
+70e6a22000-70e6a23000 r--p 0000f000 fc:00 2489 /system/lib64/android.hidl.token@1.0-utils.so
+70e6a23000-70e6a24000 rw-p 00010000 fc:00 2489 /system/lib64/android.hidl.token@1.0-utils.so
+70e6a25000-70e6a2e000 r--s 00000000 fc:00 1120 /system/usr/hyphen-data/hyph-ga.hyb
+70e6a2e000-70e6a42000 r--s 00000000 fc:00 109 /system/fonts/NotoSansOriyaUI-Regular.ttf
+70e6a42000-70e6a59000 r-xp 00000000 fc:00 2446 /system/lib64/android.hardware.graphics.mapper@2.0.so
+70e6a59000-70e6a6e000 ---p 00000000 00:00 0
+70e6a6e000-70e6a70000 r--p 0001e000 fc:00 2446 /system/lib64/android.hardware.graphics.mapper@2.0.so
+70e6a70000-70e6a71000 rw-p 00020000 fc:00 2446 /system/lib64/android.hardware.graphics.mapper@2.0.so
+70e6a72000-70e6a78000 r--s 00000000 fc:00 1084 /system/usr/hyphen-data/hyph-et.hyb
+70e6a78000-70e6a9d000 r--s 00000000 fc:00 207 /system/fonts/NotoSerifTelugu-Bold.ttf
+70e6a9d000-70e6a9f000 r-xp 00000000 fc:00 2330 /system/lib64/android.hardware.configstore-utils.so
+70e6a9f000-70e6abc000 ---p 00000000 00:00 0
+70e6abc000-70e6abd000 r--p 0000f000 fc:00 2330 /system/lib64/android.hardware.configstore-utils.so
+70e6abd000-70e6abe000 rw-p 00010000 fc:00 2330 /system/lib64/android.hardware.configstore-utils.so
+70e6abe000-70e6ac0000 r--s f8042000 00:10 20630 /dev/kgsl-3d0
+70e6ac0000-70e6adc000 r--s 00000000 fc:00 172 /system/fonts/NotoSansTeluguUI-Bold.ttf
+70e6adc000-70e6ae0000 r-xp 00000000 fc:00 2555 /system/lib64/libstagefright_omx_utils.so
+70e6ae0000-70e6afb000 ---p 00000000 00:00 0
+70e6afb000-70e6afc000 r--p 0000f000 fc:00 2555 /system/lib64/libstagefright_omx_utils.so
+70e6afc000-70e6afd000 rw-p 00010000 fc:00 2555 /system/lib64/libstagefright_omx_utils.so
+70e6afd000-70e6afe000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70e6afe000-70e6b02000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70e6b02000-70e6b27000 r--s 00000000 fc:00 271 /system/fonts/NotoSerifTelugu-Regular.ttf
+70e6b27000-70e6b61000 r-xp 00000000 fc:00 2695 /system/lib64/libdexfile.so
+70e6b61000-70e6b73000 ---p 00000000 00:00 0
+70e6b73000-70e6b75000 r--p 0003e000 fc:00 2695 /system/lib64/libdexfile.so
+70e6b75000-70e6b76000 rw-p 00040000 fc:00 2695 /system/lib64/libdexfile.so
+70e6b76000-70e6b78000 rw-p 00000000 00:05 10253452 /dev/ashmem/dalvik-indirect ref table (deleted)
+70e6b78000-70e6b85000 r--s 00000000 fc:00 1080 /system/usr/hyphen-data/hyph-cu.hyb
+70e6b85000-70e6b96000 r-xp 00000000 fc:00 2957 /system/lib64/libaudioutils.so
+70e6b96000-70e6bb4000 ---p 00000000 00:00 0
+70e6bb4000-70e6bb5000 r--p 0001f000 fc:00 2957 /system/lib64/libaudioutils.so
+70e6bb5000-70e6bb6000 rw-p 00020000 fc:00 2957 /system/lib64/libaudioutils.so
+70e6bb6000-70e6bb7000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70e6bb7000-70e6bba000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70e6bba000-70e6bbb000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70e6bbb000-70e6bd7000 r--s 00000000 fc:00 132 /system/fonts/NotoSansTeluguUI-Regular.ttf
+70e6bd7000-70e6bdc000 r-xp 00000000 fc:00 2409 /system/lib64/libprocessgroup.so
+70e6bdc000-70e6bf6000 ---p 00000000 00:00 0
+70e6bf6000-70e6bf7000 r--p 0000f000 fc:00 2409 /system/lib64/libprocessgroup.so
+70e6bf7000-70e6bf8000 rw-p 00010000 fc:00 2409 /system/lib64/libprocessgroup.so
+70e6bf8000-70e6c09000 r--s 00000000 fc:00 79 /system/fonts/NotoSansNewa-Regular.otf
+70e6c09000-70e6c1c000 r-xp 00000000 fc:00 2329 /system/lib64/android.hidl.memory.token@1.0.so
+70e6c1c000-70e6c36000 ---p 00000000 00:00 0
+70e6c36000-70e6c38000 r--p 0001e000 fc:00 2329 /system/lib64/android.hidl.memory.token@1.0.so
+70e6c38000-70e6c39000 rw-p 00020000 fc:00 2329 /system/lib64/android.hidl.memory.token@1.0.so
+70e6c3a000-70e6c4f000 r--s 00000000 fc:00 253 /system/fonts/NotoSansOriya-Bold.ttf
+70e6c4f000-70e6c6b000 r-xp 00000000 fc:00 2407 /system/lib64/libutils.so
+70e6c6b000-70e6c7e000 ---p 00000000 00:00 0
+70e6c7e000-70e6c7f000 r--p 0001f000 fc:00 2407 /system/lib64/libutils.so
+70e6c7f000-70e6c80000 rw-p 00020000 fc:00 2407 /system/lib64/libutils.so
+70e6c80000-70e6c9d000 r-xp 00000000 fc:00 2934 /system/lib64/libtinyxml2.so
+70e6c9d000-70e6cba000 ---p 00000000 00:00 0
+70e6cba000-70e6cbc000 r--p 0001e000 fc:00 2934 /system/lib64/libtinyxml2.so
+70e6cbc000-70e6cbf000 rw-p 00020000 fc:00 2934 /system/lib64/libtinyxml2.so
+70e6cbf000-70e6ccf000 r--s 00000000 fc:00 80 /system/fonts/NotoSansMarchen-Regular.otf
+70e6ccf000-70e6ce0000 r-xp 00000000 fc:00 2655 /system/lib64/libbase.so
+70e6ce0000-70e6cfe000 ---p 00000000 00:00 0
+70e6cfe000-70e6cff000 r--p 0001f000 fc:00 2655 /system/lib64/libbase.so
+70e6cff000-70e6d00000 rw-p 00020000 fc:00 2655 /system/lib64/libbase.so
+70e6d00000-70e6d09000 r--s 00000000 fc:00 1113 /system/usr/hyphen-data/hyph-cy.hyb
+70e6d09000-70e6d50000 r-xp 00000000 fc:00 2495 /system/lib64/libRScpp.so
+70e6d50000-70e6d68000 ---p 00000000 00:00 0
+70e6d68000-70e6d69000 r--p 0004f000 fc:00 2495 /system/lib64/libRScpp.so
+70e6d69000-70e6d6a000 rw-p 00050000 fc:00 2495 /system/lib64/libRScpp.so
+70e6d6b000-70e6d6d000 r--s 00088000 103:1d 1736830 /data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/base.apk
+70e6d6d000-70e6d7d000 r--s 00000000 fc:00 238 /system/fonts/NotoSansVai-Regular.ttf
+70e6d7d000-70e6d98000 r--s 00000000 fc:00 276 /system/fonts/NotoSansTelugu-Bold.ttf
+70e6d98000-70e6f2b000 r-xp 00000000 fc:00 2961 /system/lib64/libicuuc.so
+70e6f2b000-70e6f47000 ---p 00000000 00:00 0
+70e6f47000-70e6f5c000 r--p 0019b000 fc:00 2961 /system/lib64/libicuuc.so
+70e6f5c000-70e6f5d000 rw-p 001b0000 fc:00 2961 /system/lib64/libicuuc.so
+70e6f5d000-70e6f5e000 rw-p 00000000 00:00 0 [anon:.bss]
+70e6f5f000-70e6f68000 r--s 00000000 fc:00 159 /system/fonts/NotoSansLinearA-Regular.otf
+70e6f68000-70e6f84000 r--s 00000000 fc:00 170 /system/fonts/NotoSansTelugu-Regular.ttf
+70e6f84000-70e7058000 r-xp 00000000 fc:00 2356 /system/lib64/libc.so
+70e7058000-70e706e000 ---p 00000000 00:00 0
+70e706e000-70e7074000 r--p 000da000 fc:00 2356 /system/lib64/libc.so
+70e7074000-70e7076000 rw-p 000e0000 fc:00 2356 /system/lib64/libc.so
+70e7076000-70e7077000 rw-p 00000000 00:00 0 [anon:.bss]
+70e7077000-70e7078000 r--p 00000000 00:00 0 [anon:.bss]
+70e7078000-70e7080000 rw-p 00000000 00:00 0 [anon:.bss]
+70e7080000-70e7087000 r--s 00000000 fc:00 102 /system/fonts/NotoSansSharada-Regular.otf
+70e7087000-70e708e000 r-xp 00000000 fc:00 2378 /system/lib64/libheif.so
+70e708e000-70e70a4000 ---p 00000000 00:00 0
+70e70a4000-70e70a6000 r--p 0000e000 fc:00 2378 /system/lib64/libheif.so
+70e70a6000-70e70a7000 rw-p 00010000 fc:00 2378 /system/lib64/libheif.so
+70e70a7000-70e70a9000 r--s 00000000 fc:00 1116 /system/usr/hyphen-data/hyph-sl.hyb
+70e70a9000-70e70ab000 r--s 00000000 fc:00 1147 /system/usr/hyphen-data/hyph-mn-cyrl.hyb
+70e70ab000-70e70c5000 r--s 00000000 fc:00 291 /system/fonts/NotoSansBengaliUI-Bold.ttf
+70e70c5000-70e70ea000 r-xp 00000000 fc:00 2545 /system/lib64/libEGL.so
+70e70ea000-70e7109000 ---p 00000000 00:00 0
+70e7109000-70e710d000 r--p 0002c000 fc:00 2545 /system/lib64/libEGL.so
+70e710d000-70e710e000 rw-p 00030000 fc:00 2545 /system/lib64/libEGL.so
+70e710e000-70e7115000 rw-p 00000000 00:00 0 [anon:.bss]
+70e7115000-70e7119000 r--s 00000000 fc:00 1143 /system/usr/hyphen-data/hyph-es.hyb
+70e7119000-70e712e000 r--s 00000000 fc:00 71 /system/fonts/NotoSansOriya-Regular.ttf
+70e712e000-70e717a000 r--s 00000000 fc:00 272 /system/fonts/Roboto-Thin.ttf
+70e717a000-70e71db000 r-xp 00000000 fc:00 2393 /system/lib64/libpdx_default_transport.so
+70e71db000-70e71f7000 ---p 00000000 00:00 0
+70e71f7000-70e71f9000 r--p 0006e000 fc:00 2393 /system/lib64/libpdx_default_transport.so
+70e71f9000-70e71fa000 rw-p 00070000 fc:00 2393 /system/lib64/libpdx_default_transport.so
+70e71fa000-70e71fb000 rw-p 00000000 00:00 0 [anon:.bss]
+70e71fc000-70e71fe000 r--s 00000000 fc:00 1107 /system/usr/hyphen-data/hyph-fr.hyb
+70e71fe000-70e7200000 r--s 00000000 fc:00 1097 /system/usr/hyphen-data/hyph-da.hyb
+70e7200000-70e7223000 r-xp 00000000 fc:00 2380 /system/lib64/libminikin.so
+70e7223000-70e723e000 ---p 00000000 00:00 0
+70e723e000-70e723f000 r--p 0002f000 fc:00 2380 /system/lib64/libminikin.so
+70e723f000-70e7240000 rw-p 00030000 fc:00 2380 /system/lib64/libminikin.so
+70e7241000-70e724d000 r--s 00000000 fc:00 179 /system/fonts/NotoSansTaiTham-Regular.ttf
+70e724d000-70e725c000 r-xp 00000000 fc:00 2527 /system/lib64/libmediautils.so
+70e725c000-70e7279000 ---p 00000000 00:00 0
+70e7279000-70e727b000 r--p 0001e000 fc:00 2527 /system/lib64/libmediautils.so
+70e727b000-70e727c000 rw-p 00020000 fc:00 2527 /system/lib64/libmediautils.so
+70e727d000-70e7283000 r--s 00000000 fc:00 136 /system/fonts/NotoSansMiao-Regular.otf
+70e7283000-70e74d2000 r-xp 00000000 fc:00 2349 /system/lib64/libicui18n.so
+70e74d2000-70e74e6000 ---p 00000000 00:00 0
+70e74e6000-70e74fa000 r--p 0025c000 fc:00 2349 /system/lib64/libicui18n.so
+70e74fa000-70e74fb000 rw-p 00270000 fc:00 2349 /system/lib64/libicui18n.so
+70e74fc000-70e74ff000 r--s 00000000 103:1d 1474562 /data/resource-cache/vendor@overlay@framework-res__auto_generated_rro.apk@idmap
+70e74ff000-70e750c000 r--s 00000000 fc:00 126 /system/fonts/NotoSansSyriacWestern-Regular.ttf
+70e750c000-70e751b000 r-xp 00000000 fc:00 2466 /system/lib64/libmediaextractor.so
+70e751b000-70e753a000 ---p 00000000 00:00 0
+70e753a000-70e753b000 r--p 0000f000 fc:00 2466 /system/lib64/libmediaextractor.so
+70e753b000-70e753c000 rw-p 00010000 fc:00 2466 /system/lib64/libmediaextractor.so
+70e753d000-70e7540000 r--s 00000000 fc:00 107 /system/fonts/NotoSansPauCinHau-Regular.otf
+70e7540000-70e7593000 r-xp 00000000 fc:00 2363 /system/lib64/libandroidfw.so
+70e7593000-70e75ac000 ---p 00000000 00:00 0
+70e75ac000-70e75af000 r--p 0005d000 fc:00 2363 /system/lib64/libandroidfw.so
+70e75af000-70e75b0000 rw-p 00060000 fc:00 2363 /system/lib64/libandroidfw.so
+70e75b0000-70e75b2000 r--s 00000000 fc:00 1083 /system/usr/hyphen-data/hyph-be.hyb
+70e75b2000-70e75cd000 r--s 00000000 fc:00 270 /system/fonts/NotoSansBengaliUI-Regular.ttf
+70e75cd000-70e75cf000 r-xp 00000000 fc:00 2701 /system/lib64/libmemtrack.so
+70e75cf000-70e75ec000 ---p 00000000 00:00 0
+70e75ec000-70e75ed000 r--p 0000f000 fc:00 2701 /system/lib64/libmemtrack.so
+70e75ed000-70e75ee000 rw-p 00010000 fc:00 2701 /system/lib64/libmemtrack.so
+70e75ee000-70e75f0000 r--s 00000000 fc:00 209 /system/fonts/NotoSansSoraSompeng-Regular.otf
+70e75f0000-70e760d000 r--s 00000000 fc:00 243 /system/fonts/NotoSerifBengali-Bold.ttf
+70e760d000-70e7613000 r-xp 00000000 fc:00 2667 /system/lib64/libutilscallstack.so
+70e7613000-70e762c000 ---p 00000000 00:00 0
+70e762c000-70e762d000 r--p 0000f000 fc:00 2667 /system/lib64/libutilscallstack.so
+70e762d000-70e762e000 rw-p 00010000 fc:00 2667 /system/lib64/libutilscallstack.so
+70e762e000-70e7632000 r--s 00000000 fc:00 99 /system/fonts/NotoSansPahawhHmong-Regular.otf
+70e7632000-70e764f000 r--s 00000000 fc:00 205 /system/fonts/NotoSerifBengali-Regular.ttf
+70e764f000-70e7661000 r-xp 00000000 fc:00 2710 /system/lib64/libcutils.so
+70e7661000-70e767d000 ---p 00000000 00:00 0
+70e767d000-70e767f000 r--p 0001e000 fc:00 2710 /system/lib64/libcutils.so
+70e767f000-70e7680000 rw-p 00020000 fc:00 2710 /system/lib64/libcutils.so
+70e7680000-70e7683000 r--s 00000000 fc:00 257 /system/fonts/NotoSansPalmyrene-Regular.otf
+70e7683000-70e7697000 r--s 00000000 fc:00 78 /system/fonts/NotoSansKannadaUI-Bold.ttf
+70e7697000-70e769a000 r-xp 00000000 fc:00 2362 /system/lib64/libstagefright_http_support.so
+70e769a000-70e76b6000 ---p 00000000 00:00 0
+70e76b6000-70e76b7000 r--p 0000f000 fc:00 2362 /system/lib64/libstagefright_http_support.so
+70e76b7000-70e76b8000 rw-p 00010000 fc:00 2362 /system/lib64/libstagefright_http_support.so
+70e76b8000-70e76b9000 rw-p 00000000 00:00 0 [anon:linker_alloc_lob]
+70e76b9000-70e76be000 r--s 00000000 fc:00 165 /system/fonts/NotoSansMeroitic-Regular.otf
+70e76be000-70e76cb000 r--s 00000000 fc:00 112 /system/fonts/NotoSansSyriacEastern-Regular.ttf
+70e76cb000-70e76de000 r-xp 00000000 fc:00 2343 /system/lib64/libsensor.so
+70e76de000-70e76f5000 ---p 00000000 00:00 0
+70e76f5000-70e76f8000 r--p 0001d000 fc:00 2343 /system/lib64/libsensor.so
+70e76f8000-70e76f9000 rw-p 00020000 fc:00 2343 /system/lib64/libsensor.so
+70e76f9000-70e76fc000 r--s 00000000 fc:00 157 /system/fonts/NotoSansOldPermic-Regular.otf
+70e76fc000-70e7708000 r--s 00000000 fc:00 189 /system/fonts/NotoSansSyriacEstrangela-Regular.ttf
+70e7708000-70e771d000 r-xp 00000000 fc:00 2339 /system/lib64/android.hidl.token@1.0.so
+70e771d000-70e7735000 ---p 00000000 00:00 0
+70e7735000-70e7737000 r--p 0001e000 fc:00 2339 /system/lib64/android.hidl.token@1.0.so
+70e7737000-70e7738000 rw-p 00020000 fc:00 2339 /system/lib64/android.hidl.token@1.0.so
+70e7738000-70e7739000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70e7739000-70e773b000 r--s 00000000 fc:00 267 /system/fonts/NotoSansOldNorthArabian-Regular.otf
+70e773b000-70e7740000 r--s 00000000 fc:00 208 /system/fonts/NotoSansManichaean-Regular.otf
+70e7740000-70e775c000 r--s 00000000 fc:00 118 /system/fonts/NotoSansGujaratiUI-Bold.ttf
+70e775c000-70e7767000 r-xp 00000000 fc:00 2525 /system/lib64/libappfuse.so
+70e7767000-70e777b000 ---p 00000000 00:00 0
+70e777b000-70e777c000 r--p 0000f000 fc:00 2525 /system/lib64/libappfuse.so
+70e777c000-70e777d000 rw-p 00010000 fc:00 2525 /system/lib64/libappfuse.so
+70e777e000-70e7795000 r--s 00000000 fc:00 250 /system/fonts/NotoSerifKannada-Bold.ttf
+70e7795000-70e7798000 r-xp 00000000 fc:00 2413 /system/lib64/libpackagelistparser.so
+70e7798000-70e77b4000 ---p 00000000 00:00 0
+70e77b4000-70e77b5000 r--p 0000f000 fc:00 2413 /system/lib64/libpackagelistparser.so
+70e77b5000-70e77b6000 rw-p 00010000 fc:00 2413 /system/lib64/libpackagelistparser.so
+70e77b6000-70e77b8000 r--s 00000000 fc:00 147 /system/fonts/NotoSansNabataean-Regular.otf
+70e77b8000-70e77ba000 r--s 00000000 fc:00 146 /system/fonts/NotoSansMultani-Regular.otf
+70e77ba000-70e77c1000 r--s 00000000 fc:00 214 /system/fonts/NotoSansPhagsPa-Regular.ttf
+70e77c1000-70e77de000 r--s 00000000 fc:00 90 /system/fonts/NotoSansGujaratiUI-Regular.ttf
+70e77de000-70e77e0000 r-xp 00000000 fc:00 2430 /system/lib64/libdl.so
+70e77e0000-70e77fd000 ---p 00000000 00:00 0
+70e77fd000-70e77fe000 r--p 0000f000 fc:00 2430 /system/lib64/libdl.so
+70e77fe000-70e77ff000 r--p 00000000 00:00 0 [anon:.bss]
+70e7800000-70e7809000 r--s 00000000 fc:00 258 /system/fonts/NotoSansSymbols-Regular-Subsetted2.ttf
+70e7809000-70e7857000 r-xp 00000000 fc:00 2560 /system/lib64/libjpeg.so
+70e7857000-70e7868000 ---p 00000000 00:00 0
+70e7868000-70e7869000 r--p 0004f000 fc:00 2560 /system/lib64/libjpeg.so
+70e7869000-70e786a000 rw-p 00050000 fc:00 2560 /system/lib64/libjpeg.so
+70e786a000-70e7887000 r--s 00000000 fc:00 237 /system/fonts/NotoSansGujarati-Bold.ttf
+70e7887000-70e788a000 r-xp 00000000 fc:00 2608 /system/lib64/libnetd_client.so
+70e788a000-70e78a6000 ---p 00000000 00:00 0
+70e78a6000-70e78a7000 r--p 0000f000 fc:00 2608 /system/lib64/libnetd_client.so
+70e78a7000-70e78a8000 rw-p 00010000 fc:00 2608 /system/lib64/libnetd_client.so
+70e78a8000-70e78aa000 r--s 00000000 fc:00 290 /system/fonts/NotoSansMro-Regular.otf
+70e78aa000-70e78b9000 r--s 00000000 fc:00 84 /system/fonts/NotoSansLinearB-Regular.ttf
+70e78b9000-70e78d7000 r--s 00000000 fc:00 287 /system/fonts/NotoSansGujarati-Regular.ttf
+70e78d7000-70e78d8000 r-xp 00000000 fc:00 2651 /system/lib64/libvndksupport.so
+70e78d8000-70e78f6000 ---p 00000000 00:00 0
+70e78f6000-70e78f7000 r--p 0000f000 fc:00 2651 /system/lib64/libvndksupport.so
+70e78f7000-70e78f8000 rw-p 00010000 fc:00 2651 /system/lib64/libvndksupport.so
+70e78f9000-70e78fc000 r--s 00000000 fc:00 178 /system/fonts/NotoSansTaiLe-Regular.ttf
+70e78fc000-70e7900000 r--s 00000000 fc:00 163 /system/fonts/NotoSansTifinagh-Regular.ttf
+70e7900000-70e7906000 r-xp 00000000 fc:00 2659 /system/lib64/libnativeloader.so
+70e7906000-70e791f000 ---p 00000000 00:00 0
+70e791f000-70e7920000 r--p 0000f000 fc:00 2659 /system/lib64/libnativeloader.so
+70e7920000-70e7921000 rw-p 00010000 fc:00 2659 /system/lib64/libnativeloader.so
+70e7921000-70e7923000 r--s 00000000 fc:00 268 /system/fonts/NotoSansHatran-Regular.otf
+70e7923000-70e7927000 r--s 00000000 fc:00 195 /system/fonts/NotoSansTaiViet-Regular.ttf
+70e7927000-70e7945000 r--s 00000000 fc:00 139 /system/fonts/NotoSansDevanagariUI-Bold.ttf
+70e7945000-70e79a3000 r-xp 00000000 fc:00 2450 /system/lib64/libharfbuzz_ng.so
+70e79a3000-70e79b3000 ---p 00000000 00:00 0
+70e79b3000-70e79b5000 r--p 0005e000 fc:00 2450 /system/lib64/libharfbuzz_ng.so
+70e79b5000-70e79b6000 rw-p 00060000 fc:00 2450 /system/lib64/libharfbuzz_ng.so
+70e79b6000-70e79c5000 r--s 00000000 fc:00 111 /system/fonts/NotoSansKaithi-Regular.ttf
+70e79c5000-70e7a92000 r-xp 00000000 fc:00 2332 /system/lib64/libc++.so
+70e7a92000-70e7aae000 ---p 00000000 00:00 0
+70e7aae000-70e7ab8000 r--p 000d6000 fc:00 2332 /system/lib64/libc++.so
+70e7ab8000-70e7ab9000 rw-p 000e0000 fc:00 2332 /system/lib64/libc++.so
+70e7ab9000-70e7abc000 rw-p 00000000 00:00 0 [anon:.bss]
+70e7abc000-70e7abe000 r--s 00000000 fc:00 73 /system/fonts/NotoSansBassaVah-Regular.otf
+70e7abe000-70e7ac8000 r--s 00000000 fc:00 254 /system/fonts/NotoSansJavanese-Regular.ttf
+70e7ac8000-70e7acb000 r-xp 00000000 fc:00 2660 /system/lib64/libnativebridge.so
+70e7acb000-70e7ae7000 ---p 00000000 00:00 0
+70e7ae7000-70e7ae8000 r--p 0000f000 fc:00 2660 /system/lib64/libnativebridge.so
+70e7ae8000-70e7ae9000 rw-p 00010000 fc:00 2660 /system/lib64/libnativebridge.so
+70e7ae9000-70e7aee000 r--s 00000000 fc:00 158 /system/fonts/NotoSansSaurashtra-Regular.ttf
+70e7aee000-70e7b0f000 r--s 00000000 fc:00 82 /system/fonts/NotoSansDevanagari-Bold.ttf
+70e7b0f000-70e7b2e000 r-xp 00000000 fc:00 2612 /system/lib64/libpcre2.so
+70e7b2e000-70e7b3e000 ---p 00000000 00:00 0
+70e7b3e000-70e7b3f000 r--p 0001f000 fc:00 2612 /system/lib64/libpcre2.so
+70e7b3f000-70e7b40000 rw-p 00020000 fc:00 2612 /system/lib64/libpcre2.so
+70e7b40000-70e7bcc000 r-xp 00000000 fc:00 2975 /system/lib64/libgui.so
+70e7bcc000-70e7be2000 ---p 00000000 00:00 0
+70e7be2000-70e7bf5000 r--p 0008d000 fc:00 2975 /system/lib64/libgui.so
+70e7bf5000-70e7bf6000 rw-p 000a0000 fc:00 2975 /system/lib64/libgui.so
+70e7bf6000-70e7bf7000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70e7bf7000-70e7bf9000 r--s 00000000 fc:00 228 /system/fonts/NotoSansUgaritic-Regular.ttf
+70e7bf9000-70e7c08000 r--s 00000000 fc:00 89 /system/fonts/NotoSansCherokee-Regular.ttf
+70e7c08000-70e7dea000 r-xp 00000000 fc:00 2441 /system/lib64/libandroid_runtime.so
+70e7dea000-70e7e04000 ---p 00000000 00:00 0
+70e7e04000-70e7e23000 r--p 001e1000 fc:00 2441 /system/lib64/libandroid_runtime.so
+70e7e23000-70e7e24000 rw-p 00200000 fc:00 2441 /system/lib64/libandroid_runtime.so
+70e7e24000-70e7e28000 rw-p 00000000 00:00 0 [anon:.bss]
+70e7e29000-70e7e66000 r--s 00000000 fc:00 74 /system/fonts/NotoSerif-Bold.ttf
+70e7e66000-70e7ed0000 r-xp 00000000 fc:00 2547 /system/lib64/libclang_rt.ubsan_standalone-aarch64-android.so
+70e7ed0000-70e7edf000 ---p 00000000 00:00 0
+70e7edf000-70e7ee1000 r--p 00069000 fc:00 2547 /system/lib64/libclang_rt.ubsan_standalone-aarch64-android.so
+70e7ee1000-70e7ee4000 rw-p 0006b000 fc:00 2547 /system/lib64/libclang_rt.ubsan_standalone-aarch64-android.so
+70e7ee4000-70e89f6000 rw-p 00000000 00:00 0 [anon:.bss]
+70e89f6000-70e89fa000 r--s 00000000 fc:00 127 /system/fonts/NotoSansSylotiNagri-Regular.ttf
+70e89fa000-70e8a06000 r--s 00000000 fc:00 93 /system/fonts/NotoSansCanadianAboriginal-Regular.ttf
+70e8a06000-70e8a1b000 r-xp 00000000 fc:00 2460 /system/lib64/android.hardware.graphics.allocator@2.0.so
+70e8a1b000-70e8a33000 ---p 00000000 00:00 0
+70e8a33000-70e8a35000 r--p 0001e000 fc:00 2460 /system/lib64/android.hardware.graphics.allocator@2.0.so
+70e8a35000-70e8a36000 rw-p 00020000 fc:00 2460 /system/lib64/android.hardware.graphics.allocator@2.0.so
+70e8a36000-70e8a55000 r--s 00000000 fc:00 145 /system/fonts/NotoSansDevanagariUI-Regular.ttf
+70e8a55000-70e8a61000 r-xp 00000000 fc:00 2540 /system/lib64/libstagefright_xmlparser.so
+70e8a61000-70e8a74000 ---p 00000000 00:00 0
+70e8a74000-70e8a75000 r--p 0000f000 fc:00 2540 /system/lib64/libstagefright_xmlparser.so
+70e8a75000-70e8a76000 rw-p 00010000 fc:00 2540 /system/lib64/libstagefright_xmlparser.so
+70e8a76000-70e8a78000 r--s 00000000 fc:00 293 /system/fonts/NotoSansTagbanwa-Regular.ttf
+70e8a78000-70e8a8f000 r--s 00000000 fc:00 161 /system/fonts/NotoSerifKannada-Regular.ttf
+70e8a8f000-70e8b23000 r-xp 00000000 fc:00 2633 /system/lib64/libaudioclient.so
+70e8b23000-70e8b37000 ---p 00000000 00:00 0
+70e8b37000-70e8b49000 r--p 0009e000 fc:00 2633 /system/lib64/libaudioclient.so
+70e8b49000-70e8b55000 rw-p 000b0000 fc:00 2633 /system/lib64/libaudioclient.so
+70e8b55000-70e8b9f000 r--s 00000000 fc:00 83 /system/fonts/RobotoCondensed-Light.ttf
+70e8b9f000-70e8ba1000 r-xp 00000000 fc:00 2520 /system/lib64/libhardware_legacy.so
+70e8ba1000-70e8bbe000 ---p 00000000 00:00 0
+70e8bbe000-70e8bbf000 r--p 0000f000 fc:00 2520 /system/lib64/libhardware_legacy.so
+70e8bbf000-70e8bc0000 rw-p 00010000 fc:00 2520 /system/lib64/libhardware_legacy.so
+70e8bc0000-70e8be0000 r-xp 00000000 fc:00 2410 /system/lib64/android.hidl.memory@1.0.so
+70e8be0000-70e8bfa000 ---p 00000000 00:00 0
+70e8bfa000-70e8bfd000 r--p 0002d000 fc:00 2410 /system/lib64/android.hidl.memory@1.0.so
+70e8bfd000-70e8bfe000 rw-p 00030000 fc:00 2410 /system/lib64/android.hidl.memory@1.0.so
+70e8bfe000-70e8bff000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70e8bff000-70e8c02000 r--s 00000000 fc:00 273 /system/fonts/NotoSansSundanese-Regular.ttf
+70e8c02000-70e8c0f000 r--s 00000000 fc:00 115 /system/fonts/NotoSansAdlam-Regular.ttf
+70e8c0f000-70e8c18000 r-xp 00000000 fc:00 2350 /system/lib64/libnetdutils.so
+70e8c18000-70e8c2e000 ---p 00000000 00:00 0
+70e8c2e000-70e8c2f000 r--p 0000f000 fc:00 2350 /system/lib64/libnetdutils.so
+70e8c2f000-70e8c30000 rw-p 00010000 fc:00 2350 /system/lib64/libnetdutils.so
+70e8c30000-70e8c44000 r--s 00000000 fc:00 283 /system/fonts/NotoSansKannadaUI-Regular.ttf
+70e8c44000-70e8c45000 r-xp 00000000 fc:00 2926 /system/lib64/libhidlallocatorutils.so
+70e8c45000-70e8c63000 ---p 00000000 00:00 0
+70e8c63000-70e8c64000 r--p 0000f000 fc:00 2926 /system/lib64/libhidlallocatorutils.so
+70e8c64000-70e8c65000 rw-p 00010000 fc:00 2926 /system/lib64/libhidlallocatorutils.so
+70e8c65000-70e8c67000 r--s 00000000 fc:00 65 /system/fonts/NotoSansTagalog-Regular.ttf
+70e8c67000-70e8c70000 r--s 00000000 fc:00 294 /system/fonts/NotoSansChakma-Regular.ttf
+70e8c70000-70e8c92000 r--s 00000000 fc:00 116 /system/fonts/NotoSansDevanagari-Regular.ttf
+70e8c92000-70e8c94000 r-xp 00000000 fc:00 2501 /system/lib64/libsync.so
+70e8c94000-70e8cb1000 ---p 00000000 00:00 0
+70e8cb1000-70e8cb2000 r--p 0000f000 fc:00 2501 /system/lib64/libsync.so
+70e8cb2000-70e8cb3000 rw-p 00010000 fc:00 2501 /system/lib64/libsync.so
+70e8cb3000-70e8cc6000 r--s 00000000 fc:00 196 /system/fonts/NotoSerifSinhala-Regular.otf
+70e8cc6000-70e8d5c000 r-xp 00000000 fc:00 2403 /system/lib64/libmedia.so
+70e8d5c000-70e8d70000 ---p 00000000 00:00 0
+70e8d70000-70e8d88000 r--p 00098000 fc:00 2403 /system/lib64/libmedia.so
+70e8d88000-70e8d95000 rw-p 000b0000 fc:00 2403 /system/lib64/libmedia.so
+70e8d95000-70e8d96000 r--p 00000000 00:00 0 [anon:atexit handlers]
+70e8d96000-70e8d99000 r--s 00000000 fc:00 247 /system/fonts/NotoSansSamaritan-Regular.ttf
+70e8d99000-70e8dad000 r--s 00000000 fc:00 108 /system/fonts/NotoSansKannada-Bold.ttf
+70e8dad000-70e8dcd000 r--s 00000000 fc:00 303 /system/fonts/NotoSerifEthiopic-Bold.otf
+70e8dcd000-70e8de5000 r-xp 00000000 fc:00 2954 /system/lib64/libGLESv2.so
+70e8de5000-70e8dfc000 ---p 00000000 00:00 0
+70e8dfc000-70e8dfd000 r--p 0001f000 fc:00 2954 /system/lib64/libGLESv2.so
+70e8dfd000-70e8dfe000 rw-p 00020000 fc:00 2954 /system/lib64/libGLESv2.so
+70e8dfe000-70e8e06000 r--s 00000000 fc:00 265 /system/fonts/NotoSansBalinese-Regular.ttf
+70e8e06000-70e8e0e000 r--s 00000000 fc:00 219 /system/fonts/NotoSansLaoUI-Bold.ttf
+70e8e0e000-70e8e12000 r-xp 00000000 fc:00 2617 /system/lib64/libdebuggerd_client.so
+70e8e12000-70e8e2d000 ---p 00000000 00:00 0
+70e8e2d000-70e8e2e000 r--p 0000f000 fc:00 2617 /system/lib64/libdebuggerd_client.so
+70e8e2e000-70e8e2f000 rw-p 00010000 fc:00 2617 /system/lib64/libdebuggerd_client.so
+70e8e2f000-70e8e4b000 r--s 00000000 fc:00 211 /system/fonts/NotoSerifEthiopic-Regular.otf
+70e8e4b000-70e8e5e000 r-xp 00000000 fc:00 2484 /system/lib64/android.hardware.memtrack@1.0.so
+70e8e5e000-70e8e78000 ---p 00000000 00:00 0
+70e8e78000-70e8e7a000 r--p 0001e000 fc:00 2484 /system/lib64/android.hardware.memtrack@1.0.so
+70e8e7a000-70e8e7b000 rw-p 00020000 fc:00 2484 /system/lib64/android.hardware.memtrack@1.0.so
+70e8e7b000-70e8e7d000 r--s 00000000 fc:00 261 /system/fonts/NotoSansShavian-Regular.ttf
+70e8e7d000-70e8e80000 r--s 00000000 fc:00 204 /system/fonts/NotoSansRunic-Regular.ttf
+70e8e80000-70e8ea1000 r-xp 00000000 fc:00 2512 /system/lib64/android.hardware.configstore@1.0.so
+70e8ea1000-70e8ebb000 ---p 00000000 00:00 0
+70e8ebb000-70e8ebe000 r--p 0002d000 fc:00 2512 /system/lib64/android.hardware.configstore@1.0.so
+70e8ebe000-70e8ebf000 rw-p 00030000 fc:00 2512 /system/lib64/android.hardware.configstore@1.0.so
+70e8ebf000-70e8ee3000 r--s 00000000 fc:00 226 /system/fonts/NotoSansEthiopic-Bold.ttf
+70e8ee3000-70e8f1a000 r-xp 00000000 fc:00 2337 /system/lib64/libm.so
+70e8f1a000-70e8f32000 ---p 00000000 00:00 0
+70e8f32000-70e8f33000 r--p 0003f000 fc:00 2337 /system/lib64/libm.so
+70e8f33000-70e8f34000 rw-p 00040000 fc:00 2337 /system/lib64/libm.so
+70e8f34000-70e8f36000 r--s 00000000 fc:00 133 /system/fonts/NotoSansRejang-Regular.ttf
+70e8f36000-70e8f38000 r--s 00000000 fc:00 69 /system/fonts/NotoSansPhoenician-Regular.ttf
+70e8f38000-70e8f40000 r--s 00000000 fc:00 245 /system/fonts/NotoSansLaoUI-Regular.ttf
+70e8f40000-70e8f45000 r-xp 00000000 fc:00 2341 /system/lib64/libstagefright_codecbase.so
+70e8f45000-70e8f5f000 ---p 00000000 00:00 0
+70e8f5f000-70e8f60000 r--p 0000f000 fc:00 2341 /system/lib64/libstagefright_codecbase.so
+70e8f60000-70e8f61000 rw-p 00010000 fc:00 2341 /system/lib64/libstagefright_codecbase.so
+70e8f61000-70e8f62000 r--p 00000000 00:00 0 [anon:atexit handlers]
+70e8f62000-70e8f66000 r--s 00000000 fc:00 225 /system/fonts/NotoSansOldPersian-Regular.ttf
+70e8f66000-70e8f89000 r--s 00000000 fc:00 167 /system/fonts/NotoSansEthiopic-Regular.ttf
+70e8f89000-70e8f92000 r-xp 00000000 fc:00 2515 /system/lib64/libGLESv1_CM.so
+70e8f92000-70e8fa8000 ---p 00000000 00:00 0
+70e8fa8000-70e8fa9000 r--p 0000f000 fc:00 2515 /system/lib64/libGLESv1_CM.so
+70e8fa9000-70e8faa000 rw-p 00010000 fc:00 2515 /system/lib64/libGLESv1_CM.so
+70e8faa000-70e8fad000 r--s 00000000 fc:00 206 /system/fonts/NotoSansOsage-Regular.ttf
+70e8fad000-70e8fb5000 r--s 00000000 fc:00 121 /system/fonts/NotoSerifLao-Bold.ttf
+70e8fb5000-70e8fd3000 r--s 00000000 fc:00 68 /system/fonts/NotoNaskhArabicUI-Bold.ttf
+70e8fd3000-70e9010000 r-xp 00000000 fc:00 2600 /system/lib64/android.hardware.cas@1.0.so
+70e9010000-70e9026000 ---p 00000000 00:00 0
+70e9026000-70e902c000 r--p 0004a000 fc:00 2600 /system/lib64/android.hardware.cas@1.0.so
+70e902c000-70e902d000 rw-p 00050000 fc:00 2600 /system/lib64/android.hardware.cas@1.0.so
+70e902d000-70e902e000 r--s 00000000 00:05 31475 /dev/ashmem/5c7d41a6-003d-45a5-9e3b-2d34c5829a2d (deleted)
+70e902e000-70e9043000 r--s 00000000 fc:00 120 /system/fonts/NotoSansKannada-Regular.ttf
+70e9043000-70e9067000 r-xp 00000000 fc:00 2729 /system/lib64/libexpat.so
+70e9067000-70e9081000 ---p 00000000 00:00 0
+70e9081000-70e9083000 r--p 0002e000 fc:00 2729 /system/lib64/libexpat.so
+70e9083000-70e9084000 rw-p 00030000 fc:00 2729 /system/lib64/libexpat.so
+70e9084000-70e9086000 r--s 00000000 fc:00 155 /system/fonts/NotoSansOsmanya-Regular.ttf
+70e9086000-70e90c3000 r--s 00000000 fc:00 91 /system/fonts/NotoSerif-Regular.ttf
+70e90c3000-70e91ce000 r-xp 00000000 fc:00 2647 /system/lib64/libcrypto.so
+70e91ce000-70e91e2000 ---p 00000000 00:00 0
+70e91e2000-70e91f3000 r--p 0010f000 fc:00 2647 /system/lib64/libcrypto.so
+70e91f3000-70e91f4000 rw-p 00120000 fc:00 2647 /system/lib64/libcrypto.so
+70e91f4000-70e91f5000 rw-p 00000000 00:00 0 [anon:.bss]
+70e91f5000-70e91fa000 r--s 00000000 fc:00 149 /system/fonts/NotoSansNKo-Regular.ttf
+70e91fa000-70e9202000 r--s 00000000 fc:00 198 /system/fonts/NotoSerifLao-Regular.ttf
+70e9202000-70e921b000 r-xp 00000000 fc:00 2682 /system/lib64/libmedia_helper.so
+70e921b000-70e922d000 ---p 00000000 00:00 0
+70e922d000-70e9230000 r--p 0001d000 fc:00 2682 /system/lib64/libmedia_helper.so
+70e9230000-70e9231000 rw-p 00020000 fc:00 2682 /system/lib64/libmedia_helper.so
+70e9231000-70e924a000 r--s 00000000 fc:00 232 /system/fonts/NotoSansBengali-Bold.ttf
+70e924a000-70e9256000 r-xp 00000000 fc:00 2467 /system/lib64/libsoundtrigger.so
+70e9256000-70e9272000 ---p 00000000 00:00 0
+70e9272000-70e9276000 r--p 0000c000 fc:00 2467 /system/lib64/libsoundtrigger.so
+70e9276000-70e9277000 rw-p 00010000 fc:00 2467 /system/lib64/libsoundtrigger.so
+70e9277000-70e9278000 r--s 00000000 fc:01 1177 /vendor/overlay/Pixel/PixelThemeOverlay.apk
+70e9278000-70e927c000 r--s 00000000 fc:00 215 /system/fonts/NotoSansNewTaiLue-Regular.ttf
+70e927c000-70e929a000 r--s 00000000 fc:00 235 /system/fonts/NotoNaskhArabicUI-Regular.ttf
+70e929a000-70e929b000 r-xp 00000000 fc:00 2375 /system/lib64/android.hardware.graphics.common@1.1.so
+70e929b000-70e92b9000 ---p 00000000 00:00 0
+70e92b9000-70e92ba000 r--p 0000f000 fc:00 2375 /system/lib64/android.hardware.graphics.common@1.1.so
+70e92ba000-70e92bb000 rw-p 00010000 fc:00 2375 /system/lib64/android.hardware.graphics.common@1.1.so
+70e92bb000-70e92da000 r--s 00000000 fc:00 281 /system/fonts/GoogleSans-BoldItalic.ttf
+70e92da000-70e9302000 r-xp 00000000 fc:00 2529 /system/lib64/libinput.so
+70e9302000-70e931b000 ---p 00000000 00:00 0
+70e931b000-70e9322000 r--p 00029000 fc:00 2529 /system/lib64/libinput.so
+70e9322000-70e9323000 rw-p 00030000 fc:00 2529 /system/lib64/libinput.so
+70e9323000-70e9340000 r--s 00000000 fc:00 130 /system/fonts/NotoNaskhArabic-Bold.ttf
+70e9340000-70e934b000 r-xp 00000000 fc:00 2451 /system/lib64/libbpf.so
+70e934b000-70e935f000 ---p 00000000 00:00 0
+70e935f000-70e9360000 r--p 0000f000 fc:00 2451 /system/lib64/libbpf.so
+70e9360000-70e9361000 rw-p 00010000 fc:00 2451 /system/lib64/libbpf.so
+70e9361000-70e9367000 r--s 00000000 fc:00 96 /system/fonts/NotoSansKharoshthi-Regular.ttf
+70e9367000-70e9385000 r--s 00000000 fc:00 151 /system/fonts/GoogleSans-Bold.ttf
+70e9385000-70e93b8000 r-xp 00000000 fc:00 2494 /system/lib64/libpng.so
+70e93b8000-70e93d4000 ---p 00000000 00:00 0
+70e93d4000-70e93d5000 r--p 0003f000 fc:00 2494 /system/lib64/libpng.so
+70e93d5000-70e93d6000 rw-p 00040000 fc:00 2494 /system/lib64/libpng.so
+70e93d6000-70e93d7000 r--s 00004000 fc:01 1177 /vendor/overlay/Pixel/PixelThemeOverlay.apk
+70e93d7000-70e93d9000 r--s 00000000 fc:00 295 /system/fonts/NotoSansOldTurkic-Regular.ttf
+70e93d9000-70e93e5000 r--s 00000000 fc:00 86 /system/fonts/NotoSerifKhmer-Bold.otf
+70e93e5000-70e9404000 r--s 00000000 fc:00 240 /system/fonts/GoogleSans-MediumItalic.ttf
+70e9404000-70e9409000 r-xp 00000000 fc:00 2404 /system/lib64/libhidlmemory.so
+70e9409000-70e9423000 ---p 00000000 00:00 0
+70e9423000-70e9424000 r--p 0000f000 fc:00 2404 /system/lib64/libhidlmemory.so
+70e9424000-70e9425000 rw-p 00010000 fc:00 2404 /system/lib64/libhidlmemory.so
+70e9425000-70e943e000 r--s 00000000 fc:00 185 /system/fonts/NotoSansBengali-Regular.ttf
+70e943e000-70e945c000 r--s 00000000 fc:00 129 /system/fonts/GoogleSans-Medium.ttf
+70e945c000-70e960c000 r-xp 00000000 fc:00 2398 /system/lib64/libstagefright.so
+70e960c000-70e9625000 ---p 00000000 00:00 0
+70e9625000-70e9638000 r--p 001bd000 fc:00 2398 /system/lib64/libstagefright.so
+70e9638000-70e966c000 rw-p 001d0000 fc:00 2398 /system/lib64/libstagefright.so
+70e966c000-70e966d000 rw-p 00000000 00:00 0 [anon:.bss]
+70e966d000-70e966e000 r--s 00000000 103:1d 1474566 /data/resource-cache/vendor@overlay@Pixel@PixelThemeOverlay.apk@idmap
+70e966e000-70e9672000 r--s 00000000 fc:00 241 /system/fonts/NotoSansMeeteiMayek-Regular.ttf
+70e9672000-70e9680000 r--s 00000000 fc:00 150 /system/fonts/NotoSansMalayalamUI-Bold.ttf
+70e9680000-70e96a7000 r-xp 00000000 fc:00 2365 /system/lib64/libhwbinder.so
+70e96a7000-70e96bd000 ---p 00000000 00:00 0
+70e96bd000-70e96bf000 r--p 0002e000 fc:00 2365 /system/lib64/libhwbinder.so
+70e96bf000-70e96c0000 rw-p 00030000 fc:00 2365 /system/lib64/libhwbinder.so
+70e96c0000-70e96c1000 r--s 00088000 103:1d 1736830 /data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/base.apk
+70e96c1000-70e96cb000 r--s 00000000 fc:00 94 /system/fonts/NotoSansKhmerUI-Regular.ttf
+70e96cb000-70e96d9000 r--s 00000000 fc:00 275 /system/fonts/NotoSansMalayalamUI-Regular.ttf
+70e96d9000-70e96dd000 r-xp 00000000 fc:00 2386 /system/lib64/libusbhost.so
+70e96dd000-70e96f8000 ---p 00000000 00:00 0
+70e96f8000-70e96f9000 r--p 0000f000 fc:00 2386 /system/lib64/libusbhost.so
+70e96f9000-70e96fa000 rw-p 00010000 fc:00 2386 /system/lib64/libusbhost.so
+70e96fa000-70e96fb000 r--p 00000000 00:05 10266154 /dev/ashmem/dalvik-classes.dex extracted in memory from /data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/base.apk (deleted)
+70e96fb000-70e9701000 r--s 00000000 fc:00 280 /system/fonts/NotoSansCoptic-Regular.ttf
+70e9701000-70e9720000 r-xp 00000000 fc:00 2490 /system/lib64/libstagefright_bufferqueue_helper.so
+70e9720000-70e973b000 ---p 00000000 00:00 0
+70e973b000-70e973e000 r--p 0002d000 fc:00 2490 /system/lib64/libstagefright_bufferqueue_helper.so
+70e973e000-70e9740000 rw-p 00030000 fc:00 2490 /system/lib64/libstagefright_bufferqueue_helper.so
+70e9740000-70e9742000 r--s 00000000 fc:00 141 /system/fonts/NotoSansOldSouthArabian-Regular.ttf
+70e9742000-70e974c000 r--s 00000000 fc:00 229 /system/fonts/NotoSerifKhmer-Regular.otf
+70e974c000-70e9759000 r--s 00000000 fc:00 260 /system/fonts/NotoSerifMalayalam-Bold.ttf
+70e9759000-70e97c7000 r-xp 00000000 fc:00 2428 /system/lib64/libstagefright_omx.so
+70e97c7000-70e97dc000 ---p 00000000 00:00 0
+70e97dc000-70e97e6000 r--p 00076000 fc:00 2428 /system/lib64/libstagefright_omx.so
+70e97e6000-70e97ed000 rw-p 00080000 fc:00 2428 /system/lib64/libstagefright_omx.so
+70e97ed000-70e97fa000 r--s 00000000 fc:00 66 /system/fonts/NotoSerifMalayalam-Regular.ttf
+70e97fa000-70e9819000 r--s 00000000 fc:00 183 /system/fonts/GoogleSans-Italic.ttf
+70e9819000-70e9856000 r-xp 00000000 fc:00 2434 /system/lib64/libprotobuf-cpp-lite.so
+70e9856000-70e9867000 ---p 00000000 00:00 0
+70e9867000-70e9869000 r--p 0003e000 fc:00 2434 /system/lib64/libprotobuf-cpp-lite.so
+70e9869000-70e986a000 rw-p 00040000 fc:00 2434 /system/lib64/libprotobuf-cpp-lite.so
+70e986a000-70e9873000 r--s 00000000 fc:00 134 /system/fonts/NotoSansKhmerUI-Bold.ttf
+70e9873000-70e9891000 r--s 00000000 fc:00 81 /system/fonts/GoogleSans-Regular.ttf
+70e9891000-70e989e000 r-xp 00000000 fc:00 2377 /system/lib64/libmediametrics.so
+70e989e000-70e98ae000 ---p 00000000 00:00 0
+70e98ae000-70e98b0000 r--p 0000e000 fc:00 2377 /system/lib64/libmediametrics.so
+70e98b0000-70e98b1000 rw-p 00010000 fc:00 2377 /system/lib64/libmediametrics.so
+70e98b1000-70e98b9000 r--s 00000000 fc:00 152 /system/fonts/NotoSansLao-Bold.ttf
+70e98b9000-70e98c7000 r--s 00000000 fc:00 279 /system/fonts/NotoSansMalayalam-Bold.ttf
+70e98c7000-70e98ca000 r-xp 00000000 fc:00 2952 /system/lib64/libETC1.so
+70e98ca000-70e98e6000 ---p 00000000 00:00 0
+70e98e6000-70e98e7000 r--p 0000f000 fc:00 2952 /system/lib64/libETC1.so
+70e98e7000-70e98e8000 rw-p 00010000 fc:00 2952 /system/lib64/libETC1.so
+70e98e8000-70e98e9000 r--s 00000000 fc:00 1121 /system/usr/hyphen-data/hyph-und-ethi.hyb
+70e98e9000-70e98ef000 r--s 00000000 fc:00 64 /system/fonts/NotoSansBrahmi-Regular.ttf
+70e98ef000-70e990f000 rw-p 00000000 00:05 10271012 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70e990f000-70e9926000 r-xp 00000000 fc:00 2526 /system/lib64/libbacktrace.so
+70e9926000-70e993e000 ---p 00000000 00:00 0
+70e993e000-70e993f000 r--p 0001f000 fc:00 2526 /system/lib64/libbacktrace.so
+70e993f000-70e9940000 rw-p 00020000 fc:00 2526 /system/lib64/libbacktrace.so
+70e9940000-70e9941000 r--s 00000000 fc:00 1129 /system/usr/hyphen-data/hyph-tk.hyb
+70e9941000-70e99a4000 r-xp 00000000 fc:00 2528 /system/lib64/libcamera_client.so
+70e99a4000-70e99bc000 ---p 00000000 00:00 0
+70e99bc000-70e99c9000 r--p 00063000 fc:00 2528 /system/lib64/libcamera_client.so
+70e99c9000-70e99d0000 rw-p 00070000 fc:00 2528 /system/lib64/libcamera_client.so
+70e99d0000-70e99d1000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70e99d1000-70e99d3000 r--s 00000000 fc:00 200 /system/fonts/NotoSansOldItalic-Regular.ttf
+70e99d3000-70e99e1000 r--s 00000000 fc:00 153 /system/fonts/NotoSansMalayalam-Regular.ttf
+70e99e1000-70e9a01000 rw-p 00000000 00:05 10271011 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70e9a01000-70e9a1e000 r-xp 00000000 fc:00 2542 /system/lib64/libimg_utils.so
+70e9a1e000-70e9a39000 ---p 00000000 00:00 0
+70e9a39000-70e9a3c000 r--p 0001d000 fc:00 2542 /system/lib64/libimg_utils.so
+70e9a3c000-70e9a3f000 rw-p 00020000 fc:00 2542 /system/lib64/libimg_utils.so
+70e9a3f000-70e9a5c000 r--s 00000000 fc:00 262 /system/fonts/NotoNaskhArabic-Regular.ttf
+70e9a5c000-70e9a69000 r-xp 00000000 fc:00 2706 /system/lib64/libziparchive.so
+70e9a69000-70e9a7b000 ---p 00000000 00:00 0
+70e9a7b000-70e9a7c000 r--p 0000f000 fc:00 2706 /system/lib64/libziparchive.so
+70e9a7c000-70e9a7d000 rw-p 00010000 fc:00 2706 /system/lib64/libziparchive.so
+70e9a7d000-70e9a85000 r--s 00000000 fc:00 119 /system/fonts/NotoSansLao-Regular.ttf
+70e9a85000-70e9a8e000 r--s 00000000 fc:00 77 /system/fonts/NotoSansTamilUI-Bold.ttf
+70e9a8e000-70e9a97000 r--s 00000000 fc:00 160 /system/fonts/NotoSansTamilUI-Regular.ttf
+70e9a97000-70e9a9d000 r-xp 00000000 fc:00 2536 /system/lib64/libnativehelper.so
+70e9a9d000-70e9ab6000 ---p 00000000 00:00 0
+70e9ab6000-70e9ab7000 r--p 0000f000 fc:00 2536 /system/lib64/libnativehelper.so
+70e9ab7000-70e9ab8000 rw-p 00010000 fc:00 2536 /system/lib64/libnativehelper.so
+70e9ab8000-70e9ab9000 r--s 00000000 fc:00 1134 /system/usr/hyphen-data/hyph-te.hyb
+70e9ab9000-70e9ac2000 r--s 00000000 fc:00 246 /system/fonts/NotoSerifTamil-Bold.ttf
+70e9ac2000-70e9acb000 r--s 00000000 fc:00 302 /system/fonts/NotoSerifTamil-Regular.ttf
+70e9acb000-70e9af4000 r-xp 00000000 fc:00 2950 /system/lib64/libmemunreachable.so
+70e9af4000-70e9b09000 ---p 00000000 00:00 0
+70e9b09000-70e9b0b000 r--p 0002e000 fc:00 2950 /system/lib64/libmemunreachable.so
+70e9b0b000-70e9b0c000 rw-p 00030000 fc:00 2950 /system/lib64/libmemunreachable.so
+70e9b0c000-70e9b0d000 r--s 00000000 fc:00 1088 /system/usr/hyphen-data/hyph-ta.hyb
+70e9b0d000-70e9b0f000 r--s 00000000 fc:00 72 /system/fonts/NotoSansOlChiki-Regular.ttf
+70e9b0f000-70e9b2f000 rw-p 00000000 00:05 10271010 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70e9b2f000-70e9b4f000 r--s 00000000 00:10 16633 /dev/__properties__/u:object_r:persist_debug_prop:s0
+70e9b4f000-70e9b65000 r-xp 00000000 fc:00 2920 /system/lib64/android.hardware.cas.native@1.0.so
+70e9b65000-70e9b7b000 ---p 00000000 00:00 0
+70e9b7b000-70e9b7d000 r--p 0001e000 fc:00 2920 /system/lib64/android.hardware.cas.native@1.0.so
+70e9b7d000-70e9b7e000 rw-p 00020000 fc:00 2920 /system/lib64/android.hardware.cas.native@1.0.so
+70e9b7e000-70e9b7f000 r--s 00000000 fc:00 1145 /system/usr/hyphen-data/hyph-pt.hyb
+70e9b7f000-70e9b83000 r--s 00000000 fc:00 277 /system/fonts/NotoSansMandaic-Regular.ttf
+70e9b83000-70e9bdb000 r-xp 00000000 fc:00 2334 /system/lib64/libsonivox.so
+70e9bdb000-70e9bf2000 ---p 00000000 00:00 0
+70e9bf2000-70e9bf3000 r--p 0005f000 fc:00 2334 /system/lib64/libsonivox.so
+70e9bf3000-70e9bf4000 rw-p 00060000 fc:00 2334 /system/lib64/libsonivox.so
+70e9bf4000-70e9bfb000 rw-p 00000000 00:00 0 [anon:.bss]
+70e9bfb000-70e9c01000 r--s 00000000 fc:00 123 /system/fonts/NotoSansCham-Bold.ttf
+70e9c01000-70e9c0a000 r--s 00000000 fc:00 255 /system/fonts/NotoSansTamil-Bold.ttf
+70e9c0a000-70e9c13000 r--s 00000000 fc:00 135 /system/fonts/NotoSansTamil-Regular.ttf
+70e9c13000-70e9c15000 r-xp 00000000 fc:00 2519 /system/lib64/libgraphicsenv.so
+70e9c15000-70e9c32000 ---p 00000000 00:00 0
+70e9c32000-70e9c33000 r--p 0000f000 fc:00 2519 /system/lib64/libgraphicsenv.so
+70e9c33000-70e9c34000 rw-p 00010000 fc:00 2519 /system/lib64/libgraphicsenv.so
+70e9c34000-70e9c3a000 r--s 00000000 fc:00 259 /system/fonts/NotoSansCham-Regular.ttf
+70e9c3a000-70e9c42000 r--s 00000000 fc:00 114 /system/fonts/NotoSansGurmukhiUI-Bold.ttf
+70e9c42000-70e9c4a000 r--s 00000000 fc:00 122 /system/fonts/NotoSansGurmukhiUI-Regular.ttf
+70e9c4a000-70e9c5f000 r-xp 00000000 fc:00 2348 /system/lib64/android.hidl.allocator@1.0.so
+70e9c5f000-70e9c77000 ---p 00000000 00:00 0
+70e9c77000-70e9c79000 r--p 0001e000 fc:00 2348 /system/lib64/android.hidl.allocator@1.0.so
+70e9c79000-70e9c7a000 rw-p 00020000 fc:00 2348 /system/lib64/android.hidl.allocator@1.0.so
+70e9c7a000-70e9c7b000 r--s 00000000 fc:00 1095 /system/usr/hyphen-data/hyph-pa.hyb
+70e9c7b000-70e9c83000 r--s 00000000 fc:00 298 /system/fonts/NotoSerifGurmukhi-Bold.otf
+70e9c83000-70e9d01000 r-xp 00000000 fc:00 2665 /system/lib64/libbinder.so
+70e9d01000-70e9d1e000 ---p 00000000 00:00 0
+70e9d1e000-70e9d2e000 r--p 00080000 fc:00 2665 /system/lib64/libbinder.so
+70e9d2e000-70e9d2f000 rw-p 00090000 fc:00 2665 /system/lib64/libbinder.so
+70e9d2f000-70e9d4f000 rw-p 00000000 00:05 10271009 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70e9d4f000-70e9d53000 r-xp 00000000 fc:00 2454 /system/lib64/libaudiomanager.so
+70e9d53000-70e9d6e000 ---p 00000000 00:00 0
+70e9d6e000-70e9d6f000 r--p 0000f000 fc:00 2454 /system/lib64/libaudiomanager.so
+70e9d6f000-70e9d70000 rw-p 00010000 fc:00 2454 /system/lib64/libaudiomanager.so
+70e9d70000-70e9d71000 r--s 00000000 fc:00 1087 /system/usr/hyphen-data/hyph-or.hyb
+70e9d71000-70e9d91000 rw-p 00000000 00:05 10271008 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70e9d91000-70e9e21000 r-xp 00000000 fc:00 2627 /system/lib64/libft2.so
+70e9e21000-70e9e37000 ---p 00000000 00:00 0
+70e9e37000-70e9e3c000 r--p 0009b000 fc:00 2627 /system/lib64/libft2.so
+70e9e3c000-70e9e3d000 rw-p 000a0000 fc:00 2627 /system/lib64/libft2.so
+70e9e3d000-70e9e3e000 r--s 00000000 fc:00 1142 /system/usr/hyphen-data/hyph-mr.hyb
+70e9e3e000-70e9e45000 r--s 00000000 fc:00 88 /system/fonts/NotoSerifGurmukhi-Regular.otf
+70e9e45000-70e9e65000 r--s 00000000 00:10 16594 /dev/__properties__/u:object_r:exported3_default_prop:s0
+70e9e65000-70e9e7f000 r-xp 00000000 fc:00 2643 /system/lib64/libunwind.so
+70e9e7f000-70e9e94000 ---p 00000000 00:00 0
+70e9e94000-70e9e95000 r--p 0001f000 fc:00 2643 /system/lib64/libunwind.so
+70e9e95000-70e9e96000 rw-p 00020000 fc:00 2643 /system/lib64/libunwind.so
+70e9e96000-70e9eff000 rw-p 00000000 00:00 0 [anon:.bss]
+70e9eff000-70e9f00000 r--s 00000000 fc:00 1130 /system/usr/hyphen-data/hyph-ml.hyb
+70e9f00000-70e9f02000 r--s 00000000 fc:00 75 /system/fonts/NotoSansOgham-Regular.ttf
+70e9f02000-70e9f0a000 r--s 00000000 fc:00 193 /system/fonts/NotoSansGurmukhi-Bold.ttf
+70e9f0a000-70ea022000 r-xp 00000000 fc:00 2328 /system/lib64/libsqlite.so
+70ea022000-70ea033000 ---p 00000000 00:00 0
+70ea033000-70ea036000 r--p 0011d000 fc:00 2328 /system/lib64/libsqlite.so
+70ea036000-70ea038000 rw-p 00120000 fc:00 2328 /system/lib64/libsqlite.so
+70ea038000-70ea03c000 r--s 00000000 fc:00 285 /system/fonts/NotoSansGlagolitic-Regular.ttf
+70ea03c000-70ea04c000 r--s 00000000 fc:00 184 /system/fonts/NotoSerifGujarati-Bold.ttf
+70ea04c000-70ea060000 r-xp 00000000 fc:00 2731 /system/lib64/libstatslog.so
+70ea060000-70ea07b000 ---p 00000000 00:00 0
+70ea07b000-70ea07c000 r--p 0001f000 fc:00 2731 /system/lib64/libstatslog.so
+70ea07c000-70ea07d000 rw-p 00020000 fc:00 2731 /system/lib64/libstatslog.so
+70ea07d000-70ea081000 r--s 00000000 fc:00 182 /system/fonts/NotoSansBatak-Regular.ttf
+70ea081000-70ea091000 r--s 00000000 fc:00 264 /system/fonts/NotoSerifGujarati-Regular.ttf
+70ea091000-70ea160000 r-xp 00000000 fc:00 2728 /system/lib64/libdng_sdk.so
+70ea160000-70ea173000 ---p 00000000 00:00 0
+70ea173000-70ea17a000 r--p 000d9000 fc:00 2728 /system/lib64/libdng_sdk.so
+70ea17a000-70ea17b000 rw-p 000e0000 fc:00 2728 /system/lib64/libdng_sdk.so
+70ea17b000-70ea198000 r--s 00000000 fc:00 223 /system/fonts/DancingScript-Bold.ttf
+70ea198000-70ea19c000 r-xp 00000000 fc:00 2344 /system/lib64/libnativewindow.so
+70ea19c000-70ea1b7000 ---p 00000000 00:00 0
+70ea1b7000-70ea1b8000 r--p 0000f000 fc:00 2344 /system/lib64/libnativewindow.so
+70ea1b8000-70ea1b9000 rw-p 00010000 fc:00 2344 /system/lib64/libnativewindow.so
+70ea1b9000-70ea1bd000 r--s 00000000 fc:00 98 /system/fonts/NotoSansAhom-Regular.otf
+70ea1bd000-70ea1d1000 r--s 00000000 fc:00 104 /system/fonts/NotoSerifDevanagari-Bold.ttf
+70ea1d1000-70ea1d4000 r-xp 00000000 fc:00 2923 /system/lib64/libstdc++.so
+70ea1d4000-70ea1f0000 ---p 00000000 00:00 0
+70ea1f0000-70ea1f1000 r--p 0000f000 fc:00 2923 /system/lib64/libstdc++.so
+70ea1f1000-70ea1f2000 rw-p 00010000 fc:00 2923 /system/lib64/libstdc++.so
+70ea1f2000-70ea1f4000 r--s 00000000 fc:00 117 /system/fonts/NotoSansLydian-Regular.ttf
+70ea1f4000-70ea211000 r--s 00000000 fc:00 239 /system/fonts/DancingScript-Regular.ttf
+70ea211000-70ea24a000 r-xp 00000000 fc:00 2933 /system/lib64/android.hardware.graphics.bufferqueue@1.0.so
+70ea24a000-70ea268000 ---p 00000000 00:00 0
+70ea268000-70ea26c000 r--p 0003c000 fc:00 2933 /system/lib64/android.hardware.graphics.bufferqueue@1.0.so
+70ea26c000-70ea26d000 rw-p 00040000 fc:00 2933 /system/lib64/android.hardware.graphics.bufferqueue@1.0.so
+70ea26d000-70ea26f000 r--s 00000000 fc:00 244 /system/fonts/NotoSansLycian-Regular.ttf
+70ea26f000-70ea273000 r--s 00000000 fc:00 173 /system/fonts/NotoSansThaana-Bold.ttf
+70ea273000-70ea287000 r--s 00000000 fc:00 106 /system/fonts/NotoSerifDevanagari-Regular.ttf
+70ea287000-70ea29e000 r-xp 00000000 fc:00 2938 /system/lib64/libpiex.so
+70ea29e000-70ea2b6000 ---p 00000000 00:00 0
+70ea2b6000-70ea2b7000 r--p 0001f000 fc:00 2938 /system/lib64/libpiex.so
+70ea2b7000-70ea2b8000 rw-p 00020000 fc:00 2938 /system/lib64/libpiex.so
+70ea2b8000-70ea2c0000 r--s 00000000 fc:00 113 /system/fonts/NotoSansGurmukhi-Regular.ttf
+70ea2c0000-70ea2c6000 r--s 00000000 fc:00 263 /system/fonts/NotoSerifGeorgian-Bold.ttf
+70ea2c6000-70ea2cc000 r--s 00000000 fc:00 249 /system/fonts/NotoSerifGeorgian-Regular.ttf
+70ea2cc000-70ea9b4000 r-xp 00000000 fc:00 2532 /system/lib64/libhwui.so
+70ea9b4000-70ea9d3000 ---p 00000000 00:00 0
+70ea9d3000-70eaa0b000 r--p 006e8000 fc:00 2532 /system/lib64/libhwui.so
+70eaa0b000-70eaa0c000 rw-p 00720000 fc:00 2532 /system/lib64/libhwui.so
+70eaa0c000-70eaa11000 rw-p 00000000 00:00 0 [anon:.bss]
+70eaa11000-70eaa12000 r--s 00000000 fc:00 1110 /system/usr/hyphen-data/hyph-la.hyb
+70eaa12000-70eaa16000 r--s 00000000 fc:00 87 /system/fonts/NotoSansThaana-Regular.ttf
+70eaa16000-70eaa1b000 r--s 00000000 fc:00 218 /system/fonts/NotoSansGeorgian-Bold.ttf
+70eaa1b000-70eaa20000 r--s 00000000 fc:00 125 /system/fonts/NotoSansGeorgian-Regular.ttf
+70eaa20000-70eaa40000 rw-p 00000000 00:05 10271007 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70eaa40000-70eaaa0000 r-xp 00000000 fc:00 2384 /system/lib64/libhidltransport.so
+70eaaa0000-70eaabe000 ---p 00000000 00:00 0
+70eaabe000-70eaac6000 r--p 00068000 fc:00 2384 /system/lib64/libhidltransport.so
+70eaac6000-70eaac7000 rw-p 00070000 fc:00 2384 /system/lib64/libhidltransport.so
+70eaac7000-70eaacb000 r--s 00000000 fc:00 192 /system/fonts/NotoSerifArmenian-Bold.ttf
+70eaacb000-70eaad0000 r--s 00000000 fc:00 210 /system/fonts/NotoSansThaiUI-Bold.ttf
+70eaad0000-70eaaf0000 rw-p 00000000 00:05 10271006 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70eaaf0000-70eab10000 rw-p 00000000 00:05 10271005 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70eab10000-70eab57000 r-xp 00000000 fc:00 2546 /system/lib64/libmedia_omx.so
+70eab57000-70eab6d000 ---p 00000000 00:00 0
+70eab6d000-70eab7a000 r--p 00053000 fc:00 2546 /system/lib64/libmedia_omx.so
+70eab7a000-70eab7f000 rw-p 00060000 fc:00 2546 /system/lib64/libmedia_omx.so
+70eab7f000-70eab80000 r--s 00000000 fc:00 1119 /system/usr/hyphen-data/hyph-kn.hyb
+70eab80000-70eab86000 r--s 00000000 fc:00 224 /system/fonts/NotoSansThaiUI-Regular.ttf
+70eab86000-70eab8b000 r--s 00000000 fc:00 300 /system/fonts/NotoSerifThai-Bold.ttf
+70eab8b000-70eabab000 rw-p 00000000 00:05 10271004 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70eabab000-70eac21000 r-xp 00000000 fc:00 2385 /system/lib64/libvintf.so
+70eac21000-70eac31000 ---p 00000000 00:00 0
+70eac31000-70eac36000 r--p 0007b000 fc:00 2385 /system/lib64/libvintf.so
+70eac36000-70eac37000 rw-p 00080000 fc:00 2385 /system/lib64/libvintf.so
+70eac37000-70eac39000 rw-p 00000000 00:00 0 [anon:.bss]
+70eac39000-70eac3a000 r--s 00000000 fc:00 1104 /system/usr/hyphen-data/hyph-hy.hyb
+70eac3a000-70eac3f000 r--s 00000000 fc:00 212 /system/fonts/NotoSerifThai-Regular.ttf
+70eac3f000-70eac44000 r--s 00000000 fc:00 220 /system/fonts/NotoSansThai-Bold.ttf
+70eac44000-70eacb2000 r-xp 00000000 fc:00 2606 /system/lib64/android.hardware.media.omx@1.0.so
+70eacb2000-70eaccf000 ---p 00000000 00:00 0
+70eaccf000-70eacd8000 r--p 00077000 fc:00 2606 /system/lib64/android.hardware.media.omx@1.0.so
+70eacd8000-70eacd9000 rw-p 00080000 fc:00 2606 /system/lib64/android.hardware.media.omx@1.0.so
+70eacd9000-70eacdf000 r--s 00000000 fc:00 169 /system/fonts/NotoSansThai-Regular.ttf
+70eacdf000-70eace9000 r--s 00000000 fc:00 140 /system/fonts/CarroisGothicSC-Regular.ttf
+70eace9000-70ead09000 rw-p 00000000 00:05 10271003 /dev/ashmem/dalvik-CompilerMetadata (deleted)
+70ead09000-70ead22000 r-xp 00000000 fc:00 2539 /system/lib64/android.hardware.graphics.mapper@2.1.so
+70ead22000-70ead34000 ---p 00000000 00:00 0
+70ead34000-70ead37000 r--p 0001d000 fc:00 2539 /system/lib64/android.hardware.graphics.mapper@2.1.so
+70ead37000-70ead38000 rw-p 00020000 fc:00 2539 /system/lib64/android.hardware.graphics.mapper@2.1.so
+70ead38000-70ead47000 r--s 00000000 fc:00 188 /system/fonts/ComingSoon.ttf
+70ead47000-70ead5d000 r-xp 00000000 fc:00 2379 /system/lib64/libselinux.so
+70ead5d000-70ead76000 ---p 00000000 00:00 0
+70ead76000-70ead77000 r--p 0001f000 fc:00 2379 /system/lib64/libselinux.so
+70ead77000-70ead78000 rw-p 00020000 fc:00 2379 /system/lib64/libselinux.so
+70ead78000-70ead79000 rw-p 00000000 00:00 0 [anon:.bss]
+70ead79000-70ead7d000 r--s 00000000 fc:00 282 /system/fonts/NotoSerifArmenian-Regular.ttf
+70ead7d000-70ead82000 r--s 00000000 fc:00 288 /system/fonts/NotoSerifHebrew-Bold.ttf
+70ead82000-70ead83000 r-xp 00000000 fc:00 2680 /system/lib64/android.hardware.media@1.0.so
+70ead83000-70eada1000 ---p 00000000 00:00 0
+70eada1000-70eada2000 r--p 0000f000 fc:00 2680 /system/lib64/android.hardware.media@1.0.so
+70eada2000-70eada3000 rw-p 00010000 fc:00 2680 /system/lib64/android.hardware.media@1.0.so
+70eada3000-70eada8000 r--s 00000000 fc:00 248 /system/fonts/NotoSerifHebrew-Regular.ttf
+70eada8000-70eadb9000 r--s 00000000 fc:00 252 /system/fonts/CutiveMono.ttf
+70eadb9000-70eadd9000 r--s 00000000 00:10 16641 /dev/__properties__/u:object_r:radio_prop:s0
+70eadd9000-70eadda000 r-xp 00000000 fc:00 2533 /system/lib64/android.hardware.graphics.common@1.0.so
+70eadda000-70eadf8000 ---p 00000000 00:00 0
+70eadf8000-70eadf9000 r--p 0000f000 fc:00 2533 /system/lib64/android.hardware.graphics.common@1.0.so
+70eadf9000-70eadfa000 rw-p 00010000 fc:00 2533 /system/lib64/android.hardware.graphics.common@1.0.so
+70eadfa000-70eadfb000 r--s 00000000 fc:00 1126 /system/usr/hyphen-data/hyph-hr.hyb
+70eadfb000-70eadfd000 r--s 00000000 fc:00 194 /system/fonts/NotoSansLisu-Regular.ttf
+70eadfd000-70eae18000 r--s 00000000 fc:00 201 /system/fonts/DroidSansMono.ttf
+70eae18000-70eae3b000 r-xp 00000000 fc:00 2925 /system/lib64/liblzma.so
+70eae3b000-70eae57000 ---p 00000000 00:00 0
+70eae57000-70eae58000 r--p 0002f000 fc:00 2925 /system/lib64/liblzma.so
+70eae58000-70eae59000 rw-p 00030000 fc:00 2925 /system/lib64/liblzma.so
+70eae59000-70eae5f000 rw-p 00000000 00:00 0 [anon:.bss]
+70eae5f000-70eae62000 r--s 00000000 fc:00 103 /system/fonts/NotoSansLimbu-Regular.ttf
+70eae62000-70eae67000 r--s 00000000 fc:00 236 /system/fonts/NotoSansHebrew-Bold.ttf
+70eae67000-70eae84000 r--s 001c2000 fc:00 990 /system/framework/ext.jar
+70eae84000-70eaea4000 rw-p 00000000 00:05 10269720 /dev/ashmem/dalvik-LinearAlloc (deleted)
+70eaea4000-70eaede000 r-xp 00000000 fc:00 2924 /system/lib64/libwilhelm.so
+70eaede000-70eaefa000 ---p 00000000 00:00 0
+70eaefa000-70eaeff000 r--p 0003b000 fc:00 2924 /system/lib64/libwilhelm.so
+70eaeff000-70eaf00000 rw-p 00040000 fc:00 2924 /system/lib64/libwilhelm.so
+70eaf00000-70eaf03000 r--s 00000000 fc:00 242 /system/fonts/NotoSansElbasan-Regular.otf
+70eaf03000-70eaf21000 r-xp 00000000 fc:00 2415 /system/lib64/libdrmframework.so
+70eaf21000-70eaf38000 ---p 00000000 00:00 0
+70eaf38000-70eaf3d000 r--p 0002b000 fc:00 2415 /system/lib64/libdrmframework.so
+70eaf3d000-70eaf3e000 rw-p 00030000 fc:00 2415 /system/lib64/libdrmframework.so
+70eaf3e000-70eaf43000 r--s 00000000 fc:00 70 /system/fonts/NotoSansHebrew-Regular.ttf
+70eaf43000-70eaf44000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70eaf44000-70eaf48000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70eaf48000-70eaf49000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eaf49000-70eaf4c000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70eaf4c000-70eaf4d000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eaf4d000-70eaf4e000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70eaf4e000-70eaf52000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70eaf52000-70eaf98000 r-xp 00000000 fc:00 2426 /system/lib64/libunwindstack.so
+70eaf98000-70eafb6000 ---p 00000000 00:00 0
+70eafb6000-70eafbd000 r--p 00049000 fc:00 2426 /system/lib64/libunwindstack.so
+70eafbd000-70eafbe000 rw-p 00050000 fc:00 2426 /system/lib64/libunwindstack.so
+70eafbe000-70eafc0000 r--s 00000000 fc:00 162 /system/fonts/NotoSansKayahLi-Regular.ttf
+70eafc0000-70eafe4000 r-xp 00000000 fc:00 2944 /system/lib64/libvulkan.so
+70eafe4000-70eaffc000 ---p 00000000 00:00 0
+70eaffc000-70eaffe000 r--p 0002e000 fc:00 2944 /system/lib64/libvulkan.so
+70eaffe000-70eafff000 rw-p 00030000 fc:00 2944 /system/lib64/libvulkan.so
+70eafff000-70eb001000 r--s 00000000 fc:00 180 /system/fonts/NotoSansInscriptionalParthian-Regular.ttf
+70eb001000-70eb01d000 r-xp 00000000 fc:00 2400 /system/lib64/libbufferhubqueue.so
+70eb01d000-70eb030000 ---p 00000000 00:00 0
+70eb030000-70eb031000 r--p 0001f000 fc:00 2400 /system/lib64/libbufferhubqueue.so
+70eb031000-70eb032000 rw-p 00020000 fc:00 2400 /system/lib64/libbufferhubqueue.so
+70eb032000-70eb036000 r--s 00000000 fc:00 269 /system/fonts/NotoSansArmenian-Bold.ttf
+70eb036000-70eb037000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb037000-70eb03a000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70eb03a000-70eb03b000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb03b000-70eb03c000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70eb03c000-70eb040000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70eb040000-70eb042000 r-xp 00000000 fc:00 2935 /system/lib64/libhardware.so
+70eb042000-70eb05f000 ---p 00000000 00:00 0
+70eb05f000-70eb060000 r--p 0000f000 fc:00 2935 /system/lib64/libhardware.so
+70eb060000-70eb061000 rw-p 00010000 fc:00 2935 /system/lib64/libhardware.so
+70eb061000-70eb063000 r--s 00000000 fc:00 171 /system/fonts/NotoSansInscriptionalPahlavi-Regular.ttf
+70eb063000-70eb064000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb064000-70eb067000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70eb067000-70eb068000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb068000-70eb069000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70eb069000-70eb06d000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70eb06d000-70eb06e000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb06e000-70eb071000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70eb071000-70eb072000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb072000-70eb073000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70eb073000-70eb077000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70eb077000-70eb078000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb078000-70eb07b000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70eb07b000-70eb07c000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb07c000-70eb07d000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70eb07d000-70eb081000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70eb081000-70eb082000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb082000-70eb085000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70eb085000-70eb086000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb086000-70eb09d000 r-xp 00000000 fc:00 2604 /system/lib64/libz.so
+70eb09d000-70eb0b5000 ---p 00000000 00:00 0
+70eb0b5000-70eb0b6000 r--p 0001f000 fc:00 2604 /system/lib64/libz.so
+70eb0b6000-70eb0b7000 rw-p 00020000 fc:00 2604 /system/lib64/libz.so
+70eb0b7000-70eb0bb000 r--s 00000000 fc:00 289 /system/fonts/NotoSansArmenian-Regular.ttf
+70eb0bb000-70eb0bc000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70eb0bc000-70eb0c0000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70eb0c0000-70eb0c1000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb0c1000-70eb0c4000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70eb0c4000-70eb0c5000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb0c5000-70eb0c6000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70eb0c6000-70eb0ca000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70eb0ca000-70eb0cb000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb0cb000-70eb0ce000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70eb0ce000-70eb0cf000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70eb0cf000-70eb0ef000 rw-p 00000000 00:05 10270988 /dev/ashmem/dalvik-LinearAlloc (deleted)
+70eb0ef000-70eb5bb000 r-xp 00000000 fc:00 2374 /system/lib64/libpdfium.so
+70eb5bb000-70eb5cf000 ---p 00000000 00:00 0
+70eb5cf000-70eb5e6000 r--p 004d9000 fc:00 2374 /system/lib64/libpdfium.so
+70eb5e6000-70eb5ea000 rw-p 004f0000 fc:00 2374 /system/lib64/libpdfium.so
+70eb5ea000-70eb5f1000 rw-p 00000000 00:00 0 [anon:.bss]
+70eb5f1000-70eb5f2000 r--s 00000000 fc:00 1094 /system/usr/hyphen-data/hyph-hi.hyb
+70eb5f2000-70eb5f6000 rw-p 00000000 00:05 10270982 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb5f6000-70eb5fa000 rw-p 00000000 00:05 10270981 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb5fa000-70eb5fe000 rw-p 00000000 00:05 10270980 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb5fe000-70eb602000 rw-p 00000000 00:05 10270979 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb602000-70eb606000 rw-p 00000000 00:05 10270978 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb606000-70eb60a000 rw-p 00000000 00:05 10270977 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb60a000-70eb60e000 rw-p 00000000 00:05 10270976 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb60e000-70eb612000 rw-p 00000000 00:05 10270975 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb612000-70eb616000 rw-p 00000000 00:05 10270974 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb616000-70eb61a000 r-xp 00000000 fc:00 2479 /system/lib64/libspeexresampler.so
+70eb61a000-70eb635000 ---p 00000000 00:00 0
+70eb635000-70eb636000 r--p 0000f000 fc:00 2479 /system/lib64/libspeexresampler.so
+70eb636000-70eb637000 rw-p 00010000 fc:00 2479 /system/lib64/libspeexresampler.so
+70eb637000-70eb639000 r--s 00000000 fc:00 299 /system/fonts/NotoSansImperialAramaic-Regular.ttf
+70eb639000-70eb63d000 rw-p 00000000 00:05 10270973 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb63d000-70eb641000 rw-p 00000000 00:05 10270972 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb641000-70eb645000 rw-p 00000000 00:05 10270971 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb645000-70eb649000 rw-p 00000000 00:05 10270970 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb649000-70eb64d000 rw-p 00000000 00:05 10270969 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb64d000-70eb651000 rw-p 00000000 00:05 10270968 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb651000-70eb655000 rw-p 00000000 00:05 10270967 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb655000-70eb659000 rw-p 00000000 00:05 10270966 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb659000-70eb65d000 rw-p 00000000 00:05 10270965 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb65d000-70eb661000 rw-p 00000000 00:05 10270964 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb661000-70eb6c5000 r-xp 00000000 fc:00 2461 /system/lib64/libhidl-gen-utils.so
+70eb6c5000-70eb6df000 ---p 00000000 00:00 0
+70eb6df000-70eb6e1000 r--p 0006e000 fc:00 2461 /system/lib64/libhidl-gen-utils.so
+70eb6e1000-70eb6e2000 rw-p 00070000 fc:00 2461 /system/lib64/libhidl-gen-utils.so
+70eb6e2000-70eb6e6000 rw-p 00000000 00:05 10270963 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb6e6000-70eb6ea000 rw-p 00000000 00:05 10270962 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb6ea000-70eb6ee000 rw-p 00000000 00:05 10270961 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb6ee000-70eb6f2000 rw-p 00000000 00:05 10270960 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb6f2000-70eb6f6000 rw-p 00000000 00:05 10270959 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb6f6000-70eb6fa000 rw-p 00000000 00:05 10270958 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb6fa000-70eb6fe000 rw-p 00000000 00:05 10270957 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb6fe000-70eb702000 rw-p 00000000 00:05 10270956 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb702000-70eb706000 rw-p 00000000 00:05 10270955 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb706000-70eb70a000 rw-p 00000000 00:05 10270954 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb70a000-70eb70e000 rw-p 00000000 00:05 10270953 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb70e000-70eb712000 rw-p 00000000 00:05 10270952 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb712000-70eb71a000 r-xp 00000000 fc:00 2652 /system/lib64/libcamera_metadata.so
+70eb71a000-70eb72f000 ---p 00000000 00:00 0
+70eb72f000-70eb730000 r--p 0000f000 fc:00 2652 /system/lib64/libcamera_metadata.so
+70eb730000-70eb732000 rw-p 00010000 fc:00 2652 /system/lib64/libcamera_metadata.so
+70eb732000-70eb734000 r--s 00000000 fc:00 131 /system/fonts/NotoSansHanunoo-Regular.ttf
+70eb734000-70eb738000 rw-p 00000000 00:05 10270951 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb738000-70eb73c000 rw-p 00000000 00:05 10270950 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb73c000-70eb740000 rw-p 00000000 00:05 10270949 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb740000-70eb744000 rw-p 00000000 00:05 10270948 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb744000-70eb748000 rw-p 00000000 00:05 10270947 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb748000-70eb74c000 rw-p 00000000 00:05 10270946 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb74c000-70eb750000 rw-p 00000000 00:05 10270945 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb750000-70eb754000 rw-p 00000000 00:05 10270944 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb754000-70eb758000 rw-p 00000000 00:05 10270943 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb758000-70eb75c000 rw-p 00000000 00:05 10270942 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb75c000-70eb760000 rw-p 00000000 00:05 10270941 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb760000-70eb764000 rw-p 00000000 00:05 10270940 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb764000-70eb767000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb767000-70eb768000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb768000-70eb76c000 rw-p 00000000 00:05 10270939 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb76c000-70eb770000 rw-p 00000000 00:05 10270938 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb770000-70eb771000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb771000-70eb774000 r--s 00000000 fc:00 231 /system/fonts/NotoSansDeseret-Regular.ttf
+70eb774000-70eb778000 rw-p 00000000 00:05 10270937 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb778000-70eb77c000 rw-p 00000000 00:05 10270936 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb77c000-70eb780000 rw-p 00000000 00:05 10270935 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb780000-70eb784000 rw-p 00000000 00:05 10270934 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb784000-70eb788000 rw-p 00000000 00:05 10270933 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb788000-70eb78c000 rw-p 00000000 00:05 10270932 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb78c000-70eb790000 rw-p 00000000 00:05 10270931 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb790000-70eb794000 rw-p 00000000 00:05 10270930 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb794000-70eb798000 rw-p 00000000 00:05 10270929 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb798000-70eb79c000 rw-p 00000000 00:05 10270928 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb79c000-70eb7a0000 rw-p 00000000 00:05 10270927 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7a0000-70eb7a1000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb7a1000-70eb7a3000 r--s 00000000 fc:00 176 /system/fonts/NotoSansGothic-Regular.ttf
+70eb7a3000-70eb7a7000 rw-p 00000000 00:05 10270926 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7a7000-70eb7a8000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb7a8000-70eb7a9000 r--s 00000000 fc:00 1109 /system/usr/hyphen-data/hyph-gu.hyb
+70eb7a9000-70eb7ad000 rw-p 00000000 00:05 10270925 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7ad000-70eb7ae000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb7ae000-70eb7af000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb7af000-70eb7b0000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb7b0000-70eb7b2000 r--s 00000000 fc:00 191 /system/fonts/NotoSansCypriot-Regular.ttf
+70eb7b2000-70eb7b6000 rw-p 00000000 00:05 10270924 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7b6000-70eb7ba000 rw-p 00000000 00:05 10270923 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7ba000-70eb7be000 rw-p 00000000 00:05 10270922 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7be000-70eb7c2000 rw-p 00000000 00:05 10270921 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7c2000-70eb7c6000 rw-p 00000000 00:05 10270920 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7c6000-70eb7ca000 rw-p 00000000 00:05 10270919 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7ca000-70eb7ce000 rw-p 00000000 00:05 10270918 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7ce000-70eb7d2000 rw-p 00000000 00:05 10270917 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7d2000-70eb7d6000 rw-p 00000000 00:05 10270916 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7d6000-70eb7d7000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70eb7d7000-70eb7db000 rw-p 00000000 00:05 10270915 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7db000-70eb7df000 rw-p 00000000 00:05 10270914 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7df000-70eb7e3000 rw-p 00000000 00:05 10270913 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7e3000-70eb7e7000 rw-p 00000000 00:05 10270912 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7e7000-70eb7e8000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb7e8000-70eb7ea000 r--s 00000000 fc:00 174 /system/fonts/NotoSansCarian-Regular.ttf
+70eb7ea000-70eb7ee000 rw-p 00000000 00:05 10270911 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7ee000-70eb7f2000 rw-p 00000000 00:05 10270910 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7f2000-70eb7f6000 rw-p 00000000 00:05 10270909 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7f6000-70eb7f7000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb7f7000-70eb7f8000 r--s 00000000 fc:00 1096 /system/usr/hyphen-data/hyph-eu.hyb
+70eb7f8000-70eb7fc000 rw-p 00000000 00:05 10270908 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb7fc000-70eb800000 rw-p 00000000 00:05 10270907 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb800000-70eb804000 rw-p 00000000 00:05 10270906 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb804000-70eb808000 rw-p 00000000 00:05 10270905 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb808000-70eb80c000 rw-p 00000000 00:05 10270904 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb80c000-70eb810000 rw-p 00000000 00:05 10270903 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb810000-70eb814000 rw-p 00000000 00:05 10270902 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb814000-70eb815000 rw-p 00000000 00:00 0 [anon:linker_alloc_vector]
+70eb815000-70eb819000 rw-p 00000000 00:05 10270901 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb819000-70eb81d000 rw-p 00000000 00:05 10270900 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb81d000-70eb81e000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb81e000-70eb822000 rw-p 00000000 00:05 10270899 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb822000-70eb826000 rw-p 00000000 00:05 10270898 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb826000-70eb82a000 rw-p 00000000 00:05 10270897 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb82a000-70eb82e000 rw-p 00000000 00:05 10270896 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb82e000-70eb832000 rw-p 00000000 00:05 10270895 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb832000-70eb836000 rw-p 00000000 00:05 10270894 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb836000-70eb837000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb837000-70eb83b000 rw-p 00000000 00:05 10270893 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb83b000-70eb83f000 rw-p 00000000 00:05 10270892 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb83f000-70eb843000 rw-p 00000000 00:05 10270891 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb843000-70eb847000 rw-p 00000000 00:05 10270890 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb847000-70eb84b000 rw-p 00000000 00:05 10270889 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb84b000-70eb84f000 rw-p 00000000 00:05 10270888 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb84f000-70eb850000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb850000-70eb854000 rw-p 00000000 00:05 10270887 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb854000-70eb858000 rw-p 00000000 00:05 10270886 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb858000-70eb85c000 rw-p 00000000 00:05 10270885 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb85c000-70eb860000 rw-p 00000000 00:05 10270884 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb860000-70eb864000 rw-p 00000000 00:05 10270883 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb864000-70eb868000 rw-p 00000000 00:05 10270882 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb868000-70eb869000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb869000-70eb86d000 rw-p 00000000 00:05 10270881 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb86d000-70eb871000 rw-p 00000000 00:05 10270880 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb871000-70eb875000 rw-p 00000000 00:05 10270879 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb875000-70eb879000 rw-p 00000000 00:05 10270878 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb879000-70eb87d000 rw-p 00000000 00:05 10270877 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb87d000-70eb881000 rw-p 00000000 00:05 10270876 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb881000-70eb885000 rw-p 00000000 00:05 10270875 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb885000-70eb889000 rw-p 00000000 00:05 10270874 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb889000-70eb88d000 rw-p 00000000 00:05 10270873 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb88d000-70eb891000 rw-p 00000000 00:05 10270872 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb891000-70eb892000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb892000-70eb896000 rw-p 00000000 00:05 10270871 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb896000-70eb89a000 rw-p 00000000 00:05 10270870 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb89a000-70eb89b000 rw-p 00000000 00:00 0 [anon:linker_alloc_vector]
+70eb89b000-70eb89c000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70eb89c000-70eb8a0000 rw-p 00000000 00:05 10270869 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8a0000-70eb8a4000 rw-p 00000000 00:05 10270868 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8a4000-70eb8a5000 r--p 00000000 00:00 0 [anon:atexit handlers]
+70eb8a5000-70eb8a9000 rw-p 00000000 00:05 10270867 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8a9000-70eb8ad000 rw-p 00000000 00:05 10270866 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8ad000-70eb8b1000 rw-p 00000000 00:05 10270865 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8b1000-70eb8b5000 rw-p 00000000 00:05 10270864 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8b5000-70eb8b9000 rw-p 00000000 00:05 10270863 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8b9000-70eb8bd000 rw-p 00000000 00:05 10270862 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8bd000-70eb8be000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb8be000-70eb8c1000 r--s 00000000 fc:00 168 /system/fonts/NotoSansAvestan-Regular.ttf
+70eb8c1000-70eb8c5000 rw-p 00000000 00:05 10270861 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8c5000-70eb8c9000 rw-p 00000000 00:05 10270860 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8c9000-70eb8cd000 rw-p 00000000 00:05 10270859 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8cd000-70eb8d1000 rw-p 00000000 00:05 10270858 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8d1000-70eb8d5000 rw-p 00000000 00:05 10270857 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8d5000-70eb8d7000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb8d7000-70eb8db000 rw-p 00000000 00:05 10270856 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8db000-70eb8df000 rw-p 00000000 00:05 10270855 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8df000-70eb8e3000 rw-p 00000000 00:05 10270854 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8e3000-70eb8e7000 rw-p 00000000 00:05 10270853 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8e7000-70eb8eb000 rw-p 00000000 00:05 10270852 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8eb000-70eb8ec000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb8ec000-70eb8ed000 r--s 00000000 fc:00 1099 /system/usr/hyphen-data/hyph-bn.hyb
+70eb8ed000-70eb8f1000 rw-p 00000000 00:05 10270851 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8f1000-70eb8f5000 rw-p 00000000 00:05 10270850 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8f5000-70eb8f9000 rw-p 00000000 00:05 10270849 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8f9000-70eb8fd000 rw-p 00000000 00:05 10270848 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb8fd000-70eb901000 rw-p 00000000 00:05 10270847 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb901000-70eb905000 rw-p 00000000 00:05 10270846 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb905000-70eb909000 rw-p 00000000 00:05 10270845 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb909000-70eb90d000 rw-p 00000000 00:05 10270844 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb90d000-70eb911000 rw-p 00000000 00:05 10270843 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb911000-70eb915000 rw-p 00000000 00:05 10270842 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb915000-70eb916000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb916000-70eb917000 r--s 00000000 fc:00 1114 /system/usr/hyphen-data/hyph-bg.hyb
+70eb917000-70eb91b000 rw-p 00000000 00:05 10270841 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb91b000-70eb91c000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb91c000-70eb91d000 r--s 00000000 fc:00 1133 /system/usr/hyphen-data/hyph-as.hyb
+70eb91d000-70eb921000 rw-p 00000000 00:05 10270840 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb921000-70eb925000 rw-p 00000000 00:05 10270839 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb925000-70eb926000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70eb926000-70eb927000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb927000-70eb929000 r--s 00000000 fc:00 203 /system/fonts/NotoSansBuhid-Regular.ttf
+70eb929000-70eb92d000 rw-p 00000000 00:05 10270838 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb92d000-70eb931000 rw-p 00000000 00:05 10270837 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb931000-70eb935000 rw-p 00000000 00:05 10270836 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb935000-70eb939000 rw-p 00000000 00:05 10270835 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb939000-70eb93d000 rw-p 00000000 00:05 10270834 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb93d000-70eb941000 rw-p 00000000 00:05 10270833 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb941000-70eb945000 rw-p 00000000 00:05 10270832 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb945000-70eb949000 rw-p 00000000 00:05 10270831 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb949000-70eb94d000 rw-p 00000000 00:05 10270830 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb94d000-70eb951000 rw-p 00000000 00:05 10270829 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb951000-70eb991000 rw-p 00000000 00:05 10270722 /dev/ashmem/dalvik-mark stack (deleted)
+70eb991000-70eb992000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb992000-70eb996000 rw-p 00000000 00:05 10270828 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb996000-70eb99a000 rw-p 00000000 00:05 10270827 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb99a000-70eb99e000 rw-p 00000000 00:05 10270826 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb99e000-70eb9a2000 rw-p 00000000 00:05 10270825 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9a2000-70eb9a4000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb9a4000-70eb9a8000 rw-p 00000000 00:05 10270824 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9a8000-70eb9ac000 rw-p 00000000 00:05 10270823 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9ac000-70eb9b0000 rw-p 00000000 00:05 10270822 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9b0000-70eb9b1000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb9b1000-70eb9b2000 r--s 00021000 fc:01 1180 /vendor/overlay/framework-res__auto_generated_rro.apk
+70eb9b2000-70eb9b6000 rw-p 00000000 00:05 10270821 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9b6000-70eb9ba000 rw-p 00000000 00:05 10270820 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9ba000-70eb9be000 rw-p 00000000 00:05 10270819 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9be000-70eb9c2000 rw-p 00000000 00:05 10270818 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9c2000-70eb9c6000 rw-p 00000000 00:05 10270817 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9c6000-70eb9ca000 rw-p 00000000 00:05 10270816 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9ca000-70eb9ce000 rw-p 00000000 00:05 10270815 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9ce000-70eb9cf000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb9cf000-70eb9d1000 r--s 00000000 fc:00 213 /system/fonts/NotoSansBuginese-Regular.ttf
+70eb9d1000-70eb9d5000 rw-p 00000000 00:05 10270814 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9d5000-70eb9d9000 rw-p 00000000 00:05 10270813 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9d9000-70eb9dd000 rw-p 00000000 00:05 10270812 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9dd000-70eb9e1000 rw-p 00000000 00:05 10270811 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9e1000-70eb9e5000 rw-p 00000000 00:05 10270810 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9e5000-70eb9e9000 rw-p 00000000 00:05 10270809 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9e9000-70eb9ed000 rw-p 00000000 00:05 10270808 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9ed000-70eb9f1000 rw-p 00000000 00:05 10270807 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9f1000-70eb9f5000 rw-p 00000000 00:05 10270806 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9f5000-70eb9f6000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eb9f6000-70eb9f8000 rw-p 00000000 00:05 10271002 /dev/ashmem/dalvik-indirect ref table (deleted)
+70eb9f8000-70eb9fc000 rw-p 00000000 00:05 10270805 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eb9fc000-70eb9fd000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eb9fd000-70eb9ff000 rw-p 00000000 00:05 10270999 /dev/ashmem/dalvik-indirect ref table (deleted)
+70eb9ff000-70eba00000 r--s 00000000 fc:00 983 /system/framework/com.google.vr.platform.jar
+70eba00000-70eba04000 rw-p 00000000 00:05 10270804 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba04000-70eba08000 rw-p 00000000 00:05 10270803 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba08000-70eba0c000 rw-p 00000000 00:05 10270802 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba0c000-70eba10000 rw-p 00000000 00:05 10270801 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba10000-70eba14000 rw-p 00000000 00:05 10270800 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba14000-70eba18000 rw-p 00000000 00:05 10270799 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba18000-70eba1c000 rw-p 00000000 00:05 10270798 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba1c000-70eba20000 rw-p 00000000 00:05 10270797 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba20000-70eba24000 rw-p 00000000 00:05 10270796 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba24000-70eba28000 rw-p 00000000 00:05 10270795 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba28000-70eba2c000 rw-p 00000000 00:05 10270794 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba2c000-70eba2d000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eba2d000-70eba2e000 r--s 00000000 fc:00 881 /system/framework/android.test.base.jar
+70eba2e000-70eba2f000 r--s 00000000 fc:00 707 /system/framework/framework-oahl-backward-compatibility.jar
+70eba2f000-70eba30000 r--s 00000000 fc:00 705 /system/framework/android.hidl.manager-V1.0-java.jar
+70eba30000-70eba34000 rw-p 00000000 00:05 10270793 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba34000-70eba38000 rw-p 00000000 00:05 10270792 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba38000-70eba3c000 rw-p 00000000 00:05 10270791 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba3c000-70eba40000 rw-p 00000000 00:05 10270790 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba40000-70eba44000 rw-p 00000000 00:05 10270789 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba44000-70eba48000 rw-p 00000000 00:05 10270788 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba48000-70eba4c000 rw-p 00000000 00:05 10270787 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba4c000-70eba50000 rw-p 00000000 00:05 10270786 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba50000-70eba52000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70eba52000-70eba53000 r--s 00000000 fc:00 971 /system/framework/android.hidl.base-V1.0-java.jar
+70eba53000-70eba57000 rw-p 00000000 00:05 10270785 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba57000-70eba5b000 rw-p 00000000 00:05 10270784 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba5b000-70eba5f000 rw-p 00000000 00:05 10270783 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba5f000-70eba63000 rw-p 00000000 00:05 10270782 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba63000-70eba64000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70eba64000-70eba65000 r--s 00000000 fc:00 889 /system/framework/ims-common.jar
+70eba65000-70eba69000 rw-p 00000000 00:05 10270781 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba69000-70eba6d000 rw-p 00000000 00:05 10270780 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba6d000-70eba71000 rw-p 00000000 00:05 10270779 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba71000-70eba75000 rw-p 00000000 00:05 10270778 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70eba75000-70eba95000 rw-p 00000000 00:05 10267647 /dev/ashmem/dalvik-large marked objects (deleted)
+70eba95000-70ebab5000 rw-p 00000000 00:05 10267646 /dev/ashmem/dalvik-large live objects (deleted)
+70ebab5000-70ebab6000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebab6000-70ebab7000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebab7000-70ebabb000 rw-p 00000000 00:05 10270777 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebabb000-70ebadb000 r--s 00000000 00:10 16603 /dev/__properties__/u:object_r:exported_fingerprint_prop:s0
+70ebadb000-70ebadc000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebadc000-70ebadd000 r--s 00000000 fc:00 878 /system/framework/voip-common.jar
+70ebadd000-70ebadf000 rw-p 00000000 00:05 10270995 /dev/ashmem/dalvik-indirect ref table (deleted)
+70ebadf000-70ebae3000 rw-p 00000000 00:05 10270776 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebae3000-70ebae7000 rw-p 00000000 00:05 10270775 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebae7000-70ebaeb000 rw-p 00000000 00:05 10270774 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebaeb000-70ebaef000 rw-p 00000000 00:05 10270773 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebaef000-70ebb0f000 r--s 00000000 00:10 16582 /dev/__properties__/u:object_r:debug_prop:s0
+70ebb0f000-70ebb10000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebb10000-70ebb11000 r--s 00000000 fc:00 703 /system/framework/telephony-common.jar
+70ebb11000-70ebb13000 rw-p 00000000 00:05 10270994 /dev/ashmem/dalvik-indirect ref table (deleted)
+70ebb13000-70ebb17000 rw-p 00000000 00:05 10270772 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebb17000-70ebb19000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebb19000-70ebb1d000 rw-p 00000000 00:05 10270771 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebb1d000-70ebb3d000 r--s 00000000 00:10 16600 /dev/__properties__/u:object_r:exported_default_prop:s0
+70ebb3d000-70ebb5d000 r--s 00000000 00:10 16650 /dev/__properties__/u:object_r:system_prop:s0
+70ebb5d000-70ebb7d000 r--s 00000000 00:10 16610 /dev/__properties__/u:object_r:exported_vold_prop:s0
+70ebb7d000-70ebb9d000 r--s 00000000 00:10 16598 /dev/__properties__/u:object_r:exported_config_prop:s0
+70ebb9d000-70ebb9e000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebb9e000-70ebba2000 rw-p 00000000 00:05 10270770 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebba2000-70ebba6000 rw-p 00000000 00:05 10270769 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebba6000-70ebba7000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebba7000-70ebba8000 r--s 00000000 fc:00 1004 /system/framework/framework.jar
+70ebba8000-70ebbac000 rw-p 00000000 00:05 10270768 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbac000-70ebbb0000 rw-p 00000000 00:05 10270767 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbb0000-70ebbb4000 rw-p 00000000 00:05 10270766 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbb4000-70ebbb8000 rw-p 00000000 00:05 10270765 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbb8000-70ebbbc000 rw-p 00000000 00:05 10270764 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbbc000-70ebbc0000 rw-p 00000000 00:05 10270763 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbc0000-70ebbc4000 rw-p 00000000 00:05 10270762 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbc4000-70ebbe4000 r--s 00000000 00:10 16581 /dev/__properties__/u:object_r:dalvik_prop:s0
+70ebbe4000-70ebbe5000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebbe5000-70ebbe6000 r--s 00004000 fc:00 877 /system/framework/apache-xml.jar
+70ebbe6000-70ebbe8000 rw-p 00000000 00:05 10270993 /dev/ashmem/dalvik-indirect ref table (deleted)
+70ebbe8000-70ebbec000 rw-p 00000000 00:05 10270761 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbec000-70ebbf0000 rw-p 00000000 00:05 10270760 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbf0000-70ebbf4000 rw-p 00000000 00:05 10270759 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbf4000-70ebbf8000 rw-p 00000000 00:05 10270758 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebbf8000-70ebbf9000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebbf9000-70ebbfa000 r--s 00000000 fc:00 968 /system/framework/bouncycastle.jar
+70ebbfa000-70ebbfc000 rw-p 00000000 00:05 10270992 /dev/ashmem/dalvik-indirect ref table (deleted)
+70ebbfc000-70ebc00000 rw-p 00000000 00:05 10270757 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc00000-70ebc04000 rw-p 00000000 00:05 10270756 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc04000-70ebc08000 rw-p 00000000 00:05 10270755 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc08000-70ebc0c000 rw-p 00000000 00:05 10270754 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc0c000-70ebc10000 rw-p 00000000 00:05 10270753 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc10000-70ebc14000 rw-p 00000000 00:05 10270752 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc14000-70ebc15000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebc15000-70ebc16000 r--s 00000000 fc:00 960 /system/framework/okhttp.jar
+70ebc16000-70ebc1a000 rw-p 00000000 00:05 10270751 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc1a000-70ebc1e000 rw-p 00000000 00:05 10270750 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc1e000-70ebc3e000 r--s 00000000 00:10 16584 /dev/__properties__/u:object_r:default_prop:s0
+70ebc3e000-70ebc3f000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebc3f000-70ebc40000 r--s 00000000 fc:00 974 /system/framework/conscrypt.jar
+70ebc40000-70ebc42000 rw-p 00000000 00:05 10269719 /dev/ashmem/dalvik-indirect ref table (deleted)
+70ebc42000-70ebc46000 rw-p 00000000 00:05 10270749 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc46000-70ebc4a000 rw-p 00000000 00:05 10270748 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc4a000-70ebc4e000 rw-p 00000000 00:05 10270747 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc4e000-70ebc4f000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebc4f000-70ebc51000 rw-p 00000000 00:05 10269718 /dev/ashmem/dalvik-indirect ref table (deleted)
+70ebc51000-70ebc55000 rw-p 00000000 00:05 10270746 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc55000-70ebc59000 rw-p 00000000 00:05 10270745 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc59000-70ebc5d000 rw-p 00000000 00:05 10270744 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc5d000-70ebc7d000 r--s 00000000 00:10 16599 /dev/__properties__/u:object_r:exported_dalvik_prop:s0
+70ebc7d000-70ebc7e000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebc7e000-70ebc7f000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebc7f000-70ebc80000 r--s 00004000 fc:00 963 /system/framework/core-libart.jar
+70ebc80000-70ebc81000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebc81000-70ebc85000 rw-p 00000000 00:05 10270743 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc85000-70ebc89000 rw-p 00000000 00:05 10270742 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc89000-70ebc8a000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70ebc8a000-70ebc8c000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebc8c000-70ebc8d000 r--s 0001e000 fc:00 699 /system/framework/core-oj.jar
+70ebc8d000-70ebc91000 rw-p 00000000 00:05 10270741 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc91000-70ebc95000 rw-p 00000000 00:05 10270740 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc95000-70ebc99000 rw-p 00000000 00:05 10270739 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebc99000-70ebc9b000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebc9b000-70ebc9c000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebc9c000-70ebca0000 rw-p 00000000 00:05 10270738 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebca0000-70ebca4000 rw-p 00000000 00:05 10270737 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebca4000-70ebca8000 rw-p 00000000 00:05 10270736 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebca8000-70ebcac000 rw-p 00000000 00:05 10270735 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebcac000-70ebcb0000 rw-p 00000000 00:05 10270734 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebcb0000-70ebcd0000 r--s 00000000 00:10 16592 /dev/__properties__/u:object_r:exported2_system_prop:s0
+70ebcd0000-70ebcd1000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebcd1000-70ebcd5000 rw-p 00000000 00:05 10270733 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebcd5000-70ebcd9000 rw-p 00000000 00:05 10270732 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebcd9000-70ebcdd000 rw-p 00000000 00:05 10270731 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebcdd000-70ebce1000 rw-p 00000000 00:05 10270730 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebce1000-70ebce5000 rw-p 00000000 00:05 10270729 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebce5000-70ebce9000 rw-p 00000000 00:05 10270728 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebce9000-70ebcea000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebcea000-70ebcec000 rw-p 00000000 00:05 10270987 /dev/ashmem/dalvik-indirect ref table (deleted)
+70ebcec000-70ebcf9000 r--p 00646000 103:1d 639532 /data/dalvik-cache/arm64/system@framework@boot-framework.art
+70ebcf9000-70ebd19000 r--s 00000000 00:10 16620 /dev/__properties__/u:object_r:log_tag_prop:s0
+70ebd19000-70ebd39000 r--s 00000000 00:10 16621 /dev/__properties__/u:object_r:logd_prop:s0
+70ebd39000-70ebd3a000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebd3a000-70ebd3b000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebd3b000-70ebd3f000 rw-p 00000000 00:05 10270727 /dev/ashmem/dalvik-thread local mark stack (deleted)
+70ebd3f000-70ebd40000 r--p 00002000 103:1d 639556 /data/dalvik-cache/arm64/system@framework@boot-com.google.vr.platform.art
+70ebd40000-70ebd41000 r--p 00005000 103:1d 639553 /data/dalvik-cache/arm64/system@framework@boot-android.test.base.art
+70ebd41000-70ebd42000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebd42000-70ebd43000 r--p 00001000 103:1d 639550 /data/dalvik-cache/arm64/system@framework@boot-framework-oahl-backward-compatibility.art
+70ebd43000-70ebd44000 r--p 00005000 103:1d 639547 /data/dalvik-cache/arm64/system@framework@boot-android.hidl.manager-V1.0-java.art
+70ebd44000-70ebd45000 r--p 00003000 103:1d 639544 /data/dalvik-cache/arm64/system@framework@boot-android.hidl.base-V1.0-java.art
+70ebd45000-70ebd46000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebd46000-70ebd47000 r--p 0000f000 103:1d 639541 /data/dalvik-cache/arm64/system@framework@boot-ims-common.art
+70ebd47000-70ebd48000 r--p 0000d000 103:1d 639538 /data/dalvik-cache/arm64/system@framework@boot-voip-common.art
+70ebd48000-70ebd4a000 r--p 0005e000 103:1d 639535 /data/dalvik-cache/arm64/system@framework@boot-telephony-common.art
+70ebd4a000-70ebd4b000 r--p 00040000 103:1d 639529 /data/dalvik-cache/arm64/system@framework@boot-ext.art
+70ebd4b000-70ebd4c000 r--p 0004a000 103:1d 639526 /data/dalvik-cache/arm64/system@framework@boot-apache-xml.art
+70ebd4c000-70ebd4d000 r--p 00046000 103:1d 639523 /data/dalvik-cache/arm64/system@framework@boot-bouncycastle.art
+70ebd4d000-70ebd4e000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebd4e000-70ebd53000 r--p 00225000 103:1d 639511 /data/dalvik-cache/arm64/system@framework@boot.art
+70ebd53000-70ebd5a000 rw-p 00000000 fc:00 583 /system/etc/event-log-tags
+70ebd5a000-70ebd5b000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebd5b000-70ebd5c000 r--p 0002e000 103:1d 639520 /data/dalvik-cache/arm64/system@framework@boot-okhttp.art
+70ebd5c000-70ebd5d000 r--p 00035000 103:1d 639517 /data/dalvik-cache/arm64/system@framework@boot-conscrypt.art
+70ebd5d000-70ebd5f000 r--p 000d0000 103:1d 639514 /data/dalvik-cache/arm64/system@framework@boot-core-libart.art
+70ebd5f000-70ebd62000 r--p 00000000 00:00 0 [anon:atexit handlers]
+70ebd62000-70ebd63000 rw-p 00000000 00:00 0
+70ebd63000-70ebd83000 r--s 00000000 00:10 16590 /dev/__properties__/u:object_r:exported2_default_prop:s0
+70ebd83000-70ebd84000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebd84000-70ebd89000 rw-p 00000000 00:00 0
+70ebd89000-70ebda9000 r--s 00000000 00:10 16669 /dev/__properties__/properties_serial
+70ebda9000-70ebdb3000 r--s 00000000 00:10 16560 /dev/__properties__/property_info
+70ebdb3000-70ebdb4000 rw-p 00000000 00:00 0 [anon:linker_alloc_vector]
+70ebdb4000-70ebdb5000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70ebdb5000-70ebdb7000 rw-p 00000000 00:00 0 [anon:System property context nodes]
+70ebdb7000-70ebdb8000 rw-p 00000000 00:00 0 [anon:linker_alloc_vector]
+70ebdb8000-70ebdba000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70ebdba000-70ebdbb000 rw-p 00000000 00:00 0 [anon:linker_alloc]
+70ebdbb000-70ebdbd000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70ebdbd000-70ebdbe000 r--p 00000000 00:00 0 [anon:atexit handlers]
+70ebdbe000-70ebdbf000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebdbf000-70ebdc0000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70ebdc0000-70ebdc1000 rw-p 00000000 00:00 0 [anon:linker_alloc_vector]
+70ebdc1000-70ebdc2000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70ebdc2000-70ebdc3000 rw-p 00000000 00:00 0 [anon:linker_alloc_vector]
+70ebdc3000-70ebdc5000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70ebdc5000-70ebde5000 r--s 00000000 00:10 16600 /dev/__properties__/u:object_r:exported_default_prop:s0
+70ebde5000-70ebde6000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70ebde6000-70ebde8000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebde8000-70ebe08000 r--s 00000000 00:10 16582 /dev/__properties__/u:object_r:debug_prop:s0
+70ebe08000-70ebe09000 ---p 00000000 00:00 0
+70ebe09000-70ebe0a000 rw-p 00000000 00:00 0
+70ebe0a000-70ebe0b000 ---p 00000000 00:00 0
+70ebe0b000-70ebe2b000 r--s 00000000 00:10 16669 /dev/__properties__/properties_serial
+70ebe2b000-70ebe2d000 rw-p 00000000 00:00 0 [anon:System property context nodes]
+70ebe2d000-70ebf55000 r-xp 00000000 fc:00 3184 /system/bin/linker64
+70ebf55000-70ebf5f000 r--s 00000000 00:10 16560 /dev/__properties__/property_info
+70ebf5f000-70ebf60000 r--p 00000000 00:00 0 [anon:linker_alloc]
+70ebf60000-70ebf61000 rw-p 00000000 00:00 0 [anon:linker_alloc_vector]
+70ebf61000-70ebf62000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70ebf62000-70ebf63000 rw-p 00000000 00:00 0 [anon:arc4random data]
+70ebf63000-70ebf64000 rw-p 00000000 00:00 0 [anon:linker_alloc_small_objects]
+70ebf64000-70ebf65000 r--p 00000000 00:00 0 [anon:atexit handlers]
+70ebf65000-70ebf66000 ---p 00000000 00:00 0 [anon:thread signal stack guard]
+70ebf66000-70ebf6a000 rw-p 00000000 00:00 0 [anon:thread signal stack]
+70ebf6a000-70ebf6b000 rw-p 00000000 00:00 0 [anon:arc4random data]
+70ebf6b000-70ebf6c000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70ebf6c000-70ebf6f000 rw-p 00000000 00:00 0 [anon:bionic TLS]
+70ebf6f000-70ebf70000 ---p 00000000 00:00 0 [anon:bionic TLS guard]
+70ebf70000-70ebf71000 r--p 00000000 00:00 0 [vvar]
+70ebf71000-70ebf72000 r-xp 00000000 00:00 0 [vdso]
+70ebf72000-70ebf7d000 r--p 00135000 fc:00 3184 /system/bin/linker64
+70ebf7d000-70ebf7e000 rw-p 00140000 fc:00 3184 /system/bin/linker64
+70ebf7e000-70ebf81000 rw-p 00000000 00:00 0
+70ebf81000-70ebf82000 r--p 00000000 00:00 0
+70ebf82000-70ebf89000 rw-p 00000000 00:00 0
+7fc7df1000-7fc7df2000 ---p 00000000 00:00 0
+7fc7df2000-7fc85f1000 rw-p 00000000 00:00 0 [stack]
diff --git a/libsparse/.clang-format b/libsparse/.clang-format
new file mode 120000
index 0000000..fd0645f
--- /dev/null
+++ b/libsparse/.clang-format
@@ -0,0 +1 @@
+../.clang-format-2
\ No newline at end of file
diff --git a/libsparse/Android.bp b/libsparse/Android.bp
index b894656..8ad339f 100644
--- a/libsparse/Android.bp
+++ b/libsparse/Android.bp
@@ -3,13 +3,14 @@
cc_library {
name: "libsparse",
host_supported: true,
+ recovery_available: true,
unique_host_soname: true,
srcs: [
- "backed_block.c",
- "output_file.c",
- "sparse.c",
- "sparse_crc32.c",
- "sparse_err.c",
+ "backed_block.cpp",
+ "output_file.cpp",
+ "sparse.cpp",
+ "sparse_crc32.cpp",
+ "sparse_err.cpp",
"sparse_read.cpp",
],
cflags: ["-Werror"],
@@ -30,8 +31,8 @@
name: "simg2img",
host_supported: true,
srcs: [
- "simg2img.c",
- "sparse_crc32.c",
+ "simg2img.cpp",
+ "sparse_crc32.cpp",
],
static_libs: [
"libsparse",
@@ -45,7 +46,7 @@
cc_binary {
name: "img2simg",
host_supported: true,
- srcs: ["img2simg.c"],
+ srcs: ["img2simg.cpp"],
static_libs: [
"libsparse",
"libz",
@@ -57,7 +58,7 @@
cc_binary_host {
name: "append2simg",
- srcs: ["append2simg.c"],
+ srcs: ["append2simg.cpp"],
static_libs: [
"libsparse",
"libz",
diff --git a/libsparse/OWNERS b/libsparse/OWNERS
new file mode 100644
index 0000000..70b375f
--- /dev/null
+++ b/libsparse/OWNERS
@@ -0,0 +1 @@
+ccross@google.com
diff --git a/libsparse/append2simg.c b/libsparse/append2simg.c
deleted file mode 100644
index eef8764..0000000
--- a/libsparse/append2simg.c
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#define _FILE_OFFSET_BITS 64
-#define _LARGEFILE64_SOURCE 1
-#define _GNU_SOURCE
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <sparse/sparse.h>
-#include "sparse_file.h"
-#include "backed_block.h"
-
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif
-
-#if defined(__APPLE__) && defined(__MACH__)
-#define lseek64 lseek
-#endif
-#if defined(__APPLE__) && defined(__MACH__)
-#define lseek64 lseek
-#define off64_t off_t
-#endif
-
-void usage()
-{
- fprintf(stderr, "Usage: append2simg <output> <input>\n");
-}
-
-int main(int argc, char *argv[])
-{
- int output;
- int output_block;
- char *output_path;
- struct sparse_file *sparse_output;
-
- int input;
- char *input_path;
- off64_t input_len;
-
- int tmp_fd;
- char *tmp_path;
-
- int ret;
-
- if (argc == 3) {
- output_path = argv[1];
- input_path = argv[2];
- } else {
- usage();
- exit(-1);
- }
-
- ret = asprintf(&tmp_path, "%s.append2simg", output_path);
- if (ret < 0) {
- fprintf(stderr, "Couldn't allocate filename\n");
- exit(-1);
- }
-
- output = open(output_path, O_RDWR | O_BINARY);
- if (output < 0) {
- fprintf(stderr, "Couldn't open output file (%s)\n", strerror(errno));
- exit(-1);
- }
-
- sparse_output = sparse_file_import_auto(output, false, true);
- if (!sparse_output) {
- fprintf(stderr, "Couldn't import output file\n");
- exit(-1);
- }
-
- input = open(input_path, O_RDONLY | O_BINARY);
- if (input < 0) {
- fprintf(stderr, "Couldn't open input file (%s)\n", strerror(errno));
- exit(-1);
- }
-
- input_len = lseek64(input, 0, SEEK_END);
- if (input_len < 0) {
- fprintf(stderr, "Couldn't get input file length (%s)\n", strerror(errno));
- exit(-1);
- } else if (input_len % sparse_output->block_size) {
- fprintf(stderr, "Input file is not a multiple of the output file's block size");
- exit(-1);
- }
- lseek64(input, 0, SEEK_SET);
-
- output_block = sparse_output->len / sparse_output->block_size;
- if (sparse_file_add_fd(sparse_output, input, 0, input_len, output_block) < 0) {
- fprintf(stderr, "Couldn't add input file\n");
- exit(-1);
- }
- sparse_output->len += input_len;
-
- tmp_fd = open(tmp_path, O_WRONLY | O_CREAT | O_BINARY, 0664);
- if (tmp_fd < 0) {
- fprintf(stderr, "Couldn't open temporary file (%s)\n", strerror(errno));
- exit(-1);
- }
-
- lseek64(output, 0, SEEK_SET);
- if (sparse_file_write(sparse_output, tmp_fd, false, true, false) < 0) {
- fprintf(stderr, "Failed to write sparse file\n");
- exit(-1);
- }
-
- sparse_file_destroy(sparse_output);
- close(tmp_fd);
- close(output);
- close(input);
-
- ret = rename(tmp_path, output_path);
- if (ret < 0) {
- fprintf(stderr, "Failed to rename temporary file (%s)\n", strerror(errno));
- exit(-1);
- }
-
- free(tmp_path);
-
- exit(0);
-}
diff --git a/libsparse/append2simg.cpp b/libsparse/append2simg.cpp
new file mode 100644
index 0000000..99f4339
--- /dev/null
+++ b/libsparse/append2simg.cpp
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#define _FILE_OFFSET_BITS 64
+#define _LARGEFILE64_SOURCE 1
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sparse/sparse.h>
+#include "backed_block.h"
+#include "sparse_file.h"
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+#if defined(__APPLE__) && defined(__MACH__)
+#define lseek64 lseek
+#endif
+#if defined(__APPLE__) && defined(__MACH__)
+#define lseek64 lseek
+#define off64_t off_t
+#endif
+
+void usage() {
+ fprintf(stderr, "Usage: append2simg <output> <input>\n");
+}
+
+int main(int argc, char* argv[]) {
+ int output;
+ int output_block;
+ char* output_path;
+ struct sparse_file* sparse_output;
+
+ int input;
+ char* input_path;
+ off64_t input_len;
+
+ int tmp_fd;
+ char* tmp_path;
+
+ int ret;
+
+ if (argc == 3) {
+ output_path = argv[1];
+ input_path = argv[2];
+ } else {
+ usage();
+ exit(-1);
+ }
+
+ ret = asprintf(&tmp_path, "%s.append2simg", output_path);
+ if (ret < 0) {
+ fprintf(stderr, "Couldn't allocate filename\n");
+ exit(-1);
+ }
+
+ output = open(output_path, O_RDWR | O_BINARY);
+ if (output < 0) {
+ fprintf(stderr, "Couldn't open output file (%s)\n", strerror(errno));
+ exit(-1);
+ }
+
+ sparse_output = sparse_file_import_auto(output, false, true);
+ if (!sparse_output) {
+ fprintf(stderr, "Couldn't import output file\n");
+ exit(-1);
+ }
+
+ input = open(input_path, O_RDONLY | O_BINARY);
+ if (input < 0) {
+ fprintf(stderr, "Couldn't open input file (%s)\n", strerror(errno));
+ exit(-1);
+ }
+
+ input_len = lseek64(input, 0, SEEK_END);
+ if (input_len < 0) {
+ fprintf(stderr, "Couldn't get input file length (%s)\n", strerror(errno));
+ exit(-1);
+ } else if (input_len % sparse_output->block_size) {
+ fprintf(stderr, "Input file is not a multiple of the output file's block size");
+ exit(-1);
+ }
+ lseek64(input, 0, SEEK_SET);
+
+ output_block = sparse_output->len / sparse_output->block_size;
+ if (sparse_file_add_fd(sparse_output, input, 0, input_len, output_block) < 0) {
+ fprintf(stderr, "Couldn't add input file\n");
+ exit(-1);
+ }
+ sparse_output->len += input_len;
+
+ tmp_fd = open(tmp_path, O_WRONLY | O_CREAT | O_BINARY, 0664);
+ if (tmp_fd < 0) {
+ fprintf(stderr, "Couldn't open temporary file (%s)\n", strerror(errno));
+ exit(-1);
+ }
+
+ lseek64(output, 0, SEEK_SET);
+ if (sparse_file_write(sparse_output, tmp_fd, false, true, false) < 0) {
+ fprintf(stderr, "Failed to write sparse file\n");
+ exit(-1);
+ }
+
+ sparse_file_destroy(sparse_output);
+ close(tmp_fd);
+ close(output);
+ close(input);
+
+ ret = rename(tmp_path, output_path);
+ if (ret < 0) {
+ fprintf(stderr, "Failed to rename temporary file (%s)\n", strerror(errno));
+ exit(-1);
+ }
+
+ free(tmp_path);
+
+ exit(0);
+}
diff --git a/libsparse/backed_block.c b/libsparse/backed_block.c
deleted file mode 100644
index 794cd6b..0000000
--- a/libsparse/backed_block.c
+++ /dev/null
@@ -1,404 +0,0 @@
-/*
- * Copyright (C) 2010 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 <assert.h>
-#include <errno.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "backed_block.h"
-#include "sparse_defs.h"
-
-struct backed_block {
- unsigned int block;
- unsigned int len;
- enum backed_block_type type;
- union {
- struct {
- void *data;
- } data;
- struct {
- char *filename;
- int64_t offset;
- } file;
- struct {
- int fd;
- int64_t offset;
- } fd;
- struct {
- uint32_t val;
- } fill;
- };
- struct backed_block *next;
-};
-
-struct backed_block_list {
- struct backed_block *data_blocks;
- struct backed_block *last_used;
- unsigned int block_size;
-};
-
-struct backed_block *backed_block_iter_new(struct backed_block_list *bbl)
-{
- return bbl->data_blocks;
-}
-
-struct backed_block *backed_block_iter_next(struct backed_block *bb)
-{
- return bb->next;
-}
-
-unsigned int backed_block_len(struct backed_block *bb)
-{
- return bb->len;
-}
-
-unsigned int backed_block_block(struct backed_block *bb)
-{
- return bb->block;
-}
-
-void *backed_block_data(struct backed_block *bb)
-{
- assert(bb->type == BACKED_BLOCK_DATA);
- return bb->data.data;
-}
-
-const char *backed_block_filename(struct backed_block *bb)
-{
- assert(bb->type == BACKED_BLOCK_FILE);
- return bb->file.filename;
-}
-
-int backed_block_fd(struct backed_block *bb)
-{
- assert(bb->type == BACKED_BLOCK_FD);
- return bb->fd.fd;
-}
-
-int64_t backed_block_file_offset(struct backed_block *bb)
-{
- assert(bb->type == BACKED_BLOCK_FILE || bb->type == BACKED_BLOCK_FD);
- if (bb->type == BACKED_BLOCK_FILE) {
- return bb->file.offset;
- } else { /* bb->type == BACKED_BLOCK_FD */
- return bb->fd.offset;
- }
-}
-
-uint32_t backed_block_fill_val(struct backed_block *bb)
-{
- assert(bb->type == BACKED_BLOCK_FILL);
- return bb->fill.val;
-}
-
-enum backed_block_type backed_block_type(struct backed_block *bb)
-{
- return bb->type;
-}
-
-void backed_block_destroy(struct backed_block *bb)
-{
- if (bb->type == BACKED_BLOCK_FILE) {
- free(bb->file.filename);
- }
-
- free(bb);
-}
-
-struct backed_block_list *backed_block_list_new(unsigned int block_size)
-{
- struct backed_block_list *b = calloc(sizeof(struct backed_block_list), 1);
- b->block_size = block_size;
- return b;
-}
-
-void backed_block_list_destroy(struct backed_block_list *bbl)
-{
- if (bbl->data_blocks) {
- struct backed_block *bb = bbl->data_blocks;
- while (bb) {
- struct backed_block *next = bb->next;
- backed_block_destroy(bb);
- bb = next;
- }
- }
-
- free(bbl);
-}
-
-void backed_block_list_move(struct backed_block_list *from,
- struct backed_block_list *to, struct backed_block *start,
- struct backed_block *end)
-{
- struct backed_block *bb;
-
- if (start == NULL) {
- start = from->data_blocks;
- }
-
- if (!end) {
- for (end = start; end && end->next; end = end->next)
- ;
- }
-
- if (start == NULL || end == NULL) {
- return;
- }
-
- from->last_used = NULL;
- to->last_used = NULL;
- if (from->data_blocks == start) {
- from->data_blocks = end->next;
- } else {
- for (bb = from->data_blocks; bb; bb = bb->next) {
- if (bb->next == start) {
- bb->next = end->next;
- break;
- }
- }
- }
-
- if (!to->data_blocks) {
- to->data_blocks = start;
- end->next = NULL;
- } else {
- for (bb = to->data_blocks; bb; bb = bb->next) {
- if (!bb->next || bb->next->block > start->block) {
- end->next = bb->next;
- bb->next = start;
- break;
- }
- }
- }
-}
-
-/* may free b */
-static int merge_bb(struct backed_block_list *bbl,
- struct backed_block *a, struct backed_block *b)
-{
- unsigned int block_len;
-
- /* Block doesn't exist (possible if one block is the last block) */
- if (!a || !b) {
- return -EINVAL;
- }
-
- assert(a->block < b->block);
-
- /* Blocks are of different types */
- if (a->type != b->type) {
- return -EINVAL;
- }
-
- /* Blocks are not adjacent */
- block_len = a->len / bbl->block_size; /* rounds down */
- if (a->block + block_len != b->block) {
- return -EINVAL;
- }
-
- switch (a->type) {
- case BACKED_BLOCK_DATA:
- /* Don't support merging data for now */
- return -EINVAL;
- case BACKED_BLOCK_FILL:
- if (a->fill.val != b->fill.val) {
- return -EINVAL;
- }
- break;
- case BACKED_BLOCK_FILE:
- /* Already make sure b->type is BACKED_BLOCK_FILE */
- if (strcmp(a->file.filename, b->file.filename) ||
- a->file.offset + a->len != b->file.offset) {
- return -EINVAL;
- }
- break;
- case BACKED_BLOCK_FD:
- if (a->fd.fd != b->fd.fd ||
- a->fd.offset + a->len != b->fd.offset) {
- return -EINVAL;
- }
- break;
- }
-
- /* Blocks are compatible and adjacent, with a before b. Merge b into a,
- * and free b */
- a->len += b->len;
- a->next = b->next;
-
- backed_block_destroy(b);
-
- return 0;
-}
-
-static int queue_bb(struct backed_block_list *bbl, struct backed_block *new_bb)
-{
- struct backed_block *bb;
-
- if (bbl->data_blocks == NULL) {
- bbl->data_blocks = new_bb;
- return 0;
- }
-
- if (bbl->data_blocks->block > new_bb->block) {
- new_bb->next = bbl->data_blocks;
- bbl->data_blocks = new_bb;
- return 0;
- }
-
- /* Optimization: blocks are mostly queued in sequence, so save the
- pointer to the last bb that was added, and start searching from
- there if the next block number is higher */
- if (bbl->last_used && new_bb->block > bbl->last_used->block)
- bb = bbl->last_used;
- else
- bb = bbl->data_blocks;
- bbl->last_used = new_bb;
-
- for (; bb->next && bb->next->block < new_bb->block; bb = bb->next)
- ;
-
- if (bb->next == NULL) {
- bb->next = new_bb;
- } else {
- new_bb->next = bb->next;
- bb->next = new_bb;
- }
-
- merge_bb(bbl, new_bb, new_bb->next);
- if (!merge_bb(bbl, bb, new_bb)) {
- /* new_bb destroyed, point to retained as last_used */
- bbl->last_used = bb;
- }
-
- return 0;
-}
-
-/* Queues a fill block of memory to be written to the specified data blocks */
-int backed_block_add_fill(struct backed_block_list *bbl, unsigned int fill_val,
- unsigned int len, unsigned int block)
-{
- struct backed_block *bb = calloc(1, sizeof(struct backed_block));
- if (bb == NULL) {
- return -ENOMEM;
- }
-
- bb->block = block;
- bb->len = len;
- bb->type = BACKED_BLOCK_FILL;
- bb->fill.val = fill_val;
- bb->next = NULL;
-
- return queue_bb(bbl, bb);
-}
-
-/* Queues a block of memory to be written to the specified data blocks */
-int backed_block_add_data(struct backed_block_list *bbl, void *data,
- unsigned int len, unsigned int block)
-{
- struct backed_block *bb = calloc(1, sizeof(struct backed_block));
- if (bb == NULL) {
- return -ENOMEM;
- }
-
- bb->block = block;
- bb->len = len;
- bb->type = BACKED_BLOCK_DATA;
- bb->data.data = data;
- bb->next = NULL;
-
- return queue_bb(bbl, bb);
-}
-
-/* Queues a chunk of a file on disk to be written to the specified data blocks */
-int backed_block_add_file(struct backed_block_list *bbl, const char *filename,
- int64_t offset, unsigned int len, unsigned int block)
-{
- struct backed_block *bb = calloc(1, sizeof(struct backed_block));
- if (bb == NULL) {
- return -ENOMEM;
- }
-
- bb->block = block;
- bb->len = len;
- bb->type = BACKED_BLOCK_FILE;
- bb->file.filename = strdup(filename);
- bb->file.offset = offset;
- bb->next = NULL;
-
- return queue_bb(bbl, bb);
-}
-
-/* Queues a chunk of a fd to be written to the specified data blocks */
-int backed_block_add_fd(struct backed_block_list *bbl, int fd, int64_t offset,
- unsigned int len, unsigned int block)
-{
- struct backed_block *bb = calloc(1, sizeof(struct backed_block));
- if (bb == NULL) {
- return -ENOMEM;
- }
-
- bb->block = block;
- bb->len = len;
- bb->type = BACKED_BLOCK_FD;
- bb->fd.fd = fd;
- bb->fd.offset = offset;
- bb->next = NULL;
-
- return queue_bb(bbl, bb);
-}
-
-int backed_block_split(struct backed_block_list *bbl, struct backed_block *bb,
- unsigned int max_len)
-{
- struct backed_block *new_bb;
-
- max_len = ALIGN_DOWN(max_len, bbl->block_size);
-
- if (bb->len <= max_len) {
- return 0;
- }
-
- new_bb = malloc(sizeof(struct backed_block));
- if (new_bb == NULL) {
- return -ENOMEM;
- }
-
- *new_bb = *bb;
-
- 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.offset += max_len;
- break;
- case BACKED_BLOCK_FD:
- new_bb->fd.offset += max_len;
- break;
- case BACKED_BLOCK_FILL:
- break;
- }
-
- return 0;
-}
diff --git a/libsparse/backed_block.cpp b/libsparse/backed_block.cpp
new file mode 100644
index 0000000..f3d8022
--- /dev/null
+++ b/libsparse/backed_block.cpp
@@ -0,0 +1,380 @@
+/*
+ * Copyright (C) 2010 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 <assert.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "backed_block.h"
+#include "sparse_defs.h"
+
+struct backed_block {
+ unsigned int block;
+ unsigned int len;
+ enum backed_block_type type;
+ union {
+ struct {
+ void* data;
+ } data;
+ struct {
+ char* filename;
+ int64_t offset;
+ } file;
+ struct {
+ int fd;
+ int64_t offset;
+ } fd;
+ struct {
+ uint32_t val;
+ } fill;
+ };
+ struct backed_block* next;
+};
+
+struct backed_block_list {
+ struct backed_block* data_blocks;
+ struct backed_block* last_used;
+ unsigned int block_size;
+};
+
+struct backed_block* backed_block_iter_new(struct backed_block_list* bbl) {
+ return bbl->data_blocks;
+}
+
+struct backed_block* backed_block_iter_next(struct backed_block* bb) {
+ return bb->next;
+}
+
+unsigned int backed_block_len(struct backed_block* bb) {
+ return bb->len;
+}
+
+unsigned int backed_block_block(struct backed_block* bb) {
+ return bb->block;
+}
+
+void* backed_block_data(struct backed_block* bb) {
+ assert(bb->type == BACKED_BLOCK_DATA);
+ return bb->data.data;
+}
+
+const char* backed_block_filename(struct backed_block* bb) {
+ assert(bb->type == BACKED_BLOCK_FILE);
+ return bb->file.filename;
+}
+
+int backed_block_fd(struct backed_block* bb) {
+ assert(bb->type == BACKED_BLOCK_FD);
+ return bb->fd.fd;
+}
+
+int64_t backed_block_file_offset(struct backed_block* bb) {
+ assert(bb->type == BACKED_BLOCK_FILE || bb->type == BACKED_BLOCK_FD);
+ if (bb->type == BACKED_BLOCK_FILE) {
+ return bb->file.offset;
+ } else { /* bb->type == BACKED_BLOCK_FD */
+ return bb->fd.offset;
+ }
+}
+
+uint32_t backed_block_fill_val(struct backed_block* bb) {
+ assert(bb->type == BACKED_BLOCK_FILL);
+ return bb->fill.val;
+}
+
+enum backed_block_type backed_block_type(struct backed_block* bb) {
+ return bb->type;
+}
+
+void backed_block_destroy(struct backed_block* bb) {
+ if (bb->type == BACKED_BLOCK_FILE) {
+ free(bb->file.filename);
+ }
+
+ free(bb);
+}
+
+struct backed_block_list* backed_block_list_new(unsigned int block_size) {
+ struct backed_block_list* b =
+ reinterpret_cast<backed_block_list*>(calloc(sizeof(struct backed_block_list), 1));
+ b->block_size = block_size;
+ return b;
+}
+
+void backed_block_list_destroy(struct backed_block_list* bbl) {
+ if (bbl->data_blocks) {
+ struct backed_block* bb = bbl->data_blocks;
+ while (bb) {
+ struct backed_block* next = bb->next;
+ backed_block_destroy(bb);
+ bb = next;
+ }
+ }
+
+ free(bbl);
+}
+
+void backed_block_list_move(struct backed_block_list* from, struct backed_block_list* to,
+ struct backed_block* start, struct backed_block* end) {
+ struct backed_block* bb;
+
+ if (start == nullptr) {
+ start = from->data_blocks;
+ }
+
+ if (!end) {
+ for (end = start; end && end->next; end = end->next)
+ ;
+ }
+
+ if (start == nullptr || end == nullptr) {
+ return;
+ }
+
+ from->last_used = nullptr;
+ to->last_used = nullptr;
+ if (from->data_blocks == start) {
+ from->data_blocks = end->next;
+ } else {
+ for (bb = from->data_blocks; bb; bb = bb->next) {
+ if (bb->next == start) {
+ bb->next = end->next;
+ break;
+ }
+ }
+ }
+
+ if (!to->data_blocks) {
+ to->data_blocks = start;
+ end->next = nullptr;
+ } else {
+ for (bb = to->data_blocks; bb; bb = bb->next) {
+ if (!bb->next || bb->next->block > start->block) {
+ end->next = bb->next;
+ bb->next = start;
+ break;
+ }
+ }
+ }
+}
+
+/* may free b */
+static int merge_bb(struct backed_block_list* bbl, struct backed_block* a, struct backed_block* b) {
+ unsigned int block_len;
+
+ /* Block doesn't exist (possible if one block is the last block) */
+ if (!a || !b) {
+ return -EINVAL;
+ }
+
+ assert(a->block < b->block);
+
+ /* Blocks are of different types */
+ if (a->type != b->type) {
+ return -EINVAL;
+ }
+
+ /* Blocks are not adjacent */
+ block_len = a->len / bbl->block_size; /* rounds down */
+ if (a->block + block_len != b->block) {
+ return -EINVAL;
+ }
+
+ switch (a->type) {
+ case BACKED_BLOCK_DATA:
+ /* Don't support merging data for now */
+ return -EINVAL;
+ case BACKED_BLOCK_FILL:
+ if (a->fill.val != b->fill.val) {
+ return -EINVAL;
+ }
+ break;
+ case BACKED_BLOCK_FILE:
+ /* Already make sure b->type is BACKED_BLOCK_FILE */
+ if (strcmp(a->file.filename, b->file.filename) || a->file.offset + a->len != b->file.offset) {
+ return -EINVAL;
+ }
+ break;
+ case BACKED_BLOCK_FD:
+ if (a->fd.fd != b->fd.fd || a->fd.offset + a->len != b->fd.offset) {
+ return -EINVAL;
+ }
+ break;
+ }
+
+ /* Blocks are compatible and adjacent, with a before b. Merge b into a,
+ * and free b */
+ a->len += b->len;
+ a->next = b->next;
+
+ backed_block_destroy(b);
+
+ return 0;
+}
+
+static int queue_bb(struct backed_block_list* bbl, struct backed_block* new_bb) {
+ struct backed_block* bb;
+
+ if (bbl->data_blocks == nullptr) {
+ bbl->data_blocks = new_bb;
+ return 0;
+ }
+
+ if (bbl->data_blocks->block > new_bb->block) {
+ new_bb->next = bbl->data_blocks;
+ bbl->data_blocks = new_bb;
+ return 0;
+ }
+
+ /* Optimization: blocks are mostly queued in sequence, so save the
+ pointer to the last bb that was added, and start searching from
+ there if the next block number is higher */
+ if (bbl->last_used && new_bb->block > bbl->last_used->block)
+ bb = bbl->last_used;
+ else
+ bb = bbl->data_blocks;
+ bbl->last_used = new_bb;
+
+ for (; bb->next && bb->next->block < new_bb->block; bb = bb->next)
+ ;
+
+ if (bb->next == nullptr) {
+ bb->next = new_bb;
+ } else {
+ new_bb->next = bb->next;
+ bb->next = new_bb;
+ }
+
+ merge_bb(bbl, new_bb, new_bb->next);
+ if (!merge_bb(bbl, bb, new_bb)) {
+ /* new_bb destroyed, point to retained as last_used */
+ bbl->last_used = bb;
+ }
+
+ return 0;
+}
+
+/* Queues a fill block of memory to be written to the specified data blocks */
+int backed_block_add_fill(struct backed_block_list* bbl, unsigned int fill_val, unsigned int len,
+ unsigned int block) {
+ struct backed_block* bb = reinterpret_cast<backed_block*>(calloc(1, sizeof(struct backed_block)));
+ if (bb == nullptr) {
+ return -ENOMEM;
+ }
+
+ bb->block = block;
+ bb->len = len;
+ bb->type = BACKED_BLOCK_FILL;
+ bb->fill.val = fill_val;
+ bb->next = nullptr;
+
+ return queue_bb(bbl, bb);
+}
+
+/* Queues a block of memory to be written to the specified data blocks */
+int backed_block_add_data(struct backed_block_list* bbl, void* data, unsigned int len,
+ unsigned int block) {
+ struct backed_block* bb = reinterpret_cast<backed_block*>(calloc(1, sizeof(struct backed_block)));
+ if (bb == nullptr) {
+ return -ENOMEM;
+ }
+
+ bb->block = block;
+ bb->len = len;
+ bb->type = BACKED_BLOCK_DATA;
+ bb->data.data = data;
+ bb->next = nullptr;
+
+ return queue_bb(bbl, bb);
+}
+
+/* Queues a chunk of a file on disk to be written to the specified data blocks */
+int backed_block_add_file(struct backed_block_list* bbl, const char* filename, int64_t offset,
+ unsigned int len, unsigned int block) {
+ struct backed_block* bb = reinterpret_cast<backed_block*>(calloc(1, sizeof(struct backed_block)));
+ if (bb == nullptr) {
+ return -ENOMEM;
+ }
+
+ bb->block = block;
+ bb->len = len;
+ bb->type = BACKED_BLOCK_FILE;
+ bb->file.filename = strdup(filename);
+ bb->file.offset = offset;
+ bb->next = nullptr;
+
+ return queue_bb(bbl, bb);
+}
+
+/* Queues a chunk of a fd to be written to the specified data blocks */
+int backed_block_add_fd(struct backed_block_list* bbl, int fd, int64_t offset, unsigned int len,
+ unsigned int block) {
+ struct backed_block* bb = reinterpret_cast<backed_block*>(calloc(1, sizeof(struct backed_block)));
+ if (bb == nullptr) {
+ return -ENOMEM;
+ }
+
+ bb->block = block;
+ bb->len = len;
+ bb->type = BACKED_BLOCK_FD;
+ bb->fd.fd = fd;
+ bb->fd.offset = offset;
+ bb->next = nullptr;
+
+ return queue_bb(bbl, bb);
+}
+
+int backed_block_split(struct backed_block_list* bbl, struct backed_block* bb,
+ unsigned int max_len) {
+ struct backed_block* new_bb;
+
+ max_len = ALIGN_DOWN(max_len, bbl->block_size);
+
+ if (bb->len <= max_len) {
+ return 0;
+ }
+
+ new_bb = reinterpret_cast<backed_block*>(malloc(sizeof(struct backed_block)));
+ if (new_bb == nullptr) {
+ return -ENOMEM;
+ }
+
+ *new_bb = *bb;
+
+ 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.offset += max_len;
+ break;
+ case BACKED_BLOCK_FD:
+ new_bb->fd.offset += max_len;
+ break;
+ case BACKED_BLOCK_FILL:
+ break;
+ }
+
+ return 0;
+}
diff --git a/libsparse/backed_block.h b/libsparse/backed_block.h
index 1a159be..3a75460 100644
--- a/libsparse/backed_block.h
+++ b/libsparse/backed_block.h
@@ -23,42 +23,40 @@
struct backed_block;
enum backed_block_type {
- BACKED_BLOCK_DATA,
- BACKED_BLOCK_FILE,
- BACKED_BLOCK_FD,
- BACKED_BLOCK_FILL,
+ BACKED_BLOCK_DATA,
+ BACKED_BLOCK_FILE,
+ BACKED_BLOCK_FD,
+ BACKED_BLOCK_FILL,
};
-int backed_block_add_data(struct backed_block_list *bbl, void *data,
- unsigned int len, unsigned int block);
-int backed_block_add_fill(struct backed_block_list *bbl, unsigned int fill_val,
- unsigned int len, unsigned int block);
-int backed_block_add_file(struct backed_block_list *bbl, const char *filename,
- int64_t offset, unsigned int len, unsigned int block);
-int backed_block_add_fd(struct backed_block_list *bbl, int fd,
- int64_t offset, unsigned int len, unsigned int block);
+int backed_block_add_data(struct backed_block_list* bbl, void* data, unsigned int len,
+ unsigned int block);
+int backed_block_add_fill(struct backed_block_list* bbl, unsigned int fill_val, unsigned int len,
+ unsigned int block);
+int backed_block_add_file(struct backed_block_list* bbl, const char* filename, int64_t offset,
+ unsigned int len, unsigned int block);
+int backed_block_add_fd(struct backed_block_list* bbl, int fd, int64_t offset, unsigned int len,
+ unsigned int block);
-struct backed_block *backed_block_iter_new(struct backed_block_list *bbl);
-struct backed_block *backed_block_iter_next(struct backed_block *bb);
-unsigned int backed_block_len(struct backed_block *bb);
-unsigned int backed_block_block(struct backed_block *bb);
-void *backed_block_data(struct backed_block *bb);
-const char *backed_block_filename(struct backed_block *bb);
-int backed_block_fd(struct backed_block *bb);
-int64_t backed_block_file_offset(struct backed_block *bb);
-uint32_t backed_block_fill_val(struct backed_block *bb);
-enum backed_block_type backed_block_type(struct backed_block *bb);
-int backed_block_split(struct backed_block_list *bbl, struct backed_block *bb,
- unsigned int max_len);
+struct backed_block* backed_block_iter_new(struct backed_block_list* bbl);
+struct backed_block* backed_block_iter_next(struct backed_block* bb);
+unsigned int backed_block_len(struct backed_block* bb);
+unsigned int backed_block_block(struct backed_block* bb);
+void* backed_block_data(struct backed_block* bb);
+const char* backed_block_filename(struct backed_block* bb);
+int backed_block_fd(struct backed_block* bb);
+int64_t backed_block_file_offset(struct backed_block* bb);
+uint32_t backed_block_fill_val(struct backed_block* bb);
+enum backed_block_type backed_block_type(struct backed_block* bb);
+int backed_block_split(struct backed_block_list* bbl, struct backed_block* bb, unsigned int max_len);
-struct backed_block *backed_block_iter_new(struct backed_block_list *bbl);
-struct backed_block *backed_block_iter_next(struct backed_block *bb);
+struct backed_block* backed_block_iter_new(struct backed_block_list* bbl);
+struct backed_block* backed_block_iter_next(struct backed_block* bb);
-struct backed_block_list *backed_block_list_new(unsigned int block_size);
-void backed_block_list_destroy(struct backed_block_list *bbl);
+struct backed_block_list* backed_block_list_new(unsigned int block_size);
+void backed_block_list_destroy(struct backed_block_list* bbl);
-void backed_block_list_move(struct backed_block_list *from,
- struct backed_block_list *to, struct backed_block *start,
- struct backed_block *end);
+void backed_block_list_move(struct backed_block_list* from, struct backed_block_list* to,
+ struct backed_block* start, struct backed_block* end);
#endif
diff --git a/libsparse/defs.h b/libsparse/defs.h
index 34e63c5..28e5cab 100644
--- a/libsparse/defs.h
+++ b/libsparse/defs.h
@@ -17,7 +17,7 @@
#ifndef _LIBSPARSE_DEFS_H_
#ifndef __unused
-#define __unused __attribute__((__unused__))
+#define __unused __attribute__((__unused__))
#endif
#endif /* _LIBSPARSE_DEFS_H_ */
diff --git a/libsparse/img2simg.c b/libsparse/img2simg.c
deleted file mode 100644
index a0db36f..0000000
--- a/libsparse/img2simg.c
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Copyright (C) 2012 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.
- */
-
-#define _FILE_OFFSET_BITS 64
-#define _LARGEFILE64_SOURCE 1
-
-#include <fcntl.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <sparse/sparse.h>
-
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif
-
-#if defined(__APPLE__) && defined(__MACH__)
-#define lseek64 lseek
-#define off64_t off_t
-#endif
-
-void usage()
-{
- fprintf(stderr, "Usage: img2simg <raw_image_file> <sparse_image_file> [<block_size>]\n");
-}
-
-int main(int argc, char *argv[])
-{
- int in;
- int out;
- int ret;
- struct sparse_file *s;
- unsigned int block_size = 4096;
- off64_t len;
-
- if (argc < 3 || argc > 4) {
- usage();
- exit(-1);
- }
-
- if (argc == 4) {
- block_size = atoi(argv[3]);
- }
-
- if (block_size < 1024 || block_size % 4 != 0) {
- usage();
- exit(-1);
- }
-
- if (strcmp(argv[1], "-") == 0) {
- in = STDIN_FILENO;
- } else {
- in = open(argv[1], O_RDONLY | O_BINARY);
- if (in < 0) {
- fprintf(stderr, "Cannot open input file %s\n", argv[1]);
- exit(-1);
- }
- }
-
- if (strcmp(argv[2], "-") == 0) {
- out = STDOUT_FILENO;
- } else {
- out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
- if (out < 0) {
- fprintf(stderr, "Cannot open output file %s\n", argv[2]);
- exit(-1);
- }
- }
-
- len = lseek64(in, 0, SEEK_END);
- lseek64(in, 0, SEEK_SET);
-
- s = sparse_file_new(block_size, len);
- if (!s) {
- fprintf(stderr, "Failed to create sparse file\n");
- exit(-1);
- }
-
- sparse_file_verbose(s);
- ret = sparse_file_read(s, in, false, false);
- if (ret) {
- fprintf(stderr, "Failed to read file\n");
- exit(-1);
- }
-
- ret = sparse_file_write(s, out, false, true, false);
- if (ret) {
- fprintf(stderr, "Failed to write sparse file\n");
- exit(-1);
- }
-
- close(in);
- close(out);
-
- exit(0);
-}
diff --git a/libsparse/img2simg.cpp b/libsparse/img2simg.cpp
new file mode 100644
index 0000000..4c2c6ca
--- /dev/null
+++ b/libsparse/img2simg.cpp
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+#define _FILE_OFFSET_BITS 64
+#define _LARGEFILE64_SOURCE 1
+
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <sparse/sparse.h>
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+#if defined(__APPLE__) && defined(__MACH__)
+#define lseek64 lseek
+#define off64_t off_t
+#endif
+
+void usage() {
+ fprintf(stderr, "Usage: img2simg <raw_image_file> <sparse_image_file> [<block_size>]\n");
+}
+
+int main(int argc, char* argv[]) {
+ int in;
+ int out;
+ int ret;
+ struct sparse_file* s;
+ unsigned int block_size = 4096;
+ off64_t len;
+
+ if (argc < 3 || argc > 4) {
+ usage();
+ exit(-1);
+ }
+
+ if (argc == 4) {
+ block_size = atoi(argv[3]);
+ }
+
+ if (block_size < 1024 || block_size % 4 != 0) {
+ usage();
+ exit(-1);
+ }
+
+ if (strcmp(argv[1], "-") == 0) {
+ in = STDIN_FILENO;
+ } else {
+ in = open(argv[1], O_RDONLY | O_BINARY);
+ if (in < 0) {
+ fprintf(stderr, "Cannot open input file %s\n", argv[1]);
+ exit(-1);
+ }
+ }
+
+ if (strcmp(argv[2], "-") == 0) {
+ out = STDOUT_FILENO;
+ } else {
+ out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
+ if (out < 0) {
+ fprintf(stderr, "Cannot open output file %s\n", argv[2]);
+ exit(-1);
+ }
+ }
+
+ len = lseek64(in, 0, SEEK_END);
+ lseek64(in, 0, SEEK_SET);
+
+ s = sparse_file_new(block_size, len);
+ if (!s) {
+ fprintf(stderr, "Failed to create sparse file\n");
+ exit(-1);
+ }
+
+ sparse_file_verbose(s);
+ ret = sparse_file_read(s, in, false, false);
+ if (ret) {
+ fprintf(stderr, "Failed to read file\n");
+ exit(-1);
+ }
+
+ ret = sparse_file_write(s, out, false, true, false);
+ if (ret) {
+ fprintf(stderr, "Failed to write sparse file\n");
+ exit(-1);
+ }
+
+ close(in);
+ close(out);
+
+ exit(0);
+}
diff --git a/libsparse/include/sparse/sparse.h b/libsparse/include/sparse/sparse.h
index 356f65f..3d5fb0c 100644
--- a/libsparse/include/sparse/sparse.h
+++ b/libsparse/include/sparse/sparse.h
@@ -18,6 +18,7 @@
#define _LIBSPARSE_SPARSE_H_
#include <stdbool.h>
+#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
@@ -26,6 +27,11 @@
struct sparse_file;
+// The callbacks in sparse_file_callback() and sparse_file_foreach_chunk() take
+// size_t as the length type (was `int` in past). This allows clients to keep
+// their codes compatibile with both versions as needed.
+#define SPARSE_CALLBACK_USES_SIZE_T
+
/**
* sparse_file_new - create a new sparse file cookie
*
@@ -201,7 +207,7 @@
* Returns 0 on success, negative errno on error.
*/
int sparse_file_callback(struct sparse_file *s, bool sparse, bool crc,
- int (*write)(void *priv, const void *data, int len), void *priv);
+ int (*write)(void *priv, const void *data, size_t len), void *priv);
/**
* sparse_file_foreach_chunk - call a callback for data blocks in sparse file
@@ -218,7 +224,7 @@
* Returns 0 on success, negative errno on error.
*/
int sparse_file_foreach_chunk(struct sparse_file *s, bool sparse, bool crc,
- int (*write)(void *priv, const void *data, int len, unsigned int block,
+ int (*write)(void *priv, const void *data, size_t len, unsigned int block,
unsigned int nr_blocks),
void *priv);
/**
@@ -240,9 +246,24 @@
int sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc);
/**
- * sparse_file_import - import an existing sparse file
+ * sparse_file_read_buf - read a buffer into a sparse file cookie
*
* @s - sparse file cookie
+ * @buf - buffer to read from
+ * @crc - verify the crc of a file in the Android sparse file format
+ *
+ * Reads a buffer into a sparse file cookie. The buffer must remain
+ * valid until the sparse file cookie is freed. If crc is true, the
+ * crc of the sparse file will be verified.
+ *
+ * Returns 0 on success, negative errno on error.
+ */
+int sparse_file_read_buf(struct sparse_file *s, char *buf, bool crc);
+
+/**
+ * sparse_file_import - import an existing sparse file
+ *
+ * @fd - file descriptor to read from
* @verbose - print verbose errors while reading the sparse file
* @crc - verify the crc of a file in the Android sparse file format
*
@@ -255,6 +276,21 @@
struct sparse_file *sparse_file_import(int fd, bool verbose, bool crc);
/**
+ * sparse_file_import_buf - import an existing sparse file from a buffer
+ *
+ * @buf - buffer to read from
+ * @verbose - print verbose errors while reading the sparse file
+ * @crc - verify the crc of a file in the Android sparse file format
+ *
+ * Reads existing sparse file data into a sparse file cookie, recreating the same
+ * sparse cookie that was used to write it. If verbose is true, prints verbose
+ * errors when the sparse file is formatted incorrectly.
+ *
+ * Returns a new sparse file cookie on success, NULL on error.
+ */
+struct sparse_file *sparse_file_import_buf(char* buf, bool verbose, bool crc);
+
+/**
* sparse_file_import_auto - import an existing sparse or normal file
*
* @fd - file descriptor to read from
diff --git a/libsparse/output_file.c b/libsparse/output_file.c
deleted file mode 100644
index 51e60ef..0000000
--- a/libsparse/output_file.c
+++ /dev/null
@@ -1,775 +0,0 @@
-/*
- * Copyright (C) 2010 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.
- */
-
-#define _FILE_OFFSET_BITS 64
-#define _LARGEFILE64_SOURCE 1
-
-#include <fcntl.h>
-#include <inttypes.h>
-#include <limits.h>
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <zlib.h>
-
-#include "defs.h"
-#include "output_file.h"
-#include "sparse_crc32.h"
-#include "sparse_format.h"
-
-#ifndef _WIN32
-#include <sys/mman.h>
-#define O_BINARY 0
-#else
-#define ftruncate64 ftruncate
-#endif
-
-#if defined(__APPLE__) && defined(__MACH__)
-#define lseek64 lseek
-#define ftruncate64 ftruncate
-#define mmap64 mmap
-#define off64_t off_t
-#endif
-
-#define min(a, b) \
- ({ typeof(a) _a = (a); typeof(b) _b = (b); (_a < _b) ? _a : _b; })
-
-#define SPARSE_HEADER_MAJOR_VER 1
-#define SPARSE_HEADER_MINOR_VER 0
-#define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
-#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
-
-#define container_of(inner, outer_t, elem) \
- ((outer_t *)((char *)(inner) - offsetof(outer_t, elem)))
-
-struct output_file_ops {
- int (*open)(struct output_file *, int fd);
- int (*skip)(struct output_file *, int64_t);
- int (*pad)(struct output_file *, int64_t);
- int (*write)(struct output_file *, void *, size_t);
- void (*close)(struct output_file *);
-};
-
-struct sparse_file_ops {
- int (*write_data_chunk)(struct output_file *out, unsigned int len,
- void *data);
- int (*write_fill_chunk)(struct output_file *out, unsigned int len,
- uint32_t fill_val);
- int (*write_skip_chunk)(struct output_file *out, int64_t len);
- int (*write_end_chunk)(struct output_file *out);
-};
-
-struct output_file {
- int64_t cur_out_ptr;
- unsigned int chunk_cnt;
- uint32_t crc32;
- struct output_file_ops *ops;
- struct sparse_file_ops *sparse_ops;
- int use_crc;
- unsigned int block_size;
- int64_t len;
- char *zero_buf;
- uint32_t *fill_buf;
- char *buf;
-};
-
-struct output_file_gz {
- struct output_file out;
- gzFile gz_fd;
-};
-
-#define to_output_file_gz(_o) \
- container_of((_o), struct output_file_gz, out)
-
-struct output_file_normal {
- struct output_file out;
- int fd;
-};
-
-#define to_output_file_normal(_o) \
- container_of((_o), struct output_file_normal, out)
-
-struct output_file_callback {
- struct output_file out;
- void *priv;
- int (*write)(void *priv, const void *buf, int len);
-};
-
-#define to_output_file_callback(_o) \
- container_of((_o), struct output_file_callback, out)
-
-static int file_open(struct output_file *out, int fd)
-{
- struct output_file_normal *outn = to_output_file_normal(out);
-
- outn->fd = fd;
- return 0;
-}
-
-static int file_skip(struct output_file *out, int64_t cnt)
-{
- off64_t ret;
- struct output_file_normal *outn = to_output_file_normal(out);
-
- ret = lseek64(outn->fd, cnt, SEEK_CUR);
- if (ret < 0) {
- error_errno("lseek64");
- return -1;
- }
- return 0;
-}
-
-static int file_pad(struct output_file *out, int64_t len)
-{
- int ret;
- struct output_file_normal *outn = to_output_file_normal(out);
-
- ret = ftruncate64(outn->fd, len);
- if (ret < 0) {
- return -errno;
- }
-
- return 0;
-}
-
-static int file_write(struct output_file *out, void *data, size_t len)
-{
- ssize_t ret;
- struct output_file_normal *outn = to_output_file_normal(out);
-
- while (len > 0) {
- ret = write(outn->fd, data, len);
- if (ret < 0) {
- if (errno == EINTR) {
- continue;
- }
- error_errno("write");
- return -1;
- }
-
- data = (char *)data + ret;
- len -= ret;
- }
-
- return 0;
-}
-
-static void file_close(struct output_file *out)
-{
- struct output_file_normal *outn = to_output_file_normal(out);
-
- free(outn);
-}
-
-static struct output_file_ops file_ops = {
- .open = file_open,
- .skip = file_skip,
- .pad = file_pad,
- .write = file_write,
- .close = file_close,
-};
-
-static int gz_file_open(struct output_file *out, int fd)
-{
- struct output_file_gz *outgz = to_output_file_gz(out);
-
- outgz->gz_fd = gzdopen(fd, "wb9");
- if (!outgz->gz_fd) {
- error_errno("gzopen");
- return -errno;
- }
-
- return 0;
-}
-
-
-static int gz_file_skip(struct output_file *out, int64_t cnt)
-{
- off64_t ret;
- struct output_file_gz *outgz = to_output_file_gz(out);
-
- ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR);
- if (ret < 0) {
- error_errno("gzseek");
- return -1;
- }
- return 0;
-}
-
-static int gz_file_pad(struct output_file *out, int64_t len)
-{
- off64_t ret;
- struct output_file_gz *outgz = to_output_file_gz(out);
-
- ret = gztell(outgz->gz_fd);
- if (ret < 0) {
- return -1;
- }
-
- if (ret >= len) {
- return 0;
- }
-
- ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET);
- if (ret < 0) {
- return -1;
- }
-
- gzwrite(outgz->gz_fd, "", 1);
-
- return 0;
-}
-
-static int gz_file_write(struct output_file *out, void *data, size_t len)
-{
- int ret;
- struct output_file_gz *outgz = to_output_file_gz(out);
-
- while (len > 0) {
- ret = gzwrite(outgz->gz_fd, data,
- min(len, (unsigned int)INT_MAX));
- if (ret == 0) {
- error("gzwrite %s", gzerror(outgz->gz_fd, NULL));
- return -1;
- }
- len -= ret;
- data = (char *)data + ret;
- }
-
- return 0;
-}
-
-static void gz_file_close(struct output_file *out)
-{
- struct output_file_gz *outgz = to_output_file_gz(out);
-
- gzclose(outgz->gz_fd);
- free(outgz);
-}
-
-static struct output_file_ops gz_file_ops = {
- .open = gz_file_open,
- .skip = gz_file_skip,
- .pad = gz_file_pad,
- .write = gz_file_write,
- .close = gz_file_close,
-};
-
-static int callback_file_open(struct output_file *out __unused, int fd __unused)
-{
- return 0;
-}
-
-static int callback_file_skip(struct output_file *out, int64_t off)
-{
- struct output_file_callback *outc = to_output_file_callback(out);
- int to_write;
- int ret;
-
- while (off > 0) {
- to_write = min(off, (int64_t)INT_MAX);
- ret = outc->write(outc->priv, NULL, to_write);
- if (ret < 0) {
- return ret;
- }
- off -= to_write;
- }
-
- return 0;
-}
-
-static int callback_file_pad(struct output_file *out __unused, int64_t len __unused)
-{
- return -1;
-}
-
-static int callback_file_write(struct output_file *out, void *data, size_t len)
-{
- struct output_file_callback *outc = to_output_file_callback(out);
-
- return outc->write(outc->priv, data, len);
-}
-
-static void callback_file_close(struct output_file *out)
-{
- struct output_file_callback *outc = to_output_file_callback(out);
-
- free(outc);
-}
-
-static struct output_file_ops callback_file_ops = {
- .open = callback_file_open,
- .skip = callback_file_skip,
- .pad = callback_file_pad,
- .write = callback_file_write,
- .close = callback_file_close,
-};
-
-int read_all(int fd, void *buf, size_t len)
-{
- size_t total = 0;
- int ret;
- char *ptr = buf;
-
- while (total < len) {
- ret = read(fd, ptr, len - total);
-
- if (ret < 0)
- return -errno;
-
- if (ret == 0)
- return -EINVAL;
-
- ptr += ret;
- total += ret;
- }
-
- return 0;
-}
-
-static int write_sparse_skip_chunk(struct output_file *out, int64_t skip_len)
-{
- chunk_header_t chunk_header;
- int ret;
-
- if (skip_len % out->block_size) {
- error("don't care size %"PRIi64" is not a multiple of the block size %u",
- skip_len, out->block_size);
- return -1;
- }
-
- /* We are skipping data, so emit a don't care chunk. */
- chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE;
- chunk_header.reserved1 = 0;
- chunk_header.chunk_sz = skip_len / out->block_size;
- chunk_header.total_sz = CHUNK_HEADER_LEN;
- ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
- if (ret < 0)
- return -1;
-
- out->cur_out_ptr += skip_len;
- out->chunk_cnt++;
-
- return 0;
-}
-
-static int write_sparse_fill_chunk(struct output_file *out, unsigned int len,
- uint32_t fill_val)
-{
- chunk_header_t chunk_header;
- int rnd_up_len, count;
- int ret;
-
- /* Round up the fill length to a multiple of the block size */
- rnd_up_len = ALIGN(len, out->block_size);
-
- /* Finally we can safely emit a chunk of data */
- chunk_header.chunk_type = CHUNK_TYPE_FILL;
- chunk_header.reserved1 = 0;
- chunk_header.chunk_sz = rnd_up_len / out->block_size;
- chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val);
- ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
-
- if (ret < 0)
- return -1;
- ret = out->ops->write(out, &fill_val, sizeof(fill_val));
- if (ret < 0)
- return -1;
-
- if (out->use_crc) {
- count = out->block_size / sizeof(uint32_t);
- while (count--)
- out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t));
- }
-
- out->cur_out_ptr += rnd_up_len;
- out->chunk_cnt++;
-
- return 0;
-}
-
-static int write_sparse_data_chunk(struct output_file *out, unsigned int len,
- void *data)
-{
- chunk_header_t chunk_header;
- int 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;
- ret = out->ops->write(out, data, len);
- if (ret < 0)
- return -1;
- if (zero_len) {
- ret = out->ops->write(out, out->zero_buf, zero_len);
- if (ret < 0)
- return -1;
- }
-
- if (out->use_crc) {
- out->crc32 = sparse_crc32(out->crc32, data, len);
- if (zero_len)
- out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_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;
-
- if (out->use_crc) {
- chunk_header.chunk_type = CHUNK_TYPE_CRC32;
- chunk_header.reserved1 = 0;
- chunk_header.chunk_sz = 0;
- chunk_header.total_sz = CHUNK_HEADER_LEN + 4;
-
- ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
- if (ret < 0) {
- return ret;
- }
- out->ops->write(out, &out->crc32, 4);
- if (ret < 0) {
- return ret;
- }
-
- out->chunk_cnt++;
- }
-
- return 0;
-}
-
-static struct sparse_file_ops sparse_file_ops = {
- .write_data_chunk = write_sparse_data_chunk,
- .write_fill_chunk = write_sparse_fill_chunk,
- .write_skip_chunk = write_sparse_skip_chunk,
- .write_end_chunk = write_sparse_end_chunk,
-};
-
-static int write_normal_data_chunk(struct output_file *out, unsigned int len,
- void *data)
-{
- int ret;
- unsigned int rnd_up_len = ALIGN(len, out->block_size);
-
- ret = out->ops->write(out, data, len);
- if (ret < 0) {
- return ret;
- }
-
- if (rnd_up_len > len) {
- ret = out->ops->skip(out, rnd_up_len - len);
- }
-
- return ret;
-}
-
-static int write_normal_fill_chunk(struct output_file *out, unsigned int len,
- uint32_t fill_val)
-{
- int ret;
- unsigned int i;
- unsigned int write_len;
-
- /* Initialize fill_buf with the fill_val */
- for (i = 0; i < out->block_size / sizeof(uint32_t); i++) {
- out->fill_buf[i] = fill_val;
- }
-
- while (len) {
- write_len = min(len, out->block_size);
- ret = out->ops->write(out, out->fill_buf, write_len);
- if (ret < 0) {
- return ret;
- }
-
- len -= write_len;
- }
-
- return 0;
-}
-
-static int write_normal_skip_chunk(struct output_file *out, int64_t len)
-{
- return out->ops->skip(out, len);
-}
-
-int write_normal_end_chunk(struct output_file *out)
-{
- return out->ops->pad(out, out->len);
-}
-
-static struct sparse_file_ops normal_file_ops = {
- .write_data_chunk = write_normal_data_chunk,
- .write_fill_chunk = write_normal_fill_chunk,
- .write_skip_chunk = write_normal_skip_chunk,
- .write_end_chunk = write_normal_end_chunk,
-};
-
-void output_file_close(struct output_file *out)
-{
- out->sparse_ops->write_end_chunk(out);
- out->ops->close(out);
-}
-
-static int output_file_init(struct output_file *out, int block_size,
- int64_t len, bool sparse, int chunks, bool crc)
-{
- int ret;
-
- out->len = len;
- out->block_size = block_size;
- out->cur_out_ptr = 0ll;
- out->chunk_cnt = 0;
- out->crc32 = 0;
- out->use_crc = crc;
-
- out->zero_buf = calloc(block_size, 1);
- if (!out->zero_buf) {
- error_errno("malloc zero_buf");
- return -ENOMEM;
- }
-
- out->fill_buf = calloc(block_size, 1);
- if (!out->fill_buf) {
- error_errno("malloc fill_buf");
- ret = -ENOMEM;
- goto err_fill_buf;
- }
-
- if (sparse) {
- out->sparse_ops = &sparse_file_ops;
- } else {
- out->sparse_ops = &normal_file_ops;
- }
-
- if (sparse) {
- sparse_header_t sparse_header = {
- .magic = SPARSE_HEADER_MAGIC,
- .major_version = SPARSE_HEADER_MAJOR_VER,
- .minor_version = SPARSE_HEADER_MINOR_VER,
- .file_hdr_sz = SPARSE_HEADER_LEN,
- .chunk_hdr_sz = CHUNK_HEADER_LEN,
- .blk_sz = out->block_size,
- .total_blks = DIV_ROUND_UP(out->len, out->block_size),
- .total_chunks = chunks,
- .image_checksum = 0
- };
-
- if (out->use_crc) {
- sparse_header.total_chunks++;
- }
-
- ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
- if (ret < 0) {
- goto err_write;
- }
- }
-
- return 0;
-
-err_write:
- free(out->fill_buf);
-err_fill_buf:
- free(out->zero_buf);
- return ret;
-}
-
-static struct output_file *output_file_new_gz(void)
-{
- struct output_file_gz *outgz = calloc(1, sizeof(struct output_file_gz));
- if (!outgz) {
- error_errno("malloc struct outgz");
- return NULL;
- }
-
- outgz->out.ops = &gz_file_ops;
-
- return &outgz->out;
-}
-
-static struct output_file *output_file_new_normal(void)
-{
- struct output_file_normal *outn = calloc(1, sizeof(struct output_file_normal));
- if (!outn) {
- error_errno("malloc struct outn");
- return NULL;
- }
-
- outn->out.ops = &file_ops;
-
- return &outn->out;
-}
-
-struct output_file *output_file_open_callback(int (*write)(void *, const void *, int),
- void *priv, unsigned int block_size, int64_t len,
- int gz __unused, int sparse, int chunks, int crc)
-{
- int ret;
- struct output_file_callback *outc;
-
- outc = calloc(1, sizeof(struct output_file_callback));
- if (!outc) {
- error_errno("malloc struct outc");
- return NULL;
- }
-
- outc->out.ops = &callback_file_ops;
- outc->priv = priv;
- outc->write = write;
-
- ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
- if (ret < 0) {
- free(outc);
- return NULL;
- }
-
- return &outc->out;
-}
-
-struct output_file *output_file_open_fd(int fd, unsigned int block_size, int64_t len,
- int gz, int sparse, int chunks, int crc)
-{
- int ret;
- struct output_file *out;
-
- if (gz) {
- out = output_file_new_gz();
- } else {
- out = output_file_new_normal();
- }
- if (!out) {
- return NULL;
- }
-
- out->ops->open(out, fd);
-
- ret = output_file_init(out, block_size, len, sparse, chunks, crc);
- if (ret < 0) {
- free(out);
- return NULL;
- }
-
- return out;
-}
-
-/* Write a contiguous region of data blocks from a memory buffer */
-int write_data_chunk(struct output_file *out, unsigned int len, void *data)
-{
- return out->sparse_ops->write_data_chunk(out, len, data);
-}
-
-/* Write a contiguous region of data blocks with a fill value */
-int write_fill_chunk(struct output_file *out, unsigned int len,
- uint32_t fill_val)
-{
- return out->sparse_ops->write_fill_chunk(out, len, fill_val);
-}
-
-int write_fd_chunk(struct output_file *out, unsigned int len,
- int fd, int64_t offset)
-{
- int ret;
- int64_t aligned_offset;
- int aligned_diff;
- uint64_t buffer_size;
- char *ptr;
-
- aligned_offset = offset & ~(4096 - 1);
- aligned_diff = offset - aligned_offset;
- buffer_size = (uint64_t)len + (uint64_t)aligned_diff;
-
-#ifndef _WIN32
- if (buffer_size > SIZE_MAX)
- return -E2BIG;
- char *data = mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, fd,
- aligned_offset);
- if (data == MAP_FAILED) {
- return -errno;
- }
- ptr = data + aligned_diff;
-#else
- off64_t pos;
- char *data = malloc(len);
- if (!data) {
- return -errno;
- }
- pos = lseek64(fd, offset, SEEK_SET);
- if (pos < 0) {
- free(data);
- return -errno;
- }
- ret = read_all(fd, data, len);
- if (ret < 0) {
- free(data);
- return ret;
- }
- ptr = data;
-#endif
-
- ret = out->sparse_ops->write_data_chunk(out, len, ptr);
-
-#ifndef _WIN32
- munmap(data, buffer_size);
-#else
- free(data);
-#endif
-
- return ret;
-}
-
-/* Write a contiguous region of data blocks from a file */
-int write_file_chunk(struct output_file *out, unsigned int len,
- const char *file, int64_t offset)
-{
- int ret;
-
- int file_fd = open(file, O_RDONLY | O_BINARY);
- if (file_fd < 0) {
- return -errno;
- }
-
- ret = write_fd_chunk(out, len, file_fd, offset);
-
- close(file_fd);
-
- return ret;
-}
-
-int write_skip_chunk(struct output_file *out, int64_t len)
-{
- return out->sparse_ops->write_skip_chunk(out, len);
-}
diff --git a/libsparse/output_file.cpp b/libsparse/output_file.cpp
new file mode 100644
index 0000000..fe314b3
--- /dev/null
+++ b/libsparse/output_file.cpp
@@ -0,0 +1,720 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+#define _FILE_OFFSET_BITS 64
+#define _LARGEFILE64_SOURCE 1
+
+#include <fcntl.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <zlib.h>
+
+#include "defs.h"
+#include "output_file.h"
+#include "sparse_crc32.h"
+#include "sparse_format.h"
+
+#ifndef _WIN32
+#include <sys/mman.h>
+#define O_BINARY 0
+#else
+#define ftruncate64 ftruncate
+#endif
+
+#if defined(__APPLE__) && defined(__MACH__)
+#define lseek64 lseek
+#define ftruncate64 ftruncate
+#define mmap64 mmap
+#define off64_t off_t
+#endif
+
+#define min(a, b) \
+ ({ \
+ typeof(a) _a = (a); \
+ typeof(b) _b = (b); \
+ (_a < _b) ? _a : _b; \
+ })
+
+#define SPARSE_HEADER_MAJOR_VER 1
+#define SPARSE_HEADER_MINOR_VER 0
+#define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
+#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
+
+#define container_of(inner, outer_t, elem) ((outer_t*)((char*)(inner)-offsetof(outer_t, elem)))
+
+struct output_file_ops {
+ int (*open)(struct output_file*, int fd);
+ int (*skip)(struct output_file*, int64_t);
+ int (*pad)(struct output_file*, int64_t);
+ int (*write)(struct output_file*, void*, size_t);
+ void (*close)(struct output_file*);
+};
+
+struct sparse_file_ops {
+ int (*write_data_chunk)(struct output_file* out, unsigned int len, void* data);
+ int (*write_fill_chunk)(struct output_file* out, unsigned int len, uint32_t fill_val);
+ int (*write_skip_chunk)(struct output_file* out, int64_t len);
+ int (*write_end_chunk)(struct output_file* out);
+};
+
+struct output_file {
+ int64_t cur_out_ptr;
+ unsigned int chunk_cnt;
+ uint32_t crc32;
+ struct output_file_ops* ops;
+ struct sparse_file_ops* sparse_ops;
+ int use_crc;
+ unsigned int block_size;
+ int64_t len;
+ char* zero_buf;
+ uint32_t* fill_buf;
+ char* buf;
+};
+
+struct output_file_gz {
+ struct output_file out;
+ gzFile gz_fd;
+};
+
+#define to_output_file_gz(_o) container_of((_o), struct output_file_gz, out)
+
+struct output_file_normal {
+ struct output_file out;
+ int fd;
+};
+
+#define to_output_file_normal(_o) container_of((_o), struct output_file_normal, out)
+
+struct output_file_callback {
+ struct output_file out;
+ void* priv;
+ int (*write)(void* priv, const void* buf, size_t len);
+};
+
+#define to_output_file_callback(_o) container_of((_o), struct output_file_callback, out)
+
+static int file_open(struct output_file* out, int fd) {
+ struct output_file_normal* outn = to_output_file_normal(out);
+
+ outn->fd = fd;
+ return 0;
+}
+
+static int file_skip(struct output_file* out, int64_t cnt) {
+ off64_t ret;
+ struct output_file_normal* outn = to_output_file_normal(out);
+
+ ret = lseek64(outn->fd, cnt, SEEK_CUR);
+ if (ret < 0) {
+ error_errno("lseek64");
+ return -1;
+ }
+ return 0;
+}
+
+static int file_pad(struct output_file* out, int64_t len) {
+ int ret;
+ struct output_file_normal* outn = to_output_file_normal(out);
+
+ ret = ftruncate64(outn->fd, len);
+ if (ret < 0) {
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int file_write(struct output_file* out, void* data, size_t len) {
+ ssize_t ret;
+ struct output_file_normal* outn = to_output_file_normal(out);
+
+ while (len > 0) {
+ ret = write(outn->fd, data, len);
+ if (ret < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ error_errno("write");
+ return -1;
+ }
+
+ data = (char*)data + ret;
+ len -= ret;
+ }
+
+ return 0;
+}
+
+static void file_close(struct output_file* out) {
+ struct output_file_normal* outn = to_output_file_normal(out);
+
+ free(outn);
+}
+
+static struct output_file_ops file_ops = {
+ .open = file_open,
+ .skip = file_skip,
+ .pad = file_pad,
+ .write = file_write,
+ .close = file_close,
+};
+
+static int gz_file_open(struct output_file* out, int fd) {
+ struct output_file_gz* outgz = to_output_file_gz(out);
+
+ outgz->gz_fd = gzdopen(fd, "wb9");
+ if (!outgz->gz_fd) {
+ error_errno("gzopen");
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int gz_file_skip(struct output_file* out, int64_t cnt) {
+ off64_t ret;
+ struct output_file_gz* outgz = to_output_file_gz(out);
+
+ ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR);
+ if (ret < 0) {
+ error_errno("gzseek");
+ return -1;
+ }
+ return 0;
+}
+
+static int gz_file_pad(struct output_file* out, int64_t len) {
+ off64_t ret;
+ struct output_file_gz* outgz = to_output_file_gz(out);
+
+ ret = gztell(outgz->gz_fd);
+ if (ret < 0) {
+ return -1;
+ }
+
+ if (ret >= len) {
+ return 0;
+ }
+
+ ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET);
+ if (ret < 0) {
+ return -1;
+ }
+
+ gzwrite(outgz->gz_fd, "", 1);
+
+ return 0;
+}
+
+static int gz_file_write(struct output_file* out, void* data, size_t len) {
+ int ret;
+ struct output_file_gz* outgz = to_output_file_gz(out);
+
+ while (len > 0) {
+ ret = gzwrite(outgz->gz_fd, data, min(len, (unsigned int)INT_MAX));
+ if (ret == 0) {
+ error("gzwrite %s", gzerror(outgz->gz_fd, nullptr));
+ return -1;
+ }
+ len -= ret;
+ data = (char*)data + ret;
+ }
+
+ return 0;
+}
+
+static void gz_file_close(struct output_file* out) {
+ struct output_file_gz* outgz = to_output_file_gz(out);
+
+ gzclose(outgz->gz_fd);
+ free(outgz);
+}
+
+static struct output_file_ops gz_file_ops = {
+ .open = gz_file_open,
+ .skip = gz_file_skip,
+ .pad = gz_file_pad,
+ .write = gz_file_write,
+ .close = gz_file_close,
+};
+
+static int callback_file_open(struct output_file* out __unused, int fd __unused) {
+ return 0;
+}
+
+static int callback_file_skip(struct output_file* out, int64_t off) {
+ struct output_file_callback* outc = to_output_file_callback(out);
+ int to_write;
+ int ret;
+
+ while (off > 0) {
+ to_write = min(off, (int64_t)INT_MAX);
+ ret = outc->write(outc->priv, nullptr, to_write);
+ if (ret < 0) {
+ return ret;
+ }
+ off -= to_write;
+ }
+
+ return 0;
+}
+
+static int callback_file_pad(struct output_file* out __unused, int64_t len __unused) {
+ return -1;
+}
+
+static int callback_file_write(struct output_file* out, void* data, size_t len) {
+ struct output_file_callback* outc = to_output_file_callback(out);
+
+ return outc->write(outc->priv, data, len);
+}
+
+static void callback_file_close(struct output_file* out) {
+ struct output_file_callback* outc = to_output_file_callback(out);
+
+ free(outc);
+}
+
+static struct output_file_ops callback_file_ops = {
+ .open = callback_file_open,
+ .skip = callback_file_skip,
+ .pad = callback_file_pad,
+ .write = callback_file_write,
+ .close = callback_file_close,
+};
+
+int read_all(int fd, void* buf, size_t len) {
+ size_t total = 0;
+ int ret;
+ char* ptr = reinterpret_cast<char*>(buf);
+
+ while (total < len) {
+ ret = read(fd, ptr, len - total);
+
+ if (ret < 0) return -errno;
+
+ if (ret == 0) return -EINVAL;
+
+ ptr += ret;
+ total += ret;
+ }
+
+ return 0;
+}
+
+static int write_sparse_skip_chunk(struct output_file* out, int64_t skip_len) {
+ chunk_header_t chunk_header;
+ int ret;
+
+ if (skip_len % out->block_size) {
+ error("don't care size %" PRIi64 " is not a multiple of the block size %u", skip_len,
+ out->block_size);
+ return -1;
+ }
+
+ /* We are skipping data, so emit a don't care chunk. */
+ chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE;
+ chunk_header.reserved1 = 0;
+ chunk_header.chunk_sz = skip_len / out->block_size;
+ chunk_header.total_sz = CHUNK_HEADER_LEN;
+ ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
+ if (ret < 0) return -1;
+
+ out->cur_out_ptr += skip_len;
+ out->chunk_cnt++;
+
+ return 0;
+}
+
+static int write_sparse_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
+ chunk_header_t chunk_header;
+ int rnd_up_len, count;
+ int ret;
+
+ /* Round up the fill length to a multiple of the block size */
+ rnd_up_len = ALIGN(len, out->block_size);
+
+ /* Finally we can safely emit a chunk of data */
+ chunk_header.chunk_type = CHUNK_TYPE_FILL;
+ chunk_header.reserved1 = 0;
+ chunk_header.chunk_sz = rnd_up_len / out->block_size;
+ chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val);
+ ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
+
+ if (ret < 0) return -1;
+ ret = out->ops->write(out, &fill_val, sizeof(fill_val));
+ if (ret < 0) return -1;
+
+ if (out->use_crc) {
+ count = out->block_size / sizeof(uint32_t);
+ while (count--) out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t));
+ }
+
+ out->cur_out_ptr += rnd_up_len;
+ out->chunk_cnt++;
+
+ return 0;
+}
+
+static int write_sparse_data_chunk(struct output_file* out, unsigned int len, void* data) {
+ chunk_header_t chunk_header;
+ int 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;
+ ret = out->ops->write(out, data, len);
+ if (ret < 0) return -1;
+ if (zero_len) {
+ ret = out->ops->write(out, out->zero_buf, zero_len);
+ if (ret < 0) return -1;
+ }
+
+ if (out->use_crc) {
+ out->crc32 = sparse_crc32(out->crc32, data, len);
+ if (zero_len) out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_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;
+
+ if (out->use_crc) {
+ chunk_header.chunk_type = CHUNK_TYPE_CRC32;
+ chunk_header.reserved1 = 0;
+ chunk_header.chunk_sz = 0;
+ chunk_header.total_sz = CHUNK_HEADER_LEN + 4;
+
+ ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
+ if (ret < 0) {
+ return ret;
+ }
+ out->ops->write(out, &out->crc32, 4);
+ if (ret < 0) {
+ return ret;
+ }
+
+ out->chunk_cnt++;
+ }
+
+ return 0;
+}
+
+static struct sparse_file_ops sparse_file_ops = {
+ .write_data_chunk = write_sparse_data_chunk,
+ .write_fill_chunk = write_sparse_fill_chunk,
+ .write_skip_chunk = write_sparse_skip_chunk,
+ .write_end_chunk = write_sparse_end_chunk,
+};
+
+static int write_normal_data_chunk(struct output_file* out, unsigned int len, void* data) {
+ int ret;
+ unsigned int rnd_up_len = ALIGN(len, out->block_size);
+
+ ret = out->ops->write(out, data, len);
+ if (ret < 0) {
+ return ret;
+ }
+
+ if (rnd_up_len > len) {
+ ret = out->ops->skip(out, rnd_up_len - len);
+ }
+
+ return ret;
+}
+
+static int write_normal_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
+ int ret;
+ unsigned int i;
+ unsigned int write_len;
+
+ /* Initialize fill_buf with the fill_val */
+ for (i = 0; i < out->block_size / sizeof(uint32_t); i++) {
+ out->fill_buf[i] = fill_val;
+ }
+
+ while (len) {
+ write_len = min(len, out->block_size);
+ ret = out->ops->write(out, out->fill_buf, write_len);
+ if (ret < 0) {
+ return ret;
+ }
+
+ len -= write_len;
+ }
+
+ return 0;
+}
+
+static int write_normal_skip_chunk(struct output_file* out, int64_t len) {
+ return out->ops->skip(out, len);
+}
+
+int write_normal_end_chunk(struct output_file* out) {
+ return out->ops->pad(out, out->len);
+}
+
+static struct sparse_file_ops normal_file_ops = {
+ .write_data_chunk = write_normal_data_chunk,
+ .write_fill_chunk = write_normal_fill_chunk,
+ .write_skip_chunk = write_normal_skip_chunk,
+ .write_end_chunk = write_normal_end_chunk,
+};
+
+void output_file_close(struct output_file* out) {
+ out->sparse_ops->write_end_chunk(out);
+ out->ops->close(out);
+}
+
+static int output_file_init(struct output_file* out, int block_size, int64_t len, bool sparse,
+ int chunks, bool crc) {
+ int ret;
+
+ out->len = len;
+ out->block_size = block_size;
+ out->cur_out_ptr = 0ll;
+ out->chunk_cnt = 0;
+ out->crc32 = 0;
+ out->use_crc = crc;
+
+ out->zero_buf = reinterpret_cast<char*>(calloc(block_size, 1));
+ if (!out->zero_buf) {
+ error_errno("malloc zero_buf");
+ return -ENOMEM;
+ }
+
+ out->fill_buf = reinterpret_cast<uint32_t*>(calloc(block_size, 1));
+ if (!out->fill_buf) {
+ error_errno("malloc fill_buf");
+ ret = -ENOMEM;
+ goto err_fill_buf;
+ }
+
+ if (sparse) {
+ out->sparse_ops = &sparse_file_ops;
+ } else {
+ out->sparse_ops = &normal_file_ops;
+ }
+
+ if (sparse) {
+ sparse_header_t sparse_header = {
+ .magic = SPARSE_HEADER_MAGIC,
+ .major_version = SPARSE_HEADER_MAJOR_VER,
+ .minor_version = SPARSE_HEADER_MINOR_VER,
+ .file_hdr_sz = SPARSE_HEADER_LEN,
+ .chunk_hdr_sz = CHUNK_HEADER_LEN,
+ .blk_sz = out->block_size,
+ .total_blks = static_cast<unsigned>(DIV_ROUND_UP(out->len, out->block_size)),
+ .total_chunks = static_cast<unsigned>(chunks),
+ .image_checksum = 0};
+
+ if (out->use_crc) {
+ sparse_header.total_chunks++;
+ }
+
+ ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
+ if (ret < 0) {
+ goto err_write;
+ }
+ }
+
+ return 0;
+
+err_write:
+ free(out->fill_buf);
+err_fill_buf:
+ free(out->zero_buf);
+ return ret;
+}
+
+static struct output_file* output_file_new_gz(void) {
+ struct output_file_gz* outgz =
+ reinterpret_cast<struct output_file_gz*>(calloc(1, sizeof(struct output_file_gz)));
+ if (!outgz) {
+ error_errno("malloc struct outgz");
+ return nullptr;
+ }
+
+ outgz->out.ops = &gz_file_ops;
+
+ return &outgz->out;
+}
+
+static struct output_file* output_file_new_normal(void) {
+ struct output_file_normal* outn =
+ reinterpret_cast<struct output_file_normal*>(calloc(1, sizeof(struct output_file_normal)));
+ if (!outn) {
+ error_errno("malloc struct outn");
+ return nullptr;
+ }
+
+ outn->out.ops = &file_ops;
+
+ return &outn->out;
+}
+
+struct output_file* output_file_open_callback(int (*write)(void*, const void*, size_t), void* priv,
+ unsigned int block_size, int64_t len, int gz __unused,
+ int sparse, int chunks, int crc) {
+ int ret;
+ struct output_file_callback* outc;
+
+ outc =
+ reinterpret_cast<struct output_file_callback*>(calloc(1, sizeof(struct output_file_callback)));
+ if (!outc) {
+ error_errno("malloc struct outc");
+ return nullptr;
+ }
+
+ outc->out.ops = &callback_file_ops;
+ outc->priv = priv;
+ outc->write = write;
+
+ ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
+ if (ret < 0) {
+ free(outc);
+ return nullptr;
+ }
+
+ return &outc->out;
+}
+
+struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t len, int gz,
+ int sparse, int chunks, int crc) {
+ int ret;
+ struct output_file* out;
+
+ if (gz) {
+ out = output_file_new_gz();
+ } else {
+ out = output_file_new_normal();
+ }
+ if (!out) {
+ return nullptr;
+ }
+
+ out->ops->open(out, fd);
+
+ ret = output_file_init(out, block_size, len, sparse, chunks, crc);
+ if (ret < 0) {
+ free(out);
+ return nullptr;
+ }
+
+ return out;
+}
+
+/* Write a contiguous region of data blocks from a memory buffer */
+int write_data_chunk(struct output_file* out, unsigned int len, void* data) {
+ return out->sparse_ops->write_data_chunk(out, len, data);
+}
+
+/* Write a contiguous region of data blocks with a fill value */
+int write_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
+ return out->sparse_ops->write_fill_chunk(out, len, fill_val);
+}
+
+int write_fd_chunk(struct output_file* out, unsigned int len, int fd, int64_t offset) {
+ int ret;
+ int64_t aligned_offset;
+ int aligned_diff;
+ uint64_t buffer_size;
+ char* ptr;
+
+ aligned_offset = offset & ~(4096 - 1);
+ aligned_diff = offset - aligned_offset;
+ buffer_size = (uint64_t)len + (uint64_t)aligned_diff;
+
+#ifndef _WIN32
+ if (buffer_size > SIZE_MAX) return -E2BIG;
+ char* data =
+ reinterpret_cast<char*>(mmap64(nullptr, buffer_size, PROT_READ, MAP_SHARED, fd, aligned_offset));
+ if (data == MAP_FAILED) {
+ return -errno;
+ }
+ ptr = data + aligned_diff;
+#else
+ off64_t pos;
+ char* data = reinterpret_cast<char*>(malloc(len));
+ if (!data) {
+ return -errno;
+ }
+ pos = lseek64(fd, offset, SEEK_SET);
+ if (pos < 0) {
+ free(data);
+ return -errno;
+ }
+ ret = read_all(fd, data, len);
+ if (ret < 0) {
+ free(data);
+ return ret;
+ }
+ ptr = data;
+#endif
+
+ ret = out->sparse_ops->write_data_chunk(out, len, ptr);
+
+#ifndef _WIN32
+ munmap(data, buffer_size);
+#else
+ free(data);
+#endif
+
+ return ret;
+}
+
+/* Write a contiguous region of data blocks from a file */
+int write_file_chunk(struct output_file* out, unsigned int len, const char* file, int64_t offset) {
+ int ret;
+
+ int file_fd = open(file, O_RDONLY | O_BINARY);
+ if (file_fd < 0) {
+ return -errno;
+ }
+
+ ret = write_fd_chunk(out, len, file_fd, offset);
+
+ close(file_fd);
+
+ return ret;
+}
+
+int write_skip_chunk(struct output_file* out, int64_t len) {
+ return out->sparse_ops->write_skip_chunk(out, len);
+}
diff --git a/libsparse/output_file.h b/libsparse/output_file.h
index b67e94e..278430b 100644
--- a/libsparse/output_file.h
+++ b/libsparse/output_file.h
@@ -25,22 +25,19 @@
struct output_file;
-struct output_file *output_file_open_fd(int fd, unsigned int block_size, int64_t len,
- int gz, int sparse, int chunks, int crc);
-struct output_file *output_file_open_callback(int (*write)(void *, const void *, int),
- void *priv, unsigned int block_size, int64_t len, int gz, int sparse,
- int chunks, int crc);
-int write_data_chunk(struct output_file *out, unsigned int len, void *data);
-int write_fill_chunk(struct output_file *out, unsigned int len,
- uint32_t fill_val);
-int write_file_chunk(struct output_file *out, unsigned int len,
- const char *file, int64_t offset);
-int write_fd_chunk(struct output_file *out, unsigned int len,
- int fd, int64_t offset);
-int write_skip_chunk(struct output_file *out, int64_t len);
-void output_file_close(struct output_file *out);
+struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t len, int gz,
+ int sparse, int chunks, int crc);
+struct output_file* output_file_open_callback(int (*write)(void*, const void*, size_t), void* priv,
+ unsigned int block_size, int64_t len, int gz,
+ int sparse, int chunks, int crc);
+int write_data_chunk(struct output_file* out, unsigned int len, void* data);
+int write_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val);
+int write_file_chunk(struct output_file* out, unsigned int len, const char* file, int64_t offset);
+int write_fd_chunk(struct output_file* out, unsigned int len, int fd, int64_t offset);
+int write_skip_chunk(struct output_file* out, int64_t len);
+void output_file_close(struct output_file* out);
-int read_all(int fd, void *buf, size_t len);
+int read_all(int fd, void* buf, size_t len);
#ifdef __cplusplus
}
diff --git a/libsparse/simg2img.c b/libsparse/simg2img.c
deleted file mode 100644
index b9b438e..0000000
--- a/libsparse/simg2img.c
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright (C) 2010 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 <sparse/sparse.h>
-
-#include <fcntl.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif
-
-void usage()
-{
- fprintf(stderr, "Usage: simg2img <sparse_image_files> <raw_image_file>\n");
-}
-
-int main(int argc, char *argv[])
-{
- int in;
- int out;
- int i;
- struct sparse_file *s;
-
- if (argc < 3) {
- usage();
- exit(-1);
- }
-
- out = open(argv[argc - 1], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
- if (out < 0) {
- fprintf(stderr, "Cannot open output file %s\n", argv[argc - 1]);
- exit(-1);
- }
-
- for (i = 1; i < argc - 1; i++) {
- if (strcmp(argv[i], "-") == 0) {
- in = STDIN_FILENO;
- } else {
- in = open(argv[i], O_RDONLY | O_BINARY);
- if (in < 0) {
- fprintf(stderr, "Cannot open input file %s\n", argv[i]);
- exit(-1);
- }
- }
-
- s = sparse_file_import(in, true, false);
- if (!s) {
- fprintf(stderr, "Failed to read sparse file\n");
- exit(-1);
- }
-
- if (lseek(out, 0, SEEK_SET) == -1) {
- perror("lseek failed");
- exit(EXIT_FAILURE);
- }
-
- if (sparse_file_write(s, out, false, false, false) < 0) {
- fprintf(stderr, "Cannot write output file\n");
- exit(-1);
- }
- sparse_file_destroy(s);
- close(in);
- }
-
- close(out);
-
- exit(0);
-}
-
diff --git a/libsparse/simg2img.cpp b/libsparse/simg2img.cpp
new file mode 100644
index 0000000..8ba5f69
--- /dev/null
+++ b/libsparse/simg2img.cpp
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2010 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 <sparse/sparse.h>
+
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+void usage() {
+ fprintf(stderr, "Usage: simg2img <sparse_image_files> <raw_image_file>\n");
+}
+
+int main(int argc, char* argv[]) {
+ int in;
+ int out;
+ int i;
+ struct sparse_file* s;
+
+ if (argc < 3) {
+ usage();
+ exit(-1);
+ }
+
+ out = open(argv[argc - 1], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
+ if (out < 0) {
+ fprintf(stderr, "Cannot open output file %s\n", argv[argc - 1]);
+ exit(-1);
+ }
+
+ for (i = 1; i < argc - 1; i++) {
+ if (strcmp(argv[i], "-") == 0) {
+ in = STDIN_FILENO;
+ } else {
+ in = open(argv[i], O_RDONLY | O_BINARY);
+ if (in < 0) {
+ fprintf(stderr, "Cannot open input file %s\n", argv[i]);
+ exit(-1);
+ }
+ }
+
+ s = sparse_file_import(in, true, false);
+ if (!s) {
+ fprintf(stderr, "Failed to read sparse file\n");
+ exit(-1);
+ }
+
+ if (lseek(out, 0, SEEK_SET) == -1) {
+ perror("lseek failed");
+ exit(EXIT_FAILURE);
+ }
+
+ if (sparse_file_write(s, out, false, false, false) < 0) {
+ fprintf(stderr, "Cannot write output file\n");
+ exit(-1);
+ }
+ sparse_file_destroy(s);
+ close(in);
+ }
+
+ close(out);
+
+ exit(0);
+}
diff --git a/libsparse/simg2simg.c b/libsparse/simg2simg.c
deleted file mode 100644
index 5f9ccf6..0000000
--- a/libsparse/simg2simg.c
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Copyright (C) 2012 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.
- */
-
-#define _FILE_OFFSET_BITS 64
-#define _LARGEFILE64_SOURCE 1
-#define _GNU_SOURCE
-
-#include <fcntl.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <sparse/sparse.h>
-
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif
-
-void usage()
-{
- fprintf(stderr, "Usage: simg2simg <sparse image file> <sparse_image_file> <max_size>\n");
-}
-
-int main(int argc, char *argv[])
-{
- int in;
- int out;
- int i;
- int ret;
- struct sparse_file *s;
- int64_t max_size;
- struct sparse_file **out_s;
- int files;
- char filename[4096];
-
- if (argc != 4) {
- usage();
- exit(-1);
- }
-
- max_size = atoll(argv[3]);
-
- in = open(argv[1], O_RDONLY | O_BINARY);
- if (in < 0) {
- fprintf(stderr, "Cannot open input file %s\n", argv[1]);
- exit(-1);
- }
-
- s = sparse_file_import(in, true, false);
- if (!s) {
- fprintf(stderr, "Failed to import sparse file\n");
- exit(-1);
- }
-
- files = sparse_file_resparse(s, max_size, NULL, 0);
- if (files < 0) {
- fprintf(stderr, "Failed to resparse\n");
- exit(-1);
- }
-
- out_s = calloc(sizeof(struct sparse_file *), files);
- if (!out_s) {
- fprintf(stderr, "Failed to allocate sparse file array\n");
- exit(-1);
- }
-
- files = sparse_file_resparse(s, max_size, out_s, files);
- if (files < 0) {
- fprintf(stderr, "Failed to resparse\n");
- exit(-1);
- }
-
- for (i = 0; i < files; i++) {
- ret = snprintf(filename, sizeof(filename), "%s.%d", argv[2], i);
- if (ret >= (int)sizeof(filename)) {
- fprintf(stderr, "Filename too long\n");
- exit(-1);
- }
-
- out = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
- if (out < 0) {
- fprintf(stderr, "Cannot open output file %s\n", argv[2]);
- exit(-1);
- }
-
- ret = sparse_file_write(out_s[i], out, false, true, false);
- if (ret) {
- fprintf(stderr, "Failed to write sparse file\n");
- exit(-1);
- }
- close(out);
- }
-
- close(in);
-
- exit(0);
-}
diff --git a/libsparse/simg2simg.cpp b/libsparse/simg2simg.cpp
new file mode 100644
index 0000000..a2c296e
--- /dev/null
+++ b/libsparse/simg2simg.cpp
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+#define _FILE_OFFSET_BITS 64
+#define _LARGEFILE64_SOURCE 1
+
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <sparse/sparse.h>
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+void usage() {
+ fprintf(stderr, "Usage: simg2simg <sparse image file> <sparse_image_file> <max_size>\n");
+}
+
+int main(int argc, char* argv[]) {
+ int in;
+ int out;
+ int i;
+ int ret;
+ struct sparse_file* s;
+ int64_t max_size;
+ struct sparse_file** out_s;
+ int files;
+ char filename[4096];
+
+ if (argc != 4) {
+ usage();
+ exit(-1);
+ }
+
+ max_size = atoll(argv[3]);
+
+ in = open(argv[1], O_RDONLY | O_BINARY);
+ if (in < 0) {
+ fprintf(stderr, "Cannot open input file %s\n", argv[1]);
+ exit(-1);
+ }
+
+ s = sparse_file_import(in, true, false);
+ if (!s) {
+ fprintf(stderr, "Failed to import sparse file\n");
+ exit(-1);
+ }
+
+ files = sparse_file_resparse(s, max_size, nullptr, 0);
+ if (files < 0) {
+ fprintf(stderr, "Failed to resparse\n");
+ exit(-1);
+ }
+
+ out_s = calloc(sizeof(struct sparse_file*), files);
+ if (!out_s) {
+ fprintf(stderr, "Failed to allocate sparse file array\n");
+ exit(-1);
+ }
+
+ files = sparse_file_resparse(s, max_size, out_s, files);
+ if (files < 0) {
+ fprintf(stderr, "Failed to resparse\n");
+ exit(-1);
+ }
+
+ for (i = 0; i < files; i++) {
+ ret = snprintf(filename, sizeof(filename), "%s.%d", argv[2], i);
+ if (ret >= (int)sizeof(filename)) {
+ fprintf(stderr, "Filename too long\n");
+ exit(-1);
+ }
+
+ out = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
+ if (out < 0) {
+ fprintf(stderr, "Cannot open output file %s\n", argv[2]);
+ exit(-1);
+ }
+
+ ret = sparse_file_write(out_s[i], out, false, true, false);
+ if (ret) {
+ fprintf(stderr, "Failed to write sparse file\n");
+ exit(-1);
+ }
+ close(out);
+ }
+
+ close(in);
+
+ exit(0);
+}
diff --git a/libsparse/sparse.c b/libsparse/sparse.c
deleted file mode 100644
index b175860..0000000
--- a/libsparse/sparse.c
+++ /dev/null
@@ -1,395 +0,0 @@
-/*
- * Copyright (C) 2012 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 <assert.h>
-#include <stdlib.h>
-
-#include <sparse/sparse.h>
-
-#include "defs.h"
-#include "sparse_file.h"
-
-#include "output_file.h"
-#include "backed_block.h"
-#include "sparse_defs.h"
-#include "sparse_format.h"
-
-struct sparse_file *sparse_file_new(unsigned int block_size, int64_t len)
-{
- struct sparse_file *s = calloc(sizeof(struct sparse_file), 1);
- if (!s) {
- return NULL;
- }
-
- s->backed_block_list = backed_block_list_new(block_size);
- if (!s->backed_block_list) {
- free(s);
- return NULL;
- }
-
- s->block_size = block_size;
- s->len = len;
-
- return s;
-}
-
-void sparse_file_destroy(struct sparse_file *s)
-{
- backed_block_list_destroy(s->backed_block_list);
- free(s);
-}
-
-int sparse_file_add_data(struct sparse_file *s,
- void *data, unsigned int len, unsigned int block)
-{
- return backed_block_add_data(s->backed_block_list, data, len, block);
-}
-
-int sparse_file_add_fill(struct sparse_file *s,
- uint32_t fill_val, unsigned int len, unsigned int block)
-{
- return backed_block_add_fill(s->backed_block_list, fill_val, len, block);
-}
-
-int sparse_file_add_file(struct sparse_file *s,
- const char *filename, int64_t file_offset, unsigned int len,
- unsigned int block)
-{
- return backed_block_add_file(s->backed_block_list, filename, file_offset,
- len, block);
-}
-
-int sparse_file_add_fd(struct sparse_file *s,
- int fd, int64_t file_offset, unsigned int len, unsigned int block)
-{
- return backed_block_add_fd(s->backed_block_list, fd, file_offset,
- len, block);
-}
-unsigned int sparse_count_chunks(struct sparse_file *s)
-{
- struct backed_block *bb;
- unsigned int last_block = 0;
- unsigned int chunks = 0;
-
- for (bb = backed_block_iter_new(s->backed_block_list); bb;
- bb = backed_block_iter_next(bb)) {
- if (backed_block_block(bb) > last_block) {
- /* If there is a gap between chunks, add a skip chunk */
- chunks++;
- }
- chunks++;
- last_block = backed_block_block(bb) +
- DIV_ROUND_UP(backed_block_len(bb), s->block_size);
- }
- if (last_block < DIV_ROUND_UP(s->len, s->block_size)) {
- chunks++;
- }
-
- return chunks;
-}
-
-static int sparse_file_write_block(struct output_file *out,
- struct backed_block *bb)
-{
- int ret = -EINVAL;
-
- switch (backed_block_type(bb)) {
- case BACKED_BLOCK_DATA:
- ret = write_data_chunk(out, backed_block_len(bb), backed_block_data(bb));
- break;
- case BACKED_BLOCK_FILE:
- ret = write_file_chunk(out, backed_block_len(bb),
- backed_block_filename(bb),
- backed_block_file_offset(bb));
- break;
- case BACKED_BLOCK_FD:
- ret = write_fd_chunk(out, backed_block_len(bb),
- backed_block_fd(bb),
- backed_block_file_offset(bb));
- break;
- case BACKED_BLOCK_FILL:
- ret = write_fill_chunk(out, backed_block_len(bb),
- backed_block_fill_val(bb));
- break;
- }
-
- return ret;
-}
-
-static int write_all_blocks(struct sparse_file *s, struct output_file *out)
-{
- struct backed_block *bb;
- unsigned int last_block = 0;
- int64_t pad;
- int ret = 0;
-
- for (bb = backed_block_iter_new(s->backed_block_list); bb;
- bb = backed_block_iter_next(bb)) {
- if (backed_block_block(bb) > last_block) {
- unsigned int blocks = backed_block_block(bb) - last_block;
- write_skip_chunk(out, (int64_t)blocks * s->block_size);
- }
- ret = sparse_file_write_block(out, bb);
- if (ret)
- return ret;
- last_block = backed_block_block(bb) +
- DIV_ROUND_UP(backed_block_len(bb), s->block_size);
- }
-
- pad = s->len - (int64_t)last_block * s->block_size;
- assert(pad >= 0);
- if (pad > 0) {
- write_skip_chunk(out, pad);
- }
-
- return 0;
-}
-
-int sparse_file_write(struct sparse_file *s, int fd, bool gz, bool sparse,
- bool crc)
-{
- int ret;
- int chunks;
- struct output_file *out;
-
- chunks = sparse_count_chunks(s);
- out = output_file_open_fd(fd, s->block_size, s->len, gz, sparse, chunks, crc);
-
- if (!out)
- return -ENOMEM;
-
- ret = write_all_blocks(s, out);
-
- output_file_close(out);
-
- return ret;
-}
-
-int sparse_file_callback(struct sparse_file *s, bool sparse, bool crc,
- int (*write)(void *priv, const void *data, int len), void *priv)
-{
- int ret;
- int chunks;
- struct output_file *out;
-
- chunks = sparse_count_chunks(s);
- out = output_file_open_callback(write, priv, s->block_size, s->len, false,
- sparse, chunks, crc);
-
- if (!out)
- return -ENOMEM;
-
- ret = write_all_blocks(s, out);
-
- output_file_close(out);
-
- return ret;
-}
-
-struct chunk_data {
- void *priv;
- unsigned int block;
- unsigned int nr_blocks;
- int (*write)(void *priv, const void *data, int len, unsigned int block,
- unsigned int nr_blocks);
-};
-
-static int foreach_chunk_write(void *priv, const void *data, int len)
-{
- struct chunk_data *chk = priv;
-
- return chk->write(chk->priv, data, len, chk->block, chk->nr_blocks);
-}
-
-int sparse_file_foreach_chunk(struct sparse_file *s, bool sparse, bool crc,
- int (*write)(void *priv, const void *data, int len, unsigned int block,
- unsigned int nr_blocks),
- void *priv)
-{
- int ret;
- int chunks;
- struct chunk_data chk;
- struct output_file *out;
- struct backed_block *bb;
-
- chk.priv = priv;
- chk.write = write;
- chk.block = chk.nr_blocks = 0;
- chunks = sparse_count_chunks(s);
- out = output_file_open_callback(foreach_chunk_write, &chk,
- s->block_size, s->len, false, sparse,
- chunks, crc);
-
- if (!out)
- return -ENOMEM;
-
- for (bb = backed_block_iter_new(s->backed_block_list); bb;
- bb = backed_block_iter_next(bb)) {
- chk.block = backed_block_block(bb);
- chk.nr_blocks = (backed_block_len(bb) - 1) / s->block_size + 1;
- ret = sparse_file_write_block(out, bb);
- if (ret)
- return ret;
- }
-
- output_file_close(out);
-
- return ret;
-}
-
-static int out_counter_write(void *priv, const void *data __unused, int len)
-{
- int64_t *count = priv;
- *count += len;
- return 0;
-}
-
-int64_t sparse_file_len(struct sparse_file *s, bool sparse, bool crc)
-{
- int ret;
- int chunks = sparse_count_chunks(s);
- int64_t count = 0;
- struct output_file *out;
-
- out = output_file_open_callback(out_counter_write, &count,
- s->block_size, s->len, false, sparse, chunks, crc);
- if (!out) {
- return -1;
- }
-
- ret = write_all_blocks(s, out);
-
- output_file_close(out);
-
- if (ret < 0) {
- return -1;
- }
-
- return count;
-}
-
-unsigned int sparse_file_block_size(struct sparse_file *s)
-{
- return s->block_size;
-}
-
-static struct backed_block *move_chunks_up_to_len(struct sparse_file *from,
- struct sparse_file *to, unsigned int len)
-{
- int64_t count = 0;
- struct output_file *out_counter;
- struct backed_block *last_bb = NULL;
- struct backed_block *bb;
- struct backed_block *start;
- unsigned int last_block = 0;
- int64_t file_len = 0;
- int ret;
-
- /*
- * overhead is sparse file header, the potential end skip
- * chunk and crc chunk.
- */
- int overhead = sizeof(sparse_header_t) + 2 * sizeof(chunk_header_t) +
- sizeof(uint32_t);
- len -= overhead;
-
- start = backed_block_iter_new(from->backed_block_list);
- out_counter = output_file_open_callback(out_counter_write, &count,
- to->block_size, to->len, false, true, 0, false);
- if (!out_counter) {
- return NULL;
- }
-
- for (bb = start; bb; bb = backed_block_iter_next(bb)) {
- count = 0;
- if (backed_block_block(bb) > last_block)
- count += sizeof(chunk_header_t);
- last_block = backed_block_block(bb) +
- DIV_ROUND_UP(backed_block_len(bb), to->block_size);
-
- /* will call out_counter_write to update count */
- ret = sparse_file_write_block(out_counter, bb);
- if (ret) {
- bb = NULL;
- goto out;
- }
- if (file_len + count > len) {
- /*
- * If the remaining available size is more than 1/8th of the
- * requested size, split the chunk. Results in sparse files that
- * are at least 7/8ths of the requested size
- */
- file_len += sizeof(chunk_header_t);
- if (!last_bb || (len - file_len > (len / 8))) {
- backed_block_split(from->backed_block_list, bb, len - file_len);
- last_bb = bb;
- }
- goto move;
- }
- file_len += count;
- last_bb = bb;
- }
-
-move:
- backed_block_list_move(from->backed_block_list,
- to->backed_block_list, start, last_bb);
-
-out:
- output_file_close(out_counter);
-
- return bb;
-}
-
-int sparse_file_resparse(struct sparse_file *in_s, unsigned int max_len,
- struct sparse_file **out_s, int out_s_count)
-{
- struct backed_block *bb;
- struct sparse_file *s;
- struct sparse_file *tmp;
- int c = 0;
-
- tmp = sparse_file_new(in_s->block_size, in_s->len);
- if (!tmp) {
- return -ENOMEM;
- }
-
- do {
- s = sparse_file_new(in_s->block_size, in_s->len);
-
- bb = move_chunks_up_to_len(in_s, s, max_len);
-
- if (c < out_s_count) {
- out_s[c] = s;
- } else {
- backed_block_list_move(s->backed_block_list, tmp->backed_block_list,
- NULL, NULL);
- sparse_file_destroy(s);
- }
- c++;
- } while (bb);
-
- backed_block_list_move(tmp->backed_block_list, in_s->backed_block_list,
- NULL, NULL);
-
- sparse_file_destroy(tmp);
-
- return c;
-}
-
-void sparse_file_verbose(struct sparse_file *s)
-{
- s->verbose = true;
-}
diff --git a/libsparse/sparse.cpp b/libsparse/sparse.cpp
new file mode 100644
index 0000000..cb288c5
--- /dev/null
+++ b/libsparse/sparse.cpp
@@ -0,0 +1,348 @@
+/*
+ * Copyright (C) 2012 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 <assert.h>
+#include <stdlib.h>
+
+#include <sparse/sparse.h>
+
+#include "defs.h"
+#include "sparse_file.h"
+
+#include "backed_block.h"
+#include "output_file.h"
+#include "sparse_defs.h"
+#include "sparse_format.h"
+
+struct sparse_file* sparse_file_new(unsigned int block_size, int64_t len) {
+ struct sparse_file* s = reinterpret_cast<sparse_file*>(calloc(sizeof(struct sparse_file), 1));
+ if (!s) {
+ return nullptr;
+ }
+
+ s->backed_block_list = backed_block_list_new(block_size);
+ if (!s->backed_block_list) {
+ free(s);
+ return nullptr;
+ }
+
+ s->block_size = block_size;
+ s->len = len;
+
+ return s;
+}
+
+void sparse_file_destroy(struct sparse_file* s) {
+ backed_block_list_destroy(s->backed_block_list);
+ free(s);
+}
+
+int sparse_file_add_data(struct sparse_file* s, void* data, unsigned int len, unsigned int block) {
+ return backed_block_add_data(s->backed_block_list, data, len, block);
+}
+
+int sparse_file_add_fill(struct sparse_file* s, uint32_t fill_val, unsigned int len,
+ unsigned int block) {
+ return backed_block_add_fill(s->backed_block_list, fill_val, len, block);
+}
+
+int sparse_file_add_file(struct sparse_file* s, const char* filename, int64_t file_offset,
+ unsigned int len, unsigned int block) {
+ return backed_block_add_file(s->backed_block_list, filename, file_offset, len, block);
+}
+
+int sparse_file_add_fd(struct sparse_file* s, int fd, int64_t file_offset, unsigned int len,
+ unsigned int block) {
+ return backed_block_add_fd(s->backed_block_list, fd, file_offset, len, block);
+}
+unsigned int sparse_count_chunks(struct sparse_file* s) {
+ struct backed_block* bb;
+ unsigned int last_block = 0;
+ unsigned int chunks = 0;
+
+ for (bb = backed_block_iter_new(s->backed_block_list); bb; bb = backed_block_iter_next(bb)) {
+ if (backed_block_block(bb) > last_block) {
+ /* If there is a gap between chunks, add a skip chunk */
+ chunks++;
+ }
+ chunks++;
+ last_block = backed_block_block(bb) + DIV_ROUND_UP(backed_block_len(bb), s->block_size);
+ }
+ if (last_block < DIV_ROUND_UP(s->len, s->block_size)) {
+ chunks++;
+ }
+
+ return chunks;
+}
+
+static int sparse_file_write_block(struct output_file* out, struct backed_block* bb) {
+ int ret = -EINVAL;
+
+ switch (backed_block_type(bb)) {
+ case BACKED_BLOCK_DATA:
+ ret = write_data_chunk(out, backed_block_len(bb), backed_block_data(bb));
+ break;
+ case BACKED_BLOCK_FILE:
+ ret = write_file_chunk(out, backed_block_len(bb), backed_block_filename(bb),
+ backed_block_file_offset(bb));
+ break;
+ case BACKED_BLOCK_FD:
+ ret = write_fd_chunk(out, backed_block_len(bb), backed_block_fd(bb),
+ backed_block_file_offset(bb));
+ break;
+ case BACKED_BLOCK_FILL:
+ ret = write_fill_chunk(out, backed_block_len(bb), backed_block_fill_val(bb));
+ break;
+ }
+
+ return ret;
+}
+
+static int write_all_blocks(struct sparse_file* s, struct output_file* out) {
+ struct backed_block* bb;
+ unsigned int last_block = 0;
+ int64_t pad;
+ int ret = 0;
+
+ for (bb = backed_block_iter_new(s->backed_block_list); bb; bb = backed_block_iter_next(bb)) {
+ if (backed_block_block(bb) > last_block) {
+ unsigned int blocks = backed_block_block(bb) - last_block;
+ write_skip_chunk(out, (int64_t)blocks * s->block_size);
+ }
+ ret = sparse_file_write_block(out, bb);
+ if (ret) return ret;
+ last_block = backed_block_block(bb) + DIV_ROUND_UP(backed_block_len(bb), s->block_size);
+ }
+
+ pad = s->len - (int64_t)last_block * s->block_size;
+ assert(pad >= 0);
+ if (pad > 0) {
+ write_skip_chunk(out, pad);
+ }
+
+ return 0;
+}
+
+int sparse_file_write(struct sparse_file* s, int fd, bool gz, bool sparse, bool crc) {
+ int ret;
+ int chunks;
+ struct output_file* out;
+
+ chunks = sparse_count_chunks(s);
+ out = output_file_open_fd(fd, s->block_size, s->len, gz, sparse, chunks, crc);
+
+ if (!out) return -ENOMEM;
+
+ ret = write_all_blocks(s, out);
+
+ output_file_close(out);
+
+ return ret;
+}
+
+int sparse_file_callback(struct sparse_file* s, bool sparse, bool crc,
+ int (*write)(void* priv, const void* data, size_t len), void* priv) {
+ int ret;
+ int chunks;
+ struct output_file* out;
+
+ chunks = sparse_count_chunks(s);
+ out = output_file_open_callback(write, priv, s->block_size, s->len, false, sparse, chunks, crc);
+
+ if (!out) return -ENOMEM;
+
+ ret = write_all_blocks(s, out);
+
+ output_file_close(out);
+
+ return ret;
+}
+
+struct chunk_data {
+ void* priv;
+ unsigned int block;
+ unsigned int nr_blocks;
+ int (*write)(void* priv, const void* data, size_t len, unsigned int block, unsigned int nr_blocks);
+};
+
+static int foreach_chunk_write(void* priv, const void* data, size_t len) {
+ struct chunk_data* chk = reinterpret_cast<chunk_data*>(priv);
+
+ return chk->write(chk->priv, data, len, chk->block, chk->nr_blocks);
+}
+
+int sparse_file_foreach_chunk(struct sparse_file* s, bool sparse, bool crc,
+ int (*write)(void* priv, const void* data, size_t len,
+ unsigned int block, unsigned int nr_blocks),
+ void* priv) {
+ int ret;
+ int chunks;
+ struct chunk_data chk;
+ struct output_file* out;
+ struct backed_block* bb;
+
+ chk.priv = priv;
+ chk.write = write;
+ chk.block = chk.nr_blocks = 0;
+ chunks = sparse_count_chunks(s);
+ out = output_file_open_callback(foreach_chunk_write, &chk, s->block_size, s->len, false, sparse,
+ chunks, crc);
+
+ if (!out) return -ENOMEM;
+
+ for (bb = backed_block_iter_new(s->backed_block_list); bb; bb = backed_block_iter_next(bb)) {
+ chk.block = backed_block_block(bb);
+ chk.nr_blocks = (backed_block_len(bb) - 1) / s->block_size + 1;
+ ret = sparse_file_write_block(out, bb);
+ if (ret) return ret;
+ }
+
+ output_file_close(out);
+
+ return ret;
+}
+
+static int out_counter_write(void* priv, const void* data __unused, size_t len) {
+ int64_t* count = reinterpret_cast<int64_t*>(priv);
+ *count += len;
+ return 0;
+}
+
+int64_t sparse_file_len(struct sparse_file* s, bool sparse, bool crc) {
+ int ret;
+ int chunks = sparse_count_chunks(s);
+ int64_t count = 0;
+ struct output_file* out;
+
+ out = output_file_open_callback(out_counter_write, &count, s->block_size, s->len, false, sparse,
+ chunks, crc);
+ if (!out) {
+ return -1;
+ }
+
+ ret = write_all_blocks(s, out);
+
+ output_file_close(out);
+
+ if (ret < 0) {
+ return -1;
+ }
+
+ return count;
+}
+
+unsigned int sparse_file_block_size(struct sparse_file* s) {
+ return s->block_size;
+}
+
+static struct backed_block* move_chunks_up_to_len(struct sparse_file* from, struct sparse_file* to,
+ unsigned int len) {
+ int64_t count = 0;
+ struct output_file* out_counter;
+ struct backed_block* last_bb = nullptr;
+ struct backed_block* bb;
+ struct backed_block* start;
+ unsigned int last_block = 0;
+ int64_t file_len = 0;
+ int ret;
+
+ /*
+ * overhead is sparse file header, the potential end skip
+ * chunk and crc chunk.
+ */
+ int overhead = sizeof(sparse_header_t) + 2 * sizeof(chunk_header_t) + sizeof(uint32_t);
+ len -= overhead;
+
+ start = backed_block_iter_new(from->backed_block_list);
+ out_counter = output_file_open_callback(out_counter_write, &count, to->block_size, to->len, false,
+ true, 0, false);
+ if (!out_counter) {
+ return nullptr;
+ }
+
+ for (bb = start; bb; bb = backed_block_iter_next(bb)) {
+ count = 0;
+ if (backed_block_block(bb) > last_block) count += sizeof(chunk_header_t);
+ last_block = backed_block_block(bb) + DIV_ROUND_UP(backed_block_len(bb), to->block_size);
+
+ /* will call out_counter_write to update count */
+ ret = sparse_file_write_block(out_counter, bb);
+ if (ret) {
+ bb = nullptr;
+ goto out;
+ }
+ if (file_len + count > len) {
+ /*
+ * If the remaining available size is more than 1/8th of the
+ * requested size, split the chunk. Results in sparse files that
+ * are at least 7/8ths of the requested size
+ */
+ file_len += sizeof(chunk_header_t);
+ if (!last_bb || (len - file_len > (len / 8))) {
+ backed_block_split(from->backed_block_list, bb, len - file_len);
+ last_bb = bb;
+ }
+ goto move;
+ }
+ file_len += count;
+ last_bb = bb;
+ }
+
+move:
+ backed_block_list_move(from->backed_block_list, to->backed_block_list, start, last_bb);
+
+out:
+ output_file_close(out_counter);
+
+ return bb;
+}
+
+int sparse_file_resparse(struct sparse_file* in_s, unsigned int max_len, struct sparse_file** out_s,
+ int out_s_count) {
+ struct backed_block* bb;
+ struct sparse_file* s;
+ struct sparse_file* tmp;
+ int c = 0;
+
+ tmp = sparse_file_new(in_s->block_size, in_s->len);
+ if (!tmp) {
+ return -ENOMEM;
+ }
+
+ do {
+ s = sparse_file_new(in_s->block_size, in_s->len);
+
+ bb = move_chunks_up_to_len(in_s, s, max_len);
+
+ if (c < out_s_count) {
+ out_s[c] = s;
+ } else {
+ backed_block_list_move(s->backed_block_list, tmp->backed_block_list, nullptr, nullptr);
+ sparse_file_destroy(s);
+ }
+ c++;
+ } while (bb);
+
+ backed_block_list_move(tmp->backed_block_list, in_s->backed_block_list, nullptr, nullptr);
+
+ sparse_file_destroy(tmp);
+
+ return c;
+}
+
+void sparse_file_verbose(struct sparse_file* s) {
+ s->verbose = true;
+}
diff --git a/libsparse/sparse_crc32.c b/libsparse/sparse_crc32.c
deleted file mode 100644
index 38bfe4a..0000000
--- a/libsparse/sparse_crc32.c
+++ /dev/null
@@ -1,111 +0,0 @@
-/*-
- * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
- * code or tables extracted from it, as desired without restriction.
- */
-
-/*
- * First, the polynomial itself and its table of feedback terms. The
- * polynomial is
- * X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
- *
- * Note that we take it "backwards" and put the highest-order term in
- * the lowest-order bit. The X^32 term is "implied"; the LSB is the
- * X^31 term, etc. The X^0 term (usually shown as "+1") results in
- * the MSB being 1
- *
- * Note that the usual hardware shift register implementation, which
- * is what we're using (we're merely optimizing it by doing eight-bit
- * chunks at a time) shifts bits into the lowest-order term. In our
- * implementation, that means shifting towards the right. Why do we
- * do it this way? Because the calculated CRC must be transmitted in
- * order from highest-order term to lowest-order term. UARTs transmit
- * characters in order from LSB to MSB. By storing the CRC this way
- * we hand it to the UART in the order low-byte to high-byte; the UART
- * sends each low-bit to hight-bit; and the result is transmission bit
- * by bit from highest- to lowest-order term without requiring any bit
- * shuffling on our part. Reception works similarly
- *
- * The feedback terms table consists of 256, 32-bit entries. Notes
- *
- * The table can be generated at runtime if desired; code to do so
- * is shown later. It might not be obvious, but the feedback
- * terms simply represent the results of eight shift/xor opera
- * tions for all combinations of data and CRC register values
- *
- * The values must be right-shifted by eight bits by the "updcrc
- * logic; the shift must be unsigned (bring in zeroes). On some
- * hardware you could probably optimize the shift in assembler by
- * using byte-swap instructions
- * polynomial $edb88320
- *
- *
- * CRC32 code derived from work by Gary S. Brown.
- */
-
-/* Code taken from FreeBSD 8 */
-#include <stdint.h>
-
-static uint32_t crc32_tab[] = {
- 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
- 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
- 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
- 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
- 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
- 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
- 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
- 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
- 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
- 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
- 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
- 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
- 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
- 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
- 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
- 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
- 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
- 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
- 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
- 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
- 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
- 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
- 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
- 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
- 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
- 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
- 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
- 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
- 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
- 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
- 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
- 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
- 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
- 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
- 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
- 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
- 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
- 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
- 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
- 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
- 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
- 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
- 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
-};
-
-/*
- * A function that calculates the CRC-32 based on the table above is
- * given below for documentation purposes. An equivalent implementation
- * of this function that's actually used in the kernel can be found
- * in sys/libkern.h, where it can be inlined.
- */
-
-uint32_t sparse_crc32(uint32_t crc_in, const void *buf, int size)
-{
- const uint8_t *p = buf;
- uint32_t crc;
-
- crc = crc_in ^ ~0U;
- while (size--)
- crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
- return crc ^ ~0U;
-}
-
diff --git a/libsparse/sparse_crc32.cpp b/libsparse/sparse_crc32.cpp
new file mode 100644
index 0000000..267322c
--- /dev/null
+++ b/libsparse/sparse_crc32.cpp
@@ -0,0 +1,97 @@
+/*-
+ * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
+ * code or tables extracted from it, as desired without restriction.
+ */
+
+/*
+ * First, the polynomial itself and its table of feedback terms. The
+ * polynomial is
+ * X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
+ *
+ * Note that we take it "backwards" and put the highest-order term in
+ * the lowest-order bit. The X^32 term is "implied"; the LSB is the
+ * X^31 term, etc. The X^0 term (usually shown as "+1") results in
+ * the MSB being 1
+ *
+ * Note that the usual hardware shift register implementation, which
+ * is what we're using (we're merely optimizing it by doing eight-bit
+ * chunks at a time) shifts bits into the lowest-order term. In our
+ * implementation, that means shifting towards the right. Why do we
+ * do it this way? Because the calculated CRC must be transmitted in
+ * order from highest-order term to lowest-order term. UARTs transmit
+ * characters in order from LSB to MSB. By storing the CRC this way
+ * we hand it to the UART in the order low-byte to high-byte; the UART
+ * sends each low-bit to hight-bit; and the result is transmission bit
+ * by bit from highest- to lowest-order term without requiring any bit
+ * shuffling on our part. Reception works similarly
+ *
+ * The feedback terms table consists of 256, 32-bit entries. Notes
+ *
+ * The table can be generated at runtime if desired; code to do so
+ * is shown later. It might not be obvious, but the feedback
+ * terms simply represent the results of eight shift/xor opera
+ * tions for all combinations of data and CRC register values
+ *
+ * The values must be right-shifted by eight bits by the "updcrc
+ * logic; the shift must be unsigned (bring in zeroes). On some
+ * hardware you could probably optimize the shift in assembler by
+ * using byte-swap instructions
+ * polynomial $edb88320
+ *
+ *
+ * CRC32 code derived from work by Gary S. Brown.
+ */
+
+/* Code taken from FreeBSD 8 */
+#include <stdint.h>
+#include <stdio.h>
+
+static uint32_t crc32_tab[] = {
+ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
+ 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
+ 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
+ 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
+ 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
+ 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
+ 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
+ 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
+ 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
+ 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
+ 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
+ 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
+ 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
+ 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
+ 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
+ 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
+ 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
+ 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
+ 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
+ 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
+ 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
+ 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
+ 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
+ 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
+ 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
+ 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
+ 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
+ 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
+ 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
+ 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
+
+/*
+ * A function that calculates the CRC-32 based on the table above is
+ * given below for documentation purposes. An equivalent implementation
+ * of this function that's actually used in the kernel can be found
+ * in sys/libkern.h, where it can be inlined.
+ */
+
+uint32_t sparse_crc32(uint32_t crc_in, const void* buf, size_t size) {
+ const uint8_t* p = reinterpret_cast<const uint8_t*>(buf);
+ uint32_t crc;
+
+ crc = crc_in ^ ~0U;
+ while (size--) crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
+ return crc ^ ~0U;
+}
diff --git a/libsparse/sparse_crc32.h b/libsparse/sparse_crc32.h
index 50cd9e9..2702c4f 100644
--- a/libsparse/sparse_crc32.h
+++ b/libsparse/sparse_crc32.h
@@ -19,14 +19,6 @@
#include <stdint.h>
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-uint32_t sparse_crc32(uint32_t crc, const void *buf, size_t size);
-
-#ifdef __cplusplus
-}
-#endif
+uint32_t sparse_crc32(uint32_t crc, const void* buf, size_t size);
#endif
diff --git a/libsparse/sparse_defs.h b/libsparse/sparse_defs.h
index b99cfd5..9137805 100644
--- a/libsparse/sparse_defs.h
+++ b/libsparse/sparse_defs.h
@@ -39,11 +39,14 @@
typedef unsigned short int u16;
typedef unsigned char u8;
-#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
-#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
+#define DIV_ROUND_UP(x, y) (((x) + (y)-1) / (y))
+#define ALIGN(x, y) ((y)*DIV_ROUND_UP((x), (y)))
#define ALIGN_DOWN(x, y) ((y) * ((x) / (y)))
-#define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); } while (0)
+#define error(fmt, args...) \
+ do { \
+ fprintf(stderr, "error: %s: " fmt "\n", __func__, ##args); \
+ } while (0)
#define error_errno(s, args...) error(s ": %s", ##args, strerror(errno))
#endif
diff --git a/libsparse/sparse_err.c b/libsparse/sparse_err.cpp
similarity index 73%
rename from libsparse/sparse_err.c
rename to libsparse/sparse_err.cpp
index 0f392ad..6886d31 100644
--- a/libsparse/sparse_err.c
+++ b/libsparse/sparse_err.cpp
@@ -20,14 +20,13 @@
#include <stdio.h>
#include <unistd.h>
-void sparse_default_print(const char *fmt, ...)
-{
- va_list argp;
+void sparse_default_print(const char* fmt, ...) {
+ va_list argp;
- va_start(argp, fmt);
- vfprintf(stderr, fmt, argp);
- va_end(argp);
+ va_start(argp, fmt);
+ vfprintf(stderr, fmt, argp);
+ va_end(argp);
}
-void (*sparse_print_error)(const char *fmt, ...) = sparse_default_print;
-void (*sparse_print_verbose)(const char *fmt, ...) = sparse_default_print;
+void (*sparse_print_error)(const char* fmt, ...) = sparse_default_print;
+void (*sparse_print_verbose)(const char* fmt, ...) = sparse_default_print;
diff --git a/libsparse/sparse_file.h b/libsparse/sparse_file.h
index 763f43f..e565f63 100644
--- a/libsparse/sparse_file.h
+++ b/libsparse/sparse_file.h
@@ -24,12 +24,12 @@
#include <sparse/sparse.h>
struct sparse_file {
- unsigned int block_size;
- int64_t len;
- bool verbose;
+ unsigned int block_size;
+ int64_t len;
+ bool verbose;
- struct backed_block_list *backed_block_list;
- struct output_file *out;
+ struct backed_block_list* backed_block_list;
+ struct output_file* out;
};
#ifdef __cplusplus
diff --git a/libsparse/sparse_format.h b/libsparse/sparse_format.h
index 779e038..a8a721e 100644
--- a/libsparse/sparse_format.h
+++ b/libsparse/sparse_format.h
@@ -23,31 +23,31 @@
#endif
typedef struct sparse_header {
- __le32 magic; /* 0xed26ff3a */
- __le16 major_version; /* (0x1) - reject images with higher major versions */
- __le16 minor_version; /* (0x0) - allow images with higer minor versions */
- __le16 file_hdr_sz; /* 28 bytes for first revision of the file format */
- __le16 chunk_hdr_sz; /* 12 bytes for first revision of the file format */
- __le32 blk_sz; /* block size in bytes, must be a multiple of 4 (4096) */
- __le32 total_blks; /* total blocks in the non-sparse output image */
- __le32 total_chunks; /* total chunks in the sparse input image */
- __le32 image_checksum; /* CRC32 checksum of the original data, counting "don't care" */
- /* as 0. Standard 802.3 polynomial, use a Public Domain */
- /* table implementation */
+ __le32 magic; /* 0xed26ff3a */
+ __le16 major_version; /* (0x1) - reject images with higher major versions */
+ __le16 minor_version; /* (0x0) - allow images with higer minor versions */
+ __le16 file_hdr_sz; /* 28 bytes for first revision of the file format */
+ __le16 chunk_hdr_sz; /* 12 bytes for first revision of the file format */
+ __le32 blk_sz; /* block size in bytes, must be a multiple of 4 (4096) */
+ __le32 total_blks; /* total blocks in the non-sparse output image */
+ __le32 total_chunks; /* total chunks in the sparse input image */
+ __le32 image_checksum; /* CRC32 checksum of the original data, counting "don't care" */
+ /* as 0. Standard 802.3 polynomial, use a Public Domain */
+ /* table implementation */
} sparse_header_t;
-#define SPARSE_HEADER_MAGIC 0xed26ff3a
+#define SPARSE_HEADER_MAGIC 0xed26ff3a
-#define CHUNK_TYPE_RAW 0xCAC1
-#define CHUNK_TYPE_FILL 0xCAC2
-#define CHUNK_TYPE_DONT_CARE 0xCAC3
-#define CHUNK_TYPE_CRC32 0xCAC4
+#define CHUNK_TYPE_RAW 0xCAC1
+#define CHUNK_TYPE_FILL 0xCAC2
+#define CHUNK_TYPE_DONT_CARE 0xCAC3
+#define CHUNK_TYPE_CRC32 0xCAC4
typedef struct chunk_header {
- __le16 chunk_type; /* 0xCAC1 -> raw; 0xCAC2 -> fill; 0xCAC3 -> don't care */
- __le16 reserved1;
- __le32 chunk_sz; /* in blocks in output image */
- __le32 total_sz; /* in bytes of chunk input file including chunk header and data */
+ __le16 chunk_type; /* 0xCAC1 -> raw; 0xCAC2 -> fill; 0xCAC3 -> don't care */
+ __le16 reserved1;
+ __le32 chunk_sz; /* in blocks in output image */
+ __le32 total_sz; /* in bytes of chunk input file including chunk header and data */
} chunk_header_t;
/* Following a Raw or Fill or CRC32 chunk is data.
diff --git a/libsparse/sparse_read.cpp b/libsparse/sparse_read.cpp
index 4379635..c4c1823 100644
--- a/libsparse/sparse_read.cpp
+++ b/libsparse/sparse_read.cpp
@@ -17,16 +17,16 @@
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE64_SOURCE 1
-#include <algorithm>
-#include <inttypes.h>
#include <fcntl.h>
+#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <string>
#include <unistd.h>
+#include <algorithm>
+#include <string>
#include <sparse/sparse.h>
@@ -37,447 +37,541 @@
#include "sparse_file.h"
#include "sparse_format.h"
-
#if defined(__APPLE__) && defined(__MACH__)
#define lseek64 lseek
#define off64_t off_t
#endif
#define SPARSE_HEADER_MAJOR_VER 1
-#define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
+#define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
static constexpr int64_t COPY_BUF_SIZE = 1024 * 1024;
-static char *copybuf;
+static char* copybuf;
-static std::string ErrorString(int err)
-{
- if (err == -EOVERFLOW) return "EOF while reading file";
- if (err == -EINVAL) return "Invalid sparse file format";
- if (err == -ENOMEM) return "Failed allocation while reading file";
- return android::base::StringPrintf("Unknown error %d", err);
+static std::string ErrorString(int err) {
+ if (err == -EOVERFLOW) return "EOF while reading file";
+ if (err == -EINVAL) return "Invalid sparse file format";
+ if (err == -ENOMEM) return "Failed allocation while reading file";
+ return android::base::StringPrintf("Unknown error %d", err);
}
-static void verbose_error(bool verbose, int err, const char *fmt, ...)
-{
- if (!verbose) return;
+class SparseFileSource {
+ public:
+ /* Seeks the source ahead by the given offset. */
+ virtual void Seek(int64_t offset) = 0;
- std::string msg = ErrorString(err);
- if (fmt) {
- msg += " at ";
- va_list argp;
- va_start(argp, fmt);
- android::base::StringAppendV(&msg, fmt, argp);
- va_end(argp);
- }
- sparse_print_verbose("%s\n", msg.c_str());
+ /* Return the current offset. */
+ virtual int64_t GetOffset() = 0;
+
+ /* Set the current offset. Return 0 if successful. */
+ virtual int SetOffset(int64_t offset) = 0;
+
+ /* Adds the given length from the current offset of the source to the file at the given block.
+ * Return 0 if successful. */
+ virtual int AddToSparseFile(struct sparse_file* s, int64_t len, unsigned int block) = 0;
+
+ /* Get data of fixed size from the current offset and seek len bytes. Return 0 if successful. */
+ virtual int ReadValue(void* ptr, int len) = 0;
+
+ /* Find the crc32 of the next len bytes and seek ahead len bytes. Return 0 if successful. */
+ virtual int GetCrc32(uint32_t* crc32, int64_t len) = 0;
+
+ virtual ~SparseFileSource(){};
+};
+
+class SparseFileFdSource : public SparseFileSource {
+ private:
+ int fd;
+
+ public:
+ SparseFileFdSource(int fd) : fd(fd) {}
+ ~SparseFileFdSource() override {}
+
+ void Seek(int64_t off) override { lseek64(fd, off, SEEK_CUR); }
+
+ int64_t GetOffset() override { return lseek64(fd, 0, SEEK_CUR); }
+
+ int SetOffset(int64_t offset) override {
+ return lseek64(fd, offset, SEEK_SET) == offset ? 0 : -errno;
+ }
+
+ int AddToSparseFile(struct sparse_file* s, int64_t len, unsigned int block) override {
+ return sparse_file_add_fd(s, fd, GetOffset(), len, block);
+ }
+
+ int ReadValue(void* ptr, int len) override { return read_all(fd, ptr, len); }
+
+ int GetCrc32(uint32_t* crc32, int64_t len) override {
+ int chunk;
+ int ret;
+ while (len) {
+ chunk = std::min(len, COPY_BUF_SIZE);
+ ret = read_all(fd, copybuf, chunk);
+ if (ret < 0) {
+ return ret;
+ }
+ *crc32 = sparse_crc32(*crc32, copybuf, chunk);
+ len -= chunk;
+ }
+ return 0;
+ }
+};
+
+class SparseFileBufSource : public SparseFileSource {
+ private:
+ char* buf;
+ int64_t offset;
+
+ public:
+ SparseFileBufSource(char* buf) : buf(buf), offset(0) {}
+ ~SparseFileBufSource() override {}
+
+ void Seek(int64_t off) override {
+ buf += off;
+ offset += off;
+ }
+
+ int64_t GetOffset() override { return offset; }
+
+ int SetOffset(int64_t off) override {
+ buf += off - offset;
+ offset = off;
+ return 0;
+ }
+
+ int AddToSparseFile(struct sparse_file* s, int64_t len, unsigned int block) override {
+ return sparse_file_add_data(s, buf, len, block);
+ }
+
+ int ReadValue(void* ptr, int len) override {
+ memcpy(ptr, buf, len);
+ Seek(len);
+ return 0;
+ }
+
+ int GetCrc32(uint32_t* crc32, int64_t len) override {
+ *crc32 = sparse_crc32(*crc32, buf, len);
+ Seek(len);
+ return 0;
+ }
+};
+
+static void verbose_error(bool verbose, int err, const char* fmt, ...) {
+ if (!verbose) return;
+
+ std::string msg = ErrorString(err);
+ if (fmt) {
+ msg += " at ";
+ va_list argp;
+ va_start(argp, fmt);
+ android::base::StringAppendV(&msg, fmt, argp);
+ va_end(argp);
+ }
+ sparse_print_verbose("%s\n", msg.c_str());
}
-static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size,
- int fd, int64_t offset, unsigned int blocks, unsigned int block,
- uint32_t *crc32)
-{
- int ret;
- int chunk;
- int64_t len = blocks * s->block_size;
+static int process_raw_chunk(struct sparse_file* s, unsigned int chunk_size,
+ SparseFileSource* source, unsigned int blocks, unsigned int block,
+ uint32_t* crc32) {
+ int ret;
+ int64_t len = blocks * s->block_size;
- if (chunk_size % s->block_size != 0) {
- return -EINVAL;
- }
+ if (chunk_size % s->block_size != 0) {
+ return -EINVAL;
+ }
- if (chunk_size / s->block_size != blocks) {
- return -EINVAL;
- }
+ if (chunk_size / s->block_size != blocks) {
+ return -EINVAL;
+ }
- ret = sparse_file_add_fd(s, fd, offset, len, block);
- if (ret < 0) {
- return ret;
- }
+ ret = source->AddToSparseFile(s, len, block);
+ if (ret < 0) {
+ return ret;
+ }
- if (crc32) {
- while (len) {
- chunk = std::min(len, COPY_BUF_SIZE);
- ret = read_all(fd, copybuf, chunk);
- if (ret < 0) {
- return ret;
- }
- *crc32 = sparse_crc32(*crc32, copybuf, chunk);
- len -= chunk;
- }
- } else {
- lseek64(fd, len, SEEK_CUR);
- }
+ if (crc32) {
+ ret = source->GetCrc32(crc32, len);
+ if (ret < 0) {
+ return ret;
+ }
+ } else {
+ source->Seek(len);
+ }
- return 0;
+ return 0;
}
-static int process_fill_chunk(struct sparse_file *s, unsigned int chunk_size,
- int fd, unsigned int blocks, unsigned int block, uint32_t *crc32)
-{
- int ret;
- int chunk;
- int64_t len = (int64_t)blocks * s->block_size;
- uint32_t fill_val;
- uint32_t *fillbuf;
- unsigned int i;
+static int process_fill_chunk(struct sparse_file* s, unsigned int chunk_size,
+ SparseFileSource* source, unsigned int blocks, unsigned int block,
+ uint32_t* crc32) {
+ int ret;
+ int chunk;
+ int64_t len = (int64_t)blocks * s->block_size;
+ uint32_t fill_val;
+ uint32_t* fillbuf;
+ unsigned int i;
- if (chunk_size != sizeof(fill_val)) {
- return -EINVAL;
- }
+ if (chunk_size != sizeof(fill_val)) {
+ return -EINVAL;
+ }
- ret = read_all(fd, &fill_val, sizeof(fill_val));
- if (ret < 0) {
- return ret;
- }
+ ret = source->ReadValue(&fill_val, sizeof(fill_val));
+ if (ret < 0) {
+ return ret;
+ }
- ret = sparse_file_add_fill(s, fill_val, len, block);
- if (ret < 0) {
- return ret;
- }
+ ret = sparse_file_add_fill(s, fill_val, len, block);
+ if (ret < 0) {
+ return ret;
+ }
- if (crc32) {
- /* Fill copy_buf with the fill value */
- fillbuf = (uint32_t *)copybuf;
- for (i = 0; i < (COPY_BUF_SIZE / sizeof(fill_val)); i++) {
- fillbuf[i] = fill_val;
- }
+ if (crc32) {
+ /* Fill copy_buf with the fill value */
+ fillbuf = (uint32_t*)copybuf;
+ for (i = 0; i < (COPY_BUF_SIZE / sizeof(fill_val)); i++) {
+ fillbuf[i] = fill_val;
+ }
- while (len) {
- chunk = std::min(len, COPY_BUF_SIZE);
- *crc32 = sparse_crc32(*crc32, copybuf, chunk);
- len -= chunk;
- }
- }
+ while (len) {
+ chunk = std::min(len, COPY_BUF_SIZE);
+ *crc32 = sparse_crc32(*crc32, copybuf, chunk);
+ len -= chunk;
+ }
+ }
- return 0;
+ return 0;
}
-static int process_skip_chunk(struct sparse_file *s, unsigned int chunk_size,
- int fd __unused, unsigned int blocks,
- unsigned int block __unused, uint32_t *crc32)
-{
- if (chunk_size != 0) {
- return -EINVAL;
- }
+static int process_skip_chunk(struct sparse_file* s, unsigned int chunk_size,
+ SparseFileSource* source __unused, unsigned int blocks,
+ unsigned int block __unused, uint32_t* crc32) {
+ if (chunk_size != 0) {
+ return -EINVAL;
+ }
- if (crc32) {
- int64_t len = (int64_t)blocks * s->block_size;
- memset(copybuf, 0, COPY_BUF_SIZE);
+ if (crc32) {
+ int64_t len = (int64_t)blocks * s->block_size;
+ memset(copybuf, 0, COPY_BUF_SIZE);
- while (len) {
- int chunk = std::min(len, COPY_BUF_SIZE);
- *crc32 = sparse_crc32(*crc32, copybuf, chunk);
- len -= chunk;
- }
- }
+ while (len) {
+ int chunk = std::min(len, COPY_BUF_SIZE);
+ *crc32 = sparse_crc32(*crc32, copybuf, chunk);
+ len -= chunk;
+ }
+ }
- return 0;
+ return 0;
}
-static int process_crc32_chunk(int fd, unsigned int chunk_size, uint32_t *crc32)
-{
- uint32_t file_crc32;
- int ret;
+static int process_crc32_chunk(SparseFileSource* source, unsigned int chunk_size, uint32_t* crc32) {
+ uint32_t file_crc32;
- if (chunk_size != sizeof(file_crc32)) {
- return -EINVAL;
- }
+ if (chunk_size != sizeof(file_crc32)) {
+ return -EINVAL;
+ }
- ret = read_all(fd, &file_crc32, sizeof(file_crc32));
- if (ret < 0) {
- return ret;
- }
+ int ret = source->ReadValue(&file_crc32, sizeof(file_crc32));
+ if (ret < 0) {
+ return ret;
+ }
- if (crc32 != NULL && file_crc32 != *crc32) {
- return -EINVAL;
- }
+ if (crc32 != nullptr && file_crc32 != *crc32) {
+ return -EINVAL;
+ }
- return 0;
+ return 0;
}
-static int process_chunk(struct sparse_file *s, int fd, off64_t offset,
- unsigned int chunk_hdr_sz, chunk_header_t *chunk_header,
- unsigned int cur_block, uint32_t *crc_ptr)
-{
- int ret;
- unsigned int chunk_data_size;
+static int process_chunk(struct sparse_file* s, SparseFileSource* source, unsigned int chunk_hdr_sz,
+ chunk_header_t* chunk_header, unsigned int cur_block, uint32_t* crc_ptr) {
+ int ret;
+ unsigned int chunk_data_size;
+ int64_t offset = source->GetOffset();
- chunk_data_size = chunk_header->total_sz - chunk_hdr_sz;
+ chunk_data_size = chunk_header->total_sz - chunk_hdr_sz;
- switch (chunk_header->chunk_type) {
- case CHUNK_TYPE_RAW:
- ret = process_raw_chunk(s, chunk_data_size, fd, offset,
- chunk_header->chunk_sz, cur_block, crc_ptr);
- if (ret < 0) {
- verbose_error(s->verbose, ret, "data block at %" PRId64, offset);
- return ret;
- }
- return chunk_header->chunk_sz;
- case CHUNK_TYPE_FILL:
- ret = process_fill_chunk(s, chunk_data_size, fd,
- chunk_header->chunk_sz, cur_block, crc_ptr);
- if (ret < 0) {
- verbose_error(s->verbose, ret, "fill block at %" PRId64, offset);
- return ret;
- }
- return chunk_header->chunk_sz;
- case CHUNK_TYPE_DONT_CARE:
- ret = process_skip_chunk(s, chunk_data_size, fd,
- chunk_header->chunk_sz, cur_block, crc_ptr);
- if (chunk_data_size != 0) {
- if (ret < 0) {
- verbose_error(s->verbose, ret, "skip block at %" PRId64, offset);
- return ret;
- }
- }
- return chunk_header->chunk_sz;
- case CHUNK_TYPE_CRC32:
- ret = process_crc32_chunk(fd, chunk_data_size, crc_ptr);
- if (ret < 0) {
- verbose_error(s->verbose, -EINVAL, "crc block at %" PRId64,
- offset);
- return ret;
- }
- return 0;
- default:
- verbose_error(s->verbose, -EINVAL, "unknown block %04X at %" PRId64,
- chunk_header->chunk_type, offset);
- }
+ switch (chunk_header->chunk_type) {
+ case CHUNK_TYPE_RAW:
+ ret =
+ process_raw_chunk(s, chunk_data_size, source, chunk_header->chunk_sz, cur_block, crc_ptr);
+ if (ret < 0) {
+ verbose_error(s->verbose, ret, "data block at %" PRId64, offset);
+ return ret;
+ }
+ return chunk_header->chunk_sz;
+ case CHUNK_TYPE_FILL:
+ ret = process_fill_chunk(s, chunk_data_size, source, chunk_header->chunk_sz, cur_block,
+ crc_ptr);
+ if (ret < 0) {
+ verbose_error(s->verbose, ret, "fill block at %" PRId64, offset);
+ return ret;
+ }
+ return chunk_header->chunk_sz;
+ case CHUNK_TYPE_DONT_CARE:
+ ret = process_skip_chunk(s, chunk_data_size, source, chunk_header->chunk_sz, cur_block,
+ crc_ptr);
+ if (chunk_data_size != 0) {
+ if (ret < 0) {
+ verbose_error(s->verbose, ret, "skip block at %" PRId64, offset);
+ return ret;
+ }
+ }
+ return chunk_header->chunk_sz;
+ case CHUNK_TYPE_CRC32:
+ ret = process_crc32_chunk(source, chunk_data_size, crc_ptr);
+ if (ret < 0) {
+ verbose_error(s->verbose, -EINVAL, "crc block at %" PRId64, offset);
+ return ret;
+ }
+ return 0;
+ default:
+ verbose_error(s->verbose, -EINVAL, "unknown block %04X at %" PRId64, chunk_header->chunk_type,
+ offset);
+ }
- return 0;
+ return 0;
}
-static int sparse_file_read_sparse(struct sparse_file *s, int fd, bool crc)
-{
- int ret;
- unsigned int i;
- sparse_header_t sparse_header;
- chunk_header_t chunk_header;
- uint32_t crc32 = 0;
- uint32_t *crc_ptr = 0;
- unsigned int cur_block = 0;
- off64_t offset;
+static int sparse_file_read_sparse(struct sparse_file* s, SparseFileSource* source, bool crc) {
+ int ret;
+ unsigned int i;
+ sparse_header_t sparse_header;
+ chunk_header_t chunk_header;
+ uint32_t crc32 = 0;
+ uint32_t* crc_ptr = nullptr;
+ unsigned int cur_block = 0;
- if (!copybuf) {
- copybuf = (char *)malloc(COPY_BUF_SIZE);
- }
+ if (!copybuf) {
+ copybuf = (char*)malloc(COPY_BUF_SIZE);
+ }
- if (!copybuf) {
- return -ENOMEM;
- }
+ if (!copybuf) {
+ return -ENOMEM;
+ }
- if (crc) {
- crc_ptr = &crc32;
- }
+ if (crc) {
+ crc_ptr = &crc32;
+ }
- ret = read_all(fd, &sparse_header, sizeof(sparse_header));
- if (ret < 0) {
- return ret;
- }
+ ret = source->ReadValue(&sparse_header, sizeof(sparse_header));
+ if (ret < 0) {
+ return ret;
+ }
- if (sparse_header.magic != SPARSE_HEADER_MAGIC) {
- return -EINVAL;
- }
+ if (sparse_header.magic != SPARSE_HEADER_MAGIC) {
+ return -EINVAL;
+ }
- if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) {
- return -EINVAL;
- }
+ if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) {
+ return -EINVAL;
+ }
- if (sparse_header.file_hdr_sz < SPARSE_HEADER_LEN) {
- return -EINVAL;
- }
+ if (sparse_header.file_hdr_sz < SPARSE_HEADER_LEN) {
+ return -EINVAL;
+ }
- if (sparse_header.chunk_hdr_sz < sizeof(chunk_header)) {
- return -EINVAL;
- }
+ if (sparse_header.chunk_hdr_sz < sizeof(chunk_header)) {
+ return -EINVAL;
+ }
- if (sparse_header.file_hdr_sz > SPARSE_HEADER_LEN) {
- /* Skip the remaining bytes in a header that is longer than
- * we expected.
- */
- lseek64(fd, sparse_header.file_hdr_sz - SPARSE_HEADER_LEN, SEEK_CUR);
- }
+ if (sparse_header.file_hdr_sz > SPARSE_HEADER_LEN) {
+ /* Skip the remaining bytes in a header that is longer than
+ * we expected.
+ */
+ source->Seek(sparse_header.file_hdr_sz - SPARSE_HEADER_LEN);
+ }
- for (i = 0; i < sparse_header.total_chunks; i++) {
- ret = read_all(fd, &chunk_header, sizeof(chunk_header));
- if (ret < 0) {
- return ret;
- }
+ for (i = 0; i < sparse_header.total_chunks; i++) {
+ ret = source->ReadValue(&chunk_header, sizeof(chunk_header));
+ if (ret < 0) {
+ return ret;
+ }
- if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) {
- /* Skip the remaining bytes in a header that is longer than
- * we expected.
- */
- lseek64(fd, sparse_header.chunk_hdr_sz - CHUNK_HEADER_LEN, SEEK_CUR);
- }
+ if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) {
+ /* Skip the remaining bytes in a header that is longer than
+ * we expected.
+ */
+ source->Seek(sparse_header.chunk_hdr_sz - CHUNK_HEADER_LEN);
+ }
- offset = lseek64(fd, 0, SEEK_CUR);
+ ret = process_chunk(s, source, sparse_header.chunk_hdr_sz, &chunk_header, cur_block, crc_ptr);
+ if (ret < 0) {
+ return ret;
+ }
- ret = process_chunk(s, fd, offset, sparse_header.chunk_hdr_sz, &chunk_header,
- cur_block, crc_ptr);
- if (ret < 0) {
- return ret;
- }
+ cur_block += ret;
+ }
- cur_block += ret;
- }
+ if (sparse_header.total_blks != cur_block) {
+ return -EINVAL;
+ }
- if (sparse_header.total_blks != cur_block) {
- return -EINVAL;
- }
-
- return 0;
+ return 0;
}
-static int sparse_file_read_normal(struct sparse_file *s, int fd)
-{
- int ret;
- uint32_t *buf = (uint32_t *)malloc(s->block_size);
- unsigned int block = 0;
- int64_t remain = s->len;
- int64_t offset = 0;
- unsigned int to_read;
- unsigned int i;
- bool sparse_block;
+static int sparse_file_read_normal(struct sparse_file* s, int fd) {
+ int ret;
+ uint32_t* buf = (uint32_t*)malloc(s->block_size);
+ unsigned int block = 0;
+ int64_t remain = s->len;
+ int64_t offset = 0;
+ unsigned int to_read;
+ unsigned int i;
+ bool sparse_block;
- if (!buf) {
- return -ENOMEM;
- }
+ if (!buf) {
+ return -ENOMEM;
+ }
- while (remain > 0) {
- to_read = std::min(remain, (int64_t)(s->block_size));
- ret = read_all(fd, buf, to_read);
- if (ret < 0) {
- error("failed to read sparse file");
- free(buf);
- return ret;
- }
+ while (remain > 0) {
+ to_read = std::min(remain, (int64_t)(s->block_size));
+ ret = read_all(fd, buf, to_read);
+ if (ret < 0) {
+ error("failed to read sparse file");
+ free(buf);
+ return ret;
+ }
- if (to_read == s->block_size) {
- sparse_block = true;
- for (i = 1; i < s->block_size / sizeof(uint32_t); i++) {
- if (buf[0] != buf[i]) {
- sparse_block = false;
- break;
- }
- }
- } else {
- sparse_block = false;
- }
+ if (to_read == s->block_size) {
+ sparse_block = true;
+ for (i = 1; i < s->block_size / sizeof(uint32_t); i++) {
+ if (buf[0] != buf[i]) {
+ sparse_block = false;
+ break;
+ }
+ }
+ } else {
+ sparse_block = false;
+ }
- if (sparse_block) {
- /* TODO: add flag to use skip instead of fill for buf[0] == 0 */
- sparse_file_add_fill(s, buf[0], to_read, block);
- } else {
- sparse_file_add_fd(s, fd, offset, to_read, block);
- }
+ if (sparse_block) {
+ /* TODO: add flag to use skip instead of fill for buf[0] == 0 */
+ sparse_file_add_fill(s, buf[0], to_read, block);
+ } else {
+ sparse_file_add_fd(s, fd, offset, to_read, block);
+ }
- remain -= to_read;
- offset += to_read;
- block++;
- }
+ remain -= to_read;
+ offset += to_read;
+ block++;
+ }
- free(buf);
- return 0;
+ free(buf);
+ return 0;
}
-int sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc)
-{
- if (crc && !sparse) {
- return -EINVAL;
- }
+int sparse_file_read(struct sparse_file* s, int fd, bool sparse, bool crc) {
+ if (crc && !sparse) {
+ return -EINVAL;
+ }
- if (sparse) {
- return sparse_file_read_sparse(s, fd, crc);
- } else {
- return sparse_file_read_normal(s, fd);
- }
+ if (sparse) {
+ SparseFileFdSource source(fd);
+ return sparse_file_read_sparse(s, &source, crc);
+ } else {
+ return sparse_file_read_normal(s, fd);
+ }
}
-struct sparse_file *sparse_file_import(int fd, bool verbose, bool crc)
-{
- int ret;
- sparse_header_t sparse_header;
- int64_t len;
- struct sparse_file *s;
-
- ret = read_all(fd, &sparse_header, sizeof(sparse_header));
- if (ret < 0) {
- verbose_error(verbose, ret, "header");
- return NULL;
- }
-
- if (sparse_header.magic != SPARSE_HEADER_MAGIC) {
- verbose_error(verbose, -EINVAL, "header magic");
- return NULL;
- }
-
- if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) {
- verbose_error(verbose, -EINVAL, "header major version");
- return NULL;
- }
-
- if (sparse_header.file_hdr_sz < SPARSE_HEADER_LEN) {
- return NULL;
- }
-
- if (sparse_header.chunk_hdr_sz < sizeof(chunk_header_t)) {
- return NULL;
- }
-
- len = (int64_t)sparse_header.total_blks * sparse_header.blk_sz;
- s = sparse_file_new(sparse_header.blk_sz, len);
- if (!s) {
- verbose_error(verbose, -EINVAL, NULL);
- return NULL;
- }
-
- ret = lseek64(fd, 0, SEEK_SET);
- if (ret < 0) {
- verbose_error(verbose, ret, "seeking");
- sparse_file_destroy(s);
- return NULL;
- }
-
- s->verbose = verbose;
-
- ret = sparse_file_read(s, fd, true, crc);
- if (ret < 0) {
- sparse_file_destroy(s);
- return NULL;
- }
-
- return s;
+int sparse_file_read_buf(struct sparse_file* s, char* buf, bool crc) {
+ SparseFileBufSource source(buf);
+ return sparse_file_read_sparse(s, &source, crc);
}
-struct sparse_file *sparse_file_import_auto(int fd, bool crc, bool verbose)
-{
- struct sparse_file *s;
- int64_t len;
- int ret;
+static struct sparse_file* sparse_file_import_source(SparseFileSource* source, bool verbose,
+ bool crc) {
+ int ret;
+ sparse_header_t sparse_header;
+ int64_t len;
+ struct sparse_file* s;
- s = sparse_file_import(fd, verbose, crc);
- if (s) {
- return s;
- }
+ ret = source->ReadValue(&sparse_header, sizeof(sparse_header));
+ if (ret < 0) {
+ verbose_error(verbose, ret, "header");
+ return nullptr;
+ }
- len = lseek64(fd, 0, SEEK_END);
- if (len < 0) {
- return NULL;
- }
+ if (sparse_header.magic != SPARSE_HEADER_MAGIC) {
+ verbose_error(verbose, -EINVAL, "header magic");
+ return nullptr;
+ }
- lseek64(fd, 0, SEEK_SET);
+ if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) {
+ verbose_error(verbose, -EINVAL, "header major version");
+ return nullptr;
+ }
- s = sparse_file_new(4096, len);
- if (!s) {
- return NULL;
- }
+ if (sparse_header.file_hdr_sz < SPARSE_HEADER_LEN) {
+ return nullptr;
+ }
- ret = sparse_file_read_normal(s, fd);
- if (ret < 0) {
- sparse_file_destroy(s);
- return NULL;
- }
+ if (sparse_header.chunk_hdr_sz < sizeof(chunk_header_t)) {
+ return nullptr;
+ }
- return s;
+ len = (int64_t)sparse_header.total_blks * sparse_header.blk_sz;
+ s = sparse_file_new(sparse_header.blk_sz, len);
+ if (!s) {
+ verbose_error(verbose, -EINVAL, nullptr);
+ return nullptr;
+ }
+
+ ret = source->SetOffset(0);
+ if (ret < 0) {
+ verbose_error(verbose, ret, "seeking");
+ sparse_file_destroy(s);
+ return nullptr;
+ }
+
+ s->verbose = verbose;
+
+ ret = sparse_file_read_sparse(s, source, crc);
+ if (ret < 0) {
+ sparse_file_destroy(s);
+ return nullptr;
+ }
+
+ return s;
+}
+
+struct sparse_file* sparse_file_import(int fd, bool verbose, bool crc) {
+ SparseFileFdSource source(fd);
+ return sparse_file_import_source(&source, verbose, crc);
+}
+
+struct sparse_file* sparse_file_import_buf(char* buf, bool verbose, bool crc) {
+ SparseFileBufSource source(buf);
+ return sparse_file_import_source(&source, verbose, crc);
+}
+
+struct sparse_file* sparse_file_import_auto(int fd, bool crc, bool verbose) {
+ struct sparse_file* s;
+ int64_t len;
+ int ret;
+
+ s = sparse_file_import(fd, verbose, crc);
+ if (s) {
+ return s;
+ }
+
+ len = lseek64(fd, 0, SEEK_END);
+ if (len < 0) {
+ return nullptr;
+ }
+
+ lseek64(fd, 0, SEEK_SET);
+
+ s = sparse_file_new(4096, len);
+ if (!s) {
+ return nullptr;
+ }
+
+ ret = sparse_file_read_normal(s, fd);
+ if (ret < 0) {
+ sparse_file_destroy(s);
+ return nullptr;
+ }
+
+ return s;
}
diff --git a/libstats/Android.bp b/libstats/Android.bp
new file mode 100644
index 0000000..d58f294
--- /dev/null
+++ b/libstats/Android.bp
@@ -0,0 +1,37 @@
+//
+// Copyright (C) 2018 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.
+//
+
+// ==========================================================
+// Native library to write stats log to statsd socket
+// ==========================================================
+cc_library_static {
+ name: "libstatssocket",
+ srcs: [
+ "stats_event_list.c",
+ "statsd_writer.c",
+ ],
+ cflags: [
+ "-Wall",
+ "-Werror",
+ "-DLIBLOG_LOG_TAG=1006",
+ "-DWRITE_TO_STATSD=1",
+ "-DWRITE_TO_LOGD=0",
+ ],
+ export_include_dirs: ["include"],
+ shared_libs: [
+ "liblog",
+ ],
+}
diff --git a/libstats/OWNERS b/libstats/OWNERS
new file mode 100644
index 0000000..ed06fbc
--- /dev/null
+++ b/libstats/OWNERS
@@ -0,0 +1,4 @@
+bookatz@google.com
+joeo@google.com
+yaochen@google.com
+yanglu@google.com
diff --git a/libstats/include/stats_event_list.h b/libstats/include/stats_event_list.h
new file mode 100644
index 0000000..5d174ae
--- /dev/null
+++ b/libstats/include/stats_event_list.h
@@ -0,0 +1,250 @@
+/*
+ * Copyright (C) 2018, 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.
+ */
+
+#ifndef ANDROID_STATS_LOG_STATS_EVENT_LIST_H
+#define ANDROID_STATS_LOG_STATS_EVENT_LIST_H
+
+#include <log/log_event_list.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+void reset_log_context(android_log_context ctx);
+int write_to_logger(android_log_context context, log_id_t id);
+
+#ifdef __cplusplus
+}
+#endif
+
+#ifdef __cplusplus
+/**
+ * A copy of android_log_event_list class.
+ *
+ * android_log_event_list is going to be deprecated soon, so copy it here to
+ * avoid creating dependency on upstream code. TODO(b/78304629): Rewrite this
+ * code.
+ */
+class stats_event_list {
+ private:
+ android_log_context ctx;
+ int ret;
+
+ stats_event_list(const stats_event_list&) = delete;
+ void operator=(const stats_event_list&) = delete;
+
+ public:
+ explicit stats_event_list(int tag) : ret(0) {
+ ctx = create_android_logger(static_cast<uint32_t>(tag));
+ }
+ explicit stats_event_list(log_msg& log_msg) : ret(0) {
+ ctx = create_android_log_parser(log_msg.msg() + sizeof(uint32_t),
+ log_msg.entry.len - sizeof(uint32_t));
+ }
+ ~stats_event_list() { android_log_destroy(&ctx); }
+
+ int close() {
+ int retval = android_log_destroy(&ctx);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return retval;
+ }
+
+ /* To allow above C calls to use this class as parameter */
+ operator android_log_context() const { return ctx; }
+
+ /* return errors or transmit status */
+ int status() const { return ret; }
+
+ int begin() {
+ int retval = android_log_write_list_begin(ctx);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret;
+ }
+ int end() {
+ int retval = android_log_write_list_end(ctx);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret;
+ }
+
+ stats_event_list& operator<<(int32_t value) {
+ int retval = android_log_write_int32(ctx, value);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return *this;
+ }
+
+ stats_event_list& operator<<(uint32_t value) {
+ int retval = android_log_write_int32(ctx, static_cast<int32_t>(value));
+ if (retval < 0) {
+ ret = retval;
+ }
+ return *this;
+ }
+
+ stats_event_list& operator<<(bool value) {
+ int retval = android_log_write_int32(ctx, value ? 1 : 0);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return *this;
+ }
+
+ stats_event_list& operator<<(int64_t value) {
+ int retval = android_log_write_int64(ctx, value);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return *this;
+ }
+
+ stats_event_list& operator<<(uint64_t value) {
+ int retval = android_log_write_int64(ctx, static_cast<int64_t>(value));
+ if (retval < 0) {
+ ret = retval;
+ }
+ return *this;
+ }
+
+ stats_event_list& operator<<(const char* value) {
+ int retval = android_log_write_string8(ctx, value);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return *this;
+ }
+
+#if defined(_USING_LIBCXX)
+ stats_event_list& operator<<(const std::string& value) {
+ int retval = android_log_write_string8_len(ctx, value.data(), value.length());
+ if (retval < 0) {
+ ret = retval;
+ }
+ return *this;
+ }
+#endif
+
+ stats_event_list& operator<<(float value) {
+ int retval = android_log_write_float32(ctx, value);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return *this;
+ }
+
+ int write(log_id_t id = LOG_ID_EVENTS) {
+ /* facilitate -EBUSY retry */
+ if ((ret == -EBUSY) || (ret > 0)) {
+ ret = 0;
+ }
+ int retval = write_to_logger(ctx, id);
+ /* existing errors trump transmission errors */
+ if (!ret) {
+ ret = retval;
+ }
+ return ret;
+ }
+
+ /*
+ * Append<Type> methods removes any integer promotion
+ * confusion, and adds access to string with length.
+ * Append methods are also added for all types for
+ * convenience.
+ */
+
+ bool AppendInt(int32_t value) {
+ int retval = android_log_write_int32(ctx, value);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret >= 0;
+ }
+
+ bool AppendLong(int64_t value) {
+ int retval = android_log_write_int64(ctx, value);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret >= 0;
+ }
+
+ bool AppendString(const char* value) {
+ int retval = android_log_write_string8(ctx, value);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret >= 0;
+ }
+
+ bool AppendString(const char* value, size_t len) {
+ int retval = android_log_write_string8_len(ctx, value, len);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret >= 0;
+ }
+
+#if defined(_USING_LIBCXX)
+ bool AppendString(const std::string& value) {
+ int retval = android_log_write_string8_len(ctx, value.data(), value.length());
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret;
+ }
+
+ bool Append(const std::string& value) {
+ int retval = android_log_write_string8_len(ctx, value.data(), value.length());
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret;
+ }
+#endif
+
+ bool AppendFloat(float value) {
+ int retval = android_log_write_float32(ctx, value);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret >= 0;
+ }
+
+ template <typename Tvalue>
+ bool Append(Tvalue value) {
+ *this << value;
+ return ret >= 0;
+ }
+
+ bool Append(const char* value, size_t len) {
+ int retval = android_log_write_string8_len(ctx, value, len);
+ if (retval < 0) {
+ ret = retval;
+ }
+ return ret >= 0;
+ }
+
+ android_log_list_element read() { return android_log_read_next(ctx); }
+ android_log_list_element peek() { return android_log_peek_next(ctx); }
+};
+
+#endif
+#endif // ANDROID_STATS_LOG_STATS_EVENT_LIST_H
diff --git a/libstats/stats_event_list.c b/libstats/stats_event_list.c
new file mode 100644
index 0000000..3d746db
--- /dev/null
+++ b/libstats/stats_event_list.c
@@ -0,0 +1,174 @@
+/*
+ * Copyright (C) 2018, 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 "include/stats_event_list.h"
+
+#include <string.h>
+#include "statsd_writer.h"
+
+#define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(int32_t))
+
+typedef struct {
+ uint32_t tag;
+ unsigned pos; /* Read/write position into buffer */
+ unsigned count[ANDROID_MAX_LIST_NEST_DEPTH + 1]; /* Number of elements */
+ unsigned list[ANDROID_MAX_LIST_NEST_DEPTH + 1]; /* pos for list counter */
+ unsigned list_nest_depth;
+ unsigned len; /* Length or raw buffer. */
+ bool overflow;
+ bool list_stop; /* next call decrement list_nest_depth and issue a stop */
+ enum {
+ kAndroidLoggerRead = 1,
+ kAndroidLoggerWrite = 2,
+ } read_write_flag;
+ uint8_t storage[LOGGER_ENTRY_MAX_PAYLOAD];
+} android_log_context_internal;
+
+extern struct android_log_transport_write statsdLoggerWrite;
+
+static int __write_to_statsd_init(struct iovec* vec, size_t nr);
+static int (*write_to_statsd)(struct iovec* vec, size_t nr) = __write_to_statsd_init;
+
+// Similar to create_android_logger(), but instead of allocation a new buffer,
+// this function resets the buffer for resuse.
+void reset_log_context(android_log_context ctx) {
+ if (!ctx) {
+ return;
+ }
+ android_log_context_internal* context = (android_log_context_internal*)(ctx);
+ uint32_t tag = context->tag;
+ memset(context, 0, sizeof(android_log_context_internal));
+
+ context->tag = tag;
+ context->read_write_flag = kAndroidLoggerWrite;
+ size_t needed = sizeof(uint8_t) + sizeof(uint8_t);
+ if ((context->pos + needed) > MAX_EVENT_PAYLOAD) {
+ context->overflow = true;
+ }
+ /* Everything is a list */
+ context->storage[context->pos + 0] = EVENT_TYPE_LIST;
+ context->list[0] = context->pos + 1;
+ context->pos += needed;
+}
+
+int stats_write_list(android_log_context ctx) {
+ android_log_context_internal* context;
+ const char* msg;
+ ssize_t len;
+
+ context = (android_log_context_internal*)(ctx);
+ if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
+ return -EBADF;
+ }
+
+ if (context->list_nest_depth) {
+ return -EIO;
+ }
+
+ /* NB: if there was overflow, then log is truncated. Nothing reported */
+ context->storage[1] = context->count[0];
+ len = context->len = context->pos;
+ msg = (const char*)context->storage;
+ /* it's not a list */
+ if (context->count[0] <= 1) {
+ len -= sizeof(uint8_t) + sizeof(uint8_t);
+ if (len < 0) {
+ len = 0;
+ }
+ msg += sizeof(uint8_t) + sizeof(uint8_t);
+ }
+
+ struct iovec vec[2];
+ vec[0].iov_base = &context->tag;
+ vec[0].iov_len = sizeof(context->tag);
+ vec[1].iov_base = (void*)msg;
+ vec[1].iov_len = len;
+ return write_to_statsd(vec, 2);
+}
+
+int write_to_logger(android_log_context ctx, log_id_t id) {
+ int retValue = 0;
+
+ if (WRITE_TO_LOGD) {
+ retValue = android_log_write_list(ctx, id);
+ }
+
+ if (WRITE_TO_STATSD) {
+ // log_event_list's cast operator is overloaded.
+ int ret = stats_write_list(ctx);
+ // In debugging phase, we may write to both logd and statsd. Prefer to
+ // return statsd socket write error code here.
+ if (ret < 0) {
+ retValue = ret;
+ }
+ }
+
+ return retValue;
+}
+
+/* log_init_lock assumed */
+static int __write_to_statsd_initialize_locked() {
+ if (!statsdLoggerWrite.open || ((*statsdLoggerWrite.open)() < 0)) {
+ if (statsdLoggerWrite.close) {
+ (*statsdLoggerWrite.close)();
+ return -ENODEV;
+ }
+ }
+ return 1;
+}
+
+static int __write_to_stats_daemon(struct iovec* vec, size_t nr) {
+ int save_errno;
+ struct timespec ts;
+ size_t len, i;
+
+ for (len = i = 0; i < nr; ++i) {
+ len += vec[i].iov_len;
+ }
+ if (!len) {
+ return -EINVAL;
+ }
+
+ save_errno = errno;
+ clock_gettime(CLOCK_REALTIME, &ts);
+
+ int ret = (int)(*statsdLoggerWrite.write)(&ts, vec, nr);
+ errno = save_errno;
+ return ret;
+}
+
+static int __write_to_statsd_init(struct iovec* vec, size_t nr) {
+ int ret, save_errno = errno;
+
+ statsd_writer_init_lock();
+
+ if (write_to_statsd == __write_to_statsd_init) {
+ ret = __write_to_statsd_initialize_locked();
+ if (ret < 0) {
+ statsd_writer_init_unlock();
+ errno = save_errno;
+ return ret;
+ }
+
+ write_to_statsd = __write_to_stats_daemon;
+ }
+
+ statsd_writer_init_unlock();
+
+ ret = write_to_statsd(vec, nr);
+ errno = save_errno;
+ return ret;
+}
diff --git a/libstats/statsd_writer.c b/libstats/statsd_writer.c
new file mode 100644
index 0000000..9953bba
--- /dev/null
+++ b/libstats/statsd_writer.c
@@ -0,0 +1,260 @@
+/*
+ * Copyright (C) 2018, 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 "statsd_writer.h"
+
+#include <cutils/sockets.h>
+#include <endian.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <poll.h>
+#include <private/android_filesystem_config.h>
+#include <private/android_logger.h>
+#include <stdarg.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <time.h>
+#include <unistd.h>
+
+/* branchless on many architectures. */
+#define min(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
+
+static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
+
+void statsd_writer_init_lock() {
+ /*
+ * If we trigger a signal handler in the middle of locked activity and the
+ * signal handler logs a message, we could get into a deadlock state.
+ */
+ pthread_mutex_lock(&log_init_lock);
+}
+
+int statd_writer_trylock() {
+ return pthread_mutex_trylock(&log_init_lock);
+}
+
+void statsd_writer_init_unlock() {
+ pthread_mutex_unlock(&log_init_lock);
+}
+
+static int statsdAvailable();
+static int statsdOpen();
+static void statsdClose();
+static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr);
+
+struct android_log_transport_write statsdLoggerWrite = {
+ .name = "statsd",
+ .sock = -EBADF,
+ .available = statsdAvailable,
+ .open = statsdOpen,
+ .close = statsdClose,
+ .write = statsdWrite,
+};
+
+/* log_init_lock assumed */
+static int statsdOpen() {
+ int i, ret = 0;
+
+ i = atomic_load(&statsdLoggerWrite.sock);
+ if (i < 0) {
+ int sock = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
+ if (sock < 0) {
+ ret = -errno;
+ } else {
+ struct sockaddr_un un;
+ memset(&un, 0, sizeof(struct sockaddr_un));
+ un.sun_family = AF_UNIX;
+ strcpy(un.sun_path, "/dev/socket/statsdw");
+
+ if (TEMP_FAILURE_RETRY(
+ connect(sock, (struct sockaddr*)&un, sizeof(struct sockaddr_un))) < 0) {
+ ret = -errno;
+ switch (ret) {
+ case -ENOTCONN:
+ case -ECONNREFUSED:
+ case -ENOENT:
+ i = atomic_exchange(&statsdLoggerWrite.sock, ret);
+ /* FALLTHRU */
+ default:
+ break;
+ }
+ close(sock);
+ } else {
+ ret = atomic_exchange(&statsdLoggerWrite.sock, sock);
+ if ((ret >= 0) && (ret != sock)) {
+ close(ret);
+ }
+ ret = 0;
+ }
+ }
+ }
+
+ return ret;
+}
+
+static void __statsdClose(int negative_errno) {
+ int sock = atomic_exchange(&statsdLoggerWrite.sock, negative_errno);
+ if (sock >= 0) {
+ close(sock);
+ }
+}
+
+static void statsdClose() {
+ __statsdClose(-EBADF);
+}
+
+static int statsdAvailable() {
+ if (atomic_load(&statsdLoggerWrite.sock) < 0) {
+ if (access("/dev/socket/statsdw", W_OK) == 0) {
+ return 0;
+ }
+ return -EBADF;
+ }
+ return 1;
+}
+
+static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr) {
+ ssize_t ret;
+ int sock;
+ static const unsigned headerLength = 1;
+ struct iovec newVec[nr + headerLength];
+ android_log_header_t header;
+ size_t i, payloadSize;
+ static atomic_int dropped;
+
+ sock = atomic_load(&statsdLoggerWrite.sock);
+ if (sock < 0) switch (sock) {
+ case -ENOTCONN:
+ case -ECONNREFUSED:
+ case -ENOENT:
+ break;
+ default:
+ return -EBADF;
+ }
+ /*
+ * struct {
+ * // what we provide to socket
+ * android_log_header_t header;
+ * // caller provides
+ * union {
+ * struct {
+ * char prio;
+ * char payload[];
+ * } string;
+ * struct {
+ * uint32_t tag
+ * char payload[];
+ * } binary;
+ * };
+ * };
+ */
+
+ header.tid = gettid();
+ header.realtime.tv_sec = ts->tv_sec;
+ header.realtime.tv_nsec = ts->tv_nsec;
+
+ newVec[0].iov_base = (unsigned char*)&header;
+ newVec[0].iov_len = sizeof(header);
+
+ // If we dropped events before, try to tell statsd.
+ if (sock >= 0) {
+ int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
+ if (snapshot) {
+ android_log_event_int_t buffer;
+ header.id = LOG_ID_STATS;
+ buffer.header.tag = htole32(LIBLOG_LOG_TAG);
+ buffer.payload.type = EVENT_TYPE_INT;
+ buffer.payload.data = htole32(snapshot);
+
+ newVec[headerLength].iov_base = &buffer;
+ newVec[headerLength].iov_len = sizeof(buffer);
+
+ ret = TEMP_FAILURE_RETRY(writev(sock, newVec, 2));
+ if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
+ atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
+ }
+ }
+ }
+
+ header.id = LOG_ID_STATS;
+
+ for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
+ newVec[i].iov_base = vec[i - headerLength].iov_base;
+ payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
+
+ if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
+ newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
+ if (newVec[i].iov_len) {
+ ++i;
+ }
+ break;
+ }
+ }
+
+ /*
+ * The write below could be lost, but will never block.
+ *
+ * ENOTCONN occurs if statsd has died.
+ * ENOENT occurs if statsd is not running and socket is missing.
+ * ECONNREFUSED occurs if we can not reconnect to statsd.
+ * EAGAIN occurs if statsd is overloaded.
+ */
+ if (sock < 0) {
+ ret = sock;
+ } else {
+ ret = TEMP_FAILURE_RETRY(writev(sock, newVec, i));
+ if (ret < 0) {
+ ret = -errno;
+ }
+ }
+ switch (ret) {
+ case -ENOTCONN:
+ case -ECONNREFUSED:
+ case -ENOENT:
+ if (statd_writer_trylock()) {
+ return ret; /* in a signal handler? try again when less stressed
+ */
+ }
+ __statsdClose(ret);
+ ret = statsdOpen();
+ statsd_writer_init_unlock();
+
+ if (ret < 0) {
+ return ret;
+ }
+
+ ret = TEMP_FAILURE_RETRY(writev(atomic_load(&statsdLoggerWrite.sock), newVec, i));
+ if (ret < 0) {
+ ret = -errno;
+ }
+ /* FALLTHRU */
+ default:
+ break;
+ }
+
+ if (ret > (ssize_t)sizeof(header)) {
+ ret -= sizeof(header);
+ } else if (ret == -EAGAIN) {
+ atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
+ }
+
+ return ret;
+}
diff --git a/libstats/statsd_writer.h b/libstats/statsd_writer.h
new file mode 100644
index 0000000..82e14e0
--- /dev/null
+++ b/libstats/statsd_writer.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2018, 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.
+ */
+
+#ifndef ANDROID_STATS_LOG_STATS_WRITER_H
+#define ANDROID_STATS_LOG_STATS_WRITER_H
+
+#include <pthread.h>
+#include <stdatomic.h>
+#include <sys/socket.h>
+
+/**
+ * Internal lock should not be exposed. This is bad design.
+ * TODO: rewrite it in c++ code and encapsulate the functionality in a
+ * StatsdWriter class.
+ */
+void statsd_writer_init_lock();
+int statsd_writer_init_trylock();
+void statsd_writer_init_unlock();
+
+struct android_log_transport_write {
+ const char* name; /* human name to describe the transport */
+ atomic_int sock;
+ int (*available)(); /* Does not cause resources to be taken */
+ int (*open)(); /* can be called multiple times, reusing current resources */
+ void (*close)(); /* free up resources */
+ /* write log to transport, returns number of bytes propagated, or -errno */
+ int (*write)(struct timespec* ts, struct iovec* vec, size_t nr);
+};
+
+#endif // ANDROID_STATS_LOG_STATS_WRITER_H
diff --git a/libsuspend/Android.bp b/libsuspend/Android.bp
index b3e36c2..c5f1f5e 100644
--- a/libsuspend/Android.bp
+++ b/libsuspend/Android.bp
@@ -2,11 +2,6 @@
cc_library {
name: "libsuspend",
- vendor_available: true,
- vndk: {
- enabled: true,
- },
-
srcs: [
"autosuspend.c",
"autosuspend_wakeup_count.cpp",
diff --git a/libsync/Android.bp b/libsync/Android.bp
index 3fae5e6..e56f8ba 100644
--- a/libsync/Android.bp
+++ b/libsync/Android.bp
@@ -20,8 +20,9 @@
cflags: ["-Werror"],
}
-cc_library_shared {
+cc_library {
name: "libsync",
+ recovery_available: true,
defaults: ["libsync_defaults"],
}
@@ -31,21 +32,6 @@
export_include_dirs: ["include"],
}
-// libsync_recovery is only intended for the recovery binary.
-// Future versions of the kernel WILL require an updated libsync, and will break
-// anything statically linked against the current libsync.
-cc_library_static {
- name: "libsync_recovery",
- defaults: ["libsync_defaults"],
-}
-
-cc_test {
- name: "sync_test",
- defaults: ["libsync_defaults"],
- gtest: false,
- srcs: ["sync_test.c"],
-}
-
cc_test {
name: "sync-unit-tests",
shared_libs: ["libsync"],
diff --git a/libsync/OWNERS b/libsync/OWNERS
new file mode 100644
index 0000000..dc61733
--- /dev/null
+++ b/libsync/OWNERS
@@ -0,0 +1,3 @@
+ghackmann@google.com
+jessehall@google.com
+marissaw@google.com
diff --git a/libsync/include/android/sync.h b/libsync/include/android/sync.h
index 68f74a0..32bb878 100644
--- a/libsync/include/android/sync.h
+++ b/libsync/include/android/sync.h
@@ -41,28 +41,8 @@
__BEGIN_DECLS
-struct sync_fence_info_data {
- uint32_t len;
- char name[32];
- int32_t status;
- uint8_t pt_info[0];
-};
-
-struct sync_pt_info {
- uint32_t len;
- char obj_name[32];
- char driver_name[32];
- int32_t status;
- uint64_t timestamp_ns;
- uint8_t driver_data[0];
-};
-
/* timeout in msecs */
int sync_wait(int fd, int timeout);
-struct sync_fence_info_data *sync_fence_info(int fd);
-struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
- struct sync_pt_info *itr);
-void sync_fence_info_free(struct sync_fence_info_data *info);
__END_DECLS
diff --git a/libsync/include/ndk/sync.h b/libsync/include/ndk/sync.h
index 3c55783..2a59e35 100644
--- a/libsync/include/ndk/sync.h
+++ b/libsync/include/ndk/sync.h
@@ -14,16 +14,26 @@
* limitations under the License.
*/
+/**
+ * @addtogroup Sync
+ * @{
+ */
+
+/**
+ * @file sync.h
+ */
+
#ifndef ANDROID_SYNC_H
#define ANDROID_SYNC_H
#include <stdint.h>
+#include <sys/cdefs.h>
#include <linux/sync_file.h>
__BEGIN_DECLS
-#if __ANDROID_API__ >= __ANDROID_API_O__
+#if __ANDROID_API__ >= 26
/* Fences indicate the status of an asynchronous task. They are initially
* in unsignaled state (0), and make a one-time transition to either signaled
@@ -53,21 +63,27 @@
*
* The original fences remain valid, and the caller is responsible for closing
* them.
+ *
+ * Available since API level 26.
*/
-int32_t sync_merge(const char *name, int32_t fd1, int32_t fd2);
+int32_t sync_merge(const char* name, int32_t fd1, int32_t fd2) __INTRODUCED_IN(26);
/**
* Retrieve detailed information about a sync file and its fences.
*
* The returned sync_file_info must be freed by calling sync_file_info_free().
+ *
+ * Available since API level 26.
*/
-struct sync_file_info *sync_file_info(int32_t fd);
+struct sync_file_info* sync_file_info(int32_t fd) __INTRODUCED_IN(26);
/**
* Get the array of fence infos from the sync file's info.
*
* The returned array is owned by the parent sync file info, and has
* info->num_fences entries.
+ *
+ * Available since API level 26.
*/
static inline struct sync_fence_info* sync_get_fence_info(const struct sync_file_info* info) {
// This header should compile in C, but some C++ projects enable
@@ -78,11 +94,17 @@
#pragma GCC diagnostic pop
}
-/** Free a struct sync_file_info structure */
-void sync_file_info_free(struct sync_file_info *info);
+/**
+ * Free a struct sync_file_info structure
+ *
+ * Available since API level 26.
+ */
+void sync_file_info_free(struct sync_file_info* info) __INTRODUCED_IN(26);
-#endif // __ANDROID_API__ >= __ANDROID_API_O__
+#endif /* __ANDROID_API__ >= 26 */
__END_DECLS
#endif /* ANDROID_SYNC_H */
+
+/** @} */
diff --git a/libsync/sync.c b/libsync/sync.c
index 6b187fa..b8c48c7 100644
--- a/libsync/sync.c
+++ b/libsync/sync.c
@@ -30,6 +30,29 @@
#include <android/sync.h>
+/* Prototypes for deprecated functions that used to be declared in the legacy
+ * android/sync.h. They've been moved here to make sure new code does not use
+ * them, but the functions are still defined to avoid breaking existing
+ * binaries. Eventually they can be removed altogether.
+ */
+struct sync_fence_info_data {
+ uint32_t len;
+ char name[32];
+ int32_t status;
+ uint8_t pt_info[0];
+};
+struct sync_pt_info {
+ uint32_t len;
+ char obj_name[32];
+ char driver_name[32];
+ int32_t status;
+ uint64_t timestamp_ns;
+ uint8_t driver_data[0];
+};
+struct sync_fence_info_data* sync_fence_info(int fd);
+struct sync_pt_info* sync_pt_info(struct sync_fence_info_data* info, struct sync_pt_info* itr);
+void sync_fence_info_free(struct sync_fence_info_data* info);
+
/* Legacy Sync API */
struct sync_legacy_merge_data {
diff --git a/libsync/sync_test.c b/libsync/sync_test.c
deleted file mode 100644
index f1ffdcf..0000000
--- a/libsync/sync_test.c
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * sync_test.c
- *
- * Copyright 2012 Google, Inc
- *
- * 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 <pthread.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <android/sync.h>
-#include "sw_sync.h"
-
-pthread_mutex_t printf_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-struct sync_thread_data {
- int thread_no;
- int fd[2];
-};
-
-void *sync_thread(void *data)
-{
- struct sync_thread_data *sync_data = data;
- struct sync_fence_info_data *info;
- int err;
- int i;
-
- for (i = 0; i < 2; i++) {
- err = sync_wait(sync_data->fd[i], 10000);
-
- pthread_mutex_lock(&printf_mutex);
- if (err < 0) {
- printf("thread %d wait %d failed: %s\n", sync_data->thread_no,
- i, strerror(errno));
- } else {
- printf("thread %d wait %d done\n", sync_data->thread_no, i);
- }
- info = sync_fence_info(sync_data->fd[i]);
- if (info) {
- struct sync_pt_info *pt_info = NULL;
- printf(" fence %s %d\n", info->name, info->status);
-
- while ((pt_info = sync_pt_info(info, pt_info))) {
- int ts_sec = pt_info->timestamp_ns / 1000000000LL;
- int ts_usec = (pt_info->timestamp_ns % 1000000000LL) / 1000LL;
- printf(" pt %s %s %d %d.%06d", pt_info->obj_name,
- pt_info->driver_name, pt_info->status,
- ts_sec, ts_usec);
- if (!strcmp(pt_info->driver_name, "sw_sync"))
- printf(" val=%d\n", *(uint32_t *)pt_info->driver_data);
- else
- printf("\n");
- }
- sync_fence_info_free(info);
- }
- pthread_mutex_unlock(&printf_mutex);
- }
-
- return NULL;
-}
-
-int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
-{
- struct sync_thread_data sync_data[4];
- pthread_t threads[4];
- int sync_timeline_fd;
- int i, j;
- char str[256];
-
- sync_timeline_fd = sw_sync_timeline_create();
- if (sync_timeline_fd < 0) {
- perror("can't create sw_sync_timeline:");
- return 1;
- }
-
- for (i = 0; i < 3; i++) {
- sync_data[i].thread_no = i;
-
- for (j = 0; j < 2; j++) {
- unsigned val = i + j * 3 + 1;
- snprintf(str, sizeof(str), "test_fence%d-%d", i, j);
- int fd = sw_sync_fence_create(sync_timeline_fd, str, val);
- if (fd < 0) {
- printf("can't create sync pt %d: %s", val, strerror(errno));
- return 1;
- }
- sync_data[i].fd[j] = fd;
- printf("sync_data[%d].fd[%d] = %d;\n", i, j, fd);
-
- }
- }
-
- sync_data[3].thread_no = 3;
- for (j = 0; j < 2; j++) {
- snprintf(str, sizeof(str), "merged_fence%d", j);
- sync_data[3].fd[j] = sync_merge(str, sync_data[0].fd[j], sync_data[1].fd[j]);
- if (sync_data[3].fd[j] < 0) {
- printf("can't merge sync pts %d and %d: %s\n",
- sync_data[0].fd[j], sync_data[1].fd[j], strerror(errno));
- return 1;
- }
- }
-
- for (i = 0; i < 4; i++)
- pthread_create(&threads[i], NULL, sync_thread, &sync_data[i]);
-
-
- for (i = 0; i < 3; i++) {
- int err;
- printf("press enter to inc to %d\n", i+1);
- fgets(str, sizeof(str), stdin);
- err = sw_sync_timeline_inc(sync_timeline_fd, 1);
- if (err < 0) {
- perror("can't increment sync obj:");
- return 1;
- }
- }
-
- printf("press enter to close sync_timeline\n");
- fgets(str, sizeof(str), stdin);
-
- close(sync_timeline_fd);
-
- printf("press enter to end test\n");
- fgets(str, sizeof(str), stdin);
-
- for (i = 0; i < 3; i++) {
- void *val;
- pthread_join(threads[i], &val);
- }
-
- return 0;
-}
diff --git a/libsync/tests/sync_test.cpp b/libsync/tests/sync_test.cpp
index 0fb86d6..011b09d 100644
--- a/libsync/tests/sync_test.cpp
+++ b/libsync/tests/sync_test.cpp
@@ -15,6 +15,35 @@
#include <random>
#include <unordered_map>
+/* These deprecated declarations were in the legacy android/sync.h. They've been removed to
+ * encourage code to move to the modern equivalents. But they are still implemented in libsync.so
+ * to avoid breaking existing binaries; as long as that's true we should keep testing them here.
+ * That means making local copies of the declarations.
+ */
+extern "C" {
+
+struct sync_fence_info_data {
+ uint32_t len;
+ char name[32];
+ int32_t status;
+ uint8_t pt_info[0];
+};
+
+struct sync_pt_info {
+ uint32_t len;
+ char obj_name[32];
+ char driver_name[32];
+ int32_t status;
+ uint64_t timestamp_ns;
+ uint8_t driver_data[0];
+};
+
+struct sync_fence_info_data* sync_fence_info(int fd);
+struct sync_pt_info* sync_pt_info(struct sync_fence_info_data* info, struct sync_pt_info* itr);
+void sync_fence_info_free(struct sync_fence_info_data* info);
+
+} // extern "C"
+
// TODO: better stress tests?
// Handle more than 64 fd's simultaneously, i.e. fix sync_fence_info's 4k limit.
// Handle wraparound in timelines like nvidia.
diff --git a/libsystem/Android.bp b/libsystem/Android.bp
index 82bf1bc..2e22b43 100644
--- a/libsystem/Android.bp
+++ b/libsystem/Android.bp
@@ -1,6 +1,7 @@
cc_library_headers {
name: "libsystem_headers",
vendor_available: true,
+ recovery_available: true,
host_supported: true,
export_include_dirs: ["include"],
diff --git a/libsystem/include/system/camera.h b/libsystem/include/system/camera.h
index 5d0873a..7d79673 100644
--- a/libsystem/include/system/camera.h
+++ b/libsystem/include/system/camera.h
@@ -203,6 +203,15 @@
* (except disconnect and sending CAMERA_CMD_PING) after getting this.
*/
CAMERA_ERROR_RELEASED = 2,
+
+ /**
+ * Camera was released because device policy change or the client application
+ * is going to background. The client should call Camera::disconnect
+ * immediately after getting this notification. Otherwise, the camera will be
+ * released by camera service in a short time. The client should not call any
+ * method (except disconnect and sending CAMERA_CMD_PING) after getting this.
+ */
+ CAMERA_ERROR_DISABLED = 3,
CAMERA_ERROR_SERVER_DIED = 100
};
diff --git a/libsystem/include/system/graphics-base-v1.0.h b/libsystem/include/system/graphics-base-v1.0.h
new file mode 100644
index 0000000..44913cc
--- /dev/null
+++ b/libsystem/include/system/graphics-base-v1.0.h
@@ -0,0 +1,140 @@
+// This file is autogenerated by hidl-gen. Do not edit manually.
+// Source: android.hardware.graphics.common@1.0
+// Location: hardware/interfaces/graphics/common/1.0/
+
+#ifndef HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_0_EXPORTED_CONSTANTS_H_
+#define HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_0_EXPORTED_CONSTANTS_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+ HAL_PIXEL_FORMAT_RGBA_8888 = 1,
+ HAL_PIXEL_FORMAT_RGBX_8888 = 2,
+ HAL_PIXEL_FORMAT_RGB_888 = 3,
+ HAL_PIXEL_FORMAT_RGB_565 = 4,
+ HAL_PIXEL_FORMAT_BGRA_8888 = 5,
+ HAL_PIXEL_FORMAT_YCBCR_422_SP = 16,
+ HAL_PIXEL_FORMAT_YCRCB_420_SP = 17,
+ HAL_PIXEL_FORMAT_YCBCR_422_I = 20,
+ HAL_PIXEL_FORMAT_RGBA_FP16 = 22,
+ HAL_PIXEL_FORMAT_RAW16 = 32,
+ HAL_PIXEL_FORMAT_BLOB = 33,
+ HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED = 34,
+ HAL_PIXEL_FORMAT_YCBCR_420_888 = 35,
+ HAL_PIXEL_FORMAT_RAW_OPAQUE = 36,
+ HAL_PIXEL_FORMAT_RAW10 = 37,
+ HAL_PIXEL_FORMAT_RAW12 = 38,
+ HAL_PIXEL_FORMAT_RGBA_1010102 = 43,
+ HAL_PIXEL_FORMAT_Y8 = 538982489,
+ HAL_PIXEL_FORMAT_Y16 = 540422489,
+ HAL_PIXEL_FORMAT_YV12 = 842094169,
+} android_pixel_format_t;
+
+typedef enum {
+ HAL_TRANSFORM_FLIP_H = 1, // (1 << 0)
+ HAL_TRANSFORM_FLIP_V = 2, // (1 << 1)
+ HAL_TRANSFORM_ROT_90 = 4, // (1 << 2)
+ HAL_TRANSFORM_ROT_180 = 3, // (FLIP_H | FLIP_V)
+ HAL_TRANSFORM_ROT_270 = 7, // ((FLIP_H | FLIP_V) | ROT_90)
+} android_transform_t;
+
+typedef enum {
+ HAL_DATASPACE_UNKNOWN = 0,
+ HAL_DATASPACE_ARBITRARY = 1,
+ HAL_DATASPACE_STANDARD_SHIFT = 16,
+ HAL_DATASPACE_STANDARD_MASK = 4128768, // (63 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_UNSPECIFIED = 0, // (0 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_BT709 = 65536, // (1 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_BT601_625 = 131072, // (2 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED = 196608, // (3 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_BT601_525 = 262144, // (4 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED = 327680, // (5 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_BT2020 = 393216, // (6 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE = 458752, // (7 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_BT470M = 524288, // (8 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_FILM = 589824, // (9 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_DCI_P3 = 655360, // (10 << STANDARD_SHIFT)
+ HAL_DATASPACE_STANDARD_ADOBE_RGB = 720896, // (11 << STANDARD_SHIFT)
+ HAL_DATASPACE_TRANSFER_SHIFT = 22,
+ HAL_DATASPACE_TRANSFER_MASK = 130023424, // (31 << TRANSFER_SHIFT)
+ HAL_DATASPACE_TRANSFER_UNSPECIFIED = 0, // (0 << TRANSFER_SHIFT)
+ HAL_DATASPACE_TRANSFER_LINEAR = 4194304, // (1 << TRANSFER_SHIFT)
+ HAL_DATASPACE_TRANSFER_SRGB = 8388608, // (2 << TRANSFER_SHIFT)
+ HAL_DATASPACE_TRANSFER_SMPTE_170M = 12582912, // (3 << TRANSFER_SHIFT)
+ HAL_DATASPACE_TRANSFER_GAMMA2_2 = 16777216, // (4 << TRANSFER_SHIFT)
+ HAL_DATASPACE_TRANSFER_GAMMA2_6 = 20971520, // (5 << TRANSFER_SHIFT)
+ HAL_DATASPACE_TRANSFER_GAMMA2_8 = 25165824, // (6 << TRANSFER_SHIFT)
+ HAL_DATASPACE_TRANSFER_ST2084 = 29360128, // (7 << TRANSFER_SHIFT)
+ HAL_DATASPACE_TRANSFER_HLG = 33554432, // (8 << TRANSFER_SHIFT)
+ HAL_DATASPACE_RANGE_SHIFT = 27,
+ HAL_DATASPACE_RANGE_MASK = 939524096, // (7 << RANGE_SHIFT)
+ HAL_DATASPACE_RANGE_UNSPECIFIED = 0, // (0 << RANGE_SHIFT)
+ HAL_DATASPACE_RANGE_FULL = 134217728, // (1 << RANGE_SHIFT)
+ HAL_DATASPACE_RANGE_LIMITED = 268435456, // (2 << RANGE_SHIFT)
+ HAL_DATASPACE_RANGE_EXTENDED = 402653184, // (3 << RANGE_SHIFT)
+ HAL_DATASPACE_SRGB_LINEAR = 512,
+ HAL_DATASPACE_V0_SRGB_LINEAR = 138477568, // ((STANDARD_BT709 | TRANSFER_LINEAR) | RANGE_FULL)
+ HAL_DATASPACE_V0_SCRGB_LINEAR =
+ 406913024, // ((STANDARD_BT709 | TRANSFER_LINEAR) | RANGE_EXTENDED)
+ HAL_DATASPACE_SRGB = 513,
+ HAL_DATASPACE_V0_SRGB = 142671872, // ((STANDARD_BT709 | TRANSFER_SRGB) | RANGE_FULL)
+ HAL_DATASPACE_V0_SCRGB = 411107328, // ((STANDARD_BT709 | TRANSFER_SRGB) | RANGE_EXTENDED)
+ HAL_DATASPACE_JFIF = 257,
+ HAL_DATASPACE_V0_JFIF = 146931712, // ((STANDARD_BT601_625 | TRANSFER_SMPTE_170M) | RANGE_FULL)
+ HAL_DATASPACE_BT601_625 = 258,
+ HAL_DATASPACE_V0_BT601_625 =
+ 281149440, // ((STANDARD_BT601_625 | TRANSFER_SMPTE_170M) | RANGE_LIMITED)
+ HAL_DATASPACE_BT601_525 = 259,
+ HAL_DATASPACE_V0_BT601_525 =
+ 281280512, // ((STANDARD_BT601_525 | TRANSFER_SMPTE_170M) | RANGE_LIMITED)
+ HAL_DATASPACE_BT709 = 260,
+ HAL_DATASPACE_V0_BT709 = 281083904, // ((STANDARD_BT709 | TRANSFER_SMPTE_170M) | RANGE_LIMITED)
+ HAL_DATASPACE_DCI_P3_LINEAR = 139067392, // ((STANDARD_DCI_P3 | TRANSFER_LINEAR) | RANGE_FULL)
+ HAL_DATASPACE_DCI_P3 = 155844608, // ((STANDARD_DCI_P3 | TRANSFER_GAMMA2_6) | RANGE_FULL)
+ HAL_DATASPACE_DISPLAY_P3_LINEAR =
+ 139067392, // ((STANDARD_DCI_P3 | TRANSFER_LINEAR) | RANGE_FULL)
+ HAL_DATASPACE_DISPLAY_P3 = 143261696, // ((STANDARD_DCI_P3 | TRANSFER_SRGB) | RANGE_FULL)
+ HAL_DATASPACE_ADOBE_RGB = 151715840, // ((STANDARD_ADOBE_RGB | TRANSFER_GAMMA2_2) | RANGE_FULL)
+ HAL_DATASPACE_BT2020_LINEAR = 138805248, // ((STANDARD_BT2020 | TRANSFER_LINEAR) | RANGE_FULL)
+ HAL_DATASPACE_BT2020 = 147193856, // ((STANDARD_BT2020 | TRANSFER_SMPTE_170M) | RANGE_FULL)
+ HAL_DATASPACE_BT2020_PQ = 163971072, // ((STANDARD_BT2020 | TRANSFER_ST2084) | RANGE_FULL)
+ HAL_DATASPACE_DEPTH = 4096,
+ HAL_DATASPACE_SENSOR = 4097,
+} android_dataspace_t;
+
+typedef enum {
+ HAL_COLOR_MODE_NATIVE = 0,
+ HAL_COLOR_MODE_STANDARD_BT601_625 = 1,
+ HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED = 2,
+ HAL_COLOR_MODE_STANDARD_BT601_525 = 3,
+ HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED = 4,
+ HAL_COLOR_MODE_STANDARD_BT709 = 5,
+ HAL_COLOR_MODE_DCI_P3 = 6,
+ HAL_COLOR_MODE_SRGB = 7,
+ HAL_COLOR_MODE_ADOBE_RGB = 8,
+ HAL_COLOR_MODE_DISPLAY_P3 = 9,
+} android_color_mode_t;
+
+typedef enum {
+ HAL_COLOR_TRANSFORM_IDENTITY = 0,
+ HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX = 1,
+ HAL_COLOR_TRANSFORM_VALUE_INVERSE = 2,
+ HAL_COLOR_TRANSFORM_GRAYSCALE = 3,
+ HAL_COLOR_TRANSFORM_CORRECT_PROTANOPIA = 4,
+ HAL_COLOR_TRANSFORM_CORRECT_DEUTERANOPIA = 5,
+ HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA = 6,
+} android_color_transform_t;
+
+typedef enum {
+ HAL_HDR_DOLBY_VISION = 1,
+ HAL_HDR_HDR10 = 2,
+ HAL_HDR_HLG = 3,
+} android_hdr_t;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_0_EXPORTED_CONSTANTS_H_
diff --git a/libsystem/include/system/graphics-base-v1.1.h b/libsystem/include/system/graphics-base-v1.1.h
new file mode 100644
index 0000000..f95b9ba
--- /dev/null
+++ b/libsystem/include/system/graphics-base-v1.1.h
@@ -0,0 +1,48 @@
+// This file is autogenerated by hidl-gen. Do not edit manually.
+// Source: android.hardware.graphics.common@1.1
+// Location: hardware/interfaces/graphics/common/1.1/
+
+#ifndef HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_1_EXPORTED_CONSTANTS_H_
+#define HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_1_EXPORTED_CONSTANTS_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+ HAL_PIXEL_FORMAT_DEPTH_16 = 48,
+ HAL_PIXEL_FORMAT_DEPTH_24 = 49,
+ HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8 = 50,
+ HAL_PIXEL_FORMAT_DEPTH_32F = 51,
+ HAL_PIXEL_FORMAT_DEPTH_32F_STENCIL_8 = 52,
+ HAL_PIXEL_FORMAT_STENCIL_8 = 53,
+ HAL_PIXEL_FORMAT_YCBCR_P010 = 54,
+} android_pixel_format_v1_1_t;
+
+typedef enum {
+ HAL_DATASPACE_BT2020_ITU =
+ 281411584, // ((STANDARD_BT2020 | TRANSFER_SMPTE_170M) | RANGE_LIMITED)
+ HAL_DATASPACE_BT2020_ITU_PQ =
+ 298188800, // ((STANDARD_BT2020 | TRANSFER_ST2084) | RANGE_LIMITED)
+ HAL_DATASPACE_BT2020_ITU_HLG = 302383104, // ((STANDARD_BT2020 | TRANSFER_HLG) | RANGE_LIMITED)
+ HAL_DATASPACE_BT2020_HLG = 168165376, // ((STANDARD_BT2020 | TRANSFER_HLG) | RANGE_FULL)
+} android_dataspace_v1_1_t;
+
+typedef enum {
+ HAL_COLOR_MODE_BT2020 = 10,
+ HAL_COLOR_MODE_BT2100_PQ = 11,
+ HAL_COLOR_MODE_BT2100_HLG = 12,
+} android_color_mode_v1_1_t;
+
+typedef enum {
+ HAL_RENDER_INTENT_COLORIMETRIC = 0,
+ HAL_RENDER_INTENT_ENHANCE = 1,
+ HAL_RENDER_INTENT_TONE_MAP_COLORIMETRIC = 2,
+ HAL_RENDER_INTENT_TONE_MAP_ENHANCE = 3,
+} android_render_intent_v1_1_t;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_1_EXPORTED_CONSTANTS_H_
diff --git a/libsystem/include/system/graphics-base.h b/libsystem/include/system/graphics-base.h
index 2a44faf..ea92007 100644
--- a/libsystem/include/system/graphics-base.h
+++ b/libsystem/include/system/graphics-base.h
@@ -1,141 +1,7 @@
-// This file is autogenerated by hidl-gen. Do not edit manually.
-// Source: android.hardware.graphics.common@1.0
-// Root: android.hardware:hardware/interfaces
+#ifndef SYSTEM_CORE_GRAPHICS_BASE_H_
+#define SYSTEM_CORE_GRAPHICS_BASE_H_
-#ifndef HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_0_EXPORTED_CONSTANTS_H_
-#define HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_0_EXPORTED_CONSTANTS_H_
+#include "graphics-base-v1.0.h"
+#include "graphics-base-v1.1.h"
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef enum {
- HAL_PIXEL_FORMAT_RGBA_8888 = 1,
- HAL_PIXEL_FORMAT_RGBX_8888 = 2,
- HAL_PIXEL_FORMAT_RGB_888 = 3,
- HAL_PIXEL_FORMAT_RGB_565 = 4,
- HAL_PIXEL_FORMAT_BGRA_8888 = 5,
- HAL_PIXEL_FORMAT_RGBA_1010102 = 43, // 0x2B
- HAL_PIXEL_FORMAT_RGBA_FP16 = 22, // 0x16
- HAL_PIXEL_FORMAT_YV12 = 842094169, // 0x32315659
- HAL_PIXEL_FORMAT_Y8 = 538982489, // 0x20203859
- HAL_PIXEL_FORMAT_Y16 = 540422489, // 0x20363159
- HAL_PIXEL_FORMAT_RAW16 = 32, // 0x20
- HAL_PIXEL_FORMAT_RAW10 = 37, // 0x25
- HAL_PIXEL_FORMAT_RAW12 = 38, // 0x26
- HAL_PIXEL_FORMAT_RAW_OPAQUE = 36, // 0x24
- HAL_PIXEL_FORMAT_BLOB = 33, // 0x21
- HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED = 34, // 0x22
- HAL_PIXEL_FORMAT_YCBCR_420_888 = 35, // 0x23
- HAL_PIXEL_FORMAT_YCBCR_422_888 = 39, // 0x27
- HAL_PIXEL_FORMAT_YCBCR_444_888 = 40, // 0x28
- HAL_PIXEL_FORMAT_FLEX_RGB_888 = 41, // 0x29
- HAL_PIXEL_FORMAT_FLEX_RGBA_8888 = 42, // 0x2A
- HAL_PIXEL_FORMAT_YCBCR_422_SP = 16, // 0x10
- HAL_PIXEL_FORMAT_YCRCB_420_SP = 17, // 0x11
- HAL_PIXEL_FORMAT_YCBCR_422_I = 20, // 0x14
- HAL_PIXEL_FORMAT_JPEG = 256, // 0x100
-} android_pixel_format_t;
-
-typedef enum {
- HAL_TRANSFORM_FLIP_H = 1, // 0x01
- HAL_TRANSFORM_FLIP_V = 2, // 0x02
- HAL_TRANSFORM_ROT_90 = 4, // 0x04
- HAL_TRANSFORM_ROT_180 = 3, // 0x03
- HAL_TRANSFORM_ROT_270 = 7, // 0x07
-} android_transform_t;
-
-typedef enum {
- HAL_DATASPACE_UNKNOWN = 0, // 0x0
- HAL_DATASPACE_ARBITRARY = 1, // 0x1
- HAL_DATASPACE_STANDARD_SHIFT = 16,
- HAL_DATASPACE_STANDARD_MASK = 4128768, // (63 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_UNSPECIFIED = 0, // (0 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_BT709 = 65536, // (1 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_BT601_625 = 131072, // (2 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED = 196608, // (3 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_BT601_525 = 262144, // (4 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED = 327680, // (5 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_BT2020 = 393216, // (6 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE = 458752, // (7 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_BT470M = 524288, // (8 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_FILM = 589824, // (9 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_DCI_P3 = 655360, // (10 << STANDARD_SHIFT)
- HAL_DATASPACE_STANDARD_ADOBE_RGB = 720896, // (11 << STANDARD_SHIFT)
- HAL_DATASPACE_TRANSFER_SHIFT = 22,
- HAL_DATASPACE_TRANSFER_MASK = 130023424, // (31 << TRANSFER_SHIFT)
- HAL_DATASPACE_TRANSFER_UNSPECIFIED = 0, // (0 << TRANSFER_SHIFT)
- HAL_DATASPACE_TRANSFER_LINEAR = 4194304, // (1 << TRANSFER_SHIFT)
- HAL_DATASPACE_TRANSFER_SRGB = 8388608, // (2 << TRANSFER_SHIFT)
- HAL_DATASPACE_TRANSFER_SMPTE_170M = 12582912, // (3 << TRANSFER_SHIFT)
- HAL_DATASPACE_TRANSFER_GAMMA2_2 = 16777216, // (4 << TRANSFER_SHIFT)
- HAL_DATASPACE_TRANSFER_GAMMA2_6 = 20971520, // (5 << TRANSFER_SHIFT)
- HAL_DATASPACE_TRANSFER_GAMMA2_8 = 25165824, // (6 << TRANSFER_SHIFT)
- HAL_DATASPACE_TRANSFER_ST2084 = 29360128, // (7 << TRANSFER_SHIFT)
- HAL_DATASPACE_TRANSFER_HLG = 33554432, // (8 << TRANSFER_SHIFT)
- HAL_DATASPACE_RANGE_SHIFT = 27,
- HAL_DATASPACE_RANGE_MASK = 939524096, // (7 << RANGE_SHIFT)
- HAL_DATASPACE_RANGE_UNSPECIFIED = 0, // (0 << RANGE_SHIFT)
- HAL_DATASPACE_RANGE_FULL = 134217728, // (1 << RANGE_SHIFT)
- HAL_DATASPACE_RANGE_LIMITED = 268435456, // (2 << RANGE_SHIFT)
- HAL_DATASPACE_RANGE_EXTENDED = 402653184, // (3 << RANGE_SHIFT)
- HAL_DATASPACE_SRGB_LINEAR = 512, // 0x200
- HAL_DATASPACE_V0_SRGB_LINEAR = 138477568, // ((STANDARD_BT709 | TRANSFER_LINEAR) | RANGE_FULL)
- HAL_DATASPACE_V0_SCRGB_LINEAR = 406913024, // ((STANDARD_BT709 | TRANSFER_LINEAR) | RANGE_EXTENDED)
- HAL_DATASPACE_SRGB = 513, // 0x201
- HAL_DATASPACE_V0_SRGB = 142671872, // ((STANDARD_BT709 | TRANSFER_SRGB) | RANGE_FULL)
- HAL_DATASPACE_V0_SCRGB = 411107328, // ((STANDARD_BT709 | TRANSFER_SRGB) | RANGE_EXTENDED)
- HAL_DATASPACE_JFIF = 257, // 0x101
- HAL_DATASPACE_V0_JFIF = 146931712, // ((STANDARD_BT601_625 | TRANSFER_SMPTE_170M) | RANGE_FULL)
- HAL_DATASPACE_BT601_625 = 258, // 0x102
- HAL_DATASPACE_V0_BT601_625 = 281149440, // ((STANDARD_BT601_625 | TRANSFER_SMPTE_170M) | RANGE_LIMITED)
- HAL_DATASPACE_BT601_525 = 259, // 0x103
- HAL_DATASPACE_V0_BT601_525 = 281280512, // ((STANDARD_BT601_525 | TRANSFER_SMPTE_170M) | RANGE_LIMITED)
- HAL_DATASPACE_BT709 = 260, // 0x104
- HAL_DATASPACE_V0_BT709 = 281083904, // ((STANDARD_BT709 | TRANSFER_SMPTE_170M) | RANGE_LIMITED)
- HAL_DATASPACE_DCI_P3_LINEAR = 139067392, // ((STANDARD_DCI_P3 | TRANSFER_LINEAR) | RANGE_FULL)
- HAL_DATASPACE_DCI_P3 = 155844608, // ((STANDARD_DCI_P3 | TRANSFER_GAMMA2_6) | RANGE_FULL)
- HAL_DATASPACE_DISPLAY_P3_LINEAR = 139067392, // ((STANDARD_DCI_P3 | TRANSFER_LINEAR) | RANGE_FULL)
- HAL_DATASPACE_DISPLAY_P3 = 143261696, // ((STANDARD_DCI_P3 | TRANSFER_SRGB) | RANGE_FULL)
- HAL_DATASPACE_ADOBE_RGB = 151715840, // ((STANDARD_ADOBE_RGB | TRANSFER_GAMMA2_2) | RANGE_FULL)
- HAL_DATASPACE_BT2020_LINEAR = 138805248, // ((STANDARD_BT2020 | TRANSFER_LINEAR) | RANGE_FULL)
- HAL_DATASPACE_BT2020 = 147193856, // ((STANDARD_BT2020 | TRANSFER_SMPTE_170M) | RANGE_FULL)
- HAL_DATASPACE_BT2020_PQ = 163971072, // ((STANDARD_BT2020 | TRANSFER_ST2084) | RANGE_FULL)
- HAL_DATASPACE_DEPTH = 4096, // 0x1000
- HAL_DATASPACE_SENSOR = 4097, // 0x1001
-} android_dataspace_t;
-
-typedef enum {
- HAL_COLOR_MODE_NATIVE = 0,
- HAL_COLOR_MODE_STANDARD_BT601_625 = 1,
- HAL_COLOR_MODE_STANDARD_BT601_625_UNADJUSTED = 2,
- HAL_COLOR_MODE_STANDARD_BT601_525 = 3,
- HAL_COLOR_MODE_STANDARD_BT601_525_UNADJUSTED = 4,
- HAL_COLOR_MODE_STANDARD_BT709 = 5,
- HAL_COLOR_MODE_DCI_P3 = 6,
- HAL_COLOR_MODE_SRGB = 7,
- HAL_COLOR_MODE_ADOBE_RGB = 8,
- HAL_COLOR_MODE_DISPLAY_P3 = 9,
-} android_color_mode_t;
-
-typedef enum {
- HAL_COLOR_TRANSFORM_IDENTITY = 0,
- HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX = 1,
- HAL_COLOR_TRANSFORM_VALUE_INVERSE = 2,
- HAL_COLOR_TRANSFORM_GRAYSCALE = 3,
- HAL_COLOR_TRANSFORM_CORRECT_PROTANOPIA = 4,
- HAL_COLOR_TRANSFORM_CORRECT_DEUTERANOPIA = 5,
- HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA = 6,
-} android_color_transform_t;
-
-typedef enum {
- HAL_HDR_DOLBY_VISION = 1,
- HAL_HDR_HDR10 = 2,
- HAL_HDR_HLG = 3,
-} android_hdr_t;
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_0_EXPORTED_CONSTANTS_H_
+#endif // SYSTEM_CORE_GRAPHICS_BASE_H_
diff --git a/libsystem/include/system/graphics-sw.h b/libsystem/include/system/graphics-sw.h
new file mode 100644
index 0000000..9e1a88e
--- /dev/null
+++ b/libsystem/include/system/graphics-sw.h
@@ -0,0 +1,16 @@
+#ifndef SYSTEM_CORE_GRAPHICS_SW_H_
+#define SYSTEM_CORE_GRAPHICS_SW_H_
+
+/* Software formats not in the HAL definitions. */
+typedef enum {
+ HAL_PIXEL_FORMAT_YCBCR_422_888 = 39, // 0x27
+ HAL_PIXEL_FORMAT_YCBCR_444_888 = 40, // 0x28
+ HAL_PIXEL_FORMAT_FLEX_RGB_888 = 41, // 0x29
+ HAL_PIXEL_FORMAT_FLEX_RGBA_8888 = 42, // 0x2A
+} android_pixel_format_sw_t;
+
+/* for compatibility */
+#define HAL_PIXEL_FORMAT_YCbCr_422_888 HAL_PIXEL_FORMAT_YCBCR_422_888
+#define HAL_PIXEL_FORMAT_YCbCr_444_888 HAL_PIXEL_FORMAT_YCBCR_444_888
+
+#endif // SYSTEM_CORE_GRAPHICS_SW_H_
diff --git a/libsystem/include/system/graphics.h b/libsystem/include/system/graphics.h
index 1a99187..1b6060a 100644
--- a/libsystem/include/system/graphics.h
+++ b/libsystem/include/system/graphics.h
@@ -25,6 +25,7 @@
* generated.
*/
#include "graphics-base.h"
+#include "graphics-sw.h"
#ifdef __cplusplus
extern "C" {
@@ -32,8 +33,6 @@
/* for compatibility */
#define HAL_PIXEL_FORMAT_YCbCr_420_888 HAL_PIXEL_FORMAT_YCBCR_420_888
-#define HAL_PIXEL_FORMAT_YCbCr_422_888 HAL_PIXEL_FORMAT_YCBCR_422_888
-#define HAL_PIXEL_FORMAT_YCbCr_444_888 HAL_PIXEL_FORMAT_YCBCR_444_888
#define HAL_PIXEL_FORMAT_YCbCr_422_SP HAL_PIXEL_FORMAT_YCBCR_422_SP
#define HAL_PIXEL_FORMAT_YCrCb_420_SP HAL_PIXEL_FORMAT_YCRCB_420_SP
#define HAL_PIXEL_FORMAT_YCbCr_422_I HAL_PIXEL_FORMAT_YCBCR_422_I
@@ -257,6 +256,11 @@
float minLuminance;
};
+struct android_cta861_3_metadata {
+ float maxContentLightLevel;
+ float maxFrameAverageLightLevel;
+};
+
#ifdef __cplusplus
}
#endif
diff --git a/libsysutils/include/sysutils/FrameworkClient.h b/libsysutils/include/sysutils/FrameworkClient.h
deleted file mode 100644
index 4a3f0de..0000000
--- a/libsysutils/include/sysutils/FrameworkClient.h
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef _FRAMEWORK_CLIENT_H
-#define _FRAMEWORK_CLIENT_H
-
-#include "List.h"
-
-#include <pthread.h>
-
-class FrameworkClient {
- int mSocket;
- pthread_mutex_t mWriteMutex;
-
-public:
- FrameworkClient(int sock);
- virtual ~FrameworkClient() {}
-
- int sendMsg(const char *msg);
- int sendMsg(const char *msg, const char *data);
-};
-
-typedef android::sysutils::List<FrameworkClient *> FrameworkClientCollection;
-#endif
diff --git a/libsysutils/include/sysutils/OWNERS b/libsysutils/include/sysutils/OWNERS
new file mode 100644
index 0000000..b78918e
--- /dev/null
+++ b/libsysutils/include/sysutils/OWNERS
@@ -0,0 +1,2 @@
+per-file Netlink* = ek@google.com
+per-file Netlink* = lorenzo@google.com
diff --git a/libsysutils/src/FrameworkClient.cpp b/libsysutils/src/FrameworkClient.cpp
deleted file mode 100644
index 72b3d0a..0000000
--- a/libsysutils/src/FrameworkClient.cpp
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2009-2016 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.
- */
-
-#define LOG_TAG "FrameworkClient"
-
-#include <alloca.h>
-#include <errno.h>
-#include <pthread.h>
-#include <sys/types.h>
-
-#include <android/log.h>
-#include <sysutils/FrameworkClient.h>
-
-FrameworkClient::FrameworkClient(int socket) {
- mSocket = socket;
- pthread_mutex_init(&mWriteMutex, NULL);
-}
-
-int FrameworkClient::sendMsg(const char *msg) {
- int ret;
- if (mSocket < 0) {
- errno = EHOSTUNREACH;
- return -1;
- }
-
- pthread_mutex_lock(&mWriteMutex);
- ret = TEMP_FAILURE_RETRY(write(mSocket, msg, strlen(msg) +1));
- if (ret < 0) {
- SLOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
- }
- pthread_mutex_unlock(&mWriteMutex);
- return 0;
-}
-
-int FrameworkClient::sendMsg(const char *msg, const char *data) {
- size_t bufflen = strlen(msg) + strlen(data) + 1;
- char *buffer = (char *) alloca(bufflen);
- if (!buffer) {
- errno = -ENOMEM;
- return -1;
- }
- snprintf(buffer, bufflen, "%s%s", msg, data);
- return sendMsg(buffer);
-}
-
diff --git a/libsysutils/src/FrameworkListener.cpp b/libsysutils/src/FrameworkListener.cpp
index 87e2684..835e226 100644
--- a/libsysutils/src/FrameworkListener.cpp
+++ b/libsysutils/src/FrameworkListener.cpp
@@ -42,7 +42,7 @@
FrameworkListener::FrameworkListener(int sock) :
SocketListener(sock, true) {
- init(NULL, false);
+ init(nullptr, false);
}
void FrameworkListener::init(const char *socketName UNUSED, bool withSeq) {
@@ -154,7 +154,7 @@
if (!haveCmdNum) {
char *endptr;
int cmdNum = (int)strtol(tmp, &endptr, 0);
- if (endptr == NULL || *endptr != '\0') {
+ if (endptr == nullptr || *endptr != '\0') {
cli->sendMsg(500, "Invalid sequence number", false);
goto out;
}
diff --git a/libsysutils/src/NetlinkEvent.cpp b/libsysutils/src/NetlinkEvent.cpp
index 7f92904..24ea7aa 100644
--- a/libsysutils/src/NetlinkEvent.cpp
+++ b/libsysutils/src/NetlinkEvent.cpp
@@ -45,8 +45,8 @@
NetlinkEvent::NetlinkEvent() {
mAction = Action::kUnknown;
memset(mParams, 0, sizeof(mParams));
- mPath = NULL;
- mSubsystem = NULL;
+ mPath = nullptr;
+ mSubsystem = nullptr;
}
NetlinkEvent::~NetlinkEvent() {
@@ -89,7 +89,7 @@
NL_EVENT_RTM_NAME(LOCAL_QLOG_NL_EVENT);
NL_EVENT_RTM_NAME(LOCAL_NFLOG_PACKET);
default:
- return NULL;
+ return nullptr;
}
#undef NL_EVENT_RTM_NAME
}
@@ -137,6 +137,12 @@
switch(rta->rta_type) {
case IFLA_IFNAME:
asprintf(&mParams[0], "INTERFACE=%s", (char *) RTA_DATA(rta));
+ // We can get the interface change information from sysfs update
+ // already. But in case we missed those message when devices start.
+ // We do a update again when received a kLinkUp event. To make
+ // the message consistent, use IFINDEX here as well since sysfs
+ // uses IFINDEX.
+ asprintf(&mParams[1], "IFINDEX=%d", ifi->ifi_index);
mAction = (ifi->ifi_flags & IFF_LOWER_UP) ? Action::kLinkUp :
Action::kLinkDown;
mSubsystem = strdup("net");
@@ -152,7 +158,7 @@
*/
bool NetlinkEvent::parseIfAddrMessage(const struct nlmsghdr *nh) {
struct ifaddrmsg *ifaddr = (struct ifaddrmsg *) NLMSG_DATA(nh);
- struct ifa_cacheinfo *cacheinfo = NULL;
+ struct ifa_cacheinfo *cacheinfo = nullptr;
char addrstr[INET6_ADDRSTRLEN] = "";
char ifname[IFNAMSIZ] = "";
@@ -233,12 +239,13 @@
asprintf(&mParams[1], "INTERFACE=%s", ifname);
asprintf(&mParams[2], "FLAGS=%u", ifaddr->ifa_flags);
asprintf(&mParams[3], "SCOPE=%u", ifaddr->ifa_scope);
+ asprintf(&mParams[4], "IFINDEX=%u", ifaddr->ifa_index);
if (cacheinfo) {
- asprintf(&mParams[4], "PREFERRED=%u", cacheinfo->ifa_prefered);
- asprintf(&mParams[5], "VALID=%u", cacheinfo->ifa_valid);
- asprintf(&mParams[6], "CSTAMP=%u", cacheinfo->cstamp);
- asprintf(&mParams[7], "TSTAMP=%u", cacheinfo->tstamp);
+ asprintf(&mParams[5], "PREFERRED=%u", cacheinfo->ifa_prefered);
+ asprintf(&mParams[6], "VALID=%u", cacheinfo->ifa_valid);
+ asprintf(&mParams[7], "CSTAMP=%u", cacheinfo->cstamp);
+ asprintf(&mParams[8], "TSTAMP=%u", cacheinfo->tstamp);
}
return true;
@@ -279,7 +286,7 @@
bool NetlinkEvent::parseNfPacketMessage(struct nlmsghdr *nh) {
int uid = -1;
int len = 0;
- char* raw = NULL;
+ char* raw = nullptr;
struct nlattr* uid_attr = findNlAttr(nh, sizeof(struct genlmsghdr), NFULA_UID);
if (uid_attr) {
@@ -577,7 +584,7 @@
(prefixlen == 0 || !memcmp(str, prefix, prefixlen))) {
return str + prefixlen;
} else {
- return NULL;
+ return nullptr;
}
}
@@ -618,16 +625,16 @@
first = 0;
} else {
const char* a;
- if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != NULL) {
+ if ((a = HAS_CONST_PREFIX(s, end, "ACTION=")) != nullptr) {
if (!strcmp(a, "add"))
mAction = Action::kAdd;
else if (!strcmp(a, "remove"))
mAction = Action::kRemove;
else if (!strcmp(a, "change"))
mAction = Action::kChange;
- } else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != NULL) {
+ } else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != nullptr) {
mSeq = atoi(a);
- } else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != NULL) {
+ } else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != nullptr) {
mSubsystem = strdup(a);
} else if (param_idx < NL_PARAMS_MAX) {
mParams[param_idx++] = strdup(s);
@@ -649,14 +656,14 @@
const char *NetlinkEvent::findParam(const char *paramName) {
size_t len = strlen(paramName);
- for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != NULL; ++i) {
+ for (int i = 0; i < NL_PARAMS_MAX && mParams[i] != nullptr; ++i) {
const char *ptr = mParams[i] + len;
if (!strncmp(mParams[i], paramName, len) && *ptr == '=')
return ++ptr;
}
SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName);
- return NULL;
+ return nullptr;
}
nlattr* NetlinkEvent::findNlAttr(const nlmsghdr* nh, size_t hdrlen, uint16_t attr) {
diff --git a/libsysutils/src/OWNERS b/libsysutils/src/OWNERS
new file mode 100644
index 0000000..b78918e
--- /dev/null
+++ b/libsysutils/src/OWNERS
@@ -0,0 +1,2 @@
+per-file Netlink* = ek@google.com
+per-file Netlink* = lorenzo@google.com
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp
index 971f908..0625db7 100644
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -42,8 +42,8 @@
mSocket = socket;
mSocketOwned = owned;
mUseCmdNum = useCmdNum;
- pthread_mutex_init(&mWriteMutex, NULL);
- pthread_mutex_init(&mRefCountMutex, NULL);
+ pthread_mutex_init(&mWriteMutex, nullptr);
+ pthread_mutex_init(&mRefCountMutex, nullptr);
mPid = -1;
mUid = -1;
mGid = -1;
@@ -135,9 +135,9 @@
const char *end = arg + len;
char *oldresult;
- if(result == NULL) {
+ if(result == nullptr) {
SLOGW("malloc error (%s)", strerror(errno));
- return NULL;
+ return nullptr;
}
*(current++) = '"';
diff --git a/libsysutils/src/SocketListener.cpp b/libsysutils/src/SocketListener.cpp
index 3f8f3db..0c8a688 100644
--- a/libsysutils/src/SocketListener.cpp
+++ b/libsysutils/src/SocketListener.cpp
@@ -39,7 +39,7 @@
}
SocketListener::SocketListener(int socketFd, bool listen) {
- init(NULL, socketFd, listen, false);
+ init(nullptr, socketFd, listen, false);
}
SocketListener::SocketListener(const char *socketName, bool listen, bool useCmdNum) {
@@ -51,7 +51,7 @@
mSocketName = socketName;
mSock = socketFd;
mUseCmdNum = useCmdNum;
- pthread_mutex_init(&mClientsLock, NULL);
+ pthread_mutex_init(&mClientsLock, nullptr);
mClients = new SocketClientCollection();
}
@@ -102,7 +102,7 @@
return -1;
}
- if (pthread_create(&mThread, NULL, SocketListener::threadStart, this)) {
+ if (pthread_create(&mThread, nullptr, SocketListener::threadStart, this)) {
SLOGE("pthread_create (%s)", strerror(errno));
return -1;
}
@@ -147,8 +147,8 @@
SocketListener *me = reinterpret_cast<SocketListener *>(obj);
me->runListener();
- pthread_exit(NULL);
- return NULL;
+ pthread_exit(nullptr);
+ return nullptr;
}
void SocketListener::runListener() {
@@ -183,7 +183,7 @@
}
pthread_mutex_unlock(&mClientsLock);
SLOGV("mListen=%d, max=%d, mSocketName=%s", mListen, max, mSocketName);
- if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
+ if ((rc = select(max + 1, &read_fds, nullptr, nullptr, nullptr)) < 0) {
if (errno == EINTR)
continue;
SLOGE("select failed (%s) mListen=%d, max=%d", strerror(errno), mListen, max);
diff --git a/libunwindstack/Android.bp b/libunwindstack/Android.bp
index 124c70e..be2145d 100644
--- a/libunwindstack/Android.bp
+++ b/libunwindstack/Android.bp
@@ -38,6 +38,7 @@
cc_library {
name: "libunwindstack",
vendor_available: true,
+ recovery_available: true,
vndk: {
enabled: true,
support_system_process: true,
@@ -47,6 +48,8 @@
srcs: [
"ArmExidx.cpp",
+ "DexFile.cpp",
+ "DexFiles.cpp",
"DwarfCfa.cpp",
"DwarfEhFrameWithHdr.cpp",
"DwarfMemory.cpp",
@@ -60,6 +63,7 @@
"MapInfo.cpp",
"Maps.cpp",
"Memory.cpp",
+ "LocalUnwinder.cpp",
"Regs.cpp",
"RegsArm.cpp",
"RegsArm64.cpp",
@@ -85,11 +89,21 @@
},
vendor: {
cflags: ["-DNO_LIBDEXFILE_SUPPORT"],
- exclude_static_libs: ["libunwindstack_dex"],
+ exclude_srcs: [
+ "DexFile.cpp",
+ "DexFiles.cpp",
+ ],
+ exclude_shared_libs: ["libdexfile"],
+ },
+ recovery: {
+ cflags: ["-DNO_LIBDEXFILE_SUPPORT"],
+ exclude_srcs: [
+ "DexFile.cpp",
+ "DexFiles.cpp",
+ ],
exclude_shared_libs: ["libdexfile"],
},
},
- whole_static_libs: ["libunwindstack_dex"],
arch: {
x86: {
@@ -106,6 +120,10 @@
},
},
+ static_libs: [
+ "libprocinfo",
+ ],
+
shared_libs: [
"libbase",
"libdexfile",
@@ -114,76 +132,21 @@
],
}
-// Isolate the dex file processing into a separate library. Currently,
-// it is necessary to add art include directories directly, which also
-// adds the art elf.h file in the include path, overriding the system one.
-// Work to isolate libdexfile is b/72216369.
-cc_library_static {
- name: "libunwindstack_dex",
- vendor_available: false,
- defaults: ["libunwindstack_flags"],
-
- cflags: [
- "-Wexit-time-destructors",
- ],
-
- srcs: [
- "DexFile.cpp",
- "DexFiles.cpp",
- ],
- target: {
- // Always disable optimizations for host to make it easier to debug.
- host: {
- cflags: [
- "-O0",
- "-g",
- ],
- },
- },
-
- shared_libs: [
- "libbase",
- "libdexfile",
- ],
- local_include_dirs: ["include"],
- allow_undefined_symbols: true,
-
- // libdexfile will eventually properly export headers, for now include
- // these directly.
- include_dirs: [
- "art/runtime",
- ],
-}
-
//-------------------------------------------------------------------------
// Unit Tests
//-------------------------------------------------------------------------
cc_test_library {
- name: "libunwindstack_dex_test",
- vendor_available: false,
+ name: "libunwindstack_local",
defaults: ["libunwindstack_flags"],
+ srcs: ["tests/TestLocal.cpp"],
- shared: {
- enabled: false,
- },
-
- srcs: [
- "tests/DexFileTest.cpp",
- "tests/DexFilesTest.cpp",
+ cflags: [
+ "-O0",
+ "-g",
],
- local_include_dirs: ["include"],
- allow_undefined_symbols: true,
shared_libs: [
- "libbase",
"libunwindstack",
- "libdexfile",
- ],
-
- // libdexfile will eventually properly export headers, for now include
- // these directly.
- include_dirs: [
- "art/runtime",
],
}
@@ -194,6 +157,8 @@
srcs: [
"tests/ArmExidxDecodeTest.cpp",
"tests/ArmExidxExtractTest.cpp",
+ "tests/DexFileTest.cpp",
+ "tests/DexFilesTest.cpp",
"tests/DwarfCfaLogTest.cpp",
"tests/DwarfCfaTest.cpp",
"tests/DwarfDebugFrameTest.cpp",
@@ -211,6 +176,7 @@
"tests/ElfTest.cpp",
"tests/ElfTestUtils.cpp",
"tests/JitDebugTest.cpp",
+ "tests/LocalUnwinderTest.cpp",
"tests/LogFake.cpp",
"tests/MapInfoGetElfTest.cpp",
"tests/MapInfoGetLoadBiasTest.cpp",
@@ -219,10 +185,12 @@
"tests/MemoryFake.cpp",
"tests/MemoryFileTest.cpp",
"tests/MemoryLocalTest.cpp",
+ "tests/MemoryOfflineBufferTest.cpp",
"tests/MemoryOfflineTest.cpp",
"tests/MemoryRangeTest.cpp",
"tests/MemoryRemoteTest.cpp",
"tests/MemoryTest.cpp",
+ "tests/RegsInfoTest.cpp",
"tests/RegsIterateTest.cpp",
"tests/RegsStepIfSignalHandlerTest.cpp",
"tests/RegsTest.cpp",
@@ -242,26 +210,33 @@
"liblog",
"liblzma",
"libunwindstack",
+ "libdexfile",
],
static_libs: [
"libgmock",
],
- whole_static_libs: ["libunwindstack_dex_test"],
-
+ test_suites: ["device-tests"],
data: [
"tests/files/elf32.xz",
"tests/files/elf64.xz",
+ "tests/files/offline/art_quick_osr_stub_arm/*",
"tests/files/offline/bad_eh_frame_hdr_arm64/*",
"tests/files/offline/debug_frame_first_x86/*",
+ "tests/files/offline/debug_frame_load_bias_arm/*",
"tests/files/offline/eh_frame_hdr_begin_x86_64/*",
"tests/files/offline/jit_debug_arm/*",
"tests/files/offline/jit_debug_x86/*",
+ "tests/files/offline/jit_map_arm/*",
"tests/files/offline/gnu_debugdata_arm/*",
+ "tests/files/offline/offset_arm/*",
"tests/files/offline/straddle_arm/*",
"tests/files/offline/straddle_arm64/*",
],
+ required: [
+ "libunwindstack_local",
+ ],
}
//-------------------------------------------------------------------------
@@ -276,6 +251,15 @@
"libbase",
"liblzma",
],
+ target: {
+ // Always disable optimizations for host to make it easier to debug.
+ host: {
+ cflags: [
+ "-O0",
+ "-g",
+ ],
+ },
+ },
}
cc_binary {
diff --git a/libunwindstack/ArmExidx.cpp b/libunwindstack/ArmExidx.cpp
index 6e397e3..818f5d1 100644
--- a/libunwindstack/ArmExidx.cpp
+++ b/libunwindstack/ArmExidx.cpp
@@ -31,6 +31,8 @@
namespace unwindstack {
+static constexpr uint8_t LOG_CFA_REG = 64;
+
void ArmExidx::LogRawData() {
std::string log_str("Raw Data:");
for (const uint8_t data : data_) {
@@ -63,8 +65,10 @@
if (data == 1) {
// This is a CANT UNWIND entry.
status_ = ARM_STATUS_NO_UNWIND;
- if (log_) {
- log(log_indent_, "Raw Data: 0x00 0x00 0x00 0x01");
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ log(log_indent_, "Raw Data: 0x00 0x00 0x00 0x01");
+ }
log(log_indent_, "[cantunwind]");
}
return false;
@@ -86,7 +90,7 @@
// If this didn't end with a finish op, add one.
data_.push_back(ARM_OP_FINISH);
}
- if (log_) {
+ if (log_type_ == ARM_LOG_FULL) {
LogRawData();
}
return true;
@@ -163,7 +167,7 @@
data_.push_back(ARM_OP_FINISH);
}
- if (log_) {
+ if (log_type_ == ARM_LOG_FULL) {
LogRawData();
}
return true;
@@ -190,32 +194,45 @@
registers |= byte;
if (registers == 0) {
// 10000000 00000000: Refuse to unwind
- if (log_) {
+ if (log_type_ != ARM_LOG_NONE) {
log(log_indent_, "Refuse to unwind");
}
status_ = ARM_STATUS_NO_UNWIND;
return false;
}
// 1000iiii iiiiiiii: Pop up to 12 integer registers under masks {r15-r12}, {r11-r4}
- if (log_) {
- bool add_comma = false;
- std::string msg = "pop {";
- for (size_t i = 0; i < 12; i++) {
- if (registers & (1 << i)) {
- if (add_comma) {
- msg += ", ";
+ registers <<= 4;
+
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ bool add_comma = false;
+ std::string msg = "pop {";
+ for (size_t reg = 4; reg < 16; reg++) {
+ if (registers & (1 << reg)) {
+ if (add_comma) {
+ msg += ", ";
+ }
+ msg += android::base::StringPrintf("r%zu", reg);
+ add_comma = true;
}
- msg += android::base::StringPrintf("r%zu", i + 4);
- add_comma = true;
+ }
+ log(log_indent_, "%s}", msg.c_str());
+ } else {
+ uint32_t cfa_offset = __builtin_popcount(registers) * 4;
+ log_cfa_offset_ += cfa_offset;
+ for (size_t reg = 4; reg < 16; reg++) {
+ if (registers & (1 << reg)) {
+ log_regs_[reg] = cfa_offset;
+ cfa_offset -= 4;
+ }
}
}
- log(log_indent_, "%s}", msg.c_str());
+
if (log_skip_execution_) {
return true;
}
}
- registers <<= 4;
for (size_t reg = 4; reg < 16; reg++) {
if (registers & (1 << reg)) {
if (!process_memory_->Read32(cfa_, &(*regs_)[reg])) {
@@ -246,15 +263,20 @@
if (bits == 13 || bits == 15) {
// 10011101: Reserved as prefix for ARM register to register moves
// 10011111: Reserved as prefix for Intel Wireless MMX register to register moves
- if (log_) {
+ if (log_type_ != ARM_LOG_NONE) {
log(log_indent_, "[Reserved]");
}
status_ = ARM_STATUS_RESERVED;
return false;
}
// 1001nnnn: Set vsp = r[nnnn] (nnnn != 13, 15)
- if (log_) {
- log(log_indent_, "vsp = r%d", bits);
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ log(log_indent_, "vsp = r%d", bits);
+ } else {
+ log_regs_[LOG_CFA_REG] = bits;
+ }
+
if (log_skip_execution_) {
return true;
}
@@ -270,17 +292,36 @@
// 10100nnn: Pop r4-r[4+nnn]
// 10101nnn: Pop r4-r[4+nnn], r14
- if (log_) {
- std::string msg = "pop {r4";
+ if (log_type_ != ARM_LOG_NONE) {
uint8_t end_reg = byte & 0x7;
- if (end_reg) {
- msg += android::base::StringPrintf("-r%d", 4 + end_reg);
- }
- if (byte & 0x8) {
- log(log_indent_, "%s, r14}", msg.c_str());
+ if (log_type_ == ARM_LOG_FULL) {
+ std::string msg = "pop {r4";
+ if (end_reg) {
+ msg += android::base::StringPrintf("-r%d", 4 + end_reg);
+ }
+ if (byte & 0x8) {
+ log(log_indent_, "%s, r14}", msg.c_str());
+ } else {
+ log(log_indent_, "%s}", msg.c_str());
+ }
} else {
- log(log_indent_, "%s}", msg.c_str());
+ end_reg += 4;
+ uint32_t cfa_offset = (end_reg - 3) * 4;
+ if (byte & 0x8) {
+ cfa_offset += 4;
+ }
+ log_cfa_offset_ += cfa_offset;
+
+ for (uint8_t reg = 4; reg <= end_reg; reg++) {
+ log_regs_[reg] = cfa_offset;
+ cfa_offset -= 4;
+ }
+
+ if (byte & 0x8) {
+ log_regs_[14] = cfa_offset;
+ }
}
+
if (log_skip_execution_) {
return true;
}
@@ -307,8 +348,11 @@
inline bool ArmExidx::DecodePrefix_10_11_0000() {
// 10110000: Finish
- if (log_) {
- log(log_indent_, "finish");
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ log(log_indent_, "finish");
+ }
+
if (log_skip_execution_) {
status_ = ARM_STATUS_FINISH;
return false;
@@ -326,7 +370,7 @@
if (byte == 0) {
// 10110001 00000000: Spare
- if (log_) {
+ if (log_type_ != ARM_LOG_NONE) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
@@ -334,7 +378,7 @@
}
if (byte >> 4) {
// 10110001 xxxxyyyy: Spare (xxxx != 0000)
- if (log_) {
+ if (log_type_ != ARM_LOG_NONE) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
@@ -342,19 +386,32 @@
}
// 10110001 0000iiii: Pop integer registers under mask {r3, r2, r1, r0}
- if (log_) {
- bool add_comma = false;
- std::string msg = "pop {";
- for (size_t i = 0; i < 4; i++) {
- if (byte & (1 << i)) {
- if (add_comma) {
- msg += ", ";
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ bool add_comma = false;
+ std::string msg = "pop {";
+ for (size_t i = 0; i < 4; i++) {
+ if (byte & (1 << i)) {
+ if (add_comma) {
+ msg += ", ";
+ }
+ msg += android::base::StringPrintf("r%zu", i);
+ add_comma = true;
}
- msg += android::base::StringPrintf("r%zu", i);
- add_comma = true;
+ }
+ log(log_indent_, "%s}", msg.c_str());
+ } else {
+ byte &= 0xf;
+ uint32_t cfa_offset = __builtin_popcount(byte) * 4;
+ log_cfa_offset_ += cfa_offset;
+ for (size_t reg = 0; reg < 4; reg++) {
+ if (byte & (1 << reg)) {
+ log_regs_[reg] = cfa_offset;
+ cfa_offset -= 4;
+ }
}
}
- log(log_indent_, "%s}", msg.c_str());
+
if (log_skip_execution_) {
return true;
}
@@ -373,6 +430,15 @@
return true;
}
+inline void ArmExidx::AdjustRegisters(int32_t offset) {
+ for (auto& entry : log_regs_) {
+ if (entry.first >= LOG_CFA_REG) {
+ break;
+ }
+ entry.second += offset;
+ }
+}
+
inline bool ArmExidx::DecodePrefix_10_11_0010() {
// 10110010 uleb128: vsp = vsp + 0x204 + (uleb128 << 2)
uint32_t result = 0;
@@ -387,8 +453,15 @@
shift += 7;
} while (byte & 0x80);
result <<= 2;
- if (log_) {
- log(log_indent_, "vsp = vsp + %d", 0x204 + result);
+ if (log_type_ != ARM_LOG_NONE) {
+ int32_t cfa_offset = 0x204 + result;
+ if (log_type_ == ARM_LOG_FULL) {
+ log(log_indent_, "vsp = vsp + %d", cfa_offset);
+ } else {
+ log_cfa_offset_ += cfa_offset;
+ }
+ AdjustRegisters(cfa_offset);
+
if (log_skip_execution_) {
return true;
}
@@ -404,14 +477,20 @@
return false;
}
- if (log_) {
+ if (log_type_ != ARM_LOG_NONE) {
uint8_t start_reg = byte >> 4;
- std::string msg = android::base::StringPrintf("pop {d%d", start_reg);
uint8_t end_reg = start_reg + (byte & 0xf);
- if (end_reg) {
- msg += android::base::StringPrintf("-d%d", end_reg);
+
+ if (log_type_ == ARM_LOG_FULL) {
+ std::string msg = android::base::StringPrintf("pop {d%d", start_reg);
+ if (end_reg) {
+ msg += android::base::StringPrintf("-d%d", end_reg);
+ }
+ log(log_indent_, "%s}", msg.c_str());
+ } else {
+ log(log_indent_, "Unsupported DX register display");
}
- log(log_indent_, "%s}", msg.c_str());
+
if (log_skip_execution_) {
return true;
}
@@ -422,7 +501,7 @@
inline bool ArmExidx::DecodePrefix_10_11_01nn() {
// 101101nn: Spare
- if (log_) {
+ if (log_type_ != ARM_LOG_NONE) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
@@ -433,13 +512,18 @@
CHECK((byte & ~0x07) == 0xb8);
// 10111nnn: Pop VFP double-precision registers D[8]-D[8+nnn] by FSTMFDX
- if (log_) {
- std::string msg = "pop {d8";
- uint8_t last_reg = (byte & 0x7);
- if (last_reg) {
- msg += android::base::StringPrintf("-d%d", last_reg + 8);
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ uint8_t last_reg = (byte & 0x7);
+ std::string msg = "pop {d8";
+ if (last_reg) {
+ msg += android::base::StringPrintf("-d%d", last_reg + 8);
+ }
+ log(log_indent_, "%s}", msg.c_str());
+ } else {
+ log(log_indent_, "Unsupported DX register display");
}
- log(log_indent_, "%s}", msg.c_str());
+
if (log_skip_execution_) {
return true;
}
@@ -489,14 +573,19 @@
}
// 11000110 sssscccc: Intel Wireless MMX pop wR[ssss]-wR[ssss+cccc]
- if (log_) {
- uint8_t start_reg = byte >> 4;
- std::string msg = android::base::StringPrintf("pop {wR%d", start_reg);
- uint8_t end_reg = byte & 0xf;
- if (end_reg) {
- msg += android::base::StringPrintf("-wR%d", start_reg + end_reg);
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ uint8_t start_reg = byte >> 4;
+ std::string msg = android::base::StringPrintf("pop {wR%d", start_reg);
+ uint8_t end_reg = byte & 0xf;
+ if (end_reg) {
+ msg += android::base::StringPrintf("-wR%d", start_reg + end_reg);
+ }
+ log(log_indent_, "%s}", msg.c_str());
+ } else {
+ log(log_indent_, "Unsupported wRX register display");
}
- log(log_indent_, "%s}", msg.c_str());
+
if (log_skip_execution_) {
return true;
}
@@ -510,32 +599,40 @@
if (byte == 0) {
// 11000111 00000000: Spare
- if (log_) {
+ if (log_type_ != ARM_LOG_NONE) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
return false;
} else if ((byte >> 4) == 0) {
// 11000111 0000iiii: Intel Wireless MMX pop wCGR registers {wCGR0,1,2,3}
- if (log_) {
- bool add_comma = false;
- std::string msg = "pop {";
- for (size_t i = 0; i < 4; i++) {
- if (byte & (1 << i)) {
- if (add_comma) {
- msg += ", ";
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ bool add_comma = false;
+ std::string msg = "pop {";
+ for (size_t i = 0; i < 4; i++) {
+ if (byte & (1 << i)) {
+ if (add_comma) {
+ msg += ", ";
+ }
+ msg += android::base::StringPrintf("wCGR%zu", i);
+ add_comma = true;
}
- msg += android::base::StringPrintf("wCGR%zu", i);
- add_comma = true;
}
+ log(log_indent_, "%s}", msg.c_str());
+ } else {
+ log(log_indent_, "Unsupported wCGR register display");
}
- log(log_indent_, "%s}", msg.c_str());
+
+ if (log_skip_execution_) {
+ return true;
+ }
}
// Only update the cfa.
cfa_ += __builtin_popcount(byte) * 4;
} else {
// 11000111 xxxxyyyy: Spare (xxxx != 0000)
- if (log_) {
+ if (log_type_ != ARM_LOG_NONE) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
@@ -543,13 +640,18 @@
}
} else {
// 11000nnn: Intel Wireless MMX pop wR[10]-wR[10+nnn] (nnn != 6, 7)
- if (log_) {
- std::string msg = "pop {wR10";
- uint8_t nnn = byte & 0x7;
- if (nnn) {
- msg += android::base::StringPrintf("-wR%d", 10 + nnn);
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ std::string msg = "pop {wR10";
+ uint8_t nnn = byte & 0x7;
+ if (nnn) {
+ msg += android::base::StringPrintf("-wR%d", 10 + nnn);
+ }
+ log(log_indent_, "%s}", msg.c_str());
+ } else {
+ log(log_indent_, "Unsupported wRX register display");
}
- log(log_indent_, "%s}", msg.c_str());
+
if (log_skip_execution_) {
return true;
}
@@ -570,14 +672,19 @@
return false;
}
- if (log_) {
- uint8_t start_reg = byte >> 4;
- std::string msg = android::base::StringPrintf("pop {d%d", 16 + start_reg);
- uint8_t end_reg = byte & 0xf;
- if (end_reg) {
- msg += android::base::StringPrintf("-d%d", 16 + start_reg + end_reg);
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ uint8_t start_reg = byte >> 4;
+ std::string msg = android::base::StringPrintf("pop {d%d", 16 + start_reg);
+ uint8_t end_reg = byte & 0xf;
+ if (end_reg) {
+ msg += android::base::StringPrintf("-d%d", 16 + start_reg + end_reg);
+ }
+ log(log_indent_, "%s}", msg.c_str());
+ } else {
+ log(log_indent_, "Unsupported DX register display");
}
- log(log_indent_, "%s}", msg.c_str());
+
if (log_skip_execution_) {
return true;
}
@@ -590,14 +697,19 @@
return false;
}
- if (log_) {
- uint8_t start_reg = byte >> 4;
- std::string msg = android::base::StringPrintf("pop {d%d", start_reg);
- uint8_t end_reg = byte & 0xf;
- if (end_reg) {
- msg += android::base::StringPrintf("-d%d", start_reg + end_reg);
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ uint8_t start_reg = byte >> 4;
+ std::string msg = android::base::StringPrintf("pop {d%d", start_reg);
+ uint8_t end_reg = byte & 0xf;
+ if (end_reg) {
+ msg += android::base::StringPrintf("-d%d", start_reg + end_reg);
+ }
+ log(log_indent_, "%s}", msg.c_str());
+ } else {
+ log(log_indent_, "Unsupported DX register display");
}
- log(log_indent_, "%s}", msg.c_str());
+
if (log_skip_execution_) {
return true;
}
@@ -606,7 +718,7 @@
cfa_ += (byte & 0xf) * 8 + 8;
} else {
// 11001yyy: Spare (yyy != 000, 001)
- if (log_) {
+ if (log_type_ != ARM_LOG_NONE) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
@@ -619,13 +731,18 @@
CHECK((byte & ~0x07) == 0xd0);
// 11010nnn: Pop VFP double precision registers D[8]-D[8+nnn] by VPUSH
- if (log_) {
- std::string msg = "pop {d8";
- uint8_t end_reg = byte & 0x7;
- if (end_reg) {
- msg += android::base::StringPrintf("-d%d", 8 + end_reg);
+ if (log_type_ != ARM_LOG_NONE) {
+ if (log_type_ == ARM_LOG_FULL) {
+ std::string msg = "pop {d8";
+ uint8_t end_reg = byte & 0x7;
+ if (end_reg) {
+ msg += android::base::StringPrintf("-d%d", 8 + end_reg);
+ }
+ log(log_indent_, "%s}", msg.c_str());
+ } else {
+ log(log_indent_, "Unsupported DX register display");
}
- log(log_indent_, "%s}", msg.c_str());
+
if (log_skip_execution_) {
return true;
}
@@ -646,7 +763,7 @@
return DecodePrefix_11_010(byte);
default:
// 11xxxyyy: Spare (xxx != 000, 001, 010)
- if (log_) {
+ if (log_type_ != ARM_LOG_NONE) {
log(log_indent_, "Spare");
}
status_ = ARM_STATUS_SPARE;
@@ -664,8 +781,15 @@
switch (byte >> 6) {
case 0:
// 00xxxxxx: vsp = vsp + (xxxxxxx << 2) + 4
- if (log_) {
- log(log_indent_, "vsp = vsp + %d", ((byte & 0x3f) << 2) + 4);
+ if (log_type_ != ARM_LOG_NONE) {
+ int32_t cfa_offset = ((byte & 0x3f) << 2) + 4;
+ if (log_type_ == ARM_LOG_FULL) {
+ log(log_indent_, "vsp = vsp + %d", cfa_offset);
+ } else {
+ log_cfa_offset_ += cfa_offset;
+ }
+ AdjustRegisters(cfa_offset);
+
if (log_skip_execution_) {
break;
}
@@ -674,8 +798,15 @@
break;
case 1:
// 01xxxxxx: vsp = vsp - (xxxxxxx << 2) + 4
- if (log_) {
- log(log_indent_, "vsp = vsp - %d", ((byte & 0x3f) << 2) + 4);
+ if (log_type_ != ARM_LOG_NONE) {
+ uint32_t cfa_offset = ((byte & 0x3f) << 2) + 4;
+ if (log_type_ == ARM_LOG_FULL) {
+ log(log_indent_, "vsp = vsp - %d", cfa_offset);
+ } else {
+ log_cfa_offset_ -= cfa_offset;
+ }
+ AdjustRegisters(-cfa_offset);
+
if (log_skip_execution_) {
break;
}
@@ -696,4 +827,36 @@
return status_ == ARM_STATUS_FINISH;
}
+void ArmExidx::LogByReg() {
+ if (log_type_ != ARM_LOG_BY_REG) {
+ return;
+ }
+
+ uint8_t cfa_reg;
+ if (log_regs_.count(LOG_CFA_REG) == 0) {
+ cfa_reg = 13;
+ } else {
+ cfa_reg = log_regs_[LOG_CFA_REG];
+ }
+
+ if (log_cfa_offset_ != 0) {
+ char sign = (log_cfa_offset_ > 0) ? '+' : '-';
+ log(log_indent_, "cfa = r%zu %c %d", cfa_reg, sign, abs(log_cfa_offset_));
+ } else {
+ log(log_indent_, "cfa = r%zu", cfa_reg);
+ }
+
+ for (const auto& entry : log_regs_) {
+ if (entry.first >= LOG_CFA_REG) {
+ break;
+ }
+ if (entry.second == 0) {
+ log(log_indent_, "r%zu = [cfa]", entry.first);
+ } else {
+ char sign = (entry.second > 0) ? '-' : '+';
+ log(log_indent_, "r%zu = [cfa %c %d]", entry.first, sign, abs(entry.second));
+ }
+ }
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/ArmExidx.h b/libunwindstack/ArmExidx.h
index 96756a0..d9fc371 100644
--- a/libunwindstack/ArmExidx.h
+++ b/libunwindstack/ArmExidx.h
@@ -20,6 +20,7 @@
#include <stdint.h>
#include <deque>
+#include <map>
namespace unwindstack {
@@ -44,6 +45,12 @@
ARM_OP_FINISH = 0xb0,
};
+enum ArmLogType : uint8_t {
+ ARM_LOG_NONE,
+ ARM_LOG_FULL,
+ ARM_LOG_BY_REG,
+};
+
class ArmExidx {
public:
ArmExidx(RegsArm* regs, Memory* elf_memory, Memory* process_memory)
@@ -52,6 +59,8 @@
void LogRawData();
+ void LogByReg();
+
bool ExtractEntryData(uint32_t entry_offset);
bool Eval();
@@ -71,12 +80,13 @@
bool pc_set() { return pc_set_; }
void set_pc_set(bool pc_set) { pc_set_ = pc_set; }
- void set_log(bool log) { log_ = log; }
+ void set_log(ArmLogType log_type) { log_type_ = log_type; }
void set_log_skip_execution(bool skip_execution) { log_skip_execution_ = skip_execution; }
void set_log_indent(uint8_t indent) { log_indent_ = indent; }
private:
bool GetByte(uint8_t* byte);
+ void AdjustRegisters(int32_t offset);
bool DecodePrefix_10_00(uint8_t byte);
bool DecodePrefix_10_01(uint8_t byte);
@@ -103,10 +113,12 @@
Memory* elf_memory_;
Memory* process_memory_;
- bool log_ = false;
+ ArmLogType log_type_ = ARM_LOG_NONE;
uint8_t log_indent_ = 0;
bool log_skip_execution_ = false;
bool pc_set_ = false;
+ int32_t log_cfa_offset_ = 0;
+ std::map<uint8_t, int32_t> log_regs_;
};
} // namespace unwindstack
diff --git a/libunwindstack/DexFile.cpp b/libunwindstack/DexFile.cpp
index b18b0ce..8ec560c 100644
--- a/libunwindstack/DexFile.cpp
+++ b/libunwindstack/DexFile.cpp
@@ -24,6 +24,7 @@
#include <android-base/unique_fd.h>
+#include <dex/class_accessor-inl.h>
#include <dex/code_item_accessors-inl.h>
#include <dex/compact_dex_file.h>
#include <dex/dex_file-inl.h>
@@ -68,29 +69,49 @@
return false; // The DEX offset is not within the bytecode of this dex file.
}
- for (uint32_t i = 0; i < dex_file_->NumClassDefs(); ++i) {
- const art::DexFile::ClassDef& class_def = dex_file_->GetClassDef(i);
- const uint8_t* class_data = dex_file_->GetClassData(class_def);
- if (class_data == nullptr) {
- continue;
+ if (dex_file_->IsCompactDexFile()) {
+ // The data section of compact dex files might be shared.
+ // Check the subrange unique to this compact dex.
+ const auto& cdex_header = dex_file_->AsCompactDexFile()->GetHeader();
+ uint32_t begin = cdex_header.data_off_ + cdex_header.OwnedDataBegin();
+ uint32_t end = cdex_header.data_off_ + cdex_header.OwnedDataEnd();
+ if (dex_offset < begin || dex_offset >= end) {
+ return false; // The DEX offset is not within the bytecode of this dex file.
}
- for (art::ClassDataItemIterator it(*dex_file_.get(), class_data); it.HasNext(); it.Next()) {
- if (!it.IsAtMethod()) {
- continue;
- }
- const art::DexFile::CodeItem* code_item = it.GetMethodCodeItem();
- if (code_item == nullptr) {
- continue;
- }
- art::CodeItemInstructionAccessor code(*dex_file_.get(), code_item);
+ }
+
+ // The method data is cached in a std::map indexed by method end offset and
+ // contains the start offset and the method member index.
+ // Only cache the method data as it is searched. Do not read the entire
+ // set of method data into the cache at once.
+ // This is done because many unwinds only find a single frame with dex file
+ // info, so reading the entire method data is wasteful. However, still cache
+ // the data so that anything doing multiple unwinds will have this data
+ // cached for future use.
+
+ // First look in the method cache.
+ auto entry = method_cache_.upper_bound(dex_offset);
+ if (entry != method_cache_.end() && dex_offset >= entry->second.first) {
+ *method_name = dex_file_->PrettyMethod(entry->second.second, false);
+ *method_offset = dex_offset - entry->second.first;
+ return true;
+ }
+
+ // Check the methods we haven't cached.
+ for (; class_def_index_ < dex_file_->NumClassDefs(); class_def_index_++) {
+ art::ClassAccessor accessor(*dex_file_, dex_file_->GetClassDef(class_def_index_));
+
+ for (const art::ClassAccessor::Method& method : accessor.GetMethods()) {
+ art::CodeItemInstructionAccessor code = method.GetInstructions();
if (!code.HasCodeItem()) {
continue;
}
-
- uint64_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file_->Begin();
- size_t size = code.InsnsSizeInCodeUnits() * sizeof(uint16_t);
- if (offset <= dex_offset && dex_offset < offset + size) {
- *method_name = dex_file_->PrettyMethod(it.GetMemberIndex(), false);
+ uint32_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file_->Begin();
+ uint32_t offset_end = offset + code.InsnsSizeInBytes();
+ uint32_t member_index = method.GetIndex();
+ method_cache_[offset_end] = std::make_pair(offset, member_index);
+ if (offset <= dex_offset && dex_offset < offset_end) {
+ *method_name = dex_file_->PrettyMethod(member_index, false);
*method_offset = dex_offset - offset;
return true;
}
diff --git a/libunwindstack/DexFile.h b/libunwindstack/DexFile.h
index 3ce2f1e..c123158 100644
--- a/libunwindstack/DexFile.h
+++ b/libunwindstack/DexFile.h
@@ -19,8 +19,10 @@
#include <stdint.h>
+#include <map>
#include <memory>
#include <string>
+#include <utility>
#include <vector>
#include <dex/dex_file-inl.h>
@@ -37,7 +39,12 @@
static DexFile* Create(uint64_t dex_file_offset_in_memory, Memory* memory, MapInfo* info);
protected:
+ void Init();
+
std::unique_ptr<const art::DexFile> dex_file_;
+ std::map<uint32_t, std::pair<uint64_t, uint32_t>> method_cache_; // dex offset to method index.
+
+ uint32_t class_def_index_ = 0;
};
class DexFileFromFile : public DexFile {
diff --git a/libunwindstack/DwarfCfa.cpp b/libunwindstack/DwarfCfa.cpp
index 4fc95c7..cd9ef61 100644
--- a/libunwindstack/DwarfCfa.cpp
+++ b/libunwindstack/DwarfCfa.cpp
@@ -50,7 +50,17 @@
memory_->set_cur_offset(start_offset);
uint64_t cfa_offset;
cur_pc_ = fde_->pc_start;
- while ((cfa_offset = memory_->cur_offset()) < end_offset && cur_pc_ <= pc) {
+ loc_regs->pc_start = cur_pc_;
+ while (true) {
+ if (cur_pc_ > pc) {
+ loc_regs->pc_end = cur_pc_;
+ return true;
+ }
+ if ((cfa_offset = memory_->cur_offset()) >= end_offset) {
+ loc_regs->pc_end = fde_->pc_end;
+ return true;
+ }
+ loc_regs->pc_start = cur_pc_;
operands_.clear();
// Read the cfa information.
uint8_t cfa_value;
@@ -129,7 +139,6 @@
}
}
}
- return true;
}
template <typename AddressType>
@@ -255,8 +264,8 @@
}
template <typename AddressType>
-bool DwarfCfa<AddressType>::Log(uint32_t indent, uint64_t pc, uint64_t load_bias,
- uint64_t start_offset, uint64_t end_offset) {
+bool DwarfCfa<AddressType>::Log(uint32_t indent, uint64_t pc, uint64_t start_offset,
+ uint64_t end_offset) {
memory_->set_cur_offset(start_offset);
uint64_t cfa_offset;
uint64_t cur_pc = fde_->pc_start;
@@ -292,8 +301,8 @@
break;
}
if (cur_pc != old_pc) {
- log(indent, "");
- log(indent, "PC 0x%" PRIx64, cur_pc + load_bias);
+ log(0, "");
+ log(indent, "PC 0x%" PRIx64, cur_pc);
}
old_pc = cur_pc;
}
@@ -424,7 +433,10 @@
template <typename AddressType>
bool DwarfCfa<AddressType>::cfa_def_cfa_expression(dwarf_loc_regs_t* loc_regs) {
- (*loc_regs)[CFA_REG] = {.type = DWARF_LOCATION_EXPRESSION,
+ // There is only one type of expression for CFA evaluation and the DWARF
+ // specification is unclear whether it returns the address or the
+ // dereferenced value. GDB expects the value, so will we.
+ (*loc_regs)[CFA_REG] = {.type = DWARF_LOCATION_VAL_EXPRESSION,
.values = {operands_[0], memory_->cur_offset()}};
return true;
}
diff --git a/libunwindstack/DwarfCfa.h b/libunwindstack/DwarfCfa.h
index 16c66e2..c5ffb8e 100644
--- a/libunwindstack/DwarfCfa.h
+++ b/libunwindstack/DwarfCfa.h
@@ -71,8 +71,7 @@
bool GetLocationInfo(uint64_t pc, uint64_t start_offset, uint64_t end_offset,
dwarf_loc_regs_t* loc_regs);
- bool Log(uint32_t indent, uint64_t pc, uint64_t load_bias, uint64_t start_offset,
- uint64_t end_offset);
+ bool Log(uint32_t indent, uint64_t pc, uint64_t start_offset, uint64_t end_offset);
const DwarfErrorData& last_error() { return last_error_; }
DwarfErrorCode LastErrorCode() { return last_error_.code; }
diff --git a/libunwindstack/DwarfDebugFrame.h b/libunwindstack/DwarfDebugFrame.h
index 635cefd..388ab0a 100644
--- a/libunwindstack/DwarfDebugFrame.h
+++ b/libunwindstack/DwarfDebugFrame.h
@@ -26,9 +26,9 @@
namespace unwindstack {
template <typename AddressType>
-class DwarfDebugFrame : public DwarfSectionImpl<AddressType> {
+class DwarfDebugFrame : public DwarfSectionImplNoHdr<AddressType> {
public:
- DwarfDebugFrame(Memory* memory) : DwarfSectionImpl<AddressType>(memory) {
+ DwarfDebugFrame(Memory* memory) : DwarfSectionImplNoHdr<AddressType>(memory) {
this->cie32_value_ = static_cast<uint32_t>(-1);
this->cie64_value_ = static_cast<uint64_t>(-1);
}
diff --git a/libunwindstack/DwarfEhFrame.h b/libunwindstack/DwarfEhFrame.h
index 7a41e45..df441fb 100644
--- a/libunwindstack/DwarfEhFrame.h
+++ b/libunwindstack/DwarfEhFrame.h
@@ -25,9 +25,9 @@
namespace unwindstack {
template <typename AddressType>
-class DwarfEhFrame : public DwarfSectionImpl<AddressType> {
+class DwarfEhFrame : public DwarfSectionImplNoHdr<AddressType> {
public:
- DwarfEhFrame(Memory* memory) : DwarfSectionImpl<AddressType>(memory) {}
+ DwarfEhFrame(Memory* memory) : DwarfSectionImplNoHdr<AddressType>(memory) {}
virtual ~DwarfEhFrame() = default;
uint64_t GetCieOffsetFromFde32(uint32_t pointer) override {
diff --git a/libunwindstack/DwarfEhFrameWithHdr.cpp b/libunwindstack/DwarfEhFrameWithHdr.cpp
index 9a49013..668527a 100644
--- a/libunwindstack/DwarfEhFrameWithHdr.cpp
+++ b/libunwindstack/DwarfEhFrameWithHdr.cpp
@@ -22,19 +22,27 @@
#include "Check.h"
#include "DwarfEhFrameWithHdr.h"
+#include "DwarfEncoding.h"
namespace unwindstack {
+static inline bool IsEncodingRelative(uint8_t encoding) {
+ encoding >>= 4;
+ return encoding > 0 && encoding <= DW_EH_PE_funcrel;
+}
+
template <typename AddressType>
-bool DwarfEhFrameWithHdr<AddressType>::Init(uint64_t offset, uint64_t size) {
- uint8_t data[4];
+bool DwarfEhFrameWithHdr<AddressType>::Init(uint64_t offset, uint64_t size, uint64_t load_bias) {
+ load_bias_ = load_bias;
memory_.clear_func_offset();
memory_.clear_text_offset();
memory_.set_data_offset(offset);
memory_.set_cur_offset(offset);
+ pc_offset_ = offset;
// Read the first four bytes all at once.
+ uint8_t data[4];
if (!memory_.ReadBytes(data, 4)) {
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
last_error_.address = memory_.cur_offset();
@@ -81,12 +89,22 @@
}
template <typename AddressType>
-const DwarfFde* DwarfEhFrameWithHdr<AddressType>::GetFdeFromIndex(size_t index) {
- const FdeInfo* info = GetFdeInfoFromIndex(index);
- if (info == nullptr) {
+const DwarfFde* DwarfEhFrameWithHdr<AddressType>::GetFdeFromPc(uint64_t pc) {
+ uint64_t fde_offset;
+ if (!GetFdeOffsetFromPc(pc, &fde_offset)) {
return nullptr;
}
- return this->GetFdeFromOffset(info->offset);
+ const DwarfFde* fde = this->GetFdeFromOffset(fde_offset);
+ if (fde == nullptr) {
+ return nullptr;
+ }
+
+ // Guaranteed pc >= pc_start, need to check pc in the fde range.
+ if (pc < fde->pc_end) {
+ return fde;
+ }
+ last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
+ return nullptr;
}
template <typename AddressType>
@@ -100,7 +118,7 @@
memory_.set_data_offset(entries_data_offset_);
memory_.set_cur_offset(entries_offset_ + 2 * index * table_entry_size_);
- memory_.set_pc_offset(memory_.cur_offset());
+ memory_.set_pc_offset(0);
uint64_t value;
if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value) ||
!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) {
@@ -109,6 +127,11 @@
fde_info_.erase(index);
return nullptr;
}
+
+ // Relative encodings require adding in the load bias.
+ if (IsEncodingRelative(table_encoding_)) {
+ value += load_bias_;
+ }
info->pc = value;
return info;
}
@@ -174,27 +197,27 @@
memory_.set_data_offset(entries_data_offset_);
memory_.set_cur_offset(cur_entries_offset_);
+ memory_.set_pc_offset(0);
cur_entries_offset_ = 0;
FdeInfo* prev_info = nullptr;
for (size_t current = fde_info_.size();
current < fde_count_ && memory_.cur_offset() < entries_end_; current++) {
- memory_.set_pc_offset(memory_.cur_offset());
- uint64_t value;
- if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
-
FdeInfo* info = &fde_info_[current];
- if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) {
+ uint64_t value;
+ if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value) ||
+ !memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) {
fde_info_.erase(current);
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
last_error_.address = memory_.cur_offset();
return false;
}
- info->pc = value + 4;
+
+ // Relative encodings require adding in the load bias.
+ if (IsEncodingRelative(table_encoding_)) {
+ value += load_bias_;
+ }
+ info->pc = value;
if (pc < info->pc) {
if (prev_info == nullptr) {
@@ -229,6 +252,21 @@
}
}
+template <typename AddressType>
+void DwarfEhFrameWithHdr<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) {
+ for (size_t i = 0; i < fde_count_; i++) {
+ const FdeInfo* info = GetFdeInfoFromIndex(i);
+ if (info == nullptr) {
+ break;
+ }
+ const DwarfFde* fde = this->GetFdeFromOffset(info->offset);
+ if (fde == nullptr) {
+ break;
+ }
+ fdes->push_back(fde);
+ }
+}
+
// Explicitly instantiate DwarfEhFrameWithHdr
template class DwarfEhFrameWithHdr<uint32_t>;
template class DwarfEhFrameWithHdr<uint64_t>;
diff --git a/libunwindstack/DwarfEhFrameWithHdr.h b/libunwindstack/DwarfEhFrameWithHdr.h
index 3571166..e3e9ca8 100644
--- a/libunwindstack/DwarfEhFrameWithHdr.h
+++ b/libunwindstack/DwarfEhFrameWithHdr.h
@@ -21,7 +21,7 @@
#include <unordered_map>
-#include "DwarfEhFrame.h"
+#include <unwindstack/DwarfSection.h>
namespace unwindstack {
@@ -29,29 +29,43 @@
class Memory;
template <typename AddressType>
-class DwarfEhFrameWithHdr : public DwarfEhFrame<AddressType> {
+class DwarfEhFrameWithHdr : public DwarfSectionImpl<AddressType> {
public:
// Add these so that the protected members of DwarfSectionImpl
// can be accessed without needing a this->.
using DwarfSectionImpl<AddressType>::memory_;
- using DwarfSectionImpl<AddressType>::fde_count_;
+ using DwarfSectionImpl<AddressType>::pc_offset_;
using DwarfSectionImpl<AddressType>::entries_offset_;
using DwarfSectionImpl<AddressType>::entries_end_;
using DwarfSectionImpl<AddressType>::last_error_;
+ using DwarfSectionImpl<AddressType>::load_bias_;
struct FdeInfo {
AddressType pc;
uint64_t offset;
};
- DwarfEhFrameWithHdr(Memory* memory) : DwarfEhFrame<AddressType>(memory) {}
+ DwarfEhFrameWithHdr(Memory* memory) : DwarfSectionImpl<AddressType>(memory) {}
virtual ~DwarfEhFrameWithHdr() = default;
- bool Init(uint64_t offset, uint64_t size) override;
+ uint64_t GetCieOffsetFromFde32(uint32_t pointer) override {
+ return this->memory_.cur_offset() - pointer - 4;
+ }
- bool GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) override;
+ uint64_t GetCieOffsetFromFde64(uint64_t pointer) override {
+ return this->memory_.cur_offset() - pointer - 8;
+ }
- const DwarfFde* GetFdeFromIndex(size_t index) override;
+ uint64_t AdjustPcFromFde(uint64_t pc) override {
+ // The eh_frame uses relative pcs.
+ return pc + this->memory_.cur_offset() - 4;
+ }
+
+ bool Init(uint64_t offset, uint64_t size, uint64_t load_bias) override;
+
+ const DwarfFde* GetFdeFromPc(uint64_t pc) override;
+
+ bool GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset);
const FdeInfo* GetFdeInfoFromIndex(size_t index);
@@ -59,6 +73,8 @@
bool GetFdeOffsetBinary(uint64_t pc, uint64_t* fde_offset, uint64_t total_entries);
+ void GetFdes(std::vector<const DwarfFde*>* fdes) override;
+
protected:
uint8_t version_;
uint8_t ptr_encoding_;
@@ -70,6 +86,7 @@
uint64_t entries_data_offset_;
uint64_t cur_entries_offset_ = 0;
+ uint64_t fde_count_;
std::unordered_map<uint64_t, FdeInfo> fde_info_;
};
diff --git a/libunwindstack/DwarfSection.cpp b/libunwindstack/DwarfSection.cpp
index 7649798..6061f61 100644
--- a/libunwindstack/DwarfSection.cpp
+++ b/libunwindstack/DwarfSection.cpp
@@ -36,40 +36,338 @@
DwarfSection::DwarfSection(Memory* memory) : memory_(memory) {}
-const DwarfFde* DwarfSection::GetFdeFromPc(uint64_t pc) {
- uint64_t fde_offset;
- if (!GetFdeOffsetFromPc(pc, &fde_offset)) {
- return nullptr;
- }
- const DwarfFde* fde = GetFdeFromOffset(fde_offset);
- if (fde == nullptr) {
- return nullptr;
- }
-
- // Guaranteed pc >= pc_start, need to check pc in the fde range.
- if (pc < fde->pc_end) {
- return fde;
- }
- last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
- return nullptr;
-}
-
bool DwarfSection::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
- last_error_.code = DWARF_ERROR_NONE;
- const DwarfFde* fde = GetFdeFromPc(pc);
- if (fde == nullptr || fde->cie == nullptr) {
- last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
- return false;
- }
+ // Lookup the pc in the cache.
+ auto it = loc_regs_.upper_bound(pc);
+ if (it == loc_regs_.end() || pc < it->second.pc_start) {
+ last_error_.code = DWARF_ERROR_NONE;
+ const DwarfFde* fde = GetFdeFromPc(pc);
+ if (fde == nullptr || fde->cie == nullptr) {
+ last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
+ return false;
+ }
- // Now get the location information for this pc.
- dwarf_loc_regs_t loc_regs;
- if (!GetCfaLocationInfo(pc, fde, &loc_regs)) {
- return false;
+ // Now get the location information for this pc.
+ dwarf_loc_regs_t loc_regs;
+ if (!GetCfaLocationInfo(pc, fde, &loc_regs)) {
+ return false;
+ }
+ loc_regs.cie = fde->cie;
+
+ // Store it in the cache.
+ it = loc_regs_.emplace(loc_regs.pc_end, std::move(loc_regs)).first;
}
// Now eval the actual registers.
- return Eval(fde->cie, process_memory, loc_regs, regs, finished);
+ return Eval(it->second.cie, process_memory, it->second, regs, finished);
+}
+
+template <typename AddressType>
+const DwarfCie* DwarfSectionImpl<AddressType>::GetCieFromOffset(uint64_t offset) {
+ auto cie_entry = cie_entries_.find(offset);
+ if (cie_entry != cie_entries_.end()) {
+ return &cie_entry->second;
+ }
+ DwarfCie* cie = &cie_entries_[offset];
+ memory_.set_cur_offset(offset);
+ if (!FillInCieHeader(cie) || !FillInCie(cie)) {
+ // Erase the cached entry.
+ cie_entries_.erase(offset);
+ return nullptr;
+ }
+ return cie;
+}
+
+template <typename AddressType>
+bool DwarfSectionImpl<AddressType>::FillInCieHeader(DwarfCie* cie) {
+ cie->lsda_encoding = DW_EH_PE_omit;
+ uint32_t length32;
+ if (!memory_.ReadBytes(&length32, sizeof(length32))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ if (length32 == static_cast<uint32_t>(-1)) {
+ // 64 bit Cie
+ uint64_t length64;
+ if (!memory_.ReadBytes(&length64, sizeof(length64))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+
+ cie->cfa_instructions_end = memory_.cur_offset() + length64;
+ cie->fde_address_encoding = DW_EH_PE_sdata8;
+
+ uint64_t cie_id;
+ if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ if (cie_id != cie64_value_) {
+ // This is not a Cie, something has gone horribly wrong.
+ last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
+ return false;
+ }
+ } else {
+ // 32 bit Cie
+ cie->cfa_instructions_end = memory_.cur_offset() + length32;
+ cie->fde_address_encoding = DW_EH_PE_sdata4;
+
+ uint32_t cie_id;
+ if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ if (cie_id != cie32_value_) {
+ // This is not a Cie, something has gone horribly wrong.
+ last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
+ return false;
+ }
+ }
+ return true;
+}
+
+template <typename AddressType>
+bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) {
+ if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+
+ if (cie->version != 1 && cie->version != 3 && cie->version != 4) {
+ // Unrecognized version.
+ last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
+ return false;
+ }
+
+ // Read the augmentation string.
+ char aug_value;
+ do {
+ if (!memory_.ReadBytes(&aug_value, 1)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ cie->augmentation_string.push_back(aug_value);
+ } while (aug_value != '\0');
+
+ if (cie->version == 4) {
+ // Skip the Address Size field since we only use it for validation.
+ memory_.set_cur_offset(memory_.cur_offset() + 1);
+
+ // Segment Size
+ if (!memory_.ReadBytes(&cie->segment_size, 1)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ }
+
+ // Code Alignment Factor
+ if (!memory_.ReadULEB128(&cie->code_alignment_factor)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+
+ // Data Alignment Factor
+ if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+
+ if (cie->version == 1) {
+ // Return Address is a single byte.
+ uint8_t return_address_register;
+ if (!memory_.ReadBytes(&return_address_register, 1)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ cie->return_address_register = return_address_register;
+ } else if (!memory_.ReadULEB128(&cie->return_address_register)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+
+ if (cie->augmentation_string[0] != 'z') {
+ cie->cfa_instructions_offset = memory_.cur_offset();
+ return true;
+ }
+
+ uint64_t aug_length;
+ if (!memory_.ReadULEB128(&aug_length)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ cie->cfa_instructions_offset = memory_.cur_offset() + aug_length;
+
+ for (size_t i = 1; i < cie->augmentation_string.size(); i++) {
+ switch (cie->augmentation_string[i]) {
+ case 'L':
+ if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ break;
+ case 'P': {
+ uint8_t encoding;
+ if (!memory_.ReadBytes(&encoding, 1)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ memory_.set_pc_offset(pc_offset_);
+ if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ } break;
+ case 'R':
+ if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ break;
+ }
+ }
+ return true;
+}
+
+template <typename AddressType>
+const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) {
+ auto fde_entry = fde_entries_.find(offset);
+ if (fde_entry != fde_entries_.end()) {
+ return &fde_entry->second;
+ }
+ DwarfFde* fde = &fde_entries_[offset];
+ memory_.set_cur_offset(offset);
+ if (!FillInFdeHeader(fde) || !FillInFde(fde)) {
+ fde_entries_.erase(offset);
+ return nullptr;
+ }
+ return fde;
+}
+
+template <typename AddressType>
+bool DwarfSectionImpl<AddressType>::FillInFdeHeader(DwarfFde* fde) {
+ uint32_t length32;
+ if (!memory_.ReadBytes(&length32, sizeof(length32))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+
+ if (length32 == static_cast<uint32_t>(-1)) {
+ // 64 bit Fde.
+ uint64_t length64;
+ if (!memory_.ReadBytes(&length64, sizeof(length64))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ fde->cfa_instructions_end = memory_.cur_offset() + length64;
+
+ uint64_t value64;
+ if (!memory_.ReadBytes(&value64, sizeof(value64))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ if (value64 == cie64_value_) {
+ // This is a Cie, this means something has gone wrong.
+ last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
+ return false;
+ }
+
+ // Get the Cie pointer, which is necessary to properly read the rest of
+ // of the Fde information.
+ fde->cie_offset = GetCieOffsetFromFde64(value64);
+ } else {
+ // 32 bit Fde.
+ fde->cfa_instructions_end = memory_.cur_offset() + length32;
+
+ uint32_t value32;
+ if (!memory_.ReadBytes(&value32, sizeof(value32))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ if (value32 == cie32_value_) {
+ // This is a Cie, this means something has gone wrong.
+ last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
+ return false;
+ }
+
+ // Get the Cie pointer, which is necessary to properly read the rest of
+ // of the Fde information.
+ fde->cie_offset = GetCieOffsetFromFde32(value32);
+ }
+ return true;
+}
+
+template <typename AddressType>
+bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) {
+ uint64_t cur_offset = memory_.cur_offset();
+
+ const DwarfCie* cie = GetCieFromOffset(fde->cie_offset);
+ if (cie == nullptr) {
+ return false;
+ }
+ fde->cie = cie;
+
+ if (cie->segment_size != 0) {
+ // Skip over the segment selector for now.
+ cur_offset += cie->segment_size;
+ }
+ memory_.set_cur_offset(cur_offset);
+
+ // The load bias only applies to the start.
+ memory_.set_pc_offset(load_bias_);
+ bool valid = memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding, &fde->pc_start);
+ fde->pc_start = AdjustPcFromFde(fde->pc_start);
+
+ memory_.set_pc_offset(0);
+ if (!valid || !memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding, &fde->pc_end)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ fde->pc_end += fde->pc_start;
+
+ if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') {
+ // Augmentation Size
+ uint64_t aug_length;
+ if (!memory_.ReadULEB128(&aug_length)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+ uint64_t cur_offset = memory_.cur_offset();
+
+ memory_.set_pc_offset(pc_offset_);
+ if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+
+ // Set our position to after all of the augmentation data.
+ memory_.set_cur_offset(cur_offset + aug_length);
+ }
+ fde->cfa_instructions_offset = memory_.cur_offset();
+
+ return true;
}
template <typename AddressType>
@@ -190,8 +488,6 @@
// Always set the dex pc to zero when evaluating.
cur_regs->set_dex_pc(0);
- AddressType prev_cfa = regs->sp();
-
EvalInfo<AddressType> eval_info{.loc_regs = &loc_regs,
.cie = cie,
.regular_memory = regular_memory,
@@ -204,31 +500,16 @@
last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
return false;
}
- // If the stack pointer register is the CFA, and the stack
- // pointer register does not have any associated location
- // information, use the current cfa value.
- if (regs->sp_reg() == loc->values[0] && loc_regs.count(regs->sp_reg()) == 0) {
- eval_info.cfa = prev_cfa;
- } else {
- eval_info.cfa = (*cur_regs)[loc->values[0]];
- }
+ eval_info.cfa = (*cur_regs)[loc->values[0]];
eval_info.cfa += loc->values[1];
break;
- case DWARF_LOCATION_EXPRESSION:
case DWARF_LOCATION_VAL_EXPRESSION: {
AddressType value;
if (!EvalExpression(*loc, regular_memory, &value, &eval_info.regs_info, nullptr)) {
return false;
}
- if (loc->type == DWARF_LOCATION_EXPRESSION) {
- if (!regular_memory->ReadFully(value, &eval_info.cfa, sizeof(AddressType))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = value;
- return false;
- }
- } else {
- eval_info.cfa = value;
- }
+ // There is only one type of valid expression for CFA evaluation.
+ eval_info.cfa = value;
break;
}
default:
@@ -269,305 +550,6 @@
}
template <typename AddressType>
-const DwarfCie* DwarfSectionImpl<AddressType>::GetCie(uint64_t offset) {
- auto cie_entry = cie_entries_.find(offset);
- if (cie_entry != cie_entries_.end()) {
- return &cie_entry->second;
- }
- DwarfCie* cie = &cie_entries_[offset];
- memory_.set_cur_offset(offset);
- if (!FillInCie(cie)) {
- // Erase the cached entry.
- cie_entries_.erase(offset);
- return nullptr;
- }
- return cie;
-}
-
-template <typename AddressType>
-bool DwarfSectionImpl<AddressType>::FillInCie(DwarfCie* cie) {
- uint32_t length32;
- if (!memory_.ReadBytes(&length32, sizeof(length32))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- // Set the default for the lsda encoding.
- cie->lsda_encoding = DW_EH_PE_omit;
-
- if (length32 == static_cast<uint32_t>(-1)) {
- // 64 bit Cie
- uint64_t length64;
- if (!memory_.ReadBytes(&length64, sizeof(length64))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
-
- cie->cfa_instructions_end = memory_.cur_offset() + length64;
- cie->fde_address_encoding = DW_EH_PE_sdata8;
-
- uint64_t cie_id;
- if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- if (cie_id != cie64_value_) {
- // This is not a Cie, something has gone horribly wrong.
- last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
- return false;
- }
- } else {
- // 32 bit Cie
- cie->cfa_instructions_end = memory_.cur_offset() + length32;
- cie->fde_address_encoding = DW_EH_PE_sdata4;
-
- uint32_t cie_id;
- if (!memory_.ReadBytes(&cie_id, sizeof(cie_id))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- if (cie_id != cie32_value_) {
- // This is not a Cie, something has gone horribly wrong.
- last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
- return false;
- }
- }
-
- if (!memory_.ReadBytes(&cie->version, sizeof(cie->version))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
-
- if (cie->version != 1 && cie->version != 3 && cie->version != 4) {
- // Unrecognized version.
- last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
- return false;
- }
-
- // Read the augmentation string.
- char aug_value;
- do {
- if (!memory_.ReadBytes(&aug_value, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- cie->augmentation_string.push_back(aug_value);
- } while (aug_value != '\0');
-
- if (cie->version == 4) {
- // Skip the Address Size field since we only use it for validation.
- memory_.set_cur_offset(memory_.cur_offset() + 1);
-
- // Segment Size
- if (!memory_.ReadBytes(&cie->segment_size, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- }
-
- // Code Alignment Factor
- if (!memory_.ReadULEB128(&cie->code_alignment_factor)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
-
- // Data Alignment Factor
- if (!memory_.ReadSLEB128(&cie->data_alignment_factor)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
-
- if (cie->version == 1) {
- // Return Address is a single byte.
- uint8_t return_address_register;
- if (!memory_.ReadBytes(&return_address_register, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- cie->return_address_register = return_address_register;
- } else if (!memory_.ReadULEB128(&cie->return_address_register)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
-
- if (cie->augmentation_string[0] != 'z') {
- cie->cfa_instructions_offset = memory_.cur_offset();
- return true;
- }
-
- uint64_t aug_length;
- if (!memory_.ReadULEB128(&aug_length)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- cie->cfa_instructions_offset = memory_.cur_offset() + aug_length;
-
- for (size_t i = 1; i < cie->augmentation_string.size(); i++) {
- switch (cie->augmentation_string[i]) {
- case 'L':
- if (!memory_.ReadBytes(&cie->lsda_encoding, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- break;
- case 'P': {
- uint8_t encoding;
- if (!memory_.ReadBytes(&encoding, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- if (!memory_.ReadEncodedValue<AddressType>(encoding, &cie->personality_handler)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- } break;
- case 'R':
- if (!memory_.ReadBytes(&cie->fde_address_encoding, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- break;
- }
- }
- return true;
-}
-
-template <typename AddressType>
-const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromOffset(uint64_t offset) {
- auto fde_entry = fde_entries_.find(offset);
- if (fde_entry != fde_entries_.end()) {
- return &fde_entry->second;
- }
- DwarfFde* fde = &fde_entries_[offset];
- memory_.set_cur_offset(offset);
- if (!FillInFde(fde)) {
- fde_entries_.erase(offset);
- return nullptr;
- }
- return fde;
-}
-
-template <typename AddressType>
-bool DwarfSectionImpl<AddressType>::FillInFde(DwarfFde* fde) {
- uint32_t length32;
- if (!memory_.ReadBytes(&length32, sizeof(length32))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
-
- if (length32 == static_cast<uint32_t>(-1)) {
- // 64 bit Fde.
- uint64_t length64;
- if (!memory_.ReadBytes(&length64, sizeof(length64))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- fde->cfa_instructions_end = memory_.cur_offset() + length64;
-
- uint64_t value64;
- if (!memory_.ReadBytes(&value64, sizeof(value64))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- if (value64 == cie64_value_) {
- // This is a Cie, this means something has gone wrong.
- last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
- return false;
- }
-
- // Get the Cie pointer, which is necessary to properly read the rest of
- // of the Fde information.
- fde->cie_offset = GetCieOffsetFromFde64(value64);
- } else {
- // 32 bit Fde.
- fde->cfa_instructions_end = memory_.cur_offset() + length32;
-
- uint32_t value32;
- if (!memory_.ReadBytes(&value32, sizeof(value32))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- if (value32 == cie32_value_) {
- // This is a Cie, this means something has gone wrong.
- last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
- return false;
- }
-
- // Get the Cie pointer, which is necessary to properly read the rest of
- // of the Fde information.
- fde->cie_offset = GetCieOffsetFromFde32(value32);
- }
- uint64_t cur_offset = memory_.cur_offset();
-
- const DwarfCie* cie = GetCie(fde->cie_offset);
- if (cie == nullptr) {
- return false;
- }
- fde->cie = cie;
-
- if (cie->segment_size != 0) {
- // Skip over the segment selector for now.
- cur_offset += cie->segment_size;
- }
- memory_.set_cur_offset(cur_offset);
-
- if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_start)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- fde->pc_start = AdjustPcFromFde(fde->pc_start);
-
- if (!memory_.ReadEncodedValue<AddressType>(cie->fde_address_encoding & 0xf, &fde->pc_end)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- fde->pc_end += fde->pc_start;
- if (cie->augmentation_string.size() > 0 && cie->augmentation_string[0] == 'z') {
- // Augmentation Size
- uint64_t aug_length;
- if (!memory_.ReadULEB128(&aug_length)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- uint64_t cur_offset = memory_.cur_offset();
-
- if (!memory_.ReadEncodedValue<AddressType>(cie->lsda_encoding, &fde->lsda_address)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
-
- // Set our position to after all of the augmentation data.
- memory_.set_cur_offset(cur_offset + aug_length);
- }
- fde->cfa_instructions_offset = memory_.cur_offset();
-
- return true;
-}
-
-template <typename AddressType>
bool DwarfSectionImpl<AddressType>::GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde,
dwarf_loc_regs_t* loc_regs) {
DwarfCfa<AddressType> cfa(&memory_, fde);
@@ -591,17 +573,16 @@
}
template <typename AddressType>
-bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, uint64_t load_bias,
- const DwarfFde* fde) {
+bool DwarfSectionImpl<AddressType>::Log(uint8_t indent, uint64_t pc, const DwarfFde* fde) {
DwarfCfa<AddressType> cfa(&memory_, fde);
// Always print the cie information.
const DwarfCie* cie = fde->cie;
- if (!cfa.Log(indent, pc, load_bias, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
+ if (!cfa.Log(indent, pc, cie->cfa_instructions_offset, cie->cfa_instructions_end)) {
last_error_ = cfa.last_error();
return false;
}
- if (!cfa.Log(indent, pc, load_bias, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
+ if (!cfa.Log(indent, pc, fde->cfa_instructions_offset, fde->cfa_instructions_end)) {
last_error_ = cfa.last_error();
return false;
}
@@ -609,309 +590,225 @@
}
template <typename AddressType>
-bool DwarfSectionImpl<AddressType>::Init(uint64_t offset, uint64_t size) {
+bool DwarfSectionImplNoHdr<AddressType>::Init(uint64_t offset, uint64_t size, uint64_t load_bias) {
+ load_bias_ = load_bias;
entries_offset_ = offset;
+ next_entries_offset_ = offset;
entries_end_ = offset + size;
memory_.clear_func_offset();
memory_.clear_text_offset();
- memory_.set_data_offset(offset);
memory_.set_cur_offset(offset);
- memory_.set_pc_offset(offset);
-
- return CreateSortedFdeList();
-}
-
-template <typename AddressType>
-bool DwarfSectionImpl<AddressType>::GetCieInfo(uint8_t* segment_size, uint8_t* encoding) {
- uint8_t version;
- if (!memory_.ReadBytes(&version, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- // Read the augmentation string.
- std::vector<char> aug_string;
- char aug_value;
- bool get_encoding = false;
- do {
- if (!memory_.ReadBytes(&aug_value, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- if (aug_value == 'R') {
- get_encoding = true;
- }
- aug_string.push_back(aug_value);
- } while (aug_value != '\0');
-
- if (version == 4) {
- // Skip the Address Size field.
- memory_.set_cur_offset(memory_.cur_offset() + 1);
-
- // Read the segment size.
- if (!memory_.ReadBytes(segment_size, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- } else {
- *segment_size = 0;
- }
-
- if (aug_string[0] != 'z' || !get_encoding) {
- // No encoding
- return true;
- }
-
- // Skip code alignment factor
- uint8_t value;
- do {
- if (!memory_.ReadBytes(&value, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- } while (value & 0x80);
-
- // Skip data alignment factor
- do {
- if (!memory_.ReadBytes(&value, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- } while (value & 0x80);
-
- if (version == 1) {
- // Skip return address register.
- memory_.set_cur_offset(memory_.cur_offset() + 1);
- } else {
- // Skip return address register.
- do {
- if (!memory_.ReadBytes(&value, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- } while (value & 0x80);
- }
-
- // Skip the augmentation length.
- do {
- if (!memory_.ReadBytes(&value, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- } while (value & 0x80);
-
- for (size_t i = 1; i < aug_string.size(); i++) {
- if (aug_string[i] == 'R') {
- if (!memory_.ReadBytes(encoding, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- // Got the encoding, that's all we are looking for.
- return true;
- } else if (aug_string[i] == 'L') {
- memory_.set_cur_offset(memory_.cur_offset() + 1);
- } else if (aug_string[i] == 'P') {
- uint8_t encoding;
- if (!memory_.ReadBytes(&encoding, 1)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- uint64_t value;
- if (!memory_.template ReadEncodedValue<AddressType>(encoding, &value)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- }
- }
-
- // It should be impossible to get here.
- abort();
-}
-
-template <typename AddressType>
-bool DwarfSectionImpl<AddressType>::AddFdeInfo(uint64_t entry_offset, uint8_t segment_size,
- uint8_t encoding) {
- if (segment_size != 0) {
- memory_.set_cur_offset(memory_.cur_offset() + 1);
- }
-
- uint64_t start;
- if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &start)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- start = AdjustPcFromFde(start);
-
- uint64_t length;
- if (!memory_.template ReadEncodedValue<AddressType>(encoding & 0xf, &length)) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- if (length != 0) {
- fdes_.emplace_back(entry_offset, start, length);
- }
+ memory_.set_data_offset(offset);
+ pc_offset_ = offset;
return true;
}
+// Create a cached version of the fde information such that it is a std::map
+// that is indexed by end pc and contains a pair that represents the start pc
+// followed by the fde object. The fde pointers are owned by fde_entries_
+// and not by the map object.
+// It is possible for an fde to be represented by multiple entries in
+// the map. This can happen if the the start pc and end pc overlap already
+// existing entries. For example, if there is already an entry of 0x400, 0x200,
+// and an fde has a start pc of 0x100 and end pc of 0x500, two new entries
+// will be added: 0x200, 0x100 and 0x500, 0x400.
template <typename AddressType>
-bool DwarfSectionImpl<AddressType>::CreateSortedFdeList() {
- memory_.set_cur_offset(entries_offset_);
+void DwarfSectionImplNoHdr<AddressType>::InsertFde(const DwarfFde* fde) {
+ uint64_t start = fde->pc_start;
+ uint64_t end = fde->pc_end;
+ auto it = fdes_.upper_bound(start);
+ bool add_element = false;
+ while (it != fdes_.end() && start < end) {
+ if (add_element) {
+ add_element = false;
+ if (end < it->second.first) {
+ if (it->first == end) {
+ return;
+ }
+ fdes_[end] = std::make_pair(start, fde);
+ return;
+ }
+ if (start != it->second.first) {
+ fdes_[it->second.first] = std::make_pair(start, fde);
+ }
+ }
+ if (start < it->first) {
+ if (end < it->second.first) {
+ if (it->first != end) {
+ fdes_[end] = std::make_pair(start, fde);
+ }
+ return;
+ }
+ add_element = true;
+ }
+ start = it->first;
+ ++it;
+ }
+ if (start < end) {
+ fdes_[end] = std::make_pair(start, fde);
+ }
+}
- // Loop through all of the entries and read just enough to create
- // a sorted list of pcs.
- // This code assumes that first comes the cie, then the fdes that
- // it applies to.
- uint64_t cie_offset = 0;
- uint8_t address_encoding;
- uint8_t segment_size;
- while (memory_.cur_offset() < entries_end_) {
- uint64_t cur_entry_offset = memory_.cur_offset();
+template <typename AddressType>
+bool DwarfSectionImplNoHdr<AddressType>::GetNextCieOrFde(DwarfFde** fde_entry) {
+ uint64_t start_offset = next_entries_offset_;
- // Figure out the entry length and type.
- uint32_t value32;
+ memory_.set_cur_offset(next_entries_offset_);
+ uint32_t value32;
+ if (!memory_.ReadBytes(&value32, sizeof(value32))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+
+ uint64_t cie_offset;
+ uint8_t cie_fde_encoding;
+ bool entry_is_cie = false;
+ if (value32 == static_cast<uint32_t>(-1)) {
+ // 64 bit entry.
+ uint64_t value64;
+ if (!memory_.ReadBytes(&value64, sizeof(value64))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+
+ next_entries_offset_ = memory_.cur_offset() + value64;
+ // Read the Cie Id of a Cie or the pointer of the Fde.
+ if (!memory_.ReadBytes(&value64, sizeof(value64))) {
+ last_error_.code = DWARF_ERROR_MEMORY_INVALID;
+ last_error_.address = memory_.cur_offset();
+ return false;
+ }
+
+ if (value64 == cie64_value_) {
+ entry_is_cie = true;
+ cie_fde_encoding = DW_EH_PE_sdata8;
+ } else {
+ cie_offset = this->GetCieOffsetFromFde64(value64);
+ }
+ } else {
+ next_entries_offset_ = memory_.cur_offset() + value32;
+
+ // 32 bit Cie
if (!memory_.ReadBytes(&value32, sizeof(value32))) {
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
last_error_.address = memory_.cur_offset();
return false;
}
- uint64_t next_entry_offset;
- if (value32 == static_cast<uint32_t>(-1)) {
- uint64_t value64;
- if (!memory_.ReadBytes(&value64, sizeof(value64))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
- next_entry_offset = memory_.cur_offset() + value64;
-
- // Read the Cie Id of a Cie or the pointer of the Fde.
- if (!memory_.ReadBytes(&value64, sizeof(value64))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
-
- if (value64 == cie64_value_) {
- // Cie 64 bit
- address_encoding = DW_EH_PE_sdata8;
- if (!GetCieInfo(&segment_size, &address_encoding)) {
- return false;
- }
- cie_offset = cur_entry_offset;
- } else {
- uint64_t last_cie_offset = GetCieOffsetFromFde64(value64);
- if (last_cie_offset != cie_offset) {
- // This means that this Fde is not following the Cie.
- last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
- return false;
- }
-
- // Fde 64 bit
- if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) {
- return false;
- }
- }
+ if (value32 == cie32_value_) {
+ entry_is_cie = true;
+ cie_fde_encoding = DW_EH_PE_sdata4;
} else {
- next_entry_offset = memory_.cur_offset() + value32;
-
- // Read the Cie Id of a Cie or the pointer of the Fde.
- if (!memory_.ReadBytes(&value32, sizeof(value32))) {
- last_error_.code = DWARF_ERROR_MEMORY_INVALID;
- last_error_.address = memory_.cur_offset();
- return false;
- }
-
- if (value32 == cie32_value_) {
- // Cie 32 bit
- address_encoding = DW_EH_PE_sdata4;
- if (!GetCieInfo(&segment_size, &address_encoding)) {
- return false;
- }
- cie_offset = cur_entry_offset;
- } else {
- uint64_t last_cie_offset = GetCieOffsetFromFde32(value32);
- if (last_cie_offset != cie_offset) {
- // This means that this Fde is not following the Cie.
- last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
- return false;
- }
-
- // Fde 32 bit
- if (!AddFdeInfo(cur_entry_offset, segment_size, address_encoding)) {
- return false;
- }
- }
+ cie_offset = this->GetCieOffsetFromFde32(value32);
}
-
- if (next_entry_offset < memory_.cur_offset()) {
- // Simply consider the processing done in this case.
- break;
- }
- memory_.set_cur_offset(next_entry_offset);
}
- // Sort the entries.
- std::sort(fdes_.begin(), fdes_.end(), [](const FdeInfo& a, const FdeInfo& b) {
- if (a.start == b.start) return a.end < b.end;
- return a.start < b.start;
- });
+ if (entry_is_cie) {
+ DwarfCie* cie = &cie_entries_[start_offset];
+ cie->lsda_encoding = DW_EH_PE_omit;
+ cie->cfa_instructions_end = next_entries_offset_;
+ cie->fde_address_encoding = cie_fde_encoding;
- fde_count_ = fdes_.size();
+ if (!this->FillInCie(cie)) {
+ cie_entries_.erase(start_offset);
+ return false;
+ }
+ *fde_entry = nullptr;
+ } else {
+ DwarfFde* fde = &fde_entries_[start_offset];
+ fde->cfa_instructions_end = next_entries_offset_;
+ fde->cie_offset = cie_offset;
+ if (!this->FillInFde(fde)) {
+ fde_entries_.erase(start_offset);
+ return false;
+ }
+ *fde_entry = fde;
+ }
return true;
}
template <typename AddressType>
-bool DwarfSectionImpl<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) {
- if (fde_count_ == 0) {
- return false;
- }
-
- size_t first = 0;
- size_t last = fde_count_;
- while (first < last) {
- size_t current = (first + last) / 2;
- const FdeInfo* info = &fdes_[current];
- if (pc >= info->start && pc <= info->end) {
- *fde_offset = info->offset;
- return true;
- }
-
- if (pc < info->start) {
- last = current;
+void DwarfSectionImplNoHdr<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) {
+ // Loop through the already cached entries.
+ uint64_t entry_offset = entries_offset_;
+ while (entry_offset < next_entries_offset_) {
+ auto cie_it = cie_entries_.find(entry_offset);
+ if (cie_it != cie_entries_.end()) {
+ entry_offset = cie_it->second.cfa_instructions_end;
} else {
- first = current + 1;
+ auto fde_it = fde_entries_.find(entry_offset);
+ if (fde_it == fde_entries_.end()) {
+ // No fde or cie at this entry, should not be possible.
+ return;
+ }
+ entry_offset = fde_it->second.cfa_instructions_end;
+ fdes->push_back(&fde_it->second);
}
}
- return false;
+
+ while (next_entries_offset_ < entries_end_) {
+ DwarfFde* fde;
+ if (!GetNextCieOrFde(&fde)) {
+ break;
+ }
+ if (fde != nullptr) {
+ InsertFde(fde);
+ fdes->push_back(fde);
+ }
+
+ if (next_entries_offset_ < memory_.cur_offset()) {
+ // Simply consider the processing done in this case.
+ break;
+ }
+ }
}
template <typename AddressType>
-const DwarfFde* DwarfSectionImpl<AddressType>::GetFdeFromIndex(size_t index) {
- if (index >= fdes_.size()) {
- return nullptr;
+const DwarfFde* DwarfSectionImplNoHdr<AddressType>::GetFdeFromPc(uint64_t pc) {
+ // Search in the list of fdes we already have.
+ auto it = fdes_.upper_bound(pc);
+ if (it != fdes_.end()) {
+ if (pc >= it->second.first) {
+ return it->second.second;
+ }
}
- return this->GetFdeFromOffset(fdes_[index].offset);
+
+ // The section might have overlapping pcs in fdes, so it is necessary
+ // to do a linear search of the fdes by pc. As fdes are read, a cached
+ // search map is created.
+ while (next_entries_offset_ < entries_end_) {
+ DwarfFde* fde;
+ if (!GetNextCieOrFde(&fde)) {
+ return nullptr;
+ }
+ if (fde != nullptr) {
+ InsertFde(fde);
+ if (pc >= fde->pc_start && pc < fde->pc_end) {
+ return fde;
+ }
+ }
+
+ if (next_entries_offset_ < memory_.cur_offset()) {
+ // Simply consider the processing done in this case.
+ break;
+ }
+ }
+ return nullptr;
}
// Explicitly instantiate DwarfSectionImpl
template class DwarfSectionImpl<uint32_t>;
template class DwarfSectionImpl<uint64_t>;
+// Explicitly instantiate DwarfSectionImplNoHdr
+template class DwarfSectionImplNoHdr<uint32_t>;
+template class DwarfSectionImplNoHdr<uint64_t>;
+
// Explicitly instantiate DwarfDebugFrame
template class DwarfDebugFrame<uint32_t>;
template class DwarfDebugFrame<uint64_t>;
diff --git a/libunwindstack/Elf.cpp b/libunwindstack/Elf.cpp
index 02f8a9a..4723606 100644
--- a/libunwindstack/Elf.cpp
+++ b/libunwindstack/Elf.cpp
@@ -53,7 +53,7 @@
valid_ = interface_->Init(&load_bias_);
if (valid_) {
- interface_->InitHeaders();
+ interface_->InitHeaders(load_bias_);
if (init_gnu_debugdata) {
InitGnuDebugdata();
} else {
@@ -83,7 +83,7 @@
// is in the uncompressed data.
uint64_t load_bias;
if (gnu->Init(&load_bias)) {
- gnu->InitHeaders();
+ gnu->InitHeaders(load_bias);
interface_->SetGnuDebugdataInterface(gnu);
} else {
// Free all of the memory associated with the gnu_debugdata section.
@@ -103,9 +103,9 @@
bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
std::lock_guard<std::mutex> guard(lock_);
- return valid_ && (interface_->GetFunctionName(addr, load_bias_, name, func_offset) ||
- (gnu_debugdata_interface_ && gnu_debugdata_interface_->GetFunctionName(
- addr, load_bias_, name, func_offset)));
+ return valid_ && (interface_->GetFunctionName(addr, name, func_offset) ||
+ (gnu_debugdata_interface_ &&
+ gnu_debugdata_interface_->GetFunctionName(addr, name, func_offset)));
}
bool Elf::GetGlobalVariable(const std::string& name, uint64_t* memory_address) {
@@ -160,21 +160,21 @@
}
// The relative pc is always relative to the start of the map from which it comes.
-bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs,
- Memory* process_memory, bool* finished) {
+bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, Regs* regs, Memory* process_memory,
+ bool* finished) {
if (!valid_) {
return false;
}
// The relative pc expectd by StepIfSignalHandler is relative to the start of the elf.
- if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) {
+ if (regs->StepIfSignalHandler(rel_pc, this, process_memory)) {
*finished = false;
return true;
}
// Lock during the step which can update information in the object.
std::lock_guard<std::mutex> guard(lock_);
- return interface_->Step(adjusted_rel_pc, load_bias_, regs, process_memory, finished);
+ return interface_->Step(adjusted_rel_pc, regs, process_memory, finished);
}
bool Elf::IsValidElf(Memory* memory) {
@@ -220,7 +220,6 @@
if (!valid_ || pc < load_bias_) {
return false;
}
- pc -= load_bias_;
if (interface_->IsValidPc(pc)) {
return true;
diff --git a/libunwindstack/ElfInterface.cpp b/libunwindstack/ElfInterface.cpp
index 10afe33..f59a472 100644
--- a/libunwindstack/ElfInterface.cpp
+++ b/libunwindstack/ElfInterface.cpp
@@ -87,8 +87,8 @@
ISzAlloc alloc;
CXzUnpacker state;
- alloc.Alloc = [](void*, size_t size) { return malloc(size); };
- alloc.Free = [](void*, void* ptr) { return free(ptr); };
+ alloc.Alloc = [](ISzAllocPtr, size_t size) { return malloc(size); };
+ alloc.Free = [](ISzAllocPtr, void* ptr) { return free(ptr); };
XzUnpacker_Construct(&state, &alloc);
@@ -106,7 +106,7 @@
dst_remaining += 2 * gnu_debugdata_size_;
}
return_val = XzUnpacker_Code(&state, dst->GetPtr(dst_offset), &dst_remaining, &src[src_offset],
- &src_remaining, CODER_FINISH_ANY, &status);
+ &src_remaining, true, CODER_FINISH_ANY, &status);
src_offset += src_remaining;
dst_offset += dst_remaining;
} while (return_val == SZ_OK && status == CODER_STATUS_NOT_FINISHED);
@@ -124,10 +124,10 @@
}
template <typename AddressType>
-void ElfInterface::InitHeadersWithTemplate() {
+void ElfInterface::InitHeadersWithTemplate(uint64_t load_bias) {
if (eh_frame_hdr_offset_ != 0) {
eh_frame_.reset(new DwarfEhFrameWithHdr<AddressType>(memory_));
- if (!eh_frame_->Init(eh_frame_hdr_offset_, eh_frame_hdr_size_)) {
+ if (!eh_frame_->Init(eh_frame_hdr_offset_, eh_frame_hdr_size_, load_bias)) {
eh_frame_.reset(nullptr);
}
}
@@ -136,7 +136,7 @@
// If there is an eh_frame section without an eh_frame_hdr section,
// or using the frame hdr object failed to init.
eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_));
- if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_)) {
+ if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_, load_bias)) {
eh_frame_.reset(nullptr);
}
}
@@ -150,7 +150,7 @@
if (debug_frame_offset_ != 0) {
debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_));
- if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_)) {
+ if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_, load_bias)) {
debug_frame_.reset(nullptr);
debug_frame_offset_ = 0;
debug_frame_size_ = static_cast<uint64_t>(-1);
@@ -167,15 +167,10 @@
return false;
}
- if (!ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias)) {
- return false;
- }
-
- // We could still potentially unwind without the section header
- // information, so ignore any errors.
- if (!ReadSectionHeaders<EhdrType, ShdrType>(ehdr)) {
- log(0, "Malformed section header found, ignoring...");
- }
+ // If we have enough information that this is an elf file, then allow
+ // malformed program and section headers.
+ ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias);
+ ReadSectionHeaders<EhdrType, ShdrType>(ehdr);
return true;
}
@@ -200,53 +195,21 @@
}
template <typename EhdrType, typename PhdrType>
-bool ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias) {
+void ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias) {
uint64_t offset = ehdr.e_phoff;
for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
PhdrType phdr;
- if (!memory_->ReadField(offset, &phdr, &phdr.p_type, sizeof(phdr.p_type))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address =
- offset + reinterpret_cast<uintptr_t>(&phdr.p_type) - reinterpret_cast<uintptr_t>(&phdr);
- return false;
- }
-
- if (HandleType(offset, phdr.p_type, *load_bias)) {
- continue;
+ if (!memory_->ReadFully(offset, &phdr, sizeof(phdr))) {
+ return;
}
switch (phdr.p_type) {
case PT_LOAD:
{
- // Get the flags first, if this isn't an executable header, ignore it.
- if (!memory_->ReadField(offset, &phdr, &phdr.p_flags, sizeof(phdr.p_flags))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = offset + reinterpret_cast<uintptr_t>(&phdr.p_flags) -
- reinterpret_cast<uintptr_t>(&phdr);
- return false;
- }
if ((phdr.p_flags & PF_X) == 0) {
continue;
}
- if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = offset + reinterpret_cast<uintptr_t>(&phdr.p_vaddr) -
- reinterpret_cast<uintptr_t>(&phdr);
- return false;
- }
- if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = offset + reinterpret_cast<uintptr_t>(&phdr.p_offset) -
- reinterpret_cast<uintptr_t>(&phdr);
- return false;
- }
- if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = offset + reinterpret_cast<uintptr_t>(&phdr.p_memsz) -
- reinterpret_cast<uintptr_t>(&phdr);
- return false;
- }
pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr,
static_cast<size_t>(phdr.p_memsz)};
if (phdr.p_offset == 0) {
@@ -256,53 +219,26 @@
}
case PT_GNU_EH_FRAME:
- if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = offset + reinterpret_cast<uintptr_t>(&phdr.p_offset) -
- reinterpret_cast<uintptr_t>(&phdr);
- return false;
- }
// This is really the pointer to the .eh_frame_hdr section.
eh_frame_hdr_offset_ = phdr.p_offset;
- if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = offset + reinterpret_cast<uintptr_t>(&phdr.p_memsz) -
- reinterpret_cast<uintptr_t>(&phdr);
- return false;
- }
eh_frame_hdr_size_ = phdr.p_memsz;
break;
case PT_DYNAMIC:
- if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = offset + reinterpret_cast<uintptr_t>(&phdr.p_offset) -
- reinterpret_cast<uintptr_t>(&phdr);
- return false;
- }
dynamic_offset_ = phdr.p_offset;
- if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = offset + reinterpret_cast<uintptr_t>(&phdr.p_vaddr) -
- reinterpret_cast<uintptr_t>(&phdr);
- return false;
- }
dynamic_vaddr_ = phdr.p_vaddr;
- if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = offset + reinterpret_cast<uintptr_t>(&phdr.p_memsz) -
- reinterpret_cast<uintptr_t>(&phdr);
- return false;
- }
dynamic_size_ = phdr.p_memsz;
break;
+
+ default:
+ HandleUnknownType(phdr.p_type, phdr.p_offset, phdr.p_filesz);
+ break;
}
}
- return true;
}
template <typename EhdrType, typename ShdrType>
-bool ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
+void ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
uint64_t offset = ehdr.e_shoff;
uint64_t sec_offset = 0;
uint64_t sec_size = 0;
@@ -313,8 +249,7 @@
ShdrType shdr;
if (ehdr.e_shstrndx < ehdr.e_shnum) {
uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
- if (memory_->ReadField(sh_offset, &shdr, &shdr.sh_offset, sizeof(shdr.sh_offset)) &&
- memory_->ReadField(sh_offset, &shdr, &shdr.sh_size, sizeof(shdr.sh_size))) {
+ if (memory_->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
sec_offset = shdr.sh_offset;
sec_size = shdr.sh_size;
}
@@ -324,9 +259,7 @@
offset += ehdr.e_shentsize;
for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
if (!memory_->Read(offset, &shdr, sizeof(shdr))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = offset;
- return false;
+ return;
}
if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
@@ -334,18 +267,14 @@
// the string terminated names.
ShdrType str_shdr;
if (shdr.sh_link >= ehdr.e_shnum) {
- last_error_.code = ERROR_UNWIND_INFO;
- return false;
+ continue;
}
uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
if (!memory_->Read(str_offset, &str_shdr, sizeof(str_shdr))) {
- last_error_.code = ERROR_MEMORY_INVALID;
- last_error_.address = str_offset;
- return false;
+ continue;
}
if (str_shdr.sh_type != SHT_STRTAB) {
- last_error_.code = ERROR_UNWIND_INFO;
- return false;
+ continue;
}
symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize,
str_shdr.sh_offset, str_shdr.sh_size));
@@ -381,7 +310,6 @@
static_cast<uint64_t>(shdr.sh_offset)));
}
}
- return true;
}
template <typename DynType>
@@ -441,14 +369,14 @@
}
template <typename SymType>
-bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, uint64_t load_bias, std::string* name,
+bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name,
uint64_t* func_offset) {
if (symbols_.empty()) {
return false;
}
for (const auto symbol : symbols_) {
- if (symbol->GetName<SymType>(addr, load_bias, memory_, name, func_offset)) {
+ if (symbol->GetName<SymType>(addr, memory_, name, func_offset)) {
return true;
}
}
@@ -469,34 +397,25 @@
return false;
}
-bool ElfInterface::Step(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory,
- bool* finished) {
+bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
last_error_.code = ERROR_NONE;
last_error_.address = 0;
- // Adjust the load bias to get the real relative pc.
- if (pc < load_bias) {
- last_error_.code = ERROR_UNWIND_INFO;
- return false;
- }
- uint64_t adjusted_pc = pc - load_bias;
-
// Try the debug_frame first since it contains the most specific unwind
// information.
DwarfSection* debug_frame = debug_frame_.get();
- if (debug_frame != nullptr && debug_frame->Step(adjusted_pc, regs, process_memory, finished)) {
+ if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) {
return true;
}
// Try the eh_frame next.
DwarfSection* eh_frame = eh_frame_.get();
- if (eh_frame != nullptr && eh_frame->Step(adjusted_pc, regs, process_memory, finished)) {
+ if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) {
return true;
}
- // Finally try the gnu_debugdata interface, but always use a zero load bias.
if (gnu_debugdata_interface_ != nullptr &&
- gnu_debugdata_interface_->Step(pc, 0, regs, process_memory, finished)) {
+ gnu_debugdata_interface_->Step(pc, regs, process_memory, finished)) {
return true;
}
@@ -559,24 +478,26 @@
}
// Instantiate all of the needed template functions.
-template void ElfInterface::InitHeadersWithTemplate<uint32_t>();
-template void ElfInterface::InitHeadersWithTemplate<uint64_t>();
+template void ElfInterface::InitHeadersWithTemplate<uint32_t>(uint64_t);
+template void ElfInterface::InitHeadersWithTemplate<uint64_t>(uint64_t);
template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(uint64_t*);
template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(uint64_t*);
-template bool ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&, uint64_t*);
-template bool ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&, uint64_t*);
+template void ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&,
+ uint64_t*);
+template void ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&,
+ uint64_t*);
-template bool ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
-template bool ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
+template void ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
+template void ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
template bool ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(std::string*);
template bool ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(std::string*);
-template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, uint64_t, std::string*,
+template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*,
uint64_t*);
-template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, uint64_t, std::string*,
+template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*,
uint64_t*);
template bool ElfInterface::GetGlobalVariableWithTemplate<Elf32_Sym>(const std::string&, uint64_t*);
diff --git a/libunwindstack/ElfInterfaceArm.cpp b/libunwindstack/ElfInterfaceArm.cpp
index dfb8e8f..3dd5d54 100644
--- a/libunwindstack/ElfInterfaceArm.cpp
+++ b/libunwindstack/ElfInterfaceArm.cpp
@@ -26,6 +26,14 @@
namespace unwindstack {
+bool ElfInterfaceArm::Init(uint64_t* load_bias) {
+ if (!ElfInterface32::Init(load_bias)) {
+ return false;
+ }
+ load_bias_ = *load_bias;
+ return true;
+}
+
bool ElfInterfaceArm::FindEntry(uint32_t pc, uint64_t* entry_offset) {
if (start_offset_ == 0 || total_entries_ == 0) {
last_error_.code = ERROR_UNWIND_INFO;
@@ -79,41 +87,35 @@
#define PT_ARM_EXIDX 0x70000001
#endif
-bool ElfInterfaceArm::HandleType(uint64_t offset, uint32_t type, uint64_t load_bias) {
+void ElfInterfaceArm::HandleUnknownType(uint32_t type, uint64_t ph_offset, uint64_t ph_filesz) {
if (type != PT_ARM_EXIDX) {
- return false;
+ return;
}
- Elf32_Phdr phdr;
- if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) {
- return true;
- }
- if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) {
- return true;
- }
- start_offset_ = phdr.p_vaddr - load_bias;
- total_entries_ = phdr.p_memsz / 8;
- return true;
+ // The offset already takes into account the load bias.
+ start_offset_ = ph_offset;
+
+ // Always use filesz instead of memsz. In most cases they are the same,
+ // but some shared libraries wind up setting one correctly and not the other.
+ total_entries_ = ph_filesz / 8;
}
-bool ElfInterfaceArm::Step(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory,
- bool* finished) {
+bool ElfInterfaceArm::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
// Dwarf unwind information is precise about whether a pc is covered or not,
// but arm unwind information only has ranges of pc. In order to avoid
// incorrectly doing a bad unwind using arm unwind information for a
// different function, always try and unwind with the dwarf information first.
- return ElfInterface32::Step(pc, load_bias, regs, process_memory, finished) ||
- StepExidx(pc, load_bias, regs, process_memory, finished);
+ return ElfInterface32::Step(pc, regs, process_memory, finished) ||
+ StepExidx(pc, regs, process_memory, finished);
}
-bool ElfInterfaceArm::StepExidx(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory,
- bool* finished) {
+bool ElfInterfaceArm::StepExidx(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
// Adjust the load bias to get the real relative pc.
- if (pc < load_bias) {
+ if (pc < load_bias_) {
last_error_.code = ERROR_UNWIND_INFO;
return false;
}
- pc -= load_bias;
+ pc -= load_bias_;
RegsArm* regs_arm = reinterpret_cast<RegsArm*>(regs);
uint64_t entry_offset;
@@ -127,13 +129,9 @@
if (arm.ExtractEntryData(entry_offset) && arm.Eval()) {
// If the pc was not set, then use the LR registers for the PC.
if (!arm.pc_set()) {
- regs_arm->set_pc((*regs_arm)[ARM_REG_LR]);
- (*regs_arm)[ARM_REG_PC] = regs_arm->pc();
- } else {
- regs_arm->set_pc((*regs_arm)[ARM_REG_PC]);
+ (*regs_arm)[ARM_REG_PC] = (*regs_arm)[ARM_REG_LR];
}
- regs_arm->set_sp(arm.cfa());
- (*regs_arm)[ARM_REG_SP] = regs_arm->sp();
+ (*regs_arm)[ARM_REG_SP] = arm.cfa();
return_value = true;
// If the pc was set to zero, consider this the final frame.
@@ -171,4 +169,16 @@
return return_value;
}
+bool ElfInterfaceArm::GetFunctionName(uint64_t addr, std::string* name, uint64_t* offset) {
+ // For ARM, thumb function symbols have bit 0 set, but the address passed
+ // in here might not have this bit set and result in a failure to find
+ // the thumb function names. Adjust the address and offset to account
+ // for this possible case.
+ if (ElfInterface32::GetFunctionName(addr | 1, name, offset)) {
+ *offset &= ~1;
+ return true;
+ }
+ return false;
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/ElfInterfaceArm.h b/libunwindstack/ElfInterfaceArm.h
index 9c067ba..4c3a0c3 100644
--- a/libunwindstack/ElfInterfaceArm.h
+++ b/libunwindstack/ElfInterfaceArm.h
@@ -64,25 +64,30 @@
iterator begin() { return iterator(this, 0); }
iterator end() { return iterator(this, total_entries_); }
+ bool Init(uint64_t* load_bias) override;
+
bool GetPrel31Addr(uint32_t offset, uint32_t* addr);
bool FindEntry(uint32_t pc, uint64_t* entry_offset);
- bool HandleType(uint64_t offset, uint32_t type, uint64_t load_bias) override;
+ void HandleUnknownType(uint32_t type, uint64_t ph_offset, uint64_t ph_filesz) override;
- bool Step(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory,
- bool* finished) override;
+ bool Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) override;
- bool StepExidx(uint64_t pc, uint64_t load_bias, Regs* regs, Memory* process_memory,
- bool* finished);
+ bool StepExidx(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished);
+
+ bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* offset) override;
uint64_t start_offset() { return start_offset_; }
size_t total_entries() { return total_entries_; }
+ void set_load_bias(uint64_t load_bias) { load_bias_ = load_bias; }
+
protected:
uint64_t start_offset_ = 0;
size_t total_entries_ = 0;
+ uint64_t load_bias_ = 0;
std::unordered_map<size_t, uint32_t> addrs_;
};
diff --git a/libunwindstack/LocalUnwinder.cpp b/libunwindstack/LocalUnwinder.cpp
new file mode 100644
index 0000000..952b332
--- /dev/null
+++ b/libunwindstack/LocalUnwinder.cpp
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <pthread.h>
+#include <stdint.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <unwindstack/Elf.h>
+#include <unwindstack/LocalUnwinder.h>
+#include <unwindstack/MapInfo.h>
+#include <unwindstack/Maps.h>
+#include <unwindstack/Memory.h>
+#include <unwindstack/Regs.h>
+#include <unwindstack/RegsGetLocal.h>
+
+namespace unwindstack {
+
+bool LocalUnwinder::Init() {
+ pthread_rwlock_init(&maps_rwlock_, nullptr);
+
+ // Create the maps.
+ maps_.reset(new unwindstack::LocalUpdatableMaps());
+ if (!maps_->Parse()) {
+ maps_.reset();
+ return false;
+ }
+
+ process_memory_ = unwindstack::Memory::CreateProcessMemory(getpid());
+
+ return true;
+}
+
+bool LocalUnwinder::ShouldSkipLibrary(const std::string& map_name) {
+ for (const std::string& skip_library : skip_libraries_) {
+ if (skip_library == map_name) {
+ return true;
+ }
+ }
+ return false;
+}
+
+MapInfo* LocalUnwinder::GetMapInfo(uint64_t pc) {
+ pthread_rwlock_rdlock(&maps_rwlock_);
+ MapInfo* map_info = maps_->Find(pc);
+ pthread_rwlock_unlock(&maps_rwlock_);
+
+ if (map_info == nullptr) {
+ pthread_rwlock_wrlock(&maps_rwlock_);
+ // This is guaranteed not to invalidate any previous MapInfo objects so
+ // we don't need to worry about any MapInfo* values already in use.
+ if (maps_->Reparse()) {
+ map_info = maps_->Find(pc);
+ }
+ pthread_rwlock_unlock(&maps_rwlock_);
+ }
+
+ return map_info;
+}
+
+bool LocalUnwinder::Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames) {
+ std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal());
+ unwindstack::RegsGetLocal(regs.get());
+
+ size_t num_frames = 0;
+ bool adjust_pc = false;
+ while (true) {
+ uint64_t cur_pc = regs->pc();
+ uint64_t cur_sp = regs->sp();
+
+ MapInfo* map_info = GetMapInfo(cur_pc);
+ if (map_info == nullptr) {
+ break;
+ }
+
+ Elf* elf = map_info->GetElf(process_memory_, true);
+ uint64_t rel_pc = elf->GetRelPc(cur_pc, map_info);
+ uint64_t step_pc = rel_pc;
+ uint64_t pc_adjustment;
+ if (adjust_pc) {
+ pc_adjustment = regs->GetPcAdjustment(rel_pc, elf);
+ } else {
+ pc_adjustment = 0;
+ }
+ step_pc -= pc_adjustment;
+ // Skip any locations that are within this library.
+ if (num_frames != 0 || !ShouldSkipLibrary(map_info->name)) {
+ // Add frame information.
+ std::string func_name;
+ uint64_t func_offset;
+ if (elf->GetFunctionName(rel_pc, &func_name, &func_offset)) {
+ frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment,
+ func_name, func_offset);
+ } else {
+ frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, "", 0);
+ }
+ num_frames++;
+ }
+ if (!elf->valid()) {
+ break;
+ }
+ if (frame_info->size() == max_frames) {
+ break;
+ }
+
+ adjust_pc = true;
+ bool finished;
+ if (!elf->Step(rel_pc, step_pc, regs.get(), process_memory_.get(), &finished) || finished) {
+ break;
+ }
+ // pc and sp are the same, terminate the unwind.
+ if (cur_pc == regs->pc() && cur_sp == regs->sp()) {
+ break;
+ }
+ }
+ return num_frames != 0;
+}
+
+} // namespace unwindstack
diff --git a/libunwindstack/Maps.cpp b/libunwindstack/Maps.cpp
index 4c16212..e676a5a 100644
--- a/libunwindstack/Maps.cpp
+++ b/libunwindstack/Maps.cpp
@@ -24,7 +24,9 @@
#include <unistd.h>
#include <android-base/unique_fd.h>
+#include <procinfo/process_map.h>
+#include <algorithm>
#include <cctype>
#include <memory>
#include <string>
@@ -56,150 +58,16 @@
return nullptr;
}
-// Assumes that line does not end in '\n'.
-static MapInfo* InternalParseLine(const char* line) {
- // Do not use a sscanf implementation since it is not performant.
-
- // Example linux /proc/<pid>/maps lines:
- // 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so
- char* str;
- const char* old_str = line;
- uint64_t start = strtoull(old_str, &str, 16);
- if (old_str == str || *str++ != '-') {
- return nullptr;
- }
-
- old_str = str;
- uint64_t end = strtoull(old_str, &str, 16);
- if (old_str == str || !std::isspace(*str++)) {
- return nullptr;
- }
-
- while (std::isspace(*str)) {
- str++;
- }
-
- // Parse permissions data.
- if (*str == '\0') {
- return nullptr;
- }
- uint16_t flags = 0;
- if (*str == 'r') {
- flags |= PROT_READ;
- } else if (*str != '-') {
- return nullptr;
- }
- str++;
- if (*str == 'w') {
- flags |= PROT_WRITE;
- } else if (*str != '-') {
- return nullptr;
- }
- str++;
- if (*str == 'x') {
- flags |= PROT_EXEC;
- } else if (*str != '-') {
- return nullptr;
- }
- str++;
- if (*str != 'p' && *str != 's') {
- return nullptr;
- }
- str++;
-
- if (!std::isspace(*str++)) {
- return nullptr;
- }
-
- old_str = str;
- uint64_t offset = strtoull(old_str, &str, 16);
- if (old_str == str || !std::isspace(*str)) {
- return nullptr;
- }
-
- // Ignore the 00:00 values.
- old_str = str;
- (void)strtoull(old_str, &str, 16);
- if (old_str == str || *str++ != ':') {
- return nullptr;
- }
- if (std::isspace(*str)) {
- return nullptr;
- }
-
- // Skip the inode.
- old_str = str;
- (void)strtoull(str, &str, 16);
- if (old_str == str || !std::isspace(*str++)) {
- return nullptr;
- }
-
- // Skip decimal digit.
- old_str = str;
- (void)strtoull(old_str, &str, 10);
- if (old_str == str || (!std::isspace(*str) && *str != '\0')) {
- return nullptr;
- }
-
- while (std::isspace(*str)) {
- str++;
- }
- if (*str == '\0') {
- return new MapInfo(start, end, offset, flags, "");
- }
-
- // Save the name data.
- std::string name(str);
-
- // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
- if (name.substr(0, 5) == "/dev/" && name.substr(5, 7) != "ashmem/") {
- flags |= MAPS_FLAGS_DEVICE_MAP;
- }
- return new MapInfo(start, end, offset, flags, name);
-}
-
bool Maps::Parse() {
- int fd = open(GetMapsFile().c_str(), O_RDONLY | O_CLOEXEC);
- if (fd == -1) {
- return false;
- }
-
- bool return_value = true;
- char buffer[2048];
- size_t leftover = 0;
- while (true) {
- ssize_t bytes = read(fd, &buffer[leftover], 2048 - leftover);
- if (bytes == -1) {
- return_value = false;
- break;
- }
- if (bytes == 0) {
- break;
- }
- bytes += leftover;
- char* line = buffer;
- while (bytes > 0) {
- char* newline = static_cast<char*>(memchr(line, '\n', bytes));
- if (newline == nullptr) {
- memmove(buffer, line, bytes);
- break;
- }
- *newline = '\0';
-
- MapInfo* map_info = InternalParseLine(line);
- if (map_info == nullptr) {
- return_value = false;
- break;
- }
- maps_.push_back(map_info);
-
- bytes -= newline - line + 1;
- line = newline + 1;
- }
- leftover = bytes;
- }
- close(fd);
- return return_value;
+ return android::procinfo::ReadMapFile(
+ GetMapsFile(),
+ [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
+ // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
+ if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
+ flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
+ }
+ maps_.push_back(new MapInfo(start, end, pgoff, flags, name));
+ });
}
void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
@@ -209,6 +77,11 @@
maps_.push_back(map_info);
}
+void Maps::Sort() {
+ std::sort(maps_.begin(), maps_.end(),
+ [](const MapInfo* a, const MapInfo* b) { return a->start < b->start; });
+}
+
Maps::~Maps() {
for (auto& map : maps_) {
delete map;
@@ -216,30 +89,99 @@
}
bool BufferMaps::Parse() {
- const char* start_of_line = buffer_;
- do {
- std::string line;
- const char* end_of_line = strchr(start_of_line, '\n');
- if (end_of_line == nullptr) {
- line = start_of_line;
- } else {
- line = std::string(start_of_line, end_of_line - start_of_line);
- end_of_line++;
- }
-
- MapInfo* map_info = InternalParseLine(line.c_str());
- if (map_info == nullptr) {
- return false;
- }
- maps_.push_back(map_info);
-
- start_of_line = end_of_line;
- } while (start_of_line != nullptr && *start_of_line != '\0');
- return true;
+ std::string content(buffer_);
+ return android::procinfo::ReadMapFileContent(
+ &content[0],
+ [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
+ // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
+ if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
+ flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
+ }
+ maps_.push_back(new MapInfo(start, end, pgoff, flags, name));
+ });
}
const std::string RemoteMaps::GetMapsFile() const {
return "/proc/" + std::to_string(pid_) + "/maps";
}
+const std::string LocalUpdatableMaps::GetMapsFile() const {
+ return "/proc/self/maps";
+}
+
+bool LocalUpdatableMaps::Reparse() {
+ // New maps will be added at the end without deleting the old ones.
+ size_t last_map_idx = maps_.size();
+ if (!Parse()) {
+ // Delete any maps added by the Parse call.
+ for (size_t i = last_map_idx; i < maps_.size(); i++) {
+ delete maps_[i];
+ }
+ maps_.resize(last_map_idx);
+ return false;
+ }
+
+ size_t total_entries = maps_.size();
+ size_t search_map_idx = 0;
+ for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) {
+ MapInfo* new_map_info = maps_[new_map_idx];
+ uint64_t start = new_map_info->start;
+ uint64_t end = new_map_info->end;
+ uint64_t flags = new_map_info->flags;
+ std::string* name = &new_map_info->name;
+ for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) {
+ MapInfo* info = maps_[old_map_idx];
+ if (start == info->start && end == info->end && flags == info->flags && *name == info->name) {
+ // No need to check
+ search_map_idx = old_map_idx + 1;
+ delete new_map_info;
+ maps_[new_map_idx] = nullptr;
+ total_entries--;
+ break;
+ } else if (info->start > start) {
+ // Stop, there isn't going to be a match.
+ search_map_idx = old_map_idx;
+ break;
+ }
+
+ // Never delete these maps, they may be in use. The assumption is
+ // that there will only every be a handfull of these so waiting
+ // to destroy them is not too expensive.
+ saved_maps_.push_back(info);
+ maps_[old_map_idx] = nullptr;
+ total_entries--;
+ }
+ if (search_map_idx >= last_map_idx) {
+ break;
+ }
+ }
+
+ // Now move out any of the maps that never were found.
+ for (size_t i = search_map_idx; i < last_map_idx; i++) {
+ saved_maps_.push_back(maps_[i]);
+ maps_[i] = nullptr;
+ total_entries--;
+ }
+
+ // Sort all of the values such that the nullptrs wind up at the end, then
+ // resize them away.
+ std::sort(maps_.begin(), maps_.end(), [](const auto* a, const auto* b) {
+ if (a == nullptr) {
+ return false;
+ } else if (b == nullptr) {
+ return true;
+ }
+ return a->start < b->start;
+ });
+ maps_.resize(total_entries);
+
+ return true;
+}
+
+LocalUpdatableMaps::~LocalUpdatableMaps() {
+ for (auto map_info : saved_maps_) {
+ delete map_info;
+ }
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/Memory.cpp b/libunwindstack/Memory.cpp
index d4ba680..beb2aad 100644
--- a/libunwindstack/Memory.cpp
+++ b/libunwindstack/Memory.cpp
@@ -345,6 +345,25 @@
return memory_->Read(addr, dst, size);
}
+MemoryOfflineBuffer::MemoryOfflineBuffer(const uint8_t* data, uint64_t start, uint64_t end)
+ : data_(data), start_(start), end_(end) {}
+
+void MemoryOfflineBuffer::Reset(const uint8_t* data, uint64_t start, uint64_t end) {
+ data_ = data;
+ start_ = start;
+ end_ = end;
+}
+
+size_t MemoryOfflineBuffer::Read(uint64_t addr, void* dst, size_t size) {
+ if (addr < start_ || addr >= end_) {
+ return 0;
+ }
+
+ size_t read_length = std::min(size, static_cast<size_t>(end_ - addr));
+ memcpy(dst, &data_[addr - start_], read_length);
+ return read_length;
+}
+
MemoryOfflineParts::~MemoryOfflineParts() {
for (auto memory : memories_) {
delete memory;
diff --git a/libunwindstack/RegsArm.cpp b/libunwindstack/RegsArm.cpp
index 5502ce1..de22bde 100644
--- a/libunwindstack/RegsArm.cpp
+++ b/libunwindstack/RegsArm.cpp
@@ -28,21 +28,46 @@
namespace unwindstack {
-RegsArm::RegsArm()
- : RegsImpl<uint32_t>(ARM_REG_LAST, ARM_REG_SP, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
+RegsArm::RegsArm() : RegsImpl<uint32_t>(ARM_REG_LAST, Location(LOCATION_REGISTER, ARM_REG_LR)) {}
ArchEnum RegsArm::Arch() {
return ARCH_ARM;
}
+uint64_t RegsArm::pc() {
+ return regs_[ARM_REG_PC];
+}
+
+uint64_t RegsArm::sp() {
+ return regs_[ARM_REG_SP];
+}
+
+void RegsArm::set_pc(uint64_t pc) {
+ regs_[ARM_REG_PC] = pc;
+}
+
+void RegsArm::set_sp(uint64_t sp) {
+ regs_[ARM_REG_SP] = sp;
+}
+
uint64_t RegsArm::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
+ if (!elf->valid()) {
+ return 2;
+ }
+
uint64_t load_bias = elf->GetLoadBias();
if (rel_pc < load_bias) {
- return 0;
+ if (rel_pc < 2) {
+ return 0;
+ }
+ return 2;
}
uint64_t adjusted_rel_pc = rel_pc - load_bias;
if (adjusted_rel_pc < 5) {
- return 0;
+ if (adjusted_rel_pc < 2) {
+ return 0;
+ }
+ return 2;
}
if (adjusted_rel_pc & 1) {
@@ -56,17 +81,13 @@
return 4;
}
-void RegsArm::SetFromRaw() {
- set_pc(regs_[ARM_REG_PC]);
- set_sp(regs_[ARM_REG_SP]);
-}
-
bool RegsArm::SetPcFromReturnAddress(Memory*) {
- if (pc() == regs_[ARM_REG_LR]) {
+ uint32_t lr = regs_[ARM_REG_LR];
+ if (regs_[ARM_REG_PC] == lr) {
return false;
}
- set_pc(regs_[ARM_REG_LR]);
+ regs_[ARM_REG_PC] = lr;
return true;
}
@@ -94,7 +115,6 @@
RegsArm* regs = new RegsArm();
memcpy(regs->RawData(), &user->regs[0], ARM_REG_LAST * sizeof(uint32_t));
- regs->SetFromRaw();
return regs;
}
@@ -103,7 +123,6 @@
RegsArm* regs = new RegsArm();
memcpy(regs->RawData(), &arm_ucontext->uc_mcontext.regs[0], ARM_REG_LAST * sizeof(uint32_t));
- regs->SetFromRaw();
return regs;
}
@@ -118,6 +137,7 @@
uint64_t offset = 0;
if (data == 0xe3a07077 || data == 0xef900077 || data == 0xdf002777) {
+ uint64_t sp = regs_[ARM_REG_SP];
// non-RT sigreturn call.
// __restore:
//
@@ -131,17 +151,18 @@
// Form 3 (thumb):
// 0x77 0x27 movs r7, #77
// 0x00 0xdf svc 0
- if (!process_memory->ReadFully(sp(), &data, sizeof(data))) {
+ if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
return false;
}
if (data == 0x5ac3c35a) {
// SP + uc_mcontext offset + r0 offset.
- offset = sp() + 0x14 + 0xc;
+ offset = sp + 0x14 + 0xc;
} else {
// SP + r0 offset
- offset = sp() + 0xc;
+ offset = sp + 0xc;
}
} else if (data == 0xe3a070ad || data == 0xef9000ad || data == 0xdf0027ad) {
+ uint64_t sp = regs_[ARM_REG_SP];
// RT sigreturn call.
// __restore_rt:
//
@@ -155,15 +176,15 @@
// Form 3 (thumb):
// 0xad 0x27 movs r7, #ad
// 0x00 0xdf svc 0
- if (!process_memory->ReadFully(sp(), &data, sizeof(data))) {
+ if (!process_memory->ReadFully(sp, &data, sizeof(data))) {
return false;
}
- if (data == sp() + 8) {
+ if (data == sp + 8) {
// SP + 8 + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
- offset = sp() + 8 + 0x80 + 0x14 + 0xc;
+ offset = sp + 8 + 0x80 + 0x14 + 0xc;
} else {
// SP + sizeof(siginfo_t) + uc_mcontext_offset + r0 offset
- offset = sp() + 0x80 + 0x14 + 0xc;
+ offset = sp + 0x80 + 0x14 + 0xc;
}
}
if (offset == 0) {
@@ -173,8 +194,11 @@
if (!process_memory->ReadFully(offset, regs_.data(), sizeof(uint32_t) * ARM_REG_LAST)) {
return false;
}
- SetFromRaw();
return true;
}
+Regs* RegsArm::Clone() {
+ return new RegsArm(*this);
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/RegsArm64.cpp b/libunwindstack/RegsArm64.cpp
index cc6f5ce..a68f6e0 100644
--- a/libunwindstack/RegsArm64.cpp
+++ b/libunwindstack/RegsArm64.cpp
@@ -29,30 +29,42 @@
namespace unwindstack {
RegsArm64::RegsArm64()
- : RegsImpl<uint64_t>(ARM64_REG_LAST, ARM64_REG_SP, Location(LOCATION_REGISTER, ARM64_REG_LR)) {}
+ : RegsImpl<uint64_t>(ARM64_REG_LAST, Location(LOCATION_REGISTER, ARM64_REG_LR)) {}
ArchEnum RegsArm64::Arch() {
return ARCH_ARM64;
}
-uint64_t RegsArm64::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
- if (!elf->valid() || rel_pc < 4) {
+uint64_t RegsArm64::pc() {
+ return regs_[ARM64_REG_PC];
+}
+
+uint64_t RegsArm64::sp() {
+ return regs_[ARM64_REG_SP];
+}
+
+void RegsArm64::set_pc(uint64_t pc) {
+ regs_[ARM64_REG_PC] = pc;
+}
+
+void RegsArm64::set_sp(uint64_t sp) {
+ regs_[ARM64_REG_SP] = sp;
+}
+
+uint64_t RegsArm64::GetPcAdjustment(uint64_t rel_pc, Elf*) {
+ if (rel_pc < 4) {
return 0;
}
return 4;
}
-void RegsArm64::SetFromRaw() {
- set_pc(regs_[ARM64_REG_PC]);
- set_sp(regs_[ARM64_REG_SP]);
-}
-
bool RegsArm64::SetPcFromReturnAddress(Memory*) {
- if (pc() == regs_[ARM64_REG_LR]) {
+ uint64_t lr = regs_[ARM64_REG_LR];
+ if (regs_[ARM64_REG_PC] == lr) {
return false;
}
- set_pc(regs_[ARM64_REG_LR]);
+ regs_[ARM64_REG_PC] = lr;
return true;
}
@@ -100,7 +112,6 @@
uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
reg_data[ARM64_REG_PC] = user->pc;
reg_data[ARM64_REG_SP] = user->sp;
- regs->SetFromRaw();
return regs;
}
@@ -109,7 +120,6 @@
RegsArm64* regs = new RegsArm64();
memcpy(regs->RawData(), &arm64_ucontext->uc_mcontext.regs[0], ARM64_REG_LAST * sizeof(uint64_t));
- regs->SetFromRaw();
return regs;
}
@@ -131,13 +141,15 @@
}
// SP + sizeof(siginfo_t) + uc_mcontext offset + X0 offset.
- if (!process_memory->ReadFully(sp() + 0x80 + 0xb0 + 0x08, regs_.data(),
+ if (!process_memory->ReadFully(regs_[ARM64_REG_SP] + 0x80 + 0xb0 + 0x08, regs_.data(),
sizeof(uint64_t) * ARM64_REG_LAST)) {
return false;
}
-
- SetFromRaw();
return true;
}
+Regs* RegsArm64::Clone() {
+ return new RegsArm64(*this);
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/RegsInfo.h b/libunwindstack/RegsInfo.h
index 47825f5..e6dd33c 100644
--- a/libunwindstack/RegsInfo.h
+++ b/libunwindstack/RegsInfo.h
@@ -25,11 +25,13 @@
template <typename AddressType>
struct RegsInfo {
+ static constexpr size_t MAX_REGISTERS = 64;
+
RegsInfo(RegsImpl<AddressType>* regs) : regs(regs) {}
RegsImpl<AddressType>* regs = nullptr;
uint64_t saved_reg_map = 0;
- AddressType saved_regs[64];
+ AddressType saved_regs[MAX_REGISTERS];
inline AddressType Get(uint32_t reg) {
if (IsSaved(reg)) {
@@ -39,23 +41,23 @@
}
inline AddressType* Save(uint32_t reg) {
- if (reg > sizeof(saved_regs) / sizeof(AddressType)) {
- // This should never happen as since all currently supported
- // architectures have the total number of registers < 64.
+ if (reg > MAX_REGISTERS) {
+ // This should never happen since all currently supported
+ // architectures have < 64 total registers.
abort();
}
- saved_reg_map |= 1 << reg;
+ saved_reg_map |= 1ULL << reg;
saved_regs[reg] = (*regs)[reg];
return &(*regs)[reg];
}
inline bool IsSaved(uint32_t reg) {
- if (reg > sizeof(saved_regs) / sizeof(AddressType)) {
- // This should never happen as since all currently supported
- // architectures have the total number of registers < 64.
+ if (reg > MAX_REGISTERS) {
+ // This should never happen since all currently supported
+ // architectures have < 64 total registers.
abort();
}
- return saved_reg_map & (1 << reg);
+ return saved_reg_map & (1ULL << reg);
}
inline uint16_t Total() { return regs->total_regs(); }
diff --git a/libunwindstack/RegsMips.cpp b/libunwindstack/RegsMips.cpp
index 5d20bef..2e6908c 100644
--- a/libunwindstack/RegsMips.cpp
+++ b/libunwindstack/RegsMips.cpp
@@ -29,31 +29,43 @@
namespace unwindstack {
RegsMips::RegsMips()
- : RegsImpl<uint32_t>(MIPS_REG_LAST, MIPS_REG_SP, Location(LOCATION_REGISTER, MIPS_REG_RA)) {}
+ : RegsImpl<uint32_t>(MIPS_REG_LAST, Location(LOCATION_REGISTER, MIPS_REG_RA)) {}
ArchEnum RegsMips::Arch() {
return ARCH_MIPS;
}
-uint64_t RegsMips::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
- if (!elf->valid() || rel_pc < 8) {
+uint64_t RegsMips::pc() {
+ return regs_[MIPS_REG_PC];
+}
+
+uint64_t RegsMips::sp() {
+ return regs_[MIPS_REG_SP];
+}
+
+void RegsMips::set_pc(uint64_t pc) {
+ regs_[MIPS_REG_PC] = static_cast<uint32_t>(pc);
+}
+
+void RegsMips::set_sp(uint64_t sp) {
+ regs_[MIPS_REG_SP] = static_cast<uint32_t>(sp);
+}
+
+uint64_t RegsMips::GetPcAdjustment(uint64_t rel_pc, Elf*) {
+ if (rel_pc < 8) {
return 0;
}
// For now, just assume no compact branches
return 8;
}
-void RegsMips::SetFromRaw() {
- set_pc(regs_[MIPS_REG_PC]);
- set_sp(regs_[MIPS_REG_SP]);
-}
-
bool RegsMips::SetPcFromReturnAddress(Memory*) {
- if (pc() == regs_[MIPS_REG_RA]) {
+ uint32_t ra = regs_[MIPS_REG_RA];
+ if (regs_[MIPS_REG_PC] == ra) {
return false;
}
- set_pc(regs_[MIPS_REG_RA]);
+ regs_[MIPS_REG_PC] = ra;
return true;
}
@@ -101,7 +113,6 @@
memcpy(regs->RawData(), &user->regs[MIPS32_EF_R0], (MIPS_REG_R31 + 1) * sizeof(uint32_t));
reg_data[MIPS_REG_PC] = user->regs[MIPS32_EF_CP0_EPC];
- regs->SetFromRaw();
return regs;
}
@@ -114,7 +125,6 @@
(*regs)[MIPS_REG_R0 + i] = mips_ucontext->uc_mcontext.sc_regs[i];
}
(*regs)[MIPS_REG_PC] = mips_ucontext->uc_mcontext.sc_pc;
- regs->SetFromRaw();
return regs;
}
@@ -149,7 +159,7 @@
// read sc_pc and sc_regs[32] from stack
uint64_t values[MIPS_REG_LAST];
- if (!process_memory->Read(sp() + offset, values, sizeof(values))) {
+ if (!process_memory->Read(regs_[MIPS_REG_SP] + offset, values, sizeof(values))) {
return false;
}
@@ -160,9 +170,11 @@
for (int i = 0; i < 32; i++) {
regs_[MIPS_REG_R0 + i] = values[1 + i];
}
-
- SetFromRaw();
return true;
}
+Regs* RegsMips::Clone() {
+ return new RegsMips(*this);
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/RegsMips64.cpp b/libunwindstack/RegsMips64.cpp
index 4a03538..0b835a1 100644
--- a/libunwindstack/RegsMips64.cpp
+++ b/libunwindstack/RegsMips64.cpp
@@ -29,32 +29,43 @@
namespace unwindstack {
RegsMips64::RegsMips64()
- : RegsImpl<uint64_t>(MIPS64_REG_LAST, MIPS64_REG_SP,
- Location(LOCATION_REGISTER, MIPS64_REG_RA)) {}
+ : RegsImpl<uint64_t>(MIPS64_REG_LAST, Location(LOCATION_REGISTER, MIPS64_REG_RA)) {}
ArchEnum RegsMips64::Arch() {
return ARCH_MIPS64;
}
-uint64_t RegsMips64::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
- if (!elf->valid() || rel_pc < 8) {
+uint64_t RegsMips64::pc() {
+ return regs_[MIPS64_REG_PC];
+}
+
+uint64_t RegsMips64::sp() {
+ return regs_[MIPS64_REG_SP];
+}
+
+void RegsMips64::set_pc(uint64_t pc) {
+ regs_[MIPS64_REG_PC] = pc;
+}
+
+void RegsMips64::set_sp(uint64_t sp) {
+ regs_[MIPS64_REG_SP] = sp;
+}
+
+uint64_t RegsMips64::GetPcAdjustment(uint64_t rel_pc, Elf*) {
+ if (rel_pc < 8) {
return 0;
}
// For now, just assume no compact branches
return 8;
}
-void RegsMips64::SetFromRaw() {
- set_pc(regs_[MIPS64_REG_PC]);
- set_sp(regs_[MIPS64_REG_SP]);
-}
-
bool RegsMips64::SetPcFromReturnAddress(Memory*) {
- if (pc() == regs_[MIPS64_REG_RA]) {
+ uint64_t ra = regs_[MIPS64_REG_RA];
+ if (regs_[MIPS64_REG_PC] == ra) {
return false;
}
- set_pc(regs_[MIPS64_REG_RA]);
+ regs_[MIPS64_REG_PC] = ra;
return true;
}
@@ -102,7 +113,6 @@
memcpy(regs->RawData(), &user->regs[MIPS64_EF_R0], (MIPS64_REG_R31 + 1) * sizeof(uint64_t));
reg_data[MIPS64_REG_PC] = user->regs[MIPS64_EF_CP0_EPC];
- regs->SetFromRaw();
return regs;
}
@@ -113,7 +123,6 @@
// Copy 64 bit sc_regs over to 64 bit regs
memcpy(regs->RawData(), &mips64_ucontext->uc_mcontext.sc_regs[0], 32 * sizeof(uint64_t));
(*regs)[MIPS64_REG_PC] = mips64_ucontext->uc_mcontext.sc_pc;
- regs->SetFromRaw();
return regs;
}
@@ -137,20 +146,22 @@
// vdso_rt_sigreturn => read rt_sigframe
// offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset
// read 64 bit sc_regs[32] from stack into 64 bit regs_
- if (!process_memory->Read(sp() + 24 + 128 + 40, regs_.data(),
+ uint64_t sp = regs_[MIPS64_REG_SP];
+ if (!process_memory->Read(sp + 24 + 128 + 40, regs_.data(),
sizeof(uint64_t) * (MIPS64_REG_LAST - 1))) {
return false;
}
// offset = siginfo offset + sizeof(siginfo) + uc_mcontext offset + sc_pc offset
// read 64 bit sc_pc from stack into 64 bit regs_[MIPS64_REG_PC]
- if (!process_memory->Read(sp() + 24 + 128 + 40 + 576, ®s_[MIPS64_REG_PC],
- sizeof(uint64_t))) {
+ if (!process_memory->Read(sp + 24 + 128 + 40 + 576, ®s_[MIPS64_REG_PC], sizeof(uint64_t))) {
return false;
}
-
- SetFromRaw();
return true;
}
+Regs* RegsMips64::Clone() {
+ return new RegsMips64(*this);
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/RegsX86.cpp b/libunwindstack/RegsX86.cpp
index 573cb23..9bb39d1 100644
--- a/libunwindstack/RegsX86.cpp
+++ b/libunwindstack/RegsX86.cpp
@@ -28,33 +28,44 @@
namespace unwindstack {
-RegsX86::RegsX86()
- : RegsImpl<uint32_t>(X86_REG_LAST, X86_REG_SP, Location(LOCATION_SP_OFFSET, -4)) {}
+RegsX86::RegsX86() : RegsImpl<uint32_t>(X86_REG_LAST, Location(LOCATION_SP_OFFSET, -4)) {}
ArchEnum RegsX86::Arch() {
return ARCH_X86;
}
-uint64_t RegsX86::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
- if (!elf->valid() || rel_pc == 0) {
+uint64_t RegsX86::pc() {
+ return regs_[X86_REG_PC];
+}
+
+uint64_t RegsX86::sp() {
+ return regs_[X86_REG_SP];
+}
+
+void RegsX86::set_pc(uint64_t pc) {
+ regs_[X86_REG_PC] = static_cast<uint32_t>(pc);
+}
+
+void RegsX86::set_sp(uint64_t sp) {
+ regs_[X86_REG_SP] = static_cast<uint32_t>(sp);
+}
+
+uint64_t RegsX86::GetPcAdjustment(uint64_t rel_pc, Elf*) {
+ if (rel_pc == 0) {
return 0;
}
return 1;
}
-void RegsX86::SetFromRaw() {
- set_pc(regs_[X86_REG_PC]);
- set_sp(regs_[X86_REG_SP]);
-}
-
bool RegsX86::SetPcFromReturnAddress(Memory* process_memory) {
// Attempt to get the return address from the top of the stack.
uint32_t new_pc;
- if (!process_memory->ReadFully(sp_, &new_pc, sizeof(new_pc)) || new_pc == pc()) {
+ if (!process_memory->ReadFully(regs_[X86_REG_SP], &new_pc, sizeof(new_pc)) ||
+ new_pc == regs_[X86_REG_PC]) {
return false;
}
- set_pc(new_pc);
+ regs_[X86_REG_PC] = new_pc;
return true;
}
@@ -84,7 +95,6 @@
(*regs)[X86_REG_ESP] = user->esp;
(*regs)[X86_REG_EIP] = user->eip;
- regs->SetFromRaw();
return regs;
}
@@ -99,7 +109,6 @@
regs_[X86_REG_ECX] = ucontext->uc_mcontext.ecx;
regs_[X86_REG_EAX] = ucontext->uc_mcontext.eax;
regs_[X86_REG_EIP] = ucontext->uc_mcontext.eip;
- SetFromRaw();
}
Regs* RegsX86::CreateFromUcontext(void* ucontext) {
@@ -131,7 +140,7 @@
// int signum
// struct sigcontext (same format as mcontext)
struct x86_mcontext_t context;
- if (!process_memory->ReadFully(sp() + 4, &context, sizeof(context))) {
+ if (!process_memory->ReadFully(regs_[X86_REG_SP] + 4, &context, sizeof(context))) {
return false;
}
regs_[X86_REG_EBP] = context.ebp;
@@ -141,7 +150,6 @@
regs_[X86_REG_ECX] = context.ecx;
regs_[X86_REG_EAX] = context.eax;
regs_[X86_REG_EIP] = context.eip;
- SetFromRaw();
return true;
} else if ((data & 0x00ffffffffffffffULL) == 0x0080cd000000adb8ULL) {
// With SA_SIGINFO set, the return sequence is:
@@ -157,7 +165,7 @@
// Get the location of the sigcontext data.
uint32_t ptr;
- if (!process_memory->ReadFully(sp() + 8, &ptr, sizeof(ptr))) {
+ if (!process_memory->ReadFully(regs_[X86_REG_SP] + 8, &ptr, sizeof(ptr))) {
return false;
}
// Only read the portion of the data structure we care about.
@@ -171,4 +179,8 @@
return false;
}
+Regs* RegsX86::Clone() {
+ return new RegsX86(*this);
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/RegsX86_64.cpp b/libunwindstack/RegsX86_64.cpp
index 3175a90..ebad3f4 100644
--- a/libunwindstack/RegsX86_64.cpp
+++ b/libunwindstack/RegsX86_64.cpp
@@ -28,33 +28,44 @@
namespace unwindstack {
-RegsX86_64::RegsX86_64()
- : RegsImpl<uint64_t>(X86_64_REG_LAST, X86_64_REG_SP, Location(LOCATION_SP_OFFSET, -8)) {}
+RegsX86_64::RegsX86_64() : RegsImpl<uint64_t>(X86_64_REG_LAST, Location(LOCATION_SP_OFFSET, -8)) {}
ArchEnum RegsX86_64::Arch() {
return ARCH_X86_64;
}
-uint64_t RegsX86_64::GetPcAdjustment(uint64_t rel_pc, Elf* elf) {
- if (!elf->valid() || rel_pc == 0) {
+uint64_t RegsX86_64::pc() {
+ return regs_[X86_64_REG_PC];
+}
+
+uint64_t RegsX86_64::sp() {
+ return regs_[X86_64_REG_SP];
+}
+
+void RegsX86_64::set_pc(uint64_t pc) {
+ regs_[X86_64_REG_PC] = pc;
+}
+
+void RegsX86_64::set_sp(uint64_t sp) {
+ regs_[X86_64_REG_SP] = sp;
+}
+
+uint64_t RegsX86_64::GetPcAdjustment(uint64_t rel_pc, Elf*) {
+ if (rel_pc == 0) {
return 0;
}
return 1;
}
-void RegsX86_64::SetFromRaw() {
- set_pc(regs_[X86_64_REG_PC]);
- set_sp(regs_[X86_64_REG_SP]);
-}
-
bool RegsX86_64::SetPcFromReturnAddress(Memory* process_memory) {
// Attempt to get the return address from the top of the stack.
uint64_t new_pc;
- if (!process_memory->ReadFully(sp_, &new_pc, sizeof(new_pc)) || new_pc == pc()) {
+ if (!process_memory->ReadFully(regs_[X86_64_REG_SP], &new_pc, sizeof(new_pc)) ||
+ new_pc == regs_[X86_64_REG_PC]) {
return false;
}
- set_pc(new_pc);
+ regs_[X86_64_REG_PC] = new_pc;
return true;
}
@@ -100,7 +111,6 @@
(*regs)[X86_64_REG_RSP] = user->rsp;
(*regs)[X86_64_REG_RIP] = user->rip;
- regs->SetFromRaw();
return regs;
}
@@ -118,8 +128,6 @@
regs_[X86_64_REG_RCX] = ucontext->uc_mcontext.rcx;
regs_[X86_64_REG_RSP] = ucontext->uc_mcontext.rsp;
regs_[X86_64_REG_RIP] = ucontext->uc_mcontext.rip;
-
- SetFromRaw();
}
Regs* RegsX86_64::CreateFromUcontext(void* ucontext) {
@@ -152,7 +160,7 @@
// Read the mcontext data from the stack.
// sp points to the ucontext data structure, read only the mcontext part.
x86_64_ucontext_t x86_64_ucontext;
- if (!process_memory->ReadFully(sp() + 0x28, &x86_64_ucontext.uc_mcontext,
+ if (!process_memory->ReadFully(regs_[X86_64_REG_SP] + 0x28, &x86_64_ucontext.uc_mcontext,
sizeof(x86_64_mcontext_t))) {
return false;
}
@@ -160,4 +168,8 @@
return true;
}
+Regs* RegsX86_64::Clone() {
+ return new RegsX86_64(*this);
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/Symbols.cpp b/libunwindstack/Symbols.cpp
index 25def40..14ebdbb 100644
--- a/libunwindstack/Symbols.cpp
+++ b/libunwindstack/Symbols.cpp
@@ -54,10 +54,7 @@
}
template <typename SymType>
-bool Symbols::GetName(uint64_t addr, uint64_t load_bias, Memory* elf_memory, std::string* name,
- uint64_t* func_offset) {
- addr += load_bias;
-
+bool Symbols::GetName(uint64_t addr, Memory* elf_memory, std::string* name, uint64_t* func_offset) {
if (symbols_.size() != 0) {
const Info* info = GetInfoFromCache(addr);
if (info) {
@@ -81,9 +78,6 @@
if (entry.st_shndx != SHN_UNDEF && ELF32_ST_TYPE(entry.st_info) == STT_FUNC) {
// Treat st_value as virtual address.
uint64_t start_offset = entry.st_value;
- if (entry.st_shndx != SHN_ABS) {
- start_offset += load_bias;
- }
uint64_t end_offset = start_offset + entry.st_size;
// Cache the value.
@@ -134,8 +128,8 @@
}
// Instantiate all of the needed template functions.
-template bool Symbols::GetName<Elf32_Sym>(uint64_t, uint64_t, Memory*, std::string*, uint64_t*);
-template bool Symbols::GetName<Elf64_Sym>(uint64_t, uint64_t, Memory*, std::string*, uint64_t*);
+template bool Symbols::GetName<Elf32_Sym>(uint64_t, Memory*, std::string*, uint64_t*);
+template bool Symbols::GetName<Elf64_Sym>(uint64_t, Memory*, std::string*, uint64_t*);
template bool Symbols::GetGlobal<Elf32_Sym>(Memory*, const std::string&, uint64_t*);
template bool Symbols::GetGlobal<Elf64_Sym>(Memory*, const std::string&, uint64_t*);
diff --git a/libunwindstack/Symbols.h b/libunwindstack/Symbols.h
index 7d239c1..7fcd067 100644
--- a/libunwindstack/Symbols.h
+++ b/libunwindstack/Symbols.h
@@ -44,8 +44,7 @@
const Info* GetInfoFromCache(uint64_t addr);
template <typename SymType>
- bool GetName(uint64_t addr, uint64_t load_bias, Memory* elf_memory, std::string* name,
- uint64_t* func_offset);
+ bool GetName(uint64_t addr, Memory* elf_memory, std::string* name, uint64_t* func_offset);
template <typename SymType>
bool GetGlobal(Memory* elf_memory, const std::string& name, uint64_t* memory_address);
diff --git a/libunwindstack/Unwinder.cpp b/libunwindstack/Unwinder.cpp
index 7da6994..099cc9e 100644
--- a/libunwindstack/Unwinder.cpp
+++ b/libunwindstack/Unwinder.cpp
@@ -29,6 +29,7 @@
#include <unwindstack/Elf.h>
#include <unwindstack/JitDebug.h>
#include <unwindstack/MapInfo.h>
+#include <unwindstack/Maps.h>
#include <unwindstack/Unwinder.h>
#if !defined(NO_LIBDEXFILE_SUPPORT)
@@ -61,7 +62,9 @@
frame->map_offset = info->offset;
frame->map_load_bias = info->load_bias;
frame->map_flags = info->flags;
- frame->map_name = info->name;
+ if (resolve_names_) {
+ frame->map_name = info->name;
+ }
frame->rel_pc = dex_pc - info->start;
} else {
frame->rel_pc = dex_pc;
@@ -77,7 +80,6 @@
return;
}
- // dex_files_->GetMethodInformation(dex_pc - dex_offset, dex_offset, info, &frame->function_name,
dex_files_->GetMethodInformation(maps_, info, dex_pc, &frame->function_name,
&frame->function_offset);
#endif
@@ -97,7 +99,9 @@
return;
}
- frame->map_name = map_info->name;
+ if (resolve_names_) {
+ frame->map_name = map_info->name;
+ }
frame->map_offset = map_info->offset;
frame->map_start = map_info->start;
frame->map_end = map_info->end;
@@ -139,26 +143,31 @@
uint64_t cur_sp = regs_->sp();
MapInfo* map_info = maps_->Find(regs_->pc());
- uint64_t rel_pc;
uint64_t pc_adjustment = 0;
uint64_t step_pc;
+ uint64_t rel_pc;
Elf* elf;
if (map_info == nullptr) {
- rel_pc = regs_->pc();
- step_pc = rel_pc;
+ step_pc = regs_->pc();
+ rel_pc = step_pc;
last_error_.code = ERROR_INVALID_MAP;
} else {
if (ShouldStop(map_suffixes_to_ignore, map_info->name)) {
break;
}
elf = map_info->GetElf(process_memory_, true);
- rel_pc = elf->GetRelPc(regs_->pc(), map_info);
+ step_pc = regs_->pc();
+ rel_pc = elf->GetRelPc(step_pc, map_info);
+ // Everyone except elf data in gdb jit debug maps uses the relative pc.
+ if (!(map_info->flags & MAPS_FLAGS_JIT_SYMFILE_MAP)) {
+ step_pc = rel_pc;
+ }
if (adjust_pc) {
pc_adjustment = regs_->GetPcAdjustment(rel_pc, elf);
} else {
pc_adjustment = 0;
}
- step_pc = rel_pc - pc_adjustment;
+ step_pc -= pc_adjustment;
// If the pc is in an invalid elf file, try and get an Elf object
// using the jit debug information.
@@ -211,8 +220,7 @@
in_device_map = true;
} else {
bool finished;
- stepped = elf->Step(rel_pc, step_pc, map_info->elf_offset, regs_, process_memory_.get(),
- &finished);
+ stepped = elf->Step(rel_pc, step_pc, regs_, process_memory_.get(), &finished);
elf->GetLastError(&last_error_);
if (stepped && finished) {
break;
diff --git a/libunwindstack/include/unwindstack/DwarfLocation.h b/libunwindstack/include/unwindstack/DwarfLocation.h
index 0881182..3d50ccf 100644
--- a/libunwindstack/include/unwindstack/DwarfLocation.h
+++ b/libunwindstack/include/unwindstack/DwarfLocation.h
@@ -23,6 +23,8 @@
namespace unwindstack {
+struct DwarfCie;
+
enum DwarfLocationEnum : uint8_t {
DWARF_LOCATION_INVALID = 0,
DWARF_LOCATION_UNDEFINED,
@@ -38,7 +40,13 @@
uint64_t values[2];
};
-typedef std::unordered_map<uint32_t, DwarfLocation> dwarf_loc_regs_t;
+struct DwarfLocations : public std::unordered_map<uint32_t, DwarfLocation> {
+ const DwarfCie* cie;
+ // The range of PCs where the locations are valid (end is exclusive).
+ uint64_t pc_start = 0;
+ uint64_t pc_end = 0;
+};
+typedef DwarfLocations dwarf_loc_regs_t;
} // namespace unwindstack
diff --git a/libunwindstack/include/unwindstack/DwarfSection.h b/libunwindstack/include/unwindstack/DwarfSection.h
index da91fd0..e9942de 100644
--- a/libunwindstack/include/unwindstack/DwarfSection.h
+++ b/libunwindstack/include/unwindstack/DwarfSection.h
@@ -20,6 +20,7 @@
#include <stdint.h>
#include <iterator>
+#include <map>
#include <unordered_map>
#include <unwindstack/DwarfError.h>
@@ -42,7 +43,12 @@
class iterator : public std::iterator<std::bidirectional_iterator_tag, DwarfFde*> {
public:
- iterator(DwarfSection* section, size_t index) : section_(section), index_(index) {}
+ iterator(DwarfSection* section, size_t index) : index_(index) {
+ section->GetFdes(&fdes_);
+ if (index_ == static_cast<size_t>(-1)) {
+ index_ = fdes_.size();
+ }
+ }
iterator& operator++() {
index_++;
@@ -64,32 +70,31 @@
bool operator==(const iterator& rhs) { return this->index_ == rhs.index_; }
bool operator!=(const iterator& rhs) { return this->index_ != rhs.index_; }
- const DwarfFde* operator*() { return section_->GetFdeFromIndex(index_); }
+ const DwarfFde* operator*() {
+ if (index_ > fdes_.size()) return nullptr;
+ return fdes_[index_];
+ }
private:
- DwarfSection* section_ = nullptr;
+ std::vector<const DwarfFde*> fdes_;
size_t index_ = 0;
};
iterator begin() { return iterator(this, 0); }
- iterator end() { return iterator(this, fde_count_); }
+ iterator end() { return iterator(this, static_cast<size_t>(-1)); }
DwarfErrorCode LastErrorCode() { return last_error_.code; }
uint64_t LastErrorAddress() { return last_error_.address; }
- virtual bool Init(uint64_t offset, uint64_t size) = 0;
+ virtual bool Init(uint64_t offset, uint64_t size, uint64_t load_bias) = 0;
virtual bool Eval(const DwarfCie*, Memory*, const dwarf_loc_regs_t&, Regs*, bool*) = 0;
- virtual bool GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) = 0;
+ virtual bool Log(uint8_t indent, uint64_t pc, const DwarfFde* fde) = 0;
- virtual bool Log(uint8_t indent, uint64_t pc, uint64_t load_bias, const DwarfFde* fde) = 0;
+ virtual void GetFdes(std::vector<const DwarfFde*>* fdes) = 0;
- virtual const DwarfFde* GetFdeFromIndex(size_t index) = 0;
-
- const DwarfFde* GetFdeFromPc(uint64_t pc);
-
- virtual const DwarfFde* GetFdeFromOffset(uint64_t fde_offset) = 0;
+ virtual const DwarfFde* GetFdeFromPc(uint64_t pc) = 0;
virtual bool GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde, dwarf_loc_regs_t* loc_regs) = 0;
@@ -108,61 +113,82 @@
uint32_t cie32_value_ = 0;
uint64_t cie64_value_ = 0;
- uint64_t fde_count_ = 0;
std::unordered_map<uint64_t, DwarfFde> fde_entries_;
std::unordered_map<uint64_t, DwarfCie> cie_entries_;
std::unordered_map<uint64_t, dwarf_loc_regs_t> cie_loc_regs_;
+ std::map<uint64_t, dwarf_loc_regs_t> loc_regs_; // Single row indexed by pc_end.
};
template <typename AddressType>
class DwarfSectionImpl : public DwarfSection {
public:
- struct FdeInfo {
- FdeInfo(uint64_t offset, uint64_t start, uint64_t length)
- : offset(offset), start(start), end(start + length) {}
-
- uint64_t offset;
- AddressType start;
- AddressType end;
- };
-
DwarfSectionImpl(Memory* memory) : DwarfSection(memory) {}
virtual ~DwarfSectionImpl() = default;
- bool Init(uint64_t offset, uint64_t size) override;
+ const DwarfCie* GetCieFromOffset(uint64_t offset);
- bool GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) override;
-
- const DwarfFde* GetFdeFromIndex(size_t index) override;
+ const DwarfFde* GetFdeFromOffset(uint64_t offset);
bool EvalRegister(const DwarfLocation* loc, uint32_t reg, AddressType* reg_ptr, void* info);
bool Eval(const DwarfCie* cie, Memory* regular_memory, const dwarf_loc_regs_t& loc_regs,
Regs* regs, bool* finished) override;
- const DwarfCie* GetCie(uint64_t offset);
- bool FillInCie(DwarfCie* cie);
-
- const DwarfFde* GetFdeFromOffset(uint64_t offset) override;
- bool FillInFde(DwarfFde* fde);
-
bool GetCfaLocationInfo(uint64_t pc, const DwarfFde* fde, dwarf_loc_regs_t* loc_regs) override;
- bool Log(uint8_t indent, uint64_t pc, uint64_t load_bias, const DwarfFde* fde) override;
+ bool Log(uint8_t indent, uint64_t pc, const DwarfFde* fde) override;
protected:
+ bool FillInCieHeader(DwarfCie* cie);
+
+ bool FillInCie(DwarfCie* cie);
+
+ bool FillInFdeHeader(DwarfFde* fde);
+
+ bool FillInFde(DwarfFde* fde);
+
bool EvalExpression(const DwarfLocation& loc, Memory* regular_memory, AddressType* value,
RegsInfo<AddressType>* regs_info, bool* is_dex_pc);
- bool GetCieInfo(uint8_t* segment_size, uint8_t* encoding);
+ uint64_t load_bias_ = 0;
+ uint64_t entries_offset_ = 0;
+ uint64_t entries_end_ = 0;
+ uint64_t pc_offset_ = 0;
+};
- bool AddFdeInfo(uint64_t entry_offset, uint8_t segment_size, uint8_t encoding);
+template <typename AddressType>
+class DwarfSectionImplNoHdr : public DwarfSectionImpl<AddressType> {
+ public:
+ // Add these so that the protected members of DwarfSectionImpl
+ // can be accessed without needing a this->.
+ using DwarfSectionImpl<AddressType>::memory_;
+ using DwarfSectionImpl<AddressType>::pc_offset_;
+ using DwarfSectionImpl<AddressType>::entries_offset_;
+ using DwarfSectionImpl<AddressType>::entries_end_;
+ using DwarfSectionImpl<AddressType>::last_error_;
+ using DwarfSectionImpl<AddressType>::load_bias_;
+ using DwarfSectionImpl<AddressType>::cie_entries_;
+ using DwarfSectionImpl<AddressType>::fde_entries_;
+ using DwarfSectionImpl<AddressType>::cie32_value_;
+ using DwarfSectionImpl<AddressType>::cie64_value_;
- bool CreateSortedFdeList();
+ DwarfSectionImplNoHdr(Memory* memory) : DwarfSectionImpl<AddressType>(memory) {}
+ virtual ~DwarfSectionImplNoHdr() = default;
- std::vector<FdeInfo> fdes_;
- uint64_t entries_offset_;
- uint64_t entries_end_;
+ bool Init(uint64_t offset, uint64_t size, uint64_t load_bias) override;
+
+ const DwarfFde* GetFdeFromPc(uint64_t pc) override;
+
+ void GetFdes(std::vector<const DwarfFde*>* fdes) override;
+
+ protected:
+ bool GetNextCieOrFde(DwarfFde** fde_entry);
+
+ void InsertFde(const DwarfFde* fde);
+
+ uint64_t next_entries_offset_ = 0;
+
+ std::map<uint64_t, std::pair<uint64_t, const DwarfFde*>> fdes_;
};
} // namespace unwindstack
diff --git a/libunwindstack/include/unwindstack/Elf.h b/libunwindstack/include/unwindstack/Elf.h
index 385973e..f4cdbda 100644
--- a/libunwindstack/include/unwindstack/Elf.h
+++ b/libunwindstack/include/unwindstack/Elf.h
@@ -65,8 +65,8 @@
uint64_t GetRelPc(uint64_t pc, const MapInfo* map_info);
- bool Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs,
- Memory* process_memory, bool* finished);
+ bool Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, Regs* regs, Memory* process_memory,
+ bool* finished);
ElfInterface* CreateInterfaceFromMemory(Memory* memory);
diff --git a/libunwindstack/include/unwindstack/ElfInterface.h b/libunwindstack/include/unwindstack/ElfInterface.h
index 3a221bc..a45eba8 100644
--- a/libunwindstack/include/unwindstack/ElfInterface.h
+++ b/libunwindstack/include/unwindstack/ElfInterface.h
@@ -54,17 +54,15 @@
virtual bool Init(uint64_t* load_bias) = 0;
- virtual void InitHeaders() = 0;
+ virtual void InitHeaders(uint64_t load_bias) = 0;
virtual bool GetSoname(std::string* name) = 0;
- virtual bool GetFunctionName(uint64_t addr, uint64_t load_bias, std::string* name,
- uint64_t* offset) = 0;
+ virtual bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* offset) = 0;
virtual bool GetGlobalVariable(const std::string& name, uint64_t* memory_address) = 0;
- virtual bool Step(uint64_t rel_pc, uint64_t load_bias, Regs* regs, Memory* process_memory,
- bool* finished);
+ virtual bool Step(uint64_t rel_pc, Regs* regs, Memory* process_memory, bool* finished);
virtual bool IsValidPc(uint64_t pc);
@@ -100,28 +98,27 @@
protected:
template <typename AddressType>
- void InitHeadersWithTemplate();
+ void InitHeadersWithTemplate(uint64_t load_bias);
template <typename EhdrType, typename PhdrType, typename ShdrType>
bool ReadAllHeaders(uint64_t* load_bias);
template <typename EhdrType, typename PhdrType>
- bool ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias);
+ void ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias);
template <typename EhdrType, typename ShdrType>
- bool ReadSectionHeaders(const EhdrType& ehdr);
+ void ReadSectionHeaders(const EhdrType& ehdr);
template <typename DynType>
bool GetSonameWithTemplate(std::string* soname);
template <typename SymType>
- bool GetFunctionNameWithTemplate(uint64_t addr, uint64_t load_bias, std::string* name,
- uint64_t* func_offset);
+ bool GetFunctionNameWithTemplate(uint64_t addr, std::string* name, uint64_t* func_offset);
template <typename SymType>
bool GetGlobalVariableWithTemplate(const std::string& name, uint64_t* memory_address);
- virtual bool HandleType(uint64_t, uint32_t, uint64_t) { return false; }
+ virtual void HandleUnknownType(uint32_t, uint64_t, uint64_t) {}
template <typename EhdrType>
static void GetMaxSizeWithTemplate(Memory* memory, uint64_t* size);
@@ -169,15 +166,16 @@
return ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(load_bias);
}
- void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint32_t>(); }
+ void InitHeaders(uint64_t load_bias) override {
+ ElfInterface::InitHeadersWithTemplate<uint32_t>(load_bias);
+ }
bool GetSoname(std::string* soname) override {
return ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(soname);
}
- bool GetFunctionName(uint64_t addr, uint64_t load_bias, std::string* name,
- uint64_t* func_offset) override {
- return ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(addr, load_bias, name, func_offset);
+ bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) override {
+ return ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(addr, name, func_offset);
}
bool GetGlobalVariable(const std::string& name, uint64_t* memory_address) override {
@@ -198,15 +196,16 @@
return ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(load_bias);
}
- void InitHeaders() override { ElfInterface::InitHeadersWithTemplate<uint64_t>(); }
+ void InitHeaders(uint64_t load_bias) override {
+ ElfInterface::InitHeadersWithTemplate<uint64_t>(load_bias);
+ }
bool GetSoname(std::string* soname) override {
return ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(soname);
}
- bool GetFunctionName(uint64_t addr, uint64_t load_bias, std::string* name,
- uint64_t* func_offset) override {
- return ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(addr, load_bias, name, func_offset);
+ bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) override {
+ return ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(addr, name, func_offset);
}
bool GetGlobalVariable(const std::string& name, uint64_t* memory_address) override {
diff --git a/libunwindstack/include/unwindstack/LocalUnwinder.h b/libunwindstack/include/unwindstack/LocalUnwinder.h
new file mode 100644
index 0000000..80bb53e
--- /dev/null
+++ b/libunwindstack/include/unwindstack/LocalUnwinder.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef _LIBUNWINDSTACK_LOCAL_UNWINDER_H
+#define _LIBUNWINDSTACK_LOCAL_UNWINDER_H
+
+#include <pthread.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <unwindstack/Error.h>
+#include <unwindstack/Maps.h>
+#include <unwindstack/Memory.h>
+
+namespace unwindstack {
+
+// Forward declarations.
+class Elf;
+struct MapInfo;
+
+struct LocalFrameData {
+ LocalFrameData(MapInfo* map_info, uint64_t pc, uint64_t rel_pc, const std::string& function_name,
+ uint64_t function_offset)
+ : map_info(map_info),
+ pc(pc),
+ rel_pc(rel_pc),
+ function_name(function_name),
+ function_offset(function_offset) {}
+
+ MapInfo* map_info;
+ uint64_t pc;
+ uint64_t rel_pc;
+ std::string function_name;
+ uint64_t function_offset;
+};
+
+// This is a specialized class that should only be used for doing local unwinds.
+// The Unwind call can be made as multiple times on the same object, and it can
+// be called by multiple threads at the same time.
+// It is designed to be used in debugging circumstances to get a stack trace
+// as fast as possible.
+class LocalUnwinder {
+ public:
+ LocalUnwinder() = default;
+ LocalUnwinder(const std::vector<std::string>& skip_libraries) : skip_libraries_(skip_libraries) {}
+ ~LocalUnwinder() = default;
+
+ bool Init();
+
+ bool Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames);
+
+ bool ShouldSkipLibrary(const std::string& map_name);
+
+ MapInfo* GetMapInfo(uint64_t pc);
+
+ ErrorCode LastErrorCode() { return last_error_.code; }
+ uint64_t LastErrorAddress() { return last_error_.address; }
+
+ private:
+ pthread_rwlock_t maps_rwlock_;
+ std::unique_ptr<LocalUpdatableMaps> maps_ = nullptr;
+ std::shared_ptr<Memory> process_memory_;
+ std::vector<std::string> skip_libraries_;
+ ErrorData last_error_;
+};
+
+} // namespace unwindstack
+
+#endif // _LIBUNWINDSTACK_LOCAL_UNWINDER_H
diff --git a/libunwindstack/include/unwindstack/MapInfo.h b/libunwindstack/include/unwindstack/MapInfo.h
index a57fe68..ac0df41 100644
--- a/libunwindstack/include/unwindstack/MapInfo.h
+++ b/libunwindstack/include/unwindstack/MapInfo.h
@@ -34,6 +34,13 @@
struct MapInfo {
MapInfo() = default;
MapInfo(uint64_t start, uint64_t end) : start(start), end(end) {}
+ MapInfo(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, const char* name)
+ : start(start),
+ end(end),
+ offset(offset),
+ flags(flags),
+ name(name),
+ load_bias(static_cast<uint64_t>(-1)) {}
MapInfo(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, const std::string& name)
: start(start),
end(end),
diff --git a/libunwindstack/include/unwindstack/Maps.h b/libunwindstack/include/unwindstack/Maps.h
index 17a2d28..67fbed2 100644
--- a/libunwindstack/include/unwindstack/Maps.h
+++ b/libunwindstack/include/unwindstack/Maps.h
@@ -30,6 +30,10 @@
// Special flag to indicate a map is in /dev/. However, a map in
// /dev/ashmem/... does not set this flag.
static constexpr int MAPS_FLAGS_DEVICE_MAP = 0x8000;
+// Special flag to indicate that this map represents an elf file
+// created by ART for use with the gdb jit debug interface.
+// This should only ever appear in offline maps data.
+static constexpr int MAPS_FLAGS_JIT_SYMFILE_MAP = 0x4000;
class Maps {
public:
@@ -45,6 +49,8 @@
void Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, const std::string& name,
uint64_t load_bias);
+ void Sort();
+
typedef std::vector<MapInfo*>::iterator iterator;
iterator begin() { return maps_.begin(); }
iterator end() { return maps_.end(); }
@@ -81,6 +87,19 @@
virtual ~LocalMaps() = default;
};
+class LocalUpdatableMaps : public Maps {
+ public:
+ LocalUpdatableMaps() : Maps() {}
+ virtual ~LocalUpdatableMaps();
+
+ bool Reparse();
+
+ const std::string GetMapsFile() const override;
+
+ private:
+ std::vector<MapInfo*> saved_maps_;
+};
+
class BufferMaps : public Maps {
public:
BufferMaps(const char* buffer) : buffer_(buffer) {}
diff --git a/libunwindstack/include/unwindstack/Memory.h b/libunwindstack/include/unwindstack/Memory.h
index 19bce04..dee5e98 100644
--- a/libunwindstack/include/unwindstack/Memory.h
+++ b/libunwindstack/include/unwindstack/Memory.h
@@ -41,18 +41,6 @@
bool ReadFully(uint64_t addr, void* dst, size_t size);
- inline bool ReadField(uint64_t addr, void* start, void* field, size_t size) {
- if (reinterpret_cast<uintptr_t>(field) < reinterpret_cast<uintptr_t>(start)) {
- return false;
- }
- uint64_t offset = reinterpret_cast<uintptr_t>(field) - reinterpret_cast<uintptr_t>(start);
- if (__builtin_add_overflow(addr, offset, &offset)) {
- return false;
- }
- // The read will check if offset + size overflows.
- return ReadFully(offset, field, size);
- }
-
inline bool Read32(uint64_t addr, uint32_t* dst) {
return ReadFully(addr, dst, sizeof(uint32_t));
}
@@ -151,6 +139,21 @@
std::unique_ptr<MemoryRange> memory_;
};
+class MemoryOfflineBuffer : public Memory {
+ public:
+ MemoryOfflineBuffer(const uint8_t* data, uint64_t start, uint64_t end);
+ virtual ~MemoryOfflineBuffer() = default;
+
+ void Reset(const uint8_t* data, uint64_t start, uint64_t end);
+
+ size_t Read(uint64_t addr, void* dst, size_t size) override;
+
+ private:
+ const uint8_t* data_;
+ uint64_t start_;
+ uint64_t end_;
+};
+
class MemoryOfflineParts : public Memory {
public:
MemoryOfflineParts() = default;
diff --git a/libunwindstack/include/unwindstack/Regs.h b/libunwindstack/include/unwindstack/Regs.h
index b0e7ea1..878ced3 100644
--- a/libunwindstack/include/unwindstack/Regs.h
+++ b/libunwindstack/include/unwindstack/Regs.h
@@ -45,8 +45,8 @@
int16_t value;
};
- Regs(uint16_t total_regs, uint16_t sp_reg, const Location& return_loc)
- : total_regs_(total_regs), sp_reg_(sp_reg), return_loc_(return_loc) {}
+ Regs(uint16_t total_regs, const Location& return_loc)
+ : total_regs_(total_regs), return_loc_(return_loc) {}
virtual ~Regs() = default;
virtual ArchEnum Arch() = 0;
@@ -57,6 +57,9 @@
virtual uint64_t pc() = 0;
virtual uint64_t sp() = 0;
+ virtual void set_pc(uint64_t pc) = 0;
+ virtual void set_sp(uint64_t sp) = 0;
+
uint64_t dex_pc() { return dex_pc_; }
void set_dex_pc(uint64_t dex_pc) { dex_pc_ = dex_pc; }
@@ -64,15 +67,14 @@
virtual bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) = 0;
- virtual void SetFromRaw() = 0;
-
virtual bool SetPcFromReturnAddress(Memory* process_memory) = 0;
virtual void IterateRegisters(std::function<void(const char*, uint64_t)>) = 0;
- uint16_t sp_reg() { return sp_reg_; }
uint16_t total_regs() { return total_regs_; }
+ virtual Regs* Clone() = 0;
+
static ArchEnum CurrentArch();
static Regs* RemoteGet(pid_t pid);
static Regs* CreateFromUcontext(ArchEnum arch, void* ucontext);
@@ -80,7 +82,6 @@
protected:
uint16_t total_regs_;
- uint16_t sp_reg_;
Location return_loc_;
uint64_t dex_pc_ = 0;
};
@@ -88,16 +89,10 @@
template <typename AddressType>
class RegsImpl : public Regs {
public:
- RegsImpl(uint16_t total_regs, uint16_t sp_reg, Location return_loc)
- : Regs(total_regs, sp_reg, return_loc), regs_(total_regs) {}
+ RegsImpl(uint16_t total_regs, Location return_loc)
+ : Regs(total_regs, return_loc), regs_(total_regs) {}
virtual ~RegsImpl() = default;
- uint64_t pc() override { return pc_; }
- uint64_t sp() override { return sp_; }
-
- void set_pc(AddressType pc) { pc_ = pc; }
- void set_sp(AddressType sp) { sp_ = sp; }
-
bool Is32Bit() override { return sizeof(AddressType) == sizeof(uint32_t); }
inline AddressType& operator[](size_t reg) { return regs_[reg]; }
@@ -111,8 +106,6 @@
}
protected:
- AddressType pc_;
- AddressType sp_;
std::vector<AddressType> regs_;
};
diff --git a/libunwindstack/include/unwindstack/RegsArm.h b/libunwindstack/include/unwindstack/RegsArm.h
index 5af90d3..44f6744 100644
--- a/libunwindstack/include/unwindstack/RegsArm.h
+++ b/libunwindstack/include/unwindstack/RegsArm.h
@@ -34,17 +34,23 @@
RegsArm();
virtual ~RegsArm() = default;
- virtual ArchEnum Arch() override final;
+ ArchEnum Arch() override final;
uint64_t GetPcAdjustment(uint64_t rel_pc, Elf* elf) override;
- void SetFromRaw() override;
-
bool SetPcFromReturnAddress(Memory* process_memory) override;
bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) override;
- virtual void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+ void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+
+ uint64_t pc() override;
+ uint64_t sp() override;
+
+ void set_pc(uint64_t pc) override;
+ void set_sp(uint64_t sp) override;
+
+ Regs* Clone() override final;
static Regs* Read(void* data);
diff --git a/libunwindstack/include/unwindstack/RegsArm64.h b/libunwindstack/include/unwindstack/RegsArm64.h
index cb05732..a72f91f 100644
--- a/libunwindstack/include/unwindstack/RegsArm64.h
+++ b/libunwindstack/include/unwindstack/RegsArm64.h
@@ -34,17 +34,23 @@
RegsArm64();
virtual ~RegsArm64() = default;
- virtual ArchEnum Arch() override final;
+ ArchEnum Arch() override final;
uint64_t GetPcAdjustment(uint64_t rel_pc, Elf* elf) override;
- void SetFromRaw() override;
-
bool SetPcFromReturnAddress(Memory* process_memory) override;
bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) override;
- virtual void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+ void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+
+ uint64_t pc() override;
+ uint64_t sp() override;
+
+ void set_pc(uint64_t pc) override;
+ void set_sp(uint64_t sp) override;
+
+ Regs* Clone() override final;
static Regs* Read(void* data);
diff --git a/libunwindstack/include/unwindstack/RegsGetLocal.h b/libunwindstack/include/unwindstack/RegsGetLocal.h
index 557eace..f0b5e3a 100644
--- a/libunwindstack/include/unwindstack/RegsGetLocal.h
+++ b/libunwindstack/include/unwindstack/RegsGetLocal.h
@@ -33,8 +33,7 @@
#if defined(__arm__)
-inline __always_inline void RegsGetLocal(Regs* regs) {
- void* reg_data = regs->RawData();
+inline __attribute__((__always_inline__)) void AsmGetRegs(void* reg_data) {
asm volatile(
".align 2\n"
"bx pc\n"
@@ -51,14 +50,11 @@
: [base] "+r"(reg_data)
:
: "memory");
-
- regs->SetFromRaw();
}
#elif defined(__aarch64__)
-inline __always_inline void RegsGetLocal(Regs* regs) {
- void* reg_data = regs->RawData();
+inline __attribute__((__always_inline__)) void AsmGetRegs(void* reg_data) {
asm volatile(
"1:\n"
"stp x0, x1, [%[base], #0]\n"
@@ -83,21 +79,18 @@
: [base] "+r"(reg_data)
:
: "x12", "x13", "memory");
-
- regs->SetFromRaw();
}
#elif defined(__i386__) || defined(__x86_64__) || defined(__mips__)
extern "C" void AsmGetRegs(void* regs);
-inline void RegsGetLocal(Regs* regs) {
- AsmGetRegs(regs->RawData());
+#endif
- regs->SetFromRaw();
+inline __attribute__((__always_inline__)) void RegsGetLocal(Regs* regs) {
+ AsmGetRegs(regs->RawData());
}
-#endif
} // namespace unwindstack
diff --git a/libunwindstack/include/unwindstack/RegsMips.h b/libunwindstack/include/unwindstack/RegsMips.h
index 8e3c01f..c9dd202 100644
--- a/libunwindstack/include/unwindstack/RegsMips.h
+++ b/libunwindstack/include/unwindstack/RegsMips.h
@@ -34,17 +34,23 @@
RegsMips();
virtual ~RegsMips() = default;
- virtual ArchEnum Arch() override final;
+ ArchEnum Arch() override final;
uint64_t GetPcAdjustment(uint64_t rel_pc, Elf* elf) override;
- void SetFromRaw() override;
-
bool SetPcFromReturnAddress(Memory* process_memory) override;
bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) override;
- virtual void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+ void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+
+ uint64_t pc() override;
+ uint64_t sp() override;
+
+ void set_pc(uint64_t pc) override;
+ void set_sp(uint64_t sp) override;
+
+ Regs* Clone() override final;
static Regs* Read(void* data);
diff --git a/libunwindstack/include/unwindstack/RegsMips64.h b/libunwindstack/include/unwindstack/RegsMips64.h
index 8c2d443..7c42812 100644
--- a/libunwindstack/include/unwindstack/RegsMips64.h
+++ b/libunwindstack/include/unwindstack/RegsMips64.h
@@ -34,17 +34,23 @@
RegsMips64();
virtual ~RegsMips64() = default;
- virtual ArchEnum Arch() override final;
+ ArchEnum Arch() override final;
uint64_t GetPcAdjustment(uint64_t rel_pc, Elf* elf) override;
- void SetFromRaw() override;
-
bool SetPcFromReturnAddress(Memory* process_memory) override;
bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) override;
- virtual void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+ void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+
+ uint64_t pc() override;
+ uint64_t sp() override;
+
+ void set_pc(uint64_t pc) override;
+ void set_sp(uint64_t sp) override;
+
+ Regs* Clone() override final;
static Regs* Read(void* data);
diff --git a/libunwindstack/include/unwindstack/RegsX86.h b/libunwindstack/include/unwindstack/RegsX86.h
index 1bc145d..d19e449 100644
--- a/libunwindstack/include/unwindstack/RegsX86.h
+++ b/libunwindstack/include/unwindstack/RegsX86.h
@@ -35,19 +35,25 @@
RegsX86();
virtual ~RegsX86() = default;
- virtual ArchEnum Arch() override final;
+ ArchEnum Arch() override final;
uint64_t GetPcAdjustment(uint64_t rel_pc, Elf* elf) override;
- void SetFromRaw() override;
-
bool SetPcFromReturnAddress(Memory* process_memory) override;
bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) override;
void SetFromUcontext(x86_ucontext_t* ucontext);
- virtual void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+ void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+
+ uint64_t pc() override;
+ uint64_t sp() override;
+
+ void set_pc(uint64_t pc) override;
+ void set_sp(uint64_t sp) override;
+
+ Regs* Clone() override final;
static Regs* Read(void* data);
diff --git a/libunwindstack/include/unwindstack/RegsX86_64.h b/libunwindstack/include/unwindstack/RegsX86_64.h
index 4cd45d4..dc9a220 100644
--- a/libunwindstack/include/unwindstack/RegsX86_64.h
+++ b/libunwindstack/include/unwindstack/RegsX86_64.h
@@ -35,19 +35,25 @@
RegsX86_64();
virtual ~RegsX86_64() = default;
- virtual ArchEnum Arch() override final;
+ ArchEnum Arch() override final;
uint64_t GetPcAdjustment(uint64_t rel_pc, Elf* elf) override;
- void SetFromRaw() override;
-
bool SetPcFromReturnAddress(Memory* process_memory) override;
bool StepIfSignalHandler(uint64_t rel_pc, Elf* elf, Memory* process_memory) override;
void SetFromUcontext(x86_64_ucontext_t* ucontext);
- virtual void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+ void IterateRegisters(std::function<void(const char*, uint64_t)>) override final;
+
+ uint64_t pc() override;
+ uint64_t sp() override;
+
+ void set_pc(uint64_t pc) override;
+ void set_sp(uint64_t sp) override;
+
+ Regs* Clone() override final;
static Regs* Read(void* data);
diff --git a/libunwindstack/tests/ArmExidxDecodeTest.cpp b/libunwindstack/tests/ArmExidxDecodeTest.cpp
index 8d6d00d..5f3d1ea 100644
--- a/libunwindstack/tests/ArmExidxDecodeTest.cpp
+++ b/libunwindstack/tests/ArmExidxDecodeTest.cpp
@@ -36,8 +36,6 @@
class ArmExidxDecodeTest : public ::testing::TestWithParam<std::string> {
protected:
void Init(Memory* process_memory = nullptr) {
- TearDown();
-
if (process_memory == nullptr) {
process_memory = &process_memory_;
}
@@ -50,8 +48,8 @@
regs_arm_->set_sp(0);
exidx_.reset(new ArmExidx(regs_arm_.get(), &elf_memory_, process_memory));
- if (log_) {
- exidx_->set_log(true);
+ if (log_ != ARM_LOG_NONE) {
+ exidx_->set_log(log_);
exidx_->set_log_indent(0);
exidx_->set_log_skip_execution(false);
}
@@ -60,14 +58,20 @@
}
void SetUp() override {
- if (GetParam() != "no_logging") {
- log_ = false;
+ if (GetParam() == "no_logging") {
+ log_ = ARM_LOG_NONE;
+ } else if (GetParam() == "register_logging") {
+ log_ = ARM_LOG_BY_REG;
} else {
- log_ = true;
+ log_ = ARM_LOG_FULL;
}
- ResetLogs();
elf_memory_.Clear();
process_memory_.Clear();
+ ResetExidx();
+ }
+
+ void ResetExidx() {
+ ResetLogs();
Init();
}
@@ -77,7 +81,7 @@
MemoryFake elf_memory_;
MemoryFake process_memory_;
- bool log_;
+ ArmLogType log_;
};
TEST_P(ArmExidxDecodeTest, vsp_incr) {
@@ -86,38 +90,59 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp + 4\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = vsp + 4\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13 + 4\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10004U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->clear();
data_->push_back(0x01);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp + 8\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = vsp + 8\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13 + 8\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x1000cU, exidx_->cfa());
+ ASSERT_EQ(0x10008U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->clear();
data_->push_back(0x3f);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp + 256\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = vsp + 256\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13 + 256\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x1010cU, exidx_->cfa());
+ ASSERT_EQ(0x10100U, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, vsp_decr) {
@@ -126,38 +151,59 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp - 4\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = vsp - 4\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13 - 4\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0xfffcU, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->clear();
data_->push_back(0x41);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp - 8\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = vsp - 8\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13 - 8\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0xfff4U, exidx_->cfa());
+ ASSERT_EQ(0xfff8U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->clear();
data_->push_back(0x7f);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp - 256\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = vsp - 256\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13 - 256\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0xfef4U, exidx_->cfa());
+ ASSERT_EQ(0xff00U, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, refuse_unwind) {
@@ -166,10 +212,14 @@
data_->push_back(0x00);
ASSERT_FALSE(exidx_->Decode());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind Refuse to unwind\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Refuse to unwind\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(ARM_STATUS_NO_UNWIND, exidx_->status());
}
@@ -182,29 +232,60 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_TRUE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r15}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r15}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 4\n"
+ "4 unwind r15 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10004U, exidx_->cfa());
ASSERT_EQ(0x10U, (*exidx_->regs())[15]);
- ResetLogs();
+ ResetExidx();
data_->push_back(0x8f);
data_->push_back(0xff);
for (size_t i = 0; i < 12; i++) {
- process_memory_.SetData32(0x10004 + i * 4, i + 0x20);
+ process_memory_.SetData32(0x10000 + i * 4, i + 0x20);
}
exidx_->set_pc_set(false);
ASSERT_TRUE(exidx_->Decode());
ASSERT_TRUE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15}\n",
- GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15}\n",
+ GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 48\n"
+ "4 unwind r4 = [cfa - 48]\n"
+ "4 unwind r5 = [cfa - 44]\n"
+ "4 unwind r6 = [cfa - 40]\n"
+ "4 unwind r7 = [cfa - 36]\n"
+ "4 unwind r8 = [cfa - 32]\n"
+ "4 unwind r9 = [cfa - 28]\n"
+ "4 unwind r10 = [cfa - 24]\n"
+ "4 unwind r11 = [cfa - 20]\n"
+ "4 unwind r12 = [cfa - 16]\n"
+ "4 unwind r13 = [cfa - 12]\n"
+ "4 unwind r14 = [cfa - 8]\n"
+ "4 unwind r15 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
// Popping r13 results in a modified cfa.
ASSERT_EQ(0x29U, exidx_->cfa());
@@ -222,7 +303,7 @@
ASSERT_EQ(0x2aU, (*exidx_->regs())[14]);
ASSERT_EQ(0x2bU, (*exidx_->regs())[15]);
- ResetLogs();
+ ResetExidx();
exidx_->set_cfa(0x10034);
data_->push_back(0x81);
data_->push_back(0x28);
@@ -233,10 +314,22 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r7, r9, r12}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r7, r9, r12}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 12\n"
+ "4 unwind r7 = [cfa - 12]\n"
+ "4 unwind r9 = [cfa - 8]\n"
+ "4 unwind r12 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10040U, exidx_->cfa());
ASSERT_EQ(0x11U, (*exidx_->regs())[7]);
@@ -255,34 +348,63 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = r0\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = r0\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r0\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(1U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
+ exidx_->set_cfa(0x100);
+ for (size_t i = 0; i < 15; i++) {
+ (*regs_arm_)[i] = i + 1;
+ }
data_->push_back(0x93);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = r3\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = r3\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r3\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(4U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
+ exidx_->set_cfa(0x100);
+ for (size_t i = 0; i < 15; i++) {
+ (*regs_arm_)[i] = i + 1;
+ }
data_->push_back(0x9e);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = r14\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = r14\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r14\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(15U, exidx_->cfa());
}
@@ -292,22 +414,30 @@
data_->push_back(0x9d);
ASSERT_FALSE(exidx_->Decode());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind [Reserved]\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind [Reserved]\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(ARM_STATUS_RESERVED, exidx_->status());
// 10011111: Reserved as prefix for Intel Wireless MMX register to register moves
- ResetLogs();
+ ResetExidx();
data_->push_back(0x9f);
ASSERT_FALSE(exidx_->Decode());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind [Reserved]\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind [Reserved]\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(ARM_STATUS_RESERVED, exidx_->status());
}
@@ -319,53 +449,93 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r4}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r4}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 4\n"
+ "4 unwind r4 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10004U, exidx_->cfa());
ASSERT_EQ(0x14U, (*exidx_->regs())[4]);
- ResetLogs();
+ ResetExidx();
data_->push_back(0xa3);
- process_memory_.SetData32(0x10004, 0x20);
- process_memory_.SetData32(0x10008, 0x30);
- process_memory_.SetData32(0x1000c, 0x40);
- process_memory_.SetData32(0x10010, 0x50);
+ process_memory_.SetData32(0x10000, 0x20);
+ process_memory_.SetData32(0x10004, 0x30);
+ process_memory_.SetData32(0x10008, 0x40);
+ process_memory_.SetData32(0x1000c, 0x50);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r4-r7}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r4-r7}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 16\n"
+ "4 unwind r4 = [cfa - 16]\n"
+ "4 unwind r5 = [cfa - 12]\n"
+ "4 unwind r6 = [cfa - 8]\n"
+ "4 unwind r7 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10014U, exidx_->cfa());
+ ASSERT_EQ(0x10010U, exidx_->cfa());
ASSERT_EQ(0x20U, (*exidx_->regs())[4]);
ASSERT_EQ(0x30U, (*exidx_->regs())[5]);
ASSERT_EQ(0x40U, (*exidx_->regs())[6]);
ASSERT_EQ(0x50U, (*exidx_->regs())[7]);
- ResetLogs();
+ ResetExidx();
data_->push_back(0xa7);
- process_memory_.SetData32(0x10014, 0x41);
- process_memory_.SetData32(0x10018, 0x51);
- process_memory_.SetData32(0x1001c, 0x61);
- process_memory_.SetData32(0x10020, 0x71);
- process_memory_.SetData32(0x10024, 0x81);
- process_memory_.SetData32(0x10028, 0x91);
- process_memory_.SetData32(0x1002c, 0xa1);
- process_memory_.SetData32(0x10030, 0xb1);
+ process_memory_.SetData32(0x10000, 0x41);
+ process_memory_.SetData32(0x10004, 0x51);
+ process_memory_.SetData32(0x10008, 0x61);
+ process_memory_.SetData32(0x1000c, 0x71);
+ process_memory_.SetData32(0x10010, 0x81);
+ process_memory_.SetData32(0x10014, 0x91);
+ process_memory_.SetData32(0x10018, 0xa1);
+ process_memory_.SetData32(0x1001c, 0xb1);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r4-r11}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r4-r11}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 32\n"
+ "4 unwind r4 = [cfa - 32]\n"
+ "4 unwind r5 = [cfa - 28]\n"
+ "4 unwind r6 = [cfa - 24]\n"
+ "4 unwind r7 = [cfa - 20]\n"
+ "4 unwind r8 = [cfa - 16]\n"
+ "4 unwind r9 = [cfa - 12]\n"
+ "4 unwind r10 = [cfa - 8]\n"
+ "4 unwind r11 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10034U, exidx_->cfa());
+ ASSERT_EQ(0x10020U, exidx_->cfa());
ASSERT_EQ(0x41U, (*exidx_->regs())[4]);
ASSERT_EQ(0x51U, (*exidx_->regs())[5]);
ASSERT_EQ(0x61U, (*exidx_->regs())[6]);
@@ -384,57 +554,100 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r4, r14}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r4, r14}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 8\n"
+ "4 unwind r4 = [cfa - 8]\n"
+ "4 unwind r14 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10008U, exidx_->cfa());
ASSERT_EQ(0x12U, (*exidx_->regs())[4]);
ASSERT_EQ(0x22U, (*exidx_->regs())[14]);
- ResetLogs();
+ ResetExidx();
data_->push_back(0xab);
- process_memory_.SetData32(0x10008, 0x1);
- process_memory_.SetData32(0x1000c, 0x2);
- process_memory_.SetData32(0x10010, 0x3);
- process_memory_.SetData32(0x10014, 0x4);
- process_memory_.SetData32(0x10018, 0x5);
+ process_memory_.SetData32(0x10000, 0x1);
+ process_memory_.SetData32(0x10004, 0x2);
+ process_memory_.SetData32(0x10008, 0x3);
+ process_memory_.SetData32(0x1000c, 0x4);
+ process_memory_.SetData32(0x10010, 0x5);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r4-r7, r14}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r4-r7, r14}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 20\n"
+ "4 unwind r4 = [cfa - 20]\n"
+ "4 unwind r5 = [cfa - 16]\n"
+ "4 unwind r6 = [cfa - 12]\n"
+ "4 unwind r7 = [cfa - 8]\n"
+ "4 unwind r14 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x1001cU, exidx_->cfa());
+ ASSERT_EQ(0x10014U, exidx_->cfa());
ASSERT_EQ(0x1U, (*exidx_->regs())[4]);
ASSERT_EQ(0x2U, (*exidx_->regs())[5]);
ASSERT_EQ(0x3U, (*exidx_->regs())[6]);
ASSERT_EQ(0x4U, (*exidx_->regs())[7]);
ASSERT_EQ(0x5U, (*exidx_->regs())[14]);
- ResetLogs();
+ ResetExidx();
data_->push_back(0xaf);
- process_memory_.SetData32(0x1001c, 0x1a);
- process_memory_.SetData32(0x10020, 0x2a);
- process_memory_.SetData32(0x10024, 0x3a);
- process_memory_.SetData32(0x10028, 0x4a);
- process_memory_.SetData32(0x1002c, 0x5a);
- process_memory_.SetData32(0x10030, 0x6a);
- process_memory_.SetData32(0x10034, 0x7a);
- process_memory_.SetData32(0x10038, 0x8a);
- process_memory_.SetData32(0x1003c, 0x9a);
+ process_memory_.SetData32(0x10000, 0x1a);
+ process_memory_.SetData32(0x10004, 0x2a);
+ process_memory_.SetData32(0x10008, 0x3a);
+ process_memory_.SetData32(0x1000c, 0x4a);
+ process_memory_.SetData32(0x10010, 0x5a);
+ process_memory_.SetData32(0x10014, 0x6a);
+ process_memory_.SetData32(0x10018, 0x7a);
+ process_memory_.SetData32(0x1001c, 0x8a);
+ process_memory_.SetData32(0x10020, 0x9a);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r4-r11, r14}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r4-r11, r14}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 36\n"
+ "4 unwind r4 = [cfa - 36]\n"
+ "4 unwind r5 = [cfa - 32]\n"
+ "4 unwind r6 = [cfa - 28]\n"
+ "4 unwind r7 = [cfa - 24]\n"
+ "4 unwind r8 = [cfa - 20]\n"
+ "4 unwind r9 = [cfa - 16]\n"
+ "4 unwind r10 = [cfa - 12]\n"
+ "4 unwind r11 = [cfa - 8]\n"
+ "4 unwind r14 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10040U, exidx_->cfa());
+ ASSERT_EQ(0x10024U, exidx_->cfa());
ASSERT_EQ(0x1aU, (*exidx_->regs())[4]);
ASSERT_EQ(0x2aU, (*exidx_->regs())[5]);
ASSERT_EQ(0x3aU, (*exidx_->regs())[6]);
@@ -451,10 +664,17 @@
data_->push_back(0xb0);
ASSERT_FALSE(exidx_->Decode());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind finish\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind finish\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10000U, exidx_->cfa());
ASSERT_EQ(ARM_STATUS_FINISH, exidx_->status());
@@ -466,10 +686,14 @@
data_->push_back(0x00);
ASSERT_FALSE(exidx_->Decode());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10000U, exidx_->cfa());
ASSERT_EQ(ARM_STATUS_SPARE, exidx_->status());
@@ -477,15 +701,19 @@
// 10110001 xxxxyyyy: Spare (xxxx != 0000)
for (size_t x = 1; x < 16; x++) {
for (size_t y = 0; y < 16; y++) {
- ResetLogs();
+ ResetExidx();
data_->push_back(0xb1);
data_->push_back((x << 4) | y);
ASSERT_FALSE(exidx_->Decode()) << "x, y = " << x << ", " << y;
ASSERT_EQ("", GetFakeLogBuf()) << "x, y = " << x << ", " << y;
- if (log_) {
- ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint()) << "x, y = " << x << ", " << y;
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint()) << "x, y = " << x << ", " << y;
+ break;
}
ASSERT_EQ(0x10000U, exidx_->cfa()) << "x, y = " << x << ", " << y;
ASSERT_EQ(ARM_STATUS_SPARE, exidx_->status());
@@ -494,29 +722,37 @@
// 101101nn: Spare
for (size_t n = 0; n < 4; n++) {
- ResetLogs();
+ ResetExidx();
data_->push_back(0xb4 | n);
ASSERT_FALSE(exidx_->Decode()) << "n = " << n;
ASSERT_EQ("", GetFakeLogBuf()) << "n = " << n;
- if (log_) {
- ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint()) << "n = " << n;
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint()) << "n = " << n;
+ break;
}
ASSERT_EQ(0x10000U, exidx_->cfa()) << "n = " << n;
ASSERT_EQ(ARM_STATUS_SPARE, exidx_->status());
}
// 11000111 00000000: Spare
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc7);
data_->push_back(0x00);
ASSERT_FALSE(exidx_->Decode());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10000U, exidx_->cfa());
ASSERT_EQ(ARM_STATUS_SPARE, exidx_->status());
@@ -524,15 +760,19 @@
// 11000111 xxxxyyyy: Spare (xxxx != 0000)
for (size_t x = 1; x < 16; x++) {
for (size_t y = 0; y < 16; y++) {
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc7);
data_->push_back(0x10);
ASSERT_FALSE(exidx_->Decode()) << "x, y = " << x << ", " << y;
ASSERT_EQ("", GetFakeLogBuf()) << "x, y = " << x << ", " << y;
- if (log_) {
- ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint()) << "x, y = " << x << ", " << y;
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint()) << "x, y = " << x << ", " << y;
+ break;
}
ASSERT_EQ(0x10000U, exidx_->cfa()) << "x, y = " << x << ", " << y;
ASSERT_EQ(ARM_STATUS_SPARE, exidx_->status());
@@ -541,14 +781,18 @@
// 11001yyy: Spare (yyy != 000, 001)
for (size_t y = 2; y < 8; y++) {
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc8 | y);
ASSERT_FALSE(exidx_->Decode()) << "y = " << y;
ASSERT_EQ("", GetFakeLogBuf()) << "y = " << y;
- if (log_) {
- ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint()) << "y = " << y;
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint()) << "y = " << y;
+ break;
}
ASSERT_EQ(0x10000U, exidx_->cfa()) << "y = " << y;
ASSERT_EQ(ARM_STATUS_SPARE, exidx_->status());
@@ -557,14 +801,18 @@
// 11xxxyyy: Spare (xxx != 000, 001, 010)
for (size_t x = 3; x < 8; x++) {
for (size_t y = 0; y < 8; y++) {
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc0 | (x << 3) | y);
ASSERT_FALSE(exidx_->Decode()) << "x, y = " << x << ", " << y;
ASSERT_EQ("", GetFakeLogBuf()) << "x, y = " << x << ", " << y;
- if (log_) {
- ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint()) << "x, y = " << x << ", " << y;
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Spare\n", GetFakeLogPrint()) << "x, y = " << x << ", " << y;
+ break;
}
ASSERT_EQ(0x10000U, exidx_->cfa()) << "x, y = " << x << ", " << y;
ASSERT_EQ(ARM_STATUS_SPARE, exidx_->status());
@@ -580,47 +828,81 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r0}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r0}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 4\n"
+ "4 unwind r0 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10004U, exidx_->cfa());
ASSERT_EQ(0x45U, (*exidx_->regs())[0]);
- ResetLogs();
+ ResetExidx();
data_->push_back(0xb1);
data_->push_back(0x0a);
- process_memory_.SetData32(0x10004, 0x23);
- process_memory_.SetData32(0x10008, 0x24);
+ process_memory_.SetData32(0x10000, 0x23);
+ process_memory_.SetData32(0x10004, 0x24);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r1, r3}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r1, r3}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 8\n"
+ "4 unwind r1 = [cfa - 8]\n"
+ "4 unwind r3 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x1000cU, exidx_->cfa());
+ ASSERT_EQ(0x10008U, exidx_->cfa());
ASSERT_EQ(0x23U, (*exidx_->regs())[1]);
ASSERT_EQ(0x24U, (*exidx_->regs())[3]);
- ResetLogs();
+ ResetExidx();
data_->push_back(0xb1);
data_->push_back(0x0f);
- process_memory_.SetData32(0x1000c, 0x65);
- process_memory_.SetData32(0x10010, 0x54);
- process_memory_.SetData32(0x10014, 0x43);
- process_memory_.SetData32(0x10018, 0x32);
+ process_memory_.SetData32(0x10000, 0x65);
+ process_memory_.SetData32(0x10004, 0x54);
+ process_memory_.SetData32(0x10008, 0x43);
+ process_memory_.SetData32(0x1000c, 0x32);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {r0, r1, r2, r3}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {r0, r1, r2, r3}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 16\n"
+ "4 unwind r0 = [cfa - 16]\n"
+ "4 unwind r1 = [cfa - 12]\n"
+ "4 unwind r2 = [cfa - 8]\n"
+ "4 unwind r3 = [cfa - 4]\n",
+ GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x1001cU, exidx_->cfa());
+ ASSERT_EQ(0x10010U, exidx_->cfa());
ASSERT_EQ(0x65U, (*exidx_->regs())[0]);
ASSERT_EQ(0x54U, (*exidx_->regs())[1]);
ASSERT_EQ(0x43U, (*exidx_->regs())[2]);
@@ -634,28 +916,42 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp + 1024\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = vsp + 1024\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13 + 1024\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10400U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xb2);
data_->push_back(0xff);
data_->push_back(0x02);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp + 2048\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = vsp + 2048\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13 + 2048\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10c00U, exidx_->cfa());
+ ASSERT_EQ(0x10800U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xb2);
data_->push_back(0xff);
data_->push_back(0x82);
@@ -663,12 +959,19 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp + 3147776\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind vsp = vsp + 3147776\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13 + 3147776\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x311400U, exidx_->cfa());
+ ASSERT_EQ(0x310800U, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, pop_vfp_fstmfdx) {
@@ -678,25 +981,37 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d0}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d0}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x1000cU, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xb3);
data_->push_back(0x48);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d4-d12}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d4-d12}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10058U, exidx_->cfa());
+ ASSERT_EQ(0x1004cU, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, pop_vfp8_fstmfdx) {
@@ -705,36 +1020,54 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d8}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d8}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x1000cU, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xbb);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d8-d11}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d8-d11}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10030U, exidx_->cfa());
+ ASSERT_EQ(0x10024U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xbf);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d8-d15}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d8-d15}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10074U, exidx_->cfa());
+ ASSERT_EQ(0x10044U, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, pop_mmx_wr10) {
@@ -743,36 +1076,54 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {wR10}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {wR10}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported wRX register display\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10008U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc2);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {wR10-wR12}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {wR10-wR12}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported wRX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10020U, exidx_->cfa());
+ ASSERT_EQ(0x10018U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc5);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {wR10-wR15}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {wR10-wR15}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported wRX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10050U, exidx_->cfa());
+ ASSERT_EQ(0x10030U, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, pop_mmx_wr) {
@@ -782,38 +1133,56 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {wR0}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {wR0}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported wRX register display\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10008U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc6);
data_->push_back(0x25);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {wR2-wR7}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {wR2-wR7}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported wRX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10038U, exidx_->cfa());
+ ASSERT_EQ(0x10030U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc6);
data_->push_back(0xff);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {wR15-wR30}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {wR15-wR30}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported wRX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x100b8U, exidx_->cfa());
+ ASSERT_EQ(0x10080U, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, pop_mmx_wcgr) {
@@ -823,38 +1192,56 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {wCGR0}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {wCGR0}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported wCGR register display\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10004U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc7);
data_->push_back(0x0a);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {wCGR1, wCGR3}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {wCGR1, wCGR3}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported wCGR register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x1000cU, exidx_->cfa());
+ ASSERT_EQ(0x10008U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc7);
data_->push_back(0x0f);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {wCGR0, wCGR1, wCGR2, wCGR3}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {wCGR0, wCGR1, wCGR2, wCGR3}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported wCGR register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x1001cU, exidx_->cfa());
+ ASSERT_EQ(0x10010U, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, pop_vfp16_vpush) {
@@ -864,38 +1251,56 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d16}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d16}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10008U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc8);
data_->push_back(0x14);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d17-d21}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d17-d21}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10030U, exidx_->cfa());
+ ASSERT_EQ(0x10028U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc8);
data_->push_back(0xff);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d31-d46}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d31-d46}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x100b0U, exidx_->cfa());
+ ASSERT_EQ(0x10080U, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, pop_vfp_vpush) {
@@ -905,38 +1310,56 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d0}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d0}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10008U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc9);
data_->push_back(0x23);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d2-d5}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d2-d5}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10028U, exidx_->cfa());
+ ASSERT_EQ(0x10020U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xc9);
data_->push_back(0xff);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d15-d30}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d15-d30}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x100a8U, exidx_->cfa());
+ ASSERT_EQ(0x10080U, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, pop_vfp8_vpush) {
@@ -945,36 +1368,54 @@
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d8}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d8}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10008U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xd2);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d8-d10}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d8-d10}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10020U, exidx_->cfa());
+ ASSERT_EQ(0x10018U, exidx_->cfa());
- ResetLogs();
+ ResetExidx();
data_->push_back(0xd7);
ASSERT_TRUE(exidx_->Decode());
ASSERT_FALSE(exidx_->pc_set());
ASSERT_EQ("", GetFakeLogBuf());
- if (log_) {
- ASSERT_EQ("4 unwind pop {d8-d15}\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ("4 unwind pop {d8-d15}\n", GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ ASSERT_EQ("4 unwind Unsupported DX register display\n", GetFakeLogPrint());
+ break;
}
- ASSERT_EQ(0x10060U, exidx_->cfa());
+ ASSERT_EQ(0x10040U, exidx_->cfa());
}
TEST_P(ArmExidxDecodeTest, expect_truncated) {
@@ -1047,32 +1488,147 @@
TEST_P(ArmExidxDecodeTest, eval_multiple_decodes) {
// vsp = vsp + 4
data_->push_back(0x00);
- // vsp = vsp + 8
+ // vsp = vsp + 12
data_->push_back(0x02);
// Finish
data_->push_back(0xb0);
ASSERT_TRUE(exidx_->Eval());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp + 4\n"
- "4 unwind vsp = vsp + 12\n"
- "4 unwind finish\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ(
+ "4 unwind vsp = vsp + 4\n"
+ "4 unwind vsp = vsp + 12\n"
+ "4 unwind finish\n",
+ GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ("4 unwind cfa = r13 + 16\n", GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10010U, exidx_->cfa());
ASSERT_FALSE(exidx_->pc_set());
}
+TEST_P(ArmExidxDecodeTest, eval_vsp_add_after_pop) {
+ // Pop {r15}
+ data_->push_back(0x88);
+ data_->push_back(0x00);
+ // vsp = vsp + 12
+ data_->push_back(0x02);
+ // Finish
+ data_->push_back(0xb0);
+ process_memory_.SetData32(0x10000, 0x10);
+
+ ASSERT_TRUE(exidx_->Eval());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ(
+ "4 unwind pop {r15}\n"
+ "4 unwind vsp = vsp + 12\n"
+ "4 unwind finish\n",
+ GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 16\n"
+ "4 unwind r15 = [cfa - 16]\n",
+ GetFakeLogPrint());
+ break;
+ }
+ ASSERT_EQ(0x10010U, exidx_->cfa());
+ ASSERT_TRUE(exidx_->pc_set());
+ ASSERT_EQ(0x10U, (*exidx_->regs())[15]);
+}
+
+TEST_P(ArmExidxDecodeTest, eval_vsp_add_large_after_pop) {
+ // Pop {r15}
+ data_->push_back(0x88);
+ data_->push_back(0x00);
+ // vsp = vsp + 1024
+ data_->push_back(0xb2);
+ data_->push_back(0x7f);
+ // Finish
+ data_->push_back(0xb0);
+ process_memory_.SetData32(0x10000, 0x10);
+
+ ASSERT_TRUE(exidx_->Eval());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ(
+ "4 unwind pop {r15}\n"
+ "4 unwind vsp = vsp + 1024\n"
+ "4 unwind finish\n",
+ GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 1028\n"
+ "4 unwind r15 = [cfa - 1028]\n",
+ GetFakeLogPrint());
+ break;
+ }
+ ASSERT_EQ(0x10404U, exidx_->cfa());
+ ASSERT_TRUE(exidx_->pc_set());
+ ASSERT_EQ(0x10U, (*exidx_->regs())[15]);
+}
+
+TEST_P(ArmExidxDecodeTest, eval_vsp_sub_after_pop) {
+ // Pop {r15}
+ data_->push_back(0x88);
+ data_->push_back(0x00);
+ // vsp = vsp - 4
+ data_->push_back(0x41);
+ // Finish
+ data_->push_back(0xb0);
+ process_memory_.SetData32(0x10000, 0x10);
+
+ ASSERT_TRUE(exidx_->Eval());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ(
+ "4 unwind pop {r15}\n"
+ "4 unwind vsp = vsp - 8\n"
+ "4 unwind finish\n",
+ GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 - 4\n"
+ "4 unwind r15 = [cfa + 4]\n",
+ GetFakeLogPrint());
+ break;
+ }
+ ASSERT_EQ(0xfffcU, exidx_->cfa());
+ ASSERT_TRUE(exidx_->pc_set());
+ ASSERT_EQ(0x10U, (*exidx_->regs())[15]);
+}
+
TEST_P(ArmExidxDecodeTest, eval_pc_set) {
// vsp = vsp + 4
data_->push_back(0x00);
- // vsp = vsp + 8
+ // vsp = vsp + 12
data_->push_back(0x02);
// Pop {r15}
data_->push_back(0x88);
data_->push_back(0x00);
- // vsp = vsp + 8
+ // vsp = vsp + 12
data_->push_back(0x02);
// Finish
data_->push_back(0xb0);
@@ -1080,20 +1636,33 @@
process_memory_.SetData32(0x10010, 0x10);
ASSERT_TRUE(exidx_->Eval());
- if (log_) {
- ASSERT_EQ("4 unwind vsp = vsp + 4\n"
- "4 unwind vsp = vsp + 12\n"
- "4 unwind pop {r15}\n"
- "4 unwind vsp = vsp + 12\n"
- "4 unwind finish\n", GetFakeLogPrint());
- } else {
- ASSERT_EQ("", GetFakeLogPrint());
+ switch (log_) {
+ case ARM_LOG_NONE:
+ ASSERT_EQ("", GetFakeLogPrint());
+ break;
+ case ARM_LOG_FULL:
+ ASSERT_EQ(
+ "4 unwind vsp = vsp + 4\n"
+ "4 unwind vsp = vsp + 12\n"
+ "4 unwind pop {r15}\n"
+ "4 unwind vsp = vsp + 12\n"
+ "4 unwind finish\n",
+ GetFakeLogPrint());
+ break;
+ case ARM_LOG_BY_REG:
+ exidx_->LogByReg();
+ ASSERT_EQ(
+ "4 unwind cfa = r13 + 32\n"
+ "4 unwind r15 = [cfa - 16]\n",
+ GetFakeLogPrint());
+ break;
}
ASSERT_EQ(0x10020U, exidx_->cfa());
ASSERT_TRUE(exidx_->pc_set());
ASSERT_EQ(0x10U, (*exidx_->regs())[15]);
}
-INSTANTIATE_TEST_CASE_P(, ArmExidxDecodeTest, ::testing::Values("logging", "no_logging"));
+INSTANTIATE_TEST_CASE_P(, ArmExidxDecodeTest,
+ ::testing::Values("logging", "register_logging", "no_logging"));
} // namespace unwindstack
diff --git a/libunwindstack/tests/ArmExidxExtractTest.cpp b/libunwindstack/tests/ArmExidxExtractTest.cpp
index 8d0f0e5..79c799c 100644
--- a/libunwindstack/tests/ArmExidxExtractTest.cpp
+++ b/libunwindstack/tests/ArmExidxExtractTest.cpp
@@ -301,7 +301,7 @@
elf_memory_.SetData32(0x1000, 0x7fff2340);
elf_memory_.SetData32(0x1004, 1);
- exidx_->set_log(true);
+ exidx_->set_log(ARM_LOG_FULL);
exidx_->set_log_indent(0);
exidx_->set_log_skip_execution(false);
@@ -316,7 +316,7 @@
elf_memory_.SetData32(0x4000, 0x7ffa3000);
elf_memory_.SetData32(0x4004, 0x80a8b0b0);
- exidx_->set_log(true);
+ exidx_->set_log(ARM_LOG_FULL);
exidx_->set_log_indent(0);
exidx_->set_log_skip_execution(false);
@@ -330,7 +330,7 @@
elf_memory_.SetData32(0x6234, 0x2);
elf_memory_.SetData32(0x6238, 0x00112233);
- exidx_->set_log(true);
+ exidx_->set_log(ARM_LOG_FULL);
exidx_->set_log_indent(0);
exidx_->set_log_skip_execution(false);
diff --git a/libunwindstack/tests/DexFileTest.cpp b/libunwindstack/tests/DexFileTest.cpp
index 0b02c5b..4dd8cb0 100644
--- a/libunwindstack/tests/DexFileTest.cpp
+++ b/libunwindstack/tests/DexFileTest.cpp
@@ -206,15 +206,42 @@
std::string method;
uint64_t method_offset;
- dex_file->GetMethodInformation(0x102, &method, &method_offset);
+ ASSERT_TRUE(dex_file->GetMethodInformation(0x102, &method, &method_offset));
EXPECT_EQ("Main.<init>", method);
EXPECT_EQ(2U, method_offset);
- method = "not_in_a_method";
- method_offset = 0x123;
- dex_file->GetMethodInformation(0x100000, &method, &method_offset);
- EXPECT_EQ("not_in_a_method", method);
- EXPECT_EQ(0x123U, method_offset);
+ ASSERT_TRUE(dex_file->GetMethodInformation(0x118, &method, &method_offset));
+ EXPECT_EQ("Main.main", method);
+ EXPECT_EQ(0U, method_offset);
+
+ // Make sure that any data that is cached is still retrievable.
+ ASSERT_TRUE(dex_file->GetMethodInformation(0x104, &method, &method_offset));
+ EXPECT_EQ("Main.<init>", method);
+ EXPECT_EQ(4U, method_offset);
+
+ ASSERT_TRUE(dex_file->GetMethodInformation(0x119, &method, &method_offset));
+ EXPECT_EQ("Main.main", method);
+ EXPECT_EQ(1U, method_offset);
+}
+
+TEST(DexFileTest, get_method_empty) {
+ MemoryFake memory;
+ memory.SetMemory(0x4000, kDexData, sizeof(kDexData));
+ MapInfo info(0x100, 0x10000, 0x200, 0x5, "");
+ std::unique_ptr<DexFile> dex_file(DexFile::Create(0x4000, &memory, &info));
+ ASSERT_TRUE(dex_file != nullptr);
+
+ std::string method;
+ uint64_t method_offset;
+ EXPECT_FALSE(dex_file->GetMethodInformation(0x100000, &method, &method_offset));
+
+ EXPECT_FALSE(dex_file->GetMethodInformation(0x98, &method, &method_offset));
+
+ // Make sure that once the whole dex file has been cached, no problems occur.
+ EXPECT_FALSE(dex_file->GetMethodInformation(0x98, &method, &method_offset));
+
+ // Choose a value that is in the cached map, but not in a valid method.
+ EXPECT_FALSE(dex_file->GetMethodInformation(0x110, &method, &method_offset));
}
} // namespace unwindstack
diff --git a/libunwindstack/tests/DwarfCfaLogTest.cpp b/libunwindstack/tests/DwarfCfaLogTest.cpp
index b17ca33..bb2e8f0 100644
--- a/libunwindstack/tests/DwarfCfaLogTest.cpp
+++ b/libunwindstack/tests/DwarfCfaLogTest.cpp
@@ -79,7 +79,7 @@
this->memory_.SetMemory(0x2000, std::vector<uint8_t>{i});
ResetLogs();
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2000, 0x2001));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
std::string expected = "4 unwind Illegal\n";
expected += android::base::StringPrintf("4 unwind Raw Data: 0x%02x\n", i);
ASSERT_EQ(expected, GetFakeLogPrint());
@@ -90,7 +90,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_nop) {
this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x00});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2000, 0x2001));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
std::string expected =
"4 unwind DW_CFA_nop\n"
"4 unwind Raw Data: 0x00\n";
@@ -101,7 +101,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_offset) {
this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x83, 0x04});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2000, 0x2002));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2002));
std::string expected =
"4 unwind DW_CFA_offset register(3) 4\n"
"4 unwind Raw Data: 0x83 0x04\n";
@@ -111,7 +111,7 @@
ResetLogs();
this->memory_.SetMemory(0x2100, std::vector<uint8_t>{0x83, 0x84, 0x01});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2100, 0x2103));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2100, 0x2103));
expected =
"4 unwind DW_CFA_offset register(3) 132\n"
"4 unwind Raw Data: 0x83 0x84 0x01\n";
@@ -122,7 +122,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_offset_extended) {
this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x05, 0x03, 0x02});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x500, 0x503));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x503));
std::string expected =
"4 unwind DW_CFA_offset_extended register(3) 2\n"
"4 unwind Raw Data: 0x05 0x03 0x02\n";
@@ -132,7 +132,7 @@
ResetLogs();
this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x05, 0x81, 0x01, 0x82, 0x12});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x1500, 0x1505));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1500, 0x1505));
expected =
"4 unwind DW_CFA_offset_extended register(129) 2306\n"
"4 unwind Raw Data: 0x05 0x81 0x01 0x82 0x12\n";
@@ -143,7 +143,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_offset_extended_sf) {
this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x11, 0x05, 0x10});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x500, 0x503));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x503));
std::string expected =
"4 unwind DW_CFA_offset_extended_sf register(5) 16\n"
"4 unwind Raw Data: 0x11 0x05 0x10\n";
@@ -154,7 +154,7 @@
ResetLogs();
this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x11, 0x86, 0x01, 0xff, 0x7f});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x1500, 0x1505));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1500, 0x1505));
expected =
"4 unwind DW_CFA_offset_extended_sf register(134) -1\n"
"4 unwind Raw Data: 0x11 0x86 0x01 0xff 0x7f\n";
@@ -165,7 +165,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_restore) {
this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0xc2});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2000, 0x2001));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
std::string expected =
"4 unwind DW_CFA_restore register(2)\n"
"4 unwind Raw Data: 0xc2\n";
@@ -175,7 +175,7 @@
ResetLogs();
this->memory_.SetMemory(0x3000, std::vector<uint8_t>{0x82, 0x04, 0xc2});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x3000, 0x3003));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x3000, 0x3003));
expected =
"4 unwind DW_CFA_offset register(2) 4\n"
"4 unwind Raw Data: 0x82 0x04\n"
@@ -188,7 +188,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_restore_extended) {
this->memory_.SetMemory(0x4000, std::vector<uint8_t>{0x06, 0x08});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x4000, 0x4002));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4000, 0x4002));
std::string expected =
"4 unwind DW_CFA_restore_extended register(8)\n"
"4 unwind Raw Data: 0x06 0x08\n";
@@ -198,7 +198,7 @@
ResetLogs();
this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x05, 0x82, 0x02, 0x04, 0x06, 0x82, 0x02});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x5000, 0x5007));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x5000, 0x5007));
expected =
"4 unwind DW_CFA_offset_extended register(258) 4\n"
"4 unwind Raw Data: 0x05 0x82 0x02 0x04\n"
@@ -228,7 +228,7 @@
this->memory_.SetMemory(0x50, buffer, sizeof(buffer));
ResetLogs();
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x50, 0x51 + sizeof(TypeParam)));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x50, 0x51 + sizeof(TypeParam)));
std::string expected = "4 unwind DW_CFA_set_loc " + address_str + "\n";
expected += "4 unwind " + raw_data + "\n";
expected += "4 unwind \n";
@@ -240,7 +240,7 @@
ResetLogs();
this->fde_.pc_start = address + 0x10;
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x50, 0x51 + sizeof(TypeParam)));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x50, 0x51 + sizeof(TypeParam)));
expected = "4 unwind DW_CFA_set_loc " + address_str + "\n";
expected += "4 unwind " + raw_data + "\n";
expected += "4 unwind \n";
@@ -252,7 +252,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc) {
this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x44});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x201));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x201));
std::string expected =
"4 unwind DW_CFA_advance_loc 4\n"
"4 unwind Raw Data: 0x44\n"
@@ -260,22 +260,12 @@
"4 unwind PC 0x2010\n";
ASSERT_EQ(expected, GetFakeLogPrint());
ASSERT_EQ("", GetFakeLogBuf());
-
- ResetLogs();
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x200, 0x201));
- expected =
- "4 unwind DW_CFA_advance_loc 4\n"
- "4 unwind Raw Data: 0x44\n"
- "4 unwind \n"
- "4 unwind PC 0x2110\n";
- ASSERT_EQ(expected, GetFakeLogPrint());
- ASSERT_EQ("", GetFakeLogBuf());
}
TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc1) {
this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x02, 0x04});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x202));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x202));
std::string expected =
"4 unwind DW_CFA_advance_loc1 4\n"
"4 unwind Raw Data: 0x02 0x04\n"
@@ -283,22 +273,12 @@
"4 unwind PC 0x2004\n";
ASSERT_EQ(expected, GetFakeLogPrint());
ASSERT_EQ("", GetFakeLogBuf());
-
- ResetLogs();
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x10, 0x200, 0x202));
- expected =
- "4 unwind DW_CFA_advance_loc1 4\n"
- "4 unwind Raw Data: 0x02 0x04\n"
- "4 unwind \n"
- "4 unwind PC 0x2014\n";
- ASSERT_EQ(expected, GetFakeLogPrint());
- ASSERT_EQ("", GetFakeLogBuf());
}
TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc2) {
this->memory_.SetMemory(0x600, std::vector<uint8_t>{0x03, 0x04, 0x03});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x600, 0x603));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x600, 0x603));
std::string expected =
"4 unwind DW_CFA_advance_loc2 772\n"
"4 unwind Raw Data: 0x03 0x04 0x03\n"
@@ -306,22 +286,12 @@
"4 unwind PC 0x2304\n";
ASSERT_EQ(expected, GetFakeLogPrint());
ASSERT_EQ("", GetFakeLogBuf());
-
- ResetLogs();
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1000, 0x600, 0x603));
- expected =
- "4 unwind DW_CFA_advance_loc2 772\n"
- "4 unwind Raw Data: 0x03 0x04 0x03\n"
- "4 unwind \n"
- "4 unwind PC 0x3304\n";
- ASSERT_EQ(expected, GetFakeLogPrint());
- ASSERT_EQ("", GetFakeLogBuf());
}
TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc4) {
this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x04, 0x04, 0x03, 0x02, 0x01});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x500, 0x505));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x505));
std::string expected =
"4 unwind DW_CFA_advance_loc4 16909060\n"
"4 unwind Raw Data: 0x04 0x04 0x03 0x02 0x01\n"
@@ -329,22 +299,12 @@
"4 unwind PC 0x1022304\n";
ASSERT_EQ(expected, GetFakeLogPrint());
ASSERT_EQ("", GetFakeLogBuf());
-
- ResetLogs();
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x500, 0x505));
- expected =
- "4 unwind DW_CFA_advance_loc4 16909060\n"
- "4 unwind Raw Data: 0x04 0x04 0x03 0x02 0x01\n"
- "4 unwind \n"
- "4 unwind PC 0x1024304\n";
- ASSERT_EQ(expected, GetFakeLogPrint());
- ASSERT_EQ("", GetFakeLogBuf());
}
TYPED_TEST_P(DwarfCfaLogTest, cfa_undefined) {
this->memory_.SetMemory(0xa00, std::vector<uint8_t>{0x07, 0x09});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0xa00, 0xa02));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xa02));
std::string expected =
"4 unwind DW_CFA_undefined register(9)\n"
"4 unwind Raw Data: 0x07 0x09\n";
@@ -355,7 +315,7 @@
dwarf_loc_regs_t cie_loc_regs;
this->memory_.SetMemory(0x1a00, std::vector<uint8_t>{0x07, 0x81, 0x01});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x1a00, 0x1a03));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1a00, 0x1a03));
expected =
"4 unwind DW_CFA_undefined register(129)\n"
"4 unwind Raw Data: 0x07 0x81 0x01\n";
@@ -366,7 +326,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_same) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x08, 0x7f});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
std::string expected =
"4 unwind DW_CFA_same_value register(127)\n"
"4 unwind Raw Data: 0x08 0x7f\n";
@@ -376,7 +336,7 @@
ResetLogs();
this->memory_.SetMemory(0x2100, std::vector<uint8_t>{0x08, 0xff, 0x01});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2100, 0x2103));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2100, 0x2103));
expected =
"4 unwind DW_CFA_same_value register(255)\n"
"4 unwind Raw Data: 0x08 0xff 0x01\n";
@@ -387,7 +347,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_register) {
this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x09, 0x02, 0x01});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x300, 0x303));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x303));
std::string expected =
"4 unwind DW_CFA_register register(2) register(1)\n"
"4 unwind Raw Data: 0x09 0x02 0x01\n";
@@ -397,7 +357,7 @@
ResetLogs();
this->memory_.SetMemory(0x4300, std::vector<uint8_t>{0x09, 0xff, 0x01, 0xff, 0x03});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x4300, 0x4305));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4300, 0x4305));
expected =
"4 unwind DW_CFA_register register(255) register(511)\n"
"4 unwind Raw Data: 0x09 0xff 0x01 0xff 0x03\n";
@@ -408,7 +368,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_state) {
this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x0a});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x300, 0x301));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x301));
std::string expected =
"4 unwind DW_CFA_remember_state\n"
@@ -419,7 +379,7 @@
ResetLogs();
this->memory_.SetMemory(0x4300, std::vector<uint8_t>{0x0b});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x4300, 0x4301));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4300, 0x4301));
expected =
"4 unwind DW_CFA_restore_state\n"
@@ -431,7 +391,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_state_cfa_offset_restore) {
this->memory_.SetMemory(0x3000, std::vector<uint8_t>{0x0a, 0x0e, 0x40, 0x0b});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x3000, 0x3004));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x3000, 0x3004));
std::string expected =
"4 unwind DW_CFA_remember_state\n"
@@ -447,7 +407,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0c, 0x7f, 0x74});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x103));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
std::string expected =
"4 unwind DW_CFA_def_cfa register(127) 116\n"
@@ -458,7 +418,7 @@
ResetLogs();
this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0c, 0xff, 0x02, 0xf4, 0x04});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x205));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x205));
expected =
"4 unwind DW_CFA_def_cfa register(383) 628\n"
@@ -470,7 +430,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_sf) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x12, 0x30, 0x25});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x103));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
std::string expected =
"4 unwind DW_CFA_def_cfa_sf register(48) 37\n"
@@ -482,7 +442,7 @@
ResetLogs();
this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x12, 0xa3, 0x01, 0xfa, 0x7f});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x205));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x205));
expected =
"4 unwind DW_CFA_def_cfa_sf register(163) -6\n"
@@ -494,7 +454,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_register) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0d, 0x72});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
std::string expected =
"4 unwind DW_CFA_def_cfa_register register(114)\n"
@@ -505,7 +465,7 @@
ResetLogs();
this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0d, 0xf9, 0x20});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x203));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
expected =
"4 unwind DW_CFA_def_cfa_register register(4217)\n"
@@ -517,7 +477,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_offset) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0e, 0x59});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
std::string expected =
"4 unwind DW_CFA_def_cfa_offset 89\n"
@@ -526,7 +486,7 @@
ASSERT_EQ("", GetFakeLogBuf());
ResetLogs();
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
expected =
"4 unwind DW_CFA_def_cfa_offset 89\n"
@@ -537,7 +497,7 @@
ResetLogs();
this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0e, 0xd4, 0x0a});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x203));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
expected =
"4 unwind DW_CFA_def_cfa_offset 1364\n"
@@ -549,7 +509,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_offset_sf) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x13, 0x23});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
std::string expected =
"4 unwind DW_CFA_def_cfa_offset_sf 35\n"
@@ -558,7 +518,7 @@
ASSERT_EQ("", GetFakeLogBuf());
ResetLogs();
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
expected =
"4 unwind DW_CFA_def_cfa_offset_sf 35\n"
@@ -570,7 +530,7 @@
ResetLogs();
this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x13, 0xf6, 0x7f});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x203));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
expected =
"4 unwind DW_CFA_def_cfa_offset_sf -10\n"
@@ -582,7 +542,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_expression) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0f, 0x04, 0x01, 0x02, 0x04, 0x05});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x106));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x106));
std::string expected =
"4 unwind DW_CFA_def_cfa_expression 4\n"
@@ -614,7 +574,7 @@
}
expected += '\n';
this->memory_.SetMemory(0x200, ops);
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x284));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x284));
expected = "4 unwind DW_CFA_def_cfa_expression 129\n" + expected;
ASSERT_EQ(expected + op_string, GetFakeLogPrint());
@@ -624,7 +584,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_expression) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x10, 0x04, 0x02, 0xc0, 0xc1});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x105));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x105));
std::string expected =
"4 unwind DW_CFA_expression register(4) 2\n"
@@ -652,7 +612,7 @@
expected = "4 unwind DW_CFA_expression register(255) 130\n" + expected + "\n";
this->memory_.SetMemory(0x200, ops);
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x287));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x287));
ASSERT_EQ(expected + op_string, GetFakeLogPrint());
ASSERT_EQ("", GetFakeLogBuf());
@@ -661,7 +621,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_val_offset) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x14, 0x45, 0x54});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x103));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
std::string expected =
"4 unwind DW_CFA_val_offset register(69) 84\n"
@@ -672,7 +632,7 @@
ResetLogs();
this->memory_.SetMemory(0x400, std::vector<uint8_t>{0x14, 0xa2, 0x02, 0xb4, 0x05});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x400, 0x405));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x400, 0x405));
expected =
"4 unwind DW_CFA_val_offset register(290) 692\n"
@@ -684,7 +644,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_val_offset_sf) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x15, 0x56, 0x12});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x103));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
std::string expected =
"4 unwind DW_CFA_val_offset_sf register(86) 18\n"
@@ -696,7 +656,7 @@
ResetLogs();
this->memory_.SetMemory(0xa00, std::vector<uint8_t>{0x15, 0xff, 0x01, 0xc0, 0x7f});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0xa00, 0xa05));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xa05));
expected =
"4 unwind DW_CFA_val_offset_sf register(255) -64\n"
@@ -708,7 +668,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_val_expression) {
this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x16, 0x05, 0x02, 0xb0, 0xb1});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x105));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x105));
std::string expected =
"4 unwind DW_CFA_val_expression register(5) 2\n"
@@ -737,7 +697,7 @@
this->memory_.SetMemory(0xa00, ops);
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0xa00, 0xaad));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xaad));
ASSERT_EQ(expected + op_string, GetFakeLogPrint());
ASSERT_EQ("", GetFakeLogBuf());
@@ -746,7 +706,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_gnu_args_size) {
this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x2e, 0x04});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2000, 0x2002));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2002));
std::string expected =
"4 unwind DW_CFA_GNU_args_size 4\n"
@@ -757,7 +717,7 @@
ResetLogs();
this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x2e, 0xa4, 0x80, 0x04});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x5000, 0x5004));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x5000, 0x5004));
expected =
"4 unwind DW_CFA_GNU_args_size 65572\n"
@@ -769,7 +729,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_gnu_negative_offset_extended) {
this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x2f, 0x08, 0x10});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x500, 0x503));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x503));
std::string expected =
"4 unwind DW_CFA_GNU_negative_offset_extended register(8) 16\n"
@@ -780,7 +740,7 @@
ResetLogs();
this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x2f, 0x81, 0x02, 0xff, 0x01});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x1500, 0x1505));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1500, 0x1505));
expected =
"4 unwind DW_CFA_GNU_negative_offset_extended register(257) 255\n"
@@ -792,7 +752,7 @@
TYPED_TEST_P(DwarfCfaLogTest, cfa_register_override) {
this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x09, 0x02, 0x01, 0x09, 0x02, 0x04});
- ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x300, 0x306));
+ ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x306));
std::string expected =
"4 unwind DW_CFA_register register(2) register(1)\n"
diff --git a/libunwindstack/tests/DwarfCfaTest.cpp b/libunwindstack/tests/DwarfCfaTest.cpp
index 68dc30c..7395b04 100644
--- a/libunwindstack/tests/DwarfCfaTest.cpp
+++ b/libunwindstack/tests/DwarfCfaTest.cpp
@@ -737,6 +737,8 @@
ASSERT_TRUE(this->cfa_->GetLocationInfo(this->fde_.pc_start, 0x200, 0x284, &loc_regs));
ASSERT_EQ(0x284U, this->dmem_->cur_offset());
ASSERT_EQ(1U, loc_regs.size());
+ ASSERT_EQ(DWARF_LOCATION_VAL_EXPRESSION, loc_regs[CFA_REG].type);
+ ASSERT_EQ(0x81U, loc_regs[CFA_REG].values[0]);
ASSERT_EQ("", GetFakeLogPrint());
ASSERT_EQ("", GetFakeLogBuf());
diff --git a/libunwindstack/tests/DwarfDebugFrameTest.cpp b/libunwindstack/tests/DwarfDebugFrameTest.cpp
index c28a41e..d620934 100644
--- a/libunwindstack/tests/DwarfDebugFrameTest.cpp
+++ b/libunwindstack/tests/DwarfDebugFrameTest.cpp
@@ -16,7 +16,8 @@
#include <stdint.h>
-#include <gmock/gmock.h>
+#include <vector>
+
#include <gtest/gtest.h>
#include <unwindstack/DwarfError.h>
@@ -30,442 +31,377 @@
namespace unwindstack {
template <typename TypeParam>
-class MockDwarfDebugFrame : public DwarfDebugFrame<TypeParam> {
- public:
- MockDwarfDebugFrame(Memory* memory) : DwarfDebugFrame<TypeParam>(memory) {}
- ~MockDwarfDebugFrame() = default;
-
- void TestSetFdeCount(uint64_t count) { this->fde_count_ = count; }
- void TestSetOffset(uint64_t offset) { this->entries_offset_ = offset; }
- void TestSetEndOffset(uint64_t offset) { this->entries_end_ = offset; }
- void TestPushFdeInfo(const typename DwarfDebugFrame<TypeParam>::FdeInfo& info) {
- this->fdes_.push_back(info);
- }
-
- uint64_t TestGetFdeCount() { return this->fde_count_; }
- uint8_t TestGetOffset() { return this->offset_; }
- uint8_t TestGetEndOffset() { return this->end_offset_; }
- void TestGetFdeInfo(size_t index, typename DwarfDebugFrame<TypeParam>::FdeInfo* info) {
- *info = this->fdes_[index];
- }
-};
-
-template <typename TypeParam>
class DwarfDebugFrameTest : public ::testing::Test {
protected:
void SetUp() override {
memory_.Clear();
- debug_frame_ = new MockDwarfDebugFrame<TypeParam>(&memory_);
+ debug_frame_ = new DwarfDebugFrame<TypeParam>(&memory_);
ResetLogs();
}
void TearDown() override { delete debug_frame_; }
MemoryFake memory_;
- MockDwarfDebugFrame<TypeParam>* debug_frame_ = nullptr;
+ DwarfDebugFrame<TypeParam>* debug_frame_ = nullptr;
};
TYPED_TEST_CASE_P(DwarfDebugFrameTest);
// NOTE: All test class variables need to be referenced as this->.
-TYPED_TEST_P(DwarfDebugFrameTest, Init32) {
- // CIE 32 information.
- this->memory_.SetData32(0x5000, 0xfc);
- this->memory_.SetData32(0x5004, 0xffffffff);
- this->memory_.SetData8(0x5008, 1);
- this->memory_.SetData8(0x5009, '\0');
-
- // FDE 32 information.
- this->memory_.SetData32(0x5100, 0xfc);
- this->memory_.SetData32(0x5104, 0);
- this->memory_.SetData32(0x5108, 0x1500);
- this->memory_.SetData32(0x510c, 0x200);
-
- this->memory_.SetData32(0x5200, 0xfc);
- this->memory_.SetData32(0x5204, 0);
- this->memory_.SetData32(0x5208, 0x2500);
- this->memory_.SetData32(0x520c, 0x300);
-
- // CIE 32 information.
- this->memory_.SetData32(0x5300, 0xfc);
- this->memory_.SetData32(0x5304, 0xffffffff);
- this->memory_.SetData8(0x5308, 1);
- this->memory_.SetData8(0x5309, '\0');
-
- // FDE 32 information.
- this->memory_.SetData32(0x5400, 0xfc);
- this->memory_.SetData32(0x5404, 0x300);
- this->memory_.SetData32(0x5408, 0x3500);
- this->memory_.SetData32(0x540c, 0x400);
-
- this->memory_.SetData32(0x5500, 0xfc);
- this->memory_.SetData32(0x5504, 0x300);
- this->memory_.SetData32(0x5508, 0x4500);
- this->memory_.SetData32(0x550c, 0x500);
-
- ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600));
- ASSERT_EQ(4U, this->debug_frame_->TestGetFdeCount());
-
- typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
-
- this->debug_frame_->TestGetFdeInfo(0, &info);
- EXPECT_EQ(0x5100U, info.offset);
- EXPECT_EQ(0x1500U, info.start);
- EXPECT_EQ(0x1700U, info.end);
-
- this->debug_frame_->TestGetFdeInfo(1, &info);
- EXPECT_EQ(0x5200U, info.offset);
- EXPECT_EQ(0x2500U, info.start);
- EXPECT_EQ(0x2800U, info.end);
-
- this->debug_frame_->TestGetFdeInfo(2, &info);
- EXPECT_EQ(0x5400U, info.offset);
- EXPECT_EQ(0x3500U, info.start);
- EXPECT_EQ(0x3900U, info.end);
-
- this->debug_frame_->TestGetFdeInfo(3, &info);
- EXPECT_EQ(0x5500U, info.offset);
- EXPECT_EQ(0x4500U, info.start);
- EXPECT_EQ(0x4a00U, info.end);
+static void SetCie32(MemoryFake* memory, uint64_t offset, uint32_t length,
+ std::vector<uint8_t> data) {
+ memory->SetData32(offset, length);
+ offset += 4;
+ // Indicates this is a cie.
+ memory->SetData32(offset, 0xffffffff);
+ offset += 4;
+ memory->SetMemory(offset, data);
}
-TYPED_TEST_P(DwarfDebugFrameTest, Init32_fde_not_following_cie) {
- // CIE 32 information.
- this->memory_.SetData32(0x5000, 0xfc);
- this->memory_.SetData32(0x5004, 0xffffffff);
- this->memory_.SetData8(0x5008, 1);
- this->memory_.SetData8(0x5009, '\0');
-
- // FDE 32 information.
- this->memory_.SetData32(0x5100, 0xfc);
- this->memory_.SetData32(0x5104, 0x1000);
- this->memory_.SetData32(0x5108, 0x1500);
- this->memory_.SetData32(0x510c, 0x200);
-
- ASSERT_FALSE(this->debug_frame_->Init(0x5000, 0x600));
- ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->debug_frame_->LastErrorCode());
+static void SetCie64(MemoryFake* memory, uint64_t offset, uint64_t length,
+ std::vector<uint8_t> data) {
+ memory->SetData32(offset, 0xffffffff);
+ offset += 4;
+ memory->SetData64(offset, length);
+ offset += 8;
+ // Indicates this is a cie.
+ memory->SetData64(offset, 0xffffffffffffffffUL);
+ offset += 8;
+ memory->SetMemory(offset, data);
}
-TYPED_TEST_P(DwarfDebugFrameTest, Init32_do_not_fail_on_bad_next_entry) {
- // CIE 32 information.
- this->memory_.SetData32(0x5000, 0xfc);
- this->memory_.SetData32(0x5004, 0xffffffff);
- this->memory_.SetData8(0x5008, 1);
- this->memory_.SetData8(0x5009, '\0');
-
- // FDE 32 information.
- this->memory_.SetData32(0x5100, 0xfc);
- this->memory_.SetData32(0x5104, 0);
- this->memory_.SetData32(0x5108, 0x1500);
- this->memory_.SetData32(0x510c, 0x200);
-
- this->memory_.SetData32(0x5200, 0xfc);
- this->memory_.SetData32(0x5204, 0);
- this->memory_.SetData32(0x5208, 0x2500);
- this->memory_.SetData32(0x520c, 0x300);
-
- // CIE 32 information.
- this->memory_.SetData32(0x5300, 0);
- this->memory_.SetData32(0x5304, 0xffffffff);
- this->memory_.SetData8(0x5308, 1);
- this->memory_.SetData8(0x5309, '\0');
-
- // FDE 32 information.
- this->memory_.SetData32(0x5400, 0xfc);
- this->memory_.SetData32(0x5404, 0x300);
- this->memory_.SetData32(0x5408, 0x3500);
- this->memory_.SetData32(0x540c, 0x400);
-
- this->memory_.SetData32(0x5500, 0xfc);
- this->memory_.SetData32(0x5504, 0x300);
- this->memory_.SetData32(0x5508, 0x4500);
- this->memory_.SetData32(0x550c, 0x500);
-
- ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600));
- ASSERT_EQ(2U, this->debug_frame_->TestGetFdeCount());
-}
-
-TYPED_TEST_P(DwarfDebugFrameTest, Init64) {
- // CIE 64 information.
- this->memory_.SetData32(0x5000, 0xffffffff);
- this->memory_.SetData64(0x5004, 0xf4);
- this->memory_.SetData64(0x500c, 0xffffffffffffffffULL);
- this->memory_.SetData8(0x5014, 1);
- this->memory_.SetData8(0x5015, '\0');
-
- // FDE 64 information.
- this->memory_.SetData32(0x5100, 0xffffffff);
- this->memory_.SetData64(0x5104, 0xf4);
- this->memory_.SetData64(0x510c, 0);
- this->memory_.SetData64(0x5114, 0x1500);
- this->memory_.SetData64(0x511c, 0x200);
-
- this->memory_.SetData32(0x5200, 0xffffffff);
- this->memory_.SetData64(0x5204, 0xf4);
- this->memory_.SetData64(0x520c, 0);
- this->memory_.SetData64(0x5214, 0x2500);
- this->memory_.SetData64(0x521c, 0x300);
-
- // CIE 64 information.
- this->memory_.SetData32(0x5300, 0xffffffff);
- this->memory_.SetData64(0x5304, 0xf4);
- this->memory_.SetData64(0x530c, 0xffffffffffffffffULL);
- this->memory_.SetData8(0x5314, 1);
- this->memory_.SetData8(0x5315, '\0');
-
- // FDE 64 information.
- this->memory_.SetData32(0x5400, 0xffffffff);
- this->memory_.SetData64(0x5404, 0xf4);
- this->memory_.SetData64(0x540c, 0x300);
- this->memory_.SetData64(0x5414, 0x3500);
- this->memory_.SetData64(0x541c, 0x400);
-
- this->memory_.SetData32(0x5500, 0xffffffff);
- this->memory_.SetData64(0x5504, 0xf4);
- this->memory_.SetData64(0x550c, 0x300);
- this->memory_.SetData64(0x5514, 0x4500);
- this->memory_.SetData64(0x551c, 0x500);
-
- ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600));
- ASSERT_EQ(4U, this->debug_frame_->TestGetFdeCount());
-
- typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
-
- this->debug_frame_->TestGetFdeInfo(0, &info);
- EXPECT_EQ(0x5100U, info.offset);
- EXPECT_EQ(0x1500U, info.start);
- EXPECT_EQ(0x1700U, info.end);
-
- this->debug_frame_->TestGetFdeInfo(1, &info);
- EXPECT_EQ(0x5200U, info.offset);
- EXPECT_EQ(0x2500U, info.start);
- EXPECT_EQ(0x2800U, info.end);
-
- this->debug_frame_->TestGetFdeInfo(2, &info);
- EXPECT_EQ(0x5400U, info.offset);
- EXPECT_EQ(0x3500U, info.start);
- EXPECT_EQ(0x3900U, info.end);
-
- this->debug_frame_->TestGetFdeInfo(3, &info);
- EXPECT_EQ(0x5500U, info.offset);
- EXPECT_EQ(0x4500U, info.start);
- EXPECT_EQ(0x4a00U, info.end);
-}
-
-TYPED_TEST_P(DwarfDebugFrameTest, Init64_fde_not_following_cie) {
- // CIE 64 information.
- this->memory_.SetData32(0x5000, 0xffffffff);
- this->memory_.SetData64(0x5004, 0xf4);
- this->memory_.SetData64(0x500c, 0xffffffffffffffffULL);
- this->memory_.SetData8(0x5014, 1);
- this->memory_.SetData8(0x5015, '\0');
-
- // FDE 64 information.
- this->memory_.SetData32(0x5100, 0xffffffff);
- this->memory_.SetData64(0x5104, 0xf4);
- this->memory_.SetData64(0x510c, 0x1000);
- this->memory_.SetData64(0x5114, 0x1500);
- this->memory_.SetData64(0x511c, 0x200);
-
- ASSERT_FALSE(this->debug_frame_->Init(0x5000, 0x600));
- ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->debug_frame_->LastErrorCode());
-}
-
-TYPED_TEST_P(DwarfDebugFrameTest, Init64_do_not_fail_on_bad_next_entry) {
- // CIE 64 information.
- this->memory_.SetData32(0x5000, 0xffffffff);
- this->memory_.SetData64(0x5004, 0xf4);
- this->memory_.SetData64(0x500c, 0xffffffffffffffffULL);
- this->memory_.SetData8(0x5014, 1);
- this->memory_.SetData8(0x5015, '\0');
-
- // FDE 64 information.
- this->memory_.SetData32(0x5100, 0xffffffff);
- this->memory_.SetData64(0x5104, 0xf4);
- this->memory_.SetData64(0x510c, 0);
- this->memory_.SetData64(0x5114, 0x1500);
- this->memory_.SetData64(0x511c, 0x200);
-
- this->memory_.SetData32(0x5200, 0xffffffff);
- this->memory_.SetData64(0x5204, 0xf4);
- this->memory_.SetData64(0x520c, 0);
- this->memory_.SetData64(0x5214, 0x2500);
- this->memory_.SetData64(0x521c, 0x300);
-
- // CIE 64 information.
- this->memory_.SetData32(0x5300, 0xffffffff);
- this->memory_.SetData64(0x5304, 0);
- this->memory_.SetData64(0x530c, 0xffffffffffffffffULL);
- this->memory_.SetData8(0x5314, 1);
- this->memory_.SetData8(0x5315, '\0');
-
- // FDE 64 information.
- this->memory_.SetData32(0x5400, 0xffffffff);
- this->memory_.SetData64(0x5404, 0xf4);
- this->memory_.SetData64(0x540c, 0x300);
- this->memory_.SetData64(0x5414, 0x3500);
- this->memory_.SetData64(0x541c, 0x400);
-
- this->memory_.SetData32(0x5500, 0xffffffff);
- this->memory_.SetData64(0x5504, 0xf4);
- this->memory_.SetData64(0x550c, 0x300);
- this->memory_.SetData64(0x5514, 0x4500);
- this->memory_.SetData64(0x551c, 0x500);
-
- ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600));
- ASSERT_EQ(2U, this->debug_frame_->TestGetFdeCount());
-}
-
-TYPED_TEST_P(DwarfDebugFrameTest, Init_version1) {
- // CIE 32 information.
- this->memory_.SetData32(0x5000, 0xfc);
- this->memory_.SetData32(0x5004, 0xffffffff);
- this->memory_.SetData8(0x5008, 1);
- // Augment string.
- this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'R', 'P', 'L', '\0'});
- // Code alignment factor.
- this->memory_.SetMemory(0x500e, std::vector<uint8_t>{0x80, 0x00});
- // Data alignment factor.
- this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00});
- // Return address register
- this->memory_.SetData8(0x5014, 0x84);
- // Augmentation length
- this->memory_.SetMemory(0x5015, std::vector<uint8_t>{0x84, 0x00});
- // R data.
- this->memory_.SetData8(0x5017, DW_EH_PE_pcrel | DW_EH_PE_udata2);
-
- // FDE 32 information.
- this->memory_.SetData32(0x5100, 0xfc);
- this->memory_.SetData32(0x5104, 0);
- this->memory_.SetData16(0x5108, 0x1500);
- this->memory_.SetData16(0x510a, 0x200);
-
- ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x200));
- ASSERT_EQ(1U, this->debug_frame_->TestGetFdeCount());
-
- typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
- this->debug_frame_->TestGetFdeInfo(0, &info);
- EXPECT_EQ(0x5100U, info.offset);
- EXPECT_EQ(0x1500U, info.start);
- EXPECT_EQ(0x1700U, info.end);
-}
-
-TYPED_TEST_P(DwarfDebugFrameTest, Init_version4) {
- // CIE 32 information.
- this->memory_.SetData32(0x5000, 0xfc);
- this->memory_.SetData32(0x5004, 0xffffffff);
- this->memory_.SetData8(0x5008, 4);
- // Augment string.
- this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'L', 'P', 'R', '\0'});
- // Address size.
- this->memory_.SetData8(0x500e, 4);
- // Segment size.
- this->memory_.SetData8(0x500f, 0);
- // Code alignment factor.
- this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x80, 0x00});
- // Data alignment factor.
- this->memory_.SetMemory(0x5012, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00});
- // Return address register
- this->memory_.SetMemory(0x5016, std::vector<uint8_t>{0x85, 0x10});
- // Augmentation length
- this->memory_.SetMemory(0x5018, std::vector<uint8_t>{0x84, 0x00});
- // L data.
- this->memory_.SetData8(0x501a, 0x10);
- // P data.
- this->memory_.SetData8(0x501b, DW_EH_PE_udata4);
- this->memory_.SetData32(0x501c, 0x100);
- // R data.
- this->memory_.SetData8(0x5020, DW_EH_PE_pcrel | DW_EH_PE_udata2);
-
- // FDE 32 information.
- this->memory_.SetData32(0x5100, 0xfc);
- this->memory_.SetData32(0x5104, 0);
- this->memory_.SetData16(0x5108, 0x1500);
- this->memory_.SetData16(0x510a, 0x200);
-
- ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x200));
- ASSERT_EQ(1U, this->debug_frame_->TestGetFdeCount());
-
- typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
- this->debug_frame_->TestGetFdeInfo(0, &info);
- EXPECT_EQ(0x5100U, info.offset);
- EXPECT_EQ(0x1500U, info.start);
- EXPECT_EQ(0x1700U, info.end);
-}
-
-TYPED_TEST_P(DwarfDebugFrameTest, GetFdeOffsetFromPc) {
- typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
- for (size_t i = 0; i < 9; i++) {
- info.start = 0x1000 * (i + 1);
- info.end = 0x1000 * (i + 2) - 0x10;
- info.offset = 0x5000 + i * 0x20;
- this->debug_frame_->TestPushFdeInfo(info);
+static void SetFde32(MemoryFake* memory, uint64_t offset, uint32_t length, uint64_t cie_offset,
+ uint32_t pc_start, uint32_t pc_length, uint64_t segment_length = 0,
+ std::vector<uint8_t>* data = nullptr) {
+ memory->SetData32(offset, length);
+ offset += 4;
+ memory->SetData32(offset, cie_offset);
+ offset += 4 + segment_length;
+ memory->SetData32(offset, pc_start);
+ offset += 4;
+ memory->SetData32(offset, pc_length);
+ if (data != nullptr) {
+ offset += 4;
+ memory->SetMemory(offset, *data);
}
+}
- this->debug_frame_->TestSetFdeCount(0);
- uint64_t fde_offset;
- ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(0x1000, &fde_offset));
- ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
-
- this->debug_frame_->TestSetFdeCount(9);
- ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(0x100, &fde_offset));
- ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
- // Odd number of elements.
- for (size_t i = 0; i < 9; i++) {
- TypeParam pc = 0x1000 * (i + 1);
- ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index "
- << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset))
- << "Failed at index " << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset))
- << "Failed at index " << i;
- ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+static void SetFde64(MemoryFake* memory, uint64_t offset, uint64_t length, uint64_t cie_offset,
+ uint64_t pc_start, uint64_t pc_length, uint64_t segment_length = 0,
+ std::vector<uint8_t>* data = nullptr) {
+ memory->SetData32(offset, 0xffffffff);
+ offset += 4;
+ memory->SetData64(offset, length);
+ offset += 8;
+ memory->SetData64(offset, cie_offset);
+ offset += 8 + segment_length;
+ memory->SetData64(offset, pc_start);
+ offset += 8;
+ memory->SetData64(offset, pc_length);
+ if (data != nullptr) {
+ offset += 8;
+ memory->SetMemory(offset, *data);
}
+}
- // Even number of elements.
- this->debug_frame_->TestSetFdeCount(10);
- info.start = 0xa000;
- info.end = 0xaff0;
- info.offset = 0x5120;
- this->debug_frame_->TestPushFdeInfo(info);
+static void SetFourFdes32(MemoryFake* memory) {
+ SetCie32(memory, 0x5000, 0xfc, std::vector<uint8_t>{1, '\0', 0, 0, 1});
- for (size_t i = 0; i < 10; i++) {
- TypeParam pc = 0x1000 * (i + 1);
- ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index "
- << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset))
- << "Failed at index " << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset))
- << "Failed at index " << i;
- ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
- }
+ // FDE 32 information.
+ SetFde32(memory, 0x5100, 0xfc, 0, 0x1500, 0x200);
+ SetFde32(memory, 0x5200, 0xfc, 0, 0x2500, 0x300);
+
+ // CIE 32 information.
+ SetCie32(memory, 0x5300, 0xfc, std::vector<uint8_t>{1, '\0', 0, 0, 1});
+
+ // FDE 32 information.
+ SetFde32(memory, 0x5400, 0xfc, 0x300, 0x3500, 0x400);
+ SetFde32(memory, 0x5500, 0xfc, 0x300, 0x4500, 0x500);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdes32) {
+ SetFourFdes32(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
+
+ std::vector<const DwarfFde*> fdes;
+ this->debug_frame_->GetFdes(&fdes);
+
+ ASSERT_EQ(4U, fdes.size());
+
+ EXPECT_EQ(0x5000U, fdes[0]->cie_offset);
+ EXPECT_EQ(0x5110U, fdes[0]->cfa_instructions_offset);
+ EXPECT_EQ(0x5200U, fdes[0]->cfa_instructions_end);
+ EXPECT_EQ(0x1500U, fdes[0]->pc_start);
+ EXPECT_EQ(0x1700U, fdes[0]->pc_end);
+ EXPECT_EQ(0U, fdes[0]->lsda_address);
+ EXPECT_TRUE(fdes[0]->cie != nullptr);
+
+ EXPECT_EQ(0x5000U, fdes[1]->cie_offset);
+ EXPECT_EQ(0x5210U, fdes[1]->cfa_instructions_offset);
+ EXPECT_EQ(0x5300U, fdes[1]->cfa_instructions_end);
+ EXPECT_EQ(0x2500U, fdes[1]->pc_start);
+ EXPECT_EQ(0x2800U, fdes[1]->pc_end);
+ EXPECT_EQ(0U, fdes[1]->lsda_address);
+ EXPECT_TRUE(fdes[1]->cie != nullptr);
+
+ EXPECT_EQ(0x5300U, fdes[2]->cie_offset);
+ EXPECT_EQ(0x5410U, fdes[2]->cfa_instructions_offset);
+ EXPECT_EQ(0x5500U, fdes[2]->cfa_instructions_end);
+ EXPECT_EQ(0x3500U, fdes[2]->pc_start);
+ EXPECT_EQ(0x3900U, fdes[2]->pc_end);
+ EXPECT_EQ(0U, fdes[2]->lsda_address);
+ EXPECT_TRUE(fdes[2]->cie != nullptr);
+
+ EXPECT_EQ(0x5300U, fdes[3]->cie_offset);
+ EXPECT_EQ(0x5510U, fdes[3]->cfa_instructions_offset);
+ EXPECT_EQ(0x5600U, fdes[3]->cfa_instructions_end);
+ EXPECT_EQ(0x4500U, fdes[3]->pc_start);
+ EXPECT_EQ(0x4a00U, fdes[3]->pc_end);
+ EXPECT_EQ(0U, fdes[3]->lsda_address);
+ EXPECT_TRUE(fdes[3]->cie != nullptr);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdes32_after_GetFdeFromPc) {
+ SetFourFdes32(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x3600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x3500U, fde->pc_start);
+ EXPECT_EQ(0x3900U, fde->pc_end);
+
+ std::vector<const DwarfFde*> fdes;
+ this->debug_frame_->GetFdes(&fdes);
+ ASSERT_EQ(4U, fdes.size());
+
+ // Verify that they got added in the correct order.
+ EXPECT_EQ(0x1500U, fdes[0]->pc_start);
+ EXPECT_EQ(0x1700U, fdes[0]->pc_end);
+ EXPECT_EQ(0x2500U, fdes[1]->pc_start);
+ EXPECT_EQ(0x2800U, fdes[1]->pc_end);
+ EXPECT_EQ(0x3500U, fdes[2]->pc_start);
+ EXPECT_EQ(0x3900U, fdes[2]->pc_end);
+ EXPECT_EQ(0x4500U, fdes[3]->pc_start);
+ EXPECT_EQ(0x4a00U, fdes[3]->pc_end);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdes32_not_in_section) {
+ SetFourFdes32(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0));
+
+ std::vector<const DwarfFde*> fdes;
+ this->debug_frame_->GetFdes(&fdes);
+
+ ASSERT_EQ(3U, fdes.size());
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc32) {
+ SetFourFdes32(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x1600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x1500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x2600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x2500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x3600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x3500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x4600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x4500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0);
+ ASSERT_TRUE(fde == nullptr);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc32_reverse) {
+ SetFourFdes32(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x4500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x3600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x3500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x2600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x2500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x1600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x1500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0);
+ ASSERT_TRUE(fde == nullptr);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc32_not_in_section) {
+ SetFourFdes32(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0));
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600);
+ ASSERT_TRUE(fde == nullptr);
+}
+
+static void SetFourFdes64(MemoryFake* memory) {
+ // CIE 64 information.
+ SetCie64(memory, 0x5000, 0xf4, std::vector<uint8_t>{1, '\0', 0, 0, 1});
+
+ // FDE 64 information.
+ SetFde64(memory, 0x5100, 0xf4, 0, 0x1500, 0x200);
+ SetFde64(memory, 0x5200, 0xf4, 0, 0x2500, 0x300);
+
+ // CIE 64 information.
+ SetCie64(memory, 0x5300, 0xf4, std::vector<uint8_t>{1, '\0', 0, 0, 1});
+
+ // FDE 64 information.
+ SetFde64(memory, 0x5400, 0xf4, 0x300, 0x3500, 0x400);
+ SetFde64(memory, 0x5500, 0xf4, 0x300, 0x4500, 0x500);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdes64) {
+ SetFourFdes64(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
+
+ std::vector<const DwarfFde*> fdes;
+ this->debug_frame_->GetFdes(&fdes);
+
+ ASSERT_EQ(4U, fdes.size());
+
+ EXPECT_EQ(0x5000U, fdes[0]->cie_offset);
+ EXPECT_EQ(0x5124U, fdes[0]->cfa_instructions_offset);
+ EXPECT_EQ(0x5200U, fdes[0]->cfa_instructions_end);
+ EXPECT_EQ(0x1500U, fdes[0]->pc_start);
+ EXPECT_EQ(0x1700U, fdes[0]->pc_end);
+ EXPECT_EQ(0U, fdes[0]->lsda_address);
+ EXPECT_TRUE(fdes[0]->cie != nullptr);
+
+ EXPECT_EQ(0x5000U, fdes[1]->cie_offset);
+ EXPECT_EQ(0x5224U, fdes[1]->cfa_instructions_offset);
+ EXPECT_EQ(0x5300U, fdes[1]->cfa_instructions_end);
+ EXPECT_EQ(0x2500U, fdes[1]->pc_start);
+ EXPECT_EQ(0x2800U, fdes[1]->pc_end);
+ EXPECT_EQ(0U, fdes[1]->lsda_address);
+ EXPECT_TRUE(fdes[1]->cie != nullptr);
+
+ EXPECT_EQ(0x5300U, fdes[2]->cie_offset);
+ EXPECT_EQ(0x5424U, fdes[2]->cfa_instructions_offset);
+ EXPECT_EQ(0x5500U, fdes[2]->cfa_instructions_end);
+ EXPECT_EQ(0x3500U, fdes[2]->pc_start);
+ EXPECT_EQ(0x3900U, fdes[2]->pc_end);
+ EXPECT_EQ(0U, fdes[2]->lsda_address);
+ EXPECT_TRUE(fdes[2]->cie != nullptr);
+
+ EXPECT_EQ(0x5300U, fdes[3]->cie_offset);
+ EXPECT_EQ(0x5524U, fdes[3]->cfa_instructions_offset);
+ EXPECT_EQ(0x5600U, fdes[3]->cfa_instructions_end);
+ EXPECT_EQ(0x4500U, fdes[3]->pc_start);
+ EXPECT_EQ(0x4a00U, fdes[3]->pc_end);
+ EXPECT_EQ(0U, fdes[3]->lsda_address);
+ EXPECT_TRUE(fdes[3]->cie != nullptr);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdes64_after_GetFdeFromPc) {
+ SetFourFdes64(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x2600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x2500U, fde->pc_start);
+ EXPECT_EQ(0x2800U, fde->pc_end);
+
+ std::vector<const DwarfFde*> fdes;
+ this->debug_frame_->GetFdes(&fdes);
+ ASSERT_EQ(4U, fdes.size());
+
+ // Verify that they got added in the correct order.
+ EXPECT_EQ(0x1500U, fdes[0]->pc_start);
+ EXPECT_EQ(0x1700U, fdes[0]->pc_end);
+ EXPECT_EQ(0x2500U, fdes[1]->pc_start);
+ EXPECT_EQ(0x2800U, fdes[1]->pc_end);
+ EXPECT_EQ(0x3500U, fdes[2]->pc_start);
+ EXPECT_EQ(0x3900U, fdes[2]->pc_end);
+ EXPECT_EQ(0x4500U, fdes[3]->pc_start);
+ EXPECT_EQ(0x4a00U, fdes[3]->pc_end);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdes64_not_in_section) {
+ SetFourFdes64(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0));
+
+ std::vector<const DwarfFde*> fdes;
+ this->debug_frame_->GetFdes(&fdes);
+
+ ASSERT_EQ(3U, fdes.size());
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc64) {
+ SetFourFdes64(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x1600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x1500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x2600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x2500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x3600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x3500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x4600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x4500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0);
+ ASSERT_TRUE(fde == nullptr);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc64_reverse) {
+ SetFourFdes64(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x4500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x3600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x3500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x2600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x2500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0x1600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x1500U, fde->pc_start);
+
+ fde = this->debug_frame_->GetFdeFromPc(0);
+ ASSERT_TRUE(fde == nullptr);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc64_not_in_section) {
+ SetFourFdes64(&this->memory_);
+ ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0));
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600);
+ ASSERT_TRUE(fde == nullptr);
}
TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde32) {
- this->debug_frame_->TestSetOffset(0x4000);
-
- // CIE 32 information.
- this->memory_.SetData32(0xf000, 0x100);
- this->memory_.SetData32(0xf004, 0xffffffff);
- this->memory_.SetData8(0xf008, 0x1);
- this->memory_.SetData8(0xf009, '\0');
- this->memory_.SetData8(0xf00a, 4);
- this->memory_.SetData8(0xf00b, 8);
- this->memory_.SetData8(0xf00c, 0x20);
-
- // FDE 32 information.
- this->memory_.SetData32(0x14000, 0x20);
- this->memory_.SetData32(0x14004, 0xb000);
- this->memory_.SetData32(0x14008, 0x9000);
- this->memory_.SetData32(0x1400c, 0x100);
+ SetCie32(&this->memory_, 0xf000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
+ SetFde32(&this->memory_, 0x14000, 0x20, 0xf000, 0x9000, 0x100);
const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x14000);
ASSERT_TRUE(fde != nullptr);
@@ -492,24 +428,8 @@
}
TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde64) {
- this->debug_frame_->TestSetOffset(0x2000);
-
- // CIE 64 information.
- this->memory_.SetData32(0x6000, 0xffffffff);
- this->memory_.SetData64(0x6004, 0x100);
- this->memory_.SetData64(0x600c, 0xffffffffffffffffULL);
- this->memory_.SetData8(0x6014, 0x1);
- this->memory_.SetData8(0x6015, '\0');
- this->memory_.SetData8(0x6016, 4);
- this->memory_.SetData8(0x6017, 8);
- this->memory_.SetData8(0x6018, 0x20);
-
- // FDE 64 information.
- this->memory_.SetData32(0x8000, 0xffffffff);
- this->memory_.SetData64(0x8004, 0x200);
- this->memory_.SetData64(0x800c, 0x4000);
- this->memory_.SetData64(0x8014, 0x5000);
- this->memory_.SetData64(0x801c, 0x300);
+ SetCie64(&this->memory_, 0x6000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
+ SetFde64(&this->memory_, 0x8000, 0x200, 0x6000, 0x5000, 0x300);
const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x8000);
ASSERT_TRUE(fde != nullptr);
@@ -535,11 +455,357 @@
EXPECT_EQ(0x20U, fde->cie->return_address_register);
}
-REGISTER_TYPED_TEST_CASE_P(DwarfDebugFrameTest, Init32, Init32_fde_not_following_cie,
- Init32_do_not_fail_on_bad_next_entry, Init64,
- Init64_do_not_fail_on_bad_next_entry, Init64_fde_not_following_cie,
- Init_version1, Init_version4, GetFdeOffsetFromPc, GetCieFde32,
- GetCieFde64);
+static void VerifyCieVersion(const DwarfCie* cie, uint8_t version, uint8_t segment_size,
+ uint8_t fde_encoding, uint64_t return_address, uint64_t start_offset,
+ uint64_t end_offset) {
+ EXPECT_EQ(version, cie->version);
+ EXPECT_EQ(fde_encoding, cie->fde_address_encoding);
+ EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
+ EXPECT_EQ(segment_size, cie->segment_size);
+ EXPECT_EQ(1U, cie->augmentation_string.size());
+ EXPECT_EQ('\0', cie->augmentation_string[0]);
+ EXPECT_EQ(0U, cie->personality_handler);
+ EXPECT_EQ(4U, cie->code_alignment_factor);
+ EXPECT_EQ(8, cie->data_alignment_factor);
+ EXPECT_EQ(return_address, cie->return_address_register);
+ EXPECT_EQ(0x5000U + start_offset, cie->cfa_instructions_offset);
+ EXPECT_EQ(0x5000U + end_offset, cie->cfa_instructions_end);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_cie_cached) {
+ SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
+ const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata4, 0x20, 0xd, 0x104);
+
+ std::vector<uint8_t> zero(0x100, 0);
+ this->memory_.SetMemory(0x5000, zero);
+ cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata4, 0x20, 0xd, 0x104);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_cie_cached) {
+ SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
+ const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata8, 0x20, 0x19, 0x10c);
+
+ std::vector<uint8_t> zero(0x100, 0);
+ this->memory_.SetMemory(0x5000, zero);
+ cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata8, 0x20, 0x19, 0x10c);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version1) {
+ SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
+ const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata4, 0x20, 0xd, 0x104);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version1) {
+ SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
+ const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata8, 0x20, 0x19, 0x10c);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version3) {
+ SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{3, '\0', 4, 8, 0x81, 3});
+ const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieVersion(cie, 3, 0, DW_EH_PE_sdata4, 0x181, 0xe, 0x104);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version3) {
+ SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{3, '\0', 4, 8, 0x81, 3});
+ const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieVersion(cie, 3, 0, DW_EH_PE_sdata8, 0x181, 0x1a, 0x10c);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version4) {
+ SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{4, '\0', 0, 10, 4, 8, 0x81, 3});
+ const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieVersion(cie, 4, 10, DW_EH_PE_sdata4, 0x181, 0x10, 0x104);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version4) {
+ SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{4, '\0', 0, 10, 4, 8, 0x81, 3});
+ const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieVersion(cie, 4, 10, DW_EH_PE_sdata8, 0x181, 0x1c, 0x10c);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset_version_invalid) {
+ SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{0, '\0', 1, 2, 3, 4, 5, 6, 7});
+ ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x5000) == nullptr);
+ EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode());
+ SetCie64(&this->memory_, 0x6000, 0x100, std::vector<uint8_t>{0, '\0', 1, 2, 3, 4, 5, 6, 7});
+ ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x6000) == nullptr);
+ EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode());
+
+ SetCie32(&this->memory_, 0x7000, 0x100, std::vector<uint8_t>{5, '\0', 1, 2, 3, 4, 5, 6, 7});
+ ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x7000) == nullptr);
+ EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode());
+ SetCie64(&this->memory_, 0x8000, 0x100, std::vector<uint8_t>{5, '\0', 1, 2, 3, 4, 5, 6, 7});
+ ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x8000) == nullptr);
+ EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode());
+}
+
+static void VerifyCieAugment(const DwarfCie* cie, uint64_t inst_offset, uint64_t inst_end) {
+ EXPECT_EQ(1U, cie->version);
+ EXPECT_EQ(DW_EH_PE_udata2, cie->fde_address_encoding);
+ EXPECT_EQ(DW_EH_PE_textrel | DW_EH_PE_udata2, cie->lsda_encoding);
+ EXPECT_EQ(0U, cie->segment_size);
+ EXPECT_EQ(5U, cie->augmentation_string.size());
+ EXPECT_EQ('z', cie->augmentation_string[0]);
+ EXPECT_EQ('L', cie->augmentation_string[1]);
+ EXPECT_EQ('P', cie->augmentation_string[2]);
+ EXPECT_EQ('R', cie->augmentation_string[3]);
+ EXPECT_EQ('\0', cie->augmentation_string[4]);
+ EXPECT_EQ(0x12345678U, cie->personality_handler);
+ EXPECT_EQ(4U, cie->code_alignment_factor);
+ EXPECT_EQ(8, cie->data_alignment_factor);
+ EXPECT_EQ(0x10U, cie->return_address_register);
+ EXPECT_EQ(inst_offset, cie->cfa_instructions_offset);
+ EXPECT_EQ(inst_end, cie->cfa_instructions_end);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_augment) {
+ SetCie32(&this->memory_, 0x5000, 0x100,
+ std::vector<uint8_t>{/* version */ 1,
+ /* augment string */ 'z', 'L', 'P', 'R', '\0',
+ /* code alignment factor */ 4,
+ /* data alignment factor */ 8,
+ /* return address register */ 0x10,
+ /* augment length */ 0xf,
+ /* L data */ DW_EH_PE_textrel | DW_EH_PE_udata2,
+ /* P data */ DW_EH_PE_udata4, 0x78, 0x56, 0x34, 0x12,
+ /* R data */ DW_EH_PE_udata2});
+
+ const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieAugment(cie, 0x5021, 0x5104);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_augment) {
+ SetCie64(&this->memory_, 0x5000, 0x100,
+ std::vector<uint8_t>{/* version */ 1,
+ /* augment string */ 'z', 'L', 'P', 'R', '\0',
+ /* code alignment factor */ 4,
+ /* data alignment factor */ 8,
+ /* return address register */ 0x10,
+ /* augment length */ 0xf,
+ /* L data */ DW_EH_PE_textrel | DW_EH_PE_udata2,
+ /* P data */ DW_EH_PE_udata4, 0x78, 0x56, 0x34, 0x12,
+ /* R data */ DW_EH_PE_udata2});
+
+ const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000);
+ ASSERT_TRUE(cie != nullptr);
+ VerifyCieAugment(cie, 0x502d, 0x510c);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset32_augment) {
+ SetCie32(&this->memory_, 0x5000, 0xfc,
+ std::vector<uint8_t>{/* version */ 4,
+ /* augment string */ 'z', '\0',
+ /* address size */ 8,
+ /* segment size */ 0x10,
+ /* code alignment factor */ 16,
+ /* data alignment factor */ 32,
+ /* return address register */ 10,
+ /* augment length */ 0x0});
+
+ std::vector<uint8_t> data{/* augment length */ 0x80, 0x3};
+ SetFde32(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0x10, &data);
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200);
+ ASSERT_TRUE(fde != nullptr);
+ ASSERT_TRUE(fde->cie != nullptr);
+ EXPECT_EQ(4U, fde->cie->version);
+ EXPECT_EQ(0x5000U, fde->cie_offset);
+ EXPECT_EQ(0x53a2U, fde->cfa_instructions_offset);
+ EXPECT_EQ(0x5504U, fde->cfa_instructions_end);
+ EXPECT_EQ(0x4300U, fde->pc_start);
+ EXPECT_EQ(0x4600U, fde->pc_end);
+ EXPECT_EQ(0U, fde->lsda_address);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset64_augment) {
+ SetCie64(&this->memory_, 0x5000, 0xfc,
+ std::vector<uint8_t>{/* version */ 4,
+ /* augment string */ 'z', '\0',
+ /* address size */ 8,
+ /* segment size */ 0x10,
+ /* code alignment factor */ 16,
+ /* data alignment factor */ 32,
+ /* return address register */ 10,
+ /* augment length */ 0x0});
+
+ std::vector<uint8_t> data{/* augment length */ 0x80, 0x3};
+ SetFde64(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0x10, &data);
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200);
+ ASSERT_TRUE(fde != nullptr);
+ ASSERT_TRUE(fde->cie != nullptr);
+ EXPECT_EQ(4U, fde->cie->version);
+ EXPECT_EQ(0x5000U, fde->cie_offset);
+ EXPECT_EQ(0x53b6U, fde->cfa_instructions_offset);
+ EXPECT_EQ(0x550cU, fde->cfa_instructions_end);
+ EXPECT_EQ(0x4300U, fde->pc_start);
+ EXPECT_EQ(0x4600U, fde->pc_end);
+ EXPECT_EQ(0U, fde->lsda_address);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset32_lsda_address) {
+ SetCie32(&this->memory_, 0x5000, 0xfc,
+ std::vector<uint8_t>{/* version */ 1,
+ /* augment string */ 'z', 'L', '\0',
+ /* address size */ 8,
+ /* code alignment factor */ 16,
+ /* data alignment factor */ 32,
+ /* return address register */ 10,
+ /* augment length */ 0x2,
+ /* L data */ DW_EH_PE_udata2});
+
+ std::vector<uint8_t> data{/* augment length */ 0x80, 0x3,
+ /* lsda address */ 0x20, 0x45};
+ SetFde32(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0, &data);
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200);
+ ASSERT_TRUE(fde != nullptr);
+ ASSERT_TRUE(fde->cie != nullptr);
+ EXPECT_EQ(1U, fde->cie->version);
+ EXPECT_EQ(0x5000U, fde->cie_offset);
+ EXPECT_EQ(0x5392U, fde->cfa_instructions_offset);
+ EXPECT_EQ(0x5504U, fde->cfa_instructions_end);
+ EXPECT_EQ(0x4300U, fde->pc_start);
+ EXPECT_EQ(0x4600U, fde->pc_end);
+ EXPECT_EQ(0x4520U, fde->lsda_address);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset64_lsda_address) {
+ SetCie64(&this->memory_, 0x5000, 0xfc,
+ std::vector<uint8_t>{/* version */ 1,
+ /* augment string */ 'z', 'L', '\0',
+ /* address size */ 8,
+ /* code alignment factor */ 16,
+ /* data alignment factor */ 32,
+ /* return address register */ 10,
+ /* augment length */ 0x2,
+ /* L data */ DW_EH_PE_udata2});
+
+ std::vector<uint8_t> data{/* augment length */ 0x80, 0x3,
+ /* lsda address */ 0x20, 0x45};
+ SetFde64(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0, &data);
+
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200);
+ ASSERT_TRUE(fde != nullptr);
+ ASSERT_TRUE(fde->cie != nullptr);
+ EXPECT_EQ(1U, fde->cie->version);
+ EXPECT_EQ(0x5000U, fde->cie_offset);
+ EXPECT_EQ(0x53a6U, fde->cfa_instructions_offset);
+ EXPECT_EQ(0x550cU, fde->cfa_instructions_end);
+ EXPECT_EQ(0x4300U, fde->pc_start);
+ EXPECT_EQ(0x4600U, fde->pc_end);
+ EXPECT_EQ(0x4520U, fde->lsda_address);
+}
+
+TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc_interleaved) {
+ SetCie32(&this->memory_, 0x5000, 0xfc, std::vector<uint8_t>{1, '\0', 0, 0, 1});
+
+ // FDE 0 (0x100 - 0x200)
+ SetFde32(&this->memory_, 0x5100, 0xfc, 0, 0x100, 0x100);
+ // FDE 1 (0x300 - 0x500)
+ SetFde32(&this->memory_, 0x5200, 0xfc, 0, 0x300, 0x200);
+ // FDE 2 (0x700 - 0x800)
+ SetFde32(&this->memory_, 0x5300, 0xfc, 0, 0x700, 0x100);
+ // FDE 3 (0xa00 - 0xb00)
+ SetFde32(&this->memory_, 0x5400, 0xfc, 0, 0xa00, 0x100);
+ // FDE 4 (0x100 - 0xb00)
+ SetFde32(&this->memory_, 0x5500, 0xfc, 0, 0x150, 0xa00);
+ // FDE 5 (0x0 - 0x50)
+ SetFde32(&this->memory_, 0x5600, 0xfc, 0, 0, 0x50);
+
+ this->debug_frame_->Init(0x5000, 0x700, 0);
+
+ // Force reading all entries so no entries are found.
+ const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0xfffff);
+ ASSERT_TRUE(fde == nullptr);
+
+ // 0x0 - 0x50 FDE 5
+ fde = this->debug_frame_->GetFdeFromPc(0x10);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0U, fde->pc_start);
+ EXPECT_EQ(0x50U, fde->pc_end);
+
+ // 0x100 - 0x200 FDE 0
+ fde = this->debug_frame_->GetFdeFromPc(0x170);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x100U, fde->pc_start);
+ EXPECT_EQ(0x200U, fde->pc_end);
+
+ // 0x200 - 0x300 FDE 4
+ fde = this->debug_frame_->GetFdeFromPc(0x210);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x150U, fde->pc_start);
+ EXPECT_EQ(0xb50U, fde->pc_end);
+
+ // 0x300 - 0x500 FDE 1
+ fde = this->debug_frame_->GetFdeFromPc(0x310);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x300U, fde->pc_start);
+ EXPECT_EQ(0x500U, fde->pc_end);
+
+ // 0x700 - 0x800 FDE 2
+ fde = this->debug_frame_->GetFdeFromPc(0x790);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x700U, fde->pc_start);
+ EXPECT_EQ(0x800U, fde->pc_end);
+
+ // 0x800 - 0x900 FDE 4
+ fde = this->debug_frame_->GetFdeFromPc(0x850);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x150U, fde->pc_start);
+ EXPECT_EQ(0xb50U, fde->pc_end);
+
+ // 0xa00 - 0xb00 FDE 3
+ fde = this->debug_frame_->GetFdeFromPc(0xa35);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0xa00U, fde->pc_start);
+ EXPECT_EQ(0xb00U, fde->pc_end);
+
+ // 0xb00 - 0xb50 FDE 4
+ fde = this->debug_frame_->GetFdeFromPc(0xb20);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x150U, fde->pc_start);
+ EXPECT_EQ(0xb50U, fde->pc_end);
+}
+
+REGISTER_TYPED_TEST_CASE_P(
+ DwarfDebugFrameTest, GetFdes32, GetFdes32_after_GetFdeFromPc, GetFdes32_not_in_section,
+ GetFdeFromPc32, GetFdeFromPc32_reverse, GetFdeFromPc32_not_in_section, GetFdes64,
+ GetFdes64_after_GetFdeFromPc, GetFdes64_not_in_section, GetFdeFromPc64, GetFdeFromPc64_reverse,
+ GetFdeFromPc64_not_in_section, GetCieFde32, GetCieFde64, GetCieFromOffset32_cie_cached,
+ GetCieFromOffset64_cie_cached, GetCieFromOffset32_version1, GetCieFromOffset64_version1,
+ GetCieFromOffset32_version3, GetCieFromOffset64_version3, GetCieFromOffset32_version4,
+ GetCieFromOffset64_version4, GetCieFromOffset_version_invalid, GetCieFromOffset32_augment,
+ GetCieFromOffset64_augment, GetFdeFromOffset32_augment, GetFdeFromOffset64_augment,
+ GetFdeFromOffset32_lsda_address, GetFdeFromOffset64_lsda_address, GetFdeFromPc_interleaved);
typedef ::testing::Types<uint32_t, uint64_t> DwarfDebugFrameTestTypes;
INSTANTIATE_TYPED_TEST_CASE_P(, DwarfDebugFrameTest, DwarfDebugFrameTestTypes);
diff --git a/libunwindstack/tests/DwarfEhFrameTest.cpp b/libunwindstack/tests/DwarfEhFrameTest.cpp
index a73db65..9cac6e8 100644
--- a/libunwindstack/tests/DwarfEhFrameTest.cpp
+++ b/libunwindstack/tests/DwarfEhFrameTest.cpp
@@ -16,7 +16,6 @@
#include <stdint.h>
-#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <unwindstack/DwarfError.h>
@@ -30,50 +29,32 @@
namespace unwindstack {
template <typename TypeParam>
-class MockDwarfEhFrame : public DwarfEhFrame<TypeParam> {
- public:
- MockDwarfEhFrame(Memory* memory) : DwarfEhFrame<TypeParam>(memory) {}
- ~MockDwarfEhFrame() = default;
-
- void TestSetFdeCount(uint64_t count) { this->fde_count_ = count; }
- void TestSetOffset(uint64_t offset) { this->entries_offset_ = offset; }
- void TestSetEndOffset(uint64_t offset) { this->entries_end_ = offset; }
- void TestPushFdeInfo(const typename DwarfEhFrame<TypeParam>::FdeInfo& info) {
- this->fdes_.push_back(info);
- }
-
- uint64_t TestGetFdeCount() { return this->fde_count_; }
- uint8_t TestGetOffset() { return this->offset_; }
- uint8_t TestGetEndOffset() { return this->end_offset_; }
- void TestGetFdeInfo(size_t index, typename DwarfEhFrame<TypeParam>::FdeInfo* info) {
- *info = this->fdes_[index];
- }
-};
-
-template <typename TypeParam>
class DwarfEhFrameTest : public ::testing::Test {
protected:
void SetUp() override {
memory_.Clear();
- eh_frame_ = new MockDwarfEhFrame<TypeParam>(&memory_);
+ eh_frame_ = new DwarfEhFrame<TypeParam>(&memory_);
ResetLogs();
}
void TearDown() override { delete eh_frame_; }
MemoryFake memory_;
- MockDwarfEhFrame<TypeParam>* eh_frame_ = nullptr;
+ DwarfEhFrame<TypeParam>* eh_frame_ = nullptr;
};
TYPED_TEST_CASE_P(DwarfEhFrameTest);
// NOTE: All test class variables need to be referenced as this->.
-TYPED_TEST_P(DwarfEhFrameTest, Init32) {
+// Only verify different cie/fde format. All other DwarfSection corner
+// cases are tested in DwarfDebugFrameTest.cpp.
+
+TYPED_TEST_P(DwarfEhFrameTest, GetFdeCieFromOffset32) {
// CIE 32 information.
this->memory_.SetData32(0x5000, 0xfc);
+ // Indicates this is a cie for eh_frame.
this->memory_.SetData32(0x5004, 0);
- this->memory_.SetData8(0x5008, 1);
- this->memory_.SetData8(0x5009, '\0');
+ this->memory_.SetMemory(0x5008, std::vector<uint8_t>{1, '\0', 16, 32, 1});
// FDE 32 information.
this->memory_.SetData32(0x5100, 0xfc);
@@ -81,377 +62,70 @@
this->memory_.SetData32(0x5108, 0x1500);
this->memory_.SetData32(0x510c, 0x200);
- this->memory_.SetData32(0x5200, 0xfc);
- this->memory_.SetData32(0x5204, 0x204);
- this->memory_.SetData32(0x5208, 0x2500);
- this->memory_.SetData32(0x520c, 0x300);
+ const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x5100);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x5000U, fde->cie_offset);
+ EXPECT_EQ(0x5110U, fde->cfa_instructions_offset);
+ EXPECT_EQ(0x5200U, fde->cfa_instructions_end);
+ EXPECT_EQ(0x6608U, fde->pc_start);
+ EXPECT_EQ(0x6808U, fde->pc_end);
+ EXPECT_EQ(0U, fde->lsda_address);
- // CIE 32 information.
- this->memory_.SetData32(0x5300, 0xfc);
- this->memory_.SetData32(0x5304, 0);
- this->memory_.SetData8(0x5308, 1);
- this->memory_.SetData8(0x5309, '\0');
-
- // FDE 32 information.
- this->memory_.SetData32(0x5400, 0xfc);
- this->memory_.SetData32(0x5404, 0x104);
- this->memory_.SetData32(0x5408, 0x3500);
- this->memory_.SetData32(0x540c, 0x400);
-
- this->memory_.SetData32(0x5500, 0xfc);
- this->memory_.SetData32(0x5504, 0x204);
- this->memory_.SetData32(0x5508, 0x4500);
- this->memory_.SetData32(0x550c, 0x500);
-
- ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x600));
- ASSERT_EQ(4U, this->eh_frame_->TestGetFdeCount());
-
- typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
-
- this->eh_frame_->TestGetFdeInfo(0, &info);
- EXPECT_EQ(0x5100U, info.offset);
- EXPECT_EQ(0x6608U, info.start);
- EXPECT_EQ(0x6808U, info.end);
-
- this->eh_frame_->TestGetFdeInfo(1, &info);
- EXPECT_EQ(0x5200U, info.offset);
- EXPECT_EQ(0x7708U, info.start);
- EXPECT_EQ(0x7a08U, info.end);
-
- this->eh_frame_->TestGetFdeInfo(2, &info);
- EXPECT_EQ(0x5400U, info.offset);
- EXPECT_EQ(0x8908U, info.start);
- EXPECT_EQ(0x8d08U, info.end);
-
- this->eh_frame_->TestGetFdeInfo(3, &info);
- EXPECT_EQ(0x5500U, info.offset);
- EXPECT_EQ(0x9a08U, info.start);
- EXPECT_EQ(0x9f08U, info.end);
+ const DwarfCie* cie = fde->cie;
+ ASSERT_TRUE(cie != nullptr);
+ EXPECT_EQ(1U, cie->version);
+ EXPECT_EQ(DW_EH_PE_sdata4, cie->fde_address_encoding);
+ EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
+ EXPECT_EQ(0U, cie->segment_size);
+ EXPECT_EQ('\0', cie->augmentation_string[0]);
+ EXPECT_EQ(0U, cie->personality_handler);
+ EXPECT_EQ(0x500dU, cie->cfa_instructions_offset);
+ EXPECT_EQ(0x5100U, cie->cfa_instructions_end);
+ EXPECT_EQ(16U, cie->code_alignment_factor);
+ EXPECT_EQ(32U, cie->data_alignment_factor);
+ EXPECT_EQ(1U, cie->return_address_register);
}
-TYPED_TEST_P(DwarfEhFrameTest, Init32_fde_not_following_cie) {
- // CIE 32 information.
- this->memory_.SetData32(0x5000, 0xfc);
- this->memory_.SetData32(0x5004, 0);
- this->memory_.SetData8(0x5008, 1);
- this->memory_.SetData8(0x5009, '\0');
-
- // FDE 32 information.
- this->memory_.SetData32(0x5100, 0xfc);
- this->memory_.SetData32(0x5104, 0x1000);
- this->memory_.SetData32(0x5108, 0x1500);
- this->memory_.SetData32(0x510c, 0x200);
-
- ASSERT_FALSE(this->eh_frame_->Init(0x5000, 0x600));
- ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->eh_frame_->LastErrorCode());
-}
-
-TYPED_TEST_P(DwarfEhFrameTest, Init64) {
+TYPED_TEST_P(DwarfEhFrameTest, GetFdeCieFromOffset64) {
// CIE 64 information.
this->memory_.SetData32(0x5000, 0xffffffff);
- this->memory_.SetData64(0x5004, 0xf4);
+ this->memory_.SetData64(0x5004, 0xfc);
+ // Indicates this is a cie for eh_frame.
this->memory_.SetData64(0x500c, 0);
- this->memory_.SetData8(0x5014, 1);
- this->memory_.SetData8(0x5015, '\0');
+ this->memory_.SetMemory(0x5014, std::vector<uint8_t>{1, '\0', 16, 32, 1});
// FDE 64 information.
this->memory_.SetData32(0x5100, 0xffffffff);
- this->memory_.SetData64(0x5104, 0xf4);
+ this->memory_.SetData64(0x5104, 0xfc);
this->memory_.SetData64(0x510c, 0x10c);
this->memory_.SetData64(0x5114, 0x1500);
this->memory_.SetData64(0x511c, 0x200);
- this->memory_.SetData32(0x5200, 0xffffffff);
- this->memory_.SetData64(0x5204, 0xf4);
- this->memory_.SetData64(0x520c, 0x20c);
- this->memory_.SetData64(0x5214, 0x2500);
- this->memory_.SetData64(0x521c, 0x300);
-
- // CIE 64 information.
- this->memory_.SetData32(0x5300, 0xffffffff);
- this->memory_.SetData64(0x5304, 0xf4);
- this->memory_.SetData64(0x530c, 0);
- this->memory_.SetData8(0x5314, 1);
- this->memory_.SetData8(0x5315, '\0');
-
- // FDE 64 information.
- this->memory_.SetData32(0x5400, 0xffffffff);
- this->memory_.SetData64(0x5404, 0xf4);
- this->memory_.SetData64(0x540c, 0x10c);
- this->memory_.SetData64(0x5414, 0x3500);
- this->memory_.SetData64(0x541c, 0x400);
-
- this->memory_.SetData32(0x5500, 0xffffffff);
- this->memory_.SetData64(0x5504, 0xf4);
- this->memory_.SetData64(0x550c, 0x20c);
- this->memory_.SetData64(0x5514, 0x4500);
- this->memory_.SetData64(0x551c, 0x500);
-
- ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x600));
- ASSERT_EQ(4U, this->eh_frame_->TestGetFdeCount());
-
- typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
-
- this->eh_frame_->TestGetFdeInfo(0, &info);
- EXPECT_EQ(0x5100U, info.offset);
- EXPECT_EQ(0x6618U, info.start);
- EXPECT_EQ(0x6818U, info.end);
-
- this->eh_frame_->TestGetFdeInfo(1, &info);
- EXPECT_EQ(0x5200U, info.offset);
- EXPECT_EQ(0x7718U, info.start);
- EXPECT_EQ(0x7a18U, info.end);
-
- this->eh_frame_->TestGetFdeInfo(2, &info);
- EXPECT_EQ(0x5400U, info.offset);
- EXPECT_EQ(0x8918U, info.start);
- EXPECT_EQ(0x8d18U, info.end);
-
- this->eh_frame_->TestGetFdeInfo(3, &info);
- EXPECT_EQ(0x5500U, info.offset);
- EXPECT_EQ(0x9a18U, info.start);
- EXPECT_EQ(0x9f18U, info.end);
-}
-
-TYPED_TEST_P(DwarfEhFrameTest, Init64_fde_not_following_cie) {
- // CIE 64 information.
- this->memory_.SetData32(0x5000, 0xffffffff);
- this->memory_.SetData64(0x5004, 0xf4);
- this->memory_.SetData64(0x500c, 0);
- this->memory_.SetData8(0x5014, 1);
- this->memory_.SetData8(0x5015, '\0');
-
- // FDE 64 information.
- this->memory_.SetData32(0x5100, 0xffffffff);
- this->memory_.SetData64(0x5104, 0xf4);
- this->memory_.SetData64(0x510c, 0x1000);
- this->memory_.SetData64(0x5114, 0x1500);
- this->memory_.SetData64(0x511c, 0x200);
-
- ASSERT_FALSE(this->eh_frame_->Init(0x5000, 0x600));
- ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->eh_frame_->LastErrorCode());
-}
-
-TYPED_TEST_P(DwarfEhFrameTest, Init_version1) {
- // CIE 32 information.
- this->memory_.SetData32(0x5000, 0xfc);
- this->memory_.SetData32(0x5004, 0);
- this->memory_.SetData8(0x5008, 1);
- // Augment string.
- this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'R', 'P', 'L', '\0'});
- // Code alignment factor.
- this->memory_.SetMemory(0x500e, std::vector<uint8_t>{0x80, 0x00});
- // Data alignment factor.
- this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00});
- // Return address register
- this->memory_.SetData8(0x5014, 0x84);
- // Augmentation length
- this->memory_.SetMemory(0x5015, std::vector<uint8_t>{0x84, 0x00});
- // R data.
- this->memory_.SetData8(0x5017, DW_EH_PE_pcrel | DW_EH_PE_udata2);
-
- // FDE 32 information.
- this->memory_.SetData32(0x5100, 0xfc);
- this->memory_.SetData32(0x5104, 0x104);
- this->memory_.SetData16(0x5108, 0x1500);
- this->memory_.SetData16(0x510a, 0x200);
-
- ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x200));
- ASSERT_EQ(1U, this->eh_frame_->TestGetFdeCount());
-
- typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
- this->eh_frame_->TestGetFdeInfo(0, &info);
- EXPECT_EQ(0x5100U, info.offset);
- EXPECT_EQ(0x6606U, info.start);
- EXPECT_EQ(0x6806U, info.end);
-}
-
-TYPED_TEST_P(DwarfEhFrameTest, Init_version4) {
- // CIE 32 information.
- this->memory_.SetData32(0x5000, 0xfc);
- this->memory_.SetData32(0x5004, 0);
- this->memory_.SetData8(0x5008, 4);
- // Augment string.
- this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'L', 'P', 'R', '\0'});
- // Address size.
- this->memory_.SetData8(0x500e, 4);
- // Segment size.
- this->memory_.SetData8(0x500f, 0);
- // Code alignment factor.
- this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x80, 0x00});
- // Data alignment factor.
- this->memory_.SetMemory(0x5012, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00});
- // Return address register
- this->memory_.SetMemory(0x5016, std::vector<uint8_t>{0x85, 0x10});
- // Augmentation length
- this->memory_.SetMemory(0x5018, std::vector<uint8_t>{0x84, 0x00});
- // L data.
- this->memory_.SetData8(0x501a, 0x10);
- // P data.
- this->memory_.SetData8(0x501b, DW_EH_PE_udata4);
- this->memory_.SetData32(0x501c, 0x100);
- // R data.
- this->memory_.SetData8(0x5020, DW_EH_PE_pcrel | DW_EH_PE_udata2);
-
- // FDE 32 information.
- this->memory_.SetData32(0x5100, 0xfc);
- this->memory_.SetData32(0x5104, 0x104);
- this->memory_.SetData16(0x5108, 0x1500);
- this->memory_.SetData16(0x510a, 0x200);
-
- ASSERT_TRUE(this->eh_frame_->Init(0x5000, 0x200));
- ASSERT_EQ(1U, this->eh_frame_->TestGetFdeCount());
-
- typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
- this->eh_frame_->TestGetFdeInfo(0, &info);
- EXPECT_EQ(0x5100U, info.offset);
- EXPECT_EQ(0x6606U, info.start);
- EXPECT_EQ(0x6806U, info.end);
-}
-
-TYPED_TEST_P(DwarfEhFrameTest, GetFdeOffsetFromPc) {
- typename DwarfEhFrame<TypeParam>::FdeInfo info(0, 0, 0);
- for (size_t i = 0; i < 9; i++) {
- info.start = 0x1000 * (i + 1);
- info.end = 0x1000 * (i + 2) - 0x10;
- info.offset = 0x5000 + i * 0x20;
- this->eh_frame_->TestPushFdeInfo(info);
- }
-
- this->eh_frame_->TestSetFdeCount(0);
- uint64_t fde_offset;
- ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x1000, &fde_offset));
- ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
-
- this->eh_frame_->TestSetFdeCount(9);
- ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x100, &fde_offset));
- ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
- // Odd number of elements.
- for (size_t i = 0; i < 9; i++) {
- TypeParam pc = 0x1000 * (i + 1);
- ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index " << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset))
- << "Failed at index " << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset))
- << "Failed at index " << i;
- ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
- }
-
- // Even number of elements.
- this->eh_frame_->TestSetFdeCount(10);
- info.start = 0xa000;
- info.end = 0xaff0;
- info.offset = 0x5120;
- this->eh_frame_->TestPushFdeInfo(info);
-
- for (size_t i = 0; i < 10; i++) {
- TypeParam pc = 0x1000 * (i + 1);
- ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index " << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset))
- << "Failed at index " << i;
- EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
- ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset))
- << "Failed at index " << i;
- ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
- }
-}
-
-TYPED_TEST_P(DwarfEhFrameTest, GetCieFde32) {
- this->eh_frame_->TestSetOffset(0x4000);
-
- // CIE 32 information.
- this->memory_.SetData32(0xf000, 0x100);
- this->memory_.SetData32(0xf004, 0);
- this->memory_.SetData8(0xf008, 0x1);
- this->memory_.SetData8(0xf009, '\0');
- this->memory_.SetData8(0xf00a, 4);
- this->memory_.SetData8(0xf00b, 8);
- this->memory_.SetData8(0xf00c, 0x20);
-
- // FDE 32 information.
- this->memory_.SetData32(0x14000, 0x20);
- this->memory_.SetData32(0x14004, 0x5004);
- this->memory_.SetData32(0x14008, 0x9000);
- this->memory_.SetData32(0x1400c, 0x100);
-
- const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x14000);
+ const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x5100);
ASSERT_TRUE(fde != nullptr);
- EXPECT_EQ(0x14010U, fde->cfa_instructions_offset);
- EXPECT_EQ(0x14024U, fde->cfa_instructions_end);
- EXPECT_EQ(0x1d008U, fde->pc_start);
- EXPECT_EQ(0x1d108U, fde->pc_end);
- EXPECT_EQ(0xf000U, fde->cie_offset);
+ EXPECT_EQ(0x5000U, fde->cie_offset);
+ EXPECT_EQ(0x5124U, fde->cfa_instructions_offset);
+ EXPECT_EQ(0x5208U, fde->cfa_instructions_end);
+ EXPECT_EQ(0x6618U, fde->pc_start);
+ EXPECT_EQ(0x6818U, fde->pc_end);
EXPECT_EQ(0U, fde->lsda_address);
- ASSERT_TRUE(fde->cie != nullptr);
- EXPECT_EQ(1U, fde->cie->version);
- EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding);
- EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
- EXPECT_EQ(0U, fde->cie->segment_size);
- EXPECT_EQ(1U, fde->cie->augmentation_string.size());
- EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
- EXPECT_EQ(0U, fde->cie->personality_handler);
- EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset);
- EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end);
- EXPECT_EQ(4U, fde->cie->code_alignment_factor);
- EXPECT_EQ(8, fde->cie->data_alignment_factor);
- EXPECT_EQ(0x20U, fde->cie->return_address_register);
+ const DwarfCie* cie = fde->cie;
+ ASSERT_TRUE(cie != nullptr);
+ EXPECT_EQ(1U, cie->version);
+ EXPECT_EQ(DW_EH_PE_sdata8, cie->fde_address_encoding);
+ EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
+ EXPECT_EQ(0U, cie->segment_size);
+ EXPECT_EQ('\0', cie->augmentation_string[0]);
+ EXPECT_EQ(0U, cie->personality_handler);
+ EXPECT_EQ(0x5019U, cie->cfa_instructions_offset);
+ EXPECT_EQ(0x5108U, cie->cfa_instructions_end);
+ EXPECT_EQ(16U, cie->code_alignment_factor);
+ EXPECT_EQ(32U, cie->data_alignment_factor);
+ EXPECT_EQ(1U, cie->return_address_register);
}
-TYPED_TEST_P(DwarfEhFrameTest, GetCieFde64) {
- this->eh_frame_->TestSetOffset(0x2000);
-
- // CIE 64 information.
- this->memory_.SetData32(0x6000, 0xffffffff);
- this->memory_.SetData64(0x6004, 0x100);
- this->memory_.SetData64(0x600c, 0);
- this->memory_.SetData8(0x6014, 0x1);
- this->memory_.SetData8(0x6015, '\0');
- this->memory_.SetData8(0x6016, 4);
- this->memory_.SetData8(0x6017, 8);
- this->memory_.SetData8(0x6018, 0x20);
-
- // FDE 64 information.
- this->memory_.SetData32(0x8000, 0xffffffff);
- this->memory_.SetData64(0x8004, 0x200);
- this->memory_.SetData64(0x800c, 0x200c);
- this->memory_.SetData64(0x8014, 0x5000);
- this->memory_.SetData64(0x801c, 0x300);
-
- const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x8000);
- ASSERT_TRUE(fde != nullptr);
- EXPECT_EQ(0x8024U, fde->cfa_instructions_offset);
- EXPECT_EQ(0x820cU, fde->cfa_instructions_end);
- EXPECT_EQ(0xd018U, fde->pc_start);
- EXPECT_EQ(0xd318U, fde->pc_end);
- EXPECT_EQ(0x6000U, fde->cie_offset);
- EXPECT_EQ(0U, fde->lsda_address);
-
- ASSERT_TRUE(fde->cie != nullptr);
- EXPECT_EQ(1U, fde->cie->version);
- EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding);
- EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
- EXPECT_EQ(0U, fde->cie->segment_size);
- EXPECT_EQ(1U, fde->cie->augmentation_string.size());
- EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
- EXPECT_EQ(0U, fde->cie->personality_handler);
- EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset);
- EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end);
- EXPECT_EQ(4U, fde->cie->code_alignment_factor);
- EXPECT_EQ(8, fde->cie->data_alignment_factor);
- EXPECT_EQ(0x20U, fde->cie->return_address_register);
-}
-
-REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameTest, Init32, Init32_fde_not_following_cie, Init64,
- Init64_fde_not_following_cie, Init_version1, Init_version4,
- GetFdeOffsetFromPc, GetCieFde32, GetCieFde64);
+REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameTest, GetFdeCieFromOffset32, GetFdeCieFromOffset64);
typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameTestTypes;
INSTANTIATE_TYPED_TEST_CASE_P(, DwarfEhFrameTest, DwarfEhFrameTestTypes);
diff --git a/libunwindstack/tests/DwarfEhFrameWithHdrTest.cpp b/libunwindstack/tests/DwarfEhFrameWithHdrTest.cpp
index 4240419..910ae36 100644
--- a/libunwindstack/tests/DwarfEhFrameWithHdrTest.cpp
+++ b/libunwindstack/tests/DwarfEhFrameWithHdrTest.cpp
@@ -30,10 +30,10 @@
namespace unwindstack {
template <typename TypeParam>
-class MockDwarfEhFrameWithHdr : public DwarfEhFrameWithHdr<TypeParam> {
+class TestDwarfEhFrameWithHdr : public DwarfEhFrameWithHdr<TypeParam> {
public:
- MockDwarfEhFrameWithHdr(Memory* memory) : DwarfEhFrameWithHdr<TypeParam>(memory) {}
- ~MockDwarfEhFrameWithHdr() = default;
+ TestDwarfEhFrameWithHdr(Memory* memory) : DwarfEhFrameWithHdr<TypeParam>(memory) {}
+ ~TestDwarfEhFrameWithHdr() = default;
void TestSetTableEncoding(uint8_t encoding) { this->table_encoding_ = encoding; }
void TestSetEntriesOffset(uint64_t offset) { this->entries_offset_ = offset; }
@@ -64,14 +64,14 @@
protected:
void SetUp() override {
memory_.Clear();
- eh_frame_ = new MockDwarfEhFrameWithHdr<TypeParam>(&memory_);
+ eh_frame_ = new TestDwarfEhFrameWithHdr<TypeParam>(&memory_);
ResetLogs();
}
void TearDown() override { delete eh_frame_; }
MemoryFake memory_;
- MockDwarfEhFrameWithHdr<TypeParam>* eh_frame_ = nullptr;
+ TestDwarfEhFrameWithHdr<TypeParam>* eh_frame_ = nullptr;
};
TYPED_TEST_CASE_P(DwarfEhFrameWithHdrTest);
@@ -83,7 +83,7 @@
this->memory_.SetData16(0x1004, 0x500);
this->memory_.SetData32(0x1006, 126);
- ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100));
+ ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100, 0));
EXPECT_EQ(1U, this->eh_frame_->TestGetVersion());
EXPECT_EQ(DW_EH_PE_udata2, this->eh_frame_->TestGetPtrEncoding());
EXPECT_EQ(DW_EH_PE_sdata4, this->eh_frame_->TestGetTableEncoding());
@@ -97,19 +97,119 @@
// Verify a zero fde count fails to init.
this->memory_.SetData32(0x1006, 0);
- ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100));
+ ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100, 0));
ASSERT_EQ(DWARF_ERROR_NO_FDES, this->eh_frame_->LastErrorCode());
// Verify an unexpected version will cause a fail.
this->memory_.SetData32(0x1006, 126);
this->memory_.SetData8(0x1000, 0);
- ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100));
+ ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100, 0));
ASSERT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->eh_frame_->LastErrorCode());
this->memory_.SetData8(0x1000, 2);
- ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100));
+ ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100, 0));
ASSERT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->eh_frame_->LastErrorCode());
}
+TYPED_TEST_P(DwarfEhFrameWithHdrTest, Init_non_zero_load_bias) {
+ this->memory_.SetMemory(0x1000, std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata4,
+ DW_EH_PE_pcrel | DW_EH_PE_sdata4});
+ this->memory_.SetData16(0x1004, 0x500);
+ this->memory_.SetData32(0x1006, 1);
+ this->memory_.SetData32(0x100a, 0x2500);
+ this->memory_.SetData32(0x100e, 0x1400);
+
+ // CIE 32 information.
+ this->memory_.SetData32(0x1300, 0xfc);
+ this->memory_.SetData32(0x1304, 0);
+ this->memory_.SetMemory(0x1308, std::vector<uint8_t>{1, 'z', 'R', '\0', 0, 0, 0, 0, 0x1b});
+
+ // FDE 32 information.
+ this->memory_.SetData32(0x1400, 0xfc);
+ this->memory_.SetData32(0x1404, 0x104);
+ this->memory_.SetData32(0x1408, 0x10f8);
+ this->memory_.SetData32(0x140c, 0x200);
+ this->memory_.SetData16(0x1410, 0);
+
+ ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100, 0x2000));
+ EXPECT_EQ(1U, this->eh_frame_->TestGetVersion());
+ EXPECT_EQ(DW_EH_PE_udata2, this->eh_frame_->TestGetPtrEncoding());
+ EXPECT_EQ(0x1b, this->eh_frame_->TestGetTableEncoding());
+ EXPECT_EQ(4U, this->eh_frame_->TestGetTableEntrySize());
+ EXPECT_EQ(1U, this->eh_frame_->TestGetFdeCount());
+ EXPECT_EQ(0x500U, this->eh_frame_->TestGetPtrOffset());
+ EXPECT_EQ(0x100aU, this->eh_frame_->TestGetEntriesOffset());
+ EXPECT_EQ(0x1100U, this->eh_frame_->TestGetEntriesEnd());
+ EXPECT_EQ(0x1000U, this->eh_frame_->TestGetEntriesDataOffset());
+ EXPECT_EQ(0x100aU, this->eh_frame_->TestGetCurEntriesOffset());
+
+ const DwarfFde* fde = this->eh_frame_->GetFdeFromPc(0x4600);
+ ASSERT_TRUE(fde != nullptr);
+ EXPECT_EQ(0x4500U, fde->pc_start);
+ EXPECT_EQ(0x4700U, fde->pc_end);
+}
+
+TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdes) {
+ this->memory_.SetMemory(
+ 0x1000, std::vector<uint8_t>{1, DW_EH_PE_udata2, DW_EH_PE_udata4, DW_EH_PE_sdata4});
+ this->memory_.SetData16(0x1004, 0x500);
+ this->memory_.SetData32(0x1006, 4);
+
+ // Header information.
+ this->memory_.SetData32(0x100a, 0x4600);
+ this->memory_.SetData32(0x100e, 0x1500);
+ this->memory_.SetData32(0x1012, 0x5500);
+ this->memory_.SetData32(0x1016, 0x1400);
+ this->memory_.SetData32(0x101a, 0x6800);
+ this->memory_.SetData32(0x101e, 0x1700);
+ this->memory_.SetData32(0x1022, 0x7700);
+ this->memory_.SetData32(0x1026, 0x1600);
+
+ // CIE 32 information.
+ this->memory_.SetData32(0x1300, 0xfc);
+ this->memory_.SetData32(0x1304, 0);
+ this->memory_.SetMemory(0x1308, std::vector<uint8_t>{1, '\0', 0, 0, 0});
+
+ // FDE 32 information.
+ // pc 0x5500 - 0x5700
+ this->memory_.SetData32(0x1400, 0xfc);
+ this->memory_.SetData32(0x1404, 0x104);
+ this->memory_.SetData32(0x1408, 0x40f8);
+ this->memory_.SetData32(0x140c, 0x200);
+
+ // pc 0x4600 - 0x4800
+ this->memory_.SetData32(0x1500, 0xfc);
+ this->memory_.SetData32(0x1504, 0x204);
+ this->memory_.SetData32(0x1508, 0x30f8);
+ this->memory_.SetData32(0x150c, 0x200);
+
+ // pc 0x7700 - 0x7900
+ this->memory_.SetData32(0x1600, 0xfc);
+ this->memory_.SetData32(0x1604, 0x304);
+ this->memory_.SetData32(0x1608, 0x60f8);
+ this->memory_.SetData32(0x160c, 0x200);
+
+ // pc 0x6800 - 0x6a00
+ this->memory_.SetData32(0x1700, 0xfc);
+ this->memory_.SetData32(0x1704, 0x404);
+ this->memory_.SetData32(0x1708, 0x50f8);
+ this->memory_.SetData32(0x170c, 0x200);
+
+ ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100, 0));
+
+ std::vector<const DwarfFde*> fdes;
+ this->eh_frame_->GetFdes(&fdes);
+ ASSERT_EQ(4U, fdes.size());
+
+ EXPECT_EQ(0x4600U, fdes[0]->pc_start);
+ EXPECT_EQ(0x4800U, fdes[0]->pc_end);
+ EXPECT_EQ(0x5500U, fdes[1]->pc_start);
+ EXPECT_EQ(0x5700U, fdes[1]->pc_end);
+ EXPECT_EQ(0x6800U, fdes[2]->pc_start);
+ EXPECT_EQ(0x6a00U, fdes[2]->pc_end);
+ EXPECT_EQ(0x7700U, fdes[3]->pc_start);
+ EXPECT_EQ(0x7900U, fdes[3]->pc_end);
+}
+
TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_expect_cache_fail) {
this->eh_frame_->TestSetTableEntrySize(0x10);
this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
@@ -123,6 +223,7 @@
EXPECT_EQ(0x1000U, this->eh_frame_->LastErrorAddress());
}
+// We are assuming that pc rel, is really relative to the load_bias.
TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_read_pcrel) {
this->eh_frame_->TestSetTableEncoding(DW_EH_PE_pcrel | DW_EH_PE_udata4);
this->eh_frame_->TestSetEntriesOffset(0x1000);
@@ -134,8 +235,8 @@
auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
ASSERT_TRUE(info != nullptr);
- EXPECT_EQ(0x1380U, info->pc);
- EXPECT_EQ(0x1540U, info->offset);
+ EXPECT_EQ(0x340U, info->pc);
+ EXPECT_EQ(0x500U, info->offset);
}
TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_read_datarel) {
@@ -340,11 +441,7 @@
// CIE 32 information.
this->memory_.SetData32(0xf000, 0x100);
this->memory_.SetData32(0xf004, 0);
- this->memory_.SetData8(0xf008, 0x1);
- this->memory_.SetData8(0xf009, '\0');
- this->memory_.SetData8(0xf00a, 4);
- this->memory_.SetData8(0xf00b, 8);
- this->memory_.SetData8(0xf00c, 0x20);
+ this->memory_.SetMemory(0xf008, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
// FDE 32 information.
this->memory_.SetData32(0x14000, 0x20);
@@ -381,11 +478,7 @@
this->memory_.SetData32(0x6000, 0xffffffff);
this->memory_.SetData64(0x6004, 0x100);
this->memory_.SetData64(0x600c, 0);
- this->memory_.SetData8(0x6014, 0x1);
- this->memory_.SetData8(0x6015, '\0');
- this->memory_.SetData8(0x6016, 4);
- this->memory_.SetData8(0x6017, 8);
- this->memory_.SetData8(0x6018, 0x20);
+ this->memory_.SetMemory(0x6014, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
// FDE 64 information.
this->memory_.SetData32(0x8000, 0xffffffff);
@@ -430,14 +523,14 @@
ASSERT_EQ(nullptr, this->eh_frame_->GetFdeFromPc(0x800));
}
-REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameWithHdrTest, Init, GetFdeInfoFromIndex_expect_cache_fail,
- GetFdeInfoFromIndex_read_pcrel, GetFdeInfoFromIndex_read_datarel,
- GetFdeInfoFromIndex_cached, GetFdeOffsetBinary_verify,
- GetFdeOffsetBinary_index_fail, GetFdeOffsetSequential,
- GetFdeOffsetSequential_last_element, GetFdeOffsetSequential_end_check,
- GetFdeOffsetFromPc_fail_fde_count, GetFdeOffsetFromPc_binary_search,
- GetFdeOffsetFromPc_sequential_search, GetCieFde32, GetCieFde64,
- GetFdeFromPc_fde_not_found);
+REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameWithHdrTest, Init, Init_non_zero_load_bias, GetFdes,
+ GetFdeInfoFromIndex_expect_cache_fail, GetFdeInfoFromIndex_read_pcrel,
+ GetFdeInfoFromIndex_read_datarel, GetFdeInfoFromIndex_cached,
+ GetFdeOffsetBinary_verify, GetFdeOffsetBinary_index_fail,
+ GetFdeOffsetSequential, GetFdeOffsetSequential_last_element,
+ GetFdeOffsetSequential_end_check, GetFdeOffsetFromPc_fail_fde_count,
+ GetFdeOffsetFromPc_binary_search, GetFdeOffsetFromPc_sequential_search,
+ GetCieFde32, GetCieFde64, GetFdeFromPc_fde_not_found);
typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameWithHdrTestTypes;
INSTANTIATE_TYPED_TEST_CASE_P(, DwarfEhFrameWithHdrTest, DwarfEhFrameWithHdrTestTypes);
diff --git a/libunwindstack/tests/DwarfOpTest.cpp b/libunwindstack/tests/DwarfOpTest.cpp
index 6e15227..d424d5f 100644
--- a/libunwindstack/tests/DwarfOpTest.cpp
+++ b/libunwindstack/tests/DwarfOpTest.cpp
@@ -1468,7 +1468,7 @@
}
this->op_memory_.SetMemory(0, opcode_buffer);
- RegsImplFake<TypeParam> regs(32, 10);
+ RegsImplFake<TypeParam> regs(32);
for (size_t i = 0; i < 32; i++) {
regs[i] = i + 10;
}
@@ -1499,7 +1499,7 @@
};
this->op_memory_.SetMemory(0, opcode_buffer);
- RegsImplFake<TypeParam> regs(16, 10);
+ RegsImplFake<TypeParam> regs(16);
for (size_t i = 0; i < 16; i++) {
regs[i] = i + 10;
}
@@ -1526,7 +1526,7 @@
0x92, 0x80, 0x15, 0x80, 0x02};
this->op_memory_.SetMemory(0, opcode_buffer);
- RegsImplFake<TypeParam> regs(10, 10);
+ RegsImplFake<TypeParam> regs(10);
regs[5] = 0x45;
regs[6] = 0x190;
RegsInfo<TypeParam> regs_info(®s);
diff --git a/libunwindstack/tests/DwarfSectionImplTest.cpp b/libunwindstack/tests/DwarfSectionImplTest.cpp
index 37305b2..46f555a 100644
--- a/libunwindstack/tests/DwarfSectionImplTest.cpp
+++ b/libunwindstack/tests/DwarfSectionImplTest.cpp
@@ -16,7 +16,6 @@
#include <stdint.h>
-#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <unwindstack/DwarfError.h>
@@ -31,42 +30,27 @@
namespace unwindstack {
template <typename TypeParam>
-class MockDwarfSectionImpl : public DwarfSectionImpl<TypeParam> {
+class TestDwarfSectionImpl : public DwarfSectionImpl<TypeParam> {
public:
- MockDwarfSectionImpl(Memory* memory) : DwarfSectionImpl<TypeParam>(memory) {}
- virtual ~MockDwarfSectionImpl() = default;
+ TestDwarfSectionImpl(Memory* memory) : DwarfSectionImpl<TypeParam>(memory) {}
+ virtual ~TestDwarfSectionImpl() = default;
- MOCK_METHOD2(Init, bool(uint64_t, uint64_t));
+ bool Init(uint64_t, uint64_t, uint64_t) override { return false; }
- MOCK_METHOD2(GetFdeOffsetFromPc, bool(uint64_t, uint64_t*));
+ void GetFdes(std::vector<const DwarfFde*>*) override {}
- MOCK_METHOD1(GetFdeFromIndex, const DwarfFde*(size_t));
+ const DwarfFde* GetFdeFromPc(uint64_t) override { return nullptr; }
- MOCK_METHOD1(GetCieOffsetFromFde32, uint64_t(uint32_t));
+ uint64_t GetCieOffsetFromFde32(uint32_t) { return 0; }
- MOCK_METHOD1(GetCieOffsetFromFde64, uint64_t(uint64_t));
+ uint64_t GetCieOffsetFromFde64(uint64_t) { return 0; }
- MOCK_METHOD1(AdjustPcFromFde, uint64_t(uint64_t));
-
- void TestSetCie32Value(uint32_t value32) { this->cie32_value_ = value32; }
-
- void TestSetCie64Value(uint64_t value64) { this->cie64_value_ = value64; }
-
- void TestSetCachedCieEntry(uint64_t offset, const DwarfCie& cie) {
- this->cie_entries_[offset] = cie;
- }
- void TestClearCachedCieEntry() { this->cie_entries_.clear(); }
-
- void TestSetCachedFdeEntry(uint64_t offset, const DwarfFde& fde) {
- this->fde_entries_[offset] = fde;
- }
- void TestClearCachedFdeEntry() { this->fde_entries_.clear(); }
+ uint64_t AdjustPcFromFde(uint64_t) override { return 0; }
void TestSetCachedCieLocRegs(uint64_t offset, const dwarf_loc_regs_t& loc_regs) {
this->cie_loc_regs_[offset] = loc_regs;
}
void TestClearCachedCieLocRegs() { this->cie_loc_regs_.clear(); }
-
void TestClearError() { this->last_error_.code = DWARF_ERROR_NONE; }
};
@@ -75,31 +59,51 @@
protected:
void SetUp() override {
memory_.Clear();
- section_ = new MockDwarfSectionImpl<TypeParam>(&memory_);
+ section_ = new TestDwarfSectionImpl<TypeParam>(&memory_);
ResetLogs();
- section_->TestSetCie32Value(static_cast<uint32_t>(-1));
- section_->TestSetCie64Value(static_cast<uint64_t>(-1));
}
void TearDown() override { delete section_; }
MemoryFake memory_;
- MockDwarfSectionImpl<TypeParam>* section_ = nullptr;
+ TestDwarfSectionImpl<TypeParam>* section_ = nullptr;
};
TYPED_TEST_CASE_P(DwarfSectionImplTest);
// NOTE: All test class variables need to be referenced as this->.
+TYPED_TEST_P(DwarfSectionImplTest, GetCieFromOffset_fail_should_not_cache) {
+ ASSERT_TRUE(this->section_->GetCieFromOffset(0x4000) == nullptr);
+ EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
+ EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
+
+ this->section_->TestClearError();
+ ASSERT_TRUE(this->section_->GetCieFromOffset(0x4000) == nullptr);
+ EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
+ EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
+}
+
+TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_fail_should_not_cache) {
+ ASSERT_TRUE(this->section_->GetFdeFromOffset(0x4000) == nullptr);
+ EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
+ EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
+
+ this->section_->TestClearError();
+ ASSERT_TRUE(this->section_->GetFdeFromOffset(0x4000) == nullptr);
+ EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
+ EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
+}
+
TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr_eval_fail) {
DwarfCie cie{.version = 3, .return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
regs.set_sp(0x2000);
regs[5] = 0x20;
regs[9] = 0x3000;
- loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x2, 0x5002}};
+ loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x2, 0x5002}};
bool finished;
ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished));
EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
@@ -108,7 +112,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr_no_stack) {
DwarfCie cie{.version = 3, .return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -116,7 +120,7 @@
regs[5] = 0x20;
regs[9] = 0x3000;
this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x96, 0x96, 0x96});
- loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x2, 0x5002}};
+ loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x2, 0x5002}};
bool finished;
ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished));
EXPECT_EQ(DWARF_ERROR_ILLEGAL_STATE, this->section_->LastErrorCode());
@@ -124,7 +128,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr) {
DwarfCie cie{.version = 3, .return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -136,15 +140,13 @@
this->memory_.SetMemory(0x80000000, &cfa_value, sizeof(cfa_value));
loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x4, 0x5004}};
bool finished;
- ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished));
- EXPECT_FALSE(finished);
- EXPECT_EQ(0x12345U, regs.sp());
- EXPECT_EQ(0x20U, regs.pc());
+ ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished));
+ EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode());
}
TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_val_expr) {
DwarfCie cie{.version = 3, .return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -162,7 +164,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr_is_register) {
DwarfCie cie{.version = 3, .return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -170,7 +172,7 @@
regs[5] = 0x20;
regs[9] = 0x3000;
this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x50, 0x96, 0x96});
- loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x2, 0x5002}};
+ loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x2, 0x5002}};
bool finished;
ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished));
EXPECT_EQ(DWARF_ERROR_NOT_IMPLEMENTED, this->section_->LastErrorCode());
@@ -178,7 +180,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_bad_regs) {
DwarfCie cie{.return_address_register = 60};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
bool finished;
@@ -188,7 +190,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_no_cfa) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
bool finished;
@@ -198,7 +200,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_bad) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {20, 0}};
@@ -227,7 +229,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_register_prev) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -239,12 +241,12 @@
ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished));
EXPECT_FALSE(finished);
EXPECT_EQ(0x20U, regs.pc());
- EXPECT_EQ(0x2000U, regs.sp());
+ EXPECT_EQ(0x3000U, regs.sp());
}
TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_register_from_value) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -262,7 +264,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_double_indirection) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -283,7 +285,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_register_reference_chain) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -314,7 +316,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_dex_pc) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -333,7 +335,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_invalid_register) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -348,7 +350,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_different_reg_locations) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
if (sizeof(TypeParam) == sizeof(uint64_t)) {
@@ -382,7 +384,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_return_address_undefined) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -400,7 +402,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_pc_zero) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -417,7 +419,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_return_address) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -434,7 +436,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_ignore_large_reg_loc) {
DwarfCie cie{.return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -453,7 +455,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_reg_expr) {
DwarfCie cie{.version = 3, .return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -473,7 +475,7 @@
TYPED_TEST_P(DwarfSectionImplTest, Eval_reg_val_expr) {
DwarfCie cie{.version = 3, .return_address_register = 5};
- RegsImplFake<TypeParam> regs(10, 9);
+ RegsImplFake<TypeParam> regs(10);
dwarf_loc_regs_t loc_regs;
regs.set_pc(0x100);
@@ -489,334 +491,6 @@
EXPECT_EQ(0x80000000U, regs.pc());
}
-TYPED_TEST_P(DwarfSectionImplTest, GetCie_fail_should_not_cache) {
- ASSERT_TRUE(this->section_->GetCie(0x4000) == nullptr);
- EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
- EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
- this->section_->TestClearError();
- ASSERT_TRUE(this->section_->GetCie(0x4000) == nullptr);
- EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
- EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetCie_32_version_check) {
- this->memory_.SetData32(0x5000, 0x100);
- this->memory_.SetData32(0x5004, 0xffffffff);
- this->memory_.SetData8(0x5008, 0x1);
- this->memory_.SetData8(0x5009, '\0');
- this->memory_.SetData8(0x500a, 4);
- this->memory_.SetData8(0x500b, 8);
- this->memory_.SetData8(0x500c, 0x20);
-
- const DwarfCie* cie = this->section_->GetCie(0x5000);
- ASSERT_TRUE(cie != nullptr);
- EXPECT_EQ(1U, cie->version);
- EXPECT_EQ(DW_EH_PE_sdata4, cie->fde_address_encoding);
- EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
- EXPECT_EQ(0U, cie->segment_size);
- EXPECT_EQ(1U, cie->augmentation_string.size());
- EXPECT_EQ('\0', cie->augmentation_string[0]);
- EXPECT_EQ(0U, cie->personality_handler);
- EXPECT_EQ(0x500dU, cie->cfa_instructions_offset);
- EXPECT_EQ(0x5104U, cie->cfa_instructions_end);
- EXPECT_EQ(4U, cie->code_alignment_factor);
- EXPECT_EQ(8, cie->data_alignment_factor);
- EXPECT_EQ(0x20U, cie->return_address_register);
- EXPECT_EQ(DWARF_ERROR_NONE, this->section_->LastErrorCode());
-
- this->section_->TestClearCachedCieEntry();
- // Set version to 0, 2, 5 and verify we fail.
- this->memory_.SetData8(0x5008, 0x0);
- this->section_->TestClearError();
- ASSERT_TRUE(this->section_->GetCie(0x5000) == nullptr);
- EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->section_->LastErrorCode());
-
- this->memory_.SetData8(0x5008, 0x2);
- this->section_->TestClearError();
- ASSERT_TRUE(this->section_->GetCie(0x5000) == nullptr);
- EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->section_->LastErrorCode());
-
- this->memory_.SetData8(0x5008, 0x5);
- this->section_->TestClearError();
- ASSERT_TRUE(this->section_->GetCie(0x5000) == nullptr);
- EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->section_->LastErrorCode());
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetCie_negative_data_alignment_factor) {
- this->memory_.SetData32(0x5000, 0x100);
- this->memory_.SetData32(0x5004, 0xffffffff);
- this->memory_.SetData8(0x5008, 0x1);
- this->memory_.SetData8(0x5009, '\0');
- this->memory_.SetData8(0x500a, 4);
- this->memory_.SetMemory(0x500b, std::vector<uint8_t>{0xfc, 0xff, 0xff, 0xff, 0x7f});
- this->memory_.SetData8(0x5010, 0x20);
-
- const DwarfCie* cie = this->section_->GetCie(0x5000);
- ASSERT_TRUE(cie != nullptr);
- EXPECT_EQ(1U, cie->version);
- EXPECT_EQ(DW_EH_PE_sdata4, cie->fde_address_encoding);
- EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
- EXPECT_EQ(0U, cie->segment_size);
- EXPECT_EQ(1U, cie->augmentation_string.size());
- EXPECT_EQ('\0', cie->augmentation_string[0]);
- EXPECT_EQ(0U, cie->personality_handler);
- EXPECT_EQ(0x5011U, cie->cfa_instructions_offset);
- EXPECT_EQ(0x5104U, cie->cfa_instructions_end);
- EXPECT_EQ(4U, cie->code_alignment_factor);
- EXPECT_EQ(-4, cie->data_alignment_factor);
- EXPECT_EQ(0x20U, cie->return_address_register);
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetCie_64_no_augment) {
- this->memory_.SetData32(0x8000, 0xffffffff);
- this->memory_.SetData64(0x8004, 0x200);
- this->memory_.SetData64(0x800c, 0xffffffffffffffffULL);
- this->memory_.SetData8(0x8014, 0x1);
- this->memory_.SetData8(0x8015, '\0');
- this->memory_.SetData8(0x8016, 4);
- this->memory_.SetData8(0x8017, 8);
- this->memory_.SetData8(0x8018, 0x20);
-
- const DwarfCie* cie = this->section_->GetCie(0x8000);
- ASSERT_TRUE(cie != nullptr);
- EXPECT_EQ(1U, cie->version);
- EXPECT_EQ(DW_EH_PE_sdata8, cie->fde_address_encoding);
- EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
- EXPECT_EQ(0U, cie->segment_size);
- EXPECT_EQ(1U, cie->augmentation_string.size());
- EXPECT_EQ('\0', cie->augmentation_string[0]);
- EXPECT_EQ(0U, cie->personality_handler);
- EXPECT_EQ(0x8019U, cie->cfa_instructions_offset);
- EXPECT_EQ(0x820cU, cie->cfa_instructions_end);
- EXPECT_EQ(4U, cie->code_alignment_factor);
- EXPECT_EQ(8, cie->data_alignment_factor);
- EXPECT_EQ(0x20U, cie->return_address_register);
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetCie_augment) {
- this->memory_.SetData32(0x5000, 0x100);
- this->memory_.SetData32(0x5004, 0xffffffff);
- this->memory_.SetData8(0x5008, 0x1);
- this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'L', 'P', 'R', '\0'});
- this->memory_.SetData8(0x500e, 4);
- this->memory_.SetData8(0x500f, 8);
- this->memory_.SetData8(0x5010, 0x10);
- // Augment length.
- this->memory_.SetData8(0x5011, 0xf);
- // L data.
- this->memory_.SetData8(0x5012, DW_EH_PE_textrel | DW_EH_PE_udata2);
- // P data.
- this->memory_.SetData8(0x5013, DW_EH_PE_udata4);
- this->memory_.SetData32(0x5014, 0x12345678);
- // R data.
- this->memory_.SetData8(0x5018, DW_EH_PE_udata2);
-
- const DwarfCie* cie = this->section_->GetCie(0x5000);
- ASSERT_TRUE(cie != nullptr);
- EXPECT_EQ(1U, cie->version);
- EXPECT_EQ(DW_EH_PE_udata2, cie->fde_address_encoding);
- EXPECT_EQ(DW_EH_PE_textrel | DW_EH_PE_udata2, cie->lsda_encoding);
- EXPECT_EQ(0U, cie->segment_size);
- EXPECT_EQ(5U, cie->augmentation_string.size());
- EXPECT_EQ('z', cie->augmentation_string[0]);
- EXPECT_EQ('L', cie->augmentation_string[1]);
- EXPECT_EQ('P', cie->augmentation_string[2]);
- EXPECT_EQ('R', cie->augmentation_string[3]);
- EXPECT_EQ('\0', cie->augmentation_string[4]);
- EXPECT_EQ(0x12345678U, cie->personality_handler);
- EXPECT_EQ(0x5021U, cie->cfa_instructions_offset);
- EXPECT_EQ(0x5104U, cie->cfa_instructions_end);
- EXPECT_EQ(4U, cie->code_alignment_factor);
- EXPECT_EQ(8, cie->data_alignment_factor);
- EXPECT_EQ(0x10U, cie->return_address_register);
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetCie_version_3) {
- this->memory_.SetData32(0x5000, 0x100);
- this->memory_.SetData32(0x5004, 0xffffffff);
- this->memory_.SetData8(0x5008, 0x3);
- this->memory_.SetData8(0x5009, '\0');
- this->memory_.SetData8(0x500a, 4);
- this->memory_.SetData8(0x500b, 8);
- this->memory_.SetMemory(0x500c, std::vector<uint8_t>{0x81, 0x03});
-
- const DwarfCie* cie = this->section_->GetCie(0x5000);
- ASSERT_TRUE(cie != nullptr);
- EXPECT_EQ(3U, cie->version);
- EXPECT_EQ(DW_EH_PE_sdata4, cie->fde_address_encoding);
- EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
- EXPECT_EQ(0U, cie->segment_size);
- EXPECT_EQ(1U, cie->augmentation_string.size());
- EXPECT_EQ('\0', cie->augmentation_string[0]);
- EXPECT_EQ(0U, cie->personality_handler);
- EXPECT_EQ(0x500eU, cie->cfa_instructions_offset);
- EXPECT_EQ(0x5104U, cie->cfa_instructions_end);
- EXPECT_EQ(4U, cie->code_alignment_factor);
- EXPECT_EQ(8, cie->data_alignment_factor);
- EXPECT_EQ(0x181U, cie->return_address_register);
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetCie_version_4) {
- this->memory_.SetData32(0x5000, 0x100);
- this->memory_.SetData32(0x5004, 0xffffffff);
- this->memory_.SetData8(0x5008, 0x4);
- this->memory_.SetData8(0x5009, '\0');
- this->memory_.SetData8(0x500a, 4);
- this->memory_.SetData8(0x500b, 0x13);
- this->memory_.SetData8(0x500c, 4);
- this->memory_.SetData8(0x500d, 8);
- this->memory_.SetMemory(0x500e, std::vector<uint8_t>{0x81, 0x03});
-
- const DwarfCie* cie = this->section_->GetCie(0x5000);
- ASSERT_TRUE(cie != nullptr);
- EXPECT_EQ(4U, cie->version);
- EXPECT_EQ(DW_EH_PE_sdata4, cie->fde_address_encoding);
- EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
- EXPECT_EQ(0x13U, cie->segment_size);
- EXPECT_EQ(1U, cie->augmentation_string.size());
- EXPECT_EQ('\0', cie->augmentation_string[0]);
- EXPECT_EQ(0U, cie->personality_handler);
- EXPECT_EQ(0x5010U, cie->cfa_instructions_offset);
- EXPECT_EQ(0x5104U, cie->cfa_instructions_end);
- EXPECT_EQ(4U, cie->code_alignment_factor);
- EXPECT_EQ(8, cie->data_alignment_factor);
- EXPECT_EQ(0x181U, cie->return_address_register);
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_fail_should_not_cache) {
- ASSERT_TRUE(this->section_->GetFdeFromOffset(0x4000) == nullptr);
- EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
- EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
- this->section_->TestClearError();
- ASSERT_TRUE(this->section_->GetFdeFromOffset(0x4000) == nullptr);
- EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
- EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_32_no_augment) {
- this->memory_.SetData32(0x4000, 0x20);
- this->memory_.SetData32(0x4004, 0x8000);
- this->memory_.SetData32(0x4008, 0x5000);
- this->memory_.SetData32(0x400c, 0x100);
-
- EXPECT_CALL(*this->section_, GetCieOffsetFromFde32(0x8000)).WillOnce(::testing::Return(0x8000));
- DwarfCie cie{};
- cie.fde_address_encoding = DW_EH_PE_udata4;
- this->section_->TestSetCachedCieEntry(0x8000, cie);
- EXPECT_CALL(*this->section_, AdjustPcFromFde(0x5000)).WillOnce(::testing::Return(0x5000));
-
- const DwarfFde* fde = this->section_->GetFdeFromOffset(0x4000);
- ASSERT_TRUE(fde != nullptr);
- ASSERT_TRUE(fde->cie != nullptr);
- EXPECT_EQ(0x4010U, fde->cfa_instructions_offset);
- EXPECT_EQ(0x4024U, fde->cfa_instructions_end);
- EXPECT_EQ(0x5000U, fde->pc_start);
- EXPECT_EQ(0x5100U, fde->pc_end);
- EXPECT_EQ(0x8000U, fde->cie_offset);
- EXPECT_EQ(0U, fde->lsda_address);
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_32_no_augment_non_zero_segment_size) {
- this->memory_.SetData32(0x4000, 0x30);
- this->memory_.SetData32(0x4004, 0x8000);
- this->memory_.SetData32(0x4018, 0x5000);
- this->memory_.SetData32(0x401c, 0x100);
-
- EXPECT_CALL(*this->section_, GetCieOffsetFromFde32(0x8000)).WillOnce(::testing::Return(0x8000));
- DwarfCie cie{};
- cie.fde_address_encoding = DW_EH_PE_udata4;
- cie.segment_size = 0x10;
- this->section_->TestSetCachedCieEntry(0x8000, cie);
- EXPECT_CALL(*this->section_, AdjustPcFromFde(0x5000)).WillOnce(::testing::Return(0x5000));
-
- const DwarfFde* fde = this->section_->GetFdeFromOffset(0x4000);
- ASSERT_TRUE(fde != nullptr);
- ASSERT_TRUE(fde->cie != nullptr);
- EXPECT_EQ(0x4020U, fde->cfa_instructions_offset);
- EXPECT_EQ(0x4034U, fde->cfa_instructions_end);
- EXPECT_EQ(0x5000U, fde->pc_start);
- EXPECT_EQ(0x5100U, fde->pc_end);
- EXPECT_EQ(0x8000U, fde->cie_offset);
- EXPECT_EQ(0U, fde->lsda_address);
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_32_augment) {
- this->memory_.SetData32(0x4000, 0x100);
- this->memory_.SetData32(0x4004, 0x8000);
- this->memory_.SetData32(0x4008, 0x5000);
- this->memory_.SetData32(0x400c, 0x100);
- this->memory_.SetMemory(0x4010, std::vector<uint8_t>{0x82, 0x01});
- this->memory_.SetData16(0x4012, 0x1234);
-
- EXPECT_CALL(*this->section_, GetCieOffsetFromFde32(0x8000)).WillOnce(::testing::Return(0x8000));
- DwarfCie cie{};
- cie.fde_address_encoding = DW_EH_PE_udata4;
- cie.augmentation_string.push_back('z');
- cie.lsda_encoding = DW_EH_PE_udata2;
- this->section_->TestSetCachedCieEntry(0x8000, cie);
- EXPECT_CALL(*this->section_, AdjustPcFromFde(0x5000)).WillOnce(::testing::Return(0x5000));
-
- const DwarfFde* fde = this->section_->GetFdeFromOffset(0x4000);
- ASSERT_TRUE(fde != nullptr);
- ASSERT_TRUE(fde->cie != nullptr);
- EXPECT_EQ(0x4094U, fde->cfa_instructions_offset);
- EXPECT_EQ(0x4104U, fde->cfa_instructions_end);
- EXPECT_EQ(0x5000U, fde->pc_start);
- EXPECT_EQ(0x5100U, fde->pc_end);
- EXPECT_EQ(0x8000U, fde->cie_offset);
- EXPECT_EQ(0x1234U, fde->lsda_address);
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_64_no_augment) {
- this->memory_.SetData32(0x4000, 0xffffffff);
- this->memory_.SetData64(0x4004, 0x100);
- this->memory_.SetData64(0x400c, 0x12345678);
- this->memory_.SetData32(0x4014, 0x5000);
- this->memory_.SetData32(0x4018, 0x100);
-
- EXPECT_CALL(*this->section_, GetCieOffsetFromFde64(0x12345678))
- .WillOnce(::testing::Return(0x12345678));
- DwarfCie cie{};
- cie.fde_address_encoding = DW_EH_PE_udata4;
- this->section_->TestSetCachedCieEntry(0x12345678, cie);
- EXPECT_CALL(*this->section_, AdjustPcFromFde(0x5000)).WillOnce(::testing::Return(0x5000));
-
- const DwarfFde* fde = this->section_->GetFdeFromOffset(0x4000);
- ASSERT_TRUE(fde != nullptr);
- ASSERT_TRUE(fde->cie != nullptr);
- EXPECT_EQ(0x401cU, fde->cfa_instructions_offset);
- EXPECT_EQ(0x410cU, fde->cfa_instructions_end);
- EXPECT_EQ(0x5000U, fde->pc_start);
- EXPECT_EQ(0x5100U, fde->pc_end);
- EXPECT_EQ(0x12345678U, fde->cie_offset);
- EXPECT_EQ(0U, fde->lsda_address);
-}
-
-TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_cached) {
- DwarfCie cie{};
- cie.fde_address_encoding = DW_EH_PE_udata4;
- cie.augmentation_string.push_back('z');
- cie.lsda_encoding = DW_EH_PE_udata2;
-
- DwarfFde fde_cached{};
- fde_cached.cfa_instructions_offset = 0x1000;
- fde_cached.cfa_instructions_end = 0x1100;
- fde_cached.pc_start = 0x9000;
- fde_cached.pc_end = 0x9400;
- fde_cached.cie_offset = 0x30000;
- fde_cached.cie = &cie;
- this->section_->TestSetCachedFdeEntry(0x6000, fde_cached);
-
- const DwarfFde* fde = this->section_->GetFdeFromOffset(0x6000);
- ASSERT_TRUE(fde != nullptr);
- ASSERT_EQ(&cie, fde->cie);
- EXPECT_EQ(0x1000U, fde->cfa_instructions_offset);
- EXPECT_EQ(0x1100U, fde->cfa_instructions_end);
- EXPECT_EQ(0x9000U, fde->pc_start);
- EXPECT_EQ(0x9400U, fde->pc_end);
- EXPECT_EQ(0x30000U, fde->cie_offset);
-}
-
TYPED_TEST_P(DwarfSectionImplTest, GetCfaLocationInfo_cie_not_cached) {
DwarfCie cie{};
cie.cfa_instructions_offset = 0x3000;
@@ -855,7 +529,8 @@
fde.cfa_instructions_offset = 0x6000;
fde.cfa_instructions_end = 0x6002;
- dwarf_loc_regs_t cie_loc_regs{{6, {DWARF_LOCATION_REGISTER, {4, 0}}}};
+ dwarf_loc_regs_t cie_loc_regs;
+ cie_loc_regs[6] = DwarfLocation{DWARF_LOCATION_REGISTER, {4, 0}};
this->section_->TestSetCachedCieLocRegs(0x8000, cie_loc_regs);
this->memory_.SetMemory(0x6000, std::vector<uint8_t>{0x09, 0x04, 0x03});
@@ -885,7 +560,7 @@
this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x00});
this->memory_.SetMemory(0x6000, std::vector<uint8_t>{0xc2});
- ASSERT_TRUE(this->section_->Log(2, 0x1000, 0x1000, &fde));
+ ASSERT_TRUE(this->section_->Log(2, 0x1000, &fde));
ASSERT_EQ(
"4 unwind DW_CFA_nop\n"
@@ -896,18 +571,16 @@
ASSERT_EQ("", GetFakeLogBuf());
}
-REGISTER_TYPED_TEST_CASE_P(
- DwarfSectionImplTest, Eval_cfa_expr_eval_fail, Eval_cfa_expr_no_stack,
- Eval_cfa_expr_is_register, Eval_cfa_expr, Eval_cfa_val_expr, Eval_bad_regs, Eval_no_cfa,
- Eval_cfa_bad, Eval_cfa_register_prev, Eval_cfa_register_from_value, Eval_double_indirection,
- Eval_register_reference_chain, Eval_dex_pc, Eval_invalid_register, Eval_different_reg_locations,
- Eval_return_address_undefined, Eval_pc_zero, Eval_return_address, Eval_ignore_large_reg_loc,
- Eval_reg_expr, Eval_reg_val_expr, GetCie_fail_should_not_cache, GetCie_32_version_check,
- GetCie_negative_data_alignment_factor, GetCie_64_no_augment, GetCie_augment, GetCie_version_3,
- GetCie_version_4, GetFdeFromOffset_fail_should_not_cache, GetFdeFromOffset_32_no_augment,
- GetFdeFromOffset_32_no_augment_non_zero_segment_size, GetFdeFromOffset_32_augment,
- GetFdeFromOffset_64_no_augment, GetFdeFromOffset_cached, GetCfaLocationInfo_cie_not_cached,
- GetCfaLocationInfo_cie_cached, Log);
+REGISTER_TYPED_TEST_CASE_P(DwarfSectionImplTest, GetCieFromOffset_fail_should_not_cache,
+ GetFdeFromOffset_fail_should_not_cache, Eval_cfa_expr_eval_fail,
+ Eval_cfa_expr_no_stack, Eval_cfa_expr_is_register, Eval_cfa_expr,
+ Eval_cfa_val_expr, Eval_bad_regs, Eval_no_cfa, Eval_cfa_bad,
+ Eval_cfa_register_prev, Eval_cfa_register_from_value,
+ Eval_double_indirection, Eval_register_reference_chain, Eval_dex_pc,
+ Eval_invalid_register, Eval_different_reg_locations,
+ Eval_return_address_undefined, Eval_pc_zero, Eval_return_address,
+ Eval_ignore_large_reg_loc, Eval_reg_expr, Eval_reg_val_expr,
+ GetCfaLocationInfo_cie_not_cached, GetCfaLocationInfo_cie_cached, Log);
typedef ::testing::Types<uint32_t, uint64_t> DwarfSectionImplTestTypes;
INSTANTIATE_TYPED_TEST_CASE_P(, DwarfSectionImplTest, DwarfSectionImplTestTypes);
diff --git a/libunwindstack/tests/DwarfSectionTest.cpp b/libunwindstack/tests/DwarfSectionTest.cpp
index 3fcd2b6..d754fcc 100644
--- a/libunwindstack/tests/DwarfSectionTest.cpp
+++ b/libunwindstack/tests/DwarfSectionTest.cpp
@@ -30,24 +30,18 @@
MockDwarfSection(Memory* memory) : DwarfSection(memory) {}
virtual ~MockDwarfSection() = default;
- MOCK_METHOD4(Log, bool(uint8_t, uint64_t, uint64_t, const DwarfFde*));
+ MOCK_METHOD3(Init, bool(uint64_t, uint64_t, uint64_t));
MOCK_METHOD5(Eval, bool(const DwarfCie*, Memory*, const dwarf_loc_regs_t&, Regs*, bool*));
+ MOCK_METHOD3(Log, bool(uint8_t, uint64_t, const DwarfFde*));
+
+ MOCK_METHOD1(GetFdes, void(std::vector<const DwarfFde*>*));
+
+ MOCK_METHOD1(GetFdeFromPc, const DwarfFde*(uint64_t));
+
MOCK_METHOD3(GetCfaLocationInfo, bool(uint64_t, const DwarfFde*, dwarf_loc_regs_t*));
- MOCK_METHOD2(Init, bool(uint64_t, uint64_t));
-
- MOCK_METHOD2(GetFdeOffsetFromPc, bool(uint64_t, uint64_t*));
-
- MOCK_METHOD1(GetFdeFromOffset, const DwarfFde*(uint64_t));
-
- MOCK_METHOD1(GetFdeFromIndex, const DwarfFde*(size_t));
-
- MOCK_METHOD1(IsCie32, bool(uint32_t));
-
- MOCK_METHOD1(IsCie64, bool(uint64_t));
-
MOCK_METHOD1(GetCieOffsetFromFde32, uint64_t(uint32_t));
MOCK_METHOD1(GetCieOffsetFromFde64, uint64_t(uint64_t));
@@ -57,112 +51,117 @@
class DwarfSectionTest : public ::testing::Test {
protected:
+ void SetUp() override { section_.reset(new MockDwarfSection(&memory_)); }
+
MemoryFake memory_;
+ std::unique_ptr<MockDwarfSection> section_;
};
-TEST_F(DwarfSectionTest, GetFdeOffsetFromPc_fail_from_pc) {
- MockDwarfSection mock_section(&memory_);
-
- EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
- .WillOnce(::testing::Return(false));
-
- // Verify nullptr when GetFdeOffsetFromPc fails.
- ASSERT_TRUE(mock_section.GetFdeFromPc(0x1000) == nullptr);
-}
-
-TEST_F(DwarfSectionTest, GetFdeOffsetFromPc_fail_fde_pc_end) {
- MockDwarfSection mock_section(&memory_);
-
- DwarfFde fde{};
- fde.pc_end = 0x500;
-
- EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
- .WillOnce(::testing::Return(true));
- EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
-
- // Verify nullptr when GetFdeOffsetFromPc fails.
- ASSERT_TRUE(mock_section.GetFdeFromPc(0x1000) == nullptr);
-}
-
-TEST_F(DwarfSectionTest, GetFdeOffsetFromPc_pass) {
- MockDwarfSection mock_section(&memory_);
-
- DwarfFde fde{};
- fde.pc_end = 0x2000;
-
- EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
- .WillOnce(::testing::Return(true));
- EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
-
- // Verify nullptr when GetFdeOffsetFromPc fails.
- ASSERT_EQ(&fde, mock_section.GetFdeFromPc(0x1000));
-}
-
TEST_F(DwarfSectionTest, Step_fail_fde) {
- MockDwarfSection mock_section(&memory_);
-
- EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
- .WillOnce(::testing::Return(false));
+ EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(nullptr));
bool finished;
- ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr, &finished));
+ ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished));
}
TEST_F(DwarfSectionTest, Step_fail_cie_null) {
- MockDwarfSection mock_section(&memory_);
-
DwarfFde fde{};
fde.pc_end = 0x2000;
fde.cie = nullptr;
- EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
- .WillOnce(::testing::Return(true));
- EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
+ EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
bool finished;
- ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr, &finished));
+ ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished));
}
TEST_F(DwarfSectionTest, Step_fail_cfa_location) {
- MockDwarfSection mock_section(&memory_);
-
DwarfCie cie{};
DwarfFde fde{};
fde.pc_end = 0x2000;
fde.cie = &cie;
- EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
- .WillOnce(::testing::Return(true));
- EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
-
- EXPECT_CALL(mock_section, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
+ EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
+ EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
.WillOnce(::testing::Return(false));
bool finished;
- ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr, &finished));
+ ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished));
}
TEST_F(DwarfSectionTest, Step_pass) {
- MockDwarfSection mock_section(&memory_);
-
DwarfCie cie{};
DwarfFde fde{};
fde.pc_end = 0x2000;
fde.cie = &cie;
- EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
- .WillOnce(::testing::Return(true));
- EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
-
- EXPECT_CALL(mock_section, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
+ EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
+ EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
.WillOnce(::testing::Return(true));
MemoryFake process;
- EXPECT_CALL(mock_section, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
+ EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
.WillOnce(::testing::Return(true));
bool finished;
- ASSERT_TRUE(mock_section.Step(0x1000, nullptr, &process, &finished));
+ ASSERT_TRUE(section_->Step(0x1000, nullptr, &process, &finished));
+}
+
+static bool MockGetCfaLocationInfo(::testing::Unused, const DwarfFde* fde,
+ dwarf_loc_regs_t* loc_regs) {
+ loc_regs->pc_start = fde->pc_start;
+ loc_regs->pc_end = fde->pc_end;
+ return true;
+}
+
+TEST_F(DwarfSectionTest, Step_cache) {
+ DwarfCie cie{};
+ DwarfFde fde{};
+ fde.pc_start = 0x500;
+ fde.pc_end = 0x2000;
+ fde.cie = &cie;
+
+ EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
+ EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
+ .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
+
+ MemoryFake process;
+ EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
+ .WillRepeatedly(::testing::Return(true));
+
+ bool finished;
+ ASSERT_TRUE(section_->Step(0x1000, nullptr, &process, &finished));
+ ASSERT_TRUE(section_->Step(0x1000, nullptr, &process, &finished));
+ ASSERT_TRUE(section_->Step(0x1500, nullptr, &process, &finished));
+}
+
+TEST_F(DwarfSectionTest, Step_cache_not_in_pc) {
+ DwarfCie cie{};
+ DwarfFde fde0{};
+ fde0.pc_start = 0x1000;
+ fde0.pc_end = 0x2000;
+ fde0.cie = &cie;
+ EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde0));
+ EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde0, ::testing::_))
+ .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
+
+ MemoryFake process;
+ EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
+ .WillRepeatedly(::testing::Return(true));
+
+ bool finished;
+ ASSERT_TRUE(section_->Step(0x1000, nullptr, &process, &finished));
+
+ DwarfFde fde1{};
+ fde1.pc_start = 0x500;
+ fde1.pc_end = 0x800;
+ fde1.cie = &cie;
+ EXPECT_CALL(*section_, GetFdeFromPc(0x600)).WillOnce(::testing::Return(&fde1));
+ EXPECT_CALL(*section_, GetCfaLocationInfo(0x600, &fde1, ::testing::_))
+ .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
+
+ ASSERT_TRUE(section_->Step(0x600, nullptr, &process, &finished));
+ ASSERT_TRUE(section_->Step(0x700, nullptr, &process, &finished));
}
} // namespace unwindstack
diff --git a/libunwindstack/tests/ElfFake.cpp b/libunwindstack/tests/ElfFake.cpp
index ae9da5e..3d5ddd6 100644
--- a/libunwindstack/tests/ElfFake.cpp
+++ b/libunwindstack/tests/ElfFake.cpp
@@ -32,7 +32,7 @@
std::deque<FunctionData> ElfInterfaceFake::functions_;
std::deque<StepData> ElfInterfaceFake::steps_;
-bool ElfInterfaceFake::GetFunctionName(uint64_t, uint64_t, std::string* name, uint64_t* offset) {
+bool ElfInterfaceFake::GetFunctionName(uint64_t, std::string* name, uint64_t* offset) {
if (functions_.empty()) {
return false;
}
@@ -52,7 +52,7 @@
return true;
}
-bool ElfInterfaceFake::Step(uint64_t, uint64_t, Regs* regs, Memory*, bool* finished) {
+bool ElfInterfaceFake::Step(uint64_t, Regs* regs, Memory*, bool* finished) {
if (steps_.empty()) {
return false;
}
@@ -65,8 +65,8 @@
}
RegsFake* fake_regs = reinterpret_cast<RegsFake*>(regs);
- fake_regs->FakeSetPc(entry.pc);
- fake_regs->FakeSetSp(entry.sp);
+ fake_regs->set_pc(entry.pc);
+ fake_regs->set_sp(entry.sp);
*finished = entry.finished;
return true;
}
diff --git a/libunwindstack/tests/ElfFake.h b/libunwindstack/tests/ElfFake.h
index e232986..a3bf5ce 100644
--- a/libunwindstack/tests/ElfFake.h
+++ b/libunwindstack/tests/ElfFake.h
@@ -67,13 +67,13 @@
virtual ~ElfInterfaceFake() = default;
bool Init(uint64_t*) override { return false; }
- void InitHeaders() override {}
+ void InitHeaders(uint64_t) override {}
bool GetSoname(std::string*) override { return false; }
- bool GetFunctionName(uint64_t, uint64_t, std::string*, uint64_t*) override;
+ bool GetFunctionName(uint64_t, std::string*, uint64_t*) override;
bool GetGlobalVariable(const std::string&, uint64_t*) override;
- bool Step(uint64_t, uint64_t, Regs*, Memory*, bool*) override;
+ bool Step(uint64_t, Regs*, Memory*, bool*) override;
void FakeSetGlobalVariable(const std::string& global, uint64_t offset) {
globals_[global] = offset;
diff --git a/libunwindstack/tests/ElfInterfaceArmTest.cpp b/libunwindstack/tests/ElfInterfaceArmTest.cpp
index 70a52ad..43c6a97 100644
--- a/libunwindstack/tests/ElfInterfaceArmTest.cpp
+++ b/libunwindstack/tests/ElfInterfaceArmTest.cpp
@@ -242,59 +242,21 @@
ASSERT_EQ(0xa020U, entries[4]);
}
-TEST_F(ElfInterfaceArmTest, HandleType_not_arm_exidx) {
+TEST_F(ElfInterfaceArmTest, HandleUnknownType_arm_exidx) {
ElfInterfaceArmFake interface(&memory_);
- ASSERT_FALSE(interface.HandleType(0x1000, PT_NULL, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_LOAD, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_DYNAMIC, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_INTERP, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_NOTE, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_SHLIB, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_PHDR, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_TLS, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_LOOS, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_HIOS, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_LOPROC, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_HIPROC, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_GNU_EH_FRAME, 0));
- ASSERT_FALSE(interface.HandleType(0x1000, PT_GNU_STACK, 0));
-}
-
-TEST_F(ElfInterfaceArmTest, HandleType_arm_exidx) {
- ElfInterfaceArmFake interface(&memory_);
-
- Elf32_Phdr phdr;
interface.FakeSetStartOffset(0x1000);
interface.FakeSetTotalEntries(100);
- phdr.p_vaddr = 0x2000;
- phdr.p_memsz = 0xa00;
- // Verify that if reads fail, we don't set the values but still get true.
- ASSERT_TRUE(interface.HandleType(0x1000, 0x70000001, 0));
- ASSERT_EQ(0x1000U, interface.start_offset());
- ASSERT_EQ(100U, interface.total_entries());
-
- // Verify that if the second read fails, we still don't set the values.
- memory_.SetData32(
- 0x1000 + reinterpret_cast<uint64_t>(&phdr.p_vaddr) - reinterpret_cast<uint64_t>(&phdr),
- phdr.p_vaddr);
- ASSERT_TRUE(interface.HandleType(0x1000, 0x70000001, 0));
+ // Verify that if the type is not the one we want, we don't set the values.
+ interface.HandleUnknownType(0x70000000, 0x2000, 320);
ASSERT_EQ(0x1000U, interface.start_offset());
ASSERT_EQ(100U, interface.total_entries());
// Everything is correct and present.
- memory_.SetData32(
- 0x1000 + reinterpret_cast<uint64_t>(&phdr.p_memsz) - reinterpret_cast<uint64_t>(&phdr),
- phdr.p_memsz);
- ASSERT_TRUE(interface.HandleType(0x1000, 0x70000001, 0));
+ interface.HandleUnknownType(0x70000001, 0x2000, 320);
ASSERT_EQ(0x2000U, interface.start_offset());
- ASSERT_EQ(320U, interface.total_entries());
-
- // Non-zero load bias.
- ASSERT_TRUE(interface.HandleType(0x1000, 0x70000001, 0x1000));
- ASSERT_EQ(0x1000U, interface.start_offset());
- ASSERT_EQ(320U, interface.total_entries());
+ ASSERT_EQ(40U, interface.total_entries());
}
TEST_F(ElfInterfaceArmTest, StepExidx) {
@@ -302,7 +264,7 @@
// FindEntry fails.
bool finished;
- ASSERT_FALSE(interface.StepExidx(0x7000, 0, nullptr, nullptr, &finished));
+ ASSERT_FALSE(interface.StepExidx(0x7000, nullptr, nullptr, &finished));
EXPECT_EQ(ERROR_UNWIND_INFO, interface.LastErrorCode());
// ExtractEntry should fail.
@@ -316,18 +278,18 @@
regs[ARM_REG_LR] = 0x20000;
regs.set_sp(regs[ARM_REG_SP]);
regs.set_pc(0x1234);
- ASSERT_FALSE(interface.StepExidx(0x7000, 0, ®s, &process_memory_, &finished));
+ ASSERT_FALSE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished));
EXPECT_EQ(ERROR_MEMORY_INVALID, interface.LastErrorCode());
EXPECT_EQ(0x1004U, interface.LastErrorAddress());
// Eval should fail.
memory_.SetData32(0x1004, 0x81000000);
- ASSERT_FALSE(interface.StepExidx(0x7000, 0, ®s, &process_memory_, &finished));
+ ASSERT_FALSE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished));
EXPECT_EQ(ERROR_UNWIND_INFO, interface.LastErrorCode());
// Everything should pass.
memory_.SetData32(0x1004, 0x80b0b0b0);
- ASSERT_TRUE(interface.StepExidx(0x7000, 0, ®s, &process_memory_, &finished));
+ ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished));
EXPECT_EQ(ERROR_UNWIND_INFO, interface.LastErrorCode());
ASSERT_FALSE(finished);
ASSERT_EQ(0x1000U, regs.sp());
@@ -336,11 +298,13 @@
ASSERT_EQ(0x20000U, regs[ARM_REG_PC]);
// Load bias is non-zero.
- ASSERT_TRUE(interface.StepExidx(0x8000, 0x1000, ®s, &process_memory_, &finished));
+ interface.set_load_bias(0x1000);
+ ASSERT_TRUE(interface.StepExidx(0x8000, ®s, &process_memory_, &finished));
EXPECT_EQ(ERROR_UNWIND_INFO, interface.LastErrorCode());
// Pc too small.
- ASSERT_FALSE(interface.StepExidx(0x8000, 0x9000, ®s, &process_memory_, &finished));
+ interface.set_load_bias(0x9000);
+ ASSERT_FALSE(interface.StepExidx(0x8000, ®s, &process_memory_, &finished));
EXPECT_EQ(ERROR_UNWIND_INFO, interface.LastErrorCode());
}
@@ -362,7 +326,7 @@
// Everything should pass.
bool finished;
- ASSERT_TRUE(interface.StepExidx(0x7000, 0, ®s, &process_memory_, &finished));
+ ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished));
EXPECT_EQ(ERROR_NONE, interface.LastErrorCode());
ASSERT_FALSE(finished);
ASSERT_EQ(0x10004U, regs.sp());
@@ -386,7 +350,7 @@
regs.set_pc(0x1234);
bool finished;
- ASSERT_TRUE(interface.StepExidx(0x7000, 0, ®s, &process_memory_, &finished));
+ ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished));
EXPECT_EQ(ERROR_NONE, interface.LastErrorCode());
ASSERT_TRUE(finished);
ASSERT_EQ(0x10000U, regs.sp());
@@ -409,7 +373,7 @@
regs.set_pc(0x1234);
bool finished;
- ASSERT_TRUE(interface.StepExidx(0x7000, 0, ®s, &process_memory_, &finished));
+ ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished));
EXPECT_EQ(ERROR_NONE, interface.LastErrorCode());
ASSERT_TRUE(finished);
ASSERT_EQ(0x10000U, regs.sp());
@@ -436,7 +400,7 @@
regs.set_pc(0x1234);
bool finished;
- ASSERT_TRUE(interface.StepExidx(0x7000, 0, ®s, &process_memory_, &finished));
+ ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished));
EXPECT_EQ(ERROR_NONE, interface.LastErrorCode());
ASSERT_TRUE(finished);
ASSERT_EQ(0U, regs.pc());
@@ -449,7 +413,7 @@
regs.set_sp(regs[ARM_REG_SP]);
regs.set_pc(0x1234);
- ASSERT_TRUE(interface.StepExidx(0x7000, 0, ®s, &process_memory_, &finished));
+ ASSERT_TRUE(interface.StepExidx(0x7000, ®s, &process_memory_, &finished));
EXPECT_EQ(ERROR_NONE, interface.LastErrorCode());
ASSERT_TRUE(finished);
ASSERT_EQ(0U, regs.pc());
diff --git a/libunwindstack/tests/ElfInterfaceTest.cpp b/libunwindstack/tests/ElfInterfaceTest.cpp
index bf97e30..9326bff 100644
--- a/libunwindstack/tests/ElfInterfaceTest.cpp
+++ b/libunwindstack/tests/ElfInterfaceTest.cpp
@@ -97,9 +97,15 @@
template <typename ElfType>
void InitHeadersDebugFrameFail();
+ template <typename Ehdr, typename Phdr, typename ElfInterfaceType>
+ void InitProgramHeadersMalformed();
+
template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
void InitSectionHeadersMalformed();
+ template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
+ void InitSectionHeadersMalformedSymData();
+
template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType>
void InitSectionHeaders(uint64_t entry_size);
@@ -116,8 +122,7 @@
template <typename Sym>
void ElfInterfaceTest::InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset,
uint64_t sym_offset, const char* name) {
- Sym sym;
- memset(&sym, 0, sizeof(sym));
+ Sym sym = {};
sym.st_info = STT_FUNC;
sym.st_value = value;
sym.st_size = size;
@@ -132,15 +137,13 @@
void ElfInterfaceTest::SinglePtLoad() {
std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
- Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Ehdr ehdr = {};
ehdr.e_phoff = 0x100;
ehdr.e_phnum = 1;
ehdr.e_phentsize = sizeof(Phdr);
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Phdr phdr;
- memset(&phdr, 0, sizeof(phdr));
+ Phdr phdr = {};
phdr.p_type = PT_LOAD;
phdr.p_vaddr = 0x2000;
phdr.p_memsz = 0x10000;
@@ -172,15 +175,13 @@
void ElfInterfaceTest::MultipleExecutablePtLoads() {
std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
- Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Ehdr ehdr = {};
ehdr.e_phoff = 0x100;
ehdr.e_phnum = 3;
ehdr.e_phentsize = sizeof(Phdr);
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Phdr phdr;
- memset(&phdr, 0, sizeof(phdr));
+ Phdr phdr = {};
phdr.p_type = PT_LOAD;
phdr.p_vaddr = 0x2000;
phdr.p_memsz = 0x10000;
@@ -241,15 +242,13 @@
void ElfInterfaceTest::MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr() {
std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
- Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Ehdr ehdr = {};
ehdr.e_phoff = 0x100;
ehdr.e_phnum = 3;
ehdr.e_phentsize = sizeof(Phdr) + 100;
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Phdr phdr;
- memset(&phdr, 0, sizeof(phdr));
+ Phdr phdr = {};
phdr.p_type = PT_LOAD;
phdr.p_vaddr = 0x2000;
phdr.p_memsz = 0x10000;
@@ -312,15 +311,13 @@
void ElfInterfaceTest::NonExecutablePtLoads() {
std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
- Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Ehdr ehdr = {};
ehdr.e_phoff = 0x100;
ehdr.e_phnum = 3;
ehdr.e_phentsize = sizeof(Phdr);
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Phdr phdr;
- memset(&phdr, 0, sizeof(phdr));
+ Phdr phdr = {};
phdr.p_type = PT_LOAD;
phdr.p_vaddr = 0x2000;
phdr.p_memsz = 0x10000;
@@ -371,17 +368,15 @@
void ElfInterfaceTest::ManyPhdrs() {
std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
- Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Ehdr ehdr = {};
ehdr.e_phoff = 0x100;
ehdr.e_phnum = 7;
ehdr.e_phentsize = sizeof(Phdr);
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Phdr phdr;
uint64_t phdr_offset = 0x100;
- memset(&phdr, 0, sizeof(phdr));
+ Phdr phdr = {};
phdr.p_type = PT_LOAD;
phdr.p_vaddr = 0x2000;
phdr.p_memsz = 0x10000;
@@ -444,18 +439,16 @@
TEST_F(ElfInterfaceTest, elf32_arm) {
ElfInterfaceArm elf_arm(&memory_);
- Elf32_Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Elf32_Ehdr ehdr = {};
ehdr.e_phoff = 0x100;
ehdr.e_phnum = 1;
ehdr.e_phentsize = sizeof(Elf32_Phdr);
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Elf32_Phdr phdr;
- memset(&phdr, 0, sizeof(phdr));
+ Elf32_Phdr phdr = {};
phdr.p_type = PT_ARM_EXIDX;
- phdr.p_vaddr = 0x2000;
- phdr.p_memsz = 16;
+ phdr.p_offset = 0x2000;
+ phdr.p_filesz = 16;
memory_.SetMemory(0x100, &phdr, sizeof(phdr));
// Add arm exidx entries.
@@ -480,8 +473,7 @@
template <typename Ehdr, typename Phdr, typename Shdr, typename Dyn>
void ElfInterfaceTest::SonameInit(SonameTestEnum test_type) {
- Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Ehdr ehdr = {};
ehdr.e_shoff = 0x200;
ehdr.e_shnum = 2;
ehdr.e_shentsize = sizeof(Shdr);
@@ -490,8 +482,7 @@
ehdr.e_phentsize = sizeof(Phdr);
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Shdr shdr;
- memset(&shdr, 0, sizeof(shdr));
+ Shdr shdr = {};
shdr.sh_type = SHT_STRTAB;
if (test_type == SONAME_MISSING_MAP) {
shdr.sh_addr = 0x20100;
@@ -501,8 +492,7 @@
shdr.sh_offset = 0x10000;
memory_.SetMemory(0x200 + sizeof(shdr), &shdr, sizeof(shdr));
- Phdr phdr;
- memset(&phdr, 0, sizeof(phdr));
+ Phdr phdr = {};
phdr.p_type = PT_DYNAMIC;
phdr.p_offset = 0x2000;
phdr.p_memsz = sizeof(Dyn) * 3;
@@ -647,7 +637,7 @@
memory_.SetData32(0x10004, 0x500);
memory_.SetData32(0x10008, 250);
- elf.InitHeaders();
+ elf.InitHeaders(0);
EXPECT_FALSE(elf.eh_frame() == nullptr);
EXPECT_TRUE(elf.debug_frame() == nullptr);
@@ -672,15 +662,14 @@
memory_.SetData32(0x5000, 0xfc);
memory_.SetData32(0x5004, 0xffffffff);
- memory_.SetData8(0x5008, 1);
- memory_.SetData8(0x5009, '\0');
+ memory_.SetMemory(0x5008, std::vector<uint8_t>{1, '\0', 4, 8, 2});
memory_.SetData32(0x5100, 0xfc);
memory_.SetData32(0x5104, 0);
memory_.SetData32(0x5108, 0x1500);
memory_.SetData32(0x510c, 0x200);
- elf.InitHeaders();
+ elf.InitHeaders(0);
EXPECT_TRUE(elf.eh_frame() == nullptr);
EXPECT_FALSE(elf.debug_frame() == nullptr);
@@ -694,62 +683,34 @@
InitHeadersDebugFrame<ElfInterface64Fake>();
}
-template <typename ElfType>
-void ElfInterfaceTest::InitHeadersEhFrameFail() {
- ElfType elf(&memory_);
+template <typename Ehdr, typename Phdr, typename ElfInterfaceType>
+void ElfInterfaceTest::InitProgramHeadersMalformed() {
+ std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_));
- elf.FakeSetEhFrameOffset(0x1000);
- elf.FakeSetEhFrameSize(0x100);
- elf.FakeSetDebugFrameOffset(0);
- elf.FakeSetDebugFrameSize(0);
+ Ehdr ehdr = {};
+ ehdr.e_phoff = 0x100;
+ ehdr.e_phnum = 3;
+ ehdr.e_phentsize = sizeof(Phdr);
+ memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- elf.InitHeaders();
-
- EXPECT_TRUE(elf.eh_frame() == nullptr);
- EXPECT_EQ(0U, elf.eh_frame_offset());
- EXPECT_EQ(static_cast<uint64_t>(-1), elf.eh_frame_size());
- EXPECT_TRUE(elf.debug_frame() == nullptr);
+ uint64_t load_bias = 0;
+ ASSERT_TRUE(elf->Init(&load_bias));
+ EXPECT_EQ(0U, load_bias);
}
-TEST_F(ElfInterfaceTest, init_headers_eh_frame32_fail) {
- InitHeadersEhFrameFail<ElfInterface32Fake>();
+TEST_F(ElfInterfaceTest, init_program_headers_malformed32) {
+ InitProgramHeadersMalformed<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>();
}
-TEST_F(ElfInterfaceTest, init_headers_eh_frame64_fail) {
- InitHeadersEhFrameFail<ElfInterface64Fake>();
-}
-
-template <typename ElfType>
-void ElfInterfaceTest::InitHeadersDebugFrameFail() {
- ElfType elf(&memory_);
-
- elf.FakeSetEhFrameOffset(0);
- elf.FakeSetEhFrameSize(0);
- elf.FakeSetDebugFrameOffset(0x1000);
- elf.FakeSetDebugFrameSize(0x100);
-
- elf.InitHeaders();
-
- EXPECT_TRUE(elf.eh_frame() == nullptr);
- EXPECT_TRUE(elf.debug_frame() == nullptr);
- EXPECT_EQ(0U, elf.debug_frame_offset());
- EXPECT_EQ(static_cast<uint64_t>(-1), elf.debug_frame_size());
-}
-
-TEST_F(ElfInterfaceTest, init_headers_debug_frame32_fail) {
- InitHeadersDebugFrameFail<ElfInterface32Fake>();
-}
-
-TEST_F(ElfInterfaceTest, init_headers_debug_frame64_fail) {
- InitHeadersDebugFrameFail<ElfInterface64Fake>();
+TEST_F(ElfInterfaceTest, init_program_headers_malformed64) {
+ InitProgramHeadersMalformed<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>();
}
template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
void ElfInterfaceTest::InitSectionHeadersMalformed() {
std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_));
- Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Ehdr ehdr = {};
ehdr.e_shoff = 0x1000;
ehdr.e_shnum = 10;
ehdr.e_shentsize = sizeof(Shdr);
@@ -768,23 +729,95 @@
InitSectionHeadersMalformed<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>();
}
+template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
+void ElfInterfaceTest::InitSectionHeadersMalformedSymData() {
+ std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_));
+
+ uint64_t offset = 0x1000;
+
+ Ehdr ehdr = {};
+ ehdr.e_shoff = offset;
+ ehdr.e_shnum = 5;
+ ehdr.e_shentsize = sizeof(Shdr);
+ memory_.SetMemory(0, &ehdr, sizeof(ehdr));
+
+ offset += ehdr.e_shentsize;
+
+ Shdr shdr = {};
+ shdr.sh_type = SHT_SYMTAB;
+ shdr.sh_link = 4;
+ shdr.sh_addr = 0x5000;
+ shdr.sh_offset = 0x5000;
+ shdr.sh_entsize = 0x100;
+ shdr.sh_size = shdr.sh_entsize * 10;
+ memory_.SetMemory(offset, &shdr, sizeof(shdr));
+ offset += ehdr.e_shentsize;
+
+ memset(&shdr, 0, sizeof(shdr));
+ shdr.sh_type = SHT_DYNSYM;
+ shdr.sh_link = 10;
+ shdr.sh_addr = 0x6000;
+ shdr.sh_offset = 0x6000;
+ shdr.sh_entsize = 0x100;
+ shdr.sh_size = shdr.sh_entsize * 10;
+ memory_.SetMemory(offset, &shdr, sizeof(shdr));
+ offset += ehdr.e_shentsize;
+
+ memset(&shdr, 0, sizeof(shdr));
+ shdr.sh_type = SHT_DYNSYM;
+ shdr.sh_link = 2;
+ shdr.sh_addr = 0x6000;
+ shdr.sh_offset = 0x6000;
+ shdr.sh_entsize = 0x100;
+ shdr.sh_size = shdr.sh_entsize * 10;
+ memory_.SetMemory(offset, &shdr, sizeof(shdr));
+ offset += ehdr.e_shentsize;
+
+ // The string data for the entries.
+ memset(&shdr, 0, sizeof(shdr));
+ shdr.sh_type = SHT_STRTAB;
+ shdr.sh_name = 0x20000;
+ shdr.sh_offset = 0xf000;
+ shdr.sh_size = 0x1000;
+ memory_.SetMemory(offset, &shdr, sizeof(shdr));
+ offset += ehdr.e_shentsize;
+
+ uint64_t load_bias = 0;
+ ASSERT_TRUE(elf->Init(&load_bias));
+ EXPECT_EQ(0U, load_bias);
+ EXPECT_EQ(0U, elf->debug_frame_offset());
+ EXPECT_EQ(0U, elf->debug_frame_size());
+ EXPECT_EQ(0U, elf->gnu_debugdata_offset());
+ EXPECT_EQ(0U, elf->gnu_debugdata_size());
+
+ std::string name;
+ uint64_t name_offset;
+ ASSERT_FALSE(elf->GetFunctionName(0x90010, &name, &name_offset));
+}
+
+TEST_F(ElfInterfaceTest, init_section_headers_malformed_symdata32) {
+ InitSectionHeadersMalformedSymData<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>();
+}
+
+TEST_F(ElfInterfaceTest, init_section_headers_malformed_symdata64) {
+ InitSectionHeadersMalformedSymData<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>();
+}
+
template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType>
void ElfInterfaceTest::InitSectionHeaders(uint64_t entry_size) {
std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_));
uint64_t offset = 0x1000;
- Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Ehdr ehdr = {};
ehdr.e_shoff = offset;
- ehdr.e_shnum = 10;
+ ehdr.e_shnum = 5;
ehdr.e_shentsize = entry_size;
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
offset += ehdr.e_shentsize;
- Shdr shdr;
- memset(&shdr, 0, sizeof(shdr));
+ Shdr shdr = {};
shdr.sh_type = SHT_SYMTAB;
shdr.sh_link = 4;
shdr.sh_addr = 0x5000;
@@ -833,10 +866,10 @@
// Look in the first symbol table.
std::string name;
uint64_t name_offset;
- ASSERT_TRUE(elf->GetFunctionName(0x90010, 0, &name, &name_offset));
+ ASSERT_TRUE(elf->GetFunctionName(0x90010, &name, &name_offset));
EXPECT_EQ("function_one", name);
EXPECT_EQ(16U, name_offset);
- ASSERT_TRUE(elf->GetFunctionName(0xd0020, 0, &name, &name_offset));
+ ASSERT_TRUE(elf->GetFunctionName(0xd0020, &name, &name_offset));
EXPECT_EQ("function_two", name);
EXPECT_EQ(32U, name_offset);
}
@@ -863,18 +896,16 @@
uint64_t offset = 0x2000;
- Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Ehdr ehdr = {};
ehdr.e_shoff = offset;
- ehdr.e_shnum = 10;
+ ehdr.e_shnum = 6;
ehdr.e_shentsize = sizeof(Shdr);
ehdr.e_shstrndx = 2;
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
offset += ehdr.e_shentsize;
- Shdr shdr;
- memset(&shdr, 0, sizeof(shdr));
+ Shdr shdr = {};
shdr.sh_type = SHT_PROGBITS;
shdr.sh_link = 2;
shdr.sh_name = 0x200;
@@ -956,15 +987,13 @@
TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load) {
std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_));
- Elf32_Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Elf32_Ehdr ehdr = {};
ehdr.e_phoff = 0x100;
ehdr.e_phnum = 1;
ehdr.e_phentsize = sizeof(Elf32_Phdr);
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Elf32_Phdr phdr;
- memset(&phdr, 0, sizeof(phdr));
+ Elf32_Phdr phdr = {};
phdr.p_type = PT_LOAD;
phdr.p_vaddr = 0;
phdr.p_memsz = 0x10000;
@@ -984,15 +1013,13 @@
TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load_non_zero_load_bias) {
std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_));
- Elf32_Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Elf32_Ehdr ehdr = {};
ehdr.e_phoff = 0x100;
ehdr.e_phnum = 1;
ehdr.e_phentsize = sizeof(Elf32_Phdr);
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Elf32_Phdr phdr;
- memset(&phdr, 0, sizeof(phdr));
+ Elf32_Phdr phdr = {};
phdr.p_type = PT_LOAD;
phdr.p_vaddr = 0x2000;
phdr.p_memsz = 0x10000;
@@ -1017,16 +1044,14 @@
uint64_t sh_offset = 0x100;
- Elf32_Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Elf32_Ehdr ehdr = {};
ehdr.e_shstrndx = 1;
ehdr.e_shoff = sh_offset;
ehdr.e_shentsize = sizeof(Elf32_Shdr);
ehdr.e_shnum = 3;
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Elf32_Shdr shdr;
- memset(&shdr, 0, sizeof(shdr));
+ Elf32_Shdr shdr = {};
shdr.sh_type = SHT_NULL;
memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
@@ -1051,11 +1076,7 @@
// CIE 32.
memory_.SetData32(0x600, 0xfc);
memory_.SetData32(0x604, 0xffffffff);
- memory_.SetData8(0x608, 1);
- memory_.SetData8(0x609, '\0');
- memory_.SetData8(0x60a, 0x4);
- memory_.SetData8(0x60b, 0x4);
- memory_.SetData8(0x60c, 0x1);
+ memory_.SetMemory(0x608, std::vector<uint8_t>{1, '\0', 4, 4, 1});
// FDE 32.
memory_.SetData32(0x700, 0xfc);
@@ -1065,7 +1086,7 @@
uint64_t load_bias = 0;
ASSERT_TRUE(elf->Init(&load_bias));
- elf->InitHeaders();
+ elf->InitHeaders(0);
EXPECT_EQ(0U, load_bias);
EXPECT_FALSE(elf->IsValidPc(0));
EXPECT_FALSE(elf->IsValidPc(0x20ff));
@@ -1080,16 +1101,14 @@
uint64_t sh_offset = 0x100;
- Elf32_Ehdr ehdr;
- memset(&ehdr, 0, sizeof(ehdr));
+ Elf32_Ehdr ehdr = {};
ehdr.e_shstrndx = 1;
ehdr.e_shoff = sh_offset;
ehdr.e_shentsize = sizeof(Elf32_Shdr);
ehdr.e_shnum = 3;
memory_.SetMemory(0, &ehdr, sizeof(ehdr));
- Elf32_Shdr shdr;
- memset(&shdr, 0, sizeof(shdr));
+ Elf32_Shdr shdr = {};
shdr.sh_type = SHT_NULL;
memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
@@ -1114,11 +1133,7 @@
// CIE 32.
memory_.SetData32(0x600, 0xfc);
memory_.SetData32(0x604, 0);
- memory_.SetData8(0x608, 1);
- memory_.SetData8(0x609, '\0');
- memory_.SetData8(0x60a, 0x4);
- memory_.SetData8(0x60b, 0x4);
- memory_.SetData8(0x60c, 0x1);
+ memory_.SetMemory(0x608, std::vector<uint8_t>{1, '\0', 4, 4, 1});
// FDE 32.
memory_.SetData32(0x700, 0xfc);
@@ -1128,7 +1143,7 @@
uint64_t load_bias = 0;
ASSERT_TRUE(elf->Init(&load_bias));
- elf->InitHeaders();
+ elf->InitHeaders(0);
EXPECT_EQ(0U, load_bias);
EXPECT_FALSE(elf->IsValidPc(0));
EXPECT_FALSE(elf->IsValidPc(0x27ff));
diff --git a/libunwindstack/tests/ElfTest.cpp b/libunwindstack/tests/ElfTest.cpp
index eb85033..55fe16f 100644
--- a/libunwindstack/tests/ElfTest.cpp
+++ b/libunwindstack/tests/ElfTest.cpp
@@ -133,7 +133,7 @@
ASSERT_FALSE(elf.GetFunctionName(0, &name, &func_offset));
bool finished;
- ASSERT_FALSE(elf.Step(0, 0, 0, nullptr, nullptr, &finished));
+ ASSERT_FALSE(elf.Step(0, 0, nullptr, nullptr, &finished));
}
TEST_F(ElfTest, elf32_invalid_machine) {
@@ -297,16 +297,11 @@
elf.FakeSetInterface(interface);
elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0);
MapInfo map_info(0x1000, 0x2000);
ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
- elf.FakeSetLoadBias(0x3000);
- ASSERT_EQ(0x3101U, elf.GetRelPc(0x1101, &map_info));
-
elf.FakeSetValid(false);
- elf.FakeSetLoadBias(0);
ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
}
@@ -316,7 +311,6 @@
RegsArm regs;
regs[13] = 0x50000;
regs[15] = 0x8000;
- regs.SetFromRaw();
ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
elf.FakeSetInterface(interface);
@@ -329,9 +323,8 @@
}
elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0);
bool finished;
- ASSERT_TRUE(elf.Step(0x1000, 0x1000, 0x2000, ®s, &process_memory, &finished));
+ ASSERT_TRUE(elf.Step(0x3000, 0x1000, ®s, &process_memory, &finished));
EXPECT_FALSE(finished);
EXPECT_EQ(15U, regs.pc());
EXPECT_EQ(13U, regs.sp());
@@ -343,11 +336,11 @@
virtual ~ElfInterfaceMock() = default;
bool Init(uint64_t*) override { return false; }
- void InitHeaders() override {}
+ void InitHeaders(uint64_t) override {}
bool GetSoname(std::string*) override { return false; }
- bool GetFunctionName(uint64_t, uint64_t, std::string*, uint64_t*) override { return false; }
+ bool GetFunctionName(uint64_t, std::string*, uint64_t*) override { return false; }
- MOCK_METHOD5(Step, bool(uint64_t, uint64_t, Regs*, Memory*, bool*));
+ MOCK_METHOD4(Step, bool(uint64_t, Regs*, Memory*, bool*));
MOCK_METHOD2(GetGlobalVariable, bool(const std::string&, uint64_t*));
MOCK_METHOD1(IsValidPc, bool(uint64_t));
@@ -359,7 +352,6 @@
TEST_F(ElfTest, step_in_interface) {
ElfFake elf(memory_);
elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0);
RegsArm regs;
@@ -368,28 +360,10 @@
MemoryFake process_memory;
bool finished;
- EXPECT_CALL(*interface, Step(0x1000, 0, ®s, &process_memory, &finished))
+ EXPECT_CALL(*interface, Step(0x1000, ®s, &process_memory, &finished))
.WillOnce(::testing::Return(true));
- ASSERT_TRUE(elf.Step(0x1004, 0x1000, 0x2000, ®s, &process_memory, &finished));
-}
-
-TEST_F(ElfTest, step_in_interface_non_zero_load_bias) {
- ElfFake elf(memory_);
- elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0x4000);
-
- RegsArm regs;
-
- ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
- elf.FakeSetInterface(interface);
- MemoryFake process_memory;
-
- bool finished;
- EXPECT_CALL(*interface, Step(0x7300, 0x4000, ®s, &process_memory, &finished))
- .WillOnce(::testing::Return(true));
-
- ASSERT_TRUE(elf.Step(0x7304, 0x7300, 0x2000, ®s, &process_memory, &finished));
+ ASSERT_TRUE(elf.Step(0x1004, 0x1000, ®s, &process_memory, &finished));
}
TEST_F(ElfTest, get_global_invalid_elf) {
@@ -404,7 +378,6 @@
TEST_F(ElfTest, get_global_valid_not_in_interface) {
ElfFake elf(memory_);
elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0);
ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
elf.FakeSetInterface(interface);
@@ -432,10 +405,26 @@
ASSERT_FALSE(elf.GetGlobalVariable(global, &offset));
}
+TEST_F(ElfTest, get_global_valid_dynamic_zero_non_zero_load_bias) {
+ ElfFake elf(memory_);
+ elf.FakeSetValid(true);
+ elf.FakeSetLoadBias(0x100);
+
+ ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
+ elf.FakeSetInterface(interface);
+
+ uint64_t offset;
+ std::string global("something");
+ EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
+ .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
+
+ ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
+ EXPECT_EQ(0x200U, offset);
+}
+
TEST_F(ElfTest, get_global_valid_dynamic_zero) {
ElfFake elf(memory_);
elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0);
ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
elf.FakeSetInterface(interface);
@@ -457,7 +446,6 @@
TEST_F(ElfTest, get_global_valid_in_gnu_debugdata_dynamic_zero) {
ElfFake elf(memory_);
elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0);
ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
elf.FakeSetInterface(interface);
@@ -471,27 +459,9 @@
EXPECT_EQ(0x300U, offset);
}
-TEST_F(ElfTest, get_global_valid_dynamic_zero_non_zero_load_bias) {
- ElfFake elf(memory_);
- elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0x100);
-
- ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
- elf.FakeSetInterface(interface);
-
- uint64_t offset;
- std::string global("something");
- EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
- .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
-
- ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
- EXPECT_EQ(0x200U, offset);
-}
-
TEST_F(ElfTest, get_global_valid_dynamic_adjust_negative) {
ElfFake elf(memory_);
elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0);
ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
interface->MockSetDynamicOffset(0x400);
@@ -511,7 +481,6 @@
TEST_F(ElfTest, get_global_valid_dynamic_adjust_positive) {
ElfFake elf(memory_);
elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0);
ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
interface->MockSetDynamicOffset(0x1000);
@@ -531,7 +500,6 @@
TEST_F(ElfTest, is_valid_pc_elf_invalid) {
ElfFake elf(memory_);
elf.FakeSetValid(false);
- elf.FakeSetLoadBias(0);
EXPECT_FALSE(elf.IsValidPc(0x100));
EXPECT_FALSE(elf.IsValidPc(0x200));
@@ -540,7 +508,6 @@
TEST_F(ElfTest, is_valid_pc_interface) {
ElfFake elf(memory_);
elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0);
ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
elf.FakeSetInterface(interface);
@@ -550,25 +517,9 @@
EXPECT_TRUE(elf.IsValidPc(0x1500));
}
-TEST_F(ElfTest, is_valid_pc_non_zero_load_bias) {
- ElfFake elf(memory_);
- elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0x1000);
-
- ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
- elf.FakeSetInterface(interface);
-
- EXPECT_CALL(*interface, IsValidPc(0x500)).WillOnce(::testing::Return(true));
-
- EXPECT_FALSE(elf.IsValidPc(0x100));
- EXPECT_FALSE(elf.IsValidPc(0x200));
- EXPECT_TRUE(elf.IsValidPc(0x1500));
-}
-
TEST_F(ElfTest, is_valid_pc_from_gnu_debugdata) {
ElfFake elf(memory_);
elf.FakeSetValid(true);
- elf.FakeSetLoadBias(0);
ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
elf.FakeSetInterface(interface);
diff --git a/libunwindstack/tests/LocalUnwinderTest.cpp b/libunwindstack/tests/LocalUnwinderTest.cpp
new file mode 100644
index 0000000..56a18cd
--- /dev/null
+++ b/libunwindstack/tests/LocalUnwinderTest.cpp
@@ -0,0 +1,207 @@
+/*
+ * Copyright (C) 2018 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 <dlfcn.h>
+#include <inttypes.h>
+#include <signal.h>
+#include <stdint.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include <android-base/stringprintf.h>
+
+#include <unwindstack/LocalUnwinder.h>
+
+namespace unwindstack {
+
+static std::vector<LocalFrameData>* g_frame_info;
+static LocalUnwinder* g_unwinder;
+
+extern "C" void SignalLocalInnerFunction() {
+ g_unwinder->Unwind(g_frame_info, 256);
+}
+
+extern "C" void SignalLocalMiddleFunction() {
+ SignalLocalInnerFunction();
+}
+
+extern "C" void SignalLocalOuterFunction() {
+ SignalLocalMiddleFunction();
+}
+
+static void SignalLocalCallerHandler(int, siginfo_t*, void*) {
+ SignalLocalOuterFunction();
+}
+
+static std::string ErrorMsg(const std::vector<const char*>& function_names,
+ const std::vector<LocalFrameData>& frame_info) {
+ std::string unwind;
+ size_t i = 0;
+ for (const auto& frame : frame_info) {
+ unwind += android::base::StringPrintf("#%02zu pc 0x%" PRIx64 " rel_pc 0x%" PRIx64, i++,
+ frame.pc, frame.rel_pc);
+ if (frame.map_info != nullptr) {
+ if (!frame.map_info->name.empty()) {
+ unwind += " " + frame.map_info->name;
+ } else {
+ unwind += android::base::StringPrintf(" 0x%" PRIx64 "-0x%" PRIx64, frame.map_info->start,
+ frame.map_info->end);
+ }
+ if (frame.map_info->offset != 0) {
+ unwind += android::base::StringPrintf(" offset 0x%" PRIx64, frame.map_info->offset);
+ }
+ }
+ if (!frame.function_name.empty()) {
+ unwind += " " + frame.function_name;
+ if (frame.function_offset != 0) {
+ unwind += android::base::StringPrintf("+%" PRId64, frame.function_offset);
+ }
+ }
+ unwind += '\n';
+ }
+
+ return std::string(
+ "Unwind completed without finding all frames\n"
+ " Looking for function: ") +
+ function_names.front() + "\n" + "Unwind data:\n" + unwind;
+}
+
+// This test assumes that this code is compiled with optimizations turned
+// off. If this doesn't happen, then all of the calls will be optimized
+// away.
+extern "C" void LocalInnerFunction(LocalUnwinder* unwinder, bool unwind_through_signal) {
+ std::vector<LocalFrameData> frame_info;
+ g_frame_info = &frame_info;
+ g_unwinder = unwinder;
+ std::vector<const char*> expected_function_names;
+
+ if (unwind_through_signal) {
+ struct sigaction act, oldact;
+ memset(&act, 0, sizeof(act));
+ act.sa_sigaction = SignalLocalCallerHandler;
+ act.sa_flags = SA_RESTART | SA_ONSTACK;
+ ASSERT_EQ(0, sigaction(SIGUSR1, &act, &oldact));
+
+ raise(SIGUSR1);
+
+ ASSERT_EQ(0, sigaction(SIGUSR1, &oldact, nullptr));
+
+ expected_function_names = {"LocalOuterFunction", "LocalMiddleFunction",
+ "LocalInnerFunction", "SignalLocalOuterFunction",
+ "SignalLocalMiddleFunction", "SignalLocalInnerFunction"};
+ } else {
+ ASSERT_TRUE(unwinder->Unwind(&frame_info, 256));
+
+ expected_function_names = {"LocalOuterFunction", "LocalMiddleFunction", "LocalInnerFunction"};
+ }
+
+ for (auto& frame : frame_info) {
+ if (frame.function_name == expected_function_names.back()) {
+ expected_function_names.pop_back();
+ if (expected_function_names.empty()) {
+ break;
+ }
+ }
+ }
+
+ ASSERT_TRUE(expected_function_names.empty()) << ErrorMsg(expected_function_names, frame_info);
+}
+
+extern "C" void LocalMiddleFunction(LocalUnwinder* unwinder, bool unwind_through_signal) {
+ LocalInnerFunction(unwinder, unwind_through_signal);
+}
+
+extern "C" void LocalOuterFunction(LocalUnwinder* unwinder, bool unwind_through_signal) {
+ LocalMiddleFunction(unwinder, unwind_through_signal);
+}
+
+class LocalUnwinderTest : public ::testing::Test {
+ protected:
+ void SetUp() override {
+ unwinder_.reset(new LocalUnwinder);
+ ASSERT_TRUE(unwinder_->Init());
+ }
+
+ std::unique_ptr<LocalUnwinder> unwinder_;
+};
+
+TEST_F(LocalUnwinderTest, local) {
+ LocalOuterFunction(unwinder_.get(), false);
+}
+
+TEST_F(LocalUnwinderTest, local_signal) {
+ LocalOuterFunction(unwinder_.get(), true);
+}
+
+TEST_F(LocalUnwinderTest, local_multiple) {
+ ASSERT_NO_FATAL_FAILURE(LocalOuterFunction(unwinder_.get(), false));
+
+ ASSERT_NO_FATAL_FAILURE(LocalOuterFunction(unwinder_.get(), true));
+
+ ASSERT_NO_FATAL_FAILURE(LocalOuterFunction(unwinder_.get(), false));
+
+ ASSERT_NO_FATAL_FAILURE(LocalOuterFunction(unwinder_.get(), true));
+}
+
+// This test verifies that doing an unwind before and after a dlopen
+// works. It's verifying that the maps read during the first unwind
+// do not cause a problem when doing the unwind using the code in
+// the dlopen'd code.
+TEST_F(LocalUnwinderTest, unwind_after_dlopen) {
+ // Prime the maps data.
+ ASSERT_NO_FATAL_FAILURE(LocalOuterFunction(unwinder_.get(), false));
+
+ std::string testlib(testing::internal::GetArgvs()[0]);
+ auto const value = testlib.find_last_of('/');
+ if (value == std::string::npos) {
+ testlib = "../";
+ } else {
+ testlib = testlib.substr(0, value + 1) + "../";
+ }
+ testlib += "libunwindstack_local.so";
+
+ void* handle = dlopen(testlib.c_str(), RTLD_NOW);
+ ASSERT_TRUE(handle != nullptr);
+
+ void (*unwind_function)(void*, void*) =
+ reinterpret_cast<void (*)(void*, void*)>(dlsym(handle, "TestlibLevel1"));
+ ASSERT_TRUE(unwind_function != nullptr);
+
+ std::vector<LocalFrameData> frame_info;
+ unwind_function(unwinder_.get(), &frame_info);
+
+ ASSERT_EQ(0, dlclose(handle));
+
+ std::vector<const char*> expected_function_names{"TestlibLevel1", "TestlibLevel2",
+ "TestlibLevel3", "TestlibLevel4"};
+
+ for (auto& frame : frame_info) {
+ if (frame.function_name == expected_function_names.back()) {
+ expected_function_names.pop_back();
+ if (expected_function_names.empty()) {
+ break;
+ }
+ }
+ }
+
+ ASSERT_TRUE(expected_function_names.empty()) << ErrorMsg(expected_function_names, frame_info);
+}
+
+} // namespace unwindstack
diff --git a/libunwindstack/tests/MapInfoGetElfTest.cpp b/libunwindstack/tests/MapInfoGetElfTest.cpp
index f599503..861b82f 100644
--- a/libunwindstack/tests/MapInfoGetElfTest.cpp
+++ b/libunwindstack/tests/MapInfoGetElfTest.cpp
@@ -319,7 +319,7 @@
TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_AARCH64);
ehdr.e_shoff = 0x2000;
ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
- ehdr.e_shnum = 4;
+ ehdr.e_shnum = 0;
memory_->SetMemory(0x9000, &ehdr, sizeof(ehdr));
Elf* elf = info.GetElf(process_memory_, false);
@@ -341,7 +341,7 @@
TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_X86_64);
ehdr.e_shoff = 0x2000;
ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
- ehdr.e_shnum = 4;
+ ehdr.e_shnum = 0;
memory_->SetMemory(0x7000, &ehdr, sizeof(ehdr));
Elf* elf = info.GetElf(process_memory_, false);
@@ -368,7 +368,7 @@
TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_X86_64);
ehdr.e_shoff = 0x2000;
ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
- ehdr.e_shnum = 4;
+ ehdr.e_shnum = 0;
memory_->SetMemory(0x7000, &ehdr, sizeof(ehdr));
Elf* elf_in_threads[kNumConcurrentThreads];
diff --git a/libunwindstack/tests/MemoryOfflineBufferTest.cpp b/libunwindstack/tests/MemoryOfflineBufferTest.cpp
new file mode 100644
index 0000000..f022884
--- /dev/null
+++ b/libunwindstack/tests/MemoryOfflineBufferTest.cpp
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2018 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 <vector>
+
+#include <gtest/gtest.h>
+
+#include <unwindstack/Memory.h>
+
+#include "LogFake.h"
+
+namespace unwindstack {
+
+class MemoryOfflineBufferTest : public ::testing::Test {
+ protected:
+ void SetUp() override {
+ ResetLogs();
+ memory_.reset(new MemoryOfflineBuffer(buffer_.data(), kStart, kEnd));
+ }
+
+ static void SetUpTestCase() {
+ buffer_.resize(kLength);
+ for (size_t i = 0; i < kLength; i++) {
+ buffer_[i] = i % 189;
+ }
+ }
+
+ std::unique_ptr<MemoryOfflineBuffer> memory_;
+
+ static constexpr size_t kLength = 0x2000;
+ static constexpr uint64_t kStart = 0x1000;
+ static constexpr uint64_t kEnd = kStart + kLength;
+ static std::vector<uint8_t> buffer_;
+};
+
+std::vector<uint8_t> MemoryOfflineBufferTest::buffer_;
+
+static void VerifyBuffer(uint8_t* buffer, size_t start_value, size_t length) {
+ for (size_t i = 0; i < length; i++) {
+ ASSERT_EQ((start_value + i) % 189, buffer[i]) << "Failed at byte " << i;
+ }
+}
+
+TEST_F(MemoryOfflineBufferTest, read_out_of_bounds) {
+ std::vector<uint8_t> buffer(1024);
+ ASSERT_FALSE(memory_->ReadFully(0, buffer.data(), 1));
+ ASSERT_FALSE(memory_->ReadFully(0xfff, buffer.data(), 1));
+ ASSERT_FALSE(memory_->ReadFully(0xfff, buffer.data(), 2));
+ ASSERT_FALSE(memory_->ReadFully(0x3000, buffer.data(), 1));
+ ASSERT_FALSE(memory_->ReadFully(0x3001, buffer.data(), 1));
+}
+
+TEST_F(MemoryOfflineBufferTest, read) {
+ std::vector<uint8_t> buffer(1024);
+ ASSERT_TRUE(memory_->ReadFully(kStart, buffer.data(), 10));
+ ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), 0, 10));
+
+ ASSERT_TRUE(memory_->ReadFully(kStart + 555, buffer.data(), 40));
+ ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), 555, 40));
+
+ ASSERT_TRUE(memory_->ReadFully(kStart + kLength - 105, buffer.data(), 105));
+ ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), kLength - 105, 105));
+}
+
+TEST_F(MemoryOfflineBufferTest, read_past_end) {
+ std::vector<uint8_t> buffer(1024);
+ ASSERT_EQ(100U, memory_->Read(kStart + kLength - 100, buffer.data(), buffer.size()));
+ VerifyBuffer(buffer.data(), kLength - 100, 100);
+}
+
+TEST_F(MemoryOfflineBufferTest, read_after_reset) {
+ std::vector<uint8_t> buffer(1024);
+ ASSERT_TRUE(memory_->ReadFully(kStart, buffer.data(), 100));
+ ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), 0, 100));
+
+ memory_->Reset(&buffer_[10], 0x12000, 0x13000);
+ ASSERT_TRUE(memory_->ReadFully(0x12000, buffer.data(), 100));
+ ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), 10, 100));
+
+ ASSERT_EQ(50U, memory_->Read(0x13000 - 50, buffer.data(), buffer.size()));
+ ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), 0x1000 - 50 + 10, 50));
+}
+
+} // namespace unwindstack
diff --git a/libunwindstack/tests/MemoryTest.cpp b/libunwindstack/tests/MemoryTest.cpp
index 4a9ed9f..3655984 100644
--- a/libunwindstack/tests/MemoryTest.cpp
+++ b/libunwindstack/tests/MemoryTest.cpp
@@ -51,40 +51,6 @@
uint64_t four;
};
-TEST(MemoryTest, read_field) {
- MemoryFakeAlwaysReadZero memory;
-
- FakeStruct data;
- memset(&data, 0xff, sizeof(data));
- ASSERT_TRUE(memory.ReadField(0, &data, &data.one, sizeof(data.one)));
- ASSERT_EQ(0, data.one);
-
- memset(&data, 0xff, sizeof(data));
- ASSERT_TRUE(memory.ReadField(0, &data, &data.two, sizeof(data.two)));
- ASSERT_FALSE(data.two);
-
- memset(&data, 0xff, sizeof(data));
- ASSERT_TRUE(memory.ReadField(0, &data, &data.three, sizeof(data.three)));
- ASSERT_EQ(0U, data.three);
-
- memset(&data, 0xff, sizeof(data));
- ASSERT_TRUE(memory.ReadField(0, &data, &data.four, sizeof(data.four)));
- ASSERT_EQ(0U, data.four);
-}
-
-TEST(MemoryTest, read_field_fails) {
- MemoryFakeAlwaysReadZero memory;
-
- FakeStruct data;
- memset(&data, 0xff, sizeof(data));
-
- ASSERT_FALSE(memory.ReadField(UINT64_MAX, &data, &data.three, sizeof(data.three)));
-
- // Field and start reversed, should fail.
- ASSERT_FALSE(memory.ReadField(100, &data.two, &data, sizeof(data.two)));
- ASSERT_FALSE(memory.ReadField(0, &data.two, &data, sizeof(data.two)));
-}
-
TEST(MemoryTest, read_string) {
std::string name("string_in_memory");
diff --git a/libunwindstack/tests/RegsFake.h b/libunwindstack/tests/RegsFake.h
index ab23194..d6ca9b7 100644
--- a/libunwindstack/tests/RegsFake.h
+++ b/libunwindstack/tests/RegsFake.h
@@ -27,14 +27,16 @@
class RegsFake : public Regs {
public:
- RegsFake(uint16_t total_regs, uint16_t sp_reg)
- : Regs(total_regs, sp_reg, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
+ RegsFake(uint16_t total_regs) : Regs(total_regs, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
virtual ~RegsFake() = default;
ArchEnum Arch() override { return fake_arch_; }
void* RawData() override { return nullptr; }
uint64_t pc() override { return fake_pc_; }
uint64_t sp() override { return fake_sp_; }
+ void set_pc(uint64_t pc) override { fake_pc_ = pc; }
+ void set_sp(uint64_t sp) override { fake_sp_ = sp; }
+
bool SetPcFromReturnAddress(Memory*) override {
if (!fake_return_address_valid_) {
return false;
@@ -51,15 +53,13 @@
bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; }
- void SetFromRaw() override {}
-
void FakeSetArch(ArchEnum arch) { fake_arch_ = arch; }
- void FakeSetPc(uint64_t pc) { fake_pc_ = pc; }
- void FakeSetSp(uint64_t sp) { fake_sp_ = sp; }
void FakeSetDexPc(uint64_t dex_pc) { dex_pc_ = dex_pc; }
void FakeSetReturnAddress(uint64_t return_address) { fake_return_address_ = return_address; }
void FakeSetReturnAddressValid(bool valid) { fake_return_address_valid_ = valid; }
+ Regs* Clone() override { return nullptr; }
+
private:
ArchEnum fake_arch_ = ARCH_UNKNOWN;
uint64_t fake_pc_ = 0;
@@ -71,19 +71,25 @@
template <typename TypeParam>
class RegsImplFake : public RegsImpl<TypeParam> {
public:
- RegsImplFake(uint16_t total_regs, uint16_t sp_reg)
- : RegsImpl<TypeParam>(total_regs, sp_reg, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
+ RegsImplFake(uint16_t total_regs)
+ : RegsImpl<TypeParam>(total_regs, Regs::Location(Regs::LOCATION_UNKNOWN, 0)) {}
virtual ~RegsImplFake() = default;
ArchEnum Arch() override { return ARCH_UNKNOWN; }
+ uint64_t pc() override { return fake_pc_; }
+ uint64_t sp() override { return fake_sp_; }
+ void set_pc(uint64_t pc) override { fake_pc_ = pc; }
+ void set_sp(uint64_t sp) override { fake_sp_ = sp; }
uint64_t GetPcAdjustment(uint64_t, Elf*) override { return 0; }
- void SetFromRaw() override {}
bool SetPcFromReturnAddress(Memory*) override { return false; }
bool StepIfSignalHandler(uint64_t, Elf*, Memory*) override { return false; }
- void FakeSetPc(uint64_t pc) { this->pc_ = pc; }
- void FakeSetSp(uint64_t sp) { this->sp_ = sp; }
+ Regs* Clone() override { return nullptr; }
+
+ private:
+ uint64_t fake_pc_ = 0;
+ uint64_t fake_sp_ = 0;
};
} // namespace unwindstack
diff --git a/libunwindstack/tests/RegsInfoTest.cpp b/libunwindstack/tests/RegsInfoTest.cpp
new file mode 100644
index 0000000..052b5bf
--- /dev/null
+++ b/libunwindstack/tests/RegsInfoTest.cpp
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2018 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 <stdint.h>
+
+#include <gtest/gtest.h>
+
+#include <unwindstack/Regs.h>
+
+#include "RegsFake.h"
+#include "RegsInfo.h"
+
+namespace unwindstack {
+
+TEST(RegsInfoTest, single_uint32_t) {
+ RegsImplFake<uint32_t> regs(10);
+ RegsInfo<uint32_t> info(®s);
+
+ regs[1] = 0x100;
+ ASSERT_FALSE(info.IsSaved(1));
+ ASSERT_EQ(0x100U, info.Get(1));
+ ASSERT_EQ(10, info.Total());
+
+ uint32_t* value = info.Save(1);
+ ASSERT_EQ(value, ®s[1]);
+ regs[1] = 0x200;
+ ASSERT_TRUE(info.IsSaved(1));
+ ASSERT_EQ(0x100U, info.Get(1));
+ ASSERT_EQ(0x200U, regs[1]);
+}
+
+TEST(RegsInfoTest, single_uint64_t) {
+ RegsImplFake<uint64_t> regs(20);
+ RegsInfo<uint64_t> info(®s);
+
+ regs[3] = 0x300;
+ ASSERT_FALSE(info.IsSaved(3));
+ ASSERT_EQ(0x300U, info.Get(3));
+ ASSERT_EQ(20, info.Total());
+
+ uint64_t* value = info.Save(3);
+ ASSERT_EQ(value, ®s[3]);
+ regs[3] = 0x400;
+ ASSERT_TRUE(info.IsSaved(3));
+ ASSERT_EQ(0x300U, info.Get(3));
+ ASSERT_EQ(0x400U, regs[3]);
+}
+
+TEST(RegsInfoTest, all) {
+ RegsImplFake<uint64_t> regs(64);
+ RegsInfo<uint64_t> info(®s);
+
+ for (uint32_t i = 0; i < 64; i++) {
+ regs[i] = i * 0x100;
+ ASSERT_EQ(i * 0x100, info.Get(i)) << "Reg " + std::to_string(i) + " failed.";
+ }
+
+ for (uint32_t i = 0; i < 64; i++) {
+ ASSERT_FALSE(info.IsSaved(i)) << "Reg " + std::to_string(i) + " failed.";
+ uint64_t* reg = info.Save(i);
+ ASSERT_EQ(reg, ®s[i]) << "Reg " + std::to_string(i) + " failed.";
+ *reg = i * 0x1000 + 0x100;
+ ASSERT_EQ(i * 0x1000 + 0x100, regs[i]) << "Reg " + std::to_string(i) + " failed.";
+ }
+
+ for (uint32_t i = 0; i < 64; i++) {
+ ASSERT_TRUE(info.IsSaved(i)) << "Reg " + std::to_string(i) + " failed.";
+ ASSERT_EQ(i * 0x100, info.Get(i)) << "Reg " + std::to_string(i) + " failed.";
+ }
+}
+
+} // namespace unwindstack
diff --git a/libunwindstack/tests/RegsStepIfSignalHandlerTest.cpp b/libunwindstack/tests/RegsStepIfSignalHandlerTest.cpp
index ecd4051..eac12ca 100644
--- a/libunwindstack/tests/RegsStepIfSignalHandlerTest.cpp
+++ b/libunwindstack/tests/RegsStepIfSignalHandlerTest.cpp
@@ -56,7 +56,6 @@
RegsArm regs;
regs[ARM_REG_PC] = 0x5000;
regs[ARM_REG_SP] = addr;
- regs.SetFromRaw();
elf_memory_->SetData32(0x5000, pc_data);
@@ -87,7 +86,6 @@
RegsArm regs;
regs[ARM_REG_PC] = 0x5000;
regs[ARM_REG_SP] = addr;
- regs.SetFromRaw();
elf_memory_->SetData32(0x5000, pc_data);
@@ -118,7 +116,6 @@
RegsArm64 regs;
regs[ARM64_REG_PC] = 0x8000;
regs[ARM64_REG_SP] = addr;
- regs.SetFromRaw();
elf_memory_->SetData64(0x8000, 0xd4000001d2801168ULL);
@@ -138,7 +135,6 @@
RegsX86 regs;
regs[X86_REG_EIP] = 0x4100;
regs[X86_REG_ESP] = addr;
- regs.SetFromRaw();
elf_memory_->SetData64(0x4100, 0x80cd00000077b858ULL);
for (uint64_t index = 0; index <= 25; index++) {
@@ -162,7 +158,6 @@
RegsX86 regs;
regs[X86_REG_EIP] = 0x4100;
regs[X86_REG_ESP] = addr;
- regs.SetFromRaw();
elf_memory_->SetData64(0x4100, 0x0080cd000000adb8ULL);
addr += 8;
@@ -191,7 +186,6 @@
RegsX86_64 regs;
regs[X86_64_REG_RIP] = 0x7000;
regs[X86_64_REG_RSP] = addr;
- regs.SetFromRaw();
elf_memory_->SetData64(0x7000, 0x0f0000000fc0c748);
elf_memory_->SetData16(0x7008, 0x0f05);
@@ -212,7 +206,6 @@
RegsMips regs;
regs[MIPS_REG_PC] = 0x8000;
regs[MIPS_REG_SP] = addr;
- regs.SetFromRaw();
elf_memory_->SetData64(0x8000, 0x0000000c24021017ULL);
@@ -232,7 +225,6 @@
RegsMips regs;
regs[MIPS_REG_PC] = 0x8000;
regs[MIPS_REG_SP] = addr;
- regs.SetFromRaw();
elf_memory_->SetData64(0x8000, 0x0000000c24021061ULL);
@@ -252,7 +244,6 @@
RegsMips64 regs;
regs[MIPS64_REG_PC] = 0x8000;
regs[MIPS64_REG_SP] = addr;
- regs.SetFromRaw();
elf_memory_->SetData64(0x8000, 0x0000000c2402145bULL);
diff --git a/libunwindstack/tests/RegsTest.cpp b/libunwindstack/tests/RegsTest.cpp
index 8b2f6c8..90c3fe6 100644
--- a/libunwindstack/tests/RegsTest.cpp
+++ b/libunwindstack/tests/RegsTest.cpp
@@ -49,9 +49,8 @@
};
TEST_F(RegsTest, regs32) {
- RegsImplFake<uint32_t> regs32(50, 10);
+ RegsImplFake<uint32_t> regs32(50);
ASSERT_EQ(50U, regs32.total_regs());
- ASSERT_EQ(10U, regs32.sp_reg());
uint32_t* raw = reinterpret_cast<uint32_t*>(regs32.RawData());
for (size_t i = 0; i < 50; i++) {
@@ -72,9 +71,8 @@
}
TEST_F(RegsTest, regs64) {
- RegsImplFake<uint64_t> regs64(30, 12);
+ RegsImplFake<uint64_t> regs64(30);
ASSERT_EQ(30U, regs64.total_regs());
- ASSERT_EQ(12U, regs64.sp_reg());
uint64_t* raw = reinterpret_cast<uint64_t*>(regs64.RawData());
for (size_t i = 0; i < 30; i++) {
@@ -96,48 +94,48 @@
TEST_F(RegsTest, rel_pc) {
RegsArm64 arm64;
- ASSERT_EQ(4U, arm64.GetPcAdjustment(0x10, elf_.get()));
- ASSERT_EQ(4U, arm64.GetPcAdjustment(0x4, elf_.get()));
- ASSERT_EQ(0U, arm64.GetPcAdjustment(0x3, elf_.get()));
- ASSERT_EQ(0U, arm64.GetPcAdjustment(0x2, elf_.get()));
- ASSERT_EQ(0U, arm64.GetPcAdjustment(0x1, elf_.get()));
- ASSERT_EQ(0U, arm64.GetPcAdjustment(0x0, elf_.get()));
+ EXPECT_EQ(4U, arm64.GetPcAdjustment(0x10, elf_.get()));
+ EXPECT_EQ(4U, arm64.GetPcAdjustment(0x4, elf_.get()));
+ EXPECT_EQ(0U, arm64.GetPcAdjustment(0x3, elf_.get()));
+ EXPECT_EQ(0U, arm64.GetPcAdjustment(0x2, elf_.get()));
+ EXPECT_EQ(0U, arm64.GetPcAdjustment(0x1, elf_.get()));
+ EXPECT_EQ(0U, arm64.GetPcAdjustment(0x0, elf_.get()));
RegsX86 x86;
- ASSERT_EQ(1U, x86.GetPcAdjustment(0x100, elf_.get()));
- ASSERT_EQ(1U, x86.GetPcAdjustment(0x2, elf_.get()));
- ASSERT_EQ(1U, x86.GetPcAdjustment(0x1, elf_.get()));
- ASSERT_EQ(0U, x86.GetPcAdjustment(0x0, elf_.get()));
+ EXPECT_EQ(1U, x86.GetPcAdjustment(0x100, elf_.get()));
+ EXPECT_EQ(1U, x86.GetPcAdjustment(0x2, elf_.get()));
+ EXPECT_EQ(1U, x86.GetPcAdjustment(0x1, elf_.get()));
+ EXPECT_EQ(0U, x86.GetPcAdjustment(0x0, elf_.get()));
RegsX86_64 x86_64;
- ASSERT_EQ(1U, x86_64.GetPcAdjustment(0x100, elf_.get()));
- ASSERT_EQ(1U, x86_64.GetPcAdjustment(0x2, elf_.get()));
- ASSERT_EQ(1U, x86_64.GetPcAdjustment(0x1, elf_.get()));
- ASSERT_EQ(0U, x86_64.GetPcAdjustment(0x0, elf_.get()));
+ EXPECT_EQ(1U, x86_64.GetPcAdjustment(0x100, elf_.get()));
+ EXPECT_EQ(1U, x86_64.GetPcAdjustment(0x2, elf_.get()));
+ EXPECT_EQ(1U, x86_64.GetPcAdjustment(0x1, elf_.get()));
+ EXPECT_EQ(0U, x86_64.GetPcAdjustment(0x0, elf_.get()));
RegsMips mips;
- ASSERT_EQ(8U, mips.GetPcAdjustment(0x10, elf_.get()));
- ASSERT_EQ(8U, mips.GetPcAdjustment(0x8, elf_.get()));
- ASSERT_EQ(0U, mips.GetPcAdjustment(0x7, elf_.get()));
- ASSERT_EQ(0U, mips.GetPcAdjustment(0x6, elf_.get()));
- ASSERT_EQ(0U, mips.GetPcAdjustment(0x5, elf_.get()));
- ASSERT_EQ(0U, mips.GetPcAdjustment(0x4, elf_.get()));
- ASSERT_EQ(0U, mips.GetPcAdjustment(0x3, elf_.get()));
- ASSERT_EQ(0U, mips.GetPcAdjustment(0x2, elf_.get()));
- ASSERT_EQ(0U, mips.GetPcAdjustment(0x1, elf_.get()));
- ASSERT_EQ(0U, mips.GetPcAdjustment(0x0, elf_.get()));
+ EXPECT_EQ(8U, mips.GetPcAdjustment(0x10, elf_.get()));
+ EXPECT_EQ(8U, mips.GetPcAdjustment(0x8, elf_.get()));
+ EXPECT_EQ(0U, mips.GetPcAdjustment(0x7, elf_.get()));
+ EXPECT_EQ(0U, mips.GetPcAdjustment(0x6, elf_.get()));
+ EXPECT_EQ(0U, mips.GetPcAdjustment(0x5, elf_.get()));
+ EXPECT_EQ(0U, mips.GetPcAdjustment(0x4, elf_.get()));
+ EXPECT_EQ(0U, mips.GetPcAdjustment(0x3, elf_.get()));
+ EXPECT_EQ(0U, mips.GetPcAdjustment(0x2, elf_.get()));
+ EXPECT_EQ(0U, mips.GetPcAdjustment(0x1, elf_.get()));
+ EXPECT_EQ(0U, mips.GetPcAdjustment(0x0, elf_.get()));
RegsMips64 mips64;
- ASSERT_EQ(8U, mips64.GetPcAdjustment(0x10, elf_.get()));
- ASSERT_EQ(8U, mips64.GetPcAdjustment(0x8, elf_.get()));
- ASSERT_EQ(0U, mips64.GetPcAdjustment(0x7, elf_.get()));
- ASSERT_EQ(0U, mips64.GetPcAdjustment(0x6, elf_.get()));
- ASSERT_EQ(0U, mips64.GetPcAdjustment(0x5, elf_.get()));
- ASSERT_EQ(0U, mips64.GetPcAdjustment(0x4, elf_.get()));
- ASSERT_EQ(0U, mips64.GetPcAdjustment(0x3, elf_.get()));
- ASSERT_EQ(0U, mips64.GetPcAdjustment(0x2, elf_.get()));
- ASSERT_EQ(0U, mips64.GetPcAdjustment(0x1, elf_.get()));
- ASSERT_EQ(0U, mips64.GetPcAdjustment(0x0, elf_.get()));
+ EXPECT_EQ(8U, mips64.GetPcAdjustment(0x10, elf_.get()));
+ EXPECT_EQ(8U, mips64.GetPcAdjustment(0x8, elf_.get()));
+ EXPECT_EQ(0U, mips64.GetPcAdjustment(0x7, elf_.get()));
+ EXPECT_EQ(0U, mips64.GetPcAdjustment(0x6, elf_.get()));
+ EXPECT_EQ(0U, mips64.GetPcAdjustment(0x5, elf_.get()));
+ EXPECT_EQ(0U, mips64.GetPcAdjustment(0x4, elf_.get()));
+ EXPECT_EQ(0U, mips64.GetPcAdjustment(0x3, elf_.get()));
+ EXPECT_EQ(0U, mips64.GetPcAdjustment(0x2, elf_.get()));
+ EXPECT_EQ(0U, mips64.GetPcAdjustment(0x1, elf_.get()));
+ EXPECT_EQ(0U, mips64.GetPcAdjustment(0x0, elf_.get()));
}
TEST_F(RegsTest, rel_pc_arm) {
@@ -145,34 +143,36 @@
// Check fence posts.
elf_->FakeSetLoadBias(0);
- ASSERT_EQ(2U, arm.GetPcAdjustment(0x5, elf_.get()));
- ASSERT_EQ(0U, arm.GetPcAdjustment(0x4, elf_.get()));
- ASSERT_EQ(0U, arm.GetPcAdjustment(0x3, elf_.get()));
- ASSERT_EQ(0U, arm.GetPcAdjustment(0x2, elf_.get()));
- ASSERT_EQ(0U, arm.GetPcAdjustment(0x1, elf_.get()));
- ASSERT_EQ(0U, arm.GetPcAdjustment(0x0, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x5, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x4, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x3, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x2, elf_.get()));
+ EXPECT_EQ(0U, arm.GetPcAdjustment(0x1, elf_.get()));
+ EXPECT_EQ(0U, arm.GetPcAdjustment(0x0, elf_.get()));
elf_->FakeSetLoadBias(0x100);
- ASSERT_EQ(0U, arm.GetPcAdjustment(0xff, elf_.get()));
- ASSERT_EQ(2U, arm.GetPcAdjustment(0x105, elf_.get()));
- ASSERT_EQ(0U, arm.GetPcAdjustment(0x104, elf_.get()));
- ASSERT_EQ(0U, arm.GetPcAdjustment(0x103, elf_.get()));
- ASSERT_EQ(0U, arm.GetPcAdjustment(0x102, elf_.get()));
- ASSERT_EQ(0U, arm.GetPcAdjustment(0x101, elf_.get()));
- ASSERT_EQ(0U, arm.GetPcAdjustment(0x100, elf_.get()));
+ EXPECT_EQ(0U, arm.GetPcAdjustment(0x1, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x2, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0xff, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x105, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x104, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x103, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x102, elf_.get()));
+ EXPECT_EQ(0U, arm.GetPcAdjustment(0x101, elf_.get()));
+ EXPECT_EQ(0U, arm.GetPcAdjustment(0x100, elf_.get()));
// Check thumb instructions handling.
elf_->FakeSetLoadBias(0);
memory_->SetData32(0x2000, 0);
- ASSERT_EQ(2U, arm.GetPcAdjustment(0x2005, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x2005, elf_.get()));
memory_->SetData32(0x2000, 0xe000f000);
- ASSERT_EQ(4U, arm.GetPcAdjustment(0x2005, elf_.get()));
+ EXPECT_EQ(4U, arm.GetPcAdjustment(0x2005, elf_.get()));
elf_->FakeSetLoadBias(0x400);
memory_->SetData32(0x2100, 0);
- ASSERT_EQ(2U, arm.GetPcAdjustment(0x2505, elf_.get()));
+ EXPECT_EQ(2U, arm.GetPcAdjustment(0x2505, elf_.get()));
memory_->SetData32(0x2100, 0xf111f111);
- ASSERT_EQ(4U, arm.GetPcAdjustment(0x2505, elf_.get()));
+ EXPECT_EQ(4U, arm.GetPcAdjustment(0x2505, elf_.get()));
}
TEST_F(RegsTest, elf_invalid) {
@@ -183,90 +183,85 @@
RegsMips regs_mips;
RegsMips64 regs_mips64;
MapInfo map_info(0x1000, 0x2000);
- Elf* invalid_elf = new Elf(new MemoryFake);
+ Elf* invalid_elf = new Elf(nullptr);
map_info.elf.reset(invalid_elf);
regs_arm.set_pc(0x1500);
EXPECT_EQ(0x500U, invalid_elf->GetRelPc(regs_arm.pc(), &map_info));
- EXPECT_EQ(4U, regs_arm.GetPcAdjustment(0x500U, invalid_elf));
+ EXPECT_EQ(2U, regs_arm.GetPcAdjustment(0x500U, invalid_elf));
+ EXPECT_EQ(2U, regs_arm.GetPcAdjustment(0x511U, invalid_elf));
regs_arm64.set_pc(0x1600);
EXPECT_EQ(0x600U, invalid_elf->GetRelPc(regs_arm64.pc(), &map_info));
- EXPECT_EQ(0U, regs_arm64.GetPcAdjustment(0x600U, invalid_elf));
+ EXPECT_EQ(4U, regs_arm64.GetPcAdjustment(0x600U, invalid_elf));
regs_x86.set_pc(0x1700);
EXPECT_EQ(0x700U, invalid_elf->GetRelPc(regs_x86.pc(), &map_info));
- EXPECT_EQ(0U, regs_x86.GetPcAdjustment(0x700U, invalid_elf));
+ EXPECT_EQ(1U, regs_x86.GetPcAdjustment(0x700U, invalid_elf));
regs_x86_64.set_pc(0x1800);
EXPECT_EQ(0x800U, invalid_elf->GetRelPc(regs_x86_64.pc(), &map_info));
- EXPECT_EQ(0U, regs_x86_64.GetPcAdjustment(0x800U, invalid_elf));
+ EXPECT_EQ(1U, regs_x86_64.GetPcAdjustment(0x800U, invalid_elf));
regs_mips.set_pc(0x1900);
EXPECT_EQ(0x900U, invalid_elf->GetRelPc(regs_mips.pc(), &map_info));
- EXPECT_EQ(0U, regs_mips.GetPcAdjustment(0x900U, invalid_elf));
+ EXPECT_EQ(8U, regs_mips.GetPcAdjustment(0x900U, invalid_elf));
regs_mips64.set_pc(0x1a00);
EXPECT_EQ(0xa00U, invalid_elf->GetRelPc(regs_mips64.pc(), &map_info));
- EXPECT_EQ(0U, regs_mips64.GetPcAdjustment(0xa00U, invalid_elf));
+ EXPECT_EQ(8U, regs_mips64.GetPcAdjustment(0xa00U, invalid_elf));
}
-TEST_F(RegsTest, arm_set_from_raw) {
+TEST_F(RegsTest, arm_verify_sp_pc) {
RegsArm arm;
uint32_t* regs = reinterpret_cast<uint32_t*>(arm.RawData());
regs[13] = 0x100;
regs[15] = 0x200;
- arm.SetFromRaw();
EXPECT_EQ(0x100U, arm.sp());
EXPECT_EQ(0x200U, arm.pc());
}
-TEST_F(RegsTest, arm64_set_from_raw) {
+TEST_F(RegsTest, arm64_verify_sp_pc) {
RegsArm64 arm64;
uint64_t* regs = reinterpret_cast<uint64_t*>(arm64.RawData());
regs[31] = 0xb100000000ULL;
regs[32] = 0xc200000000ULL;
- arm64.SetFromRaw();
EXPECT_EQ(0xb100000000U, arm64.sp());
EXPECT_EQ(0xc200000000U, arm64.pc());
}
-TEST_F(RegsTest, x86_set_from_raw) {
+TEST_F(RegsTest, x86_verify_sp_pc) {
RegsX86 x86;
uint32_t* regs = reinterpret_cast<uint32_t*>(x86.RawData());
regs[4] = 0x23450000;
regs[8] = 0xabcd0000;
- x86.SetFromRaw();
EXPECT_EQ(0x23450000U, x86.sp());
EXPECT_EQ(0xabcd0000U, x86.pc());
}
-TEST_F(RegsTest, x86_64_set_from_raw) {
+TEST_F(RegsTest, x86_64_verify_sp_pc) {
RegsX86_64 x86_64;
uint64_t* regs = reinterpret_cast<uint64_t*>(x86_64.RawData());
regs[7] = 0x1200000000ULL;
regs[16] = 0x4900000000ULL;
- x86_64.SetFromRaw();
EXPECT_EQ(0x1200000000U, x86_64.sp());
EXPECT_EQ(0x4900000000U, x86_64.pc());
}
-TEST_F(RegsTest, mips_set_from_raw) {
+TEST_F(RegsTest, mips_verify_sp_pc) {
RegsMips mips;
uint32_t* regs = reinterpret_cast<uint32_t*>(mips.RawData());
regs[29] = 0x100;
regs[32] = 0x200;
- mips.SetFromRaw();
EXPECT_EQ(0x100U, mips.sp());
EXPECT_EQ(0x200U, mips.pc());
}
-TEST_F(RegsTest, mips64_set_from_raw) {
+TEST_F(RegsTest, mips64_verify_sp_pc) {
RegsMips64 mips64;
uint64_t* regs = reinterpret_cast<uint64_t*>(mips64.RawData());
regs[29] = 0xb100000000ULL;
regs[32] = 0xc200000000ULL;
- mips64.SetFromRaw();
EXPECT_EQ(0xb100000000U, mips64.sp());
EXPECT_EQ(0xc200000000U, mips64.pc());
}
@@ -291,4 +286,39 @@
EXPECT_EQ(ARCH_MIPS64, mips64_regs.Arch());
}
+template <typename RegisterType>
+void clone_test(Regs* regs) {
+ RegisterType* register_values = reinterpret_cast<RegisterType*>(regs->RawData());
+ int num_regs = regs->total_regs();
+ for (int i = 0; i < num_regs; ++i) {
+ register_values[i] = i;
+ }
+
+ std::unique_ptr<Regs> clone(regs->Clone());
+ ASSERT_EQ(regs->total_regs(), clone->total_regs());
+ RegisterType* clone_values = reinterpret_cast<RegisterType*>(clone->RawData());
+ for (int i = 0; i < num_regs; ++i) {
+ EXPECT_EQ(register_values[i], clone_values[i]);
+ EXPECT_NE(®ister_values[i], &clone_values[i]);
+ }
+}
+
+TEST_F(RegsTest, clone) {
+ std::vector<std::unique_ptr<Regs>> regs;
+ regs.emplace_back(new RegsArm());
+ regs.emplace_back(new RegsArm64());
+ regs.emplace_back(new RegsX86());
+ regs.emplace_back(new RegsX86_64());
+ regs.emplace_back(new RegsMips());
+ regs.emplace_back(new RegsMips64());
+
+ for (auto& r : regs) {
+ if (r->Is32Bit()) {
+ clone_test<uint32_t>(r.get());
+ } else {
+ clone_test<uint64_t>(r.get());
+ }
+ }
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/tests/SymbolsTest.cpp b/libunwindstack/tests/SymbolsTest.cpp
index 45a7b58..b40a253 100644
--- a/libunwindstack/tests/SymbolsTest.cpp
+++ b/libunwindstack/tests/SymbolsTest.cpp
@@ -70,18 +70,18 @@
std::string name;
uint64_t func_offset;
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x5000, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
ASSERT_EQ("fake_function", name);
ASSERT_EQ(0U, func_offset);
name.clear();
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x500f, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x500f, &this->memory_, &name, &func_offset));
ASSERT_EQ("fake_function", name);
ASSERT_EQ(0xfU, func_offset);
// Check one before and one after the function.
- ASSERT_FALSE(symbols.GetName<TypeParam>(0x4fff, 0, &this->memory_, &name, &func_offset));
- ASSERT_FALSE(symbols.GetName<TypeParam>(0x5010, 0, &this->memory_, &name, &func_offset));
+ ASSERT_FALSE(symbols.GetName<TypeParam>(0x4fff, &this->memory_, &name, &func_offset));
+ ASSERT_FALSE(symbols.GetName<TypeParam>(0x5010, &this->memory_, &name, &func_offset));
}
TYPED_TEST_P(SymbolsTest, no_symbol) {
@@ -98,7 +98,7 @@
// First verify that we can get the name.
std::string name;
uint64_t func_offset;
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x5000, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
ASSERT_EQ("fake_function", name);
ASSERT_EQ(0U, func_offset);
@@ -107,7 +107,7 @@
this->memory_.SetMemory(offset, &sym, sizeof(sym));
// Clear the cache to force the symbol data to be re-read.
symbols.ClearCache();
- ASSERT_FALSE(symbols.GetName<TypeParam>(0x5000, 0, &this->memory_, &name, &func_offset));
+ ASSERT_FALSE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
// Set the function back, and set the shndx to UNDEF.
sym.st_info = STT_FUNC;
@@ -115,7 +115,7 @@
this->memory_.SetMemory(offset, &sym, sizeof(sym));
// Clear the cache to force the symbol data to be re-read.
symbols.ClearCache();
- ASSERT_FALSE(symbols.GetName<TypeParam>(0x5000, 0, &this->memory_, &name, &func_offset));
+ ASSERT_FALSE(symbols.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
}
TYPED_TEST_P(SymbolsTest, multiple_entries) {
@@ -144,34 +144,34 @@
std::string name;
uint64_t func_offset;
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x3005, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x3005, &this->memory_, &name, &func_offset));
ASSERT_EQ("function_two", name);
ASSERT_EQ(1U, func_offset);
name.clear();
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x5004, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x5004, &this->memory_, &name, &func_offset));
ASSERT_EQ("function_one", name);
ASSERT_EQ(4U, func_offset);
name.clear();
- ASSERT_TRUE(symbols.GetName<TypeParam>(0xa011, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0xa011, &this->memory_, &name, &func_offset));
ASSERT_EQ("function_three", name);
ASSERT_EQ(1U, func_offset);
// Reget some of the others to verify getting one function name doesn't
// affect any of the next calls.
name.clear();
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x5008, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x5008, &this->memory_, &name, &func_offset));
ASSERT_EQ("function_one", name);
ASSERT_EQ(8U, func_offset);
name.clear();
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x3008, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x3008, &this->memory_, &name, &func_offset));
ASSERT_EQ("function_two", name);
ASSERT_EQ(4U, func_offset);
name.clear();
- ASSERT_TRUE(symbols.GetName<TypeParam>(0xa01a, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0xa01a, &this->memory_, &name, &func_offset));
ASSERT_EQ("function_three", name);
ASSERT_EQ(0xaU, func_offset);
}
@@ -203,47 +203,21 @@
std::string name;
uint64_t func_offset;
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x3005, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x3005, &this->memory_, &name, &func_offset));
ASSERT_EQ("function_two", name);
ASSERT_EQ(1U, func_offset);
name.clear();
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x5004, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x5004, &this->memory_, &name, &func_offset));
ASSERT_EQ("function_one", name);
ASSERT_EQ(4U, func_offset);
name.clear();
- ASSERT_TRUE(symbols.GetName<TypeParam>(0xa011, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0xa011, &this->memory_, &name, &func_offset));
ASSERT_EQ("function_three", name);
ASSERT_EQ(1U, func_offset);
}
-TYPED_TEST_P(SymbolsTest, load_bias) {
- Symbols symbols(0x1000, sizeof(TypeParam), sizeof(TypeParam), 0x2000, 0x100);
-
- TypeParam sym;
- this->InitSym(&sym, 0x5000, 0x10, 0x40);
- uint64_t offset = 0x1000;
- this->memory_.SetMemory(offset, &sym, sizeof(sym));
-
- std::string fake_name("fake_function");
- this->memory_.SetMemory(0x2040, fake_name.c_str(), fake_name.size() + 1);
-
- // Set a non-zero load_bias that should be a valid function offset.
- std::string name;
- uint64_t func_offset;
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x5004, 0x1000, &this->memory_, &name, &func_offset));
- ASSERT_EQ("fake_function", name);
- ASSERT_EQ(4U, func_offset);
-
- // Set a flag that should cause the load_bias to be ignored.
- sym.st_shndx = SHN_ABS;
- this->memory_.SetMemory(offset, &sym, sizeof(sym));
- // Clear the cache to force the symbol data to be re-read.
- symbols.ClearCache();
- ASSERT_FALSE(symbols.GetName<TypeParam>(0x5004, 0x1000, &this->memory_, &name, &func_offset));
-}
-
TYPED_TEST_P(SymbolsTest, symtab_value_out_of_bounds) {
Symbols symbols_end_at_100(0x1000, sizeof(TypeParam) * 2, sizeof(TypeParam), 0x2000, 0x100);
Symbols symbols_end_at_200(0x1000, sizeof(TypeParam) * 2, sizeof(TypeParam), 0x2000, 0x200);
@@ -265,18 +239,16 @@
std::string name;
uint64_t func_offset;
// Verify that we can get the function name properly for both entries.
- ASSERT_TRUE(symbols_end_at_200.GetName<TypeParam>(0x5000, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols_end_at_200.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
ASSERT_EQ("fake_function", name);
ASSERT_EQ(0U, func_offset);
- ASSERT_TRUE(symbols_end_at_200.GetName<TypeParam>(0x3000, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols_end_at_200.GetName<TypeParam>(0x3000, &this->memory_, &name, &func_offset));
ASSERT_EQ("function", name);
ASSERT_EQ(0U, func_offset);
// Now use the symbol table that ends at 0x100.
- ASSERT_FALSE(
- symbols_end_at_100.GetName<TypeParam>(0x5000, 0, &this->memory_, &name, &func_offset));
- ASSERT_FALSE(
- symbols_end_at_100.GetName<TypeParam>(0x3000, 0, &this->memory_, &name, &func_offset));
+ ASSERT_FALSE(symbols_end_at_100.GetName<TypeParam>(0x5000, &this->memory_, &name, &func_offset));
+ ASSERT_FALSE(symbols_end_at_100.GetName<TypeParam>(0x3000, &this->memory_, &name, &func_offset));
}
// Verify the entire func table is cached.
@@ -302,9 +274,9 @@
// Do call that should cache all of the entries (except the string data).
std::string name;
uint64_t func_offset;
- ASSERT_FALSE(symbols.GetName<TypeParam>(0x6000, 0, &this->memory_, &name, &func_offset));
+ ASSERT_FALSE(symbols.GetName<TypeParam>(0x6000, &this->memory_, &name, &func_offset));
this->memory_.Clear();
- ASSERT_FALSE(symbols.GetName<TypeParam>(0x6000, 0, &this->memory_, &name, &func_offset));
+ ASSERT_FALSE(symbols.GetName<TypeParam>(0x6000, &this->memory_, &name, &func_offset));
// Clear the memory and only put the symbol data string data in memory.
this->memory_.Clear();
@@ -317,15 +289,15 @@
fake_name = "third_entry";
this->memory_.SetMemory(0xa300, fake_name.c_str(), fake_name.size() + 1);
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x5001, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x5001, &this->memory_, &name, &func_offset));
ASSERT_EQ("first_entry", name);
ASSERT_EQ(1U, func_offset);
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x2002, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x2002, &this->memory_, &name, &func_offset));
ASSERT_EQ("second_entry", name);
ASSERT_EQ(2U, func_offset);
- ASSERT_TRUE(symbols.GetName<TypeParam>(0x1003, 0, &this->memory_, &name, &func_offset));
+ ASSERT_TRUE(symbols.GetName<TypeParam>(0x1003, &this->memory_, &name, &func_offset));
ASSERT_EQ("third_entry", name);
ASSERT_EQ(3U, func_offset);
}
@@ -381,17 +353,17 @@
EXPECT_FALSE(symbols.GetGlobal<TypeParam>(&this->memory_, "function_1", &offset));
std::string name;
- EXPECT_TRUE(symbols.GetName<TypeParam>(0x10002, 0, &this->memory_, &name, &offset));
+ EXPECT_TRUE(symbols.GetName<TypeParam>(0x10002, &this->memory_, &name, &offset));
EXPECT_EQ("function_0", name);
EXPECT_EQ(2U, offset);
- EXPECT_TRUE(symbols.GetName<TypeParam>(0x12004, 0, &this->memory_, &name, &offset));
+ EXPECT_TRUE(symbols.GetName<TypeParam>(0x12004, &this->memory_, &name, &offset));
EXPECT_EQ("function_1", name);
EXPECT_EQ(4U, offset);
}
REGISTER_TYPED_TEST_CASE_P(SymbolsTest, function_bounds_check, no_symbol, multiple_entries,
- multiple_entries_nonstandard_size, load_bias, symtab_value_out_of_bounds,
+ multiple_entries_nonstandard_size, symtab_value_out_of_bounds,
symtab_read_cached, get_global);
typedef ::testing::Types<Elf32_Sym, Elf64_Sym> SymbolsTestTypes;
diff --git a/libunwindstack/tests/TestLocal.cpp b/libunwindstack/tests/TestLocal.cpp
new file mode 100644
index 0000000..fa0baff
--- /dev/null
+++ b/libunwindstack/tests/TestLocal.cpp
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2018 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 <unwindstack/LocalUnwinder.h>
+
+#include <vector>
+
+extern "C" void TestlibLevel4(void* unwinder_data, void* frame_data) {
+ unwindstack::LocalUnwinder* unwinder =
+ reinterpret_cast<unwindstack::LocalUnwinder*>(unwinder_data);
+ std::vector<unwindstack::LocalFrameData>* frame_info =
+ reinterpret_cast<std::vector<unwindstack::LocalFrameData>*>(frame_data);
+ unwinder->Unwind(frame_info, 256);
+}
+
+extern "C" void TestlibLevel3(void* unwinder_data, void* frame_data) {
+ TestlibLevel4(unwinder_data, frame_data);
+}
+
+extern "C" void TestlibLevel2(void* unwinder_data, void* frame_data) {
+ TestlibLevel3(unwinder_data, frame_data);
+}
+
+extern "C" void TestlibLevel1(void* unwinder_data, void* frame_data) {
+ TestlibLevel2(unwinder_data, frame_data);
+}
diff --git a/libunwindstack/tests/UnwindOfflineTest.cpp b/libunwindstack/tests/UnwindOfflineTest.cpp
index af4a5b5..a65c077 100644
--- a/libunwindstack/tests/UnwindOfflineTest.cpp
+++ b/libunwindstack/tests/UnwindOfflineTest.cpp
@@ -18,6 +18,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
+#include <sys/mman.h>
#include <unistd.h>
#include <gtest/gtest.h>
@@ -45,6 +46,12 @@
namespace unwindstack {
+static void AddMemory(std::string file_name, MemoryOfflineParts* parts) {
+ MemoryOffline* memory = new MemoryOffline;
+ ASSERT_TRUE(memory->Init(file_name.c_str(), 0));
+ parts->Add(memory);
+}
+
class UnwindOfflineTest : public ::testing::Test {
protected:
void TearDown() override {
@@ -63,9 +70,24 @@
maps_.reset(new BufferMaps(data.c_str()));
ASSERT_TRUE(maps_->Parse());
- std::unique_ptr<MemoryOffline> stack_memory(new MemoryOffline);
- ASSERT_TRUE(stack_memory->Init((dir_ + "stack.data").c_str(), 0));
- process_memory_.reset(stack_memory.release());
+ std::string stack_name(dir_ + "stack.data");
+ struct stat st;
+ if (stat(stack_name.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
+ std::unique_ptr<MemoryOffline> stack_memory(new MemoryOffline);
+ ASSERT_TRUE(stack_memory->Init((dir_ + "stack.data").c_str(), 0));
+ process_memory_.reset(stack_memory.release());
+ } else {
+ std::unique_ptr<MemoryOfflineParts> stack_memory(new MemoryOfflineParts);
+ for (size_t i = 0;; i++) {
+ stack_name = dir_ + "stack" + std::to_string(i) + ".data";
+ if (stat(stack_name.c_str(), &st) == -1 || !S_ISREG(st.st_mode)) {
+ ASSERT_TRUE(i != 0) << "No stack data files found.";
+ break;
+ }
+ AddMemory(stack_name, stack_memory.get());
+ }
+ process_memory_.reset(stack_memory.release());
+ }
switch (arch) {
case ARCH_ARM: {
@@ -122,7 +144,6 @@
(*regs)[entry->second] = value;
}
fclose(fp);
- regs->SetFromRaw();
}
static std::unordered_map<std::string, uint32_t> arm_regs_;
@@ -180,7 +201,7 @@
}
TEST_F(UnwindOfflineTest, pc_straddle_arm) {
- Init("straddle_arm/", ARCH_ARM);
+ ASSERT_NO_FATAL_FAILURE(Init("straddle_arm/", ARCH_ARM));
Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
unwinder.Unwind();
@@ -188,7 +209,7 @@
std::string frame_info(DumpFrames(unwinder));
ASSERT_EQ(4U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
EXPECT_EQ(
- " #00 pc 0001a9f8 libc.so (abort+63)\n"
+ " #00 pc 0001a9f8 libc.so (abort+64)\n"
" #01 pc 00006a1b libbase.so (_ZN7android4base14DefaultAborterEPKc+6)\n"
" #02 pc 00007441 libbase.so (_ZN7android4base10LogMessageD2Ev+748)\n"
" #03 pc 00015147 /does/not/exist/libhidlbase.so\n",
@@ -204,7 +225,7 @@
}
TEST_F(UnwindOfflineTest, pc_in_gnu_debugdata_arm) {
- Init("gnu_debugdata_arm/", ARCH_ARM);
+ ASSERT_NO_FATAL_FAILURE(Init("gnu_debugdata_arm/", ARCH_ARM));
Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
unwinder.Unwind();
@@ -224,7 +245,7 @@
}
TEST_F(UnwindOfflineTest, pc_straddle_arm64) {
- Init("straddle_arm64/", ARCH_ARM64);
+ ASSERT_NO_FATAL_FAILURE(Init("straddle_arm64/", ARCH_ARM64));
Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
unwinder.Unwind();
@@ -255,14 +276,8 @@
EXPECT_EQ(0x7fe0d84110U, unwinder.frames()[5].sp);
}
-static void AddMemory(std::string file_name, MemoryOfflineParts* parts) {
- MemoryOffline* memory = new MemoryOffline;
- ASSERT_TRUE(memory->Init(file_name.c_str(), 0));
- parts->Add(memory);
-}
-
TEST_F(UnwindOfflineTest, jit_debug_x86) {
- Init("jit_debug_x86/", ARCH_X86);
+ ASSERT_NO_FATAL_FAILURE(Init("jit_debug_x86/", ARCH_X86));
MemoryOfflineParts* memory = new MemoryOfflineParts;
AddMemory(dir_ + "descriptor.data", memory);
@@ -285,7 +300,7 @@
" #01 pc 00067f00 libarttestd.so (Java_Main_unwindInProcess+10032)\n"
" #02 pc 000021a8 (offset 0x2000) 137-cfi.odex (boolean Main.unwindInProcess(boolean, int, "
"boolean)+136)\n"
- " #03 pc 0000fe81 anonymous:ee74c000 (boolean Main.bar(boolean)+65)\n"
+ " #03 pc 0000fe80 anonymous:ee74c000 (boolean Main.bar(boolean)+64)\n"
" #04 pc 006ad4d2 libartd.so (art_quick_invoke_stub+338)\n"
" #05 pc 00146ab5 libartd.so "
"(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+885)\n"
@@ -300,7 +315,7 @@
"20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
" #09 pc 00684362 libartd.so (artQuickToInterpreterBridge+1058)\n"
" #10 pc 006b35bd libartd.so (art_quick_to_interpreter_bridge+77)\n"
- " #11 pc 0000fe04 anonymous:ee74c000 (int Main.compare(Main, Main)+52)\n"
+ " #11 pc 0000fe03 anonymous:ee74c000 (int Main.compare(Main, Main)+51)\n"
" #12 pc 006ad4d2 libartd.so (art_quick_invoke_stub+338)\n"
" #13 pc 00146ab5 libartd.so "
"(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+885)\n"
@@ -315,8 +330,8 @@
"20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
" #17 pc 00684362 libartd.so (artQuickToInterpreterBridge+1058)\n"
" #18 pc 006b35bd libartd.so (art_quick_to_interpreter_bridge+77)\n"
- " #19 pc 0000fd3c anonymous:ee74c000 (int Main.compare(java.lang.Object, "
- "java.lang.Object)+108)\n"
+ " #19 pc 0000fd3b anonymous:ee74c000 (int Main.compare(java.lang.Object, "
+ "java.lang.Object)+107)\n"
" #20 pc 006ad4d2 libartd.so (art_quick_invoke_stub+338)\n"
" #21 pc 00146ab5 libartd.so "
"(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+885)\n"
@@ -331,9 +346,9 @@
"20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
" #25 pc 00684362 libartd.so (artQuickToInterpreterBridge+1058)\n"
" #26 pc 006b35bd libartd.so (art_quick_to_interpreter_bridge+77)\n"
- " #27 pc 0000fbdc anonymous:ee74c000 (int "
+ " #27 pc 0000fbdb anonymous:ee74c000 (int "
"java.util.Arrays.binarySearch0(java.lang.Object[], int, int, java.lang.Object, "
- "java.util.Comparator)+332)\n"
+ "java.util.Comparator)+331)\n"
" #28 pc 006ad6a2 libartd.so (art_quick_invoke_static_stub+418)\n"
" #29 pc 00146acb libartd.so "
"(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+907)\n"
@@ -348,7 +363,7 @@
"20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
" #33 pc 00684362 libartd.so (artQuickToInterpreterBridge+1058)\n"
" #34 pc 006b35bd libartd.so (art_quick_to_interpreter_bridge+77)\n"
- " #35 pc 0000f625 anonymous:ee74c000 (boolean Main.foo()+165)\n"
+ " #35 pc 0000f624 anonymous:ee74c000 (boolean Main.foo()+164)\n"
" #36 pc 006ad4d2 libartd.so (art_quick_invoke_stub+338)\n"
" #37 pc 00146ab5 libartd.so "
"(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+885)\n"
@@ -363,7 +378,7 @@
"20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
" #41 pc 00684362 libartd.so (artQuickToInterpreterBridge+1058)\n"
" #42 pc 006b35bd libartd.so (art_quick_to_interpreter_bridge+77)\n"
- " #43 pc 0000eedc anonymous:ee74c000 (void Main.runPrimary()+60)\n"
+ " #43 pc 0000eedb anonymous:ee74c000 (void Main.runPrimary()+59)\n"
" #44 pc 006ad4d2 libartd.so (art_quick_invoke_stub+338)\n"
" #45 pc 00146ab5 libartd.so "
"(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+885)\n"
@@ -378,7 +393,7 @@
"20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
" #49 pc 00684362 libartd.so (artQuickToInterpreterBridge+1058)\n"
" #50 pc 006b35bd libartd.so (art_quick_to_interpreter_bridge+77)\n"
- " #51 pc 0000ac22 anonymous:ee74c000 (void Main.main(java.lang.String[])+98)\n"
+ " #51 pc 0000ac21 anonymous:ee74c000 (void Main.main(java.lang.String[])+97)\n"
" #52 pc 006ad6a2 libartd.so (art_quick_invoke_static_stub+418)\n"
" #53 pc 00146acb libartd.so "
"(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+907)\n"
@@ -420,7 +435,7 @@
EXPECT_EQ(0xffeb52a0U, unwinder.frames()[1].sp);
EXPECT_EQ(0xec6061a8U, unwinder.frames()[2].pc);
EXPECT_EQ(0xffeb5ce0U, unwinder.frames()[2].sp);
- EXPECT_EQ(0xee75be81U, unwinder.frames()[3].pc);
+ EXPECT_EQ(0xee75be80U, unwinder.frames()[3].pc);
EXPECT_EQ(0xffeb5d30U, unwinder.frames()[3].sp);
EXPECT_EQ(0xf728e4d2U, unwinder.frames()[4].pc);
EXPECT_EQ(0xffeb5d60U, unwinder.frames()[4].sp);
@@ -436,7 +451,7 @@
EXPECT_EQ(0xffeb5fb0U, unwinder.frames()[9].sp);
EXPECT_EQ(0xf72945bdU, unwinder.frames()[10].pc);
EXPECT_EQ(0xffeb6110U, unwinder.frames()[10].sp);
- EXPECT_EQ(0xee75be04U, unwinder.frames()[11].pc);
+ EXPECT_EQ(0xee75be03U, unwinder.frames()[11].pc);
EXPECT_EQ(0xffeb6160U, unwinder.frames()[11].sp);
EXPECT_EQ(0xf728e4d2U, unwinder.frames()[12].pc);
EXPECT_EQ(0xffeb6180U, unwinder.frames()[12].sp);
@@ -452,7 +467,7 @@
EXPECT_EQ(0xffeb63e0U, unwinder.frames()[17].sp);
EXPECT_EQ(0xf72945bdU, unwinder.frames()[18].pc);
EXPECT_EQ(0xffeb6530U, unwinder.frames()[18].sp);
- EXPECT_EQ(0xee75bd3cU, unwinder.frames()[19].pc);
+ EXPECT_EQ(0xee75bd3bU, unwinder.frames()[19].pc);
EXPECT_EQ(0xffeb6580U, unwinder.frames()[19].sp);
EXPECT_EQ(0xf728e4d2U, unwinder.frames()[20].pc);
EXPECT_EQ(0xffeb65b0U, unwinder.frames()[20].sp);
@@ -468,7 +483,7 @@
EXPECT_EQ(0xffeb6810U, unwinder.frames()[25].sp);
EXPECT_EQ(0xf72945bdU, unwinder.frames()[26].pc);
EXPECT_EQ(0xffeb6960U, unwinder.frames()[26].sp);
- EXPECT_EQ(0xee75bbdcU, unwinder.frames()[27].pc);
+ EXPECT_EQ(0xee75bbdbU, unwinder.frames()[27].pc);
EXPECT_EQ(0xffeb69b0U, unwinder.frames()[27].sp);
EXPECT_EQ(0xf728e6a2U, unwinder.frames()[28].pc);
EXPECT_EQ(0xffeb69f0U, unwinder.frames()[28].sp);
@@ -484,7 +499,7 @@
EXPECT_EQ(0xffeb6c50U, unwinder.frames()[33].sp);
EXPECT_EQ(0xf72945bdU, unwinder.frames()[34].pc);
EXPECT_EQ(0xffeb6dd0U, unwinder.frames()[34].sp);
- EXPECT_EQ(0xee75b625U, unwinder.frames()[35].pc);
+ EXPECT_EQ(0xee75b624U, unwinder.frames()[35].pc);
EXPECT_EQ(0xffeb6e20U, unwinder.frames()[35].sp);
EXPECT_EQ(0xf728e4d2U, unwinder.frames()[36].pc);
EXPECT_EQ(0xffeb6e50U, unwinder.frames()[36].sp);
@@ -500,7 +515,7 @@
EXPECT_EQ(0xffeb70a0U, unwinder.frames()[41].sp);
EXPECT_EQ(0xf72945bdU, unwinder.frames()[42].pc);
EXPECT_EQ(0xffeb71f0U, unwinder.frames()[42].sp);
- EXPECT_EQ(0xee75aedcU, unwinder.frames()[43].pc);
+ EXPECT_EQ(0xee75aedbU, unwinder.frames()[43].pc);
EXPECT_EQ(0xffeb7240U, unwinder.frames()[43].sp);
EXPECT_EQ(0xf728e4d2U, unwinder.frames()[44].pc);
EXPECT_EQ(0xffeb72a0U, unwinder.frames()[44].sp);
@@ -516,7 +531,7 @@
EXPECT_EQ(0xffeb74f0U, unwinder.frames()[49].sp);
EXPECT_EQ(0xf72945bdU, unwinder.frames()[50].pc);
EXPECT_EQ(0xffeb7680U, unwinder.frames()[50].sp);
- EXPECT_EQ(0xee756c22U, unwinder.frames()[51].pc);
+ EXPECT_EQ(0xee756c21U, unwinder.frames()[51].pc);
EXPECT_EQ(0xffeb76d0U, unwinder.frames()[51].sp);
EXPECT_EQ(0xf728e6a2U, unwinder.frames()[52].pc);
EXPECT_EQ(0xffeb76f0U, unwinder.frames()[52].sp);
@@ -555,7 +570,7 @@
}
TEST_F(UnwindOfflineTest, jit_debug_arm) {
- Init("jit_debug_arm/", ARCH_ARM);
+ ASSERT_NO_FATAL_FAILURE(Init("jit_debug_arm/", ARCH_ARM));
MemoryOfflineParts* memory = new MemoryOfflineParts;
AddMemory(dir_ + "descriptor.data", memory);
@@ -575,7 +590,7 @@
std::string frame_info(DumpFrames(unwinder));
ASSERT_EQ(76U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
EXPECT_EQ(
- " #00 pc 00018a5e libarttestd.so (Java_Main_unwindInProcess+865)\n"
+ " #00 pc 00018a5e libarttestd.so (Java_Main_unwindInProcess+866)\n"
" #01 pc 0000212d (offset 0x2000) 137-cfi.odex (boolean Main.unwindInProcess(boolean, int, "
"boolean)+92)\n"
" #02 pc 00011cb1 anonymous:e2796000 (boolean Main.bar(boolean)+72)\n"
@@ -873,7 +888,7 @@
// fallback to iterating over the cies/fdes and ignore the eh_frame_hdr.
// No .gnu_debugdata section in the elf file, so no symbols.
TEST_F(UnwindOfflineTest, bad_eh_frame_hdr_arm64) {
- Init("bad_eh_frame_hdr_arm64/", ARCH_ARM64);
+ ASSERT_NO_FATAL_FAILURE(Init("bad_eh_frame_hdr_arm64/", ARCH_ARM64));
Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
unwinder.Unwind();
@@ -902,7 +917,7 @@
// The elf has bad eh_frame unwind information for the pcs. If eh_frame
// is used first, the unwind will not match the expected output.
TEST_F(UnwindOfflineTest, debug_frame_first_x86) {
- Init("debug_frame_first_x86/", ARCH_X86);
+ ASSERT_NO_FATAL_FAILURE(Init("debug_frame_first_x86/", ARCH_X86));
Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
unwinder.Unwind();
@@ -930,7 +945,7 @@
// Make sure that a pc that is at the beginning of an fde unwinds correctly.
TEST_F(UnwindOfflineTest, eh_frame_hdr_begin_x86_64) {
- Init("eh_frame_hdr_begin_x86_64/", ARCH_X86_64);
+ ASSERT_NO_FATAL_FAILURE(Init("eh_frame_hdr_begin_x86_64/", ARCH_X86_64));
Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
unwinder.Unwind();
@@ -956,4 +971,272 @@
EXPECT_EQ(0x7ffcc85971a0U, unwinder.frames()[4].sp);
}
+TEST_F(UnwindOfflineTest, art_quick_osr_stub_arm) {
+ ASSERT_NO_FATAL_FAILURE(Init("art_quick_osr_stub_arm/", ARCH_ARM));
+
+ MemoryOfflineParts* memory = new MemoryOfflineParts;
+ AddMemory(dir_ + "descriptor.data", memory);
+ AddMemory(dir_ + "stack.data", memory);
+ for (size_t i = 0; i < 2; i++) {
+ AddMemory(dir_ + "entry" + std::to_string(i) + ".data", memory);
+ AddMemory(dir_ + "jit" + std::to_string(i) + ".data", memory);
+ }
+ process_memory_.reset(memory);
+
+ JitDebug jit_debug(process_memory_);
+ Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
+ unwinder.SetJitDebug(&jit_debug, regs_->Arch());
+ unwinder.Unwind();
+
+ std::string frame_info(DumpFrames(unwinder));
+ ASSERT_EQ(25U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
+ EXPECT_EQ(
+ " #00 pc 0000c788 <anonymous:d0250000> "
+ "(com.example.simpleperf.simpleperfexamplewithnative.MixActivity.access$000)\n"
+ " #01 pc 0000cdd5 <anonymous:d0250000> "
+ "(com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run+60)\n"
+ " #02 pc 004135bb libart.so (art_quick_osr_stub+42)\n"
+ " #03 pc 002657a5 libart.so "
+ "(_ZN3art3jit3Jit25MaybeDoOnStackReplacementEPNS_6ThreadEPNS_9ArtMethodEjiPNS_6JValueE+876)\n"
+ " #04 pc 004021a7 libart.so (MterpMaybeDoOnStackReplacement+86)\n"
+ " #05 pc 00412474 libart.so (ExecuteMterpImpl+66164)\n"
+ " #06 pc cd8365b0 <unknown>\n" // symbol in dex file
+ " #07 pc 001d7f1b libart.so "
+ "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
+ "6JValueEb+374)\n"
+ " #08 pc 001dc593 libart.so "
+ "(_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadERKNS_"
+ "20CodeItemDataAccessorEPNS_11ShadowFrameEPNS_6JValueE+154)\n"
+ " #09 pc 001f4d01 libart.so "
+ "(_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_"
+ "11InstructionEtPNS_6JValueE+732)\n"
+ " #10 pc 003fe427 libart.so (MterpInvokeInterface+1354)\n"
+ " #11 pc 00405b94 libart.so (ExecuteMterpImpl+14740)\n"
+ " #12 pc 7004873e <unknown>\n" // symbol in dex file
+ " #13 pc 001d7f1b libart.so "
+ "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
+ "6JValueEb+374)\n"
+ " #14 pc 001dc4d5 libart.so "
+ "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
+ "20CodeItemDataAccessorEPNS_11ShadowFrameE+92)\n"
+ " #15 pc 003f25ab libart.so (artQuickToInterpreterBridge+970)\n"
+ " #16 pc 00417aff libart.so (art_quick_to_interpreter_bridge+30)\n"
+ " #17 pc 00413575 libart.so (art_quick_invoke_stub_internal+68)\n"
+ " #18 pc 00418531 libart.so (art_quick_invoke_stub+236)\n"
+ " #19 pc 000b468d libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+136)\n"
+ " #20 pc 00362f49 libart.so "
+ "(_ZN3art12_GLOBAL__N_118InvokeWithArgArrayERKNS_33ScopedObjectAccessAlreadyRunnableEPNS_"
+ "9ArtMethodEPNS0_8ArgArrayEPNS_6JValueEPKc+52)\n"
+ " #21 pc 00363cd9 libart.so "
+ "(_ZN3art35InvokeVirtualOrInterfaceWithJValuesERKNS_33ScopedObjectAccessAlreadyRunnableEP8_"
+ "jobjectP10_jmethodIDP6jvalue+332)\n"
+ " #22 pc 003851dd libart.so (_ZN3art6Thread14CreateCallbackEPv+868)\n"
+ " #23 pc 00062925 libc.so (_ZL15__pthread_startPv+22)\n"
+ " #24 pc 0001de39 libc.so (__start_thread+24)\n",
+ frame_info);
+ EXPECT_EQ(0xd025c788U, unwinder.frames()[0].pc);
+ EXPECT_EQ(0xcd4ff140U, unwinder.frames()[0].sp);
+ EXPECT_EQ(0xd025cdd5U, unwinder.frames()[1].pc);
+ EXPECT_EQ(0xcd4ff140U, unwinder.frames()[1].sp);
+ EXPECT_EQ(0xe4a755bbU, unwinder.frames()[2].pc);
+ EXPECT_EQ(0xcd4ff160U, unwinder.frames()[2].sp);
+ EXPECT_EQ(0xe48c77a5U, unwinder.frames()[3].pc);
+ EXPECT_EQ(0xcd4ff190U, unwinder.frames()[3].sp);
+ EXPECT_EQ(0xe4a641a7U, unwinder.frames()[4].pc);
+ EXPECT_EQ(0xcd4ff298U, unwinder.frames()[4].sp);
+ EXPECT_EQ(0xe4a74474U, unwinder.frames()[5].pc);
+ EXPECT_EQ(0xcd4ff2b8U, unwinder.frames()[5].sp);
+ EXPECT_EQ(0xcd8365b0U, unwinder.frames()[6].pc);
+ EXPECT_EQ(0xcd4ff2e0U, unwinder.frames()[6].sp);
+ EXPECT_EQ(0xe4839f1bU, unwinder.frames()[7].pc);
+ EXPECT_EQ(0xcd4ff2e0U, unwinder.frames()[7].sp);
+ EXPECT_EQ(0xe483e593U, unwinder.frames()[8].pc);
+ EXPECT_EQ(0xcd4ff330U, unwinder.frames()[8].sp);
+ EXPECT_EQ(0xe4856d01U, unwinder.frames()[9].pc);
+ EXPECT_EQ(0xcd4ff380U, unwinder.frames()[9].sp);
+ EXPECT_EQ(0xe4a60427U, unwinder.frames()[10].pc);
+ EXPECT_EQ(0xcd4ff430U, unwinder.frames()[10].sp);
+ EXPECT_EQ(0xe4a67b94U, unwinder.frames()[11].pc);
+ EXPECT_EQ(0xcd4ff498U, unwinder.frames()[11].sp);
+ EXPECT_EQ(0x7004873eU, unwinder.frames()[12].pc);
+ EXPECT_EQ(0xcd4ff4c0U, unwinder.frames()[12].sp);
+ EXPECT_EQ(0xe4839f1bU, unwinder.frames()[13].pc);
+ EXPECT_EQ(0xcd4ff4c0U, unwinder.frames()[13].sp);
+ EXPECT_EQ(0xe483e4d5U, unwinder.frames()[14].pc);
+ EXPECT_EQ(0xcd4ff510U, unwinder.frames()[14].sp);
+ EXPECT_EQ(0xe4a545abU, unwinder.frames()[15].pc);
+ EXPECT_EQ(0xcd4ff538U, unwinder.frames()[15].sp);
+ EXPECT_EQ(0xe4a79affU, unwinder.frames()[16].pc);
+ EXPECT_EQ(0xcd4ff640U, unwinder.frames()[16].sp);
+ EXPECT_EQ(0xe4a75575U, unwinder.frames()[17].pc);
+ EXPECT_EQ(0xcd4ff6b0U, unwinder.frames()[17].sp);
+ EXPECT_EQ(0xe4a7a531U, unwinder.frames()[18].pc);
+ EXPECT_EQ(0xcd4ff6e8U, unwinder.frames()[18].sp);
+ EXPECT_EQ(0xe471668dU, unwinder.frames()[19].pc);
+ EXPECT_EQ(0xcd4ff770U, unwinder.frames()[19].sp);
+ EXPECT_EQ(0xe49c4f49U, unwinder.frames()[20].pc);
+ EXPECT_EQ(0xcd4ff7c8U, unwinder.frames()[20].sp);
+ EXPECT_EQ(0xe49c5cd9U, unwinder.frames()[21].pc);
+ EXPECT_EQ(0xcd4ff850U, unwinder.frames()[21].sp);
+ EXPECT_EQ(0xe49e71ddU, unwinder.frames()[22].pc);
+ EXPECT_EQ(0xcd4ff8e8U, unwinder.frames()[22].sp);
+ EXPECT_EQ(0xe7df3925U, unwinder.frames()[23].pc);
+ EXPECT_EQ(0xcd4ff958U, unwinder.frames()[23].sp);
+ EXPECT_EQ(0xe7daee39U, unwinder.frames()[24].pc);
+ EXPECT_EQ(0xcd4ff960U, unwinder.frames()[24].sp);
+}
+
+TEST_F(UnwindOfflineTest, jit_map_arm) {
+ ASSERT_NO_FATAL_FAILURE(Init("jit_map_arm/", ARCH_ARM));
+
+ maps_->Add(0xd025c788, 0xd025c9f0, 0, PROT_READ | PROT_EXEC | MAPS_FLAGS_JIT_SYMFILE_MAP,
+ "jit_map0.so", 0);
+ maps_->Add(0xd025cd98, 0xd025cff4, 0, PROT_READ | PROT_EXEC | MAPS_FLAGS_JIT_SYMFILE_MAP,
+ "jit_map1.so", 0);
+ maps_->Sort();
+
+ Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
+ unwinder.Unwind();
+
+ std::string frame_info(DumpFrames(unwinder));
+ ASSERT_EQ(6U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
+ EXPECT_EQ(
+ " #00 pc 00000000 jit_map0.so "
+ "(com.example.simpleperf.simpleperfexamplewithnative.MixActivity.access$000)\n"
+ " #01 pc 0000003d jit_map1.so "
+ "(com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run+60)\n"
+ " #02 pc 004135bb libart.so (art_quick_osr_stub+42)\n"
+
+ " #03 pc 003851dd libart.so (_ZN3art6Thread14CreateCallbackEPv+868)\n"
+ " #04 pc 00062925 libc.so (_ZL15__pthread_startPv+22)\n"
+ " #05 pc 0001de39 libc.so (__start_thread+24)\n",
+ frame_info);
+
+ EXPECT_EQ(0xd025c788U, unwinder.frames()[0].pc);
+ EXPECT_EQ(0xcd4ff140U, unwinder.frames()[0].sp);
+ EXPECT_EQ(0xd025cdd5U, unwinder.frames()[1].pc);
+ EXPECT_EQ(0xcd4ff140U, unwinder.frames()[1].sp);
+ EXPECT_EQ(0xe4a755bbU, unwinder.frames()[2].pc);
+ EXPECT_EQ(0xcd4ff160U, unwinder.frames()[2].sp);
+ EXPECT_EQ(0xe49e71ddU, unwinder.frames()[3].pc);
+ EXPECT_EQ(0xcd4ff8e8U, unwinder.frames()[3].sp);
+ EXPECT_EQ(0xe7df3925U, unwinder.frames()[4].pc);
+ EXPECT_EQ(0xcd4ff958U, unwinder.frames()[4].sp);
+ EXPECT_EQ(0xe7daee39U, unwinder.frames()[5].pc);
+ EXPECT_EQ(0xcd4ff960U, unwinder.frames()[5].sp);
+}
+
+TEST_F(UnwindOfflineTest, offset_arm) {
+ ASSERT_NO_FATAL_FAILURE(Init("offset_arm/", ARCH_ARM));
+
+ Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
+ unwinder.Unwind();
+
+ std::string frame_info(DumpFrames(unwinder));
+ ASSERT_EQ(19U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
+ EXPECT_EQ(
+ " #00 pc 0032bfa0 (offset 0x42000) libunwindstack_test (SignalInnerFunction+40)\n"
+ " #01 pc 0032bfeb (offset 0x42000) libunwindstack_test (SignalMiddleFunction+2)\n"
+ " #02 pc 0032bff3 (offset 0x42000) libunwindstack_test (SignalOuterFunction+2)\n"
+ " #03 pc 0032fed3 (offset 0x42000) libunwindstack_test "
+ "(_ZN11unwindstackL19SignalCallerHandlerEiP7siginfoPv+26)\n"
+ " #04 pc 00026528 (offset 0x25000) libc.so\n"
+ " #05 pc 00000000 <unknown>\n"
+ " #06 pc 0032c2d9 (offset 0x42000) libunwindstack_test (InnerFunction+736)\n"
+ " #07 pc 0032cc4f (offset 0x42000) libunwindstack_test (MiddleFunction+42)\n"
+ " #08 pc 0032cc81 (offset 0x42000) libunwindstack_test (OuterFunction+42)\n"
+ " #09 pc 0032e547 (offset 0x42000) libunwindstack_test "
+ "(_ZN11unwindstackL19RemoteThroughSignalEij+270)\n"
+ " #10 pc 0032ed99 (offset 0x42000) libunwindstack_test "
+ "(_ZN11unwindstack55UnwindTest_remote_through_signal_with_invalid_func_Test8TestBodyEv+16)\n"
+ " #11 pc 00354453 (offset 0x42000) libunwindstack_test (_ZN7testing4Test3RunEv+154)\n"
+ " #12 pc 00354de7 (offset 0x42000) libunwindstack_test (_ZN7testing8TestInfo3RunEv+194)\n"
+ " #13 pc 00355105 (offset 0x42000) libunwindstack_test (_ZN7testing8TestCase3RunEv+180)\n"
+ " #14 pc 0035a215 (offset 0x42000) libunwindstack_test "
+ "(_ZN7testing8internal12UnitTestImpl11RunAllTestsEv+664)\n"
+ " #15 pc 00359f4f (offset 0x42000) libunwindstack_test (_ZN7testing8UnitTest3RunEv+110)\n"
+ " #16 pc 0034d3db (offset 0x42000) libunwindstack_test (main+38)\n"
+ " #17 pc 00092c0d (offset 0x25000) libc.so (__libc_init+48)\n"
+ " #18 pc 0004202f (offset 0x42000) libunwindstack_test (_start_main+38)\n",
+ frame_info);
+
+ EXPECT_EQ(0x2e55fa0U, unwinder.frames()[0].pc);
+ EXPECT_EQ(0xf43d2cccU, unwinder.frames()[0].sp);
+ EXPECT_EQ(0x2e55febU, unwinder.frames()[1].pc);
+ EXPECT_EQ(0xf43d2ce0U, unwinder.frames()[1].sp);
+ EXPECT_EQ(0x2e55ff3U, unwinder.frames()[2].pc);
+ EXPECT_EQ(0xf43d2ce8U, unwinder.frames()[2].sp);
+ EXPECT_EQ(0x2e59ed3U, unwinder.frames()[3].pc);
+ EXPECT_EQ(0xf43d2cf0U, unwinder.frames()[3].sp);
+ EXPECT_EQ(0xf4136528U, unwinder.frames()[4].pc);
+ EXPECT_EQ(0xf43d2d10U, unwinder.frames()[4].sp);
+ EXPECT_EQ(0U, unwinder.frames()[5].pc);
+ EXPECT_EQ(0xffcc0ee0U, unwinder.frames()[5].sp);
+ EXPECT_EQ(0x2e562d9U, unwinder.frames()[6].pc);
+ EXPECT_EQ(0xffcc0ee0U, unwinder.frames()[6].sp);
+ EXPECT_EQ(0x2e56c4fU, unwinder.frames()[7].pc);
+ EXPECT_EQ(0xffcc1060U, unwinder.frames()[7].sp);
+ EXPECT_EQ(0x2e56c81U, unwinder.frames()[8].pc);
+ EXPECT_EQ(0xffcc1078U, unwinder.frames()[8].sp);
+ EXPECT_EQ(0x2e58547U, unwinder.frames()[9].pc);
+ EXPECT_EQ(0xffcc1090U, unwinder.frames()[9].sp);
+ EXPECT_EQ(0x2e58d99U, unwinder.frames()[10].pc);
+ EXPECT_EQ(0xffcc1438U, unwinder.frames()[10].sp);
+ EXPECT_EQ(0x2e7e453U, unwinder.frames()[11].pc);
+ EXPECT_EQ(0xffcc1448U, unwinder.frames()[11].sp);
+ EXPECT_EQ(0x2e7ede7U, unwinder.frames()[12].pc);
+ EXPECT_EQ(0xffcc1458U, unwinder.frames()[12].sp);
+ EXPECT_EQ(0x2e7f105U, unwinder.frames()[13].pc);
+ EXPECT_EQ(0xffcc1490U, unwinder.frames()[13].sp);
+ EXPECT_EQ(0x2e84215U, unwinder.frames()[14].pc);
+ EXPECT_EQ(0xffcc14c0U, unwinder.frames()[14].sp);
+ EXPECT_EQ(0x2e83f4fU, unwinder.frames()[15].pc);
+ EXPECT_EQ(0xffcc1510U, unwinder.frames()[15].sp);
+ EXPECT_EQ(0x2e773dbU, unwinder.frames()[16].pc);
+ EXPECT_EQ(0xffcc1528U, unwinder.frames()[16].sp);
+ EXPECT_EQ(0xf41a2c0dU, unwinder.frames()[17].pc);
+ EXPECT_EQ(0xffcc1540U, unwinder.frames()[17].sp);
+ EXPECT_EQ(0x2b6c02fU, unwinder.frames()[18].pc);
+ EXPECT_EQ(0xffcc1558U, unwinder.frames()[18].sp);
+}
+
+// Test using a non-zero load bias library that has the fde entries
+// encoded as 0xb, which is not set as pc relative.
+TEST_F(UnwindOfflineTest, debug_frame_load_bias_arm) {
+ ASSERT_NO_FATAL_FAILURE(Init("debug_frame_load_bias_arm/", ARCH_ARM));
+
+ Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
+ unwinder.Unwind();
+
+ std::string frame_info(DumpFrames(unwinder));
+ ASSERT_EQ(8U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
+ EXPECT_EQ(
+ " #00 pc 0005138c libc.so (__ioctl+8)\n"
+ " #01 pc 0002140f libc.so (ioctl+30)\n"
+ " #02 pc 00039535 libbinder.so (_ZN7android14IPCThreadState14talkWithDriverEb+204)\n"
+ " #03 pc 00039633 libbinder.so (_ZN7android14IPCThreadState20getAndExecuteCommandEv+10)\n"
+ " #04 pc 00039b57 libbinder.so (_ZN7android14IPCThreadState14joinThreadPoolEb+38)\n"
+ " #05 pc 00000c21 mediaserver (main+104)\n"
+ " #06 pc 00084b89 libc.so (__libc_init+48)\n"
+ " #07 pc 00000b77 mediaserver (_start_main+38)\n",
+ frame_info);
+
+ EXPECT_EQ(0xf0be238cU, unwinder.frames()[0].pc);
+ EXPECT_EQ(0xffd4a638U, unwinder.frames()[0].sp);
+ EXPECT_EQ(0xf0bb240fU, unwinder.frames()[1].pc);
+ EXPECT_EQ(0xffd4a638U, unwinder.frames()[1].sp);
+ EXPECT_EQ(0xf1a75535U, unwinder.frames()[2].pc);
+ EXPECT_EQ(0xffd4a650U, unwinder.frames()[2].sp);
+ EXPECT_EQ(0xf1a75633U, unwinder.frames()[3].pc);
+ EXPECT_EQ(0xffd4a6b0U, unwinder.frames()[3].sp);
+ EXPECT_EQ(0xf1a75b57U, unwinder.frames()[4].pc);
+ EXPECT_EQ(0xffd4a6d0U, unwinder.frames()[4].sp);
+ EXPECT_EQ(0x8d1cc21U, unwinder.frames()[5].pc);
+ EXPECT_EQ(0xffd4a6e8U, unwinder.frames()[5].sp);
+ EXPECT_EQ(0xf0c15b89U, unwinder.frames()[6].pc);
+ EXPECT_EQ(0xffd4a700U, unwinder.frames()[6].sp);
+ EXPECT_EQ(0x8d1cb77U, unwinder.frames()[7].pc);
+ EXPECT_EQ(0xffd4a718U, unwinder.frames()[7].sp);
+}
+
} // namespace unwindstack
diff --git a/libunwindstack/tests/UnwindTest.cpp b/libunwindstack/tests/UnwindTest.cpp
index 242cc6a..ea992c7 100644
--- a/libunwindstack/tests/UnwindTest.cpp
+++ b/libunwindstack/tests/UnwindTest.cpp
@@ -32,6 +32,7 @@
#include <vector>
#include <android-base/stringprintf.h>
+#include <android-base/threads.h>
#include <unwindstack/Maps.h>
#include <unwindstack/Memory.h>
@@ -106,15 +107,12 @@
Unwinder unwinder(512, maps, regs, process_memory);
unwinder.Unwind();
- std::string expected_function = expected_function_names.back();
- expected_function_names.pop_back();
for (auto& frame : unwinder.frames()) {
- if (frame.function_name == expected_function) {
+ if (frame.function_name == expected_function_names.back()) {
+ expected_function_names.pop_back();
if (expected_function_names.empty()) {
break;
}
- expected_function = expected_function_names.back();
- expected_function_names.pop_back();
}
}
@@ -234,8 +232,7 @@
usleep(1000);
}
ASSERT_NE(0, tid.load());
- // Portable tgkill method.
- ASSERT_EQ(0, syscall(__NR_tgkill, getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
+ ASSERT_EQ(0, tgkill(getpid(), tid.load(), SIGUSR1)) << "Error: " << strerror(errno);
// Wait for context data.
void* ucontext;
diff --git a/libunwindstack/tests/UnwinderTest.cpp b/libunwindstack/tests/UnwinderTest.cpp
index e44b225..2428f68 100644
--- a/libunwindstack/tests/UnwinderTest.cpp
+++ b/libunwindstack/tests/UnwinderTest.cpp
@@ -133,7 +133,7 @@
};
MapsFake UnwinderTest::maps_;
-RegsFake UnwinderTest::regs_(5, 0);
+RegsFake UnwinderTest::regs_(5);
std::shared_ptr<Memory> UnwinderTest::process_memory_(nullptr);
TEST_F(UnwinderTest, multiple_frames) {
@@ -141,8 +141,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x10000);
ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
@@ -201,8 +201,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x10000);
ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
@@ -221,7 +221,7 @@
EXPECT_EQ(0x10000U, frame->sp);
EXPECT_EQ("", frame->function_name);
EXPECT_EQ(0U, frame->function_offset);
- EXPECT_EQ("/system/fake/libc.so", frame->map_name);
+ EXPECT_EQ("", frame->map_name);
EXPECT_EQ(0U, frame->map_offset);
EXPECT_EQ(0x1000U, frame->map_start);
EXPECT_EQ(0x8000U, frame->map_end);
@@ -235,7 +235,7 @@
EXPECT_EQ(0x10010U, frame->sp);
EXPECT_EQ("", frame->function_name);
EXPECT_EQ(0U, frame->function_offset);
- EXPECT_EQ("/system/fake/libc.so", frame->map_name);
+ EXPECT_EQ("", frame->map_name);
EXPECT_EQ(0U, frame->map_offset);
EXPECT_EQ(0x1000U, frame->map_start);
EXPECT_EQ(0x8000U, frame->map_end);
@@ -249,7 +249,7 @@
EXPECT_EQ(0x10020U, frame->sp);
EXPECT_EQ("", frame->function_name);
EXPECT_EQ(0U, frame->function_offset);
- EXPECT_EQ("/system/fake/libc.so", frame->map_name);
+ EXPECT_EQ("", frame->map_name);
EXPECT_EQ(0U, frame->map_offset);
EXPECT_EQ(0x1000U, frame->map_start);
EXPECT_EQ(0x8000U, frame->map_end);
@@ -260,8 +260,8 @@
TEST_F(UnwinderTest, non_zero_load_bias) {
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
- regs_.FakeSetPc(0xa5500);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0xa5500);
+ regs_.set_sp(0x10000);
ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
Unwinder unwinder(64, &maps_, ®s_, process_memory_);
@@ -288,8 +288,8 @@
TEST_F(UnwinderTest, non_zero_elf_offset) {
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
- regs_.FakeSetPc(0xa7500);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0xa7500);
+ regs_.set_sp(0x10000);
ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
Unwinder unwinder(64, &maps_, ®s_, process_memory_);
@@ -316,8 +316,8 @@
TEST_F(UnwinderTest, non_zero_map_offset) {
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
- regs_.FakeSetPc(0x43000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x43000);
+ regs_.set_sp(0x10000);
ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
Unwinder unwinder(64, &maps_, ®s_, process_memory_);
@@ -349,8 +349,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x10000);
ElfInterfaceFake::FakePushStepData(StepData(0x1000, 0x10000, true));
ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
@@ -383,8 +383,8 @@
ElfInterfaceFake::FakePushStepData(StepData(0x1102 + i * 0x100, 0x10010 + i * 0x10, false));
}
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x10000);
Unwinder unwinder(20, &maps_, ®s_, process_memory_);
unwinder.Unwind();
@@ -415,8 +415,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
- regs_.FakeSetPc(0x20000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x20000);
+ regs_.set_sp(0x10000);
ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
ElfInterfaceFake::FakePushStepData(StepData(0x20002, 0x10030, false));
@@ -481,8 +481,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x63000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x63000);
ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x50020, false));
ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
@@ -527,8 +527,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
- regs_.FakeSetPc(0x13000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x13000);
+ regs_.set_sp(0x10000);
ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
@@ -546,8 +546,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x13000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x13000);
ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
@@ -563,8 +563,8 @@
TEST_F(UnwinderTest, pc_without_map) {
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
- regs_.FakeSetPc(0x41000);
- regs_.FakeSetSp(0x13000);
+ regs_.set_pc(0x41000);
+ regs_.set_sp(0x13000);
Unwinder unwinder(64, &maps_, ®s_, process_memory_);
unwinder.Unwind();
@@ -593,8 +593,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
// Fake as if code called a nullptr function.
- regs_.FakeSetPc(0);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0);
+ regs_.set_sp(0x10000);
regs_.FakeSetReturnAddress(0x1202);
regs_.FakeSetReturnAddressValid(true);
@@ -657,8 +657,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
// Fake as if code called a nullptr function.
- regs_.FakeSetPc(0);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0);
+ regs_.set_sp(0x10000);
regs_.FakeSetReturnAddress(0x1202);
regs_.FakeSetReturnAddressValid(true);
@@ -691,8 +691,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
// Fake as if code called a nullptr function.
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x10000);
ElfInterfaceFake::FakePushStepData(StepData(0x43402, 0x10010, false));
ElfInterfaceFake::FakePushStepData(StepData(0x53502, 0x10020, false));
ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
@@ -745,8 +745,8 @@
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x10000);
ElfInterfaceFake::FakePushStepData(StepData(0x33402, 0x10010, false));
ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
@@ -805,8 +805,8 @@
TEST_F(UnwinderTest, dex_pc_in_map) {
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x10000);
regs_.FakeSetDexPc(0xa3400);
Unwinder unwinder(64, &maps_, ®s_, process_memory_);
@@ -846,8 +846,8 @@
TEST_F(UnwinderTest, dex_pc_not_in_map) {
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x10000);
regs_.FakeSetDexPc(0x50000);
Unwinder unwinder(64, &maps_, ®s_, process_memory_);
@@ -888,8 +888,8 @@
TEST_F(UnwinderTest, dex_pc_multiple_frames) {
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
- regs_.FakeSetPc(0x1000);
- regs_.FakeSetSp(0x10000);
+ regs_.set_pc(0x1000);
+ regs_.set_sp(0x10000);
regs_.FakeSetDexPc(0xa3400);
ElfInterfaceFake::FakePushStepData(StepData(0x33402, 0x10010, false));
ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
diff --git a/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/descriptor.data b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/descriptor.data
new file mode 100644
index 0000000..300646b
--- /dev/null
+++ b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/descriptor.data
Binary files differ
diff --git a/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/entry0.data b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/entry0.data
new file mode 100644
index 0000000..999cb79
--- /dev/null
+++ b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/entry0.data
Binary files differ
diff --git a/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/entry1.data b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/entry1.data
new file mode 100644
index 0000000..6aa1c82
--- /dev/null
+++ b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/entry1.data
Binary files differ
diff --git a/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/jit0.data b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/jit0.data
new file mode 100644
index 0000000..19d7b65
--- /dev/null
+++ b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/jit0.data
Binary files differ
diff --git a/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/jit1.data b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/jit1.data
new file mode 100644
index 0000000..edcd3e1
--- /dev/null
+++ b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/jit1.data
Binary files differ
diff --git a/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/libart.so b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/libart.so
new file mode 100644
index 0000000..09ba495
--- /dev/null
+++ b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/libart.so
Binary files differ
diff --git a/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/libc.so b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/libc.so
new file mode 100644
index 0000000..39c9025
--- /dev/null
+++ b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/libc.so
Binary files differ
diff --git a/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/maps.txt b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/maps.txt
new file mode 100644
index 0000000..55aaaf6
--- /dev/null
+++ b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/maps.txt
@@ -0,0 +1,3 @@
+d0250000-d2600000 r-xp 0 00:00 0 <anonymous:d0250000>
+e466e000-e4ae8000 r-xp 0 00:00 0 libart.so
+e7d91000-e7e31000 r-xp 0 00:00 0 libc.so
diff --git a/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/regs.txt b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/regs.txt
new file mode 100644
index 0000000..0b51814
--- /dev/null
+++ b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/regs.txt
@@ -0,0 +1,16 @@
+r0: e814103c
+r1: 12dcf218
+r2: 1a90df75
+r3: ffffffbf
+r4: 0
+r5: 12dc0800
+r6: 12dcf218
+r7: 1a90df75
+r8: 0
+r9: dd23cc00
+r10: 1c
+r11: cd4ff16c
+ip: 0
+sp: cd4ff140
+lr: d025cdd7
+pc: d025c788
diff --git a/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/stack.data b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/stack.data
new file mode 100644
index 0000000..f00917b
--- /dev/null
+++ b/libunwindstack/tests/files/offline/art_quick_osr_stub_arm/stack.data
Binary files differ
diff --git a/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/libbinder.so b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/libbinder.so
new file mode 100644
index 0000000..4b7bf44
--- /dev/null
+++ b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/libbinder.so
Binary files differ
diff --git a/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/libc.so b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/libc.so
new file mode 100644
index 0000000..013858e
--- /dev/null
+++ b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/libc.so
Binary files differ
diff --git a/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/maps.txt b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/maps.txt
new file mode 100644
index 0000000..10f1325
--- /dev/null
+++ b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/maps.txt
@@ -0,0 +1,3 @@
+8d1c000-8d1f000 r-xp 0 00:00 0 mediaserver
+f0b91000-f0c2c000 r-xp 0 00:00 0 libc.so
+f1a41000-f1a97000 r-xp 0 00:00 0 libbinder.so
diff --git a/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/mediaserver b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/mediaserver
new file mode 100644
index 0000000..9e4a83f
--- /dev/null
+++ b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/mediaserver
Binary files differ
diff --git a/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/regs.txt b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/regs.txt
new file mode 100644
index 0000000..f147247
--- /dev/null
+++ b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/regs.txt
@@ -0,0 +1,16 @@
+r0: 3
+r1: c0306201
+r2: ffd4a658
+r3: 0
+r4: f0c36d8c
+r5: ffd4a658
+r6: f0168000
+r7: 36
+r8: ffd4a678
+r9: f016802c
+r10: ffd4a660
+r11: 0
+ip: 0
+sp: ffd4a638
+lr: f0bb2413
+pc: f0be238c
diff --git a/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/stack.data b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/stack.data
new file mode 100644
index 0000000..847c819
--- /dev/null
+++ b/libunwindstack/tests/files/offline/debug_frame_load_bias_arm/stack.data
Binary files differ
diff --git a/libunwindstack/tests/files/offline/jit_map_arm/jit_map0.so b/libunwindstack/tests/files/offline/jit_map_arm/jit_map0.so
new file mode 100644
index 0000000..e667883
--- /dev/null
+++ b/libunwindstack/tests/files/offline/jit_map_arm/jit_map0.so
Binary files differ
diff --git a/libunwindstack/tests/files/offline/jit_map_arm/jit_map1.so b/libunwindstack/tests/files/offline/jit_map_arm/jit_map1.so
new file mode 100644
index 0000000..9a1d714
--- /dev/null
+++ b/libunwindstack/tests/files/offline/jit_map_arm/jit_map1.so
Binary files differ
diff --git a/libunwindstack/tests/files/offline/jit_map_arm/libart.so b/libunwindstack/tests/files/offline/jit_map_arm/libart.so
new file mode 100644
index 0000000..09ba495
--- /dev/null
+++ b/libunwindstack/tests/files/offline/jit_map_arm/libart.so
Binary files differ
diff --git a/libunwindstack/tests/files/offline/jit_map_arm/libc.so b/libunwindstack/tests/files/offline/jit_map_arm/libc.so
new file mode 100644
index 0000000..39c9025
--- /dev/null
+++ b/libunwindstack/tests/files/offline/jit_map_arm/libc.so
Binary files differ
diff --git a/libunwindstack/tests/files/offline/jit_map_arm/maps.txt b/libunwindstack/tests/files/offline/jit_map_arm/maps.txt
new file mode 100644
index 0000000..5aaec54
--- /dev/null
+++ b/libunwindstack/tests/files/offline/jit_map_arm/maps.txt
@@ -0,0 +1,2 @@
+e466e000-e4ae8000 r-xp 0 00:00 0 libart.so
+e7d91000-e7e31000 r-xp 0 00:00 0 libc.so
diff --git a/libunwindstack/tests/files/offline/jit_map_arm/regs.txt b/libunwindstack/tests/files/offline/jit_map_arm/regs.txt
new file mode 100644
index 0000000..0b51814
--- /dev/null
+++ b/libunwindstack/tests/files/offline/jit_map_arm/regs.txt
@@ -0,0 +1,16 @@
+r0: e814103c
+r1: 12dcf218
+r2: 1a90df75
+r3: ffffffbf
+r4: 0
+r5: 12dc0800
+r6: 12dcf218
+r7: 1a90df75
+r8: 0
+r9: dd23cc00
+r10: 1c
+r11: cd4ff16c
+ip: 0
+sp: cd4ff140
+lr: d025cdd7
+pc: d025c788
diff --git a/libunwindstack/tests/files/offline/jit_map_arm/stack.data b/libunwindstack/tests/files/offline/jit_map_arm/stack.data
new file mode 100644
index 0000000..fb8feeb
--- /dev/null
+++ b/libunwindstack/tests/files/offline/jit_map_arm/stack.data
Binary files differ
diff --git a/libunwindstack/tests/files/offline/offset_arm/libc.so b/libunwindstack/tests/files/offline/offset_arm/libc.so
new file mode 100644
index 0000000..9f5c8ca
--- /dev/null
+++ b/libunwindstack/tests/files/offline/offset_arm/libc.so
Binary files differ
diff --git a/libunwindstack/tests/files/offline/offset_arm/libunwindstack_test b/libunwindstack/tests/files/offline/offset_arm/libunwindstack_test
new file mode 100644
index 0000000..7a30bfa
--- /dev/null
+++ b/libunwindstack/tests/files/offline/offset_arm/libunwindstack_test
Binary files differ
diff --git a/libunwindstack/tests/files/offline/offset_arm/maps.txt b/libunwindstack/tests/files/offline/offset_arm/maps.txt
new file mode 100644
index 0000000..6224464
--- /dev/null
+++ b/libunwindstack/tests/files/offline/offset_arm/maps.txt
@@ -0,0 +1,2 @@
+2b6c000-2e92000 r-xp 42000 00:00 0 libunwindstack_test
+f4135000-f41a9000 r-xp 25000 00:00 0 libc.so
diff --git a/libunwindstack/tests/files/offline/offset_arm/regs.txt b/libunwindstack/tests/files/offline/offset_arm/regs.txt
new file mode 100644
index 0000000..1f4ac8f
--- /dev/null
+++ b/libunwindstack/tests/files/offline/offset_arm/regs.txt
@@ -0,0 +1,16 @@
+r0: 5
+r1: 5
+r2: 4
+r3: 1
+r4: 73804b6b
+r5: f3c9c000
+r6: 2ea09ac
+r7: 10624dd3
+r8: f41b5d8c
+r9: f3c9c000
+r10: 6f17
+r11: f3c94048
+ip: 2ea0807
+sp: f43d2ccc
+lr: 2e55fef
+pc: 2e55fa0
diff --git a/libunwindstack/tests/files/offline/offset_arm/stack0.data b/libunwindstack/tests/files/offline/offset_arm/stack0.data
new file mode 100644
index 0000000..23a9874
--- /dev/null
+++ b/libunwindstack/tests/files/offline/offset_arm/stack0.data
Binary files differ
diff --git a/libunwindstack/tests/files/offline/offset_arm/stack1.data b/libunwindstack/tests/files/offline/offset_arm/stack1.data
new file mode 100644
index 0000000..49bdd1e
--- /dev/null
+++ b/libunwindstack/tests/files/offline/offset_arm/stack1.data
Binary files differ
diff --git a/libunwindstack/tools/unwind_for_offline.cpp b/libunwindstack/tools/unwind_for_offline.cpp
index 74868d4..640992f 100644
--- a/libunwindstack/tools/unwind_for_offline.cpp
+++ b/libunwindstack/tools/unwind_for_offline.cpp
@@ -30,6 +30,7 @@
#include <memory>
#include <string>
#include <unordered_map>
+#include <utility>
#include <vector>
#include <unwindstack/Elf.h>
@@ -68,7 +69,7 @@
bool SaveRegs(unwindstack::Regs* regs) {
std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("regs.txt", "w+"), &fclose);
if (fp == nullptr) {
- printf("Failed to create file regs.txt.\n");
+ perror("Failed to create file regs.txt");
return false;
}
regs->IterateRegisters([&fp](const char* name, uint64_t value) {
@@ -78,30 +79,45 @@
return true;
}
-bool SaveStack(pid_t pid, uint64_t sp_start, uint64_t sp_end) {
- std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("stack.data", "w+"), &fclose);
- if (fp == nullptr) {
- printf("Failed to create stack.data.\n");
- return false;
- }
+bool SaveStack(pid_t pid, const std::vector<std::pair<uint64_t, uint64_t>>& stacks) {
+ for (size_t i = 0; i < stacks.size(); i++) {
+ std::string file_name;
+ if (stacks.size() != 1) {
+ file_name = "stack" + std::to_string(i) + ".data";
+ } else {
+ file_name = "stack.data";
+ }
- size_t bytes = fwrite(&sp_start, 1, sizeof(sp_start), fp.get());
- if (bytes != sizeof(sp_start)) {
- perror("Failed to write all data.");
- return false;
- }
+ // Do this first, so if it fails, we don't create the file.
+ uint64_t sp_start = stacks[i].first;
+ uint64_t sp_end = stacks[i].second;
+ std::vector<uint8_t> buffer(sp_end - sp_start);
+ auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);
+ if (!process_memory->Read(sp_start, buffer.data(), buffer.size())) {
+ printf("Unable to read stack data.\n");
+ return false;
+ }
- std::vector<uint8_t> buffer(sp_end - sp_start);
- auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);
- if (!process_memory->Read(sp_start, buffer.data(), buffer.size())) {
- printf("Unable to read stack data.\n");
- return false;
- }
+ printf("Saving the stack 0x%" PRIx64 "-0x%" PRIx64 "\n", sp_start, sp_end);
- bytes = fwrite(buffer.data(), 1, buffer.size(), fp.get());
- if (bytes != buffer.size()) {
- printf("Failed to write all stack data: stack size %zu, written %zu\n", buffer.size(), bytes);
- return 1;
+ std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(file_name.c_str(), "w+"), &fclose);
+ if (fp == nullptr) {
+ perror("Failed to create stack.data");
+ return false;
+ }
+
+ size_t bytes = fwrite(&sp_start, 1, sizeof(sp_start), fp.get());
+ if (bytes != sizeof(sp_start)) {
+ printf("Failed to write sp_start data: sizeof(sp_start) %zu, written %zu\n", sizeof(sp_start),
+ bytes);
+ return false;
+ }
+
+ bytes = fwrite(buffer.data(), 1, buffer.size(), fp.get());
+ if (bytes != buffer.size()) {
+ printf("Failed to write all stack data: stack size %zu, written %zu\n", buffer.size(), bytes);
+ return false;
+ }
}
return true;
@@ -110,17 +126,11 @@
bool CreateElfFromMemory(std::shared_ptr<unwindstack::Memory>& memory, map_info_t* info) {
std::string cur_name;
if (info->name.empty()) {
- cur_name = android::base::StringPrintf("anonymous:%" PRIx64, info->start);
+ cur_name = android::base::StringPrintf("anonymous_%" PRIx64, info->start);
} else {
- cur_name = basename(info->name.c_str());
- cur_name = android::base::StringPrintf("%s:%" PRIx64, basename(info->name.c_str()), info->start);
+ cur_name = android::base::StringPrintf("%s_%" PRIx64, basename(info->name.c_str()), info->start);
}
- std::unique_ptr<FILE, decltype(&fclose)> output(fopen(cur_name.c_str(), "w+"), &fclose);
- if (output == nullptr) {
- printf("Cannot create %s\n", cur_name.c_str());
- return false;
- }
std::vector<uint8_t> buffer(info->end - info->start);
// If this is a mapped in file, it might not be possible to read the entire
// map, so read all that is readable.
@@ -129,6 +139,13 @@
printf("Cannot read data from address %" PRIx64 " length %zu\n", info->start, buffer.size());
return false;
}
+
+ std::unique_ptr<FILE, decltype(&fclose)> output(fopen(cur_name.c_str(), "w+"), &fclose);
+ if (output == nullptr) {
+ perror((std::string("Cannot create ") + cur_name).c_str());
+ return false;
+ }
+
size_t bytes_written = fwrite(buffer.data(), 1, bytes, output.get());
if (bytes_written != bytes) {
printf("Failed to write all data to file: bytes read %zu, written %zu\n", bytes, bytes_written);
@@ -144,13 +161,14 @@
bool CopyElfFromFile(map_info_t* info) {
std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(info->name.c_str(), "r"), &fclose);
if (fp == nullptr) {
+ perror((std::string("Cannot open ") + info->name).c_str());
return false;
}
std::string cur_name = basename(info->name.c_str());
std::unique_ptr<FILE, decltype(&fclose)> output(fopen(cur_name.c_str(), "w+"), &fclose);
if (output == nullptr) {
- printf("Cannot create file %s\n", cur_name.c_str());
+ perror((std::string("Cannot create file " + cur_name)).c_str());
return false;
}
std::vector<uint8_t> buffer(10000);
@@ -198,9 +216,21 @@
unwinder.Unwind();
std::unordered_map<uint64_t, map_info_t> maps_by_start;
- uint64_t last_sp;
+ std::vector<std::pair<uint64_t, uint64_t>> stacks;
+ uint64_t sp_map_start = 0;
+ unwindstack::MapInfo* map_info = maps.Find(sp);
+ if (map_info != nullptr) {
+ stacks.emplace_back(std::make_pair(sp, map_info->end));
+ sp_map_start = map_info->start;
+ }
+
for (auto frame : unwinder.frames()) {
- last_sp = frame.sp;
+ map_info = maps.Find(frame.sp);
+ if (map_info != nullptr && sp_map_start != map_info->start) {
+ stacks.emplace_back(std::make_pair(frame.sp, map_info->end));
+ sp_map_start = map_info->start;
+ }
+
if (maps_by_start.count(frame.map_start) == 0) {
auto info = &maps_by_start[frame.map_start];
info->start = frame.map_start;
@@ -211,7 +241,12 @@
// Try to create the elf from memory, this will handle cases where
// the data only exists in memory such as vdso data on x86.
if (!CreateElfFromMemory(process_memory, info)) {
- return 1;
+ printf("Ignoring map ");
+ if (!info->name.empty()) {
+ printf("%s\n", info->name.c_str());
+ } else {
+ printf("anonymous:%" PRIx64 "\n", info->start);
+ }
}
}
}
@@ -221,7 +256,7 @@
printf("%s\n", unwinder.FormatFrame(i).c_str());
}
- if (!SaveStack(pid, sp, last_sp)) {
+ if (!SaveStack(pid, stacks)) {
return 1;
}
@@ -232,7 +267,7 @@
std::unique_ptr<FILE, decltype(&fclose)> fp(fopen("maps.txt", "w+"), &fclose);
if (fp == nullptr) {
- printf("Failed to create maps.txt.\n");
+ perror("Failed to create maps.txt");
return false;
}
diff --git a/libunwindstack/tools/unwind_info.cpp b/libunwindstack/tools/unwind_info.cpp
index a0abcca..266a6db 100644
--- a/libunwindstack/tools/unwind_info.cpp
+++ b/libunwindstack/tools/unwind_info.cpp
@@ -37,7 +37,7 @@
namespace unwindstack {
-void DumpArm(ElfInterfaceArm* interface) {
+void DumpArm(Elf* elf, ElfInterfaceArm* interface) {
if (interface == nullptr) {
printf("No ARM Unwind Information.\n\n");
return;
@@ -48,13 +48,11 @@
uint64_t load_bias = entry.second.table_offset;
printf(" PC Range 0x%" PRIx64 " - 0x%" PRIx64 "\n", entry.second.offset + load_bias,
entry.second.table_size + load_bias);
- for (auto addr : *interface) {
+ for (auto pc : *interface) {
std::string name;
- printf(" PC 0x%" PRIx64, addr + load_bias);
+ printf(" PC 0x%" PRIx64, pc + load_bias);
uint64_t func_offset;
- uint64_t pc = addr + load_bias;
- // This might be a thumb function, so set the low bit.
- if (interface->GetFunctionName(pc | 1, load_bias, &name, &func_offset) && !name.empty()) {
+ if (elf->GetFunctionName(pc + load_bias, &name, &func_offset) && !name.empty()) {
printf(" <%s>", name.c_str());
}
printf("\n");
@@ -64,7 +62,7 @@
continue;
}
ArmExidx arm(nullptr, interface->memory(), nullptr);
- arm.set_log(true);
+ arm.set_log(ARM_LOG_FULL);
arm.set_log_skip_execution(true);
arm.set_log_indent(2);
if (!arm.ExtractEntryData(entry)) {
@@ -83,21 +81,21 @@
printf("\n");
}
-void DumpDwarfSection(ElfInterface* interface, DwarfSection* section, uint64_t load_bias) {
+void DumpDwarfSection(Elf* elf, DwarfSection* section, uint64_t) {
for (const DwarfFde* fde : *section) {
// Sometimes there are entries that have empty length, skip those since
// they don't contain any interesting information.
if (fde == nullptr || fde->pc_start == fde->pc_end) {
continue;
}
- printf("\n PC 0x%" PRIx64, fde->pc_start + load_bias);
+ printf("\n PC 0x%" PRIx64 "-0x%" PRIx64, fde->pc_start, fde->pc_end);
std::string name;
uint64_t func_offset;
- if (interface->GetFunctionName(fde->pc_start, load_bias, &name, &func_offset) && !name.empty()) {
+ if (elf->GetFunctionName(fde->pc_start, &name, &func_offset) && !name.empty()) {
printf(" <%s>", name.c_str());
}
printf("\n");
- if (!section->Log(2, UINT64_MAX, load_bias, fde)) {
+ if (!section->Log(2, UINT64_MAX, fde)) {
printf("Failed to process cfa information for entry at 0x%" PRIx64 "\n", fde->pc_start);
}
}
@@ -127,13 +125,13 @@
ElfInterface* interface = elf.interface();
if (elf.machine_type() == EM_ARM) {
- DumpArm(reinterpret_cast<ElfInterfaceArm*>(interface));
+ DumpArm(&elf, reinterpret_cast<ElfInterfaceArm*>(interface));
printf("\n");
}
if (interface->eh_frame() != nullptr) {
printf("eh_frame information:\n");
- DumpDwarfSection(interface, interface->eh_frame(), elf.GetLoadBias());
+ DumpDwarfSection(&elf, interface->eh_frame(), elf.GetLoadBias());
printf("\n");
} else {
printf("\nno eh_frame information\n");
@@ -141,7 +139,7 @@
if (interface->debug_frame() != nullptr) {
printf("\ndebug_frame information:\n");
- DumpDwarfSection(interface, interface->debug_frame(), elf.GetLoadBias());
+ DumpDwarfSection(&elf, interface->debug_frame(), elf.GetLoadBias());
printf("\n");
} else {
printf("\nno debug_frame information\n");
@@ -152,12 +150,12 @@
if (gnu_debugdata_interface != nullptr) {
if (gnu_debugdata_interface->eh_frame() != nullptr) {
printf("\ngnu_debugdata (eh_frame):\n");
- DumpDwarfSection(gnu_debugdata_interface, gnu_debugdata_interface->eh_frame(), 0);
+ DumpDwarfSection(&elf, gnu_debugdata_interface->eh_frame(), 0);
printf("\n");
}
if (gnu_debugdata_interface->debug_frame() != nullptr) {
printf("\ngnu_debugdata (debug_frame):\n");
- DumpDwarfSection(gnu_debugdata_interface, gnu_debugdata_interface->debug_frame(), 0);
+ DumpDwarfSection(&elf, gnu_debugdata_interface->debug_frame(), 0);
printf("\n");
}
} else {
diff --git a/libunwindstack/tools/unwind_reg_info.cpp b/libunwindstack/tools/unwind_reg_info.cpp
index 47a4f91..0f01566 100644
--- a/libunwindstack/tools/unwind_reg_info.cpp
+++ b/libunwindstack/tools/unwind_reg_info.cpp
@@ -34,7 +34,9 @@
#include <unwindstack/ElfInterface.h>
#include <unwindstack/Log.h>
+#include "ArmExidx.h"
#include "DwarfOp.h"
+#include "ElfInterfaceArm.h"
namespace unwindstack {
@@ -136,6 +138,32 @@
}
}
+void PrintArmRegInformation(ElfInterfaceArm* interface, uint64_t pc) {
+ printf("\nArm exidx:\n");
+ uint64_t entry_offset;
+ if (!interface->FindEntry(pc, &entry_offset)) {
+ return;
+ }
+
+ ArmExidx arm(nullptr, interface->memory(), nullptr);
+
+ log_to_stdout(true);
+ arm.set_log(ARM_LOG_BY_REG);
+ arm.set_log_skip_execution(true);
+ arm.set_log_indent(1);
+ if (!arm.ExtractEntryData(entry_offset)) {
+ if (arm.status() != ARM_STATUS_NO_UNWIND) {
+ printf(" Error trying to extract data.\n");
+ }
+ return;
+ }
+ if (arm.data()->size() != 0 && arm.Eval()) {
+ arm.LogByReg();
+ } else {
+ printf(" Error tring to evaluate exidx data.\n");
+ }
+}
+
int GetInfo(const char* file, uint64_t pc) {
MemoryFileAtOffset* memory = new MemoryFileAtOffset;
if (!memory->Init(file, 0)) {
@@ -162,12 +190,22 @@
printf("Soname: %s\n\n", soname.c_str());
}
- printf("PC 0x%" PRIx64 ":\n", pc);
+ printf("PC 0x%" PRIx64, pc);
+ std::string function_name;
+ uint64_t function_offset;
+ if (elf.GetFunctionName(pc, &function_name, &function_offset)) {
+ printf(" (%s)", function_name.c_str());
+ }
+ printf(":\n");
+
+ if (elf.machine_type() == EM_ARM) {
+ PrintArmRegInformation(reinterpret_cast<ElfInterfaceArm*>(interface), pc - load_bias);
+ }
DwarfSection* section = interface->eh_frame();
if (section != nullptr) {
printf("\neh_frame:\n");
- PrintRegInformation(section, memory, pc - load_bias, elf.class_type());
+ PrintRegInformation(section, memory, pc, elf.class_type());
} else {
printf("\nno eh_frame information\n");
}
@@ -175,7 +213,7 @@
section = interface->debug_frame();
if (section != nullptr) {
printf("\ndebug_frame:\n");
- PrintRegInformation(section, memory, pc - load_bias, elf.class_type());
+ PrintRegInformation(section, memory, pc, elf.class_type());
printf("\n");
} else {
printf("\nno debug_frame information\n");
diff --git a/libunwindstack/tools/unwind_symbols.cpp b/libunwindstack/tools/unwind_symbols.cpp
index 086dffe..f8e3e92 100644
--- a/libunwindstack/tools/unwind_symbols.cpp
+++ b/libunwindstack/tools/unwind_symbols.cpp
@@ -95,7 +95,6 @@
}
std::string name;
- uint64_t load_bias = elf.GetLoadBias();
if (argc == 3) {
std::string cur_name;
uint64_t func_offset;
@@ -113,8 +112,8 @@
// This is a crude way to get the symbols in order.
for (const auto& entry : elf.interface()->pt_loads()) {
- uint64_t start = entry.second.offset + load_bias;
- uint64_t end = entry.second.table_size + load_bias;
+ uint64_t start = entry.second.offset;
+ uint64_t end = entry.second.table_size;
for (uint64_t addr = start; addr < end; addr += 4) {
std::string cur_name;
uint64_t func_offset;
diff --git a/libusbhost/include/usbhost/usbhost.h b/libusbhost/include/usbhost/usbhost.h
index 9758b18..7e62542 100644
--- a/libusbhost/include/usbhost/usbhost.h
+++ b/libusbhost/include/usbhost/usbhost.h
@@ -137,6 +137,7 @@
/* Returns the USB product ID from the device descriptor for the USB device */
uint16_t usb_device_get_product_id(struct usb_device *device);
+/* Returns a pointer to device descriptor */
const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device);
/* Returns a USB descriptor string for the given string ID.
@@ -156,6 +157,12 @@
int usb_device_get_string_ucs2(struct usb_device* device, int id, int timeout, void** ucs2_out,
size_t* response_size);
+/* Returns the length in bytes read into the raw descriptors array */
+size_t usb_device_get_descriptors_length(const struct usb_device* device);
+
+/* Returns a pointer to the raw descriptors array */
+const unsigned char* usb_device_get_raw_descriptors(const struct usb_device* device);
+
/* Returns a USB descriptor string for the given string ID.
* Used to implement usb_device_get_manufacturer_name,
* usb_device_get_product_name and usb_device_get_serial.
diff --git a/libusbhost/usbhost.c b/libusbhost/usbhost.c
index cb8d430..415488f 100644
--- a/libusbhost/usbhost.c
+++ b/libusbhost/usbhost.c
@@ -76,9 +76,11 @@
int wddbus;
};
+#define MAX_DESCRIPTORS_LENGTH 4096
+
struct usb_device {
char dev_name[64];
- unsigned char desc[4096];
+ unsigned char desc[MAX_DESCRIPTORS_LENGTH];
int desc_length;
int fd;
int writeable;
@@ -387,6 +389,8 @@
return device;
failed:
+ // TODO It would be more appropriate to have callers do this
+ // since this function doesn't "own" this file descriptor.
close(fd);
free(device);
return NULL;
@@ -455,11 +459,18 @@
return __le16_to_cpu(desc->idProduct);
}
-const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device)
-{
+const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device* device) {
return (struct usb_device_descriptor*)device->desc;
}
+size_t usb_device_get_descriptors_length(const struct usb_device* device) {
+ return device->desc_length;
+}
+
+const unsigned char* usb_device_get_raw_descriptors(const struct usb_device* device) {
+ return device->desc;
+}
+
/* Returns a USB descriptor string for the given string ID.
* Return value: < 0 on error. 0 on success.
* The string is returned in ucs2_out in USB-native UCS-2 encoding.
diff --git a/libutils/Android.bp b/libutils/Android.bp
index 80dcdcb..d635e65 100644
--- a/libutils/Android.bp
+++ b/libutils/Android.bp
@@ -15,6 +15,7 @@
cc_library_headers {
name: "libutils_headers",
vendor_available: true,
+ recovery_available: true,
host_supported: true,
header_libs: [
@@ -46,6 +47,7 @@
cc_defaults {
name: "libutils_defaults",
vendor_available: true,
+ recovery_available: true,
vndk: {
enabled: true,
support_system_process: true,
@@ -89,6 +91,10 @@
},
},
+ recovery: {
+ exclude_shared_libs: ["libvndksupport"],
+ },
+
host: {
cflags: ["-DLIBUTILS_NATIVE=1"],
@@ -129,7 +135,6 @@
"PropertyMap.cpp",
"RefBase.cpp",
"SharedBuffer.cpp",
- "Static.cpp",
"StopWatch.cpp",
"String8.cpp",
"String16.cpp",
@@ -150,6 +155,7 @@
],
},
linux: {
+ shared_libs: ["libbase"],
srcs: [
"Looper.cpp",
],
@@ -160,7 +166,6 @@
cc_library {
name: "libutilscallstack",
defaults: ["libutils_defaults"],
- vendor_available: false,
srcs: [
"CallStack.cpp",
diff --git a/libutils/CallStack.cpp b/libutils/CallStack.cpp
index bd6015e..fe6f33d 100644
--- a/libutils/CallStack.cpp
+++ b/libutils/CallStack.cpp
@@ -16,16 +16,15 @@
#define LOG_TAG "CallStack"
-#include <utils/CallStack.h>
-
-#include <memory>
-
#include <utils/Printer.h>
#include <utils/Errors.h>
#include <utils/Log.h>
#include <backtrace/Backtrace.h>
+#define CALLSTACK_WEAK // Don't generate weak definitions.
+#include <utils/CallStack.h>
+
namespace android {
CallStack::CallStack() {
@@ -76,4 +75,30 @@
}
}
+// The following four functions may be used via weak symbol references from libutils.
+// Clients assume that if any of these symbols are available, then deleteStack() is.
+
+#ifdef WEAKS_AVAILABLE
+
+CallStack::CallStackUPtr CallStack::getCurrentInternal(int ignoreDepth) {
+ CallStack::CallStackUPtr stack(new CallStack());
+ stack->update(ignoreDepth + 1);
+ return stack;
+}
+
+void CallStack::logStackInternal(const char* logtag, const CallStack* stack,
+ android_LogPriority priority) {
+ stack->log(logtag, priority);
+}
+
+String8 CallStack::stackToStringInternal(const char* prefix, const CallStack* stack) {
+ return stack->toString(prefix);
+}
+
+void CallStack::deleteStack(CallStack* stack) {
+ delete stack;
+}
+
+#endif // WEAKS_AVAILABLE
+
}; // namespace android
diff --git a/libutils/FileMap.cpp b/libutils/FileMap.cpp
index 3c4d81c..583c6b9 100644
--- a/libutils/FileMap.cpp
+++ b/libutils/FileMap.cpp
@@ -48,10 +48,10 @@
// Constructor. Create an empty object.
FileMap::FileMap(void)
- : mFileName(NULL),
- mBasePtr(NULL),
+ : mFileName(nullptr),
+ mBasePtr(nullptr),
mBaseLength(0),
- mDataPtr(NULL),
+ mDataPtr(nullptr),
mDataLength(0)
#if defined(__MINGW32__)
,
@@ -69,9 +69,9 @@
, mFileHandle(other.mFileHandle), mFileMapping(other.mFileMapping)
#endif
{
- other.mFileName = NULL;
- other.mBasePtr = NULL;
- other.mDataPtr = NULL;
+ other.mFileName = nullptr;
+ other.mBasePtr = nullptr;
+ other.mDataPtr = nullptr;
#if defined(__MINGW32__)
other.mFileHandle = INVALID_HANDLE_VALUE;
other.mFileMapping = NULL;
@@ -86,9 +86,9 @@
mDataOffset = other.mDataOffset;
mDataPtr = other.mDataPtr;
mDataLength = other.mDataLength;
- other.mFileName = NULL;
- other.mBasePtr = NULL;
- other.mDataPtr = NULL;
+ other.mFileName = nullptr;
+ other.mBasePtr = nullptr;
+ other.mDataPtr = nullptr;
#if defined(__MINGW32__)
mFileHandle = other.mFileHandle;
mFileMapping = other.mFileMapping;
@@ -101,7 +101,7 @@
// Destructor.
FileMap::~FileMap(void)
{
- if (mFileName != NULL) {
+ if (mFileName != nullptr) {
free(mFileName);
}
#if defined(__MINGW32__)
@@ -196,7 +196,7 @@
if (!readOnly)
prot |= PROT_WRITE;
- ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset);
+ ptr = mmap(nullptr, adjLength, prot, flags, fd, adjOffset);
if (ptr == MAP_FAILED) {
ALOGE("mmap(%lld,%zu) failed: %s\n",
(long long)adjOffset, adjLength, strerror(errno));
@@ -205,7 +205,7 @@
mBasePtr = ptr;
#endif // !defined(__MINGW32__)
- mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
+ mFileName = origFileName != nullptr ? strdup(origFileName) : nullptr;
mBaseLength = adjLength;
mDataOffset = offset;
mDataPtr = (char*) mBasePtr + adjust;
diff --git a/libutils/Looper.cpp b/libutils/Looper.cpp
index 6c57b2e..102fdf0 100644
--- a/libutils/Looper.cpp
+++ b/libutils/Looper.cpp
@@ -29,7 +29,7 @@
void WeakMessageHandler::handleMessage(const Message& message) {
sp<MessageHandler> handler = mHandler.promote();
- if (handler != NULL) {
+ if (handler != nullptr) {
handler->handleMessage(message);
}
}
@@ -60,24 +60,22 @@
static pthread_once_t gTLSOnce = PTHREAD_ONCE_INIT;
static pthread_key_t gTLSKey = 0;
-Looper::Looper(bool allowNonCallbacks) :
- mAllowNonCallbacks(allowNonCallbacks), mSendingMessage(false),
- mPolling(false), mEpollFd(-1), mEpollRebuildRequired(false),
- mNextRequestSeq(0), mResponseIndex(0), mNextMessageUptime(LLONG_MAX) {
- mWakeEventFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
- LOG_ALWAYS_FATAL_IF(mWakeEventFd < 0, "Could not make wake event fd: %s",
- strerror(errno));
+Looper::Looper(bool allowNonCallbacks)
+ : mAllowNonCallbacks(allowNonCallbacks),
+ mSendingMessage(false),
+ mPolling(false),
+ mEpollRebuildRequired(false),
+ mNextRequestSeq(0),
+ mResponseIndex(0),
+ mNextMessageUptime(LLONG_MAX) {
+ mWakeEventFd.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC));
+ LOG_ALWAYS_FATAL_IF(mWakeEventFd.get() < 0, "Could not make wake event fd: %s", strerror(errno));
AutoMutex _l(mLock);
rebuildEpollLocked();
}
Looper::~Looper() {
- close(mWakeEventFd);
- mWakeEventFd = -1;
- if (mEpollFd >= 0) {
- close(mEpollFd);
- }
}
void Looper::initTLSKey() {
@@ -87,7 +85,7 @@
void Looper::threadDestructor(void *st) {
Looper* const self = static_cast<Looper*>(st);
- if (self != NULL) {
+ if (self != nullptr) {
self->decStrong((void*)threadDestructor);
}
}
@@ -95,13 +93,13 @@
void Looper::setForThread(const sp<Looper>& looper) {
sp<Looper> old = getForThread(); // also has side-effect of initializing TLS
- if (looper != NULL) {
+ if (looper != nullptr) {
looper->incStrong((void*)threadDestructor);
}
pthread_setspecific(gTLSKey, looper.get());
- if (old != NULL) {
+ if (old != nullptr) {
old->decStrong((void*)threadDestructor);
}
}
@@ -116,7 +114,7 @@
sp<Looper> Looper::prepare(int opts) {
bool allowNonCallbacks = opts & PREPARE_ALLOW_NON_CALLBACKS;
sp<Looper> looper = Looper::getForThread();
- if (looper == NULL) {
+ if (looper == nullptr) {
looper = new Looper(allowNonCallbacks);
Looper::setForThread(looper);
}
@@ -137,18 +135,18 @@
#if DEBUG_CALLBACKS
ALOGD("%p ~ rebuildEpollLocked - rebuilding epoll set", this);
#endif
- close(mEpollFd);
+ mEpollFd.reset();
}
// Allocate the new epoll instance and register the wake pipe.
- mEpollFd = epoll_create(EPOLL_SIZE_HINT);
+ mEpollFd.reset(epoll_create(EPOLL_SIZE_HINT));
LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno));
struct epoll_event eventItem;
memset(& eventItem, 0, sizeof(epoll_event)); // zero out unused members of data field union
eventItem.events = EPOLLIN;
- eventItem.data.fd = mWakeEventFd;
- int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeEventFd, & eventItem);
+ eventItem.data.fd = mWakeEventFd.get();
+ int result = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, mWakeEventFd.get(), &eventItem);
LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake event fd to epoll instance: %s",
strerror(errno));
@@ -157,7 +155,7 @@
struct epoll_event eventItem;
request.initEventItem(&eventItem);
- int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, request.fd, & eventItem);
+ int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, request.fd, &eventItem);
if (epollResult < 0) {
ALOGE("Error adding epoll events for fd %d while rebuilding epoll set: %s",
request.fd, strerror(errno));
@@ -190,9 +188,9 @@
"fd=%d, events=0x%x, data=%p",
this, ident, fd, events, data);
#endif
- if (outFd != NULL) *outFd = fd;
- if (outEvents != NULL) *outEvents = events;
- if (outData != NULL) *outData = data;
+ if (outFd != nullptr) *outFd = fd;
+ if (outEvents != nullptr) *outEvents = events;
+ if (outData != nullptr) *outData = data;
return ident;
}
}
@@ -201,9 +199,9 @@
#if DEBUG_POLL_AND_WAKE
ALOGD("%p ~ pollOnce - returning result %d", this, result);
#endif
- if (outFd != NULL) *outFd = 0;
- if (outEvents != NULL) *outEvents = 0;
- if (outData != NULL) *outData = NULL;
+ if (outFd != nullptr) *outFd = 0;
+ if (outEvents != nullptr) *outEvents = 0;
+ if (outData != nullptr) *outData = nullptr;
return result;
}
@@ -239,7 +237,7 @@
mPolling = true;
struct epoll_event eventItems[EPOLL_MAX_EVENTS];
- int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
+ int eventCount = epoll_wait(mEpollFd.get(), eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
// No longer idling.
mPolling = false;
@@ -281,7 +279,7 @@
for (int i = 0; i < eventCount; i++) {
int fd = eventItems[i].data.fd;
uint32_t epollEvents = eventItems[i].events;
- if (fd == mWakeEventFd) {
+ if (fd == mWakeEventFd.get()) {
if (epollEvents & EPOLLIN) {
awoken();
} else {
@@ -401,11 +399,11 @@
#endif
uint64_t inc = 1;
- ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd, &inc, sizeof(uint64_t)));
+ ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd.get(), &inc, sizeof(uint64_t)));
if (nWrite != sizeof(uint64_t)) {
if (errno != EAGAIN) {
- LOG_ALWAYS_FATAL("Could not write wake signal to fd %d: %s",
- mWakeEventFd, strerror(errno));
+ LOG_ALWAYS_FATAL("Could not write wake signal to fd %d: %s", mWakeEventFd.get(),
+ strerror(errno));
}
}
}
@@ -416,7 +414,7 @@
#endif
uint64_t counter;
- TEMP_FAILURE_RETRY(read(mWakeEventFd, &counter, sizeof(uint64_t)));
+ TEMP_FAILURE_RETRY(read(mWakeEventFd.get(), &counter, sizeof(uint64_t)));
}
void Looper::pushResponse(int events, const Request& request) {
@@ -427,7 +425,7 @@
}
int Looper::addFd(int fd, int ident, int events, Looper_callbackFunc callback, void* data) {
- return addFd(fd, ident, events, callback ? new SimpleLooperCallback(callback) : NULL, data);
+ return addFd(fd, ident, events, callback ? new SimpleLooperCallback(callback) : nullptr, data);
}
int Looper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callback, void* data) {
@@ -467,14 +465,14 @@
ssize_t requestIndex = mRequests.indexOfKey(fd);
if (requestIndex < 0) {
- int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, & eventItem);
+ int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, fd, &eventItem);
if (epollResult < 0) {
ALOGE("Error adding epoll events for fd %d: %s", fd, strerror(errno));
return -1;
}
mRequests.add(fd, request);
} else {
- int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_MOD, fd, & eventItem);
+ int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_MOD, fd, &eventItem);
if (epollResult < 0) {
if (errno == ENOENT) {
// Tolerate ENOENT because it means that an older file descriptor was
@@ -495,7 +493,7 @@
"being recycled, falling back on EPOLL_CTL_ADD: %s",
this, strerror(errno));
#endif
- epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, & eventItem);
+ epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, fd, &eventItem);
if (epollResult < 0) {
ALOGE("Error modifying or adding epoll events for fd %d: %s",
fd, strerror(errno));
@@ -542,7 +540,7 @@
// updating the epoll set so that we avoid accidentally leaking callbacks.
mRequests.removeItemsAt(requestIndex);
- int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, NULL);
+ int epollResult = epoll_ctl(mEpollFd.get(), EPOLL_CTL_DEL, fd, nullptr);
if (epollResult < 0) {
if (seq != -1 && (errno == EBADF || errno == ENOENT)) {
// Tolerate EBADF or ENOENT when the sequence number is known because it
diff --git a/libutils/NativeHandle.cpp b/libutils/NativeHandle.cpp
index 97d06b8..d437a9f 100644
--- a/libutils/NativeHandle.cpp
+++ b/libutils/NativeHandle.cpp
@@ -20,7 +20,7 @@
namespace android {
sp<NativeHandle> NativeHandle::create(native_handle_t* handle, bool ownsHandle) {
- return handle ? new NativeHandle(handle, ownsHandle) : NULL;
+ return handle ? new NativeHandle(handle, ownsHandle) : nullptr;
}
NativeHandle::NativeHandle(native_handle_t* handle, bool ownsHandle)
diff --git a/libutils/Printer.cpp b/libutils/Printer.cpp
index cbf042e..c9ae210 100644
--- a/libutils/Printer.cpp
+++ b/libutils/Printer.cpp
@@ -73,7 +73,7 @@
}
void LogPrinter::printLine(const char* string) {
- if (string == NULL) {
+ if (string == nullptr) {
ALOGW("%s: NULL string passed in", __FUNCTION__);
return;
}
@@ -107,7 +107,7 @@
}
void FdPrinter::printLine(const char* string) {
- if (string == NULL) {
+ if (string == nullptr) {
ALOGW("%s: NULL string passed in", __FUNCTION__);
return;
} else if (mFd < 0) {
@@ -127,16 +127,16 @@
mTarget(target),
mPrefix(prefix ?: "") {
- if (target == NULL) {
+ if (target == nullptr) {
ALOGW("%s: Target string was NULL", __FUNCTION__);
}
}
void String8Printer::printLine(const char* string) {
- if (string == NULL) {
+ if (string == nullptr) {
ALOGW("%s: NULL string passed in", __FUNCTION__);
return;
- } else if (mTarget == NULL) {
+ } else if (mTarget == nullptr) {
ALOGW("%s: Target string was NULL", __FUNCTION__);
return;
}
diff --git a/libutils/ProcessCallStack.cpp b/libutils/ProcessCallStack.cpp
index b8fb6dc..f054de9 100644
--- a/libutils/ProcessCallStack.cpp
+++ b/libutils/ProcessCallStack.cpp
@@ -42,14 +42,14 @@
static const char* PATH_SELF_TASK = "/proc/self/task";
static void dumpProcessHeader(Printer& printer, pid_t pid, const char* timeStr) {
- if (timeStr == NULL) {
+ if (timeStr == nullptr) {
ALOGW("%s: timeStr was NULL", __FUNCTION__);
return;
}
char path[PATH_MAX];
char procNameBuf[MAX_PROC_PATH];
- char* procName = NULL;
+ char* procName = nullptr;
FILE* fp;
snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
@@ -76,7 +76,7 @@
static String8 getThreadName(pid_t tid) {
char path[PATH_MAX];
- char* procName = NULL;
+ char* procName = nullptr;
char procNameBuf[MAX_PROC_PATH];
FILE* fp;
@@ -88,7 +88,7 @@
ALOGE("%s: Failed to open %s", __FUNCTION__, path);
}
- if (procName == NULL) {
+ if (procName == nullptr) {
// Reading /proc/self/task/%d/comm failed due to a race
return String8::format("[err-unknown-tid-%d]", tid);
}
@@ -128,7 +128,7 @@
void ProcessCallStack::update() {
std::unique_ptr<DIR, decltype(&closedir)> dp(opendir(PATH_SELF_TASK), closedir);
- if (dp == NULL) {
+ if (dp == nullptr) {
ALOGE("%s: Failed to update the process's call stacks: %s",
__FUNCTION__, strerror(errno));
return;
@@ -140,7 +140,7 @@
// Get current time.
{
- time_t t = time(NULL);
+ time_t t = time(nullptr);
struct tm tm;
localtime_r(&t, &tm);
@@ -152,7 +152,7 @@
* - Read every file in directory => get every tid
*/
dirent* ep;
- while ((ep = readdir(dp.get())) != NULL) {
+ while ((ep = readdir(dp.get())) != nullptr) {
pid_t tid = -1;
sscanf(ep->d_name, "%d", &tid);
diff --git a/libutils/PropertyMap.cpp b/libutils/PropertyMap.cpp
index 4bcdd0f..b8c065d 100644
--- a/libutils/PropertyMap.cpp
+++ b/libutils/PropertyMap.cpp
@@ -112,7 +112,7 @@
}
status_t PropertyMap::load(const String8& filename, PropertyMap** outMap) {
- *outMap = NULL;
+ *outMap = nullptr;
Tokenizer* tokenizer;
status_t status = Tokenizer::open(filename, &tokenizer);
diff --git a/libutils/RefBase.cpp b/libutils/RefBase.cpp
index 8bccb0f..3f1e79a 100644
--- a/libutils/RefBase.cpp
+++ b/libutils/RefBase.cpp
@@ -17,30 +17,41 @@
#define LOG_TAG "RefBase"
// #define LOG_NDEBUG 0
+#include <memory>
+
#include <utils/RefBase.h>
#include <utils/CallStack.h>
+#include <utils/Mutex.h>
+
#ifndef __unused
#define __unused __attribute__((__unused__))
#endif
-// compile with refcounting debugging enabled
-#define DEBUG_REFS 0
+// Compile with refcounting debugging enabled.
+#define DEBUG_REFS 0
+
+// The following three are ignored unless DEBUG_REFS is set.
// whether ref-tracking is enabled by default, if not, trackMe(true, false)
// needs to be called explicitly
-#define DEBUG_REFS_ENABLED_BY_DEFAULT 0
+#define DEBUG_REFS_ENABLED_BY_DEFAULT 0
// whether callstack are collected (significantly slows things down)
-#define DEBUG_REFS_CALLSTACK_ENABLED 1
+#define DEBUG_REFS_CALLSTACK_ENABLED 1
// folder where stack traces are saved when DEBUG_REFS is enabled
// this folder needs to exist and be writable
-#define DEBUG_REFS_CALLSTACK_PATH "/data/debug"
+#define DEBUG_REFS_CALLSTACK_PATH "/data/debug"
// log all reference counting operations
-#define PRINT_REFS 0
+#define PRINT_REFS 0
+
+// Continue after logging a stack trace if ~RefBase discovers that reference
+// count has never been incremented. Normally we conspicuously crash in that
+// case.
+#define DEBUG_REFBASE_DESTRUCTION 1
// ---------------------------------------------------------------------------
@@ -184,7 +195,7 @@
char inc = refs->ref >= 0 ? '+' : '-';
ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
#if DEBUG_REFS_CALLSTACK_ENABLED
- refs->stack.log(LOG_TAG);
+ CallStack::logStack(LOG_TAG, refs->stack.get());
#endif
refs = refs->next;
}
@@ -198,14 +209,14 @@
char inc = refs->ref >= 0 ? '+' : '-';
ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
#if DEBUG_REFS_CALLSTACK_ENABLED
- refs->stack.log(LOG_TAG);
+ CallStack::logStack(LOG_TAG, refs->stack.get());
#endif
refs = refs->next;
}
}
if (dumpStack) {
ALOGE("above errors at:");
- CallStack stack(LOG_TAG);
+ CallStack::logStack(LOG_TAG);
}
}
@@ -279,7 +290,7 @@
this);
int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644);
if (rc >= 0) {
- write(rc, text.string(), text.length());
+ (void)write(rc, text.string(), text.length());
close(rc);
ALOGD("STACK TRACE for %p saved in %s", this, name);
}
@@ -294,7 +305,7 @@
ref_entry* next;
const void* id;
#if DEBUG_REFS_CALLSTACK_ENABLED
- CallStack stack;
+ CallStack::CallStackUPtr stack;
#endif
int32_t ref;
};
@@ -311,7 +322,7 @@
ref->ref = mRef;
ref->id = id;
#if DEBUG_REFS_CALLSTACK_ENABLED
- ref->stack.update(2);
+ ref->stack = CallStack::getCurrent(2);
#endif
ref->next = *refs;
*refs = ref;
@@ -346,7 +357,7 @@
ref = ref->next;
}
- CallStack stack(LOG_TAG);
+ CallStack::logStack(LOG_TAG);
}
}
@@ -373,7 +384,7 @@
inc, refs->id, refs->ref);
out->append(buf);
#if DEBUG_REFS_CALLSTACK_ENABLED
- out->append(refs->stack.toString("\t\t"));
+ out->append(CallStack::stackToString("\t\t", refs->stack.get()));
#else
out->append("\t\t(call stacks disabled)");
#endif
@@ -700,19 +711,19 @@
if (mRefs->mWeak.load(std::memory_order_relaxed) == 0) {
delete mRefs;
}
- } else if (mRefs->mStrong.load(std::memory_order_relaxed)
- == INITIAL_STRONG_VALUE) {
+ } else if (mRefs->mStrong.load(std::memory_order_relaxed) == INITIAL_STRONG_VALUE) {
// We never acquired a strong reference on this object.
- LOG_ALWAYS_FATAL_IF(mRefs->mWeak.load() != 0,
- "RefBase: Explicit destruction with non-zero weak "
- "reference count");
- // TODO: Always report if we get here. Currently MediaMetadataRetriever
- // C++ objects are inconsistently managed and sometimes get here.
- // There may be other cases, but we believe they should all be fixed.
- delete mRefs;
+#if DEBUG_REFBASE_DESTRUCTION
+ // Treating this as fatal is prone to causing boot loops. For debugging, it's
+ // better to treat as non-fatal.
+ ALOGD("RefBase: Explicit destruction, weak count = %d (in %p)", mRefs->mWeak.load(), this);
+ CallStack::logStack(LOG_TAG);
+#else
+ LOG_ALWAYS_FATAL("RefBase: Explicit destruction, weak count = %d", mRefs->mWeak.load());
+#endif
}
// For debugging purposes, clear mRefs. Ineffective against outstanding wp's.
- const_cast<weakref_impl*&>(mRefs) = NULL;
+ const_cast<weakref_impl*&>(mRefs) = nullptr;
}
void RefBase::extendObjectLifetime(int32_t mode)
diff --git a/libutils/SharedBuffer.cpp b/libutils/SharedBuffer.cpp
index bad98b2..7910c6e 100644
--- a/libutils/SharedBuffer.cpp
+++ b/libutils/SharedBuffer.cpp
@@ -75,7 +75,7 @@
"Invalid buffer size %zu", newSize);
buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize);
- if (buf != NULL) {
+ if (buf != nullptr) {
buf->mSize = newSize;
return buf;
}
@@ -94,7 +94,7 @@
if (onlyOwner()) {
return const_cast<SharedBuffer*>(this);
}
- return 0;
+ return nullptr;
}
SharedBuffer* SharedBuffer::reset(size_t new_size) const
diff --git a/libutils/SharedBuffer.h b/libutils/SharedBuffer.h
index 48358cd..fdf13a9 100644
--- a/libutils/SharedBuffer.h
+++ b/libutils/SharedBuffer.h
@@ -124,11 +124,11 @@
}
SharedBuffer* SharedBuffer::bufferFromData(void* data) {
- return data ? static_cast<SharedBuffer *>(data)-1 : 0;
+ return data ? static_cast<SharedBuffer *>(data)-1 : nullptr;
}
const SharedBuffer* SharedBuffer::bufferFromData(const void* data) {
- return data ? static_cast<const SharedBuffer *>(data)-1 : 0;
+ return data ? static_cast<const SharedBuffer *>(data)-1 : nullptr;
}
size_t SharedBuffer::sizeFromData(const void* data) {
@@ -139,7 +139,7 @@
return (mRefs.load(std::memory_order_acquire) == 1);
}
-}; // namespace android
+} // namespace android
// ---------------------------------------------------------------------------
diff --git a/libutils/Static.cpp b/libutils/Static.cpp
deleted file mode 100644
index 3ed07a1..0000000
--- a/libutils/Static.cpp
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright (C) 2008 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.
- */
-
-// All static variables go here, to control initialization and
-// destruction order in the library.
-
-namespace android {
-
-// For String8.cpp
-extern void initialize_string8();
-extern void terminate_string8();
-
-// For String16.cpp
-extern void initialize_string16();
-extern void terminate_string16();
-
-class LibUtilsFirstStatics
-{
-public:
- LibUtilsFirstStatics()
- {
- initialize_string8();
- initialize_string16();
- }
-
- ~LibUtilsFirstStatics()
- {
- terminate_string16();
- terminate_string8();
- }
-};
-
-static LibUtilsFirstStatics gFirstStatics;
-int gDarwinCantLoadAllObjects = 1;
-
-} // namespace android
diff --git a/libutils/String16.cpp b/libutils/String16.cpp
index ad335c3..5c0b406 100644
--- a/libutils/String16.cpp
+++ b/libutils/String16.cpp
@@ -24,29 +24,16 @@
namespace android {
-static SharedBuffer* gEmptyStringBuf = NULL;
-static char16_t* gEmptyString = NULL;
+static inline char16_t* getEmptyString() {
+ static SharedBuffer* gEmptyStringBuf = [] {
+ SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t));
+ char16_t* str = static_cast<char16_t*>(buf->data());
+ *str = 0;
+ return buf;
+ }();
-static inline char16_t* getEmptyString()
-{
gEmptyStringBuf->acquire();
- return gEmptyString;
-}
-
-void initialize_string16()
-{
- SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t));
- char16_t* str = (char16_t*)buf->data();
- *str = 0;
- gEmptyStringBuf = buf;
- gEmptyString = str;
-}
-
-void terminate_string16()
-{
- SharedBuffer::bufferFromData(gEmptyString)->release();
- gEmptyStringBuf = NULL;
- gEmptyString = NULL;
+ return static_cast<char16_t*>(gEmptyStringBuf->data());
}
// ---------------------------------------------------------------------------
@@ -79,6 +66,23 @@
return getEmptyString();
}
+static char16_t* allocFromUTF16(const char16_t* u16str, size_t u16len) {
+ if (u16len >= SIZE_MAX / sizeof(char16_t)) {
+ android_errorWriteLog(0x534e4554, "73826242");
+ abort();
+ }
+
+ SharedBuffer* buf = SharedBuffer::alloc((u16len + 1) * sizeof(char16_t));
+ ALOG_ASSERT(buf, "Unable to allocate shared buffer");
+ if (buf) {
+ char16_t* str = (char16_t*)buf->data();
+ memcpy(str, u16str, u16len * sizeof(char16_t));
+ str[u16len] = 0;
+ return str;
+ }
+ return getEmptyString();
+}
+
// ---------------------------------------------------------------------------
String16::String16()
@@ -87,7 +91,7 @@
}
String16::String16(StaticLinkage)
- : mString(0)
+ : mString(nullptr)
{
// this constructor is used when we can't rely on the static-initializers
// having run. In this case we always allocate an empty string. It's less
@@ -111,35 +115,9 @@
setTo(o, len, begin);
}
-String16::String16(const char16_t* o)
-{
- size_t len = strlen16(o);
- SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t));
- ALOG_ASSERT(buf, "Unable to allocate shared buffer");
- if (buf) {
- char16_t* str = (char16_t*)buf->data();
- strcpy16(str, o);
- mString = str;
- return;
- }
+String16::String16(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o))) {}
- mString = getEmptyString();
-}
-
-String16::String16(const char16_t* o, size_t len)
-{
- SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t));
- ALOG_ASSERT(buf, "Unable to allocate shared buffer");
- if (buf) {
- char16_t* str = (char16_t*)buf->data();
- memcpy(str, o, len*sizeof(char16_t));
- str[len] = 0;
- mString = str;
- return;
- }
-
- mString = getEmptyString();
-}
+String16::String16(const char16_t* o, size_t len) : mString(allocFromUTF16(o, len)) {}
String16::String16(const String8& o)
: mString(allocFromUTF8(o.string(), o.size()))
@@ -201,6 +179,11 @@
status_t String16::setTo(const char16_t* other, size_t len)
{
+ if (len >= SIZE_MAX / sizeof(char16_t)) {
+ android_errorWriteLog(0x534e4554, "73826242");
+ abort();
+ }
+
SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
->editResize((len+1)*sizeof(char16_t));
if (buf) {
@@ -224,6 +207,11 @@
return NO_ERROR;
}
+ if (myLen >= SIZE_MAX / sizeof(char16_t) - otherLen) {
+ android_errorWriteLog(0x534e4554, "73826242");
+ abort();
+ }
+
SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
->editResize((myLen+otherLen+1)*sizeof(char16_t));
if (buf) {
@@ -245,6 +233,11 @@
return NO_ERROR;
}
+ if (myLen >= SIZE_MAX / sizeof(char16_t) - otherLen) {
+ android_errorWriteLog(0x534e4554, "73826242");
+ abort();
+ }
+
SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
->editResize((myLen+otherLen+1)*sizeof(char16_t));
if (buf) {
@@ -349,7 +342,7 @@
{
const size_t N = size();
const char16_t* str = string();
- char16_t* edit = NULL;
+ char16_t* edit = nullptr;
for (size_t i=0; i<N; i++) {
const char16_t v = str[i];
if (v >= 'A' && v <= 'Z') {
@@ -371,7 +364,7 @@
{
const size_t N = size();
const char16_t* str = string();
- char16_t* edit = NULL;
+ char16_t* edit = nullptr;
for (size_t i=0; i<N; i++) {
if (str[i] == replaceThis) {
if (!edit) {
diff --git a/libutils/String8.cpp b/libutils/String8.cpp
index ad0e72e..8d318f7 100644
--- a/libutils/String8.cpp
+++ b/libutils/String8.cpp
@@ -40,40 +40,16 @@
// to OS_PATH_SEPARATOR.
#define RES_PATH_SEPARATOR '/'
-static SharedBuffer* gEmptyStringBuf = NULL;
-static char* gEmptyString = NULL;
+static inline char* getEmptyString() {
+ static SharedBuffer* gEmptyStringBuf = [] {
+ SharedBuffer* buf = SharedBuffer::alloc(1);
+ char* str = static_cast<char*>(buf->data());
+ *str = 0;
+ return buf;
+ }();
-extern int gDarwinCantLoadAllObjects;
-int gDarwinIsReallyAnnoying;
-
-void initialize_string8();
-
-static inline char* getEmptyString()
-{
gEmptyStringBuf->acquire();
- return gEmptyString;
-}
-
-void initialize_string8()
-{
- // HACK: This dummy dependency forces linking libutils Static.cpp,
- // which is needed to initialize String8/String16 classes.
- // These variables are named for Darwin, but are needed elsewhere too,
- // including static linking on any platform.
- gDarwinIsReallyAnnoying = gDarwinCantLoadAllObjects;
-
- SharedBuffer* buf = SharedBuffer::alloc(1);
- char* str = (char*)buf->data();
- *str = 0;
- gEmptyStringBuf = buf;
- gEmptyString = str;
-}
-
-void terminate_string8()
-{
- SharedBuffer::bufferFromData(gEmptyString)->release();
- gEmptyStringBuf = NULL;
- gEmptyString = NULL;
+ return static_cast<char*>(gEmptyStringBuf->data());
}
// ---------------------------------------------------------------------------
@@ -82,7 +58,7 @@
{
if (len > 0) {
if (len == SIZE_MAX) {
- return NULL;
+ return nullptr;
}
SharedBuffer* buf = SharedBuffer::alloc(len+1);
ALOG_ASSERT(buf, "Unable to allocate shared buffer");
@@ -92,7 +68,7 @@
str[len] = 0;
return str;
}
- return NULL;
+ return nullptr;
}
return getEmptyString();
@@ -150,7 +126,7 @@
}
String8::String8(StaticLinkage)
- : mString(0)
+ : mString(nullptr)
{
// this constructor is used when we can't rely on the static-initializers
// having run. In this case we always allocate an empty string. It's less
@@ -171,7 +147,7 @@
String8::String8(const char* o)
: mString(allocFromUTF8(o, strlen(o)))
{
- if (mString == NULL) {
+ if (mString == nullptr) {
mString = getEmptyString();
}
}
@@ -179,7 +155,7 @@
String8::String8(const char* o, size_t len)
: mString(allocFromUTF8(o, len))
{
- if (mString == NULL) {
+ if (mString == nullptr) {
mString = getEmptyString();
}
}
@@ -343,7 +319,7 @@
* second vsnprintf access undefined args.
*/
va_copy(tmp_args, args);
- n = vsnprintf(NULL, 0, fmt, tmp_args);
+ n = vsnprintf(nullptr, 0, fmt, tmp_args);
va_end(tmp_args);
if (n != 0) {
@@ -384,7 +360,7 @@
mString = str;
return str;
}
- return NULL;
+ return nullptr;
}
void String8::unlockBuffer()
@@ -536,7 +512,7 @@
const char*const buf = mString;
cp = strrchr(buf, OS_PATH_SEPARATOR);
- if (cp == NULL)
+ if (cp == nullptr)
return String8(*this);
else
return String8(cp+1);
@@ -548,7 +524,7 @@
const char*const str = mString;
cp = strrchr(str, OS_PATH_SEPARATOR);
- if (cp == NULL)
+ if (cp == nullptr)
return String8("");
else
return String8(str, cp - str);
@@ -567,7 +543,7 @@
cp = strchr(buf, OS_PATH_SEPARATOR);
}
- if (cp == NULL) {
+ if (cp == nullptr) {
String8 res = buf != str ? String8(buf) : *this;
if (outRemains) *outRemains = String8("");
return res;
@@ -591,15 +567,15 @@
// only look at the filename
lastSlash = strrchr(str, OS_PATH_SEPARATOR);
- if (lastSlash == NULL)
+ if (lastSlash == nullptr)
lastSlash = str;
else
lastSlash++;
// find the last dot
lastDot = strrchr(lastSlash, '.');
- if (lastDot == NULL)
- return NULL;
+ if (lastDot == nullptr)
+ return nullptr;
// looks good, ship it
return const_cast<char*>(lastDot);
@@ -610,7 +586,7 @@
char* ext;
ext = find_extension();
- if (ext != NULL)
+ if (ext != nullptr)
return String8(ext);
else
return String8("");
@@ -622,7 +598,7 @@
const char* const str = mString;
ext = find_extension();
- if (ext == NULL)
+ if (ext == nullptr)
return String8(*this);
else
return String8(str, ext - str);
diff --git a/libutils/Threads.cpp b/libutils/Threads.cpp
index 7d7f0e2..43ec6c1 100644
--- a/libutils/Threads.cpp
+++ b/libutils/Threads.cpp
@@ -163,7 +163,7 @@
// Note that *threadID is directly available to the parent only, as it is
// assigned after the child starts. Use memory barrier / lock if the child
// or other threads also need access.
- if (threadId != NULL) {
+ if (threadId != nullptr) {
*threadId = (android_thread_id_t)thread; // XXX: this is not portable
}
return 1;
@@ -768,7 +768,7 @@
strong.clear();
// And immediately, re-acquire a strong reference for the next loop
strong = weak.promote();
- } while(strong != 0);
+ } while(strong != nullptr);
return 0;
}
diff --git a/libutils/Timers.cpp b/libutils/Timers.cpp
index b2df9a5..c3641ef 100644
--- a/libutils/Timers.cpp
+++ b/libutils/Timers.cpp
@@ -45,7 +45,7 @@
// is windows.
struct timeval t;
t.tv_sec = t.tv_usec = 0;
- gettimeofday(&t, NULL);
+ gettimeofday(&t, nullptr);
return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
}
#endif
diff --git a/libutils/Tokenizer.cpp b/libutils/Tokenizer.cpp
index b68a2cf..f73d699 100644
--- a/libutils/Tokenizer.cpp
+++ b/libutils/Tokenizer.cpp
@@ -28,7 +28,7 @@
namespace android {
static inline bool isDelimiter(char ch, const char* delimiters) {
- return strchr(delimiters, ch) != NULL;
+ return strchr(delimiters, ch) != nullptr;
}
Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer,
@@ -46,7 +46,7 @@
}
status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
- *outTokenizer = NULL;
+ *outTokenizer = nullptr;
int result = NO_ERROR;
int fd = ::open(filename.string(), O_RDONLY);
@@ -64,12 +64,12 @@
FileMap* fileMap = new FileMap();
bool ownBuffer = false;
char* buffer;
- if (fileMap->create(NULL, fd, 0, length, true)) {
+ if (fileMap->create(nullptr, fd, 0, length, true)) {
fileMap->advise(FileMap::SEQUENTIAL);
buffer = static_cast<char*>(fileMap->getDataPtr());
} else {
delete fileMap;
- fileMap = NULL;
+ fileMap = nullptr;
// Fall back to reading into a buffer since we can't mmap files in sysfs.
// The length we obtained from stat is wrong too (it will always be 4096)
@@ -81,7 +81,7 @@
result = -errno;
ALOGE("Error reading file '%s': %s", filename.string(), strerror(errno));
delete[] buffer;
- buffer = NULL;
+ buffer = nullptr;
} else {
length = size_t(nrd);
}
@@ -98,7 +98,7 @@
status_t Tokenizer::fromContents(const String8& filename,
const char* contents, Tokenizer** outTokenizer) {
- *outTokenizer = new Tokenizer(filename, NULL,
+ *outTokenizer = new Tokenizer(filename, nullptr,
const_cast<char*>(contents), false, strlen(contents));
return OK;
}
diff --git a/libutils/Unicode.cpp b/libutils/Unicode.cpp
index 1086831..e00fb81 100644
--- a/libutils/Unicode.cpp
+++ b/libutils/Unicode.cpp
@@ -159,7 +159,7 @@
return -1;
}
size_t dummy_index;
- if (next_index == NULL) {
+ if (next_index == nullptr) {
next_index = &dummy_index;
}
size_t num_read;
@@ -173,7 +173,7 @@
ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len)
{
- if (src == NULL || src_len == 0) {
+ if (src == nullptr || src_len == 0) {
return -1;
}
@@ -195,7 +195,7 @@
void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst, size_t dst_len)
{
- if (src == NULL || src_len == 0 || dst == NULL) {
+ if (src == nullptr || src_len == 0 || dst == nullptr) {
return;
}
@@ -363,7 +363,7 @@
void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst, size_t dst_len)
{
- if (src == NULL || src_len == 0 || dst == NULL) {
+ if (src == nullptr || src_len == 0 || dst == nullptr) {
return;
}
@@ -440,7 +440,7 @@
ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len)
{
- if (src == NULL || src_len == 0) {
+ if (src == nullptr || src_len == 0) {
return -1;
}
@@ -490,7 +490,7 @@
size_t utf8_to_utf32_length(const char *src, size_t src_len)
{
- if (src == NULL || src_len == 0) {
+ if (src == nullptr || src_len == 0) {
return 0;
}
size_t ret = 0;
@@ -515,7 +515,7 @@
void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst)
{
- if (src == NULL || src_len == 0 || dst == NULL) {
+ if (src == nullptr || src_len == 0 || dst == nullptr) {
return;
}
diff --git a/libutils/VectorImpl.cpp b/libutils/VectorImpl.cpp
index ef3277f..00a904d 100644
--- a/libutils/VectorImpl.cpp
+++ b/libutils/VectorImpl.cpp
@@ -44,7 +44,7 @@
// ----------------------------------------------------------------------------
VectorImpl::VectorImpl(size_t itemSize, uint32_t flags)
- : mStorage(0), mCount(0), mFlags(flags), mItemSize(itemSize)
+ : mStorage(nullptr), mCount(0), mFlags(flags), mItemSize(itemSize)
{
}
@@ -77,7 +77,7 @@
mCount = rhs.mCount;
SharedBuffer::bufferFromData(mStorage)->acquire();
} else {
- mStorage = 0;
+ mStorage = nullptr;
mCount = 0;
}
}
@@ -89,14 +89,14 @@
if (mStorage) {
const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage);
SharedBuffer* editable = sb->attemptEdit();
- if (editable == 0) {
+ if (editable == nullptr) {
// If we're here, we're not the only owner of the buffer.
// We must make a copy of it.
editable = SharedBuffer::alloc(sb->size());
// Fail instead of returning a pointer to storage that's not
// editable. Otherwise we'd be editing the contents of a buffer
// for which we're not the only owner, which is undefined behaviour.
- LOG_ALWAYS_FATAL_IF(editable == NULL);
+ LOG_ALWAYS_FATAL_IF(editable == nullptr);
_do_copy(editable->data(), mStorage, mCount);
release_storage();
mStorage = editable->data();
@@ -141,7 +141,7 @@
ssize_t VectorImpl::insertAt(size_t index, size_t numItems)
{
- return insertAt(0, index, numItems);
+ return insertAt(nullptr, index, numItems);
}
ssize_t VectorImpl::insertAt(const void* item, size_t index, size_t numItems)
@@ -177,7 +177,7 @@
const ssize_t count = size();
if (count > 1) {
void* array = const_cast<void*>(arrayImpl());
- void* temp = 0;
+ void* temp = nullptr;
ssize_t i = 1;
while (i < count) {
void* item = reinterpret_cast<char*>(array) + mItemSize*(i);
@@ -205,7 +205,7 @@
_do_copy(next, curr, 1);
next = curr;
--j;
- curr = NULL;
+ curr = nullptr;
if (j >= 0) {
curr = reinterpret_cast<char*>(array) + mItemSize*(j);
}
@@ -233,7 +233,7 @@
void VectorImpl::push()
{
- push(0);
+ push(nullptr);
}
void VectorImpl::push(const void* item)
@@ -243,7 +243,7 @@
ssize_t VectorImpl::add()
{
- return add(0);
+ return add(nullptr);
}
ssize_t VectorImpl::add(const void* item)
@@ -253,7 +253,7 @@
ssize_t VectorImpl::replaceAt(size_t index)
{
- return replaceAt(0, index);
+ return replaceAt(nullptr, index);
}
ssize_t VectorImpl::replaceAt(const void* prototype, size_t index)
@@ -267,10 +267,10 @@
void* item = editItemLocation(index);
if (item != prototype) {
- if (item == 0)
+ if (item == nullptr)
return NO_MEMORY;
_do_destroy(item, 1);
- if (prototype == 0) {
+ if (prototype == nullptr) {
_do_construct(item, 1);
} else {
_do_copy(item, prototype, 1);
@@ -294,7 +294,7 @@
void VectorImpl::finish_vector()
{
release_storage();
- mStorage = 0;
+ mStorage = nullptr;
mCount = 0;
}
@@ -315,7 +315,7 @@
return reinterpret_cast<char*>(buffer) + index*mItemSize;
}
}
- return 0;
+ return nullptr;
}
const void* VectorImpl::itemLocation(size_t index) const
@@ -330,7 +330,7 @@
return reinterpret_cast<const char*>(buffer) + index*mItemSize;
}
}
- return 0;
+ return nullptr;
}
ssize_t VectorImpl::setCapacity(size_t new_capacity)
@@ -418,7 +418,7 @@
if (sb) {
mStorage = sb->data();
} else {
- return NULL;
+ return nullptr;
}
} else {
SharedBuffer* sb = SharedBuffer::alloc(new_alloc_size);
@@ -435,7 +435,7 @@
release_storage();
mStorage = const_cast<void*>(array);
} else {
- return NULL;
+ return nullptr;
}
}
} else {
diff --git a/libutils/include/utils/AndroidThreads.h b/libutils/include/utils/AndroidThreads.h
index 4c2dd49..a8d7851 100644
--- a/libutils/include/utils/AndroidThreads.h
+++ b/libutils/include/utils/AndroidThreads.h
@@ -106,7 +106,7 @@
const char* threadName = "android:unnamed_thread",
int32_t threadPriority = PRIORITY_DEFAULT,
size_t threadStackSize = 0,
- thread_id_t *threadId = 0)
+ thread_id_t *threadId = nullptr)
{
return androidCreateThreadEtc(entryFunction, userData, threadName,
threadPriority, threadStackSize, threadId) ? true : false;
@@ -118,7 +118,7 @@
}
// ----------------------------------------------------------------------------
-}; // namespace android
+} // namespace android
#endif // __cplusplus
// ----------------------------------------------------------------------------
diff --git a/libutils/include/utils/CallStack.h b/libutils/include/utils/CallStack.h
index 27e89f4..56004fe 100644
--- a/libutils/include/utils/CallStack.h
+++ b/libutils/include/utils/CallStack.h
@@ -17,6 +17,8 @@
#ifndef ANDROID_CALLSTACK_H
#define ANDROID_CALLSTACK_H
+#include <memory>
+
#include <android/log.h>
#include <backtrace/backtrace_constants.h>
#include <utils/String8.h>
@@ -25,6 +27,19 @@
#include <stdint.h>
#include <sys/types.h>
+#if !defined(__APPLE__) && !defined(_WIN32)
+# define WEAKS_AVAILABLE 1
+#endif
+#ifndef CALLSTACK_WEAK
+# ifdef WEAKS_AVAILABLE
+# define CALLSTACK_WEAK __attribute__((weak))
+# else // !WEAKS_AVAILABLE
+# define CALLSTACK_WEAK
+# endif // !WEAKS_AVAILABLE
+#endif // CALLSTACK_WEAK predefined
+
+#define ALWAYS_INLINE __attribute__((always_inline))
+
namespace android {
class Printer;
@@ -36,7 +51,7 @@
CallStack();
// Create a callstack with the current thread's stack trace.
// Immediately dump it to logcat using the given logtag.
- CallStack(const char* logtag, int32_t ignoreDepth=1);
+ CallStack(const char* logtag, int32_t ignoreDepth = 1);
~CallStack();
// Reset the stack frames (same as creating an empty call stack).
@@ -44,18 +59,18 @@
// Immediately collect the stack traces for the specified thread.
// The default is to dump the stack of the current call.
- void update(int32_t ignoreDepth=1, pid_t tid=BACKTRACE_CURRENT_THREAD);
+ void update(int32_t ignoreDepth = 1, pid_t tid = BACKTRACE_CURRENT_THREAD);
// Dump a stack trace to the log using the supplied logtag.
void log(const char* logtag,
android_LogPriority priority = ANDROID_LOG_DEBUG,
- const char* prefix = 0) const;
+ const char* prefix = nullptr) const;
// Dump a stack trace to the specified file descriptor.
- void dump(int fd, int indent = 0, const char* prefix = 0) const;
+ void dump(int fd, int indent = 0, const char* prefix = nullptr) const;
// Return a string (possibly very long) containing the complete stack trace.
- String8 toString(const char* prefix = 0) const;
+ String8 toString(const char* prefix = nullptr) const;
// Dump a serialized representation of the stack trace to the specified printer.
void print(Printer& printer) const;
@@ -63,10 +78,91 @@
// Get the count of stack frames that are in this call stack.
size_t size() const { return mFrameLines.size(); }
-private:
+ // DO NOT USE ANYTHING BELOW HERE. The following public members are expected
+ // to disappear again shortly, once a better replacement facility exists.
+ // The replacement facility will be incompatible!
+
+ // Debugging accesses to some basic functionality. These use weak symbols to
+ // avoid introducing a dependency on libutilscallstack. Such a dependency from
+ // libutils results in a cyclic build dependency. These routines can be called
+ // from within libutils. But if the actual library is unavailable, they do
+ // nothing.
+ //
+ // DO NOT USE THESE. They will disappear.
+ struct StackDeleter {
+#ifdef WEAKS_AVAILABLE
+ void operator()(CallStack* stack) {
+ deleteStack(stack);
+ }
+#else
+ void operator()(CallStack*) {}
+#endif
+ };
+
+ typedef std::unique_ptr<CallStack, StackDeleter> CallStackUPtr;
+
+ // Return current call stack if possible, nullptr otherwise.
+#ifdef WEAKS_AVAILABLE
+ static CallStackUPtr ALWAYS_INLINE getCurrent(int32_t ignoreDepth = 1) {
+ if (reinterpret_cast<uintptr_t>(getCurrentInternal) == 0) {
+ ALOGW("CallStack::getCurrentInternal not linked, returning null");
+ return CallStackUPtr(nullptr);
+ } else {
+ return getCurrentInternal(ignoreDepth);
+ }
+ }
+#else // !WEAKS_AVAILABLE
+ static CallStackUPtr ALWAYS_INLINE getCurrent(int32_t = 1) {
+ return CallStackUPtr(nullptr);
+ }
+#endif // !WEAKS_AVAILABLE
+
+#ifdef WEAKS_AVAILABLE
+ static void ALWAYS_INLINE logStack(const char* logtag, CallStack* stack = getCurrent().get(),
+ android_LogPriority priority = ANDROID_LOG_DEBUG) {
+ if (reinterpret_cast<uintptr_t>(logStackInternal) != 0 && stack != nullptr) {
+ logStackInternal(logtag, stack, priority);
+ } else {
+ ALOGW("CallStack::logStackInternal not linked");
+ }
+ }
+
+#else
+ static void ALWAYS_INLINE logStack(const char*, CallStack* = getCurrent().get(),
+ android_LogPriority = ANDROID_LOG_DEBUG) {
+ }
+#endif // !WEAKS_AVAILABLE
+
+#ifdef WEAKS_AVAILABLE
+ static String8 ALWAYS_INLINE stackToString(const char* prefix = nullptr,
+ const CallStack* stack = getCurrent().get()) {
+ if (reinterpret_cast<uintptr_t>(stackToStringInternal) != 0 && stack != nullptr) {
+ return stackToStringInternal(prefix, stack);
+ } else {
+ return String8("<CallStack package not linked>");
+ }
+ }
+#else // !WEAKS_AVAILABLE
+ static String8 ALWAYS_INLINE stackToString(const char* = nullptr,
+ const CallStack* = getCurrent().get()) {
+ return String8("<CallStack package not linked>");
+ }
+#endif // !WEAKS_AVAILABLE
+
+ private:
+#ifdef WEAKS_AVAILABLE
+ static CallStackUPtr CALLSTACK_WEAK getCurrentInternal(int32_t ignoreDepth);
+ static void CALLSTACK_WEAK logStackInternal(const char* logtag, const CallStack* stack,
+ android_LogPriority priority);
+ static String8 CALLSTACK_WEAK stackToStringInternal(const char* prefix, const CallStack* stack);
+ // The deleter is only invoked on non-null pointers. Hence it will never be
+ // invoked if CallStack is not linked.
+ static void CALLSTACK_WEAK deleteStack(CallStack* stack);
+#endif // WEAKS_AVAILABLE
+
Vector<String8> mFrameLines;
};
-}; // namespace android
+} // namespace android
#endif // ANDROID_CALLSTACK_H
diff --git a/libutils/include/utils/Condition.h b/libutils/include/utils/Condition.h
index 9bf82eb..540ed82 100644
--- a/libutils/include/utils/Condition.h
+++ b/libutils/include/utils/Condition.h
@@ -124,7 +124,7 @@
#else // __APPLE__
// Apple doesn't support POSIX clocks.
struct timeval t;
- gettimeofday(&t, NULL);
+ gettimeofday(&t, nullptr);
ts.tv_sec = t.tv_sec;
ts.tv_nsec = t.tv_usec*1000;
#endif
@@ -159,7 +159,7 @@
#endif // !defined(_WIN32)
// ---------------------------------------------------------------------------
-}; // namespace android
+} // namespace android
// ---------------------------------------------------------------------------
#endif // _LIBS_UTILS_CONDITON_H
diff --git a/libutils/include/utils/Debug.h b/libutils/include/utils/Debug.h
index 4cbb462..96bd70e 100644
--- a/libutils/include/utils/Debug.h
+++ b/libutils/include/utils/Debug.h
@@ -35,6 +35,6 @@
CompileTimeAssert<( _exp )>();
// ---------------------------------------------------------------------------
-}; // namespace android
+} // namespace android
#endif // ANDROID_UTILS_DEBUG_H
diff --git a/libutils/include/utils/Errors.h b/libutils/include/utils/Errors.h
index 16e1fa2..7093a20 100644
--- a/libutils/include/utils/Errors.h
+++ b/libutils/include/utils/Errors.h
@@ -81,8 +81,8 @@
# define NO_ERROR 0L
#endif
-}; // namespace android
-
+} // namespace android
+
// ---------------------------------------------------------------------------
#endif // ANDROID_ERRORS_H
diff --git a/libutils/include/utils/FileMap.h b/libutils/include/utils/FileMap.h
index 7d372e1..8d402a3 100644
--- a/libutils/include/utils/FileMap.h
+++ b/libutils/include/utils/FileMap.h
@@ -124,6 +124,6 @@
static long mPageSize;
};
-}; // namespace android
+} // namespace android
#endif // __LIBS_FILE_MAP_H
diff --git a/libutils/include/utils/Flattenable.h b/libutils/include/utils/Flattenable.h
index 675e211..0a19019 100644
--- a/libutils/include/utils/Flattenable.h
+++ b/libutils/include/utils/Flattenable.h
@@ -198,8 +198,6 @@
}
};
-
-}; // namespace android
-
+} // namespace android
#endif /* ANDROID_UTILS_FLATTENABLE_H */
diff --git a/libutils/include/utils/Functor.h b/libutils/include/utils/Functor.h
index 3182a9c..c0c8d57 100644
--- a/libutils/include/utils/Functor.h
+++ b/libutils/include/utils/Functor.h
@@ -32,6 +32,6 @@
virtual status_t operator ()(int /*what*/, void* /*data*/) { return NO_ERROR; }
};
-}; // namespace android
+} // namespace android
#endif // ANDROID_FUNCTOR_H
diff --git a/libutils/include/utils/KeyedVector.h b/libutils/include/utils/KeyedVector.h
index 03bfe27..7bda99b 100644
--- a/libutils/include/utils/KeyedVector.h
+++ b/libutils/include/utils/KeyedVector.h
@@ -211,7 +211,7 @@
return i >= 0 ? KeyedVector<KEY,VALUE>::valueAt(i) : mDefault;
}
-}; // namespace android
+} // namespace android
// ---------------------------------------------------------------------------
diff --git a/libutils/include/utils/LightRefBase.h b/libutils/include/utils/LightRefBase.h
index 65257ed..e488e60 100644
--- a/libutils/include/utils/LightRefBase.h
+++ b/libutils/include/utils/LightRefBase.h
@@ -69,4 +69,4 @@
virtual ~VirtualLightRefBase() = default;
};
-}; // namespace android
+} // namespace android
diff --git a/libutils/include/utils/List.h b/libutils/include/utils/List.h
index daca016..25b56fd 100644
--- a/libutils/include/utils/List.h
+++ b/libutils/include/utils/List.h
@@ -329,6 +329,6 @@
return *this;
}
-}; // namespace android
+} // namespace android
#endif // _LIBS_UTILS_LIST_H
diff --git a/libutils/include/utils/Looper.h b/libutils/include/utils/Looper.h
index a62e67f..c439c5c 100644
--- a/libutils/include/utils/Looper.h
+++ b/libutils/include/utils/Looper.h
@@ -24,6 +24,8 @@
#include <sys/epoll.h>
+#include <android-base/unique_fd.h>
+
namespace android {
/*
@@ -262,7 +264,7 @@
*/
int pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData);
inline int pollOnce(int timeoutMillis) {
- return pollOnce(timeoutMillis, NULL, NULL, NULL);
+ return pollOnce(timeoutMillis, nullptr, nullptr, nullptr);
}
/**
@@ -272,7 +274,7 @@
*/
int pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outData);
inline int pollAll(int timeoutMillis) {
- return pollAll(timeoutMillis, NULL, NULL, NULL);
+ return pollAll(timeoutMillis, nullptr, nullptr, nullptr);
}
/**
@@ -447,7 +449,7 @@
const bool mAllowNonCallbacks; // immutable
- int mWakeEventFd; // immutable
+ android::base::unique_fd mWakeEventFd; // immutable
Mutex mLock;
Vector<MessageEnvelope> mMessageEnvelopes; // guarded by mLock
@@ -457,7 +459,7 @@
// any use of it is racy anyway.
volatile bool mPolling;
- int mEpollFd; // guarded by mLock but only modified on the looper thread
+ android::base::unique_fd mEpollFd; // guarded by mLock but only modified on the looper thread
bool mEpollRebuildRequired; // guarded by mLock
// Locked list of file descriptor monitoring requests.
diff --git a/libutils/include/utils/LruCache.h b/libutils/include/utils/LruCache.h
index 89dccd6..36775d0 100644
--- a/libutils/include/utils/LruCache.h
+++ b/libutils/include/utils/LruCache.h
@@ -71,7 +71,7 @@
Entry* parent;
Entry* child;
- Entry(TKey _key, TValue _value) : key(_key), value(_value), parent(NULL), child(NULL) {
+ Entry(TKey _key, TValue _value) : key(_key), value(_value), parent(nullptr), child(nullptr) {
}
const TKey& getKey() const final { return key; }
};
@@ -162,9 +162,9 @@
template <typename TKey, typename TValue>
LruCache<TKey, TValue>::LruCache(uint32_t maxCapacity)
: mSet(new LruCacheSet())
- , mListener(NULL)
- , mOldest(NULL)
- , mYoungest(NULL)
+ , mListener(nullptr)
+ , mOldest(nullptr)
+ , mYoungest(nullptr)
, mMaxCapacity(maxCapacity)
, mNullValue(0) {
mSet->max_load_factor(1.0);
@@ -236,7 +236,7 @@
template <typename TKey, typename TValue>
bool LruCache<TKey, TValue>::removeOldest() {
- if (mOldest != NULL) {
+ if (mOldest != nullptr) {
return remove(mOldest->key);
// TODO: should probably abort if false
}
@@ -254,12 +254,12 @@
template <typename TKey, typename TValue>
void LruCache<TKey, TValue>::clear() {
if (mListener) {
- for (Entry* p = mOldest; p != NULL; p = p->child) {
+ for (Entry* p = mOldest; p != nullptr; p = p->child) {
(*mListener)(p->key, p->value);
}
}
- mYoungest = NULL;
- mOldest = NULL;
+ mYoungest = nullptr;
+ mOldest = nullptr;
for (auto entry : *mSet.get()) {
delete entry;
}
@@ -268,7 +268,7 @@
template <typename TKey, typename TValue>
void LruCache<TKey, TValue>::attachToCache(Entry& entry) {
- if (mYoungest == NULL) {
+ if (mYoungest == nullptr) {
mYoungest = mOldest = &entry;
} else {
entry.parent = mYoungest;
@@ -279,19 +279,19 @@
template <typename TKey, typename TValue>
void LruCache<TKey, TValue>::detachFromCache(Entry& entry) {
- if (entry.parent != NULL) {
+ if (entry.parent != nullptr) {
entry.parent->child = entry.child;
} else {
mOldest = entry.child;
}
- if (entry.child != NULL) {
+ if (entry.child != nullptr) {
entry.child->parent = entry.parent;
} else {
mYoungest = entry.parent;
}
- entry.parent = NULL;
- entry.child = NULL;
+ entry.parent = nullptr;
+ entry.child = nullptr;
}
}
diff --git a/libutils/include/utils/Mutex.h b/libutils/include/utils/Mutex.h
index af6076c..29c2e8c 100644
--- a/libutils/include/utils/Mutex.h
+++ b/libutils/include/utils/Mutex.h
@@ -100,7 +100,7 @@
Mutex();
explicit Mutex(const char* name);
- explicit Mutex(int type, const char* name = NULL);
+ explicit Mutex(int type, const char* name = nullptr);
~Mutex();
// lock or unlock the mutex
@@ -160,10 +160,10 @@
#if !defined(_WIN32)
inline Mutex::Mutex() {
- pthread_mutex_init(&mMutex, NULL);
+ pthread_mutex_init(&mMutex, nullptr);
}
inline Mutex::Mutex(__attribute__((unused)) const char* name) {
- pthread_mutex_init(&mMutex, NULL);
+ pthread_mutex_init(&mMutex, nullptr);
}
inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) {
if (type == SHARED) {
@@ -173,7 +173,7 @@
pthread_mutex_init(&mMutex, &attr);
pthread_mutexattr_destroy(&attr);
} else {
- pthread_mutex_init(&mMutex, NULL);
+ pthread_mutex_init(&mMutex, nullptr);
}
}
inline Mutex::~Mutex() {
@@ -212,7 +212,7 @@
typedef Mutex::Autolock AutoMutex;
// ---------------------------------------------------------------------------
-}; // namespace android
+} // namespace android
// ---------------------------------------------------------------------------
#endif // _LIBS_UTILS_MUTEX_H
diff --git a/libutils/include/utils/Printer.h b/libutils/include/utils/Printer.h
index bb66287..7465927 100644
--- a/libutils/include/utils/Printer.h
+++ b/libutils/include/utils/Printer.h
@@ -45,7 +45,7 @@
// (Note that the default ALOG behavior is to ignore blank lines)
LogPrinter(const char* logtag,
android_LogPriority priority = ANDROID_LOG_DEBUG,
- const char* prefix = 0,
+ const char* prefix = nullptr,
bool ignoreBlankLines = false);
// Print the specified line to logcat. No \n at the end is necessary.
@@ -66,7 +66,7 @@
// Create a printer using the specified file descriptor.
// - Each line will be prefixed with 'indent' number of blank spaces.
// - In addition, each line will be prefixed with the 'prefix' string.
- FdPrinter(int fd, unsigned int indent = 0, const char* prefix = 0);
+ FdPrinter(int fd, unsigned int indent = 0, const char* prefix = nullptr);
// Print the specified line to the file descriptor. \n is appended automatically.
virtual void printLine(const char* string);
@@ -90,7 +90,7 @@
// Create a printer using the specified String8 as the target.
// - In addition, each line will be prefixed with the 'prefix' string.
// - target's memory lifetime must be a superset of this String8Printer.
- String8Printer(String8* target, const char* prefix = 0);
+ String8Printer(String8* target, const char* prefix = nullptr);
// Append the specified line to the String8. \n is appended automatically.
virtual void printLine(const char* string);
@@ -114,6 +114,6 @@
const char* mPrefix;
};
-}; // namespace android
+} // namespace android
#endif // ANDROID_PRINTER_H
diff --git a/libutils/include/utils/ProcessCallStack.h b/libutils/include/utils/ProcessCallStack.h
index 32458b8..7e06086 100644
--- a/libutils/include/utils/ProcessCallStack.h
+++ b/libutils/include/utils/ProcessCallStack.h
@@ -43,13 +43,13 @@
// Print all stack traces to the log using the supplied logtag.
void log(const char* logtag, android_LogPriority priority = ANDROID_LOG_DEBUG,
- const char* prefix = 0) const;
+ const char* prefix = nullptr) const;
// Dump all stack traces to the specified file descriptor.
- void dump(int fd, int indent = 0, const char* prefix = 0) const;
+ void dump(int fd, int indent = 0, const char* prefix = nullptr) const;
// Return a string (possibly very long) containing all the stack traces.
- String8 toString(const char* prefix = 0) const;
+ String8 toString(const char* prefix = nullptr) const;
// Dump a serialized representation of all the stack traces to the specified printer.
void print(Printer& printer) const;
@@ -74,6 +74,6 @@
struct tm mTimeUpdated;
};
-}; // namespace android
+} // namespace android
#endif // ANDROID_PROCESS_CALLSTACK_H
diff --git a/libutils/include/utils/RWLock.h b/libutils/include/utils/RWLock.h
index d5b81d3..64e370e 100644
--- a/libutils/include/utils/RWLock.h
+++ b/libutils/include/utils/RWLock.h
@@ -48,7 +48,7 @@
RWLock();
explicit RWLock(const char* name);
- explicit RWLock(int type, const char* name = NULL);
+ explicit RWLock(int type, const char* name = nullptr);
~RWLock();
status_t readLock();
@@ -82,10 +82,10 @@
};
inline RWLock::RWLock() {
- pthread_rwlock_init(&mRWLock, NULL);
+ pthread_rwlock_init(&mRWLock, nullptr);
}
inline RWLock::RWLock(__attribute__((unused)) const char* name) {
- pthread_rwlock_init(&mRWLock, NULL);
+ pthread_rwlock_init(&mRWLock, nullptr);
}
inline RWLock::RWLock(int type, __attribute__((unused)) const char* name) {
if (type == SHARED) {
@@ -95,7 +95,7 @@
pthread_rwlock_init(&mRWLock, &attr);
pthread_rwlockattr_destroy(&attr);
} else {
- pthread_rwlock_init(&mRWLock, NULL);
+ pthread_rwlock_init(&mRWLock, nullptr);
}
}
inline RWLock::~RWLock() {
@@ -120,7 +120,7 @@
#endif // !defined(_WIN32)
// ---------------------------------------------------------------------------
-}; // namespace android
+} // namespace android
// ---------------------------------------------------------------------------
#endif // _LIBS_UTILS_RWLOCK_H
diff --git a/libutils/include/utils/RefBase.h b/libutils/include/utils/RefBase.h
index 223b666..1780cf2 100644
--- a/libutils/include/utils/RefBase.h
+++ b/libutils/include/utils/RefBase.h
@@ -354,7 +354,7 @@
public:
typedef typename RefBase::weakref_type weakref_type;
- inline wp() : m_ptr(0) { }
+ inline wp() : m_ptr(nullptr) { }
wp(T* other); // NOLINT(implicit)
wp(const wp<T>& other);
@@ -505,7 +505,7 @@
wp<T>& wp<T>::operator = (T* other)
{
weakref_type* newRefs =
- other ? other->createWeak(this) : 0;
+ other ? other->createWeak(this) : nullptr;
if (m_ptr) m_refs->decWeak(this);
m_ptr = other;
m_refs = newRefs;
@@ -528,7 +528,7 @@
wp<T>& wp<T>::operator = (const sp<T>& other)
{
weakref_type* newRefs =
- other != NULL ? other->createWeak(this) : 0;
+ other != nullptr ? other->createWeak(this) : nullptr;
T* otherPtr(other.m_ptr);
if (m_ptr) m_refs->decWeak(this);
m_ptr = otherPtr;
@@ -563,7 +563,7 @@
wp<T>& wp<T>::operator = (const sp<U>& other)
{
weakref_type* newRefs =
- other != NULL ? other->createWeak(this) : 0;
+ other != nullptr ? other->createWeak(this) : 0;
U* otherPtr(other.m_ptr);
if (m_ptr) m_refs->decWeak(this);
m_ptr = otherPtr;
@@ -683,7 +683,7 @@
ReferenceMover::move_references(d, s, n);
}
-}; // namespace android
+} // namespace android
// ---------------------------------------------------------------------------
diff --git a/libutils/include/utils/Singleton.h b/libutils/include/utils/Singleton.h
index bc47a5c..44d8ad7 100644
--- a/libutils/include/utils/Singleton.h
+++ b/libutils/include/utils/Singleton.h
@@ -51,7 +51,7 @@
static TYPE& getInstance() {
Mutex::Autolock _l(sLock);
TYPE* instance = sInstance;
- if (instance == 0) {
+ if (instance == nullptr) {
instance = new TYPE();
sInstance = instance;
}
@@ -60,7 +60,7 @@
static bool hasInstance() {
Mutex::Autolock _l(sLock);
- return sInstance != 0;
+ return sInstance != nullptr;
}
protected:
@@ -90,12 +90,12 @@
#define ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) \
template<> ::android::Mutex \
(::android::Singleton< TYPE >::sLock)(::android::Mutex::PRIVATE); \
- template<> TYPE* ::android::Singleton< TYPE >::sInstance(0); /* NOLINT */ \
+ template<> TYPE* ::android::Singleton< TYPE >::sInstance(nullptr); /* NOLINT */ \
template class ::android::Singleton< TYPE >;
// ---------------------------------------------------------------------------
-}; // namespace android
+} // namespace android
#endif // ANDROID_UTILS_SINGLETON_H
diff --git a/libutils/include/utils/SortedVector.h b/libutils/include/utils/SortedVector.h
index 47c1376..394db12 100644
--- a/libutils/include/utils/SortedVector.h
+++ b/libutils/include/utils/SortedVector.h
@@ -288,8 +288,7 @@
return compare_type( *reinterpret_cast<const TYPE*>(lhs), *reinterpret_cast<const TYPE*>(rhs) );
}
-}; // namespace android
-
+} // namespace android
// ---------------------------------------------------------------------------
diff --git a/libutils/include/utils/StopWatch.h b/libutils/include/utils/StopWatch.h
index 76d78d0..9b14ac8 100644
--- a/libutils/include/utils/StopWatch.h
+++ b/libutils/include/utils/StopWatch.h
@@ -52,9 +52,7 @@
int mNumLaps;
};
-
-}; // namespace android
-
+} // namespace android
// ---------------------------------------------------------------------------
diff --git a/libutils/include/utils/String16.h b/libutils/include/utils/String16.h
index 5f0ce06..afbc2ed 100644
--- a/libutils/include/utils/String16.h
+++ b/libutils/include/utils/String16.h
@@ -243,7 +243,7 @@
return mString;
}
-}; // namespace android
+} // namespace android
// ---------------------------------------------------------------------------
diff --git a/libutils/include/utils/String8.h b/libutils/include/utils/String8.h
index 94ac32f..c8f584e 100644
--- a/libutils/include/utils/String8.h
+++ b/libutils/include/utils/String8.h
@@ -187,7 +187,7 @@
* "/tmp" --> "tmp" (remain = "")
* "bar.c" --> "bar.c" (remain = "")
*/
- String8 walkPath(String8* outRemains = NULL) const;
+ String8 walkPath(String8* outRemains = nullptr) const;
/*
* Return the filename extension. This is the last '.' and any number
diff --git a/libutils/include/utils/StrongPointer.h b/libutils/include/utils/StrongPointer.h
index ae6d9c8..360fce5 100644
--- a/libutils/include/utils/StrongPointer.h
+++ b/libutils/include/utils/StrongPointer.h
@@ -52,7 +52,7 @@
template<typename T>
class sp {
public:
- inline sp() : m_ptr(0) { }
+ inline sp() : m_ptr(nullptr) { }
sp(T* other); // NOLINT(implicit)
sp(const sp<T>& other);
@@ -230,7 +230,7 @@
void sp<T>::clear() {
if (m_ptr) {
m_ptr->decStrong(this);
- m_ptr = 0;
+ m_ptr = nullptr;
}
}
@@ -239,7 +239,7 @@
m_ptr = ptr;
}
-}; // namespace android
+} // namespace android
// ---------------------------------------------------------------------------
diff --git a/libutils/include/utils/SystemClock.h b/libutils/include/utils/SystemClock.h
index 01db340..f816fba 100644
--- a/libutils/include/utils/SystemClock.h
+++ b/libutils/include/utils/SystemClock.h
@@ -26,7 +26,7 @@
int64_t elapsedRealtime();
int64_t elapsedRealtimeNano();
-}; // namespace android
+} // namespace android
#endif // ANDROID_UTILS_SYSTEMCLOCK_H
diff --git a/libutils/include/utils/Thread.h b/libutils/include/utils/Thread.h
index 598298d..3525138 100644
--- a/libutils/include/utils/Thread.h
+++ b/libutils/include/utils/Thread.h
@@ -110,8 +110,7 @@
#endif
};
-
-}; // namespace android
+} // namespace android
// ---------------------------------------------------------------------------
#endif // _LIBS_UTILS_THREAD_H
diff --git a/libutils/include/utils/ThreadDefs.h b/libutils/include/utils/ThreadDefs.h
index ae091e4..8eb3d1c 100644
--- a/libutils/include/utils/ThreadDefs.h
+++ b/libutils/include/utils/ThreadDefs.h
@@ -66,9 +66,8 @@
};
// ---------------------------------------------------------------------------
-}; // namespace android
+} // namespace android
#endif // __cplusplus
// ---------------------------------------------------------------------------
-
#endif // _LIBS_UTILS_THREAD_DEFS_H
diff --git a/libutils/include/utils/Trace.h b/libutils/include/utils/Trace.h
index 5e9229c..4b9c91e 100644
--- a/libutils/include/utils/Trace.h
+++ b/libutils/include/utils/Trace.h
@@ -49,7 +49,7 @@
uint64_t mTag;
};
-}; // namespace android
+} // namespace android
#else // !__ANDROID__
diff --git a/libutils/include/utils/TypeHelpers.h b/libutils/include/utils/TypeHelpers.h
index 28fbca5..1554f52 100644
--- a/libutils/include/utils/TypeHelpers.h
+++ b/libutils/include/utils/TypeHelpers.h
@@ -329,7 +329,7 @@
return hash_type(uintptr_t(value));
}
-}; // namespace android
+} // namespace android
// ---------------------------------------------------------------------------
diff --git a/libutils/include/utils/Vector.h b/libutils/include/utils/Vector.h
index a1a0234..ddf71de 100644
--- a/libutils/include/utils/Vector.h
+++ b/libutils/include/utils/Vector.h
@@ -425,8 +425,7 @@
move_backward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
}
-}; // namespace android
-
+} // namespace android
// ---------------------------------------------------------------------------
diff --git a/libutils/include/utils/VectorImpl.h b/libutils/include/utils/VectorImpl.h
index 4dd91fd..41b9f33 100644
--- a/libutils/include/utils/VectorImpl.h
+++ b/libutils/include/utils/VectorImpl.h
@@ -157,7 +157,7 @@
virtual int do_compare(const void* lhs, const void* rhs) const = 0;
private:
- ssize_t _indexOrderOf(const void* item, size_t* order = 0) const;
+ ssize_t _indexOrderOf(const void* item, size_t* order = nullptr) const;
// these are made private, because they can't be used on a SortedVector
// (they don't have an implementation either)
@@ -175,8 +175,7 @@
ssize_t replaceAt(const void* item, size_t index);
};
-}; // namespace android
-
+} // namespace android
// ---------------------------------------------------------------------------
diff --git a/libutils/include/utils/misc.h b/libutils/include/utils/misc.h
index af5ea02..32615b3 100644
--- a/libutils/include/utils/misc.h
+++ b/libutils/include/utils/misc.h
@@ -35,6 +35,6 @@
void add_sysprop_change_callback(sysprop_change_callback cb, int priority);
void report_sysprop_change();
-}; // namespace android
+} // namespace android
#endif // _LIBS_UTILS_MISC_H
diff --git a/libutils/misc.cpp b/libutils/misc.cpp
index da28dfa..f77e189 100644
--- a/libutils/misc.cpp
+++ b/libutils/misc.cpp
@@ -23,7 +23,7 @@
#include <utils/Log.h>
#include <utils/Vector.h>
-#if defined(__ANDROID__)
+#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)
#include <dlfcn.h>
#include <vndksupport/linker.h>
#endif
@@ -41,13 +41,13 @@
#if !defined(_WIN32)
static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER;
-static Vector<sysprop_change_callback_info>* gSyspropList = NULL;
+static Vector<sysprop_change_callback_info>* gSyspropList = nullptr;
#endif
#if !defined(_WIN32)
void add_sysprop_change_callback(sysprop_change_callback cb, int priority) {
pthread_mutex_lock(&gSyspropMutex);
- if (gSyspropList == NULL) {
+ if (gSyspropList == nullptr) {
gSyspropList = new Vector<sysprop_change_callback_info>();
}
sysprop_change_callback_info info;
@@ -70,7 +70,7 @@
void add_sysprop_change_callback(sysprop_change_callback, int) {}
#endif
-#if defined(__ANDROID__)
+#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)
void (*get_report_sysprop_change_func())() {
void (*func)() = nullptr;
void* handle = android_load_sphal_library("libutils.so", RTLD_NOW);
@@ -85,7 +85,7 @@
void report_sysprop_change() {
do_report_sysprop_change();
-#if defined(__ANDROID__)
+#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)
// libutils.so is double loaded; from the default namespace and from the
// 'sphal' namespace. Redirect the sysprop change event to the other instance
// of libutils.so loaded in the 'sphal' namespace so that listeners attached
@@ -103,7 +103,7 @@
#if !defined(_WIN32)
pthread_mutex_lock(&gSyspropMutex);
Vector<sysprop_change_callback_info> listeners;
- if (gSyspropList != NULL) {
+ if (gSyspropList != nullptr) {
listeners = *gSyspropList;
}
pthread_mutex_unlock(&gSyspropMutex);
diff --git a/libutils/tests/Looper_test.cpp b/libutils/tests/Looper_test.cpp
index 8ebcfaf..2282ced 100644
--- a/libutils/tests/Looper_test.cpp
+++ b/libutils/tests/Looper_test.cpp
@@ -339,7 +339,7 @@
Pipe pipe;
pipe.writeSignal();
- mLooper->addFd(pipe.receiveFd, expectedIdent, Looper::EVENT_INPUT, NULL, expectedData);
+ mLooper->addFd(pipe.receiveFd, expectedIdent, Looper::EVENT_INPUT, nullptr, expectedData);
StopWatch stopWatch("pollOnce");
int fd;
@@ -364,7 +364,7 @@
TEST_F(LooperTest, AddFd_WhenCallbackAdded_ReturnsOne) {
Pipe pipe;
- int result = mLooper->addFd(pipe.receiveFd, 0, Looper::EVENT_INPUT, NULL, NULL);
+ int result = mLooper->addFd(pipe.receiveFd, 0, Looper::EVENT_INPUT, nullptr, nullptr);
EXPECT_EQ(1, result)
<< "addFd should return 1 because FD was added";
@@ -372,7 +372,7 @@
TEST_F(LooperTest, AddFd_WhenIdentIsNegativeAndCallbackIsNull_ReturnsError) {
Pipe pipe;
- int result = mLooper->addFd(pipe.receiveFd, -1, Looper::EVENT_INPUT, NULL, NULL);
+ int result = mLooper->addFd(pipe.receiveFd, -1, Looper::EVENT_INPUT, nullptr, nullptr);
EXPECT_EQ(-1, result)
<< "addFd should return -1 because arguments were invalid";
@@ -381,7 +381,7 @@
TEST_F(LooperTest, AddFd_WhenNoCallbackAndAllowNonCallbacksIsFalse_ReturnsError) {
Pipe pipe;
sp<Looper> looper = new Looper(false /*allowNonCallbacks*/);
- int result = looper->addFd(pipe.receiveFd, 0, 0, NULL, NULL);
+ int result = looper->addFd(pipe.receiveFd, 0, 0, nullptr, nullptr);
EXPECT_EQ(-1, result)
<< "addFd should return -1 because arguments were invalid";
diff --git a/libutils/tests/LruCache_test.cpp b/libutils/tests/LruCache_test.cpp
index 4e885bb..c4d917b 100644
--- a/libutils/tests/LruCache_test.cpp
+++ b/libutils/tests/LruCache_test.cpp
@@ -110,7 +110,7 @@
class EntryRemovedCallback : public OnEntryRemoved<SimpleKey, StringValue> {
public:
- EntryRemovedCallback() : callbackCount(0), lastKey(-1), lastValue(NULL) { }
+ EntryRemovedCallback() : callbackCount(0), lastKey(-1), lastValue(nullptr) { }
~EntryRemovedCallback() {}
void operator()(SimpleKey& k, StringValue& v) {
callbackCount += 1;
@@ -153,7 +153,7 @@
TEST_F(LruCacheTest, Empty) {
LruCache<SimpleKey, StringValue> cache(100);
- EXPECT_EQ(NULL, cache.get(0));
+ EXPECT_EQ(nullptr, cache.get(0));
EXPECT_EQ(0u, cache.size());
}
@@ -175,7 +175,7 @@
cache.put(1, "one");
cache.put(2, "two");
cache.put(3, "three");
- EXPECT_EQ(NULL, cache.get(1));
+ EXPECT_EQ(nullptr, cache.get(1));
EXPECT_STREQ("two", cache.get(2));
EXPECT_STREQ("three", cache.get(3));
EXPECT_EQ(2u, cache.size());
@@ -188,7 +188,7 @@
cache.put(2, "two");
cache.put(3, "three");
cache.removeOldest();
- EXPECT_EQ(NULL, cache.get(1));
+ EXPECT_EQ(nullptr, cache.get(1));
EXPECT_STREQ("two", cache.get(2));
EXPECT_STREQ("three", cache.get(3));
EXPECT_EQ(2u, cache.size());
@@ -203,7 +203,7 @@
EXPECT_STREQ("one", cache.get(1));
cache.removeOldest();
EXPECT_STREQ("one", cache.get(1));
- EXPECT_EQ(NULL, cache.get(2));
+ EXPECT_EQ(nullptr, cache.get(2));
EXPECT_STREQ("three", cache.get(3));
EXPECT_EQ(2u, cache.size());
}
@@ -230,7 +230,7 @@
int index = random() % kNumKeys;
uint32_t key = hash_int(index);
const char *val = cache.get(key);
- if (val != NULL) {
+ if (val != nullptr) {
EXPECT_EQ(strings[index], val);
hitCount++;
} else {
diff --git a/libutils/tests/Vector_test.cpp b/libutils/tests/Vector_test.cpp
index e074a92..5336c40 100644
--- a/libutils/tests/Vector_test.cpp
+++ b/libutils/tests/Vector_test.cpp
@@ -98,7 +98,7 @@
// Checks that the size calculation (not the capacity calculation) doesn't
// overflow : the size here will be (1 + SIZE_MAX).
- EXPECT_DEATH(vector.insertArrayAt(NULL, 0, SIZE_MAX), "new_size overflow");
+ EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, SIZE_MAX), "new_size overflow");
}
TEST_F(VectorTest, _grow_OverflowCapacityDoubling) {
@@ -106,14 +106,14 @@
// This should fail because the calculated capacity will overflow even though
// the size of the vector doesn't.
- EXPECT_DEATH(vector.insertArrayAt(NULL, 0, (SIZE_MAX - 1)), "new_capacity overflow");
+ EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, (SIZE_MAX - 1)), "new_capacity overflow");
}
TEST_F(VectorTest, _grow_OverflowBufferAlloc) {
Vector<int> vector;
// This should fail because the capacity * sizeof(int) overflows, even
// though the capacity itself doesn't.
- EXPECT_DEATH(vector.insertArrayAt(NULL, 0, (SIZE_MAX / 2)), "new_alloc_size overflow");
+ EXPECT_DEATH(vector.insertArrayAt(nullptr, 0, (SIZE_MAX / 2)), "new_alloc_size overflow");
}
TEST_F(VectorTest, editArray_Shared) {
diff --git a/libziparchive/Android.bp b/libziparchive/Android.bp
index 075fb86..2606aa9 100644
--- a/libziparchive/Android.bp
+++ b/libziparchive/Android.bp
@@ -58,9 +58,11 @@
name: "libziparchive",
host_supported: true,
vendor_available: true,
+ recovery_available: true,
vndk: {
enabled: true,
},
+ double_loadable: true,
defaults: [
"libziparchive_defaults",
@@ -92,6 +94,10 @@
host_supported: true,
defaults: ["libziparchive_flags"],
+ data: [
+ "testdata/**/*",
+ ],
+
srcs: [
"entry_name_utils_test.cc",
"zip_archive_test.cc",
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index 5e5e7af..9536fc7 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -33,6 +33,10 @@
#include <memory>
#include <vector>
+#if defined(__BIONIC__)
+#include <android/fdsan.h>
+#endif
+
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/macros.h> // TEMP_FAILURE_RETRY may or may not be in unistd
@@ -165,6 +169,44 @@
return 0;
}
+ZipArchive::ZipArchive(const int fd, bool assume_ownership)
+ : mapped_zip(fd),
+ close_file(assume_ownership),
+ directory_offset(0),
+ central_directory(),
+ directory_map(new android::FileMap()),
+ num_entries(0),
+ hash_table_size(0),
+ hash_table(nullptr) {
+#if defined(__BIONIC__)
+ if (assume_ownership) {
+ android_fdsan_exchange_owner_tag(fd, 0, reinterpret_cast<uint64_t>(this));
+ }
+#endif
+}
+
+ZipArchive::ZipArchive(void* address, size_t length)
+ : mapped_zip(address, length),
+ close_file(false),
+ directory_offset(0),
+ central_directory(),
+ directory_map(new android::FileMap()),
+ num_entries(0),
+ hash_table_size(0),
+ hash_table(nullptr) {}
+
+ZipArchive::~ZipArchive() {
+ if (close_file && mapped_zip.GetFileDescriptor() >= 0) {
+#if defined(__BIONIC__)
+ android_fdsan_close_with_tag(mapped_zip.GetFileDescriptor(), reinterpret_cast<uint64_t>(this));
+#else
+ close(mapped_zip.GetFileDescriptor());
+#endif
+ }
+
+ free(hash_table);
+}
+
static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* archive,
off64_t file_length, off64_t read_amount, uint8_t* scan_buffer) {
const off64_t search_start = file_length - read_amount;
diff --git a/libziparchive/zip_archive_private.h b/libziparchive/zip_archive_private.h
index 18e0229..0a73300 100644
--- a/libziparchive/zip_archive_private.h
+++ b/libziparchive/zip_archive_private.h
@@ -156,33 +156,9 @@
uint32_t hash_table_size;
ZipString* hash_table;
- ZipArchive(const int fd, bool assume_ownership)
- : mapped_zip(fd),
- close_file(assume_ownership),
- directory_offset(0),
- central_directory(),
- directory_map(new android::FileMap()),
- num_entries(0),
- hash_table_size(0),
- hash_table(nullptr) {}
-
- ZipArchive(void* address, size_t length)
- : mapped_zip(address, length),
- close_file(false),
- directory_offset(0),
- central_directory(),
- directory_map(new android::FileMap()),
- num_entries(0),
- hash_table_size(0),
- hash_table(nullptr) {}
-
- ~ZipArchive() {
- if (close_file && mapped_zip.GetFileDescriptor() >= 0) {
- close(mapped_zip.GetFileDescriptor());
- }
-
- free(hash_table);
- }
+ ZipArchive(const int fd, bool assume_ownership);
+ ZipArchive(void* address, size_t length);
+ ~ZipArchive();
bool InitializeCentralDirectory(const char* debug_file_name, off64_t cd_start_offset,
size_t cd_size);
diff --git a/libziparchive/zip_archive_test.cc b/libziparchive/zip_archive_test.cc
index ad673dc..23dd5dc 100644
--- a/libziparchive/zip_archive_test.cc
+++ b/libziparchive/zip_archive_test.cc
@@ -34,7 +34,7 @@
#include <ziparchive/zip_archive.h>
#include <ziparchive/zip_archive_stream_entry.h>
-static std::string test_data_dir;
+static std::string test_data_dir = android::base::GetExecutableDirectory() + "/testdata";
static const std::string kMissingZip = "missing.zip";
static const std::string kValidZip = "valid.zip";
@@ -643,6 +643,55 @@
ASSERT_STREQ("I/O error", ErrorCodeString(kIoError));
}
+// A zip file whose local file header at offset zero is corrupted.
+//
+// ---------------
+// cat foo > a.txt
+// zip a.zip a.txt
+// cat a.zip | xxd -i
+//
+// Manual changes :
+// [2] = 0xff // Corrupt the LFH signature of entry 0.
+// [3] = 0xff // Corrupt the LFH signature of entry 0.
+static const std::vector<uint8_t> kZipFileWithBrokenLfhSignature{
+ //[lfh-sig-----------], [lfh contents---------------------------------
+ 0x50, 0x4b, 0xff, 0xff, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0x80,
+ //--------------------------------------------------------------------
+ 0x09, 0x4b, 0xa8, 0x65, 0x32, 0x7e, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00,
+ //-------------------------------] [file-name-----------------], [---
+ 0x00, 0x00, 0x05, 0x00, 0x1c, 0x00, 0x61, 0x2e, 0x74, 0x78, 0x74, 0x55,
+ // entry-contents------------------------------------------------------
+ 0x54, 0x09, 0x00, 0x03, 0x51, 0x24, 0x8b, 0x59, 0x51, 0x24, 0x8b, 0x59,
+ //--------------------------------------------------------------------
+ 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0x89, 0x42, 0x00, 0x00, 0x04, 0x88,
+ //-------------------------------------], [cd-record-sig-------], [---
+ 0x13, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x0a, 0x50, 0x4b, 0x01, 0x02, 0x1e,
+ // cd-record-----------------------------------------------------------
+ 0x03, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0x80, 0x09, 0x4b, 0xa8,
+ //--------------------------------------------------------------------
+ 0x65, 0x32, 0x7e, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05,
+ //--------------------------------------------------------------------
+ 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xa0,
+ //-] [lfh-file-header-off-], [file-name-----------------], [extra----
+ 0x81, 0x00, 0x00, 0x00, 0x00, 0x61, 0x2e, 0x74, 0x78, 0x74, 0x55, 0x54,
+ //--------------------------------------------------------------------
+ 0x05, 0x00, 0x03, 0x51, 0x24, 0x8b, 0x59, 0x75, 0x78, 0x0b, 0x00, 0x01,
+ //-------------------------------------------------------], [eocd-sig-
+ 0x04, 0x89, 0x42, 0x00, 0x00, 0x04, 0x88, 0x13, 0x00, 0x00, 0x50, 0x4b,
+ //-------], [---------------------------------------------------------
+ 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x4b, 0x00,
+ //-------------------------------------------]
+ 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00};
+
+TEST(ziparchive, BrokenLfhSignature) {
+ TemporaryFile tmp_file;
+ ASSERT_NE(-1, tmp_file.fd);
+ ASSERT_TRUE(android::base::WriteFully(tmp_file.fd, &kZipFileWithBrokenLfhSignature[0],
+ kZipFileWithBrokenLfhSignature.size()));
+ ZipArchiveHandle handle;
+ ASSERT_EQ(-1, OpenArchiveFd(tmp_file.fd, "LeadingNonZipBytes", &handle));
+}
+
class VectorReader : public zip_archive::Reader {
public:
VectorReader(const std::vector<uint8_t>& input) : Reader(), input_(input) {}
@@ -729,41 +778,3 @@
ASSERT_EQ(0u, writer.GetOutput().size());
}
}
-
-int main(int argc, char** argv) {
- ::testing::InitGoogleTest(&argc, argv);
-
- static struct option options[] = {{"test_data_dir", required_argument, nullptr, 't'},
- {nullptr, 0, nullptr, 0}};
-
- while (true) {
- int option_index;
- const int c = getopt_long_only(argc, argv, "", options, &option_index);
- if (c == -1) {
- break;
- }
-
- if (c == 't') {
- test_data_dir = optarg;
- }
- }
-
- if (test_data_dir.size() == 0) {
- printf("Test data flag (--test_data_dir) required\n\n");
- return -1;
- }
-
- if (test_data_dir[0] != '/') {
- std::vector<char> cwd_buffer(1024);
- const char* cwd = getcwd(cwd_buffer.data(), cwd_buffer.size() - 1);
- if (cwd == nullptr) {
- printf("Cannot get current working directory, use an absolute path instead, was %s\n\n",
- test_data_dir.c_str());
- return -2;
- }
- test_data_dir = '/' + test_data_dir;
- test_data_dir = cwd + test_data_dir;
- }
-
- return RUN_ALL_TESTS();
-}
diff --git a/llkd/Android.bp b/llkd/Android.bp
new file mode 100644
index 0000000..a6edd26
--- /dev/null
+++ b/llkd/Android.bp
@@ -0,0 +1,42 @@
+cc_library_headers {
+ name: "llkd_headers",
+
+ export_include_dirs: ["include"],
+}
+
+cc_library_static {
+ name: "libllkd",
+
+ srcs: [
+ "libllkd.cpp",
+ ],
+
+ shared_libs: [
+ "libbase",
+ "libcutils",
+ "liblog",
+ ],
+
+ export_include_dirs: ["include"],
+
+ cflags: ["-Werror"],
+}
+
+cc_binary {
+ name: "llkd",
+
+ srcs: [
+ "llkd.cpp",
+ ],
+ shared_libs: [
+ "libbase",
+ "libcutils",
+ "liblog",
+ ],
+ static_libs: [
+ "libllkd",
+ ],
+ cflags: ["-Werror"],
+
+ init_rc: ["llkd.rc"],
+}
diff --git a/llkd/OWNERS b/llkd/OWNERS
new file mode 100644
index 0000000..b6af537
--- /dev/null
+++ b/llkd/OWNERS
@@ -0,0 +1,2 @@
+salyzyn@google.com
+surenb@google.com
diff --git a/llkd/README.md b/llkd/README.md
new file mode 100644
index 0000000..2314583
--- /dev/null
+++ b/llkd/README.md
@@ -0,0 +1,131 @@
+Android Live-LocK Daemon
+========================
+
+Introduction
+------------
+
+Android Live-LocK Daemon (llkd) is used to catch kernel deadlocks and mitigate.
+
+Code is structured to allow integration into another service as either as part
+of the main loop, or spun off as a thread should that be necessary. A default
+standalone implementation is provided by llkd component.
+
+The 'C' interface from libllkd component is thus:
+
+ #include "llkd.h"
+ bool llkInit(const char* threadname) /* return true if enabled */
+ unsigned llkCheckMillseconds(void) /* ms to sleep for next check */
+
+If a threadname is provided, a thread will be automatically spawned, otherwise
+caller must call llkCheckMilliseconds in its main loop. Function will return
+the period of time before the next expected call to this handler.
+
+Operations
+----------
+
+If a thread is in D or Z state with no forward progress for longer than
+ro.llk.timeout_ms, or ro.llk.[D|Z].timeout_ms, kill the process or parent
+process respectively. If another scan shows the same process continues to
+exist, then have a confirmed live-lock condition and need to panic. Panic
+the kernel in a manner to provide the greatest bugreporting details as to the
+condition. Add a alarm self watchdog should llkd ever get locked up that is
+double the expected time to flow through the mainloop. Sampling is every
+ro.llk_sample_ms.
+
+Default will not monitor init, or [kthreadd] and all that [kthreadd] spawns.
+This reduces the effectiveness of llkd by limiting its coverage. If there is
+value in covering [kthreadd] spawned threads, the requirement will be that
+the drivers not remain in a persistent 'D' state, or that they have mechanisms
+to recover the thread should it be killed externally (this is good driver
+coding hygiene, a common request to add such to publicly reviewed kernel.org
+maintained drivers). For instance use wait_event_interruptible() instead of
+wait_event(). The blacklists can be adjusted accordingly if these
+conditions are met to cover kernel components.
+
+An accompanying gTest set have been added, and will setup a persistent D or Z
+process, with and without forward progress, but not in a live-lock state
+because that would require a buggy kernel, or a module or kernel modification
+to stimulate. The test will check that llkd will mitigate first by killing
+the appropriate process. D state is setup by vfork() waiting for exec() in
+child process. Z state is setup by fork() and an un-waited for child process.
+Should be noted that both of these conditions should never happen on Android
+on purpose, and llkd effectively sweeps up processes that create these
+conditions. If the test can, it will reconfigure llkd to expedite the test
+duration by adjusting the ro.llk.* Android properties. Tests run the D state
+with some scheduling progress to ensure that ABA checking prevents false
+triggers. If 100% reliable ABA on platform, then ro.llk.killtest can be
+set to false; however this will result in some of the unit tests to panic
+kernel instead of deal with more graceful kill operation.
+
+Android Properties
+------------------
+
+Android Properties llkd respond to (*prop*_ms parms are in milliseconds):
+
+#### ro.config.low_ram
+default false, if true do not sysrq t (dump all threads).
+
+#### ro.llk.enable
+default false, allow live-lock daemon to be enabled.
+
+#### llk.enable
+default ro.llk.enable, and evaluated for eng.
+
+#### ro.khungtask.enable
+default false, allow [khungtask] daemon to be enabled.
+
+#### khungtask.enable
+default ro.khungtask.enable and evaluated for eng.
+
+#### ro.llk.mlockall
+default false, enable call to mlockall().
+
+#### ro.khungtask.timeout
+default value 12 minutes, [khungtask] maximum timelimit.
+
+#### ro.llk.timeout_ms
+default 10 minutes, D or Z maximum timelimit, double this value and it sets
+the alarm watchdog for llkd.
+
+#### ro.llk.D.timeout_ms
+default ro.llk.timeout_ms, D maximum timelimit.
+
+#### ro.llk.Z.timeout_ms
+default ro.llk.timeout_ms, Z maximum timelimit.
+
+#### ro.llk.check_ms
+default 2 minutes samples of threads for D or Z.
+
+#### ro.llk.blacklist.process
+default 0,1,2 (kernel, init and [kthreadd]) plus process names
+init,[kthreadd],[khungtaskd],lmkd,lmkd.llkd,llkd,watchdogd,
+[watchdogd],[watchdogd/0],...,[watchdogd/***get_nprocs**-1*].
+The string false is the equivalent to an empty list.
+Do not watch these processes. A process can be comm, cmdline or pid reference.
+NB: automated default here can be larger than the current maximum property
+size of 92.
+NB: false is a very very very unlikely process to want to blacklist.
+
+#### ro.llk.blacklist.parent
+default 0,2 (kernel and [kthreadd]).
+The string false is the equivalent to an empty list.
+Do not watch processes that have this parent.
+A parent process can be comm, cmdline or pid reference.
+
+#### ro.llk.blacklist.uid
+default *empty* or false, comma separated list of uid numbers or names.
+The string false is the equivalent to an empty list.
+Do not watch processes that match this uid.
+
+Architectural Concerns
+----------------------
+
+- built-in [khungtask] daemon is too generic and trips on driver code that
+ sits around in D state too much. To switch to S instead makes the task(s)
+ killable, so the drivers should be able to resurrect them if needed.
+- Properties are limited to 92 characters.
+- Create kernel module and associated gTest to actually test panic.
+- Create gTest to test out blacklist (ro.llk.blacklist.*properties* generally
+ not be inputs). Could require more test-only interfaces to libllkd.
+- Speed up gTest using something else than ro.llk.*properties*, which should
+ not be inputs as they should be baked into the product.
diff --git a/llkd/include/llkd.h b/llkd/include/llkd.h
new file mode 100644
index 0000000..e3ae4bb
--- /dev/null
+++ b/llkd/include/llkd.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef _LLKD_H_
+#define _LLKD_H_
+
+#ifndef LOG_TAG
+#define LOG_TAG "livelock"
+#endif
+
+#include <stdbool.h>
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+bool llkInit(const char* threadname); /* threadname NULL, not spawned */
+unsigned llkCheckMilliseconds(void);
+
+/* clang-format off */
+#define LLK_ENABLE_WRITEABLE_PROPERTY "llk.enable"
+#define LLK_ENABLE_PROPERTY "ro." LLK_ENABLE_WRITEABLE_PROPERTY
+#define LLK_ENABLE_DEFAULT false /* "eng" and userdebug true */
+#define KHT_ENABLE_WRITEABLE_PROPERTY "khungtask.enable"
+#define KHT_ENABLE_PROPERTY "ro." KHT_ENABLE_WRITEABLE_PROPERTY
+#define LLK_MLOCKALL_PROPERTY "ro.llk.mlockall"
+#define LLK_MLOCKALL_DEFAULT true
+#define LLK_KILLTEST_PROPERTY "ro.llk.killtest"
+#define LLK_KILLTEST_DEFAULT true
+#define LLK_TIMEOUT_MS_PROPERTY "ro.llk.timeout_ms"
+#define KHT_TIMEOUT_PROPERTY "ro.khungtask.timeout"
+#define LLK_D_TIMEOUT_MS_PROPERTY "ro.llk.D.timeout_ms"
+#define LLK_Z_TIMEOUT_MS_PROPERTY "ro.llk.Z.timeout_ms"
+#define LLK_CHECK_MS_PROPERTY "ro.llk.check_ms"
+/* LLK_CHECK_MS_DEFAULT = actual timeout_ms / LLK_CHECKS_PER_TIMEOUT_DEFAULT */
+#define LLK_CHECKS_PER_TIMEOUT_DEFAULT 5
+#define LLK_BLACKLIST_PROCESS_PROPERTY "ro.llk.blacklist.process"
+#define LLK_BLACKLIST_PROCESS_DEFAULT \
+ "0,1,2,init,[kthreadd],[khungtaskd],lmkd,lmkd.llkd,llkd,watchdogd,[watchdogd],[watchdogd/0]"
+#define LLK_BLACKLIST_PARENT_PROPERTY "ro.llk.blacklist.parent"
+#define LLK_BLACKLIST_PARENT_DEFAULT "0,2,[kthreadd]"
+#define LLK_BLACKLIST_UID_PROPERTY "ro.llk.blacklist.uid"
+#define LLK_BLACKLIST_UID_DEFAULT ""
+/* clang-format on */
+
+__END_DECLS
+
+#ifdef __cplusplus
+extern "C++" { /* In case this included wrapped with __BEGIN_DECLS */
+
+#include <chrono>
+
+__BEGIN_DECLS
+/* C++ code allowed to not specify threadname argument for this C linkage */
+bool llkInit(const char* threadname = nullptr);
+__END_DECLS
+std::chrono::milliseconds llkCheck(bool checkRunning = false);
+
+/* clang-format off */
+#define LLK_TIMEOUT_MS_DEFAULT std::chrono::duration_cast<milliseconds>(std::chrono::minutes(10))
+#define LLK_TIMEOUT_MS_MINIMUM std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::seconds(10))
+#define LLK_CHECK_MS_MINIMUM std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::seconds(1))
+/* clang-format on */
+
+} /* extern "C++" */
+#endif /* __cplusplus */
+
+#endif /* _LLKD_H_ */
diff --git a/llkd/libllkd.cpp b/llkd/libllkd.cpp
new file mode 100644
index 0000000..48551f2
--- /dev/null
+++ b/llkd/libllkd.cpp
@@ -0,0 +1,1172 @@
+/*
+ * Copyright (C) 2018 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 "llkd.h"
+
+#include <ctype.h>
+#include <dirent.h> // opendir() and readdir()
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <pwd.h> // getpwuid()
+#include <signal.h>
+#include <stdint.h>
+#include <sys/cdefs.h> // ___STRING, __predict_true() and _predict_false()
+#include <sys/mman.h> // mlockall()
+#include <sys/prctl.h>
+#include <sys/stat.h> // lstat()
+#include <sys/syscall.h> // __NR_getdents64
+#include <sys/sysinfo.h> // get_nprocs_conf()
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <chrono>
+#include <ios>
+#include <sstream>
+#include <string>
+#include <unordered_map>
+#include <unordered_set>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
+#include <android-base/properties.h>
+#include <android-base/strings.h>
+#include <cutils/android_get_control_file.h>
+#include <log/log_main.h>
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
+
+#define TASK_COMM_LEN 16 // internal kernel, not uapi, from .../linux/include/linux/sched.h
+
+using namespace std::chrono_literals;
+using namespace std::chrono;
+using namespace std::literals;
+
+namespace {
+
+constexpr pid_t kernelPid = 0;
+constexpr pid_t initPid = 1;
+constexpr pid_t kthreaddPid = 2;
+
+constexpr char procdir[] = "/proc/";
+
+// Configuration
+milliseconds llkUpdate; // last check ms signature
+milliseconds llkCycle; // ms to next thread check
+bool llkEnable = LLK_ENABLE_DEFAULT; // llk daemon enabled
+bool llkRunning = false; // thread is running
+bool llkMlockall = LLK_MLOCKALL_DEFAULT; // run mlocked
+bool llkTestWithKill = LLK_KILLTEST_DEFAULT; // issue test kills
+milliseconds llkTimeoutMs = LLK_TIMEOUT_MS_DEFAULT; // default timeout
+enum { llkStateD, llkStateZ, llkNumStates }; // state indexes
+milliseconds llkStateTimeoutMs[llkNumStates]; // timeout override for each detection state
+milliseconds llkCheckMs; // checking interval to inspect any
+ // persistent live-locked states
+bool llkLowRam; // ro.config.low_ram
+bool khtEnable = LLK_ENABLE_DEFAULT; // [khungtaskd] panic
+// [khungtaskd] should have a timeout beyond the granularity of llkTimeoutMs.
+// Provides a wide angle of margin b/c khtTimeout is also its granularity.
+seconds khtTimeout = duration_cast<seconds>(llkTimeoutMs * (1 + LLK_CHECKS_PER_TIMEOUT_DEFAULT) /
+ LLK_CHECKS_PER_TIMEOUT_DEFAULT);
+
+// Blacklist variables, initialized with comma separated lists of high false
+// positive and/or dangerous references, e.g. without self restart, for pid,
+// ppid, name and uid:
+
+// list of pids, or tids or names to skip. kernel pid (0), init pid (1),
+// [kthreadd] pid (2), ourselves, "init", "[kthreadd]", "lmkd", "llkd" or
+// combinations of watchdogd in kernel and user space.
+std::unordered_set<std::string> llkBlacklistProcess;
+// list of parent pids, comm or cmdline names to skip. default:
+// kernel pid (0), [kthreadd] (2), or ourselves, enforced and implied
+std::unordered_set<std::string> llkBlacklistParent;
+// list of uids, and uid names, to skip, default nothing
+std::unordered_set<std::string> llkBlacklistUid;
+
+class dir {
+ public:
+ enum level { proc, task, numLevels };
+
+ private:
+ int fd;
+ size_t available_bytes;
+ dirent* next;
+ // each directory level picked to be just north of 4K in size
+ static constexpr size_t buffEntries = 15;
+ static dirent buff[numLevels][buffEntries];
+
+ bool fill(enum level index) {
+ if (index >= numLevels) return false;
+ if (available_bytes != 0) return true;
+ if (__predict_false(fd < 0)) return false;
+ // getdents64 has no libc wrapper
+ auto rc = TEMP_FAILURE_RETRY(syscall(__NR_getdents64, fd, buff[index], sizeof(buff[0]), 0));
+ if (rc <= 0) return false;
+ available_bytes = rc;
+ next = buff[index];
+ return true;
+ }
+
+ public:
+ dir() : fd(-1), available_bytes(0), next(nullptr) {}
+
+ explicit dir(const char* directory)
+ : fd(__predict_true(directory != nullptr)
+ ? ::open(directory, O_CLOEXEC | O_DIRECTORY | O_RDONLY)
+ : -1),
+ available_bytes(0),
+ next(nullptr) {}
+
+ explicit dir(const std::string&& directory)
+ : fd(::open(directory.c_str(), O_CLOEXEC | O_DIRECTORY | O_RDONLY)),
+ available_bytes(0),
+ next(nullptr) {}
+
+ explicit dir(const std::string& directory)
+ : fd(::open(directory.c_str(), O_CLOEXEC | O_DIRECTORY | O_RDONLY)),
+ available_bytes(0),
+ next(nullptr) {}
+
+ // Don't need any copy or move constructors.
+ explicit dir(const dir& c) = delete;
+ explicit dir(dir& c) = delete;
+ explicit dir(dir&& c) = delete;
+
+ ~dir() {
+ if (fd >= 0) {
+ ::close(fd);
+ }
+ }
+
+ operator bool() const { return fd >= 0; }
+
+ void reset(void) {
+ if (fd >= 0) {
+ ::close(fd);
+ fd = -1;
+ available_bytes = 0;
+ next = nullptr;
+ }
+ }
+
+ dir& reset(const char* directory) {
+ reset();
+ // available_bytes will _always_ be zero here as its value is
+ // intimately tied to fd < 0 or not.
+ fd = ::open(directory, O_CLOEXEC | O_DIRECTORY | O_RDONLY);
+ return *this;
+ }
+
+ void rewind(void) {
+ if (fd >= 0) {
+ ::lseek(fd, off_t(0), SEEK_SET);
+ available_bytes = 0;
+ next = nullptr;
+ }
+ }
+
+ dirent* read(enum level index = proc, dirent* def = nullptr) {
+ if (!fill(index)) return def;
+ auto ret = next;
+ available_bytes -= next->d_reclen;
+ next = reinterpret_cast<dirent*>(reinterpret_cast<char*>(next) + next->d_reclen);
+ return ret;
+ }
+} llkTopDirectory;
+
+dirent dir::buff[dir::numLevels][dir::buffEntries];
+
+// helper functions
+
+bool llkIsMissingExeLink(pid_t tid) {
+ char c;
+ // CAP_SYS_PTRACE is required to prevent ret == -1, but ENOENT is signal
+ auto ret = ::readlink((procdir + std::to_string(tid) + "/exe").c_str(), &c, sizeof(c));
+ return (ret == -1) && (errno == ENOENT);
+}
+
+// Common routine where caller accepts empty content as error/passthrough.
+// Reduces the churn of reporting read errors in the callers.
+std::string ReadFile(std::string&& path) {
+ std::string content;
+ if (!android::base::ReadFileToString(path, &content)) {
+ PLOG(DEBUG) << "Read " << path << " failed";
+ content = "";
+ }
+ return content;
+}
+
+std::string llkProcGetName(pid_t tid, const char* node = "/cmdline") {
+ std::string content = ReadFile(procdir + std::to_string(tid) + node);
+ static constexpr char needles[] = " \t\r\n"; // including trailing nul
+ auto pos = content.find_first_of(needles, 0, sizeof(needles));
+ if (pos != std::string::npos) {
+ content.erase(pos);
+ }
+ return content;
+}
+
+uid_t llkProcGetUid(pid_t tid) {
+ // Get the process' uid. The following read from /status is admittedly
+ // racy, prone to corruption due to shape-changes. The consequences are
+ // not catastrophic as we sample a few times before taking action.
+ //
+ // If /loginuid worked on reliably, or on Android (all tasks report -1)...
+ // Android lmkd causes /cgroup to contain memory:/<dom>/uid_<uid>/pid_<pid>
+ // which is tighter, but also not reliable.
+ std::string content = ReadFile(procdir + std::to_string(tid) + "/status");
+ static constexpr char Uid[] = "\nUid:";
+ auto pos = content.find(Uid);
+ if (pos == std::string::npos) {
+ return -1;
+ }
+ pos += ::strlen(Uid);
+ while ((pos < content.size()) && ::isblank(content[pos])) {
+ ++pos;
+ }
+ content.erase(0, pos);
+ for (pos = 0; (pos < content.size()) && ::isdigit(content[pos]); ++pos) {
+ ;
+ }
+ // Content of form 'Uid: 0 0 0 0', newline is error
+ if ((pos >= content.size()) || !::isblank(content[pos])) {
+ return -1;
+ }
+ content.erase(pos);
+ uid_t ret;
+ if (!android::base::ParseInt(content, &ret, uid_t(0))) {
+ return -1;
+ }
+ return ret;
+}
+
+struct proc {
+ pid_t tid; // monitored thread id (in Z or D state).
+ nanoseconds schedUpdate; // /proc/<tid>/sched "se.avg.lastUpdateTime",
+ uint64_t nrSwitches; // /proc/<tid>/sched "nr_switches" for
+ // refined ABA problem detection, determine
+ // forward scheduling progress.
+ milliseconds update; // llkUpdate millisecond signature of last.
+ milliseconds count; // duration in state.
+ pid_t pid; // /proc/<pid> before iterating through
+ // /proc/<pid>/task/<tid> for threads.
+ pid_t ppid; // /proc/<tid>/stat field 4 parent pid.
+ uid_t uid; // /proc/<tid>/status Uid: field.
+ unsigned time; // sum of /proc/<tid>/stat field 14 utime &
+ // 15 stime for coarse ABA problem detection.
+ std::string cmdline; // cached /cmdline content
+ char state; // /proc/<tid>/stat field 3: Z or D
+ // (others we do not monitor: S, R, T or ?)
+ char comm[TASK_COMM_LEN + 3]; // space for adding '[' and ']'
+ bool exeMissingValid; // exeMissing has been cached
+ bool cmdlineValid; // cmdline has been cached
+ bool updated; // cleared before monitoring pass.
+ bool killed; // sent a kill to this thread, next panic...
+
+ void setComm(const char* _comm) { strncpy(comm + 1, _comm, sizeof(comm) - 2); }
+
+ proc(pid_t tid, pid_t pid, pid_t ppid, const char* _comm, int time, char state)
+ : tid(tid),
+ schedUpdate(0),
+ nrSwitches(0),
+ update(llkUpdate),
+ count(0ms),
+ pid(pid),
+ ppid(ppid),
+ uid(-1),
+ time(time),
+ state(state),
+ exeMissingValid(false),
+ cmdlineValid(false),
+ updated(true),
+ killed(!llkTestWithKill) {
+ memset(comm, '\0', sizeof(comm));
+ setComm(_comm);
+ }
+
+ const char* getComm(void) {
+ if (comm[1] == '\0') { // comm Valid?
+ strncpy(comm + 1, llkProcGetName(tid, "/comm").c_str(), sizeof(comm) - 2);
+ }
+ if (!exeMissingValid) {
+ if (llkIsMissingExeLink(tid)) {
+ comm[0] = '[';
+ }
+ exeMissingValid = true;
+ }
+ size_t len = strlen(comm + 1);
+ if (__predict_true(len < (sizeof(comm) - 1))) {
+ if (comm[0] == '[') {
+ if ((comm[len] != ']') && __predict_true(len < (sizeof(comm) - 2))) {
+ comm[++len] = ']';
+ comm[++len] = '\0';
+ }
+ } else {
+ if (comm[len] == ']') {
+ comm[len] = '\0';
+ }
+ }
+ }
+ return &comm[comm[0] != '['];
+ }
+
+ const char* getCmdline(void) {
+ if (!cmdlineValid) {
+ cmdline = llkProcGetName(tid);
+ cmdlineValid = true;
+ }
+ return cmdline.c_str();
+ }
+
+ uid_t getUid(void) {
+ if (uid <= 0) { // Churn on root user, because most likely to setuid()
+ uid = llkProcGetUid(tid);
+ }
+ return uid;
+ }
+
+ void reset(void) { // reset cache, if we detected pid rollover
+ uid = -1;
+ state = '?';
+ cmdline = "";
+ comm[0] = '\0';
+ exeMissingValid = false;
+ cmdlineValid = false;
+ }
+};
+
+std::unordered_map<pid_t, proc> tids;
+
+// Check range and setup defaults, in order of propagation:
+// llkTimeoutMs
+// llkCheckMs
+// ...
+// KISS to keep it all self-contained, and called multiple times as parameters
+// are interpreted so that defaults, llkCheckMs and llkCycle make sense.
+void llkValidate() {
+ if (llkTimeoutMs == 0ms) {
+ llkTimeoutMs = LLK_TIMEOUT_MS_DEFAULT;
+ }
+ llkTimeoutMs = std::max(llkTimeoutMs, LLK_TIMEOUT_MS_MINIMUM);
+ if (llkCheckMs == 0ms) {
+ llkCheckMs = llkTimeoutMs / LLK_CHECKS_PER_TIMEOUT_DEFAULT;
+ }
+ llkCheckMs = std::min(llkCheckMs, llkTimeoutMs);
+
+ for (size_t state = 0; state < ARRAY_SIZE(llkStateTimeoutMs); ++state) {
+ if (llkStateTimeoutMs[state] == 0ms) {
+ llkStateTimeoutMs[state] = llkTimeoutMs;
+ }
+ llkStateTimeoutMs[state] =
+ std::min(std::max(llkStateTimeoutMs[state], LLK_TIMEOUT_MS_MINIMUM), llkTimeoutMs);
+ llkCheckMs = std::min(llkCheckMs, llkStateTimeoutMs[state]);
+ }
+
+ llkCheckMs = std::max(llkCheckMs, LLK_CHECK_MS_MINIMUM);
+ if (llkCycle == 0ms) {
+ llkCycle = llkCheckMs;
+ }
+ llkCycle = std::min(llkCycle, llkCheckMs);
+}
+
+milliseconds llkGetTimespecDiffMs(timespec* from, timespec* to) {
+ return duration_cast<milliseconds>(seconds(to->tv_sec - from->tv_sec)) +
+ duration_cast<milliseconds>(nanoseconds(to->tv_nsec - from->tv_nsec));
+}
+
+std::string llkProcGetName(pid_t tid, const char* comm, const char* cmdline) {
+ if ((cmdline != nullptr) && (*cmdline != '\0')) {
+ return cmdline;
+ }
+ if ((comm != nullptr) && (*comm != '\0')) {
+ return comm;
+ }
+
+ // UNLIKELY! Here because killed before we kill it?
+ // Assume change is afoot, do not call llkTidAlloc
+
+ // cmdline ?
+ std::string content = llkProcGetName(tid);
+ if (content.size() != 0) {
+ return content;
+ }
+ // Comm instead?
+ content = llkProcGetName(tid, "/comm");
+ if (llkIsMissingExeLink(tid) && (content.size() != 0)) {
+ return '[' + content + ']';
+ }
+ return content;
+}
+
+int llkKillOneProcess(pid_t pid, char state, pid_t tid, const char* tcomm = nullptr,
+ const char* tcmdline = nullptr, const char* pcomm = nullptr,
+ const char* pcmdline = nullptr) {
+ std::string forTid;
+ if (tid != pid) {
+ forTid = " for '" + llkProcGetName(tid, tcomm, tcmdline) + "' (" + std::to_string(tid) + ")";
+ }
+ LOG(INFO) << "Killing '" << llkProcGetName(pid, pcomm, pcmdline) << "' (" << pid
+ << ") to check forward scheduling progress in " << state << " state" << forTid;
+ // CAP_KILL required
+ errno = 0;
+ auto r = ::kill(pid, SIGKILL);
+ if (r) {
+ PLOG(ERROR) << "kill(" << pid << ")=" << r << ' ';
+ }
+
+ return r;
+}
+
+// Kill one process
+int llkKillOneProcess(pid_t pid, proc* tprocp) {
+ return llkKillOneProcess(pid, tprocp->state, tprocp->tid, tprocp->getComm(),
+ tprocp->getCmdline());
+}
+
+// Kill one process specified by kprocp
+int llkKillOneProcess(proc* kprocp, proc* tprocp) {
+ if (kprocp == nullptr) {
+ return -2;
+ }
+
+ return llkKillOneProcess(kprocp->tid, tprocp->state, tprocp->tid, tprocp->getComm(),
+ tprocp->getCmdline(), kprocp->getComm(), kprocp->getCmdline());
+}
+
+// Acquire file descriptor from environment, or open and cache it.
+// NB: cache is unnecessary in our current context, pedantically
+// required to prevent leakage of file descriptors in the future.
+int llkFileToWriteFd(const std::string& file) {
+ static std::unordered_map<std::string, int> cache;
+ auto search = cache.find(file);
+ if (search != cache.end()) return search->second;
+ auto fd = android_get_control_file(file.c_str());
+ if (fd >= 0) return fd;
+ fd = TEMP_FAILURE_RETRY(::open(file.c_str(), O_WRONLY | O_CLOEXEC));
+ if (fd >= 0) cache.emplace(std::make_pair(file, fd));
+ return fd;
+}
+
+// Wrap android::base::WriteStringToFile to use android_get_control_file.
+bool llkWriteStringToFile(const std::string& string, const std::string& file) {
+ auto fd = llkFileToWriteFd(file);
+ if (fd < 0) return false;
+ return android::base::WriteStringToFd(string, fd);
+}
+
+bool llkWriteStringToFileConfirm(const std::string& string, const std::string& file) {
+ auto fd = llkFileToWriteFd(file);
+ auto ret = (fd < 0) ? false : android::base::WriteStringToFd(string, fd);
+ std::string content;
+ if (!android::base::ReadFileToString(file, &content)) return ret;
+ return android::base::Trim(content) == string;
+}
+
+void llkPanicKernel(bool dump, pid_t tid, const char* state) __noreturn;
+void llkPanicKernel(bool dump, pid_t tid, const char* state) {
+ auto sysrqTriggerFd = llkFileToWriteFd("/proc/sysrq-trigger");
+ if (sysrqTriggerFd < 0) {
+ // DYB
+ llkKillOneProcess(initPid, 'R', tid);
+ // The answer to life, the universe and everything
+ ::exit(42);
+ // NOTREACHED
+ }
+ ::sync();
+ if (dump) {
+ // Show all locks that are held
+ android::base::WriteStringToFd("d", sysrqTriggerFd);
+ // This can trigger hardware watchdog, that is somewhat _ok_.
+ // But useless if pstore configured for <256KB, low ram devices ...
+ if (!llkLowRam) {
+ android::base::WriteStringToFd("t", sysrqTriggerFd);
+ }
+ ::usleep(200000); // let everything settle
+ }
+ llkWriteStringToFile("SysRq : Trigger a crash : 'livelock,"s + state + "'\n", "/dev/kmsg");
+ android::base::WriteStringToFd("c", sysrqTriggerFd);
+ // NOTREACHED
+ // DYB
+ llkKillOneProcess(initPid, 'R', tid);
+ // I sat at my desk, stared into the garden and thought '42 will do'.
+ // I typed it out. End of story
+ ::exit(42);
+ // NOTREACHED
+}
+
+void llkAlarmHandler(int) {
+ llkPanicKernel(false, ::getpid(), "alarm");
+}
+
+milliseconds GetUintProperty(const std::string& key, milliseconds def) {
+ return milliseconds(android::base::GetUintProperty(key, static_cast<uint64_t>(def.count()),
+ static_cast<uint64_t>(def.max().count())));
+}
+
+seconds GetUintProperty(const std::string& key, seconds def) {
+ return seconds(android::base::GetUintProperty(key, static_cast<uint64_t>(def.count()),
+ static_cast<uint64_t>(def.max().count())));
+}
+
+proc* llkTidLookup(pid_t tid) {
+ auto search = tids.find(tid);
+ if (search == tids.end()) {
+ return nullptr;
+ }
+ return &search->second;
+}
+
+void llkTidRemove(pid_t tid) {
+ tids.erase(tid);
+}
+
+proc* llkTidAlloc(pid_t tid, pid_t pid, pid_t ppid, const char* comm, int time, char state) {
+ auto it = tids.emplace(std::make_pair(tid, proc(tid, pid, ppid, comm, time, state)));
+ return &it.first->second;
+}
+
+std::string llkFormat(milliseconds ms) {
+ auto sec = duration_cast<seconds>(ms);
+ std::ostringstream s;
+ s << sec.count() << '.';
+ auto f = s.fill('0');
+ auto w = s.width(3);
+ s << std::right << (ms - sec).count();
+ s.width(w);
+ s.fill(f);
+ s << 's';
+ return s.str();
+}
+
+std::string llkFormat(seconds s) {
+ return std::to_string(s.count()) + 's';
+}
+
+std::string llkFormat(bool flag) {
+ return flag ? "true" : "false";
+}
+
+std::string llkFormat(const std::unordered_set<std::string>& blacklist) {
+ std::string ret;
+ for (auto entry : blacklist) {
+ if (ret.size()) {
+ ret += ",";
+ }
+ ret += entry;
+ }
+ return ret;
+}
+
+// We only officially support comma separators, but wetware being what they
+// are will take some liberty and I do not believe they should be punished.
+std::unordered_set<std::string> llkSplit(const std::string& s) {
+ std::unordered_set<std::string> result;
+
+ // Special case, allow boolean false to empty the list, otherwise expected
+ // source of input from android::base::GetProperty will supply the default
+ // value on empty content in the property.
+ if (s == "false") return result;
+
+ size_t base = 0;
+ while (s.size() > base) {
+ auto found = s.find_first_of(", \t:", base);
+ // Only emplace content, empty entries are not an option
+ if (found != base) result.emplace(s.substr(base, found - base));
+ if (found == s.npos) break;
+ base = found + 1;
+ }
+ return result;
+}
+
+bool llkSkipName(const std::string& name,
+ const std::unordered_set<std::string>& blacklist = llkBlacklistProcess) {
+ if ((name.size() == 0) || (blacklist.size() == 0)) {
+ return false;
+ }
+
+ return blacklist.find(name) != blacklist.end();
+}
+
+bool llkSkipPid(pid_t pid) {
+ return llkSkipName(std::to_string(pid), llkBlacklistProcess);
+}
+
+bool llkSkipPpid(pid_t ppid) {
+ return llkSkipName(std::to_string(ppid), llkBlacklistParent);
+}
+
+bool llkSkipUid(uid_t uid) {
+ // Match by number?
+ if (llkSkipName(std::to_string(uid), llkBlacklistUid)) {
+ return true;
+ }
+
+ // Match by name?
+ auto pwd = ::getpwuid(uid);
+ return (pwd != nullptr) && __predict_true(pwd->pw_name != nullptr) &&
+ __predict_true(pwd->pw_name[0] != '\0') && llkSkipName(pwd->pw_name, llkBlacklistUid);
+}
+
+bool getValidTidDir(dirent* dp, std::string* piddir) {
+ if (!::isdigit(dp->d_name[0])) {
+ return false;
+ }
+
+ // Corner case can not happen in reality b/c of above ::isdigit check
+ if (__predict_false(dp->d_type != DT_DIR)) {
+ if (__predict_false(dp->d_type == DT_UNKNOWN)) { // can't b/c procfs
+ struct stat st;
+ *piddir = procdir;
+ *piddir += dp->d_name;
+ return (lstat(piddir->c_str(), &st) == 0) && (st.st_mode & S_IFDIR);
+ }
+ return false;
+ }
+
+ *piddir = procdir;
+ *piddir += dp->d_name;
+ return true;
+}
+
+bool llkIsMonitorState(char state) {
+ return (state == 'Z') || (state == 'D');
+}
+
+// returns -1 if not found
+long long getSchedValue(const std::string& schedString, const char* key) {
+ auto pos = schedString.find(key);
+ if (pos == std::string::npos) {
+ return -1;
+ }
+ pos = schedString.find(':', pos);
+ if (__predict_false(pos == std::string::npos)) {
+ return -1;
+ }
+ while ((++pos < schedString.size()) && ::isblank(schedString[pos])) {
+ ;
+ }
+ long long ret;
+ if (!android::base::ParseInt(schedString.substr(pos), &ret, static_cast<long long>(0))) {
+ return -1;
+ }
+ return ret;
+}
+
+// Primary ABA mitigation watching last time schedule activity happened
+void llkCheckSchedUpdate(proc* procp, const std::string& piddir) {
+ // Audit finds /proc/<tid>/sched is just over 1K, and
+ // is rarely larger than 2K, even less on Android.
+ // For example, the "se.avg.lastUpdateTime" field we are
+ // interested in typically within the primary set in
+ // the first 1K.
+ //
+ // Proc entries can not be read >1K atomically via libbase,
+ // but if there are problems we assume at least a few
+ // samples of reads occur before we take any real action.
+ std::string schedString = ReadFile(piddir + "/sched");
+ if (schedString.size() == 0) {
+ // /schedstat is not as standardized, but in 3.1+
+ // Android devices, the third field is nr_switches
+ // from /sched:
+ schedString = ReadFile(piddir + "/schedstat");
+ if (schedString.size() == 0) {
+ return;
+ }
+ auto val = static_cast<unsigned long long>(-1);
+ if (((::sscanf(schedString.c_str(), "%*d %*d %llu", &val)) == 1) &&
+ (val != static_cast<unsigned long long>(-1)) && (val != 0) &&
+ (val != procp->nrSwitches)) {
+ procp->nrSwitches = val;
+ procp->count = 0ms;
+ procp->killed = !llkTestWithKill;
+ }
+ return;
+ }
+
+ auto val = getSchedValue(schedString, "\nse.avg.lastUpdateTime");
+ if (val == -1) {
+ val = getSchedValue(schedString, "\nse.svg.last_update_time");
+ }
+ if (val != -1) {
+ auto schedUpdate = nanoseconds(val);
+ if (schedUpdate != procp->schedUpdate) {
+ procp->schedUpdate = schedUpdate;
+ procp->count = 0ms;
+ procp->killed = !llkTestWithKill;
+ }
+ }
+
+ val = getSchedValue(schedString, "\nnr_switches");
+ if (val != -1) {
+ if (static_cast<uint64_t>(val) != procp->nrSwitches) {
+ procp->nrSwitches = val;
+ procp->count = 0ms;
+ procp->killed = !llkTestWithKill;
+ }
+ }
+}
+
+void llkLogConfig(void) {
+ LOG(INFO) << "ro.config.low_ram=" << llkFormat(llkLowRam) << "\n"
+ << LLK_ENABLE_PROPERTY "=" << llkFormat(llkEnable) << "\n"
+ << KHT_ENABLE_PROPERTY "=" << llkFormat(khtEnable) << "\n"
+ << LLK_MLOCKALL_PROPERTY "=" << llkFormat(llkMlockall) << "\n"
+ << LLK_KILLTEST_PROPERTY "=" << llkFormat(llkTestWithKill) << "\n"
+ << KHT_TIMEOUT_PROPERTY "=" << llkFormat(khtTimeout) << "\n"
+ << LLK_TIMEOUT_MS_PROPERTY "=" << llkFormat(llkTimeoutMs) << "\n"
+ << LLK_D_TIMEOUT_MS_PROPERTY "=" << llkFormat(llkStateTimeoutMs[llkStateD]) << "\n"
+ << LLK_Z_TIMEOUT_MS_PROPERTY "=" << llkFormat(llkStateTimeoutMs[llkStateZ]) << "\n"
+ << LLK_CHECK_MS_PROPERTY "=" << llkFormat(llkCheckMs) << "\n"
+ << LLK_BLACKLIST_PROCESS_PROPERTY "=" << llkFormat(llkBlacklistProcess) << "\n"
+ << LLK_BLACKLIST_PARENT_PROPERTY "=" << llkFormat(llkBlacklistParent) << "\n"
+ << LLK_BLACKLIST_UID_PROPERTY "=" << llkFormat(llkBlacklistUid);
+}
+
+void* llkThread(void* obj) {
+ LOG(INFO) << "started";
+
+ std::string name = std::to_string(::gettid());
+ if (!llkSkipName(name)) {
+ llkBlacklistProcess.emplace(name);
+ }
+ name = static_cast<const char*>(obj);
+ prctl(PR_SET_NAME, name.c_str());
+ if (__predict_false(!llkSkipName(name))) {
+ llkBlacklistProcess.insert(name);
+ }
+ // No longer modifying llkBlacklistProcess.
+ llkRunning = true;
+ llkLogConfig();
+ while (llkRunning) {
+ ::usleep(duration_cast<microseconds>(llkCheck(true)).count());
+ }
+ // NOTREACHED
+ LOG(INFO) << "exiting";
+ return nullptr;
+}
+
+} // namespace
+
+milliseconds llkCheck(bool checkRunning) {
+ if (!llkEnable || (checkRunning != llkRunning)) {
+ return milliseconds::max();
+ }
+
+ // Reset internal watchdog, which is a healthy engineering margin of
+ // double the maximum wait or cycle time for the mainloop that calls us.
+ //
+ // This alarm is effectively the live lock detection of llkd, as
+ // we understandably can not monitor ourselves otherwise.
+ ::alarm(duration_cast<seconds>(llkTimeoutMs * 2).count());
+
+ // kernel jiffy precision fastest acquisition
+ static timespec last;
+ timespec now;
+ ::clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
+ auto ms = llkGetTimespecDiffMs(&last, &now);
+ if (ms < llkCycle) {
+ return llkCycle - ms;
+ }
+ last = now;
+
+ LOG(VERBOSE) << "opendir(\"" << procdir << "\")";
+ if (__predict_false(!llkTopDirectory)) {
+ // gid containing AID_READPROC required
+ llkTopDirectory.reset(procdir);
+ if (__predict_false(!llkTopDirectory)) {
+ // Most likely reason we could be here is a resource limit.
+ // Keep our processing down to a minimum, but not so low that
+ // we do not recover in a timely manner should the issue be
+ // transitory.
+ LOG(DEBUG) << "opendir(\"" << procdir << "\") failed";
+ return llkTimeoutMs;
+ }
+ }
+
+ for (auto& it : tids) {
+ it.second.updated = false;
+ }
+
+ auto prevUpdate = llkUpdate;
+ llkUpdate += ms;
+ ms -= llkCycle;
+ auto myPid = ::getpid();
+ auto myTid = ::gettid();
+ for (auto dp = llkTopDirectory.read(); dp != nullptr; dp = llkTopDirectory.read()) {
+ std::string piddir;
+
+ if (!getValidTidDir(dp, &piddir)) {
+ continue;
+ }
+
+ // Get the process tasks
+ std::string taskdir = piddir + "/task/";
+ int pid = -1;
+ LOG(VERBOSE) << "+opendir(\"" << taskdir << "\")";
+ dir taskDirectory(taskdir);
+ if (__predict_false(!taskDirectory)) {
+ LOG(DEBUG) << "+opendir(\"" << taskdir << "\") failed";
+ }
+ for (auto tp = taskDirectory.read(dir::task, dp); tp != nullptr;
+ tp = taskDirectory.read(dir::task)) {
+ if (!getValidTidDir(tp, &piddir)) {
+ continue;
+ }
+
+ // Get the process stat
+ std::string stat = ReadFile(piddir + "/stat");
+ if (stat.size() == 0) {
+ continue;
+ }
+ unsigned tid = -1;
+ char pdir[TASK_COMM_LEN + 1];
+ char state = '?';
+ unsigned ppid = -1;
+ unsigned utime = -1;
+ unsigned stime = -1;
+ int dummy;
+ pdir[0] = '\0';
+ // tid should not change value
+ auto match = ::sscanf(
+ stat.c_str(),
+ "%u (%" ___STRING(
+ TASK_COMM_LEN) "[^)]) %c %u %*d %*d %*d %*d %*d %*d %*d %*d %*d %u %u %d",
+ &tid, pdir, &state, &ppid, &utime, &stime, &dummy);
+ if (pid == -1) {
+ pid = tid;
+ }
+ LOG(VERBOSE) << "match " << match << ' ' << tid << " (" << pdir << ") " << state << ' '
+ << ppid << " ... " << utime << ' ' << stime << ' ' << dummy;
+ if (match != 7) {
+ continue;
+ }
+
+ auto procp = llkTidLookup(tid);
+ if (procp == nullptr) {
+ procp = llkTidAlloc(tid, pid, ppid, pdir, utime + stime, state);
+ } else {
+ // comm can change ...
+ procp->setComm(pdir);
+ procp->updated = true;
+ // pid/ppid/tid wrap?
+ if (((procp->update != prevUpdate) && (procp->update != llkUpdate)) ||
+ (procp->ppid != ppid) || (procp->pid != pid)) {
+ procp->reset();
+ } else if (procp->time != (utime + stime)) { // secondary ABA.
+ // watching utime+stime granularity jiffy
+ procp->state = '?';
+ }
+ procp->update = llkUpdate;
+ procp->pid = pid;
+ procp->ppid = ppid;
+ procp->time = utime + stime;
+ if (procp->state != state) {
+ procp->count = 0ms;
+ procp->killed = !llkTestWithKill;
+ procp->state = state;
+ } else {
+ procp->count += llkCycle;
+ }
+ }
+
+ // Filter checks in intuitive order of CPU cost to evaluate
+ // If tid unique continue, if ppid or pid unique break
+
+ if (pid == myPid) {
+ break;
+ }
+ if (!llkIsMonitorState(state)) {
+ continue;
+ }
+ if ((tid == myTid) || llkSkipPid(tid)) {
+ continue;
+ }
+ if (llkSkipPpid(ppid)) {
+ break;
+ }
+
+ if (llkSkipName(procp->getComm())) {
+ continue;
+ }
+ if (llkSkipName(procp->getCmdline())) {
+ break;
+ }
+
+ auto pprocp = llkTidLookup(ppid);
+ if (pprocp == nullptr) {
+ pprocp = llkTidAlloc(ppid, ppid, 0, "", 0, '?');
+ }
+ if ((pprocp != nullptr) && (llkSkipName(pprocp->getComm(), llkBlacklistParent) ||
+ llkSkipName(pprocp->getCmdline(), llkBlacklistParent))) {
+ break;
+ }
+
+ if ((llkBlacklistUid.size() != 0) && llkSkipUid(procp->getUid())) {
+ continue;
+ }
+
+ // ABA mitigation watching last time schedule activity happened
+ llkCheckSchedUpdate(procp, piddir);
+
+ // Can only fall through to here if registered D or Z state !!!
+ if (procp->count < llkStateTimeoutMs[(state == 'Z') ? llkStateZ : llkStateD]) {
+ LOG(VERBOSE) << state << ' ' << llkFormat(procp->count) << ' ' << ppid << "->"
+ << pid << "->" << tid << ' ' << procp->getComm();
+ continue;
+ }
+
+ // We have to kill it to determine difference between live lock
+ // and persistent state blocked on a resource. Is there something
+ // wrong with a process that has no forward scheduling progress in
+ // Z or D? Yes, generally means improper accounting in the
+ // process, but not always ...
+ //
+ // Whomever we hit with a test kill must accept the Android
+ // Aphorism that everything can be burned to the ground and
+ // must survive.
+ if (procp->killed == false) {
+ procp->killed = true;
+ // confirm: re-read uid before committing to a panic.
+ procp->uid = -1;
+ switch (state) {
+ case 'Z': // kill ppid to free up a Zombie
+ // Killing init will kernel panic without diagnostics
+ // so skip right to controlled kernel panic with
+ // diagnostics.
+ if (ppid == initPid) {
+ break;
+ }
+ LOG(WARNING) << "Z " << llkFormat(procp->count) << ' ' << ppid << "->"
+ << pid << "->" << tid << ' ' << procp->getComm() << " [kill]";
+ if ((llkKillOneProcess(pprocp, procp) >= 0) ||
+ (llkKillOneProcess(ppid, procp) >= 0)) {
+ continue;
+ }
+ break;
+
+ case 'D': // kill tid to free up an uninterruptible D
+ // If ABA is doing its job, we would not need or
+ // want the following. Test kill is a Hail Mary
+ // to make absolutely sure there is no forward
+ // scheduling progress. The cost when ABA is
+ // not working is we kill a process that likes to
+ // stay in 'D' state, instead of panicing the
+ // kernel (worse).
+ LOG(WARNING) << "D " << llkFormat(procp->count) << ' ' << pid << "->" << tid
+ << ' ' << procp->getComm() << " [kill]";
+ if ((llkKillOneProcess(llkTidLookup(pid), procp) >= 0) ||
+ (llkKillOneProcess(pid, 'D', tid) >= 0) ||
+ (llkKillOneProcess(procp, procp) >= 0) ||
+ (llkKillOneProcess(tid, 'D', tid) >= 0)) {
+ continue;
+ }
+ break;
+ }
+ }
+ // We are here because we have confirmed kernel live-lock
+ LOG(ERROR) << state << ' ' << llkFormat(procp->count) << ' ' << ppid << "->" << pid
+ << "->" << tid << ' ' << procp->getComm() << " [panic]";
+ llkPanicKernel(true, tid, (state == 'Z') ? "zombie" : "driver");
+ }
+ LOG(VERBOSE) << "+closedir()";
+ }
+ llkTopDirectory.rewind();
+ LOG(VERBOSE) << "closedir()";
+
+ // garbage collection of old process references
+ for (auto p = tids.begin(); p != tids.end();) {
+ if (!p->second.updated) {
+ IF_ALOG(LOG_VERBOSE, LOG_TAG) {
+ std::string ppidCmdline = llkProcGetName(p->second.ppid, nullptr, nullptr);
+ if (ppidCmdline.size()) {
+ ppidCmdline = "(" + ppidCmdline + ")";
+ }
+ std::string pidCmdline;
+ if (p->second.pid != p->second.tid) {
+ pidCmdline = llkProcGetName(p->second.pid, nullptr, p->second.getCmdline());
+ if (pidCmdline.size()) {
+ pidCmdline = "(" + pidCmdline + ")";
+ }
+ }
+ std::string tidCmdline =
+ llkProcGetName(p->second.tid, p->second.getComm(), p->second.getCmdline());
+ if (tidCmdline.size()) {
+ tidCmdline = "(" + tidCmdline + ")";
+ }
+ LOG(VERBOSE) << "thread " << p->second.ppid << ppidCmdline << "->" << p->second.pid
+ << pidCmdline << "->" << p->second.tid << tidCmdline << " removed";
+ }
+ p = tids.erase(p);
+ } else {
+ ++p;
+ }
+ }
+ if (__predict_false(tids.empty())) {
+ llkTopDirectory.reset();
+ }
+
+ llkCycle = llkCheckMs;
+
+ timespec end;
+ ::clock_gettime(CLOCK_MONOTONIC_COARSE, &end);
+ auto milli = llkGetTimespecDiffMs(&now, &end);
+ LOG((milli > 10s) ? ERROR : (milli > 1s) ? WARNING : VERBOSE) << "sample " << llkFormat(milli);
+
+ // cap to minimum sleep for 1 second since last cycle
+ if (llkCycle < (ms + 1s)) {
+ return 1s;
+ }
+ return llkCycle - ms;
+}
+
+unsigned llkCheckMilliseconds() {
+ return duration_cast<milliseconds>(llkCheck()).count();
+}
+
+bool llkInit(const char* threadname) {
+ llkLowRam = android::base::GetBoolProperty("ro.config.low_ram", false);
+ if (!LLK_ENABLE_DEFAULT && android::base::GetBoolProperty("ro.debuggable", false)) {
+ llkEnable = android::base::GetProperty(LLK_ENABLE_PROPERTY, "eng") == "eng";
+ khtEnable = android::base::GetProperty(KHT_ENABLE_PROPERTY, "eng") == "eng";
+ }
+ llkEnable = android::base::GetBoolProperty(LLK_ENABLE_PROPERTY, llkEnable);
+ if (llkEnable && !llkTopDirectory.reset(procdir)) {
+ // Most likely reason we could be here is llkd was started
+ // incorrectly without the readproc permissions. Keep our
+ // processing down to a minimum.
+ llkEnable = false;
+ }
+ khtEnable = android::base::GetBoolProperty(KHT_ENABLE_PROPERTY, khtEnable);
+ llkMlockall = android::base::GetBoolProperty(LLK_MLOCKALL_PROPERTY, llkMlockall);
+ llkTestWithKill = android::base::GetBoolProperty(LLK_KILLTEST_PROPERTY, llkTestWithKill);
+ // if LLK_TIMOUT_MS_PROPERTY was not set, we will use a set
+ // KHT_TIMEOUT_PROPERTY as co-operative guidance for the default value.
+ khtTimeout = GetUintProperty(KHT_TIMEOUT_PROPERTY, khtTimeout);
+ if (khtTimeout == 0s) {
+ khtTimeout = duration_cast<seconds>(llkTimeoutMs * (1 + LLK_CHECKS_PER_TIMEOUT_DEFAULT) /
+ LLK_CHECKS_PER_TIMEOUT_DEFAULT);
+ }
+ llkTimeoutMs =
+ khtTimeout * LLK_CHECKS_PER_TIMEOUT_DEFAULT / (1 + LLK_CHECKS_PER_TIMEOUT_DEFAULT);
+ llkTimeoutMs = GetUintProperty(LLK_TIMEOUT_MS_PROPERTY, llkTimeoutMs);
+ llkValidate(); // validate llkTimeoutMs, llkCheckMs and llkCycle
+ llkStateTimeoutMs[llkStateD] = GetUintProperty(LLK_D_TIMEOUT_MS_PROPERTY, llkTimeoutMs);
+ llkStateTimeoutMs[llkStateZ] = GetUintProperty(LLK_Z_TIMEOUT_MS_PROPERTY, llkTimeoutMs);
+ llkCheckMs = GetUintProperty(LLK_CHECK_MS_PROPERTY, llkCheckMs);
+ llkValidate(); // validate all (effectively minus llkTimeoutMs)
+ std::string defaultBlacklistProcess(
+ std::to_string(kernelPid) + "," + std::to_string(initPid) + "," +
+ std::to_string(kthreaddPid) + "," + std::to_string(::getpid()) + "," +
+ std::to_string(::gettid()) + "," LLK_BLACKLIST_PROCESS_DEFAULT);
+ if (threadname) {
+ defaultBlacklistProcess += ","s + threadname;
+ }
+ for (int cpu = 1; cpu < get_nprocs_conf(); ++cpu) {
+ defaultBlacklistProcess += ",[watchdog/" + std::to_string(cpu) + "]";
+ }
+ defaultBlacklistProcess =
+ android::base::GetProperty(LLK_BLACKLIST_PROCESS_PROPERTY, defaultBlacklistProcess);
+ llkBlacklistProcess = llkSplit(defaultBlacklistProcess);
+ if (!llkSkipName("[khungtaskd]")) { // ALWAYS ignore as special
+ llkBlacklistProcess.emplace("[khungtaskd]");
+ }
+ llkBlacklistParent = llkSplit(android::base::GetProperty(
+ LLK_BLACKLIST_PARENT_PROPERTY, std::to_string(kernelPid) + "," + std::to_string(kthreaddPid) +
+ "," LLK_BLACKLIST_PARENT_DEFAULT));
+ llkBlacklistUid =
+ llkSplit(android::base::GetProperty(LLK_BLACKLIST_UID_PROPERTY, LLK_BLACKLIST_UID_DEFAULT));
+
+ // internal watchdog
+ ::signal(SIGALRM, llkAlarmHandler);
+
+ // kernel hung task configuration? Otherwise leave it as-is
+ if (khtEnable) {
+ // EUID must be AID_ROOT to write to /proc/sys/kernel/ nodes, there
+ // are no capability overrides. For security reasons we do not want
+ // to run as AID_ROOT. We may not be able to write them successfully,
+ // we will try, but the least we can do is read the values back to
+ // confirm expectations and report whether configured or not.
+ auto configured = llkWriteStringToFileConfirm(std::to_string(khtTimeout.count()),
+ "/proc/sys/kernel/hung_task_timeout_secs");
+ if (configured) {
+ llkWriteStringToFile("65535", "/proc/sys/kernel/hung_task_warnings");
+ llkWriteStringToFile("65535", "/proc/sys/kernel/hung_task_check_count");
+ configured = llkWriteStringToFileConfirm("1", "/proc/sys/kernel/hung_task_panic");
+ }
+ if (configured) {
+ LOG(INFO) << "[khungtaskd] configured";
+ } else {
+ LOG(WARNING) << "[khungtaskd] not configurable";
+ }
+ }
+
+ bool logConfig = true;
+ if (llkEnable) {
+ if (llkMlockall &&
+ // MCL_ONFAULT pins pages as they fault instead of loading
+ // everything immediately all at once. (Which would be bad,
+ // because as of this writing, we have a lot of mapped pages we
+ // never use.) Old kernels will see MCL_ONFAULT and fail with
+ // EINVAL; we ignore this failure.
+ //
+ // N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT
+ // pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault
+ // in pages.
+
+ // CAP_IPC_LOCK required
+ mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) {
+ PLOG(WARNING) << "mlockall failed ";
+ }
+
+ if (threadname) {
+ pthread_attr_t attr;
+
+ if (!pthread_attr_init(&attr)) {
+ sched_param param;
+
+ memset(¶m, 0, sizeof(param));
+ pthread_attr_setschedparam(&attr, ¶m);
+ pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
+ if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
+ pthread_t thread;
+ if (!pthread_create(&thread, &attr, llkThread, const_cast<char*>(threadname))) {
+ // wait a second for thread to start
+ for (auto retry = 50; retry && !llkRunning; --retry) {
+ ::usleep(20000);
+ }
+ logConfig = !llkRunning; // printed in llkd context?
+ } else {
+ LOG(ERROR) << "failed to spawn llkd thread";
+ }
+ } else {
+ LOG(ERROR) << "failed to detach llkd thread";
+ }
+ pthread_attr_destroy(&attr);
+ } else {
+ LOG(ERROR) << "failed to allocate attibutes for llkd thread";
+ }
+ }
+ } else {
+ LOG(DEBUG) << "[khungtaskd] left unconfigured";
+ }
+ if (logConfig) {
+ llkLogConfig();
+ }
+
+ return llkEnable;
+}
diff --git a/llkd/llkd.cpp b/llkd/llkd.cpp
new file mode 100644
index 0000000..f10253d
--- /dev/null
+++ b/llkd/llkd.cpp
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2018 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 "llkd.h"
+
+#include <sched.h>
+#include <unistd.h>
+
+#include <chrono>
+
+#include <android-base/logging.h>
+
+using namespace std::chrono;
+
+int main(int, char**) {
+ LOG(INFO) << "started";
+
+ bool enabled = llkInit();
+
+ // Would like this policy to be automatic as part of libllkd,
+ // but that would be presumptuous and bad side-effect.
+ struct sched_param param;
+ memset(¶m, 0, sizeof(param));
+ sched_setscheduler(0, SCHED_BATCH, ¶m);
+
+ while (true) {
+ if (enabled) {
+ ::usleep(duration_cast<microseconds>(llkCheck()).count());
+ } else {
+ ::pause();
+ }
+ }
+ // NOTREACHED
+
+ LOG(INFO) << "exiting";
+ return 0;
+}
diff --git a/llkd/llkd.rc b/llkd/llkd.rc
new file mode 100644
index 0000000..e538cdb
--- /dev/null
+++ b/llkd/llkd.rc
@@ -0,0 +1,49 @@
+# eng default for ro.llk.enable and ro.khungtask.enable
+on property:ro.debuggable=*
+ setprop llk.enable ${ro.llk.enable:-0}
+ setprop khungtask.enable ${ro.khungtask.enable:-0}
+
+on property:ro.debuggable=1
+ setprop llk.enable ${ro.llk.enable:-1}
+ setprop khungtask.enable ${ro.khungtask.enable:-1}
+
+on property:ro.llk.enable=eng
+ setprop llk.enable ${ro.debuggable:-0}
+
+on property:ro.khungtask.enable=eng
+ setprop khungtask.enable ${ro.debuggable:-0}
+
+on property:llk.enable=1
+ setprop llk.enable true
+
+on property:llk.enable=0
+ setprop llk.enable false
+
+on property:khungtask.enable=1
+ setprop khungtask.enable true
+
+on property:khungtask.enable=0
+ setprop khungtask.enable false
+
+# Configure [khungtaskd]
+on property:khungtask.enable=true
+ write /proc/sys/kernel/hung_task_timeout_secs ${ro.khungtask.timeout:-720}
+ write /proc/sys/kernel/hung_task_warnings 65535
+ write /proc/sys/kernel/hung_task_check_count 65535
+ write /proc/sys/kernel/hung_task_panic 1
+
+on property:khungtask.enable=false
+ write /proc/sys/kernel/hung_task_panic 0
+
+on property:llk.enable=true
+ start llkd
+
+service llkd /system/bin/llkd
+ class late_start
+ disabled
+ user llkd
+ group llkd readproc
+ capabilities KILL IPC_LOCK
+ file /dev/kmsg w
+ file /proc/sysrq-trigger w
+ writepid /dev/cpuset/system-background/tasks
diff --git a/llkd/tests/Android.bp b/llkd/tests/Android.bp
new file mode 100644
index 0000000..6dd5938
--- /dev/null
+++ b/llkd/tests/Android.bp
@@ -0,0 +1,41 @@
+// Copyright (C) 2018 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.
+
+cc_test {
+ name: "llkd_unit_test",
+
+ shared_libs: [
+ "libbase",
+ "liblog",
+ ],
+ header_libs: [
+ "llkd_headers",
+ ],
+
+ target: {
+ android: {
+ srcs: [
+ "llkd_test.cpp",
+ ],
+ },
+ },
+
+ cflags: [
+ "-Wall",
+ "-Wextra",
+ "-Werror",
+ ],
+
+ compile_multilib: "first",
+}
diff --git a/llkd/tests/llkd_test.cpp b/llkd/tests/llkd_test.cpp
new file mode 100644
index 0000000..3a15ff1
--- /dev/null
+++ b/llkd/tests/llkd_test.cpp
@@ -0,0 +1,287 @@
+/*
+ * Copyright (C) 2018 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 <signal.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include <chrono>
+#include <iostream>
+#include <string>
+
+#include <android-base/properties.h>
+#include <gtest/gtest.h>
+#include <log/log_time.h> // for MS_PER_SEC and US_PER_SEC
+
+#include "llkd.h"
+
+using namespace std::chrono;
+using namespace std::chrono_literals;
+
+namespace {
+
+milliseconds GetUintProperty(const std::string& key, milliseconds def) {
+ return milliseconds(android::base::GetUintProperty(key, static_cast<uint64_t>(def.count()),
+ static_cast<uint64_t>(def.max().count())));
+}
+
+seconds GetUintProperty(const std::string& key, seconds def) {
+ return seconds(android::base::GetUintProperty(key, static_cast<uint64_t>(def.count()),
+ static_cast<uint64_t>(def.max().count())));
+}
+
+// GTEST_LOG_(WARNING) output is fugly, this has much less noise
+// ToDo: look into fixing googletest to produce output that matches style of
+// all the other status messages, and can switch off __line__ and
+// __function__ noise
+#define GTEST_LOG_WARNING std::cerr << "[ WARNING ] "
+#define GTEST_LOG_INFO std::cerr << "[ INFO ] "
+
+// Properties is _not_ a high performance ABI!
+void rest() {
+ usleep(200000);
+}
+
+void execute(const char* command) {
+ if (getuid() || system(command)) {
+ system((std::string("su root ") + command).c_str());
+ }
+}
+
+seconds llkdSleepPeriod(char state) {
+ auto default_eng = android::base::GetProperty(LLK_ENABLE_PROPERTY, "eng") == "eng";
+ auto default_enable = LLK_ENABLE_DEFAULT;
+ if (!LLK_ENABLE_DEFAULT && default_eng &&
+ android::base::GetBoolProperty("ro.debuggable", false)) {
+ default_enable = true;
+ }
+ default_enable = android::base::GetBoolProperty(LLK_ENABLE_PROPERTY, default_enable);
+ if (default_eng) {
+ GTEST_LOG_INFO << LLK_ENABLE_PROPERTY " defaults to \"eng\" thus "
+ << (default_enable ? "true" : "false") << "\n";
+ }
+ // Hail Mary hope is unconfigured.
+ if ((GetUintProperty(LLK_TIMEOUT_MS_PROPERTY, LLK_TIMEOUT_MS_DEFAULT) !=
+ duration_cast<milliseconds>(120s)) ||
+ (GetUintProperty(LLK_CHECK_MS_PROPERTY,
+ LLK_TIMEOUT_MS_DEFAULT / LLK_CHECKS_PER_TIMEOUT_DEFAULT) !=
+ duration_cast<milliseconds>(10s))) {
+ execute("stop llkd");
+ rest();
+ std::string setprop("setprop ");
+ execute((setprop + LLK_ENABLE_WRITEABLE_PROPERTY + " false").c_str());
+ rest();
+ execute((setprop + LLK_TIMEOUT_MS_PROPERTY + " 120000").c_str());
+ rest();
+ execute((setprop + KHT_TIMEOUT_PROPERTY + " 130").c_str());
+ rest();
+ execute((setprop + LLK_CHECK_MS_PROPERTY + " 10000").c_str());
+ rest();
+ execute((setprop + LLK_ENABLE_PROPERTY + " true").c_str());
+ rest();
+ execute((setprop + LLK_ENABLE_WRITEABLE_PROPERTY + " true").c_str());
+ rest();
+ }
+ default_enable = LLK_ENABLE_DEFAULT;
+ if (!LLK_ENABLE_DEFAULT && (android::base::GetProperty(LLK_ENABLE_PROPERTY, "eng") == "eng") &&
+ android::base::GetBoolProperty("ro.debuggable", false)) {
+ default_enable = true;
+ }
+ default_enable = android::base::GetBoolProperty(LLK_ENABLE_PROPERTY, default_enable);
+ if (default_enable) {
+ execute("start llkd");
+ rest();
+ GTEST_LOG_INFO << "llkd enabled\n";
+ } else {
+ GTEST_LOG_WARNING << "llkd disabled\n";
+ }
+
+ /* KISS follows llk_init() */
+ milliseconds llkTimeoutMs = LLK_TIMEOUT_MS_DEFAULT;
+ seconds khtTimeout = duration_cast<seconds>(
+ llkTimeoutMs * (1 + LLK_CHECKS_PER_TIMEOUT_DEFAULT) / LLK_CHECKS_PER_TIMEOUT_DEFAULT);
+ khtTimeout = GetUintProperty(KHT_TIMEOUT_PROPERTY, khtTimeout);
+ llkTimeoutMs =
+ khtTimeout * LLK_CHECKS_PER_TIMEOUT_DEFAULT / (1 + LLK_CHECKS_PER_TIMEOUT_DEFAULT);
+ llkTimeoutMs = GetUintProperty(LLK_TIMEOUT_MS_PROPERTY, llkTimeoutMs);
+ if (llkTimeoutMs < LLK_TIMEOUT_MS_MINIMUM) {
+ llkTimeoutMs = LLK_TIMEOUT_MS_MINIMUM;
+ }
+ milliseconds llkCheckMs = llkTimeoutMs / LLK_CHECKS_PER_TIMEOUT_DEFAULT;
+ auto timeout = GetUintProperty(
+ (state == 'Z') ? LLK_Z_TIMEOUT_MS_PROPERTY : LLK_D_TIMEOUT_MS_PROPERTY, llkTimeoutMs);
+ if (timeout < LLK_TIMEOUT_MS_MINIMUM) {
+ timeout = LLK_TIMEOUT_MS_MINIMUM;
+ }
+
+ if (llkCheckMs > timeout) {
+ llkCheckMs = timeout;
+ }
+ llkCheckMs = GetUintProperty(LLK_CHECK_MS_PROPERTY, llkCheckMs);
+ timeout += llkCheckMs;
+ auto sec = duration_cast<seconds>(timeout);
+ if (sec == 0s) {
+ ++sec;
+ } else if (sec > 59s) {
+ GTEST_LOG_WARNING << "llkd is configured for about " << duration_cast<minutes>(sec).count()
+ << " minutes to react\n";
+ }
+
+ // 33% margin for the test to naturally timeout waiting for llkd to respond
+ return (sec * 4 + 2s) / 3;
+}
+
+inline void waitForPid(pid_t child_pid) {
+ int wstatus;
+ ASSERT_LE(0, waitpid(child_pid, &wstatus, 0));
+ EXPECT_FALSE(WIFEXITED(wstatus)) << "[ INFO ] exit=" << WEXITSTATUS(wstatus);
+ ASSERT_TRUE(WIFSIGNALED(wstatus));
+ ASSERT_EQ(WTERMSIG(wstatus), SIGKILL);
+}
+
+bool checkKill(const char* reason) {
+ if (android::base::GetBoolProperty(LLK_KILLTEST_PROPERTY, LLK_KILLTEST_DEFAULT)) {
+ return false;
+ }
+ auto bootreason = android::base::GetProperty("sys.boot.reason", "nothing");
+ if (bootreason == reason) {
+ GTEST_LOG_INFO << "Expected test result confirmed " << reason << "\n";
+ return true;
+ }
+ GTEST_LOG_WARNING << "Expected test result is " << reason << "\n";
+
+ // apct adjustment if needed (set LLK_KILLTEST_PROPERTY to "off" to allow test)
+ //
+ // if (android::base::GetProperty(LLK_KILLTEST_PROPERTY, "") == "false") {
+ // GTEST_LOG_WARNING << "Bypassing test\n";
+ // return true;
+ // }
+
+ return false;
+}
+
+} // namespace
+
+// The tests that use this helper are to simulate processes stuck in 'D'
+// state that are experiencing forward scheduled progress. As such the
+// expectation is that llkd will _not_ perform any mitigations. The sleepfor
+// argument helps us set the amount of forward scheduler progress.
+static void llkd_driver_ABA(const microseconds sleepfor) {
+ const auto period = llkdSleepPeriod('D');
+ if (period <= sleepfor) {
+ GTEST_LOG_WARNING << "llkd configuration too short for "
+ << duration_cast<milliseconds>(sleepfor).count() << "ms work cycle\n";
+ return;
+ }
+
+ auto child_pid = fork();
+ ASSERT_LE(0, child_pid);
+ int wstatus;
+ if (!child_pid) {
+ auto ratio = period / sleepfor;
+ ASSERT_LT(0, ratio);
+ // vfork() parent is uninterruptable D state waiting for child to exec()
+ while (--ratio > 0) {
+ auto driver_pid = vfork();
+ ASSERT_LE(0, driver_pid);
+ if (driver_pid) { // parent
+ waitpid(driver_pid, &wstatus, 0);
+ if (!WIFEXITED(wstatus)) {
+ exit(42);
+ }
+ if (WEXITSTATUS(wstatus) != 42) {
+ exit(42);
+ }
+ } else {
+ usleep(sleepfor.count());
+ exit(42);
+ }
+ }
+ exit(0);
+ }
+ ASSERT_LE(0, waitpid(child_pid, &wstatus, 0));
+ EXPECT_TRUE(WIFEXITED(wstatus));
+ if (WIFEXITED(wstatus)) {
+ EXPECT_EQ(0, WEXITSTATUS(wstatus));
+ }
+ ASSERT_FALSE(WIFSIGNALED(wstatus)) << "[ INFO ] signo=" << WTERMSIG(wstatus);
+}
+
+TEST(llkd, driver_ABA_fast) {
+ llkd_driver_ABA(5ms);
+}
+
+TEST(llkd, driver_ABA_slow) {
+ llkd_driver_ABA(1s);
+}
+
+TEST(llkd, driver_ABA_glacial) {
+ llkd_driver_ABA(1min);
+}
+
+// Following tests must be last in this file to capture possible errant
+// kernel_panic mitigation failure.
+
+// The following tests simulate processes stick in 'Z' or 'D' state with
+// no forward scheduling progress, but interruptible. As such the expectation
+// is that llkd will perform kill mitigation and not progress to kernel_panic.
+
+TEST(llkd, zombie) {
+ if (checkKill("kernel_panic,sysrq,livelock,zombie")) {
+ return;
+ }
+
+ const auto period = llkdSleepPeriod('Z');
+
+ /* Create a Persistent Zombie Process */
+ pid_t child_pid = fork();
+ ASSERT_LE(0, child_pid);
+ if (!child_pid) {
+ auto zombie_pid = fork();
+ ASSERT_LE(0, zombie_pid);
+ if (!zombie_pid) {
+ sleep(1);
+ exit(0);
+ }
+ sleep(period.count());
+ exit(42);
+ }
+
+ waitForPid(child_pid);
+}
+
+TEST(llkd, driver) {
+ if (checkKill("kernel_panic,sysrq,livelock,driver")) {
+ return;
+ }
+
+ const auto period = llkdSleepPeriod('D');
+
+ /* Create a Persistent Device Process */
+ auto child_pid = fork();
+ ASSERT_LE(0, child_pid);
+ if (!child_pid) {
+ // vfork() parent is uninterruptable D state waiting for child to exec()
+ auto driver_pid = vfork();
+ ASSERT_LE(0, driver_pid);
+ sleep(period.count());
+ exit(driver_pid ? 42 : 0);
+ }
+
+ waitForPid(child_pid);
+}
diff --git a/lmkd/Android.bp b/lmkd/Android.bp
index 76d308a..903d0e2 100644
--- a/lmkd/Android.bp
+++ b/lmkd/Android.bp
@@ -3,18 +3,49 @@
srcs: ["lmkd.c"],
shared_libs: [
- "liblog",
"libcutils",
+ "liblog",
],
- cflags: ["-Werror"],
-
+ static_libs: [
+ "libstatslogc",
+ "libstatssocket",
+ ],
+ local_include_dirs: ["include"],
+ cflags: ["-Werror", "-DLMKD_TRACE_KILLS"],
init_rc: ["lmkd.rc"],
-
product_variables: {
- debuggable: {
+ use_lmkd_stats_log: {
cflags: [
- "-DLMKD_TRACE_KILLS"
+ "-DLMKD_LOG_STATS"
],
},
},
+ logtags: ["event.logtags"],
+}
+
+cc_library_static {
+ name: "libstatslogc",
+ srcs: ["statslog.c"],
+ cflags: [
+ "-Wall",
+ "-Werror",
+ ],
+ shared_libs: [
+ "liblog",
+ ],
+ static_libs: ["libstatssocket",],
+}
+
+cc_library_static {
+ name: "liblmkd_utils",
+ srcs: ["liblmkd_utils.c"],
+ shared_libs: [
+ "libcutils",
+ ],
+ export_include_dirs: ["include"],
+ cppflags: [
+ "-g",
+ "-Wall",
+ "-Werror",
+ ]
}
diff --git a/lmkd/README.md b/lmkd/README.md
new file mode 100644
index 0000000..656a6ea
--- /dev/null
+++ b/lmkd/README.md
@@ -0,0 +1,65 @@
+Android Low Memory Killer Daemon
+================================
+
+
+Introduction
+------------
+
+Android Low Memory Killer Daemon (lmkd) is a process monitoring memory
+state of a running Android system and reacting to high memory pressure
+by killing the least essential process(es) to keep system performing
+at acceptable levels.
+
+
+Background
+----------
+
+Historically on Android systems memory monitoring and killing of
+non-essential processes was handled by a kernel lowmemorykiller driver.
+Since Linux Kernel 4.12 the lowmemorykiller driver has been removed and
+instead userspace lmkd daemon performs these tasks.
+
+
+Android Properties
+------------------
+
+lmkd can be configured on a particular system using the following Android
+properties:
+
+ ro.config.low_ram: choose between low-memory vs high-performance
+ device. Default = false.
+
+ ro.lmk.use_minfree_levels: use free memory and file cache thresholds for
+ making decisions when to kill. This mode works
+ the same way kernel lowmemorykiller driver used
+ to work. Default = false
+
+ ro.lmk.low: min oom_adj score for processes eligible to be
+ killed at low vmpressure level. Default = 1001
+ (disabled)
+
+ ro.lmk.medium: min oom_adj score for processes eligible to be
+ killed at medium vmpressure level. Default = 800
+ (non-essential processes)
+
+ ro.lmk.critical: min oom_adj score for processes eligible to be
+ killed at critical vmpressure level. Default = 0
+ (all processes)
+
+ ro.lmk.critical_upgrade: enables upgrade to critical level. Default = false
+
+ ro.lmk.upgrade_pressure: max mem_pressure at which level will be upgraded
+ because system is swapping too much. Default = 100
+ (disabled)
+
+ ro.lmk.downgrade_pressure: min mem_pressure at which vmpressure event will
+ be ignored because enough free memory is still
+ available. Default = 100 (disabled)
+
+ ro.lmk.kill_heaviest_task: kill heaviest eligible task (best decision) vs.
+ any eligible task (fast decision). Default = false
+
+ ro.lmk.kill_timeout_ms: duration in ms after a kill when no additional
+ kill will be done, Default = 0 (disabled)
+
+ ro.lmk.debug: enable lmkd debug logs, Default = false
diff --git a/lmkd/event.logtags b/lmkd/event.logtags
new file mode 100644
index 0000000..7c2cd18
--- /dev/null
+++ b/lmkd/event.logtags
@@ -0,0 +1,38 @@
+# The entries in this file map a sparse set of log tag numbers to tag names.
+# This is installed on the device, in /system/etc, and parsed by logcat.
+#
+# Tag numbers are decimal integers, from 0 to 2^31. (Let's leave the
+# negative values alone for now.)
+#
+# Tag names are one or more ASCII letters and numbers or underscores, i.e.
+# "[A-Z][a-z][0-9]_". Do not include spaces or punctuation (the former
+# impacts log readability, the latter makes regex searches more annoying).
+#
+# Tag numbers and names are separated by whitespace. Blank lines and lines
+# starting with '#' are ignored.
+#
+# Optionally, after the tag names can be put a description for the value(s)
+# of the tag. Description are in the format
+# (<name>|data type[|data unit])
+# Multiple values are separated by commas.
+#
+# The data type is a number from the following values:
+# 1: int
+# 2: long
+# 3: string
+# 4: list
+#
+# The data unit is a number taken from the following list:
+# 1: Number of objects
+# 2: Number of bytes
+# 3: Number of milliseconds
+# 4: Number of allocations
+# 5: Id
+# 6: Percent
+# s: Number of seconds (monotonic time)
+# Default value for data of type int/long is 2 (bytes).
+#
+# TODO: generate ".java" and ".h" files with integer constants from this file.
+
+# for meminfo logs
+10195355 meminfo (MemFree|1),(Cached|1),(SwapCached|1),(Buffers|1),(Shmem|1),(Unevictable|1),(SwapFree|1),(ActiveAnon|1),(InactiveAnon|1),(ActiveFile|1),(InactiveFile|1),(SReclaimable|1),(SUnreclaim|1),(KernelStack|1),(PageTables|1),(ION_heap|1),(ION_heap_pool|1),(CmaFree|1)
diff --git a/lmkd/include/liblmkd_utils.h b/lmkd/include/liblmkd_utils.h
new file mode 100644
index 0000000..72e3f4a
--- /dev/null
+++ b/lmkd/include/liblmkd_utils.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2018 Google, Inc
+ *
+ * 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.
+ */
+
+#ifndef _LIBLMKD_UTILS_H_
+#define _LIBLMKD_UTILS_H_
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <lmkd.h>
+
+__BEGIN_DECLS
+
+/*
+ * Connects to lmkd process and returns socket handle.
+ * On success returns socket handle.
+ * On error, -1 is returned, and errno is set appropriately.
+ */
+int lmkd_connect();
+
+/*
+ * Registers a process with lmkd and sets its oomadj score.
+ * On success returns 0.
+ * On error, -1 is returned.
+ * In the case of error errno is set appropriately.
+ */
+int lmkd_register_proc(int sock, struct lmk_procprio *params);
+
+/*
+ * Creates memcg directory for given process.
+ * On success returns 0.
+ * -1 is returned if path creation failed.
+ * -2 is returned if tasks file open operation failed.
+ * -3 is returned if tasks file write operation failed.
+ * In the case of error errno is set appropriately.
+ */
+int create_memcg(uid_t uid, pid_t pid);
+
+__END_DECLS
+
+#endif /* _LIBLMKD_UTILS_H_ */
diff --git a/lmkd/include/lmkd.h b/lmkd/include/lmkd.h
new file mode 100644
index 0000000..fe6364d
--- /dev/null
+++ b/lmkd/include/lmkd.h
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2018 Google, Inc
+ *
+ * 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.
+ */
+
+#ifndef _LMKD_H_
+#define _LMKD_H_
+
+#include <arpa/inet.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+__BEGIN_DECLS
+
+/*
+ * Supported LMKD commands
+ */
+enum lmk_cmd {
+ LMK_TARGET = 0, /* Associate minfree with oom_adj_score */
+ LMK_PROCPRIO, /* Register a process and set its oom_adj_score */
+ LMK_PROCREMOVE, /* Unregister a process */
+};
+
+/*
+ * Max number of targets in LMK_TARGET command.
+ */
+#define MAX_TARGETS 6
+
+/*
+ * Max packet length in bytes.
+ * Longest packet is LMK_TARGET followed by MAX_TARGETS
+ * of minfree and oom_adj_score values
+ */
+#define CTRL_PACKET_MAX_SIZE (sizeof(int) * (MAX_TARGETS * 2 + 1))
+
+/* LMKD packet - first int is lmk_cmd followed by payload */
+typedef int LMKD_CTRL_PACKET[CTRL_PACKET_MAX_SIZE / sizeof(int)];
+
+/* Get LMKD packet command */
+inline enum lmk_cmd lmkd_pack_get_cmd(LMKD_CTRL_PACKET pack) {
+ return (enum lmk_cmd)ntohl(pack[0]);
+}
+
+/* LMK_TARGET packet payload */
+struct lmk_target {
+ int minfree;
+ int oom_adj_score;
+};
+
+/*
+ * For LMK_TARGET packet get target_idx-th payload.
+ * Warning: no checks performed, caller should ensure valid parameters.
+ */
+inline void lmkd_pack_get_target(LMKD_CTRL_PACKET packet,
+ int target_idx, struct lmk_target *target) {
+ target->minfree = ntohl(packet[target_idx * 2 + 1]);
+ target->oom_adj_score = ntohl(packet[target_idx * 2 + 2]);
+}
+
+/*
+ * Prepare LMK_TARGET packet and return packet size in bytes.
+ * Warning: no checks performed, caller should ensure valid parameters.
+ */
+inline size_t lmkd_pack_set_target(LMKD_CTRL_PACKET packet,
+ struct lmk_target *targets,
+ size_t target_cnt) {
+ int idx = 0;
+ packet[idx++] = htonl(LMK_TARGET);
+ while (target_cnt) {
+ packet[idx++] = htonl(targets->minfree);
+ packet[idx++] = htonl(targets->oom_adj_score);
+ targets++;
+ target_cnt--;
+ }
+ return idx * sizeof(int);
+}
+
+/* LMK_PROCPRIO packet payload */
+struct lmk_procprio {
+ pid_t pid;
+ uid_t uid;
+ int oomadj;
+};
+
+/*
+ * For LMK_PROCPRIO packet get its payload.
+ * Warning: no checks performed, caller should ensure valid parameters.
+ */
+inline void lmkd_pack_get_procprio(LMKD_CTRL_PACKET packet,
+ struct lmk_procprio *params) {
+ params->pid = (pid_t)ntohl(packet[1]);
+ params->uid = (uid_t)ntohl(packet[2]);
+ params->oomadj = ntohl(packet[3]);
+}
+
+/*
+ * Prepare LMK_PROCPRIO packet and return packet size in bytes.
+ * Warning: no checks performed, caller should ensure valid parameters.
+ */
+inline size_t lmkd_pack_set_procprio(LMKD_CTRL_PACKET packet,
+ struct lmk_procprio *params) {
+ packet[0] = htonl(LMK_PROCPRIO);
+ packet[1] = htonl(params->pid);
+ packet[2] = htonl(params->uid);
+ packet[3] = htonl(params->oomadj);
+ return 4 * sizeof(int);
+}
+
+/* LMK_PROCREMOVE packet payload */
+struct lmk_procremove {
+ pid_t pid;
+};
+
+/*
+ * For LMK_PROCREMOVE packet get its payload.
+ * Warning: no checks performed, caller should ensure valid parameters.
+ */
+inline void lmkd_pack_get_procremove(LMKD_CTRL_PACKET packet,
+ struct lmk_procremove *params) {
+ params->pid = (pid_t)ntohl(packet[1]);
+}
+
+/*
+ * Prepare LMK_PROCREMOVE packet and return packet size in bytes.
+ * Warning: no checks performed, caller should ensure valid parameters.
+ */
+inline size_t lmkd_pack_set_procremove(LMKD_CTRL_PACKET packet,
+ struct lmk_procprio *params) {
+ packet[0] = htonl(LMK_PROCREMOVE);
+ packet[1] = htonl(params->pid);
+ return 2 * sizeof(int);
+}
+
+__END_DECLS
+
+#endif /* _LMKD_H_ */
diff --git a/lmkd/liblmkd_utils.c b/lmkd/liblmkd_utils.c
new file mode 100644
index 0000000..fa3b7a9
--- /dev/null
+++ b/lmkd/liblmkd_utils.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2018 Google, Inc
+ *
+ * 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 <fcntl.h>
+#include <sys/cdefs.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <liblmkd_utils.h>
+#include <cutils/sockets.h>
+
+int lmkd_connect() {
+ return socket_local_client("lmkd",
+ ANDROID_SOCKET_NAMESPACE_RESERVED,
+ SOCK_SEQPACKET);
+}
+
+int lmkd_register_proc(int sock, struct lmk_procprio *params) {
+ LMKD_CTRL_PACKET packet;
+ size_t size;
+ int ret;
+
+ size = lmkd_pack_set_procprio(packet, params);
+ ret = TEMP_FAILURE_RETRY(write(sock, packet, size));
+
+ return (ret < 0) ? -1 : 0;
+}
+
+int create_memcg(uid_t uid, pid_t pid) {
+ char buf[256];
+ int tasks_file;
+ int written;
+
+ snprintf(buf, sizeof(buf), "/dev/memcg/apps/uid_%u", uid);
+ if (mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0 &&
+ errno != EEXIST) {
+ return -1;
+ }
+
+ snprintf(buf, sizeof(buf), "/dev/memcg/apps/uid_%u/pid_%u", uid, pid);
+ if (mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0 &&
+ errno != EEXIST) {
+ return -1;
+ }
+
+ snprintf(buf, sizeof(buf), "/dev/memcg/apps/uid_%u/pid_%u/tasks", uid, pid);
+ tasks_file = open(buf, O_WRONLY);
+ if (tasks_file < 0) {
+ return -2;
+ }
+ written = snprintf(buf, sizeof(buf), "%u", pid);
+ if (__predict_false(written >= (int)sizeof(buf))) {
+ written = sizeof(buf) - 1;
+ }
+ written = TEMP_FAILURE_RETRY(write(tasks_file, buf, written));
+ close(tasks_file);
+
+ return (written < 0) ? -3 : 0;
+}
+
diff --git a/lmkd/lmkd.c b/lmkd/lmkd.c
index 338e5fa..02534f2 100644
--- a/lmkd/lmkd.c
+++ b/lmkd/lmkd.c
@@ -16,11 +16,12 @@
#define LOG_TAG "lowmemorykiller"
-#include <arpa/inet.h>
#include <errno.h>
#include <inttypes.h>
+#include <pwd.h>
#include <sched.h>
#include <signal.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/cdefs.h>
@@ -28,13 +29,19 @@
#include <sys/eventfd.h>
#include <sys/mman.h>
#include <sys/socket.h>
-#include <sys/types.h>
#include <sys/sysinfo.h>
+#include <sys/types.h>
#include <unistd.h>
#include <cutils/properties.h>
#include <cutils/sockets.h>
+#include <lmkd.h>
#include <log/log.h>
+#include <log/log_event_list.h>
+
+#ifdef LMKD_LOG_STATS
+#include "statslog.h"
+#endif
/*
* Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces
@@ -50,8 +57,8 @@
#else /* LMKD_TRACE_KILLS */
-#define TRACE_KILL_START(pid)
-#define TRACE_KILL_END()
+#define TRACE_KILL_START(pid) ((void)(pid))
+#define TRACE_KILL_END() ((void)0)
#endif /* LMKD_TRACE_KILLS */
@@ -63,29 +70,29 @@
#define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes"
#define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes"
#define ZONEINFO_PATH "/proc/zoneinfo"
+#define MEMINFO_PATH "/proc/meminfo"
#define LINE_MAX 128
+/* Android Logger event logtags (see event.logtags) */
+#define MEMINFO_LOG_TAG 10195355
+
+/* gid containing AID_SYSTEM required */
#define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree"
#define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
#define EIGHT_MEGA (1 << 23)
-enum lmk_cmd {
- LMK_TARGET,
- LMK_PROCPRIO,
- LMK_PROCREMOVE,
-};
+/* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */
+#define SYSTEM_ADJ (-900)
-#define MAX_TARGETS 6
-/*
- * longest is LMK_TARGET followed by MAX_TARGETS each minfree and minkillprio
- * values
- */
-#define CTRL_PACKET_MAX (sizeof(int) * (MAX_TARGETS * 2 + 1))
+#define STRINGIFY(x) STRINGIFY_INTERNAL(x)
+#define STRINGIFY_INTERNAL(x) #x
+
+#define min(a, b) (((a) < (b)) ? (a) : (b))
/* default to old in-kernel interface if no memory pressure events */
-static int use_inkernel_interface = 1;
+static bool use_inkernel_interface = true;
static bool has_inkernel_module;
/* memory pressure levels */
@@ -102,14 +109,9 @@
"critical"
};
-struct mem_size {
- int free_mem;
- int free_swap;
-};
-
struct {
- int min_free; /* recorded but not used yet */
- int max_free;
+ int64_t min_nr_free_pages; /* recorded but not used yet */
+ int64_t max_nr_free_pages;
} low_pressure_mem = { -1, -1 };
static int level_oomadj[VMPRESS_LEVEL_COUNT];
@@ -118,17 +120,39 @@
static bool enable_pressure_upgrade;
static int64_t upgrade_pressure;
static int64_t downgrade_pressure;
-static bool is_go_device;
+static bool low_ram_device;
static bool kill_heaviest_task;
static unsigned long kill_timeout_ms;
+static bool use_minfree_levels;
+static bool per_app_memcg;
+static int swap_free_low_percentage;
-/* control socket listen and data */
-static int ctrl_lfd;
-static int ctrl_dfd = -1;
-static int ctrl_dfd_reopened; /* did we reopen ctrl conn on this loop? */
+static android_log_context ctx;
-/* 3 memory pressure levels, 1 ctrl listen socket, 1 ctrl data socket */
-#define MAX_EPOLL_EVENTS 5
+/* data required to handle events */
+struct event_handler_info {
+ int data;
+ void (*handler)(int data, uint32_t events);
+};
+
+/* data required to handle socket events */
+struct sock_event_handler_info {
+ int sock;
+ struct event_handler_info handler_info;
+};
+
+/* max supported number of data connections */
+#define MAX_DATA_CONN 2
+
+/* socket event handler data */
+static struct sock_event_handler_info ctrl_sock;
+static struct sock_event_handler_info data_sock[MAX_DATA_CONN];
+
+/* vmpressure event handler data */
+static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT];
+
+/* 3 memory pressure levels, 1 ctrl listen socket, 2 ctrl data socket */
+#define MAX_EPOLL_EVENTS (1 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT)
static int epollfd;
static int maxevents;
@@ -140,11 +164,117 @@
static int lowmem_minfree[MAX_TARGETS];
static int lowmem_targets_size;
-struct sysmeminfo {
- int nr_free_pages;
- int nr_file_pages;
- int nr_shmem;
- int totalreserve_pages;
+/* Fields to parse in /proc/zoneinfo */
+enum zoneinfo_field {
+ ZI_NR_FREE_PAGES = 0,
+ ZI_NR_FILE_PAGES,
+ ZI_NR_SHMEM,
+ ZI_NR_UNEVICTABLE,
+ ZI_WORKINGSET_REFAULT,
+ ZI_HIGH,
+ ZI_FIELD_COUNT
+};
+
+static const char* const zoneinfo_field_names[ZI_FIELD_COUNT] = {
+ "nr_free_pages",
+ "nr_file_pages",
+ "nr_shmem",
+ "nr_unevictable",
+ "workingset_refault",
+ "high",
+};
+
+union zoneinfo {
+ struct {
+ int64_t nr_free_pages;
+ int64_t nr_file_pages;
+ int64_t nr_shmem;
+ int64_t nr_unevictable;
+ int64_t workingset_refault;
+ int64_t high;
+ /* fields below are calculated rather than read from the file */
+ int64_t totalreserve_pages;
+ } field;
+ int64_t arr[ZI_FIELD_COUNT];
+};
+
+/* Fields to parse in /proc/meminfo */
+enum meminfo_field {
+ MI_NR_FREE_PAGES = 0,
+ MI_CACHED,
+ MI_SWAP_CACHED,
+ MI_BUFFERS,
+ MI_SHMEM,
+ MI_UNEVICTABLE,
+ MI_TOTAL_SWAP,
+ MI_FREE_SWAP,
+ MI_ACTIVE_ANON,
+ MI_INACTIVE_ANON,
+ MI_ACTIVE_FILE,
+ MI_INACTIVE_FILE,
+ MI_SRECLAIMABLE,
+ MI_SUNRECLAIM,
+ MI_KERNEL_STACK,
+ MI_PAGE_TABLES,
+ MI_ION_HELP,
+ MI_ION_HELP_POOL,
+ MI_CMA_FREE,
+ MI_FIELD_COUNT
+};
+
+static const char* const meminfo_field_names[MI_FIELD_COUNT] = {
+ "MemFree:",
+ "Cached:",
+ "SwapCached:",
+ "Buffers:",
+ "Shmem:",
+ "Unevictable:",
+ "SwapTotal:",
+ "SwapFree:",
+ "Active(anon):",
+ "Inactive(anon):",
+ "Active(file):",
+ "Inactive(file):",
+ "SReclaimable:",
+ "SUnreclaim:",
+ "KernelStack:",
+ "PageTables:",
+ "ION_heap:",
+ "ION_heap_pool:",
+ "CmaFree:",
+};
+
+union meminfo {
+ struct {
+ int64_t nr_free_pages;
+ int64_t cached;
+ int64_t swap_cached;
+ int64_t buffers;
+ int64_t shmem;
+ int64_t unevictable;
+ int64_t total_swap;
+ int64_t free_swap;
+ int64_t active_anon;
+ int64_t inactive_anon;
+ int64_t active_file;
+ int64_t inactive_file;
+ int64_t sreclaimable;
+ int64_t sunreclaimable;
+ int64_t kernel_stack;
+ int64_t page_tables;
+ int64_t ion_heap;
+ int64_t ion_heap_pool;
+ int64_t cma_free;
+ /* fields below are calculated rather than read from the file */
+ int64_t nr_file_pages;
+ } field;
+ int64_t arr[MI_FIELD_COUNT];
+};
+
+enum field_match_result {
+ NO_MATCH,
+ PARSE_FAIL,
+ PARSE_SUCCESS
};
struct adjslot_list {
@@ -160,6 +290,16 @@
struct proc *pidhash_next;
};
+struct reread_data {
+ const char* const filename;
+ int fd;
+};
+
+#ifdef LMKD_LOG_STATS
+static bool enable_stats_log;
+static android_log_context log_ctx;
+#endif
+
#define PIDHASH_SZ 1024
static struct proc *pidhash[PIDHASH_SZ];
#define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
@@ -170,12 +310,43 @@
/* PAGE_SIZE / 1024 */
static long page_k;
+static bool parse_int64(const char* str, int64_t* ret) {
+ char* endptr;
+ long long val = strtoll(str, &endptr, 10);
+ if (str == endptr || val > INT64_MAX) {
+ return false;
+ }
+ *ret = (int64_t)val;
+ return true;
+}
+
+static enum field_match_result match_field(const char* cp, const char* ap,
+ const char* const field_names[],
+ int field_count, int64_t* field,
+ int *field_idx) {
+ int64_t val;
+ int i;
+
+ for (i = 0; i < field_count; i++) {
+ if (!strcmp(cp, field_names[i])) {
+ *field_idx = i;
+ return parse_int64(ap, field) ? PARSE_SUCCESS : PARSE_FAIL;
+ }
+ }
+ return NO_MATCH;
+}
+
+/*
+ * Read file content from the beginning up to max_len bytes or EOF
+ * whichever happens first.
+ */
static ssize_t read_all(int fd, char *buf, size_t max_len)
{
ssize_t ret = 0;
+ off_t offset = 0;
while (max_len > 0) {
- ssize_t r = read(fd, buf, max_len);
+ ssize_t r = TEMP_FAILURE_RETRY(pread(fd, buf, max_len, offset));
if (r == 0) {
break;
}
@@ -184,12 +355,44 @@
}
ret += r;
buf += r;
+ offset += r;
max_len -= r;
}
return ret;
}
+/*
+ * Read a new or already opened file from the beginning.
+ * If the file has not been opened yet data->fd should be set to -1.
+ * To be used with files which are read often and possibly during high
+ * memory pressure to minimize file opening which by itself requires kernel
+ * memory allocation and might result in a stall on memory stressed system.
+ */
+static int reread_file(struct reread_data *data, char *buf, size_t buf_size) {
+ ssize_t size;
+
+ if (data->fd == -1) {
+ data->fd = open(data->filename, O_RDONLY | O_CLOEXEC);
+ if (data->fd == -1) {
+ ALOGE("%s open: %s", data->filename, strerror(errno));
+ return -1;
+ }
+ }
+
+ size = read_all(data->fd, buf, buf_size - 1);
+ if (size < 0) {
+ ALOGE("%s read: %s", data->filename, strerror(errno));
+ close(data->fd);
+ data->fd = -1;
+ return -1;
+ }
+ ALOG_ASSERT((size_t)size < buf_size - 1, "%s too large", data->filename);
+ buf[size] = 0;
+
+ return 0;
+}
+
static struct proc *pid_lookup(int pid) {
struct proc *procp;
@@ -263,77 +466,112 @@
return 0;
}
-static void writefilestring(const char *path, char *s) {
+/*
+ * Write a string to a file.
+ * Returns false if the file does not exist.
+ */
+static bool writefilestring(const char *path, const char *s,
+ bool err_if_missing) {
int fd = open(path, O_WRONLY | O_CLOEXEC);
- int len = strlen(s);
- int ret;
+ ssize_t len = strlen(s);
+ ssize_t ret;
if (fd < 0) {
- ALOGE("Error opening %s; errno=%d", path, errno);
- return;
+ if (err_if_missing) {
+ ALOGE("Error opening %s; errno=%d", path, errno);
+ }
+ return false;
}
- ret = write(fd, s, len);
+ ret = TEMP_FAILURE_RETRY(write(fd, s, len));
if (ret < 0) {
ALOGE("Error writing %s; errno=%d", path, errno);
} else if (ret < len) {
- ALOGE("Short write on %s; length=%d", path, ret);
+ ALOGE("Short write on %s; length=%zd", path, ret);
}
close(fd);
+ return true;
}
-static void cmd_procprio(int pid, int uid, int oomadj) {
+static void cmd_procprio(LMKD_CTRL_PACKET packet) {
struct proc *procp;
char path[80];
char val[20];
int soft_limit_mult;
+ struct lmk_procprio params;
+ bool is_system_server;
+ struct passwd *pwdrec;
- if (oomadj < OOM_SCORE_ADJ_MIN || oomadj > OOM_SCORE_ADJ_MAX) {
- ALOGE("Invalid PROCPRIO oomadj argument %d", oomadj);
+ lmkd_pack_get_procprio(packet, ¶ms);
+
+ if (params.oomadj < OOM_SCORE_ADJ_MIN ||
+ params.oomadj > OOM_SCORE_ADJ_MAX) {
+ ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj);
return;
}
- snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", pid);
- snprintf(val, sizeof(val), "%d", oomadj);
- writefilestring(path, val);
-
- if (use_inkernel_interface)
+ /* gid containing AID_READPROC required */
+ /* CAP_SYS_RESOURCE required */
+ /* CAP_DAC_OVERRIDE required */
+ snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid);
+ snprintf(val, sizeof(val), "%d", params.oomadj);
+ if (!writefilestring(path, val, false)) {
+ ALOGW("Failed to open %s; errno=%d: process %d might have been killed",
+ path, errno, params.pid);
+ /* If this file does not exist the process is dead. */
return;
-
- if (oomadj >= 900) {
- soft_limit_mult = 0;
- } else if (oomadj >= 800) {
- soft_limit_mult = 0;
- } else if (oomadj >= 700) {
- soft_limit_mult = 0;
- } else if (oomadj >= 600) {
- // Launcher should be perceptible, don't kill it.
- oomadj = 200;
- soft_limit_mult = 1;
- } else if (oomadj >= 500) {
- soft_limit_mult = 0;
- } else if (oomadj >= 400) {
- soft_limit_mult = 0;
- } else if (oomadj >= 300) {
- soft_limit_mult = 1;
- } else if (oomadj >= 200) {
- soft_limit_mult = 2;
- } else if (oomadj >= 100) {
- soft_limit_mult = 10;
- } else if (oomadj >= 0) {
- soft_limit_mult = 20;
- } else {
- // Persistent processes will have a large
- // soft limit 512MB.
- soft_limit_mult = 64;
}
- snprintf(path, sizeof(path), "/dev/memcg/apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", uid, pid);
- snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
- writefilestring(path, val);
+ if (use_inkernel_interface) {
+ return;
+ }
- procp = pid_lookup(pid);
+ if (per_app_memcg) {
+ if (params.oomadj >= 900) {
+ soft_limit_mult = 0;
+ } else if (params.oomadj >= 800) {
+ soft_limit_mult = 0;
+ } else if (params.oomadj >= 700) {
+ soft_limit_mult = 0;
+ } else if (params.oomadj >= 600) {
+ // Launcher should be perceptible, don't kill it.
+ params.oomadj = 200;
+ soft_limit_mult = 1;
+ } else if (params.oomadj >= 500) {
+ soft_limit_mult = 0;
+ } else if (params.oomadj >= 400) {
+ soft_limit_mult = 0;
+ } else if (params.oomadj >= 300) {
+ soft_limit_mult = 1;
+ } else if (params.oomadj >= 200) {
+ soft_limit_mult = 2;
+ } else if (params.oomadj >= 100) {
+ soft_limit_mult = 10;
+ } else if (params.oomadj >= 0) {
+ soft_limit_mult = 20;
+ } else {
+ // Persistent processes will have a large
+ // soft limit 512MB.
+ soft_limit_mult = 64;
+ }
+
+ snprintf(path, sizeof(path), MEMCG_SYSFS_PATH
+ "apps/uid_%d/pid_%d/memory.soft_limit_in_bytes",
+ params.uid, params.pid);
+ snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
+
+ /*
+ * system_server process has no memcg under /dev/memcg/apps but should be
+ * registered with lmkd. This is the best way so far to identify it.
+ */
+ is_system_server = (params.oomadj == SYSTEM_ADJ &&
+ (pwdrec = getpwnam("system")) != NULL &&
+ params.uid == pwdrec->pw_uid);
+ writefilestring(path, val, !is_system_server);
+ }
+
+ procp = pid_lookup(params.pid);
if (!procp) {
procp = malloc(sizeof(struct proc));
if (!procp) {
@@ -341,33 +579,39 @@
return;
}
- procp->pid = pid;
- procp->uid = uid;
- procp->oomadj = oomadj;
+ procp->pid = params.pid;
+ procp->uid = params.uid;
+ procp->oomadj = params.oomadj;
proc_insert(procp);
} else {
proc_unslot(procp);
- procp->oomadj = oomadj;
+ procp->oomadj = params.oomadj;
proc_slot(procp);
}
}
-static void cmd_procremove(int pid) {
- if (use_inkernel_interface)
- return;
+static void cmd_procremove(LMKD_CTRL_PACKET packet) {
+ struct lmk_procremove params;
- pid_remove(pid);
+ if (use_inkernel_interface) {
+ return;
+ }
+
+ lmkd_pack_get_procremove(packet, ¶ms);
+ pid_remove(params.pid);
}
-static void cmd_target(int ntargets, int *params) {
+static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) {
int i;
+ struct lmk_target target;
if (ntargets > (int)ARRAY_SIZE(lowmem_adj))
return;
for (i = 0; i < ntargets; i++) {
- lowmem_minfree[i] = ntohl(*params++);
- lowmem_adj[i] = ntohl(*params++);
+ lmkd_pack_get_target(packet, i, &target);
+ lowmem_minfree[i] = target.minfree;
+ lowmem_adj[i] = target.oom_adj_score;
}
lowmem_targets_size = ntargets;
@@ -393,22 +637,29 @@
strlcat(killpriostr, val, sizeof(killpriostr));
}
- writefilestring(INKERNEL_MINFREE_PATH, minfreestr);
- writefilestring(INKERNEL_ADJ_PATH, killpriostr);
+ writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true);
+ writefilestring(INKERNEL_ADJ_PATH, killpriostr, true);
}
}
-static void ctrl_data_close(void) {
- ALOGI("Closing Activity Manager data connection");
- close(ctrl_dfd);
- ctrl_dfd = -1;
+static void ctrl_data_close(int dsock_idx) {
+ struct epoll_event epev;
+
+ ALOGI("closing lmkd data connection");
+ if (epoll_ctl(epollfd, EPOLL_CTL_DEL, data_sock[dsock_idx].sock, &epev) == -1) {
+ // Log a warning and keep going
+ ALOGW("epoll_ctl for data connection socket failed; errno=%d", errno);
+ }
maxevents--;
+
+ close(data_sock[dsock_idx].sock);
+ data_sock[dsock_idx].sock = -1;
}
-static int ctrl_data_read(char *buf, size_t bufsz) {
+static int ctrl_data_read(int dsock_idx, char *buf, size_t bufsz) {
int ret = 0;
- ret = read(ctrl_dfd, buf, bufsz);
+ ret = TEMP_FAILURE_RETRY(read(data_sock[dsock_idx].sock, buf, bufsz));
if (ret == -1) {
ALOGE("control data socket read failed; errno=%d", errno);
@@ -420,39 +671,43 @@
return ret;
}
-static void ctrl_command_handler(void) {
- int ibuf[CTRL_PACKET_MAX / sizeof(int)];
+static void ctrl_command_handler(int dsock_idx) {
+ LMKD_CTRL_PACKET packet;
int len;
- int cmd = -1;
+ enum lmk_cmd cmd;
int nargs;
int targets;
- len = ctrl_data_read((char *)ibuf, CTRL_PACKET_MAX);
+ len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE);
if (len <= 0)
return;
+ if (len < (int)sizeof(int)) {
+ ALOGE("Wrong control socket read length len=%d", len);
+ return;
+ }
+
+ cmd = lmkd_pack_get_cmd(packet);
nargs = len / sizeof(int) - 1;
if (nargs < 0)
goto wronglen;
- cmd = ntohl(ibuf[0]);
-
switch(cmd) {
case LMK_TARGET:
targets = nargs / 2;
if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj))
goto wronglen;
- cmd_target(targets, &ibuf[1]);
+ cmd_target(targets, packet);
break;
case LMK_PROCPRIO:
if (nargs != 3)
goto wronglen;
- cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3]));
+ cmd_procprio(packet);
break;
case LMK_PROCREMOVE:
if (nargs != 1)
goto wronglen;
- cmd_procremove(ntohl(ibuf[1]));
+ cmd_procremove(packet);
break;
default:
ALOGE("Received unknown command code %d", cmd);
@@ -465,124 +720,259 @@
ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len);
}
-static void ctrl_data_handler(uint32_t events) {
- if (events & EPOLLHUP) {
- ALOGI("ActivityManager disconnected");
- if (!ctrl_dfd_reopened)
- ctrl_data_close();
- } else if (events & EPOLLIN) {
- ctrl_command_handler();
+static void ctrl_data_handler(int data, uint32_t events) {
+ if (events & EPOLLIN) {
+ ctrl_command_handler(data);
}
}
-static void ctrl_connect_handler(uint32_t events __unused) {
- struct epoll_event epev;
+static int get_free_dsock() {
+ for (int i = 0; i < MAX_DATA_CONN; i++) {
+ if (data_sock[i].sock < 0) {
+ return i;
+ }
+ }
+ return -1;
+}
- if (ctrl_dfd >= 0) {
- ctrl_data_close();
- ctrl_dfd_reopened = 1;
+static void ctrl_connect_handler(int data __unused, uint32_t events __unused) {
+ struct epoll_event epev;
+ int free_dscock_idx = get_free_dsock();
+
+ if (free_dscock_idx < 0) {
+ /*
+ * Number of data connections exceeded max supported. This should not
+ * happen but if it does we drop all existing connections and accept
+ * the new one. This prevents inactive connections from monopolizing
+ * data socket and if we drop ActivityManager connection it will
+ * immediately reconnect.
+ */
+ for (int i = 0; i < MAX_DATA_CONN; i++) {
+ ctrl_data_close(i);
+ }
+ free_dscock_idx = 0;
}
- ctrl_dfd = accept(ctrl_lfd, NULL, NULL);
-
- if (ctrl_dfd < 0) {
+ data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL);
+ if (data_sock[free_dscock_idx].sock < 0) {
ALOGE("lmkd control socket accept failed; errno=%d", errno);
return;
}
- ALOGI("ActivityManager connected");
- maxevents++;
+ ALOGI("lmkd data connection established");
+ /* use data to store data connection idx */
+ data_sock[free_dscock_idx].handler_info.data = free_dscock_idx;
+ data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler;
epev.events = EPOLLIN;
- epev.data.ptr = (void *)ctrl_data_handler;
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_dfd, &epev) == -1) {
+ epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info);
+ if (epoll_ctl(epollfd, EPOLL_CTL_ADD, data_sock[free_dscock_idx].sock, &epev) == -1) {
ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno);
- ctrl_data_close();
+ ctrl_data_close(free_dscock_idx);
return;
}
+ maxevents++;
}
-static int zoneinfo_parse_protection(char *cp) {
- int max = 0;
- int zoneval;
+#ifdef LMKD_LOG_STATS
+static void memory_stat_parse_line(char *line, struct memory_stat *mem_st) {
+ char key[LINE_MAX + 1];
+ int64_t value;
+
+ sscanf(line, "%" STRINGIFY(LINE_MAX) "s %" SCNd64 "", key, &value);
+
+ if (strcmp(key, "total_") < 0) {
+ return;
+ }
+
+ if (!strcmp(key, "total_pgfault"))
+ mem_st->pgfault = value;
+ else if (!strcmp(key, "total_pgmajfault"))
+ mem_st->pgmajfault = value;
+ else if (!strcmp(key, "total_rss"))
+ mem_st->rss_in_bytes = value;
+ else if (!strcmp(key, "total_cache"))
+ mem_st->cache_in_bytes = value;
+ else if (!strcmp(key, "total_swap"))
+ mem_st->swap_in_bytes = value;
+}
+
+static int memory_stat_parse(struct memory_stat *mem_st, int pid, uid_t uid) {
+ FILE *fp;
+ char buf[PATH_MAX];
+
+ /*
+ * Per-application memory.stat files are available only when
+ * per-application memcgs are enabled.
+ */
+ if (!per_app_memcg)
+ return -1;
+
+ snprintf(buf, sizeof(buf), MEMCG_PROCESS_MEMORY_STAT_PATH, uid, pid);
+
+ fp = fopen(buf, "r");
+
+ if (fp == NULL) {
+ ALOGE("%s open failed: %s", buf, strerror(errno));
+ return -1;
+ }
+
+ while (fgets(buf, PAGE_SIZE, fp) != NULL ) {
+ memory_stat_parse_line(buf, mem_st);
+ }
+ fclose(fp);
+
+ return 0;
+}
+#endif
+
+/* /prop/zoneinfo parsing routines */
+static int64_t zoneinfo_parse_protection(char *cp) {
+ int64_t max = 0;
+ long long zoneval;
char *save_ptr;
- for (cp = strtok_r(cp, "(), ", &save_ptr); cp; cp = strtok_r(NULL, "), ", &save_ptr)) {
- zoneval = strtol(cp, &cp, 0);
- if (zoneval > max)
- max = zoneval;
+ for (cp = strtok_r(cp, "(), ", &save_ptr); cp;
+ cp = strtok_r(NULL, "), ", &save_ptr)) {
+ zoneval = strtoll(cp, &cp, 0);
+ if (zoneval > max) {
+ max = (zoneval > INT64_MAX) ? INT64_MAX : zoneval;
+ }
}
return max;
}
-static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) {
+static bool zoneinfo_parse_line(char *line, union zoneinfo *zi) {
char *cp = line;
char *ap;
char *save_ptr;
+ int64_t val;
+ int field_idx;
cp = strtok_r(line, " ", &save_ptr);
- if (!cp)
- return;
+ if (!cp) {
+ return true;
+ }
- ap = strtok_r(NULL, " ", &save_ptr);
- if (!ap)
- return;
+ if (!strcmp(cp, "protection:")) {
+ ap = strtok_r(NULL, ")", &save_ptr);
+ } else {
+ ap = strtok_r(NULL, " ", &save_ptr);
+ }
- if (!strcmp(cp, "nr_free_pages"))
- mip->nr_free_pages += strtol(ap, NULL, 0);
- else if (!strcmp(cp, "nr_file_pages"))
- mip->nr_file_pages += strtol(ap, NULL, 0);
- else if (!strcmp(cp, "nr_shmem"))
- mip->nr_shmem += strtol(ap, NULL, 0);
- else if (!strcmp(cp, "high"))
- mip->totalreserve_pages += strtol(ap, NULL, 0);
- else if (!strcmp(cp, "protection:"))
- mip->totalreserve_pages += zoneinfo_parse_protection(ap);
+ if (!ap) {
+ return true;
+ }
+
+ switch (match_field(cp, ap, zoneinfo_field_names,
+ ZI_FIELD_COUNT, &val, &field_idx)) {
+ case (PARSE_SUCCESS):
+ zi->arr[field_idx] += val;
+ break;
+ case (NO_MATCH):
+ if (!strcmp(cp, "protection:")) {
+ zi->field.totalreserve_pages +=
+ zoneinfo_parse_protection(ap);
+ }
+ break;
+ case (PARSE_FAIL):
+ default:
+ return false;
+ }
+ return true;
}
-static int zoneinfo_parse(struct sysmeminfo *mip) {
- int fd;
- ssize_t size;
+static int zoneinfo_parse(union zoneinfo *zi) {
+ static struct reread_data file_data = {
+ .filename = ZONEINFO_PATH,
+ .fd = -1,
+ };
char buf[PAGE_SIZE];
char *save_ptr;
char *line;
- memset(mip, 0, sizeof(struct sysmeminfo));
+ memset(zi, 0, sizeof(union zoneinfo));
- fd = open(ZONEINFO_PATH, O_RDONLY | O_CLOEXEC);
- if (fd == -1) {
- ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno);
+ if (reread_file(&file_data, buf, sizeof(buf)) < 0) {
return -1;
}
- size = read_all(fd, buf, sizeof(buf) - 1);
- if (size < 0) {
- ALOGE("%s read: errno=%d", ZONEINFO_PATH, errno);
- close(fd);
- return -1;
+ for (line = strtok_r(buf, "\n", &save_ptr); line;
+ line = strtok_r(NULL, "\n", &save_ptr)) {
+ if (!zoneinfo_parse_line(line, zi)) {
+ ALOGE("%s parse error", file_data.filename);
+ return -1;
+ }
}
- ALOG_ASSERT((size_t)size < sizeof(buf) - 1, "/proc/zoneinfo too large");
- buf[size] = 0;
+ zi->field.totalreserve_pages += zi->field.high;
- for (line = strtok_r(buf, "\n", &save_ptr); line; line = strtok_r(NULL, "\n", &save_ptr))
- zoneinfo_parse_line(line, mip);
-
- close(fd);
return 0;
}
-static int get_free_memory(struct mem_size *ms) {
- struct sysinfo si;
+/* /prop/meminfo parsing routines */
+static bool meminfo_parse_line(char *line, union meminfo *mi) {
+ char *cp = line;
+ char *ap;
+ char *save_ptr;
+ int64_t val;
+ int field_idx;
+ enum field_match_result match_res;
- if (sysinfo(&si) < 0)
+ cp = strtok_r(line, " ", &save_ptr);
+ if (!cp) {
+ return false;
+ }
+
+ ap = strtok_r(NULL, " ", &save_ptr);
+ if (!ap) {
+ return false;
+ }
+
+ match_res = match_field(cp, ap, meminfo_field_names, MI_FIELD_COUNT,
+ &val, &field_idx);
+ if (match_res == PARSE_SUCCESS) {
+ mi->arr[field_idx] = val / page_k;
+ }
+ return (match_res != PARSE_FAIL);
+}
+
+static int meminfo_parse(union meminfo *mi) {
+ static struct reread_data file_data = {
+ .filename = MEMINFO_PATH,
+ .fd = -1,
+ };
+ char buf[PAGE_SIZE];
+ char *save_ptr;
+ char *line;
+
+ memset(mi, 0, sizeof(union meminfo));
+
+ if (reread_file(&file_data, buf, sizeof(buf)) < 0) {
return -1;
+ }
- ms->free_mem = (int)(si.freeram * si.mem_unit / PAGE_SIZE);
- ms->free_swap = (int)(si.freeswap * si.mem_unit / PAGE_SIZE);
+ for (line = strtok_r(buf, "\n", &save_ptr); line;
+ line = strtok_r(NULL, "\n", &save_ptr)) {
+ if (!meminfo_parse_line(line, mi)) {
+ ALOGE("%s parse error", file_data.filename);
+ return -1;
+ }
+ }
+ mi->field.nr_file_pages = mi->field.cached + mi->field.swap_cached +
+ mi->field.buffers;
return 0;
}
+static void meminfo_log(union meminfo *mi) {
+ for (int field_idx = 0; field_idx < MI_FIELD_COUNT; field_idx++) {
+ android_log_write_int32(ctx, (int32_t)min(mi->arr[field_idx] * page_k, INT32_MAX));
+ }
+
+ android_log_write_list(ctx, LOG_ID_EVENTS);
+ android_log_reset(ctx);
+}
+
static int proc_get_size(int pid) {
char path[PATH_MAX];
char line[LINE_MAX];
@@ -591,6 +981,7 @@
int total;
ssize_t ret;
+ /* gid containing AID_READPROC required */
snprintf(path, PATH_MAX, "/proc/%d/statm", pid);
fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd == -1)
@@ -614,6 +1005,7 @@
char *cp;
ssize_t ret;
+ /* gid containing AID_READPROC required */
snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid);
fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd == -1)
@@ -667,6 +1059,11 @@
int tasksize;
int r;
+#ifdef LMKD_LOG_STATS
+ struct memory_stat mem_st = {};
+ int memory_stat_parse_result = -1;
+#endif
+
taskname = proc_get_name(pid);
if (!taskname) {
pid_remove(pid);
@@ -679,12 +1076,19 @@
return -1;
}
+#ifdef LMKD_LOG_STATS
+ if (enable_stats_log) {
+ memory_stat_parse_result = memory_stat_parse(&mem_st, pid, uid);
+ }
+#endif
+
TRACE_KILL_START(pid);
+ /* CAP_KILL required */
r = kill(pid, SIGKILL);
ALOGI(
"Killing '%s' (%d), uid %d, adj %d\n"
- " to free %ldkB because system is under %s memory pressure oom_adj %d\n",
+ " to free %ldkB because system is under %s memory pressure (min_oom_adj=%d)\n",
taskname, pid, uid, procp->oomadj, tasksize * page_k,
level_name[level], min_score_adj);
pid_remove(pid);
@@ -694,6 +1098,15 @@
if (r) {
ALOGE("kill(%d): errno=%d", pid, errno);
return -1;
+ } else {
+#ifdef LMKD_LOG_STATS
+ if (memory_stat_parse_result == 0) {
+ stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname,
+ procp->oomadj, mem_st.pgfault, mem_st.pgmajfault, mem_st.rss_in_bytes,
+ mem_st.cache_in_bytes, mem_st.swap_in_bytes);
+ }
+#endif
+ return tasksize;
}
return tasksize;
@@ -705,54 +1118,72 @@
* Returns the size of the killed processes.
*/
static int find_and_kill_processes(enum vmpressure_level level,
- int pages_to_free) {
+ int min_score_adj, int pages_to_free) {
int i;
int killed_size;
int pages_freed = 0;
- int min_score_adj = level_oomadj[level];
+
+#ifdef LMKD_LOG_STATS
+ bool lmk_state_change_start = false;
+#endif
for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) {
struct proc *procp;
while (true) {
- if (is_go_device)
- procp = proc_adj_lru(i);
- else
- procp = proc_get_heaviest(i);
+ procp = kill_heaviest_task ?
+ proc_get_heaviest(i) : proc_adj_lru(i);
if (!procp)
break;
killed_size = kill_one_process(procp, min_score_adj, level);
if (killed_size >= 0) {
+#ifdef LMKD_LOG_STATS
+ if (enable_stats_log && !lmk_state_change_start) {
+ lmk_state_change_start = true;
+ stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED,
+ LMK_STATE_CHANGE_START);
+ }
+#endif
+
pages_freed += killed_size;
if (pages_freed >= pages_to_free) {
+
+#ifdef LMKD_LOG_STATS
+ if (enable_stats_log && lmk_state_change_start) {
+ stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED,
+ LMK_STATE_CHANGE_STOP);
+ }
+#endif
return pages_freed;
}
}
}
}
+#ifdef LMKD_LOG_STATS
+ if (enable_stats_log && lmk_state_change_start) {
+ stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, LMK_STATE_CHANGE_STOP);
+ }
+#endif
+
return pages_freed;
}
-static int64_t get_memory_usage(const char* path) {
+static int64_t get_memory_usage(struct reread_data *file_data) {
int ret;
int64_t mem_usage;
char buf[32];
- int fd = open(path, O_RDONLY | O_CLOEXEC);
- if (fd == -1) {
- ALOGE("%s open: errno=%d", path, errno);
+
+ if (reread_file(file_data, buf, sizeof(buf)) < 0) {
return -1;
}
- ret = read_all(fd, buf, sizeof(buf) - 1);
- close(fd);
- if (ret < 0) {
- ALOGE("%s error: errno=%d", path, errno);
+ if (!parse_int64(buf, &mem_usage)) {
+ ALOGE("%s parse error", file_data->filename);
return -1;
}
- sscanf(buf, "%" SCNd64, &mem_usage);
if (mem_usage == 0) {
ALOGE("No memory!");
return -1;
@@ -760,29 +1191,30 @@
return mem_usage;
}
-void record_low_pressure_levels(struct mem_size *free_mem) {
- if (low_pressure_mem.min_free == -1 ||
- low_pressure_mem.min_free > free_mem->free_mem) {
+void record_low_pressure_levels(union meminfo *mi) {
+ if (low_pressure_mem.min_nr_free_pages == -1 ||
+ low_pressure_mem.min_nr_free_pages > mi->field.nr_free_pages) {
if (debug_process_killing) {
- ALOGI("Low pressure min memory update from %d to %d",
- low_pressure_mem.min_free, free_mem->free_mem);
+ ALOGI("Low pressure min memory update from %" PRId64 " to %" PRId64,
+ low_pressure_mem.min_nr_free_pages, mi->field.nr_free_pages);
}
- low_pressure_mem.min_free = free_mem->free_mem;
+ low_pressure_mem.min_nr_free_pages = mi->field.nr_free_pages;
}
/*
* Free memory at low vmpressure events occasionally gets spikes,
* possibly a stale low vmpressure event with memory already
* freed up (no memory pressure should have been reported).
- * Ignore large jumps in max_free that would mess up our stats.
+ * Ignore large jumps in max_nr_free_pages that would mess up our stats.
*/
- if (low_pressure_mem.max_free == -1 ||
- (low_pressure_mem.max_free < free_mem->free_mem &&
- free_mem->free_mem - low_pressure_mem.max_free < low_pressure_mem.max_free * 0.1)) {
+ if (low_pressure_mem.max_nr_free_pages == -1 ||
+ (low_pressure_mem.max_nr_free_pages < mi->field.nr_free_pages &&
+ mi->field.nr_free_pages - low_pressure_mem.max_nr_free_pages <
+ low_pressure_mem.max_nr_free_pages * 0.1)) {
if (debug_process_killing) {
- ALOGI("Low pressure max memory update from %d to %d",
- low_pressure_mem.max_free, free_mem->free_mem);
+ ALOGI("Low pressure max memory update from %" PRId64 " to %" PRId64,
+ low_pressure_mem.max_nr_free_pages, mi->field.nr_free_pages);
}
- low_pressure_mem.max_free = free_mem->free_mem;
+ low_pressure_mem.max_nr_free_pages = mi->field.nr_free_pages;
}
}
@@ -802,15 +1234,29 @@
(to->tv_usec - from->tv_usec) / 1000;
}
-static void mp_event_common(enum vmpressure_level level) {
+static void mp_event_common(int data, uint32_t events __unused) {
int ret;
unsigned long long evcount;
int64_t mem_usage, memsw_usage;
int64_t mem_pressure;
enum vmpressure_level lvl;
- struct mem_size free_mem;
+ union meminfo mi;
+ union zoneinfo zi;
static struct timeval last_report_tm;
static unsigned long skip_count = 0;
+ enum vmpressure_level level = (enum vmpressure_level)data;
+ long other_free = 0, other_file = 0;
+ int min_score_adj;
+ int pages_to_free = 0;
+ int minfree = 0;
+ static struct reread_data mem_usage_file_data = {
+ .filename = MEMCG_MEMORY_USAGE,
+ .fd = -1,
+ };
+ static struct reread_data memsw_usage_file_data = {
+ .filename = MEMCG_MEMORYSW_USAGE,
+ .fd = -1,
+ };
/*
* Check all event counters from low to critical
@@ -819,7 +1265,8 @@
*/
for (lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) {
if (mpevfd[lvl] != -1 &&
- read(mpevfd[lvl], &evcount, sizeof(evcount)) > 0 &&
+ TEMP_FAILURE_RETRY(read(mpevfd[lvl],
+ &evcount, sizeof(evcount))) > 0 &&
evcount > 0 && lvl > level) {
level = lvl;
}
@@ -835,30 +1282,65 @@
}
if (skip_count > 0) {
- if (debug_process_killing) {
- ALOGI("%lu memory pressure events were skipped after a kill!",
- skip_count);
- }
+ ALOGI("%lu memory pressure events were skipped after a kill!",
+ skip_count);
skip_count = 0;
}
- if (get_free_memory(&free_mem) == 0) {
- if (level == VMPRESS_LEVEL_LOW) {
- record_low_pressure_levels(&free_mem);
- }
- } else {
+ if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) {
ALOGE("Failed to get free memory!");
return;
}
+ if (use_minfree_levels) {
+ int i;
+
+ other_free = mi.field.nr_free_pages - zi.field.totalreserve_pages;
+ if (mi.field.nr_file_pages > (mi.field.shmem + mi.field.unevictable + mi.field.swap_cached)) {
+ other_file = (mi.field.nr_file_pages - mi.field.shmem -
+ mi.field.unevictable - mi.field.swap_cached);
+ } else {
+ other_file = 0;
+ }
+
+ min_score_adj = OOM_SCORE_ADJ_MAX + 1;
+ for (i = 0; i < lowmem_targets_size; i++) {
+ minfree = lowmem_minfree[i];
+ if (other_free < minfree && other_file < minfree) {
+ min_score_adj = lowmem_adj[i];
+ break;
+ }
+ }
+
+ if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
+ if (debug_process_killing) {
+ ALOGI("Ignore %s memory pressure event "
+ "(free memory=%ldkB, cache=%ldkB, limit=%ldkB)",
+ level_name[level], other_free * page_k, other_file * page_k,
+ (long)lowmem_minfree[lowmem_targets_size - 1] * page_k);
+ }
+ return;
+ }
+
+ /* Free up enough pages to push over the highest minfree level */
+ pages_to_free = lowmem_minfree[lowmem_targets_size - 1] -
+ ((other_free < other_file) ? other_free : other_file);
+ goto do_kill;
+ }
+
+ if (level == VMPRESS_LEVEL_LOW) {
+ record_low_pressure_levels(&mi);
+ }
+
if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) {
/* Do not monitor this pressure level */
return;
}
- mem_usage = get_memory_usage(MEMCG_MEMORY_USAGE);
- memsw_usage = get_memory_usage(MEMCG_MEMORYSW_USAGE);
- if (memsw_usage < 0 || mem_usage < 0) {
+ if ((mem_usage = get_memory_usage(&mem_usage_file_data)) < 0) {
+ goto do_kill;
+ }
+ if ((memsw_usage = get_memory_usage(&memsw_usage_file_data)) < 0) {
goto do_kill;
}
@@ -875,79 +1357,100 @@
}
}
- // If the pressure is larger than downgrade_pressure lmk will not
- // kill any process, since enough memory is available.
- if (mem_pressure > downgrade_pressure) {
- if (debug_process_killing) {
- ALOGI("Ignore %s memory pressure", level_name[level]);
+ // If we still have enough swap space available, check if we want to
+ // ignore/downgrade pressure events.
+ if (mi.field.free_swap >=
+ mi.field.total_swap * swap_free_low_percentage / 100) {
+ // If the pressure is larger than downgrade_pressure lmk will not
+ // kill any process, since enough memory is available.
+ if (mem_pressure > downgrade_pressure) {
+ if (debug_process_killing) {
+ ALOGI("Ignore %s memory pressure", level_name[level]);
+ }
+ return;
+ } else if (level == VMPRESS_LEVEL_CRITICAL && mem_pressure > upgrade_pressure) {
+ if (debug_process_killing) {
+ ALOGI("Downgrade critical memory pressure");
+ }
+ // Downgrade event, since enough memory available.
+ level = downgrade_level(level);
}
- return;
- } else if (level == VMPRESS_LEVEL_CRITICAL &&
- mem_pressure > upgrade_pressure) {
- if (debug_process_killing) {
- ALOGI("Downgrade critical memory pressure");
- }
- // Downgrade event, since enough memory available.
- level = downgrade_level(level);
}
do_kill:
- if (is_go_device) {
+ if (low_ram_device) {
/* For Go devices kill only one task */
- if (find_and_kill_processes(level, 0) == 0) {
+ if (find_and_kill_processes(level, level_oomadj[level], 0) == 0) {
if (debug_process_killing) {
ALOGI("Nothing to kill");
}
+ } else {
+ meminfo_log(&mi);
}
} else {
- /* If pressure level is less than critical and enough free swap then ignore */
- if (level < VMPRESS_LEVEL_CRITICAL && free_mem.free_swap > low_pressure_mem.max_free) {
- if (debug_process_killing) {
- ALOGI("Ignoring pressure since %d swap pages are available ", free_mem.free_swap);
+ int pages_freed;
+
+ if (!use_minfree_levels) {
+ /* If pressure level is less than critical and enough free swap then ignore */
+ if (level < VMPRESS_LEVEL_CRITICAL &&
+ mi.field.free_swap > low_pressure_mem.max_nr_free_pages) {
+ if (debug_process_killing) {
+ ALOGI("Ignoring pressure since %" PRId64
+ " swap pages are available ",
+ mi.field.free_swap);
+ }
+ return;
}
- return;
+ /* Free up enough memory to downgrate the memory pressure to low level */
+ if (mi.field.nr_free_pages < low_pressure_mem.max_nr_free_pages) {
+ pages_to_free = low_pressure_mem.max_nr_free_pages -
+ mi.field.nr_free_pages;
+ } else {
+ if (debug_process_killing) {
+ ALOGI("Ignoring pressure since more memory is "
+ "available (%" PRId64 ") than watermark (%" PRId64 ")",
+ mi.field.nr_free_pages, low_pressure_mem.max_nr_free_pages);
+ }
+ return;
+ }
+ min_score_adj = level_oomadj[level];
}
- /* Free up enough memory to downgrate the memory pressure to low level */
- if (free_mem.free_mem < low_pressure_mem.max_free) {
- int pages_to_free = low_pressure_mem.max_free - free_mem.free_mem;
- if (debug_process_killing) {
- ALOGI("Trying to free %d pages", pages_to_free);
- }
- int pages_freed = find_and_kill_processes(level, pages_to_free);
- if (pages_freed < pages_to_free) {
- if (debug_process_killing) {
- ALOGI("Unable to free enough memory (pages freed=%d)",
- pages_freed);
- }
- } else {
- gettimeofday(&last_report_tm, NULL);
- }
+ pages_freed = find_and_kill_processes(level, min_score_adj, pages_to_free);
+
+ if (use_minfree_levels) {
+ ALOGI("Killing because cache %ldkB is below "
+ "limit %ldkB for oom_adj %d\n"
+ " Free memory is %ldkB %s reserved",
+ other_file * page_k, minfree * page_k, min_score_adj,
+ other_free * page_k, other_free >= 0 ? "above" : "below");
+ }
+
+ if (pages_freed < pages_to_free) {
+ ALOGI("Unable to free enough memory (pages to free=%d, pages freed=%d)",
+ pages_to_free, pages_freed);
+ } else {
+ ALOGI("Reclaimed enough memory (pages to free=%d, pages freed=%d)",
+ pages_to_free, pages_freed);
+ gettimeofday(&last_report_tm, NULL);
+ }
+ if (pages_freed > 0) {
+ meminfo_log(&mi);
}
}
}
-static void mp_event_low(uint32_t events __unused) {
- mp_event_common(VMPRESS_LEVEL_LOW);
-}
-
-static void mp_event_medium(uint32_t events __unused) {
- mp_event_common(VMPRESS_LEVEL_MEDIUM);
-}
-
-static void mp_event_critical(uint32_t events __unused) {
- mp_event_common(VMPRESS_LEVEL_CRITICAL);
-}
-
-static bool init_mp_common(void *event_handler, enum vmpressure_level level) {
+static bool init_mp_common(enum vmpressure_level level) {
int mpfd;
int evfd;
int evctlfd;
char buf[256];
struct epoll_event epev;
int ret;
- const char *levelstr = level_name[level];
+ int level_idx = (int)level;
+ const char *levelstr = level_name[level_idx];
+ /* gid containing AID_SYSTEM required */
mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC);
if (mpfd < 0) {
ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
@@ -972,7 +1475,7 @@
goto err;
}
- ret = write(evctlfd, buf, strlen(buf) + 1);
+ ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1));
if (ret == -1) {
ALOGE("cgroup.event_control write failed for level %s; errno=%d",
levelstr, errno);
@@ -980,7 +1483,10 @@
}
epev.events = EPOLLIN;
- epev.data.ptr = event_handler;
+ /* use data to store event level */
+ vmpressure_hinfo[level_idx].data = level_idx;
+ vmpressure_hinfo[level_idx].handler = mp_event_common;
+ epev.data.ptr = (void *)&vmpressure_hinfo[level_idx];
ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev);
if (ret == -1) {
ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno);
@@ -1017,21 +1523,27 @@
return -1;
}
- ctrl_lfd = android_get_control_socket("lmkd");
- if (ctrl_lfd < 0) {
+ // mark data connections as not connected
+ for (int i = 0; i < MAX_DATA_CONN; i++) {
+ data_sock[i].sock = -1;
+ }
+
+ ctrl_sock.sock = android_get_control_socket("lmkd");
+ if (ctrl_sock.sock < 0) {
ALOGE("get lmkd control socket failed");
return -1;
}
- ret = listen(ctrl_lfd, 1);
+ ret = listen(ctrl_sock.sock, MAX_DATA_CONN);
if (ret < 0) {
ALOGE("lmkd control socket listen failed (errno=%d)", errno);
return -1;
}
epev.events = EPOLLIN;
- epev.data.ptr = (void *)ctrl_connect_handler;
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_lfd, &epev) == -1) {
+ ctrl_sock.handler_info.handler = ctrl_connect_handler;
+ epev.data.ptr = (void *)&(ctrl_sock.handler_info);
+ if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) {
ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno);
return -1;
}
@@ -1043,10 +1555,9 @@
if (use_inkernel_interface) {
ALOGI("Using in-kernel low memory killer interface");
} else {
- if (!init_mp_common((void *)&mp_event_low, VMPRESS_LEVEL_LOW) ||
- !init_mp_common((void *)&mp_event_medium, VMPRESS_LEVEL_MEDIUM) ||
- !init_mp_common((void *)&mp_event_critical,
- VMPRESS_LEVEL_CRITICAL)) {
+ if (!init_mp_common(VMPRESS_LEVEL_LOW) ||
+ !init_mp_common(VMPRESS_LEVEL_MEDIUM) ||
+ !init_mp_common(VMPRESS_LEVEL_CRITICAL)) {
ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer");
return -1;
}
@@ -1061,12 +1572,14 @@
}
static void mainloop(void) {
+ struct event_handler_info* handler_info;
+ struct epoll_event *evt;
+
while (1) {
struct epoll_event events[maxevents];
int nevents;
int i;
- ctrl_dfd_reopened = 0;
nevents = epoll_wait(epollfd, events, maxevents, -1);
if (nevents == -1) {
@@ -1076,11 +1589,33 @@
continue;
}
- for (i = 0; i < nevents; ++i) {
- if (events[i].events & EPOLLERR)
+ /*
+ * First pass to see if any data socket connections were dropped.
+ * Dropped connection should be handled before any other events
+ * to deallocate data connection and correctly handle cases when
+ * connection gets dropped and reestablished in the same epoll cycle.
+ * In such cases it's essential to handle connection closures first.
+ */
+ for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) {
+ if ((evt->events & EPOLLHUP) && evt->data.ptr) {
+ ALOGI("lmkd data connection dropped");
+ handler_info = (struct event_handler_info*)evt->data.ptr;
+ ctrl_data_close(handler_info->data);
+ }
+ }
+
+ /* Second pass to handle all other events */
+ for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) {
+ if (evt->events & EPOLLERR)
ALOGD("EPOLLERR on event #%d", i);
- if (events[i].data.ptr)
- (*(void (*)(uint32_t))events[i].data.ptr)(events[i].events);
+ if (evt->events & EPOLLHUP) {
+ /* This case was handled in the first pass */
+ continue;
+ }
+ if (evt->data.ptr) {
+ handler_info = (struct event_handler_info*)evt->data.ptr;
+ handler_info->handler(handler_info->data, evt->events);
+ }
}
}
}
@@ -1107,26 +1642,55 @@
downgrade_pressure =
(int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100);
kill_heaviest_task =
- property_get_bool("ro.lmk.kill_heaviest_task", true);
- is_go_device = property_get_bool("ro.config.low_ram", false);
+ property_get_bool("ro.lmk.kill_heaviest_task", false);
+ low_ram_device = property_get_bool("ro.config.low_ram", false);
kill_timeout_ms =
(unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0);
+ use_minfree_levels =
+ property_get_bool("ro.lmk.use_minfree_levels", false);
+ per_app_memcg =
+ property_get_bool("ro.config.per_app_memcg", low_ram_device);
+ swap_free_low_percentage =
+ property_get_int32("ro.lmk.swap_free_low_percentage", 10);
- // MCL_ONFAULT pins pages as they fault instead of loading
- // everything immediately all at once. (Which would be bad,
- // because as of this writing, we have a lot of mapped pages we
- // never use.) Old kernels will see MCL_ONFAULT and fail with
- // EINVAL; we ignore this failure.
- //
- // N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT
- // pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault
- // in pages.
- if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && errno != EINVAL)
- ALOGW("mlockall failed: errno=%d", errno);
+ ctx = create_android_logger(MEMINFO_LOG_TAG);
- sched_setscheduler(0, SCHED_FIFO, ¶m);
- if (!init())
+#ifdef LMKD_LOG_STATS
+ statslog_init(&log_ctx, &enable_stats_log);
+#endif
+
+ if (!init()) {
+ if (!use_inkernel_interface) {
+ /*
+ * MCL_ONFAULT pins pages as they fault instead of loading
+ * everything immediately all at once. (Which would be bad,
+ * because as of this writing, we have a lot of mapped pages we
+ * never use.) Old kernels will see MCL_ONFAULT and fail with
+ * EINVAL; we ignore this failure.
+ *
+ * N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT
+ * pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault
+ * in pages.
+ */
+ /* CAP_IPC_LOCK required */
+ if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) {
+ ALOGW("mlockall failed %s", strerror(errno));
+ }
+
+ /* CAP_NICE required */
+ if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
+ ALOGW("set SCHED_FIFO failed %s", strerror(errno));
+ }
+ }
+
mainloop();
+ }
+
+#ifdef LMKD_LOG_STATS
+ statslog_destroy(&log_ctx);
+#endif
+
+ android_log_destroy(&ctx);
ALOGI("exiting");
return 0;
diff --git a/lmkd/lmkd.rc b/lmkd/lmkd.rc
index 3bb84ab..76b6055 100644
--- a/lmkd/lmkd.rc
+++ b/lmkd/lmkd.rc
@@ -1,6 +1,8 @@
service lmkd /system/bin/lmkd
class core
- group root readproc
+ user lmkd
+ group lmkd system readproc
+ capabilities DAC_OVERRIDE KILL IPC_LOCK SYS_NICE SYS_RESOURCE
critical
socket lmkd seqpacket 0660 system system
writepid /dev/cpuset/system-background/tasks
diff --git a/lmkd/statslog.c b/lmkd/statslog.c
new file mode 100644
index 0000000..66d1164
--- /dev/null
+++ b/lmkd/statslog.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2018 Google, Inc
+ *
+ * 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 <assert.h>
+#include <errno.h>
+#include <log/log_id.h>
+#include <stats_event_list.h>
+#include <time.h>
+
+static int64_t getElapsedRealTimeNs() {
+ struct timespec t;
+ t.tv_sec = t.tv_nsec = 0;
+ clock_gettime(CLOCK_BOOTTIME, &t);
+ return (int64_t)t.tv_sec * 1000000000LL + t.tv_nsec;
+}
+
+/**
+ * Logs the change in LMKD state which is used as start/stop boundaries for logging
+ * LMK_KILL_OCCURRED event.
+ * Code: LMK_STATE_CHANGED = 54
+ */
+int
+stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t state) {
+ assert(ctx != NULL);
+ int ret = -EINVAL;
+ if (!ctx) {
+ return ret;
+ }
+
+ reset_log_context(ctx);
+
+ if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_int32(ctx, code)) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_int32(ctx, state)) < 0) {
+ return ret;
+ }
+
+ return write_to_logger(ctx, LOG_ID_STATS);
+}
+
+/**
+ * Logs the event when LMKD kills a process to reduce memory pressure.
+ * Code: LMK_KILL_OCCURRED = 51
+ */
+int
+stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
+ char const* process_name, int32_t oom_score, int64_t pgfault,
+ int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
+ int64_t swap_in_bytes) {
+ assert(ctx != NULL);
+ int ret = -EINVAL;
+ if (!ctx) {
+ return ret;
+ }
+ reset_log_context(ctx);
+
+ if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_int32(ctx, code)) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_int32(ctx, uid)) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_string8(ctx, (process_name == NULL) ? "" : process_name)) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_int32(ctx, oom_score)) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_int64(ctx, pgfault)) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_int64(ctx, pgmajfault)) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_int64(ctx, rss_in_bytes)) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_int64(ctx, cache_in_bytes)) < 0) {
+ return ret;
+ }
+
+ if ((ret = android_log_write_int64(ctx, swap_in_bytes)) < 0) {
+ return ret;
+ }
+
+ return write_to_logger(ctx, LOG_ID_STATS);
+}
diff --git a/lmkd/statslog.h b/lmkd/statslog.h
new file mode 100644
index 0000000..edebb19
--- /dev/null
+++ b/lmkd/statslog.h
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2018 Google, Inc
+ *
+ * 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.
+ */
+
+#ifndef _STATSLOG_H_
+#define _STATSLOG_H_
+
+#include <assert.h>
+#include <stats_event_list.h>
+#include <stdbool.h>
+#include <sys/cdefs.h>
+
+#include <cutils/properties.h>
+
+__BEGIN_DECLS
+
+/*
+ * These are defined in
+ * http://cs/android/frameworks/base/cmds/statsd/src/atoms.proto
+ */
+#define LMK_KILL_OCCURRED 51
+#define LMK_STATE_CHANGED 54
+#define LMK_STATE_CHANGE_START 1
+#define LMK_STATE_CHANGE_STOP 2
+
+/*
+ * The single event tag id for all stats logs.
+ * Keep this in sync with system/core/logcat/event.logtags
+ */
+const static int kStatsEventTag = 1937006964;
+
+static inline void statslog_init(android_log_context* log_ctx, bool* enable_stats_log) {
+ assert(log_ctx != NULL);
+ assert(enable_stats_log != NULL);
+ *enable_stats_log = property_get_bool("ro.lmk.log_stats", false);
+
+ if (*enable_stats_log) {
+ *log_ctx = create_android_logger(kStatsEventTag);
+ }
+}
+
+static inline void statslog_destroy(android_log_context* log_ctx) {
+ assert(log_ctx != NULL);
+ if (*log_ctx) {
+ android_log_destroy(log_ctx);
+ }
+}
+
+struct memory_stat {
+ int64_t pgfault;
+ int64_t pgmajfault;
+ int64_t rss_in_bytes;
+ int64_t cache_in_bytes;
+ int64_t swap_in_bytes;
+};
+
+#define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%u/memory.stat"
+
+/**
+ * Logs the change in LMKD state which is used as start/stop boundaries for logging
+ * LMK_KILL_OCCURRED event.
+ * Code: LMK_STATE_CHANGED = 54
+ */
+int
+stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t state);
+
+/**
+ * Logs the event when LMKD kills a process to reduce memory pressure.
+ * Code: LMK_KILL_OCCURRED = 51
+ */
+int
+stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
+ char const* process_name, int32_t oom_score, int64_t pgfault,
+ int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
+ int64_t swap_in_bytes);
+
+__END_DECLS
+
+#endif /* _STATSLOG_H_ */
diff --git a/lmkd/tests/Android.bp b/lmkd/tests/Android.bp
new file mode 100644
index 0000000..cbf44e9
--- /dev/null
+++ b/lmkd/tests/Android.bp
@@ -0,0 +1,40 @@
+// Copyright (C) 2018 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.
+
+cc_test {
+ name: "lmkd_unit_test",
+
+ shared_libs: [
+ "libbase",
+ "liblog",
+ ],
+
+ static_libs: [
+ "liblmkd_utils",
+ ],
+
+ target: {
+ android: {
+ srcs: ["lmkd_test.cpp"],
+ },
+ },
+
+ cflags: [
+ "-Wall",
+ "-Wextra",
+ "-Werror",
+ ],
+
+ compile_multilib: "first",
+}
diff --git a/lmkd/tests/lmkd_test.cpp b/lmkd/tests/lmkd_test.cpp
new file mode 100644
index 0000000..1996bae
--- /dev/null
+++ b/lmkd/tests/lmkd_test.cpp
@@ -0,0 +1,374 @@
+/*
+ * Copyright (C) 2018 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 <sstream>
+#include <stdio.h>
+#include <string.h>
+#include <string>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+#include <gtest/gtest.h>
+#include <lmkd.h>
+#include <liblmkd_utils.h>
+#include <log/log_properties.h>
+#include <private/android_filesystem_config.h>
+
+using namespace android::base;
+
+#define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree"
+#define LMKDTEST_RESPAWN_FLAG "LMKDTEST_RESPAWN"
+
+#define LMKD_LOGCAT_MARKER "lowmemorykiller"
+#define LMKD_KILL_MARKER_TEMPLATE LMKD_LOGCAT_MARKER ": Killing '%s'"
+#define OOM_MARKER "Out of memory"
+#define OOM_KILL_MARKER "Killed process"
+#define MIN_LOG_SIZE 100
+
+#define ONE_MB (1 << 20)
+
+/* Test constant parameters */
+#define OOM_ADJ_MAX 1000
+#define OOM_ADJ_MIN 0
+#define OOM_ADJ_STEP 100
+#define STEP_COUNT ((OOM_ADJ_MAX - OOM_ADJ_MIN) / OOM_ADJ_STEP + 1)
+
+#define ALLOC_STEP (ONE_MB)
+#define ALLOC_DELAY 1000
+
+/* Utility functions */
+std::string readCommand(const std::string& command) {
+ FILE* fp = popen(command.c_str(), "r");
+ std::string content;
+ ReadFdToString(fileno(fp), &content);
+ pclose(fp);
+ return content;
+}
+
+std::string readLogcat(const std::string& marker) {
+ std::string content = readCommand("logcat -d -b all");
+ size_t pos = content.find(marker);
+ if (pos == std::string::npos) return "";
+ content.erase(0, pos);
+ return content;
+}
+
+bool writeFile(const std::string& file, const std::string& string) {
+ if (getuid() == static_cast<unsigned>(AID_ROOT)) {
+ return WriteStringToFile(string, file);
+ }
+ return string == readCommand(
+ "echo -n '" + string + "' | su root tee " + file + " 2>&1");
+}
+
+bool writeKmsg(const std::string& marker) {
+ return writeFile("/dev/kmsg", marker);
+}
+
+std::string getTextAround(const std::string& text, size_t pos,
+ size_t lines_before, size_t lines_after) {
+ size_t start_pos = pos;
+
+ // find start position
+ // move up lines_before number of lines
+ while (lines_before > 0 &&
+ (start_pos = text.rfind('\n', start_pos)) != std::string::npos) {
+ lines_before--;
+ }
+ // move to the beginning of the line
+ start_pos = text.rfind('\n', start_pos);
+ start_pos = (start_pos == std::string::npos) ? 0 : start_pos + 1;
+
+ // find end position
+ // move down lines_after number of lines
+ while (lines_after > 0 &&
+ (pos = text.find('\n', pos)) != std::string::npos) {
+ pos++;
+ lines_after--;
+ }
+ return text.substr(start_pos, (pos == std::string::npos) ?
+ std::string::npos : pos - start_pos);
+}
+
+bool getExecPath(std::string &path) {
+ char buf[PATH_MAX + 1];
+ int ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
+ if (ret < 0) {
+ return false;
+ }
+ buf[ret] = '\0';
+ path = buf;
+ return true;
+}
+
+/* Child synchronization primitives */
+#define STATE_INIT 0
+#define STATE_CHILD_READY 1
+#define STATE_PARENT_READY 2
+
+struct state_sync {
+ pthread_mutex_t mutex;
+ pthread_cond_t condition;
+ int state;
+};
+
+struct state_sync * init_state_sync_obj() {
+ struct state_sync *ssync;
+
+ ssync = (struct state_sync*)mmap(NULL, sizeof(struct state_sync),
+ PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
+ if (ssync == MAP_FAILED) {
+ return NULL;
+ }
+
+ pthread_mutexattr_t mattr;
+ pthread_mutexattr_init(&mattr);
+ pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
+ pthread_mutex_init(&ssync->mutex, &mattr);
+
+ pthread_condattr_t cattr;
+ pthread_condattr_init(&cattr);
+ pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
+ pthread_cond_init(&ssync->condition, &cattr);
+
+ ssync->state = STATE_INIT;
+ return ssync;
+}
+
+void destroy_state_sync_obj(struct state_sync *ssync) {
+ pthread_cond_destroy(&ssync->condition);
+ pthread_mutex_destroy(&ssync->mutex);
+ munmap(ssync, sizeof(struct state_sync));
+}
+
+void signal_state(struct state_sync *ssync, int state) {
+ pthread_mutex_lock(&ssync->mutex);
+ ssync->state = state;
+ pthread_cond_signal(&ssync->condition);
+ pthread_mutex_unlock(&ssync->mutex);
+}
+
+void wait_for_state(struct state_sync *ssync, int state) {
+ pthread_mutex_lock(&ssync->mutex);
+ while (ssync->state != state) {
+ pthread_cond_wait(&ssync->condition, &ssync->mutex);
+ }
+ pthread_mutex_unlock(&ssync->mutex);
+}
+
+/* Memory allocation and data sharing */
+struct shared_data {
+ size_t allocated;
+ bool finished;
+ size_t total_size;
+ size_t step_size;
+ size_t step_delay;
+ int oomadj;
+};
+
+volatile void *gptr;
+void add_pressure(struct shared_data *data) {
+ volatile void *ptr;
+ size_t allocated_size = 0;
+
+ data->finished = false;
+ while (allocated_size < data->total_size) {
+ ptr = mmap(NULL, data->step_size, PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+ if (ptr != MAP_FAILED) {
+ /* create ptr aliasing to prevent compiler optimizing the access */
+ gptr = ptr;
+ /* make data non-zero */
+ memset((void*)ptr, (int)(allocated_size + 1), data->step_size);
+ allocated_size += data->step_size;
+ data->allocated = allocated_size;
+ }
+ usleep(data->step_delay);
+ }
+ data->finished = (allocated_size >= data->total_size);
+}
+
+/* Memory stress test main body */
+void runMemStressTest() {
+ struct shared_data *data;
+ struct state_sync *ssync;
+ int sock;
+ pid_t pid;
+ uid_t uid = getuid();
+
+ // check if in-kernel LMK driver is present
+ if (!access(INKERNEL_MINFREE_PATH, W_OK)) {
+ GTEST_LOG_(INFO) << "Must not have kernel lowmemorykiller driver,"
+ << " terminating test";
+ return;
+ }
+
+ ASSERT_FALSE((sock = lmkd_connect()) < 0)
+ << "Failed to connect to lmkd process, err=" << strerror(errno);
+
+ /* allocate shared memory to communicate params with a child */
+ data = (struct shared_data*)mmap(NULL, sizeof(struct shared_data),
+ PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
+ ASSERT_FALSE(data == MAP_FAILED) << "Memory allocation failure";
+ data->total_size = (size_t)-1; /* allocate until killed */
+ data->step_size = ALLOC_STEP;
+ data->step_delay = ALLOC_DELAY;
+
+ /* allocate state sync object */
+ ASSERT_FALSE((ssync = init_state_sync_obj()) == NULL)
+ << "Memory allocation failure";
+
+ /* run the test gradually decreasing oomadj */
+ data->oomadj = OOM_ADJ_MAX;
+ while (data->oomadj >= OOM_ADJ_MIN) {
+ ASSERT_FALSE((pid = fork()) < 0)
+ << "Failed to spawn a child process, err=" << strerror(errno);
+ if (pid != 0) {
+ /* Parent */
+ struct lmk_procprio params;
+ /* wait for child to start and get ready */
+ wait_for_state(ssync, STATE_CHILD_READY);
+ params.pid = pid;
+ params.uid = uid;
+ params.oomadj = data->oomadj;
+ ASSERT_FALSE(lmkd_register_proc(sock, ¶ms) < 0)
+ << "Failed to communicate with lmkd, err=" << strerror(errno);
+ // signal the child it can proceed
+ signal_state(ssync, STATE_PARENT_READY);
+ waitpid(pid, NULL, 0);
+ if (data->finished) {
+ GTEST_LOG_(INFO) << "Child [pid=" << pid << "] allocated "
+ << data->allocated / ONE_MB << "MB";
+ } else {
+ GTEST_LOG_(INFO) << "Child [pid=" << pid << "] allocated "
+ << data->allocated / ONE_MB
+ << "MB before being killed";
+ }
+ data->oomadj -= OOM_ADJ_STEP;
+ } else {
+ /* Child */
+ pid = getpid();
+ GTEST_LOG_(INFO) << "Child [pid=" << pid
+ << "] is running at oomadj="
+ << data->oomadj;
+ data->allocated = 0;
+ data->finished = false;
+ ASSERT_FALSE(create_memcg(uid, pid) != 0)
+ << "Child [pid=" << pid << "] failed to create a cgroup";
+ signal_state(ssync, STATE_CHILD_READY);
+ wait_for_state(ssync, STATE_PARENT_READY);
+ add_pressure(data);
+ /* should not reach here, child should be killed by OOM/LMK */
+ FAIL() << "Child [pid=" << pid << "] was not killed";
+ break;
+ }
+ }
+ destroy_state_sync_obj(ssync);
+ munmap(data, sizeof(struct shared_data));
+ close(sock);
+}
+
+TEST(lmkd, check_for_oom) {
+ // test requirements
+ // userdebug build
+ if (!__android_log_is_debuggable()) {
+ GTEST_LOG_(INFO) << "Must be userdebug build, terminating test";
+ return;
+ }
+
+ // if respawned test process then run the test and exit (no analysis)
+ if (getenv(LMKDTEST_RESPAWN_FLAG) != NULL) {
+ runMemStressTest();
+ return;
+ }
+
+ // Main test process
+ // mark the beginning of the test
+ std::string marker = StringPrintf(
+ "LMKD test start %lu\n", static_cast<unsigned long>(time(nullptr)));
+ ASSERT_TRUE(writeKmsg(marker));
+
+ // get executable complete path
+ std::string test_path;
+ ASSERT_TRUE(getExecPath(test_path));
+
+ std::string test_output;
+ if (getuid() != static_cast<unsigned>(AID_ROOT)) {
+ // if not root respawn itself as root and capture output
+ std::string command = StringPrintf(
+ "%s=true su root %s 2>&1", LMKDTEST_RESPAWN_FLAG,
+ test_path.c_str());
+ std::string test_output = readCommand(command);
+ GTEST_LOG_(INFO) << test_output;
+ } else {
+ // main test process is root, run the test
+ runMemStressTest();
+ }
+
+ // Analyze results
+ // capture logcat containind kernel logs
+ std::string logcat_out = readLogcat(marker);
+
+ // 1. extract LMKD kills from logcat output, count kills
+ std::stringstream kill_logs;
+ int hit_count = 0;
+ size_t pos = 0;
+ marker = StringPrintf(LMKD_KILL_MARKER_TEMPLATE, test_path.c_str());
+
+ while (true) {
+ if ((pos = logcat_out.find(marker, pos)) != std::string::npos) {
+ kill_logs << getTextAround(logcat_out, pos, 0, 1);
+ pos += marker.length();
+ hit_count++;
+ } else {
+ break;
+ }
+ }
+ GTEST_LOG_(INFO) << "====Logged kills====" << std::endl
+ << kill_logs.str();
+ EXPECT_TRUE(hit_count == STEP_COUNT) << "Number of kills " << hit_count
+ << " is less than expected "
+ << STEP_COUNT;
+
+ // 2. check kernel logs for OOM kills
+ pos = logcat_out.find(OOM_MARKER);
+ bool oom_detected = (pos != std::string::npos);
+ bool oom_kill_detected = (oom_detected &&
+ logcat_out.find(OOM_KILL_MARKER, pos) != std::string::npos);
+
+ EXPECT_FALSE(oom_kill_detected) << "OOM kill is detected!";
+ if (oom_detected || oom_kill_detected) {
+ // capture logcat with logs around all OOMs
+ pos = 0;
+ while ((pos = logcat_out.find(OOM_MARKER, pos)) != std::string::npos) {
+ GTEST_LOG_(INFO) << "====Logs around OOM====" << std::endl
+ << getTextAround(logcat_out, pos,
+ MIN_LOG_SIZE / 2, MIN_LOG_SIZE / 2);
+ pos += strlen(OOM_MARKER);
+ }
+ }
+
+ // output complete logcat with kernel (might get truncated)
+ GTEST_LOG_(INFO) << "====Complete logcat output====" << std::endl
+ << logcat_out;
+}
+
diff --git a/logcat/.clang-format b/logcat/.clang-format
deleted file mode 100644
index 393c309..0000000
--- a/logcat/.clang-format
+++ /dev/null
@@ -1,11 +0,0 @@
-BasedOnStyle: Google
-AllowShortFunctionsOnASingleLine: false
-
-CommentPragmas: NOLINT:.*
-DerivePointerAlignment: false
-IndentWidth: 4
-PointerAlignment: Left
-TabWidth: 4
-PenaltyExcessCharacter: 32
-
-Cpp11BracedListStyle: false
diff --git a/logcat/Android.bp b/logcat/Android.bp
index 01beb53..b0563a6 100644
--- a/logcat/Android.bp
+++ b/logcat/Android.bp
@@ -1,5 +1,5 @@
//
-// Copyright (C) 2006-2017 The Android Open Source Project
+// Copyright (C) 2006 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.
@@ -31,25 +31,13 @@
logtags: ["event.logtags"],
}
-cc_library {
- name: "liblogcat",
-
- defaults: ["logcat_defaults"],
- srcs: [
- "logcat.cpp",
- "getopt_long.cpp",
- "logcat_system.cpp",
- ],
- export_include_dirs: ["include"],
-}
-
cc_binary {
name: "logcat",
defaults: ["logcat_defaults"],
- shared_libs: ["liblogcat"],
srcs: [
"logcat_main.cpp",
+ "logcat.cpp",
],
}
@@ -57,9 +45,9 @@
name: "logcatd",
defaults: ["logcat_defaults"],
- shared_libs: ["liblogcat"],
srcs: [
"logcatd_main.cpp",
+ "logcat.cpp",
],
}
diff --git a/logcat/event.logtags b/logcat/event.logtags
index 0983676..da8d2d4 100644
--- a/logcat/event.logtags
+++ b/logcat/event.logtags
@@ -67,8 +67,9 @@
# ZygoteInit class preloading ends:
3030 boot_progress_preload_end (time|2|3)
-# Dalvik VM
+# Dalvik VM / ART
20003 dvm_lock_sample (process|3),(main|1|5),(thread|3),(time|1|3),(file|3),(line|1|5),(ownerfile|3),(ownerline|1|5),(sample_percent|1|6)
+20004 art_hidden_api_access (access_method|1),(flags|1),(class|3),(member|3),(type_signature|3)
75000 sqlite_mem_alarm_current (current|1|2)
75001 sqlite_mem_alarm_max (max|1|2)
@@ -112,6 +113,9 @@
# graphics timestamp
# 60100 - 60199 reserved for surfaceflinger
+# audio
+# 61000 - 61199 reserved for audioserver
+
# 0 for screen off, 1 for screen on, 2 for key-guard done
70000 screen_toggled (screen_state|1|5)
@@ -119,6 +123,9 @@
70200 aggregation (aggregation time|2|3)
70201 aggregation_test (field1|1|2),(field2|1|2),(field3|1|2),(field4|1|2),(field5|1|2)
+# gms refuses to register this log tag, b/30156345
+70220 gms_unknown
+
# libc failure logging
80100 bionic_event_memcpy_buffer_overflow (uid|1)
80105 bionic_event_strcat_buffer_overflow (uid|1)
diff --git a/logcat/getopt_long.cpp b/logcat/getopt_long.cpp
deleted file mode 100644
index da99906..0000000
--- a/logcat/getopt_long.cpp
+++ /dev/null
@@ -1,401 +0,0 @@
-/* $OpenBSD: getopt_long.c,v 1.26 2013/06/08 22:47:56 millert Exp $ */
-/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
-
-/*
- * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- * Sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F39502-99-1-0512.
- */
-/*-
- * Copyright (c) 2000 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Dieter Baron and Thomas Klausner.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/cdefs.h>
-
-#include <log/getopt.h>
-
-#define PRINT_ERROR ((context->opterr) && (*options != ':'))
-
-#define FLAG_PERMUTE 0x01 // permute non-options to the end of argv
-#define FLAG_ALLARGS 0x02 // treat non-options as args to option "-1"
-
-// return values
-#define BADCH (int)'?'
-#define BADARG ((*options == ':') ? (int)':' : (int)'?')
-#define INORDER (int)1
-
-#define D_PREFIX 0
-#define DD_PREFIX 1
-#define W_PREFIX 2
-
-// Compute the greatest common divisor of a and b.
-static int gcd(int a, int b) {
- int c = a % b;
- while (c) {
- a = b;
- b = c;
- c = a % b;
- }
- return b;
-}
-
-// Exchange the block from nonopt_start to nonopt_end with the block from
-// nonopt_end to opt_end (keeping the same order of arguments in each block).
-// Returns optind - (nonopt_end - nonopt_start) for convenience.
-static int permute_args(getopt_context* context, char* const* nargv) {
- // compute lengths of blocks and number and size of cycles
- int nnonopts = context->nonopt_end - context->nonopt_start;
- int nopts = context->optind - context->nonopt_end;
- int ncycle = gcd(nnonopts, nopts);
- int cyclelen = (context->optind - context->nonopt_start) / ncycle;
-
- for (int i = 0; i < ncycle; i++) {
- int cstart = context->nonopt_end + i;
- int pos = cstart;
- for (int j = 0; j < cyclelen; j++) {
- if (pos >= context->nonopt_end) {
- pos -= nnonopts;
- } else {
- pos += nopts;
- }
- char* swap = nargv[pos];
- const_cast<char**>(nargv)[pos] = nargv[cstart];
- const_cast<char**>(nargv)[cstart] = swap;
- }
- }
- return context->optind - (context->nonopt_end - context->nonopt_start);
-}
-
-// parse_long_options_r --
-// Parse long options in argc/argv argument vector.
-// Returns -1 if short_too is set and the option does not match long_options.
-static int parse_long_options_r(char* const* nargv, const char* options,
- const struct option* long_options, int* idx,
- bool short_too, struct getopt_context* context) {
- const char* current_argv = context->place;
- const char* current_dash;
- switch (context->dash_prefix) {
- case D_PREFIX:
- current_dash = "-";
- break;
- case DD_PREFIX:
- current_dash = "--";
- break;
- case W_PREFIX:
- current_dash = "-W ";
- break;
- default:
- current_dash = "";
- break;
- }
- context->optind++;
-
- const char* has_equal;
- size_t current_argv_len;
- if (!!(has_equal = strchr(current_argv, '='))) {
- // argument found (--option=arg)
- current_argv_len = has_equal - current_argv;
- has_equal++;
- } else {
- current_argv_len = strlen(current_argv);
- }
-
- int match = -1;
- bool exact_match = false;
- bool second_partial_match = false;
- for (int i = 0; long_options[i].name; i++) {
- // find matching long option
- if (strncmp(current_argv, long_options[i].name, current_argv_len)) {
- continue;
- }
-
- if (strlen(long_options[i].name) == current_argv_len) {
- // exact match
- match = i;
- exact_match = true;
- break;
- }
- // If this is a known short option, don't allow
- // a partial match of a single character.
- if (short_too && current_argv_len == 1) continue;
-
- if (match == -1) { // first partial match
- match = i;
- } else if (long_options[i].has_arg != long_options[match].has_arg ||
- long_options[i].flag != long_options[match].flag ||
- long_options[i].val != long_options[match].val) {
- second_partial_match = true;
- }
- }
- if (!exact_match && second_partial_match) {
- // ambiguous abbreviation
- if (PRINT_ERROR) {
- fprintf(context->optstderr ?: stderr,
- "option `%s%.*s' is ambiguous", current_dash,
- (int)current_argv_len, current_argv);
- }
- context->optopt = 0;
- return BADCH;
- }
- if (match != -1) { // option found
- if (long_options[match].has_arg == no_argument && has_equal) {
- if (PRINT_ERROR) {
- fprintf(context->optstderr ?: stderr,
- "option `%s%.*s' doesn't allow an argument",
- current_dash, (int)current_argv_len, current_argv);
- }
- // XXX: GNU sets optopt to val regardless of flag
- context->optopt =
- long_options[match].flag ? 0 : long_options[match].val;
- return BADCH;
- }
- if (long_options[match].has_arg == required_argument ||
- long_options[match].has_arg == optional_argument) {
- if (has_equal) {
- context->optarg = has_equal;
- } else if (long_options[match].has_arg == required_argument) {
- // optional argument doesn't use next nargv
- context->optarg = nargv[context->optind++];
- }
- }
- if ((long_options[match].has_arg == required_argument) &&
- !context->optarg) {
- // Missing argument; leading ':' indicates no error
- // should be generated.
- if (PRINT_ERROR) {
- fprintf(context->optstderr ?: stderr,
- "option `%s%s' requires an argument", current_dash,
- current_argv);
- }
- // XXX: GNU sets optopt to val regardless of flag
- context->optopt =
- long_options[match].flag ? 0 : long_options[match].val;
- context->optind--;
- return BADARG;
- }
- } else { // unknown option
- if (short_too) {
- context->optind--;
- return -1;
- }
- if (PRINT_ERROR) {
- fprintf(context->optstderr ?: stderr, "unrecognized option `%s%s'",
- current_dash, current_argv);
- }
- context->optopt = 0;
- return BADCH;
- }
- if (idx) *idx = match;
- if (long_options[match].flag) {
- *long_options[match].flag = long_options[match].val;
- return 0;
- }
- return long_options[match].val;
-}
-
-// getopt_long_r --
-// Parse argc/argv argument vector.
-int getopt_long_r(int nargc, char* const* nargv, const char* options,
- const struct option* long_options, int* idx,
- struct getopt_context* context) {
- if (!options) return -1;
-
- // XXX Some GNU programs (like cvs) set optind to 0 instead of
- // XXX using optreset. Work around this braindamage.
- if (!context->optind) context->optind = context->optreset = 1;
-
- // Disable GNU extensions if options string begins with a '+'.
- int flags = FLAG_PERMUTE;
- if (*options == '-') {
- flags |= FLAG_ALLARGS;
- } else if (*options == '+') {
- flags &= ~FLAG_PERMUTE;
- }
- if (*options == '+' || *options == '-') options++;
-
- context->optarg = nullptr;
- if (context->optreset) context->nonopt_start = context->nonopt_end = -1;
-start:
- if (context->optreset || !*context->place) { // update scanning pointer
- context->optreset = 0;
- if (context->optind >= nargc) { // end of argument vector
- context->place = EMSG;
- if (context->nonopt_end != -1) {
- // do permutation, if we have to
- context->optind = permute_args(context, nargv);
- } else if (context->nonopt_start != -1) {
- // If we skipped non-options, set optind to the first of them.
- context->optind = context->nonopt_start;
- }
- context->nonopt_start = context->nonopt_end = -1;
- return -1;
- }
- if (*(context->place = nargv[context->optind]) != '-' ||
- context->place[1] == '\0') {
- context->place = EMSG; // found non-option
- if (flags & FLAG_ALLARGS) {
- // GNU extension: return non-option as argument to option 1
- context->optarg = nargv[context->optind++];
- return INORDER;
- }
- if (!(flags & FLAG_PERMUTE)) {
- // If no permutation wanted, stop parsing at first non-option.
- return -1;
- }
- // do permutation
- if (context->nonopt_start == -1) {
- context->nonopt_start = context->optind;
- } else if (context->nonopt_end != -1) {
- context->nonopt_start = permute_args(context, nargv);
- context->nonopt_end = -1;
- }
- context->optind++;
- // process next argument
- goto start;
- }
- if (context->nonopt_start != -1 && context->nonopt_end == -1) {
- context->nonopt_end = context->optind;
- }
-
- // If we have "-" do nothing, if "--" we are done.
- if (context->place[1] != '\0' && *++(context->place) == '-' &&
- context->place[1] == '\0') {
- context->optind++;
- context->place = EMSG;
- // We found an option (--), so if we skipped
- // non-options, we have to permute.
- if (context->nonopt_end != -1) {
- context->optind = permute_args(context, nargv);
- }
- context->nonopt_start = context->nonopt_end = -1;
- return -1;
- }
- }
-
- int optchar;
- // Check long options if:
- // 1) we were passed some
- // 2) the arg is not just "-"
- // 3) either the arg starts with -- we are getopt_long_only()
- if (long_options && context->place != nargv[context->optind] &&
- (*context->place == '-')) {
- bool short_too = false;
- context->dash_prefix = D_PREFIX;
- if (*context->place == '-') {
- context->place++; // --foo long option
- context->dash_prefix = DD_PREFIX;
- } else if (*context->place != ':' && strchr(options, *context->place)) {
- short_too = true; // could be short option too
- }
-
- optchar = parse_long_options_r(nargv, options, long_options, idx,
- short_too, context);
- if (optchar != -1) {
- context->place = EMSG;
- return optchar;
- }
- }
-
- const char* oli; // option letter list index
- if ((optchar = (int)*(context->place)++) == (int)':' ||
- (optchar == (int)'-' && *context->place != '\0') ||
- !(oli = strchr(options, optchar))) {
- // If the user specified "-" and '-' isn't listed in
- // options, return -1 (non-option) as per POSIX.
- // Otherwise, it is an unknown option character (or ':').
- if (optchar == (int)'-' && *context->place == '\0') return -1;
- if (!*context->place) context->optind++;
- if (PRINT_ERROR) {
- fprintf(context->optstderr ?: stderr, "invalid option -- %c",
- optchar);
- }
- context->optopt = optchar;
- return BADCH;
- }
-
- static const char recargchar[] = "option requires an argument -- %c";
- if (long_options && optchar == 'W' && oli[1] == ';') {
- // -W long-option
- if (*context->place) { // no space
- ; // NOTHING
- } else if (++(context->optind) >= nargc) { // no arg
- context->place = EMSG;
- if (PRINT_ERROR) {
- fprintf(context->optstderr ?: stderr, recargchar, optchar);
- }
- context->optopt = optchar;
- return BADARG;
- } else { // white space
- context->place = nargv[context->optind];
- }
- context->dash_prefix = W_PREFIX;
- optchar = parse_long_options_r(nargv, options, long_options, idx, false,
- context);
- context->place = EMSG;
- return optchar;
- }
- if (*++oli != ':') { // doesn't take argument
- if (!*context->place) context->optind++;
- } else { // takes (optional) argument
- context->optarg = nullptr;
- if (*context->place) { // no white space
- context->optarg = context->place;
- } else if (oli[1] != ':') { // arg not optional
- if (++(context->optind) >= nargc) { // no arg
- context->place = EMSG;
- if (PRINT_ERROR) {
- fprintf(context->optstderr ?: stderr, recargchar, optchar);
- }
- context->optopt = optchar;
- return BADARG;
- }
- context->optarg = nargv[context->optind];
- }
- context->place = EMSG;
- context->optind++;
- }
- // dump back option letter
- return optchar;
-}
diff --git a/logcat/include/log/getopt.h b/logcat/include/log/getopt.h
deleted file mode 100644
index 0da2b10..0000000
--- a/logcat/include/log/getopt.h
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (C) 2017 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.
- */
-
-#ifndef _LOG_GETOPT_H_
-#define _LOG_GETOPT_H_
-
-#ifndef __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 1
-#elif __ANDROID_API__ > 24 /* > Nougat */
-#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE
-
-#include <getopt.h>
-#include <sys/cdefs.h>
-
-struct getopt_context {
- int opterr;
- int optind;
- int optopt;
- int optreset;
- const char* optarg;
- FILE* optstderr; /* NULL defaults to stderr */
- /* private */
- const char* place;
- int nonopt_start;
- int nonopt_end;
- int dash_prefix;
- /* expansion space */
- int __extra__;
- void* __stuff__;
-};
-
-#define EMSG ""
-#define NO_PREFIX (-1)
-
-#define INIT_GETOPT_CONTEXT(context) \
- context = { 1, 1, '?', 0, NULL, NULL, EMSG, -1, -1, NO_PREFIX, 0, NULL }
-
-__BEGIN_DECLS
-int getopt_long_r(int nargc, char* const* nargv, const char* options,
- const struct option* long_options, int* idx,
- struct getopt_context* context);
-
-__END_DECLS
-
-#endif /* __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE */
-
-#endif /* !_LOG_GETOPT_H_ */
diff --git a/logcat/include/log/logcat.h b/logcat/include/log/logcat.h
deleted file mode 100644
index 009672c..0000000
--- a/logcat/include/log/logcat.h
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- * Copyright (C) 2005-2017 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.
- */
-
-#ifndef _LIBS_LOGCAT_H /* header boilerplate */
-#define _LIBS_LOGCAT_H
-
-#include <stdio.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#ifndef __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 1
-#elif __ANDROID_API__ > 24 /* > Nougat */
-#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE
-
-/* For managing an in-process logcat function, rather than forking/execing
- *
- * It also serves as the basis for the logcat command.
- *
- * The following C API allows a logcat instance to be created, run
- * to completion, and then release all the associated resources.
- */
-
-/*
- * The opaque context
- */
-#ifndef __android_logcat_context_defined /* typedef boilerplate */
-#define __android_logcat_context_defined
-typedef struct android_logcat_context_internal* android_logcat_context;
-#endif
-
-/* Creates a context associated with this logcat instance
- *
- * Returns a pointer to the context, or a NULL on error.
- */
-android_logcat_context create_android_logcat();
-
-/* Collects and outputs the logcat data to output and error file descriptors
- *
- * Will block, performed in-thread and in-process
- *
- * The output file descriptor variable, if greater than or equal to 0, is
- * where the output (ie: stdout) will be sent. The file descriptor is closed
- * on android_logcat_destroy which terminates the instance, or when an -f flag
- * (output redirect to a file) is present in the command. The error file
- * descriptor variable, if greater than or equal to 0, is where the error
- * stream (ie: stderr) will be sent, also closed on android_logcat_destroy.
- * The error file descriptor can be set to equal to the output file descriptor,
- * which will mix output and error stream content, and will defer closure of
- * the file descriptor on -f flag redirection. Negative values for the file
- * descriptors will use stdout and stderr FILE references respectively
- * internally, and will not close the references as noted above.
- *
- * Return value is 0 for success, non-zero for errors.
- */
-int android_logcat_run_command(android_logcat_context ctx, int output, int error,
- int argc, char* const* argv, char* const* envp);
-
-/* Will not block, performed in-process
- *
- * Starts a thread, opens a pipe, returns reading end fd, saves off argv.
- * The command supports 2>&1 (mix content) and 2>/dev/null (drop content) for
- * scripted error (stderr) redirection.
- */
-int android_logcat_run_command_thread(android_logcat_context ctx, int argc,
- char* const* argv, char* const* envp);
-int android_logcat_run_command_thread_running(android_logcat_context ctx);
-
-/* Finished with context
- *
- * Kill the command thread ASAP (if any), and free up all associated resources.
- *
- * Return value is the result of the android_logcat_run_command, or
- * non-zero for any errors.
- */
-int android_logcat_destroy(android_logcat_context* ctx);
-
-/* derived helpers */
-
-/*
- * In-process thread that acts like somewhat like libc-like system and popen
- * respectively. Can not handle shell scripting, only pure calls to the
- * logcat operations. The android_logcat_system is a wrapper for the
- * create_android_logcat, android_logcat_run_command and android_logcat_destroy
- * API above. The android_logcat_popen is a wrapper for the
- * android_logcat_run_command_thread API above. The android_logcat_pclose is
- * a wrapper for a reasonable wait until output has subsided for command
- * completion, fclose on the FILE pointer and the android_logcat_destroy API.
- */
-int android_logcat_system(const char* command);
-FILE* android_logcat_popen(android_logcat_context* ctx, const char* command);
-int android_logcat_pclose(android_logcat_context* ctx, FILE* output);
-
-#endif /* __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _LIBS_LOGCAT_H */
diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp
index ff85f54..0f56337 100644
--- a/logcat/logcat.cpp
+++ b/logcat/logcat.cpp
@@ -14,12 +14,15 @@
* limitations under the License.
*/
+#include "logcat.h"
+
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
+#include <getopt.h>
#include <math.h>
#include <pthread.h>
#include <sched.h>
@@ -47,8 +50,6 @@
#include <cutils/sched_policy.h>
#include <cutils/sockets.h>
#include <log/event_tag_map.h>
-#include <log/getopt.h>
-#include <log/logcat.h>
#include <log/logprint.h>
#include <private/android_logger.h>
#include <system/thread_defs.h>
@@ -854,14 +855,8 @@
// net for stability dealing with possible mistaken inputs.
static const char delimiters[] = ",:; \t\n\r\f";
- struct getopt_context optctx;
- INIT_GETOPT_CONTEXT(optctx);
- optctx.opterr = !!context->error;
- optctx.optstderr = context->error;
-
- for (;;) {
- int ret;
-
+ optind = 0;
+ while (true) {
int option_index = 0;
// list of long-argument only strings for later comparison
static const char pid_str[] = "pid";
@@ -902,19 +897,18 @@
};
// clang-format on
- ret = getopt_long_r(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
- long_options, &option_index, &optctx);
- if (ret < 0) break;
+ int c = getopt_long(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
+ &option_index);
+ if (c == -1) break;
- switch (ret) {
+ switch (c) {
case 0:
// only long options
if (long_options[option_index].name == pid_str) {
// ToDo: determine runtime PID_MAX?
- if (!getSizeTArg(optctx.optarg, &pid, 1)) {
+ if (!getSizeTArg(optarg, &pid, 1)) {
logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
- long_options[option_index].name,
- optctx.optarg);
+ long_options[option_index].name, optarg);
goto exit;
}
break;
@@ -924,11 +918,9 @@
ANDROID_LOG_NONBLOCK;
// ToDo: implement API that supports setting a wrap timeout
size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
- if (optctx.optarg &&
- !getSizeTArg(optctx.optarg, &dummy, 1)) {
+ if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
- long_options[option_index].name,
- optctx.optarg);
+ long_options[option_index].name, optarg);
goto exit;
}
if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
@@ -949,8 +941,7 @@
break;
}
if (long_options[option_index].name == id_str) {
- setId = (optctx.optarg && optctx.optarg[0]) ? optctx.optarg
- : nullptr;
+ setId = (optarg && optarg[0]) ? optarg : nullptr;
}
break;
@@ -978,32 +969,27 @@
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
// FALLTHRU
case 'T':
- if (strspn(optctx.optarg, "0123456789") !=
- strlen(optctx.optarg)) {
- char* cp = parseTime(tail_time, optctx.optarg);
+ if (strspn(optarg, "0123456789") != strlen(optarg)) {
+ char* cp = parseTime(tail_time, optarg);
if (!cp) {
- logcat_panic(context, HELP_FALSE,
- "-%c \"%s\" not in time format\n", ret,
- optctx.optarg);
+ logcat_panic(context, HELP_FALSE, "-%c \"%s\" not in time format\n", c,
+ optarg);
goto exit;
}
if (*cp) {
- char c = *cp;
+ char ch = *cp;
*cp = '\0';
if (context->error) {
- fprintf(
- context->error,
- "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
- ret, optctx.optarg, c, cp + 1);
+ fprintf(context->error, "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
+ c, optarg, ch, cp + 1);
}
- *cp = c;
+ *cp = ch;
}
} else {
- if (!getSizeTArg(optctx.optarg, &tail_lines, 1)) {
+ if (!getSizeTArg(optarg, &tail_lines, 1)) {
if (context->error) {
- fprintf(context->error,
- "WARNING: -%c %s invalid, setting to 1\n",
- ret, optctx.optarg);
+ fprintf(context->error, "WARNING: -%c %s invalid, setting to 1\n", c,
+ optarg);
}
tail_lines = 1;
}
@@ -1015,21 +1001,19 @@
break;
case 'e':
- context->regex = new pcrecpp::RE(optctx.optarg);
+ context->regex = new pcrecpp::RE(optarg);
break;
case 'm': {
- if (!getSizeTArg(optctx.optarg, &context->maxCount)) {
+ if (!getSizeTArg(optarg, &context->maxCount)) {
logcat_panic(context, HELP_FALSE,
- "-%c \"%s\" isn't an "
- "integer greater than zero\n",
- ret, optctx.optarg);
+ "-%c \"%s\" isn't an integer greater than zero\n", c, optarg);
goto exit;
}
} break;
case 'g':
- if (!optctx.optarg) {
+ if (!optarg) {
getLogSize = true;
break;
}
@@ -1037,8 +1021,8 @@
case 'G': {
char* cp;
- if (strtoll(optctx.optarg, &cp, 0) > 0) {
- setLogSize = strtoll(optctx.optarg, &cp, 0);
+ if (strtoll(optarg, &cp, 0) > 0) {
+ setLogSize = strtoll(optarg, &cp, 0);
} else {
setLogSize = 0;
}
@@ -1071,19 +1055,18 @@
} break;
case 'p':
- if (!optctx.optarg) {
+ if (!optarg) {
getPruneList = true;
break;
}
// FALLTHRU
case 'P':
- setPruneList = optctx.optarg;
+ setPruneList = optarg;
break;
case 'b': {
- std::unique_ptr<char, void (*)(void*)> buffers(
- strdup(optctx.optarg), free);
+ std::unique_ptr<char, void (*)(void*)> buffers(strdup(optarg), free);
char* arg = buffers.get();
unsigned idMask = 0;
char* sv = nullptr; // protect against -ENOMEM above
@@ -1147,40 +1130,33 @@
case 'f':
if ((tail_time == log_time::EPOCH) && !tail_lines) {
- tail_time = lastLogTime(optctx.optarg);
+ tail_time = lastLogTime(optarg);
}
// redirect output to a file
- context->outputFileName = optctx.optarg;
+ context->outputFileName = optarg;
break;
case 'r':
- if (!getSizeTArg(optctx.optarg, &context->logRotateSizeKBytes,
- 1)) {
- logcat_panic(context, HELP_TRUE,
- "Invalid parameter \"%s\" to -r\n",
- optctx.optarg);
+ if (!getSizeTArg(optarg, &context->logRotateSizeKBytes, 1)) {
+ logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -r\n", optarg);
goto exit;
}
break;
case 'n':
- if (!getSizeTArg(optctx.optarg, &context->maxRotatedLogs, 1)) {
- logcat_panic(context, HELP_TRUE,
- "Invalid parameter \"%s\" to -n\n",
- optctx.optarg);
+ if (!getSizeTArg(optarg, &context->maxRotatedLogs, 1)) {
+ logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -n\n", optarg);
goto exit;
}
break;
case 'v': {
- if (!strcmp(optctx.optarg, "help") ||
- !strcmp(optctx.optarg, "--help")) {
+ if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
show_format_help(context);
context->retval = EXIT_SUCCESS;
goto exit;
}
- std::unique_ptr<char, void (*)(void*)> formats(
- strdup(optctx.optarg), free);
+ std::unique_ptr<char, void (*)(void*)> formats(strdup(optarg), free);
char* arg = formats.get();
char* sv = nullptr; // protect against -ENOMEM above
while (!!(arg = strtok_r(arg, delimiters, &sv))) {
@@ -1300,8 +1276,7 @@
break;
case ':':
- logcat_panic(context, HELP_TRUE,
- "Option -%c needs an argument\n", optctx.optopt);
+ logcat_panic(context, HELP_TRUE, "Option -%c needs an argument\n", optopt);
goto exit;
case 'h':
@@ -1310,8 +1285,7 @@
goto exit;
default:
- logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n",
- optctx.optopt);
+ logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n", optopt);
goto exit;
}
}
@@ -1400,7 +1374,7 @@
"Invalid filter expression in logcat args\n");
goto exit;
}
- } else if (argc == optctx.optind) {
+ } else if (argc == optind) {
// Add from environment variable
const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
@@ -1416,7 +1390,7 @@
}
} else {
// Add from commandline
- for (int i = optctx.optind ; i < argc ; i++) {
+ for (int i = optind ; i < argc ; i++) {
// skip stderr redirections of _all_ kinds
if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
// skip stdout redirections of _all_ kinds
@@ -1707,105 +1681,6 @@
return __logcat(context);
}
-// starts a thread, opens a pipe, returns reading end.
-int android_logcat_run_command_thread(android_logcat_context ctx,
- int argc, char* const* argv,
- char* const* envp) {
- android_logcat_context_internal* context = ctx;
-
- int save_errno = EBUSY;
- if ((context->fds[0] >= 0) || (context->fds[1] >= 0)) goto exit;
-
- if (pipe(context->fds) < 0) {
- save_errno = errno;
- goto exit;
- }
-
- pthread_attr_t attr;
- if (pthread_attr_init(&attr)) {
- save_errno = errno;
- goto close_exit;
- }
-
- struct sched_param param;
- memset(¶m, 0, sizeof(param));
- pthread_attr_setschedparam(&attr, ¶m);
- pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
- if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
- save_errno = errno;
- goto pthread_attr_exit;
- }
-
- context->stop = false;
- context->thread_stopped = false;
- context->output_fd = context->fds[1];
- // save off arguments so they remain while thread is active.
- for (int i = 0; i < argc; ++i) {
- context->args.push_back(std::string(argv[i]));
- }
- // save off environment so they remain while thread is active.
- if (envp) for (size_t i = 0; envp[i]; ++i) {
- context->envs.push_back(std::string(envp[i]));
- }
-
- for (auto& str : context->args) {
- context->argv_hold.push_back(str.c_str());
- }
- context->argv_hold.push_back(nullptr);
- for (auto& str : context->envs) {
- context->envp_hold.push_back(str.c_str());
- }
- context->envp_hold.push_back(nullptr);
-
- context->argc = context->argv_hold.size() - 1;
- context->argv = (char* const*)&context->argv_hold[0];
- context->envp = (char* const*)&context->envp_hold[0];
-
-#ifdef DEBUG
- fprintf(stderr, "argv[%d] = {", context->argc);
- for (auto str : context->argv_hold) {
- fprintf(stderr, " \"%s\"", str ?: "nullptr");
- }
- fprintf(stderr, " }\n");
- fflush(stderr);
-#endif
- context->retval = EXIT_SUCCESS;
- if (pthread_create(&context->thr, &attr,
- (void*(*)(void*))__logcat, context)) {
- save_errno = errno;
- goto argv_exit;
- }
- pthread_attr_destroy(&attr);
-
- return context->fds[0];
-
-argv_exit:
- context->argv_hold.clear();
- context->args.clear();
- context->envp_hold.clear();
- context->envs.clear();
-pthread_attr_exit:
- pthread_attr_destroy(&attr);
-close_exit:
- close(context->fds[0]);
- context->fds[0] = -1;
- close(context->fds[1]);
- context->fds[1] = -1;
-exit:
- errno = save_errno;
- context->stop = true;
- context->thread_stopped = true;
- context->retval = EXIT_FAILURE;
- return -1;
-}
-
-// test if the thread is still doing 'stuff'
-int android_logcat_run_command_thread_running(android_logcat_context ctx) {
- android_logcat_context_internal* context = ctx;
-
- return context->thread_stopped == false;
-}
-
// Finished with context
int android_logcat_destroy(android_logcat_context* ctx) {
android_logcat_context_internal* context = *ctx;
diff --git a/logcat/logcat.h b/logcat/logcat.h
new file mode 100644
index 0000000..85ed7da
--- /dev/null
+++ b/logcat/logcat.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2005-2017 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.
+ */
+
+#pragma once
+
+#include <stdio.h>
+
+/*
+ * The opaque context
+ */
+typedef struct android_logcat_context_internal* android_logcat_context;
+
+/* Creates a context associated with this logcat instance
+ *
+ * Returns a pointer to the context, or a NULL on error.
+ */
+android_logcat_context create_android_logcat();
+
+/* Collects and outputs the logcat data to output and error file descriptors
+ *
+ * Will block, performed in-thread and in-process
+ *
+ * The output file descriptor variable, if greater than or equal to 0, is
+ * where the output (ie: stdout) will be sent. The file descriptor is closed
+ * on android_logcat_destroy which terminates the instance, or when an -f flag
+ * (output redirect to a file) is present in the command. The error file
+ * descriptor variable, if greater than or equal to 0, is where the error
+ * stream (ie: stderr) will be sent, also closed on android_logcat_destroy.
+ * The error file descriptor can be set to equal to the output file descriptor,
+ * which will mix output and error stream content, and will defer closure of
+ * the file descriptor on -f flag redirection. Negative values for the file
+ * descriptors will use stdout and stderr FILE references respectively
+ * internally, and will not close the references as noted above.
+ *
+ * Return value is 0 for success, non-zero for errors.
+ */
+int android_logcat_run_command(android_logcat_context ctx, int output, int error, int argc,
+ char* const* argv, char* const* envp);
+
+/* Finished with context
+ *
+ * Kill the command thread ASAP (if any), and free up all associated resources.
+ *
+ * Return value is the result of the android_logcat_run_command, or
+ * non-zero for any errors.
+ */
+int android_logcat_destroy(android_logcat_context* ctx);
diff --git a/logcat/logcat_main.cpp b/logcat/logcat_main.cpp
index 9477e79..ecfa2ba 100644
--- a/logcat/logcat_main.cpp
+++ b/logcat/logcat_main.cpp
@@ -17,7 +17,7 @@
#include <signal.h>
#include <stdlib.h>
-#include <log/logcat.h>
+#include "logcat.h"
int main(int argc, char** argv, char** envp) {
android_logcat_context ctx = create_android_logcat();
diff --git a/logcat/logcat_system.cpp b/logcat/logcat_system.cpp
deleted file mode 100644
index 6dfd110..0000000
--- a/logcat/logcat_system.cpp
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * Copyright (C) 2017 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 <ctype.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <string>
-#include <vector>
-
-#include <log/logcat.h>
-
-static std::string unquote(const char*& cp, const char*& delim) {
- if ((*cp == '\'') || (*cp == '"')) {
- // KISS: Simple quotes. Do not handle the case
- // of concatenation like "blah"foo'bar'
- char quote = *cp++;
- delim = strchr(cp, quote);
- if (!delim) delim = cp + strlen(cp);
- std::string str(cp, delim);
- if (*delim) ++delim;
- return str;
- }
- delim = strpbrk(cp, " \t\f\r\n");
- if (!delim) delim = cp + strlen(cp);
- return std::string(cp, delim);
-}
-
-static bool __android_logcat_parse(const char* command,
- std::vector<std::string>& args,
- std::vector<std::string>& envs) {
- for (const char *delim, *cp = command; cp && *cp; cp = delim) {
- while (isspace(*cp)) ++cp;
- if ((args.size() == 0) && (*cp != '=') && !isdigit(*cp)) {
- const char* env = cp;
- while (isalnum(*cp) || (*cp == '_')) ++cp;
- if (cp && (*cp == '=')) {
- std::string str(env, ++cp);
- str += unquote(cp, delim);
- envs.push_back(str);
- continue;
- }
- cp = env;
- }
- args.push_back(unquote(cp, delim));
- if ((args.size() == 1) && (args[0] != "logcat") &&
- (args[0] != "/system/bin/logcat")) {
- return false;
- }
- }
- return args.size() != 0;
-}
-
-FILE* android_logcat_popen(android_logcat_context* ctx, const char* command) {
- *ctx = NULL;
-
- std::vector<std::string> args;
- std::vector<std::string> envs;
- if (!__android_logcat_parse(command, args, envs)) return NULL;
-
- std::vector<const char*> argv;
- for (auto& str : args) {
- argv.push_back(str.c_str());
- }
- argv.push_back(NULL);
-
- std::vector<const char*> envp;
- for (auto& str : envs) {
- envp.push_back(str.c_str());
- }
- envp.push_back(NULL);
-
- *ctx = create_android_logcat();
- if (!*ctx) return NULL;
-
- int fd = android_logcat_run_command_thread(
- *ctx, argv.size() - 1, (char* const*)&argv[0], (char* const*)&envp[0]);
- argv.clear();
- args.clear();
- envp.clear();
- envs.clear();
- if (fd < 0) {
- android_logcat_destroy(ctx);
- return NULL;
- }
-
- int duped = dup(fd);
- FILE* retval = fdopen(duped, "reb");
- if (!retval) {
- close(duped);
- android_logcat_destroy(ctx);
- }
- return retval;
-}
-
-int android_logcat_pclose(android_logcat_context* ctx, FILE* output) {
- if (*ctx) {
- static const useconds_t wait_sample = 20000;
- // Wait two seconds maximum
- for (size_t retry = ((2 * 1000000) + wait_sample - 1) / wait_sample;
- android_logcat_run_command_thread_running(*ctx) && retry; --retry) {
- usleep(wait_sample);
- }
- }
-
- if (output) fclose(output);
- return android_logcat_destroy(ctx);
-}
-
-int android_logcat_system(const char* command) {
- std::vector<std::string> args;
- std::vector<std::string> envs;
- if (!__android_logcat_parse(command, args, envs)) return -1;
-
- std::vector<const char*> argv;
- for (auto& str : args) {
- argv.push_back(str.c_str());
- }
- argv.push_back(NULL);
-
- std::vector<const char*> envp;
- for (auto& str : envs) {
- envp.push_back(str.c_str());
- }
- envp.push_back(NULL);
-
- android_logcat_context ctx = create_android_logcat();
- if (!ctx) return -1;
- /* Command return value */
- int retval = android_logcat_run_command(ctx, -1, -1, argv.size() - 1,
- (char* const*)&argv[0],
- (char* const*)&envp[0]);
- /* destroy return value */
- int ret = android_logcat_destroy(&ctx);
- /* Paranoia merging any discrepancies between the two return values */
- if (!ret) ret = retval;
- return ret;
-}
diff --git a/logcat/logcatd_main.cpp b/logcat/logcatd_main.cpp
index 9109eb1..c131846 100644
--- a/logcat/logcatd_main.cpp
+++ b/logcat/logcatd_main.cpp
@@ -21,7 +21,7 @@
#include <string>
#include <vector>
-#include <log/logcat.h>
+#include "logcat.h"
int main(int argc, char** argv, char** envp) {
android_logcat_context ctx = create_android_logcat();
diff --git a/logcat/tests/Android.mk b/logcat/tests/Android.mk
index defd3c4..66f6724 100644
--- a/logcat/tests/Android.mk
+++ b/logcat/tests/Android.mk
@@ -32,7 +32,6 @@
benchmark_src_files := \
logcat_benchmark.cpp \
- exec_benchmark.cpp \
# Build benchmarks for the device. Run with:
# adb shell /data/nativetest/logcat-benchmarks/logcat-benchmarks
@@ -41,7 +40,7 @@
LOCAL_MODULE_TAGS := $(test_tags)
LOCAL_CFLAGS += $(test_c_flags)
LOCAL_SRC_FILES := $(benchmark_src_files)
-LOCAL_SHARED_LIBRARIES := libbase liblogcat
+LOCAL_SHARED_LIBRARIES := libbase
include $(BUILD_NATIVE_BENCHMARK)
# -----------------------------------------------------------------------------
@@ -51,7 +50,6 @@
test_src_files := \
logcat_test.cpp \
logcatd_test.cpp \
- liblogcat_test.cpp \
# Build tests for the device (with .so). Run with:
# adb shell /data/nativetest/logcat-unit-tests/logcat-unit-tests
@@ -59,6 +57,6 @@
LOCAL_MODULE := $(test_module_prefix)unit-tests
LOCAL_MODULE_TAGS := $(test_tags)
LOCAL_CFLAGS += $(test_c_flags)
-LOCAL_SHARED_LIBRARIES := liblog libbase liblogcat
+LOCAL_SHARED_LIBRARIES := liblog libbase
LOCAL_SRC_FILES := $(test_src_files)
include $(BUILD_NATIVE_TEST)
diff --git a/logcat/tests/exec_benchmark.cpp b/logcat/tests/exec_benchmark.cpp
deleted file mode 100644
index c30a5f5..0000000
--- a/logcat/tests/exec_benchmark.cpp
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * Copyright (C) 2017 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 <stdio.h>
-
-#include <android-base/file.h>
-#include <benchmark/benchmark.h>
-#include <log/logcat.h>
-
-// Dump the statistics and report results
-
-static void logcat_popen_libc(benchmark::State& state, const char* cmd) {
- while (state.KeepRunning()) {
- FILE* fp = popen(cmd, "r");
- std::string ret;
- android::base::ReadFdToString(fileno(fp), &ret);
- pclose(fp);
- }
-}
-
-static void BM_logcat_stat_popen_libc(benchmark::State& state) {
- logcat_popen_libc(state, "logcat -b all -S");
-}
-BENCHMARK(BM_logcat_stat_popen_libc);
-
-static void logcat_popen_liblogcat(benchmark::State& state, const char* cmd) {
- while (state.KeepRunning()) {
- android_logcat_context ctx;
- FILE* fp = android_logcat_popen(&ctx, cmd);
- std::string ret;
- android::base::ReadFdToString(fileno(fp), &ret);
- android_logcat_pclose(&ctx, fp);
- }
-}
-
-static void BM_logcat_stat_popen_liblogcat(benchmark::State& state) {
- logcat_popen_liblogcat(state, "logcat -b all -S");
-}
-BENCHMARK(BM_logcat_stat_popen_liblogcat);
-
-static void logcat_system_libc(benchmark::State& state, const char* cmd) {
- while (state.KeepRunning()) {
- system(cmd);
- }
-}
-
-static void BM_logcat_stat_system_libc(benchmark::State& state) {
- logcat_system_libc(state, "logcat -b all -S >/dev/null 2>/dev/null");
-}
-BENCHMARK(BM_logcat_stat_system_libc);
-
-static void logcat_system_liblogcat(benchmark::State& state, const char* cmd) {
- while (state.KeepRunning()) {
- android_logcat_system(cmd);
- }
-}
-
-static void BM_logcat_stat_system_liblogcat(benchmark::State& state) {
- logcat_system_liblogcat(state, "logcat -b all -S >/dev/null 2>/dev/null");
-}
-BENCHMARK(BM_logcat_stat_system_liblogcat);
-
-// Dump the logs and report results
-
-static void BM_logcat_dump_popen_libc(benchmark::State& state) {
- logcat_popen_libc(state, "logcat -b all -d");
-}
-BENCHMARK(BM_logcat_dump_popen_libc);
-
-static void BM_logcat_dump_popen_liblogcat(benchmark::State& state) {
- logcat_popen_liblogcat(state, "logcat -b all -d");
-}
-BENCHMARK(BM_logcat_dump_popen_liblogcat);
-
-static void BM_logcat_dump_system_libc(benchmark::State& state) {
- logcat_system_libc(state, "logcat -b all -d >/dev/null 2>/dev/null");
-}
-BENCHMARK(BM_logcat_dump_system_libc);
-
-static void BM_logcat_dump_system_liblogcat(benchmark::State& state) {
- logcat_system_liblogcat(state, "logcat -b all -d >/dev/null 2>/dev/null");
-}
-BENCHMARK(BM_logcat_dump_system_liblogcat);
diff --git a/logcat/tests/liblogcat_test.cpp b/logcat/tests/liblogcat_test.cpp
deleted file mode 100644
index c8a00da..0000000
--- a/logcat/tests/liblogcat_test.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (C) 2017 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 <log/logcat.h>
-
-#define logcat_define(context) android_logcat_context context
-#define logcat_popen(context, command) android_logcat_popen(&(context), command)
-#define logcat_pclose(context, fp) android_logcat_pclose(&(context), fp)
-#define logcat_system(command) android_logcat_system(command)
-#define logcat liblogcat
-
-#include "logcat_test.cpp"
diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp
index 786fb14..cc1632a 100644
--- a/logcat/tests/logcat_test.cpp
+++ b/logcat/tests/logcat_test.cpp
@@ -37,12 +37,6 @@
#include <log/log.h>
#include <log/log_event_list.h>
-#ifndef logcat_popen
-#define logcat_define(context)
-#define logcat_popen(context, command) popen((command), "r")
-#define logcat_pclose(context, fp) pclose(fp)
-#define logcat_system(command) system(command)
-#endif
#ifndef logcat_executable
#define USING_LOGCAT_EXECUTABLE_DEFAULT
#define logcat_executable "logcat"
@@ -78,7 +72,6 @@
TEST(logcat, buckets) {
FILE* fp;
- logcat_define(ctx);
#undef LOG_TAG
#define LOG_TAG "inject.buckets"
@@ -90,10 +83,9 @@
__android_log_bswrite(0, logcat_executable ".inject.buckets");
rest();
- ASSERT_TRUE(NULL !=
- (fp = logcat_popen(
- ctx, logcat_executable
- " -b radio -b events -b system -b main -d 2>/dev/null")));
+ ASSERT_TRUE(NULL != (fp = popen(logcat_executable
+ " -b radio -b events -b system -b main -d 2>/dev/null",
+ "r")));
char buffer[BIG_BUFFER];
@@ -111,7 +103,7 @@
}
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
EXPECT_EQ(ids, 15);
@@ -120,7 +112,6 @@
TEST(logcat, event_tag_filter) {
FILE* fp;
- logcat_define(ctx);
#undef LOG_TAG
#define LOG_TAG "inject.filter"
@@ -135,7 +126,7 @@
logcat_executable
" -b radio -b system -b main --pid=%d -d -s inject.filter 2>/dev/null",
getpid());
- ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, command.c_str())));
+ ASSERT_TRUE(NULL != (fp = popen(command.c_str(), "r")));
char buffer[BIG_BUFFER];
@@ -145,7 +136,7 @@
if (strncmp(begin, buffer, sizeof(begin) - 1)) ++count;
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
// logcat, liblogcat and logcatd test instances result in the progression
// of 3, 6 and 9 for our counts as each round is performed.
@@ -191,7 +182,6 @@
do {
FILE* fp;
- logcat_define(ctx);
char needle[32];
time_t now;
@@ -205,9 +195,8 @@
#endif
strftime(needle, sizeof(needle), "[ %Y-", ptm);
- ASSERT_TRUE(NULL != (fp = logcat_popen(
- ctx, logcat_executable
- " -v long -v year -b all -t 3 2>/dev/null")));
+ ASSERT_TRUE(NULL !=
+ (fp = popen(logcat_executable " -v long -v year -b all -t 3 2>/dev/null", "r")));
char buffer[BIG_BUFFER];
@@ -218,7 +207,7 @@
++count;
}
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
} while ((count < 3) && --tries && inject(3 - count));
@@ -268,12 +257,10 @@
do {
FILE* fp;
- logcat_define(ctx);
- ASSERT_TRUE(NULL !=
- (fp = logcat_popen(ctx, logcat_executable
- " -v long -v America/Los_Angeles "
- "-b all -t 3 2>/dev/null")));
+ ASSERT_TRUE(NULL != (fp = popen(logcat_executable
+ " -v long -v America/Los_Angeles -b all -t 3 2>/dev/null",
+ "r")));
char buffer[BIG_BUFFER];
@@ -287,7 +274,7 @@
}
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
} while ((count < 3) && --tries && inject(3 - count));
@@ -296,11 +283,11 @@
TEST(logcat, ntz) {
FILE* fp;
- logcat_define(ctx);
- ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, logcat_executable
- " -v long -v America/Los_Angeles -v "
- "zone -b all -t 3 2>/dev/null")));
+ ASSERT_TRUE(NULL !=
+ (fp = popen(logcat_executable
+ " -v long -v America/Los_Angeles -v zone -b all -t 3 2>/dev/null",
+ "r")));
char buffer[BIG_BUFFER];
@@ -312,7 +299,7 @@
}
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
ASSERT_EQ(0, count);
}
@@ -330,8 +317,7 @@
"ANDROID_PRINTF_LOG=long logcat -b all -t %d 2>/dev/null", num);
FILE* fp;
- logcat_define(ctx);
- ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, buffer)));
+ ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
count = 0;
@@ -339,7 +325,7 @@
++count;
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
} while ((count < num) && --tries && inject(num - count));
@@ -377,8 +363,7 @@
do {
snprintf(buffer, sizeof(buffer), "%s -t 10 2>&1", cmd);
- logcat_define(ctx);
- ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, buffer)));
+ ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
count = 0;
while ((input = fgetLongTime(buffer, sizeof(buffer), fp))) {
@@ -391,7 +376,7 @@
free(last_timestamp);
last_timestamp = strdup(input);
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
} while ((count < 10) && --tries && inject(10 - count));
@@ -401,8 +386,7 @@
EXPECT_TRUE(second_timestamp != NULL);
snprintf(buffer, sizeof(buffer), "%s -t '%s' 2>&1", cmd, first_timestamp);
- logcat_define(ctx);
- ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, buffer)));
+ ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
int second_count = 0;
int last_timestamp_count = -1;
@@ -442,7 +426,7 @@
last_timestamp_count = second_count;
}
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
EXPECT_TRUE(found);
if (!found) {
@@ -483,10 +467,8 @@
ASSERT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts)));
FILE* fp;
- logcat_define(ctx);
ASSERT_TRUE(NULL !=
- (fp = logcat_popen(ctx, logcat_executable
- " -v brief -b events -t 100 2>/dev/null")));
+ (fp = popen(logcat_executable " -v brief -b events -t 100 2>/dev/null", "r")));
char buffer[BIG_BUFFER];
@@ -507,7 +489,7 @@
}
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
ASSERT_EQ(1, count);
}
@@ -521,12 +503,9 @@
FILE* fp[256]; // does this count as a multitude!
memset(fp, 0, sizeof(fp));
- logcat_define(ctx[sizeof(fp) / sizeof(fp[0])]);
size_t num = 0;
do {
- EXPECT_TRUE(NULL !=
- (fp[num] = logcat_popen(ctx[num], logcat_executable
- " -v brief -b events -t 100")));
+ EXPECT_TRUE(NULL != (fp[num] = popen(logcat_executable " -v brief -b events -t 100", "r")));
if (!fp[num]) {
fprintf(stderr,
"WARNING: limiting to %zu simultaneous logcat operations\n",
@@ -556,7 +535,7 @@
}
}
- logcat_pclose(ctx[idx], fp[idx]);
+ pclose(fp[idx]);
}
ASSERT_EQ(num, count);
@@ -564,10 +543,9 @@
static int get_groups(const char* cmd) {
FILE* fp;
- logcat_define(ctx);
// NB: crash log only available in user space
- EXPECT_TRUE(NULL != (fp = logcat_popen(ctx, cmd)));
+ EXPECT_TRUE(NULL != (fp = popen(cmd, "r")));
if (fp == NULL) {
return 0;
@@ -631,7 +609,7 @@
}
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
return count;
}
@@ -815,7 +793,7 @@
snprintf(command, sizeof(command), comm, buf);
int ret;
- EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
+ EXPECT_FALSE(IsFalse(ret = system(command), command));
if (!ret) {
snprintf(command, sizeof(command), "ls -s %s 2>/dev/null", buf);
@@ -861,7 +839,7 @@
snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir);
int ret;
- EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
+ EXPECT_FALSE(IsFalse(ret = system(command), command));
if (!ret) {
snprintf(command, sizeof(command), "ls %s 2>/dev/null", tmp_out_dir);
@@ -920,7 +898,7 @@
snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir, log_filename);
int ret;
- EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
+ EXPECT_FALSE(IsFalse(ret = system(command), command));
if (ret) {
snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir);
EXPECT_FALSE(IsFalse(system(command), command));
@@ -969,7 +947,7 @@
// re-run the command, it should only add a few lines more content if it
// continues where it left off.
snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir, log_filename);
- EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
+ EXPECT_FALSE(IsFalse(ret = system(command), command));
if (ret) {
snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir);
EXPECT_FALSE(IsFalse(system(command), command));
@@ -1052,7 +1030,7 @@
tmp_out_dir, log_filename, num_val);
int ret;
- EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
+ EXPECT_FALSE(IsFalse(ret = system(command), command));
if (ret) {
snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir);
EXPECT_FALSE(IsFalse(system(command), command));
@@ -1082,7 +1060,7 @@
strcat(command, clear_cmd);
int ret;
- EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
+ EXPECT_FALSE(IsFalse(ret = system(command), command));
if (ret) {
snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir);
EXPECT_FALSE(system(command));
@@ -1120,7 +1098,7 @@
snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir, log_filename);
- int ret = logcat_system(command);
+ int ret = system(command);
if (ret) {
fprintf(stderr, "system(\"%s\")=%d", command, ret);
return -1;
@@ -1194,7 +1172,7 @@
" -b all -d"
" -f /das/nein/gerfingerpoken/logcat/log.txt"
" -n 256 -r 1024";
- EXPECT_FALSE(IsFalse(0 == logcat_system(command), command));
+ EXPECT_FALSE(IsFalse(0 == system(command), command));
}
#ifndef logcat
@@ -1329,10 +1307,7 @@
#endif
static bool get_white_black(char** list) {
- FILE* fp;
- logcat_define(ctx);
-
- fp = logcat_popen(ctx, logcat_executable " -p 2>/dev/null");
+ FILE* fp = popen(logcat_executable " -p 2>/dev/null", "r");
if (fp == NULL) {
fprintf(stderr, "ERROR: logcat -p 2>/dev/null\n");
return false;
@@ -1360,19 +1335,15 @@
asprintf(list, "%s", buf);
}
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
return *list != NULL;
}
static bool set_white_black(const char* list) {
- FILE* fp;
- logcat_define(ctx);
-
char buffer[BIG_BUFFER];
-
snprintf(buffer, sizeof(buffer), logcat_executable " -P '%s' 2>&1",
list ? list : "");
- fp = logcat_popen(ctx, buffer);
+ FILE* fp = popen(buffer, "r");
if (fp == NULL) {
fprintf(stderr, "ERROR: %s\n", buffer);
return false;
@@ -1391,10 +1362,10 @@
continue;
}
fprintf(stderr, "%s\n", buf);
- logcat_pclose(ctx, fp);
+ pclose(fp);
return false;
}
- return logcat_pclose(ctx, fp) == 0;
+ return pclose(fp) == 0;
}
TEST(logcat, white_black_adjust) {
@@ -1429,7 +1400,6 @@
TEST(logcat, regex) {
FILE* fp;
- logcat_define(ctx);
int count = 0;
char buffer[BIG_BUFFER];
@@ -1450,7 +1420,7 @@
// Let the logs settle
rest();
- ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, buffer)));
+ ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
while (fgets(buffer, sizeof(buffer), fp)) {
if (!strncmp(begin, buffer, sizeof(begin) - 1)) {
@@ -1462,14 +1432,13 @@
count++;
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
ASSERT_EQ(2, count);
}
TEST(logcat, maxcount) {
FILE* fp;
- logcat_define(ctx);
int count = 0;
char buffer[BIG_BUFFER];
@@ -1488,7 +1457,7 @@
rest();
- ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, buffer)));
+ ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
while (fgets(buffer, sizeof(buffer), fp)) {
if (!strncmp(begin, buffer, sizeof(begin) - 1)) {
@@ -1498,7 +1467,7 @@
count++;
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
ASSERT_EQ(3, count);
}
@@ -1510,13 +1479,7 @@
;
static bool End_to_End(const char* tag, const char* fmt, ...) {
- logcat_define(ctx);
- FILE* fp = logcat_popen(ctx, logcat_executable
- " -v brief"
- " -b events"
- " -v descriptive"
- " -t 100"
- " 2>/dev/null");
+ FILE* fp = popen(logcat_executable " -v brief -b events -v descriptive -t 100 2>/dev/null", "r");
if (!fp) {
fprintf(stderr, "End_to_End: popen failed");
return false;
@@ -1551,7 +1514,7 @@
}
}
- logcat_pclose(ctx, fp);
+ pclose(fp);
if ((count == 0) && (lastMatch.length() > 0)) {
// Help us pinpoint where things went wrong ...
@@ -1741,13 +1704,12 @@
}
static bool reportedSecurity(const char* command) {
- logcat_define(ctx);
- FILE* fp = logcat_popen(ctx, command);
+ FILE* fp = popen(command, "r");
if (!fp) return true;
std::string ret;
bool val = android::base::ReadFdToString(fileno(fp), &ret);
- logcat_pclose(ctx, fp);
+ pclose(fp);
if (!val) return true;
return std::string::npos != ret.find("'security'");
@@ -1762,13 +1724,12 @@
}
static size_t commandOutputSize(const char* command) {
- logcat_define(ctx);
- FILE* fp = logcat_popen(ctx, command);
+ FILE* fp = popen(command, "r");
if (!fp) return 0;
std::string ret;
if (!android::base::ReadFdToString(fileno(fp), &ret)) return 0;
- if (logcat_pclose(ctx, fp) != 0) return 0;
+ if (pclose(fp) != 0) return 0;
return ret.size();
}
diff --git a/logd/CommandListener.cpp b/logd/CommandListener.cpp
index 06c0ab5..7a843d8 100644
--- a/logd/CommandListener.cpp
+++ b/logd/CommandListener.cpp
@@ -288,9 +288,9 @@
uid = AID_ROOT;
}
- const char* name = NULL;
- const char* format = NULL;
- const char* id = NULL;
+ const char* name = nullptr;
+ const char* format = nullptr;
+ const char* id = nullptr;
for (int i = 1; i < argc; ++i) {
static const char _name[] = "name=";
if (!strncmp(argv[i], _name, strlen(_name))) {
diff --git a/logd/FlushCommand.cpp b/logd/FlushCommand.cpp
old mode 100755
new mode 100644
index 70ecbe0..658e079
--- a/logd/FlushCommand.cpp
+++ b/logd/FlushCommand.cpp
@@ -36,7 +36,7 @@
// reference counts are used to ensure that individual
// LogTimeEntry lifetime is managed when not protected.
void FlushCommand::runSocketCommand(SocketClient* client) {
- LogTimeEntry* entry = NULL;
+ LogTimeEntry* entry = nullptr;
LastLogTimes& times = mReader.logbuf().mTimes;
LogTimeEntry::wrlock();
diff --git a/logd/FlushCommand.h b/logd/FlushCommand.h
old mode 100755
new mode 100644
diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp
old mode 100755
new mode 100644
index b76160d..fc8c960
--- a/logd/LogAudit.cpp
+++ b/logd/LogAudit.cpp
@@ -19,6 +19,7 @@
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
@@ -112,7 +113,7 @@
std::map<std::string, std::string> LogAudit::populateDenialMap() {
std::ifstream bug_file("/system/etc/selinux/selinux_denial_metadata");
std::string line;
- // allocate a map for the static map pointer in logParse to keep track of,
+ // allocate a map for the static map pointer in auditParse to keep track of,
// this function only runs once
std::map<std::string, std::string> denial_to_bug;
if (bug_file.good()) {
@@ -140,7 +141,8 @@
return "";
}
-void LogAudit::logParse(const std::string& string, std::string* bug_num) {
+void LogAudit::auditParse(const std::string& string, uid_t uid,
+ std::string* bug_num) {
if (!__android_log_is_debuggable()) {
bug_num->assign("");
return;
@@ -162,16 +164,21 @@
} else {
bug_num->assign("");
}
+
+ if (uid >= AID_APP_START && uid <= AID_APP_END) {
+ bug_num->append(" app=");
+ bug_num->append(android::uidToName(uid));
+ }
}
int LogAudit::logPrint(const char* fmt, ...) {
- if (fmt == NULL) {
+ if (fmt == nullptr) {
return -EINVAL;
}
va_list args;
- char* str = NULL;
+ char* str = nullptr;
va_start(args, fmt);
int rc = vasprintf(&str, fmt, args);
va_end(args);
@@ -190,8 +197,27 @@
while ((cp = strstr(str, " "))) {
memmove(cp, cp + 1, strlen(cp + 1) + 1);
}
+ pid_t pid = getpid();
+ pid_t tid = gettid();
+ uid_t uid = AID_LOGD;
+ static const char pid_str[] = " pid=";
+ char* pidptr = strstr(str, pid_str);
+ if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
+ cp = pidptr + sizeof(pid_str) - 1;
+ pid = 0;
+ while (isdigit(*cp)) {
+ pid = (pid * 10) + (*cp - '0');
+ ++cp;
+ }
+ tid = pid;
+ logbuf->wrlock();
+ uid = logbuf->pidToUid(pid);
+ logbuf->unlock();
+ memmove(pidptr, cp, strlen(cp) + 1);
+ }
+
bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
- static std::string bug_metadata;
+ static std::string denial_metadata;
if ((fdDmesg >= 0) && initialized) {
struct iovec iov[4];
static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
@@ -203,7 +229,7 @@
static char* last_str;
static bool last_info;
- if (last_str != NULL) {
+ if (last_str != nullptr) {
static const char avc[] = "): avc: ";
char* avcl = strstr(last_str, avc);
bool skip = false;
@@ -228,8 +254,8 @@
last_info ? sizeof(log_info) : sizeof(log_warning);
iov[1].iov_base = last_str;
iov[1].iov_len = strlen(last_str);
- iov[2].iov_base = const_cast<char*>(bug_metadata.c_str());
- iov[2].iov_len = bug_metadata.length();
+ iov[2].iov_base = const_cast<char*>(denial_metadata.c_str());
+ iov[2].iov_len = denial_metadata.length();
if (count > 1) {
iov[3].iov_base = const_cast<char*>(resume);
iov[3].iov_len = strlen(resume);
@@ -240,23 +266,23 @@
writev(fdDmesg, iov, arraysize(iov));
free(last_str);
- last_str = NULL;
+ last_str = nullptr;
}
}
- if (last_str == NULL) {
+ if (last_str == nullptr) {
count = 0;
last_str = strdup(str);
last_info = info;
}
if (count == 0) {
- logParse(str, &bug_metadata);
+ auditParse(str, uid, &denial_metadata);
iov[0].iov_base = info ? const_cast<char*>(log_info)
: const_cast<char*>(log_warning);
iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning);
iov[1].iov_base = str;
iov[1].iov_len = strlen(str);
- iov[2].iov_base = const_cast<char*>(bug_metadata.c_str());
- iov[2].iov_len = bug_metadata.length();
+ iov[2].iov_base = const_cast<char*>(denial_metadata.c_str());
+ iov[2].iov_len = denial_metadata.length();
iov[3].iov_base = const_cast<char*>(newline);
iov[3].iov_len = strlen(newline);
@@ -269,9 +295,6 @@
return 0;
}
- pid_t pid = getpid();
- pid_t tid = gettid();
- uid_t uid = AID_LOGD;
log_time now;
static const char audit_str[] = " audit(";
@@ -296,29 +319,13 @@
now = log_time(CLOCK_REALTIME);
}
- static const char pid_str[] = " pid=";
- char* pidptr = strstr(str, pid_str);
- if (pidptr && isdigit(pidptr[sizeof(pid_str) - 1])) {
- cp = pidptr + sizeof(pid_str) - 1;
- pid = 0;
- while (isdigit(*cp)) {
- pid = (pid * 10) + (*cp - '0');
- ++cp;
- }
- tid = pid;
- logbuf->wrlock();
- uid = logbuf->pidToUid(pid);
- logbuf->unlock();
- memmove(pidptr, cp, strlen(cp) + 1);
- }
-
// log to events
size_t str_len = strnlen(str, LOGGER_ENTRY_MAX_PAYLOAD);
if (((fdDmesg < 0) || !initialized) && !hasMetadata(str, str_len))
- logParse(str, &bug_metadata);
- str_len = (str_len + bug_metadata.length() <= LOGGER_ENTRY_MAX_PAYLOAD)
- ? str_len + bug_metadata.length()
+ auditParse(str, uid, &denial_metadata);
+ str_len = (str_len + denial_metadata.length() <= LOGGER_ENTRY_MAX_PAYLOAD)
+ ? str_len + denial_metadata.length()
: LOGGER_ENTRY_MAX_PAYLOAD;
size_t message_len = str_len + sizeof(android_log_event_string_t);
@@ -331,15 +338,14 @@
reinterpret_cast<android_log_event_string_t*>(buffer);
event->header.tag = htole32(AUDITD_LOG_TAG);
event->type = EVENT_TYPE_STRING;
- event->length = htole32(message_len);
- memcpy(event->data, str, str_len - bug_metadata.length());
- memcpy(event->data + str_len - bug_metadata.length(),
- bug_metadata.c_str(), bug_metadata.length());
+ event->length = htole32(str_len);
+ memcpy(event->data, str, str_len - denial_metadata.length());
+ memcpy(event->data + str_len - denial_metadata.length(),
+ denial_metadata.c_str(), denial_metadata.length());
rc = logbuf->log(
LOG_ID_EVENTS, now, uid, pid, tid, reinterpret_cast<char*>(event),
- (message_len <= USHRT_MAX) ? (unsigned short)message_len
- : USHRT_MAX);
+ (message_len <= UINT16_MAX) ? (uint16_t)message_len : UINT16_MAX);
if (rc >= 0) {
notify |= 1 << LOG_ID_EVENTS;
}
@@ -351,7 +357,7 @@
static const char comm_str[] = " comm=\"";
const char* comm = strstr(str, comm_str);
const char* estr = str + strlen(str);
- const char* commfree = NULL;
+ const char* commfree = nullptr;
if (comm) {
estr = comm;
comm += sizeof(comm_str) - 1;
@@ -380,7 +386,8 @@
prefix_len = LOGGER_ENTRY_MAX_PAYLOAD;
}
size_t suffix_len = strnlen(ecomm, LOGGER_ENTRY_MAX_PAYLOAD - prefix_len);
- message_len = str_len + prefix_len + suffix_len + bug_metadata.length() + 2;
+ message_len =
+ str_len + prefix_len + suffix_len + denial_metadata.length() + 2;
if (main) { // begin scope for main buffer
char newstr[message_len];
@@ -390,11 +397,11 @@
strncpy(newstr + 1 + str_len, str, prefix_len);
strncpy(newstr + 1 + str_len + prefix_len, ecomm, suffix_len);
strncpy(newstr + 1 + str_len + prefix_len + suffix_len,
- bug_metadata.c_str(), bug_metadata.length());
+ denial_metadata.c_str(), denial_metadata.length());
- rc = logbuf->log(LOG_ID_MAIN, now, uid, pid, tid, newstr,
- (message_len <= USHRT_MAX) ? (unsigned short)message_len
- : USHRT_MAX);
+ rc = logbuf->log(
+ LOG_ID_MAIN, now, uid, pid, tid, newstr,
+ (message_len <= UINT16_MAX) ? (uint16_t)message_len : UINT16_MAX);
if (rc >= 0) {
notify |= 1 << LOG_ID_MAIN;
diff --git a/logd/LogAudit.h b/logd/LogAudit.h
index 5904966..c3d7a3e 100644
--- a/logd/LogAudit.h
+++ b/logd/LogAudit.h
@@ -48,7 +48,7 @@
std::map<std::string, std::string> populateDenialMap();
std::string denialParse(const std::string& denial, char terminator,
const std::string& search_term);
- void logParse(const std::string& string, std::string* bug_num);
+ void auditParse(const std::string& string, uid_t uid, std::string* bug_num);
int logPrint(const char* fmt, ...)
__attribute__((__format__(__printf__, 2, 3)));
};
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
index b8af2f0..fd1b8b2 100644
--- a/logd/LogBuffer.cpp
+++ b/logd/LogBuffer.cpp
@@ -171,7 +171,9 @@
}
// audit message (except sequence number) identical?
- if (last->isBinary()) {
+ if (last->isBinary() &&
+ (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
+ (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
sizeof(int32_t))) {
return DIFFERENT;
@@ -197,7 +199,7 @@
}
int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
- pid_t tid, const char* msg, unsigned short len) {
+ pid_t tid, const char* msg, uint16_t len) {
if (log_id >= LOG_ID_MAX) {
return -EINVAL;
}
@@ -238,7 +240,7 @@
LogBufferElement* currentLast = lastLoggedElements[log_id];
if (currentLast) {
LogBufferElement* dropped = droppedElements[log_id];
- unsigned short count = dropped ? dropped->getDropped() : 0;
+ uint16_t count = dropped ? dropped->getDropped() : 0;
//
// State Init
// incoming:
@@ -582,13 +584,13 @@
LogBufferElementMap map;
public:
- bool coalesce(LogBufferElement* element, unsigned short dropped) {
+ bool coalesce(LogBufferElement* element, uint16_t dropped) {
LogBufferElementKey key(element->getUid(), element->getPid(),
element->getTid());
LogBufferElementMap::iterator it = map.find(key.getKey());
if (it != map.end()) {
LogBufferElement* found = it->second;
- unsigned short moreDropped = found->getDropped();
+ uint16_t moreDropped = found->getDropped();
if ((dropped + moreDropped) > USHRT_MAX) {
map.erase(it);
} else {
@@ -845,7 +847,7 @@
mLastSet[id] = true;
}
- unsigned short dropped = element->getDropped();
+ uint16_t dropped = element->getDropped();
// remove any leading drops
if (leading && dropped) {
@@ -925,7 +927,7 @@
kick = true;
- unsigned short len = element->getMsgLen();
+ uint16_t len = element->getMsgLen();
// do not create any leading drops
if (leading) {
@@ -1113,9 +1115,6 @@
// client wants to start from the beginning
it = mLogElements.begin();
} else {
- // 3 second limit to continue search for out-of-order entries.
- log_time min = start - pruneMargin;
-
// Cap to 300 iterations we look back for out-of-order entries.
size_t count = 300;
@@ -1131,7 +1130,7 @@
} else if (element->getRealTime() == start) {
last = ++it;
break;
- } else if (!--count || (element->getRealTime() < min)) {
+ } else if (!--count) {
break;
}
}
diff --git a/logd/LogBuffer.h b/logd/LogBuffer.h
index 0942987..774d4ab 100644
--- a/logd/LogBuffer.h
+++ b/logd/LogBuffer.h
@@ -115,7 +115,7 @@
}
int log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
- const char* msg, unsigned short len) override;
+ const char* msg, uint16_t len) override;
// lastTid is an optional context to help detect if the last previous
// valid message was from the same source so we can differentiate chatty
// filter types (identical or expired)
diff --git a/logd/LogBufferElement.cpp b/logd/LogBufferElement.cpp
index f20ac45..19e6d68 100644
--- a/logd/LogBufferElement.cpp
+++ b/logd/LogBufferElement.cpp
@@ -35,7 +35,7 @@
LogBufferElement::LogBufferElement(log_id_t log_id, log_time realtime,
uid_t uid, pid_t pid, pid_t tid,
- const char* msg, unsigned short len)
+ const char* msg, uint16_t len)
: mUid(uid),
mPid(pid),
mTid(tid),
@@ -71,7 +71,7 @@
: 0;
}
-unsigned short LogBufferElement::setDropped(unsigned short value) {
+uint16_t LogBufferElement::setDropped(uint16_t value) {
// The tag information is saved in mMsg data, if the tag is non-zero
// save only the information needed to get the tag.
if (getTag() != 0) {
@@ -91,7 +91,7 @@
// caller must own and free character string
char* android::tidToName(pid_t tid) {
- char* retval = NULL;
+ char* retval = nullptr;
char buffer[256];
snprintf(buffer, sizeof(buffer), "/proc/%u/comm", tid);
int fd = open(buffer, O_RDONLY);
@@ -114,7 +114,7 @@
char* name = android::pidToName(tid);
if (!retval) {
retval = name;
- name = NULL;
+ name = nullptr;
}
// check if comm is truncated, see if cmdline has full representation
@@ -162,15 +162,15 @@
if (!strncmp(name + 1, commName + 1, len)) {
if (commName[len + 1] == '\0') {
free(const_cast<char*>(commName));
- commName = NULL;
+ commName = nullptr;
} else {
free(const_cast<char*>(name));
- name = NULL;
+ name = nullptr;
}
}
}
if (name) {
- char* buf = NULL;
+ char* buf = nullptr;
asprintf(&buf, "(%s)", name);
if (buf) {
free(const_cast<char*>(name));
@@ -178,7 +178,7 @@
}
}
if (commName) {
- char* buf = NULL;
+ char* buf = nullptr;
asprintf(&buf, " %s", commName);
if (buf) {
free(const_cast<char*>(commName));
@@ -187,7 +187,7 @@
}
// identical to below to calculate the buffer size required
const char* type = lastSame ? "identical" : "expire";
- size_t len = snprintf(NULL, 0, format_uid, mUid, name ? name : "",
+ size_t len = snprintf(nullptr, 0, format_uid, mUid, name ? name : "",
commName ? commName : "", type, getDropped(),
(getDropped() > 1) ? "s" : "");
@@ -247,7 +247,7 @@
iovec[0].iov_base = &entry;
iovec[0].iov_len = entry.hdr_size;
- char* buffer = NULL;
+ char* buffer = nullptr;
if (mDropped) {
entry.len = populateDroppedMessage(buffer, parent, lastSame);
diff --git a/logd/LogBufferElement.h b/logd/LogBufferElement.h
index b168645..57b0a95 100644
--- a/logd/LogBufferElement.h
+++ b/logd/LogBufferElement.h
@@ -18,6 +18,7 @@
#define _LOGD_LOG_BUFFER_ELEMENT_H__
#include <stdatomic.h>
+#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
@@ -56,7 +57,7 @@
public:
LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
- pid_t tid, const char* msg, unsigned short len);
+ pid_t tid, const char* msg, uint16_t len);
LogBufferElement(const LogBufferElement& elem);
~LogBufferElement();
@@ -77,11 +78,11 @@
return mTid;
}
uint32_t getTag() const;
- unsigned short getDropped(void) const {
+ uint16_t getDropped(void) const {
return mDropped ? mDroppedCount : 0;
}
- unsigned short setDropped(unsigned short value);
- unsigned short getMsgLen() const {
+ uint16_t setDropped(uint16_t value);
+ uint16_t getMsgLen() const {
return mDropped ? 0 : mMsgLen;
}
const char* getMsg() const {
diff --git a/logd/LogBufferInterface.h b/logd/LogBufferInterface.h
index ff73a22..f31e244 100644
--- a/logd/LogBufferInterface.h
+++ b/logd/LogBufferInterface.h
@@ -31,7 +31,7 @@
// Handles a log entry when available in LogListener.
// Returns the size of the handled log message.
virtual int log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
- pid_t tid, const char* msg, unsigned short len) = 0;
+ pid_t tid, const char* msg, uint16_t len) = 0;
virtual uid_t pidToUid(pid_t pid);
virtual pid_t tidToPid(pid_t tid);
diff --git a/logd/LogCommand.cpp b/logd/LogCommand.cpp
index 6d7c0a5..8bff9da 100644
--- a/logd/LogCommand.cpp
+++ b/logd/LogCommand.cpp
@@ -44,9 +44,9 @@
char* ptr;
static const char ws[] = " \n";
- for (buf = strtok_r(buf, ws, &ptr); buf; buf = strtok_r(NULL, ws, &ptr)) {
+ for (buf = strtok_r(buf, ws, &ptr); buf; buf = strtok_r(nullptr, ws, &ptr)) {
errno = 0;
- gid_t Gid = strtol(buf, NULL, 10);
+ gid_t Gid = strtol(buf, nullptr, 10);
if (errno != 0) {
return false;
}
@@ -98,7 +98,7 @@
continue;
}
- char* line = NULL;
+ char* line = nullptr;
size_t len = 0;
while (getline(&line, &len, file) > 0) {
static const char groups_string[] = "Groups:\t";
diff --git a/logd/LogKlog.cpp b/logd/LogKlog.cpp
old mode 100755
new mode 100644
index 7a7ac7d..e4393a3
--- a/logd/LogKlog.cpp
+++ b/logd/LogKlog.cpp
@@ -821,11 +821,10 @@
}
// Log message
- int rc = logbuf->log(LOG_ID_KERNEL, now, uid, pid, tid, newstr,
- (unsigned short)n);
+ int rc = logbuf->log(LOG_ID_KERNEL, now, uid, pid, tid, newstr, (uint16_t)n);
// notify readers
- if (!rc) {
+ if (rc > 0) {
reader->notifyNewLog(static_cast<log_mask_t>(1 << LOG_ID_KERNEL));
}
diff --git a/logd/LogListener.cpp b/logd/LogListener.cpp
old mode 100755
new mode 100644
index fc51dcf..2f22778
--- a/logd/LogListener.cpp
+++ b/logd/LogListener.cpp
@@ -50,7 +50,7 @@
alignas(4) char control[CMSG_SPACE(sizeof(struct ucred))];
struct msghdr hdr = {
- NULL, 0, &iov, 1, control, sizeof(control), 0,
+ nullptr, 0, &iov, 1, control, sizeof(control), 0,
};
int socket = cli->getSocket();
@@ -66,10 +66,10 @@
buffer[n] = 0;
- struct ucred* cred = NULL;
+ struct ucred* cred = nullptr;
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr);
- while (cmsg != NULL) {
+ while (cmsg != nullptr) {
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_CREDENTIALS) {
cred = (struct ucred*)CMSG_DATA(cmsg);
@@ -79,7 +79,7 @@
}
struct ucred fake_cred;
- if (cred == NULL) {
+ if (cred == nullptr) {
cred = &fake_cred;
cred->pid = 0;
cred->uid = DEFAULT_OVERFLOWUID;
@@ -136,7 +136,7 @@
if (logbuf != nullptr) {
int res = logbuf->log(
logId, header->realtime, cred->uid, cred->pid, header->tid, msg,
- ((size_t)n <= USHRT_MAX) ? (unsigned short)n : USHRT_MAX);
+ ((size_t)n <= UINT16_MAX) ? (uint16_t)n : UINT16_MAX);
if (res > 0 && reader != nullptr) {
reader->notifyNewLog(static_cast<log_mask_t>(1 << logId));
}
diff --git a/logd/LogReader.cpp b/logd/LogReader.cpp
old mode 100755
new mode 100644
diff --git a/logd/LogReader.h b/logd/LogReader.h
old mode 100755
new mode 100644
diff --git a/logd/LogStatistics.cpp b/logd/LogStatistics.cpp
index af59ddc..116e08e 100644
--- a/logd/LogStatistics.cpp
+++ b/logd/LogStatistics.cpp
@@ -56,7 +56,7 @@
// caller must own and free character string
char* pidToName(pid_t pid) {
- char* retval = NULL;
+ char* retval = nullptr;
if (pid == 0) { // special case from auditd/klogd for kernel
retval = strdup("logd");
} else {
@@ -83,7 +83,7 @@
if (element->getDropped()) return;
log_id_t log_id = element->getLogId();
- unsigned short size = element->getMsgLen();
+ uint16_t size = element->getMsgLen();
mSizesTotal[log_id] += size;
SizesTotal += size;
++mElementsTotal[log_id];
@@ -91,7 +91,7 @@
void LogStatistics::add(LogBufferElement* element) {
log_id_t log_id = element->getLogId();
- unsigned short size = element->getMsgLen();
+ uint16_t size = element->getMsgLen();
mSizes[log_id] += size;
++mElements[log_id];
@@ -161,7 +161,7 @@
void LogStatistics::subtract(LogBufferElement* element) {
log_id_t log_id = element->getLogId();
- unsigned short size = element->getMsgLen();
+ uint16_t size = element->getMsgLen();
mSizes[log_id] -= size;
--mElements[log_id];
if (element->getDropped()) {
@@ -206,7 +206,7 @@
// entry->setDropped(1) must follow this call, caller should do this explicitly.
void LogStatistics::drop(LogBufferElement* element) {
log_id_t log_id = element->getLogId();
- unsigned short size = element->getMsgLen();
+ uint16_t size = element->getMsgLen();
mSizes[log_id] -= size;
++mDroppedElements[log_id];
@@ -286,7 +286,7 @@
name = strdup(nameTmp);
} else if (fastcmp<strcmp>(name, nameTmp)) {
free(const_cast<char*>(name));
- name = NULL;
+ name = nullptr;
break;
}
}
@@ -613,13 +613,13 @@
std::string LogStatistics::format(uid_t uid, pid_t pid,
unsigned int logMask) const {
- static const unsigned short spaces_total = 19;
+ static const uint16_t spaces_total = 19;
// Report on total logging, current and for all time
std::string output = "size/num";
size_t oldLength;
- short spaces = 1;
+ int16_t spaces = 1;
log_id_for_each(id) {
if (!(logMask & (1 << id))) continue;
@@ -872,7 +872,7 @@
pidTable_t& writablePidTable = const_cast<pidTable_t&>(pidTable);
const char* name = writablePidTable.add(pid)->second.getName();
if (!name) {
- return NULL;
+ return nullptr;
}
return strdup(name);
}
diff --git a/logd/LogStatistics.h b/logd/LogStatistics.h
index ac3cf9a..d6b8ab3 100644
--- a/logd/LogStatistics.h
+++ b/logd/LogStatistics.h
@@ -520,7 +520,7 @@
return;
}
++msg;
- unsigned short len = element->getMsgLen();
+ uint16_t len = element->getMsgLen();
len = (len <= 1) ? 0 : strnlen(msg, len - 1);
if (!len) {
name = std::string_view("<NULL>", strlen("<NULL>"));
diff --git a/logd/LogTags.cpp b/logd/LogTags.cpp
index ff7e762..1ab9dd1 100644
--- a/logd/LogTags.cpp
+++ b/logd/LogTags.cpp
@@ -91,7 +91,7 @@
fd = TEMP_FAILURE_RETRY(open(
filename, O_WRONLY | O_TRUNC | O_CLOEXEC | O_NOFOLLOW | O_BINARY));
if (fd >= 0) {
- time_t now = time(NULL);
+ time_t now = time(nullptr);
struct tm tm;
localtime_r(&now, &tm);
char timebuf[20];
@@ -208,7 +208,7 @@
} else if (lineStart) {
if (*cp == '#') {
/* comment; just scan to end */
- lineStart = NULL;
+ lineStart = nullptr;
} else if (isdigit(*cp)) {
unsigned long Tag = strtoul(cp, &cp, 10);
if (warn && (Tag > emptyTag)) {
@@ -235,7 +235,7 @@
if (hasAlpha &&
((cp >= endp) || (*cp == '#') || isspace(*cp))) {
if (Tag > emptyTag) {
- if (*cp != '\n') lineStart = NULL;
+ if (*cp != '\n') lineStart = nullptr;
continue;
}
while ((cp < endp) && (*cp != '\n') && isspace(*cp))
@@ -245,14 +245,14 @@
while ((cp < endp) && (*cp != '\n')) {
if (*cp == '#') {
uid = sniffUid(cp, endp);
- lineStart = NULL;
+ lineStart = nullptr;
break;
}
++cp;
}
while ((cp > format) && isspace(cp[-1])) {
--cp;
- lineStart = NULL;
+ lineStart = nullptr;
}
std::string Format(format, cp - format);
@@ -263,7 +263,7 @@
android::prdebug("tag name invalid %.*s",
(int)(cp - name + 1), name);
}
- lineStart = NULL;
+ lineStart = nullptr;
}
} else if (!isspace(*cp)) {
break;
@@ -364,7 +364,7 @@
android::RWLock::AutoRLock readLock(const_cast<android::RWLock&>(rwlock));
it = tag2name.find(tag);
- if ((it == tag2name.end()) || (it->second.length() == 0)) return NULL;
+ if ((it == tag2name.end()) || (it->second.length() == 0)) return nullptr;
return it->second.c_str();
}
@@ -383,7 +383,7 @@
const char* android::tagToName(uint32_t tag) {
LogTags* me = logtags;
- if (!me) return NULL;
+ if (!me) return nullptr;
me->WritePmsgEventLogTags(tag);
return me->tagToName(tag);
}
@@ -412,7 +412,7 @@
android::RWLock::AutoRLock readLock(const_cast<android::RWLock&>(rwlock));
iform = tag2format.find(tag);
- if (iform == tag2format.end()) return NULL;
+ if (iform == tag2format.end()) return nullptr;
return iform->second.c_str();
}
@@ -441,7 +441,7 @@
bool& unique) {
key2tag_const_iterator ik;
- bool write = format != NULL;
+ bool write = format != nullptr;
unique = write;
if (!write) {
@@ -679,7 +679,7 @@
// are in readonly mode.
uint32_t LogTags::nameToTag(uid_t uid, const char* name, const char* format) {
std::string Name = std::string(name);
- bool write = format != NULL;
+ bool write = format != nullptr;
bool updateUid = uid != AID_ROOT;
bool updateFormat = format && *format;
bool unique;
@@ -848,7 +848,7 @@
if (!list) {
// switch to read entry only if format == "*"
- if (format && (format[0] == '*') && !format[1]) format = NULL;
+ if (format && (format[0] == '*') && !format[1]) format = nullptr;
// WAI: for null format, only works for a single entry, we can have
// multiple entries, one for each format, so we find first entry
diff --git a/logd/LogTags.h b/logd/LogTags.h
index 203318d..e4d165a 100644
--- a/logd/LogTags.h
+++ b/logd/LogTags.h
@@ -87,14 +87,14 @@
bool RebuildFileEventLogTags(const char* filename, bool warn = true);
void AddEventLogTags(uint32_t tag, uid_t uid, const std::string& Name,
- const std::string& Format, const char* source = NULL,
+ const std::string& Format, const char* source = nullptr,
bool warn = false);
void WriteDynamicEventLogTags(uint32_t tag, uid_t uid);
void WriteDebugEventLogTags(uint32_t tag, uid_t uid);
// push tag details to persistent storage
void WritePersistEventLogTags(uint32_t tag, uid_t uid = AID_ROOT,
- const char* source = NULL);
+ const char* source = nullptr);
static const uint32_t emptyTag = uint32_t(-1);
diff --git a/logd/LogTimes.cpp b/logd/LogTimes.cpp
old mode 100755
new mode 100644
diff --git a/logd/LogTimes.h b/logd/LogTimes.h
old mode 100755
new mode 100644
diff --git a/logd/LogWhiteBlackList.cpp b/logd/LogWhiteBlackList.cpp
index 4b8b080..9d762dc 100644
--- a/logd/LogWhiteBlackList.cpp
+++ b/logd/LogWhiteBlackList.cpp
@@ -51,7 +51,7 @@
}
PruneList::PruneList() {
- init(NULL);
+ init(nullptr);
}
PruneList::~PruneList() {
@@ -79,7 +79,7 @@
// default here means take ro.logd.filter, persist.logd.filter then
// internal default in that order.
if (str && !strcmp(str, _default)) {
- str = NULL;
+ str = nullptr;
}
static const char _disable[] = "disable";
if (str && !strcmp(str, _disable)) {
diff --git a/logd/logd.rc b/logd/logd.rc
index bd303b7..c740ecf 100644
--- a/logd/logd.rc
+++ b/logd/logd.rc
@@ -6,6 +6,7 @@
file /dev/kmsg w
user logd
group logd system package_info readproc
+ capabilities SYSLOG AUDIT_CONTROL SETGID
writepid /dev/cpuset/system-background/tasks
service logd-reinit /system/bin/logd --reinit
diff --git a/logd/main.cpp b/logd/main.cpp
index 4af0d21..b697d44 100644
--- a/logd/main.cpp
+++ b/logd/main.cpp
@@ -33,7 +33,6 @@
#include <syslog.h>
#include <unistd.h>
-#include <cstdbool>
#include <memory>
#include <android-base/macros.h>
@@ -88,37 +87,27 @@
//
static int drop_privs(bool klogd, bool auditd) {
- // Tricky, if ro.build.type is "eng" then this is true because of the
- // side effect that ro.debuggable == 1 as well, else it is false.
- bool eng =
- __android_logger_property_get_bool("ro.build.type", BOOL_DEFAULT_FALSE);
-
- struct sched_param param;
- memset(¶m, 0, sizeof(param));
+ sched_param param = {};
if (set_sched_policy(0, SP_BACKGROUND) < 0) {
android::prdebug("failed to set background scheduling policy");
- if (!eng) return -1;
+ return -1;
}
if (sched_setscheduler((pid_t)0, SCHED_BATCH, ¶m) < 0) {
android::prdebug("failed to set batch scheduler");
- if (!eng) return -1;
+ return -1;
}
if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
android::prdebug("failed to set background cgroup");
- if (!eng) return -1;
- }
-
- if (!eng && (prctl(PR_SET_DUMPABLE, 0) < 0)) {
- android::prdebug("failed to clear PR_SET_DUMPABLE");
return -1;
}
- if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
- android::prdebug("failed to set PR_SET_KEEPCAPS");
- if (!eng) return -1;
+ if (__android_logger_property_get_bool("ro.debuggable", BOOL_DEFAULT_FALSE) &&
+ prctl(PR_SET_DUMPABLE, 0) == -1) {
+ android::prdebug("failed to clear PR_SET_DUMPABLE");
+ return -1;
}
std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(),
@@ -139,24 +128,24 @@
android::prdebug(
"failed to set CAP_SETGID, CAP_SYSLOG or CAP_AUDIT_CONTROL (%d)",
errno);
- if (!eng) return -1;
+ return -1;
}
gid_t groups[] = { AID_READPROC };
if (setgroups(arraysize(groups), groups) == -1) {
android::prdebug("failed to set AID_READPROC groups");
- if (!eng) return -1;
+ return -1;
}
if (setgid(AID_LOGD) != 0) {
android::prdebug("failed to set AID_LOGD gid");
- if (!eng) return -1;
+ return -1;
}
if (setuid(AID_LOGD) != 0) {
android::prdebug("failed to set AID_LOGD uid");
- if (!eng) return -1;
+ return -1;
}
if (cap_set_flag(caps.get(), CAP_PERMITTED, 1, cap_value, CAP_CLEAR) < 0) {
@@ -167,7 +156,7 @@
}
if (cap_set_proc(caps.get()) < 0) {
android::prdebug("failed to clear CAP_SETGID (%d)", errno);
- if (!eng) return -1;
+ return -1;
}
return 0;
@@ -474,7 +463,7 @@
bool auditd =
__android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);
if (drop_privs(klogd, auditd) != 0) {
- return -1;
+ return EXIT_FAILURE;
}
// Serves the purpose of managing the last logs times read on a
@@ -502,7 +491,7 @@
LogReader* reader = new LogReader(logBuf);
if (reader->startListener()) {
- exit(1);
+ return EXIT_FAILURE;
}
// LogListener listens on /dev/socket/logdw for client
@@ -512,7 +501,7 @@
LogListener* swl = new LogListener(logBuf, reader);
// Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
if (swl->startListener(600)) {
- exit(1);
+ return EXIT_FAILURE;
}
// Command listener listens on /dev/socket/logd for incoming logd
@@ -520,7 +509,7 @@
CommandListener* cl = new CommandListener(logBuf, reader, swl);
if (cl->startListener()) {
- exit(1);
+ return EXIT_FAILURE;
}
// LogAudit listens on NETLINK_AUDIT socket for selinux
@@ -555,5 +544,5 @@
TEMP_FAILURE_RETRY(pause());
- exit(0);
+ return EXIT_SUCCESS;
}
diff --git a/logwrapper/Android.bp b/logwrapper/Android.bp
index f163f57..c378646 100644
--- a/logwrapper/Android.bp
+++ b/logwrapper/Android.bp
@@ -1,10 +1,18 @@
-
+cc_defaults {
+ name: "logwrapper_defaults",
+ cflags: [
+ "-Werror",
+ ],
+}
// ========================================================
// Static and shared library
// ========================================================
+
cc_library {
name: "liblogwrap",
+ defaults: ["logwrapper_defaults"],
+ recovery_available: true,
srcs: ["logwrap.c"],
shared_libs: [
"libcutils",
@@ -12,32 +20,42 @@
],
export_include_dirs: ["include"],
local_include_dirs: ["include"],
- cflags: [
- "-Werror",
- ],
}
// ========================================================
// Executable
// ========================================================
+
+cc_defaults {
+ name: "logwrapper_common",
+ defaults: ["logwrapper_defaults"],
+ local_include_dirs: ["include"],
+ srcs: [
+ "logwrap.c",
+ "logwrapper.c",
+ ],
+ shared_libs: ["libcutils", "liblog"],
+}
+
cc_binary {
name: "logwrapper",
- srcs: ["logwrapper.c"],
- static_libs: [
- "liblog",
- "liblogwrap",
- "libcutils",
- ],
- cflags: [
- "-Werror",
- ],
+ defaults: ["logwrapper_common"],
+}
+
+cc_binary {
+ name: "logwrapper_vendor",
+ defaults: ["logwrapper_common"],
+ stem: "logwrapper",
+ vendor: true,
}
// ========================================================
// Benchmark
// ========================================================
+
cc_benchmark {
name: "android_fork_execvp_ext_benchmark",
+ defaults: ["logwrapper_defaults"],
srcs: [
"android_fork_execvp_ext_benchmark.cpp",
],
@@ -47,7 +65,4 @@
"liblog",
"liblogwrap",
],
- cflags: [
- "-Werror",
- ],
}
diff --git a/logwrapper/logwrap.c b/logwrapper/logwrap.c
index 7076078..8621993 100644
--- a/logwrapper/logwrap.c
+++ b/logwrapper/logwrap.c
@@ -31,7 +31,6 @@
#include <cutils/klog.h>
#include <log/log.h>
#include <logwrap/logwrap.h>
-#include <private/android_filesystem_config.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
#define MIN(a,b) (((a)<(b))?(a):(b))
diff --git a/mkbootimg/Android.bp b/mkbootimg/Android.bp
new file mode 100644
index 0000000..576a677
--- /dev/null
+++ b/mkbootimg/Android.bp
@@ -0,0 +1,33 @@
+// Copyright 2012 The Android Open Source Project
+
+cc_library_headers {
+ name: "libmkbootimg_abi_headers",
+ vendor_available: true,
+ export_include_dirs: ["include"],
+}
+
+cc_library_headers {
+ name: "bootimg_headers",
+ vendor_available: true,
+ recovery_available: true,
+ export_include_dirs: ["include/bootimg"],
+ host_supported: true,
+ target: {
+ windows: {
+ enabled: true,
+ },
+ },
+}
+
+cc_library {
+ name: "libmkbootimg_abi_check",
+ vendor_available: true,
+ vndk: {
+ enabled: true,
+ },
+ srcs: [
+ "mkbootimg_dummy.cpp",
+ ],
+ header_libs: ["libmkbootimg_abi_headers"],
+ export_header_lib_headers: ["libmkbootimg_abi_headers"],
+}
diff --git a/mkbootimg/Android.mk b/mkbootimg/Android.mk
index 8661d7d..92e1e27 100644
--- a/mkbootimg/Android.mk
+++ b/mkbootimg/Android.mk
@@ -9,3 +9,12 @@
LOCAL_MODULE := mkbootimg
include $(BUILD_PREBUILT)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := unpack_bootimg
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_IS_HOST_MODULE := true
+
+LOCAL_MODULE := unpack_bootimg
+
+include $(BUILD_PREBUILT)
diff --git a/mkbootimg/OWNERS b/mkbootimg/OWNERS
new file mode 100644
index 0000000..3a00860
--- /dev/null
+++ b/mkbootimg/OWNERS
@@ -0,0 +1,2 @@
+hridya@google.com
+tbao@google.com
diff --git a/mkbootimg/bootimg.h b/mkbootimg/bootimg.h
deleted file mode 100644
index 60834fe..0000000
--- a/mkbootimg/bootimg.h
+++ /dev/null
@@ -1,111 +0,0 @@
-/* tools/mkbootimg/bootimg.h
-**
-** Copyright 2007, 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 <stdint.h>
-
-#ifndef _BOOT_IMAGE_H_
-#define _BOOT_IMAGE_H_
-
-typedef struct boot_img_hdr boot_img_hdr;
-
-#define BOOT_MAGIC "ANDROID!"
-#define BOOT_MAGIC_SIZE 8
-#define BOOT_NAME_SIZE 16
-#define BOOT_ARGS_SIZE 512
-#define BOOT_EXTRA_ARGS_SIZE 1024
-
-struct boot_img_hdr
-{
- uint8_t magic[BOOT_MAGIC_SIZE];
-
- uint32_t kernel_size; /* size in bytes */
- uint32_t kernel_addr; /* physical load addr */
-
- uint32_t ramdisk_size; /* size in bytes */
- uint32_t ramdisk_addr; /* physical load addr */
-
- uint32_t second_size; /* size in bytes */
- uint32_t second_addr; /* physical load addr */
-
- uint32_t tags_addr; /* physical addr for kernel tags */
- uint32_t page_size; /* flash page size we assume */
- uint32_t unused; /* reserved for future expansion: MUST be 0 */
-
- /* operating system version and security patch level; for
- * version "A.B.C" and patch level "Y-M-D":
- * ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
- * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M)
- * os_version = ver << 11 | lvl */
- uint32_t os_version;
-
- uint8_t name[BOOT_NAME_SIZE]; /* asciiz product name */
-
- uint8_t cmdline[BOOT_ARGS_SIZE];
-
- uint32_t id[8]; /* timestamp / checksum / sha1 / etc */
-
- /* Supplemental command line data; kept here to maintain
- * binary compatibility with older versions of mkbootimg */
- uint8_t extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
-} __attribute__((packed));
-
-/*
-** +-----------------+
-** | boot header | 1 page
-** +-----------------+
-** | kernel | n pages
-** +-----------------+
-** | ramdisk | m pages
-** +-----------------+
-** | second stage | o pages
-** +-----------------+
-**
-** n = (kernel_size + page_size - 1) / page_size
-** m = (ramdisk_size + page_size - 1) / page_size
-** o = (second_size + page_size - 1) / page_size
-**
-** 0. all entities are page_size aligned in flash
-** 1. kernel and ramdisk are required (size != 0)
-** 2. second is optional (second_size == 0 -> no second)
-** 3. load each element (kernel, ramdisk, second) at
-** the specified physical address (kernel_addr, etc)
-** 4. prepare tags at tag_addr. kernel_args[] is
-** appended to the kernel commandline in the tags.
-** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
-** 6. if second_size != 0: jump to second_addr
-** else: jump to kernel_addr
-*/
-
-#if 0
-typedef struct ptentry ptentry;
-
-struct ptentry {
- char name[16]; /* asciiz partition name */
- unsigned start; /* starting block number */
- unsigned length; /* length in blocks */
- unsigned flags; /* set to zero */
-};
-
-/* MSM Partition Table ATAG
-**
-** length: 2 + 7 * n
-** atag: 0x4d534d70
-** <ptentry> x n
-*/
-#endif
-
-#endif
diff --git a/mkbootimg/include/abi_check/mkbootimg_abi_check.h b/mkbootimg/include/abi_check/mkbootimg_abi_check.h
new file mode 100644
index 0000000..d478aba
--- /dev/null
+++ b/mkbootimg/include/abi_check/mkbootimg_abi_check.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2018 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 <bootimg/bootimg.h>
+
+// This header has been created for the following reaons:
+// 1) In order for a change in a user defined type to be classified as API /
+// ABI breaking, it needs to be referenced by an 'exported interface'
+// (in this case the function mkbootimg_dummy).
+// 2) Since 'mkbootimg_dummy' needs to be exported, we need to have it
+// exposed through a public header.
+// 3) It is desirable not to pollute bootimg.h with interfaces which are not
+// 'used' in reality by on device binaries. Furthermore, bootimg.h might
+// be exported by a library in the future, so we must avoid polluting it.
+void mkbootimg_dummy(boot_img_hdr*);
diff --git a/mkbootimg/include/bootimg/bootimg.h b/mkbootimg/include/bootimg/bootimg.h
new file mode 100644
index 0000000..bce308b
--- /dev/null
+++ b/mkbootimg/include/bootimg/bootimg.h
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2007 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.
+ */
+
+#pragma once
+
+#include <stdint.h>
+
+#define BOOT_MAGIC "ANDROID!"
+#define BOOT_MAGIC_SIZE 8
+#define BOOT_NAME_SIZE 16
+#define BOOT_ARGS_SIZE 512
+#define BOOT_EXTRA_ARGS_SIZE 1024
+
+// The bootloader expects the structure of boot_img_hdr with header
+// version 0 to be as follows:
+struct boot_img_hdr_v0 {
+ // Must be BOOT_MAGIC.
+ uint8_t magic[BOOT_MAGIC_SIZE];
+
+ uint32_t kernel_size; /* size in bytes */
+ uint32_t kernel_addr; /* physical load addr */
+
+ uint32_t ramdisk_size; /* size in bytes */
+ uint32_t ramdisk_addr; /* physical load addr */
+
+ uint32_t second_size; /* size in bytes */
+ uint32_t second_addr; /* physical load addr */
+
+ uint32_t tags_addr; /* physical addr for kernel tags */
+ uint32_t page_size; /* flash page size we assume */
+
+ // Version of the boot image header.
+ uint32_t header_version;
+
+ // Operating system version and security patch level.
+ // For version "A.B.C" and patch level "Y-M-D":
+ // (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
+ // os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0]
+ uint32_t os_version;
+
+#if __cplusplus
+ void SetOsVersion(unsigned major, unsigned minor, unsigned patch) {
+ os_version &= ((1 << 11) - 1);
+ os_version |= (((major & 0x7f) << 25) | ((minor & 0x7f) << 18) | ((patch & 0x7f) << 11));
+ }
+
+ void SetOsPatchLevel(unsigned year, unsigned month) {
+ os_version &= ~((1 << 11) - 1);
+ os_version |= (((year - 2000) & 0x7f) << 4) | ((month & 0xf) << 0);
+ }
+#endif
+
+ uint8_t name[BOOT_NAME_SIZE]; /* asciiz product name */
+
+ uint8_t cmdline[BOOT_ARGS_SIZE];
+
+ uint32_t id[8]; /* timestamp / checksum / sha1 / etc */
+
+ // Supplemental command line data; kept here to maintain
+ // binary compatibility with older versions of mkbootimg.
+ uint8_t extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
+} __attribute__((packed));
+
+/*
+ * It is expected that callers would explicitly specify which version of the
+ * boot image header they need to use.
+ */
+typedef struct boot_img_hdr_v0 boot_img_hdr;
+
+/* When a boot header is of version 0, the structure of boot image is as
+ * follows:
+ *
+ * +-----------------+
+ * | boot header | 1 page
+ * +-----------------+
+ * | kernel | n pages
+ * +-----------------+
+ * | ramdisk | m pages
+ * +-----------------+
+ * | second stage | o pages
+ * +-----------------+
+ *
+ * n = (kernel_size + page_size - 1) / page_size
+ * m = (ramdisk_size + page_size - 1) / page_size
+ * o = (second_size + page_size - 1) / page_size
+ *
+ * 0. all entities are page_size aligned in flash
+ * 1. kernel and ramdisk are required (size != 0)
+ * 2. second is optional (second_size == 0 -> no second)
+ * 3. load each element (kernel, ramdisk, second) at
+ * the specified physical address (kernel_addr, etc)
+ * 4. prepare tags at tag_addr. kernel_args[] is
+ * appended to the kernel commandline in the tags.
+ * 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
+ * 6. if second_size != 0: jump to second_addr
+ * else: jump to kernel_addr
+ */
+
+struct boot_img_hdr_v1 : public boot_img_hdr_v0 {
+ uint32_t recovery_dtbo_size; /* size in bytes for recovery DTBO image */
+ uint64_t recovery_dtbo_offset; /* offset to recovery dtbo in boot image */
+ uint32_t header_size;
+} __attribute__((packed));
+
+/* When the boot image header has a version of 1, the structure of the boot
+ * image is as follows:
+ *
+ * +-----------------+
+ * | boot header | 1 page
+ * +-----------------+
+ * | kernel | n pages
+ * +-----------------+
+ * | ramdisk | m pages
+ * +-----------------+
+ * | second stage | o pages
+ * +-----------------+
+ * | recovery dtbo | p pages
+ * +-----------------+
+ * n = (kernel_size + page_size - 1) / page_size
+ * m = (ramdisk_size + page_size - 1) / page_size
+ * o = (second_size + page_size - 1) / page_size
+ * p = (recovery_dtbo_size + page_size - 1) / page_size
+ *
+ * 0. all entities are page_size aligned in flash
+ * 1. kernel and ramdisk are required (size != 0)
+ * 2. recovery_dtbo is required for recovery.img in non-A/B devices(recovery_dtbo_size != 0)
+ * 3. second is optional (second_size == 0 -> no second)
+ * 4. load each element (kernel, ramdisk, second) at
+ * the specified physical address (kernel_addr, etc)
+ * 5. If booting to recovery mode in a non-A/B device, extract recovery dtbo and
+ * apply the correct set of overlays on the base device tree depending on the
+ * hardware/product revision.
+ * 6. prepare tags at tag_addr. kernel_args[] is
+ * appended to the kernel commandline in the tags.
+ * 7. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
+ * 8. if second_size != 0: jump to second_addr
+ * else: jump to kernel_addr
+ */
diff --git a/mkbootimg/mkbootimg b/mkbootimg/mkbootimg
index 5a13da2..fda9af0 100755
--- a/mkbootimg/mkbootimg
+++ b/mkbootimg/mkbootimg
@@ -45,6 +45,22 @@
f.write(pack(str(pad) + 'x'))
+def get_number_of_pages(image_size, page_size):
+ """calculates the number of pages required for the image"""
+ return (image_size + page_size - 1) / page_size
+
+
+def get_recovery_dtbo_offset(args):
+ """calculates the offset of recovery_dtbo image in the boot image"""
+ num_header_pages = 1 # header occupies a page
+ num_kernel_pages = get_number_of_pages(filesize(args.kernel), args.pagesize)
+ num_ramdisk_pages = get_number_of_pages(filesize(args.ramdisk), args.pagesize)
+ num_second_pages = get_number_of_pages(filesize(args.second), args.pagesize)
+ dtbo_offset = args.pagesize * (num_header_pages + num_kernel_pages +
+ num_ramdisk_pages + num_second_pages)
+ return dtbo_offset
+
+
def write_header(args):
BOOT_MAGIC = 'ANDROID!'.encode()
args.output.write(pack('8s', BOOT_MAGIC))
@@ -57,7 +73,7 @@
args.base + args.second_offset, # physical load addr
args.base + args.tags_offset, # physical addr for kernel tags
args.pagesize, # flash page size we assume
- 0, # future expansion: MUST be 0
+ args.header_version, # version of bootimage header
(args.os_version << 11) | args.os_patch_level)) # os version and patch level
args.output.write(pack('16s', args.board.encode())) # asciiz product name
args.output.write(pack('512s', args.cmdline[:512].encode()))
@@ -66,10 +82,23 @@
update_sha(sha, args.kernel)
update_sha(sha, args.ramdisk)
update_sha(sha, args.second)
+
+ if args.header_version > 0:
+ update_sha(sha, args.recovery_dtbo)
+
img_id = pack('32s', sha.digest())
args.output.write(img_id)
args.output.write(pack('1024s', args.cmdline[512:].encode()))
+
+ if args.header_version > 0:
+ args.output.write(pack('I', filesize(args.recovery_dtbo))) # size in bytes
+ if args.recovery_dtbo:
+ args.output.write(pack('Q', get_recovery_dtbo_offset(args))) # recovery dtbo offset
+ else:
+ args.output.write(pack('Q', 0)) # Will be set to 0 for devices without a recovery dtbo
+ args.output.write(pack('I', args.output.tell() + 4)) # size of boot header
+
pad_file(args.output, args.pagesize)
return img_id
@@ -132,6 +161,7 @@
required=True)
parser.add_argument('--ramdisk', help='path to the ramdisk', type=FileType('rb'))
parser.add_argument('--second', help='path to the 2nd bootloader', type=FileType('rb'))
+ parser.add_argument('--recovery_dtbo', help='path to the recovery DTBO', type=FileType('rb'))
parser.add_argument('--cmdline', help='extra arguments to be passed on the '
'kernel command line', default='', action=ValidateStrLenAction, maxlen=1536)
parser.add_argument('--base', help='base address', type=parse_int, default=0x10000000)
@@ -150,6 +180,7 @@
choices=[2**i for i in range(11,15)], default=2048)
parser.add_argument('--id', help='print the image ID on standard output',
action='store_true')
+ parser.add_argument('--header_version', help='boot image header version', type=parse_int, default=0)
parser.add_argument('-o', '--output', help='output file name', type=FileType('wb'),
required=True)
return parser.parse_args()
@@ -160,6 +191,8 @@
write_padded_file(args.output, args.ramdisk, args.pagesize)
write_padded_file(args.output, args.second, args.pagesize)
+ if args.header_version > 0:
+ write_padded_file(args.output, args.recovery_dtbo, args.pagesize)
def main():
args = parse_cmdline()
diff --git a/adb/remount_service.h b/mkbootimg/mkbootimg_dummy.cpp
similarity index 70%
copy from adb/remount_service.h
copy to mkbootimg/mkbootimg_dummy.cpp
index 7bda1be..410d379 100644
--- a/adb/remount_service.h
+++ b/mkbootimg/mkbootimg_dummy.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -14,12 +14,11 @@
* limitations under the License.
*/
-#ifndef _REMOUNT_SERVICE_H_
-#define _REMOUNT_SERVICE_H_
+#include <abi_check/mkbootimg_abi_check.h>
-#include <string>
-
-bool make_block_device_writable(const std::string&);
-void remount_service(int, void*);
-
-#endif
+void mkbootimg_dummy(boot_img_hdr* hdr) {
+ // TODO: Hack to trigger abi checks, remove this.
+ if (hdr) {
+ hdr--;
+ }
+}
diff --git a/mkbootimg/unpack_bootimg b/mkbootimg/unpack_bootimg
new file mode 100755
index 0000000..c37acd5
--- /dev/null
+++ b/mkbootimg/unpack_bootimg
@@ -0,0 +1,134 @@
+#!/usr/bin/env python
+# Copyright 2018, 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.
+
+"""unpacks the bootimage.
+
+Extracts the kernel, ramdisk, second bootloader and recovery dtbo images.
+"""
+
+from __future__ import print_function
+from argparse import ArgumentParser, FileType
+from struct import unpack
+import os
+
+
+def create_out_dir(dir_path):
+ """creates a directory 'dir_path' if it does not exist"""
+ if not os.path.exists(dir_path):
+ os.makedirs(dir_path)
+
+
+def extract_image(offset, size, bootimage, extracted_image_name):
+ """extracts an image from the bootimage"""
+ bootimage.seek(offset)
+ with open(extracted_image_name, 'wb') as file_out:
+ file_out.write(bootimage.read(size))
+
+
+def get_number_of_pages(image_size, page_size):
+ """calculates the number of pages required for the image"""
+ return (image_size + page_size - 1) / page_size
+
+
+def unpack_bootimage(args):
+ """extracts kernel, ramdisk, second bootloader and recovery dtbo"""
+ boot_magic = unpack('8s', args.boot_img.read(8))
+ print('boot_magic: %s' % boot_magic)
+ kernel_ramdisk_second_info = unpack('10I', args.boot_img.read(10 * 4))
+ print('kernel_size: %s' % kernel_ramdisk_second_info[0])
+ print('kernel load address: %s' % kernel_ramdisk_second_info[1])
+ print('ramdisk size: %s' % kernel_ramdisk_second_info[2])
+ print('ramdisk load address: %s' % kernel_ramdisk_second_info[3])
+ print('second bootloader size: %s' % kernel_ramdisk_second_info[4])
+ print('second bootloader load address: %s' % kernel_ramdisk_second_info[5])
+ print('kernel tags load address: %s' % kernel_ramdisk_second_info[6])
+ print('page size: %s' % kernel_ramdisk_second_info[7])
+ print('boot image header version: %s' % kernel_ramdisk_second_info[8])
+ print('os version and patch level: %s' % kernel_ramdisk_second_info[9])
+
+ product_name = unpack('16s', args.boot_img.read(16))
+ print('product name: %s' % product_name)
+ cmdline = unpack('512s', args.boot_img.read(512))
+ print('command line args: %s' % cmdline)
+
+ args.boot_img.read(32) # ignore SHA
+
+ extra_cmdline = unpack('1024s', args.boot_img.read(1024))
+ print('additional command line args: %s' % extra_cmdline)
+
+ kernel_size = kernel_ramdisk_second_info[0]
+ ramdisk_size = kernel_ramdisk_second_info[2]
+ second_size = kernel_ramdisk_second_info[4]
+ page_size = kernel_ramdisk_second_info[7]
+ version = kernel_ramdisk_second_info[8]
+ if version > 0:
+ recovery_dtbo_size = unpack('I', args.boot_img.read(1 * 4))[0]
+ print('recovery dtbo size: %s' % recovery_dtbo_size)
+ recovery_dtbo_offset = unpack('Q', args.boot_img.read(8))[0]
+ print('recovery dtbo offset: %s' % recovery_dtbo_offset)
+ boot_header_size = unpack('I', args.boot_img.read(4))[0]
+ print('boot header size: %s' % boot_header_size)
+ else:
+ recovery_dtbo_size = 0
+
+ # The first page contains the boot header
+ num_header_pages = 1
+
+ num_kernel_pages = get_number_of_pages(kernel_size, page_size)
+ kernel_offset = page_size * num_header_pages # header occupies a page
+ image_info_list = [(kernel_offset, kernel_size, 'kernel')]
+
+ num_ramdisk_pages = get_number_of_pages(ramdisk_size, page_size)
+ ramdisk_offset = page_size * (num_header_pages + num_kernel_pages
+ ) # header + kernel
+ image_info_list.append((ramdisk_offset, ramdisk_size, 'ramdisk'))
+
+ second_offset = page_size * (
+ num_header_pages + num_kernel_pages + num_ramdisk_pages
+ ) # header + kernel + ramdisk
+ image_info_list.append((second_offset, second_size, 'second'))
+
+ if recovery_dtbo_size > 0:
+ image_info_list.append((recovery_dtbo_offset, recovery_dtbo_size,
+ 'recovery_dtbo'))
+
+ for image_info in image_info_list:
+ extract_image(image_info[0], image_info[1], args.boot_img,
+ os.path.join(args.out, image_info[2]))
+
+
+def parse_cmdline():
+ """parse command line arguments"""
+ parser = ArgumentParser(
+ description='Unpacks boot.img/recovery.img, extracts the kernel,'
+ 'ramdisk, second bootloader and recovery dtbo')
+ parser.add_argument(
+ '--boot_img',
+ help='path to boot image',
+ type=FileType('rb'),
+ required=True)
+ parser.add_argument('--out', help='path to out binaries', default='out')
+ return parser.parse_args()
+
+
+def main():
+ """parse arguments and unpack boot image"""
+ args = parse_cmdline()
+ create_out_dir(args.out)
+ unpack_bootimage(args)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/property_service/libpropertyinfoparser/Android.bp b/property_service/libpropertyinfoparser/Android.bp
index ea9b968..70f6faa 100644
--- a/property_service/libpropertyinfoparser/Android.bp
+++ b/property_service/libpropertyinfoparser/Android.bp
@@ -2,6 +2,7 @@
name: "libpropertyinfoparser",
host_supported: true,
vendor_available: true,
+ recovery_available: true,
srcs: ["property_info_parser.cpp"],
cpp_std: "experimental",
diff --git a/property_service/libpropertyinfoserializer/Android.bp b/property_service/libpropertyinfoserializer/Android.bp
index 72ae19a..51c1226 100644
--- a/property_service/libpropertyinfoserializer/Android.bp
+++ b/property_service/libpropertyinfoserializer/Android.bp
@@ -17,6 +17,7 @@
cc_library_static {
name: "libpropertyinfoserializer",
defaults: ["propertyinfoserializer_defaults"],
+ recovery_available: true,
srcs: [
"property_info_file.cpp",
"property_info_serializer.cpp",
@@ -35,4 +36,5 @@
"property_info_serializer_test.cpp",
],
static_libs: ["libpropertyinfoserializer"],
+ test_suites: ["device-tests"],
}
diff --git a/property_service/property_info_checker/Android.bp b/property_service/property_info_checker/Android.bp
index 6ee649a..7d66199 100644
--- a/property_service/property_info_checker/Android.bp
+++ b/property_service/property_info_checker/Android.bp
@@ -7,6 +7,7 @@
"libpropertyinfoserializer",
"libpropertyinfoparser",
"libbase",
+ "libsepol",
],
srcs: ["property_info_checker.cpp"],
}
diff --git a/property_service/property_info_checker/property_info_checker.cpp b/property_service/property_info_checker/property_info_checker.cpp
index e4f8264..52c4383 100644
--- a/property_service/property_info_checker/property_info_checker.cpp
+++ b/property_service/property_info_checker/property_info_checker.cpp
@@ -1,26 +1,150 @@
#include <iostream>
+#include <memory>
#include <string>
#include <vector>
#include <android-base/file.h>
-
+#include <property_info_parser/property_info_parser.h>
#include <property_info_serializer/property_info_serializer.h>
+#include <sepol/context.h>
+#include <sepol/context_record.h>
+#include <sepol/handle.h>
+#include <sepol/policydb.h>
+#include <sepol/policydb/policydb.h>
using android::base::ReadFileToString;
using android::properties::BuildTrie;
using android::properties::ParsePropertyInfoFile;
+using android::properties::PropertyInfoArea;
using android::properties::PropertyInfoEntry;
+class ContextChecker {
+ public:
+ ContextChecker()
+ : policy_file_(nullptr),
+ sepol_handle_(nullptr),
+ sepol_policy_file_(nullptr),
+ sepol_policy_db_(nullptr) {}
+
+ ~ContextChecker() {
+ if (sepol_policy_db_ != nullptr) {
+ sepol_policydb_free(sepol_policy_db_);
+ }
+
+ if (sepol_policy_file_ != nullptr) {
+ sepol_policy_file_free(sepol_policy_file_);
+ }
+
+ if (sepol_handle_ != nullptr) {
+ sepol_handle_destroy(sepol_handle_);
+ }
+
+ if (policy_file_ != nullptr) {
+ fclose(policy_file_);
+ }
+ }
+
+ bool Initialize(const char* policy_file) {
+ policy_file_ = fopen(policy_file, "re");
+ if (policy_file_ == nullptr) {
+ std::cerr << "Could not open policy file, " << policy_file << std::endl;
+ return false;
+ }
+
+ sepol_handle_ = sepol_handle_create();
+ if (sepol_handle_ == nullptr) {
+ std::cerr << "Could not create policy handle." << std::endl;
+ return false;
+ }
+
+ if (sepol_policy_file_create(&sepol_policy_file_) < 0) {
+ std::cerr << "Could not create policy file." << std::endl;
+ return false;
+ }
+
+ if (sepol_policydb_create(&sepol_policy_db_) < 0) {
+ std::cerr << "Could not create policy db." << std::endl;
+ return false;
+ }
+
+ sepol_policy_file_set_fp(sepol_policy_file_, policy_file_);
+ sepol_policy_file_set_handle(sepol_policy_file_, sepol_handle_);
+
+ if (sepol_policydb_read(sepol_policy_db_, sepol_policy_file_) < 0) {
+ std::cerr << "Could not read policy file into policy db." << std::endl;
+ return false;
+ }
+
+ auto* attr =
+ reinterpret_cast<type_datum*>(hashtab_search(policy_db_->p_types.table, "property_type"));
+ if (attr == nullptr || attr->flavor != TYPE_ATTRIB) {
+ std::cerr << "'property_type' is not defined correctly." << std::endl;
+ return false;
+ }
+
+ property_type_bit_ = attr->s.value - 1;
+
+ return true;
+ }
+
+ bool CheckContext(const char* context) {
+ sepol_context_t* sepol_context_raw;
+ if (sepol_context_from_string(sepol_handle_, context, &sepol_context_raw) < 0) {
+ std::cerr << "Could not allocate context for " << context << std::endl;
+ return false;
+ }
+ auto sepol_context = std::unique_ptr<sepol_context_t, decltype(&sepol_context_free)>{
+ sepol_context_raw, sepol_context_free};
+
+ if (sepol_context_check(sepol_handle_, sepol_policy_db_, sepol_context.get()) < 0) {
+ std::cerr << "Sepol context check failed for " << context << std::endl;
+ return false;
+ }
+
+ const char* context_type = sepol_context_get_type(sepol_context.get());
+
+ auto* type =
+ reinterpret_cast<type_datum*>(hashtab_search(policy_db_->p_types.table, context_type));
+ if (type == nullptr) {
+ std::cerr << "Could not find context '" << context << "' in policy database" << std::endl;
+ return false;
+ }
+
+ if (type->flavor != TYPE_TYPE) {
+ std::cerr << "Context '" << context << "' is not defined as a type in policy database"
+ << std::endl;
+ return false;
+ }
+
+ if (!ebitmap_get_bit(&policy_db_->type_attr_map[type->s.value - 1], property_type_bit_)) {
+ std::cerr << "Context '" << context << "' does not have property_type attribute" << std::endl;
+ return false;
+ }
+
+ return true;
+ }
+
+ private:
+ FILE* policy_file_;
+ sepol_handle_t* sepol_handle_;
+ sepol_policy_file_t* sepol_policy_file_;
+ union {
+ sepol_policydb_t* sepol_policy_db_;
+ policydb_t* policy_db_;
+ };
+ unsigned int property_type_bit_;
+};
+
int main(int argc, char** argv) {
- if (argc < 2) {
- std::cerr << "A list of property info files to be checked is expected on the command line"
- << std::endl;
+ if (argc < 3) {
+ std::cerr << "usage: " << argv[0]
+ << " COMPILED_SEPOLICY PROPERTY_INFO_FILE [PROPERTY_INFO_FILE]..." << std::endl;
return -1;
}
auto property_info_entries = std::vector<PropertyInfoEntry>{};
- for (int i = 1; i < argc; ++i) {
+ for (int i = 2; i < argc; ++i) {
auto filename = argv[i];
auto file_contents = std::string{};
if (!ReadFileToString(filename, &file_contents)) {
@@ -47,5 +171,17 @@
return -1;
}
+ auto checker = ContextChecker{};
+ if (!checker.Initialize(argv[1])) {
+ return -1;
+ }
+
+ auto property_info_area = reinterpret_cast<PropertyInfoArea*>(serialized_contexts.data());
+ for (size_t i = 0; i < property_info_area->num_contexts(); ++i) {
+ if (!checker.CheckContext(property_info_area->context(i))) {
+ return -1;
+ }
+ }
+
return 0;
}
diff --git a/qemu_pipe/Android.bp b/qemu_pipe/Android.bp
index 93c347b..c6bda4a 100644
--- a/qemu_pipe/Android.bp
+++ b/qemu_pipe/Android.bp
@@ -3,6 +3,7 @@
cc_library_static {
name: "libqemu_pipe",
vendor_available: true,
+ recovery_available: true,
sanitize: {
misc_undefined: ["integer"],
},
diff --git a/rootdir/Android.mk b/rootdir/Android.mk
index feb100e..2429b49 100644
--- a/rootdir/Android.mk
+++ b/rootdir/Android.mk
@@ -93,6 +93,31 @@
else
LOCAL_POST_INSTALL_CMD += ; ln -sf /system/product $(TARGET_ROOT_OUT)/product
endif
+ifdef BOARD_USES_PRODUCT_SERVICESIMAGE
+ LOCAL_POST_INSTALL_CMD += ; mkdir -p $(TARGET_ROOT_OUT)/product-services
+else
+ LOCAL_POST_INSTALL_CMD += ; ln -sf /system/product-services $(TARGET_ROOT_OUT)/product-services
+endif
+ifdef BOARD_USES_METADATA_PARTITION
+ LOCAL_POST_INSTALL_CMD += ; mkdir -p $(TARGET_ROOT_OUT)/metadata
+endif
+
+# For /odm partition.
+LOCAL_POST_INSTALL_CMD += ; mkdir -p $(TARGET_ROOT_OUT)/odm
+# For Treble Generic System Image (GSI), system-as-root GSI needs to work on
+# both devices with and without /odm partition. Those symlinks are for devices
+# without /odm partition. For devices with /odm partition, mount odm.img under
+# /odm will hide those symlinks.
+LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/app $(TARGET_ROOT_OUT)/odm/app
+LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/bin $(TARGET_ROOT_OUT)/odm/bin
+LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/etc $(TARGET_ROOT_OUT)/odm/etc
+LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/firmware $(TARGET_ROOT_OUT)/odm/firmware
+LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/framework $(TARGET_ROOT_OUT)/odm/framework
+LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/lib $(TARGET_ROOT_OUT)/odm/lib
+LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/lib64 $(TARGET_ROOT_OUT)/odm/lib64
+LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/overlay $(TARGET_ROOT_OUT)/odm/overlay
+LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/priv-app $(TARGET_ROOT_OUT)/odm/priv-app
+
ifdef BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE
LOCAL_POST_INSTALL_CMD += ; mkdir -p $(TARGET_ROOT_OUT)/cache
else
@@ -130,73 +155,30 @@
bcp_md5 :=
bcp_dep :=
-# If BOARD_VNDK_VERSION is defined, append PLATFORM_VNDK_VERSION to base name.
+# Append PLATFORM_VNDK_VERSION to base name.
define append_vndk_version
$(strip \
- $(if $(BOARD_VNDK_VERSION), \
- $(basename $(1)).$(PLATFORM_VNDK_VERSION)$(suffix $(1)), \
- $(1) \
- ) \
+ $(basename $(1)).$(PLATFORM_VNDK_VERSION)$(suffix $(1)) \
)
endef
-# Update namespace configuration file with library lists and VNDK version
+
+#######################################
+# ld.config.txt selection variables
#
-# $(1): Input source file (ld.config.txt)
-# $(2): Output built module
-# $(3): VNDK version suffix
-# $(4): true if libz must be included in llndk not in vndk-sp
-define update_and_install_ld_config
-# If $(4) is true, move libz to llndk from vndk-sp.
-$(if $(filter true,$(4)),\
- $(eval llndk_libraries_list := $(LLNDK_LIBRARIES) libz) \
- $(eval vndksp_libraries_list := $(filter-out libz,$(VNDK_SAMEPROCESS_LIBRARIES))),\
- $(eval llndk_libraries_list := $(LLNDK_LIBRARIES)) \
- $(eval vndksp_libraries_list := $(VNDK_SAMEPROCESS_LIBRARIES)))
+_enforce_vndk_at_runtime := false
+ifdef BOARD_VNDK_VERSION
+ ifneq ($(BOARD_VNDK_RUNTIME_DISABLE),true)
+ _enforce_vndk_at_runtime := true
+ endif
+endif
-llndk_libraries := $(call normalize-path-list,$(addsuffix .so,\
- $(filter-out $(VNDK_PRIVATE_LIBRARIES),$(llndk_libraries_list))))
-private_llndk_libraries := $(call normalize-path-list,$(addsuffix .so,\
- $(filter $(VNDK_PRIVATE_LIBRARIES),$(llndk_libraries_list))))
-vndk_sameprocess_libraries := $(call normalize-path-list,$(addsuffix .so,\
- $(filter-out $(VNDK_PRIVATE_LIBRARIES),$(vndksp_libraries_list))))
-vndk_core_libraries := $(call normalize-path-list,$(addsuffix .so,\
- $(filter-out $(VNDK_PRIVATE_LIBRARIES),$(VNDK_CORE_LIBRARIES))))
-sanitizer_runtime_libraries := $(call normalize-path-list,$(addsuffix .so,\
- $(ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
- $(UBSAN_RUNTIME_LIBRARY) \
- $(TSAN_RUNTIME_LIBRARY) \
- $(2ND_ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
- $(2ND_UBSAN_RUNTIME_LIBRARY) \
- $(2ND_TSAN_RUNTIME_LIBRARY)))
-# If BOARD_VNDK_VERSION is not defined, VNDK version suffix will not be used.
-vndk_version_suffix := $(if $(strip $(3)),-$(strip $(3)))
-
-$(2): PRIVATE_LLNDK_LIBRARIES := $$(llndk_libraries)
-$(2): PRIVATE_PRIVATE_LLNDK_LIBRARIES := $$(private_llndk_libraries)
-$(2): PRIVATE_VNDK_SAMEPROCESS_LIBRARIES := $$(vndk_sameprocess_libraries)
-$(2): PRIVATE_VNDK_CORE_LIBRARIES := $$(vndk_core_libraries)
-$(2): PRIVATE_SANITIZER_RUNTIME_LIBRARIES := $$(sanitizer_runtime_libraries)
-$(2): PRIVATE_VNDK_VERSION := $$(vndk_version_suffix)
-$(2): $(1)
- @echo "Generate: $$< -> $$@"
- @mkdir -p $$(dir $$@)
- $$(hide) sed -e 's?%LLNDK_LIBRARIES%?$$(PRIVATE_LLNDK_LIBRARIES)?g' $$< >$$@
- $$(hide) sed -i -e 's?%PRIVATE_LLNDK_LIBRARIES%?$$(PRIVATE_PRIVATE_LLNDK_LIBRARIES)?g' $$@
- $$(hide) sed -i -e 's?%VNDK_SAMEPROCESS_LIBRARIES%?$$(PRIVATE_VNDK_SAMEPROCESS_LIBRARIES)?g' $$@
- $$(hide) sed -i -e 's?%VNDK_CORE_LIBRARIES%?$$(PRIVATE_VNDK_CORE_LIBRARIES)?g' $$@
- $$(hide) sed -i -e 's?%SANITIZER_RUNTIME_LIBRARIES%?$$(PRIVATE_SANITIZER_RUNTIME_LIBRARIES)?g' $$@
- $$(hide) sed -i -e 's?%VNDK_VER%?$$(PRIVATE_VNDK_VERSION)?g' $$@
-
-llndk_libraries_list :=
-vndksp_libraries_list :=
-llndk_libraries :=
-private_llndk_libraries :=
-vndk_sameprocess_libraries :=
-vndk_core_libraries :=
-sanitizer_runtime_libraries :=
-vndk_version_suffix :=
-endef # update_and_install_ld_config
+_enforce_vndk_lite_at_runtime := false
+ifeq ($(_enforce_vndk_at_runtime),false)
+ ifeq ($(PRODUCT_TREBLE_LINKER_NAMESPACES)|$(SANITIZE_TARGET),true|)
+ _enforce_vndk_lite_at_runtime := true
+ endif
+endif
#######################################
# ld.config.txt
@@ -204,73 +186,112 @@
# For VNDK enforced devices that have defined BOARD_VNDK_VERSION, use
# "ld.config.txt" as a source file. This configuration includes strict VNDK
# run-time restrictions for vendor process.
+#
# Other treblized devices, that have not defined BOARD_VNDK_VERSION or that
# have set BOARD_VNDK_RUNTIME_DISABLE to true, use "ld.config.vndk_lite.txt"
# as a source file. This configuration does not have strict VNDK run-time
# restrictions.
+#
# If the device is not treblized, use "ld.config.legacy.txt" for legacy
# namespace configuration.
+#
include $(CLEAR_VARS)
LOCAL_MODULE := ld.config.txt
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)
-_enforce_vndk_at_runtime := false
-ifdef BOARD_VNDK_VERSION
-ifneq ($(BOARD_VNDK_RUNTIME_DISABLE),true)
- _enforce_vndk_at_runtime := true
-endif
-endif
-
ifeq ($(_enforce_vndk_at_runtime),true)
+
# for VNDK enforced devices
LOCAL_MODULE_STEM := $(call append_vndk_version,$(LOCAL_MODULE))
include $(BUILD_SYSTEM)/base_rules.mk
-$(eval $(call update_and_install_ld_config,\
- $(LOCAL_PATH)/etc/ld.config.txt,\
- $(LOCAL_BUILT_MODULE),\
- $(PLATFORM_VNDK_VERSION)))
+ld_config_template := $(LOCAL_PATH)/etc/ld.config.txt
+vndk_version := $(PLATFORM_VNDK_VERSION)
+include $(LOCAL_PATH)/update_and_install_ld_config.mk
-else ifeq ($(PRODUCT_TREBLE_LINKER_NAMESPACES)|$(SANITIZE_TARGET),true|)
-# for treblized but VNDK non-enforced devices
-LOCAL_MODULE_STEM := $(call append_vndk_version,$(LOCAL_MODULE))
+else ifeq ($(_enforce_vndk_lite_at_runtime),true)
+
+# for treblized but VNDK lightly enforced devices
+LOCAL_MODULE_STEM := ld.config.vndk_lite.txt
include $(BUILD_SYSTEM)/base_rules.mk
-$(eval $(call update_and_install_ld_config,\
- $(LOCAL_PATH)/etc/ld.config.vndk_lite.txt,\
- $(LOCAL_BUILT_MODULE),\
- $(if $(BOARD_VNDK_VERSION),$(PLATFORM_VNDK_VERSION)),\
- true))
+ld_config_template := $(LOCAL_PATH)/etc/ld.config.vndk_lite.txt
+vndk_version := $(PLATFORM_VNDK_VERSION)
+libz_is_llndk := true
+include $(LOCAL_PATH)/update_and_install_ld_config.mk
else
+
# for legacy non-treblized devices
-LOCAL_SRC_FILES := etc/ld.config.legacy.txt
LOCAL_MODULE_STEM := $(LOCAL_MODULE)
+LOCAL_SRC_FILES := etc/ld.config.legacy.txt
include $(BUILD_PREBUILT)
-endif # if _enforce_vndk_at_runtime is true
+endif # ifeq ($(_enforce_vndk_at_runtime),true)
-_enforce_vndk_at_runtime :=
+# ld.config.txt for VNDK versions older than PLATFORM_VNDK_VERSION
+# are built with the VNDK libraries lists under /prebuilts/vndk.
+#
+# ld.config.$(VER).txt is built and installed for all VNDK versions
+# listed in PRODUCT_EXTRA_VNDK_VERSIONS.
+#
+# $(1): VNDK version
+define build_versioned_ld_config
+include $(CLEAR_VARS)
+LOCAL_MODULE := ld.config.$(1).txt
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)
+LOCAL_MODULE_STEM := $$(LOCAL_MODULE)
+include $(BUILD_SYSTEM)/base_rules.mk
+ld_config_template := $(LOCAL_PATH)/etc/ld.config.txt
+vndk_version := $(1)
+lib_list_from_prebuilts := true
+include $(LOCAL_PATH)/update_and_install_ld_config.mk
+endef
+
+# For VNDK snapshot versions prior to 28, ld.config.txt is installed from the
+# prebuilt under /prebuilts/vndk
+vndk_snapshots := $(wildcard prebuilts/vndk/*)
+supported_vndk_snapshot_versions := \
+ $(strip $(foreach ver,$(patsubst prebuilts/vndk/v%,%,$(vndk_snapshots)),\
+ $(if $(call math_gt_or_eq,$(ver),28),$(ver),)))
+$(eval $(foreach ver,$(supported_vndk_snapshot_versions),\
+ $(call build_versioned_ld_config,$(ver))))
+
+vndk_snapshots :=
+supported_vndk_snapshot_versions :=
#######################################
-# ld.config.noenforce.txt
+# ld.config.vndk_lite.txt
#
-# This file is a temporary configuration file only for GSI. Originally GSI has
-# BOARD_VNDK_VERSION defined and has strict VNDK enforcing rule based on
-# "ld.config.txt". However for the devices, that have not defined
-# BOARD_VNDK_VERSION, GSI provides this configuration file which is based on
-# "ld.config.vndk_lite.txt".
-# Do not install this file for the devices other than GSI.
+# This module is only for GSI.
+#
+ifeq ($(_enforce_vndk_lite_at_runtime),false)
+
include $(CLEAR_VARS)
-LOCAL_MODULE := ld.config.noenforce.txt
+LOCAL_MODULE := ld.config.vndk_lite.txt
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)
LOCAL_MODULE_STEM := $(LOCAL_MODULE)
include $(BUILD_SYSTEM)/base_rules.mk
-$(eval $(call update_and_install_ld_config,\
- $(LOCAL_PATH)/etc/ld.config.vndk_lite.txt,\
- $(LOCAL_BUILT_MODULE),\
- $(PLATFORM_VNDK_VERSION),\
- true))
+ld_config_template := $(LOCAL_PATH)/etc/ld.config.vndk_lite.txt
+vndk_version := $(PLATFORM_VNDK_VERSION)
+libz_is_llndk := true
+include $(LOCAL_PATH)/update_and_install_ld_config.mk
+
+endif # ifeq ($(_enforce_vndk_lite_at_runtime),false)
+
+_enforce_vndk_at_runtime :=
+_enforce_vndk_lite_at_runtime :=
+
+#######################################
+# ld.config.txt for recovery
+include $(CLEAR_VARS)
+LOCAL_MODULE := ld.config.recovery.txt
+LOCAL_MODULE_CLASS := ETC
+LOCAL_SRC_FILES := etc/ld.config.recovery.txt
+LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/etc
+LOCAL_MODULE_STEM := ld.config.txt
+include $(BUILD_PREBUILT)
#######################################
# llndk.libraries.txt
diff --git a/rootdir/etc/ld.config.legacy.txt b/rootdir/etc/ld.config.legacy.txt
index d55ec57..ca6aafe 100644
--- a/rootdir/etc/ld.config.legacy.txt
+++ b/rootdir/etc/ld.config.legacy.txt
@@ -10,6 +10,9 @@
dir.legacy = /odm
dir.legacy = /sbin
+# Except for /postinstall, where only /system is searched
+dir.postinstall = /postinstall
+
[legacy]
namespace.default.isolated = false
@@ -23,3 +26,15 @@
namespace.default.asan.search.paths += /odm/${LIB}
namespace.default.asan.search.paths += /data/asan/vendor/${LIB}
namespace.default.asan.search.paths += /vendor/${LIB}
+
+###############################################################################
+# Namespace config for binaries under /postinstall.
+# Only one default namespace is defined and it has no directories other than
+# /system/lib in the search paths. This is because linker calls realpath on the
+# search paths and this causes selinux denial if the paths (/vendor, /odm) are
+# not allowed to the poinstall binaries. There is no reason to allow the
+# binaries to access the paths.
+###############################################################################
+[postinstall]
+namespace.default.isolated = false
+namespace.default.search.paths = /system/${LIB}
diff --git a/rootdir/etc/ld.config.recovery.txt b/rootdir/etc/ld.config.recovery.txt
new file mode 100644
index 0000000..5d6c01a
--- /dev/null
+++ b/rootdir/etc/ld.config.recovery.txt
@@ -0,0 +1,9 @@
+# Copyright (C) 2018 The Android Open Source Project
+#
+# Bionic loader config file for recovery mode
+#
+
+dir.recovery = /system/bin
+
+[recovery]
+namespace.default.search.paths = /system/${LIB}
diff --git a/rootdir/etc/ld.config.txt b/rootdir/etc/ld.config.txt
index c8d87c8..620f0fd 100644
--- a/rootdir/etc/ld.config.txt
+++ b/rootdir/etc/ld.config.txt
@@ -7,6 +7,8 @@
# absolute path of an executable is selected.
dir.system = /system/bin/
dir.system = /system/xbin/
+dir.system = /%PRODUCT%/bin/
+dir.system = /%PRODUCT_SERVICES%/bin/
dir.vendor = /odm/bin/
dir.vendor = /vendor/bin/
@@ -24,6 +26,8 @@
dir.system = /data/benchmarktest
dir.system = /data/benchmarktest64
+dir.postinstall = /postinstall
+
[system]
additional.namespaces = sphal,vndk,rs
@@ -35,7 +39,9 @@
###############################################################################
namespace.default.isolated = true
-namespace.default.search.paths = /system/${LIB}
+namespace.default.search.paths = /system/${LIB}
+namespace.default.search.paths += /%PRODUCT%/${LIB}
+namespace.default.search.paths += /%PRODUCT_SERVICES%/${LIB}
# We can't have entire /system/${LIB} as permitted paths because doing so
# makes it possible to load libs in /system/${LIB}/vndk* directories by
@@ -47,6 +53,8 @@
namespace.default.permitted.paths = /system/${LIB}/drm
namespace.default.permitted.paths += /system/${LIB}/extractors
namespace.default.permitted.paths += /system/${LIB}/hw
+namespace.default.permitted.paths += /%PRODUCT%/${LIB}
+namespace.default.permitted.paths += /%PRODUCT_SERVICES%/${LIB}
# These are where odex files are located. libart has to be able to dlopen the files
namespace.default.permitted.paths += /system/framework
namespace.default.permitted.paths += /system/app
@@ -54,15 +62,25 @@
namespace.default.permitted.paths += /vendor/framework
namespace.default.permitted.paths += /vendor/app
namespace.default.permitted.paths += /vendor/priv-app
+namespace.default.permitted.paths += /odm/framework
+namespace.default.permitted.paths += /odm/app
+namespace.default.permitted.paths += /odm/priv-app
namespace.default.permitted.paths += /oem/app
-namespace.default.permitted.paths += /product/framework
-namespace.default.permitted.paths += /product/app
-namespace.default.permitted.paths += /product/priv-app
+namespace.default.permitted.paths += /%PRODUCT%/framework
+namespace.default.permitted.paths += /%PRODUCT%/app
+namespace.default.permitted.paths += /%PRODUCT%/priv-app
+namespace.default.permitted.paths += /%PRODUCT_SERVICES%/framework
+namespace.default.permitted.paths += /%PRODUCT_SERVICES%/app
+namespace.default.permitted.paths += /%PRODUCT_SERVICES%/priv-app
namespace.default.permitted.paths += /data
namespace.default.permitted.paths += /mnt/expand
namespace.default.asan.search.paths = /data/asan/system/${LIB}
namespace.default.asan.search.paths += /system/${LIB}
+namespace.default.asan.search.paths += /data/asan/product/${LIB}
+namespace.default.asan.search.paths += /product/${LIB}
+namespace.default.asan.search.paths += /data/asan/product-services/${LIB}
+namespace.default.asan.search.paths += /product-services/${LIB}
namespace.default.asan.permitted.paths = /data
namespace.default.asan.permitted.paths += /system/${LIB}/drm
@@ -74,10 +92,18 @@
namespace.default.asan.permitted.paths += /vendor/framework
namespace.default.asan.permitted.paths += /vendor/app
namespace.default.asan.permitted.paths += /vendor/priv-app
+namespace.default.asan.permitted.paths += /odm/framework
+namespace.default.asan.permitted.paths += /odm/app
+namespace.default.asan.permitted.paths += /odm/priv-app
namespace.default.asan.permitted.paths += /oem/app
-namespace.default.asan.permitted.paths += /product/framework
-namespace.default.asan.permitted.paths += /product/app
-namespace.default.asan.permitted.paths += /product/priv-app
+namespace.default.asan.permitted.paths += /%PRODUCT%/${LIB}
+namespace.default.asan.permitted.paths += /%PRODUCT%/framework
+namespace.default.asan.permitted.paths += /%PRODUCT%/app
+namespace.default.asan.permitted.paths += /%PRODUCT%/priv-app
+namespace.default.asan.permitted.paths += /%PRODUCT_SERVICES%/${LIB}
+namespace.default.asan.permitted.paths += /%PRODUCT_SERVICES%/framework
+namespace.default.asan.permitted.paths += /%PRODUCT_SERVICES%/app
+namespace.default.asan.permitted.paths += /%PRODUCT_SERVICES%/priv-app
namespace.default.asan.permitted.paths += /mnt/expand
###############################################################################
@@ -210,13 +236,20 @@
namespace.vndk.asan.permitted.paths += /data/asan/system/${LIB}/vndk-sp%VNDK_VER%/hw
namespace.vndk.asan.permitted.paths += /system/${LIB}/vndk-sp%VNDK_VER%/hw
+# The "vndk" namespace links to "default" namespace for LLNDK libs and links to
+# "sphal" namespace for vendor libs. The ordering matters. The "default"
+# namespace has higher priority than the "sphal" namespace.
+namespace.vndk.links = default,sphal
+
# When these NDK libs are required inside this namespace, then it is redirected
# to the default namespace. This is possible since their ABI is stable across
# Android releases.
-namespace.vndk.links = default
namespace.vndk.link.default.shared_libs = %LLNDK_LIBRARIES%
namespace.vndk.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
+# Allow VNDK-SP extensions to use vendor libraries
+namespace.vndk.link.sphal.allow_all_shared_libs = true
+
###############################################################################
# Namespace config for vendor processes. In O, no restriction is enforced for
# them. However, in O-MR1, access to /system/${LIB} will not be allowed to
@@ -305,7 +338,25 @@
###############################################################################
namespace.system.isolated = false
-namespace.system.search.paths = /system/${LIB}
+namespace.system.search.paths = /system/${LIB}
+namespace.system.search.paths += /%PRODUCT%/${LIB}
+namespace.system.search.paths += /%PRODUCT_SERVICES%/${LIB}
namespace.system.asan.search.paths = /data/asan/system/${LIB}
namespace.system.asan.search.paths += /system/${LIB}
+namespace.system.asan.search.paths += /data/asan/product/${LIB}
+namespace.system.asan.search.paths += /product/${LIB}
+
+###############################################################################
+# Namespace config for binaries under /postinstall.
+# Only one default namespace is defined and it has no directories other than
+# /system/lib in the search paths. This is because linker calls realpath on the
+# search paths and this causes selinux denial if the paths (/vendor, /odm) are
+# not allowed to the poinstall binaries. There is no reason to allow the
+# binaries to access the paths.
+###############################################################################
+[postinstall]
+namespace.default.isolated = false
+namespace.default.search.paths = /system/${LIB}
+namespace.default.search.paths += /%PRODUCT%/${LIB}
+namespace.default.search.paths += /%PRODUCT_SERVICES%/${LIB}
diff --git a/rootdir/etc/ld.config.vndk_lite.txt b/rootdir/etc/ld.config.vndk_lite.txt
index 5256cb1..db65c14 100644
--- a/rootdir/etc/ld.config.vndk_lite.txt
+++ b/rootdir/etc/ld.config.vndk_lite.txt
@@ -7,6 +7,7 @@
# absolute path of an executable is selected.
dir.system = /system/bin/
dir.system = /system/xbin/
+dir.system = /product/bin/
dir.vendor = /odm/bin/
dir.vendor = /vendor/bin/
@@ -24,6 +25,8 @@
dir.system = /data/benchmarktest
dir.system = /data/benchmarktest64
+dir.postinstall = /postinstall
+
[system]
additional.namespaces = sphal,vndk,rs
@@ -38,6 +41,7 @@
namespace.default.search.paths = /system/${LIB}
namespace.default.search.paths += /odm/${LIB}
namespace.default.search.paths += /vendor/${LIB}
+namespace.default.search.paths += /product/${LIB}
namespace.default.asan.search.paths = /data/asan/system/${LIB}
namespace.default.asan.search.paths += /system/${LIB}
@@ -45,6 +49,8 @@
namespace.default.asan.search.paths += /odm/${LIB}
namespace.default.asan.search.paths += /data/asan/vendor/${LIB}
namespace.default.asan.search.paths += /vendor/${LIB}
+namespace.default.asan.search.paths += /data/asan/product/${LIB}
+namespace.default.asan.search.paths += /product/${LIB}
###############################################################################
# "sphal" namespace
@@ -203,6 +209,7 @@
namespace.default.search.paths += /system/${LIB}/vndk%VNDK_VER%
namespace.default.search.paths += /system/${LIB}/vndk-sp%VNDK_VER%
namespace.default.search.paths += /system/${LIB}
+namespace.default.search.paths += /product/${LIB}
namespace.default.asan.search.paths = /data/asan/odm/${LIB}
namespace.default.asan.search.paths += /odm/${LIB}
@@ -222,3 +229,18 @@
namespace.default.asan.search.paths += /system/${LIB}/vndk-sp%VNDK_VER%
namespace.default.asan.search.paths += /data/asan/system/${LIB}
namespace.default.asan.search.paths += /system/${LIB}
+namespace.default.asan.search.paths += /data/asan/product/${LIB}
+namespace.default.asan.search.paths += /product/${LIB}
+
+###############################################################################
+# Namespace config for binaries under /postinstall.
+# Only one default namespace is defined and it has no directories other than
+# /system/lib in the search paths. This is because linker calls realpath on the
+# search paths and this causes selinux denial if the paths (/vendor, /odm) are
+# not allowed to the poinstall binaries. There is no reason to allow the
+# binaries to access the paths.
+###############################################################################
+[postinstall]
+namespace.default.isolated = false
+namespace.default.search.paths = /system/${LIB}
+namespace.default.search.paths += /product/${LIB}
diff --git a/rootdir/etc/public.libraries.iot.txt b/rootdir/etc/public.libraries.iot.txt
new file mode 100644
index 0000000..ff0813d
--- /dev/null
+++ b/rootdir/etc/public.libraries.iot.txt
@@ -0,0 +1,27 @@
+# See https://android.googlesource.com/platform/ndk/+/master/docs/PlatformApis.md
+libandroid.so
+libandroidthings.so
+libaaudio.so
+libc.so
+libcamera2ndk.so
+libdl.so
+libEGL.so
+libGLESv1_CM.so
+libGLESv2.so
+libGLESv3.so
+libicui18n.so
+libicuuc.so
+libjnigraphics.so
+liblog.so
+libmediandk.so
+libm.so
+libnativewindow.so
+libneuralnetworks.so
+libOpenMAXAL.so
+libOpenSLES.so
+libRS.so
+libstdc++.so
+libsync.so
+libvulkan.so
+libwebviewchromium_plat_support.so
+libz.so
diff --git a/rootdir/init.rc b/rootdir/init.rc
index a213ffb..bf22951 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -21,19 +21,20 @@
# Set the security context of /adb_keys if present.
restorecon /adb_keys
- # Shouldn't be necessary, but sdcard won't start without it. http://b/22568628.
- mkdir /mnt 0775 root system
-
# Set the security context of /postinstall if present.
restorecon /postinstall
# Mount cgroup mount point for cpu accounting
mount cgroup none /acct nodev noexec nosuid cpuacct
+ chmod 0555 /acct
mkdir /acct/uid
# root memory control cgroup, used by lmkd
mkdir /dev/memcg 0700 root system
mount cgroup none /dev/memcg nodev noexec nosuid memory
+ # memory.pressure_level used by lmkd
+ chown root system /dev/memcg/memory.pressure_level
+ chmod 0040 /dev/memcg/memory.pressure_level
# app mem cgroups, used by activity manager, lmkd and zygote
mkdir /dev/memcg/apps/ 0755 system system
# cgroup for system_server and surfaceflinger
@@ -80,13 +81,10 @@
chmod 0664 /dev/stune/top-app/tasks
chmod 0664 /dev/stune/rt/tasks
- # Mount staging areas for devices managed by vold
- # See storage config details at http://source.android.com/tech/storage/
- mount tmpfs tmpfs /mnt nodev noexec nosuid mode=0755,uid=0,gid=1000
restorecon_recursive /mnt
mount configfs none /config nodev noexec nosuid
- chmod 0775 /config/sdcardfs
+ chmod 0770 /config/sdcardfs
chown system package_info /config/sdcardfs
mkdir /mnt/secure 0700 root root
@@ -182,6 +180,12 @@
copy /dev/cpuset/cpus /dev/cpuset/system-background/cpus
copy /dev/cpuset/mems /dev/cpuset/system-background/mems
+ # restricted is for system tasks that are being throttled
+ # due to screen off.
+ mkdir /dev/cpuset/restricted
+ copy /dev/cpuset/cpus /dev/cpuset/restricted/cpus
+ copy /dev/cpuset/mems /dev/cpuset/restricted/mems
+
mkdir /dev/cpuset/top-app
copy /dev/cpuset/cpus /dev/cpuset/top-app/cpus
copy /dev/cpuset/mems /dev/cpuset/top-app/mems
@@ -192,11 +196,13 @@
chown system system /dev/cpuset/background
chown system system /dev/cpuset/system-background
chown system system /dev/cpuset/top-app
+ chown system system /dev/cpuset/restricted
chown system system /dev/cpuset/tasks
chown system system /dev/cpuset/foreground/tasks
chown system system /dev/cpuset/background/tasks
chown system system /dev/cpuset/system-background/tasks
chown system system /dev/cpuset/top-app/tasks
+ chown system system /dev/cpuset/restricted/tasks
# set system-background to 0775 so SurfaceFlinger can touch it
chmod 0775 /dev/cpuset/system-background
@@ -205,6 +211,7 @@
chmod 0664 /dev/cpuset/background/tasks
chmod 0664 /dev/cpuset/system-background/tasks
chmod 0664 /dev/cpuset/top-app/tasks
+ chmod 0664 /dev/cpuset/restricted/tasks
chmod 0664 /dev/cpuset/tasks
@@ -230,6 +237,8 @@
# pstore/ramoops previous console log
mount pstore pstore /sys/fs/pstore nodev noexec nosuid
+ chown system log /sys/fs/pstore
+ chmod 0550 /sys/fs/pstore
chown system log /sys/fs/pstore/console-ramoops
chmod 0440 /sys/fs/pstore/console-ramoops
chown system log /sys/fs/pstore/console-ramoops-0
@@ -319,8 +328,8 @@
start vndservicemanager
# Once everything is setup, no need to modify /.
- # The bind+ro combination avoids modifying any other mount flags.
- mount rootfs rootfs / remount bind ro
+ # The bind+remount combination allows this to work in containers.
+ mount rootfs rootfs / remount bind ro nodev
# Mount shared so changes propagate into child namespaces
mount rootfs rootfs / shared rec
# Mount default storage into root namespace
@@ -366,6 +375,10 @@
# create the lost+found directories, so as to enforce our permissions
mkdir /cache/lost+found 0770 root root
+ restorecon_recursive /metadata
+ mkdir /metadata/vold
+ chmod 0700 /metadata/vold
+
on late-fs
# Ensure that tracefs has the correct permissions.
# This does not work correctly if it is called in post-fs.
@@ -422,7 +435,9 @@
mkdir /data/misc/radio 0770 system radio
mkdir /data/misc/sms 0770 system radio
mkdir /data/misc/carrierid 0770 system radio
+ mkdir /data/misc/apns 0770 system radio
mkdir /data/misc/zoneinfo 0775 system system
+ mkdir /data/misc/network_watchlist 0774 system system
mkdir /data/misc/textclassifier 0771 system system
mkdir /data/misc/vpn 0770 system vpn
mkdir /data/misc/shared_relro 0771 shared_relro shared_relro
@@ -455,6 +470,8 @@
mkdir /data/misc/gcov 0770 root root
mkdir /data/vendor 0771 root root
+ mkdir /data/vendor_ce 0771 root root
+ mkdir /data/vendor_de 0771 root root
mkdir /data/vendor/hardware 0771 root root
# For security reasons, /data/local/tmp should always be empty.
@@ -509,6 +526,7 @@
mkdir /data/ss 0700 system system
mkdir /data/system 0775 system system
+ mkdir /data/system/dropbox 0700 system system
mkdir /data/system/heapdump 0700 system system
mkdir /data/system/users 0775 system system
@@ -573,6 +591,9 @@
hostname localhost
domainname localdomain
+ # IPsec SA default expiration length
+ write /proc/sys/net/core/xfrm_acq_expires 3600
+
# Memory management. Basic kernel parameters, and allow the high
# level system server to be able to adjust the kernel OOM driver
# parameters to match how it is managing things.
@@ -683,6 +704,7 @@
on property:vold.decrypt=trigger_post_fs_data
trigger post-fs-data
+ trigger zygote-start
on property:vold.decrypt=trigger_restart_min_framework
# A/B update verifier that marks a successful boot.
@@ -690,6 +712,8 @@
class_start main
on property:vold.decrypt=trigger_restart_framework
+ stop surfaceflinger
+ start surfaceflinger
# A/B update verifier that marks a successful boot.
exec_start update_verifier
class_start main
@@ -714,6 +738,9 @@
on property:security.perf_harden=0
write /proc/sys/kernel/perf_event_paranoid 1
+ write /proc/sys/kernel/perf_event_max_sample_rate ${debug.perf_event_max_sample_rate:-100000}
+ write /proc/sys/kernel/perf_cpu_time_max_percent ${debug.perf_cpu_time_max_percent:-25}
+ write /proc/sys/kernel/perf_event_mlock_kb ${debug.perf_event_mlock_kb:-516}
on property:security.perf_harden=1
write /proc/sys/kernel/perf_event_paranoid 3
@@ -724,17 +751,12 @@
## Daemon processes to be run by init.
##
-service ueventd /sbin/ueventd
+service ueventd /system/bin/ueventd
class core
critical
seclabel u:r:ueventd:s0
shutdown critical
-service healthd /system/bin/healthd
- class core
- critical
- group root system wakelock
-
service console /system/bin/sh
class core
console
diff --git a/rootdir/init.usb.configfs.rc b/rootdir/init.usb.configfs.rc
index de1aab3..3a33c94 100644
--- a/rootdir/init.usb.configfs.rc
+++ b/rootdir/init.usb.configfs.rc
@@ -2,7 +2,6 @@
write /config/usb_gadget/g1/UDC "none"
stop adbd
setprop sys.usb.ffs.ready 0
- setprop sys.usb.ffs.mtp.ready 0
write /config/usb_gadget/g1/bDeviceClass 0
write /config/usb_gadget/g1/bDeviceSubClass 0
write /config/usb_gadget/g1/bDeviceProtocol 0
@@ -24,7 +23,7 @@
write /config/usb_gadget/g1/UDC ${sys.usb.controller}
setprop sys.usb.state ${sys.usb.config}
-on property:sys.usb.ffs.mtp.ready=1 && property:sys.usb.config=mtp && property:sys.usb.configfs=1
+on property:sys.usb.config=mtp && property:sys.usb.configfs=1
write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "mtp"
symlink /config/usb_gadget/g1/functions/mtp.gs0 /config/usb_gadget/g1/configs/b.1/f1
write /config/usb_gadget/g1/UDC ${sys.usb.controller}
@@ -33,15 +32,14 @@
on property:sys.usb.config=mtp,adb && property:sys.usb.configfs=1
start adbd
-on property:sys.usb.ffs.ready=1 && property:sys.usb.ffs.mtp.ready=1 && \
-property:sys.usb.config=mtp,adb && property:sys.usb.configfs=1
+on property:sys.usb.ffs.ready=1 && property:sys.usb.config=mtp,adb && property:sys.usb.configfs=1
write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "mtp_adb"
symlink /config/usb_gadget/g1/functions/mtp.gs0 /config/usb_gadget/g1/configs/b.1/f1
symlink /config/usb_gadget/g1/functions/ffs.adb /config/usb_gadget/g1/configs/b.1/f2
write /config/usb_gadget/g1/UDC ${sys.usb.controller}
setprop sys.usb.state ${sys.usb.config}
-on property:sys.usb.ffs.mtp.ready=1 && property:sys.usb.config=ptp && property:sys.usb.configfs=1
+on property:sys.usb.config=ptp && property:sys.usb.configfs=1
write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "ptp"
symlink /config/usb_gadget/g1/functions/ptp.gs1 /config/usb_gadget/g1/configs/b.1/f1
write /config/usb_gadget/g1/UDC ${sys.usb.controller}
@@ -50,8 +48,7 @@
on property:sys.usb.config=ptp,adb && property:sys.usb.configfs=1
start adbd
-on property:sys.usb.ffs.ready=1 && property:sys.usb.ffs.mtp.ready=1 && \
-property:sys.usb.config=ptp,adb && property:sys.usb.configfs=1
+on property:sys.usb.ffs.ready=1 && property:sys.usb.config=ptp,adb && property:sys.usb.configfs=1
write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "ptp_adb"
symlink /config/usb_gadget/g1/functions/ptp.gs1 /config/usb_gadget/g1/configs/b.1/f1
symlink /config/usb_gadget/g1/functions/ffs.adb /config/usb_gadget/g1/configs/b.1/f2
diff --git a/rootdir/ueventd.rc b/rootdir/ueventd.rc
index b03d83b..2f85dec 100644
--- a/rootdir/ueventd.rc
+++ b/rootdir/ueventd.rc
@@ -1,3 +1,5 @@
+firmware_directories /etc/firmware/ /odm/firmware/ /vendor/firmware/ /firmware/image/
+
subsystem adf
devname uevent_devname
diff --git a/rootdir/update_and_install_ld_config.mk b/rootdir/update_and_install_ld_config.mk
new file mode 100644
index 0000000..4d6df77
--- /dev/null
+++ b/rootdir/update_and_install_ld_config.mk
@@ -0,0 +1,125 @@
+#####################################################################
+# Builds linker config file, ld.config.txt, from the specified template
+# under $(LOCAL_PATH)/etc/*.
+#
+# Inputs:
+# (expected to follow an include of $(BUILD_SYSTEM)/base_rules.mk)
+# ld_config_template: template linker config file to use,
+# e.g. $(LOCAL_PATH)/etc/ld.config.txt
+# vndk_version: version of the VNDK library lists used to update the
+# template linker config file, e.g. 28
+# lib_list_from_prebuilts: should be set to 'true' if the VNDK library
+# lists should be read from /prebuilts/vndk/*
+# libz_is_llndk: should be set to 'true' if libz must be included in
+# llndk and not in vndk-sp
+# Outputs:
+# Builds and installs ld.config.$VER.txt or ld.config.vndk_lite.txt
+#####################################################################
+
+# Read inputs
+ld_config_template := $(strip $(ld_config_template))
+vndk_version := $(strip $(vndk_version))
+lib_list_from_prebuilts := $(strip $(lib_list_from_prebuilts))
+libz_is_llndk := $(strip $(libz_is_llndk))
+
+intermediates_dir := $(call intermediates-dir-for,ETC,$(LOCAL_MODULE))
+library_lists_dir := $(intermediates_dir)
+ifeq ($(lib_list_from_prebuilts),true)
+ library_lists_dir := prebuilts/vndk/v$(vndk_version)/$(TARGET_ARCH)/configs
+endif
+
+llndk_libraries_file := $(library_lists_dir)/llndk.libraries.$(vndk_version).txt
+vndksp_libraries_file := $(library_lists_dir)/vndksp.libraries.$(vndk_version).txt
+vndkcore_libraries_file := $(library_lists_dir)/vndkcore.libraries.txt
+vndkprivate_libraries_file := $(library_lists_dir)/vndkprivate.libraries.txt
+
+sanitizer_runtime_libraries := $(call normalize-path-list,$(addsuffix .so,\
+ $(ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
+ $(UBSAN_RUNTIME_LIBRARY) \
+ $(TSAN_RUNTIME_LIBRARY) \
+ $(2ND_ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
+ $(2ND_UBSAN_RUNTIME_LIBRARY) \
+ $(2ND_TSAN_RUNTIME_LIBRARY)))
+# If BOARD_VNDK_VERSION is not defined, VNDK version suffix will not be used.
+vndk_version_suffix := $(if $(vndk_version),-$(vndk_version))
+
+ifneq ($(lib_list_from_prebuilts),true)
+ifeq ($(libz_is_llndk),true)
+ llndk_libraries_list := $(LLNDK_LIBRARIES) libz
+ vndksp_libraries_list := $(filter-out libz,$(VNDK_SAMEPROCESS_LIBRARIES))
+else
+ llndk_libraries_list := $(LLNDK_LIBRARIES)
+ vndksp_libraries_list := $(VNDK_SAMEPROCESS_LIBRARIES)
+endif
+
+# $(1): list of libraries
+# $(2): output file to write the list of libraries to
+define write-libs-to-file
+$(2): PRIVATE_LIBRARIES := $(1)
+$(2):
+ echo -n > $$@ && $$(foreach lib,$$(PRIVATE_LIBRARIES),echo $$(lib).so >> $$@;)
+endef
+$(eval $(call write-libs-to-file,$(llndk_libraries_list),$(llndk_libraries_file)))
+$(eval $(call write-libs-to-file,$(vndksp_libraries_list),$(vndksp_libraries_file)))
+$(eval $(call write-libs-to-file,$(VNDK_CORE_LIBRARIES),$(vndkcore_libraries_file)))
+$(eval $(call write-libs-to-file,$(VNDK_PRIVATE_LIBRARIES),$(vndkprivate_libraries_file)))
+endif # ifneq ($(lib_list_from_prebuilts),true)
+
+# Given a file with a list of libs, filter-out the VNDK private libraries
+# and write resulting list to a new file in "a:b:c" format
+#
+# $(1): libs file from which to filter-out VNDK private libraries
+# $(2): output file with the filtered list of lib names
+$(LOCAL_BUILT_MODULE): private-filter-out-private-libs = \
+ paste -sd ":" $(1) > $(2) && \
+ cat $(PRIVATE_VNDK_PRIVATE_LIBRARIES_FILE) | xargs -n 1 -I privatelib bash -c "sed -i.bak 's/privatelib//' $(2)" && \
+ sed -i.bak -e 's/::\+/:/g ; s/^:\+// ; s/:\+$$//' $(2) && \
+ rm -f $(2).bak
+$(LOCAL_BUILT_MODULE): PRIVATE_LLNDK_LIBRARIES_FILE := $(llndk_libraries_file)
+$(LOCAL_BUILT_MODULE): PRIVATE_VNDK_SP_LIBRARIES_FILE := $(vndksp_libraries_file)
+$(LOCAL_BUILT_MODULE): PRIVATE_VNDK_CORE_LIBRARIES_FILE := $(vndkcore_libraries_file)
+$(LOCAL_BUILT_MODULE): PRIVATE_VNDK_PRIVATE_LIBRARIES_FILE := $(vndkprivate_libraries_file)
+$(LOCAL_BUILT_MODULE): PRIVATE_SANITIZER_RUNTIME_LIBRARIES := $(sanitizer_runtime_libraries)
+$(LOCAL_BUILT_MODULE): PRIVATE_VNDK_VERSION_SUFFIX := $(vndk_version_suffix)
+$(LOCAL_BUILT_MODULE): PRIVATE_INTERMEDIATES_DIR := $(intermediates_dir)
+deps := $(llndk_libraries_file) $(vndksp_libraries_file) $(vndkcore_libraries_file) \
+ $(vndkprivate_libraries_file)
+
+$(LOCAL_BUILT_MODULE): $(ld_config_template) $(deps)
+ @echo "Generate: $< -> $@"
+ @mkdir -p $(dir $@)
+ $(call private-filter-out-private-libs,$(PRIVATE_LLNDK_LIBRARIES_FILE),$(PRIVATE_INTERMEDIATES_DIR)/llndk_filtered)
+ $(hide) sed -e "s?%LLNDK_LIBRARIES%?$$(cat $(PRIVATE_INTERMEDIATES_DIR)/llndk_filtered)?g" $< >$@
+ $(call private-filter-out-private-libs,$(PRIVATE_VNDK_SP_LIBRARIES_FILE),$(PRIVATE_INTERMEDIATES_DIR)/vndksp_filtered)
+ $(hide) sed -i.bak -e "s?%VNDK_SAMEPROCESS_LIBRARIES%?$$(cat $(PRIVATE_INTERMEDIATES_DIR)/vndksp_filtered)?g" $@
+ $(call private-filter-out-private-libs,$(PRIVATE_VNDK_CORE_LIBRARIES_FILE),$(PRIVATE_INTERMEDIATES_DIR)/vndkcore_filtered)
+ $(hide) sed -i.bak -e "s?%VNDK_CORE_LIBRARIES%?$$(cat $(PRIVATE_INTERMEDIATES_DIR)/vndkcore_filtered)?g" $@
+
+ $(hide) echo -n > $(PRIVATE_INTERMEDIATES_DIR)/private_llndk && \
+ cat $(PRIVATE_VNDK_PRIVATE_LIBRARIES_FILE) | \
+ xargs -n 1 -I privatelib bash -c "(grep privatelib $(PRIVATE_LLNDK_LIBRARIES_FILE) || true) >> $(PRIVATE_INTERMEDIATES_DIR)/private_llndk" && \
+ paste -sd ":" $(PRIVATE_INTERMEDIATES_DIR)/private_llndk | \
+ sed -i.bak -e "s?%PRIVATE_LLNDK_LIBRARIES%?$$(cat -)?g" $@
+
+ $(hide) sed -i.bak -e 's?%SANITIZER_RUNTIME_LIBRARIES%?$(PRIVATE_SANITIZER_RUNTIME_LIBRARIES)?g' $@
+ $(hide) sed -i.bak -e 's?%VNDK_VER%?$(PRIVATE_VNDK_VERSION_SUFFIX)?g' $@
+ $(hide) sed -i.bak -e 's?%PRODUCT%?$(TARGET_COPY_OUT_PRODUCT)?g' $@
+ $(hide) sed -i.bak -e 's?%PRODUCT_SERVICES%?$(TARGET_COPY_OUT_PRODUCT_SERVICES)?g' $@
+ $(hide) rm -f $@.bak
+
+ld_config_template :=
+vndk_version :=
+lib_list_from_prebuilts :=
+libz_is_llndk :=
+intermediates_dir :=
+library_lists_dir :=
+llndk_libraries_file :=
+vndksp_libraries_file :=
+vndkcore_libraries_file :=
+vndkprivate_libraries_file :=
+deps :=
+sanitizer_runtime_libraries :=
+vndk_version_suffix :=
+llndk_libraries_list :=
+vndksp_libraries_list :=
+write-libs-to-file :=
diff --git a/run-as/Android.bp b/run-as/Android.bp
new file mode 100644
index 0000000..840a43c
--- /dev/null
+++ b/run-as/Android.bp
@@ -0,0 +1,28 @@
+//
+// Copyright (C) 2018 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.
+//
+
+cc_binary {
+ name: "run-as",
+ srcs: [
+ "run-as.cpp",
+ ],
+ shared_libs: [
+ "libbase",
+ "libselinux",
+ "libpackagelistparser",
+ "libminijail",
+ ],
+}
diff --git a/run-as/Android.mk b/run-as/Android.mk
deleted file mode 100644
index 7111fbe..0000000
--- a/run-as/Android.mk
+++ /dev/null
@@ -1,8 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-LOCAL_CFLAGS := -Wall -Werror
-LOCAL_MODULE := run-as
-LOCAL_SHARED_LIBRARIES := libselinux libpackagelistparser libminijail
-LOCAL_SRC_FILES := run-as.cpp
-include $(BUILD_EXECUTABLE)
diff --git a/run-as/run-as.cpp b/run-as/run-as.cpp
index b27cfad..d005ecf 100644
--- a/run-as/run-as.cpp
+++ b/run-as/run-as.cpp
@@ -28,6 +28,7 @@
#include <libminijail.h>
#include <scoped_minijail.h>
+#include <android-base/properties.h>
#include <packagelistparser/packagelistparser.h>
#include <private/android_filesystem_config.h>
#include <selinux/android.h>
@@ -40,6 +41,7 @@
// The 'run-as' binary is installed with CAP_SETUID and CAP_SETGID file
// capabilities, but will check the following:
//
+// - that the ro.boot.disable_runas property is not set
// - that it is invoked from the 'shell' or 'root' user (abort otherwise)
// - that '<package-name>' is the name of an installed and debuggable package
// - that the package's data directory is well-formed
@@ -139,6 +141,12 @@
error(1, 0, "only 'shell' or 'root' users can run this program");
}
+ // Some devices can disable running run-as, such as Chrome OS when running in
+ // non-developer mode.
+ if (android::base::GetBoolProperty("ro.boot.disable_runas", false)) {
+ error(1, 0, "run-as is disabled from the kernel commandline");
+ }
+
char* pkgname = argv[1];
int cmd_argv_offset = 2;
diff --git a/sdcard/sdcard.cpp b/sdcard/sdcard.cpp
index 574bbfe..dc36596 100644
--- a/sdcard/sdcard.cpp
+++ b/sdcard/sdcard.cpp
@@ -43,6 +43,44 @@
#include <private/android_filesystem_config.h>
+#define PROP_SDCARDFS_DEVICE "ro.sys.sdcardfs"
+#define PROP_SDCARDFS_USER "persist.sys.sdcardfs"
+
+static bool supports_esdfs(void) {
+ std::string filesystems;
+ if (!android::base::ReadFileToString("/proc/filesystems", &filesystems)) {
+ PLOG(ERROR) << "Could not read /proc/filesystems";
+ return false;
+ }
+ for (const auto& fs : android::base::Split(filesystems, "\n")) {
+ if (fs.find("esdfs") != std::string::npos) return true;
+ }
+ return false;
+}
+
+static bool should_use_sdcardfs(void) {
+ char property[PROPERTY_VALUE_MAX];
+
+ // Allow user to have a strong opinion about state
+ property_get(PROP_SDCARDFS_USER, property, "");
+ if (!strcmp(property, "force_on")) {
+ LOG(WARNING) << "User explicitly enabled sdcardfs";
+ return true;
+ } else if (!strcmp(property, "force_off")) {
+ LOG(WARNING) << "User explicitly disabled sdcardfs";
+ return !supports_esdfs();
+ }
+
+ // Fall back to device opinion about state
+ if (property_get_bool(PROP_SDCARDFS_DEVICE, true)) {
+ LOG(WARNING) << "Device explicitly enabled sdcardfs";
+ return true;
+ } else {
+ LOG(WARNING) << "Device explicitly disabled sdcardfs";
+ return !supports_esdfs();
+ }
+}
+
// NOTE: This is a vestigial program that simply exists to mount the in-kernel
// sdcardfs filesystem. The older FUSE-based design that used to live here has
// been completely removed to avoid confusion.
@@ -61,7 +99,7 @@
static bool sdcardfs_setup(const std::string& source_path, const std::string& dest_path,
uid_t fsuid, gid_t fsgid, bool multi_user, userid_t userid, gid_t gid,
- mode_t mask, bool derive_gid, bool default_normal) {
+ mode_t mask, bool derive_gid, bool default_normal, bool use_esdfs) {
// Try several attempts, each time with one less option, to gracefully
// handle older kernels that aren't updated yet.
for (int i = 0; i < 4; i++) {
@@ -72,7 +110,7 @@
auto opts = android::base::StringPrintf("fsuid=%d,fsgid=%d,%smask=%d,userid=%d,gid=%d",
fsuid, fsgid, new_opts.c_str(), mask, userid, gid);
- if (mount(source_path.c_str(), dest_path.c_str(), "sdcardfs",
+ if (mount(source_path.c_str(), dest_path.c_str(), use_esdfs ? "esdfs" : "sdcardfs",
MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts.c_str()) == -1) {
PLOG(WARNING) << "Failed to mount sdcardfs with options " << opts;
} else {
@@ -104,9 +142,21 @@
return true;
}
+static bool sdcardfs_setup_secondary(const std::string& default_path, const std::string& source_path,
+ const std::string& dest_path, uid_t fsuid, gid_t fsgid,
+ bool multi_user, userid_t userid, gid_t gid, mode_t mask,
+ bool derive_gid, bool default_normal, bool use_esdfs) {
+ if (use_esdfs) {
+ return sdcardfs_setup(source_path, dest_path, fsuid, fsgid, multi_user, userid, gid, mask,
+ derive_gid, default_normal, use_esdfs);
+ } else {
+ return sdcardfs_setup_bind_remount(default_path, dest_path, gid, mask);
+ }
+}
+
static void run_sdcardfs(const std::string& source_path, const std::string& label, uid_t uid,
gid_t gid, userid_t userid, bool multi_user, bool full_write,
- bool derive_gid, bool default_normal) {
+ bool derive_gid, bool default_normal, bool use_esdfs) {
std::string dest_path_default = "/mnt/runtime/default/" + label;
std::string dest_path_read = "/mnt/runtime/read/" + label;
std::string dest_path_write = "/mnt/runtime/write/" + label;
@@ -116,10 +166,13 @@
// Multi-user storage is fully isolated per user, so "other"
// permissions are completely masked off.
if (!sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
- AID_SDCARD_RW, 0006, derive_gid, default_normal) ||
- !sdcardfs_setup_bind_remount(dest_path_default, dest_path_read, AID_EVERYBODY, 0027) ||
- !sdcardfs_setup_bind_remount(dest_path_default, dest_path_write, AID_EVERYBODY,
- full_write ? 0007 : 0027)) {
+ AID_SDCARD_RW, 0006, derive_gid, default_normal, use_esdfs) ||
+ !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_read, uid, gid,
+ multi_user, userid, AID_EVERYBODY, 0027, derive_gid,
+ default_normal, use_esdfs) ||
+ !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_write, uid, gid,
+ multi_user, userid, AID_EVERYBODY, full_write ? 0007 : 0027,
+ derive_gid, default_normal, use_esdfs)) {
LOG(FATAL) << "failed to sdcardfs_setup";
}
} else {
@@ -127,11 +180,13 @@
// the Android directories are masked off to a single user
// deep inside attr_from_stat().
if (!sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
- AID_SDCARD_RW, 0006, derive_gid, default_normal) ||
- !sdcardfs_setup_bind_remount(dest_path_default, dest_path_read, AID_EVERYBODY,
- full_write ? 0027 : 0022) ||
- !sdcardfs_setup_bind_remount(dest_path_default, dest_path_write, AID_EVERYBODY,
- full_write ? 0007 : 0022)) {
+ AID_SDCARD_RW, 0006, derive_gid, default_normal, use_esdfs) ||
+ !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_read, uid, gid,
+ multi_user, userid, AID_EVERYBODY, full_write ? 0027 : 0022,
+ derive_gid, default_normal, use_esdfs) ||
+ !sdcardfs_setup_secondary(dest_path_default, source_path, dest_path_write, uid, gid,
+ multi_user, userid, AID_EVERYBODY, full_write ? 0007 : 0022,
+ derive_gid, default_normal, use_esdfs)) {
LOG(FATAL) << "failed to sdcardfs_setup";
}
}
@@ -242,6 +297,6 @@
}
run_sdcardfs(source_path, label, uid, gid, userid, multi_user, full_write, derive_gid,
- default_normal);
+ default_normal, !should_use_sdcardfs());
return 1;
}
diff --git a/shell_and_utilities/Android.bp b/shell_and_utilities/Android.bp
index 3ccb92f..3d7521c 100644
--- a/shell_and_utilities/Android.bp
+++ b/shell_and_utilities/Android.bp
@@ -1,19 +1,48 @@
phony {
name: "shell_and_utilities",
required: [
+ "shell_and_utilities_system",
+ "shell_and_utilities_recovery",
+ "shell_and_utilities_vendor",
+ ],
+}
+
+phony {
+ name: "shell_and_utilities_system",
+ required: [
"awk",
- "awk_vendor",
"bzip2",
"grep",
- "grep_vendor",
+ "logwrapper",
"mkshrc",
- "mkshrc_vendor",
+ "newfs_msdos",
"reboot",
"sh",
- "sh_vendor",
+ "tcpdump",
"toolbox",
- "toolbox_vendor",
"toybox",
+ ],
+}
+
+phony {
+ name: "shell_and_utilities_recovery",
+ required: [
+ "grep.recovery",
+ "sh.recovery",
+ "toolbox.recovery",
+ "toybox.recovery",
+ ],
+}
+
+phony {
+ name: "shell_and_utilities_vendor",
+ required: [
+ "awk_vendor",
+ "grep_vendor",
+ "logwrapper_vendor",
+ "mkshrc_vendor",
+ "sh_vendor",
+ "toolbox_vendor",
"toybox_vendor",
],
}
diff --git a/shell_and_utilities/README.md b/shell_and_utilities/README.md
index c423c69..d8cf867 100644
--- a/shell_and_utilities/README.md
+++ b/shell_and_utilities/README.md
@@ -18,11 +18,9 @@
(a) didn't stand out given all the other systems-level changes and (b)
in Marshmallow we changed direction and started the move to toybox.
-Not everything is provided by toybox, though. We currently still use
-the BSD dd and grep (because the toybox versions are still unfinished),
-and for the bzip2 command-line tools we use the ones that are part of
-the bzip2 distribution. The awk added in Android P is Brian Kernighan's
-"one true" awk.
+Not everything is provided by toybox, though. For the bzip2 command-line tools
+we use the ones that are part of the bzip2 distribution. The awk added in
+Android P is Brian Kernighan's "one true" awk.
The lists below show what tools were provided and where they came from in
each release starting with Gingerbread. This doesn't tell the full story,
@@ -179,15 +177,41 @@
toybox: acpi base64 basename blockdev cal cat chcon chgrp chmod chown
chroot chrt cksum clear cmp comm cp cpio cut date df diff dirname dmesg
-dos2unix du echo env expand expr fallocate false file find flock free
+dos2unix du echo env expand expr fallocate false file find flock fmt free
getenforce groups gunzip gzip head hostname hwclock id ifconfig inotifyd
-insmod ionice iorenice kill killall ln load\_policy log logname losetup
-ls lsmod lsof lspci lsusb md5sum microcom mkdir mkfifo mknod mkswap
-mktemp modinfo modprobe more mount mountpoint mv netstat nice nl nohup
-od paste patch pgrep pidof pkill pmap printenv printf ps pwd readlink
-realpath renice restorecon rm rmdir rmmod runcon sed sendevent seq
-setenforce setprop setsid sha1sum sha224sum sha256sum sha384sum
-sha512sum sleep sort split start stat stop strings swapoff swapon sync
-sysctl tac tail tar taskset tee time timeout top touch tr true truncate
-tty ulimit umount uname uniq unix2dos uptime usleep uudecode uuencode
-vmstat wc which whoami xargs xxd yes zcat
+insmod ionice iorenice kill killall ln load\_policy log logname losetup ls
+lsmod lsof lspci lsusb md5sum microcom mkdir mkfifo mknod mkswap mktemp
+modinfo modprobe more mount mountpoint mv netstat nice nl nohup od paste
+patch pgrep pidof pkill pmap printenv printf ps pwd readlink realpath
+renice restorecon rm rmdir rmmod runcon sed sendevent seq setenforce
+setprop setsid sha1sum sha224sum sha256sum sha384sum sha512sum sleep
+sort split start stat stop strings stty swapoff swapon sync sysctl tac
+tail tar taskset tee time timeout top touch tr true truncate tty ulimit
+umount uname uniq unix2dos uptime usleep uudecode uuencode vmstat wc
+which whoami xargs xxd yes zcat
+
+Android Q
+---------
+
+BSD: grep fsck\_msdos newfs\_msdos
+
+bzip2: bzcat bzip2 bunzip2
+
+one-true-awk: awk
+
+toolbox: getevent getprop
+
+toybox: acpi base64 basename blockdev cal cat chcon chgrp chmod chown
+chroot chrt cksum clear cmp comm cp cpio cut date dd df diff dirname
+dmesg dos2unix du echo env expand expr fallocate false file find flock
+fmt free getenforce groups gunzip gzip head hostname hwclock id ifconfig
+inotifyd insmod ionice iorenice kill killall ln load\_policy log logname
+losetup ls lsmod lsof lspci lsusb md5sum microcom mkdir mkfifo mknod
+mkswap mktemp modinfo modprobe more mount mountpoint mv nc netcat netstat
+nice nl nohup nsenter od paste patch pgrep pidof pkill pmap printenv
+printf ps pwd readlink realpath renice restorecon rm rmdir rmmod runcon
+sed sendevent seq setenforce setprop setsid sha1sum sha224sum sha256sum
+sha384sum sha512sum sleep sort split start stat stop strings stty swapoff
+swapon sync sysctl tac tail tar taskset tee time timeout top touch tr
+true truncate tty ulimit umount uname uniq unix2dos unshare uptime usleep
+uudecode uuencode vmstat wc which whoami xargs xxd yes zcat
diff --git a/storaged/Android.bp b/storaged/Android.bp
new file mode 100644
index 0000000..7466728
--- /dev/null
+++ b/storaged/Android.bp
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+cc_defaults {
+ name: "storaged_defaults",
+
+ shared_libs: [
+ "android.hardware.health@1.0",
+ "android.hardware.health@2.0",
+ "libbase",
+ "libbinder",
+ "libcutils",
+ "libhidlbase",
+ "libhidltransport",
+ "libhwbinder",
+ "liblog",
+ "libprotobuf-cpp-lite",
+ "libsysutils",
+ "libutils",
+ "libz",
+ ],
+
+ cflags: [
+ "-Wall",
+ "-Werror",
+ "-Wextra",
+ "-Wno-unused-parameter"
+ ],
+}
+
+cc_library_static {
+ name: "libstoraged",
+
+ defaults: ["storaged_defaults"],
+
+ aidl: {
+ export_aidl_headers: true,
+ local_include_dirs: ["binder"],
+ include_dirs: ["frameworks/native/aidl/binder"],
+ },
+
+ srcs: [
+ "storaged.cpp",
+ "storaged_diskstats.cpp",
+ "storaged_info.cpp",
+ "storaged_service.cpp",
+ "storaged_utils.cpp",
+ "storaged_uid_monitor.cpp",
+ "uid_info.cpp",
+ "storaged.proto",
+ ":storaged_aidl",
+ "binder/android/os/storaged/IStoragedPrivate.aidl",
+ ],
+
+ static_libs: ["libhealthhalutils"],
+ header_libs: ["libbatteryservice_headers"],
+
+ logtags: ["EventLogTags.logtags"],
+
+ proto: {
+ type: "lite",
+ export_proto_headers: true,
+ },
+
+ export_include_dirs: ["include"],
+}
+
+cc_binary {
+ name: "storaged",
+
+ defaults: ["storaged_defaults"],
+
+ init_rc: ["storaged.rc"],
+
+ srcs: ["main.cpp"],
+
+ static_libs: [
+ "libhealthhalutils",
+ "libstoraged",
+ ],
+}
+
+/*
+ * Run with:
+ * adb shell /data/nativetest/storaged-unit-tests/storaged-unit-tests
+ */
+cc_test {
+ name: "storaged-unit-tests",
+
+ defaults: ["storaged_defaults"],
+
+ srcs: ["tests/storaged_test.cpp"],
+
+ static_libs: [
+ "libhealthhalutils",
+ "libstoraged",
+ ],
+}
+
+// AIDL interface between storaged and framework.jar
+filegroup {
+ name: "storaged_aidl",
+ srcs: [
+ "binder/android/os/IStoraged.aidl",
+ ],
+}
diff --git a/storaged/Android.mk b/storaged/Android.mk
deleted file mode 100644
index a1abe0f..0000000
--- a/storaged/Android.mk
+++ /dev/null
@@ -1,44 +0,0 @@
-# Copyright 2016 The Android Open Source Project
-
-LOCAL_PATH := $(call my-dir)
-
-LIBSTORAGED_SHARED_LIBRARIES := \
- libbinder \
- libbase \
- libutils \
- libcutils \
- liblog \
- libsysutils \
- libbatteryservice \
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := \
- storaged.cpp \
- storaged_info.cpp \
- storaged_service.cpp \
- storaged_utils.cpp \
- storaged_uid_monitor.cpp \
- EventLogTags.logtags
-
-LOCAL_MODULE := libstoraged
-LOCAL_CFLAGS := -Werror
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/include external/googletest/googletest/include
-LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
-LOCAL_SHARED_LIBRARIES := $(LIBSTORAGED_SHARED_LIBRARIES)
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := storaged
-LOCAL_INIT_RC := storaged.rc
-LOCAL_SRC_FILES := main.cpp
-# libstoraged is an internal static library, only main.cpp and storaged_test.cpp should be using it
-LOCAL_STATIC_LIBRARIES := libstoraged
-LOCAL_SHARED_LIBRARIES := $(LIBSTORAGED_SHARED_LIBRARIES)
-LOCAL_CFLAGS := -Wall -Werror -Wno-unused-parameter
-LOCAL_C_INCLUDES := external/googletest/googletest/include
-
-include $(BUILD_EXECUTABLE)
-
-include $(call first-makefiles-under,$(LOCAL_PATH))
diff --git a/storaged/OWNERS b/storaged/OWNERS
index 7445270..d033f00 100644
--- a/storaged/OWNERS
+++ b/storaged/OWNERS
@@ -1 +1,2 @@
-jinqian@google.com
+salyzyn@google.com
+dvander@google.com
diff --git a/init/init_first_stage.h b/storaged/binder/android/os/IStoraged.aidl
similarity index 74%
copy from init/init_first_stage.h
copy to storaged/binder/android/os/IStoraged.aidl
index c7a3867..0bcc70c 100644
--- a/init/init_first_stage.h
+++ b/storaged/binder/android/os/IStoraged.aidl
@@ -14,16 +14,11 @@
* limitations under the License.
*/
-#ifndef _INIT_FIRST_STAGE_H
-#define _INIT_FIRST_STAGE_H
+package android.os;
-namespace android {
-namespace init {
-
-bool DoFirstStageMount();
-void SetInitAvbVersionInRecovery();
-
-} // namespace init
-} // namespace android
-
-#endif
+/** {@hide} */
+interface IStoraged {
+ void onUserStarted(int userId);
+ void onUserStopped(int userId);
+ int getRecentPerf();
+}
\ No newline at end of file
diff --git a/init/init_first_stage.h b/storaged/binder/android/os/storaged/IStoragedPrivate.aidl
similarity index 74%
copy from init/init_first_stage.h
copy to storaged/binder/android/os/storaged/IStoragedPrivate.aidl
index c7a3867..9c888e3 100644
--- a/init/init_first_stage.h
+++ b/storaged/binder/android/os/storaged/IStoragedPrivate.aidl
@@ -14,16 +14,12 @@
* limitations under the License.
*/
-#ifndef _INIT_FIRST_STAGE_H
-#define _INIT_FIRST_STAGE_H
+package android.os.storaged;
-namespace android {
-namespace init {
+import android.os.storaged.UidInfo;
-bool DoFirstStageMount();
-void SetInitAvbVersionInRecovery();
-
-} // namespace init
-} // namespace android
-
-#endif
+/** {@hide} */
+interface IStoragedPrivate {
+ UidInfo[] dumpUids();
+ int[] dumpPerfHistory();
+}
\ No newline at end of file
diff --git a/init/init_first_stage.h b/storaged/binder/android/os/storaged/UidInfo.aidl
similarity index 74%
copy from init/init_first_stage.h
copy to storaged/binder/android/os/storaged/UidInfo.aidl
index c7a3867..440f386 100644
--- a/init/init_first_stage.h
+++ b/storaged/binder/android/os/storaged/UidInfo.aidl
@@ -14,16 +14,6 @@
* limitations under the License.
*/
-#ifndef _INIT_FIRST_STAGE_H
-#define _INIT_FIRST_STAGE_H
+package android.os.storaged;
-namespace android {
-namespace init {
-
-bool DoFirstStageMount();
-void SetInitAvbVersionInRecovery();
-
-} // namespace init
-} // namespace android
-
-#endif
+parcelable UidInfo cpp_header "include/uid_info.h";
diff --git a/storaged/include/storaged.h b/storaged/include/storaged.h
index fa68406..6f92048 100644
--- a/storaged/include/storaged.h
+++ b/storaged/include/storaged.h
@@ -26,28 +26,18 @@
#include <unordered_map>
#include <vector>
-#include <batteryservice/IBatteryPropertiesListener.h>
-#include <batteryservice/IBatteryPropertiesRegistrar.h>
+#include <utils/Mutex.h>
-#include "storaged_info.h"
-#include "storaged_uid_monitor.h"
-
-using namespace android;
+#include <android/hardware/health/2.0/IHealth.h>
#define FRIEND_TEST(test_case_name, test_name) \
friend class test_case_name##_##test_name##_Test
-/* For debug */
-#ifdef DEBUG
-#define debuginfo(fmt, ...) \
- do {printf("%s():\t" fmt "\t[%s:%d]\n", __FUNCTION__, ##__VA_ARGS__, __FILE__, __LINE__);} \
- while(0)
-#else
-#define debuginfo(...)
-#endif
-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+#define IS_ALIGNED(x, align) (!((x) & ((align) - 1)))
+#define ROUND_UP(x, align) (((x) + ((align) - 1)) & ~((align) - 1))
+
#define SECTOR_SIZE ( 512 )
#define SEC_TO_MSEC ( 1000 )
#define MSEC_TO_USEC ( 1000 )
@@ -55,184 +45,24 @@
#define SEC_TO_USEC ( 1000000 )
#define HOUR_TO_SEC ( 3600 )
#define DAY_TO_SEC ( 3600 * 24 )
+#define WEEK_TO_DAYS ( 7 )
+#define YEAR_TO_WEEKS ( 52 )
-// number of attributes diskstats has
-#define DISK_STATS_SIZE ( 11 )
-// maximum size limit of a stats file
-#define DISK_STATS_FILE_MAX_SIZE ( 256 )
-#define DISK_STATS_IO_IN_FLIGHT_IDX ( 8 )
-struct disk_stats {
- /* It will be extremely unlikely for any of the following entries to overflow.
- * For read_bytes(which will be greater than any of the following entries), it
- * will take 27 years to overflow uint64_t at the reading rate of 20GB/s, which
- * is the peak memory transfer rate for current memory.
- * The diskstats entries (first 11) need to be at top in this structure _after_
- * compiler's optimization.
- */
- uint64_t read_ios; // number of read I/Os processed
- uint64_t read_merges; // number of read I/Os merged with in-queue I/Os
- uint64_t read_sectors; // number of sectors read
- uint64_t read_ticks; // total wait time for read requests
- uint64_t write_ios; // number of write I/Os processed
- uint64_t write_merges; // number of write I/Os merged with in-queue I/Os
- uint64_t write_sectors; // number of sectors written
- uint64_t write_ticks; // total wait time for write requests
- uint64_t io_in_flight; // number of I/Os currently in flight
- uint64_t io_ticks; // total time this block device has been active
- uint64_t io_in_queue; // total wait time for all requests
+#include "storaged_diskstats.h"
+#include "storaged_info.h"
+#include "storaged_uid_monitor.h"
+#include "storaged.pb.h"
+#include "uid_info.h"
- uint64_t start_time; // monotonic time accounting starts
- uint64_t end_time; // monotonic time accounting ends
- uint32_t counter; // private counter for accumulate calculations
- double io_avg; // average io_in_flight for accumulate calculations
-};
-
-
-
-struct disk_perf {
- uint32_t read_perf; // read speed (kbytes/s)
- uint32_t read_ios; // read I/Os per second
- uint32_t write_perf; // write speed (kbytes/s)
- uint32_t write_ios; // write I/Os per second
- uint32_t queue; // I/Os in queue
-};
-
-#define CMD_MAX_LEN ( 64 )
-struct task_info {
- uint32_t pid; // task id
- uint64_t rchar; // characters read
- uint64_t wchar; // characters written
- uint64_t syscr; // read syscalls
- uint64_t syscw; // write syscalls
- uint64_t read_bytes; // bytes read (from storage layer)
- uint64_t write_bytes; // bytes written (to storage layer)
- uint64_t cancelled_write_bytes; // cancelled write byte by truncate
-
- uint64_t starttime; // start time of task
-
- char cmd[CMD_MAX_LEN]; // filename of the executable
-};
-
-class lock_t {
- sem_t* mSem;
-public:
- lock_t(sem_t* sem) {
- mSem = sem;
- sem_wait(mSem);
- }
- ~lock_t() {
- sem_post(mSem);
- }
-};
-
-class stream_stats {
-private:
- double mSum;
- double mSquareSum;
- uint32_t mCnt;
-public:
- stream_stats() : mSum(0), mSquareSum(0), mCnt(0) {};
- ~stream_stats() {};
- double get_mean() {
- return mSum / mCnt;
- }
- double get_std() {
- return sqrt(mSquareSum / mCnt - mSum * mSum / (mCnt * mCnt));
- }
- void add(uint32_t num) {
- mSum += (double)num;
- mSquareSum += (double)num * (double)num;
- mCnt++;
- }
- void evict(uint32_t num) {
- if (mSum < num || mSquareSum < (double)num * (double)num) return;
- mSum -= (double)num;
- mSquareSum -= (double)num * (double)num;
- mCnt--;
- }
-};
-
-#define MMC_DISK_STATS_PATH "/sys/block/mmcblk0/stat"
-#define SDA_DISK_STATS_PATH "/sys/block/sda/stat"
-#define EMMC_ECSD_PATH "/d/mmc0/mmc0:0001/ext_csd"
-#define UID_IO_STATS_PATH "/proc/uid_io/stats"
-
-class disk_stats_monitor {
-private:
- FRIEND_TEST(storaged_test, disk_stats_monitor);
- const char* DISK_STATS_PATH;
- struct disk_stats mPrevious;
- struct disk_stats mAccumulate;
- bool mStall;
- std::queue<struct disk_perf> mBuffer;
- struct {
- stream_stats read_perf; // read speed (bytes/s)
- stream_stats read_ios; // read I/Os per second
- stream_stats write_perf; // write speed (bytes/s)
- stream_stats write_ios; // write I/O per second
- stream_stats queue; // I/Os in queue
- } mStats;
- bool mValid;
- const uint32_t mWindow;
- const double mSigma;
- struct disk_perf mMean;
- struct disk_perf mStd;
-
- void update_mean();
- void update_std();
- void add(struct disk_perf* perf);
- void evict(struct disk_perf* perf);
- bool detect(struct disk_perf* perf);
-
- void update(struct disk_stats* stats);
-
-public:
- disk_stats_monitor(uint32_t window_size = 5, double sigma = 1.0) :
- mStall(false),
- mValid(false),
- mWindow(window_size),
- mSigma(sigma) {
- memset(&mPrevious, 0, sizeof(mPrevious));
- memset(&mMean, 0, sizeof(mMean));
- memset(&mStd, 0, sizeof(mStd));
-
- if (access(MMC_DISK_STATS_PATH, R_OK) >= 0) {
- DISK_STATS_PATH = MMC_DISK_STATS_PATH;
- } else {
- DISK_STATS_PATH = SDA_DISK_STATS_PATH;
- }
- }
- void update(void);
-};
-
-class disk_stats_publisher {
-private:
- FRIEND_TEST(storaged_test, disk_stats_publisher);
- const char* DISK_STATS_PATH;
- struct disk_stats mAccumulate;
- struct disk_stats mPrevious;
-public:
- disk_stats_publisher(void) {
- memset(&mAccumulate, 0, sizeof(struct disk_stats));
- memset(&mPrevious, 0, sizeof(struct disk_stats));
-
- if (access(MMC_DISK_STATS_PATH, R_OK) >= 0) {
- DISK_STATS_PATH = MMC_DISK_STATS_PATH;
- } else {
- DISK_STATS_PATH = SDA_DISK_STATS_PATH;
- }
- }
-
- ~disk_stats_publisher(void) {}
- void publish(void);
- void update(void);
-};
+using namespace std;
+using namespace android;
// Periodic chores intervals in seconds
#define DEFAULT_PERIODIC_CHORES_INTERVAL_UNIT ( 60 )
#define DEFAULT_PERIODIC_CHORES_INTERVAL_DISK_STATS_PUBLISH ( 3600 )
#define DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO ( 3600 )
-#define DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO_LIMIT (300)
+#define DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO_LIMIT ( 300 )
+#define DEFAULT_PERIODIC_CHORES_INTERVAL_FLUSH_PROTO ( 3600 )
// UID IO threshold in bytes
#define DEFAULT_PERIODIC_CHORES_UID_IO_THRESHOLD ( 1024 * 1024 * 1024ULL )
@@ -241,25 +71,35 @@
int periodic_chores_interval_unit;
int periodic_chores_interval_disk_stats_publish;
int periodic_chores_interval_uid_io;
- bool proc_uid_io_available; // whether uid_io is accessible
- bool diskstats_available; // whether diskstats is accessible
+ int periodic_chores_interval_flush_proto;
int event_time_check_usec; // check how much cputime spent in event loop
};
-class storaged_t : public BnBatteryPropertiesListener,
- public IBinder::DeathRecipient {
-private:
+class storaged_t : public android::hardware::health::V2_0::IHealthInfoCallback,
+ public android::hardware::hidl_death_recipient {
+ private:
time_t mTimer;
storaged_config mConfig;
- disk_stats_publisher mDiskStats;
- disk_stats_monitor mDsm;
+ unique_ptr<disk_stats_monitor> mDsm;
uid_monitor mUidm;
time_t mStarttime;
- sp<IBatteryPropertiesRegistrar> battery_properties;
- std::unique_ptr<storage_info_t> storage_info;
-public:
+ sp<android::hardware::health::V2_0::IHealth> health;
+ unique_ptr<storage_info_t> storage_info;
+ static const uint32_t current_version;
+ unordered_map<userid_t, bool> proto_loaded;
+ void load_proto(userid_t user_id);
+ char* prepare_proto(userid_t user_id, StoragedProto* proto);
+ void flush_proto(userid_t user_id, StoragedProto* proto);
+ void flush_proto_data(userid_t user_id, const char* data, ssize_t size);
+ string proto_path(userid_t user_id) {
+ return string("/data/misc_ce/") + to_string(user_id) +
+ "/storaged/storaged.proto";
+ }
+ void init_health_service();
+
+ public:
storaged_t(void);
- ~storaged_t() {}
+ void init(void);
void event(void);
void event_checked(void);
void pause(void) {
@@ -270,24 +110,37 @@
return mStarttime;
}
- std::unordered_map<uint32_t, struct uid_info> get_uids(void) {
+ unordered_map<uint32_t, uid_info> get_uids(void) {
return mUidm.get_uid_io_stats();
}
- std::map<uint64_t, struct uid_records> get_uid_records(
+
+ vector<int> get_perf_history(void) {
+ return storage_info->get_perf_history();
+ }
+
+ uint32_t get_recent_perf(void) { return storage_info->get_recent_perf(); }
+
+ map<uint64_t, struct uid_records> get_uid_records(
double hours, uint64_t threshold, bool force_report) {
return mUidm.dump(hours, threshold, force_report);
}
+
void update_uid_io_interval(int interval) {
if (interval >= DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO_LIMIT) {
mConfig.periodic_chores_interval_uid_io = interval;
}
}
- void init_battery_service();
- virtual void batteryPropertiesChanged(struct BatteryProperties props);
- void binderDied(const wp<IBinder>& who);
+ void add_user_ce(userid_t user_id);
+ void remove_user_ce(userid_t user_id);
+
+ virtual ::android::hardware::Return<void> healthInfoChanged(
+ const ::android::hardware::health::V2_0::HealthInfo& info);
+ void serviceDied(uint64_t cookie, const wp<::android::hidl::base::V1_0::IBase>& who);
void report_storage_info();
+
+ void flush_protos(unordered_map<int, StoragedProto>* protos);
};
// Eventlog tag
diff --git a/storaged/include/storaged_diskstats.h b/storaged/include/storaged_diskstats.h
new file mode 100644
index 0000000..0b93ba6
--- /dev/null
+++ b/storaged/include/storaged_diskstats.h
@@ -0,0 +1,199 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#ifndef _STORAGED_DISKSTATS_H_
+#define _STORAGED_DISKSTATS_H_
+
+#include <stdint.h>
+
+#include <android/hardware/health/2.0/IHealth.h>
+
+// number of attributes diskstats has
+#define DISK_STATS_SIZE ( 11 )
+
+#define MMC_DISK_STATS_PATH "/sys/block/mmcblk0/stat"
+#define SDA_DISK_STATS_PATH "/sys/block/sda/stat"
+
+struct disk_stats {
+ /* It will be extremely unlikely for any of the following entries to overflow.
+ * For read_bytes(which will be greater than any of the following entries), it
+ * will take 27 years to overflow uint64_t at the reading rate of 20GB/s, which
+ * is the peak memory transfer rate for current memory.
+ * The diskstats entries (first 11) need to be at top in this structure _after_
+ * compiler's optimization.
+ */
+ uint64_t read_ios; // number of read I/Os processed
+ uint64_t read_merges; // number of read I/Os merged with in-queue I/Os
+ uint64_t read_sectors; // number of sectors read
+ uint64_t read_ticks; // total wait time for read requests
+ uint64_t write_ios; // number of write I/Os processed
+ uint64_t write_merges; // number of write I/Os merged with in-queue I/Os
+ uint64_t write_sectors; // number of sectors written
+ uint64_t write_ticks; // total wait time for write requests
+ uint64_t io_in_flight; // number of I/Os currently in flight
+ uint64_t io_ticks; // total time this block device has been active
+ uint64_t io_in_queue; // total wait time for all requests
+
+ uint64_t start_time; // monotonic time accounting starts
+ uint64_t end_time; // monotonic time accounting ends
+ uint32_t counter; // private counter for accumulate calculations
+ double io_avg; // average io_in_flight for accumulate calculations
+
+ bool is_zero() {
+ return read_ios == 0 && write_ios == 0 &&
+ io_in_flight == 0 && io_ticks == 0 && io_in_queue == 0;
+ }
+
+ friend disk_stats operator- (disk_stats curr, const disk_stats& prev) {
+ curr.read_ios -= prev.read_ios;
+ curr.read_merges -= prev.read_merges;
+ curr.read_sectors -= prev.read_sectors;
+ curr.read_ticks -= prev.read_ticks;
+ curr.write_ios -= prev.write_ios;
+ curr.write_merges -= prev.write_merges;
+ curr.write_sectors -= prev.write_sectors;
+ curr.write_ticks -= prev.write_ticks;
+ /* skips io_in_flight, use current value */
+ curr.io_ticks -= prev.io_ticks;
+ curr.io_in_queue -= prev.io_in_queue;
+ return curr;
+ }
+
+ friend bool operator== (const disk_stats& a, const disk_stats& b) {
+ return a.read_ios == b.read_ios &&
+ a.read_merges == b.read_merges &&
+ a.read_sectors == b.read_sectors &&
+ a.read_ticks == b.read_ticks &&
+ a.write_ios == b.write_ios &&
+ a.write_merges == b.write_merges &&
+ a.write_sectors == b.write_sectors &&
+ a.write_ticks == b.write_ticks &&
+ /* skips io_in_flight */
+ a.io_ticks == b.io_ticks &&
+ a.io_in_queue == b.io_in_queue;
+ }
+
+ disk_stats& operator+= (const disk_stats& stats) {
+ read_ios += stats.read_ios;
+ read_merges += stats.read_merges;
+ read_sectors += stats.read_sectors;
+ read_ticks += stats.read_ticks;
+ write_ios += stats.write_ios;
+ write_merges += stats.write_merges;
+ write_sectors += stats.write_sectors;
+ write_ticks += stats.write_ticks;
+ /* skips io_in_flight, use current value */
+ io_ticks += stats.io_ticks;
+ io_in_queue += stats.io_in_queue;
+ return *this;
+ }
+};
+
+struct disk_perf {
+ uint32_t read_perf; // read speed (kbytes/s)
+ uint32_t read_ios; // read I/Os per second
+ uint32_t write_perf; // write speed (kbytes/s)
+ uint32_t write_ios; // write I/Os per second
+ uint32_t queue; // I/Os in queue
+ bool is_zero() {
+ return read_perf == 0 && read_ios == 0 &&
+ write_perf == 0 && write_ios == 0 && queue == 0;
+ }
+};
+
+class stream_stats {
+private:
+ double mSum;
+ double mSquareSum;
+ uint32_t mCnt;
+public:
+ stream_stats() : mSum(0), mSquareSum(0), mCnt(0) {};
+ ~stream_stats() {};
+ double get_mean() {
+ return mSum / mCnt;
+ }
+ double get_std() {
+ return sqrt(mSquareSum / mCnt - mSum * mSum / (mCnt * mCnt));
+ }
+ void add(uint32_t num) {
+ mSum += (double)num;
+ mSquareSum += (double)num * (double)num;
+ mCnt++;
+ }
+ void evict(uint32_t num) {
+ if (mSum < num || mSquareSum < (double)num * (double)num) return;
+ mSum -= (double)num;
+ mSquareSum -= (double)num * (double)num;
+ mCnt--;
+ }
+};
+
+class disk_stats_monitor {
+private:
+ FRIEND_TEST(storaged_test, disk_stats_monitor);
+ const char* const DISK_STATS_PATH;
+ struct disk_stats mPrevious;
+ struct disk_stats mAccumulate; /* reset after stall */
+ struct disk_stats mAccumulate_pub; /* reset after publish */
+ bool mStall;
+ std::queue<struct disk_perf> mBuffer;
+ struct {
+ stream_stats read_perf; // read speed (bytes/s)
+ stream_stats read_ios; // read I/Os per second
+ stream_stats write_perf; // write speed (bytes/s)
+ stream_stats write_ios; // write I/O per second
+ stream_stats queue; // I/Os in queue
+ } mStats;
+ bool mValid;
+ const uint32_t mWindow;
+ const double mSigma;
+ struct disk_perf mMean;
+ struct disk_perf mStd;
+ android::sp<android::hardware::health::V2_0::IHealth> mHealth;
+
+ void update_mean();
+ void update_std();
+ void add(struct disk_perf* perf);
+ void evict(struct disk_perf* perf);
+ bool detect(struct disk_perf* perf);
+
+ void update(struct disk_stats* stats);
+
+public:
+ disk_stats_monitor(const android::sp<android::hardware::health::V2_0::IHealth>& healthService,
+ uint32_t window_size = 5, double sigma = 1.0)
+ : DISK_STATS_PATH(
+ healthService != nullptr
+ ? nullptr
+ : (access(MMC_DISK_STATS_PATH, R_OK) == 0
+ ? MMC_DISK_STATS_PATH
+ : (access(SDA_DISK_STATS_PATH, R_OK) == 0 ? SDA_DISK_STATS_PATH : nullptr))),
+ mPrevious(),
+ mAccumulate(),
+ mAccumulate_pub(),
+ mStall(false),
+ mValid(false),
+ mWindow(window_size),
+ mSigma(sigma),
+ mMean(),
+ mStd(),
+ mHealth(healthService) {}
+ bool enabled() { return mHealth != nullptr || DISK_STATS_PATH != nullptr; }
+ void update(void);
+ void publish(void);
+};
+
+#endif /* _STORAGED_DISKSTATS_H_ */
diff --git a/storaged/include/storaged_info.h b/storaged/include/storaged_info.h
index 7d04c7a..9c3d0e7 100644
--- a/storaged/include/storaged_info.h
+++ b/storaged/include/storaged_info.h
@@ -19,14 +19,26 @@
#include <string.h>
+#include <chrono>
+
+#include <android/hardware/health/2.0/IHealth.h>
+#include <utils/Mutex.h>
+
+#include "storaged.h"
+#include "storaged.pb.h"
+
#define FRIEND_TEST(test_case_name, test_name) \
friend class test_case_name##_##test_name##_Test
using namespace std;
+using namespace android;
+using namespace chrono;
+using namespace storaged_proto;
class storage_info_t {
-protected:
+ protected:
FRIEND_TEST(storaged_test, storage_info_t);
+ FRIEND_TEST(storaged_test, storage_info_t_proto);
// emmc lifetime
uint16_t eol; // pre-eol (end of life) information
uint16_t lifetime_a; // device life time estimation (type A)
@@ -36,16 +48,38 @@
const string userdata_path = "/data";
uint64_t userdata_total_kb;
uint64_t userdata_free_kb;
+ // io perf history
+ time_point<system_clock> day_start_tp;
+ vector<uint32_t> recent_perf;
+ uint32_t nr_samples;
+ vector<uint32_t> daily_perf;
+ uint32_t nr_days;
+ vector<uint32_t> weekly_perf;
+ uint32_t nr_weeks;
+ Mutex si_mutex;
storage_info_t() : eol(0), lifetime_a(0), lifetime_b(0),
- userdata_total_kb(0), userdata_free_kb(0) {}
+ userdata_total_kb(0), userdata_free_kb(0), nr_samples(0),
+ daily_perf(WEEK_TO_DAYS, 0), nr_days(0),
+ weekly_perf(YEAR_TO_WEEKS, 0), nr_weeks(0) {
+ day_start_tp = system_clock::now();
+ day_start_tp -= chrono::seconds(duration_cast<chrono::seconds>(
+ day_start_tp.time_since_epoch()).count() % DAY_TO_SEC);
+ }
void publish();
storage_info_t* s_info;
-public:
- static storage_info_t* get_storage_info();
- virtual ~storage_info_t() {}
+
+ public:
+ static storage_info_t* get_storage_info(
+ const sp<android::hardware::health::V2_0::IHealth>& healthService);
+ virtual ~storage_info_t() {};
virtual void report() {};
- void refresh();
+ void load_perf_history_proto(const IOPerfHistory& perf_history);
+ void refresh(IOPerfHistory* perf_history);
+ void update_perf_history(uint32_t bw,
+ const time_point<system_clock>& tp);
+ vector<int> get_perf_history();
+ uint32_t get_recent_perf();
};
class emmc_info_t : public storage_info_t {
@@ -69,4 +103,18 @@
virtual void report();
};
+class health_storage_info_t : public storage_info_t {
+ private:
+ using IHealth = hardware::health::V2_0::IHealth;
+ using StorageInfo = hardware::health::V2_0::StorageInfo;
+
+ sp<IHealth> mHealth;
+ void set_values_from_hal_storage_info(const StorageInfo& halInfo);
+
+ public:
+ health_storage_info_t(const sp<IHealth>& service) : mHealth(service){};
+ virtual ~health_storage_info_t() {}
+ virtual void report();
+};
+
#endif /* _STORAGED_INFO_H_ */
diff --git a/storaged/include/storaged_service.h b/storaged/include/storaged_service.h
index a8ddf4c..7ec6864 100644
--- a/storaged/include/storaged_service.h
+++ b/storaged/include/storaged_service.h
@@ -19,42 +19,38 @@
#include <vector>
-#include <binder/IInterface.h>
-#include <binder/IBinder.h>
+#include <binder/BinderService.h>
-#include "storaged.h"
+#include "android/os/BnStoraged.h"
+#include "android/os/storaged/BnStoragedPrivate.h"
-using namespace android;
+using namespace std;
+using namespace android::os;
+using namespace android::os::storaged;
-// Interface
-class IStoraged : public IInterface {
+class StoragedService : public BinderService<StoragedService>, public BnStoraged {
+private:
+ void dumpUidRecordsDebug(int fd, const vector<struct uid_record>& entries);
+ void dumpUidRecords(int fd, const vector<struct uid_record>& entries);
public:
- enum {
- DUMPUIDS = IBinder::FIRST_CALL_TRANSACTION,
- };
- // Request the service to run the test function
- virtual std::vector<struct uid_info> dump_uids(const char* option) = 0;
+ static status_t start();
+ static char const* getServiceName() { return "storaged"; }
+ virtual status_t dump(int fd, const Vector<String16> &args) override;
- DECLARE_META_INTERFACE(Storaged);
+ binder::Status onUserStarted(int32_t userId);
+ binder::Status onUserStopped(int32_t userId);
+ binder::Status getRecentPerf(int32_t* _aidl_return);
};
-// Client
-class BpStoraged : public BpInterface<IStoraged> {
+class StoragedPrivateService : public BinderService<StoragedPrivateService>, public BnStoragedPrivate {
public:
- BpStoraged(const sp<IBinder>& impl) : BpInterface<IStoraged>(impl){};
- virtual std::vector<struct uid_info> dump_uids(const char* option);
+ static status_t start();
+ static char const* getServiceName() { return "storaged_pri"; }
+
+ binder::Status dumpUids(vector<UidInfo>* _aidl_return);
+ binder::Status dumpPerfHistory(vector<int32_t>* _aidl_return);
};
-// Server
-class BnStoraged : public BnInterface<IStoraged> {
- virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0);
-};
-
-class Storaged : public BnStoraged {
- virtual std::vector<struct uid_info> dump_uids(const char* option);
- virtual status_t dump(int fd, const Vector<String16>& args);
-};
-
-sp<IStoraged> get_storaged_service();
+sp<IStoragedPrivate> get_storaged_pri_service();
#endif /* _STORAGED_SERVICE_H_ */
\ No newline at end of file
diff --git a/storaged/include/storaged_uid_monitor.h b/storaged/include/storaged_uid_monitor.h
index 901a872..3a718fa 100644
--- a/storaged/include/storaged_uid_monitor.h
+++ b/storaged/include/storaged_uid_monitor.h
@@ -23,88 +23,103 @@
#include <unordered_map>
#include <vector>
-enum uid_stat_t {
- FOREGROUND = 0,
- BACKGROUND = 1,
- UID_STATS = 2
+#include <cutils/multiuser.h>
+#include <utils/Mutex.h>
+
+#include "storaged.pb.h"
+#include "uid_info.h"
+
+#define FRIEND_TEST(test_case_name, test_name) \
+friend class test_case_name##_##test_name##_Test
+
+using namespace std;
+using namespace storaged_proto;
+using namespace android;
+using namespace android::os::storaged;
+
+class uid_info : public UidInfo {
+public:
+ bool parse_uid_io_stats(string&& s);
};
-enum charger_stat_t {
- CHARGER_OFF = 0,
- CHARGER_ON = 1,
- CHARGER_STATS = 2
-};
-
-enum io_type_t {
- READ = 0,
- WRITE = 1,
- IO_TYPES = 2
-};
-
-struct uid_io_stats {
- uint64_t rchar; // characters read
- uint64_t wchar; // characters written
- uint64_t read_bytes; // bytes read (from storage layer)
- uint64_t write_bytes; // bytes written (to storage layer)
- uint64_t fsync; // number of fsync syscalls
-};
-
-struct uid_info {
- uint32_t uid; // user id
- std::string name; // package name
- struct uid_io_stats io[UID_STATS]; // [0]:foreground [1]:background
+class io_usage {
+public:
+ io_usage() : bytes{{{0}}} {};
+ uint64_t bytes[IO_TYPES][UID_STATS][CHARGER_STATS];
+ bool is_zero() const;
+ io_usage& operator+= (const io_usage& stats) {
+ for (int i = 0; i < IO_TYPES; i++) {
+ for (int j = 0; j < UID_STATS; j++) {
+ for (int k = 0; k < CHARGER_STATS; k++) {
+ bytes[i][j][k] += stats.bytes[i][j][k];
+ }
+ }
+ }
+ return *this;
+ }
};
struct uid_io_usage {
- uint64_t bytes[IO_TYPES][UID_STATS][CHARGER_STATS];
+ userid_t user_id;
+ io_usage uid_ios;
+ // mapped from task comm to task io usage
+ map<string, io_usage> task_ios;
};
struct uid_record {
- std::string name;
+ string name;
struct uid_io_usage ios;
};
struct uid_records {
uint64_t start_ts;
- std::vector<struct uid_record> entries;
+ vector<struct uid_record> entries;
};
class uid_monitor {
private:
+ FRIEND_TEST(storaged_test, uid_monitor);
// last dump from /proc/uid_io/stats, uid -> uid_info
- std::unordered_map<uint32_t, struct uid_info> last_uid_io_stats;
+ unordered_map<uint32_t, uid_info> last_uid_io_stats;
// current io usage for next report, app name -> uid_io_usage
- std::unordered_map<std::string, struct uid_io_usage> curr_io_stats;
+ unordered_map<string, struct uid_io_usage> curr_io_stats;
// io usage records, end timestamp -> {start timestamp, vector of records}
- std::map<uint64_t, struct uid_records> records;
+ map<uint64_t, struct uid_records> io_history;
// charger ON/OFF
charger_stat_t charger_stat;
// protects curr_io_stats, last_uid_io_stats, records and charger_stat
- sem_t um_lock;
+ Mutex uidm_mutex;
// start time for IO records
uint64_t start_ts;
+ // true if UID_IO_STATS_PATH is accessible
+ const bool enable;
// reads from /proc/uid_io/stats
- std::unordered_map<uint32_t, struct uid_info> get_uid_io_stats_locked();
+ unordered_map<uint32_t, uid_info> get_uid_io_stats_locked();
// flushes curr_io_stats to records
void add_records_locked(uint64_t curr_ts);
// updates curr_io_stats and set last_uid_io_stats
void update_curr_io_stats_locked();
+ // writes io_history to protobuf
+ void update_uid_io_proto(unordered_map<int, StoragedProto>* protos);
public:
uid_monitor();
- ~uid_monitor();
// called by storaged main thread
void init(charger_stat_t stat);
// called by storaged -u
- std::unordered_map<uint32_t, struct uid_info> get_uid_io_stats();
+ unordered_map<uint32_t, uid_info> get_uid_io_stats();
// called by dumpsys
- std::map<uint64_t, struct uid_records> dump(
+ map<uint64_t, struct uid_records> dump(
double hours, uint64_t threshold, bool force_report);
// called by battery properties listener
void set_charger_state(charger_stat_t stat);
// called by storaged periodic_chore or dump with force_report
- void report();
+ bool enabled() { return enable; };
+ void report(unordered_map<int, StoragedProto>* protos);
+ // restores io_history from protobuf
+ void load_uid_io_proto(const UidIOUsage& proto);
+ void clear_user_history(userid_t user_id);
};
#endif /* _STORAGED_UID_MONITOR_H_ */
diff --git a/storaged/include/storaged_utils.h b/storaged/include/storaged_utils.h
index 2161c40..62cb12d 100644
--- a/storaged/include/storaged_utils.h
+++ b/storaged/include/storaged_utils.h
@@ -24,21 +24,20 @@
#include "storaged.h"
+using namespace android::os::storaged;
+
// Diskstats
bool parse_disk_stats(const char* disk_stats_path, struct disk_stats* stats);
struct disk_perf get_disk_perf(struct disk_stats* stats);
-struct disk_stats get_inc_disk_stats(struct disk_stats* prev, struct disk_stats* curr);
+void get_inc_disk_stats(const struct disk_stats* prev, const struct disk_stats* curr, struct disk_stats* inc);
void add_disk_stats(struct disk_stats* src, struct disk_stats* dst);
-bool parse_emmc_ecsd(int ext_csd_fd, struct emmc_info* info);
// UID I/O
-void sort_running_uids_info(std::vector<struct uid_info> &uids);
+map<string, io_usage> merge_io_usage(const vector<uid_record>& entries);
+void sort_running_uids_info(std::vector<UidInfo> &uids);
// Logging
-void log_console_running_uids_info(std::vector<struct uid_info> uids);
+void log_console_running_uids_info(const std::vector<UidInfo>& uids, bool flag_dump_task);
+void log_console_perf_history(const vector<int>& perf_history);
-void log_debug_disk_perf(struct disk_perf* perf, const char* type);
-
-void log_event_disk_stats(struct disk_stats* stats, const char* type);
-void log_event_emmc_info(struct emmc_info* info_);
#endif /* _STORAGED_UTILS_H_ */
diff --git a/storaged/include/uid_info.h b/storaged/include/uid_info.h
new file mode 100644
index 0000000..c5533ac
--- /dev/null
+++ b/storaged/include/uid_info.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+#ifndef _UID_INFO_H_
+#define _UID_INFO_H_
+
+#include <string>
+#include <unordered_map>
+
+#include <binder/Parcelable.h>
+
+namespace android {
+namespace os {
+namespace storaged {
+
+enum uid_stat_t {
+ FOREGROUND = 0,
+ BACKGROUND = 1,
+ UID_STATS = 2
+};
+
+enum charger_stat_t {
+ CHARGER_OFF = 0,
+ CHARGER_ON = 1,
+ CHARGER_STATS = 2
+};
+
+enum io_type_t {
+ READ = 0,
+ WRITE = 1,
+ IO_TYPES = 2
+};
+
+struct io_stats {
+ uint64_t rchar; // characters read
+ uint64_t wchar; // characters written
+ uint64_t read_bytes; // bytes read (from storage layer)
+ uint64_t write_bytes; // bytes written (to storage layer)
+ uint64_t fsync; // number of fsync syscalls
+};
+
+class task_info {
+public:
+ std::string comm;
+ pid_t pid;
+ io_stats io[UID_STATS];
+ bool parse_task_io_stats(std::string&& s);
+};
+
+class UidInfo : public Parcelable {
+public:
+ uint32_t uid; // user id
+ std::string name; // package name
+ io_stats io[UID_STATS]; // [0]:foreground [1]:background
+ std::unordered_map<uint32_t, task_info> tasks; // mapped from pid
+
+ status_t writeToParcel(Parcel* parcel) const override;
+ status_t readFromParcel(const Parcel* parcel) override;
+};
+
+} // namespace storaged
+} // namespace os
+} // namespace android
+
+#endif /* _UID_INFO_H_ */
\ No newline at end of file
diff --git a/storaged/main.cpp b/storaged/main.cpp
index 6b82904..3817fb5 100644
--- a/storaged/main.cpp
+++ b/storaged/main.cpp
@@ -21,9 +21,6 @@
#include <getopt.h>
#include <pthread.h>
#include <stdio.h>
-#include <sys/capability.h>
-#include <sys/prctl.h>
-#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <vector>
@@ -42,72 +39,81 @@
#include <storaged_service.h>
#include <storaged_utils.h>
-sp<storaged_t> storaged;
+using namespace std;
+using namespace android;
+
+sp<storaged_t> storaged_sp;
// Function of storaged's main thread
void* storaged_main(void* /* unused */) {
- storaged = new storaged_t();
+ storaged_sp = new storaged_t();
- storaged->init_battery_service();
- storaged->report_storage_info();
+ storaged_sp->init();
+ storaged_sp->report_storage_info();
LOG_TO(SYSTEM, INFO) << "storaged: Start";
for (;;) {
- storaged->event_checked();
- storaged->pause();
+ storaged_sp->event_checked();
+ storaged_sp->pause();
}
return NULL;
}
-static void help_message(void) {
+void help_message(void) {
printf("usage: storaged [OPTION]\n");
printf(" -u --uid Dump uid I/O usage to stdout\n");
+ printf(" -t --task Dump task I/O usage to stdout\n");
+ printf(" -p --perf Dump I/O perf history to stdout\n");
printf(" -s --start Start storaged (default)\n");
fflush(stdout);
}
int main(int argc, char** argv) {
- int flag_main_service = 0;
- int flag_dump_uid = 0;
+ bool flag_main_service = false;
+ bool flag_dump_uid = false;
+ bool flag_dump_task = false;
+ bool flag_dump_perf = false;
int opt;
for (;;) {
int opt_idx = 0;
static struct option long_options[] = {
- {"start", no_argument, 0, 's'},
- {"kill", no_argument, 0, 'k'},
- {"uid", no_argument, 0, 'u'},
- {"help", no_argument, 0, 'h'}
+ {"perf", no_argument, nullptr, 'p'},
+ {"start", no_argument, nullptr, 's'},
+ {"task", no_argument, nullptr, 't'},
+ {"uid", no_argument, nullptr, 'u'},
+ {nullptr, 0, nullptr, 0}
};
- opt = getopt_long(argc, argv, ":skdhu0", long_options, &opt_idx);
+ opt = getopt_long(argc, argv, ":pstu", long_options, &opt_idx);
if (opt == -1) {
break;
}
switch (opt) {
+ case 'p':
+ flag_dump_perf = true;
+ break;
case 's':
- flag_main_service = 1;
+ flag_main_service = true;
+ break;
+ case 't':
+ flag_dump_task = true;
break;
case 'u':
- flag_dump_uid = 1;
+ flag_dump_uid = true;
break;
- case 'h':
+ default:
help_message();
return 0;
- case '?':
- default:
- fprintf(stderr, "no supported option\n");
- help_message();
- return -1;
}
}
if (argc == 1) {
- flag_main_service = 1;
+ flag_main_service = true;
}
- if (flag_main_service && flag_dump_uid) {
+ if (flag_main_service && (flag_dump_uid || flag_dump_task)) {
fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
help_message();
return -1;
@@ -122,7 +128,12 @@
return -1;
}
- defaultServiceManager()->addService(String16("storaged"), new Storaged());
+ if (StoragedService::start() != android::OK ||
+ StoragedPrivateService::start() != android::OK) {
+ PLOG_TO(SYSTEM, ERROR) << "Failed to start storaged service";
+ return -1;
+ }
+
android::ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
pthread_join(storaged_main_thread, NULL);
@@ -130,23 +141,33 @@
return 0;
}
- if (flag_dump_uid) {
- sp<IStoraged> storaged_service = get_storaged_service();
- if (storaged_service == NULL) {
- fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
- return -1;
- }
- std::vector<struct uid_info> res = storaged_service->dump_uids(NULL);
+ sp<IStoragedPrivate> storaged_service = get_storaged_pri_service();
+ if (storaged_service == NULL) {
+ fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
+ return -1;
+ }
- if (res.size() == 0) {
- fprintf(stderr, "UID I/O is not readable in this version of kernel.\n");
+ if (flag_dump_uid || flag_dump_task) {
+ vector<UidInfo> uid_io;
+ binder::Status status = storaged_service->dumpUids(&uid_io);
+ if (!status.isOk() || uid_io.size() == 0) {
+ fprintf(stderr, "UID I/O info is not available.\n");
return 0;
}
- sort_running_uids_info(res);
- log_console_running_uids_info(res);
+ sort_running_uids_info(uid_io);
+ log_console_running_uids_info(uid_io, flag_dump_task);
+ }
- return 0;
+ if (flag_dump_perf) {
+ vector<int> perf_history;
+ binder::Status status = storaged_service->dumpPerfHistory(&perf_history);
+ if (!status.isOk() || perf_history.size() == 0) {
+ fprintf(stderr, "I/O perf history is not available.\n");
+ return 0;
+ }
+
+ log_console_perf_history(perf_history);
}
return 0;
diff --git a/storaged/storaged.cpp b/storaged/storaged.cpp
index 06afea6..f346c38 100644
--- a/storaged/storaged.cpp
+++ b/storaged/storaged.cpp
@@ -16,184 +16,118 @@
#define LOG_TAG "storaged"
+#include <dirent.h>
#include <stdlib.h>
+#include <stdio.h>
#include <time.h>
#include <unistd.h>
+#include <zlib.h>
+#include <chrono>
+#include <fstream>
+#include <sstream>
+#include <string>
+
+#include <android-base/file.h>
#include <android-base/logging.h>
+#include <android-base/unique_fd.h>
+#include <android/hidl/manager/1.0/IServiceManager.h>
#include <batteryservice/BatteryServiceConstants.h>
-#include <batteryservice/IBatteryPropertiesRegistrar.h>
-#include <binder/IPCThreadState.h>
-#include <binder/IServiceManager.h>
#include <cutils/properties.h>
+#include <healthhalutils/HealthHalUtils.h>
+#include <hidl/HidlTransportSupport.h>
+#include <hwbinder/IPCThreadState.h>
#include <log/log.h>
#include <storaged.h>
#include <storaged_utils.h>
-/* disk_stats_publisher */
-void disk_stats_publisher::publish(void) {
- // Logging
- struct disk_perf perf = get_disk_perf(&mAccumulate);
- log_debug_disk_perf(&perf, "regular");
- log_event_disk_stats(&mAccumulate, "regular");
- // Reset global structures
- memset(&mAccumulate, 0, sizeof(struct disk_stats));
-}
+using namespace android::base;
+using namespace chrono;
+using namespace google::protobuf::io;
+using namespace storaged_proto;
-void disk_stats_publisher::update(void) {
- struct disk_stats curr;
- if (parse_disk_stats(DISK_STATS_PATH, &curr)) {
- struct disk_stats inc = get_inc_disk_stats(&mPrevious, &curr);
- add_disk_stats(&inc, &mAccumulate);
-#ifdef DEBUG
-// log_kernel_disk_stats(&mPrevious, "prev stats");
-// log_kernel_disk_stats(&curr, "curr stats");
-// log_kernel_disk_stats(&inc, "inc stats");
-// log_kernel_disk_stats(&mAccumulate, "accumulated stats");
-#endif
- mPrevious = curr;
- }
-}
+namespace {
-/* disk_stats_monitor */
-void disk_stats_monitor::update_mean() {
- CHECK(mValid);
- mMean.read_perf = (uint32_t)mStats.read_perf.get_mean();
- mMean.read_ios = (uint32_t)mStats.read_ios.get_mean();
- mMean.write_perf = (uint32_t)mStats.write_perf.get_mean();
- mMean.write_ios = (uint32_t)mStats.write_ios.get_mean();
- mMean.queue = (uint32_t)mStats.queue.get_mean();
-}
+/*
+ * The system user is the initial user that is implicitly created on first boot
+ * and hosts most of the system services. Keep this in sync with
+ * frameworks/base/core/java/android/os/UserManager.java
+ */
+constexpr int USER_SYSTEM = 0;
-void disk_stats_monitor::update_std() {
- CHECK(mValid);
- mStd.read_perf = (uint32_t)mStats.read_perf.get_std();
- mStd.read_ios = (uint32_t)mStats.read_ios.get_std();
- mStd.write_perf = (uint32_t)mStats.write_perf.get_std();
- mStd.write_ios = (uint32_t)mStats.write_ios.get_std();
- mStd.queue = (uint32_t)mStats.queue.get_std();
-}
+constexpr ssize_t benchmark_unit_size = 16 * 1024; // 16KB
-void disk_stats_monitor::add(struct disk_perf* perf) {
- mStats.read_perf.add(perf->read_perf);
- mStats.read_ios.add(perf->read_ios);
- mStats.write_perf.add(perf->write_perf);
- mStats.write_ios.add(perf->write_ios);
- mStats.queue.add(perf->queue);
-}
+constexpr ssize_t min_benchmark_size = 128 * 1024; // 128KB
-void disk_stats_monitor::evict(struct disk_perf* perf) {
- mStats.read_perf.evict(perf->read_perf);
- mStats.read_ios.evict(perf->read_ios);
- mStats.write_perf.evict(perf->write_perf);
- mStats.write_ios.evict(perf->write_ios);
- mStats.queue.evict(perf->queue);
-}
+} // namespace
-bool disk_stats_monitor::detect(struct disk_perf* perf) {
- return ((double)perf->queue >= (double)mMean.queue + mSigma * (double)mStd.queue) &&
- ((double)perf->read_perf < (double)mMean.read_perf - mSigma * (double)mStd.read_perf) &&
- ((double)perf->write_perf < (double)mMean.write_perf - mSigma * (double)mStd.write_perf);
-}
+const uint32_t storaged_t::current_version = 4;
-void disk_stats_monitor::update(struct disk_stats* stats) {
- struct disk_stats inc = get_inc_disk_stats(&mPrevious, stats);
- struct disk_perf perf = get_disk_perf(&inc);
- // Update internal data structures
- if (LIKELY(mValid)) {
- CHECK_EQ(mBuffer.size(), mWindow);
+using android::hardware::interfacesEqual;
+using android::hardware::Return;
+using android::hardware::health::V1_0::BatteryStatus;
+using android::hardware::health::V1_0::toString;
+using android::hardware::health::V2_0::get_health_service;
+using android::hardware::health::V2_0::HealthInfo;
+using android::hardware::health::V2_0::IHealth;
+using android::hardware::health::V2_0::Result;
+using android::hidl::manager::V1_0::IServiceManager;
- if (UNLIKELY(detect(&perf))) {
- mStall = true;
- add_disk_stats(&inc, &mAccumulate);
- log_debug_disk_perf(&mMean, "stalled_mean");
- log_debug_disk_perf(&mStd, "stalled_std");
- } else {
- if (mStall) {
- struct disk_perf acc_perf = get_disk_perf(&mAccumulate);
- log_debug_disk_perf(&acc_perf, "stalled");
- log_event_disk_stats(&mAccumulate, "stalled");
- mStall = false;
- memset(&mAccumulate, 0, sizeof(mAccumulate));
- }
- }
- evict(&mBuffer.front());
- mBuffer.pop();
- add(&perf);
- mBuffer.push(perf);
-
- update_mean();
- update_std();
-
- } else { /* mValid == false */
- CHECK_LT(mBuffer.size(), mWindow);
- add(&perf);
- mBuffer.push(perf);
- if (mBuffer.size() == mWindow) {
- mValid = true;
- update_mean();
- update_std();
- }
- }
-
- mPrevious = *stats;
-}
-
-void disk_stats_monitor::update(void) {
- struct disk_stats curr;
- if (LIKELY(parse_disk_stats(DISK_STATS_PATH, &curr))) {
- update(&curr);
- }
-}
-
-static sp<IBatteryPropertiesRegistrar> get_battery_properties_service() {
- sp<IServiceManager> sm = defaultServiceManager();
- if (sm == NULL) return NULL;
-
- sp<IBinder> binder = sm->getService(String16("batteryproperties"));
- if (binder == NULL) return NULL;
-
- sp<IBatteryPropertiesRegistrar> battery_properties =
- interface_cast<IBatteryPropertiesRegistrar>(binder);
-
- return battery_properties;
-}
-
-static inline charger_stat_t is_charger_on(int64_t prop) {
- return (prop == BATTERY_STATUS_CHARGING || prop == BATTERY_STATUS_FULL) ?
+inline charger_stat_t is_charger_on(BatteryStatus prop) {
+ return (prop == BatteryStatus::CHARGING || prop == BatteryStatus::FULL) ?
CHARGER_ON : CHARGER_OFF;
}
-void storaged_t::batteryPropertiesChanged(struct BatteryProperties props) {
- mUidm.set_charger_state(is_charger_on(props.batteryStatus));
+Return<void> storaged_t::healthInfoChanged(const HealthInfo& props) {
+ mUidm.set_charger_state(is_charger_on(props.legacy.batteryStatus));
+ return android::hardware::Void();
}
-void storaged_t::init_battery_service() {
- if (!mConfig.proc_uid_io_available)
+void storaged_t::init() {
+ init_health_service();
+ mDsm = std::make_unique<disk_stats_monitor>(health);
+ storage_info.reset(storage_info_t::get_storage_info(health));
+}
+
+void storaged_t::init_health_service() {
+ if (!mUidm.enabled())
return;
- battery_properties = get_battery_properties_service();
- if (battery_properties == NULL) {
- LOG_TO(SYSTEM, WARNING) << "failed to find batteryproperties service";
+ health = get_health_service();
+ if (health == NULL) {
+ LOG_TO(SYSTEM, WARNING) << "health: failed to find IHealth service";
return;
}
- struct BatteryProperty val;
- battery_properties->getProperty(BATTERY_PROP_BATTERY_STATUS, &val);
- mUidm.init(is_charger_on(val.valueInt64));
+ BatteryStatus status = BatteryStatus::UNKNOWN;
+ auto ret = health->getChargeStatus([&](Result r, BatteryStatus v) {
+ if (r != Result::SUCCESS) {
+ LOG_TO(SYSTEM, WARNING)
+ << "health: cannot get battery status " << toString(r);
+ return;
+ }
+ if (v == BatteryStatus::UNKNOWN) {
+ LOG_TO(SYSTEM, WARNING) << "health: invalid battery status";
+ }
+ status = v;
+ });
+ if (!ret.isOk()) {
+ LOG_TO(SYSTEM, WARNING) << "health: get charge status transaction error "
+ << ret.description();
+ }
+ mUidm.init(is_charger_on(status));
// register listener after init uid_monitor
- battery_properties->registerListener(this);
- IInterface::asBinder(battery_properties)->linkToDeath(this);
+ health->registerCallback(this);
+ health->linkToDeath(this, 0 /* cookie */);
}
-void storaged_t::binderDied(const wp<IBinder>& who) {
- if (battery_properties != NULL &&
- IInterface::asBinder(battery_properties) == who) {
- LOG_TO(SYSTEM, ERROR) << "batteryproperties service died, exiting";
- IPCThreadState::self()->stopProcess();
+void storaged_t::serviceDied(uint64_t cookie, const wp<::android::hidl::base::V1_0::IBase>& who) {
+ if (health != NULL && interfacesEqual(health, who.promote())) {
+ LOG_TO(SYSTEM, ERROR) << "health service died, exiting";
+ android::hardware::IPCThreadState::self()->stopProcess();
exit(1);
} else {
LOG_TO(SYSTEM, ERROR) << "unknown service died";
@@ -206,44 +140,195 @@
/* storaged_t */
storaged_t::storaged_t(void) {
- if (access(MMC_DISK_STATS_PATH, R_OK) < 0 && access(SDA_DISK_STATS_PATH, R_OK) < 0) {
- mConfig.diskstats_available = false;
- } else {
- mConfig.diskstats_available = true;
- }
-
- mConfig.proc_uid_io_available = (access(UID_IO_STATS_PATH, R_OK) == 0);
-
mConfig.periodic_chores_interval_unit =
- property_get_int32("ro.storaged.event.interval", DEFAULT_PERIODIC_CHORES_INTERVAL_UNIT);
+ property_get_int32("ro.storaged.event.interval",
+ DEFAULT_PERIODIC_CHORES_INTERVAL_UNIT);
mConfig.event_time_check_usec =
property_get_int32("ro.storaged.event.perf_check", 0);
mConfig.periodic_chores_interval_disk_stats_publish =
- property_get_int32("ro.storaged.disk_stats_pub", DEFAULT_PERIODIC_CHORES_INTERVAL_DISK_STATS_PUBLISH);
+ property_get_int32("ro.storaged.disk_stats_pub",
+ DEFAULT_PERIODIC_CHORES_INTERVAL_DISK_STATS_PUBLISH);
mConfig.periodic_chores_interval_uid_io =
- property_get_int32("ro.storaged.uid_io.interval", DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO);
+ property_get_int32("ro.storaged.uid_io.interval",
+ DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO);
- storage_info.reset(storage_info_t::get_storage_info());
+ mConfig.periodic_chores_interval_flush_proto =
+ property_get_int32("ro.storaged.flush_proto.interval",
+ DEFAULT_PERIODIC_CHORES_INTERVAL_FLUSH_PROTO);
mStarttime = time(NULL);
+ mTimer = 0;
}
-void storaged_t::event(void) {
- if (mConfig.diskstats_available) {
- mDiskStats.update();
- mDsm.update();
- storage_info->refresh();
- if (mTimer && (mTimer % mConfig.periodic_chores_interval_disk_stats_publish) == 0) {
- mDiskStats.publish();
+void storaged_t::add_user_ce(userid_t user_id) {
+ load_proto(user_id);
+ proto_loaded[user_id] = true;
+}
+
+void storaged_t::remove_user_ce(userid_t user_id) {
+ proto_loaded[user_id] = false;
+ mUidm.clear_user_history(user_id);
+ RemoveFileIfExists(proto_path(user_id), nullptr);
+}
+
+void storaged_t::load_proto(userid_t user_id) {
+ string proto_file = proto_path(user_id);
+ ifstream in(proto_file, ofstream::in | ofstream::binary);
+
+ if (!in.good()) return;
+
+ stringstream ss;
+ ss << in.rdbuf();
+ StoragedProto proto;
+ proto.ParseFromString(ss.str());
+
+ const UidIOUsage& uid_io_usage = proto.uid_io_usage();
+ uint32_t computed_crc = crc32(current_version,
+ reinterpret_cast<const Bytef*>(uid_io_usage.SerializeAsString().c_str()),
+ uid_io_usage.ByteSize());
+ if (proto.crc() != computed_crc) {
+ LOG_TO(SYSTEM, WARNING) << "CRC mismatch in " << proto_file;
+ return;
+ }
+
+ mUidm.load_uid_io_proto(proto.uid_io_usage());
+
+ if (user_id == USER_SYSTEM) {
+ storage_info->load_perf_history_proto(proto.perf_history());
+ }
+}
+
+char* storaged_t:: prepare_proto(userid_t user_id, StoragedProto* proto) {
+ proto->set_version(current_version);
+
+ const UidIOUsage& uid_io_usage = proto->uid_io_usage();
+ proto->set_crc(crc32(current_version,
+ reinterpret_cast<const Bytef*>(uid_io_usage.SerializeAsString().c_str()),
+ uid_io_usage.ByteSize()));
+
+ uint32_t pagesize = sysconf(_SC_PAGESIZE);
+ if (user_id == USER_SYSTEM) {
+ proto->set_padding("", 1);
+ vector<char> padding;
+ ssize_t size = ROUND_UP(MAX(min_benchmark_size, proto->ByteSize()),
+ pagesize);
+ padding = vector<char>(size - proto->ByteSize(), 0xFD);
+ proto->set_padding(padding.data(), padding.size());
+ while (!IS_ALIGNED(proto->ByteSize(), pagesize)) {
+ padding.push_back(0xFD);
+ proto->set_padding(padding.data(), padding.size());
}
}
- if (mConfig.proc_uid_io_available && mTimer &&
- (mTimer % mConfig.periodic_chores_interval_uid_io) == 0) {
- mUidm.report();
+ char* data = nullptr;
+ if (posix_memalign(reinterpret_cast<void**>(&data),
+ pagesize, proto->ByteSize())) {
+ PLOG_TO(SYSTEM, ERROR) << "Faied to alloc aligned buffer (size: "
+ << proto->ByteSize() << ")";
+ return data;
+ }
+
+ proto->SerializeToArray(data, proto->ByteSize());
+ return data;
+}
+
+void storaged_t::flush_proto_data(userid_t user_id,
+ const char* data, ssize_t size) {
+ string proto_file = proto_path(user_id);
+ string tmp_file = proto_file + "_tmp";
+ unique_fd fd(TEMP_FAILURE_RETRY(open(tmp_file.c_str(),
+ O_SYNC | O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC |
+ (user_id == USER_SYSTEM ? O_DIRECT : 0),
+ S_IRUSR | S_IWUSR)));
+ if (fd == -1) {
+ PLOG_TO(SYSTEM, ERROR) << "Faied to open tmp file: " << tmp_file;
+ return;
+ }
+
+ if (user_id == USER_SYSTEM) {
+ time_point<steady_clock> start, end;
+ uint32_t benchmark_size = 0;
+ uint64_t benchmark_time_ns = 0;
+ ssize_t ret;
+ bool first_write = true;
+
+ while (size > 0) {
+ start = steady_clock::now();
+ ret = write(fd, data, MIN(benchmark_unit_size, size));
+ if (ret <= 0) {
+ PLOG_TO(SYSTEM, ERROR) << "Faied to write tmp file: " << tmp_file;
+ return;
+ }
+ end = steady_clock::now();
+ /*
+ * compute bandwidth after the first write and if write returns
+ * exactly unit size.
+ */
+ if (!first_write && ret == benchmark_unit_size) {
+ benchmark_size += benchmark_unit_size;
+ benchmark_time_ns += duration_cast<nanoseconds>(end - start).count();
+ }
+ size -= ret;
+ data += ret;
+ first_write = false;
+ }
+
+ if (benchmark_size) {
+ int perf = benchmark_size * 1000000LLU / benchmark_time_ns;
+ storage_info->update_perf_history(perf, system_clock::now());
+ }
+ } else {
+ if (!WriteFully(fd, data, size)) {
+ PLOG_TO(SYSTEM, ERROR) << "Faied to write tmp file: " << tmp_file;
+ return;
+ }
+ }
+
+ fd.reset(-1);
+ rename(tmp_file.c_str(), proto_file.c_str());
+}
+
+void storaged_t::flush_proto(userid_t user_id, StoragedProto* proto) {
+ unique_ptr<char> proto_data(prepare_proto(user_id, proto));
+ if (proto_data == nullptr) return;
+
+ flush_proto_data(user_id, proto_data.get(), proto->ByteSize());
+}
+
+void storaged_t::flush_protos(unordered_map<int, StoragedProto>* protos) {
+ for (auto& it : *protos) {
+ /*
+ * Don't flush proto if we haven't attempted to load it from file.
+ */
+ if (proto_loaded[it.first]) {
+ flush_proto(it.first, &it.second);
+ }
+ }
+}
+
+void storaged_t::event(void) {
+ unordered_map<int, StoragedProto> protos;
+
+ if (mDsm->enabled()) {
+ mDsm->update();
+ if (!(mTimer % mConfig.periodic_chores_interval_disk_stats_publish)) {
+ mDsm->publish();
+ }
+ }
+
+ if (!(mTimer % mConfig.periodic_chores_interval_uid_io)) {
+ mUidm.report(&protos);
+ }
+
+ if (storage_info) {
+ storage_info->refresh(protos[USER_SYSTEM].mutable_perf_history());
+ }
+
+ if (!(mTimer % mConfig.periodic_chores_interval_flush_proto)) {
+ flush_protos(&protos);
}
mTimer += mConfig.periodic_chores_interval_unit;
diff --git a/storaged/storaged.proto b/storaged/storaged.proto
new file mode 100644
index 0000000..2000c0b
--- /dev/null
+++ b/storaged/storaged.proto
@@ -0,0 +1,60 @@
+syntax = "proto2";
+option optimize_for = LITE_RUNTIME;
+package storaged_proto;
+option java_package = "com.android.storaged.proto";
+option java_outer_classname = "Storaged";
+
+message IOUsage {
+ optional uint64 rd_fg_chg_on = 1;
+ optional uint64 rd_fg_chg_off = 2;
+ optional uint64 rd_bg_chg_on = 3;
+ optional uint64 rd_bg_chg_off = 4;
+ optional uint64 wr_fg_chg_on = 5;
+ optional uint64 wr_fg_chg_off = 6;
+ optional uint64 wr_bg_chg_on = 7;
+ optional uint64 wr_bg_chg_off = 8;
+}
+
+message TaskIOUsage {
+ optional string task_name = 1;
+ optional IOUsage ios = 2;
+}
+
+message UidRecord {
+ optional string uid_name = 1;
+ optional uint32 user_id = 2;
+ optional IOUsage uid_io = 3;
+ repeated TaskIOUsage task_io = 4;
+}
+
+message UidIORecords {
+ optional uint64 start_ts = 1;
+ repeated UidRecord entries = 2;
+}
+
+message UidIOItem {
+ optional uint64 end_ts = 1;
+ optional UidIORecords records = 2;
+}
+
+message UidIOUsage {
+ repeated UidIOItem uid_io_items = 2;
+}
+
+message IOPerfHistory {
+ optional uint64 day_start_sec = 1;
+ repeated uint32 recent_perf = 2;
+ optional uint32 nr_samples = 3;
+ repeated uint32 daily_perf = 4;
+ optional uint32 nr_days = 5;
+ repeated uint32 weekly_perf = 6;
+ optional uint32 nr_weeks = 7;
+}
+
+message StoragedProto {
+ optional uint32 crc = 1;
+ optional uint32 version = 2;
+ optional UidIOUsage uid_io_usage = 3;
+ optional IOPerfHistory perf_history = 4;
+ optional bytes padding = 5;
+}
diff --git a/storaged/storaged.rc b/storaged/storaged.rc
index a24c7fb..0614fad 100644
--- a/storaged/storaged.rc
+++ b/storaged/storaged.rc
@@ -1,7 +1,8 @@
service storaged /system/bin/storaged
class main
+ capabilities DAC_READ_SEARCH
priority 10
file /d/mmc0/mmc0:0001/ext_csd r
writepid /dev/cpuset/system-background/tasks
user root
- group package_info
\ No newline at end of file
+ group package_info
diff --git a/storaged/storaged_diskstats.cpp b/storaged/storaged_diskstats.cpp
new file mode 100644
index 0000000..1050033
--- /dev/null
+++ b/storaged/storaged_diskstats.cpp
@@ -0,0 +1,326 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#define LOG_TAG "storaged"
+
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <sstream>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <log/log_event_list.h>
+
+#include "storaged.h"
+#include "storaged_diskstats.h"
+
+namespace {
+
+using android::sp;
+using android::hardware::health::V2_0::DiskStats;
+using android::hardware::health::V2_0::IHealth;
+using android::hardware::health::V2_0::Result;
+using android::hardware::health::V2_0::toString;
+
+#ifdef DEBUG
+void log_debug_disk_perf(struct disk_perf* perf, const char* type) {
+ // skip if the input structure are all zeros
+ if (perf == NULL || perf->is_zero()) return;
+
+ LOG_TO(SYSTEM, INFO) << "disk_perf " << type
+ << " rd: " << perf->read_perf << " kbps, " << perf->read_ios << " iops"
+ << " wr: " << perf->write_perf << " kbps, " << perf->write_ios << " iops"
+ << " q: " << perf->queue;
+}
+#else
+void log_debug_disk_perf(struct disk_perf* perf, const char* type) {}
+#endif
+
+void log_event_disk_stats(struct disk_stats* stats, const char* type) {
+ // skip if the input structure are all zeros
+ if (stats == NULL || stats->is_zero()) return;
+
+ android_log_event_list(EVENTLOGTAG_DISKSTATS)
+ << type << stats->start_time << stats->end_time
+ << stats->read_ios << stats->read_merges
+ << stats->read_sectors << stats->read_ticks
+ << stats->write_ios << stats->write_merges
+ << stats->write_sectors << stats->write_ticks
+ << (uint64_t)stats->io_avg << stats->io_ticks << stats->io_in_queue
+ << LOG_ID_EVENTS;
+}
+
+} // namespace
+
+bool get_time(struct timespec* ts) {
+ // Use monotonic to exclude suspend time so that we measure IO bytes/sec
+ // when system is running.
+ int ret = clock_gettime(CLOCK_MONOTONIC, ts);
+ if (ret < 0) {
+ PLOG_TO(SYSTEM, ERROR) << "clock_gettime() failed";
+ return false;
+ }
+ return true;
+}
+
+void init_disk_stats_other(const struct timespec& ts, struct disk_stats* stats) {
+ stats->start_time = 0;
+ stats->end_time = (uint64_t)ts.tv_sec * SEC_TO_MSEC + ts.tv_nsec / (MSEC_TO_USEC * USEC_TO_NSEC);
+ stats->counter = 1;
+ stats->io_avg = (double)stats->io_in_flight;
+}
+
+bool parse_disk_stats(const char* disk_stats_path, struct disk_stats* stats) {
+ // Get time
+ struct timespec ts;
+ if (!get_time(&ts)) {
+ return false;
+ }
+
+ std::string buffer;
+ if (!android::base::ReadFileToString(disk_stats_path, &buffer)) {
+ PLOG_TO(SYSTEM, ERROR) << disk_stats_path << ": ReadFileToString failed.";
+ return false;
+ }
+
+ // Regular diskstats entries
+ std::stringstream ss(buffer);
+ for (uint i = 0; i < DISK_STATS_SIZE; ++i) {
+ ss >> *((uint64_t*)stats + i);
+ }
+ // Other entries
+ init_disk_stats_other(ts, stats);
+ return true;
+}
+
+void convert_hal_disk_stats(struct disk_stats* dst, const DiskStats& src) {
+ dst->read_ios = src.reads;
+ dst->read_merges = src.readMerges;
+ dst->read_sectors = src.readSectors;
+ dst->read_ticks = src.readTicks;
+ dst->write_ios = src.writes;
+ dst->write_merges = src.writeMerges;
+ dst->write_sectors = src.writeSectors;
+ dst->write_ticks = src.writeTicks;
+ dst->io_in_flight = src.ioInFlight;
+ dst->io_ticks = src.ioTicks;
+ dst->io_in_queue = src.ioInQueue;
+}
+
+bool get_disk_stats_from_health_hal(const sp<IHealth>& service, struct disk_stats* stats) {
+ struct timespec ts;
+ if (!get_time(&ts)) {
+ return false;
+ }
+
+ bool success = false;
+ auto ret = service->getDiskStats([&success, stats](auto result, const auto& halStats) {
+ if (result != Result::SUCCESS || halStats.size() == 0) {
+ LOG_TO(SYSTEM, ERROR) << "getDiskStats failed with result " << toString(result)
+ << " and size " << halStats.size();
+ return;
+ }
+
+ convert_hal_disk_stats(stats, halStats[0]);
+ success = true;
+ });
+
+ if (!ret.isOk()) {
+ LOG_TO(SYSTEM, ERROR) << "getDiskStats failed with " << ret.description();
+ return false;
+ }
+
+ if (!success) {
+ return false;
+ }
+
+ init_disk_stats_other(ts, stats);
+ return true;
+}
+
+struct disk_perf get_disk_perf(struct disk_stats* stats)
+{
+ struct disk_perf perf = {};
+
+ if (stats->io_ticks) {
+ if (stats->read_ticks) {
+ unsigned long long divisor = stats->read_ticks * stats->io_ticks;
+ perf.read_perf = ((unsigned long long)SECTOR_SIZE *
+ stats->read_sectors * stats->io_in_queue +
+ (divisor >> 1)) / divisor;
+ perf.read_ios = ((unsigned long long)SEC_TO_MSEC *
+ stats->read_ios * stats->io_in_queue +
+ (divisor >> 1)) / divisor;
+ }
+ if (stats->write_ticks) {
+ unsigned long long divisor = stats->write_ticks * stats->io_ticks;
+ perf.write_perf = ((unsigned long long)SECTOR_SIZE *
+ stats->write_sectors * stats->io_in_queue +
+ (divisor >> 1)) / divisor;
+ perf.write_ios = ((unsigned long long)SEC_TO_MSEC *
+ stats->write_ios * stats->io_in_queue +
+ (divisor >> 1)) / divisor;
+ }
+ perf.queue = (stats->io_in_queue + (stats->io_ticks >> 1)) /
+ stats->io_ticks;
+ }
+ return perf;
+}
+
+void get_inc_disk_stats(const struct disk_stats* prev, const struct disk_stats* curr,
+ struct disk_stats* inc)
+{
+ *inc = *curr - *prev;
+ inc->start_time = prev->end_time;
+ inc->end_time = curr->end_time;
+ inc->io_avg = curr->io_avg;
+ inc->counter = 1;
+}
+
+// Add src to dst
+void add_disk_stats(struct disk_stats* src, struct disk_stats* dst)
+{
+ if (dst->end_time != 0 && dst->end_time != src->start_time) {
+ LOG_TO(SYSTEM, WARNING) << "Two dis-continuous periods of diskstats"
+ << " are added. dst end with " << dst->end_time
+ << ", src start with " << src->start_time;
+ }
+
+ *dst += *src;
+
+ dst->io_in_flight = src->io_in_flight;
+ if (dst->counter + src->counter) {
+ dst->io_avg =
+ ((dst->io_avg * dst->counter) + (src->io_avg * src->counter)) /
+ (dst->counter + src->counter);
+ }
+ dst->counter += src->counter;
+ dst->end_time = src->end_time;
+ if (dst->start_time == 0) {
+ dst->start_time = src->start_time;
+ }
+}
+
+/* disk_stats_monitor */
+void disk_stats_monitor::update_mean()
+{
+ CHECK(mValid);
+ mMean.read_perf = (uint32_t)mStats.read_perf.get_mean();
+ mMean.read_ios = (uint32_t)mStats.read_ios.get_mean();
+ mMean.write_perf = (uint32_t)mStats.write_perf.get_mean();
+ mMean.write_ios = (uint32_t)mStats.write_ios.get_mean();
+ mMean.queue = (uint32_t)mStats.queue.get_mean();
+}
+
+void disk_stats_monitor::update_std()
+{
+ CHECK(mValid);
+ mStd.read_perf = (uint32_t)mStats.read_perf.get_std();
+ mStd.read_ios = (uint32_t)mStats.read_ios.get_std();
+ mStd.write_perf = (uint32_t)mStats.write_perf.get_std();
+ mStd.write_ios = (uint32_t)mStats.write_ios.get_std();
+ mStd.queue = (uint32_t)mStats.queue.get_std();
+}
+
+void disk_stats_monitor::add(struct disk_perf* perf)
+{
+ mStats.read_perf.add(perf->read_perf);
+ mStats.read_ios.add(perf->read_ios);
+ mStats.write_perf.add(perf->write_perf);
+ mStats.write_ios.add(perf->write_ios);
+ mStats.queue.add(perf->queue);
+}
+
+void disk_stats_monitor::evict(struct disk_perf* perf) {
+ mStats.read_perf.evict(perf->read_perf);
+ mStats.read_ios.evict(perf->read_ios);
+ mStats.write_perf.evict(perf->write_perf);
+ mStats.write_ios.evict(perf->write_ios);
+ mStats.queue.evict(perf->queue);
+}
+
+bool disk_stats_monitor::detect(struct disk_perf* perf)
+{
+ return ((double)perf->queue >= (double)mMean.queue + mSigma * (double)mStd.queue) &&
+ ((double)perf->read_perf < (double)mMean.read_perf - mSigma * (double)mStd.read_perf) &&
+ ((double)perf->write_perf < (double)mMean.write_perf - mSigma * (double)mStd.write_perf);
+}
+
+void disk_stats_monitor::update(struct disk_stats* curr)
+{
+ disk_stats inc;
+ get_inc_disk_stats(&mPrevious, curr, &inc);
+ add_disk_stats(&inc, &mAccumulate_pub);
+
+ struct disk_perf perf = get_disk_perf(&inc);
+ log_debug_disk_perf(&perf, "regular");
+
+ add(&perf);
+ mBuffer.push(perf);
+ if (mBuffer.size() > mWindow) {
+ evict(&mBuffer.front());
+ mBuffer.pop();
+ mValid = true;
+ }
+
+ // Update internal data structures
+ if (LIKELY(mValid)) {
+ CHECK_EQ(mBuffer.size(), mWindow);
+ update_mean();
+ update_std();
+ if (UNLIKELY(detect(&perf))) {
+ mStall = true;
+ add_disk_stats(&inc, &mAccumulate);
+ log_debug_disk_perf(&mMean, "stalled_mean");
+ log_debug_disk_perf(&mStd, "stalled_std");
+ } else {
+ if (mStall) {
+ struct disk_perf acc_perf = get_disk_perf(&mAccumulate);
+ log_debug_disk_perf(&acc_perf, "stalled");
+ log_event_disk_stats(&mAccumulate, "stalled");
+ mStall = false;
+ memset(&mAccumulate, 0, sizeof(mAccumulate));
+ }
+ }
+ }
+
+ mPrevious = *curr;
+}
+
+void disk_stats_monitor::update() {
+ disk_stats curr;
+ if (mHealth != nullptr) {
+ if (!get_disk_stats_from_health_hal(mHealth, &curr)) {
+ return;
+ }
+ } else {
+ if (!parse_disk_stats(DISK_STATS_PATH, &curr)) {
+ return;
+ }
+ }
+
+ update(&curr);
+}
+
+void disk_stats_monitor::publish(void)
+{
+ struct disk_perf perf = get_disk_perf(&mAccumulate_pub);
+ log_debug_disk_perf(&perf, "regular");
+ log_event_disk_stats(&mAccumulate, "regular");
+ // Reset global structures
+ memset(&mAccumulate_pub, 0, sizeof(struct disk_stats));
+}
diff --git a/storaged/storaged_info.cpp b/storaged/storaged_info.cpp
index b5fb13e..5605f66 100644
--- a/storaged/storaged_info.cpp
+++ b/storaged/storaged_info.cpp
@@ -20,6 +20,8 @@
#include <string.h>
#include <sys/statvfs.h>
+#include <numeric>
+
#include <android-base/file.h>
#include <android-base/parseint.h>
#include <android-base/logging.h>
@@ -27,9 +29,16 @@
#include <log/log_event_list.h>
#include "storaged.h"
+#include "storaged_info.h"
using namespace std;
+using namespace chrono;
using namespace android::base;
+using namespace storaged_proto;
+
+using android::hardware::health::V2_0::IHealth;
+using android::hardware::health::V2_0::Result;
+using android::hardware::health::V2_0::StorageInfo;
const string emmc_info_t::emmc_sysfs = "/sys/bus/mmc/devices/mmc0:0001/";
const string emmc_info_t::emmc_debugfs = "/d/mmc0/mmc0:0001/ext_csd";
@@ -39,14 +48,20 @@
const string ufs_info_t::health_file = "/sys/devices/soc/624000.ufshc/health";
-static bool FileExists(const std::string& filename)
+namespace {
+
+bool FileExists(const std::string& filename)
{
struct stat buffer;
return stat(filename.c_str(), &buffer) == 0;
}
-storage_info_t* storage_info_t::get_storage_info()
-{
+} // namespace
+
+storage_info_t* storage_info_t::get_storage_info(const sp<IHealth>& healthService) {
+ if (healthService != nullptr) {
+ return new health_storage_info_t(healthService);
+ }
if (FileExists(emmc_info_t::emmc_sysfs) ||
FileExists(emmc_info_t::emmc_debugfs)) {
return new emmc_info_t;
@@ -57,7 +72,39 @@
return new storage_info_t;
}
-void storage_info_t::refresh()
+void storage_info_t::load_perf_history_proto(const IOPerfHistory& perf_history)
+{
+ Mutex::Autolock _l(si_mutex);
+
+ if (!perf_history.has_day_start_sec() ||
+ perf_history.daily_perf_size() > (int)daily_perf.size() ||
+ perf_history.weekly_perf_size() > (int)weekly_perf.size()) {
+ LOG_TO(SYSTEM, ERROR) << "Invalid IOPerfHistory proto";
+ return;
+ }
+
+ day_start_tp = {};
+ day_start_tp += chrono::seconds(perf_history.day_start_sec());
+
+ nr_samples = perf_history.nr_samples();
+ for (auto bw : perf_history.recent_perf()) {
+ recent_perf.push_back(bw);
+ }
+
+ nr_days = perf_history.nr_days();
+ int i = 0;
+ for (auto bw : perf_history.daily_perf()) {
+ daily_perf[i++] = bw;
+ }
+
+ nr_weeks = perf_history.nr_weeks();
+ i = 0;
+ for (auto bw : perf_history.weekly_perf()) {
+ weekly_perf[i++] = bw;
+ }
+}
+
+void storage_info_t::refresh(IOPerfHistory* perf_history)
{
struct statvfs buf;
if (statvfs(userdata_path.c_str(), &buf) != 0) {
@@ -67,6 +114,24 @@
userdata_total_kb = buf.f_bsize * buf.f_blocks >> 10;
userdata_free_kb = buf.f_bfree * buf.f_blocks >> 10;
+
+ Mutex::Autolock _l(si_mutex);
+
+ perf_history->Clear();
+ perf_history->set_day_start_sec(
+ duration_cast<chrono::seconds>(day_start_tp.time_since_epoch()).count());
+ for (const uint32_t& bw : recent_perf) {
+ perf_history->add_recent_perf(bw);
+ }
+ perf_history->set_nr_samples(nr_samples);
+ for (const uint32_t& bw : daily_perf) {
+ perf_history->add_daily_perf(bw);
+ }
+ perf_history->set_nr_days(nr_days);
+ for (const uint32_t& bw : weekly_perf) {
+ perf_history->add_weekly_perf(bw);
+ }
+ perf_history->set_nr_weeks(nr_weeks);
}
void storage_info_t::publish()
@@ -76,6 +141,95 @@
<< LOG_ID_EVENTS;
}
+void storage_info_t::update_perf_history(uint32_t bw,
+ const time_point<system_clock>& tp)
+{
+ Mutex::Autolock _l(si_mutex);
+
+ if (tp > day_start_tp &&
+ duration_cast<chrono::seconds>(tp - day_start_tp).count() < DAY_TO_SEC) {
+ if (nr_samples >= recent_perf.size()) {
+ recent_perf.push_back(bw);
+ } else {
+ recent_perf[nr_samples] = bw;
+ }
+ nr_samples++;
+ return;
+ }
+
+ if (nr_samples < recent_perf.size()) {
+ recent_perf.erase(recent_perf.begin() + nr_samples, recent_perf.end());
+ }
+
+ uint32_t daily_avg_bw = 0;
+ if (!recent_perf.empty()) {
+ daily_avg_bw = accumulate(recent_perf.begin(), recent_perf.end(), 0) / recent_perf.size();
+ }
+
+ day_start_tp = tp - chrono::seconds(duration_cast<chrono::seconds>(
+ tp.time_since_epoch()).count() % DAY_TO_SEC);
+
+ nr_samples = 0;
+ if (recent_perf.empty())
+ recent_perf.resize(1);
+ recent_perf[nr_samples++] = bw;
+
+ if (nr_days < WEEK_TO_DAYS) {
+ daily_perf[nr_days++] = daily_avg_bw;
+ return;
+ }
+
+ DCHECK(nr_days > 0);
+ uint32_t week_avg_bw = accumulate(daily_perf.begin(),
+ daily_perf.begin() + nr_days, 0) / nr_days;
+
+ nr_days = 0;
+ daily_perf[nr_days++] = daily_avg_bw;
+
+ if (nr_weeks >= YEAR_TO_WEEKS) {
+ nr_weeks = 0;
+ }
+ weekly_perf[nr_weeks++] = week_avg_bw;
+}
+
+vector<int> storage_info_t::get_perf_history()
+{
+ Mutex::Autolock _l(si_mutex);
+
+ vector<int> ret(3 + recent_perf.size() + daily_perf.size() + weekly_perf.size());
+
+ ret[0] = recent_perf.size();
+ ret[1] = daily_perf.size();
+ ret[2] = weekly_perf.size();
+
+ int start = 3;
+ for (size_t i = 0; i < recent_perf.size(); i++) {
+ int idx = (recent_perf.size() + nr_samples - 1 - i) % recent_perf.size();
+ ret[start + i] = recent_perf[idx];
+ }
+
+ start += recent_perf.size();
+ for (size_t i = 0; i < daily_perf.size(); i++) {
+ int idx = (daily_perf.size() + nr_days - 1 - i) % daily_perf.size();
+ ret[start + i] = daily_perf[idx];
+ }
+
+ start += daily_perf.size();
+ for (size_t i = 0; i < weekly_perf.size(); i++) {
+ int idx = (weekly_perf.size() + nr_weeks - 1 - i) % weekly_perf.size();
+ ret[start + i] = weekly_perf[idx];
+ }
+
+ return ret;
+}
+
+uint32_t storage_info_t::get_recent_perf() {
+ Mutex::Autolock _l(si_mutex);
+ if (recent_perf.size() == 0) return 0;
+ return accumulate(recent_perf.begin(), recent_perf.end(), recent_perf.size() / 2) /
+ recent_perf.size();
+}
+
void emmc_info_t::report()
{
if (!report_sysfs() && !report_debugfs())
@@ -121,6 +275,8 @@
return true;
}
+namespace {
+
const size_t EXT_CSD_FILE_MIN_SIZE = 1024;
/* 2 characters in string for each byte */
const size_t EXT_CSD_REV_IDX = 192 * 2;
@@ -128,6 +284,8 @@
const size_t EXT_DEVICE_LIFE_TIME_EST_A_IDX = 268 * 2;
const size_t EXT_DEVICE_LIFE_TIME_EST_B_IDX = 269 * 2;
+} // namespace
+
bool emmc_info_t::report_debugfs()
{
string buffer;
@@ -210,3 +368,25 @@
publish();
}
+void health_storage_info_t::report() {
+ auto ret = mHealth->getStorageInfo([this](auto result, const auto& halInfos) {
+ if (result != Result::SUCCESS || halInfos.size() == 0) {
+ LOG_TO(SYSTEM, DEBUG) << "getStorageInfo failed with result " << toString(result)
+ << " and size " << halInfos.size();
+ return;
+ }
+ set_values_from_hal_storage_info(halInfos[0]);
+ publish();
+ });
+
+ if (!ret.isOk()) {
+ LOG_TO(SYSTEM, DEBUG) << "getStorageInfo failed with " << ret.description();
+ }
+}
+
+void health_storage_info_t::set_values_from_hal_storage_info(const StorageInfo& halInfo) {
+ eol = halInfo.eol;
+ lifetime_a = halInfo.lifetimeA;
+ lifetime_b = halInfo.lifetimeB;
+ version = halInfo.version;
+}
diff --git a/storaged/storaged_service.cpp b/storaged/storaged_service.cpp
index b1d3bfd..17ea25b 100644
--- a/storaged/storaged_service.cpp
+++ b/storaged/storaged_service.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <inttypes.h>
#include <stdint.h>
#include <vector>
@@ -29,60 +30,69 @@
#include <private/android_filesystem_config.h>
#include <storaged.h>
+#include <storaged_utils.h>
#include <storaged_service.h>
+using namespace std;
using namespace android::base;
-extern sp<storaged_t> storaged;
+extern sp<storaged_t> storaged_sp;
-std::vector<struct uid_info> BpStoraged::dump_uids(const char* /*option*/) {
- Parcel data, reply;
- data.writeInterfaceToken(IStoraged::getInterfaceDescriptor());
-
- remote()->transact(DUMPUIDS, data, &reply);
-
- uint32_t res_size = reply.readInt32();
- std::vector<struct uid_info> res(res_size);
- for (auto&& uid : res) {
- uid.uid = reply.readInt32();
- uid.name = reply.readCString();
- reply.read(&uid.io, sizeof(uid.io));
- }
- return res;
+status_t StoragedService::start() {
+ return BinderService<StoragedService>::publish();
}
-IMPLEMENT_META_INTERFACE(Storaged, "Storaged");
-status_t BnStoraged::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
- switch(code) {
- case DUMPUIDS: {
- if (!data.checkInterface(this))
- return BAD_TYPE;
- std::vector<struct uid_info> res = dump_uids(NULL);
- reply->writeInt32(res.size());
- for (auto uid : res) {
- reply->writeInt32(uid.uid);
- reply->writeCString(uid.name.c_str());
- reply->write(&uid.io, sizeof(uid.io));
- }
- return NO_ERROR;
- }
- break;
- default:
- return BBinder::onTransact(code, data, reply, flags);
+void StoragedService::dumpUidRecords(int fd, const vector<uid_record>& entries) {
+ map<string, io_usage> merged_entries = merge_io_usage(entries);
+ for (const auto& rec : merged_entries) {
+ dprintf(fd, "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
+ " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
+ rec.first.c_str(),
+ rec.second.bytes[READ][FOREGROUND][CHARGER_OFF],
+ rec.second.bytes[WRITE][FOREGROUND][CHARGER_OFF],
+ rec.second.bytes[READ][BACKGROUND][CHARGER_OFF],
+ rec.second.bytes[WRITE][BACKGROUND][CHARGER_OFF],
+ rec.second.bytes[READ][FOREGROUND][CHARGER_ON],
+ rec.second.bytes[WRITE][FOREGROUND][CHARGER_ON],
+ rec.second.bytes[READ][BACKGROUND][CHARGER_ON],
+ rec.second.bytes[WRITE][BACKGROUND][CHARGER_ON]);
}
}
-std::vector<struct uid_info> Storaged::dump_uids(const char* /* option */) {
- std::vector<struct uid_info> uids_v;
- std::unordered_map<uint32_t, struct uid_info> uids_m = storaged->get_uids();
+void StoragedService::dumpUidRecordsDebug(int fd, const vector<uid_record>& entries) {
+ for (const auto& record : entries) {
+ const io_usage& uid_usage = record.ios.uid_ios;
+ dprintf(fd, "%s_%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
+ " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
+ record.name.c_str(), record.ios.user_id,
+ uid_usage.bytes[READ][FOREGROUND][CHARGER_OFF],
+ uid_usage.bytes[WRITE][FOREGROUND][CHARGER_OFF],
+ uid_usage.bytes[READ][BACKGROUND][CHARGER_OFF],
+ uid_usage.bytes[WRITE][BACKGROUND][CHARGER_OFF],
+ uid_usage.bytes[READ][FOREGROUND][CHARGER_ON],
+ uid_usage.bytes[WRITE][FOREGROUND][CHARGER_ON],
+ uid_usage.bytes[READ][BACKGROUND][CHARGER_ON],
+ uid_usage.bytes[WRITE][BACKGROUND][CHARGER_ON]);
- for (const auto& it : uids_m) {
- uids_v.push_back(it.second);
+ for (const auto& task_it : record.ios.task_ios) {
+ const io_usage& task_usage = task_it.second;
+ const string& comm = task_it.first;
+ dprintf(fd, "-> %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
+ " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
+ comm.c_str(),
+ task_usage.bytes[READ][FOREGROUND][CHARGER_OFF],
+ task_usage.bytes[WRITE][FOREGROUND][CHARGER_OFF],
+ task_usage.bytes[READ][BACKGROUND][CHARGER_OFF],
+ task_usage.bytes[WRITE][BACKGROUND][CHARGER_OFF],
+ task_usage.bytes[READ][FOREGROUND][CHARGER_ON],
+ task_usage.bytes[WRITE][FOREGROUND][CHARGER_ON],
+ task_usage.bytes[READ][BACKGROUND][CHARGER_ON],
+ task_usage.bytes[WRITE][BACKGROUND][CHARGER_ON]);
+ }
}
- return uids_v;
}
-status_t Storaged::dump(int fd, const Vector<String16>& args) {
+status_t StoragedService::dump(int fd, const Vector<String16>& args) {
IPCThreadState* self = IPCThreadState::self();
const int pid = self->getCallingPid();
const int uid = self->getCallingUid();
@@ -96,6 +106,7 @@
int time_window = 0;
uint64_t threshold = 0;
bool force_report = false;
+ bool debug = false;
for (size_t i = 0; i < args.size(); i++) {
const auto& arg = args[i];
if (arg == String16("--hours")) {
@@ -123,47 +134,87 @@
force_report = true;
continue;
}
+ if (arg == String16("--debug")) {
+ debug = true;
+ continue;
+ }
}
uint64_t last_ts = 0;
- const std::map<uint64_t, struct uid_records>& records =
- storaged->get_uid_records(hours, threshold, force_report);
+ map<uint64_t, struct uid_records> records =
+ storaged_sp->get_uid_records(hours, threshold, force_report);
for (const auto& it : records) {
if (last_ts != it.second.start_ts) {
- dprintf(fd, "%llu", (unsigned long long)it.second.start_ts);
+ dprintf(fd, "%" PRIu64, it.second.start_ts);
}
- dprintf(fd, ",%llu\n", (unsigned long long)it.first);
+ dprintf(fd, ",%" PRIu64 "\n", it.first);
last_ts = it.first;
- for (const auto& record : it.second.entries) {
- dprintf(fd, "%s %ju %ju %ju %ju %ju %ju %ju %ju\n",
- record.name.c_str(),
- record.ios.bytes[READ][FOREGROUND][CHARGER_OFF],
- record.ios.bytes[WRITE][FOREGROUND][CHARGER_OFF],
- record.ios.bytes[READ][BACKGROUND][CHARGER_OFF],
- record.ios.bytes[WRITE][BACKGROUND][CHARGER_OFF],
- record.ios.bytes[READ][FOREGROUND][CHARGER_ON],
- record.ios.bytes[WRITE][FOREGROUND][CHARGER_ON],
- record.ios.bytes[READ][BACKGROUND][CHARGER_ON],
- record.ios.bytes[WRITE][BACKGROUND][CHARGER_ON]);
+ if (!debug) {
+ dumpUidRecords(fd, it.second.entries);
+ } else {
+ dumpUidRecordsDebug(fd, it.second.entries);
}
}
if (time_window) {
- storaged->update_uid_io_interval(time_window);
+ storaged_sp->update_uid_io_interval(time_window);
}
return NO_ERROR;
}
-sp<IStoraged> get_storaged_service() {
+binder::Status StoragedService::onUserStarted(int32_t userId) {
+ storaged_sp->add_user_ce(userId);
+ return binder::Status::ok();
+}
+
+binder::Status StoragedService::onUserStopped(int32_t userId) {
+ storaged_sp->remove_user_ce(userId);
+ return binder::Status::ok();
+}
+
+binder::Status StoragedService::getRecentPerf(int32_t* _aidl_return) {
+ uint32_t recent_perf = storaged_sp->get_recent_perf();
+ if (recent_perf > INT32_MAX) {
+ *_aidl_return = INT32_MAX;
+ } else {
+ *_aidl_return = static_cast<int32_t>(recent_perf);
+ }
+ return binder::Status::ok();
+}
+
+status_t StoragedPrivateService::start() {
+ return BinderService<StoragedPrivateService>::publish();
+}
+
+binder::Status StoragedPrivateService::dumpUids(
+ vector<::android::os::storaged::UidInfo>* _aidl_return) {
+ unordered_map<uint32_t, uid_info> uids_m = storaged_sp->get_uids();
+
+ for (const auto& it : uids_m) {
+ UidInfo uinfo;
+ uinfo.uid = it.second.uid;
+ uinfo.name = it.second.name;
+ uinfo.tasks = it.second.tasks;
+ memcpy(&uinfo.io, &it.second.io, sizeof(uinfo.io));
+ _aidl_return->push_back(uinfo);
+ }
+ return binder::Status::ok();
+}
+
+binder::Status StoragedPrivateService::dumpPerfHistory(
+ vector<int32_t>* _aidl_return) {
+ *_aidl_return = storaged_sp->get_perf_history();
+ return binder::Status::ok();
+}
+
+sp<IStoragedPrivate> get_storaged_pri_service() {
sp<IServiceManager> sm = defaultServiceManager();
if (sm == NULL) return NULL;
- sp<IBinder> binder = sm->getService(String16("storaged"));
+ sp<IBinder> binder = sm->getService(String16("storaged_pri"));
if (binder == NULL) return NULL;
- sp<IStoraged> storaged = interface_cast<IStoraged>(binder);
-
- return storaged;
+ return interface_cast<IStoragedPrivate>(binder);
}
diff --git a/storaged/storaged_uid_monitor.cpp b/storaged/storaged_uid_monitor.cpp
index dd8bdd6..5745782 100644
--- a/storaged/storaged_uid_monitor.cpp
+++ b/storaged/storaged_uid_monitor.cpp
@@ -38,16 +38,87 @@
using namespace android;
using namespace android::base;
using namespace android::content::pm;
+using namespace android::os::storaged;
+using namespace storaged_proto;
-static bool refresh_uid_names;
+namespace {
-std::unordered_map<uint32_t, struct uid_info> uid_monitor::get_uid_io_stats()
+bool refresh_uid_names;
+const char* UID_IO_STATS_PATH = "/proc/uid_io/stats";
+
+} // namepsace
+
+std::unordered_map<uint32_t, uid_info> uid_monitor::get_uid_io_stats()
{
- std::unique_ptr<lock_t> lock(new lock_t(&um_lock));
+ Mutex::Autolock _l(uidm_mutex);
return get_uid_io_stats_locked();
};
-static void get_uid_names(const vector<int>& uids, const vector<std::string*>& uid_names)
+/* return true on parse success and false on failure */
+bool uid_info::parse_uid_io_stats(std::string&& s)
+{
+ std::vector<std::string> fields = Split(s, " ");
+ if (fields.size() < 11 ||
+ !ParseUint(fields[0], &uid) ||
+ !ParseUint(fields[1], &io[FOREGROUND].rchar) ||
+ !ParseUint(fields[2], &io[FOREGROUND].wchar) ||
+ !ParseUint(fields[3], &io[FOREGROUND].read_bytes) ||
+ !ParseUint(fields[4], &io[FOREGROUND].write_bytes) ||
+ !ParseUint(fields[5], &io[BACKGROUND].rchar) ||
+ !ParseUint(fields[6], &io[BACKGROUND].wchar) ||
+ !ParseUint(fields[7], &io[BACKGROUND].read_bytes) ||
+ !ParseUint(fields[8], &io[BACKGROUND].write_bytes) ||
+ !ParseUint(fields[9], &io[FOREGROUND].fsync) ||
+ !ParseUint(fields[10], &io[BACKGROUND].fsync)) {
+ LOG_TO(SYSTEM, WARNING) << "Invalid uid I/O stats: \""
+ << s << "\"";
+ return false;
+ }
+ return true;
+}
+
+/* return true on parse success and false on failure */
+bool task_info::parse_task_io_stats(std::string&& s)
+{
+ std::vector<std::string> fields = Split(s, ",");
+ size_t size = fields.size();
+ if (size < 13 ||
+ !ParseInt(fields[size - 11], &pid) ||
+ !ParseUint(fields[size - 10], &io[FOREGROUND].rchar) ||
+ !ParseUint(fields[size - 9], &io[FOREGROUND].wchar) ||
+ !ParseUint(fields[size - 8], &io[FOREGROUND].read_bytes) ||
+ !ParseUint(fields[size - 7], &io[FOREGROUND].write_bytes) ||
+ !ParseUint(fields[size - 6], &io[BACKGROUND].rchar) ||
+ !ParseUint(fields[size - 5], &io[BACKGROUND].wchar) ||
+ !ParseUint(fields[size - 4], &io[BACKGROUND].read_bytes) ||
+ !ParseUint(fields[size - 3], &io[BACKGROUND].write_bytes) ||
+ !ParseUint(fields[size - 2], &io[FOREGROUND].fsync) ||
+ !ParseUint(fields[size - 1], &io[BACKGROUND].fsync)) {
+ LOG_TO(SYSTEM, WARNING) << "Invalid task I/O stats: \""
+ << s << "\"";
+ return false;
+ }
+ comm = Join(std::vector<std::string>(
+ fields.begin() + 1, fields.end() - 11), ',');
+ return true;
+}
+
+bool io_usage::is_zero() const
+{
+ for (int i = 0; i < IO_TYPES; i++) {
+ for (int j = 0; j < UID_STATS; j++) {
+ for (int k = 0; k < CHARGER_STATS; k++) {
+ if (bytes[i][j][k])
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+namespace {
+
+void get_uid_names(const vector<int>& uids, const vector<std::string*>& uid_names)
{
sp<IServiceManager> sm = defaultServiceManager();
if (sm == NULL) {
@@ -79,17 +150,19 @@
refresh_uid_names = false;
}
-std::unordered_map<uint32_t, struct uid_info> uid_monitor::get_uid_io_stats_locked()
+} // namespace
+
+std::unordered_map<uint32_t, uid_info> uid_monitor::get_uid_io_stats_locked()
{
- std::unordered_map<uint32_t, struct uid_info> uid_io_stats;
+ std::unordered_map<uint32_t, uid_info> uid_io_stats;
std::string buffer;
if (!ReadFileToString(UID_IO_STATS_PATH, &buffer)) {
PLOG_TO(SYSTEM, ERROR) << UID_IO_STATS_PATH << ": ReadFileToString failed";
return uid_io_stats;
}
- std::vector<std::string> io_stats = Split(buffer, "\n");
- struct uid_info u;
+ std::vector<std::string> io_stats = Split(std::move(buffer), "\n");
+ uid_info u;
vector<int> uids;
vector<std::string*> uid_names;
@@ -97,32 +170,24 @@
if (io_stats[i].empty()) {
continue;
}
- std::vector<std::string> fields = Split(io_stats[i], " ");
- if (fields.size() < 11 ||
- !ParseUint(fields[0], &u.uid) ||
- !ParseUint(fields[1], &u.io[FOREGROUND].rchar) ||
- !ParseUint(fields[2], &u.io[FOREGROUND].wchar) ||
- !ParseUint(fields[3], &u.io[FOREGROUND].read_bytes) ||
- !ParseUint(fields[4], &u.io[FOREGROUND].write_bytes) ||
- !ParseUint(fields[5], &u.io[BACKGROUND].rchar) ||
- !ParseUint(fields[6], &u.io[BACKGROUND].wchar) ||
- !ParseUint(fields[7], &u.io[BACKGROUND].read_bytes) ||
- !ParseUint(fields[8], &u.io[BACKGROUND].write_bytes) ||
- !ParseUint(fields[9], &u.io[FOREGROUND].fsync) ||
- !ParseUint(fields[10], &u.io[BACKGROUND].fsync)) {
- LOG_TO(SYSTEM, WARNING) << "Invalid I/O stats: \""
- << io_stats[i] << "\"";
- continue;
- }
- uid_io_stats[u.uid] = u;
- uid_io_stats[u.uid].name = std::to_string(u.uid);
- uids.push_back(u.uid);
- uid_names.push_back(&uid_io_stats[u.uid].name);
- if (last_uid_io_stats.find(u.uid) == last_uid_io_stats.end()) {
- refresh_uid_names = true;
+ if (io_stats[i].compare(0, 4, "task")) {
+ if (!u.parse_uid_io_stats(std::move(io_stats[i])))
+ continue;
+ uid_io_stats[u.uid] = u;
+ uid_io_stats[u.uid].name = std::to_string(u.uid);
+ uids.push_back(u.uid);
+ uid_names.push_back(&uid_io_stats[u.uid].name);
+ if (last_uid_io_stats.find(u.uid) == last_uid_io_stats.end()) {
+ refresh_uid_names = true;
+ } else {
+ uid_io_stats[u.uid].name = last_uid_io_stats[u.uid].name;
+ }
} else {
- uid_io_stats[u.uid].name = last_uid_io_stats[u.uid].name;
+ task_info t;
+ if (!t.parse_task_io_stats(std::move(io_stats[i])))
+ continue;
+ uid_io_stats[u.uid].tasks[t.pid] = t;
}
}
@@ -133,34 +198,41 @@
return uid_io_stats;
}
-static const int MAX_UID_RECORDS_SIZE = 1000 * 48; // 1000 uids in 48 hours
+namespace {
-static inline int records_size(
- const std::map<uint64_t, struct uid_records>& curr_records)
+const int MAX_UID_RECORDS_SIZE = 1000 * 48; // 1000 uids in 48 hours
+
+inline size_t history_size(
+ const std::map<uint64_t, struct uid_records>& history)
{
- int count = 0;
- for (auto const& it : curr_records) {
+ size_t count = 0;
+ for (auto const& it : history) {
count += it.second.entries.size();
}
return count;
}
-static struct uid_io_usage zero_io_usage;
+} // namespace
void uid_monitor::add_records_locked(uint64_t curr_ts)
{
// remove records more than 5 days old
if (curr_ts > 5 * DAY_TO_SEC) {
- auto it = records.lower_bound(curr_ts - 5 * DAY_TO_SEC);
- records.erase(records.begin(), it);
+ auto it = io_history.lower_bound(curr_ts - 5 * DAY_TO_SEC);
+ io_history.erase(io_history.begin(), it);
}
struct uid_records new_records;
for (const auto& p : curr_io_stats) {
struct uid_record record = {};
record.name = p.first;
- record.ios = p.second;
- if (memcmp(&record.ios, &zero_io_usage, sizeof(struct uid_io_usage))) {
+ if (!p.second.uid_ios.is_zero()) {
+ record.ios.user_id = p.second.user_id;
+ record.ios.uid_ios = p.second.uid_ios;
+ for (const auto& p_task : p.second.task_ios) {
+ if (!p_task.second.is_zero())
+ record.ios.task_ios[p_task.first] = p_task.second;
+ }
new_records.entries.push_back(record);
}
}
@@ -173,25 +245,25 @@
return;
// make some room for new records
- int overflow = records_size(records) +
+ ssize_t overflow = history_size(io_history) +
new_records.entries.size() - MAX_UID_RECORDS_SIZE;
- while (overflow > 0 && records.size() > 0) {
- auto del_it = records.begin();
+ while (overflow > 0 && io_history.size() > 0) {
+ auto del_it = io_history.begin();
overflow -= del_it->second.entries.size();
- records.erase(records.begin());
+ io_history.erase(io_history.begin());
}
- records[curr_ts] = new_records;
+ io_history[curr_ts] = new_records;
}
std::map<uint64_t, struct uid_records> uid_monitor::dump(
double hours, uint64_t threshold, bool force_report)
{
if (force_report) {
- report();
+ report(nullptr);
}
- std::unique_ptr<lock_t> lock(new lock_t(&um_lock));
+ Mutex::Autolock _l(uidm_mutex);
std::map<uint64_t, struct uid_records> dump_records;
uint64_t first_ts = 0;
@@ -200,19 +272,20 @@
first_ts = time(NULL) - hours * HOUR_TO_SEC;
}
- for (auto it = records.lower_bound(first_ts); it != records.end(); ++it) {
+ for (auto it = io_history.lower_bound(first_ts); it != io_history.end(); ++it) {
const std::vector<struct uid_record>& recs = it->second.entries;
struct uid_records filtered;
for (const auto& rec : recs) {
- if (rec.ios.bytes[READ][FOREGROUND][CHARGER_ON] +
- rec.ios.bytes[READ][FOREGROUND][CHARGER_OFF] +
- rec.ios.bytes[READ][BACKGROUND][CHARGER_ON] +
- rec.ios.bytes[READ][BACKGROUND][CHARGER_OFF] +
- rec.ios.bytes[WRITE][FOREGROUND][CHARGER_ON] +
- rec.ios.bytes[WRITE][FOREGROUND][CHARGER_OFF] +
- rec.ios.bytes[WRITE][BACKGROUND][CHARGER_ON] +
- rec.ios.bytes[WRITE][BACKGROUND][CHARGER_OFF] > threshold) {
+ const io_usage& uid_usage = rec.ios.uid_ios;
+ if (uid_usage.bytes[READ][FOREGROUND][CHARGER_ON] +
+ uid_usage.bytes[READ][FOREGROUND][CHARGER_OFF] +
+ uid_usage.bytes[READ][BACKGROUND][CHARGER_ON] +
+ uid_usage.bytes[READ][BACKGROUND][CHARGER_OFF] +
+ uid_usage.bytes[WRITE][FOREGROUND][CHARGER_ON] +
+ uid_usage.bytes[WRITE][FOREGROUND][CHARGER_OFF] +
+ uid_usage.bytes[WRITE][BACKGROUND][CHARGER_ON] +
+ uid_usage.bytes[WRITE][BACKGROUND][CHARGER_OFF] > threshold) {
filtered.entries.push_back(rec);
}
}
@@ -230,20 +303,21 @@
void uid_monitor::update_curr_io_stats_locked()
{
- std::unordered_map<uint32_t, struct uid_info> uid_io_stats =
+ std::unordered_map<uint32_t, uid_info> uid_io_stats =
get_uid_io_stats_locked();
if (uid_io_stats.empty()) {
return;
}
for (const auto& it : uid_io_stats) {
- const struct uid_info& uid = it.second;
-
+ const uid_info& uid = it.second;
if (curr_io_stats.find(uid.name) == curr_io_stats.end()) {
- curr_io_stats[uid.name] = {};
+ curr_io_stats[uid.name] = {};
}
struct uid_io_usage& usage = curr_io_stats[uid.name];
+ usage.user_id = multiuser_get_user_id(uid.uid);
+
int64_t fg_rd_delta = uid.io[FOREGROUND].read_bytes -
last_uid_io_stats[uid.uid].io[FOREGROUND].read_bytes;
int64_t bg_rd_delta = uid.io[BACKGROUND].read_bytes -
@@ -253,30 +327,177 @@
int64_t bg_wr_delta = uid.io[BACKGROUND].write_bytes -
last_uid_io_stats[uid.uid].io[BACKGROUND].write_bytes;
- usage.bytes[READ][FOREGROUND][charger_stat] +=
+ usage.uid_ios.bytes[READ][FOREGROUND][charger_stat] +=
(fg_rd_delta < 0) ? 0 : fg_rd_delta;
- usage.bytes[READ][BACKGROUND][charger_stat] +=
+ usage.uid_ios.bytes[READ][BACKGROUND][charger_stat] +=
(bg_rd_delta < 0) ? 0 : bg_rd_delta;
- usage.bytes[WRITE][FOREGROUND][charger_stat] +=
+ usage.uid_ios.bytes[WRITE][FOREGROUND][charger_stat] +=
(fg_wr_delta < 0) ? 0 : fg_wr_delta;
- usage.bytes[WRITE][BACKGROUND][charger_stat] +=
+ usage.uid_ios.bytes[WRITE][BACKGROUND][charger_stat] +=
(bg_wr_delta < 0) ? 0 : bg_wr_delta;
+
+ for (const auto& task_it : uid.tasks) {
+ const task_info& task = task_it.second;
+ const pid_t pid = task_it.first;
+ const std::string& comm = task_it.second.comm;
+ int64_t task_fg_rd_delta = task.io[FOREGROUND].read_bytes -
+ last_uid_io_stats[uid.uid].tasks[pid].io[FOREGROUND].read_bytes;
+ int64_t task_bg_rd_delta = task.io[BACKGROUND].read_bytes -
+ last_uid_io_stats[uid.uid].tasks[pid].io[BACKGROUND].read_bytes;
+ int64_t task_fg_wr_delta = task.io[FOREGROUND].write_bytes -
+ last_uid_io_stats[uid.uid].tasks[pid].io[FOREGROUND].write_bytes;
+ int64_t task_bg_wr_delta = task.io[BACKGROUND].write_bytes -
+ last_uid_io_stats[uid.uid].tasks[pid].io[BACKGROUND].write_bytes;
+
+ io_usage& task_usage = usage.task_ios[comm];
+ task_usage.bytes[READ][FOREGROUND][charger_stat] +=
+ (task_fg_rd_delta < 0) ? 0 : task_fg_rd_delta;
+ task_usage.bytes[READ][BACKGROUND][charger_stat] +=
+ (task_bg_rd_delta < 0) ? 0 : task_bg_rd_delta;
+ task_usage.bytes[WRITE][FOREGROUND][charger_stat] +=
+ (task_fg_wr_delta < 0) ? 0 : task_fg_wr_delta;
+ task_usage.bytes[WRITE][BACKGROUND][charger_stat] +=
+ (task_bg_wr_delta < 0) ? 0 : task_bg_wr_delta;
+ }
}
last_uid_io_stats = uid_io_stats;
}
-void uid_monitor::report()
+void uid_monitor::report(unordered_map<int, StoragedProto>* protos)
{
- std::unique_ptr<lock_t> lock(new lock_t(&um_lock));
+ if (!enabled()) return;
+
+ Mutex::Autolock _l(uidm_mutex);
update_curr_io_stats_locked();
add_records_locked(time(NULL));
+
+ if (protos) {
+ update_uid_io_proto(protos);
+ }
+}
+
+namespace {
+
+void set_io_usage_proto(IOUsage* usage_proto, const io_usage& usage)
+{
+ usage_proto->set_rd_fg_chg_on(usage.bytes[READ][FOREGROUND][CHARGER_ON]);
+ usage_proto->set_rd_fg_chg_off(usage.bytes[READ][FOREGROUND][CHARGER_OFF]);
+ usage_proto->set_rd_bg_chg_on(usage.bytes[READ][BACKGROUND][CHARGER_ON]);
+ usage_proto->set_rd_bg_chg_off(usage.bytes[READ][BACKGROUND][CHARGER_OFF]);
+ usage_proto->set_wr_fg_chg_on(usage.bytes[WRITE][FOREGROUND][CHARGER_ON]);
+ usage_proto->set_wr_fg_chg_off(usage.bytes[WRITE][FOREGROUND][CHARGER_OFF]);
+ usage_proto->set_wr_bg_chg_on(usage.bytes[WRITE][BACKGROUND][CHARGER_ON]);
+ usage_proto->set_wr_bg_chg_off(usage.bytes[WRITE][BACKGROUND][CHARGER_OFF]);
+}
+
+void get_io_usage_proto(io_usage* usage, const IOUsage& io_proto)
+{
+ usage->bytes[READ][FOREGROUND][CHARGER_ON] = io_proto.rd_fg_chg_on();
+ usage->bytes[READ][FOREGROUND][CHARGER_OFF] = io_proto.rd_fg_chg_off();
+ usage->bytes[READ][BACKGROUND][CHARGER_ON] = io_proto.rd_bg_chg_on();
+ usage->bytes[READ][BACKGROUND][CHARGER_OFF] = io_proto.rd_bg_chg_off();
+ usage->bytes[WRITE][FOREGROUND][CHARGER_ON] = io_proto.wr_fg_chg_on();
+ usage->bytes[WRITE][FOREGROUND][CHARGER_OFF] = io_proto.wr_fg_chg_off();
+ usage->bytes[WRITE][BACKGROUND][CHARGER_ON] = io_proto.wr_bg_chg_on();
+ usage->bytes[WRITE][BACKGROUND][CHARGER_OFF] = io_proto.wr_bg_chg_off();
+}
+
+} // namespace
+
+void uid_monitor::update_uid_io_proto(unordered_map<int, StoragedProto>* protos)
+{
+ for (const auto& item : io_history) {
+ const uint64_t& end_ts = item.first;
+ const struct uid_records& recs = item.second;
+ unordered_map<userid_t, UidIOItem*> user_items;
+
+ for (const auto& entry : recs.entries) {
+ userid_t user_id = entry.ios.user_id;
+ UidIOItem* item_proto = user_items[user_id];
+ if (item_proto == nullptr) {
+ item_proto = (*protos)[user_id].mutable_uid_io_usage()
+ ->add_uid_io_items();
+ user_items[user_id] = item_proto;
+ }
+ item_proto->set_end_ts(end_ts);
+
+ UidIORecords* recs_proto = item_proto->mutable_records();
+ recs_proto->set_start_ts(recs.start_ts);
+
+ UidRecord* rec_proto = recs_proto->add_entries();
+ rec_proto->set_uid_name(entry.name);
+ rec_proto->set_user_id(user_id);
+
+ IOUsage* uid_io_proto = rec_proto->mutable_uid_io();
+ const io_usage& uio_ios = entry.ios.uid_ios;
+ set_io_usage_proto(uid_io_proto, uio_ios);
+
+ for (const auto& task_io : entry.ios.task_ios) {
+ const std::string& task_name = task_io.first;
+ const io_usage& task_ios = task_io.second;
+
+ TaskIOUsage* task_io_proto = rec_proto->add_task_io();
+ task_io_proto->set_task_name(task_name);
+ set_io_usage_proto(task_io_proto->mutable_ios(), task_ios);
+ }
+ }
+ }
+}
+
+void uid_monitor::clear_user_history(userid_t user_id)
+{
+ Mutex::Autolock _l(uidm_mutex);
+
+ for (auto& item : io_history) {
+ vector<uid_record>* entries = &item.second.entries;
+ entries->erase(
+ remove_if(entries->begin(), entries->end(),
+ [user_id](const uid_record& rec) {
+ return rec.ios.user_id == user_id;}),
+ entries->end());
+ }
+
+ for (auto it = io_history.begin(); it != io_history.end(); ) {
+ if (it->second.entries.empty()) {
+ it = io_history.erase(it);
+ } else {
+ it++;
+ }
+ }
+}
+
+void uid_monitor::load_uid_io_proto(const UidIOUsage& uid_io_proto)
+{
+ if (!enabled()) return;
+
+ Mutex::Autolock _l(uidm_mutex);
+
+ for (const auto& item_proto : uid_io_proto.uid_io_items()) {
+ const UidIORecords& records_proto = item_proto.records();
+ struct uid_records* recs = &io_history[item_proto.end_ts()];
+
+ recs->start_ts = records_proto.start_ts();
+ for (const auto& rec_proto : records_proto.entries()) {
+ struct uid_record record;
+ record.name = rec_proto.uid_name();
+ record.ios.user_id = rec_proto.user_id();
+ get_io_usage_proto(&record.ios.uid_ios, rec_proto.uid_io());
+
+ for (const auto& task_io_proto : rec_proto.task_io()) {
+ get_io_usage_proto(
+ &record.ios.task_ios[task_io_proto.task_name()],
+ task_io_proto.ios());
+ }
+ recs->entries.push_back(record);
+ }
+ }
}
void uid_monitor::set_charger_state(charger_stat_t stat)
{
- std::unique_ptr<lock_t> lock(new lock_t(&um_lock));
+ Mutex::Autolock _l(uidm_mutex);
if (charger_stat == stat) {
return;
@@ -289,16 +510,11 @@
void uid_monitor::init(charger_stat_t stat)
{
charger_stat = stat;
+
start_ts = time(NULL);
last_uid_io_stats = get_uid_io_stats();
}
uid_monitor::uid_monitor()
-{
- sem_init(&um_lock, 0, 1);
-}
-
-uid_monitor::~uid_monitor()
-{
- sem_destroy(&um_lock);
+ : enable(!access(UID_IO_STATS_PATH, R_OK)) {
}
diff --git a/storaged/storaged_utils.cpp b/storaged/storaged_utils.cpp
index 74b7436..4fd4bc9 100644
--- a/storaged/storaged_utils.cpp
+++ b/storaged/storaged_utils.cpp
@@ -18,6 +18,7 @@
#include <dirent.h>
#include <fcntl.h>
+#include <inttypes.h>
#include <linux/time.h>
#include <stdint.h>
#include <stdio.h>
@@ -41,124 +42,7 @@
#include <storaged.h>
#include <storaged_utils.h>
-bool parse_disk_stats(const char* disk_stats_path, struct disk_stats* stats) {
- // Get time
- struct timespec ts;
- // Use monotonic to exclude suspend time so that we measure IO bytes/sec
- // when system is running.
- int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
- if (ret < 0) {
- PLOG_TO(SYSTEM, ERROR) << "clock_gettime() failed";
- return false;
- }
-
- std::string buffer;
- if (!android::base::ReadFileToString(disk_stats_path, &buffer)) {
- PLOG_TO(SYSTEM, ERROR) << disk_stats_path << ": ReadFileToString failed.";
- return false;
- }
-
- // Regular diskstats entries
- std::stringstream ss(buffer);
- for (uint i = 0; i < DISK_STATS_SIZE; ++i) {
- ss >> *((uint64_t*)stats + i);
- }
- // Other entries
- stats->start_time = 0;
- stats->end_time = (uint64_t)ts.tv_sec * SEC_TO_MSEC +
- ts.tv_nsec / (MSEC_TO_USEC * USEC_TO_NSEC);
- stats->counter = 1;
- stats->io_avg = (double)stats->io_in_flight;
- return true;
-}
-
-struct disk_perf get_disk_perf(struct disk_stats* stats) {
- struct disk_perf perf;
- memset(&perf, 0, sizeof(struct disk_perf)); // initialize
-
- if (stats->io_ticks) {
- if (stats->read_ticks) {
- unsigned long long divisor = stats->read_ticks * stats->io_ticks;
- perf.read_perf = ((unsigned long long)SECTOR_SIZE *
- stats->read_sectors *
- stats->io_in_queue +
- (divisor >> 1)) /
- divisor;
- perf.read_ios = ((unsigned long long)SEC_TO_MSEC *
- stats->read_ios *
- stats->io_in_queue +
- (divisor >> 1)) /
- divisor;
- }
- if (stats->write_ticks) {
- unsigned long long divisor = stats->write_ticks * stats->io_ticks;
- perf.write_perf = ((unsigned long long)SECTOR_SIZE *
- stats->write_sectors *
- stats->io_in_queue +
- (divisor >> 1)) /
- divisor;
- perf.write_ios = ((unsigned long long)SEC_TO_MSEC *
- stats->write_ios *
- stats->io_in_queue +
- (divisor >> 1)) /
- divisor;
- }
- perf.queue = (stats->io_in_queue + (stats->io_ticks >> 1)) /
- stats->io_ticks;
- }
- return perf;
-}
-
-struct disk_stats get_inc_disk_stats(struct disk_stats* prev, struct disk_stats* curr) {
- struct disk_stats inc;
- for (uint i = 0; i < DISK_STATS_SIZE; ++i) {
- if (i == DISK_STATS_IO_IN_FLIGHT_IDX) {
- continue;
- }
-
- *((uint64_t*)&inc + i) =
- *((uint64_t*)curr + i) - *((uint64_t*)prev + i);
- }
- // io_in_flight is exception
- inc.io_in_flight = curr->io_in_flight;
-
- inc.start_time = prev->end_time;
- inc.end_time = curr->end_time;
- inc.io_avg = curr->io_avg;
- inc.counter = 1;
-
- return inc;
-}
-
-// Add src to dst
-void add_disk_stats(struct disk_stats* src, struct disk_stats* dst) {
- if (dst->end_time != 0 && dst->end_time != src->start_time) {
- LOG_TO(SYSTEM, WARNING) << "Two dis-continuous periods of diskstats"
- << " are added. dst end with " << dst->end_time
- << ", src start with " << src->start_time;
- }
-
- for (uint i = 0; i < DISK_STATS_SIZE; ++i) {
- if (i == DISK_STATS_IO_IN_FLIGHT_IDX) {
- continue;
- }
-
- *((uint64_t*)dst + i) += *((uint64_t*)src + i);
- }
-
- dst->io_in_flight = src->io_in_flight;
- if (dst->counter + src->counter) {
- dst->io_avg = ((dst->io_avg * dst->counter) + (src->io_avg * src->counter)) /
- (dst->counter + src->counter);
- }
- dst->counter += src->counter;
- dst->end_time = src->end_time;
- if (dst->start_time == 0) {
- dst->start_time = src->start_time;
- }
-}
-
-static bool cmp_uid_info(struct uid_info l, struct uid_info r) {
+bool cmp_uid_info(const UidInfo& l, const UidInfo& r) {
// Compare background I/O first.
for (int i = UID_STATS - 1; i >= 0; i--) {
uint64_t l_bytes = l.io[i].read_bytes + l.io[i].write_bytes;
@@ -177,56 +61,72 @@
return l.name < r.name;
}
-void sort_running_uids_info(std::vector<struct uid_info> &uids) {
+void sort_running_uids_info(std::vector<UidInfo> &uids) {
std::sort(uids.begin(), uids.end(), cmp_uid_info);
}
// Logging functions
-void log_console_running_uids_info(std::vector<struct uid_info> uids) {
+void log_console_running_uids_info(const std::vector<UidInfo>& uids, bool flag_dump_task) {
printf("name/uid fg_rchar fg_wchar fg_rbytes fg_wbytes "
"bg_rchar bg_wchar bg_rbytes bg_wbytes fg_fsync bg_fsync\n");
for (const auto& uid : uids) {
- printf("%s %ju %ju %ju %ju %ju %ju %ju %ju %ju %ju\n", uid.name.c_str(),
+ printf("%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
+ " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
+ uid.name.c_str(),
uid.io[0].rchar, uid.io[0].wchar, uid.io[0].read_bytes, uid.io[0].write_bytes,
uid.io[1].rchar, uid.io[1].wchar, uid.io[1].read_bytes, uid.io[1].write_bytes,
uid.io[0].fsync, uid.io[1].fsync);
+ if (flag_dump_task) {
+ for (const auto& task_it : uid.tasks) {
+ const task_info& task = task_it.second;
+ printf("-> %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
+ " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
+ task.comm.c_str(),
+ task.io[0].rchar, task.io[0].wchar, task.io[0].read_bytes, task.io[0].write_bytes,
+ task.io[1].rchar, task.io[1].wchar, task.io[1].read_bytes, task.io[1].write_bytes,
+ task.io[0].fsync, task.io[1].fsync);
+ }
+ }
}
fflush(stdout);
}
-#if DEBUG
-void log_debug_disk_perf(struct disk_perf* perf, const char* type) {
- // skip if the input structure are all zeros
- if (perf == NULL) return;
- struct disk_perf zero_cmp;
- memset(&zero_cmp, 0, sizeof(zero_cmp));
- if (memcmp(&zero_cmp, perf, sizeof(struct disk_perf)) == 0) return;
+void log_console_perf_history(const vector<int>& perf_history) {
+ if (perf_history.size() < 3 ||
+ perf_history.size() != perf_history[0] +
+ perf_history[1] +
+ perf_history[2] + (size_t)3) {
+ return;
+ }
- LOG_TO(SYSTEM, INFO) << "perf(ios) " << type
- << " rd:" << perf->read_perf << "KB/s(" << perf->read_ios << "/s)"
- << " wr:" << perf->write_perf << "KB/s(" << perf->write_ios << "/s)"
- << " q:" << perf->queue;
-}
-#else
-void log_debug_disk_perf(struct disk_perf* /* perf */, const char* /* type */) {}
-#endif
+ printf("\nI/O perf history (KB/s) : most_recent <--------- least_recent \n");
-void log_event_disk_stats(struct disk_stats* stats, const char* type) {
- // skip if the input structure are all zeros
- if (stats == NULL) return;
- struct disk_stats zero_cmp;
- memset(&zero_cmp, 0, sizeof(zero_cmp));
- // skip event logging diskstats when it is zero increment (all first 11 entries are zero)
- if (memcmp(&zero_cmp, stats, sizeof(uint64_t) * DISK_STATS_SIZE) == 0) return;
+ std::stringstream line;
+ int start = 3;
+ int end = 3 + perf_history[0];
+ std::copy(perf_history.begin() + start, perf_history.begin() + end,
+ std::ostream_iterator<int>(line, " "));
+ printf("last 24 hours : %s\n", line.str().c_str());
- android_log_event_list(EVENTLOGTAG_DISKSTATS)
- << type << stats->start_time << stats->end_time
- << stats->read_ios << stats->read_merges
- << stats->read_sectors << stats->read_ticks
- << stats->write_ios << stats->write_merges
- << stats->write_sectors << stats->write_ticks
- << (uint64_t)stats->io_avg << stats->io_ticks << stats->io_in_queue
- << LOG_ID_EVENTS;
+ line.str("");
+ start = end;
+ end += perf_history[1];
+ std::copy(perf_history.begin() + start, perf_history.begin() + end,
+ std::ostream_iterator<int>(line, " "));
+ printf("last 7 days : %s\n", line.str().c_str());
+
+ line.str("");
+ start = end;
+ std::copy(perf_history.begin() + start, perf_history.end(),
+ std::ostream_iterator<int>(line, " "));
+ printf("last 52 weeks : %s\n", line.str().c_str());
}
+map<string, io_usage> merge_io_usage(const vector<uid_record>& entries) {
+ map<string, io_usage> merged_entries;
+ for (const auto& record : entries) {
+ merged_entries[record.name] += record.ios.uid_ios;
+ }
+ return merged_entries;
+}
diff --git a/storaged/tests/Android.mk b/storaged/tests/Android.mk
deleted file mode 100644
index 26d04b1..0000000
--- a/storaged/tests/Android.mk
+++ /dev/null
@@ -1,45 +0,0 @@
-#
-# Copyright (C) 2014 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.
-#
-
-LOCAL_PATH := $(call my-dir)
-
-test_module_prefix := storaged-
-test_tags := tests
-
-# -----------------------------------------------------------------------------
-# Unit tests.
-# -----------------------------------------------------------------------------
-
-test_c_flags := \
- -fstack-protector-all \
- -g \
- -Wall -Wextra \
- -Werror \
- -fno-builtin \
-
-test_src_files := \
- storaged_test.cpp \
-
-# Build tests for the logger. Run with:
-# adb shell /data/nativetest/storaged-unit-tests/storaged-unit-tests
-include $(CLEAR_VARS)
-LOCAL_MODULE := $(test_module_prefix)unit-tests
-LOCAL_MODULE_TAGS := $(test_tags)
-LOCAL_CFLAGS += $(test_c_flags)
-LOCAL_STATIC_LIBRARIES := libstoraged
-LOCAL_SHARED_LIBRARIES := libbase libcutils liblog libpackagelistparser
-LOCAL_SRC_FILES := $(test_src_files)
-include $(BUILD_NATIVE_TEST)
diff --git a/storaged/tests/storaged_test.cpp b/storaged/tests/storaged_test.cpp
index b103ac1..ec47b65 100644
--- a/storaged/tests/storaged_test.cpp
+++ b/storaged/tests/storaged_test.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <chrono>
#include <deque>
#include <fcntl.h>
#include <random>
@@ -24,13 +25,20 @@
#include <gtest/gtest.h>
+#include <healthhalutils/HealthHalUtils.h>
#include <storaged.h> // data structures
#include <storaged_utils.h> // functions to test
#define MMC_DISK_STATS_PATH "/sys/block/mmcblk0/stat"
#define SDA_DISK_STATS_PATH "/sys/block/sda/stat"
-static void pause(uint32_t sec) {
+using namespace std;
+using namespace chrono;
+using namespace storaged_proto;
+
+namespace {
+
+void write_and_pause(uint32_t sec) {
const char* path = "/cache/test";
int fd = open(path, O_WRONLY | O_CREAT, 0600);
ASSERT_LT(-1, fd);
@@ -53,6 +61,8 @@
sleep(sec);
}
+} // namespace
+
// the return values of the tested functions should be the expected ones
const char* DISK_STATS_PATH;
TEST(storaged_test, retvals) {
@@ -77,13 +87,11 @@
EXPECT_FALSE(parse_disk_stats(wrong_path, &stats));
// reading a wrong path should not damage the output structure
- EXPECT_EQ(0, memcmp(&stats, &old_stats, sizeof(disk_stats)));
+ EXPECT_EQ(stats, old_stats);
}
TEST(storaged_test, disk_stats) {
- struct disk_stats stats;
- memset(&stats, 0, sizeof(struct disk_stats));
-
+ struct disk_stats stats = {};
ASSERT_TRUE(parse_disk_stats(DISK_STATS_PATH, &stats));
// every entry of stats (except io_in_flight) should all be greater than 0
@@ -93,11 +101,7 @@
}
// accumulation of the increments should be the same with the overall increment
- struct disk_stats base, tmp, curr, acc, inc[5];
- memset(&base, 0, sizeof(struct disk_stats));
- memset(&tmp, 0, sizeof(struct disk_stats));
- memset(&acc, 0, sizeof(struct disk_stats));
-
+ struct disk_stats base = {}, tmp = {}, curr, acc = {}, inc[5];
for (uint i = 0; i < 5; ++i) {
ASSERT_TRUE(parse_disk_stats(DISK_STATS_PATH, &curr));
if (i == 0) {
@@ -106,22 +110,18 @@
sleep(5);
continue;
}
- inc[i] = get_inc_disk_stats(&tmp, &curr);
+ get_inc_disk_stats(&tmp, &curr, &inc[i]);
add_disk_stats(&inc[i], &acc);
tmp = curr;
- pause(5);
+ write_and_pause(5);
}
- struct disk_stats overall_inc;
- memset(&overall_inc, 0, sizeof(disk_stats));
- overall_inc= get_inc_disk_stats(&base, &curr);
+ struct disk_stats overall_inc = {};
+ get_inc_disk_stats(&base, &curr, &overall_inc);
- for (uint i = 0; i < DISK_STATS_SIZE; ++i) {
- if (i == 8) continue; // skip io_in_flight which can be 0
- EXPECT_EQ(*((uint64_t*)&overall_inc + i), *((uint64_t*)&acc + i));
- }
+ EXPECT_EQ(overall_inc, acc);
}
-static double mean(std::deque<uint32_t> nums) {
+double mean(std::deque<uint32_t> nums) {
double sum = 0.0;
for (uint32_t i : nums) {
sum += i;
@@ -129,7 +129,7 @@
return sum / nums.size();
}
-static double standard_deviation(std::deque<uint32_t> nums) {
+double standard_deviation(std::deque<uint32_t> nums) {
double sum = 0.0;
double avg = mean(nums);
for (uint32_t i : nums) {
@@ -181,7 +181,7 @@
}
}
-static struct disk_perf disk_perf_multiply(struct disk_perf perf, double mul) {
+struct disk_perf disk_perf_multiply(struct disk_perf perf, double mul) {
struct disk_perf retval;
retval.read_perf = (double)perf.read_perf * mul;
retval.read_ios = (double)perf.read_ios * mul;
@@ -192,7 +192,7 @@
return retval;
}
-static struct disk_stats disk_stats_add(struct disk_stats stats1, struct disk_stats stats2) {
+struct disk_stats disk_stats_add(struct disk_stats stats1, struct disk_stats stats2) {
struct disk_stats retval;
retval.read_ios = stats1.read_ios + stats2.read_ios;
retval.read_merges = stats1.read_merges + stats2.read_merges;
@@ -210,11 +210,42 @@
return retval;
}
+void expect_increasing(struct disk_stats stats1, struct disk_stats stats2) {
+ EXPECT_LE(stats1.read_ios, stats2.read_ios);
+ EXPECT_LE(stats1.read_merges, stats2.read_merges);
+ EXPECT_LE(stats1.read_sectors, stats2.read_sectors);
+ EXPECT_LE(stats1.read_ticks, stats2.read_ticks);
+ EXPECT_LE(stats1.write_ios, stats2.write_ios);
+ EXPECT_LE(stats1.write_merges, stats2.write_merges);
+ EXPECT_LE(stats1.write_sectors, stats2.write_sectors);
+ EXPECT_LE(stats1.write_ticks, stats2.write_ticks);
+ EXPECT_LE(stats1.io_ticks, stats2.io_ticks);
+ EXPECT_LE(stats1.io_in_queue, stats2.io_in_queue);
+
+ EXPECT_TRUE(stats1.read_ios < stats2.read_ios ||
+ stats1.read_merges < stats2.read_merges ||
+ stats1.read_sectors < stats2.read_sectors ||
+ stats1.read_ticks < stats2.read_ticks ||
+ stats1.write_ios < stats2.write_ios ||
+ stats1.write_merges < stats2.write_merges ||
+ stats1.write_sectors < stats2.write_sectors ||
+ stats1.write_ticks < stats2.write_ticks ||
+ stats1.io_ticks < stats2.io_ticks ||
+ stats1.io_in_queue < stats2.io_in_queue);
+}
+
TEST(storaged_test, disk_stats_monitor) {
+ using android::hardware::health::V2_0::get_health_service;
+
+ auto healthService = get_health_service();
+
// asserting that there is one file for diskstats
- ASSERT_TRUE(access(MMC_DISK_STATS_PATH, R_OK) >= 0 || access(SDA_DISK_STATS_PATH, R_OK) >= 0);
+ ASSERT_TRUE(healthService != nullptr || access(MMC_DISK_STATS_PATH, R_OK) >= 0 ||
+ access(SDA_DISK_STATS_PATH, R_OK) >= 0);
+
// testing if detect() will return the right value
- disk_stats_monitor dsm_detect;
+ disk_stats_monitor dsm_detect{healthService};
+ ASSERT_TRUE(dsm_detect.enabled());
// feed monitor with constant perf data for io perf baseline
// using constant perf is reasonable since the functionality of stream_stats
// has already been tested
@@ -257,7 +288,7 @@
}
// testing if stalled disk_stats can be correctly accumulated in the monitor
- disk_stats_monitor dsm_acc;
+ disk_stats_monitor dsm_acc{healthService};
struct disk_stats norm_inc = {
.read_ios = 200,
.read_merges = 0,
@@ -294,14 +325,12 @@
.io_avg = 0
};
- struct disk_stats stats_base;
- memset(&stats_base, 0, sizeof(stats_base));
-
+ struct disk_stats stats_base = {};
int loop_size = 100;
for (int i = 0; i < loop_size; ++i) {
stats_base = disk_stats_add(stats_base, norm_inc);
dsm_acc.update(&stats_base);
- EXPECT_EQ(dsm_acc.mValid, (uint32_t)(i + 1) >= dsm_acc.mWindow);
+ EXPECT_EQ(dsm_acc.mValid, (uint32_t)i >= dsm_acc.mWindow);
EXPECT_FALSE(dsm_acc.mStall);
}
@@ -316,36 +345,284 @@
EXPECT_TRUE(dsm_acc.mValid);
EXPECT_FALSE(dsm_acc.mStall);
}
-}
-static void expect_increasing(struct disk_stats stats1, struct disk_stats stats2) {
- EXPECT_LE(stats1.read_ios, stats2.read_ios);
- EXPECT_LE(stats1.read_merges, stats2.read_merges);
- EXPECT_LE(stats1.read_sectors, stats2.read_sectors);
- EXPECT_LE(stats1.read_ticks, stats2.read_ticks);
-
- EXPECT_LE(stats1.write_ios, stats2.write_ios);
- EXPECT_LE(stats1.write_merges, stats2.write_merges);
- EXPECT_LE(stats1.write_sectors, stats2.write_sectors);
- EXPECT_LE(stats1.write_ticks, stats2.write_ticks);
-
- EXPECT_LE(stats1.io_ticks, stats2.io_ticks);
- EXPECT_LE(stats1.io_in_queue, stats2.io_in_queue);
-}
-
-#define TEST_LOOPS 20
-TEST(storaged_test, disk_stats_publisher) {
- // asserting that there is one file for diskstats
- ASSERT_TRUE(access(MMC_DISK_STATS_PATH, R_OK) >= 0 || access(SDA_DISK_STATS_PATH, R_OK) >= 0);
- disk_stats_publisher dsp;
- struct disk_stats prev;
- memset(&prev, 0, sizeof(prev));
-
- for (int i = 0; i < TEST_LOOPS; ++i) {
- dsp.update();
- expect_increasing(prev, dsp.mPrevious);
- prev = dsp.mPrevious;
- pause(10);
+ struct disk_stats stats_prev = {};
+ loop_size = 10;
+ write_and_pause(5);
+ for (int i = 0; i < loop_size; ++i) {
+ dsm_detect.update();
+ expect_increasing(stats_prev, dsm_detect.mPrevious);
+ stats_prev = dsm_detect.mPrevious;
+ write_and_pause(5);
}
}
+TEST(storaged_test, storage_info_t) {
+ storage_info_t si;
+ time_point<steady_clock> tp;
+ time_point<system_clock> stp;
+
+ // generate perf history [least_recent ------> most recent]
+ // day 1: 5, 10, 15, 20 | daily average 12
+ // day 2: 25, 30, 35, 40, 45 | daily average 35
+ // day 3: 50, 55, 60, 65, 70 | daily average 60
+ // day 4: 75, 80, 85, 90, 95 | daily average 85
+ // day 5: 100, 105, 110, 115, | daily average 107
+ // day 6: 120, 125, 130, 135, 140 | daily average 130
+ // day 7: 145, 150, 155, 160, 165 | daily average 155
+ // end of week 1: | weekly average 83
+ // day 1: 170, 175, 180, 185, 190 | daily average 180
+ // day 2: 195, 200, 205, 210, 215 | daily average 205
+ // day 3: 220, 225, 230, 235 | daily average 227
+ // day 4: 240, 245, 250, 255, 260 | daily average 250
+ // day 5: 265, 270, 275, 280, 285 | daily average 275
+ // day 6: 290, 295, 300, 305, 310 | daily average 300
+ // day 7: 315, 320, 325, 330, 335 | daily average 325
+ // end of week 2: | weekly average 251
+ // day 1: 340, 345, 350, 355 | daily average 347
+ // day 2: 360, 365, 370, 375
+ si.day_start_tp = {};
+ for (int i = 0; i < 75; i++) {
+ tp += hours(5);
+ stp = {};
+ stp += duration_cast<chrono::seconds>(tp.time_since_epoch());
+ si.update_perf_history((i + 1) * 5, stp);
+ }
+
+ vector<int> history = si.get_perf_history();
+ EXPECT_EQ(history.size(), 66UL);
+ size_t i = 0;
+ EXPECT_EQ(history[i++], 4);
+ EXPECT_EQ(history[i++], 7); // 7 days
+ EXPECT_EQ(history[i++], 52); // 52 weeks
+ // last 24 hours
+ EXPECT_EQ(history[i++], 375);
+ EXPECT_EQ(history[i++], 370);
+ EXPECT_EQ(history[i++], 365);
+ EXPECT_EQ(history[i++], 360);
+ // daily average of last 7 days
+ EXPECT_EQ(history[i++], 347);
+ EXPECT_EQ(history[i++], 325);
+ EXPECT_EQ(history[i++], 300);
+ EXPECT_EQ(history[i++], 275);
+ EXPECT_EQ(history[i++], 250);
+ EXPECT_EQ(history[i++], 227);
+ EXPECT_EQ(history[i++], 205);
+ // weekly average of last 52 weeks
+ EXPECT_EQ(history[i++], 251);
+ EXPECT_EQ(history[i++], 83);
+ for (; i < history.size(); i++) {
+ EXPECT_EQ(history[i], 0);
+ }
+}
+
+TEST(storaged_test, storage_info_t_proto) {
+ storage_info_t si;
+ si.day_start_tp = {};
+
+ IOPerfHistory proto;
+ proto.set_nr_samples(10);
+ proto.set_day_start_sec(0);
+ si.load_perf_history_proto(proto);
+
+ // Skip ahead > 1 day, with no data points in the previous day.
+ time_point<system_clock> stp;
+ stp += hours(36);
+ si.update_perf_history(100, stp);
+
+ vector<int> history = si.get_perf_history();
+ EXPECT_EQ(history.size(), 63UL);
+ EXPECT_EQ(history[0], 1);
+ EXPECT_EQ(history[1], 7);
+ EXPECT_EQ(history[2], 52);
+ EXPECT_EQ(history[3], 100);
+ for (size_t i = 4; i < history.size(); i++) {
+ EXPECT_EQ(history[i], 0);
+ }
+}
+
+TEST(storaged_test, uid_monitor) {
+ uid_monitor uidm;
+
+ uidm.io_history[200] = {
+ .start_ts = 100,
+ .entries = {
+ { "app1", {
+ .user_id = 0,
+ .uid_ios.bytes[WRITE][FOREGROUND][CHARGER_ON] = 1000,
+ }
+ },
+ { "app2", {
+ .user_id = 0,
+ .uid_ios.bytes[READ][FOREGROUND][CHARGER_OFF] = 1000,
+ }
+ },
+ { "app1", {
+ .user_id = 1,
+ .uid_ios.bytes[WRITE][FOREGROUND][CHARGER_ON] = 1000,
+ .uid_ios.bytes[READ][FOREGROUND][CHARGER_ON] = 1000,
+ }
+ },
+ },
+ };
+
+ uidm.io_history[300] = {
+ .start_ts = 200,
+ .entries = {
+ { "app1", {
+ .user_id = 1,
+ .uid_ios.bytes[WRITE][FOREGROUND][CHARGER_OFF] = 1000,
+ }
+ },
+ { "app3", {
+ .user_id = 0,
+ .uid_ios.bytes[READ][BACKGROUND][CHARGER_OFF] = 1000,
+ }
+ },
+ },
+ };
+
+ unordered_map<int, StoragedProto> protos;
+
+ uidm.update_uid_io_proto(&protos);
+
+ EXPECT_EQ(protos.size(), 2U);
+ EXPECT_EQ(protos.count(0), 1UL);
+ EXPECT_EQ(protos.count(1), 1UL);
+
+ EXPECT_EQ(protos[0].uid_io_usage().uid_io_items_size(), 2);
+ const UidIOItem& user_0_item_0 = protos[0].uid_io_usage().uid_io_items(0);
+ EXPECT_EQ(user_0_item_0.end_ts(), 200UL);
+ EXPECT_EQ(user_0_item_0.records().start_ts(), 100UL);
+ EXPECT_EQ(user_0_item_0.records().entries_size(), 2);
+ EXPECT_EQ(user_0_item_0.records().entries(0).uid_name(), "app1");
+ EXPECT_EQ(user_0_item_0.records().entries(0).user_id(), 0UL);
+ EXPECT_EQ(user_0_item_0.records().entries(0).uid_io().wr_fg_chg_on(), 1000UL);
+ EXPECT_EQ(user_0_item_0.records().entries(1).uid_name(), "app2");
+ EXPECT_EQ(user_0_item_0.records().entries(1).user_id(), 0UL);
+ EXPECT_EQ(user_0_item_0.records().entries(1).uid_io().rd_fg_chg_off(), 1000UL);
+ const UidIOItem& user_0_item_1 = protos[0].uid_io_usage().uid_io_items(1);
+ EXPECT_EQ(user_0_item_1.end_ts(), 300UL);
+ EXPECT_EQ(user_0_item_1.records().start_ts(), 200UL);
+ EXPECT_EQ(user_0_item_1.records().entries_size(), 1);
+ EXPECT_EQ(user_0_item_1.records().entries(0).uid_name(), "app3");
+ EXPECT_EQ(user_0_item_1.records().entries(0).user_id(), 0UL);
+ EXPECT_EQ(user_0_item_1.records().entries(0).uid_io().rd_bg_chg_off(), 1000UL);
+
+ EXPECT_EQ(protos[1].uid_io_usage().uid_io_items_size(), 2);
+ const UidIOItem& user_1_item_0 = protos[1].uid_io_usage().uid_io_items(0);
+ EXPECT_EQ(user_1_item_0.end_ts(), 200UL);
+ EXPECT_EQ(user_1_item_0.records().start_ts(), 100UL);
+ EXPECT_EQ(user_1_item_0.records().entries_size(), 1);
+ EXPECT_EQ(user_1_item_0.records().entries(0).uid_name(), "app1");
+ EXPECT_EQ(user_1_item_0.records().entries(0).user_id(), 1UL);
+ EXPECT_EQ(user_1_item_0.records().entries(0).uid_io().rd_fg_chg_on(), 1000UL);
+ EXPECT_EQ(user_1_item_0.records().entries(0).uid_io().wr_fg_chg_on(), 1000UL);
+ const UidIOItem& user_1_item_1 = protos[1].uid_io_usage().uid_io_items(1);
+ EXPECT_EQ(user_1_item_1.end_ts(), 300UL);
+ EXPECT_EQ(user_1_item_1.records().start_ts(), 200UL);
+ EXPECT_EQ(user_1_item_1.records().entries_size(), 1);
+ EXPECT_EQ(user_1_item_1.records().entries(0).uid_name(), "app1");
+ EXPECT_EQ(user_1_item_1.records().entries(0).user_id(), 1UL);
+ EXPECT_EQ(user_1_item_1.records().entries(0).uid_io().wr_fg_chg_off(), 1000UL);
+
+ uidm.io_history.clear();
+
+ uidm.io_history[300] = {
+ .start_ts = 200,
+ .entries = {
+ { "app1", {
+ .user_id = 0,
+ .uid_ios.bytes[WRITE][FOREGROUND][CHARGER_ON] = 1000,
+ }
+ },
+ },
+ };
+
+ uidm.io_history[400] = {
+ .start_ts = 300,
+ .entries = {
+ { "app1", {
+ .user_id = 0,
+ .uid_ios.bytes[WRITE][FOREGROUND][CHARGER_ON] = 1000,
+ }
+ },
+ },
+ };
+
+ uidm.load_uid_io_proto(protos[0].uid_io_usage());
+ uidm.load_uid_io_proto(protos[1].uid_io_usage());
+
+ EXPECT_EQ(uidm.io_history.size(), 3UL);
+ EXPECT_EQ(uidm.io_history.count(200), 1UL);
+ EXPECT_EQ(uidm.io_history.count(300), 1UL);
+ EXPECT_EQ(uidm.io_history.count(400), 1UL);
+
+ EXPECT_EQ(uidm.io_history[200].start_ts, 100UL);
+ const vector<struct uid_record>& entries_0 = uidm.io_history[200].entries;
+ EXPECT_EQ(entries_0.size(), 3UL);
+ EXPECT_EQ(entries_0[0].name, "app1");
+ EXPECT_EQ(entries_0[0].ios.user_id, 0UL);
+ EXPECT_EQ(entries_0[0].ios.uid_ios.bytes[WRITE][FOREGROUND][CHARGER_ON], 1000UL);
+ EXPECT_EQ(entries_0[1].name, "app2");
+ EXPECT_EQ(entries_0[1].ios.user_id, 0UL);
+ EXPECT_EQ(entries_0[1].ios.uid_ios.bytes[READ][FOREGROUND][CHARGER_OFF], 1000UL);
+ EXPECT_EQ(entries_0[2].name, "app1");
+ EXPECT_EQ(entries_0[2].ios.user_id, 1UL);
+ EXPECT_EQ(entries_0[2].ios.uid_ios.bytes[WRITE][FOREGROUND][CHARGER_ON], 1000UL);
+ EXPECT_EQ(entries_0[2].ios.uid_ios.bytes[READ][FOREGROUND][CHARGER_ON], 1000UL);
+
+ EXPECT_EQ(uidm.io_history[300].start_ts, 200UL);
+ const vector<struct uid_record>& entries_1 = uidm.io_history[300].entries;
+ EXPECT_EQ(entries_1.size(), 3UL);
+ EXPECT_EQ(entries_1[0].name, "app1");
+ EXPECT_EQ(entries_1[0].ios.user_id, 0UL);
+ EXPECT_EQ(entries_1[0].ios.uid_ios.bytes[WRITE][FOREGROUND][CHARGER_ON], 1000UL);
+ EXPECT_EQ(entries_1[1].name, "app3");
+ EXPECT_EQ(entries_1[1].ios.user_id, 0UL);
+ EXPECT_EQ(entries_1[1].ios.uid_ios.bytes[READ][BACKGROUND][CHARGER_OFF], 1000UL);
+ EXPECT_EQ(entries_1[2].name, "app1");
+ EXPECT_EQ(entries_1[2].ios.user_id, 1UL);
+ EXPECT_EQ(entries_1[2].ios.uid_ios.bytes[WRITE][FOREGROUND][CHARGER_OFF], 1000UL);
+
+ EXPECT_EQ(uidm.io_history[400].start_ts, 300UL);
+ const vector<struct uid_record>& entries_2 = uidm.io_history[400].entries;
+ EXPECT_EQ(entries_2.size(), 1UL);
+ EXPECT_EQ(entries_2[0].name, "app1");
+ EXPECT_EQ(entries_2[0].ios.user_id, 0UL);
+ EXPECT_EQ(entries_2[0].ios.uid_ios.bytes[WRITE][FOREGROUND][CHARGER_ON], 1000UL);
+
+ map<string, io_usage> merged_entries_0 = merge_io_usage(entries_0);
+ EXPECT_EQ(merged_entries_0.size(), 2UL);
+ EXPECT_EQ(merged_entries_0.count("app1"), 1UL);
+ EXPECT_EQ(merged_entries_0.count("app2"), 1UL);
+ EXPECT_EQ(merged_entries_0["app1"].bytes[READ][FOREGROUND][CHARGER_ON], 1000UL);
+ EXPECT_EQ(merged_entries_0["app1"].bytes[WRITE][FOREGROUND][CHARGER_ON], 2000UL);
+ EXPECT_EQ(merged_entries_0["app2"].bytes[READ][FOREGROUND][CHARGER_OFF], 1000UL);
+
+ map<string, io_usage> merged_entries_1 = merge_io_usage(entries_1);
+ EXPECT_EQ(merged_entries_1.size(), 2UL);
+ EXPECT_EQ(merged_entries_1.count("app1"), 1UL);
+ EXPECT_EQ(merged_entries_1.count("app3"), 1UL);
+ EXPECT_EQ(merged_entries_1["app1"].bytes[WRITE][FOREGROUND][CHARGER_OFF], 1000UL);
+ EXPECT_EQ(merged_entries_1["app1"].bytes[WRITE][FOREGROUND][CHARGER_ON], 1000UL);
+ EXPECT_EQ(merged_entries_1["app3"].bytes[READ][BACKGROUND][CHARGER_OFF], 1000UL);
+
+ map<string, io_usage> merged_entries_2 = merge_io_usage(entries_2);
+ EXPECT_EQ(merged_entries_2.size(), 1UL);
+ EXPECT_EQ(merged_entries_2.count("app1"), 1UL);
+ EXPECT_EQ(merged_entries_2["app1"].bytes[WRITE][FOREGROUND][CHARGER_ON], 1000UL);
+
+ uidm.clear_user_history(0);
+
+ EXPECT_EQ(uidm.io_history.size(), 2UL);
+ EXPECT_EQ(uidm.io_history.count(200), 1UL);
+ EXPECT_EQ(uidm.io_history.count(300), 1UL);
+
+ EXPECT_EQ(uidm.io_history[200].entries.size(), 1UL);
+ EXPECT_EQ(uidm.io_history[300].entries.size(), 1UL);
+
+ uidm.clear_user_history(1);
+
+ EXPECT_EQ(uidm.io_history.size(), 0UL);
+}
diff --git a/storaged/tools/ranker.py b/storaged/tools/ranker.py
new file mode 100644
index 0000000..d8096b7
--- /dev/null
+++ b/storaged/tools/ranker.py
@@ -0,0 +1,181 @@
+# Copyright 2017 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.
+
+"""Parser and ranker for dumpsys storaged output.
+
+This module parses output from dumpsys storaged by ranking uids based on
+their io usage measured in 8 different stats. It must be provided the input
+file through command line argument -i/--input.
+
+For more details, see:
+ $ python ranker.py -h
+
+Example:
+ $ python ranker.py -i io.txt -o output.txt -u 20 -cnt
+"""
+
+import argparse
+import sys
+
+IO_NAMES = ["[READ][FOREGROUND][CHARGER_OFF]",
+ "[WRITE][FOREGROUND][CHARGER_OFF]",
+ "[READ][BACKGROUND][CHARGER_OFF]",
+ "[WRITE][BACKGROUND][CHARGER_OFF]",
+ "[READ][FOREGROUND][CHARGER_ON]",
+ "[WRITE][FOREGROUND][CHARGER_ON]",
+ "[READ][BACKGROUND][CHARGER_ON]",
+ "[WRITE][BACKGROUND][CHARGER_ON]"]
+
+
+def get_args():
+ """Get arguments from command line.
+
+ The only required argument is input file.
+
+ Returns:
+ Args containing cmdline arguments
+ """
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument("-i", "--input", dest="input", required="true",
+ help="input io FILE, must provide", metavar="FILE")
+ parser.add_argument("-o", "--output", dest="output", default="stdout",
+ help="output FILE, default to stdout", metavar="FILE")
+ parser.add_argument("-u", "--uidcnt", dest="uidcnt", type=int, default=10,
+ help="set number of uids to display for each rank, "
+ "default 10")
+ parser.add_argument("-c", "--combine", dest="combine", default=False,
+ action="store_true", help="add io stats for same uids, "
+ "default to take io stats of last appearing uids")
+ parser.add_argument("-n", "--native", dest="native", default=False,
+ action="store_true", help="only include native apps in "
+ "ranking, default to include all apps")
+ parser.add_argument("-t", "--task", dest="task", default=False,
+ action="store_true", help="display task io under uids, "
+ "default to not display tasks")
+ return parser.parse_args()
+
+
+def is_number(word):
+ try:
+ int(word)
+ return True
+ except ValueError:
+ return False
+
+
+def combine_or_filter(args):
+ """Parser for io input.
+
+ Either args.combine io stats for the same uids
+ or take the io stats for the last uid and ignore
+ the same uids before it.
+
+ If task is required, store task ios along with uid
+ for later display.
+
+ Returns:
+ The structure for the return value uids is as follows:
+ uids: {uid -> [UID_STATS, TASK_STATS(optional)]}
+ UID_STATS: [io1, io2, ..., io8]
+ TASK_STATS: {task_name -> [io1, io2, ..., io8]}
+ """
+ fin = open(args.input, "r")
+ uids = {}
+ cur_uid = 0
+ task_enabled = args.task
+ for line in fin:
+ words = line.split()
+ if words[0] == "->":
+ # task io
+ if not task_enabled:
+ continue
+ # get task command line
+ i = len(words) - 8
+ task = " ".join(words[1:i])
+ if task in uids[cur_uid][1]:
+ task_io = uids[cur_uid][1][task]
+ for j in range(8):
+ task_io[j] += long(words[i+j])
+ else:
+ task_io = []
+ for j in range(8):
+ task_io.append(long(words[i+j]))
+ uids[cur_uid][1][task] = task_io
+
+ elif len(words) > 8:
+ if not is_number(words[0]) and args.native:
+ # uid not requested, ignore its tasks as well
+ task_enabled = False
+ continue
+ task_enabled = args.task
+ i = len(words) - 8
+ uid = " ".join(words[:i])
+ if uid in uids and args.combine:
+ uid_io = uids[uid][0]
+ for j in range(8):
+ uid_io[j] += long(words[i+j])
+ uids[uid][0] = uid_io
+ else:
+ uid_io = [long(words[i+j]) for j in range(8)]
+ uids[uid] = [uid_io]
+ if task_enabled:
+ uids[uid].append({})
+ cur_uid = uid
+
+ return uids
+
+
+def rank_uids(uids):
+ """Sort uids based on eight different io stats.
+
+ Returns:
+ uid_rank is a 2d list of tuples:
+ The first dimension represent the 8 different io stats.
+ The second dimension is a sorted list of tuples by tup[0],
+ each tuple is a uid's perticular stat at the first dimension and the uid.
+ """
+ uid_rank = [[(uids[uid][0][i], uid) for uid in uids] for i in range(8)]
+ for i in range(8):
+ uid_rank[i].sort(key=lambda tup: tup[0], reverse=True)
+ return uid_rank
+
+
+def display_uids(uid_rank, uids, args):
+ """Display ranked uid io, along with task io if specified."""
+ fout = sys.stdout
+ if args.output != "stdout":
+ fout = open(args.output, "w")
+
+ for i in range(8):
+ fout.write("RANKING BY " + IO_NAMES[i] + "\n")
+ for j in range(min(args.uidcnt, len(uid_rank[0]))):
+ uid = uid_rank[i][j][1]
+ uid_stat = " ".join([str(uid_io) for uid_io in uids[uid][0]])
+ fout.write(uid + " " + uid_stat + "\n")
+ if args.task:
+ for task in uids[uid][1]:
+ task_stat = " ".join([str(task_io) for task_io in uids[uid][1][task]])
+ fout.write("-> " + task + " " + task_stat + "\n")
+ fout.write("\n")
+
+
+def main():
+ args = get_args()
+ uids = combine_or_filter(args)
+ uid_rank = rank_uids(uids)
+ display_uids(uid_rank, uids, args)
+
+if __name__ == "__main__":
+ main()
diff --git a/storaged/uid_info.cpp b/storaged/uid_info.cpp
new file mode 100644
index 0000000..58e3fd2
--- /dev/null
+++ b/storaged/uid_info.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2017 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 <binder/Parcel.h>
+
+#include "uid_info.h"
+
+using namespace android;
+using namespace android::os::storaged;
+
+status_t UidInfo::writeToParcel(Parcel* parcel) const {
+ parcel->writeInt32(uid);
+ parcel->writeCString(name.c_str());
+ parcel->write(&io, sizeof(io));
+
+ parcel->writeInt32(tasks.size());
+ for (const auto& task_it : tasks) {
+ parcel->writeInt32(task_it.first);
+ parcel->writeCString(task_it.second.comm.c_str());
+ parcel->write(&task_it.second.io, sizeof(task_it.second.io));
+ }
+ return NO_ERROR;
+}
+
+status_t UidInfo::readFromParcel(const Parcel* parcel) {
+ uid = parcel->readInt32();
+ name = parcel->readCString();
+ parcel->read(&io, sizeof(io));
+
+ uint32_t tasks_size = parcel->readInt32();
+ for (uint32_t i = 0; i < tasks_size; i++) {
+ task_info task;
+ task.pid = parcel->readInt32();
+ task.comm = parcel->readCString();
+ parcel->read(&task.io, sizeof(task.io));
+ tasks[task.pid] = task;
+ }
+ return NO_ERROR;
+}
diff --git a/toolbox/Android.bp b/toolbox/Android.bp
index ddd95b2..f08cf93 100644
--- a/toolbox/Android.bp
+++ b/toolbox/Android.bp
@@ -5,32 +5,8 @@
"-Werror",
"-Wno-unused-parameter",
"-Wno-unused-const-variable",
- "-include bsd-compatibility.h",
"-D_FILE_OFFSET_BITS=64",
- ],
- local_include_dirs: ["upstream-netbsd/include/"],
-}
-
-cc_library_static {
- name: "libtoolbox_dd",
- defaults: ["toolbox_defaults"],
- vendor_available: true,
- srcs: [
- "upstream-netbsd/bin/dd/args.c",
- "upstream-netbsd/bin/dd/conv.c",
- "upstream-netbsd/bin/dd/dd.c",
- "upstream-netbsd/bin/dd/dd_hostops.c",
- "upstream-netbsd/bin/dd/misc.c",
- "upstream-netbsd/bin/dd/position.c",
- "upstream-netbsd/lib/libc/gen/getbsize.c",
- "upstream-netbsd/lib/libc/gen/humanize_number.c",
- "upstream-netbsd/lib/libc/stdlib/strsuftoll.c",
- "upstream-netbsd/lib/libc/string/swab.c",
- "upstream-netbsd/lib/libutil/raise_default_signal.c",
- ],
- cflags: [
- "-Dmain=dd_main",
- "-DNO_CONV",
+ "-DWITHOUT_NLS",
],
}
@@ -50,29 +26,25 @@
"toolbox.c",
"getevent.c",
"getprop.cpp",
- "newfs_msdos.c",
],
generated_headers: [
"toolbox_input_labels",
],
- whole_static_libs: ["libtoolbox_dd"],
shared_libs: [
"libbase",
- "libcutils",
],
static_libs: ["libpropertyinfoparser"],
symlinks: [
- "dd",
"getevent",
"getprop",
- "newfs_msdos",
],
}
cc_binary {
name: "toolbox",
defaults: ["toolbox_binary_defaults"],
+ recovery_available: true,
}
cc_binary {
@@ -89,7 +61,7 @@
srcs: ["r.c"],
}
-// We build BSD grep separately, so it can provide egrep and fgrep too.
+// We build BSD grep separately (but see http://b/111849261).
cc_defaults {
name: "grep_common",
defaults: ["toolbox_defaults"],
@@ -104,7 +76,6 @@
"egrep",
"fgrep",
],
-
sanitize: {
integer_overflow: false,
},
@@ -113,6 +84,7 @@
cc_binary {
name: "grep",
defaults: ["grep_common"],
+ recovery_available: true,
}
// Build vendor grep.
diff --git a/toolbox/MODULE_LICENSE_BSD b/toolbox/MODULE_LICENSE_APACHE2
similarity index 100%
rename from toolbox/MODULE_LICENSE_BSD
rename to toolbox/MODULE_LICENSE_APACHE2
diff --git a/toolbox/NOTICE b/toolbox/NOTICE
index e9ab58d..8e8a91c 100644
--- a/toolbox/NOTICE
+++ b/toolbox/NOTICE
@@ -1,4 +1,4 @@
-Copyright (C) 2010 The Android Open Source Project
+Copyright (C) 2017 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.
@@ -14,974 +14,3 @@
-------------------------------------------------------------------
-Copyright (C) 2014, The Android Open Source Project
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
-OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1987, 1993
- The Regents of the University of California. All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1987, 1993, 1994
- The Regents of the University of California. All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1988, 1993
- The Regents of the University of California. All rights reserved.
-
-This code is derived from software contributed to Berkeley by
-Jeffrey Mogul.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1988, 1993, 1994
- The Regents of the University of California. All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1988, 1993, 1994
- The Regents of the University of California. All rights reserved.
-
-This code is derived from software contributed to Berkeley by
-David Hitz of Auspex Systems Inc.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1988, 1993, 1994, 2003
- The Regents of the University of California. All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1989, 1993
- The Regents of the University of California. All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1989, 1993
- The Regents of the University of California. All rights reserved.
-
-This code is derived from software contributed to Berkeley by
-Kevin Fall.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1989, 1993, 1994
- The Regents of the University of California. All rights reserved.
-
-This code is derived from software contributed to Berkeley by
-Chris Newcomb.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1989, 1993, 1994
- The Regents of the University of California. All rights reserved.
-
-This code is derived from software contributed to Berkeley by
-Ken Smith of The State University of New York at Buffalo.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1990, 1993, 1994, 2003
- The Regents of the University of California. All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1991, 1993
- The Regents of the University of California. All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1991, 1993, 1994
- The Regents of the University of California. All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1991, 1993, 1994
- The Regents of the University of California. All rights reserved.
-
-This code is derived from software contributed to Berkeley by
-Keith Muller of the University of California, San Diego and Lance
-Visser of Convex Computer Corporation.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1992, 1993, 1994
- The Regents of the University of California. All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the University nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
-All rights reserved.
-
-This code is derived from software contributed to The NetBSD Foundation
-by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
-NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
-``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
-BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1998 Robert Nordier
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
-OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
-DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
-GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
-IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
-IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
-Copyright (C) 2008 Gabor Kovesdan <gabor@FreeBSD.org>
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
-Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
-Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
-Copyright (C) 2008-2010 Gabor Kovesdan <gabor@FreeBSD.org>
-Copyright (C) 2010 Dimitry Andric <dimitry@andric.com>
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
-Copyright (c) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 2001-2002,2004 The NetBSD Foundation, Inc.
-All rights reserved.
-
-This code is derived from software contributed to The NetBSD Foundation
-by Luke Mewburn.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
-``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
-BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 2007 The NetBSD Foundation, Inc.
-All rights reserved.
-
-This code is derived from software contributed to The NetBSD Foundation
-by Luke Mewburn.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
-``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
-BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 2008, The Android Open Source Project
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
- * Neither the name of Google, Inc. nor the names of its contributors
- may be used to endorse or promote products derived from this
- software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
-OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 2009, The Android Open Source Project
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
- * Neither the name of Google, Inc. nor the names of its contributors
- may be used to endorse or promote products derived from this
- software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
-OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 2010 The NetBSD Foundation, Inc.
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
-``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
-BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 2010, The Android Open Source Project
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
- * Neither the name of Google, Inc. nor the names of its contributors
- may be used to endorse or promote products derived from this
- software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
-OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 2012, The Android Open Source Project
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
- * Neither the name of Google, Inc. nor the names of its contributors
- may be used to endorse or promote products derived from this
- software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
-OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 2013, The Android Open Source Project
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
- * Neither the name of Google, Inc. nor the names of its contributors
- may be used to endorse or promote products derived from this
- software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
-OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
-Copyright (c) 2014, The Android Open Source Project
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
- * Neither the name of Google, Inc. nor the names of its contributors
- may be used to endorse or promote products derived from this
- software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
-OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGE.
-
--------------------------------------------------------------------
-
diff --git a/toolbox/bsd-compatibility.h b/toolbox/bsd-compatibility.h
deleted file mode 100644
index 7c3ddd4..0000000
--- a/toolbox/bsd-compatibility.h
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Copyright (C) 2014, The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <stdbool.h>
-#include <sys/types.h>
-
-/* We want chown to support user.group as well as user:group. */
-#define SUPPORT_DOT
-
-/* We don't localize /system/bin! */
-#define WITHOUT_NLS
-
-// NetBSD uses _DIAGASSERT to null-check arguments and the like.
-#include <assert.h>
-#define _DIAGASSERT(e) ((e) ? (void) 0 : __assert2(__FILE__, __LINE__, __func__, #e))
-
-// TODO: update our <sys/cdefs.h> to support this properly.
-#define __type_fit(t, a) (0 == 0)
-
-// TODO: should this be in our <sys/cdefs.h>?
-#define __arraycount(a) (sizeof(a) / sizeof((a)[0]))
-
-// This at least matches GNU dd(1) behavior.
-#define SIGINFO SIGUSR1
-
-#define S_ISWHT(x) false
-
-__BEGIN_DECLS
-
-/* From NetBSD <stdlib.h>. */
-#define HN_DECIMAL 0x01
-#define HN_NOSPACE 0x02
-#define HN_B 0x04
-#define HN_DIVISOR_1000 0x08
-#define HN_GETSCALE 0x10
-#define HN_AUTOSCALE 0x20
-int humanize_number(char *, size_t, int64_t, const char *, int, int);
-int dehumanize_number(const char *, int64_t *);
-char *getbsize(int *, long *);
-long long strsuftoll(const char *, const char *, long long, long long);
-long long strsuftollx(const char *, const char *, long long, long long,
- char *, size_t);
-
-/* From NetBSD <string.h>. */
-void strmode(mode_t, char*);
-
-/* From NetBSD <sys/param.h>. */
-#define MAXBSIZE 65536
-
-/* From NetBSD <sys/stat.h>. */
-#define DEFFILEMODE (S_IRUSR | S_IWUSR)
-
-/* From NetBSD <unistd.h>. */
-void swab(const void * __restrict, void * __restrict, ssize_t);
-
-/* From NetBSD <util.h>. */
-int raise_default_signal(int);
-
-__END_DECLS
diff --git a/toolbox/getprop.cpp b/toolbox/getprop.cpp
index 9e324a0..ca345cb 100644
--- a/toolbox/getprop.cpp
+++ b/toolbox/getprop.cpp
@@ -1,18 +1,18 @@
-//
-// Copyright (C) 2017 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.
-//
+/*
+ * Copyright (C) 2017 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 <getopt.h>
#include <sys/system_properties.h>
diff --git a/toolbox/newfs_msdos.c b/toolbox/newfs_msdos.c
deleted file mode 100644
index d7047e2..0000000
--- a/toolbox/newfs_msdos.c
+++ /dev/null
@@ -1,1099 +0,0 @@
-/*
- * Copyright (c) 1998 Robert Nordier
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
- * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef lint
-static const char rcsid[] =
- "$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.33 2009/04/11 14:56:29 ed Exp $";
-#endif /* not lint */
-
-#include <sys/param.h>
-
-#ifndef ANDROID
-#include <sys/fdcio.h>
-#include <sys/disk.h>
-#include <sys/disklabel.h>
-#include <sys/mount.h>
-#else
-#include <stdarg.h>
-#include <linux/fs.h>
-#include <linux/hdreg.h>
-#endif
-
-#include <sys/stat.h>
-#include <sys/time.h>
-
-#include <ctype.h>
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <inttypes.h>
-#include <paths.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-
-#define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
-#define BPN 4 /* bits per nibble */
-#define NPB 2 /* nibbles per byte */
-
-#define DOSMAGIC 0xaa55 /* DOS magic number */
-#define MINBPS 512 /* minimum bytes per sector */
-#define MAXSPC 128 /* maximum sectors per cluster */
-#define MAXNFT 16 /* maximum number of FATs */
-#define DEFBLK 4096 /* default block size */
-#define DEFBLK16 2048 /* default block size FAT16 */
-#define DEFRDE 512 /* default root directory entries */
-#define RESFTE 2 /* reserved FAT entries */
-#define MINCLS12 1 /* minimum FAT12 clusters */
-#define MINCLS16 0x1000 /* minimum FAT16 clusters */
-#define MINCLS32 2 /* minimum FAT32 clusters */
-#define MAXCLS12 0xfed /* maximum FAT12 clusters */
-#define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
-#define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
-
-#define mincls(fat) ((fat) == 12 ? MINCLS12 : \
- (fat) == 16 ? MINCLS16 : \
- MINCLS32)
-
-#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
- (fat) == 16 ? MAXCLS16 : \
- MAXCLS32)
-
-#define mk1(p, x) \
- (p) = (u_int8_t)(x)
-
-#define mk2(p, x) \
- (p)[0] = (u_int8_t)(x), \
- (p)[1] = (u_int8_t)((x) >> 010)
-
-#define mk4(p, x) \
- (p)[0] = (u_int8_t)(x), \
- (p)[1] = (u_int8_t)((x) >> 010), \
- (p)[2] = (u_int8_t)((x) >> 020), \
- (p)[3] = (u_int8_t)((x) >> 030)
-
-#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
-#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
-#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
-#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
-
-struct bs {
- u_int8_t jmp[3]; /* bootstrap entry point */
- u_int8_t oem[8]; /* OEM name and version */
-};
-
-struct bsbpb {
- u_int8_t bps[2]; /* bytes per sector */
- u_int8_t spc; /* sectors per cluster */
- u_int8_t res[2]; /* reserved sectors */
- u_int8_t nft; /* number of FATs */
- u_int8_t rde[2]; /* root directory entries */
- u_int8_t sec[2]; /* total sectors */
- u_int8_t mid; /* media descriptor */
- u_int8_t spf[2]; /* sectors per FAT */
- u_int8_t spt[2]; /* sectors per track */
- u_int8_t hds[2]; /* drive heads */
- u_int8_t hid[4]; /* hidden sectors */
- u_int8_t bsec[4]; /* big total sectors */
-};
-
-struct bsxbpb {
- u_int8_t bspf[4]; /* big sectors per FAT */
- u_int8_t xflg[2]; /* FAT control flags */
- u_int8_t vers[2]; /* file system version */
- u_int8_t rdcl[4]; /* root directory start cluster */
- u_int8_t infs[2]; /* file system info sector */
- u_int8_t bkbs[2]; /* backup boot sector */
- u_int8_t rsvd[12]; /* reserved */
-};
-
-struct bsx {
- u_int8_t drv; /* drive number */
- u_int8_t rsvd; /* reserved */
- u_int8_t sig; /* extended boot signature */
- u_int8_t volid[4]; /* volume ID number */
- u_int8_t label[11]; /* volume label */
- u_int8_t type[8]; /* file system type */
-};
-
-struct de {
- u_int8_t namext[11]; /* name and extension */
- u_int8_t attr; /* attributes */
- u_int8_t rsvd[10]; /* reserved */
- u_int8_t time[2]; /* creation time */
- u_int8_t date[2]; /* creation date */
- u_int8_t clus[2]; /* starting cluster */
- u_int8_t size[4]; /* size */
-};
-
-struct bpb {
- u_int bps; /* bytes per sector */
- u_int spc; /* sectors per cluster */
- u_int res; /* reserved sectors */
- u_int nft; /* number of FATs */
- u_int rde; /* root directory entries */
- u_int sec; /* total sectors */
- u_int mid; /* media descriptor */
- u_int spf; /* sectors per FAT */
- u_int spt; /* sectors per track */
- u_int hds; /* drive heads */
- u_int hid; /* hidden sectors */
- u_int bsec; /* big total sectors */
- u_int bspf; /* big sectors per FAT */
- u_int rdcl; /* root directory start cluster */
- u_int infs; /* file system info sector */
- u_int bkbs; /* backup boot sector */
-};
-
-#define BPBGAP 0, 0, 0, 0, 0, 0
-
-static struct {
- const char *name;
- struct bpb bpb;
-} const stdfmt[] = {
- {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1, BPBGAP}},
- {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1, BPBGAP}},
- {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2, BPBGAP}},
- {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2, BPBGAP}},
- {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2, BPBGAP}},
- {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2, BPBGAP}},
- {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}},
- {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2, BPBGAP}},
- {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}},
- {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}}
-};
-
-static const u_int8_t bootcode[] = {
- 0xfa, /* cli */
- 0x31, 0xc0, /* xor ax,ax */
- 0x8e, 0xd0, /* mov ss,ax */
- 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
- 0xfb, /* sti */
- 0x8e, 0xd8, /* mov ds,ax */
- 0xe8, 0x00, 0x00, /* call $ + 3 */
- 0x5e, /* pop si */
- 0x83, 0xc6, 0x19, /* add si,+19h */
- 0xbb, 0x07, 0x00, /* mov bx,0007h */
- 0xfc, /* cld */
- 0xac, /* lodsb */
- 0x84, 0xc0, /* test al,al */
- 0x74, 0x06, /* jz $ + 8 */
- 0xb4, 0x0e, /* mov ah,0eh */
- 0xcd, 0x10, /* int 10h */
- 0xeb, 0xf5, /* jmp $ - 9 */
- 0x30, 0xe4, /* xor ah,ah */
- 0xcd, 0x16, /* int 16h */
- 0xcd, 0x19, /* int 19h */
- 0x0d, 0x0a,
- 'N', 'o', 'n', '-', 's', 'y', 's', 't',
- 'e', 'm', ' ', 'd', 'i', 's', 'k',
- 0x0d, 0x0a,
- 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
- 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
- ' ', 'r', 'e', 'b', 'o', 'o', 't',
- 0x0d, 0x0a,
- 0
-};
-
-static void check_mounted(const char *, mode_t);
-static void getstdfmt(const char *, struct bpb *);
-static void getdiskinfo(int, const char *, const char *, int, struct bpb *);
-static void print_bpb(struct bpb *);
-static u_int ckgeom(const char *, u_int, const char *);
-static u_int argtou(const char *, u_int, u_int, const char *);
-static off_t argtooff(const char *, const char *);
-static int oklabel(const char *);
-static void mklabel(u_int8_t *, const char *);
-static void setstr(u_int8_t *, const char *, size_t);
-static void usage(void);
-
-/*
- * Construct a FAT12, FAT16, or FAT32 file system.
- */
-int newfs_msdos_main(int argc, char *argv[])
-{
- static const char opts[] = "@:NAB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
- const char *opt_B = NULL, *opt_L = NULL, *opt_O = NULL, *opt_f = NULL;
- u_int opt_F = 0, opt_I = 0, opt_S = 0, opt_a = 0, opt_b = 0, opt_c = 0;
- u_int opt_e = 0, opt_h = 0, opt_i = 0, opt_k = 0, opt_m = 0, opt_n = 0;
- u_int opt_o = 0, opt_r = 0, opt_s = 0, opt_u = 0;
- u_int opt_A = 0;
- int opt_N = 0;
- int Iflag = 0, mflag = 0, oflag = 0;
- char buf[MAXPATHLEN];
- struct stat sb;
- struct timeval tv;
- struct bpb bpb;
- struct tm *tm;
- struct bs *bs;
- struct bsbpb *bsbpb;
- struct bsxbpb *bsxbpb;
- struct bsx *bsx;
- struct de *de;
- u_int8_t *img;
- const char *fname, *dtype, *bname;
- ssize_t n;
- time_t now;
- u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
- u_int extra_res, alignment=0, set_res, set_spf, set_spc, tempx, attempts=0;
- int ch, fd, fd1;
- off_t opt_create = 0, opt_ofs = 0;
-
- while ((ch = getopt(argc, argv, opts)) != -1)
- switch (ch) {
- case '@':
- opt_ofs = argtooff(optarg, "offset");
- break;
- case 'N':
- opt_N = 1;
- break;
- case 'A':
- opt_A = 1;
- break;
- case 'B':
- opt_B = optarg;
- break;
- case 'C':
- opt_create = argtooff(optarg, "create size");
- break;
- case 'F':
- if (strcmp(optarg, "12") && strcmp(optarg, "16") && strcmp(optarg, "32"))
- errx(1, "%s: bad FAT type", optarg);
- opt_F = atoi(optarg);
- break;
- case 'I':
- opt_I = argto4(optarg, 0, "volume ID");
- Iflag = 1;
- break;
- case 'L':
- if (!oklabel(optarg))
- errx(1, "%s: bad volume label", optarg);
- opt_L = optarg;
- break;
- case 'O':
- if (strlen(optarg) > 8)
- errx(1, "%s: bad OEM string", optarg);
- opt_O = optarg;
- break;
- case 'S':
- opt_S = argto2(optarg, 1, "bytes/sector");
- break;
- case 'a':
- opt_a = argto4(optarg, 1, "sectors/FAT");
- break;
- case 'b':
- opt_b = argtox(optarg, 1, "block size");
- opt_c = 0;
- break;
- case 'c':
- opt_c = argto1(optarg, 1, "sectors/cluster");
- opt_b = 0;
- break;
- case 'e':
- opt_e = argto2(optarg, 1, "directory entries");
- break;
- case 'f':
- opt_f = optarg;
- break;
- case 'h':
- opt_h = argto2(optarg, 1, "drive heads");
- break;
- case 'i':
- opt_i = argto2(optarg, 1, "info sector");
- break;
- case 'k':
- opt_k = argto2(optarg, 1, "backup sector");
- break;
- case 'm':
- opt_m = argto1(optarg, 0, "media descriptor");
- mflag = 1;
- break;
- case 'n':
- opt_n = argto1(optarg, 1, "number of FATs");
- break;
- case 'o':
- opt_o = argto4(optarg, 0, "hidden sectors");
- oflag = 1;
- break;
- case 'r':
- opt_r = argto2(optarg, 1, "reserved sectors");
- break;
- case 's':
- opt_s = argto4(optarg, 1, "file system size");
- break;
- case 'u':
- opt_u = argto2(optarg, 1, "sectors/track");
- break;
- default:
- usage();
- }
- argc -= optind;
- argv += optind;
- if (argc < 1 || argc > 2)
- usage();
- fname = *argv++;
- if (!opt_create && !strchr(fname, '/')) {
- snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
- if (!(fname = strdup(buf)))
- err(1, "%s", buf);
- }
- dtype = *argv;
- if (opt_A) {
- if (opt_r)
- errx(1, "align (-A) is incompatible with -r");
- if (opt_N)
- errx(1, "align (-A) is incompatible with -N");
- }
- if (opt_create) {
- if (opt_N)
- errx(1, "create (-C) is incompatible with -N");
- fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
- if (fd == -1)
- errx(1, "failed to create %s", fname);
- if (ftruncate(fd, opt_create))
- errx(1, "failed to initialize %jd bytes", (intmax_t)opt_create);
- } else if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1)
- err(1, "%s", fname);
- if (fstat(fd, &sb))
- err(1, "%s", fname);
- if (opt_create) {
- if (!S_ISREG(sb.st_mode))
- warnx("warning, %s is not a regular file", fname);
- } else {
- if (!S_ISCHR(sb.st_mode))
- warnx("warning, %s is not a character device", fname);
- }
- if (!opt_N)
- check_mounted(fname, sb.st_mode);
- if (opt_ofs && opt_ofs != lseek(fd, opt_ofs, SEEK_SET))
- errx(1, "cannot seek to %jd", (intmax_t)opt_ofs);
- memset(&bpb, 0, sizeof(bpb));
- if (opt_f) {
- getstdfmt(opt_f, &bpb);
- bpb.bsec = bpb.sec;
- bpb.sec = 0;
- bpb.bspf = bpb.spf;
- bpb.spf = 0;
- }
- if (opt_h)
- bpb.hds = opt_h;
- if (opt_u)
- bpb.spt = opt_u;
- if (opt_S)
- bpb.bps = opt_S;
- if (opt_s)
- bpb.bsec = opt_s;
- if (oflag)
- bpb.hid = opt_o;
- if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag))) {
- off_t delta;
- getdiskinfo(fd, fname, dtype, oflag, &bpb);
- if (opt_s) {
- bpb.bsec = opt_s;
- }
- bpb.bsec -= (opt_ofs / bpb.bps);
- delta = bpb.bsec % bpb.spt;
- if (delta != 0) {
- warnx("trim %d sectors from %d to adjust to a multiple of %d",
- (int)delta, bpb.bsec, bpb.spt);
- bpb.bsec -= delta;
- }
- if (bpb.spc == 0) { /* set defaults */
- if (bpb.bsec <= 6000) /* about 3MB -> 512 bytes */
- bpb.spc = 1;
- else if (bpb.bsec <= (1<<17)) /* 64M -> 4k */
- bpb.spc = 8;
- else if (bpb.bsec <= (1<<19)) /* 256M -> 8k */
- bpb.spc = 16;
- else if (bpb.bsec <= (1<<22)) /* 2G -> 16k, some versions of windows
- require a minimum of 65527 clusters */
- bpb.spc = 32;
- else
- bpb.spc = 64; /* otherwise 32k */
- }
- }
- if (!powerof2(bpb.bps))
- errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
- if (bpb.bps < MINBPS)
- errx(1, "bytes/sector (%u) is too small; minimum is %u",
- bpb.bps, MINBPS);
- if (!(fat = opt_F)) {
- if (opt_f)
- fat = 12;
- else if (!opt_e && (opt_i || opt_k))
- fat = 32;
- }
- if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
- errx(1, "-%c is not a legal FAT%s option",
- fat == 32 ? 'e' : opt_i ? 'i' : 'k',
- fat == 32 ? "32" : "12/16");
- if (opt_f && fat == 32)
- bpb.rde = 0;
- if (opt_b) {
- if (!powerof2(opt_b))
- errx(1, "block size (%u) is not a power of 2", opt_b);
- if (opt_b < bpb.bps)
- errx(1, "block size (%u) is too small; minimum is %u",
- opt_b, bpb.bps);
- if (opt_b > bpb.bps * MAXSPC)
- errx(1, "block size (%u) is too large; maximum is %u", opt_b, bpb.bps * MAXSPC);
- bpb.spc = opt_b / bpb.bps;
- }
- if (opt_c) {
- if (!powerof2(opt_c))
- errx(1, "sectors/cluster (%u) is not a power of 2", opt_c);
- bpb.spc = opt_c;
- }
- if (opt_r)
- bpb.res = opt_r;
- if (opt_n) {
- if (opt_n > MAXNFT)
- errx(1, "number of FATs (%u) is too large; maximum is %u", opt_n, MAXNFT);
- bpb.nft = opt_n;
- }
- if (opt_e)
- bpb.rde = opt_e;
- if (mflag) {
- if (opt_m < 0xf0)
- errx(1, "illegal media descriptor (%#x)", opt_m);
- bpb.mid = opt_m;
- }
- if (opt_a)
- bpb.bspf = opt_a;
- if (opt_i)
- bpb.infs = opt_i;
- if (opt_k)
- bpb.bkbs = opt_k;
- bss = 1;
- bname = NULL;
- fd1 = -1;
- if (opt_B) {
- bname = opt_B;
- if (!strchr(bname, '/')) {
- snprintf(buf, sizeof(buf), "/boot/%s", bname);
- if (!(bname = strdup(buf)))
- err(1, "%s", buf);
- }
- if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
- err(1, "%s", bname);
- if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
- sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
- errx(1, "%s: inappropriate file type or format", bname);
- bss = sb.st_size / bpb.bps;
- }
- if (!bpb.nft)
- bpb.nft = 2;
- if (!fat) {
- if (bpb.bsec < (bpb.res ? bpb.res : bss) +
- howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
- ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
- bpb.nft +
- howmany(bpb.rde ? bpb.rde : DEFRDE,
- bpb.bps / sizeof(struct de)) +
- (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
- (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
- fat = 12;
- else if (bpb.rde || bpb.bsec <
- (bpb.res ? bpb.res : bss) +
- howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
- howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
- (MAXCLS16 + 1) *
- (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
- fat = 16;
- else
- fat = 32;
- }
- x = bss;
- if (fat == 32) {
- if (!bpb.infs) {
- if (x == MAXU16 || x == bpb.bkbs)
- errx(1, "no room for info sector");
- bpb.infs = x;
- }
- if (bpb.infs != MAXU16 && x <= bpb.infs)
- x = bpb.infs + 1;
- if (!bpb.bkbs) {
- if (x == MAXU16)
- errx(1, "no room for backup sector");
- bpb.bkbs = x;
- } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
- errx(1, "backup sector would overwrite info sector");
- if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
- x = bpb.bkbs + 1;
- }
-
- extra_res = 0;
- set_res = !bpb.res;
- set_spf = !bpb.bspf;
- set_spc = !bpb.spc;
- tempx = x;
- /*
- * Attempt to align if opt_A is set. This is done by increasing the number
- * of reserved blocks. This can cause other factors to change, which can in
- * turn change the alignment. This should take at most 2 iterations, as
- * increasing the reserved amount may cause the FAT size to decrease by 1,
- * requiring another nft reserved blocks. If spc changes, it will
- * be half of its previous size, and thus will not throw off alignment.
- */
- do {
- x = tempx;
- if (set_res)
- bpb.res = (fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x) + extra_res;
- else if (bpb.res < x)
- errx(1, "too few reserved sectors");
- if (fat != 32 && !bpb.rde)
- bpb.rde = DEFRDE;
- rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
- if (set_spc)
- for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
- bpb.spc < MAXSPC &&
- bpb.res +
- howmany((RESFTE + maxcls(fat)) * (fat / BPN),
- bpb.bps * NPB) * bpb.nft +
- rds +
- (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
- bpb.spc <<= 1);
- if (fat != 32 && bpb.bspf > MAXU16)
- errx(1, "too many sectors/FAT for FAT12/16");
- x1 = bpb.res + rds;
- x = bpb.bspf ? bpb.bspf : 1;
- if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
- errx(1, "meta data exceeds file system size");
- x1 += x * bpb.nft;
- x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
- (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
- x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN), bpb.bps * NPB);
- if (set_spf) {
- if (!bpb.bspf) {
- bpb.bspf = x2;
- }
- x1 += (bpb.bspf - 1) * bpb.nft;
- }
- if(set_res) {
- /* attempt to align root directory */
- alignment = (bpb.res + bpb.bspf * bpb.nft) % bpb.spc;
- extra_res += bpb.spc - alignment;
- }
- attempts++;
- } while(opt_A && alignment != 0 && attempts < 2);
- if (alignment != 0)
- warnx("warning: Alignment failed.");
-
- cls = (bpb.bsec - x1) / bpb.spc;
- x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
- if (cls > x)
- cls = x;
- if (bpb.bspf < x2)
- warnx("warning: sectors/FAT limits file system to %u clusters", cls);
- if (cls < mincls(fat))
- errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat, mincls(fat));
- if (cls > maxcls(fat)) {
- cls = maxcls(fat);
- bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
- warnx("warning: FAT type limits file system to %u sectors", bpb.bsec);
- }
- printf("%s: %u sector%s in %u FAT%u cluster%s (%u bytes/cluster)\n",
- fname, cls * bpb.spc, cls * bpb.spc == 1 ? "" : "s", cls, fat,
- cls == 1 ? "" : "s", bpb.bps * bpb.spc);
- if (!bpb.mid)
- bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
- if (fat == 32)
- bpb.rdcl = RESFTE;
- if (bpb.hid + bpb.bsec <= MAXU16) {
- bpb.sec = bpb.bsec;
- bpb.bsec = 0;
- }
- if (fat != 32) {
- bpb.spf = bpb.bspf;
- bpb.bspf = 0;
- }
- print_bpb(&bpb);
- if (!opt_N) {
- gettimeofday(&tv, NULL);
- now = tv.tv_sec;
- tm = localtime(&now);
- if (!(img = malloc(bpb.bps)))
- err(1, "%u", bpb.bps);
- dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
- for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
- x = lsn;
- if (opt_B && fat == 32 && bpb.bkbs != MAXU16 && bss <= bpb.bkbs && x >= bpb.bkbs) {
- x -= bpb.bkbs;
- if (!x && lseek(fd1, opt_ofs, SEEK_SET))
- err(1, "%s", bname);
- }
- if (opt_B && x < bss) {
- if ((n = read(fd1, img, bpb.bps)) == -1)
- err(1, "%s", bname);
- if ((unsigned)n != bpb.bps)
- errx(1, "%s: can't read sector %u", bname, x);
- } else
- memset(img, 0, bpb.bps);
- if (!lsn || (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
- x1 = sizeof(struct bs);
- bsbpb = (struct bsbpb *)(img + x1);
- mk2(bsbpb->bps, bpb.bps);
- mk1(bsbpb->spc, bpb.spc);
- mk2(bsbpb->res, bpb.res);
- mk1(bsbpb->nft, bpb.nft);
- mk2(bsbpb->rde, bpb.rde);
- mk2(bsbpb->sec, bpb.sec);
- mk1(bsbpb->mid, bpb.mid);
- mk2(bsbpb->spf, bpb.spf);
- mk2(bsbpb->spt, bpb.spt);
- mk2(bsbpb->hds, bpb.hds);
- mk4(bsbpb->hid, bpb.hid);
- mk4(bsbpb->bsec, bpb.bsec);
- x1 += sizeof(struct bsbpb);
- if (fat == 32) {
- bsxbpb = (struct bsxbpb *)(img + x1);
- mk4(bsxbpb->bspf, bpb.bspf);
- mk2(bsxbpb->xflg, 0);
- mk2(bsxbpb->vers, 0);
- mk4(bsxbpb->rdcl, bpb.rdcl);
- mk2(bsxbpb->infs, bpb.infs);
- mk2(bsxbpb->bkbs, bpb.bkbs);
- x1 += sizeof(struct bsxbpb);
- }
- bsx = (struct bsx *)(img + x1);
- mk1(bsx->sig, 0x29);
- if (Iflag)
- x = opt_I;
- else
- x = (((u_int)(1 + tm->tm_mon) << 8 |
- (u_int)tm->tm_mday) +
- ((u_int)tm->tm_sec << 8 |
- (u_int)(tv.tv_usec / 10))) << 16 |
- ((u_int)(1900 + tm->tm_year) +
- ((u_int)tm->tm_hour << 8 |
- (u_int)tm->tm_min));
- mk4(bsx->volid, x);
- mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
- snprintf(buf, sizeof(buf), "FAT%u", fat);
- setstr(bsx->type, buf, sizeof(bsx->type));
- if (!opt_B) {
- x1 += sizeof(struct bsx);
- bs = (struct bs *)img;
- mk1(bs->jmp[0], 0xeb);
- mk1(bs->jmp[1], x1 - 2);
- mk1(bs->jmp[2], 0x90);
- setstr(bs->oem, opt_O ? opt_O : "BSD 4.4",
- sizeof(bs->oem));
- memcpy(img + x1, bootcode, sizeof(bootcode));
- mk2(img + MINBPS - 2, DOSMAGIC);
- }
- } else if (fat == 32 && bpb.infs != MAXU16 &&
- (lsn == bpb.infs || (bpb.bkbs != MAXU16 &&
- lsn == bpb.bkbs + bpb.infs))) {
- mk4(img, 0x41615252);
- mk4(img + MINBPS - 28, 0x61417272);
- mk4(img + MINBPS - 24, 0xffffffff);
- mk4(img + MINBPS - 20, bpb.rdcl);
- mk2(img + MINBPS - 2, DOSMAGIC);
- } else if (lsn >= bpb.res && lsn < dir &&
- !((lsn - bpb.res) % (bpb.spf ? bpb.spf : bpb.bspf))) {
- mk1(img[0], bpb.mid);
- for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
- mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
- } else if (lsn == dir && opt_L) {
- de = (struct de *)img;
- mklabel(de->namext, opt_L);
- mk1(de->attr, 050);
- x = (u_int)tm->tm_hour << 11 |
- (u_int)tm->tm_min << 5 |
- (u_int)tm->tm_sec >> 1;
- mk2(de->time, x);
- x = (u_int)(tm->tm_year - 80) << 9 |
- (u_int)(tm->tm_mon + 1) << 5 |
- (u_int)tm->tm_mday;
- mk2(de->date, x);
- }
- if ((n = write(fd, img, bpb.bps)) == -1)
- err(1, "%s", fname);
- if ((unsigned)n != bpb.bps) {
- errx(1, "%s: can't write sector %u", fname, lsn);
- exit(1);
- }
- }
- free(img);
- }
- return 0;
-}
-
-/*
- * Exit with error if file system is mounted.
- */
-static void check_mounted(const char *fname, mode_t mode)
-{
-#ifdef ANDROID
- warnx("Skipping mount checks");
-#else
- struct statfs *mp;
- const char *s1, *s2;
- size_t len;
- int n, r;
-
- if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
- err(1, "getmntinfo");
- len = strlen(_PATH_DEV);
- s1 = fname;
- if (!strncmp(s1, _PATH_DEV, len))
- s1 += len;
- r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
- for (; n--; mp++) {
- s2 = mp->f_mntfromname;
- if (!strncmp(s2, _PATH_DEV, len))
- s2 += len;
- if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) || !strcmp(s1, s2))
- errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
- }
-#endif
-}
-
-/*
- * Get a standard format.
- */
-static void getstdfmt(const char *fmt, struct bpb *bpb)
-{
- u_int x, i;
-
- x = sizeof(stdfmt) / sizeof(stdfmt[0]);
- for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
- if (i == x)
- errx(1, "%s: unknown standard format", fmt);
- *bpb = stdfmt[i].bpb;
-}
-
-/*
- * Get disk slice, partition, and geometry information.
- */
-
-#ifdef ANDROID
-static void getdiskinfo(int fd, const char *fname, const char *dtype,
- __unused int oflag,struct bpb *bpb)
-{
- struct hd_geometry geom;
- u_long block_size;
-
- if (ioctl(fd, BLKSSZGET, &bpb->bps)) {
- fprintf(stderr, "Error getting bytes / sector (%s)\n", strerror(errno));
- exit(1);
- }
-
- ckgeom(fname, bpb->bps, "bytes/sector");
-
- if (ioctl(fd, BLKGETSIZE, &block_size)) {
- fprintf(stderr, "Error getting blocksize (%s)\n", strerror(errno));
- exit(1);
- }
-
- if (block_size > UINT32_MAX) {
- fprintf(stderr, "Error blocksize too large: %lu\n", block_size);
- exit(1);
- }
-
- bpb->bsec = (u_int)block_size;
-
- if (ioctl(fd, HDIO_GETGEO, &geom)) {
- fprintf(stderr, "Error getting gemoetry (%s) - trying sane values\n", strerror(errno));
- geom.heads = 64;
- geom.sectors = 63;
- }
-
- if (!geom.heads) {
- printf("Bogus heads from kernel - setting sane value\n");
- geom.heads = 64;
- }
-
- if (!geom.sectors) {
- printf("Bogus sectors from kernel - setting sane value\n");
- geom.sectors = 63;
- }
-
- bpb->spt = geom.sectors;
- ckgeom(fname, bpb->spt, "sectors/track");
-
- bpb->hds = geom.heads;
- ckgeom(fname, bpb->hds, "drive heads");
-}
-
-#else
-
-static void getdiskinfo(int fd, const char *fname, const char *dtype,
- __unused int oflag, struct bpb *bpb)
-{
- struct disklabel *lp, dlp;
- struct fd_type type;
- off_t ms, hs = 0;
-
- lp = NULL;
-
- /* If the user specified a disk type, try to use that */
- if (dtype != NULL) {
- lp = getdiskbyname(dtype);
- }
-
- /* Maybe it's a floppy drive */
- if (lp == NULL) {
- if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
- struct stat st;
-
- if (fstat(fd, &st))
- err(1, "Cannot get disk size");
- /* create a fake geometry for a file image */
- ms = st.st_size;
- dlp.d_secsize = 512;
- dlp.d_nsectors = 63;
- dlp.d_ntracks = 255;
- dlp.d_secperunit = ms / dlp.d_secsize;
- lp = &dlp;
- } else if (ioctl(fd, FD_GTYPE, &type) != -1) {
- dlp.d_secsize = 128 << type.secsize;
- dlp.d_nsectors = type.sectrac;
- dlp.d_ntracks = type.heads;
- dlp.d_secperunit = ms / dlp.d_secsize;
- lp = &dlp;
- }
- }
-
- /* Maybe it's a fixed drive */
- if (lp == NULL) {
- if (ioctl(fd, DIOCGDINFO, &dlp) == -1) {
- if (bpb->bps == 0 && ioctl(fd, DIOCGSECTORSIZE, &dlp.d_secsize) == -1)
- errx(1, "Cannot get sector size, %s", strerror(errno));
-
- /* XXX Should we use bpb->bps if it's set? */
- dlp.d_secperunit = ms / dlp.d_secsize;
-
- if (bpb->spt == 0 && ioctl(fd, DIOCGFWSECTORS, &dlp.d_nsectors) == -1) {
- warnx("Cannot get number of sectors per track, %s", strerror(errno));
- dlp.d_nsectors = 63;
- }
- if (bpb->hds == 0 && ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) {
- warnx("Cannot get number of heads, %s", strerror(errno));
- if (dlp.d_secperunit <= 63*1*1024)
- dlp.d_ntracks = 1;
- else if (dlp.d_secperunit <= 63*16*1024)
- dlp.d_ntracks = 16;
- else
- dlp.d_ntracks = 255;
- }
- }
-
- hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
- lp = &dlp;
- }
-
- if (bpb->bps == 0)
- bpb->bps = ckgeom(fname, lp->d_secsize, "bytes/sector");
- if (bpb->spt == 0)
- bpb->spt = ckgeom(fname, lp->d_nsectors, "sectors/track");
- if (bpb->hds == 0)
- bpb->hds = ckgeom(fname, lp->d_ntracks, "drive heads");
- if (bpb->bsec == 0)
- bpb->bsec = lp->d_secperunit;
- if (bpb->hid == 0)
- bpb->hid = hs;
-}
-#endif
-
-/*
- * Print out BPB values.
- */
-static void print_bpb(struct bpb *bpb)
-{
- printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
- bpb->nft);
- if (bpb->rde)
- printf(" rde=%u", bpb->rde);
- if (bpb->sec)
- printf(" sec=%u", bpb->sec);
- printf(" mid=%#x", bpb->mid);
- if (bpb->spf)
- printf(" spf=%u", bpb->spf);
- printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
- if (bpb->bsec)
- printf(" bsec=%u", bpb->bsec);
- if (!bpb->spf) {
- printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
- printf(" infs=");
- printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
- printf(" bkbs=");
- printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
- }
- printf("\n");
-}
-
-/*
- * Check a disk geometry value.
- */
-static u_int ckgeom(const char *fname, u_int val, const char *msg)
-{
- if (!val)
- errx(1, "%s: no default %s", fname, msg);
- if (val > MAXU16)
- errx(1, "%s: illegal %s %d", fname, msg, val);
- return val;
-}
-
-/*
- * Convert and check a numeric option argument.
- */
-static u_int argtou(const char *arg, u_int lo, u_int hi, const char *msg)
-{
- char *s;
- u_long x;
-
- errno = 0;
- x = strtoul(arg, &s, 0);
- if (errno || !*arg || *s || x < lo || x > hi)
- errx(1, "%s: bad %s", arg, msg);
- return x;
-}
-
-/*
- * Same for off_t, with optional skmgpP suffix
- */
-static off_t argtooff(const char *arg, const char *msg)
-{
- char *s;
- off_t x;
-
- x = strtoll(arg, &s, 0);
- /* allow at most one extra char */
- if (errno || x < 0 || (s[0] && s[1]) )
- errx(1, "%s: bad %s", arg, msg);
- if (*s) { /* the extra char is the multiplier */
- switch (*s) {
- default:
- errx(1, "%s: bad %s", arg, msg);
- /* notreached */
-
- case 's': /* sector */
- case 'S':
- x <<= 9; /* times 512 */
- break;
-
- case 'k': /* kilobyte */
- case 'K':
- x <<= 10; /* times 1024 */
- break;
-
- case 'm': /* megabyte */
- case 'M':
- x <<= 20; /* times 1024*1024 */
- break;
-
- case 'g': /* gigabyte */
- case 'G':
- x <<= 30; /* times 1024*1024*1024 */
- break;
-
- case 'p': /* partition start */
- case 'P': /* partition start */
- case 'l': /* partition length */
- case 'L': /* partition length */
- errx(1, "%s: not supported yet %s", arg, msg);
- /* notreached */
- }
- }
- return x;
-}
-
-/*
- * Check a volume label.
- */
-static int oklabel(const char *src)
-{
- int c, i;
-
- for (i = 0; i <= 11; i++) {
- c = (u_char)*src++;
- if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
- break;
- }
- return i && !c;
-}
-
-/*
- * Make a volume label.
- */
-static void mklabel(u_int8_t *dest, const char *src)
-{
- int c, i;
-
- for (i = 0; i < 11; i++) {
- c = *src ? toupper(*src++) : ' ';
- *dest++ = !i && c == '\xe5' ? 5 : c;
- }
-}
-
-/*
- * Copy string, padding with spaces.
- */
-static void setstr(u_int8_t *dest, const char *src, size_t len)
-{
- while (len--)
- *dest++ = *src ? *src++ : ' ';
-}
-
-/*
- * Print usage message.
- */
-static void usage(void)
-{
- fprintf(stderr,
- "usage: newfs_msdos [ -options ] special [disktype]\n"
- "where the options are:\n"
- "\t-@ create file system at specified offset\n"
- "\t-A Attempt to cluster align root directory\n"
- "\t-B get bootstrap from file\n"
- "\t-C create image file with specified size\n"
- "\t-F FAT type (12, 16, or 32)\n"
- "\t-I volume ID\n"
- "\t-L volume label\n"
- "\t-N don't create file system: just print out parameters\n"
- "\t-O OEM string\n"
- "\t-S bytes/sector\n"
- "\t-a sectors/FAT\n"
- "\t-b block size\n"
- "\t-c sectors/cluster\n"
- "\t-e root directory entries\n"
- "\t-f standard format\n"
- "\t-h drive heads\n"
- "\t-i file system info sector\n"
- "\t-k backup boot sector\n"
- "\t-m media descriptor\n"
- "\t-n number of FATs\n"
- "\t-o hidden sectors\n"
- "\t-r reserved sectors\n"
- "\t-s file system size (sectors)\n"
- "\t-u sectors/track\n");
- exit(1);
-}
diff --git a/toolbox/tools.h b/toolbox/tools.h
index 505f528..abeb3ef 100644
--- a/toolbox/tools.h
+++ b/toolbox/tools.h
@@ -1,5 +1,3 @@
-TOOL(dd)
TOOL(getevent)
TOOL(getprop)
-TOOL(newfs_msdos)
TOOL(toolbox)
diff --git a/toolbox/upstream-netbsd/bin/dd/args.c b/toolbox/upstream-netbsd/bin/dd/args.c
deleted file mode 100644
index 207e300..0000000
--- a/toolbox/upstream-netbsd/bin/dd/args.c
+++ /dev/null
@@ -1,391 +0,0 @@
-/* $NetBSD: args.c,v 1.38 2013/07/17 12:55:48 christos Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Keith Muller of the University of California, San Diego and Lance
- * Visser of Convex Computer Corporation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
-#else
-__RCSID("$NetBSD: args.c,v 1.38 2013/07/17 12:55:48 christos Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/types.h>
-#include <sys/time.h>
-
-#include <err.h>
-#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "dd.h"
-#include "extern.h"
-
-static int c_arg(const void *, const void *);
-
-#ifdef NO_MSGFMT
-static void f_msgfmt(char *) __dead;
-#else
-static void f_msgfmt(char *);
-#endif /* NO_MSGFMT */
-
-#ifdef NO_CONV
-static void f_conv(char *) __dead;
-#else
-static void f_conv(char *);
-static int c_conv(const void *, const void *);
-#endif /* NO_CONV */
-
-static void f_bs(char *);
-static void f_cbs(char *);
-static void f_count(char *);
-static void f_files(char *);
-static void f_ibs(char *);
-static void f_if(char *);
-static void f_obs(char *);
-static void f_of(char *);
-static void f_seek(char *);
-static void f_skip(char *);
-static void f_progress(char *);
-
-static const struct arg {
- const char *name;
- void (*f)(char *);
- u_int set, noset;
-} args[] = {
- /* the array needs to be sorted by the first column so
- bsearch() can be used to find commands quickly */
- { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
- { "cbs", f_cbs, C_CBS, C_CBS },
- { "conv", f_conv, 0, 0 },
- { "count", f_count, C_COUNT, C_COUNT },
- { "files", f_files, C_FILES, C_FILES },
- { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
- { "if", f_if, C_IF, C_IF },
- { "iseek", f_skip, C_SKIP, C_SKIP },
- { "msgfmt", f_msgfmt, 0, 0 },
- { "obs", f_obs, C_OBS, C_BS|C_OBS },
- { "of", f_of, C_OF, C_OF },
- { "oseek", f_seek, C_SEEK, C_SEEK },
- { "progress", f_progress, 0, 0 },
- { "seek", f_seek, C_SEEK, C_SEEK },
- { "skip", f_skip, C_SKIP, C_SKIP },
-};
-
-/*
- * args -- parse JCL syntax of dd.
- */
-void
-jcl(char **argv)
-{
- struct arg *ap, tmp;
- char *oper, *arg;
-
- in.dbsz = out.dbsz = 512;
-
- while ((oper = *++argv) != NULL) {
- if ((oper = strdup(oper)) == NULL) {
- errx(EXIT_FAILURE,
- "unable to allocate space for the argument %s",
- *argv);
- /* NOTREACHED */
- }
- if ((arg = strchr(oper, '=')) == NULL) {
- errx(EXIT_FAILURE, "unknown operand %s", oper);
- /* NOTREACHED */
- }
- *arg++ = '\0';
- if (!*arg) {
- errx(EXIT_FAILURE, "no value specified for %s", oper);
- /* NOTREACHED */
- }
- tmp.name = oper;
- if (!(ap = bsearch(&tmp, args,
- __arraycount(args), sizeof(*args), c_arg))) {
- errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
- /* NOTREACHED */
- }
- if (ddflags & ap->noset) {
- errx(EXIT_FAILURE,
- "%s: illegal argument combination or already set",
- tmp.name);
- /* NOTREACHED */
- }
- ddflags |= ap->set;
- ap->f(arg);
- }
-
- /* Final sanity checks. */
-
- if (ddflags & C_BS) {
- /*
- * Bs is turned off by any conversion -- we assume the user
- * just wanted to set both the input and output block sizes
- * and didn't want the bs semantics, so we don't warn.
- */
- if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
- C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
- ddflags &= ~C_BS;
- ddflags |= C_IBS|C_OBS;
- }
-
- /* Bs supersedes ibs and obs. */
- if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
- warnx("bs supersedes ibs and obs");
- }
-
- /*
- * Ascii/ebcdic and cbs implies block/unblock.
- * Block/unblock requires cbs and vice-versa.
- */
- if (ddflags & (C_BLOCK|C_UNBLOCK)) {
- if (!(ddflags & C_CBS)) {
- errx(EXIT_FAILURE, "record operations require cbs");
- /* NOTREACHED */
- }
- cfunc = ddflags & C_BLOCK ? block : unblock;
- } else if (ddflags & C_CBS) {
- if (ddflags & (C_ASCII|C_EBCDIC)) {
- if (ddflags & C_ASCII) {
- ddflags |= C_UNBLOCK;
- cfunc = unblock;
- } else {
- ddflags |= C_BLOCK;
- cfunc = block;
- }
- } else {
- errx(EXIT_FAILURE,
- "cbs meaningless if not doing record operations");
- /* NOTREACHED */
- }
- } else
- cfunc = def;
-
- /* Read, write and seek calls take off_t as arguments.
- *
- * The following check is not done because an off_t is a quad
- * for current NetBSD implementations.
- *
- * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
- * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
- */
-}
-
-static int
-c_arg(const void *a, const void *b)
-{
-
- return (strcmp(((const struct arg *)a)->name,
- ((const struct arg *)b)->name));
-}
-
-static void
-f_bs(char *arg)
-{
-
- in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
-}
-
-static void
-f_cbs(char *arg)
-{
-
- cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
-}
-
-static void
-f_count(char *arg)
-{
-
- cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
- if (!cpy_cnt)
- terminate(0);
-}
-
-static void
-f_files(char *arg)
-{
-
- files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
- if (!files_cnt)
- terminate(0);
-}
-
-static void
-f_ibs(char *arg)
-{
-
- if (!(ddflags & C_BS))
- in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
-}
-
-static void
-f_if(char *arg)
-{
-
- in.name = arg;
-}
-
-#ifdef NO_MSGFMT
-/* Build a small version (i.e. for a ramdisk root) */
-static void
-f_msgfmt(char *arg)
-{
-
- errx(EXIT_FAILURE, "msgfmt option disabled");
- /* NOTREACHED */
-}
-#else /* NO_MSGFMT */
-static void
-f_msgfmt(char *arg)
-{
-
- /*
- * If the format string is not valid, dd_write_msg() will print
- * an error and exit.
- */
- dd_write_msg(arg, 0);
-
- msgfmt = arg;
-}
-#endif /* NO_MSGFMT */
-
-static void
-f_obs(char *arg)
-{
-
- if (!(ddflags & C_BS))
- out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
-}
-
-static void
-f_of(char *arg)
-{
-
- out.name = arg;
-}
-
-static void
-f_seek(char *arg)
-{
-
- out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
-}
-
-static void
-f_skip(char *arg)
-{
-
- in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
-}
-
-static void
-f_progress(char *arg)
-{
-
- progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
-}
-
-#ifdef NO_CONV
-/* Build a small version (i.e. for a ramdisk root) */
-static void
-f_conv(char *arg)
-{
-
- errx(EXIT_FAILURE, "conv option disabled");
- /* NOTREACHED */
-}
-#else /* NO_CONV */
-
-static const struct conv {
- const char *name;
- u_int set, noset;
- const u_char *ctab;
-} clist[] = {
- { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
- { "block", C_BLOCK, C_UNBLOCK, NULL },
- { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
- { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
- { "lcase", C_LCASE, C_UCASE, NULL },
- { "noerror", C_NOERROR, 0, NULL },
- { "notrunc", C_NOTRUNC, 0, NULL },
- { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
- { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
- { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
- { "osync", C_OSYNC, C_BS, NULL },
- { "sparse", C_SPARSE, 0, NULL },
- { "swab", C_SWAB, 0, NULL },
- { "sync", C_SYNC, 0, NULL },
- { "ucase", C_UCASE, C_LCASE, NULL },
- { "unblock", C_UNBLOCK, C_BLOCK, NULL },
- /* If you add items to this table, be sure to add the
- * conversions to the C_BS check in the jcl routine above.
- */
-};
-
-static void
-f_conv(char *arg)
-{
- struct conv *cp, tmp;
-
- while (arg != NULL) {
- tmp.name = strsep(&arg, ",");
- if (!(cp = bsearch(&tmp, clist,
- __arraycount(clist), sizeof(*clist), c_conv))) {
- errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
- /* NOTREACHED */
- }
- if (ddflags & cp->noset) {
- errx(EXIT_FAILURE,
- "%s: illegal conversion combination", tmp.name);
- /* NOTREACHED */
- }
- ddflags |= cp->set;
- if (cp->ctab)
- ctab = cp->ctab;
- }
-}
-
-static int
-c_conv(const void *a, const void *b)
-{
-
- return (strcmp(((const struct conv *)a)->name,
- ((const struct conv *)b)->name));
-}
-
-#endif /* NO_CONV */
diff --git a/toolbox/upstream-netbsd/bin/dd/conv.c b/toolbox/upstream-netbsd/bin/dd/conv.c
deleted file mode 100644
index d4a8a09..0000000
--- a/toolbox/upstream-netbsd/bin/dd/conv.c
+++ /dev/null
@@ -1,283 +0,0 @@
-/* $NetBSD: conv.c,v 1.17 2003/08/07 09:05:10 agc Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Keith Muller of the University of California, San Diego and Lance
- * Visser of Convex Computer Corporation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
-#else
-__RCSID("$NetBSD: conv.c,v 1.17 2003/08/07 09:05:10 agc Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/param.h>
-#include <sys/time.h>
-
-#include <err.h>
-#include <string.h>
-#include <stdlib.h>
-
-#include "dd.h"
-#include "extern.h"
-
-/*
- * def --
- * Copy input to output. Input is buffered until reaches obs, and then
- * output until less than obs remains. Only a single buffer is used.
- * Worst case buffer calculation is (ibs + obs - 1).
- */
-void
-def(void)
-{
- uint64_t cnt;
- u_char *inp;
- const u_char *t;
-
- if ((t = ctab) != NULL)
- for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
- *inp = t[*inp];
-
- /* Make the output buffer look right. */
- out.dbp = in.dbp;
- out.dbcnt = in.dbcnt;
-
- if (in.dbcnt >= out.dbsz) {
- /* If the output buffer is full, write it. */
- dd_out(0);
-
- /*
- * Ddout copies the leftover output to the beginning of
- * the buffer and resets the output buffer. Reset the
- * input buffer to match it.
- */
- in.dbp = out.dbp;
- in.dbcnt = out.dbcnt;
- }
-}
-
-void
-def_close(void)
-{
-
- /* Just update the count, everything is already in the buffer. */
- if (in.dbcnt)
- out.dbcnt = in.dbcnt;
-}
-
-#ifdef NO_CONV
-/* Build a smaller version (i.e. for a miniroot) */
-/* These can not be called, but just in case... */
-static const char no_block[] = "unblock and -DNO_CONV?";
-void block(void) { errx(EXIT_FAILURE, "%s", no_block + 2); }
-void block_close(void) { errx(EXIT_FAILURE, "%s", no_block + 2); }
-void unblock(void) { errx(EXIT_FAILURE, "%s", no_block); }
-void unblock_close(void) { errx(EXIT_FAILURE, "%s", no_block); }
-#else /* NO_CONV */
-
-/*
- * Copy variable length newline terminated records with a max size cbsz
- * bytes to output. Records less than cbs are padded with spaces.
- *
- * max in buffer: MAX(ibs, cbsz)
- * max out buffer: obs + cbsz
- */
-void
-block(void)
-{
- static int intrunc;
- int ch = 0; /* pacify gcc */
- uint64_t cnt, maxlen;
- u_char *inp, *outp;
- const u_char *t;
-
- /*
- * Record truncation can cross block boundaries. If currently in a
- * truncation state, keep tossing characters until reach a newline.
- * Start at the beginning of the buffer, as the input buffer is always
- * left empty.
- */
- if (intrunc) {
- for (inp = in.db, cnt = in.dbrcnt;
- cnt && *inp++ != '\n'; --cnt);
- if (!cnt) {
- in.dbcnt = 0;
- in.dbp = in.db;
- return;
- }
- intrunc = 0;
- /* Adjust the input buffer numbers. */
- in.dbcnt = cnt - 1;
- in.dbp = inp + cnt - 1;
- }
-
- /*
- * Copy records (max cbsz size chunks) into the output buffer. The
- * translation is done as we copy into the output buffer.
- */
- for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
- maxlen = MIN(cbsz, in.dbcnt);
- if ((t = ctab) != NULL)
- for (cnt = 0;
- cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
- *outp++ = t[ch];
- else
- for (cnt = 0;
- cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
- *outp++ = ch;
- /*
- * Check for short record without a newline. Reassemble the
- * input block.
- */
- if (ch != '\n' && in.dbcnt < cbsz) {
- (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
- break;
- }
-
- /* Adjust the input buffer numbers. */
- in.dbcnt -= cnt;
- if (ch == '\n')
- --in.dbcnt;
-
- /* Pad short records with spaces. */
- if (cnt < cbsz)
- (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
- else {
- /*
- * If the next character wouldn't have ended the
- * block, it's a truncation.
- */
- if (!in.dbcnt || *inp != '\n')
- ++st.trunc;
-
- /* Toss characters to a newline. */
- for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
- if (!in.dbcnt)
- intrunc = 1;
- else
- --in.dbcnt;
- }
-
- /* Adjust output buffer numbers. */
- out.dbp += cbsz;
- if ((out.dbcnt += cbsz) >= out.dbsz)
- dd_out(0);
- outp = out.dbp;
- }
- in.dbp = in.db + in.dbcnt;
-}
-
-void
-block_close(void)
-{
-
- /*
- * Copy any remaining data into the output buffer and pad to a record.
- * Don't worry about truncation or translation, the input buffer is
- * always empty when truncating, and no characters have been added for
- * translation. The bottom line is that anything left in the input
- * buffer is a truncated record. Anything left in the output buffer
- * just wasn't big enough.
- */
- if (in.dbcnt) {
- ++st.trunc;
- (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
- (void)memset(out.dbp + in.dbcnt,
- ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
- out.dbcnt += cbsz;
- }
-}
-
-/*
- * Convert fixed length (cbsz) records to variable length. Deletes any
- * trailing blanks and appends a newline.
- *
- * max in buffer: MAX(ibs, cbsz) + cbsz
- * max out buffer: obs + cbsz
- */
-void
-unblock(void)
-{
- uint64_t cnt;
- u_char *inp;
- const u_char *t;
-
- /* Translation and case conversion. */
- if ((t = ctab) != NULL)
- for (cnt = in.dbrcnt, inp = in.dbp - 1; cnt--; inp--)
- *inp = t[*inp];
- /*
- * Copy records (max cbsz size chunks) into the output buffer. The
- * translation has to already be done or we might not recognize the
- * spaces.
- */
- for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
- for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
- if (t >= inp) {
- cnt = t - inp + 1;
- (void)memmove(out.dbp, inp, cnt);
- out.dbp += cnt;
- out.dbcnt += cnt;
- }
- ++out.dbcnt;
- *out.dbp++ = '\n';
- if (out.dbcnt >= out.dbsz)
- dd_out(0);
- }
- if (in.dbcnt)
- (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
- in.dbp = in.db + in.dbcnt;
-}
-
-void
-unblock_close(void)
-{
- uint64_t cnt;
- u_char *t;
-
- if (in.dbcnt) {
- warnx("%s: short input record", in.name);
- for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
- if (t >= in.db) {
- cnt = t - in.db + 1;
- (void)memmove(out.dbp, in.db, cnt);
- out.dbp += cnt;
- out.dbcnt += cnt;
- }
- ++out.dbcnt;
- *out.dbp++ = '\n';
- }
-}
-
-#endif /* NO_CONV */
diff --git a/toolbox/upstream-netbsd/bin/dd/dd.c b/toolbox/upstream-netbsd/bin/dd/dd.c
deleted file mode 100644
index 03d080c..0000000
--- a/toolbox/upstream-netbsd/bin/dd/dd.c
+++ /dev/null
@@ -1,598 +0,0 @@
-/* $NetBSD: dd.c,v 1.49 2012/02/21 01:49:01 matt Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Keith Muller of the University of California, San Diego and Lance
- * Visser of Convex Computer Corporation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
- The Regents of the University of California. All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
-#else
-__RCSID("$NetBSD: dd.c,v 1.49 2012/02/21 01:49:01 matt Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <sys/mtio.h>
-#include <sys/time.h>
-
-#include <ctype.h>
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <locale.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "dd.h"
-#include "extern.h"
-
-static void dd_close(void);
-static void dd_in(void);
-static void getfdtype(IO *);
-static void redup_clean_fd(IO *);
-static void setup(void);
-
-int main(int, char *[]);
-
-IO in, out; /* input/output state */
-STAT st; /* statistics */
-void (*cfunc)(void); /* conversion function */
-uint64_t cpy_cnt; /* # of blocks to copy */
-static off_t pending = 0; /* pending seek if sparse */
-u_int ddflags; /* conversion options */
-uint64_t cbsz; /* conversion block size */
-u_int files_cnt = 1; /* # of files to copy */
-uint64_t progress = 0; /* display sign of life */
-const u_char *ctab; /* conversion table */
-sigset_t infoset; /* a set blocking SIGINFO */
-const char *msgfmt = "posix"; /* default summary() message format */
-
-/*
- * Ops for stdin/stdout and crunch'd dd. These are always host ops.
- */
-static const struct ddfops ddfops_stdfd = {
- .op_open = open,
- .op_close = close,
- .op_fcntl = fcntl,
- .op_ioctl = ioctl,
- .op_fstat = fstat,
- .op_fsync = fsync,
- .op_ftruncate = ftruncate,
- .op_lseek = lseek,
- .op_read = read,
- .op_write = write,
-};
-extern const struct ddfops ddfops_prog;
-
-int
-main(int argc, char *argv[])
-{
- int ch;
-
- setprogname(argv[0]);
- (void)setlocale(LC_ALL, "");
-
- while ((ch = getopt(argc, argv, "")) != -1) {
- switch (ch) {
- default:
- errx(EXIT_FAILURE, "usage: dd [operand ...]");
- /* NOTREACHED */
- }
- }
- argc -= (optind - 1);
- argv += (optind - 1);
-
- jcl(argv);
-#ifndef CRUNCHOPS
- if (ddfops_prog.op_init && ddfops_prog.op_init() == -1)
- err(1, "prog init");
-#endif
- setup();
-
- (void)signal(SIGINFO, summaryx);
- (void)signal(SIGINT, terminate);
- (void)sigemptyset(&infoset);
- (void)sigaddset(&infoset, SIGINFO);
-
- (void)atexit(summary);
-
- while (files_cnt--)
- dd_in();
-
- dd_close();
- exit(0);
- /* NOTREACHED */
-}
-
-static void
-setup(void)
-{
-#ifdef CRUNCHOPS
- const struct ddfops *prog_ops = &ddfops_stdfd;
-#else
- const struct ddfops *prog_ops = &ddfops_prog;
-#endif
-
- if (in.name == NULL) {
- in.name = "stdin";
- in.fd = STDIN_FILENO;
- in.ops = &ddfops_stdfd;
- } else {
- in.ops = prog_ops;
- in.fd = ddop_open(in, in.name, O_RDONLY, 0);
- if (in.fd < 0)
- err(EXIT_FAILURE, "%s", in.name);
- /* NOTREACHED */
-
- /* Ensure in.fd is outside the stdio descriptor range */
- redup_clean_fd(&in);
- }
-
- getfdtype(&in);
-
- if (files_cnt > 1 && !(in.flags & ISTAPE)) {
- errx(EXIT_FAILURE, "files is not supported for non-tape devices");
- /* NOTREACHED */
- }
-
- if (out.name == NULL) {
- /* No way to check for read access here. */
- out.fd = STDOUT_FILENO;
- out.name = "stdout";
- out.ops = &ddfops_stdfd;
- } else {
- out.ops = prog_ops;
-#define OFLAGS \
- (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
- out.fd = ddop_open(out, out.name, O_RDWR | OFLAGS, DEFFILEMODE);
- /*
- * May not have read access, so try again with write only.
- * Without read we may have a problem if output also does
- * not support seeks.
- */
- if (out.fd < 0) {
- out.fd = ddop_open(out, out.name, O_WRONLY | OFLAGS,
- DEFFILEMODE);
- out.flags |= NOREAD;
- }
- if (out.fd < 0) {
- err(EXIT_FAILURE, "%s", out.name);
- /* NOTREACHED */
- }
-
- /* Ensure out.fd is outside the stdio descriptor range */
- redup_clean_fd(&out);
- }
-
- getfdtype(&out);
-
- /*
- * Allocate space for the input and output buffers. If not doing
- * record oriented I/O, only need a single buffer.
- */
- if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
- size_t dbsz = out.dbsz;
- if (!(ddflags & C_BS))
- dbsz += in.dbsz - 1;
- if ((in.db = malloc(dbsz)) == NULL) {
- err(EXIT_FAILURE, NULL);
- /* NOTREACHED */
- }
- out.db = in.db;
- } else if ((in.db =
- malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
- (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
- err(EXIT_FAILURE, NULL);
- /* NOTREACHED */
- }
- in.dbp = in.db;
- out.dbp = out.db;
-
- /* Position the input/output streams. */
- if (in.offset)
- pos_in();
- if (out.offset)
- pos_out();
-
- /*
- * Truncate the output file; ignore errors because it fails on some
- * kinds of output files, tapes, for example.
- */
- if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
- (void)ddop_ftruncate(out, out.fd, (off_t)out.offset * out.dbsz);
-
- /*
- * If converting case at the same time as another conversion, build a
- * table that does both at once. If just converting case, use the
- * built-in tables.
- */
- if (ddflags & (C_LCASE|C_UCASE)) {
-#ifdef NO_CONV
- /* Should not get here, but just in case... */
- errx(EXIT_FAILURE, "case conv and -DNO_CONV");
- /* NOTREACHED */
-#else /* NO_CONV */
- u_int cnt;
-
- if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
- if (ddflags & C_LCASE) {
- for (cnt = 0; cnt < 256; ++cnt)
- casetab[cnt] = tolower(ctab[cnt]);
- } else {
- for (cnt = 0; cnt < 256; ++cnt)
- casetab[cnt] = toupper(ctab[cnt]);
- }
- } else {
- if (ddflags & C_LCASE) {
- for (cnt = 0; cnt < 256; ++cnt)
- casetab[cnt] = tolower(cnt);
- } else {
- for (cnt = 0; cnt < 256; ++cnt)
- casetab[cnt] = toupper(cnt);
- }
- }
-
- ctab = casetab;
-#endif /* NO_CONV */
- }
-
- (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */
-}
-
-static void
-getfdtype(IO *io)
-{
- struct mtget mt;
- struct stat sb;
-
- if (io->ops->op_fstat(io->fd, &sb)) {
- err(EXIT_FAILURE, "%s", io->name);
- /* NOTREACHED */
- }
- if (S_ISCHR(sb.st_mode))
- io->flags |= io->ops->op_ioctl(io->fd, MTIOCGET, &mt)
- ? ISCHR : ISTAPE;
- else if (io->ops->op_lseek(io->fd, (off_t)0, SEEK_CUR) == -1
- && errno == ESPIPE)
- io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */
-}
-
-/*
- * Move the parameter file descriptor to a descriptor that is outside the
- * stdio descriptor range, if necessary. This is required to avoid
- * accidentally outputting completion or error messages into the
- * output file that were intended for the tty.
- */
-static void
-redup_clean_fd(IO *io)
-{
- int fd = io->fd;
- int newfd;
-
- if (fd != STDIN_FILENO && fd != STDOUT_FILENO &&
- fd != STDERR_FILENO)
- /* File descriptor is ok, return immediately. */
- return;
-
- /*
- * 3 is the first descriptor greater than STD*_FILENO. Any
- * free descriptor valued 3 or above is acceptable...
- */
- newfd = io->ops->op_fcntl(fd, F_DUPFD, 3);
- if (newfd < 0) {
- err(EXIT_FAILURE, "dupfd IO");
- /* NOTREACHED */
- }
-
- io->ops->op_close(fd);
- io->fd = newfd;
-}
-
-static void
-dd_in(void)
-{
- int flags;
- int64_t n;
-
- for (flags = ddflags;;) {
- if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
- return;
-
- /*
- * Clear the buffer first if doing "sync" on input.
- * If doing block operations use spaces. This will
- * affect not only the C_NOERROR case, but also the
- * last partial input block which should be padded
- * with zero and not garbage.
- */
- if (flags & C_SYNC) {
- if (flags & (C_BLOCK|C_UNBLOCK))
- (void)memset(in.dbp, ' ', in.dbsz);
- else
- (void)memset(in.dbp, 0, in.dbsz);
- }
-
- n = ddop_read(in, in.fd, in.dbp, in.dbsz);
- if (n == 0) {
- in.dbrcnt = 0;
- return;
- }
-
- /* Read error. */
- if (n < 0) {
-
- /*
- * If noerror not specified, die. POSIX requires that
- * the warning message be followed by an I/O display.
- */
- if (!(flags & C_NOERROR)) {
- err(EXIT_FAILURE, "%s", in.name);
- /* NOTREACHED */
- }
- warn("%s", in.name);
- summary();
-
- /*
- * If it's not a tape drive or a pipe, seek past the
- * error. If your OS doesn't do the right thing for
- * raw disks this section should be modified to re-read
- * in sector size chunks.
- */
- if (!(in.flags & (ISPIPE|ISTAPE)) &&
- ddop_lseek(in, in.fd, (off_t)in.dbsz, SEEK_CUR))
- warn("%s", in.name);
-
- /* If sync not specified, omit block and continue. */
- if (!(ddflags & C_SYNC))
- continue;
-
- /* Read errors count as full blocks. */
- in.dbcnt += in.dbrcnt = in.dbsz;
- ++st.in_full;
-
- /* Handle full input blocks. */
- } else if ((uint64_t)n == in.dbsz) {
- in.dbcnt += in.dbrcnt = n;
- ++st.in_full;
-
- /* Handle partial input blocks. */
- } else {
- /* If sync, use the entire block. */
- if (ddflags & C_SYNC)
- in.dbcnt += in.dbrcnt = in.dbsz;
- else
- in.dbcnt += in.dbrcnt = n;
- ++st.in_part;
- }
-
- /*
- * POSIX states that if bs is set and no other conversions
- * than noerror, notrunc or sync are specified, the block
- * is output without buffering as it is read.
- */
- if (ddflags & C_BS) {
- out.dbcnt = in.dbcnt;
- dd_out(1);
- in.dbcnt = 0;
- continue;
- }
-
- if (ddflags & C_SWAB) {
- if ((n = in.dbrcnt) & 1) {
- ++st.swab;
- --n;
- }
- swab(in.dbp, in.dbp, n);
- }
-
- in.dbp += in.dbrcnt;
- (*cfunc)();
- }
-}
-
-/*
- * Cleanup any remaining I/O and flush output. If necessary, output file
- * is truncated.
- */
-static void
-dd_close(void)
-{
-
- if (cfunc == def)
- def_close();
- else if (cfunc == block)
- block_close();
- else if (cfunc == unblock)
- unblock_close();
- if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
- (void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
- out.dbcnt = out.dbsz;
- }
- /* If there are pending sparse blocks, make sure
- * to write out the final block un-sparse
- */
- if ((out.dbcnt == 0) && pending) {
- memset(out.db, 0, out.dbsz);
- out.dbcnt = out.dbsz;
- out.dbp = out.db + out.dbcnt;
- pending -= out.dbsz;
- }
- if (out.dbcnt)
- dd_out(1);
-
- /*
- * Reporting nfs write error may be deferred until next
- * write(2) or close(2) system call. So, we need to do an
- * extra check. If an output is stdout, the file structure
- * may be shared with other processes and close(2) just
- * decreases the reference count.
- */
- if (out.fd == STDOUT_FILENO && ddop_fsync(out, out.fd) == -1
- && errno != EINVAL) {
- err(EXIT_FAILURE, "fsync stdout");
- /* NOTREACHED */
- }
- if (ddop_close(out, out.fd) == -1) {
- err(EXIT_FAILURE, "close");
- /* NOTREACHED */
- }
-}
-
-void
-dd_out(int force)
-{
- static int warned;
- int64_t cnt, n, nw;
- u_char *outp;
-
- /*
- * Write one or more blocks out. The common case is writing a full
- * output block in a single write; increment the full block stats.
- * Otherwise, we're into partial block writes. If a partial write,
- * and it's a character device, just warn. If a tape device, quit.
- *
- * The partial writes represent two cases. 1: Where the input block
- * was less than expected so the output block was less than expected.
- * 2: Where the input block was the right size but we were forced to
- * write the block in multiple chunks. The original versions of dd(1)
- * never wrote a block in more than a single write, so the latter case
- * never happened.
- *
- * One special case is if we're forced to do the write -- in that case
- * we play games with the buffer size, and it's usually a partial write.
- */
- outp = out.db;
- for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
- for (cnt = n;; cnt -= nw) {
-
- if (!force && ddflags & C_SPARSE) {
- int sparse, i;
- sparse = 1; /* Is buffer sparse? */
- for (i = 0; i < cnt; i++)
- if (outp[i] != 0) {
- sparse = 0;
- break;
- }
- if (sparse) {
- pending += cnt;
- outp += cnt;
- nw = 0;
- break;
- }
- }
- if (pending != 0) {
- if (ddop_lseek(out,
- out.fd, pending, SEEK_CUR) == -1)
- err(EXIT_FAILURE, "%s: seek error creating sparse file",
- out.name);
- }
- nw = bwrite(&out, outp, cnt);
- if (nw <= 0) {
- if (nw == 0)
- errx(EXIT_FAILURE,
- "%s: end of device", out.name);
- /* NOTREACHED */
- if (errno != EINTR)
- err(EXIT_FAILURE, "%s", out.name);
- /* NOTREACHED */
- nw = 0;
- }
- if (pending) {
- st.bytes += pending;
- st.sparse += pending/out.dbsz;
- st.out_full += pending/out.dbsz;
- pending = 0;
- }
- outp += nw;
- st.bytes += nw;
- if (nw == n) {
- if ((uint64_t)n != out.dbsz)
- ++st.out_part;
- else
- ++st.out_full;
- break;
- }
- ++st.out_part;
- if (nw == cnt)
- break;
- if (out.flags & ISCHR && !warned) {
- warned = 1;
- warnx("%s: short write on character device", out.name);
- }
- if (out.flags & ISTAPE)
- errx(EXIT_FAILURE,
- "%s: short write on tape device", out.name);
- /* NOTREACHED */
-
- }
- if ((out.dbcnt -= n) < out.dbsz)
- break;
- }
-
- /* Reassemble the output block. */
- if (out.dbcnt)
- (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
- out.dbp = out.db + out.dbcnt;
-
- if (progress && (st.out_full + st.out_part) % progress == 0)
- (void)write(STDERR_FILENO, ".", 1);
-}
-
-/*
- * A protected against SIGINFO write
- */
-ssize_t
-bwrite(IO *io, const void *buf, size_t len)
-{
- sigset_t oset;
- ssize_t rv;
- int oerrno;
-
- (void)sigprocmask(SIG_BLOCK, &infoset, &oset);
- rv = io->ops->op_write(io->fd, buf, len);
- oerrno = errno;
- (void)sigprocmask(SIG_SETMASK, &oset, NULL);
- errno = oerrno;
- return (rv);
-}
diff --git a/toolbox/upstream-netbsd/bin/dd/dd.h b/toolbox/upstream-netbsd/bin/dd/dd.h
deleted file mode 100644
index b01c7b3..0000000
--- a/toolbox/upstream-netbsd/bin/dd/dd.h
+++ /dev/null
@@ -1,127 +0,0 @@
-/* $NetBSD: dd.h,v 1.15 2011/02/04 19:42:12 pooka Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Keith Muller of the University of California, San Diego and Lance
- * Visser of Convex Computer Corporation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * @(#)dd.h 8.3 (Berkeley) 4/2/94
- */
-
-#include <sys/stat.h>
-
-struct ddfops {
- int (*op_init)(void);
-
- int (*op_open)(const char *, int, ...);
- int (*op_close)(int);
-
- int (*op_fcntl)(int, int, ...);
-#ifdef __ANDROID__
- int (*op_ioctl)(int, int, ...);
-#else
- int (*op_ioctl)(int, unsigned long, ...);
-#endif
-
- int (*op_fstat)(int, struct stat *);
- int (*op_fsync)(int);
- int (*op_ftruncate)(int, off_t);
-
- off_t (*op_lseek)(int, off_t, int);
-
- ssize_t (*op_read)(int, void *, size_t);
- ssize_t (*op_write)(int, const void *, size_t);
-};
-
-#define ddop_open(dir, a1, a2, ...) dir.ops->op_open(a1, a2, __VA_ARGS__)
-#define ddop_close(dir, a1) dir.ops->op_close(a1)
-#define ddop_fcntl(dir, a1, a2, ...) dir.ops->op_fcntl(a1, a2, __VA_ARGS__)
-#define ddop_ioctl(dir, a1, a2, ...) dir.ops->op_ioctl(a1, a2, __VA_ARGS__)
-#define ddop_fsync(dir, a1) dir.ops->op_fsync(a1)
-#define ddop_ftruncate(dir, a1, a2) dir.ops->op_ftruncate(a1, a2)
-#define ddop_lseek(dir, a1, a2, a3) dir.ops->op_lseek(a1, a2, a3)
-#define ddop_read(dir, a1, a2, a3) dir.ops->op_read(a1, a2, a3)
-#define ddop_write(dir, a1, a2, a3) dir.ops->op_write(a1, a2, a3)
-
-/* Input/output stream state. */
-typedef struct {
- u_char *db; /* buffer address */
- u_char *dbp; /* current buffer I/O address */
- uint64_t dbcnt; /* current buffer byte count */
- int64_t dbrcnt; /* last read byte count */
- uint64_t dbsz; /* buffer size */
-
-#define ISCHR 0x01 /* character device (warn on short) */
-#define ISPIPE 0x02 /* pipe (not truncatable) */
-#define ISTAPE 0x04 /* tape (not seekable) */
-#define NOREAD 0x08 /* not readable */
- u_int flags;
-
- const char *name; /* name */
- int fd; /* file descriptor */
- uint64_t offset; /* # of blocks to skip */
- struct ddfops const *ops; /* ops to use with fd */
-} IO;
-
-typedef struct {
- uint64_t in_full; /* # of full input blocks */
- uint64_t in_part; /* # of partial input blocks */
- uint64_t out_full; /* # of full output blocks */
- uint64_t out_part; /* # of partial output blocks */
- uint64_t trunc; /* # of truncated records */
- uint64_t swab; /* # of odd-length swab blocks */
- uint64_t sparse; /* # of sparse output blocks */
- uint64_t bytes; /* # of bytes written */
- struct timeval start; /* start time of dd */
-} STAT;
-
-/* Flags (in ddflags). */
-#define C_ASCII 0x00001
-#define C_BLOCK 0x00002
-#define C_BS 0x00004
-#define C_CBS 0x00008
-#define C_COUNT 0x00010
-#define C_EBCDIC 0x00020
-#define C_FILES 0x00040
-#define C_IBS 0x00080
-#define C_IF 0x00100
-#define C_LCASE 0x00200
-#define C_NOERROR 0x00400
-#define C_NOTRUNC 0x00800
-#define C_OBS 0x01000
-#define C_OF 0x02000
-#define C_SEEK 0x04000
-#define C_SKIP 0x08000
-#define C_SWAB 0x10000
-#define C_SYNC 0x20000
-#define C_UCASE 0x40000
-#define C_UNBLOCK 0x80000
-#define C_OSYNC 0x100000
-#define C_SPARSE 0x200000
diff --git a/toolbox/upstream-netbsd/bin/dd/dd_hostops.c b/toolbox/upstream-netbsd/bin/dd/dd_hostops.c
deleted file mode 100644
index d6e7a89..0000000
--- a/toolbox/upstream-netbsd/bin/dd/dd_hostops.c
+++ /dev/null
@@ -1,53 +0,0 @@
-/* $NetBSD: dd_hostops.c,v 1.1 2011/02/04 19:42:12 pooka Exp $ */
-
-/*-
- * Copyright (c) 2010 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__RCSID("$NetBSD: dd_hostops.c,v 1.1 2011/02/04 19:42:12 pooka Exp $");
-#endif /* !lint */
-
-#include <sys/types.h>
-#include <sys/ioctl.h>
-
-#include <fcntl.h>
-#include <unistd.h>
-
-#include "dd.h"
-
-const struct ddfops ddfops_prog = {
- .op_open = open,
- .op_close = close,
- .op_fcntl = fcntl,
- .op_ioctl = ioctl,
- .op_fstat = fstat,
- .op_fsync = fsync,
- .op_ftruncate = ftruncate,
- .op_lseek = lseek,
- .op_read = read,
- .op_write = write,
-};
diff --git a/toolbox/upstream-netbsd/bin/dd/extern.h b/toolbox/upstream-netbsd/bin/dd/extern.h
deleted file mode 100644
index 9c59021..0000000
--- a/toolbox/upstream-netbsd/bin/dd/extern.h
+++ /dev/null
@@ -1,82 +0,0 @@
-/* $NetBSD: extern.h,v 1.22 2011/11/07 22:24:23 jym Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Keith Muller of the University of California, San Diego and Lance
- * Visser of Convex Computer Corporation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * @(#)extern.h 8.3 (Berkeley) 4/2/94
- */
-
-#include <sys/cdefs.h>
-
-#ifdef NO_CONV
-__dead void block(void);
-__dead void block_close(void);
-__dead void unblock(void);
-__dead void unblock_close(void);
-#else
-void block(void);
-void block_close(void);
-void unblock(void);
-void unblock_close(void);
-#endif
-
-#ifndef NO_MSGFMT
-int dd_write_msg(const char *, int);
-#endif
-
-void dd_out(int);
-void def(void);
-void def_close(void);
-void jcl(char **);
-void pos_in(void);
-void pos_out(void);
-void summary(void);
-void summaryx(int);
-__dead void terminate(int);
-void unblock(void);
-void unblock_close(void);
-ssize_t bwrite(IO *, const void *, size_t);
-
-extern IO in, out;
-extern STAT st;
-extern void (*cfunc)(void);
-extern uint64_t cpy_cnt;
-extern uint64_t cbsz;
-extern u_int ddflags;
-extern u_int files_cnt;
-extern uint64_t progress;
-extern const u_char *ctab;
-extern const u_char a2e_32V[], a2e_POSIX[];
-extern const u_char e2a_32V[], e2a_POSIX[];
-extern const u_char a2ibm_32V[], a2ibm_POSIX[];
-extern u_char casetab[];
-extern const char *msgfmt;
diff --git a/toolbox/upstream-netbsd/bin/dd/misc.c b/toolbox/upstream-netbsd/bin/dd/misc.c
deleted file mode 100644
index 0fac98b..0000000
--- a/toolbox/upstream-netbsd/bin/dd/misc.c
+++ /dev/null
@@ -1,342 +0,0 @@
-/* $NetBSD: misc.c,v 1.23 2011/11/07 22:24:23 jym Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Keith Muller of the University of California, San Diego and Lance
- * Visser of Convex Computer Corporation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94";
-#else
-__RCSID("$NetBSD: misc.c,v 1.23 2011/11/07 22:24:23 jym Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/time.h>
-
-#include <err.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <util.h>
-#include <inttypes.h>
-
-#include "dd.h"
-#include "extern.h"
-
-#define tv2mS(tv) ((tv).tv_sec * 1000LL + ((tv).tv_usec + 500) / 1000)
-
-static void posix_summary(void);
-#ifndef NO_MSGFMT
-static void custom_summary(void);
-static void human_summary(void);
-static void quiet_summary(void);
-
-static void buffer_write(const char *, size_t, int);
-#endif /* NO_MSGFMT */
-
-void
-summary(void)
-{
-
- if (progress)
- (void)write(STDERR_FILENO, "\n", 1);
-
-#ifdef NO_MSGFMT
- return posix_summary();
-#else /* NO_MSGFMT */
- if (strncmp(msgfmt, "human", sizeof("human")) == 0)
- return human_summary();
-
- if (strncmp(msgfmt, "posix", sizeof("posix")) == 0)
- return posix_summary();
-
- if (strncmp(msgfmt, "quiet", sizeof("quiet")) == 0)
- return quiet_summary();
-
- return custom_summary();
-#endif /* NO_MSGFMT */
-}
-
-static void
-posix_summary(void)
-{
- char buf[100];
- int64_t mS;
- struct timeval tv;
-
- if (progress)
- (void)write(STDERR_FILENO, "\n", 1);
-
- (void)gettimeofday(&tv, NULL);
- mS = tv2mS(tv) - tv2mS(st.start);
- if (mS == 0)
- mS = 1;
-
- /* Use snprintf(3) so that we don't reenter stdio(3). */
- (void)snprintf(buf, sizeof(buf),
- "%llu+%llu records in\n%llu+%llu records out\n",
- (unsigned long long)st.in_full, (unsigned long long)st.in_part,
- (unsigned long long)st.out_full, (unsigned long long)st.out_part);
- (void)write(STDERR_FILENO, buf, strlen(buf));
- if (st.swab) {
- (void)snprintf(buf, sizeof(buf), "%llu odd length swab %s\n",
- (unsigned long long)st.swab,
- (st.swab == 1) ? "block" : "blocks");
- (void)write(STDERR_FILENO, buf, strlen(buf));
- }
- if (st.trunc) {
- (void)snprintf(buf, sizeof(buf), "%llu truncated %s\n",
- (unsigned long long)st.trunc,
- (st.trunc == 1) ? "block" : "blocks");
- (void)write(STDERR_FILENO, buf, strlen(buf));
- }
- if (st.sparse) {
- (void)snprintf(buf, sizeof(buf), "%llu sparse output %s\n",
- (unsigned long long)st.sparse,
- (st.sparse == 1) ? "block" : "blocks");
- (void)write(STDERR_FILENO, buf, strlen(buf));
- }
- (void)snprintf(buf, sizeof(buf),
- "%llu bytes transferred in %lu.%03d secs (%llu bytes/sec)\n",
- (unsigned long long) st.bytes,
- (long) (mS / 1000),
- (int) (mS % 1000),
- (unsigned long long) (st.bytes * 1000LL / mS));
- (void)write(STDERR_FILENO, buf, strlen(buf));
-}
-
-/* ARGSUSED */
-void
-summaryx(int notused)
-{
-
- summary();
-}
-
-/* ARGSUSED */
-void
-terminate(int signo)
-{
-
- summary();
- (void)raise_default_signal(signo);
- _exit(127);
-}
-
-#ifndef NO_MSGFMT
-/*
- * Buffer write(2) calls
- */
-static void
-buffer_write(const char *str, size_t size, int flush)
-{
- static char wbuf[128];
- static size_t cnt = 0; /* Internal counter to allow wbuf to wrap */
-
- unsigned int i;
-
- for (i = 0; i < size; i++) {
- if (str != NULL) {
- wbuf[cnt++] = str[i];
- }
- if (cnt >= sizeof(wbuf)) {
- (void)write(STDERR_FILENO, wbuf, cnt);
- cnt = 0;
- }
- }
-
- if (flush != 0) {
- (void)write(STDERR_FILENO, wbuf, cnt);
- cnt = 0;
- }
-}
-
-/*
- * Write summary to stderr according to format 'fmt'. If 'enable' is 0, it
- * will not attempt to write anything. Can be used to validate the
- * correctness of the 'fmt' string.
- */
-int
-dd_write_msg(const char *fmt, int enable)
-{
- char hbuf[7], nbuf[32];
- const char *ptr;
- int64_t mS;
- struct timeval tv;
-
- (void)gettimeofday(&tv, NULL);
- mS = tv2mS(tv) - tv2mS(st.start);
- if (mS == 0)
- mS = 1;
-
-#define ADDC(c) do { if (enable != 0) buffer_write(&c, 1, 0); } \
- while (/*CONSTCOND*/0)
-#define ADDS(p) do { if (enable != 0) buffer_write(p, strlen(p), 0); } \
- while (/*CONSTCOND*/0)
-
- for (ptr = fmt; *ptr; ptr++) {
- if (*ptr != '%') {
- ADDC(*ptr);
- continue;
- }
-
- switch (*++ptr) {
- case 'b':
- (void)snprintf(nbuf, sizeof(nbuf), "%llu",
- (unsigned long long)st.bytes);
- ADDS(nbuf);
- break;
- case 'B':
- if (humanize_number(hbuf, sizeof(hbuf),
- st.bytes, "B",
- HN_AUTOSCALE, HN_DECIMAL) == -1)
- warnx("humanize_number (bytes transferred)");
- ADDS(hbuf);
- break;
- case 'e':
- (void)snprintf(nbuf, sizeof(nbuf), "%llu",
- (unsigned long long) (st.bytes * 1000LL / mS));
- ADDS(nbuf);
- break;
- case 'E':
- if (humanize_number(hbuf, sizeof(hbuf),
- st.bytes * 1000LL / mS, "B",
- HN_AUTOSCALE, HN_DECIMAL) == -1)
- warnx("humanize_number (bytes per second)");
- ADDS(hbuf); ADDS("/sec");
- break;
- case 'i':
- (void)snprintf(nbuf, sizeof(nbuf), "%llu",
- (unsigned long long)st.in_part);
- ADDS(nbuf);
- break;
- case 'I':
- (void)snprintf(nbuf, sizeof(nbuf), "%llu",
- (unsigned long long)st.in_full);
- ADDS(nbuf);
- break;
- case 'o':
- (void)snprintf(nbuf, sizeof(nbuf), "%llu",
- (unsigned long long)st.out_part);
- ADDS(nbuf);
- break;
- case 'O':
- (void)snprintf(nbuf, sizeof(nbuf), "%llu",
- (unsigned long long)st.out_full);
- ADDS(nbuf);
- break;
- case 's':
- (void)snprintf(nbuf, sizeof(nbuf), "%li.%03d",
- (long) (mS / 1000), (int) (mS % 1000));
- ADDS(nbuf);
- break;
- case 'p':
- (void)snprintf(nbuf, sizeof(nbuf), "%llu",
- (unsigned long long)st.sparse);
- ADDS(nbuf);
- break;
- case 't':
- (void)snprintf(nbuf, sizeof(nbuf), "%llu",
- (unsigned long long)st.trunc);
- ADDS(nbuf);
- break;
- case 'w':
- (void)snprintf(nbuf, sizeof(nbuf), "%llu",
- (unsigned long long)st.swab);
- ADDS(nbuf);
- break;
- case 'P':
- ADDS("block");
- if (st.sparse != 1) ADDS("s");
- break;
- case 'T':
- ADDS("block");
- if (st.trunc != 1) ADDS("s");
- break;
- case 'W':
- ADDS("block");
- if (st.swab != 1) ADDS("s");
- break;
- case '%':
- ADDC(*ptr);
- break;
- default:
- if (*ptr == '\0')
- goto done;
- errx(EXIT_FAILURE, "unknown specifier '%c' in "
- "msgfmt string", *ptr);
- /* NOTREACHED */
- }
- }
-
-done:
- /* flush buffer */
- buffer_write(NULL, 0, 1);
- return 0;
-}
-
-static void
-custom_summary(void)
-{
-
- dd_write_msg(msgfmt, 1);
-}
-
-static void
-human_summary(void)
-{
- (void)dd_write_msg("%I+%i records in\n%O+%o records out\n", 1);
- if (st.swab) {
- (void)dd_write_msg("%w odd length swab %W\n", 1);
- }
- if (st.trunc) {
- (void)dd_write_msg("%t truncated %T\n", 1);
- }
- if (st.sparse) {
- (void)dd_write_msg("%p sparse output %P\n", 1);
- }
- (void)dd_write_msg("%b bytes (%B) transferred in %s secs "
- "(%e bytes/sec - %E)\n", 1);
-}
-
-static void
-quiet_summary(void)
-{
-
- /* stay quiet */
-}
-#endif /* NO_MSGFMT */
diff --git a/toolbox/upstream-netbsd/bin/dd/position.c b/toolbox/upstream-netbsd/bin/dd/position.c
deleted file mode 100644
index 36dd580..0000000
--- a/toolbox/upstream-netbsd/bin/dd/position.c
+++ /dev/null
@@ -1,185 +0,0 @@
-/* $NetBSD: position.c,v 1.18 2010/11/22 21:04:28 pooka Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Keith Muller of the University of California, San Diego and Lance
- * Visser of Convex Computer Corporation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94";
-#else
-__RCSID("$NetBSD: position.c,v 1.18 2010/11/22 21:04:28 pooka Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <sys/mtio.h>
-#include <sys/time.h>
-
-#include <err.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "dd.h"
-#include "extern.h"
-
-/*
- * Position input/output data streams before starting the copy. Device type
- * dependent. Seekable devices use lseek, and the rest position by reading.
- * Seeking past the end of file can cause null blocks to be written to the
- * output.
- */
-void
-pos_in(void)
-{
- int bcnt, cnt, nr, warned;
-
- /* If not a pipe or tape device, try to seek on it. */
- if (!(in.flags & (ISPIPE|ISTAPE))) {
- if (ddop_lseek(in, in.fd,
- (off_t)in.offset * (off_t)in.dbsz, SEEK_CUR) == -1) {
- err(EXIT_FAILURE, "%s", in.name);
- /* NOTREACHED */
- }
- return;
- /* NOTREACHED */
- }
-
- /*
- * Read the data. If a pipe, read until satisfy the number of bytes
- * being skipped. No differentiation for reading complete and partial
- * blocks for other devices.
- */
- for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {
- if ((nr = ddop_read(in, in.fd, in.db, bcnt)) > 0) {
- if (in.flags & ISPIPE) {
- if (!(bcnt -= nr)) {
- bcnt = in.dbsz;
- --cnt;
- }
- } else
- --cnt;
- continue;
- }
-
- if (nr == 0) {
- if (files_cnt > 1) {
- --files_cnt;
- continue;
- }
- errx(EXIT_FAILURE, "skip reached end of input");
- /* NOTREACHED */
- }
-
- /*
- * Input error -- either EOF with no more files, or I/O error.
- * If noerror not set die. POSIX requires that the warning
- * message be followed by an I/O display.
- */
- if (ddflags & C_NOERROR) {
- if (!warned) {
-
- warn("%s", in.name);
- warned = 1;
- summary();
- }
- continue;
- }
- err(EXIT_FAILURE, "%s", in.name);
- /* NOTREACHED */
- }
-}
-
-void
-pos_out(void)
-{
- struct mtop t_op;
- int n;
- uint64_t cnt;
-
- /*
- * If not a tape, try seeking on the file. Seeking on a pipe is
- * going to fail, but don't protect the user -- they shouldn't
- * have specified the seek operand.
- */
- if (!(out.flags & ISTAPE)) {
- if (ddop_lseek(out, out.fd,
- (off_t)out.offset * (off_t)out.dbsz, SEEK_SET) == -1)
- err(EXIT_FAILURE, "%s", out.name);
- /* NOTREACHED */
- return;
- }
-
- /* If no read access, try using mtio. */
- if (out.flags & NOREAD) {
- t_op.mt_op = MTFSR;
- t_op.mt_count = out.offset;
-
- if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) < 0)
- err(EXIT_FAILURE, "%s", out.name);
- /* NOTREACHED */
- return;
- }
-
- /* Read it. */
- for (cnt = 0; cnt < out.offset; ++cnt) {
- if ((n = ddop_read(out, out.fd, out.db, out.dbsz)) > 0)
- continue;
-
- if (n < 0)
- err(EXIT_FAILURE, "%s", out.name);
- /* NOTREACHED */
-
- /*
- * If reach EOF, fill with NUL characters; first, back up over
- * the EOF mark. Note, cnt has not yet been incremented, so
- * the EOF read does not count as a seek'd block.
- */
- t_op.mt_op = MTBSR;
- t_op.mt_count = 1;
- if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) == -1)
- err(EXIT_FAILURE, "%s", out.name);
- /* NOTREACHED */
-
- while (cnt++ < out.offset)
- if ((uint64_t)(n = bwrite(&out,
- out.db, out.dbsz)) != out.dbsz)
- err(EXIT_FAILURE, "%s", out.name);
- /* NOTREACHED */
- break;
- }
-}
diff --git a/toolbox/upstream-netbsd/include/namespace.h b/toolbox/upstream-netbsd/include/namespace.h
deleted file mode 100644
index e69de29..0000000
--- a/toolbox/upstream-netbsd/include/namespace.h
+++ /dev/null
diff --git a/toolbox/upstream-netbsd/include/sys/extattr.h b/toolbox/upstream-netbsd/include/sys/extattr.h
deleted file mode 100644
index e69de29..0000000
--- a/toolbox/upstream-netbsd/include/sys/extattr.h
+++ /dev/null
diff --git a/toolbox/upstream-netbsd/include/util.h b/toolbox/upstream-netbsd/include/util.h
deleted file mode 100644
index e69de29..0000000
--- a/toolbox/upstream-netbsd/include/util.h
+++ /dev/null
diff --git a/toolbox/upstream-netbsd/lib/libc/gen/getbsize.c b/toolbox/upstream-netbsd/lib/libc/gen/getbsize.c
deleted file mode 100644
index a9ce2c1..0000000
--- a/toolbox/upstream-netbsd/lib/libc/gen/getbsize.c
+++ /dev/null
@@ -1,118 +0,0 @@
-/* $NetBSD: getbsize.c,v 1.17 2012/06/25 22:32:43 abs Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char sccsid[] = "@(#)getbsize.c 8.1 (Berkeley) 6/4/93";
-#else
-__RCSID("$NetBSD: getbsize.c,v 1.17 2012/06/25 22:32:43 abs Exp $");
-#endif
-#endif /* not lint */
-
-#include "namespace.h"
-
-#include <assert.h>
-#include <err.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#ifdef __weak_alias
-__weak_alias(getbsize,_getbsize)
-#endif
-
-char *
-getbsize(int *headerlenp, long *blocksizep)
-{
- static char header[20];
- long n, max, mul, blocksize;
- char *ep, *p;
- const char *form;
-
-#define KB (1024L)
-#define MB (1024L * 1024L)
-#define GB (1024L * 1024L * 1024L)
-#define MAXB GB /* No tera, peta, nor exa. */
- form = "";
- if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
- if ((n = strtol(p, &ep, 10)) < 0)
- goto underflow;
- if (n == 0)
- n = 1;
- if (*ep && ep[1])
- goto fmterr;
- switch (*ep) {
- case 'G': case 'g':
- form = "G";
- max = MAXB / GB;
- mul = GB;
- break;
- case 'K': case 'k':
- form = "K";
- max = MAXB / KB;
- mul = KB;
- break;
- case 'M': case 'm':
- form = "M";
- max = MAXB / MB;
- mul = MB;
- break;
- case '\0':
- max = MAXB;
- mul = 1;
- break;
- default:
-fmterr: warnx("%s: unknown blocksize", p);
- n = 512;
- mul = 1;
- max = 0;
- break;
- }
- if (n > max) {
- warnx("maximum blocksize is %ldG", MAXB / GB);
- n = max;
- }
- if ((blocksize = n * mul) < 512) {
-underflow: warnx("%s: minimum blocksize is 512", p);
- form = "";
- blocksize = n = 512;
- }
- } else
- blocksize = n = 512;
-
- if (headerlenp)
- *headerlenp =
- snprintf(header, sizeof(header), "%ld%s-blocks", n, form);
- if (blocksizep)
- *blocksizep = blocksize;
- return (header);
-}
diff --git a/toolbox/upstream-netbsd/lib/libc/gen/humanize_number.c b/toolbox/upstream-netbsd/lib/libc/gen/humanize_number.c
deleted file mode 100644
index 533560f..0000000
--- a/toolbox/upstream-netbsd/lib/libc/gen/humanize_number.c
+++ /dev/null
@@ -1,161 +0,0 @@
-/* $NetBSD: humanize_number.c,v 1.16 2012/03/17 20:01:14 christos Exp $ */
-
-/*
- * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
- * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: humanize_number.c,v 1.16 2012/03/17 20:01:14 christos Exp $");
-#endif /* LIBC_SCCS and not lint */
-
-#include "namespace.h"
-#include <assert.h>
-#include <inttypes.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <locale.h>
-
-int
-humanize_number(char *buf, size_t len, int64_t bytes,
- const char *suffix, int scale, int flags)
-{
- const char *prefixes, *sep;
- int b, r, s1, s2, sign;
- int64_t divisor, max, post = 1;
- size_t i, baselen, maxscale;
-
- _DIAGASSERT(buf != NULL);
- _DIAGASSERT(suffix != NULL);
- _DIAGASSERT(scale >= 0);
-
- if (flags & HN_DIVISOR_1000) {
- /* SI for decimal multiplies */
- divisor = 1000;
- if (flags & HN_B)
- prefixes = "B\0k\0M\0G\0T\0P\0E";
- else
- prefixes = "\0\0k\0M\0G\0T\0P\0E";
- } else {
- /*
- * binary multiplies
- * XXX IEC 60027-2 recommends Ki, Mi, Gi...
- */
- divisor = 1024;
- if (flags & HN_B)
- prefixes = "B\0K\0M\0G\0T\0P\0E";
- else
- prefixes = "\0\0K\0M\0G\0T\0P\0E";
- }
-
-#define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
- maxscale = 7;
-
- if ((size_t)scale >= maxscale &&
- (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
- return (-1);
-
- if (buf == NULL || suffix == NULL)
- return (-1);
-
- if (len > 0)
- buf[0] = '\0';
- if (bytes < 0) {
- sign = -1;
- baselen = 3; /* sign, digit, prefix */
- if (-bytes < INT64_MAX / 100)
- bytes *= -100;
- else {
- bytes = -bytes;
- post = 100;
- baselen += 2;
- }
- } else {
- sign = 1;
- baselen = 2; /* digit, prefix */
- if (bytes < INT64_MAX / 100)
- bytes *= 100;
- else {
- post = 100;
- baselen += 2;
- }
- }
- if (flags & HN_NOSPACE)
- sep = "";
- else {
- sep = " ";
- baselen++;
- }
- baselen += strlen(suffix);
-
- /* Check if enough room for `x y' + suffix + `\0' */
- if (len < baselen + 1)
- return (-1);
-
- if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
- /* See if there is additional columns can be used. */
- for (max = 100, i = len - baselen; i-- > 0;)
- max *= 10;
-
- /*
- * Divide the number until it fits the given column.
- * If there will be an overflow by the rounding below,
- * divide once more.
- */
- for (i = 0; bytes >= max - 50 && i < maxscale; i++)
- bytes /= divisor;
-
- if (scale & HN_GETSCALE) {
- _DIAGASSERT(__type_fit(int, i));
- return (int)i;
- }
- } else
- for (i = 0; i < (size_t)scale && i < maxscale; i++)
- bytes /= divisor;
- bytes *= post;
-
- /* If a value <= 9.9 after rounding and ... */
- if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
- /* baselen + \0 + .N */
- if (len < baselen + 1 + 2)
- return (-1);
- b = ((int)bytes + 5) / 10;
- s1 = b / 10;
- s2 = b % 10;
- r = snprintf(buf, len, "%d%s%d%s%s%s",
- sign * s1, localeconv()->decimal_point, s2,
- sep, SCALE2PREFIX(i), suffix);
- } else
- r = snprintf(buf, len, "%" PRId64 "%s%s%s",
- sign * ((bytes + 50) / 100),
- sep, SCALE2PREFIX(i), suffix);
-
- return (r);
-}
diff --git a/toolbox/upstream-netbsd/lib/libc/stdlib/strsuftoll.c b/toolbox/upstream-netbsd/lib/libc/stdlib/strsuftoll.c
deleted file mode 100644
index 80fc52f..0000000
--- a/toolbox/upstream-netbsd/lib/libc/stdlib/strsuftoll.c
+++ /dev/null
@@ -1,249 +0,0 @@
-/* $NetBSD: strsuftoll.c,v 1.9 2011/10/22 22:08:47 christos Exp $ */
-/*-
- * Copyright (c) 2001-2002,2004 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Luke Mewburn.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-/*-
- * Copyright (c) 1991, 1993, 1994
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Keith Muller of the University of California, San Diego and Lance
- * Visser of Convex Computer Corporation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#if HAVE_NBTOOL_CONFIG_H
-#include "nbtool_config.h"
-#endif
-
-#include <sys/cdefs.h>
-
-#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: strsuftoll.c,v 1.9 2011/10/22 22:08:47 christos Exp $");
-#endif /* LIBC_SCCS and not lint */
-
-#ifdef _LIBC
-#include "namespace.h"
-#endif
-
-#if !HAVE_STRSUFTOLL
-
-#include <sys/types.h>
-#include <sys/time.h>
-
-#include <assert.h>
-#include <ctype.h>
-#include <err.h>
-#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#ifdef _LIBC
-# ifdef __weak_alias
-__weak_alias(strsuftoll, _strsuftoll)
-__weak_alias(strsuftollx, _strsuftollx)
-# endif
-#endif /* LIBC */
-
-/*
- * Convert an expression of the following forms to a (u)int64_t.
- * 1) A positive decimal number.
- * 2) A positive decimal number followed by a b (mult by 512).
- * 3) A positive decimal number followed by a k (mult by 1024).
- * 4) A positive decimal number followed by a m (mult by 1048576).
- * 5) A positive decimal number followed by a g (mult by 1073741824).
- * 6) A positive decimal number followed by a t (mult by 1099511627776).
- * 7) A positive decimal number followed by a w (mult by sizeof int)
- * 8) Two or more positive decimal numbers (with/without k,b or w).
- * separated by x (also * for backwards compatibility), specifying
- * the product of the indicated values.
- * Returns the result upon successful conversion, or exits with an
- * appropriate error.
- *
- */
-/* LONGLONG */
-long long
-strsuftoll(const char *desc, const char *val,
- long long min, long long max)
-{
- long long result;
- char errbuf[100];
-
- result = strsuftollx(desc, val, min, max, errbuf, sizeof(errbuf));
- if (*errbuf != '\0')
- errx(EXIT_FAILURE, "%s", errbuf);
- return result;
-}
-
-/*
- * As strsuftoll(), but returns the error message into the provided buffer
- * rather than exiting with it.
- */
-/* LONGLONG */
-static long long
-__strsuftollx(const char *desc, const char *val,
- long long min, long long max, char *ebuf, size_t ebuflen, size_t depth)
-{
- long long num, t;
- char *expr;
-
- _DIAGASSERT(desc != NULL);
- _DIAGASSERT(val != NULL);
- _DIAGASSERT(ebuf != NULL);
-
- if (depth > 16) {
- snprintf(ebuf, ebuflen, "%s: Recursion limit exceeded", desc);
- return 0;
- }
-
- while (isspace((unsigned char)*val)) /* Skip leading space */
- val++;
-
- errno = 0;
- num = strtoll(val, &expr, 10);
- if (errno == ERANGE)
- goto erange; /* Overflow */
-
- if (expr == val) /* No digits */
- goto badnum;
-
- switch (*expr) {
- case 'b':
- t = num;
- num *= 512; /* 1 block */
- if (t > num)
- goto erange;
- ++expr;
- break;
- case 'k':
- t = num;
- num *= 1024; /* 1 kibibyte */
- if (t > num)
- goto erange;
- ++expr;
- break;
- case 'm':
- t = num;
- num *= 1048576; /* 1 mebibyte */
- if (t > num)
- goto erange;
- ++expr;
- break;
- case 'g':
- t = num;
- num *= 1073741824; /* 1 gibibyte */
- if (t > num)
- goto erange;
- ++expr;
- break;
- case 't':
- t = num;
- num *= 1099511627776LL; /* 1 tebibyte */
- if (t > num)
- goto erange;
- ++expr;
- break;
- case 'w':
- t = num;
- num *= sizeof(int); /* 1 word */
- if (t > num)
- goto erange;
- ++expr;
- break;
- }
-
- switch (*expr) {
- case '\0':
- break;
- case '*': /* Backward compatible */
- case 'x':
- t = num;
- num *= __strsuftollx(desc, expr + 1, min, max, ebuf, ebuflen,
- depth + 1);
- if (*ebuf != '\0')
- return 0;
- if (t > num) {
- erange:
- errno = ERANGE;
- snprintf(ebuf, ebuflen, "%s: %s", desc, strerror(errno));
- return 0;
- }
- break;
- default:
- badnum:
- snprintf(ebuf, ebuflen, "%s `%s': illegal number", desc, val);
- return 0;
- }
- if (num < min) {
- /* LONGLONG */
- snprintf(ebuf, ebuflen, "%s %lld is less than %lld.",
- desc, (long long)num, (long long)min);
- return 0;
- }
- if (num > max) {
- /* LONGLONG */
- snprintf(ebuf, ebuflen, "%s %lld is greater than %lld.",
- desc, (long long)num, (long long)max);
- return 0;
- }
- *ebuf = '\0';
- return num;
-}
-
-long long
-strsuftollx(const char *desc, const char *val,
- long long min, long long max, char *ebuf, size_t ebuflen)
-{
- return __strsuftollx(desc, val, min, max, ebuf, ebuflen, 0);
-}
-#endif /* !HAVE_STRSUFTOLL */
diff --git a/toolbox/upstream-netbsd/lib/libc/string/swab.c b/toolbox/upstream-netbsd/lib/libc/string/swab.c
deleted file mode 100644
index 392b186..0000000
--- a/toolbox/upstream-netbsd/lib/libc/string/swab.c
+++ /dev/null
@@ -1,80 +0,0 @@
-/* $NetBSD: swab.c,v 1.18 2011/01/04 17:14:07 martin Exp $ */
-
-/*
- * Copyright (c) 1988, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Jeffrey Mogul.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char sccsid[] = "@(#)swab.c 8.1 (Berkeley) 6/4/93";
-#else
-__RCSID("$NetBSD: swab.c,v 1.18 2011/01/04 17:14:07 martin Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
-#include <assert.h>
-#include <unistd.h>
-
-void
-swab(const void * __restrict from, void * __restrict to, ssize_t len)
-{
- char temp;
- const char *fp;
- char *tp;
-
- if (len <= 1)
- return;
-
- _DIAGASSERT(from != NULL);
- _DIAGASSERT(to != NULL);
-
- len /= 2;
- fp = (const char *)from;
- tp = (char *)to;
-#define STEP temp = *fp++,*tp++ = *fp++,*tp++ = temp
-
- if (__predict_false(len == 1)) {
- STEP;
- return;
- }
-
- /* round to multiple of 8 */
- while ((--len % 8) != 0)
- STEP;
- len /= 8;
- if (len == 0)
- return;
- while (len-- != 0) {
- STEP; STEP; STEP; STEP;
- STEP; STEP; STEP; STEP;
- }
-}
diff --git a/toolbox/upstream-netbsd/lib/libutil/raise_default_signal.c b/toolbox/upstream-netbsd/lib/libutil/raise_default_signal.c
deleted file mode 100644
index 50cffd4..0000000
--- a/toolbox/upstream-netbsd/lib/libutil/raise_default_signal.c
+++ /dev/null
@@ -1,117 +0,0 @@
-/* $NetBSD: raise_default_signal.c,v 1.3 2008/04/28 20:23:03 martin Exp $ */
-
-/*-
- * Copyright (c) 2007 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Luke Mewburn.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#if HAVE_NBTOOL_CONFIG_H
-#include "nbtool_config.h"
-#endif
-
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: raise_default_signal.c,v 1.3 2008/04/28 20:23:03 martin Exp $");
-#endif
-
-#include <errno.h>
-#include <signal.h>
-#include <stdio.h>
-#include <string.h>
-#include <util.h>
-
-#if ! HAVE_RAISE_DEFAULT_SIGNAL
-/*
- * raise_default_signal sig
- * Raise the default signal handler for sig, by
- * - block all signals
- * - set the signal handler to SIG_DFL
- * - raise the signal
- * - unblock the signal to deliver it
- *
- * The original signal mask and signal handler is restored on exit
- * (whether successful or not).
- *
- * Returns 0 on success, or -1 on failure with errno set to
- * on of the values for sigemptyset(), sigaddset(), sigprocmask(),
- * sigaction(), or raise().
- */
-int
-raise_default_signal(int sig)
-{
- struct sigaction origact, act;
- sigset_t origmask, fullmask, mask;
- int retval, oerrno;
-
- retval = -1;
-
- /* Setup data structures */
- /* XXX memset(3) isn't async-safe according to signal(7) */
- (void)memset(&act, 0, sizeof(act));
- act.sa_handler = SIG_DFL;
- act.sa_flags = 0;
- if ((sigemptyset(&act.sa_mask) == -1) ||
- (sigfillset(&fullmask) == -1) ||
- (sigemptyset(&mask) == -1) ||
- (sigaddset(&mask, sig) == -1))
- goto restore_none;
-
- /* Block all signals */
- if (sigprocmask(SIG_BLOCK, &fullmask, &origmask) == -1)
- goto restore_none;
- /* (use 'goto restore_mask' to restore state) */
-
- /* Enable the SIG_DFL handler */
- if (sigaction(sig, &act, &origact) == -1)
- goto restore_mask;
- /* (use 'goto restore_act' to restore state) */
-
- /* Raise the signal, and unblock the signal to deliver it */
- if ((raise(sig) == -1) ||
- (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1))
- goto restore_act;
-
- /* Flag successful raise() */
- retval = 0;
-
- /* Restore the original handler */
- restore_act:
- oerrno = errno;
- (void)sigaction(sig, &origact, NULL);
- errno = oerrno;
-
- /* Restore the original mask */
- restore_mask:
- oerrno = errno;
- (void)sigprocmask(SIG_SETMASK, &origmask, NULL);
- errno = oerrno;
-
- restore_none:
- return retval;
-}
-
-#endif /* ! HAVE_RAISE_DEFAULT_SIGNAL */
diff --git a/trusty/OWNERS b/trusty/OWNERS
index 357b4f4..e807d71 100644
--- a/trusty/OWNERS
+++ b/trusty/OWNERS
@@ -1,3 +1,7 @@
-bohr@google.com
-swillden@google.com
+arve@google.com
dkrahn@google.com
+drewry@google.com
+gmar@google.com
+ncbray@google.com
+rpere@google.com
+swillden@google.com
diff --git a/trusty/keymaster/3.0/TrustyKeymaster3Device.cpp b/trusty/keymaster/3.0/TrustyKeymaster3Device.cpp
new file mode 100644
index 0000000..8e3b3b1
--- /dev/null
+++ b/trusty/keymaster/3.0/TrustyKeymaster3Device.cpp
@@ -0,0 +1,448 @@
+/*
+ **
+ ** Copyright 2018, 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.
+ */
+
+#define LOG_TAG "android.hardware.keymaster@3.0-impl.trusty"
+
+#include <authorization_set.h>
+#include <cutils/log.h>
+#include <keymaster/android_keymaster_messages.h>
+#include <trusty_keymaster/TrustyKeymaster3Device.h>
+
+using ::keymaster::AbortOperationRequest;
+using ::keymaster::AbortOperationResponse;
+using ::keymaster::AddEntropyRequest;
+using ::keymaster::AddEntropyResponse;
+using ::keymaster::AttestKeyRequest;
+using ::keymaster::AttestKeyResponse;
+using ::keymaster::AuthorizationSet;
+using ::keymaster::BeginOperationRequest;
+using ::keymaster::BeginOperationResponse;
+using ::keymaster::ExportKeyRequest;
+using ::keymaster::ExportKeyResponse;
+using ::keymaster::FinishOperationRequest;
+using ::keymaster::FinishOperationResponse;
+using ::keymaster::GenerateKeyRequest;
+using ::keymaster::GenerateKeyResponse;
+using ::keymaster::GetKeyCharacteristicsRequest;
+using ::keymaster::GetKeyCharacteristicsResponse;
+using ::keymaster::ImportKeyRequest;
+using ::keymaster::ImportKeyResponse;
+using ::keymaster::UpdateOperationRequest;
+using ::keymaster::UpdateOperationResponse;
+using ::keymaster::ng::Tag;
+
+namespace keymaster {
+
+namespace {
+
+inline keymaster_tag_t legacy_enum_conversion(const Tag value) {
+ return keymaster_tag_t(value);
+}
+inline Tag legacy_enum_conversion(const keymaster_tag_t value) {
+ return Tag(value);
+}
+inline keymaster_purpose_t legacy_enum_conversion(const KeyPurpose value) {
+ return keymaster_purpose_t(value);
+}
+inline keymaster_key_format_t legacy_enum_conversion(const KeyFormat value) {
+ return keymaster_key_format_t(value);
+}
+inline ErrorCode legacy_enum_conversion(const keymaster_error_t value) {
+ return ErrorCode(value);
+}
+
+inline keymaster_tag_type_t typeFromTag(const keymaster_tag_t tag) {
+ return keymaster_tag_get_type(tag);
+}
+
+class KmParamSet : public keymaster_key_param_set_t {
+ public:
+ KmParamSet(const hidl_vec<KeyParameter>& keyParams) {
+ params = new keymaster_key_param_t[keyParams.size()];
+ length = keyParams.size();
+ for (size_t i = 0; i < keyParams.size(); ++i) {
+ auto tag = legacy_enum_conversion(keyParams[i].tag);
+ switch (typeFromTag(tag)) {
+ case KM_ENUM:
+ case KM_ENUM_REP:
+ params[i] = keymaster_param_enum(tag, keyParams[i].f.integer);
+ break;
+ case KM_UINT:
+ case KM_UINT_REP:
+ params[i] = keymaster_param_int(tag, keyParams[i].f.integer);
+ break;
+ case KM_ULONG:
+ case KM_ULONG_REP:
+ params[i] = keymaster_param_long(tag, keyParams[i].f.longInteger);
+ break;
+ case KM_DATE:
+ params[i] = keymaster_param_date(tag, keyParams[i].f.dateTime);
+ break;
+ case KM_BOOL:
+ if (keyParams[i].f.boolValue)
+ params[i] = keymaster_param_bool(tag);
+ else
+ params[i].tag = KM_TAG_INVALID;
+ break;
+ case KM_BIGNUM:
+ case KM_BYTES:
+ params[i] = keymaster_param_blob(tag, &keyParams[i].blob[0],
+ keyParams[i].blob.size());
+ break;
+ case KM_INVALID:
+ default:
+ params[i].tag = KM_TAG_INVALID;
+ /* just skip */
+ break;
+ }
+ }
+ }
+ KmParamSet(KmParamSet&& other) : keymaster_key_param_set_t{other.params, other.length} {
+ other.length = 0;
+ other.params = nullptr;
+ }
+ KmParamSet(const KmParamSet&) = delete;
+ ~KmParamSet() { delete[] params; }
+};
+
+inline hidl_vec<uint8_t> kmBlob2hidlVec(const keymaster_key_blob_t& blob) {
+ hidl_vec<uint8_t> result;
+ result.setToExternal(const_cast<unsigned char*>(blob.key_material), blob.key_material_size);
+ return result;
+}
+
+inline hidl_vec<uint8_t> kmBlob2hidlVec(const keymaster_blob_t& blob) {
+ hidl_vec<uint8_t> result;
+ result.setToExternal(const_cast<unsigned char*>(blob.data), blob.data_length);
+ return result;
+}
+
+inline hidl_vec<uint8_t> kmBuffer2hidlVec(const ::keymaster::Buffer& buf) {
+ hidl_vec<uint8_t> result;
+ result.setToExternal(const_cast<unsigned char*>(buf.peek_read()), buf.available_read());
+ return result;
+}
+
+inline static hidl_vec<hidl_vec<uint8_t>> kmCertChain2Hidl(
+ const keymaster_cert_chain_t& cert_chain) {
+ hidl_vec<hidl_vec<uint8_t>> result;
+ if (!cert_chain.entry_count || !cert_chain.entries) return result;
+
+ result.resize(cert_chain.entry_count);
+ for (size_t i = 0; i < cert_chain.entry_count; ++i) {
+ result[i] = kmBlob2hidlVec(cert_chain.entries[i]);
+ }
+
+ return result;
+}
+
+static inline hidl_vec<KeyParameter> kmParamSet2Hidl(const keymaster_key_param_set_t& set) {
+ hidl_vec<KeyParameter> result;
+ if (set.length == 0 || set.params == nullptr) return result;
+
+ result.resize(set.length);
+ keymaster_key_param_t* params = set.params;
+ for (size_t i = 0; i < set.length; ++i) {
+ auto tag = params[i].tag;
+ result[i].tag = legacy_enum_conversion(tag);
+ switch (typeFromTag(tag)) {
+ case KM_ENUM:
+ case KM_ENUM_REP:
+ result[i].f.integer = params[i].enumerated;
+ break;
+ case KM_UINT:
+ case KM_UINT_REP:
+ result[i].f.integer = params[i].integer;
+ break;
+ case KM_ULONG:
+ case KM_ULONG_REP:
+ result[i].f.longInteger = params[i].long_integer;
+ break;
+ case KM_DATE:
+ result[i].f.dateTime = params[i].date_time;
+ break;
+ case KM_BOOL:
+ result[i].f.boolValue = params[i].boolean;
+ break;
+ case KM_BIGNUM:
+ case KM_BYTES:
+ result[i].blob.setToExternal(const_cast<unsigned char*>(params[i].blob.data),
+ params[i].blob.data_length);
+ break;
+ case KM_INVALID:
+ default:
+ params[i].tag = KM_TAG_INVALID;
+ /* just skip */
+ break;
+ }
+ }
+ return result;
+}
+
+void addClientAndAppData(const hidl_vec<uint8_t>& clientId, const hidl_vec<uint8_t>& appData,
+ ::keymaster::AuthorizationSet* params) {
+ params->Clear();
+ if (clientId.size()) {
+ params->push_back(::keymaster::TAG_APPLICATION_ID, clientId.data(), clientId.size());
+ }
+ if (appData.size()) {
+ params->push_back(::keymaster::TAG_APPLICATION_DATA, appData.data(), appData.size());
+ }
+}
+
+} // anonymous namespace
+
+TrustyKeymaster3Device::TrustyKeymaster3Device(TrustyKeymaster* impl) : impl_(impl) {}
+
+TrustyKeymaster3Device::~TrustyKeymaster3Device() {}
+
+Return<void> TrustyKeymaster3Device::getHardwareFeatures(getHardwareFeatures_cb _hidl_cb) {
+ _hidl_cb(true /* is_secure */, true /* supports_ec */,
+ true /* supports_symmetric_cryptography */, true /* supports_attestation */,
+ true /* supportsAllDigests */, "TrustyKeymaster", "Google");
+ return Void();
+}
+
+Return<ErrorCode> TrustyKeymaster3Device::addRngEntropy(const hidl_vec<uint8_t>& data) {
+ if (data.size() == 0) return ErrorCode::OK;
+ AddEntropyRequest request;
+ request.random_data.Reinitialize(data.data(), data.size());
+
+ AddEntropyResponse response;
+ impl_->AddRngEntropy(request, &response);
+
+ return legacy_enum_conversion(response.error);
+}
+
+Return<void> TrustyKeymaster3Device::generateKey(const hidl_vec<KeyParameter>& keyParams,
+ generateKey_cb _hidl_cb) {
+ GenerateKeyRequest request;
+ request.key_description.Reinitialize(KmParamSet(keyParams));
+
+ GenerateKeyResponse response;
+ impl_->GenerateKey(request, &response);
+
+ KeyCharacteristics resultCharacteristics;
+ hidl_vec<uint8_t> resultKeyBlob;
+ if (response.error == KM_ERROR_OK) {
+ resultKeyBlob = kmBlob2hidlVec(response.key_blob);
+ resultCharacteristics.teeEnforced = kmParamSet2Hidl(response.enforced);
+ resultCharacteristics.softwareEnforced = kmParamSet2Hidl(response.unenforced);
+ }
+ _hidl_cb(legacy_enum_conversion(response.error), resultKeyBlob, resultCharacteristics);
+ return Void();
+}
+
+Return<void> TrustyKeymaster3Device::getKeyCharacteristics(const hidl_vec<uint8_t>& keyBlob,
+ const hidl_vec<uint8_t>& clientId,
+ const hidl_vec<uint8_t>& appData,
+ getKeyCharacteristics_cb _hidl_cb) {
+ GetKeyCharacteristicsRequest request;
+ request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
+ addClientAndAppData(clientId, appData, &request.additional_params);
+
+ GetKeyCharacteristicsResponse response;
+ impl_->GetKeyCharacteristics(request, &response);
+
+ KeyCharacteristics resultCharacteristics;
+ if (response.error == KM_ERROR_OK) {
+ resultCharacteristics.teeEnforced = kmParamSet2Hidl(response.enforced);
+ resultCharacteristics.softwareEnforced = kmParamSet2Hidl(response.unenforced);
+ }
+ _hidl_cb(legacy_enum_conversion(response.error), resultCharacteristics);
+ return Void();
+}
+
+Return<void> TrustyKeymaster3Device::importKey(const hidl_vec<KeyParameter>& params,
+ KeyFormat keyFormat,
+ const hidl_vec<uint8_t>& keyData,
+ importKey_cb _hidl_cb) {
+ ImportKeyRequest request;
+ request.key_description.Reinitialize(KmParamSet(params));
+ request.key_format = legacy_enum_conversion(keyFormat);
+ request.SetKeyMaterial(keyData.data(), keyData.size());
+
+ ImportKeyResponse response;
+ impl_->ImportKey(request, &response);
+
+ KeyCharacteristics resultCharacteristics;
+ hidl_vec<uint8_t> resultKeyBlob;
+ if (response.error == KM_ERROR_OK) {
+ resultKeyBlob = kmBlob2hidlVec(response.key_blob);
+ resultCharacteristics.teeEnforced = kmParamSet2Hidl(response.enforced);
+ resultCharacteristics.softwareEnforced = kmParamSet2Hidl(response.unenforced);
+ }
+ _hidl_cb(legacy_enum_conversion(response.error), resultKeyBlob, resultCharacteristics);
+ return Void();
+}
+
+Return<void> TrustyKeymaster3Device::exportKey(KeyFormat exportFormat,
+ const hidl_vec<uint8_t>& keyBlob,
+ const hidl_vec<uint8_t>& clientId,
+ const hidl_vec<uint8_t>& appData,
+ exportKey_cb _hidl_cb) {
+ ExportKeyRequest request;
+ request.key_format = legacy_enum_conversion(exportFormat);
+ request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
+ addClientAndAppData(clientId, appData, &request.additional_params);
+
+ ExportKeyResponse response;
+ impl_->ExportKey(request, &response);
+
+ hidl_vec<uint8_t> resultKeyBlob;
+ if (response.error == KM_ERROR_OK) {
+ resultKeyBlob.setToExternal(response.key_data, response.key_data_length);
+ }
+ _hidl_cb(legacy_enum_conversion(response.error), resultKeyBlob);
+ return Void();
+}
+
+Return<void> TrustyKeymaster3Device::attestKey(const hidl_vec<uint8_t>& keyToAttest,
+ const hidl_vec<KeyParameter>& attestParams,
+ attestKey_cb _hidl_cb) {
+ AttestKeyRequest request;
+ request.SetKeyMaterial(keyToAttest.data(), keyToAttest.size());
+ request.attest_params.Reinitialize(KmParamSet(attestParams));
+
+ AttestKeyResponse response;
+ impl_->AttestKey(request, &response);
+
+ hidl_vec<hidl_vec<uint8_t>> resultCertChain;
+ if (response.error == KM_ERROR_OK) {
+ resultCertChain = kmCertChain2Hidl(response.certificate_chain);
+ }
+ _hidl_cb(legacy_enum_conversion(response.error), resultCertChain);
+ return Void();
+}
+
+Return<void> TrustyKeymaster3Device::upgradeKey(const hidl_vec<uint8_t>& keyBlobToUpgrade,
+ const hidl_vec<KeyParameter>& upgradeParams,
+ upgradeKey_cb _hidl_cb) {
+ UpgradeKeyRequest request;
+ request.SetKeyMaterial(keyBlobToUpgrade.data(), keyBlobToUpgrade.size());
+ request.upgrade_params.Reinitialize(KmParamSet(upgradeParams));
+
+ UpgradeKeyResponse response;
+ impl_->UpgradeKey(request, &response);
+
+ if (response.error == KM_ERROR_OK) {
+ _hidl_cb(ErrorCode::OK, kmBlob2hidlVec(response.upgraded_key));
+ } else {
+ _hidl_cb(legacy_enum_conversion(response.error), hidl_vec<uint8_t>());
+ }
+ return Void();
+}
+
+Return<ErrorCode> TrustyKeymaster3Device::deleteKey(const hidl_vec<uint8_t>& keyBlob) {
+ DeleteKeyRequest request;
+ request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
+
+ DeleteKeyResponse response;
+ impl_->DeleteKey(request, &response);
+
+ return legacy_enum_conversion(response.error);
+}
+
+Return<ErrorCode> TrustyKeymaster3Device::deleteAllKeys() {
+ DeleteAllKeysRequest request;
+ DeleteAllKeysResponse response;
+ impl_->DeleteAllKeys(request, &response);
+
+ return legacy_enum_conversion(response.error);
+}
+
+Return<ErrorCode> TrustyKeymaster3Device::destroyAttestationIds() {
+ return ErrorCode::UNIMPLEMENTED;
+}
+
+Return<void> TrustyKeymaster3Device::begin(KeyPurpose purpose, const hidl_vec<uint8_t>& key,
+ const hidl_vec<KeyParameter>& inParams,
+ begin_cb _hidl_cb) {
+ BeginOperationRequest request;
+ request.purpose = legacy_enum_conversion(purpose);
+ request.SetKeyMaterial(key.data(), key.size());
+ request.additional_params.Reinitialize(KmParamSet(inParams));
+
+ BeginOperationResponse response;
+ impl_->BeginOperation(request, &response);
+
+ hidl_vec<KeyParameter> resultParams;
+ if (response.error == KM_ERROR_OK) {
+ resultParams = kmParamSet2Hidl(response.output_params);
+ }
+
+ _hidl_cb(legacy_enum_conversion(response.error), resultParams, response.op_handle);
+ return Void();
+}
+
+Return<void> TrustyKeymaster3Device::update(uint64_t operationHandle,
+ const hidl_vec<KeyParameter>& inParams,
+ const hidl_vec<uint8_t>& input, update_cb _hidl_cb) {
+ UpdateOperationRequest request;
+ request.op_handle = operationHandle;
+ request.input.Reinitialize(input.data(), input.size());
+ request.additional_params.Reinitialize(KmParamSet(inParams));
+
+ UpdateOperationResponse response;
+ impl_->UpdateOperation(request, &response);
+
+ uint32_t resultConsumed = 0;
+ hidl_vec<KeyParameter> resultParams;
+ hidl_vec<uint8_t> resultBlob;
+ if (response.error == KM_ERROR_OK) {
+ resultConsumed = response.input_consumed;
+ resultParams = kmParamSet2Hidl(response.output_params);
+ resultBlob = kmBuffer2hidlVec(response.output);
+ }
+ _hidl_cb(legacy_enum_conversion(response.error), resultConsumed, resultParams, resultBlob);
+ return Void();
+}
+
+Return<void> TrustyKeymaster3Device::finish(uint64_t operationHandle,
+ const hidl_vec<KeyParameter>& inParams,
+ const hidl_vec<uint8_t>& input,
+ const hidl_vec<uint8_t>& signature,
+ finish_cb _hidl_cb) {
+ FinishOperationRequest request;
+ request.op_handle = operationHandle;
+ request.input.Reinitialize(input.data(), input.size());
+ request.signature.Reinitialize(signature.data(), signature.size());
+ request.additional_params.Reinitialize(KmParamSet(inParams));
+
+ FinishOperationResponse response;
+ impl_->FinishOperation(request, &response);
+
+ hidl_vec<KeyParameter> resultParams;
+ hidl_vec<uint8_t> resultBlob;
+ if (response.error == KM_ERROR_OK) {
+ resultParams = kmParamSet2Hidl(response.output_params);
+ resultBlob = kmBuffer2hidlVec(response.output);
+ }
+ _hidl_cb(legacy_enum_conversion(response.error), resultParams, resultBlob);
+ return Void();
+}
+
+Return<ErrorCode> TrustyKeymaster3Device::abort(uint64_t operationHandle) {
+ AbortOperationRequest request;
+ request.op_handle = operationHandle;
+
+ AbortOperationResponse response;
+ impl_->AbortOperation(request, &response);
+
+ return legacy_enum_conversion(response.error);
+}
+} // namespace keymaster
diff --git a/trusty/keymaster/3.0/android.hardware.keymaster@3.0-service.trusty.rc b/trusty/keymaster/3.0/android.hardware.keymaster@3.0-service.trusty.rc
new file mode 100644
index 0000000..e9d3054
--- /dev/null
+++ b/trusty/keymaster/3.0/android.hardware.keymaster@3.0-service.trusty.rc
@@ -0,0 +1,4 @@
+service vendor.keymaster-3-0 /vendor/bin/hw/android.hardware.keymaster@3.0-service.trusty
+ class early_hal
+ user nobody
+ group system drmrpc
diff --git a/trusty/keymaster/3.0/service.cpp b/trusty/keymaster/3.0/service.cpp
new file mode 100644
index 0000000..0d8436e
--- /dev/null
+++ b/trusty/keymaster/3.0/service.cpp
@@ -0,0 +1,43 @@
+/*
+**
+** Copyright 2018, 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/logging.h>
+#include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
+#include <hidl/HidlTransportSupport.h>
+#include <trusty_keymaster/TrustyKeymaster.h>
+#include <trusty_keymaster/TrustyKeymaster3Device.h>
+
+int main() {
+ ::android::hardware::configureRpcThreadpool(1, true);
+ auto trustyKeymaster = new keymaster::TrustyKeymaster();
+ int err = trustyKeymaster->Initialize();
+ if (err != 0) {
+ LOG(FATAL) << "Could not initialize TrustyKeymaster (" << err << ")";
+ return -1;
+ }
+
+ auto keymaster = new ::keymaster::TrustyKeymaster3Device(trustyKeymaster);
+
+ auto status = keymaster->registerAsService();
+ if (status != android::OK) {
+ LOG(FATAL) << "Could not register service for Keymaster 3.0 (" << status << ")";
+ return -1;
+ }
+
+ android::hardware::joinRpcThreadpool();
+ return -1; // Should never get here.
+}
diff --git a/trusty/keymaster/Android.bp b/trusty/keymaster/Android.bp
index 5428e73..819851f 100644
--- a/trusty/keymaster/Android.bp
+++ b/trusty/keymaster/Android.bp
@@ -27,19 +27,21 @@
name: "trusty_keymaster_tipc",
vendor: true,
srcs: [
- "trusty_keymaster_device.cpp",
- "trusty_keymaster_ipc.cpp",
- "trusty_keymaster_main.cpp",
+ "ipc/trusty_keymaster_ipc.cpp",
+ "legacy/trusty_keymaster_device.cpp",
+ "legacy/trusty_keymaster_main.cpp",
],
cflags: [
"-Wall",
"-Werror",
],
+
+ local_include_dirs: ["include"],
+
shared_libs: [
"libcrypto",
"libcutils",
"libkeymaster_portable",
- "libkeymaster_staging",
"libtrusty",
"libkeymaster_messages",
"libsoftkeymasterdevice",
@@ -53,9 +55,9 @@
vendor: true,
relative_install_path: "hw",
srcs: [
- "module.cpp",
- "trusty_keymaster_ipc.cpp",
- "trusty_keymaster_device.cpp",
+ "ipc/trusty_keymaster_ipc.cpp",
+ "legacy/module.cpp",
+ "legacy/trusty_keymaster_device.cpp",
],
cflags: [
@@ -64,6 +66,8 @@
"-Werror",
],
+ local_include_dirs: ["include"],
+
shared_libs: [
"libcrypto",
"libkeymaster_messages",
@@ -73,3 +77,34 @@
],
header_libs: ["libhardware_headers"],
}
+
+cc_binary {
+ name: "android.hardware.keymaster@3.0-service.trusty",
+ defaults: ["hidl_defaults"],
+ relative_install_path: "hw",
+ vendor: true,
+ init_rc: ["3.0/android.hardware.keymaster@3.0-service.trusty.rc"],
+ srcs: [
+ "3.0/service.cpp",
+ "3.0/TrustyKeymaster3Device.cpp",
+ "ipc/trusty_keymaster_ipc.cpp",
+ "TrustyKeymaster.cpp",
+ ],
+
+ local_include_dirs: ["include"],
+
+ shared_libs: [
+ "liblog",
+ "libcutils",
+ "libdl",
+ "libbase",
+ "libutils",
+ "libhardware",
+ "libhidlbase",
+ "libhidltransport",
+ "libtrusty",
+ "libkeymaster_messages",
+ "libkeymaster3device",
+ "android.hardware.keymaster@3.0"
+ ],
+}
diff --git a/trusty/keymaster/TrustyKeymaster.cpp b/trusty/keymaster/TrustyKeymaster.cpp
new file mode 100644
index 0000000..7f5e87f
--- /dev/null
+++ b/trusty/keymaster/TrustyKeymaster.cpp
@@ -0,0 +1,196 @@
+/*
+ * Copyright 2018 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 <cutils/log.h>
+#include <keymaster/android_keymaster_messages.h>
+#include <keymaster/keymaster_configuration.h>
+#include <trusty_keymaster/TrustyKeymaster.h>
+#include <trusty_keymaster/ipc/trusty_keymaster_ipc.h>
+
+namespace keymaster {
+
+int TrustyKeymaster::Initialize() {
+ int err;
+
+ err = trusty_keymaster_connect();
+ if (err) {
+ ALOGE("Failed to connect to trusty keymaster %d", err);
+ return err;
+ }
+
+ ConfigureRequest req;
+ req.os_version = GetOsVersion();
+ req.os_patchlevel = GetOsPatchlevel();
+
+ ConfigureResponse rsp;
+ Configure(req, &rsp);
+
+ if (rsp.error != KM_ERROR_OK) {
+ ALOGE("Failed to configure keymaster %d", rsp.error);
+ return -1;
+ }
+
+ return 0;
+}
+
+TrustyKeymaster::TrustyKeymaster() {}
+
+TrustyKeymaster::~TrustyKeymaster() {
+ trusty_keymaster_disconnect();
+}
+
+static void ForwardCommand(enum keymaster_command command, const Serializable& req,
+ KeymasterResponse* rsp) {
+ keymaster_error_t err;
+ err = trusty_keymaster_send(command, req, rsp);
+ if (err != KM_ERROR_OK) {
+ ALOGE("Failed to send cmd %d err: %d", command, err);
+ rsp->error = err;
+ }
+}
+
+void TrustyKeymaster::GetVersion(const GetVersionRequest& request, GetVersionResponse* response) {
+ ForwardCommand(KM_GET_VERSION, request, response);
+}
+
+void TrustyKeymaster::SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
+ SupportedAlgorithmsResponse* response) {
+ ForwardCommand(KM_GET_SUPPORTED_ALGORITHMS, request, response);
+}
+
+void TrustyKeymaster::SupportedBlockModes(const SupportedBlockModesRequest& request,
+ SupportedBlockModesResponse* response) {
+ ForwardCommand(KM_GET_SUPPORTED_BLOCK_MODES, request, response);
+}
+
+void TrustyKeymaster::SupportedPaddingModes(const SupportedPaddingModesRequest& request,
+ SupportedPaddingModesResponse* response) {
+ ForwardCommand(KM_GET_SUPPORTED_PADDING_MODES, request, response);
+}
+
+void TrustyKeymaster::SupportedDigests(const SupportedDigestsRequest& request,
+ SupportedDigestsResponse* response) {
+ ForwardCommand(KM_GET_SUPPORTED_DIGESTS, request, response);
+}
+
+void TrustyKeymaster::SupportedImportFormats(const SupportedImportFormatsRequest& request,
+ SupportedImportFormatsResponse* response) {
+ ForwardCommand(KM_GET_SUPPORTED_IMPORT_FORMATS, request, response);
+}
+
+void TrustyKeymaster::SupportedExportFormats(const SupportedExportFormatsRequest& request,
+ SupportedExportFormatsResponse* response) {
+ ForwardCommand(KM_GET_SUPPORTED_EXPORT_FORMATS, request, response);
+}
+
+void TrustyKeymaster::AddRngEntropy(const AddEntropyRequest& request,
+ AddEntropyResponse* response) {
+ ForwardCommand(KM_ADD_RNG_ENTROPY, request, response);
+}
+
+void TrustyKeymaster::Configure(const ConfigureRequest& request, ConfigureResponse* response) {
+ ForwardCommand(KM_CONFIGURE, request, response);
+}
+
+void TrustyKeymaster::GenerateKey(const GenerateKeyRequest& request,
+ GenerateKeyResponse* response) {
+ GenerateKeyRequest datedRequest(request.message_version);
+ datedRequest.key_description = request.key_description;
+
+ if (!request.key_description.Contains(TAG_CREATION_DATETIME)) {
+ datedRequest.key_description.push_back(TAG_CREATION_DATETIME, java_time(time(NULL)));
+ }
+
+ ForwardCommand(KM_GENERATE_KEY, datedRequest, response);
+}
+
+void TrustyKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
+ GetKeyCharacteristicsResponse* response) {
+ ForwardCommand(KM_GET_KEY_CHARACTERISTICS, request, response);
+}
+
+void TrustyKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
+ ForwardCommand(KM_IMPORT_KEY, request, response);
+}
+
+void TrustyKeymaster::ImportWrappedKey(const ImportWrappedKeyRequest& request,
+ ImportWrappedKeyResponse* response) {
+ ForwardCommand(KM_IMPORT_WRAPPED_KEY, request, response);
+}
+
+void TrustyKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
+ ForwardCommand(KM_EXPORT_KEY, request, response);
+}
+
+void TrustyKeymaster::AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response) {
+ ForwardCommand(KM_ATTEST_KEY, request, response);
+}
+
+void TrustyKeymaster::UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response) {
+ ForwardCommand(KM_UPGRADE_KEY, request, response);
+}
+
+void TrustyKeymaster::DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response) {
+ ForwardCommand(KM_DELETE_KEY, request, response);
+}
+
+void TrustyKeymaster::DeleteAllKeys(const DeleteAllKeysRequest& request,
+ DeleteAllKeysResponse* response) {
+ ForwardCommand(KM_DELETE_ALL_KEYS, request, response);
+}
+
+void TrustyKeymaster::BeginOperation(const BeginOperationRequest& request,
+ BeginOperationResponse* response) {
+ ForwardCommand(KM_BEGIN_OPERATION, request, response);
+}
+
+void TrustyKeymaster::UpdateOperation(const UpdateOperationRequest& request,
+ UpdateOperationResponse* response) {
+ ForwardCommand(KM_UPDATE_OPERATION, request, response);
+}
+
+void TrustyKeymaster::FinishOperation(const FinishOperationRequest& request,
+ FinishOperationResponse* response) {
+ ForwardCommand(KM_FINISH_OPERATION, request, response);
+}
+
+void TrustyKeymaster::AbortOperation(const AbortOperationRequest& request,
+ AbortOperationResponse* response) {
+ ForwardCommand(KM_ABORT_OPERATION, request, response);
+}
+
+/* Methods for Keymaster 4.0 functionality -- not yet implemented */
+GetHmacSharingParametersResponse TrustyKeymaster::GetHmacSharingParameters() {
+ GetHmacSharingParametersResponse response;
+ response.error = KM_ERROR_UNIMPLEMENTED;
+ return response;
+}
+
+ComputeSharedHmacResponse TrustyKeymaster::ComputeSharedHmac(
+ const ComputeSharedHmacRequest& /* request */) {
+ ComputeSharedHmacResponse response;
+ response.error = KM_ERROR_UNIMPLEMENTED;
+ return response;
+}
+
+VerifyAuthorizationResponse TrustyKeymaster::VerifyAuthorization(
+ const VerifyAuthorizationRequest& /* request */) {
+ VerifyAuthorizationResponse response;
+ response.error = KM_ERROR_UNIMPLEMENTED;
+ return response;
+}
+
+} // namespace keymaster
diff --git a/trusty/keymaster/include/trusty_keymaster/TrustyKeymaster.h b/trusty/keymaster/include/trusty_keymaster/TrustyKeymaster.h
new file mode 100644
index 0000000..030b645
--- /dev/null
+++ b/trusty/keymaster/include/trusty_keymaster/TrustyKeymaster.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2018 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.
+ */
+
+#ifndef TRUSTY_KEYMASTER_H_
+#define TRUSTY_KEYMASTER_H_
+
+#include <keymaster/android_keymaster_messages.h>
+
+namespace keymaster {
+
+class TrustyKeymaster {
+ public:
+ TrustyKeymaster();
+ ~TrustyKeymaster();
+ int Initialize();
+ void GetVersion(const GetVersionRequest& request, GetVersionResponse* response);
+ void SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
+ SupportedAlgorithmsResponse* response);
+ void SupportedBlockModes(const SupportedBlockModesRequest& request,
+ SupportedBlockModesResponse* response);
+ void SupportedPaddingModes(const SupportedPaddingModesRequest& request,
+ SupportedPaddingModesResponse* response);
+ void SupportedDigests(const SupportedDigestsRequest& request,
+ SupportedDigestsResponse* response);
+ void SupportedImportFormats(const SupportedImportFormatsRequest& request,
+ SupportedImportFormatsResponse* response);
+ void SupportedExportFormats(const SupportedExportFormatsRequest& request,
+ SupportedExportFormatsResponse* response);
+ void AddRngEntropy(const AddEntropyRequest& request, AddEntropyResponse* response);
+ void Configure(const ConfigureRequest& request, ConfigureResponse* response);
+ void GenerateKey(const GenerateKeyRequest& request, GenerateKeyResponse* response);
+ void GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
+ GetKeyCharacteristicsResponse* response);
+ void ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response);
+ void ImportWrappedKey(const ImportWrappedKeyRequest& request,
+ ImportWrappedKeyResponse* response);
+ void ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response);
+ void AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response);
+ void UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response);
+ void DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response);
+ void DeleteAllKeys(const DeleteAllKeysRequest& request, DeleteAllKeysResponse* response);
+ void BeginOperation(const BeginOperationRequest& request, BeginOperationResponse* response);
+ void UpdateOperation(const UpdateOperationRequest& request, UpdateOperationResponse* response);
+ void FinishOperation(const FinishOperationRequest& request, FinishOperationResponse* response);
+ void AbortOperation(const AbortOperationRequest& request, AbortOperationResponse* response);
+ GetHmacSharingParametersResponse GetHmacSharingParameters();
+ ComputeSharedHmacResponse ComputeSharedHmac(const ComputeSharedHmacRequest& request);
+ VerifyAuthorizationResponse VerifyAuthorization(const VerifyAuthorizationRequest& request);
+};
+
+} // namespace keymaster
+
+#endif // TRUSTY_KEYMASTER_H_
diff --git a/trusty/keymaster/include/trusty_keymaster/TrustyKeymaster3Device.h b/trusty/keymaster/include/trusty_keymaster/TrustyKeymaster3Device.h
new file mode 100644
index 0000000..6fc79ce
--- /dev/null
+++ b/trusty/keymaster/include/trusty_keymaster/TrustyKeymaster3Device.h
@@ -0,0 +1,84 @@
+/*
+ **
+ ** Copyright 2018, 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.
+ */
+
+#ifndef HIDL_android_hardware_keymaster_V3_0_TrustyKeymaster3Device_H_
+#define HIDL_android_hardware_keymaster_V3_0_TrustyKeymaster3Device_H_
+
+#include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
+
+#include <hidl/MQDescriptor.h>
+#include <hidl/Status.h>
+
+#include <trusty_keymaster/TrustyKeymaster.h>
+
+namespace keymaster {
+
+using ::android::sp;
+using ::android::hardware::hidl_string;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::hardware::keymaster::V3_0::ErrorCode;
+using ::android::hardware::keymaster::V3_0::IKeymasterDevice;
+using ::android::hardware::keymaster::V3_0::KeyCharacteristics;
+using ::android::hardware::keymaster::V3_0::KeyFormat;
+using ::android::hardware::keymaster::V3_0::KeyParameter;
+using ::android::hardware::keymaster::V3_0::KeyPurpose;
+
+class TrustyKeymaster3Device : public IKeymasterDevice {
+ public:
+ TrustyKeymaster3Device(TrustyKeymaster* impl);
+ virtual ~TrustyKeymaster3Device();
+
+ Return<void> getHardwareFeatures(getHardwareFeatures_cb _hidl_cb);
+ Return<ErrorCode> addRngEntropy(const hidl_vec<uint8_t>& data) override;
+ Return<void> generateKey(const hidl_vec<KeyParameter>& keyParams,
+ generateKey_cb _hidl_cb) override;
+ Return<void> getKeyCharacteristics(const hidl_vec<uint8_t>& keyBlob,
+ const hidl_vec<uint8_t>& clientId,
+ const hidl_vec<uint8_t>& appData,
+ getKeyCharacteristics_cb _hidl_cb) override;
+ Return<void> importKey(const hidl_vec<KeyParameter>& params, KeyFormat keyFormat,
+ const hidl_vec<uint8_t>& keyData, importKey_cb _hidl_cb) override;
+ Return<void> exportKey(KeyFormat exportFormat, const hidl_vec<uint8_t>& keyBlob,
+ const hidl_vec<uint8_t>& clientId, const hidl_vec<uint8_t>& appData,
+ exportKey_cb _hidl_cb) override;
+ Return<void> attestKey(const hidl_vec<uint8_t>& keyToAttest,
+ const hidl_vec<KeyParameter>& attestParams,
+ attestKey_cb _hidl_cb) override;
+ Return<void> upgradeKey(const hidl_vec<uint8_t>& keyBlobToUpgrade,
+ const hidl_vec<KeyParameter>& upgradeParams,
+ upgradeKey_cb _hidl_cb) override;
+ Return<ErrorCode> deleteKey(const hidl_vec<uint8_t>& keyBlob) override;
+ Return<ErrorCode> deleteAllKeys() override;
+ Return<ErrorCode> destroyAttestationIds() override;
+ Return<void> begin(KeyPurpose purpose, const hidl_vec<uint8_t>& key,
+ const hidl_vec<KeyParameter>& inParams, begin_cb _hidl_cb) override;
+ Return<void> update(uint64_t operationHandle, const hidl_vec<KeyParameter>& inParams,
+ const hidl_vec<uint8_t>& input, update_cb _hidl_cb) override;
+ Return<void> finish(uint64_t operationHandle, const hidl_vec<KeyParameter>& inParams,
+ const hidl_vec<uint8_t>& input, const hidl_vec<uint8_t>& signature,
+ finish_cb _hidl_cb) override;
+ Return<ErrorCode> abort(uint64_t operationHandle) override;
+
+ private:
+ std::unique_ptr<TrustyKeymaster> impl_;
+};
+
+} // namespace keymaster
+
+#endif // HIDL_android_hardware_keymaster_V3_0_TrustyKeymaster3Device_H_
diff --git a/trusty/keymaster/keymaster_ipc.h b/trusty/keymaster/include/trusty_keymaster/ipc/keymaster_ipc.h
similarity index 83%
rename from trusty/keymaster/keymaster_ipc.h
rename to trusty/keymaster/include/trusty_keymaster/ipc/keymaster_ipc.h
index d63757b..13e6725 100644
--- a/trusty/keymaster/keymaster_ipc.h
+++ b/trusty/keymaster/include/trusty_keymaster/ipc/keymaster_ipc.h
@@ -46,6 +46,13 @@
KM_ATTEST_KEY = (16 << KEYMASTER_REQ_SHIFT),
KM_UPGRADE_KEY = (17 << KEYMASTER_REQ_SHIFT),
KM_CONFIGURE = (18 << KEYMASTER_REQ_SHIFT),
+ KM_GET_HMAC_SHARING_PARAMETERS = (19 << KEYMASTER_REQ_SHIFT),
+ KM_COMPUTE_SHARED_HMAC = (20 << KEYMASTER_REQ_SHIFT),
+ KM_VERIFY_AUTHORIZATION = (21 << KEYMASTER_REQ_SHIFT),
+ KM_DELETE_KEY = (22 << KEYMASTER_REQ_SHIFT),
+ KM_DELETE_ALL_KEYS = (23 << KEYMASTER_REQ_SHIFT),
+ KM_DESTROY_ATTESTATION_IDS = (24 << KEYMASTER_REQ_SHIFT),
+ KM_IMPORT_WRAPPED_KEY = (25 << KEYMASTER_REQ_SHIFT),
};
#ifdef __ANDROID__
diff --git a/trusty/keymaster/trusty_keymaster_ipc.h b/trusty/keymaster/include/trusty_keymaster/ipc/trusty_keymaster_ipc.h
similarity index 66%
rename from trusty/keymaster/trusty_keymaster_ipc.h
rename to trusty/keymaster/include/trusty_keymaster/ipc/trusty_keymaster_ipc.h
index c15f7c1..16207e6 100644
--- a/trusty/keymaster/trusty_keymaster_ipc.h
+++ b/trusty/keymaster/include/trusty_keymaster/ipc/trusty_keymaster_ipc.h
@@ -17,13 +17,24 @@
#ifndef TRUSTY_KEYMASTER_TRUSTY_KEYMASTER_IPC_H_
#define TRUSTY_KEYMASTER_TRUSTY_KEYMASTER_IPC_H_
+#include <keymaster/android_keymaster_messages.h>
+#include <trusty_keymaster/ipc/keymaster_ipc.h>
+
__BEGIN_DECLS
+const uint32_t TRUSTY_KEYMASTER_RECV_BUF_SIZE = 2 * PAGE_SIZE;
+const uint32_t TRUSTY_KEYMASTER_SEND_BUF_SIZE =
+ (PAGE_SIZE - sizeof(struct keymaster_message) - 16 /* tipc header */);
+
int trusty_keymaster_connect(void);
int trusty_keymaster_call(uint32_t cmd, void* in, uint32_t in_size, uint8_t* out,
uint32_t* out_size);
void trusty_keymaster_disconnect(void);
+keymaster_error_t translate_error(int err);
+keymaster_error_t trusty_keymaster_send(uint32_t command, const keymaster::Serializable& req,
+ keymaster::KeymasterResponse* rsp);
+
__END_DECLS
#endif // TRUSTY_KEYMASTER_TRUSTY_KEYMASTER_IPC_H_
diff --git a/trusty/keymaster/trusty_keymaster_device.h b/trusty/keymaster/include/trusty_keymaster/legacy/trusty_keymaster_device.h
similarity index 96%
rename from trusty/keymaster/trusty_keymaster_device.h
rename to trusty/keymaster/include/trusty_keymaster/legacy/trusty_keymaster_device.h
index cfada1b..5a80795 100644
--- a/trusty/keymaster/trusty_keymaster_device.h
+++ b/trusty/keymaster/include/trusty_keymaster/legacy/trusty_keymaster_device.h
@@ -36,7 +36,8 @@
* These are the only symbols that will be exported by libtrustykeymaster. All functionality
* can be reached via the function pointers in device_.
*/
- __attribute__((visibility("default"))) explicit TrustyKeymasterDevice(const hw_module_t* module);
+ __attribute__((visibility("default"))) explicit TrustyKeymasterDevice(
+ const hw_module_t* module);
__attribute__((visibility("default"))) hw_device_t* hw_device();
~TrustyKeymasterDevice();
@@ -134,12 +135,15 @@
keymaster_operation_handle_t operation_handle,
const keymaster_key_param_set_t* in_params,
const keymaster_blob_t* input, size_t* input_consumed,
- keymaster_key_param_set_t* out_params, keymaster_blob_t* output);
+ keymaster_key_param_set_t* out_params,
+ keymaster_blob_t* output);
static keymaster_error_t finish(const keymaster2_device_t* dev,
keymaster_operation_handle_t operation_handle,
const keymaster_key_param_set_t* in_params,
- const keymaster_blob_t* input, const keymaster_blob_t* signature,
- keymaster_key_param_set_t* out_params, keymaster_blob_t* output);
+ const keymaster_blob_t* input,
+ const keymaster_blob_t* signature,
+ keymaster_key_param_set_t* out_params,
+ keymaster_blob_t* output);
static keymaster_error_t abort(const keymaster2_device_t* dev,
keymaster_operation_handle_t operation_handle);
diff --git a/trusty/keymaster/ipc/trusty_keymaster_ipc.cpp b/trusty/keymaster/ipc/trusty_keymaster_ipc.cpp
new file mode 100644
index 0000000..8c5cff6
--- /dev/null
+++ b/trusty/keymaster/ipc/trusty_keymaster_ipc.cpp
@@ -0,0 +1,182 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#define LOG_TAG "TrustyKeymaster"
+
+// TODO: make this generic in libtrusty
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/uio.h>
+#include <unistd.h>
+
+#include <algorithm>
+
+#include <log/log.h>
+#include <trusty/tipc.h>
+
+#include <trusty_keymaster/ipc/keymaster_ipc.h>
+#include <trusty_keymaster/ipc/trusty_keymaster_ipc.h>
+
+#define TRUSTY_DEVICE_NAME "/dev/trusty-ipc-dev0"
+
+static int handle_ = -1;
+
+int trusty_keymaster_connect() {
+ int rc = tipc_connect(TRUSTY_DEVICE_NAME, KEYMASTER_PORT);
+ if (rc < 0) {
+ return rc;
+ }
+
+ handle_ = rc;
+ return 0;
+}
+
+int trusty_keymaster_call(uint32_t cmd, void* in, uint32_t in_size, uint8_t* out,
+ uint32_t* out_size) {
+ if (handle_ < 0) {
+ ALOGE("not connected\n");
+ return -EINVAL;
+ }
+
+ size_t msg_size = in_size + sizeof(struct keymaster_message);
+ struct keymaster_message* msg = reinterpret_cast<struct keymaster_message*>(malloc(msg_size));
+ if (!msg) {
+ ALOGE("failed to allocate msg buffer\n");
+ return -EINVAL;
+ }
+
+ msg->cmd = cmd;
+ memcpy(msg->payload, in, in_size);
+
+ ssize_t rc = write(handle_, msg, msg_size);
+ free(msg);
+
+ if (rc < 0) {
+ ALOGE("failed to send cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT, strerror(errno));
+ return -errno;
+ }
+ size_t out_max_size = *out_size;
+ *out_size = 0;
+ struct iovec iov[2];
+ struct keymaster_message header;
+ iov[0] = {.iov_base = &header, .iov_len = sizeof(struct keymaster_message)};
+ while (true) {
+ iov[1] = {.iov_base = out + *out_size,
+ .iov_len = std::min<uint32_t>(KEYMASTER_MAX_BUFFER_LENGTH,
+ out_max_size - *out_size)};
+ rc = readv(handle_, iov, 2);
+ if (rc < 0) {
+ ALOGE("failed to retrieve response for cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT,
+ strerror(errno));
+ return -errno;
+ }
+
+ if ((size_t)rc < sizeof(struct keymaster_message)) {
+ ALOGE("invalid response size (%d)\n", (int)rc);
+ return -EINVAL;
+ }
+
+ if ((cmd | KEYMASTER_RESP_BIT) != (header.cmd & ~(KEYMASTER_STOP_BIT))) {
+ ALOGE("invalid command (%d)", header.cmd);
+ return -EINVAL;
+ }
+ *out_size += ((size_t)rc - sizeof(struct keymaster_message));
+ if (header.cmd & KEYMASTER_STOP_BIT) {
+ break;
+ }
+ }
+
+ return rc;
+}
+
+void trusty_keymaster_disconnect() {
+ if (handle_ >= 0) {
+ tipc_close(handle_);
+ }
+ handle_ = -1;
+}
+
+keymaster_error_t translate_error(int err) {
+ switch (err) {
+ case 0:
+ return KM_ERROR_OK;
+ case -EPERM:
+ case -EACCES:
+ return KM_ERROR_SECURE_HW_ACCESS_DENIED;
+
+ case -ECANCELED:
+ return KM_ERROR_OPERATION_CANCELLED;
+
+ case -ENODEV:
+ return KM_ERROR_UNIMPLEMENTED;
+
+ case -ENOMEM:
+ return KM_ERROR_MEMORY_ALLOCATION_FAILED;
+
+ case -EBUSY:
+ return KM_ERROR_SECURE_HW_BUSY;
+
+ case -EIO:
+ return KM_ERROR_SECURE_HW_COMMUNICATION_FAILED;
+
+ case -EOVERFLOW:
+ return KM_ERROR_INVALID_INPUT_LENGTH;
+
+ default:
+ return KM_ERROR_UNKNOWN_ERROR;
+ }
+}
+
+keymaster_error_t trusty_keymaster_send(uint32_t command, const keymaster::Serializable& req,
+ keymaster::KeymasterResponse* rsp) {
+ uint32_t req_size = req.SerializedSize();
+ if (req_size > TRUSTY_KEYMASTER_SEND_BUF_SIZE) {
+ ALOGE("Request too big: %u Max size: %u", req_size, TRUSTY_KEYMASTER_SEND_BUF_SIZE);
+ return KM_ERROR_INVALID_INPUT_LENGTH;
+ }
+
+ uint8_t send_buf[TRUSTY_KEYMASTER_SEND_BUF_SIZE];
+ keymaster::Eraser send_buf_eraser(send_buf, TRUSTY_KEYMASTER_SEND_BUF_SIZE);
+ req.Serialize(send_buf, send_buf + req_size);
+
+ // Send it
+ uint8_t recv_buf[TRUSTY_KEYMASTER_RECV_BUF_SIZE];
+ keymaster::Eraser recv_buf_eraser(recv_buf, TRUSTY_KEYMASTER_RECV_BUF_SIZE);
+ uint32_t rsp_size = TRUSTY_KEYMASTER_RECV_BUF_SIZE;
+ int rc = trusty_keymaster_call(command, send_buf, req_size, recv_buf, &rsp_size);
+ if (rc < 0) {
+ // Reset the connection on tipc error
+ trusty_keymaster_disconnect();
+ trusty_keymaster_connect();
+ ALOGE("tipc error: %d\n", rc);
+ // TODO(swillden): Distinguish permanent from transient errors and set error_ appropriately.
+ return translate_error(rc);
+ } else {
+ ALOGE("Received %d byte response\n", rsp_size);
+ }
+
+ const uint8_t* p = recv_buf;
+ if (!rsp->Deserialize(&p, p + rsp_size)) {
+ ALOGE("Error deserializing response of size %d\n", (int)rsp_size);
+ return KM_ERROR_UNKNOWN_ERROR;
+ } else if (rsp->error != KM_ERROR_OK) {
+ ALOGE("Response of size %d contained error code %d\n", (int)rsp_size, (int)rsp->error);
+ return rsp->error;
+ }
+ return rsp->error;
+}
diff --git a/trusty/keymaster/Makefile b/trusty/keymaster/legacy/Makefile
similarity index 100%
rename from trusty/keymaster/Makefile
rename to trusty/keymaster/legacy/Makefile
diff --git a/trusty/keymaster/module.cpp b/trusty/keymaster/legacy/module.cpp
similarity index 65%
rename from trusty/keymaster/module.cpp
rename to trusty/keymaster/legacy/module.cpp
index b472680..7aa1a4e 100644
--- a/trusty/keymaster/module.cpp
+++ b/trusty/keymaster/legacy/module.cpp
@@ -19,14 +19,15 @@
#include <hardware/hardware.h>
#include <hardware/keymaster0.h>
-#include "trusty_keymaster_device.h"
+#include <trusty_keymaster/legacy/trusty_keymaster_device.h>
using keymaster::TrustyKeymasterDevice;
/*
* Generic device handling
*/
-static int trusty_keymaster_open(const hw_module_t* module, const char* name, hw_device_t** device) {
+static int trusty_keymaster_open(const hw_module_t* module, const char* name,
+ hw_device_t** device) {
if (strcmp(name, KEYSTORE_KEYMASTER) != 0) {
return -EINVAL;
}
@@ -42,20 +43,20 @@
}
static struct hw_module_methods_t keystore_module_methods = {
- .open = trusty_keymaster_open,
+ .open = trusty_keymaster_open,
};
struct keystore_module HAL_MODULE_INFO_SYM __attribute__((visibility("default"))) = {
- .common =
- {
- .tag = HARDWARE_MODULE_TAG,
- .module_api_version = KEYMASTER_MODULE_API_VERSION_2_0,
- .hal_api_version = HARDWARE_HAL_API_VERSION,
- .id = KEYSTORE_HARDWARE_MODULE_ID,
- .name = "Trusty Keymaster HAL",
- .author = "The Android Open Source Project",
- .methods = &keystore_module_methods,
- .dso = 0,
- .reserved = {},
- },
+ .common =
+ {
+ .tag = HARDWARE_MODULE_TAG,
+ .module_api_version = KEYMASTER_MODULE_API_VERSION_2_0,
+ .hal_api_version = HARDWARE_HAL_API_VERSION,
+ .id = KEYSTORE_HARDWARE_MODULE_ID,
+ .name = "Trusty Keymaster HAL",
+ .author = "The Android Open Source Project",
+ .methods = &keystore_module_methods,
+ .dso = 0,
+ .reserved = {},
+ },
};
diff --git a/trusty/keymaster/trusty_keymaster_device.cpp b/trusty/keymaster/legacy/trusty_keymaster_device.cpp
similarity index 79%
rename from trusty/keymaster/trusty_keymaster_device.cpp
rename to trusty/keymaster/legacy/trusty_keymaster_device.cpp
index b8c2032..ea00a92 100644
--- a/trusty/keymaster/trusty_keymaster_device.cpp
+++ b/trusty/keymaster/legacy/trusty_keymaster_device.cpp
@@ -33,50 +33,15 @@
#include <keymaster/authorization_set.h>
#include <log/log.h>
-#include "keymaster_ipc.h"
-#include "trusty_keymaster_device.h"
-#include "trusty_keymaster_ipc.h"
-
-// Maximum size of message from Trusty is 8K (for RSA attestation key and chain)
-const uint32_t RECV_BUF_SIZE = 2*PAGE_SIZE;
-const uint32_t SEND_BUF_SIZE = (PAGE_SIZE - sizeof(struct keymaster_message) - 16 /* tipc header */);
+#include <trusty_keymaster/ipc/keymaster_ipc.h>
+#include <trusty_keymaster/ipc/trusty_keymaster_ipc.h>
+#include <trusty_keymaster/legacy/trusty_keymaster_device.h>
const size_t kMaximumAttestationChallengeLength = 128;
const size_t kMaximumFinishInputLength = 2048;
namespace keymaster {
-static keymaster_error_t translate_error(int err) {
- switch (err) {
- case 0:
- return KM_ERROR_OK;
- case -EPERM:
- case -EACCES:
- return KM_ERROR_SECURE_HW_ACCESS_DENIED;
-
- case -ECANCELED:
- return KM_ERROR_OPERATION_CANCELLED;
-
- case -ENODEV:
- return KM_ERROR_UNIMPLEMENTED;
-
- case -ENOMEM:
- return KM_ERROR_MEMORY_ALLOCATION_FAILED;
-
- case -EBUSY:
- return KM_ERROR_SECURE_HW_BUSY;
-
- case -EIO:
- return KM_ERROR_SECURE_HW_COMMUNICATION_FAILED;
-
- case -EOVERFLOW:
- return KM_ERROR_INVALID_INPUT_LENGTH;
-
- default:
- return KM_ERROR_UNKNOWN_ERROR;
- }
-}
-
TrustyKeymasterDevice::TrustyKeymasterDevice(const hw_module_t* module) {
static_assert(std::is_standard_layout<TrustyKeymasterDevice>::value,
"TrustyKeymasterDevice must be standard layout");
@@ -121,7 +86,7 @@
GetVersionRequest version_request;
GetVersionResponse version_response;
- error_ = Send(KM_GET_VERSION, version_request, &version_response);
+ error_ = trusty_keymaster_send(KM_GET_VERSION, version_request, &version_response);
if (error_ == KM_ERROR_INVALID_ARGUMENT || error_ == KM_ERROR_UNIMPLEMENTED) {
ALOGE("\"Bad parameters\" error on GetVersion call. Version 0 is not supported.");
error_ = KM_ERROR_VERSION_MISMATCH;
@@ -186,7 +151,7 @@
}
ConfigureResponse response(message_version_);
- keymaster_error_t err = Send(KM_CONFIGURE, request, &response);
+ keymaster_error_t err = trusty_keymaster_send(KM_CONFIGURE, request, &response);
if (err != KM_ERROR_OK) {
return err;
}
@@ -204,12 +169,12 @@
AddEntropyRequest request(message_version_);
request.random_data.Reinitialize(data, data_length);
AddEntropyResponse response(message_version_);
- return Send(KM_ADD_RNG_ENTROPY, request, &response);
+ return trusty_keymaster_send(KM_ADD_RNG_ENTROPY, request, &response);
}
keymaster_error_t TrustyKeymasterDevice::generate_key(
- const keymaster_key_param_set_t* params, keymaster_key_blob_t* key_blob,
- keymaster_key_characteristics_t* characteristics) {
+ const keymaster_key_param_set_t* params, keymaster_key_blob_t* key_blob,
+ keymaster_key_characteristics_t* characteristics) {
ALOGD("Device received generate_key");
if (error_ != KM_ERROR_OK) {
@@ -227,14 +192,14 @@
request.key_description.push_back(TAG_CREATION_DATETIME, java_time(time(NULL)));
GenerateKeyResponse response(message_version_);
- keymaster_error_t err = Send(KM_GENERATE_KEY, request, &response);
+ keymaster_error_t err = trusty_keymaster_send(KM_GENERATE_KEY, request, &response);
if (err != KM_ERROR_OK) {
return err;
}
key_blob->key_material_size = response.key_blob.key_material_size;
key_blob->key_material =
- DuplicateBuffer(response.key_blob.key_material, response.key_blob.key_material_size);
+ DuplicateBuffer(response.key_blob.key_material, response.key_blob.key_material_size);
if (!key_blob->key_material) {
return KM_ERROR_MEMORY_ALLOCATION_FAILED;
}
@@ -248,8 +213,8 @@
}
keymaster_error_t TrustyKeymasterDevice::get_key_characteristics(
- const keymaster_key_blob_t* key_blob, const keymaster_blob_t* client_id,
- const keymaster_blob_t* app_data, keymaster_key_characteristics_t* characteristics) {
+ const keymaster_key_blob_t* key_blob, const keymaster_blob_t* client_id,
+ const keymaster_blob_t* app_data, keymaster_key_characteristics_t* characteristics) {
ALOGD("Device received get_key_characteristics");
if (error_ != KM_ERROR_OK) {
@@ -267,7 +232,7 @@
AddClientAndAppData(client_id, app_data, &request);
GetKeyCharacteristicsResponse response(message_version_);
- keymaster_error_t err = Send(KM_GET_KEY_CHARACTERISTICS, request, &response);
+ keymaster_error_t err = trusty_keymaster_send(KM_GET_KEY_CHARACTERISTICS, request, &response);
if (err != KM_ERROR_OK) {
return err;
}
@@ -279,9 +244,9 @@
}
keymaster_error_t TrustyKeymasterDevice::import_key(
- const keymaster_key_param_set_t* params, keymaster_key_format_t key_format,
- const keymaster_blob_t* key_data, keymaster_key_blob_t* key_blob,
- keymaster_key_characteristics_t* characteristics) {
+ const keymaster_key_param_set_t* params, keymaster_key_format_t key_format,
+ const keymaster_blob_t* key_data, keymaster_key_blob_t* key_blob,
+ keymaster_key_characteristics_t* characteristics) {
ALOGD("Device received import_key");
if (error_ != KM_ERROR_OK) {
@@ -302,14 +267,14 @@
request.SetKeyMaterial(key_data->data, key_data->data_length);
ImportKeyResponse response(message_version_);
- keymaster_error_t err = Send(KM_IMPORT_KEY, request, &response);
+ keymaster_error_t err = trusty_keymaster_send(KM_IMPORT_KEY, request, &response);
if (err != KM_ERROR_OK) {
return err;
}
key_blob->key_material_size = response.key_blob.key_material_size;
key_blob->key_material =
- DuplicateBuffer(response.key_blob.key_material, response.key_blob.key_material_size);
+ DuplicateBuffer(response.key_blob.key_material, response.key_blob.key_material_size);
if (!key_blob->key_material) {
return KM_ERROR_MEMORY_ALLOCATION_FAILED;
}
@@ -348,7 +313,7 @@
AddClientAndAppData(client_id, app_data, &request);
ExportKeyResponse response(message_version_);
- keymaster_error_t err = Send(KM_EXPORT_KEY, request, &response);
+ keymaster_error_t err = trusty_keymaster_send(KM_EXPORT_KEY, request, &response);
if (err != KM_ERROR_OK) {
return err;
}
@@ -393,7 +358,7 @@
}
AttestKeyResponse response(message_version_);
- keymaster_error_t err = Send(KM_ATTEST_KEY, request, &response);
+ keymaster_error_t err = trusty_keymaster_send(KM_ATTEST_KEY, request, &response);
if (err != KM_ERROR_OK) {
return err;
}
@@ -401,7 +366,7 @@
// Allocate and clear storage for cert_chain.
keymaster_cert_chain_t& rsp_chain = response.certificate_chain;
cert_chain->entries = reinterpret_cast<keymaster_blob_t*>(
- malloc(rsp_chain.entry_count * sizeof(*cert_chain->entries)));
+ malloc(rsp_chain.entry_count * sizeof(*cert_chain->entries)));
if (!cert_chain->entries) {
return KM_ERROR_MEMORY_ALLOCATION_FAILED;
}
@@ -425,9 +390,9 @@
return KM_ERROR_OK;
}
-keymaster_error_t TrustyKeymasterDevice::upgrade_key(const keymaster_key_blob_t* key_to_upgrade,
- const keymaster_key_param_set_t* upgrade_params,
- keymaster_key_blob_t* upgraded_key) {
+keymaster_error_t TrustyKeymasterDevice::upgrade_key(
+ const keymaster_key_blob_t* key_to_upgrade, const keymaster_key_param_set_t* upgrade_params,
+ keymaster_key_blob_t* upgraded_key) {
ALOGD("Device received upgrade_key");
if (error_ != KM_ERROR_OK) {
@@ -445,7 +410,7 @@
request.upgrade_params.Reinitialize(*upgrade_params);
UpgradeKeyResponse response(message_version_);
- keymaster_error_t err = Send(KM_UPGRADE_KEY, request, &response);
+ keymaster_error_t err = trusty_keymaster_send(KM_UPGRADE_KEY, request, &response);
if (err != KM_ERROR_OK) {
return err;
}
@@ -487,7 +452,7 @@
request.additional_params.Reinitialize(*in_params);
BeginOperationResponse response(message_version_);
- keymaster_error_t err = Send(KM_BEGIN_OPERATION, request, &response);
+ keymaster_error_t err = trusty_keymaster_send(KM_BEGIN_OPERATION, request, &response);
if (err != KM_ERROR_OK) {
return err;
}
@@ -535,12 +500,12 @@
request.additional_params.Reinitialize(*in_params);
}
if (input && input->data_length > 0) {
- size_t max_input_size = SEND_BUF_SIZE - request.SerializedSize();
+ size_t max_input_size = TRUSTY_KEYMASTER_SEND_BUF_SIZE - request.SerializedSize();
request.input.Reinitialize(input->data, std::min(input->data_length, max_input_size));
}
UpdateOperationResponse response(message_version_);
- keymaster_error_t err = Send(KM_UPDATE_OPERATION, request, &response);
+ keymaster_error_t err = trusty_keymaster_send(KM_UPDATE_OPERATION, request, &response);
if (err != KM_ERROR_OK) {
return err;
}
@@ -578,8 +543,8 @@
return error_;
}
if (input && input->data_length > kMaximumFinishInputLength) {
- ALOGE("%zu-byte input to finish; only %zu bytes allowed",
- input->data_length, kMaximumFinishInputLength);
+ ALOGE("%zu-byte input to finish; only %zu bytes allowed", input->data_length,
+ kMaximumFinishInputLength);
return KM_ERROR_INVALID_INPUT_LENGTH;
}
@@ -603,7 +568,7 @@
}
FinishOperationResponse response(message_version_);
- keymaster_error_t err = Send(KM_FINISH_OPERATION, request, &response);
+ keymaster_error_t err = trusty_keymaster_send(KM_FINISH_OPERATION, request, &response);
if (err != KM_ERROR_OK) {
return err;
}
@@ -638,7 +603,7 @@
AbortOperationRequest request(message_version_);
request.op_handle = operation_handle;
AbortOperationResponse response(message_version_);
- return Send(KM_ABORT_OPERATION, request, &response);
+ return trusty_keymaster_send(KM_ABORT_OPERATION, request, &response);
}
hw_device_t* TrustyKeymasterDevice::hw_device() {
@@ -669,25 +634,25 @@
/* static */
keymaster_error_t TrustyKeymasterDevice::generate_key(
- const keymaster2_device_t* dev, const keymaster_key_param_set_t* params,
- keymaster_key_blob_t* key_blob, keymaster_key_characteristics_t* characteristics) {
+ const keymaster2_device_t* dev, const keymaster_key_param_set_t* params,
+ keymaster_key_blob_t* key_blob, keymaster_key_characteristics_t* characteristics) {
return convert_device(dev)->generate_key(params, key_blob, characteristics);
}
/* static */
keymaster_error_t TrustyKeymasterDevice::get_key_characteristics(
- const keymaster2_device_t* dev, const keymaster_key_blob_t* key_blob,
- const keymaster_blob_t* client_id, const keymaster_blob_t* app_data,
- keymaster_key_characteristics_t* characteristics) {
+ const keymaster2_device_t* dev, const keymaster_key_blob_t* key_blob,
+ const keymaster_blob_t* client_id, const keymaster_blob_t* app_data,
+ keymaster_key_characteristics_t* characteristics) {
return convert_device(dev)->get_key_characteristics(key_blob, client_id, app_data,
characteristics);
}
/* static */
keymaster_error_t TrustyKeymasterDevice::import_key(
- const keymaster2_device_t* dev, const keymaster_key_param_set_t* params,
- keymaster_key_format_t key_format, const keymaster_blob_t* key_data,
- keymaster_key_blob_t* key_blob, keymaster_key_characteristics_t* characteristics) {
+ const keymaster2_device_t* dev, const keymaster_key_param_set_t* params,
+ keymaster_key_format_t key_format, const keymaster_blob_t* key_data,
+ keymaster_key_blob_t* key_blob, keymaster_key_characteristics_t* characteristics) {
return convert_device(dev)->import_key(params, key_format, key_data, key_blob, characteristics);
}
@@ -711,10 +676,9 @@
}
/* static */
-keymaster_error_t TrustyKeymasterDevice::upgrade_key(const keymaster2_device_t* dev,
- const keymaster_key_blob_t* key_to_upgrade,
- const keymaster_key_param_set_t* upgrade_params,
- keymaster_key_blob_t* upgraded_key) {
+keymaster_error_t TrustyKeymasterDevice::upgrade_key(
+ const keymaster2_device_t* dev, const keymaster_key_blob_t* key_to_upgrade,
+ const keymaster_key_param_set_t* upgrade_params, keymaster_key_blob_t* upgraded_key) {
return convert_device(dev)->upgrade_key(key_to_upgrade, upgrade_params, upgraded_key);
}
@@ -730,9 +694,9 @@
/* static */
keymaster_error_t TrustyKeymasterDevice::update(
- const keymaster2_device_t* dev, keymaster_operation_handle_t operation_handle,
- const keymaster_key_param_set_t* in_params, const keymaster_blob_t* input,
- size_t* input_consumed, keymaster_key_param_set_t* out_params, keymaster_blob_t* output) {
+ const keymaster2_device_t* dev, keymaster_operation_handle_t operation_handle,
+ const keymaster_key_param_set_t* in_params, const keymaster_blob_t* input,
+ size_t* input_consumed, keymaster_key_param_set_t* out_params, keymaster_blob_t* output) {
return convert_device(dev)->update(operation_handle, in_params, input, input_consumed,
out_params, output);
}
@@ -755,42 +719,4 @@
return convert_device(dev)->abort(operation_handle);
}
-keymaster_error_t TrustyKeymasterDevice::Send(uint32_t command, const Serializable& req,
- KeymasterResponse* rsp) {
- uint32_t req_size = req.SerializedSize();
- if (req_size > SEND_BUF_SIZE) {
- return KM_ERROR_MEMORY_ALLOCATION_FAILED;
- }
- uint8_t send_buf[SEND_BUF_SIZE];
- Eraser send_buf_eraser(send_buf, SEND_BUF_SIZE);
- req.Serialize(send_buf, send_buf + req_size);
-
- // Send it
- uint8_t recv_buf[RECV_BUF_SIZE];
- Eraser recv_buf_eraser(recv_buf, RECV_BUF_SIZE);
- uint32_t rsp_size = RECV_BUF_SIZE;
- ALOGV("Sending %d byte request\n", (int)req.SerializedSize());
- int rc = trusty_keymaster_call(command, send_buf, req_size, recv_buf, &rsp_size);
- if (rc < 0) {
- // Reset the connection on tipc error
- trusty_keymaster_disconnect();
- trusty_keymaster_connect();
- ALOGE("tipc error: %d\n", rc);
- // TODO(swillden): Distinguish permanent from transient errors and set error_ appropriately.
- return translate_error(rc);
- } else {
- ALOGV("Received %d byte response\n", rsp_size);
- }
-
- const uint8_t* p = recv_buf;
- if (!rsp->Deserialize(&p, p + rsp_size)) {
- ALOGE("Error deserializing response of size %d\n", (int)rsp_size);
- return KM_ERROR_UNKNOWN_ERROR;
- } else if (rsp->error != KM_ERROR_OK) {
- ALOGE("Response of size %d contained error code %d\n", (int)rsp_size, (int)rsp->error);
- return rsp->error;
- }
- return rsp->error;
-}
-
} // namespace keymaster
diff --git a/trusty/keymaster/trusty_keymaster_device_test.cpp b/trusty/keymaster/legacy/trusty_keymaster_device_test.cpp
similarity index 90%
rename from trusty/keymaster/trusty_keymaster_device_test.cpp
rename to trusty/keymaster/legacy/trusty_keymaster_device_test.cpp
index 9227964..68def58 100644
--- a/trusty/keymaster/trusty_keymaster_device_test.cpp
+++ b/trusty/keymaster/legacy/trusty_keymaster_device_test.cpp
@@ -28,15 +28,15 @@
#include <keymaster/keymaster_tags.h>
#include <keymaster/soft_keymaster_context.h>
+#include <trusty_keymaster/legacy/trusty_keymaster_device.h>
#include "android_keymaster_test_utils.h"
-#include "trusty_keymaster_device.h"
#include "openssl_utils.h"
-using std::string;
using std::ifstream;
using std::istreambuf_iterator;
+using std::string;
-static keymaster::AndroidKeymaster *impl_ = nullptr;
+static keymaster::AndroidKeymaster* impl_ = nullptr;
extern "C" {
int __android_log_print();
@@ -65,8 +65,8 @@
template <typename Req, typename Rsp>
static int fake_call(keymaster::AndroidKeymaster* device,
- void (keymaster::AndroidKeymaster::*method)(const Req&, Rsp*), void* in_buf,
- uint32_t in_size, void* out_buf, uint32_t* out_size) {
+ void (keymaster::AndroidKeymaster::*method)(const Req&, Rsp*), void* in_buf,
+ uint32_t in_size, void* out_buf, uint32_t* out_size) {
Req req;
const uint8_t* in = static_cast<uint8_t*>(in_buf);
req.Deserialize(&in, in + in_size);
@@ -80,29 +80,28 @@
}
int trusty_keymaster_call(uint32_t cmd, void* in_buf, uint32_t in_size, void* out_buf,
- uint32_t* out_size) {
+ uint32_t* out_size) {
switch (cmd) {
- case KM_GENERATE_KEY:
- return fake_call(impl_, &keymaster::AndroidKeymaster::GenerateKey, in_buf, in_size,
- out_buf, out_size);
- case KM_BEGIN_OPERATION:
- return fake_call(impl_, &keymaster::AndroidKeymaster::BeginOperation, in_buf, in_size,
- out_buf, out_size);
- case KM_UPDATE_OPERATION:
- return fake_call(impl_, &keymaster::AndroidKeymaster::UpdateOperation, in_buf, in_size,
- out_buf, out_size);
- case KM_FINISH_OPERATION:
- return fake_call(impl_, &keymaster::AndroidKeymaster::FinishOperation, in_buf, in_size,
- out_buf, out_size);
- case KM_IMPORT_KEY:
- return fake_call(impl_, &keymaster::AndroidKeymaster::ImportKey, in_buf, in_size, out_buf,
- out_size);
- case KM_EXPORT_KEY:
- return fake_call(impl_, &keymaster::AndroidKeymaster::ExportKey, in_buf, in_size, out_buf,
- out_size);
+ case KM_GENERATE_KEY:
+ return fake_call(impl_, &keymaster::AndroidKeymaster::GenerateKey, in_buf, in_size,
+ out_buf, out_size);
+ case KM_BEGIN_OPERATION:
+ return fake_call(impl_, &keymaster::AndroidKeymaster::BeginOperation, in_buf, in_size,
+ out_buf, out_size);
+ case KM_UPDATE_OPERATION:
+ return fake_call(impl_, &keymaster::AndroidKeymaster::UpdateOperation, in_buf, in_size,
+ out_buf, out_size);
+ case KM_FINISH_OPERATION:
+ return fake_call(impl_, &keymaster::AndroidKeymaster::FinishOperation, in_buf, in_size,
+ out_buf, out_size);
+ case KM_IMPORT_KEY:
+ return fake_call(impl_, &keymaster::AndroidKeymaster::ImportKey, in_buf, in_size,
+ out_buf, out_size);
+ case KM_EXPORT_KEY:
+ return fake_call(impl_, &keymaster::AndroidKeymaster::ExportKey, in_buf, in_size,
+ out_buf, out_size);
}
return -EINVAL;
-
}
namespace keymaster {
@@ -127,15 +126,15 @@
size_t dsa_message_len(const keymaster_dsa_keygen_params_t& params) {
switch (params.key_size) {
- case 256:
- case 1024:
- return 48;
- case 2048:
- case 4096:
- return 72;
- default:
- // Oops.
- return 0;
+ case 256:
+ case 1024:
+ return 48;
+ case 2048:
+ case 4096:
+ return 72;
+ default:
+ // Oops.
+ return 0;
}
}
@@ -323,9 +322,9 @@
Malloc_Delete sig_deleter(signature);
signature[siglen / 2]++;
- EXPECT_EQ(
- KM_ERROR_VERIFICATION_FAILED,
- device.verify_data(&sig_params, ptr, size, message.get(), message_len, signature, siglen));
+ EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED,
+ device.verify_data(&sig_params, ptr, size, message.get(), message_len, signature,
+ siglen));
}
TEST_F(VerificationTest, RsaBadMessage) {
@@ -345,9 +344,9 @@
&signature, &siglen));
Malloc_Delete sig_deleter(signature);
message[0]++;
- EXPECT_EQ(
- KM_ERROR_VERIFICATION_FAILED,
- device.verify_data(&sig_params, ptr, size, message.get(), message_len, signature, siglen));
+ EXPECT_EQ(KM_ERROR_VERIFICATION_FAILED,
+ device.verify_data(&sig_params, ptr, size, message.get(), message_len, signature,
+ siglen));
}
TEST_F(VerificationTest, RsaShortMessage) {
diff --git a/trusty/keymaster/legacy/trusty_keymaster_main.cpp b/trusty/keymaster/legacy/trusty_keymaster_main.cpp
new file mode 100644
index 0000000..e3e70e6
--- /dev/null
+++ b/trusty/keymaster/legacy/trusty_keymaster_main.cpp
@@ -0,0 +1,408 @@
+/*
+ * Copyright 2014 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 <keymaster/keymaster_configuration.h>
+
+#include <stdio.h>
+#include <memory>
+
+#include <openssl/evp.h>
+#include <openssl/x509.h>
+
+#include <trusty_keymaster/legacy/trusty_keymaster_device.h>
+
+using keymaster::TrustyKeymasterDevice;
+
+unsigned char rsa_privkey_pk8_der[] = {
+ 0x30, 0x82, 0x02, 0x75, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
+ 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x02, 0x5f, 0x30, 0x82, 0x02, 0x5b,
+ 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xc6, 0x09, 0x54, 0x09, 0x04, 0x7d, 0x86, 0x34,
+ 0x81, 0x2d, 0x5a, 0x21, 0x81, 0x76, 0xe4, 0x5c, 0x41, 0xd6, 0x0a, 0x75, 0xb1, 0x39, 0x01,
+ 0xf2, 0x34, 0x22, 0x6c, 0xff, 0xe7, 0x76, 0x52, 0x1c, 0x5a, 0x77, 0xb9, 0xe3, 0x89, 0x41,
+ 0x7b, 0x71, 0xc0, 0xb6, 0xa4, 0x4d, 0x13, 0xaf, 0xe4, 0xe4, 0xa2, 0x80, 0x5d, 0x46, 0xc9,
+ 0xda, 0x29, 0x35, 0xad, 0xb1, 0xff, 0x0c, 0x1f, 0x24, 0xea, 0x06, 0xe6, 0x2b, 0x20, 0xd7,
+ 0x76, 0x43, 0x0a, 0x4d, 0x43, 0x51, 0x57, 0x23, 0x3c, 0x6f, 0x91, 0x67, 0x83, 0xc3, 0x0e,
+ 0x31, 0x0f, 0xcb, 0xd8, 0x9b, 0x85, 0xc2, 0xd5, 0x67, 0x71, 0x16, 0x97, 0x85, 0xac, 0x12,
+ 0xbc, 0xa2, 0x44, 0xab, 0xda, 0x72, 0xbf, 0xb1, 0x9f, 0xc4, 0x4d, 0x27, 0xc8, 0x1e, 0x1d,
+ 0x92, 0xde, 0x28, 0x4f, 0x40, 0x61, 0xed, 0xfd, 0x99, 0x28, 0x07, 0x45, 0xea, 0x6d, 0x25,
+ 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x80, 0x1b, 0xe0, 0xf0, 0x4d, 0x9c, 0xae, 0x37,
+ 0x18, 0x69, 0x1f, 0x03, 0x53, 0x38, 0x30, 0x8e, 0x91, 0x56, 0x4b, 0x55, 0x89, 0x9f, 0xfb,
+ 0x50, 0x84, 0xd2, 0x46, 0x0e, 0x66, 0x30, 0x25, 0x7e, 0x05, 0xb3, 0xce, 0xab, 0x02, 0x97,
+ 0x2d, 0xfa, 0xbc, 0xd6, 0xce, 0x5f, 0x6e, 0xe2, 0x58, 0x9e, 0xb6, 0x79, 0x11, 0xed, 0x0f,
+ 0xac, 0x16, 0xe4, 0x3a, 0x44, 0x4b, 0x8c, 0x86, 0x1e, 0x54, 0x4a, 0x05, 0x93, 0x36, 0x57,
+ 0x72, 0xf8, 0xba, 0xf6, 0xb2, 0x2f, 0xc9, 0xe3, 0xc5, 0xf1, 0x02, 0x4b, 0x06, 0x3a, 0xc0,
+ 0x80, 0xa7, 0xb2, 0x23, 0x4c, 0xf8, 0xae, 0xe8, 0xf6, 0xc4, 0x7b, 0xbf, 0x4f, 0xd3, 0xac,
+ 0xe7, 0x24, 0x02, 0x90, 0xbe, 0xf1, 0x6c, 0x0b, 0x3f, 0x7f, 0x3c, 0xdd, 0x64, 0xce, 0x3a,
+ 0xb5, 0x91, 0x2c, 0xf6, 0xe3, 0x2f, 0x39, 0xab, 0x18, 0x83, 0x58, 0xaf, 0xcc, 0xcd, 0x80,
+ 0x81, 0x02, 0x41, 0x00, 0xe4, 0xb4, 0x9e, 0xf5, 0x0f, 0x76, 0x5d, 0x3b, 0x24, 0xdd, 0xe0,
+ 0x1a, 0xce, 0xaa, 0xf1, 0x30, 0xf2, 0xc7, 0x66, 0x70, 0xa9, 0x1a, 0x61, 0xae, 0x08, 0xaf,
+ 0x49, 0x7b, 0x4a, 0x82, 0xbe, 0x6d, 0xee, 0x8f, 0xcd, 0xd5, 0xe3, 0xf7, 0xba, 0x1c, 0xfb,
+ 0x1f, 0x0c, 0x92, 0x6b, 0x88, 0xf8, 0x8c, 0x92, 0xbf, 0xab, 0x13, 0x7f, 0xba, 0x22, 0x85,
+ 0x22, 0x7b, 0x83, 0xc3, 0x42, 0xff, 0x7c, 0x55, 0x02, 0x41, 0x00, 0xdd, 0xab, 0xb5, 0x83,
+ 0x9c, 0x4c, 0x7f, 0x6b, 0xf3, 0xd4, 0x18, 0x32, 0x31, 0xf0, 0x05, 0xb3, 0x1a, 0xa5, 0x8a,
+ 0xff, 0xdd, 0xa5, 0xc7, 0x9e, 0x4c, 0xce, 0x21, 0x7f, 0x6b, 0xc9, 0x30, 0xdb, 0xe5, 0x63,
+ 0xd4, 0x80, 0x70, 0x6c, 0x24, 0xe9, 0xeb, 0xfc, 0xab, 0x28, 0xa6, 0xcd, 0xef, 0xd3, 0x24,
+ 0xb7, 0x7e, 0x1b, 0xf7, 0x25, 0x1b, 0x70, 0x90, 0x92, 0xc2, 0x4f, 0xf5, 0x01, 0xfd, 0x91,
+ 0x02, 0x40, 0x23, 0xd4, 0x34, 0x0e, 0xda, 0x34, 0x45, 0xd8, 0xcd, 0x26, 0xc1, 0x44, 0x11,
+ 0xda, 0x6f, 0xdc, 0xa6, 0x3c, 0x1c, 0xcd, 0x4b, 0x80, 0xa9, 0x8a, 0xd5, 0x2b, 0x78, 0xcc,
+ 0x8a, 0xd8, 0xbe, 0xb2, 0x84, 0x2c, 0x1d, 0x28, 0x04, 0x05, 0xbc, 0x2f, 0x6c, 0x1b, 0xea,
+ 0x21, 0x4a, 0x1d, 0x74, 0x2a, 0xb9, 0x96, 0xb3, 0x5b, 0x63, 0xa8, 0x2a, 0x5e, 0x47, 0x0f,
+ 0xa8, 0x8d, 0xbf, 0x82, 0x3c, 0xdd, 0x02, 0x40, 0x1b, 0x7b, 0x57, 0x44, 0x9a, 0xd3, 0x0d,
+ 0x15, 0x18, 0x24, 0x9a, 0x5f, 0x56, 0xbb, 0x98, 0x29, 0x4d, 0x4b, 0x6a, 0xc1, 0x2f, 0xfc,
+ 0x86, 0x94, 0x04, 0x97, 0xa5, 0xa5, 0x83, 0x7a, 0x6c, 0xf9, 0x46, 0x26, 0x2b, 0x49, 0x45,
+ 0x26, 0xd3, 0x28, 0xc1, 0x1e, 0x11, 0x26, 0x38, 0x0f, 0xde, 0x04, 0xc2, 0x4f, 0x91, 0x6d,
+ 0xec, 0x25, 0x08, 0x92, 0xdb, 0x09, 0xa6, 0xd7, 0x7c, 0xdb, 0xa3, 0x51, 0x02, 0x40, 0x77,
+ 0x62, 0xcd, 0x8f, 0x4d, 0x05, 0x0d, 0xa5, 0x6b, 0xd5, 0x91, 0xad, 0xb5, 0x15, 0xd2, 0x4d,
+ 0x7c, 0xcd, 0x32, 0xcc, 0xa0, 0xd0, 0x5f, 0x86, 0x6d, 0x58, 0x35, 0x14, 0xbd, 0x73, 0x24,
+ 0xd5, 0xf3, 0x36, 0x45, 0xe8, 0xed, 0x8b, 0x4a, 0x1c, 0xb3, 0xcc, 0x4a, 0x1d, 0x67, 0x98,
+ 0x73, 0x99, 0xf2, 0xa0, 0x9f, 0x5b, 0x3f, 0xb6, 0x8c, 0x88, 0xd5, 0xe5, 0xd9, 0x0a, 0xc3,
+ 0x34, 0x92, 0xd6};
+unsigned int rsa_privkey_pk8_der_len = 633;
+
+unsigned char dsa_privkey_pk8_der[] = {
+ 0x30, 0x82, 0x01, 0x4b, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x2b, 0x06, 0x07, 0x2a, 0x86,
+ 0x48, 0xce, 0x38, 0x04, 0x01, 0x30, 0x82, 0x01, 0x1e, 0x02, 0x81, 0x81, 0x00, 0xa3, 0xf3,
+ 0xe9, 0xb6, 0x7e, 0x7d, 0x88, 0xf6, 0xb7, 0xe5, 0xf5, 0x1f, 0x3b, 0xee, 0xac, 0xd7, 0xad,
+ 0xbc, 0xc9, 0xd1, 0x5a, 0xf8, 0x88, 0xc4, 0xef, 0x6e, 0x3d, 0x74, 0x19, 0x74, 0xe7, 0xd8,
+ 0xe0, 0x26, 0x44, 0x19, 0x86, 0xaf, 0x19, 0xdb, 0x05, 0xe9, 0x3b, 0x8b, 0x58, 0x58, 0xde,
+ 0xe5, 0x4f, 0x48, 0x15, 0x01, 0xea, 0xe6, 0x83, 0x52, 0xd7, 0xc1, 0x21, 0xdf, 0xb9, 0xb8,
+ 0x07, 0x66, 0x50, 0xfb, 0x3a, 0x0c, 0xb3, 0x85, 0xee, 0xbb, 0x04, 0x5f, 0xc2, 0x6d, 0x6d,
+ 0x95, 0xfa, 0x11, 0x93, 0x1e, 0x59, 0x5b, 0xb1, 0x45, 0x8d, 0xe0, 0x3d, 0x73, 0xaa, 0xf2,
+ 0x41, 0x14, 0x51, 0x07, 0x72, 0x3d, 0xa2, 0xf7, 0x58, 0xcd, 0x11, 0xa1, 0x32, 0xcf, 0xda,
+ 0x42, 0xb7, 0xcc, 0x32, 0x80, 0xdb, 0x87, 0x82, 0xec, 0x42, 0xdb, 0x5a, 0x55, 0x24, 0x24,
+ 0xa2, 0xd1, 0x55, 0x29, 0xad, 0xeb, 0x02, 0x15, 0x00, 0xeb, 0xea, 0x17, 0xd2, 0x09, 0xb3,
+ 0xd7, 0x21, 0x9a, 0x21, 0x07, 0x82, 0x8f, 0xab, 0xfe, 0x88, 0x71, 0x68, 0xf7, 0xe3, 0x02,
+ 0x81, 0x80, 0x19, 0x1c, 0x71, 0xfd, 0xe0, 0x03, 0x0c, 0x43, 0xd9, 0x0b, 0xf6, 0xcd, 0xd6,
+ 0xa9, 0x70, 0xe7, 0x37, 0x86, 0x3a, 0x78, 0xe9, 0xa7, 0x47, 0xa7, 0x47, 0x06, 0x88, 0xb1,
+ 0xaf, 0xd7, 0xf3, 0xf1, 0xa1, 0xd7, 0x00, 0x61, 0x28, 0x88, 0x31, 0x48, 0x60, 0xd8, 0x11,
+ 0xef, 0xa5, 0x24, 0x1a, 0x81, 0xc4, 0x2a, 0xe2, 0xea, 0x0e, 0x36, 0xd2, 0xd2, 0x05, 0x84,
+ 0x37, 0xcf, 0x32, 0x7d, 0x09, 0xe6, 0x0f, 0x8b, 0x0c, 0xc8, 0xc2, 0xa4, 0xb1, 0xdc, 0x80,
+ 0xca, 0x68, 0xdf, 0xaf, 0xd2, 0x90, 0xc0, 0x37, 0x58, 0x54, 0x36, 0x8f, 0x49, 0xb8, 0x62,
+ 0x75, 0x8b, 0x48, 0x47, 0xc0, 0xbe, 0xf7, 0x9a, 0x92, 0xa6, 0x68, 0x05, 0xda, 0x9d, 0xaf,
+ 0x72, 0x9a, 0x67, 0xb3, 0xb4, 0x14, 0x03, 0xae, 0x4f, 0x4c, 0x76, 0xb9, 0xd8, 0x64, 0x0a,
+ 0xba, 0x3b, 0xa8, 0x00, 0x60, 0x4d, 0xae, 0x81, 0xc3, 0xc5, 0x04, 0x17, 0x02, 0x15, 0x00,
+ 0x81, 0x9d, 0xfd, 0x53, 0x0c, 0xc1, 0x8f, 0xbe, 0x8b, 0xea, 0x00, 0x26, 0x19, 0x29, 0x33,
+ 0x91, 0x84, 0xbe, 0xad, 0x81};
+unsigned int dsa_privkey_pk8_der_len = 335;
+
+unsigned char ec_privkey_pk8_der[] = {
+ 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
+ 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04,
+ 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20, 0x73, 0x7c, 0x2e, 0xcd, 0x7b, 0x8d,
+ 0x19, 0x40, 0xbf, 0x29, 0x30, 0xaa, 0x9b, 0x4e, 0xd3, 0xff, 0x94, 0x1e, 0xed, 0x09,
+ 0x36, 0x6b, 0xc0, 0x32, 0x99, 0x98, 0x64, 0x81, 0xf3, 0xa4, 0xd8, 0x59, 0xa1, 0x44,
+ 0x03, 0x42, 0x00, 0x04, 0xbf, 0x85, 0xd7, 0x72, 0x0d, 0x07, 0xc2, 0x54, 0x61, 0x68,
+ 0x3b, 0xc6, 0x48, 0xb4, 0x77, 0x8a, 0x9a, 0x14, 0xdd, 0x8a, 0x02, 0x4e, 0x3b, 0xdd,
+ 0x8c, 0x7d, 0xdd, 0x9a, 0xb2, 0xb5, 0x28, 0xbb, 0xc7, 0xaa, 0x1b, 0x51, 0xf1, 0x4e,
+ 0xbb, 0xbb, 0x0b, 0xd0, 0xce, 0x21, 0xbc, 0xc4, 0x1c, 0x6e, 0xb0, 0x00, 0x83, 0xcf,
+ 0x33, 0x76, 0xd1, 0x1f, 0xd4, 0x49, 0x49, 0xe0, 0xb2, 0x18, 0x3b, 0xfe};
+unsigned int ec_privkey_pk8_der_len = 138;
+
+keymaster_key_param_t ec_params[] = {
+ keymaster_param_enum(KM_TAG_ALGORITHM, KM_ALGORITHM_EC),
+ keymaster_param_long(KM_TAG_EC_CURVE, KM_EC_CURVE_P_521),
+ keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_SIGN),
+ keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_VERIFY),
+ keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
+ keymaster_param_bool(KM_TAG_NO_AUTH_REQUIRED),
+};
+keymaster_key_param_set_t ec_param_set = {ec_params, sizeof(ec_params) / sizeof(*ec_params)};
+
+keymaster_key_param_t rsa_params[] = {
+ keymaster_param_enum(KM_TAG_ALGORITHM, KM_ALGORITHM_RSA),
+ keymaster_param_int(KM_TAG_KEY_SIZE, 1024),
+ keymaster_param_long(KM_TAG_RSA_PUBLIC_EXPONENT, 65537),
+ keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_SIGN),
+ keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_VERIFY),
+ keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE),
+ keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
+ keymaster_param_bool(KM_TAG_NO_AUTH_REQUIRED),
+};
+keymaster_key_param_set_t rsa_param_set = {rsa_params, sizeof(rsa_params) / sizeof(*rsa_params)};
+
+struct EVP_PKEY_Delete {
+ void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); }
+};
+
+struct EVP_PKEY_CTX_Delete {
+ void operator()(EVP_PKEY_CTX* p) { EVP_PKEY_CTX_free(p); }
+};
+
+static bool do_operation(TrustyKeymasterDevice* device, keymaster_purpose_t purpose,
+ keymaster_key_blob_t* key, keymaster_blob_t* input,
+ keymaster_blob_t* signature, keymaster_blob_t* output) {
+ keymaster_key_param_t params[] = {
+ keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE),
+ keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
+ };
+ keymaster_key_param_set_t param_set = {params, sizeof(params) / sizeof(*params)};
+ keymaster_operation_handle_t op_handle;
+ keymaster_error_t error = device->begin(purpose, key, ¶m_set, nullptr, &op_handle);
+ if (error != KM_ERROR_OK) {
+ printf("Keymaster begin() failed: %d\n", error);
+ return false;
+ }
+ size_t input_consumed;
+ error = device->update(op_handle, nullptr, input, &input_consumed, nullptr, nullptr);
+ if (error != KM_ERROR_OK) {
+ printf("Keymaster update() failed: %d\n", error);
+ return false;
+ }
+ if (input_consumed != input->data_length) {
+ // This should never happen. If it does, it's a bug in the keymaster implementation.
+ printf("Keymaster update() did not consume all data.\n");
+ device->abort(op_handle);
+ return false;
+ }
+ error = device->finish(op_handle, nullptr, nullptr, signature, nullptr, output);
+ if (error != KM_ERROR_OK) {
+ printf("Keymaster finish() failed: %d\n", error);
+ return false;
+ }
+ return true;
+}
+
+static bool test_import_rsa(TrustyKeymasterDevice* device) {
+ printf("===================\n");
+ printf("= RSA Import Test =\n");
+ printf("===================\n\n");
+
+ printf("=== Importing RSA keypair === \n");
+ keymaster_key_blob_t key;
+ keymaster_blob_t private_key = {rsa_privkey_pk8_der, rsa_privkey_pk8_der_len};
+ int error =
+ device->import_key(&rsa_param_set, KM_KEY_FORMAT_PKCS8, &private_key, &key, nullptr);
+ if (error != KM_ERROR_OK) {
+ printf("Error importing RSA key: %d\n\n", error);
+ return false;
+ }
+ std::unique_ptr<const uint8_t[]> key_deleter(key.key_material);
+
+ printf("=== Signing with imported RSA key ===\n");
+ size_t message_len = 1024 / 8;
+ std::unique_ptr<uint8_t[]> message(new uint8_t[message_len]);
+ memset(message.get(), 'a', message_len);
+ keymaster_blob_t input = {message.get(), message_len}, signature;
+
+ if (!do_operation(device, KM_PURPOSE_SIGN, &key, &input, nullptr, &signature)) {
+ printf("Error signing data with imported RSA key\n\n");
+ return false;
+ }
+ std::unique_ptr<const uint8_t[]> signature_deleter(signature.data);
+
+ printf("=== Verifying with imported RSA key === \n");
+ if (!do_operation(device, KM_PURPOSE_VERIFY, &key, &input, &signature, nullptr)) {
+ printf("Error verifying data with imported RSA key\n\n");
+ return false;
+ }
+
+ printf("\n");
+ return true;
+}
+
+static bool test_rsa(TrustyKeymasterDevice* device) {
+ printf("============\n");
+ printf("= RSA Test =\n");
+ printf("============\n\n");
+
+ printf("=== Generating RSA key pair ===\n");
+ keymaster_key_blob_t key;
+ int error = device->generate_key(&rsa_param_set, &key, nullptr);
+ if (error != KM_ERROR_OK) {
+ printf("Error generating RSA key pair: %d\n\n", error);
+ return false;
+ }
+ std::unique_ptr<const uint8_t[]> key_deleter(key.key_material);
+
+ printf("=== Signing with RSA key === \n");
+ size_t message_len = 1024 / 8;
+ std::unique_ptr<uint8_t[]> message(new uint8_t[message_len]);
+ memset(message.get(), 'a', message_len);
+ keymaster_blob_t input = {message.get(), message_len}, signature;
+
+ if (!do_operation(device, KM_PURPOSE_SIGN, &key, &input, nullptr, &signature)) {
+ printf("Error signing data with RSA key\n\n");
+ return false;
+ }
+ std::unique_ptr<const uint8_t[]> signature_deleter(signature.data);
+
+ printf("=== Verifying with RSA key === \n");
+ if (!do_operation(device, KM_PURPOSE_VERIFY, &key, &input, &signature, nullptr)) {
+ printf("Error verifying data with RSA key\n\n");
+ return false;
+ }
+
+ printf("=== Exporting RSA public key ===\n");
+ keymaster_blob_t exported_key;
+ error = device->export_key(KM_KEY_FORMAT_X509, &key, nullptr, nullptr, &exported_key);
+ if (error != KM_ERROR_OK) {
+ printf("Error exporting RSA public key: %d\n\n", error);
+ return false;
+ }
+
+ printf("=== Verifying with exported key ===\n");
+ const uint8_t* tmp = exported_key.data;
+ std::unique_ptr<EVP_PKEY, EVP_PKEY_Delete> pkey(
+ d2i_PUBKEY(NULL, &tmp, exported_key.data_length));
+ std::unique_ptr<EVP_PKEY_CTX, EVP_PKEY_CTX_Delete> ctx(EVP_PKEY_CTX_new(pkey.get(), NULL));
+ if (EVP_PKEY_verify_init(ctx.get()) != 1) {
+ printf("Error initializing openss EVP context\n\n");
+ return false;
+ }
+ if (EVP_PKEY_type(pkey->type) != EVP_PKEY_RSA) {
+ printf("Exported key was the wrong type?!?\n\n");
+ return false;
+ }
+
+ EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_NO_PADDING);
+ if (EVP_PKEY_verify(ctx.get(), signature.data, signature.data_length, message.get(),
+ message_len) != 1) {
+ printf("Verification with exported pubkey failed.\n\n");
+ return false;
+ } else {
+ printf("Verification succeeded\n");
+ }
+
+ printf("\n");
+ return true;
+}
+
+static bool test_import_ecdsa(TrustyKeymasterDevice* device) {
+ printf("=====================\n");
+ printf("= ECDSA Import Test =\n");
+ printf("=====================\n\n");
+
+ printf("=== Importing ECDSA keypair === \n");
+ keymaster_key_blob_t key;
+ keymaster_blob_t private_key = {ec_privkey_pk8_der, ec_privkey_pk8_der_len};
+ int error = device->import_key(&ec_param_set, KM_KEY_FORMAT_PKCS8, &private_key, &key, nullptr);
+ if (error != KM_ERROR_OK) {
+ printf("Error importing ECDSA key: %d\n\n", error);
+ return false;
+ }
+ std::unique_ptr<const uint8_t[]> deleter(key.key_material);
+
+ printf("=== Signing with imported ECDSA key ===\n");
+ size_t message_len = 30 /* arbitrary */;
+ std::unique_ptr<uint8_t[]> message(new uint8_t[message_len]);
+ memset(message.get(), 'a', message_len);
+ keymaster_blob_t input = {message.get(), message_len}, signature;
+
+ if (!do_operation(device, KM_PURPOSE_SIGN, &key, &input, nullptr, &signature)) {
+ printf("Error signing data with imported ECDSA key\n\n");
+ return false;
+ }
+ std::unique_ptr<const uint8_t[]> signature_deleter(signature.data);
+
+ printf("=== Verifying with imported ECDSA key === \n");
+ if (!do_operation(device, KM_PURPOSE_VERIFY, &key, &input, &signature, nullptr)) {
+ printf("Error verifying data with imported ECDSA key\n\n");
+ return false;
+ }
+
+ printf("\n");
+ return true;
+}
+
+static bool test_ecdsa(TrustyKeymasterDevice* device) {
+ printf("==============\n");
+ printf("= ECDSA Test =\n");
+ printf("==============\n\n");
+
+ printf("=== Generating ECDSA key pair ===\n");
+ keymaster_key_blob_t key;
+ int error = device->generate_key(&ec_param_set, &key, nullptr);
+ if (error != KM_ERROR_OK) {
+ printf("Error generating ECDSA key pair: %d\n\n", error);
+ return false;
+ }
+ std::unique_ptr<const uint8_t[]> key_deleter(key.key_material);
+
+ printf("=== Signing with ECDSA key === \n");
+ size_t message_len = 30 /* arbitrary */;
+ std::unique_ptr<uint8_t[]> message(new uint8_t[message_len]);
+ memset(message.get(), 'a', message_len);
+ keymaster_blob_t input = {message.get(), message_len}, signature;
+
+ if (!do_operation(device, KM_PURPOSE_SIGN, &key, &input, nullptr, &signature)) {
+ printf("Error signing data with ECDSA key\n\n");
+ return false;
+ }
+ std::unique_ptr<const uint8_t[]> signature_deleter(signature.data);
+
+ printf("=== Verifying with ECDSA key === \n");
+ if (!do_operation(device, KM_PURPOSE_VERIFY, &key, &input, &signature, nullptr)) {
+ printf("Error verifying data with ECDSA key\n\n");
+ return false;
+ }
+
+ printf("=== Exporting ECDSA public key ===\n");
+ keymaster_blob_t exported_key;
+ error = device->export_key(KM_KEY_FORMAT_X509, &key, nullptr, nullptr, &exported_key);
+ if (error != KM_ERROR_OK) {
+ printf("Error exporting ECDSA public key: %d\n\n", error);
+ return false;
+ }
+
+ printf("=== Verifying with exported key ===\n");
+ const uint8_t* tmp = exported_key.data;
+ std::unique_ptr<EVP_PKEY, EVP_PKEY_Delete> pkey(
+ d2i_PUBKEY(NULL, &tmp, exported_key.data_length));
+ std::unique_ptr<EVP_PKEY_CTX, EVP_PKEY_CTX_Delete> ctx(EVP_PKEY_CTX_new(pkey.get(), NULL));
+ if (EVP_PKEY_verify_init(ctx.get()) != 1) {
+ printf("Error initializing openssl EVP context\n\n");
+ return false;
+ }
+ if (EVP_PKEY_type(pkey->type) != EVP_PKEY_EC) {
+ printf("Exported key was the wrong type?!?\n\n");
+ return false;
+ }
+
+ if (EVP_PKEY_verify(ctx.get(), signature.data, signature.data_length, message.get(),
+ message_len) != 1) {
+ printf("Verification with exported pubkey failed.\n\n");
+ return false;
+ } else {
+ printf("Verification succeeded\n");
+ }
+
+ printf("\n");
+ return true;
+}
+
+int main(void) {
+ TrustyKeymasterDevice device(NULL);
+ keymaster::ConfigureDevice(reinterpret_cast<keymaster2_device_t*>(&device));
+ if (device.session_error() != KM_ERROR_OK) {
+ printf("Failed to initialize Trusty session: %d\n", device.session_error());
+ return 1;
+ }
+ printf("Trusty session initialized\n");
+
+ bool success = true;
+ success &= test_rsa(&device);
+ success &= test_import_rsa(&device);
+ success &= test_ecdsa(&device);
+ success &= test_import_ecdsa(&device);
+
+ if (success) {
+ printf("\nTESTS PASSED!\n");
+ } else {
+ printf("\n!!!!TESTS FAILED!!!\n");
+ }
+
+ return success ? 0 : 1;
+}
diff --git a/trusty/keymaster/trusty_keymaster_ipc.cpp b/trusty/keymaster/trusty_keymaster_ipc.cpp
deleted file mode 100644
index 686e7ae..0000000
--- a/trusty/keymaster/trusty_keymaster_ipc.cpp
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * Copyright (C) 2015 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.
- */
-
-#define LOG_TAG "TrustyKeymaster"
-
-// TODO: make this generic in libtrusty
-
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/uio.h>
-#include <unistd.h>
-
-#include <algorithm>
-
-#include <log/log.h>
-#include <trusty/tipc.h>
-
-#include "keymaster_ipc.h"
-#include "trusty_keymaster_ipc.h"
-
-#define TRUSTY_DEVICE_NAME "/dev/trusty-ipc-dev0"
-
-static int handle_ = -1;
-
-int trusty_keymaster_connect() {
- int rc = tipc_connect(TRUSTY_DEVICE_NAME, KEYMASTER_PORT);
- if (rc < 0) {
- return rc;
- }
-
- handle_ = rc;
- return 0;
-}
-
-int trusty_keymaster_call(uint32_t cmd, void* in, uint32_t in_size, uint8_t* out,
- uint32_t* out_size) {
- if (handle_ < 0) {
- ALOGE("not connected\n");
- return -EINVAL;
- }
-
- size_t msg_size = in_size + sizeof(struct keymaster_message);
- struct keymaster_message* msg = reinterpret_cast<struct keymaster_message*>(malloc(msg_size));
- if (!msg) {
- ALOGE("failed to allocate msg buffer\n");
- return -EINVAL;
- }
-
- msg->cmd = cmd;
- memcpy(msg->payload, in, in_size);
-
- ssize_t rc = write(handle_, msg, msg_size);
- free(msg);
-
- if (rc < 0) {
- ALOGE("failed to send cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT, strerror(errno));
- return -errno;
- }
- size_t out_max_size = *out_size;
- *out_size = 0;
- struct iovec iov[2];
- struct keymaster_message header;
- iov[0] = {.iov_base = &header, .iov_len = sizeof(struct keymaster_message)};
- while (true) {
- iov[1] = {
- .iov_base = out + *out_size,
- .iov_len = std::min<uint32_t>(KEYMASTER_MAX_BUFFER_LENGTH, out_max_size - *out_size)};
- rc = readv(handle_, iov, 2);
- if (rc < 0) {
- ALOGE("failed to retrieve response for cmd (%d) to %s: %s\n", cmd, KEYMASTER_PORT,
- strerror(errno));
- return -errno;
- }
-
- if ((size_t)rc < sizeof(struct keymaster_message)) {
- ALOGE("invalid response size (%d)\n", (int)rc);
- return -EINVAL;
- }
-
- if ((cmd | KEYMASTER_RESP_BIT) != (header.cmd & ~(KEYMASTER_STOP_BIT))) {
- ALOGE("invalid command (%d)", header.cmd);
- return -EINVAL;
- }
- *out_size += ((size_t)rc - sizeof(struct keymaster_message));
- if (header.cmd & KEYMASTER_STOP_BIT) {
- break;
- }
- }
-
- return rc;
-}
-
-void trusty_keymaster_disconnect() {
- if (handle_ >= 0) {
- tipc_close(handle_);
- }
- handle_ = -1;
-}
diff --git a/trusty/keymaster/trusty_keymaster_main.cpp b/trusty/keymaster/trusty_keymaster_main.cpp
deleted file mode 100644
index ed78b7f..0000000
--- a/trusty/keymaster/trusty_keymaster_main.cpp
+++ /dev/null
@@ -1,401 +0,0 @@
-/*
- * Copyright 2014 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 <keymaster/keymaster_configuration.h>
-
-#include <stdio.h>
-#include <memory>
-
-#include <openssl/evp.h>
-#include <openssl/x509.h>
-
-#include "trusty_keymaster_device.h"
-
-using keymaster::TrustyKeymasterDevice;
-
-unsigned char rsa_privkey_pk8_der[] = {
- 0x30, 0x82, 0x02, 0x75, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
- 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x02, 0x5f, 0x30, 0x82, 0x02, 0x5b, 0x02, 0x01,
- 0x00, 0x02, 0x81, 0x81, 0x00, 0xc6, 0x09, 0x54, 0x09, 0x04, 0x7d, 0x86, 0x34, 0x81, 0x2d, 0x5a,
- 0x21, 0x81, 0x76, 0xe4, 0x5c, 0x41, 0xd6, 0x0a, 0x75, 0xb1, 0x39, 0x01, 0xf2, 0x34, 0x22, 0x6c,
- 0xff, 0xe7, 0x76, 0x52, 0x1c, 0x5a, 0x77, 0xb9, 0xe3, 0x89, 0x41, 0x7b, 0x71, 0xc0, 0xb6, 0xa4,
- 0x4d, 0x13, 0xaf, 0xe4, 0xe4, 0xa2, 0x80, 0x5d, 0x46, 0xc9, 0xda, 0x29, 0x35, 0xad, 0xb1, 0xff,
- 0x0c, 0x1f, 0x24, 0xea, 0x06, 0xe6, 0x2b, 0x20, 0xd7, 0x76, 0x43, 0x0a, 0x4d, 0x43, 0x51, 0x57,
- 0x23, 0x3c, 0x6f, 0x91, 0x67, 0x83, 0xc3, 0x0e, 0x31, 0x0f, 0xcb, 0xd8, 0x9b, 0x85, 0xc2, 0xd5,
- 0x67, 0x71, 0x16, 0x97, 0x85, 0xac, 0x12, 0xbc, 0xa2, 0x44, 0xab, 0xda, 0x72, 0xbf, 0xb1, 0x9f,
- 0xc4, 0x4d, 0x27, 0xc8, 0x1e, 0x1d, 0x92, 0xde, 0x28, 0x4f, 0x40, 0x61, 0xed, 0xfd, 0x99, 0x28,
- 0x07, 0x45, 0xea, 0x6d, 0x25, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x80, 0x1b, 0xe0, 0xf0,
- 0x4d, 0x9c, 0xae, 0x37, 0x18, 0x69, 0x1f, 0x03, 0x53, 0x38, 0x30, 0x8e, 0x91, 0x56, 0x4b, 0x55,
- 0x89, 0x9f, 0xfb, 0x50, 0x84, 0xd2, 0x46, 0x0e, 0x66, 0x30, 0x25, 0x7e, 0x05, 0xb3, 0xce, 0xab,
- 0x02, 0x97, 0x2d, 0xfa, 0xbc, 0xd6, 0xce, 0x5f, 0x6e, 0xe2, 0x58, 0x9e, 0xb6, 0x79, 0x11, 0xed,
- 0x0f, 0xac, 0x16, 0xe4, 0x3a, 0x44, 0x4b, 0x8c, 0x86, 0x1e, 0x54, 0x4a, 0x05, 0x93, 0x36, 0x57,
- 0x72, 0xf8, 0xba, 0xf6, 0xb2, 0x2f, 0xc9, 0xe3, 0xc5, 0xf1, 0x02, 0x4b, 0x06, 0x3a, 0xc0, 0x80,
- 0xa7, 0xb2, 0x23, 0x4c, 0xf8, 0xae, 0xe8, 0xf6, 0xc4, 0x7b, 0xbf, 0x4f, 0xd3, 0xac, 0xe7, 0x24,
- 0x02, 0x90, 0xbe, 0xf1, 0x6c, 0x0b, 0x3f, 0x7f, 0x3c, 0xdd, 0x64, 0xce, 0x3a, 0xb5, 0x91, 0x2c,
- 0xf6, 0xe3, 0x2f, 0x39, 0xab, 0x18, 0x83, 0x58, 0xaf, 0xcc, 0xcd, 0x80, 0x81, 0x02, 0x41, 0x00,
- 0xe4, 0xb4, 0x9e, 0xf5, 0x0f, 0x76, 0x5d, 0x3b, 0x24, 0xdd, 0xe0, 0x1a, 0xce, 0xaa, 0xf1, 0x30,
- 0xf2, 0xc7, 0x66, 0x70, 0xa9, 0x1a, 0x61, 0xae, 0x08, 0xaf, 0x49, 0x7b, 0x4a, 0x82, 0xbe, 0x6d,
- 0xee, 0x8f, 0xcd, 0xd5, 0xe3, 0xf7, 0xba, 0x1c, 0xfb, 0x1f, 0x0c, 0x92, 0x6b, 0x88, 0xf8, 0x8c,
- 0x92, 0xbf, 0xab, 0x13, 0x7f, 0xba, 0x22, 0x85, 0x22, 0x7b, 0x83, 0xc3, 0x42, 0xff, 0x7c, 0x55,
- 0x02, 0x41, 0x00, 0xdd, 0xab, 0xb5, 0x83, 0x9c, 0x4c, 0x7f, 0x6b, 0xf3, 0xd4, 0x18, 0x32, 0x31,
- 0xf0, 0x05, 0xb3, 0x1a, 0xa5, 0x8a, 0xff, 0xdd, 0xa5, 0xc7, 0x9e, 0x4c, 0xce, 0x21, 0x7f, 0x6b,
- 0xc9, 0x30, 0xdb, 0xe5, 0x63, 0xd4, 0x80, 0x70, 0x6c, 0x24, 0xe9, 0xeb, 0xfc, 0xab, 0x28, 0xa6,
- 0xcd, 0xef, 0xd3, 0x24, 0xb7, 0x7e, 0x1b, 0xf7, 0x25, 0x1b, 0x70, 0x90, 0x92, 0xc2, 0x4f, 0xf5,
- 0x01, 0xfd, 0x91, 0x02, 0x40, 0x23, 0xd4, 0x34, 0x0e, 0xda, 0x34, 0x45, 0xd8, 0xcd, 0x26, 0xc1,
- 0x44, 0x11, 0xda, 0x6f, 0xdc, 0xa6, 0x3c, 0x1c, 0xcd, 0x4b, 0x80, 0xa9, 0x8a, 0xd5, 0x2b, 0x78,
- 0xcc, 0x8a, 0xd8, 0xbe, 0xb2, 0x84, 0x2c, 0x1d, 0x28, 0x04, 0x05, 0xbc, 0x2f, 0x6c, 0x1b, 0xea,
- 0x21, 0x4a, 0x1d, 0x74, 0x2a, 0xb9, 0x96, 0xb3, 0x5b, 0x63, 0xa8, 0x2a, 0x5e, 0x47, 0x0f, 0xa8,
- 0x8d, 0xbf, 0x82, 0x3c, 0xdd, 0x02, 0x40, 0x1b, 0x7b, 0x57, 0x44, 0x9a, 0xd3, 0x0d, 0x15, 0x18,
- 0x24, 0x9a, 0x5f, 0x56, 0xbb, 0x98, 0x29, 0x4d, 0x4b, 0x6a, 0xc1, 0x2f, 0xfc, 0x86, 0x94, 0x04,
- 0x97, 0xa5, 0xa5, 0x83, 0x7a, 0x6c, 0xf9, 0x46, 0x26, 0x2b, 0x49, 0x45, 0x26, 0xd3, 0x28, 0xc1,
- 0x1e, 0x11, 0x26, 0x38, 0x0f, 0xde, 0x04, 0xc2, 0x4f, 0x91, 0x6d, 0xec, 0x25, 0x08, 0x92, 0xdb,
- 0x09, 0xa6, 0xd7, 0x7c, 0xdb, 0xa3, 0x51, 0x02, 0x40, 0x77, 0x62, 0xcd, 0x8f, 0x4d, 0x05, 0x0d,
- 0xa5, 0x6b, 0xd5, 0x91, 0xad, 0xb5, 0x15, 0xd2, 0x4d, 0x7c, 0xcd, 0x32, 0xcc, 0xa0, 0xd0, 0x5f,
- 0x86, 0x6d, 0x58, 0x35, 0x14, 0xbd, 0x73, 0x24, 0xd5, 0xf3, 0x36, 0x45, 0xe8, 0xed, 0x8b, 0x4a,
- 0x1c, 0xb3, 0xcc, 0x4a, 0x1d, 0x67, 0x98, 0x73, 0x99, 0xf2, 0xa0, 0x9f, 0x5b, 0x3f, 0xb6, 0x8c,
- 0x88, 0xd5, 0xe5, 0xd9, 0x0a, 0xc3, 0x34, 0x92, 0xd6};
-unsigned int rsa_privkey_pk8_der_len = 633;
-
-unsigned char dsa_privkey_pk8_der[] = {
- 0x30, 0x82, 0x01, 0x4b, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x2b, 0x06, 0x07, 0x2a, 0x86, 0x48,
- 0xce, 0x38, 0x04, 0x01, 0x30, 0x82, 0x01, 0x1e, 0x02, 0x81, 0x81, 0x00, 0xa3, 0xf3, 0xe9, 0xb6,
- 0x7e, 0x7d, 0x88, 0xf6, 0xb7, 0xe5, 0xf5, 0x1f, 0x3b, 0xee, 0xac, 0xd7, 0xad, 0xbc, 0xc9, 0xd1,
- 0x5a, 0xf8, 0x88, 0xc4, 0xef, 0x6e, 0x3d, 0x74, 0x19, 0x74, 0xe7, 0xd8, 0xe0, 0x26, 0x44, 0x19,
- 0x86, 0xaf, 0x19, 0xdb, 0x05, 0xe9, 0x3b, 0x8b, 0x58, 0x58, 0xde, 0xe5, 0x4f, 0x48, 0x15, 0x01,
- 0xea, 0xe6, 0x83, 0x52, 0xd7, 0xc1, 0x21, 0xdf, 0xb9, 0xb8, 0x07, 0x66, 0x50, 0xfb, 0x3a, 0x0c,
- 0xb3, 0x85, 0xee, 0xbb, 0x04, 0x5f, 0xc2, 0x6d, 0x6d, 0x95, 0xfa, 0x11, 0x93, 0x1e, 0x59, 0x5b,
- 0xb1, 0x45, 0x8d, 0xe0, 0x3d, 0x73, 0xaa, 0xf2, 0x41, 0x14, 0x51, 0x07, 0x72, 0x3d, 0xa2, 0xf7,
- 0x58, 0xcd, 0x11, 0xa1, 0x32, 0xcf, 0xda, 0x42, 0xb7, 0xcc, 0x32, 0x80, 0xdb, 0x87, 0x82, 0xec,
- 0x42, 0xdb, 0x5a, 0x55, 0x24, 0x24, 0xa2, 0xd1, 0x55, 0x29, 0xad, 0xeb, 0x02, 0x15, 0x00, 0xeb,
- 0xea, 0x17, 0xd2, 0x09, 0xb3, 0xd7, 0x21, 0x9a, 0x21, 0x07, 0x82, 0x8f, 0xab, 0xfe, 0x88, 0x71,
- 0x68, 0xf7, 0xe3, 0x02, 0x81, 0x80, 0x19, 0x1c, 0x71, 0xfd, 0xe0, 0x03, 0x0c, 0x43, 0xd9, 0x0b,
- 0xf6, 0xcd, 0xd6, 0xa9, 0x70, 0xe7, 0x37, 0x86, 0x3a, 0x78, 0xe9, 0xa7, 0x47, 0xa7, 0x47, 0x06,
- 0x88, 0xb1, 0xaf, 0xd7, 0xf3, 0xf1, 0xa1, 0xd7, 0x00, 0x61, 0x28, 0x88, 0x31, 0x48, 0x60, 0xd8,
- 0x11, 0xef, 0xa5, 0x24, 0x1a, 0x81, 0xc4, 0x2a, 0xe2, 0xea, 0x0e, 0x36, 0xd2, 0xd2, 0x05, 0x84,
- 0x37, 0xcf, 0x32, 0x7d, 0x09, 0xe6, 0x0f, 0x8b, 0x0c, 0xc8, 0xc2, 0xa4, 0xb1, 0xdc, 0x80, 0xca,
- 0x68, 0xdf, 0xaf, 0xd2, 0x90, 0xc0, 0x37, 0x58, 0x54, 0x36, 0x8f, 0x49, 0xb8, 0x62, 0x75, 0x8b,
- 0x48, 0x47, 0xc0, 0xbe, 0xf7, 0x9a, 0x92, 0xa6, 0x68, 0x05, 0xda, 0x9d, 0xaf, 0x72, 0x9a, 0x67,
- 0xb3, 0xb4, 0x14, 0x03, 0xae, 0x4f, 0x4c, 0x76, 0xb9, 0xd8, 0x64, 0x0a, 0xba, 0x3b, 0xa8, 0x00,
- 0x60, 0x4d, 0xae, 0x81, 0xc3, 0xc5, 0x04, 0x17, 0x02, 0x15, 0x00, 0x81, 0x9d, 0xfd, 0x53, 0x0c,
- 0xc1, 0x8f, 0xbe, 0x8b, 0xea, 0x00, 0x26, 0x19, 0x29, 0x33, 0x91, 0x84, 0xbe, 0xad, 0x81};
-unsigned int dsa_privkey_pk8_der_len = 335;
-
-unsigned char ec_privkey_pk8_der[] = {
- 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
- 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02,
- 0x01, 0x01, 0x04, 0x20, 0x73, 0x7c, 0x2e, 0xcd, 0x7b, 0x8d, 0x19, 0x40, 0xbf, 0x29, 0x30, 0xaa,
- 0x9b, 0x4e, 0xd3, 0xff, 0x94, 0x1e, 0xed, 0x09, 0x36, 0x6b, 0xc0, 0x32, 0x99, 0x98, 0x64, 0x81,
- 0xf3, 0xa4, 0xd8, 0x59, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xbf, 0x85, 0xd7, 0x72, 0x0d, 0x07,
- 0xc2, 0x54, 0x61, 0x68, 0x3b, 0xc6, 0x48, 0xb4, 0x77, 0x8a, 0x9a, 0x14, 0xdd, 0x8a, 0x02, 0x4e,
- 0x3b, 0xdd, 0x8c, 0x7d, 0xdd, 0x9a, 0xb2, 0xb5, 0x28, 0xbb, 0xc7, 0xaa, 0x1b, 0x51, 0xf1, 0x4e,
- 0xbb, 0xbb, 0x0b, 0xd0, 0xce, 0x21, 0xbc, 0xc4, 0x1c, 0x6e, 0xb0, 0x00, 0x83, 0xcf, 0x33, 0x76,
- 0xd1, 0x1f, 0xd4, 0x49, 0x49, 0xe0, 0xb2, 0x18, 0x3b, 0xfe};
-unsigned int ec_privkey_pk8_der_len = 138;
-
-keymaster_key_param_t ec_params[] = {
- keymaster_param_enum(KM_TAG_ALGORITHM, KM_ALGORITHM_EC),
- keymaster_param_long(KM_TAG_EC_CURVE, KM_EC_CURVE_P_521),
- keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_SIGN),
- keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_VERIFY),
- keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
- keymaster_param_bool(KM_TAG_NO_AUTH_REQUIRED),
-};
-keymaster_key_param_set_t ec_param_set = {ec_params, sizeof(ec_params) / sizeof(*ec_params)};
-
-keymaster_key_param_t rsa_params[] = {
- keymaster_param_enum(KM_TAG_ALGORITHM, KM_ALGORITHM_RSA),
- keymaster_param_int(KM_TAG_KEY_SIZE, 1024),
- keymaster_param_long(KM_TAG_RSA_PUBLIC_EXPONENT, 65537),
- keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_SIGN),
- keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_VERIFY),
- keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE),
- keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
- keymaster_param_bool(KM_TAG_NO_AUTH_REQUIRED),
-};
-keymaster_key_param_set_t rsa_param_set = {rsa_params, sizeof(rsa_params) / sizeof(*rsa_params)};
-
-struct EVP_PKEY_Delete {
- void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); }
-};
-
-struct EVP_PKEY_CTX_Delete {
- void operator()(EVP_PKEY_CTX* p) { EVP_PKEY_CTX_free(p); }
-};
-
-static bool do_operation(TrustyKeymasterDevice* device, keymaster_purpose_t purpose,
- keymaster_key_blob_t* key, keymaster_blob_t* input,
- keymaster_blob_t* signature, keymaster_blob_t* output) {
- keymaster_key_param_t params[] = {
- keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE),
- keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
- };
- keymaster_key_param_set_t param_set = {params, sizeof(params) / sizeof(*params)};
- keymaster_operation_handle_t op_handle;
- keymaster_error_t error = device->begin(purpose, key, ¶m_set, nullptr, &op_handle);
- if (error != KM_ERROR_OK) {
- printf("Keymaster begin() failed: %d\n", error);
- return false;
- }
- size_t input_consumed;
- error = device->update(op_handle, nullptr, input, &input_consumed, nullptr, nullptr);
- if (error != KM_ERROR_OK) {
- printf("Keymaster update() failed: %d\n", error);
- return false;
- }
- if (input_consumed != input->data_length) {
- // This should never happen. If it does, it's a bug in the keymaster implementation.
- printf("Keymaster update() did not consume all data.\n");
- device->abort(op_handle);
- return false;
- }
- error = device->finish(op_handle, nullptr, nullptr, signature, nullptr, output);
- if (error != KM_ERROR_OK) {
- printf("Keymaster finish() failed: %d\n", error);
- return false;
- }
- return true;
-}
-
-static bool test_import_rsa(TrustyKeymasterDevice* device) {
- printf("===================\n");
- printf("= RSA Import Test =\n");
- printf("===================\n\n");
-
- printf("=== Importing RSA keypair === \n");
- keymaster_key_blob_t key;
- keymaster_blob_t private_key = {rsa_privkey_pk8_der, rsa_privkey_pk8_der_len};
- int error = device->import_key(&rsa_param_set, KM_KEY_FORMAT_PKCS8, &private_key, &key, nullptr);
- if (error != KM_ERROR_OK) {
- printf("Error importing RSA key: %d\n\n", error);
- return false;
- }
- std::unique_ptr<const uint8_t[]> key_deleter(key.key_material);
-
- printf("=== Signing with imported RSA key ===\n");
- size_t message_len = 1024 / 8;
- std::unique_ptr<uint8_t[]> message(new uint8_t[message_len]);
- memset(message.get(), 'a', message_len);
- keymaster_blob_t input = {message.get(), message_len}, signature;
-
- if (!do_operation(device, KM_PURPOSE_SIGN, &key, &input, nullptr, &signature)) {
- printf("Error signing data with imported RSA key\n\n");
- return false;
- }
- std::unique_ptr<const uint8_t[]> signature_deleter(signature.data);
-
- printf("=== Verifying with imported RSA key === \n");
- if (!do_operation(device, KM_PURPOSE_VERIFY, &key, &input, &signature, nullptr)) {
- printf("Error verifying data with imported RSA key\n\n");
- return false;
- }
-
- printf("\n");
- return true;
-}
-
-static bool test_rsa(TrustyKeymasterDevice* device) {
- printf("============\n");
- printf("= RSA Test =\n");
- printf("============\n\n");
-
- printf("=== Generating RSA key pair ===\n");
- keymaster_key_blob_t key;
- int error = device->generate_key(&rsa_param_set, &key, nullptr);
- if (error != KM_ERROR_OK) {
- printf("Error generating RSA key pair: %d\n\n", error);
- return false;
- }
- std::unique_ptr<const uint8_t[]> key_deleter(key.key_material);
-
- printf("=== Signing with RSA key === \n");
- size_t message_len = 1024 / 8;
- std::unique_ptr<uint8_t[]> message(new uint8_t[message_len]);
- memset(message.get(), 'a', message_len);
- keymaster_blob_t input = {message.get(), message_len}, signature;
-
- if (!do_operation(device, KM_PURPOSE_SIGN, &key, &input, nullptr, &signature)) {
- printf("Error signing data with RSA key\n\n");
- return false;
- }
- std::unique_ptr<const uint8_t[]> signature_deleter(signature.data);
-
- printf("=== Verifying with RSA key === \n");
- if (!do_operation(device, KM_PURPOSE_VERIFY, &key, &input, &signature, nullptr)) {
- printf("Error verifying data with RSA key\n\n");
- return false;
- }
-
- printf("=== Exporting RSA public key ===\n");
- keymaster_blob_t exported_key;
- error = device->export_key(KM_KEY_FORMAT_X509, &key, nullptr, nullptr, &exported_key);
- if (error != KM_ERROR_OK) {
- printf("Error exporting RSA public key: %d\n\n", error);
- return false;
- }
-
- printf("=== Verifying with exported key ===\n");
- const uint8_t* tmp = exported_key.data;
- std::unique_ptr<EVP_PKEY, EVP_PKEY_Delete> pkey(
- d2i_PUBKEY(NULL, &tmp, exported_key.data_length));
- std::unique_ptr<EVP_PKEY_CTX, EVP_PKEY_CTX_Delete> ctx(EVP_PKEY_CTX_new(pkey.get(), NULL));
- if (EVP_PKEY_verify_init(ctx.get()) != 1) {
- printf("Error initializing openss EVP context\n\n");
- return false;
- }
- if (EVP_PKEY_type(pkey->type) != EVP_PKEY_RSA) {
- printf("Exported key was the wrong type?!?\n\n");
- return false;
- }
-
- EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_NO_PADDING);
- if (EVP_PKEY_verify(ctx.get(), signature.data, signature.data_length, message.get(),
- message_len) != 1) {
- printf("Verification with exported pubkey failed.\n\n");
- return false;
- } else {
- printf("Verification succeeded\n");
- }
-
- printf("\n");
- return true;
-}
-
-static bool test_import_ecdsa(TrustyKeymasterDevice* device) {
- printf("=====================\n");
- printf("= ECDSA Import Test =\n");
- printf("=====================\n\n");
-
- printf("=== Importing ECDSA keypair === \n");
- keymaster_key_blob_t key;
- keymaster_blob_t private_key = {ec_privkey_pk8_der, ec_privkey_pk8_der_len};
- int error = device->import_key(&ec_param_set, KM_KEY_FORMAT_PKCS8, &private_key, &key, nullptr);
- if (error != KM_ERROR_OK) {
- printf("Error importing ECDSA key: %d\n\n", error);
- return false;
- }
- std::unique_ptr<const uint8_t[]> deleter(key.key_material);
-
- printf("=== Signing with imported ECDSA key ===\n");
- size_t message_len = 30 /* arbitrary */;
- std::unique_ptr<uint8_t[]> message(new uint8_t[message_len]);
- memset(message.get(), 'a', message_len);
- keymaster_blob_t input = {message.get(), message_len}, signature;
-
- if (!do_operation(device, KM_PURPOSE_SIGN, &key, &input, nullptr, &signature)) {
- printf("Error signing data with imported ECDSA key\n\n");
- return false;
- }
- std::unique_ptr<const uint8_t[]> signature_deleter(signature.data);
-
- printf("=== Verifying with imported ECDSA key === \n");
- if (!do_operation(device, KM_PURPOSE_VERIFY, &key, &input, &signature, nullptr)) {
- printf("Error verifying data with imported ECDSA key\n\n");
- return false;
- }
-
- printf("\n");
- return true;
-}
-
-static bool test_ecdsa(TrustyKeymasterDevice* device) {
- printf("==============\n");
- printf("= ECDSA Test =\n");
- printf("==============\n\n");
-
- printf("=== Generating ECDSA key pair ===\n");
- keymaster_key_blob_t key;
- int error = device->generate_key(&ec_param_set, &key, nullptr);
- if (error != KM_ERROR_OK) {
- printf("Error generating ECDSA key pair: %d\n\n", error);
- return false;
- }
- std::unique_ptr<const uint8_t[]> key_deleter(key.key_material);
-
- printf("=== Signing with ECDSA key === \n");
- size_t message_len = 30 /* arbitrary */;
- std::unique_ptr<uint8_t[]> message(new uint8_t[message_len]);
- memset(message.get(), 'a', message_len);
- keymaster_blob_t input = {message.get(), message_len}, signature;
-
- if (!do_operation(device, KM_PURPOSE_SIGN, &key, &input, nullptr, &signature)) {
- printf("Error signing data with ECDSA key\n\n");
- return false;
- }
- std::unique_ptr<const uint8_t[]> signature_deleter(signature.data);
-
- printf("=== Verifying with ECDSA key === \n");
- if (!do_operation(device, KM_PURPOSE_VERIFY, &key, &input, &signature, nullptr)) {
- printf("Error verifying data with ECDSA key\n\n");
- return false;
- }
-
- printf("=== Exporting ECDSA public key ===\n");
- keymaster_blob_t exported_key;
- error = device->export_key(KM_KEY_FORMAT_X509, &key, nullptr, nullptr, &exported_key);
- if (error != KM_ERROR_OK) {
- printf("Error exporting ECDSA public key: %d\n\n", error);
- return false;
- }
-
- printf("=== Verifying with exported key ===\n");
- const uint8_t* tmp = exported_key.data;
- std::unique_ptr<EVP_PKEY, EVP_PKEY_Delete> pkey(
- d2i_PUBKEY(NULL, &tmp, exported_key.data_length));
- std::unique_ptr<EVP_PKEY_CTX, EVP_PKEY_CTX_Delete> ctx(EVP_PKEY_CTX_new(pkey.get(), NULL));
- if (EVP_PKEY_verify_init(ctx.get()) != 1) {
- printf("Error initializing openssl EVP context\n\n");
- return false;
- }
- if (EVP_PKEY_type(pkey->type) != EVP_PKEY_EC) {
- printf("Exported key was the wrong type?!?\n\n");
- return false;
- }
-
- if (EVP_PKEY_verify(ctx.get(), signature.data, signature.data_length, message.get(),
- message_len) != 1) {
- printf("Verification with exported pubkey failed.\n\n");
- return false;
- } else {
- printf("Verification succeeded\n");
- }
-
- printf("\n");
- return true;
-}
-
-int main(void) {
- TrustyKeymasterDevice device(NULL);
- keymaster::ConfigureDevice(reinterpret_cast<keymaster2_device_t*>(&device));
- if (device.session_error() != KM_ERROR_OK) {
- printf("Failed to initialize Trusty session: %d\n", device.session_error());
- return 1;
- }
- printf("Trusty session initialized\n");
-
- bool success = true;
- success &= test_rsa(&device);
- success &= test_import_rsa(&device);
- success &= test_ecdsa(&device);
- success &= test_import_ecdsa(&device);
-
- if (success) {
- printf("\nTESTS PASSED!\n");
- } else {
- printf("\n!!!!TESTS FAILED!!!\n");
- }
-
- return success ? 0 : 1;
-}
diff --git a/trusty/libtrusty/tipc-test/tipc_test.c b/trusty/libtrusty/tipc-test/tipc_test.c
index 1fb34c9..d20d4ee 100644
--- a/trusty/libtrusty/tipc-test/tipc_test.c
+++ b/trusty/libtrusty/tipc-test/tipc_test.c
@@ -587,8 +587,15 @@
static int ta2ta_ipc_test(void)
{
+ enum test_message_header {
+ TEST_PASSED = 0,
+ TEST_FAILED = 1,
+ TEST_MESSAGE = 2,
+ };
+
int fd;
- char rx_buf[64];
+ int ret;
+ unsigned char rx_buf[256];
if (!opt_silent) {
printf("%s:\n", __func__);
@@ -601,12 +608,31 @@
return fd;
}
- /* wait for test to complete */
- (void) read(fd, rx_buf, sizeof(rx_buf));
+ /* Wait for tests to complete and read status */
+ while (true) {
+ ret = read(fd, rx_buf, sizeof(rx_buf));
+ if (ret <= 0 || ret >= (int)sizeof(rx_buf)) {
+ fprintf(stderr, "%s: Read failed: %d\n", __func__, ret);
+ tipc_close(fd);
+ return -1;
+ }
+
+ if (rx_buf[0] == TEST_PASSED) {
+ break;
+ } else if (rx_buf[0] == TEST_FAILED) {
+ break;
+ } else if (rx_buf[0] == TEST_MESSAGE) {
+ write(STDOUT_FILENO, rx_buf + 1, ret - 1);
+ } else {
+ fprintf(stderr, "%s: Bad message header: %d\n",
+ __func__, rx_buf[0]);
+ break;
+ }
+ }
tipc_close(fd);
- return 0;
+ return rx_buf[0] == TEST_PASSED ? 0 : -1;
}
typedef struct uuid
diff --git a/trusty/trusty-base.mk b/trusty/trusty-base.mk
index 9c3a7df..0a0ecec 100644
--- a/trusty/trusty-base.mk
+++ b/trusty/trusty-base.mk
@@ -20,7 +20,7 @@
#
PRODUCT_PACKAGES += \
- keystore.trusty \
+ android.hardware.keymaster@3.0-service.trusty \
gatekeeper.trusty
PRODUCT_PROPERTY_OVERRIDES += \
diff --git a/watchdogd/Android.bp b/watchdogd/Android.bp
new file mode 100644
index 0000000..0fbc33c
--- /dev/null
+++ b/watchdogd/Android.bp
@@ -0,0 +1,14 @@
+cc_binary {
+ name: "watchdogd",
+ recovery_available: true,
+ srcs: ["watchdogd.cpp"],
+ cflags: [
+ "-Wall",
+ "-Wextra",
+ "-Werror",
+ ],
+ shared_libs: ["libbase"],
+ sanitize: {
+ misc_undefined: ["signed-integer-overflow"],
+ },
+}
diff --git a/init/watchdogd.cpp b/watchdogd/watchdogd.cpp
similarity index 77%
rename from init/watchdogd.cpp
rename to watchdogd/watchdogd.cpp
index e0164b4..5dc41e6 100644
--- a/init/watchdogd.cpp
+++ b/watchdogd/watchdogd.cpp
@@ -23,19 +23,10 @@
#include <android-base/logging.h>
-#include "log.h"
-
-#ifdef _INIT_INIT_H
-#error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals"
-#endif
-
#define DEV_NAME "/dev/watchdog"
-namespace android {
-namespace init {
-
-int watchdogd_main(int argc, char **argv) {
- InitKernelLogging(argv);
+int main(int argc, char** argv) {
+ android::base::InitLogging(argv, &android::base::KernelLogger);
int interval = 10;
if (argc >= 2) interval = atoi(argv[1]);
@@ -45,7 +36,7 @@
LOG(INFO) << "watchdogd started (interval " << interval << ", margin " << margin << ")!";
- int fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
+ int fd = open(DEV_NAME, O_RDWR | O_CLOEXEC);
if (fd == -1) {
PLOG(ERROR) << "Failed to open " << DEV_NAME;
return 1;
@@ -65,9 +56,8 @@
interval = 1;
}
LOG(WARNING) << "Adjusted interval to timeout returned by driver: "
- << "timeout " << timeout
- << ", interval " << interval
- << ", margin " << margin;
+ << "timeout " << timeout << ", interval " << interval << ", margin "
+ << margin;
}
}
@@ -76,6 +66,3 @@
sleep(interval);
}
}
-
-} // namespace init
-} // namespace android