Merge "zram: support zram_writeback"
diff --git a/adb/client/adb_install.cpp b/adb/client/adb_install.cpp
index 054cbac..21e0874 100644
--- a/adb/client/adb_install.cpp
+++ b/adb/client/adb_install.cpp
@@ -24,12 +24,14 @@
 #include <string>
 #include <vector>
 
+#include <android-base/file.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+
 #include "adb.h"
 #include "adb_client.h"
+#include "adb_unique_fd.h"
 #include "adb_utils.h"
-#include "android-base/file.h"
-#include "android-base/stringprintf.h"
-#include "android-base/strings.h"
 #include "client/file_sync_client.h"
 #include "commandline.h"
 #include "fastdeploy.h"
@@ -179,8 +181,8 @@
             return 1;
         }
 
-        int localFd = adb_open(file, O_RDONLY);
-        if (localFd < 0) {
+        unique_fd local_fd(adb_open(file, O_RDONLY | O_CLOEXEC));
+        if (local_fd < 0) {
             fprintf(stderr, "adb: failed to open %s: %s\n", file, strerror(errno));
             return 1;
         }
@@ -201,19 +203,15 @@
             cmd += " --apex";
         }
 
-        int remoteFd = adb_connect(cmd, &error);
-        if (remoteFd < 0) {
+        unique_fd remote_fd(adb_connect(cmd, &error));
+        if (remote_fd < 0) {
             fprintf(stderr, "adb: connect error for write: %s\n", error.c_str());
-            adb_close(localFd);
             return 1;
         }
 
         char buf[BUFSIZ];
-        copy_to_file(localFd, remoteFd);
-        read_status_line(remoteFd, buf, sizeof(buf));
-
-        adb_close(localFd);
-        adb_close(remoteFd);
+        copy_to_file(local_fd.get(), remote_fd.get());
+        read_status_line(remote_fd.get(), buf, sizeof(buf));
 
         if (!strncmp("Success", buf, 7)) {
             fputs(buf, stdout);
@@ -410,14 +408,15 @@
 
     // Create install session
     std::string error;
-    int fd = adb_connect(cmd, &error);
-    if (fd < 0) {
-        fprintf(stderr, "adb: connect error for create: %s\n", error.c_str());
-        return EXIT_FAILURE;
-    }
     char buf[BUFSIZ];
-    read_status_line(fd, buf, sizeof(buf));
-    adb_close(fd);
+    {
+        unique_fd fd(adb_connect(cmd, &error));
+        if (fd < 0) {
+            fprintf(stderr, "adb: connect error for create: %s\n", error.c_str());
+            return EXIT_FAILURE;
+        }
+        read_status_line(fd.get(), buf, sizeof(buf));
+    }
 
     int session_id = -1;
     if (!strncmp("Success", buf, 7)) {
@@ -450,27 +449,23 @@
                                             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) {
+        unique_fd local_fd(adb_open(file, O_RDONLY | O_CLOEXEC));
+        if (local_fd < 0) {
             fprintf(stderr, "adb: failed to open %s: %s\n", file, strerror(errno));
             success = 0;
             goto finalize_session;
         }
 
         std::string error;
-        int remoteFd = adb_connect(cmd, &error);
-        if (remoteFd < 0) {
+        unique_fd remote_fd(adb_connect(cmd, &error));
+        if (remote_fd < 0) {
             fprintf(stderr, "adb: connect error for write: %s\n", error.c_str());
-            adb_close(localFd);
             success = 0;
             goto finalize_session;
         }
 
-        copy_to_file(localFd, remoteFd);
-        read_status_line(remoteFd, buf, sizeof(buf));
-
-        adb_close(localFd);
-        adb_close(remoteFd);
+        copy_to_file(local_fd.get(), remote_fd.get());
+        read_status_line(remote_fd.get(), buf, sizeof(buf));
 
         if (strncmp("Success", buf, 7)) {
             fprintf(stderr, "adb: failed to write %s\n", file);
@@ -484,13 +479,14 @@
     // Commit session if we streamed everything okay; otherwise abandon
     std::string service = android::base::StringPrintf("%s install-%s %d", install_cmd.c_str(),
                                                       success ? "commit" : "abandon", session_id);
-    fd = adb_connect(service, &error);
-    if (fd < 0) {
-        fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str());
-        return EXIT_FAILURE;
+    {
+        unique_fd fd(adb_connect(service, &error));
+        if (fd < 0) {
+            fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str());
+            return EXIT_FAILURE;
+        }
+        read_status_line(fd.get(), buf, sizeof(buf));
     }
-    read_status_line(fd, buf, sizeof(buf));
-    adb_close(fd);
 
     if (!strncmp("Success", buf, 7)) {
         fputs(buf, stdout);
@@ -501,6 +497,213 @@
     return EXIT_FAILURE;
 }
 
+int install_multi_package(int argc, const char** argv) {
+    // Find all APK arguments starting at end.
+    // All other arguments passed through verbatim.
+    bool apex_found = false;
+    int first_package = -1;
+    for (int i = argc - 1; i >= 0; i--) {
+        const char* file = argv[i];
+        if (android::base::EndsWithIgnoreCase(file, ".apk") ||
+            android::base::EndsWithIgnoreCase(file, ".apex")) {
+            first_package = i;
+            if (android::base::EndsWithIgnoreCase(file, ".apex")) {
+                apex_found = true;
+            }
+        } else {
+            break;
+        }
+    }
+
+    if (first_package == -1) error_exit("need APK or APEX files on command line");
+
+    if (use_legacy_install()) {
+        fprintf(stderr, "adb: multi-package install is not supported on this device\n");
+        return EXIT_FAILURE;
+    }
+    std::string install_cmd = "exec:cmd package";
+
+    std::string multi_package_cmd =
+            android::base::StringPrintf("%s install-create --multi-package", install_cmd.c_str());
+    if (apex_found) {
+        multi_package_cmd += " --staged";
+    }
+
+    // Create multi-package install session
+    std::string error;
+    char buf[BUFSIZ];
+    {
+        unique_fd fd(adb_connect(multi_package_cmd, &error));
+        if (fd < 0) {
+            fprintf(stderr, "adb: connect error for create multi-package: %s\n", error.c_str());
+            return EXIT_FAILURE;
+        }
+        read_status_line(fd.get(), buf, sizeof(buf));
+    }
+
+    int parent_session_id = -1;
+    if (!strncmp("Success", buf, 7)) {
+        char* start = strrchr(buf, '[');
+        char* end = strrchr(buf, ']');
+        if (start && end) {
+            *end = '\0';
+            parent_session_id = strtol(start + 1, nullptr, 10);
+        }
+    }
+    if (parent_session_id < 0) {
+        fprintf(stderr, "adb: failed to create multi-package session\n");
+        fputs(buf, stderr);
+        return EXIT_FAILURE;
+    }
+
+    fprintf(stdout, "Created parent session ID %d.\n", parent_session_id);
+
+    std::vector<int> session_ids;
+
+    // Valid session, now create the individual sessions and stream the APKs
+    int success = EXIT_FAILURE;
+    std::string individual_cmd =
+            android::base::StringPrintf("%s install-create", install_cmd.c_str());
+    std::string all_session_ids = "";
+    for (int i = 1; i < first_package; i++) {
+        individual_cmd += " " + escape_arg(argv[i]);
+    }
+    if (apex_found) {
+        individual_cmd += " --staged";
+    }
+    std::string individual_apex_cmd = individual_cmd + " --apex";
+    std::string cmd = "";
+    for (int i = first_package; i < argc; i++) {
+        const char* file = argv[i];
+        char buf[BUFSIZ];
+        {
+            unique_fd fd;
+            // Create individual install session
+            if (android::base::EndsWithIgnoreCase(file, ".apex")) {
+                fd.reset(adb_connect(individual_apex_cmd, &error));
+            } else {
+                fd.reset(adb_connect(individual_cmd, &error));
+            }
+            if (fd < 0) {
+                fprintf(stderr, "adb: connect error for create: %s\n", error.c_str());
+                goto finalize_multi_package_session;
+            }
+            read_status_line(fd.get(), buf, sizeof(buf));
+        }
+
+        int session_id = -1;
+        if (!strncmp("Success", buf, 7)) {
+            char* start = strrchr(buf, '[');
+            char* end = strrchr(buf, ']');
+            if (start && end) {
+                *end = '\0';
+                session_id = strtol(start + 1, nullptr, 10);
+            }
+        }
+        if (session_id < 0) {
+            fprintf(stderr, "adb: failed to create multi-package session\n");
+            fputs(buf, stderr);
+            goto finalize_multi_package_session;
+        }
+
+        fprintf(stdout, "Created child session ID %d.\n", session_id);
+        session_ids.push_back(session_id);
+
+        struct stat sb;
+        if (stat(file, &sb) == -1) {
+            fprintf(stderr, "adb: failed to stat %s: %s\n", file, strerror(errno));
+            goto finalize_multi_package_session;
+        }
+
+        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());
+
+        unique_fd local_fd(adb_open(file, O_RDONLY | O_CLOEXEC));
+        if (local_fd < 0) {
+            fprintf(stderr, "adb: failed to open %s: %s\n", file, strerror(errno));
+            goto finalize_multi_package_session;
+        }
+
+        std::string error;
+        unique_fd remote_fd(adb_connect(cmd, &error));
+        if (remote_fd < 0) {
+            fprintf(stderr, "adb: connect error for write: %s\n", error.c_str());
+            goto finalize_multi_package_session;
+        }
+
+        copy_to_file(local_fd.get(), remote_fd.get());
+        read_status_line(remote_fd.get(), buf, sizeof(buf));
+
+        if (strncmp("Success", buf, 7)) {
+            fprintf(stderr, "adb: failed to write %s\n", file);
+            fputs(buf, stderr);
+            goto finalize_multi_package_session;
+        }
+
+        all_session_ids += android::base::StringPrintf(" %d", session_id);
+    }
+
+    cmd = android::base::StringPrintf("%s install-add-session %d%s", install_cmd.c_str(),
+                                      parent_session_id, all_session_ids.c_str());
+    {
+        unique_fd fd(adb_connect(cmd, &error));
+        if (fd < 0) {
+            fprintf(stderr, "adb: connect error for install-add-session: %s\n", error.c_str());
+            goto finalize_multi_package_session;
+        }
+        read_status_line(fd.get(), buf, sizeof(buf));
+    }
+
+    if (strncmp("Success", buf, 7)) {
+        fprintf(stderr, "adb: failed to link sessions (%s)\n", cmd.c_str());
+        fputs(buf, stderr);
+        goto finalize_multi_package_session;
+    }
+
+    // no failures means we can proceed with the assumption of success
+    success = 0;
+
+finalize_multi_package_session:
+    // Commit session if we streamed everything okay; otherwise abandon
+    std::string service =
+            android::base::StringPrintf("%s install-%s %d", install_cmd.c_str(),
+                                        success == 0 ? "commit" : "abandon", parent_session_id);
+    {
+        unique_fd fd(adb_connect(service, &error));
+        if (fd < 0) {
+            fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str());
+            return EXIT_FAILURE;
+        }
+        read_status_line(fd.get(), buf, sizeof(buf));
+    }
+
+    if (!strncmp("Success", buf, 7)) {
+        fputs(buf, stdout);
+        if (success == 0) {
+            return 0;
+        }
+    } else {
+        fprintf(stderr, "adb: failed to finalize session\n");
+        fputs(buf, stderr);
+    }
+
+    // try to abandon all remaining sessions
+    for (std::size_t i = 0; i < session_ids.size(); i++) {
+        service = android::base::StringPrintf("%s install-abandon %d", install_cmd.c_str(),
+                                              session_ids[i]);
+        fprintf(stderr, "Attempting to abandon session ID %d\n", session_ids[i]);
+        unique_fd fd(adb_connect(service, &error));
+        if (fd < 0) {
+            fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str());
+            continue;
+        }
+        read_status_line(fd.get(), buf, sizeof(buf));
+    }
+    return EXIT_FAILURE;
+}
+
 int delete_device_file(const std::string& filename) {
     std::string cmd = "rm -f " + escape_arg(filename);
     return send_shell_command(cmd);
diff --git a/adb/client/adb_install.h b/adb/client/adb_install.h
index 5b6c4cb..9946604 100644
--- a/adb/client/adb_install.h
+++ b/adb/client/adb_install.h
@@ -20,6 +20,7 @@
 
 int install_app(int argc, const char** argv);
 int install_multiple_app(int argc, const char** argv);
+int install_multi_package(int argc, const char** argv);
 int uninstall_app(int argc, const char** argv);
 
 int delete_device_file(const std::string& filename);
diff --git a/adb/client/commandline.cpp b/adb/client/commandline.cpp
index ce51b1c..9bc42e1 100644
--- a/adb/client/commandline.cpp
+++ b/adb/client/commandline.cpp
@@ -142,10 +142,13 @@
         "     -x: disable remote exit codes and stdout/stderr separation\n"
         " emu COMMAND              run emulator console command\n"
         "\n"
-        "app installation:\n"
+        "app installation (see also `adb shell cmd package help`):\n"
         " install [-lrtsdg] [--instant] PACKAGE\n"
+        "     push a single package to the device and install it\n"
         " install-multiple [-lrtsdpg] [--instant] PACKAGE...\n"
-        "     push package(s) to the device and install them\n"
+        "     push multiple APKs to the device for a single package and install them\n"
+        " install-multi-package [-lrtsdpg] [--instant] PACKAGE...\n"
+        "     push one or more packages to the device and install them atomically\n"
         "     -r: replace existing application\n"
         "     -t: allow test packages\n"
         "     -d: allow version code downgrade (debuggable packages only)\n"
@@ -192,7 +195,9 @@
         " get-state                print offline | bootloader | device\n"
         " get-serialno             print <serial-number>\n"
         " get-devpath              print <device-path>\n"
-        " remount                  remount partitions read-write\n"
+        " remount [-R]\n"
+        "      remount partitions read-write. if a reboot is required, -R will\n"
+        "      will automatically reboot the device.\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"
@@ -1732,6 +1737,9 @@
     } else if (!strcmp(argv[0], "install-multiple")) {
         if (argc < 2) error_exit("install-multiple requires an argument");
         return install_multiple_app(argc, argv);
+    } else if (!strcmp(argv[0], "install-multi-package")) {
+        if (argc < 3) error_exit("install-multi-package requires an argument");
+        return install_multi_package(argc, argv);
     } else if (!strcmp(argv[0], "uninstall")) {
         if (argc < 2) error_exit("uninstall requires an argument");
         return uninstall_app(argc, argv);
diff --git a/adb/daemon/mdns.cpp b/adb/daemon/mdns.cpp
index 849378f..3530f48 100644
--- a/adb/daemon/mdns.cpp
+++ b/adb/daemon/mdns.cpp
@@ -74,7 +74,7 @@
 
     if (error != kDNSServiceErr_NoError) {
         LOG(ERROR) << "Could not register mDNS service (" << error << ").";
-        mdns_registered = false;
+        return;
     }
 
     mdns_registered = true;
diff --git a/adb/daemon/remount_service.cpp b/adb/daemon/remount_service.cpp
index 80b3e06..3c9dd04 100644
--- a/adb/daemon/remount_service.cpp
+++ b/adb/daemon/remount_service.cpp
@@ -240,59 +240,57 @@
     std::vector<std::string> partitions{"/",        "/odm",   "/oem", "/product_services",
                                         "/product", "/vendor"};
 
-    bool verity_enabled = (system_verified || vendor_verified);
+    if (system_verified || vendor_verified) {
+        // Disable verity automatically (reboot will be required).
+        set_verity_enabled_state_service(unique_fd(dup(fd.get())), false);
 
-    // 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()) {
+        // If overlayfs is not supported, we try and remount or set up
+        // un-deduplication. If it is supported, we can go ahead and wait for
+        // a reboot.
+        if (fs_mgr_overlayfs_valid() != OverlayfsValidResult::kNotSupported) {
+            if (user_requested_reboot) {
+                if (android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot")) {
+                    WriteFdExactly(fd.get(), "rebooting device\n");
+                } else {
+                    WriteFdExactly(fd.get(), "reboot failed\n");
+                }
+            }
+            return;
+        }
+    } else if (fs_mgr_overlayfs_setup()) {
+        // If we can use overlayfs, lets get it in place first before we
+        // struggle with determining deduplication operations.
         Fstab fstab;
         if (ReadDefaultFstab(&fstab) && fs_mgr_overlayfs_mount_all(&fstab)) {
             WriteFdExactly(fd.get(), "overlayfs mounted\n");
         }
     }
 
-    // Find partitions that are deduplicated, and can be un-deduplicated.
+    // If overlayfs is supported, we don't bother trying to un-deduplicate
+    // partitions.
     std::set<std::string> dedup;
-    for (const auto& part : partitions) {
-        auto partition = part;
-        if ((part == "/") && !find_mount("/system", false).empty()) partition = "/system";
-        std::string dev = find_mount(partition.c_str(), partition == "/");
-        if (dev.empty() || !fs_mgr_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);
+    if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kNotSupported) {
+        // Find partitions that are deduplicated, and can be un-deduplicated.
+        for (const auto& part : partitions) {
+            auto partition = part;
+            if ((part == "/") && !find_mount("/system", false).empty()) partition = "/system";
+            std::string dev = find_mount(partition.c_str(), partition == "/");
+            if (dev.empty() || !fs_mgr_has_shared_blocks(partition, dev)) {
+                continue;
             }
-            reboot_for_remount(fd.get(), !dedup.empty());
-            return;
+            if (can_unshare_blocks(fd.get(), dev.c_str())) {
+                dedup.emplace(partition);
+            }
         }
-        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.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");
+        // Reboot now if the user requested it (and an operation needs a reboot).
+        if (user_requested_reboot) {
+            if (!dedup.empty()) {
+                reboot_for_remount(fd.get(), !dedup.empty());
+                return;
+            }
+            WriteFdExactly(fd.get(), "No reboot needed, skipping -R.\n");
+        }
     }
 
     bool success = true;
diff --git a/base/include/android-base/unique_fd.h b/base/include/android-base/unique_fd.h
index 4e6c879..c8d12cf 100644
--- a/base/include/android-base/unique_fd.h
+++ b/base/include/android-base/unique_fd.h
@@ -103,7 +103,7 @@
   void reset(int new_value = -1) { reset(new_value, nullptr); }
 
   int get() const { return fd_; }
-  operator int() const { return get(); }
+  operator int() const { return get(); }  // NOLINT
 
   int release() __attribute__((warn_unused_result)) {
     tag(fd_, this, nullptr);
diff --git a/fs_mgr/Android.bp b/fs_mgr/Android.bp
index 1616a61..974e13e 100644
--- a/fs_mgr/Android.bp
+++ b/fs_mgr/Android.bp
@@ -59,6 +59,7 @@
         "libfs_avb",
         "libfstab",
         "libdm",
+        "libgsi",
     ],
     export_static_lib_headers: [
         "libfs_avb",
@@ -99,31 +100,14 @@
         "fs_mgr_boot_config.cpp",
         "fs_mgr_slotselect.cpp",
     ],
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
     export_include_dirs: ["include_fstab"],
-    header_libs: ["libbase_headers"],
-}
-
-cc_library_static {
-    name: "libfs_avb",
-    defaults: ["fs_mgr_defaults"],
-    recovery_available: true,
-    export_include_dirs: ["libfs_avb/include"],
-    srcs: [
-        "libfs_avb/avb_ops.cpp",
-        "libfs_avb/fs_avb.cpp",
-    ],
-    static_libs: [
-        "libavb",
-        "libfstab",
-        "libdm",
-    ],
-    export_static_lib_headers: [
-        "libfstab",
-    ],
-    shared_libs: [
-        "libcrypto",
-    ],
     header_libs: [
         "libbase_headers",
+        "libgsi_headers",
     ],
 }
diff --git a/fs_mgr/README.overlayfs.md b/fs_mgr/README.overlayfs.md
index 960410c..8784c94 100644
--- a/fs_mgr/README.overlayfs.md
+++ b/fs_mgr/README.overlayfs.md
@@ -53,9 +53,13 @@
     $ adb disable-verity
     $ adb reboot
 
-can be replaced with:
+*or*
 
-    $ adb reboot -R
+    $ adb remount
+
+can be replaced in both places with:
+
+    $ adb remount -R
 
 which will not reboot if everything is already prepared and ready
 to go.
diff --git a/fs_mgr/fs_mgr.cpp b/fs_mgr/fs_mgr.cpp
index 76d80ea..70a1045 100644
--- a/fs_mgr/fs_mgr.cpp
+++ b/fs_mgr/fs_mgr.cpp
@@ -524,13 +524,13 @@
 }
 
 // Mark the given block device as read-only, using the BLKROSET ioctl.
-bool fs_mgr_set_blk_ro(const std::string& blockdev) {
+bool fs_mgr_set_blk_ro(const std::string& blockdev, bool readonly) {
     unique_fd fd(TEMP_FAILURE_RETRY(open(blockdev.c_str(), O_RDONLY | O_CLOEXEC)));
     if (fd < 0) {
         return false;
     }
 
-    int ON = 1;
+    int ON = readonly;
     return ioctl(fd, BLKROSET, &ON) == 0;
 }
 
@@ -557,9 +557,17 @@
     mkdir(target.c_str(), 0755);
     errno = 0;
     unsigned long mountflags = entry.flags;
-    int ret = mount(source.c_str(), target.c_str(), entry.fs_type.c_str(), mountflags,
+    int ret = 0;
+    int save_errno = 0;
+    do {
+        if (save_errno == EAGAIN) {
+            PINFO << "Retrying mount (source=" << source << ",target=" << target
+                  << ",type=" << entry.fs_type << ")=" << ret << "(" << save_errno << ")";
+        }
+        ret = mount(source.c_str(), target.c_str(), entry.fs_type.c_str(), mountflags,
                     entry.fs_options.c_str());
-    int save_errno = errno;
+        save_errno = errno;
+    } while (ret && save_errno == EAGAIN);
     const char* target_missing = "";
     const char* source_missing = "";
     if (save_errno == ENOENT) {
@@ -955,6 +963,17 @@
     std::map<std::string, std::string> device_map_;
 };
 
+static bool IsMountPointMounted(const std::string& mount_point) {
+    // Check if this is already mounted.
+    Fstab fstab;
+    if (!ReadFstabFromFile("/proc/mounts", &fstab)) {
+        return false;
+    }
+    auto it = std::find_if(fstab.begin(), fstab.end(),
+                           [&](const auto& entry) { return entry.mount_point == mount_point; });
+    return it != fstab.end();
+}
+
 // 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.
 // Returns -1 on error, and  FS_MGR_MNTALL_* otherwise.
@@ -971,9 +990,18 @@
     for (size_t i = 0; i < fstab->size(); i++) {
         auto& current_entry = (*fstab)[i];
 
+        // If a filesystem should have been mounted in the first stage, we
+        // ignore it here. With one exception, if the filesystem is
+        // formattable, then it can only be formatted in the second stage,
+        // so we allow it to mount here.
+        if (current_entry.fs_mgr_flags.first_stage_mount &&
+            (!current_entry.fs_mgr_flags.formattable ||
+             IsMountPointMounted(current_entry.mount_point))) {
+            continue;
+        }
+
         // Don't mount entries that are managed by vold or not for the mount mode.
         if (current_entry.fs_mgr_flags.vold_managed || current_entry.fs_mgr_flags.recovery_only ||
-            current_entry.fs_mgr_flags.first_stage_mount ||
             ((mount_mode == MOUNT_MODE_LATE) && !current_entry.fs_mgr_flags.late_mount) ||
             ((mount_mode == MOUNT_MODE_EARLY) && current_entry.fs_mgr_flags.late_mount)) {
             continue;
diff --git a/fs_mgr/fs_mgr_fstab.cpp b/fs_mgr/fs_mgr_fstab.cpp
index 91f1b8c..c9f34a7 100644
--- a/fs_mgr/fs_mgr_fstab.cpp
+++ b/fs_mgr/fs_mgr_fstab.cpp
@@ -31,6 +31,7 @@
 #include <android-base/parseint.h>
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
+#include <libgsi/libgsi.h>
 
 #include "fs_mgr_priv.h"
 
@@ -658,6 +659,35 @@
     return boot_devices;
 }
 
+static void EraseFstabEntry(Fstab* fstab, const std::string& mount_point) {
+    auto iter = std::remove_if(fstab->begin(), fstab->end(),
+                               [&](const auto& entry) { return entry.mount_point == mount_point; });
+    fstab->erase(iter, fstab->end());
+}
+
+static void TransformFstabForGsi(Fstab* fstab) {
+    EraseFstabEntry(fstab, "/system");
+    EraseFstabEntry(fstab, "/data");
+
+    fstab->emplace_back(BuildGsiSystemFstabEntry());
+
+    constexpr uint32_t kFlags = MS_NOATIME | MS_NOSUID | MS_NODEV;
+
+    FstabEntry userdata = {
+            .blk_device = "userdata_gsi",
+            .mount_point = "/data",
+            .fs_type = "ext4",
+            .flags = kFlags,
+            .reserved_size = 128 * 1024 * 1024,
+    };
+    userdata.fs_mgr_flags.wait = true;
+    userdata.fs_mgr_flags.check = true;
+    userdata.fs_mgr_flags.logical = true;
+    userdata.fs_mgr_flags.quota = true;
+    userdata.fs_mgr_flags.late_mount = true;
+    fstab->emplace_back(userdata);
+}
+
 bool ReadFstabFromFile(const std::string& path, Fstab* fstab) {
     auto fstab_file = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
     if (!fstab_file) {
@@ -665,10 +695,15 @@
         return false;
     }
 
-    if (!fs_mgr_read_fstab_file(fstab_file.get(), path == "/proc/mounts", fstab)) {
+    bool is_proc_mounts = path == "/proc/mounts";
+
+    if (!fs_mgr_read_fstab_file(fstab_file.get(), is_proc_mounts, fstab)) {
         LERROR << __FUNCTION__ << "(): failed to load fstab from : '" << path << "'";
         return false;
     }
+    if (!is_proc_mounts && !access(android::gsi::kGsiBootedIndicatorFile, F_OK)) {
+        TransformFstabForGsi(fstab);
+    }
 
     return true;
 }
@@ -1051,3 +1086,17 @@
 int fs_mgr_is_checkpoint_blk(const struct fstab_rec* fstab) {
     return fstab->fs_mgr_flags & MF_CHECKPOINT_BLK;
 }
+
+FstabEntry BuildGsiSystemFstabEntry() {
+    FstabEntry system = {
+            .blk_device = "system_gsi",
+            .mount_point = "/system",
+            .fs_type = "ext4",
+            .flags = MS_RDONLY,
+            .fs_options = "barrier=1",
+    };
+    system.fs_mgr_flags.wait = true;
+    system.fs_mgr_flags.logical = true;
+    system.fs_mgr_flags.first_stage_mount = true;
+    return system;
+}
diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp
index 7dae7f1..0f89467 100644
--- a/fs_mgr/fs_mgr_overlayfs.cpp
+++ b/fs_mgr/fs_mgr_overlayfs.cpp
@@ -48,6 +48,7 @@
 #include <fs_mgr_overlayfs.h>
 #include <fstab/fstab.h>
 #include <libdm/dm.h>
+#include <libgsi/libgsi.h>
 #include <liblp/builder.h>
 #include <liblp/liblp.h>
 
@@ -277,6 +278,9 @@
     // Don't check entries that are managed by vold.
     if (entry->fs_mgr_flags.vold_managed || entry->fs_mgr_flags.recovery_only) return false;
 
+    // *_other doesn't want overlayfs.
+    if (entry->fs_mgr_flags.slot_select_other) return false;
+
     // Only concerned with readonly partitions.
     if (!(entry->flags & MS_RDONLY)) return false;
 
@@ -595,7 +599,11 @@
     entry.mount_point = kScratchMountPoint;
     entry.fs_type = mnt_type;
     entry.flags = MS_RELATIME;
-    if (readonly) entry.flags |= MS_RDONLY;
+    if (readonly) {
+        entry.flags |= MS_RDONLY;
+    } else {
+        fs_mgr_set_blk_ro(device_path, false);
+    }
     auto save_errno = errno;
     auto mounted = fs_mgr_do_mount_one(entry) == 0;
     if (!mounted) {
@@ -653,6 +661,7 @@
         return false;
     }
     command += " " + scratch_device;
+    fs_mgr_set_blk_ro(scratch_device, false);
     auto ret = system(command.c_str());
     if (ret) {
         LERROR << "make " << mnt_type << " filesystem on " << scratch_device << " return=" << ret;
@@ -794,8 +803,9 @@
 bool fs_mgr_overlayfs_invalid() {
     if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kNotSupported) return true;
 
-    // in recovery or fastbootd mode, not allowed!
+    // in recovery, fastbootd, or gsi mode, not allowed!
     if (fs_mgr_access("/system/bin/recovery")) return true;
+    if (android::gsi::IsGsiRunning()) return true;
 
     return false;
 }
diff --git a/fs_mgr/fs_mgr_priv.h b/fs_mgr/fs_mgr_priv.h
index 61d138d..39ceff7 100644
--- a/fs_mgr/fs_mgr_priv.h
+++ b/fs_mgr/fs_mgr_priv.h
@@ -140,7 +140,7 @@
                           const std::chrono::milliseconds relative_timeout,
                           FileWaitMode wait_mode = FileWaitMode::Exists);
 
-bool fs_mgr_set_blk_ro(const std::string& blockdev);
+bool fs_mgr_set_blk_ro(const std::string& blockdev, bool readonly = true);
 bool fs_mgr_update_for_slotselect(Fstab* fstab);
 bool fs_mgr_is_device_unlocked();
 const std::string& get_android_dt_dir();
diff --git a/fs_mgr/include_fstab/fstab/fstab.h b/fs_mgr/include_fstab/fstab/fstab.h
index 1ed9304..5d8496d 100644
--- a/fs_mgr/include_fstab/fstab/fstab.h
+++ b/fs_mgr/include_fstab/fstab/fstab.h
@@ -193,3 +193,6 @@
 FstabEntry FstabRecToFstabEntry(const fstab_rec* fstab_rec);
 Fstab LegacyFstabToFstab(const struct fstab* legacy_fstab);
 fstab* FstabToLegacyFstab(const Fstab& fstab);
+
+// Helper method to build a GSI fstab entry for mounting /system.
+FstabEntry BuildGsiSystemFstabEntry();
diff --git a/fs_mgr/libdm/Android.bp b/fs_mgr/libdm/Android.bp
index fc1aafb..c8c2d83 100644
--- a/fs_mgr/libdm/Android.bp
+++ b/fs_mgr/libdm/Android.bp
@@ -33,6 +33,11 @@
         "libbase_headers",
         "liblog_headers",
     ],
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
 }
 
 cc_test {
diff --git a/fs_mgr/libfs_avb/Android.bp b/fs_mgr/libfs_avb/Android.bp
new file mode 100644
index 0000000..6f368e4
--- /dev/null
+++ b/fs_mgr/libfs_avb/Android.bp
@@ -0,0 +1,105 @@
+//
+// Copyright (C) 2019 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: "libfs_avb",
+    defaults: ["fs_mgr_defaults"],
+    recovery_available: true,
+    host_supported: true,
+    export_include_dirs: ["include"],
+    srcs: [
+        "avb_ops.cpp",
+        "avb_util.cpp",
+        "fs_avb.cpp",
+        "util.cpp",
+    ],
+    static_libs: [
+        "libavb",
+        "libdm",
+        "libfstab",
+    ],
+    export_static_lib_headers: [
+        "libfstab",
+    ],
+    shared_libs: [
+        "libcrypto",
+    ],
+    header_libs: [
+        "libbase_headers",
+    ],
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+}
+
+cc_defaults {
+    name: "libfs_avb_host_test_defaults",
+    required: [
+        "avbtool",
+    ],
+    data: [
+        "tests/data/*",
+    ],
+    static_libs: [
+        "libgtest_host",
+    ],
+    shared_libs: [
+        "libbase",
+        "libchrome",
+    ],
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+    cflags: [
+        "-DHOST_TEST",
+    ],
+}
+
+cc_library_host_static {
+    name: "libfs_avb_test_util",
+    defaults: ["libfs_avb_host_test_defaults"],
+    srcs: [
+        "tests/fs_avb_test_util.cpp",
+    ],
+}
+
+cc_test_host {
+    name: "libfs_avb_test",
+    defaults: ["libfs_avb_host_test_defaults"],
+    static_libs: [
+        "libfs_avb_test_util",
+    ],
+    srcs: [
+        "tests/basic_test.cpp",
+    ],
+}
+
+cc_test_host {
+    name: "libfs_avb_internal_test",
+    defaults: ["libfs_avb_host_test_defaults"],
+    static_libs: [
+        "libfs_avb_test_util",
+        "libfstab",
+    ],
+    srcs: [
+        "util.cpp",
+        "tests/util_test.cpp",
+    ],
+}
diff --git a/fs_mgr/libfs_avb/avb_ops.cpp b/fs_mgr/libfs_avb/avb_ops.cpp
index c985a97..3efa794 100644
--- a/fs_mgr/libfs_avb/avb_ops.cpp
+++ b/fs_mgr/libfs_avb/avb_ops.cpp
@@ -37,7 +37,7 @@
 #include <libavb/libavb.h>
 #include <utils/Compat.h>
 
-#include "fs_mgr_priv.h"
+#include "util.h"
 
 using namespace std::literals;
 
@@ -127,7 +127,7 @@
     const std::string path = "/dev/block/by-name/"s + partition;
 
     // Ensures the device path (a symlink created by init) is ready to access.
-    if (!fs_mgr_wait_for_file(path, 1s)) {
+    if (!WaitForFile(path, 1s)) {
         return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
     }
 
@@ -186,6 +186,7 @@
             avb_slot_verify(&avb_ops_, requested_partitions, ab_suffix.c_str(), flags,
                             AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, &avb_slot_data);
 
+    if (!avb_slot_data) return verify_result;
     // Copies avb_slot_data->vbmeta_images[].
     for (size_t i = 0; i < avb_slot_data->num_vbmeta_images; i++) {
         out_vbmeta_images->emplace_back(VBMetaData(avb_slot_data->vbmeta_images[i].vbmeta_data,
diff --git a/fs_mgr/libfs_avb/avb_util.cpp b/fs_mgr/libfs_avb/avb_util.cpp
new file mode 100644
index 0000000..0ceb6ee
--- /dev/null
+++ b/fs_mgr/libfs_avb/avb_util.cpp
@@ -0,0 +1,177 @@
+/*
+ * Copyright (C) 2019 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 "avb_util.h"
+
+#include <array>
+#include <sstream>
+
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+
+#include "util.h"
+
+using android::base::unique_fd;
+
+namespace android {
+namespace fs_mgr {
+
+// 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
+bool ConstructVerityTable(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)) {
+        verity_mode = "enforcing";  // Defaults to enforcing when it's absent.
+    }
+
+    // Converts veritymode to the format used in kernel.
+    std::string dm_verity_mode;
+    if (verity_mode == "enforcing") {
+        dm_verity_mode = "restart_on_corruption";
+    } else if (verity_mode == "logging") {
+        dm_verity_mode = "ignore_corruption";
+    } else if (verity_mode != "eio") {  // Default dm_verity_mode is eio.
+        LERROR << "Unknown androidboot.veritymode: " << verity_mode;
+        return false;
+    }
+
+    std::ostringstream hash_algorithm;
+    hash_algorithm << hashtree_desc.hash_algorithm;
+
+    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) {
+        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()) {
+        target.SetVerityMode(dm_verity_mode);
+    }
+    // Always use ignore_zero_blocks.
+    target.IgnoreZeroBlocks();
+
+    LINFO << "Built verity table: '" << target.GetParameterString() << "'";
+
+    return table->AddTarget(std::make_unique<android::dm::DmTargetVerity>(target));
+}
+
+bool HashtreeDmVeritySetup(FstabEntry* fstab_entry, const AvbHashtreeDescriptor& hashtree_desc,
+                           const std::string& salt, const std::string& root_digest,
+                           bool wait_for_verity_dev) {
+    android::dm::DmTable table;
+    if (!ConstructVerityTable(hashtree_desc, salt, root_digest, fstab_entry->blk_device, &table) ||
+        !table.valid()) {
+        LERROR << "Failed to construct verity table.";
+        return false;
+    }
+    table.set_readonly(true);
+
+    const std::string mount_point(basename(fstab_entry->mount_point.c_str()));
+    android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance();
+    if (!dm.CreateDevice(mount_point, table)) {
+        LERROR << "Couldn't create verity device!";
+        return false;
+    }
+
+    std::string dev_path;
+    if (!dm.GetDmDevicePathByName(mount_point, &dev_path)) {
+        LERROR << "Couldn't get verity device path!";
+        return false;
+    }
+
+    // Marks the underlying block device as read-only.
+    SetBlockDeviceReadOnly(fstab_entry->blk_device);
+
+    // Updates fstab_rec->blk_device to verity device name.
+    fstab_entry->blk_device = dev_path;
+
+    // Makes sure we've set everything up properly.
+    if (wait_for_verity_dev && !WaitForFile(dev_path, 1s)) {
+        return false;
+    }
+
+    return true;
+}
+
+bool GetHashtreeDescriptor(const std::string& partition_name,
+                           const std::vector<VBMetaData>& vbmeta_images,
+                           AvbHashtreeDescriptor* out_hashtree_desc, std::string* out_salt,
+                           std::string* out_digest) {
+    bool found = false;
+    const uint8_t* desc_partition_name;
+
+    for (const auto& vbmeta : vbmeta_images) {
+        size_t num_descriptors;
+        std::unique_ptr<const AvbDescriptor* [], decltype(&avb_free)> descriptors(
+                avb_descriptor_get_all(vbmeta.data(), vbmeta.size(), &num_descriptors), avb_free);
+
+        if (!descriptors || num_descriptors < 1) {
+            continue;
+        }
+
+        for (size_t n = 0; n < num_descriptors && !found; n++) {
+            AvbDescriptor desc;
+            if (!avb_descriptor_validate_and_byteswap(descriptors[n], &desc)) {
+                LWARNING << "Descriptor[" << n << "] is invalid";
+                continue;
+            }
+            if (desc.tag == AVB_DESCRIPTOR_TAG_HASHTREE) {
+                desc_partition_name =
+                        (const uint8_t*)descriptors[n] + sizeof(AvbHashtreeDescriptor);
+                if (!avb_hashtree_descriptor_validate_and_byteswap(
+                            (AvbHashtreeDescriptor*)descriptors[n], out_hashtree_desc)) {
+                    continue;
+                }
+                if (out_hashtree_desc->partition_name_len != partition_name.length()) {
+                    continue;
+                }
+                // Notes that desc_partition_name is not NUL-terminated.
+                std::string hashtree_partition_name((const char*)desc_partition_name,
+                                                    out_hashtree_desc->partition_name_len);
+                if (hashtree_partition_name == partition_name) {
+                    found = true;
+                }
+            }
+        }
+
+        if (found) break;
+    }
+
+    if (!found) {
+        LERROR << "Partition descriptor not found: " << partition_name.c_str();
+        return false;
+    }
+
+    const uint8_t* desc_salt = desc_partition_name + out_hashtree_desc->partition_name_len;
+    *out_salt = BytesToHex(desc_salt, out_hashtree_desc->salt_len);
+
+    const uint8_t* desc_digest = desc_salt + out_hashtree_desc->salt_len;
+    *out_digest = BytesToHex(desc_digest, out_hashtree_desc->root_digest_len);
+
+    return true;
+}
+
+}  // namespace fs_mgr
+}  // namespace android
diff --git a/fs_mgr/libfs_avb/avb_util.h b/fs_mgr/libfs_avb/avb_util.h
new file mode 100644
index 0000000..b81e931
--- /dev/null
+++ b/fs_mgr/libfs_avb/avb_util.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2019 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>
+
+#include <libavb/libavb.h>
+#include <libdm/dm.h>
+
+#include "fs_avb/fs_avb.h"
+
+namespace android {
+namespace fs_mgr {
+
+// AvbHashtreeDescriptor to dm-verity table setup.
+bool GetHashtreeDescriptor(const std::string& partition_name,
+                           const std::vector<VBMetaData>& vbmeta_images,
+                           AvbHashtreeDescriptor* out_hashtree_desc, std::string* out_salt,
+                           std::string* out_digest);
+
+bool ConstructVerityTable(const AvbHashtreeDescriptor& hashtree_desc, const std::string& salt,
+                          const std::string& root_digest, const std::string& blk_device,
+                          android::dm::DmTable* table);
+
+bool HashtreeDmVeritySetup(FstabEntry* fstab_entry, const AvbHashtreeDescriptor& hashtree_desc,
+                           const std::string& salt, const std::string& root_digest,
+                           bool wait_for_verity_dev);
+
+}  // namespace fs_mgr
+}  // namespace android
diff --git a/fs_mgr/libfs_avb/fs_avb.cpp b/fs_mgr/libfs_avb/fs_avb.cpp
index cf920f9..957aa87 100644
--- a/fs_mgr/libfs_avb/fs_avb.cpp
+++ b/fs_mgr/libfs_avb/fs_avb.cpp
@@ -28,84 +28,30 @@
 
 #include <android-base/file.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 <libavb/libavb.h>
 #include <libdm/dm.h>
 
 #include "avb_ops.h"
-#include "fs_mgr_priv.h"
+#include "avb_util.h"
 #include "sha.h"
+#include "util.h"
+
+using android::base::Basename;
+using android::base::ParseUint;
+using android::base::StringPrintf;
 
 namespace android {
 namespace fs_mgr {
 
-static inline bool nibble_value(const char& c, uint8_t* value) {
-    FS_MGR_CHECK(value != nullptr);
-
-    switch (c) {
-        case '0' ... '9':
-            *value = c - '0';
-            break;
-        case 'a' ... 'f':
-            *value = c - 'a' + 10;
-            break;
-        case 'A' ... 'F':
-            *value = c - 'A' + 10;
-            break;
-        default:
-            return false;
-    }
-
-    return true;
-}
-
-static bool hex_to_bytes(uint8_t* bytes, size_t bytes_len, const std::string& hex) {
-    FS_MGR_CHECK(bytes != nullptr);
-
-    if (hex.size() % 2 != 0) {
-        return false;
-    }
-    if (hex.size() / 2 > bytes_len) {
-        return false;
-    }
-    for (size_t i = 0, j = 0, n = hex.size(); i < n; i += 2, ++j) {
-        uint8_t high;
-        if (!nibble_value(hex[i], &high)) {
-            return false;
-        }
-        uint8_t low;
-        if (!nibble_value(hex[i + 1], &low)) {
-            return false;
-        }
-        bytes[j] = (high << 4) | low;
-    }
-    return true;
-}
-
-static std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
-    FS_MGR_CHECK(bytes != nullptr);
-
-    static const char* hex_digits = "0123456789abcdef";
-    std::string hex;
-
-    for (size_t i = 0; i < bytes_len; i++) {
-        hex.push_back(hex_digits[(bytes[i] & 0xF0) >> 4]);
-        hex.push_back(hex_digits[bytes[i] & 0x0F]);
-    }
-    return hex;
-}
-
 template <typename Hasher>
-static std::pair<size_t, bool> verify_vbmeta_digest(const std::vector<VBMetaData>& vbmeta_images,
-                                                    const uint8_t* expected_digest) {
+std::pair<size_t, bool> VerifyVbmetaDigest(const std::vector<VBMetaData>& vbmeta_images,
+                                           const uint8_t* expected_digest) {
     size_t total_size = 0;
     Hasher hasher;
-    for (size_t n = 0; n < vbmeta_images.size(); n++) {
-        hasher.update(vbmeta_images[n].vbmeta_data(), vbmeta_images[n].vbmeta_size());
-        total_size += vbmeta_images[n].vbmeta_size();
+    for (const auto& vbmeta : vbmeta_images) {
+        hasher.update(vbmeta.data(), vbmeta.size());
+        total_size += vbmeta.size();
     }
 
     bool matched = (memcmp(hasher.finalize(), expected_digest, Hasher::DIGEST_SIZE) == 0);
@@ -148,7 +94,7 @@
 
     std::string value;
     if (!fs_mgr_get_boot_config("vbmeta.size", &value) ||
-        !android::base::ParseUint(value.c_str(), &avb_verifier->vbmeta_size_)) {
+        !ParseUint(value.c_str(), &avb_verifier->vbmeta_size_)) {
         LERROR << "Invalid hash size: " << value.c_str();
         return nullptr;
     }
@@ -177,7 +123,7 @@
         return nullptr;
     }
 
-    if (!hex_to_bytes(avb_verifier->digest_, sizeof(avb_verifier->digest_), digest)) {
+    if (!HexToBytes(avb_verifier->digest_, sizeof(avb_verifier->digest_), digest)) {
         LERROR << "Hash digest contains non-hexidecimal character: " << digest.c_str();
         return nullptr;
     }
@@ -196,10 +142,10 @@
 
     if (hash_alg_ == kSHA256) {
         std::tie(total_size, digest_matched) =
-                verify_vbmeta_digest<SHA256Hasher>(vbmeta_images, digest_);
+                VerifyVbmetaDigest<SHA256Hasher>(vbmeta_images, digest_);
     } else if (hash_alg_ == kSHA512) {
         std::tie(total_size, digest_matched) =
-                verify_vbmeta_digest<SHA512Hasher>(vbmeta_images, digest_);
+                VerifyVbmetaDigest<SHA512Hasher>(vbmeta_images, digest_);
     }
 
     if (total_size != vbmeta_size_) {
@@ -216,155 +162,9 @@
     return true;
 }
 
-// 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 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)) {
-        verity_mode = "enforcing";  // Defaults to enforcing when it's absent.
-    }
-
-    // Converts veritymode to the format used in kernel.
-    std::string dm_verity_mode;
-    if (verity_mode == "enforcing") {
-        dm_verity_mode = "restart_on_corruption";
-    } else if (verity_mode == "logging") {
-        dm_verity_mode = "ignore_corruption";
-    } else if (verity_mode != "eio") {  // Default dm_verity_mode is eio.
-        LERROR << "Unknown androidboot.veritymode: " << verity_mode;
-        return false;
-    }
-
-    std::ostringstream hash_algorithm;
-    hash_algorithm << hashtree_desc.hash_algorithm;
-
-    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) {
-        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()) {
-        target.SetVerityMode(dm_verity_mode);
-    }
-    // Always use ignore_zero_blocks.
-    target.IgnoreZeroBlocks();
-
-    LINFO << "Built verity table: '" << target.GetParameterString() << "'";
-
-    return table->AddTarget(std::make_unique<android::dm::DmTargetVerity>(target));
-}
-
-static bool hashtree_dm_verity_setup(FstabEntry* fstab_entry,
-                                     const AvbHashtreeDescriptor& hashtree_desc,
-                                     const std::string& salt, const std::string& root_digest,
-                                     bool wait_for_verity_dev) {
-    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);
-
-    const std::string mount_point(basename(fstab_entry->mount_point.c_str()));
-    android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance();
-    if (!dm.CreateDevice(mount_point, table)) {
-        LERROR << "Couldn't create verity device!";
-        return false;
-    }
-
-    std::string dev_path;
-    if (!dm.GetDmDevicePathByName(mount_point, &dev_path)) {
-        LERROR << "Couldn't get verity device path!";
-        return false;
-    }
-
-    // Marks the underlying block device as read-only.
-    fs_mgr_set_blk_ro(fstab_entry->blk_device);
-
-    // Updates fstab_rec->blk_device to verity device name.
-    fstab_entry->blk_device = dev_path;
-
-    // Makes sure we've set everything up properly.
-    if (wait_for_verity_dev && !fs_mgr_wait_for_file(dev_path, 1s)) {
-        return false;
-    }
-
-    return true;
-}
-
-static bool get_hashtree_descriptor(const std::string& partition_name,
-                                    const std::vector<VBMetaData>& vbmeta_images,
-                                    AvbHashtreeDescriptor* out_hashtree_desc, std::string* out_salt,
-                                    std::string* out_digest) {
-    bool found = false;
-    const uint8_t* desc_partition_name;
-
-    for (size_t i = 0; i < vbmeta_images.size() && !found; i++) {
-        // Get descriptors from vbmeta_images[i].
-        size_t num_descriptors;
-        std::unique_ptr<const AvbDescriptor* [], decltype(&avb_free)> descriptors(
-                avb_descriptor_get_all(vbmeta_images[i].vbmeta_data(),
-                                       vbmeta_images[i].vbmeta_size(), &num_descriptors),
-                avb_free);
-
-        if (!descriptors || num_descriptors < 1) {
-            continue;
-        }
-
-        for (size_t j = 0; j < num_descriptors && !found; j++) {
-            AvbDescriptor desc;
-            if (!avb_descriptor_validate_and_byteswap(descriptors[j], &desc)) {
-                LWARNING << "Descriptor[" << j << "] is invalid";
-                continue;
-            }
-            if (desc.tag == AVB_DESCRIPTOR_TAG_HASHTREE) {
-                desc_partition_name =
-                        (const uint8_t*)descriptors[j] + sizeof(AvbHashtreeDescriptor);
-                if (!avb_hashtree_descriptor_validate_and_byteswap(
-                            (AvbHashtreeDescriptor*)descriptors[j], out_hashtree_desc)) {
-                    continue;
-                }
-                if (out_hashtree_desc->partition_name_len != partition_name.length()) {
-                    continue;
-                }
-                // Notes that desc_partition_name is not NUL-terminated.
-                std::string hashtree_partition_name((const char*)desc_partition_name,
-                                                    out_hashtree_desc->partition_name_len);
-                if (hashtree_partition_name == partition_name) {
-                    found = true;
-                }
-            }
-        }
-    }
-
-    if (!found) {
-        LERROR << "Partition descriptor not found: " << partition_name.c_str();
-        return false;
-    }
-
-    const uint8_t* desc_salt = desc_partition_name + out_hashtree_desc->partition_name_len;
-    *out_salt = bytes_to_hex(desc_salt, out_hashtree_desc->salt_len);
-
-    const uint8_t* desc_digest = desc_salt + out_hashtree_desc->salt_len;
-    *out_digest = bytes_to_hex(desc_digest, out_hashtree_desc->root_digest_len);
-
-    return true;
-}
 
 AvbUniquePtr AvbHandle::Open() {
-    bool is_device_unlocked = fs_mgr_is_device_unlocked();
+    bool is_device_unlocked = IsDeviceUnlocked();
 
     AvbUniquePtr avb_handle(new AvbHandle());
     if (!avb_handle) {
@@ -407,8 +207,7 @@
     }
 
     // Sets the MAJOR.MINOR for init to set it into "ro.boot.avb_version".
-    avb_handle->avb_version_ =
-            android::base::StringPrintf("%d.%d", AVB_VERSION_MAJOR, AVB_VERSION_MINOR);
+    avb_handle->avb_version_ = StringPrintf("%d.%d", AVB_VERSION_MAJOR, AVB_VERSION_MINOR);
 
     // Checks whether FLAGS_VERIFICATION_DISABLED is set:
     //   - Only the top-level vbmeta struct is read.
@@ -416,7 +215,7 @@
     //     and AVB HASHTREE descriptor(s).
     AvbVBMetaImageHeader vbmeta_header;
     avb_vbmeta_image_header_to_host_byte_order(
-            (AvbVBMetaImageHeader*)avb_handle->vbmeta_images_[0].vbmeta_data(), &vbmeta_header);
+            (AvbVBMetaImageHeader*)avb_handle->vbmeta_images_[0].data(), &vbmeta_header);
     bool verification_disabled = ((AvbVBMetaImageFlags)vbmeta_header.flags &
                                   AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED);
 
@@ -462,7 +261,7 @@
     if (fstab_entry->fs_mgr_flags.logical) {
         partition_name = fstab_entry->logical_partition_name;
     } else {
-        partition_name = basename(fstab_entry->blk_device.c_str());
+        partition_name = Basename(fstab_entry->blk_device);
     }
 
     if (fstab_entry->fs_mgr_flags.slot_select) {
@@ -475,14 +274,14 @@
     AvbHashtreeDescriptor hashtree_descriptor;
     std::string salt;
     std::string root_digest;
-    if (!get_hashtree_descriptor(partition_name, vbmeta_images_, &hashtree_descriptor, &salt,
-                                 &root_digest)) {
+    if (!GetHashtreeDescriptor(partition_name, vbmeta_images_, &hashtree_descriptor, &salt,
+                               &root_digest)) {
         return AvbHashtreeResult::kFail;
     }
 
     // Converts HASHTREE descriptor to verity_table_params.
-    if (!hashtree_dm_verity_setup(fstab_entry, hashtree_descriptor, salt, root_digest,
-                                  wait_for_verity_dev)) {
+    if (!HashtreeDmVeritySetup(fstab_entry, hashtree_descriptor, salt, root_digest,
+                               wait_for_verity_dev)) {
         return AvbHashtreeResult::kFail;
     }
 
diff --git a/fs_mgr/libfs_avb/include/fs_avb/fs_avb.h b/fs_mgr/libfs_avb/include/fs_avb/fs_avb.h
index 0c2b231..eca6984 100644
--- a/fs_mgr/libfs_avb/include/fs_avb/fs_avb.h
+++ b/fs_mgr/libfs_avb/include/fs_avb/fs_avb.h
@@ -37,7 +37,7 @@
     // Constructors
     VBMetaData() : vbmeta_ptr_(nullptr), vbmeta_size_(0){};
 
-    VBMetaData(uint8_t* data, size_t size)
+    VBMetaData(const uint8_t* data, size_t size)
         : vbmeta_ptr_(new (std::nothrow) uint8_t[size]), vbmeta_size_(size) {
         // The ownership of data is NOT transferred, i.e., the caller still
         // needs to release the memory as we make a copy here.
@@ -49,8 +49,8 @@
 
     // Get methods for each data member.
     const std::string& device_path() const { return device_path_; }
-    uint8_t* vbmeta_data() const { return vbmeta_ptr_.get(); }
-    const size_t& vbmeta_size() const { return vbmeta_size_; }
+    uint8_t* data() const { return vbmeta_ptr_.get(); }
+    const size_t& size() const { return vbmeta_size_; }
 
     // Maximum size of a vbmeta data - 64 KiB.
     static const size_t kMaxVBMetaSize = 64 * 1024;
diff --git a/fs_mgr/libfs_avb/tests/Android.bp b/fs_mgr/libfs_avb/tests/Android.bp
deleted file mode 100644
index 24e1d76..0000000
--- a/fs_mgr/libfs_avb/tests/Android.bp
+++ /dev/null
@@ -1,40 +0,0 @@
-//
-// Copyright (C) 2019 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_host {
-    name: "libfs_avb_host_unittest",
-    required: [
-        "avbtool",
-    ],
-    data: [
-        "data/*",
-    ],
-    static_libs: [
-        "libgtest_host",
-    ],
-    shared_libs: [
-        "libbase",
-        "libchrome",
-    ],
-    srcs: [
-        "fs_avb_unittest_util.cpp",
-    ],
-    target: {
-        darwin: {
-            enabled: false,
-        },
-    },
-}
diff --git a/fs_mgr/libfs_avb/tests/basic_test.cpp b/fs_mgr/libfs_avb/tests/basic_test.cpp
new file mode 100644
index 0000000..5a1cd0d
--- /dev/null
+++ b/fs_mgr/libfs_avb/tests/basic_test.cpp
@@ -0,0 +1,273 @@
+/*
+ * Copyright (C) 2019 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 "fs_avb_test_util.h"
+
+#include <stdlib.h>
+
+#include <android-base/file.h>
+#include <base/files/file_util.h>
+#include <base/strings/string_util.h>
+
+namespace fs_avb_host_test {
+
+TEST_F(BaseFsAvbTest, GenerateImage) {
+    const size_t image_size = 5 * 1024 * 1024;
+    base::FilePath boot_path = GenerateImage("boot.img", image_size);
+    EXPECT_NE(0U, boot_path.value().size());
+
+    // Checks file size is as expected.
+    int64_t file_size;
+    ASSERT_TRUE(base::GetFileSize(boot_path, &file_size));
+    EXPECT_EQ(file_size, image_size);
+
+    // Checks file content is as expected.
+    std::vector<uint8_t> expected_content;
+    expected_content.resize(image_size);
+    for (size_t n = 0; n < image_size; n++) {
+        expected_content[n] = uint8_t(n);
+    }
+    std::vector<uint8_t> actual_content;
+    actual_content.resize(image_size);
+    EXPECT_TRUE(
+            base::ReadFile(boot_path, reinterpret_cast<char*>(actual_content.data()), image_size));
+    EXPECT_EQ(expected_content, actual_content);
+}
+
+TEST_F(BaseFsAvbTest, GenerateVBMetaImage) {
+    GenerateVBMetaImage("vbmeta.img", "SHA256_RSA2048", 0, data_dir_.Append("testkey_rsa2048.pem"),
+                        {}, /* include_descriptor_image_paths */
+                        {}, /* chain_partitions */
+                        "--internal_release_string \"unit test\"");
+    EXPECT_EQ("5eba9ad4e775645e7eac441a563c200681ae868158d06f6a6cd36d06c07bd781",
+              CalcVBMetaDigest("vbmeta.img", "sha256"));
+    EXPECT_EQ(
+            "Minimum libavb version:   1.0\n"
+            "Header Block:             256 bytes\n"
+            "Authentication Block:     320 bytes\n"
+            "Auxiliary Block:          576 bytes\n"
+            "Algorithm:                SHA256_RSA2048\n"
+            "Rollback Index:           0\n"
+            "Flags:                    0\n"
+            "Release String:           'unit test'\n"
+            "Descriptors:\n"
+            "    (none)\n",
+            InfoImage("vbmeta.img"));
+}
+
+TEST_F(BaseFsAvbTest, AddHashFooter) {
+    // Generates a raw boot.img
+    const size_t image_size = 5 * 1024 * 1024;
+    const size_t partition_size = 10 * 1024 * 1024;
+    base::FilePath boot_path = GenerateImage("boot.img", image_size);
+    EXPECT_NE(0U, boot_path.value().size());
+    // Checks file size is as expected.
+    int64_t file_size;
+    ASSERT_TRUE(base::GetFileSize(boot_path, &file_size));
+    EXPECT_EQ(file_size, image_size);
+    // Appends AVB Hash Footer.
+    AddAvbFooter(boot_path, "hash", "boot", partition_size, "SHA256_RSA4096", 10,
+                 data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
+                 "--internal_release_string \"unit test\"");
+    // Extracts boot vbmeta from boot.img into boot-vbmeta.img.
+    ExtractVBMetaImage(boot_path, "boot-vbmeta.img");
+    EXPECT_EQ(
+            "Minimum libavb version:   1.0\n"
+            "Header Block:             256 bytes\n"
+            "Authentication Block:     576 bytes\n"
+            "Auxiliary Block:          1216 bytes\n"
+            "Algorithm:                SHA256_RSA4096\n"
+            "Rollback Index:           10\n"
+            "Flags:                    0\n"
+            "Release String:           'unit test'\n"
+            "Descriptors:\n"
+            "    Hash descriptor:\n"
+            "      Image Size:            5242880 bytes\n"
+            "      Hash Algorithm:        sha256\n"
+            "      Partition Name:        boot\n"
+            "      Salt:                  d00df00d\n"
+            "      Digest:                "
+            "222dd01e98284a1fcd7781f85d1392e43a530511a64eff96db197db90ebc4df1\n"
+            "      Flags:                 0\n",
+            InfoImage("boot-vbmeta.img"));
+}
+
+TEST_F(BaseFsAvbTest, AddHashtreeFooter) {
+    // Generates a raw system.img
+    const size_t image_size = 50 * 1024 * 1024;
+    const size_t partition_size = 60 * 1024 * 1024;
+    base::FilePath system_path = GenerateImage("system.img", image_size);
+    EXPECT_NE(0U, system_path.value().size());
+    // Checks file size is as expected.
+    int64_t file_size;
+    ASSERT_TRUE(base::GetFileSize(system_path, &file_size));
+    EXPECT_EQ(file_size, image_size);
+    // Appends AVB Hashtree Footer.
+    AddAvbFooter(system_path, "hashtree", "system", partition_size, "SHA512_RSA8192", 20,
+                 data_dir_.Append("testkey_rsa8192.pem"), "d00df00d",
+                 "--internal_release_string \"unit test\"");
+    // Extracts system vbmeta from system.img into system-vbmeta.img.
+    ExtractVBMetaImage(system_path, "system-vbmeta.img");
+    EXPECT_EQ(
+            "Minimum libavb version:   1.0\n"
+            "Header Block:             256 bytes\n"
+            "Authentication Block:     1088 bytes\n"
+            "Auxiliary Block:          2304 bytes\n"
+            "Algorithm:                SHA512_RSA8192\n"
+            "Rollback Index:           20\n"
+            "Flags:                    0\n"
+            "Release String:           'unit test'\n"
+            "Descriptors:\n"
+            "    Hashtree descriptor:\n"
+            "      Version of dm-verity:  1\n"
+            "      Image Size:            52428800 bytes\n"
+            "      Tree Offset:           52428800\n"
+            "      Tree Size:             413696 bytes\n"
+            "      Data Block Size:       4096 bytes\n"
+            "      Hash Block Size:       4096 bytes\n"
+            "      FEC num roots:         2\n"
+            "      FEC offset:            52842496\n"
+            "      FEC size:              417792 bytes\n"
+            "      Hash Algorithm:        sha1\n"
+            "      Partition Name:        system\n"
+            "      Salt:                  d00df00d\n"
+            "      Root Digest:           d20d40c02298e385ab6d398a61a3b91dc9947d99\n"
+            "      Flags:                 0\n",
+            InfoImage("system-vbmeta.img"));
+}
+
+TEST_F(BaseFsAvbTest, GenerateVBMetaImageWithDescriptors) {
+    // Generates a raw boot.img
+    const size_t boot_image_size = 5 * 1024 * 1024;
+    const size_t boot_partition_size = 10 * 1024 * 1024;
+    base::FilePath boot_path = GenerateImage("boot.img", boot_image_size);
+    // Adds AVB Hash Footer.
+    AddAvbFooter(boot_path, "hash", "boot", boot_partition_size, "SHA256_RSA4096", 10,
+                 data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
+                 "--internal_release_string \"unit test\"");
+
+    // Generates a raw system.img, use a smaller size to speed-up unit test.
+    const size_t system_image_size = 10 * 1024 * 1024;
+    const size_t system_partition_size = 15 * 1024 * 1024;
+    base::FilePath system_path = GenerateImage("system.img", system_image_size);
+    // Adds AVB Hashtree Footer.
+    AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA8192", 20,
+                 data_dir_.Append("testkey_rsa8192.pem"), "d00df00d",
+                 "--internal_release_string \"unit test\"");
+
+    // Makes a vbmeta.img including both 'boot' and 'system' descriptors.
+    GenerateVBMetaImage("vbmeta.img", "SHA256_RSA2048", 0, data_dir_.Append("testkey_rsa2048.pem"),
+                        {boot_path, system_path}, /* include_descriptor_image_paths */
+                        {},                       /* chain_partitions */
+                        "--internal_release_string \"unit test\"");
+    EXPECT_EQ("a069cbfc30c816cddf3b53f1ad53b7ca5d61a3d93845eb596bbb1b40caa1c62f",
+              CalcVBMetaDigest("vbmeta.img", "sha256"));
+    EXPECT_EQ(
+            "Minimum libavb version:   1.0\n"
+            "Header Block:             256 bytes\n"
+            "Authentication Block:     320 bytes\n"
+            "Auxiliary Block:          960 bytes\n"
+            "Algorithm:                SHA256_RSA2048\n"
+            "Rollback Index:           0\n"
+            "Flags:                    0\n"
+            "Release String:           'unit test'\n"
+            "Descriptors:\n"
+            "    Hash descriptor:\n"
+            "      Image Size:            5242880 bytes\n"
+            "      Hash Algorithm:        sha256\n"
+            "      Partition Name:        boot\n"
+            "      Salt:                  d00df00d\n"
+            "      Digest:                "
+            "222dd01e98284a1fcd7781f85d1392e43a530511a64eff96db197db90ebc4df1\n"
+            "      Flags:                 0\n"
+            "    Hashtree descriptor:\n"
+            "      Version of dm-verity:  1\n"
+            "      Image Size:            10485760 bytes\n"
+            "      Tree Offset:           10485760\n"
+            "      Tree Size:             86016 bytes\n"
+            "      Data Block Size:       4096 bytes\n"
+            "      Hash Block Size:       4096 bytes\n"
+            "      FEC num roots:         2\n"
+            "      FEC offset:            10571776\n"
+            "      FEC size:              90112 bytes\n"
+            "      Hash Algorithm:        sha1\n"
+            "      Partition Name:        system\n"
+            "      Salt:                  d00df00d\n"
+            "      Root Digest:           a3d5dd307341393d85de356c384ff543ec1ed81b\n"
+            "      Flags:                 0\n",
+            InfoImage("vbmeta.img"));
+}
+
+TEST_F(BaseFsAvbTest, GenerateVBMetaImageWithChainDescriptors) {
+    // Generates a raw boot.img
+    const size_t boot_image_size = 5 * 1024 * 1024;
+    const size_t boot_partition_size = 10 * 1024 * 1024;
+    base::FilePath boot_path = GenerateImage("boot.img", boot_image_size);
+    // Adds AVB Hash Footer.
+    AddAvbFooter(boot_path, "hash", "boot", boot_partition_size, "SHA256_RSA2048", 10,
+                 data_dir_.Append("testkey_rsa2048.pem"), "d00df00d",
+                 "--internal_release_string \"unit test\"");
+
+    // Generates a raw system.img, use a smaller size to speed-up unit test.
+    const size_t system_image_size = 10 * 1024 * 1024;
+    const size_t system_partition_size = 15 * 1024 * 1024;
+    base::FilePath system_path = GenerateImage("system.img", system_image_size);
+    // Adds AVB Hashtree Footer.
+    AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA4096", 20,
+                 data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
+                 "--internal_release_string \"unit test\"");
+
+    // Make a vbmeta image with chain partitions.
+    base::FilePath rsa2048_public_key =
+            ExtractPublicKeyAvb(data_dir_.Append("testkey_rsa2048.pem"));
+    base::FilePath rsa4096_public_key =
+            ExtractPublicKeyAvb(data_dir_.Append("testkey_rsa4096.pem"));
+    GenerateVBMetaImage("vbmeta.img", "SHA256_RSA8192", 0, data_dir_.Append("testkey_rsa8192.pem"),
+                        {},                               /* include_descriptor_image_paths */
+                        {{"boot", 1, rsa2048_public_key}, /* chain_partitions */
+                         {"system", 2, rsa4096_public_key}},
+                        "--internal_release_string \"unit test\"");
+
+    // vbmeta digest calculation includes the chained vbmeta from boot.img and system.img.
+    EXPECT_EQ("abbe11b316901f3336e26630f64c4732dadbe14532186ac8640e4141a403721f",
+              CalcVBMetaDigest("vbmeta.img", "sha256"));
+    EXPECT_EQ(
+            "Minimum libavb version:   1.0\n"
+            "Header Block:             256 bytes\n"
+            "Authentication Block:     1088 bytes\n"
+            "Auxiliary Block:          3840 bytes\n"
+            "Algorithm:                SHA256_RSA8192\n"
+            "Rollback Index:           0\n"
+            "Flags:                    0\n"
+            "Release String:           'unit test'\n"
+            "Descriptors:\n"
+            "    Chain Partition descriptor:\n"
+            "      Partition Name:          boot\n"
+            "      Rollback Index Location: 1\n"
+            "      Public key (sha1):       cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
+            "    Chain Partition descriptor:\n"
+            "      Partition Name:          system\n"
+            "      Rollback Index Location: 2\n"
+            "      Public key (sha1):       2597c218aae470a130f61162feaae70afd97f011\n",
+            InfoImage("vbmeta.img"));
+}
+
+}  // namespace fs_avb_host_test
+
+int main(int argc, char** argv) {
+    ::testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/fs_mgr/libfs_avb/tests/fs_avb_test_util.cpp b/fs_mgr/libfs_avb/tests/fs_avb_test_util.cpp
new file mode 100644
index 0000000..95b17d8
--- /dev/null
+++ b/fs_mgr/libfs_avb/tests/fs_avb_test_util.cpp
@@ -0,0 +1,220 @@
+/*
+ * Copyright (C) 2019 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 "fs_avb_test_util.h"
+
+#include <stdlib.h>
+
+#include <android-base/file.h>
+#include <base/files/file_util.h>
+#include <base/strings/string_util.h>
+
+namespace fs_avb_host_test {
+
+// Need to match the data setting in Android.bp:
+//     data: ["tests/data/*"]
+base::FilePath BaseFsAvbTest::data_dir_ = base::FilePath("tests/data");
+
+void BaseFsAvbTest::SetUp() {
+    // Changes current directory to test executable directory so that relative path
+    // references to test dependencies don't rely on being manually run from
+    // the executable directory. With this, we can just open "./tests/data/testkey_rsa2048.pem"
+    // from the source.
+    base::SetCurrentDirectory(base::FilePath(android::base::GetExecutableDirectory()));
+
+    // Creates a temporary directory, e.g., /tmp/libfs_avb-tests.XXXXXX to stash images in.
+    base::FilePath tmp_dir;
+    ASSERT_TRUE(GetTempDir(&tmp_dir));
+    base::CreateTemporaryDirInDir(tmp_dir, "libfs_avb-tests.", &test_dir_);
+}
+
+void BaseFsAvbTest::TearDown() {
+    // Nukes temporary directory.
+    ASSERT_NE(std::string::npos, test_dir_.value().find("libfs_avb-tests"));
+    ASSERT_TRUE(base::DeleteFile(test_dir_, true /* recursive */));
+}
+
+std::string BaseFsAvbTest::CalcVBMetaDigest(const std::string& file_name,
+                                            const std::string& hash_algorithm) {
+    auto iter = vbmeta_images_.find(file_name);
+    EXPECT_NE(iter, vbmeta_images_.end());  // ensures file_name is generated before.
+
+    // Gets the image path from iterator->second.path: VBMetaImage.path.
+    base::FilePath image_path = iter->second.path;
+    base::FilePath vbmeta_digest_path = test_dir_.Append("vbmeta_digest");
+    EXPECT_COMMAND(0,
+                   "avbtool calculate_vbmeta_digest --image %s --hash_algorithm %s"
+                   " --output %s",
+                   image_path.value().c_str(), hash_algorithm.c_str(),
+                   vbmeta_digest_path.value().c_str());
+    // Reads the content of the output digest file.
+    std::string vbmeta_digest_data;
+    EXPECT_TRUE(base::ReadFileToString(vbmeta_digest_path, &vbmeta_digest_data));
+    // Returns the trimmed digest.
+    std::string trimmed_digest_data;
+    base::TrimString(vbmeta_digest_data, " \t\n", &trimmed_digest_data);
+    return trimmed_digest_data;
+}
+
+void BaseFsAvbTest::GenerateVBMetaImage(
+        const std::string& file_name, const std::string& avb_algorithm, uint64_t rollback_index,
+        const base::FilePath& key_path,
+        const std::vector<base::FilePath>& include_descriptor_image_paths,
+        const std::vector<ChainPartitionConfig>& chain_partitions,
+        const std::string& additional_options) {
+    // --algorithm and --key
+    std::string signing_options;
+    if (avb_algorithm == "") {
+        signing_options = " --algorithm NONE ";
+    } else {
+        signing_options =
+                std::string(" --algorithm ") + avb_algorithm + " --key " + key_path.value() + " ";
+    }
+    // --include_descriptors_from_image
+    std::string include_descriptor_options;
+    for (const auto& path : include_descriptor_image_paths) {
+        include_descriptor_options += " --include_descriptors_from_image " + path.value();
+    }
+    // --chain_partitions
+    std::string chain_partition_options;
+    for (const auto& partition : chain_partitions) {
+        chain_partition_options += base::StringPrintf(
+                " --chain_partition %s:%u:%s", partition.partition_name.c_str(),
+                partition.rollback_index_location, partition.key_blob_path.value().c_str());
+    }
+    // Starts to 'make_vbmeta_image'.
+    VBMetaImage vbmeta_image;
+    vbmeta_image.path = test_dir_.Append(file_name);
+    EXPECT_COMMAND(0,
+                   "avbtool make_vbmeta_image"
+                   " --rollback_index %" PRIu64
+                   " %s %s %s %s"
+                   " --output %s",
+                   rollback_index, signing_options.c_str(), include_descriptor_options.c_str(),
+                   chain_partition_options.c_str(), additional_options.c_str(),
+                   vbmeta_image.path.value().c_str());
+    int64_t file_size;
+    ASSERT_TRUE(base::GetFileSize(vbmeta_image.path, &file_size));
+    vbmeta_image.content.resize(file_size);
+    ASSERT_TRUE(base::ReadFile(vbmeta_image.path,
+                               reinterpret_cast<char*>(vbmeta_image.content.data()), file_size));
+    // Stores the generated vbmeta image into vbmeta_images_ member object.
+    vbmeta_images_.emplace(file_name, std::move(vbmeta_image));
+}
+
+void BaseFsAvbTest::ExtractVBMetaImage(const base::FilePath& image_path,
+                                       const std::string& output_file_name,
+                                       const size_t padding_size) {
+    VBMetaImage vbmeta_image;
+    vbmeta_image.path = test_dir_.Append(output_file_name);
+    EXPECT_COMMAND(0,
+                   "avbtool extract_vbmeta_image"
+                   " --image %s"
+                   " --output %s"
+                   " --padding_size %zu",
+                   image_path.value().c_str(), vbmeta_image.path.value().c_str(), padding_size);
+    int64_t file_size;
+    ASSERT_TRUE(base::GetFileSize(vbmeta_image.path, &file_size));
+    vbmeta_image.content.resize(file_size);
+    ASSERT_TRUE(base::ReadFile(vbmeta_image.path,
+                               reinterpret_cast<char*>(vbmeta_image.content.data()), file_size));
+    // Stores the extracted vbmeta image into vbmeta_images_ member object.
+    vbmeta_images_.emplace(output_file_name, std::move(vbmeta_image));
+}
+
+// Generates a file with name |file_name| of size |image_size| with
+// known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..).
+base::FilePath BaseFsAvbTest::GenerateImage(const std::string& file_name, size_t image_size,
+                                            uint8_t start_byte) {
+    std::vector<uint8_t> image;
+    image.resize(image_size);
+    for (size_t n = 0; n < image_size; n++) {
+        image[n] = uint8_t(n + start_byte);
+    }
+    base::FilePath image_path = test_dir_.Append(file_name);
+    EXPECT_EQ(image_size,
+              static_cast<const size_t>(base::WriteFile(
+                      image_path, reinterpret_cast<const char*>(image.data()), image.size())));
+    return image_path;
+}
+
+void BaseFsAvbTest::AddAvbFooter(const base::FilePath& image_path, const std::string& footer_type,
+                                 const std::string& partition_name, const uint64_t partition_size,
+                                 const std::string& avb_algorithm, uint64_t rollback_index,
+                                 const base::FilePath& key_path, const std::string& salt,
+                                 const std::string& additional_options) {
+    // 'add_hash_footer' or 'add_hashtree_footer'.
+    EXPECT_TRUE(footer_type == "hash" or footer_type == "hashtree");
+    std::string add_footer_option = "add_" + footer_type + "_footer";
+
+    std::string signing_options;
+    if (avb_algorithm == "") {
+        signing_options = " --algorithm NONE ";
+    } else {
+        signing_options =
+                std::string(" --algorithm ") + avb_algorithm + " --key " + key_path.value() + " ";
+    }
+    EXPECT_COMMAND(0,
+                   "avbtool %s"
+                   " --image %s"
+                   " --partition_name %s "
+                   " --partition_size %" PRIu64 " --rollback_index %" PRIu64
+                   " --salt %s"
+                   " %s %s",
+                   add_footer_option.c_str(), image_path.value().c_str(), partition_name.c_str(),
+                   partition_size, rollback_index, salt.c_str(), signing_options.c_str(),
+                   additional_options.c_str());
+}
+
+std::string BaseFsAvbTest::InfoImage(const base::FilePath& image_path) {
+    base::FilePath tmp_path = test_dir_.Append("info_output.txt");
+    EXPECT_COMMAND(0, "avbtool info_image --image %s --output %s", image_path.value().c_str(),
+                   tmp_path.value().c_str());
+    std::string info_data;
+    EXPECT_TRUE(base::ReadFileToString(tmp_path, &info_data));
+    return info_data;
+}
+
+std::string BaseFsAvbTest::InfoImage(const std::string& file_name) {
+    auto iter = vbmeta_images_.find(file_name);
+    EXPECT_NE(iter, vbmeta_images_.end());  // ensures file_name is generated before.
+    // Gets the image path from iterator->second.path: VBMetaImage.path.
+    base::FilePath image_path = iter->second.path;
+    return InfoImage(image_path);
+}
+
+base::FilePath BaseFsAvbTest::ExtractPublicKeyAvb(const base::FilePath& key_path) {
+    std::string file_name = key_path.RemoveExtension().BaseName().value();
+    base::FilePath tmp_path = test_dir_.Append(file_name + "public_key.bin");
+    EXPECT_COMMAND(0,
+                   "avbtool extract_public_key --key %s"
+                   " --output %s",
+                   key_path.value().c_str(), tmp_path.value().c_str());
+    return tmp_path;
+}
+
+std::string BaseFsAvbTest::ExtractPublicKeyAvbBlob(const base::FilePath& key_path) {
+    base::FilePath tmp_path = test_dir_.Append("public_key.bin");
+    EXPECT_COMMAND(0,
+                   "avbtool extract_public_key --key %s"
+                   " --output %s",
+                   key_path.value().c_str(), tmp_path.value().c_str());
+    std::string key_data;
+    EXPECT_TRUE(base::ReadFileToString(tmp_path, &key_data));
+    return key_data;
+}
+
+}  // namespace fs_avb_host_test
diff --git a/fs_mgr/libfs_avb/tests/fs_avb_unittest_util.h b/fs_mgr/libfs_avb/tests/fs_avb_test_util.h
similarity index 97%
rename from fs_mgr/libfs_avb/tests/fs_avb_unittest_util.h
rename to fs_mgr/libfs_avb/tests/fs_avb_test_util.h
index f329466..f80dc5f 100644
--- a/fs_mgr/libfs_avb/tests/fs_avb_unittest_util.h
+++ b/fs_mgr/libfs_avb/tests/fs_avb_test_util.h
@@ -79,7 +79,7 @@
 
     // Generate a file with name |file_name| of size |image_size| with
     // known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..).
-    base::FilePath GenerateImage(const std::string file_name, size_t image_size,
+    base::FilePath GenerateImage(const std::string& file_name, size_t image_size,
                                  uint8_t start_byte = 0);
     // Invokes 'avbtool add_hash_footer' or 'avbtool add_hashtree_footer' to sign
     // the |image_path|. The |footer_type| can be either "hash" or "hashtree".
@@ -107,6 +107,8 @@
     base::FilePath test_dir_;
     // Maps vbmeta image name (e.g., vbmeta_a.img, system_a.img) to VBMetaImage.
     std::map<std::string, VBMetaImage> vbmeta_images_;
+
+    static base::FilePath data_dir_;
 };
 
 }  // namespace fs_avb_host_test
diff --git a/fs_mgr/libfs_avb/tests/fs_avb_unittest_util.cpp b/fs_mgr/libfs_avb/tests/fs_avb_unittest_util.cpp
deleted file mode 100644
index 216d1cb..0000000
--- a/fs_mgr/libfs_avb/tests/fs_avb_unittest_util.cpp
+++ /dev/null
@@ -1,465 +0,0 @@
-/*
- * Copyright (C) 2019 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 "fs_avb_unittest_util.h"
-
-#include <stdlib.h>
-
-#include <android-base/file.h>
-#include <base/files/file_util.h>
-#include <base/strings/string_util.h>
-
-namespace fs_avb_host_test {
-
-void BaseFsAvbTest::SetUp() {
-    // Changes current directory to test executable directory so that relative path
-    // references to test dependencies don't rely on being manually run from
-    // the executable directory. With this, we can just open "./data/testkey_rsa2048.pem"
-    // from the source.
-    base::SetCurrentDirectory(base::FilePath(android::base::GetExecutableDirectory()));
-
-    // Creates a temporary directory, e.g., /tmp/libfs_avb-tests.XXXXXX to stash images in.
-    base::FilePath tmp_dir;
-    ASSERT_TRUE(GetTempDir(&tmp_dir));
-    base::CreateTemporaryDirInDir(tmp_dir, "libfs_avb-tests.", &test_dir_);
-}
-
-void BaseFsAvbTest::TearDown() {
-    // Nukes temporary directory.
-    ASSERT_NE(std::string::npos, test_dir_.value().find("libfs_avb-tests"));
-    ASSERT_TRUE(base::DeleteFile(test_dir_, true /* recursive */));
-}
-
-std::string BaseFsAvbTest::CalcVBMetaDigest(const std::string& file_name,
-                                            const std::string& hash_algorithm) {
-    auto iter = vbmeta_images_.find(file_name);
-    EXPECT_NE(iter, vbmeta_images_.end());  // ensures file_name is generated before.
-
-    // Gets the image path from iterator->second.path: VBMetaImage.path.
-    base::FilePath image_path = iter->second.path;
-    base::FilePath vbmeta_digest_path = test_dir_.Append("vbmeta_digest");
-    EXPECT_COMMAND(0,
-                   "avbtool calculate_vbmeta_digest --image %s --hash_algorithm %s"
-                   " --output %s",
-                   image_path.value().c_str(), hash_algorithm.c_str(),
-                   vbmeta_digest_path.value().c_str());
-    // Reads the content of the output digest file.
-    std::string vbmeta_digest_data;
-    EXPECT_TRUE(base::ReadFileToString(vbmeta_digest_path, &vbmeta_digest_data));
-    // Returns the trimmed digest.
-    std::string trimmed_digest_data;
-    base::TrimString(vbmeta_digest_data, " \t\n", &trimmed_digest_data);
-    return trimmed_digest_data;
-}
-
-void BaseFsAvbTest::GenerateVBMetaImage(
-        const std::string& file_name, const std::string& avb_algorithm, uint64_t rollback_index,
-        const base::FilePath& key_path,
-        const std::vector<base::FilePath>& include_descriptor_image_paths,
-        const std::vector<ChainPartitionConfig>& chain_partitions,
-        const std::string& additional_options) {
-    // --algorithm and --key
-    std::string signing_options;
-    if (avb_algorithm == "") {
-        signing_options = " --algorithm NONE ";
-    } else {
-        signing_options =
-                std::string(" --algorithm ") + avb_algorithm + " --key " + key_path.value() + " ";
-    }
-    // --include_descriptors_from_image
-    std::string include_descriptor_options;
-    for (const auto& path : include_descriptor_image_paths) {
-        include_descriptor_options += " --include_descriptors_from_image " + path.value();
-    }
-    // --chain_partitions
-    std::string chain_partition_options;
-    for (const auto& partition : chain_partitions) {
-        chain_partition_options += base::StringPrintf(
-                " --chain_partition %s:%u:%s", partition.partition_name.c_str(),
-                partition.rollback_index_location, partition.key_blob_path.value().c_str());
-    }
-    // Starts to 'make_vbmeta_image'.
-    VBMetaImage vbmeta_image;
-    vbmeta_image.path = test_dir_.Append(file_name);
-    EXPECT_COMMAND(0,
-                   "avbtool make_vbmeta_image"
-                   " --rollback_index %" PRIu64
-                   " %s %s %s %s"
-                   " --output %s",
-                   rollback_index, signing_options.c_str(), include_descriptor_options.c_str(),
-                   chain_partition_options.c_str(), additional_options.c_str(),
-                   vbmeta_image.path.value().c_str());
-    int64_t file_size;
-    ASSERT_TRUE(base::GetFileSize(vbmeta_image.path, &file_size));
-    vbmeta_image.content.resize(file_size);
-    ASSERT_TRUE(base::ReadFile(vbmeta_image.path,
-                               reinterpret_cast<char*>(vbmeta_image.content.data()), file_size));
-    // Stores the generated vbmeta image into vbmeta_images_ member object.
-    vbmeta_images_.emplace(file_name, std::move(vbmeta_image));
-}
-
-void BaseFsAvbTest::ExtractVBMetaImage(const base::FilePath& image_path,
-                                       const std::string& output_file_name,
-                                       const size_t padding_size) {
-    VBMetaImage vbmeta_image;
-    vbmeta_image.path = test_dir_.Append(output_file_name);
-    EXPECT_COMMAND(0,
-                   "avbtool extract_vbmeta_image"
-                   " --image %s"
-                   " --output %s"
-                   " --padding_size %zu",
-                   image_path.value().c_str(), vbmeta_image.path.value().c_str(), padding_size);
-    int64_t file_size;
-    ASSERT_TRUE(base::GetFileSize(vbmeta_image.path, &file_size));
-    vbmeta_image.content.resize(file_size);
-    ASSERT_TRUE(base::ReadFile(vbmeta_image.path,
-                               reinterpret_cast<char*>(vbmeta_image.content.data()), file_size));
-    // Stores the extracted vbmeta image into vbmeta_images_ member object.
-    vbmeta_images_.emplace(output_file_name, std::move(vbmeta_image));
-}
-
-// Generates a file with name |file_name| of size |image_size| with
-// known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..).
-base::FilePath BaseFsAvbTest::GenerateImage(const std::string file_name, size_t image_size,
-                                            uint8_t start_byte) {
-    std::vector<uint8_t> image;
-    image.resize(image_size);
-    for (size_t n = 0; n < image_size; n++) {
-        image[n] = uint8_t(n + start_byte);
-    }
-    base::FilePath image_path = test_dir_.Append(file_name);
-    EXPECT_EQ(image_size,
-              static_cast<const size_t>(base::WriteFile(
-                      image_path, reinterpret_cast<const char*>(image.data()), image.size())));
-    return image_path;
-}
-
-void BaseFsAvbTest::AddAvbFooter(const base::FilePath& image_path, const std::string& footer_type,
-                                 const std::string& partition_name, const uint64_t partition_size,
-                                 const std::string& avb_algorithm, uint64_t rollback_index,
-                                 const base::FilePath& key_path, const std::string& salt,
-                                 const std::string& additional_options) {
-    // 'add_hash_footer' or 'add_hashtree_footer'.
-    EXPECT_TRUE(footer_type == "hash" or footer_type == "hashtree");
-    std::string add_footer_option = "add_" + footer_type + "_footer";
-
-    std::string signing_options;
-    if (avb_algorithm == "") {
-        signing_options = " --algorithm NONE ";
-    } else {
-        signing_options =
-                std::string(" --algorithm ") + avb_algorithm + " --key " + key_path.value() + " ";
-    }
-    EXPECT_COMMAND(0,
-                   "avbtool %s"
-                   " --image %s"
-                   " --partition_name %s "
-                   " --partition_size %" PRIu64 " --rollback_index %" PRIu64
-                   " --salt %s"
-                   " %s %s",
-                   add_footer_option.c_str(), image_path.value().c_str(), partition_name.c_str(),
-                   partition_size, rollback_index, salt.c_str(), signing_options.c_str(),
-                   additional_options.c_str());
-}
-
-std::string BaseFsAvbTest::InfoImage(const base::FilePath& image_path) {
-    base::FilePath tmp_path = test_dir_.Append("info_output.txt");
-    EXPECT_COMMAND(0, "avbtool info_image --image %s --output %s", image_path.value().c_str(),
-                   tmp_path.value().c_str());
-    std::string info_data;
-    EXPECT_TRUE(base::ReadFileToString(tmp_path, &info_data));
-    return info_data;
-}
-
-std::string BaseFsAvbTest::InfoImage(const std::string& file_name) {
-    auto iter = vbmeta_images_.find(file_name);
-    EXPECT_NE(iter, vbmeta_images_.end());  // ensures file_name is generated before.
-    // Gets the image path from iterator->second.path: VBMetaImage.path.
-    base::FilePath image_path = iter->second.path;
-    return InfoImage(image_path);
-}
-
-base::FilePath BaseFsAvbTest::ExtractPublicKeyAvb(const base::FilePath& key_path) {
-    std::string file_name = key_path.RemoveExtension().BaseName().value();
-    base::FilePath tmp_path = test_dir_.Append(file_name + "public_key.bin");
-    EXPECT_COMMAND(0,
-                   "avbtool extract_public_key --key %s"
-                   " --output %s",
-                   key_path.value().c_str(), tmp_path.value().c_str());
-    return tmp_path;
-}
-
-std::string BaseFsAvbTest::ExtractPublicKeyAvbBlob(const base::FilePath& key_path) {
-    base::FilePath tmp_path = test_dir_.Append("public_key.bin");
-    EXPECT_COMMAND(0,
-                   "avbtool extract_public_key --key %s"
-                   " --output %s",
-                   key_path.value().c_str(), tmp_path.value().c_str());
-    std::string key_data;
-    EXPECT_TRUE(base::ReadFileToString(tmp_path, &key_data));
-    return key_data;
-}
-
-TEST_F(BaseFsAvbTest, GenerateImage) {
-    const size_t image_size = 5 * 1024 * 1024;
-    base::FilePath boot_path = GenerateImage("boot.img", image_size);
-    EXPECT_NE(0U, boot_path.value().size());
-
-    // Checks file size is as expected.
-    int64_t file_size;
-    ASSERT_TRUE(base::GetFileSize(boot_path, &file_size));
-    EXPECT_EQ(file_size, image_size);
-
-    // Checks file content is as expected.
-    std::vector<uint8_t> expected_content;
-    expected_content.resize(image_size);
-    for (size_t n = 0; n < image_size; n++) {
-        expected_content[n] = uint8_t(n);
-    }
-    std::vector<uint8_t> actual_content;
-    actual_content.resize(image_size);
-    EXPECT_TRUE(
-            base::ReadFile(boot_path, reinterpret_cast<char*>(actual_content.data()), image_size));
-    EXPECT_EQ(expected_content, actual_content);
-}
-
-TEST_F(BaseFsAvbTest, GenerateVBMetaImage) {
-    GenerateVBMetaImage("vbmeta.img", "SHA256_RSA2048", 0,
-                        base::FilePath("data/testkey_rsa2048.pem"),
-                        {}, /* include_descriptor_image_paths */
-                        {}, /* chain_partitions */
-                        "--internal_release_string \"unit test\"");
-    EXPECT_EQ("5eba9ad4e775645e7eac441a563c200681ae868158d06f6a6cd36d06c07bd781",
-              CalcVBMetaDigest("vbmeta.img", "sha256"));
-    EXPECT_EQ(
-            "Minimum libavb version:   1.0\n"
-            "Header Block:             256 bytes\n"
-            "Authentication Block:     320 bytes\n"
-            "Auxiliary Block:          576 bytes\n"
-            "Algorithm:                SHA256_RSA2048\n"
-            "Rollback Index:           0\n"
-            "Flags:                    0\n"
-            "Release String:           'unit test'\n"
-            "Descriptors:\n"
-            "    (none)\n",
-            InfoImage("vbmeta.img"));
-}
-
-TEST_F(BaseFsAvbTest, AddHashFooter) {
-    // Generates a raw boot.img
-    const size_t image_size = 5 * 1024 * 1024;
-    const size_t partition_size = 10 * 1024 * 1024;
-    base::FilePath boot_path = GenerateImage("boot.img", image_size);
-    EXPECT_NE(0U, boot_path.value().size());
-    // Checks file size is as expected.
-    int64_t file_size;
-    ASSERT_TRUE(base::GetFileSize(boot_path, &file_size));
-    EXPECT_EQ(file_size, image_size);
-    // Appends AVB Hash Footer.
-    AddAvbFooter(boot_path, "hash", "boot", partition_size, "SHA256_RSA4096", 10,
-                 base::FilePath("data/testkey_rsa4096.pem"), "d00df00d",
-                 "--internal_release_string \"unit test\"");
-    // Extracts boot vbmeta from boot.img into boot-vbmeta.img.
-    ExtractVBMetaImage(boot_path, "boot-vbmeta.img");
-    EXPECT_EQ(
-            "Minimum libavb version:   1.0\n"
-            "Header Block:             256 bytes\n"
-            "Authentication Block:     576 bytes\n"
-            "Auxiliary Block:          1216 bytes\n"
-            "Algorithm:                SHA256_RSA4096\n"
-            "Rollback Index:           10\n"
-            "Flags:                    0\n"
-            "Release String:           'unit test'\n"
-            "Descriptors:\n"
-            "    Hash descriptor:\n"
-            "      Image Size:            5242880 bytes\n"
-            "      Hash Algorithm:        sha256\n"
-            "      Partition Name:        boot\n"
-            "      Salt:                  d00df00d\n"
-            "      Digest:                "
-            "222dd01e98284a1fcd7781f85d1392e43a530511a64eff96db197db90ebc4df1\n"
-            "      Flags:                 0\n",
-            InfoImage("boot-vbmeta.img"));
-}
-
-TEST_F(BaseFsAvbTest, AddHashtreeFooter) {
-    // Generates a raw system.img
-    const size_t image_size = 50 * 1024 * 1024;
-    const size_t partition_size = 60 * 1024 * 1024;
-    base::FilePath system_path = GenerateImage("system.img", image_size);
-    EXPECT_NE(0U, system_path.value().size());
-    // Checks file size is as expected.
-    int64_t file_size;
-    ASSERT_TRUE(base::GetFileSize(system_path, &file_size));
-    EXPECT_EQ(file_size, image_size);
-    // Appends AVB Hashtree Footer.
-    AddAvbFooter(system_path, "hashtree", "system", partition_size, "SHA512_RSA8192", 20,
-                 base::FilePath("data/testkey_rsa8192.pem"), "d00df00d",
-                 "--internal_release_string \"unit test\"");
-    // Extracts system vbmeta from system.img into system-vbmeta.img.
-    ExtractVBMetaImage(system_path, "system-vbmeta.img");
-    EXPECT_EQ(
-            "Minimum libavb version:   1.0\n"
-            "Header Block:             256 bytes\n"
-            "Authentication Block:     1088 bytes\n"
-            "Auxiliary Block:          2304 bytes\n"
-            "Algorithm:                SHA512_RSA8192\n"
-            "Rollback Index:           20\n"
-            "Flags:                    0\n"
-            "Release String:           'unit test'\n"
-            "Descriptors:\n"
-            "    Hashtree descriptor:\n"
-            "      Version of dm-verity:  1\n"
-            "      Image Size:            52428800 bytes\n"
-            "      Tree Offset:           52428800\n"
-            "      Tree Size:             413696 bytes\n"
-            "      Data Block Size:       4096 bytes\n"
-            "      Hash Block Size:       4096 bytes\n"
-            "      FEC num roots:         2\n"
-            "      FEC offset:            52842496\n"
-            "      FEC size:              417792 bytes\n"
-            "      Hash Algorithm:        sha1\n"
-            "      Partition Name:        system\n"
-            "      Salt:                  d00df00d\n"
-            "      Root Digest:           d20d40c02298e385ab6d398a61a3b91dc9947d99\n"
-            "      Flags:                 0\n",
-            InfoImage("system-vbmeta.img"));
-}
-
-TEST_F(BaseFsAvbTest, GenerateVBMetaImageWithDescriptors) {
-    // Generates a raw boot.img
-    const size_t boot_image_size = 5 * 1024 * 1024;
-    const size_t boot_partition_size = 10 * 1024 * 1024;
-    base::FilePath boot_path = GenerateImage("boot.img", boot_image_size);
-    // Adds AVB Hash Footer.
-    AddAvbFooter(boot_path, "hash", "boot", boot_partition_size, "SHA256_RSA4096", 10,
-                 base::FilePath("data/testkey_rsa4096.pem"), "d00df00d",
-                 "--internal_release_string \"unit test\"");
-
-    // Generates a raw system.img, use a smaller size to speed-up unit test.
-    const size_t system_image_size = 10 * 1024 * 1024;
-    const size_t system_partition_size = 15 * 1024 * 1024;
-    base::FilePath system_path = GenerateImage("system.img", system_image_size);
-    // Adds AVB Hashtree Footer.
-    AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA8192", 20,
-                 base::FilePath("data/testkey_rsa8192.pem"), "d00df00d",
-                 "--internal_release_string \"unit test\"");
-
-    // Makes a vbmeta.img including both 'boot' and 'system' descriptors.
-    GenerateVBMetaImage("vbmeta.img", "SHA256_RSA2048", 0,
-                        base::FilePath("data/testkey_rsa2048.pem"),
-                        {boot_path, system_path}, /* include_descriptor_image_paths */
-                        {},                       /* chain_partitions */
-                        "--internal_release_string \"unit test\"");
-    EXPECT_EQ("a069cbfc30c816cddf3b53f1ad53b7ca5d61a3d93845eb596bbb1b40caa1c62f",
-              CalcVBMetaDigest("vbmeta.img", "sha256"));
-    EXPECT_EQ(
-            "Minimum libavb version:   1.0\n"
-            "Header Block:             256 bytes\n"
-            "Authentication Block:     320 bytes\n"
-            "Auxiliary Block:          960 bytes\n"
-            "Algorithm:                SHA256_RSA2048\n"
-            "Rollback Index:           0\n"
-            "Flags:                    0\n"
-            "Release String:           'unit test'\n"
-            "Descriptors:\n"
-            "    Hash descriptor:\n"
-            "      Image Size:            5242880 bytes\n"
-            "      Hash Algorithm:        sha256\n"
-            "      Partition Name:        boot\n"
-            "      Salt:                  d00df00d\n"
-            "      Digest:                "
-            "222dd01e98284a1fcd7781f85d1392e43a530511a64eff96db197db90ebc4df1\n"
-            "      Flags:                 0\n"
-            "    Hashtree descriptor:\n"
-            "      Version of dm-verity:  1\n"
-            "      Image Size:            10485760 bytes\n"
-            "      Tree Offset:           10485760\n"
-            "      Tree Size:             86016 bytes\n"
-            "      Data Block Size:       4096 bytes\n"
-            "      Hash Block Size:       4096 bytes\n"
-            "      FEC num roots:         2\n"
-            "      FEC offset:            10571776\n"
-            "      FEC size:              90112 bytes\n"
-            "      Hash Algorithm:        sha1\n"
-            "      Partition Name:        system\n"
-            "      Salt:                  d00df00d\n"
-            "      Root Digest:           a3d5dd307341393d85de356c384ff543ec1ed81b\n"
-            "      Flags:                 0\n",
-            InfoImage("vbmeta.img"));
-}
-
-TEST_F(BaseFsAvbTest, GenerateVBMetaImageWithChainDescriptors) {
-    // Generates a raw boot.img
-    const size_t boot_image_size = 5 * 1024 * 1024;
-    const size_t boot_partition_size = 10 * 1024 * 1024;
-    base::FilePath boot_path = GenerateImage("boot.img", boot_image_size);
-    // Adds AVB Hash Footer.
-    AddAvbFooter(boot_path, "hash", "boot", boot_partition_size, "SHA256_RSA2048", 10,
-                 base::FilePath("data/testkey_rsa2048.pem"), "d00df00d",
-                 "--internal_release_string \"unit test\"");
-
-    // Generates a raw system.img, use a smaller size to speed-up unit test.
-    const size_t system_image_size = 10 * 1024 * 1024;
-    const size_t system_partition_size = 15 * 1024 * 1024;
-    base::FilePath system_path = GenerateImage("system.img", system_image_size);
-    // Adds AVB Hashtree Footer.
-    AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA4096", 20,
-                 base::FilePath("data/testkey_rsa4096.pem"), "d00df00d",
-                 "--internal_release_string \"unit test\"");
-
-    // Make a vbmeta image with chain partitions.
-    base::FilePath rsa2048_public_key =
-            ExtractPublicKeyAvb(base::FilePath("data/testkey_rsa2048.pem"));
-    base::FilePath rsa4096_public_key =
-            ExtractPublicKeyAvb(base::FilePath("data/testkey_rsa4096.pem"));
-    GenerateVBMetaImage("vbmeta.img", "SHA256_RSA8192", 0,
-                        base::FilePath("data/testkey_rsa8192.pem"),
-                        {},                               /* include_descriptor_image_paths */
-                        {{"boot", 1, rsa2048_public_key}, /* chain_partitions */
-                         {"system", 2, rsa4096_public_key}},
-                        "--internal_release_string \"unit test\"");
-
-    // vbmeta digest calculation includes the chained vbmeta from boot.img and system.img.
-    EXPECT_EQ("abbe11b316901f3336e26630f64c4732dadbe14532186ac8640e4141a403721f",
-              CalcVBMetaDigest("vbmeta.img", "sha256"));
-    EXPECT_EQ(
-            "Minimum libavb version:   1.0\n"
-            "Header Block:             256 bytes\n"
-            "Authentication Block:     1088 bytes\n"
-            "Auxiliary Block:          3840 bytes\n"
-            "Algorithm:                SHA256_RSA8192\n"
-            "Rollback Index:           0\n"
-            "Flags:                    0\n"
-            "Release String:           'unit test'\n"
-            "Descriptors:\n"
-            "    Chain Partition descriptor:\n"
-            "      Partition Name:          boot\n"
-            "      Rollback Index Location: 1\n"
-            "      Public key (sha1):       cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
-            "    Chain Partition descriptor:\n"
-            "      Partition Name:          system\n"
-            "      Rollback Index Location: 2\n"
-            "      Public key (sha1):       2597c218aae470a130f61162feaae70afd97f011\n",
-            InfoImage("vbmeta.img"));
-}
-
-}  // namespace fs_avb_host_test
-
-int main(int argc, char** argv) {
-    ::testing::InitGoogleTest(&argc, argv);
-    return RUN_ALL_TESTS();
-}
diff --git a/fs_mgr/libfs_avb/tests/util_test.cpp b/fs_mgr/libfs_avb/tests/util_test.cpp
new file mode 100644
index 0000000..835e8fd
--- /dev/null
+++ b/fs_mgr/libfs_avb/tests/util_test.cpp
@@ -0,0 +1,211 @@
+/*
+ * Copyright (C) 2019 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 <unistd.h>
+#include <future>
+#include <string>
+#include <thread>
+
+#include <base/files/file_util.h>
+
+#include "fs_avb_test_util.h"
+#include "util.h"
+
+// Target functions to test:
+using android::fs_mgr::BytesToHex;
+using android::fs_mgr::HexToBytes;
+using android::fs_mgr::NibbleValue;
+using android::fs_mgr::WaitForFile;
+
+namespace fs_avb_host_test {
+
+TEST(BasicUtilTest, NibbleValue09) {
+    uint8_t value;
+
+    EXPECT_TRUE(NibbleValue('0', &value));
+    EXPECT_EQ(0, value);
+    EXPECT_TRUE(NibbleValue('1', &value));
+    EXPECT_EQ(1, value);
+    EXPECT_TRUE(NibbleValue('2', &value));
+    EXPECT_EQ(2, value);
+    EXPECT_TRUE(NibbleValue('3', &value));
+    EXPECT_EQ(3, value);
+    EXPECT_TRUE(NibbleValue('4', &value));
+    EXPECT_EQ(4, value);
+    EXPECT_TRUE(NibbleValue('5', &value));
+    EXPECT_EQ(5, value);
+    EXPECT_TRUE(NibbleValue('6', &value));
+    EXPECT_EQ(6, value);
+    EXPECT_TRUE(NibbleValue('7', &value));
+    EXPECT_EQ(7, value);
+    EXPECT_TRUE(NibbleValue('8', &value));
+    EXPECT_EQ(8, value);
+    EXPECT_TRUE(NibbleValue('9', &value));
+    EXPECT_EQ(9, value);
+}
+
+TEST(BasicUtilTest, NibbleValueAF) {
+    uint8_t value;
+
+    EXPECT_TRUE(NibbleValue('a', &value));
+    EXPECT_EQ(10, value);
+    EXPECT_TRUE(NibbleValue('b', &value));
+    EXPECT_EQ(11, value);
+    EXPECT_TRUE(NibbleValue('c', &value));
+    EXPECT_EQ(12, value);
+    EXPECT_TRUE(NibbleValue('d', &value));
+    EXPECT_EQ(13, value);
+    EXPECT_TRUE(NibbleValue('e', &value));
+    EXPECT_EQ(14, value);
+    EXPECT_TRUE(NibbleValue('f', &value));
+    EXPECT_EQ(15, value);
+
+    EXPECT_TRUE(NibbleValue('A', &value));
+    EXPECT_EQ(10, value);
+    EXPECT_TRUE(NibbleValue('B', &value));
+    EXPECT_EQ(11, value);
+    EXPECT_TRUE(NibbleValue('C', &value));
+    EXPECT_EQ(12, value);
+    EXPECT_TRUE(NibbleValue('D', &value));
+    EXPECT_EQ(13, value);
+    EXPECT_TRUE(NibbleValue('E', &value));
+    EXPECT_EQ(14, value);
+    EXPECT_TRUE(NibbleValue('F', &value));
+    EXPECT_EQ(15, value);
+}
+
+TEST(BasicUtilTest, NibbleValueInvalid) {
+    uint8_t value;
+
+    EXPECT_FALSE(NibbleValue('G', &value));
+    EXPECT_FALSE(NibbleValue('H', &value));
+    EXPECT_FALSE(NibbleValue('I', &value));
+    EXPECT_FALSE(NibbleValue('x', &value));
+    EXPECT_FALSE(NibbleValue('y', &value));
+    EXPECT_FALSE(NibbleValue('z', &value));
+}
+
+TEST(BasicUtilTest, HexToBytes) {
+    std::string hex = "000102030405060708090A0B0C0D0E0F";
+    uint8_t bytes[16];
+
+    EXPECT_TRUE(HexToBytes((uint8_t*)bytes, sizeof(bytes), hex));
+    for (size_t i = 0; i < sizeof(bytes); i++) {
+        EXPECT_EQ(i, bytes[i]);
+    }
+}
+
+TEST(BasicUtilTest, HexToBytes2) {
+    std::string hex = "101112131415161718191A1B1C1D1E1F";
+    uint8_t bytes[16];
+
+    EXPECT_TRUE(HexToBytes((uint8_t*)bytes, sizeof(bytes), hex));
+    for (size_t i = 0; i < sizeof(bytes); i++) {
+        EXPECT_EQ(16 + i, bytes[i]);
+    }
+}
+
+TEST(BasicUtilTest, BytesToHex) {
+    const uint8_t bytes[16]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
+
+    EXPECT_EQ("0102", BytesToHex((uint8_t*)bytes, 2));
+    EXPECT_EQ("01020304", BytesToHex((uint8_t*)bytes, 4));
+    EXPECT_EQ("0102030405060708", BytesToHex((uint8_t*)bytes, 8));
+    EXPECT_EQ("0102030405060708090a0b0c0d0e0f10", BytesToHex((uint8_t*)bytes, 16));
+
+    EXPECT_EQ("01", BytesToHex((uint8_t*)bytes, 1));
+    EXPECT_EQ("010203", BytesToHex((uint8_t*)bytes, 3));
+    EXPECT_EQ("0102030405", BytesToHex((uint8_t*)bytes, 5));
+}
+
+TEST(BasicUtilTest, HexToBytesInValidOddLenHex) {
+    std::string hex = "12345";
+    uint8_t bytes[16];
+
+    EXPECT_FALSE(HexToBytes((uint8_t*)bytes, sizeof(bytes), hex));
+}
+
+TEST(BasicUtilTest, HexToBytesInsufficientByteLen) {
+    std::string hex = "101112131415161718191A1B1C1D1E1F";
+    uint8_t bytes[8];
+
+    EXPECT_FALSE(HexToBytes((uint8_t*)bytes, sizeof(bytes), hex));
+}
+
+TEST(BasicUtilTest, WaitForFile) {
+    // Gets system tmp dir.
+    base::FilePath tmp_dir;
+    ASSERT_TRUE(GetTempDir(&tmp_dir));
+
+    // Waits this path.
+    base::FilePath wait_path = tmp_dir.Append("libfs_avb-test-exist-dir");
+    ASSERT_TRUE(base::DeleteFile(wait_path, false /* resursive */));
+
+    EXPECT_TRUE(base::CreateDirectory(wait_path));
+    EXPECT_TRUE(WaitForFile(wait_path.value(), 1s));
+
+    // Removes the wait_path.
+    ASSERT_TRUE(base::DeleteFile(wait_path, false /* resursive */));
+}
+
+TEST(BasicUtilTest, WaitForFileNonExist) {
+    base::FilePath wait_path("/path/not/exist");
+    EXPECT_FALSE(WaitForFile(wait_path.value(), 200ms));
+}
+
+TEST(BasicUtilTest, WaitForFileDeferCreation) {
+    // Gets system tmp dir.
+    base::FilePath tmp_dir;
+    ASSERT_TRUE(GetTempDir(&tmp_dir));
+
+    // Waits this path.
+    base::FilePath wait_path = tmp_dir.Append("libfs_avb-test-exist-dir");
+    ASSERT_TRUE(base::DeleteFile(wait_path, false /* resursive */));
+    auto wait_file = std::async(WaitForFile, wait_path.value(), 500ms);
+
+    // Sleeps 100ms before creating the wait_path.
+    std::this_thread::sleep_for(100ms);
+    EXPECT_TRUE(base::CreateDirectory(wait_path));
+
+    // Checks WaitForFile() returns success.
+    EXPECT_TRUE(wait_file.get());
+
+    // Removes the wait_path.
+    ASSERT_TRUE(base::DeleteFile(wait_path, false /* resursive */));
+}
+
+TEST(BasicUtilTest, WaitForFileDeferCreationFailure) {
+    // Gets system tmp dir.
+    base::FilePath tmp_dir;
+    ASSERT_TRUE(GetTempDir(&tmp_dir));
+
+    // Waits this path.
+    base::FilePath wait_path = tmp_dir.Append("libfs_avb-test-exist-dir");
+    ASSERT_TRUE(base::DeleteFile(wait_path, false /* resursive */));
+    auto wait_file = std::async(WaitForFile, wait_path.value(), 50ms);
+
+    // Sleeps 100ms before creating the wait_path.
+    std::this_thread::sleep_for(100ms);
+    EXPECT_TRUE(base::CreateDirectory(wait_path));
+
+    // Checks WaitForFile() returns failure, because it only waits 50ms.
+    EXPECT_FALSE(wait_file.get());
+
+    // Removes the wait_path.
+    ASSERT_TRUE(base::DeleteFile(wait_path, false /* resursive */));
+}
+
+}  // namespace fs_avb_host_test
diff --git a/fs_mgr/libfs_avb/util.cpp b/fs_mgr/libfs_avb/util.cpp
new file mode 100644
index 0000000..17d47d9
--- /dev/null
+++ b/fs_mgr/libfs_avb/util.cpp
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2019 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 "util.h"
+
+#include <sys/ioctl.h>
+#include <thread>
+
+#include <android-base/unique_fd.h>
+#include <linux/fs.h>
+
+namespace android {
+namespace fs_mgr {
+
+bool NibbleValue(const char& c, uint8_t* value) {
+    CHECK(value != nullptr);
+
+    switch (c) {
+        case '0' ... '9':
+            *value = c - '0';
+            break;
+        case 'a' ... 'f':
+            *value = c - 'a' + 10;
+            break;
+        case 'A' ... 'F':
+            *value = c - 'A' + 10;
+            break;
+        default:
+            return false;
+    }
+
+    return true;
+}
+
+bool HexToBytes(uint8_t* bytes, size_t bytes_len, const std::string& hex) {
+    CHECK(bytes != nullptr);
+
+    if (hex.size() % 2 != 0) {
+        return false;
+    }
+    if (hex.size() / 2 > bytes_len) {
+        return false;
+    }
+    for (size_t i = 0, j = 0, n = hex.size(); i < n; i += 2, ++j) {
+        uint8_t high;
+        if (!NibbleValue(hex[i], &high)) {
+            return false;
+        }
+        uint8_t low;
+        if (!NibbleValue(hex[i + 1], &low)) {
+            return false;
+        }
+        bytes[j] = (high << 4) | low;
+    }
+    return true;
+}
+
+std::string BytesToHex(const uint8_t* bytes, size_t bytes_len) {
+    CHECK(bytes != nullptr);
+
+    static const char* hex_digits = "0123456789abcdef";
+    std::string hex;
+
+    for (size_t i = 0; i < bytes_len; i++) {
+        hex.push_back(hex_digits[(bytes[i] & 0xF0) >> 4]);
+        hex.push_back(hex_digits[bytes[i] & 0x0F]);
+    }
+    return hex;
+}
+
+bool WaitForFile(const std::string& filename, const std::chrono::milliseconds relative_timeout) {
+    auto start_time = std::chrono::steady_clock::now();
+
+    while (true) {
+        if (0 == access(filename.c_str(), F_OK) || errno != ENOENT) {
+            return true;
+        }
+
+        std::this_thread::sleep_for(50ms);
+
+        auto now = std::chrono::steady_clock::now();
+        auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
+        if (time_elapsed > relative_timeout) return false;
+    }
+}
+
+bool IsDeviceUnlocked() {
+    std::string verified_boot_state;
+
+    if (fs_mgr_get_boot_config("verifiedbootstate", &verified_boot_state)) {
+        return verified_boot_state == "orange";
+    }
+    return false;
+}
+
+bool SetBlockDeviceReadOnly(const std::string& blockdev) {
+    android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blockdev.c_str(), O_RDONLY | O_CLOEXEC)));
+    if (fd < 0) {
+        return false;
+    }
+
+    int ON = 1;
+    return ioctl(fd, BLKROSET, &ON) == 0;
+}
+
+}  // namespace fs_mgr
+}  // namespace android
diff --git a/fs_mgr/libfs_avb/util.h b/fs_mgr/libfs_avb/util.h
new file mode 100644
index 0000000..cb861f4
--- /dev/null
+++ b/fs_mgr/libfs_avb/util.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2019 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 <chrono>
+#include <string>
+
+#ifdef HOST_TEST
+#include <base/logging.h>
+#else
+#include <android-base/logging.h>
+#endif
+
+#define FS_AVB_TAG "[libfs_avb]"
+
+// Logs a message to kernel
+#define LINFO LOG(INFO) << FS_AVB_TAG
+#define LWARNING LOG(WARNING) << FS_AVB_TAG
+#define LERROR LOG(ERROR) << FS_AVB_TAG
+#define LFATAL LOG(FATAL) << FS_AVB_TAG
+
+// Logs a message with strerror(errno) at the end
+#define PINFO PLOG(INFO) << FS_AVB_TAG
+#define PWARNING PLOG(WARNING) << FS_AVB_TAG
+#define PERROR PLOG(ERROR) << FS_AVB_TAG
+#define PFATAL PLOG(FATAL) << FS_AVB_TAG
+
+extern bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val);
+
+using namespace std::chrono_literals;
+
+namespace android {
+namespace fs_mgr {
+
+bool NibbleValue(const char& c, uint8_t* value);
+
+bool HexToBytes(uint8_t* bytes, size_t bytes_len, const std::string& hex);
+
+std::string BytesToHex(const uint8_t* bytes, size_t bytes_len);
+
+bool WaitForFile(const std::string& filename, const std::chrono::milliseconds relative_timeout);
+
+bool IsDeviceUnlocked();
+
+bool SetBlockDeviceReadOnly(const std::string& blockdev);
+
+}  // namespace fs_mgr
+}  // namespace android
diff --git a/fs_mgr/liblp/builder.cpp b/fs_mgr/liblp/builder.cpp
index 07e3c8a..b99ff8f 100644
--- a/fs_mgr/liblp/builder.cpp
+++ b/fs_mgr/liblp/builder.cpp
@@ -33,6 +33,8 @@
 bool MetadataBuilder::sABOverrideSet;
 bool MetadataBuilder::sABOverrideValue;
 
+static const std::string kDefaultGroup = "default";
+
 bool LinearExtent::AddTo(LpMetadata* out) const {
     if (device_index_ >= out->block_devices.size()) {
         LERROR << "Extent references unknown block device.";
@@ -403,7 +405,7 @@
     geometry_.metadata_slot_count = metadata_slot_count;
     geometry_.logical_block_size = logical_block_size;
 
-    if (!AddGroup("default", 0)) {
+    if (!AddGroup(kDefaultGroup, 0)) {
         return false;
     }
     return true;
@@ -419,7 +421,7 @@
 }
 
 Partition* MetadataBuilder::AddPartition(const std::string& name, uint32_t attributes) {
-    return AddPartition(name, "default", attributes);
+    return AddPartition(name, kDefaultGroup, attributes);
 }
 
 Partition* MetadataBuilder::AddPartition(const std::string& name, const std::string& group_name,
@@ -675,6 +677,10 @@
 }
 
 std::unique_ptr<LpMetadata> MetadataBuilder::Export() {
+    if (!ValidatePartitionGroups()) {
+        return nullptr;
+    }
+
     std::unique_ptr<LpMetadata> metadata = std::make_unique<LpMetadata>();
     metadata->header = header_;
     metadata->geometry = geometry_;
@@ -695,7 +701,7 @@
             LERROR << "Partition group name is too long: " << group->name();
             return nullptr;
         }
-        if (auto_slot_suffixing_ && group->name() != "default") {
+        if (auto_slot_suffixing_ && group->name() != kDefaultGroup) {
             out.flags |= LP_GROUP_SLOT_SUFFIXED;
         }
         strncpy(out.name, group->name().c_str(), sizeof(out.name));
@@ -877,7 +883,7 @@
 }
 
 void MetadataBuilder::RemoveGroupAndPartitions(const std::string& group_name) {
-    if (group_name == "default") {
+    if (group_name == kDefaultGroup) {
         // Cannot remove the default group.
         return;
     }
@@ -1000,5 +1006,53 @@
     return true;
 }
 
+std::vector<Partition*> MetadataBuilder::ListPartitionsInGroup(const std::string& group_name) {
+    std::vector<Partition*> partitions;
+    for (const auto& partition : partitions_) {
+        if (partition->group_name() == group_name) {
+            partitions.emplace_back(partition.get());
+        }
+    }
+    return partitions;
+}
+
+bool MetadataBuilder::ChangePartitionGroup(Partition* partition, const std::string& group_name) {
+    if (!FindGroup(group_name)) {
+        LERROR << "Partition cannot change to unknown group: " << group_name;
+        return false;
+    }
+    partition->set_group_name(group_name);
+    return true;
+}
+
+bool MetadataBuilder::ValidatePartitionGroups() const {
+    for (const auto& group : groups_) {
+        if (!group->maximum_size()) {
+            continue;
+        }
+        uint64_t used = TotalSizeOfGroup(group.get());
+        if (used > group->maximum_size()) {
+            LERROR << "Partition group " << group->name() << " exceeds maximum size (" << used
+                   << " bytes used, maximum " << group->maximum_size() << ")";
+            return false;
+        }
+    }
+    return true;
+}
+
+bool MetadataBuilder::ChangeGroupSize(const std::string& group_name, uint64_t maximum_size) {
+    if (group_name == kDefaultGroup) {
+        LERROR << "Cannot change the size of the default group";
+        return false;
+    }
+    PartitionGroup* group = FindGroup(group_name);
+    if (!group) {
+        LERROR << "Cannot change size of unknown partition group: " << group_name;
+        return false;
+    }
+    group->set_maximum_size(maximum_size);
+    return true;
+}
+
 }  // namespace fs_mgr
 }  // namespace android
diff --git a/fs_mgr/liblp/builder_test.cpp b/fs_mgr/liblp/builder_test.cpp
index 3793964..7d615a3 100644
--- a/fs_mgr/liblp/builder_test.cpp
+++ b/fs_mgr/liblp/builder_test.cpp
@@ -549,6 +549,58 @@
     EXPECT_EQ(partition->size(), 16384);
 }
 
+TEST_F(BuilderTest, ListPartitionsInGroup) {
+    BlockDeviceInfo device_info("super", 1024 * 1024, 0, 0, 4096);
+    unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(device_info, 1024, 1);
+    ASSERT_NE(builder, nullptr);
+
+    ASSERT_TRUE(builder->AddGroup("groupA", 16384));
+    ASSERT_TRUE(builder->AddGroup("groupB", 16384));
+
+    Partition* system = builder->AddPartition("system", "groupA", 0);
+    Partition* vendor = builder->AddPartition("vendor", "groupA", 0);
+    Partition* product = builder->AddPartition("product", "groupB", 0);
+    ASSERT_NE(system, nullptr);
+    ASSERT_NE(vendor, nullptr);
+    ASSERT_NE(product, nullptr);
+
+    auto groupA = builder->ListPartitionsInGroup("groupA");
+    auto groupB = builder->ListPartitionsInGroup("groupB");
+    auto groupC = builder->ListPartitionsInGroup("groupC");
+    ASSERT_THAT(groupA, ElementsAre(system, vendor));
+    ASSERT_THAT(groupB, ElementsAre(product));
+    ASSERT_TRUE(groupC.empty());
+}
+
+TEST_F(BuilderTest, ChangeGroups) {
+    BlockDeviceInfo device_info("super", 1024 * 1024, 0, 0, 4096);
+    unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(device_info, 1024, 1);
+    ASSERT_NE(builder, nullptr);
+
+    ASSERT_TRUE(builder->AddGroup("groupA", 16384));
+    ASSERT_TRUE(builder->AddGroup("groupB", 32768));
+
+    Partition* system = builder->AddPartition("system", "groupA", 0);
+    Partition* vendor = builder->AddPartition("vendor", "groupB", 0);
+    ASSERT_NE(system, nullptr);
+    ASSERT_NE(vendor, nullptr);
+    ASSERT_NE(builder->Export(), nullptr);
+
+    ASSERT_FALSE(builder->ChangePartitionGroup(system, "groupXYZ"));
+    ASSERT_TRUE(builder->ChangePartitionGroup(system, "groupB"));
+    ASSERT_NE(builder->Export(), nullptr);
+
+    // Violate group constraint by reassigning groups.
+    ASSERT_TRUE(builder->ResizePartition(system, 16384 + 4096));
+    ASSERT_TRUE(builder->ChangePartitionGroup(system, "groupA"));
+    ASSERT_EQ(builder->Export(), nullptr);
+
+    ASSERT_FALSE(builder->ChangeGroupSize("default", 2));
+    ASSERT_FALSE(builder->ChangeGroupSize("unknown", 2));
+    ASSERT_TRUE(builder->ChangeGroupSize("groupA", 32768));
+    ASSERT_NE(builder->Export(), nullptr);
+}
+
 constexpr unsigned long long operator"" _GiB(unsigned long long x) {  // NOLINT
     return x << 30;
 }
diff --git a/fs_mgr/liblp/include/liblp/builder.h b/fs_mgr/liblp/include/liblp/builder.h
index 57cce21..53f480f 100644
--- a/fs_mgr/liblp/include/liblp/builder.h
+++ b/fs_mgr/liblp/include/liblp/builder.h
@@ -80,6 +80,8 @@
 };
 
 class PartitionGroup final {
+    friend class MetadataBuilder;
+
   public:
     explicit PartitionGroup(const std::string& name, uint64_t maximum_size)
         : name_(name), maximum_size_(maximum_size) {}
@@ -88,6 +90,8 @@
     uint64_t maximum_size() const { return maximum_size_; }
 
   private:
+    void set_maximum_size(uint64_t maximum_size) { maximum_size_ = maximum_size; }
+
     std::string name_;
     uint64_t maximum_size_;
 };
@@ -116,6 +120,7 @@
 
   private:
     void ShrinkTo(uint64_t aligned_size);
+    void set_group_name(const std::string& group_name) { group_name_ = group_name; }
 
     std::string name_;
     std::string group_name_;
@@ -235,6 +240,21 @@
     // underlying filesystem or contents of the partition on disk.
     bool ResizePartition(Partition* partition, uint64_t requested_size);
 
+    // Return the list of partitions belonging to a group.
+    std::vector<Partition*> ListPartitionsInGroup(const std::string& group_name);
+
+    // Changes a partition's group. Size constraints will not be checked until
+    // the metadata is exported, to avoid errors during potential group and
+    // size shuffling operations. This will return false if the new group does
+    // not exist.
+    bool ChangePartitionGroup(Partition* partition, const std::string& group_name);
+
+    // Changes the size of a partition group. Size constraints will not be
+    // checked until metadata is exported, to avoid errors during group
+    // reshuffling. This will return false if the group does not exist, or if
+    // the group name is "default".
+    bool ChangeGroupSize(const std::string& group_name, uint64_t maximum_size);
+
     // Amount of space that can be allocated to logical partitions.
     uint64_t AllocatableSpace() const;
     uint64_t UsedSpace() const;
@@ -283,6 +303,7 @@
     bool ImportPartition(const LpMetadata& metadata, const LpMetadataPartition& source);
     bool IsABDevice() const;
     bool IsRetrofitDevice() const;
+    bool ValidatePartitionGroups() const;
 
     struct Interval {
         uint32_t device_index;
diff --git a/fs_mgr/liblp/utility.cpp b/fs_mgr/liblp/utility.cpp
index 9ccabe9..ecf94a4 100644
--- a/fs_mgr/liblp/utility.cpp
+++ b/fs_mgr/liblp/utility.cpp
@@ -19,6 +19,11 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#if defined(__linux__)
+#include <linux/fs.h>
+#include <sys/ioctl.h>
+#endif
+
 #include <android-base/file.h>
 #include <ext4_utils/ext4_utils.h>
 #include <openssl/sha.h>
@@ -155,5 +160,16 @@
     return true;
 }
 
+bool SetBlockReadonly(int fd, bool readonly) {
+#if defined(__linux__)
+    int val = readonly;
+    return ioctl(fd, BLKROSET, &val) == 0;
+#else
+    (void)fd;
+    (void)readonly;
+    return true;
+#endif
+}
+
 }  // namespace fs_mgr
 }  // namespace android
diff --git a/fs_mgr/liblp/utility.h b/fs_mgr/liblp/utility.h
index 8b70919..e8b2ca9 100644
--- a/fs_mgr/liblp/utility.h
+++ b/fs_mgr/liblp/utility.h
@@ -29,6 +29,7 @@
 #define LWARN LOG(WARNING) << LP_TAG
 #define LINFO LOG(INFO) << LP_TAG
 #define LERROR LOG(ERROR) << LP_TAG
+#define PWARNING PLOG(WARNING) << LP_TAG
 #define PERROR PLOG(ERROR) << LP_TAG
 
 namespace android {
@@ -88,6 +89,9 @@
 bool UpdateBlockDevicePartitionName(LpMetadataBlockDevice* device, const std::string& name);
 bool UpdatePartitionGroupName(LpMetadataPartitionGroup* group, const std::string& name);
 
+// Call BLKROSET ioctl on fd so that fd is readonly / read-writable.
+bool SetBlockReadonly(int fd, bool readonly);
+
 }  // namespace fs_mgr
 }  // namespace android
 
diff --git a/fs_mgr/liblp/writer.cpp b/fs_mgr/liblp/writer.cpp
index 454258b..54a1883 100644
--- a/fs_mgr/liblp/writer.cpp
+++ b/fs_mgr/liblp/writer.cpp
@@ -259,6 +259,12 @@
         return false;
     }
 
+    // On retrofit devices, super_partition is system_other and might be set to readonly by
+    // fs_mgr_set_blk_ro(). Unset readonly so that fd can be written to.
+    if (!SetBlockReadonly(fd.get(), false)) {
+        PWARNING << __PRETTY_FUNCTION__ << " BLKROSET 0 failed: " << super_partition;
+    }
+
     // Write zeroes to the first block.
     std::string zeroes(LP_PARTITION_RESERVED_BYTES, 0);
     if (SeekFile64(fd, 0, SEEK_SET) < 0) {
diff --git a/fs_mgr/tests/adb-remount-test.sh b/fs_mgr/tests/adb-remount-test.sh
index 160e076..825109f 100755
--- a/fs_mgr/tests/adb-remount-test.sh
+++ b/fs_mgr/tests/adb-remount-test.sh
@@ -366,7 +366,7 @@
 fi
 D=`adb_sh df -k </dev/null` &&
   H=`echo "${D}" | head -1` &&
-  D=`echo "${D}" | grep "^overlay "` &&
+  D=`echo "${D}" | grep -v " /vendor/..*$" | grep "^overlay "` &&
   echo "${H}" &&
   echo "${D}" &&
   echo "${ORANGE}[  WARNING ]${NORMAL} overlays present before setup" >&2 ||
@@ -378,7 +378,7 @@
   D=`echo / /
      echo "${D}" | grep -v /dev/root`
 fi
-D=`echo "${D}" | cut -s -d' ' -f1`
+D=`echo "${D}" | cut -s -d' ' -f1 | sort -u`
 D=`adb_sh df -k ${D} </dev/null`
 echo "${D}"
 if [ X"${D}" = X"${D##* 100[%] }" ]; then
@@ -410,7 +410,7 @@
   fi
   D=`adb_sh df -k </dev/null` &&
     H=`echo "${D}" | head -1` &&
-    D=`echo "${D}" | grep "^overlay " || true` &&
+    D=`echo "${D}" | grep -v " /vendor/..*$" | grep "^overlay " || true` &&
     [ -z "${D}" ] ||
     ( echo "${H}" && echo "${D}" && false ) ||
     die -t ${T} "overlay takeover unexpected at this phase"
@@ -443,7 +443,7 @@
   echo "${H}"
   D=`adb_sh df -k </dev/null` &&
     H=`echo "${D}" | head -1` &&
-    D=`echo "${D}" | grep "^overlay " || true` &&
+    D=`echo "${D}" | grep -v " /vendor/..*$" | grep "^overlay " || true` &&
     [ -z "${D}" ] ||
     ( echo "${H}" && echo "${D}" && false ) ||
     ( [ -n "${L}" ] && echo "${L}" && false ) ||
@@ -463,7 +463,7 @@
   die -t "${T}" "adb remount failed"
 D=`adb_sh df -k </dev/null` &&
   H=`echo "${D}" | head -1` &&
-  D=`echo "${D}" | grep "^overlay "` ||
+  D=`echo "${D}" | grep -v " /vendor/..*$" | grep "^overlay "` ||
   ( [ -n "${L}" ] && echo "${L}" && false )
 ret=${?}
 uses_dynamic_scratch=false
@@ -506,7 +506,9 @@
     echo "${D}" &&
     echo "${D}" | grep "^overlay .* /system\$" >/dev/null ||
     die  "overlay takeover after remount"
-  !(adb_sh grep "^overlay " /proc/mounts </dev/null | grep " overlay ro,") &&
+  !(adb_sh grep "^overlay " /proc/mounts </dev/null |
+    grep -v "^overlay /\(vendor\|system\)/..* overlay ro," |
+    grep " overlay ro,") &&
     !(adb_sh grep " rw," /proc/mounts </dev/null |
       skip_administrative_mounts data) ||
     die "remount overlayfs missed a spot (ro)"
@@ -539,7 +541,7 @@
 if ${overlayfs_needed}; then
   D=`adb_su df -k </dev/null` &&
     H=`echo "${D}" | head -1` &&
-    D=`echo "${D}" | grep "^overlay "` ||
+    D=`echo "${D}" | grep -v " /vendor/..*$" | grep "^overlay "` ||
     ( echo "${L}" && false ) ||
     die -d "overlay takeover failed after reboot"
 
@@ -621,7 +623,7 @@
     adb_root &&
       D=`adb_sh df -k </dev/null` &&
       H=`echo "${D}" | head -1` &&
-      D=`echo "${D}" | grep "^overlay "` &&
+      D=`echo "${D}" | grep -v " /vendor/..*$" | grep "^overlay "` &&
       echo "${H}" &&
       echo "${D}" &&
       echo "${D}" | grep "^overlay .* /system\$" >/dev/null ||
diff --git a/healthd/BatteryMonitor.cpp b/healthd/BatteryMonitor.cpp
index a1519da..06c8176 100644
--- a/healthd/BatteryMonitor.cpp
+++ b/healthd/BatteryMonitor.cpp
@@ -26,6 +26,8 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
+
+#include <algorithm>
 #include <memory>
 
 #include <android-base/file.h>
@@ -476,10 +478,16 @@
 
         while ((entry = readdir(dir.get()))) {
             const char* name = entry->d_name;
+            std::vector<String8>::iterator itIgnoreName;
 
             if (!strcmp(name, ".") || !strcmp(name, ".."))
                 continue;
 
+            itIgnoreName = find(hc->ignorePowerSupplyNames.begin(),
+                                hc->ignorePowerSupplyNames.end(), String8(name));
+            if (itIgnoreName != hc->ignorePowerSupplyNames.end())
+                continue;
+
             // Look for "type" file in each subdirectory
             path.clear();
             path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
diff --git a/healthd/include/healthd/healthd.h b/healthd/include/healthd/healthd.h
index c01e8d7..a900071 100644
--- a/healthd/include/healthd/healthd.h
+++ b/healthd/include/healthd/healthd.h
@@ -22,6 +22,8 @@
 #include <utils/Errors.h>
 #include <utils/String8.h>
 
+#include <vector>
+
 // periodic_chores_interval_fast, periodic_chores_interval_slow: intervals at
 // which healthd wakes up to poll health state and perform periodic chores,
 // in units of seconds:
@@ -71,6 +73,7 @@
     int (*energyCounter)(int64_t *);
     int boot_min_cap;
     bool (*screen_on)(android::BatteryProperties *props);
+    std::vector<android::String8> ignorePowerSupplyNames;
 };
 
 enum EventWakeup {
diff --git a/init/Android.bp b/init/Android.bp
index e7b8516..3369fb9 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -77,6 +77,7 @@
         "libext4_utils",
         "libfs_mgr",
         "libfscrypt",
+        "libgsi",
         "libhidl-gen-utils",
         "libkeyutils",
         "liblog",
@@ -85,6 +86,7 @@
         "libselinux",
         "libutils",
     ],
+    bootstrap: true,
 }
 
 cc_library_static {
diff --git a/init/Android.mk b/init/Android.mk
index bdd0301..69c63e1 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -91,6 +91,7 @@
     libz \
     libselinux \
     libcap \
+    libgsi \
 
 LOCAL_SANITIZE := signed-integer-overflow
 # First stage init is weird: it may start without stdout/stderr, and no /proc.
diff --git a/init/README.md b/init/README.md
index 8d47e0a..3a7c71c 100644
--- a/init/README.md
+++ b/init/README.md
@@ -482,9 +482,8 @@
   -f: force installation of the module even if the version of the running kernel
   and the version of the kernel for which the module was compiled do not match.
 
-`load_all_props`
-> Loads properties from /system, /vendor, et cetera.
-  This is included in the default init.rc.
+`load_system_props`
+> (This action is deprecated and no-op.)
 
 `load_persist_props`
 > Loads persistent properties when /data has been decrypted.
diff --git a/init/builtins.cpp b/init/builtins.cpp
index 7fd4e27..e1a86e4 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -54,6 +54,7 @@
 #include <fs_mgr.h>
 #include <fscrypt/fscrypt.h>
 #include <fscrypt/fscrypt_init_extensions.h>
+#include <libgsi/libgsi.h>
 #include <selinux/android.h>
 #include <selinux/label.h>
 #include <selinux/selinux.h>
@@ -100,6 +101,9 @@
 }
 
 static Result<Success> do_class_start(const BuiltinArguments& args) {
+    // Do not start a class if it has a property persist.dont_start_class.CLASS set to 1.
+    if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false))
+        return Success();
     // Starting a class does not start services which are explicitly disabled.
     // They must  be started individually.
     for (const auto& service : ServiceList::GetInstance()) {
@@ -124,6 +128,9 @@
 }
 
 static Result<Success> do_class_restart(const BuiltinArguments& args) {
+    // Do not restart a class if it has a property persist.dont_start_class.CLASS set to 1.
+    if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false))
+        return Success();
     ForEachServiceInClass(args[1], &Service::Restart);
     return Success();
 }
@@ -514,6 +521,9 @@
         return Success();
     } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
         /* Setup a wipe via recovery, and reboot into recovery */
+        if (android::gsi::IsGsiRunning()) {
+            return Error() << "cannot wipe within GSI";
+        }
         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" };
         return reboot_into_recovery(options);
@@ -966,7 +976,7 @@
 }
 
 static Result<Success> do_load_system_props(const BuiltinArguments& args) {
-    load_system_props();
+    LOG(INFO) << "deprecated action `load_system_props` called.";
     return Success();
 }
 
@@ -1016,7 +1026,8 @@
     }
     service->AddReapCallback([reboot_reason](const siginfo_t& siginfo) {
         if (siginfo.si_code != CLD_EXITED || siginfo.si_status != 0) {
-            if (fscrypt_is_native()) {
+            // TODO (b/122850122): support this in gsi
+            if (fscrypt_is_native() && !android::gsi::IsGsiRunning()) {
                 LOG(ERROR) << "Rebooting into recovery, reason: " << reboot_reason;
                 if (auto result = reboot_into_recovery(
                             {"--prompt_and_wipe_data", "--reason="s + reboot_reason});
@@ -1087,6 +1098,86 @@
     }
 }
 
+static Result<Success> bind_mount_file(const char* source, const char* mount_point,
+                                       bool remount_private) {
+    if (remount_private && mount(nullptr, mount_point, nullptr, MS_PRIVATE, nullptr) == -1) {
+        return ErrnoError() << "Could not change " << mount_point << " to a private mount point";
+    }
+    if (mount(source, mount_point, nullptr, MS_BIND, nullptr) == -1) {
+        return ErrnoError() << "Could not bind-mount " << source << " to " << mount_point;
+    }
+    return Success();
+}
+
+static Result<Success> bind_mount_bionic(const char* linker_source, const char* lib_dir_source,
+                                         const char* linker_mount_point, const char* lib_mount_dir,
+                                         bool remount_private) {
+    if (access(linker_source, F_OK) != 0) {
+        return Success();
+    }
+    if (auto result = bind_mount_file(linker_source, linker_mount_point, remount_private);
+        !result) {
+        return result;
+    }
+    for (auto libname : kBionicLibFileNames) {
+        std::string mount_point = lib_mount_dir + libname;
+        std::string source = lib_dir_source + libname;
+        if (auto result = bind_mount_file(source.c_str(), mount_point.c_str(), remount_private);
+            !result) {
+            return result;
+        }
+    }
+    return Success();
+}
+
+// The bootstrap bionic libs and the bootstrap linker are bind-mounted to
+// the mount points for pre-apexd processes.
+static Result<Success> do_prepare_bootstrap_bionic(const BuiltinArguments& args) {
+    static bool prepare_bootstrap_bionic_done = false;
+    if (prepare_bootstrap_bionic_done) {
+        return Error() << "prepare_bootstrap_bionic was already executed. Cannot be executed again";
+    }
+    if (auto result = bind_mount_bionic(kBootstrapLinkerPath, kBootstrapBionicLibsDir,
+                                        kLinkerMountPoint, kBionicLibsMountPointDir, false);
+        !result) {
+        return result;
+    }
+    if (auto result = bind_mount_bionic(kBootstrapLinkerPath64, kBootstrapBionicLibsDir64,
+                                        kLinkerMountPoint64, kBionicLibsMountPointDir64, false);
+        !result) {
+        return result;
+    }
+
+    LOG(INFO) << "prepare_bootstrap_bionic done";
+    prepare_bootstrap_bionic_done = true;
+    return Success();
+}
+
+// The bionic libs and the dynamic linker from the runtime APEX are bind-mounted
+// to the mount points. As a result, the previous mounts done by
+// prepare_bootstrap_bionic become hidden.
+static Result<Success> do_setup_runtime_bionic(const BuiltinArguments& args) {
+    static bool setup_runtime_bionic_done = false;
+    if (setup_runtime_bionic_done) {
+        return Error() << "setup_runtime_bionic was already executed. Cannot be executed again";
+    }
+    if (auto result = bind_mount_bionic(kRuntimeLinkerPath, kRuntimeBionicLibsDir,
+                                        kLinkerMountPoint, kBionicLibsMountPointDir, true);
+        !result) {
+        return result;
+    }
+    if (auto result = bind_mount_bionic(kRuntimeLinkerPath64, kRuntimeBionicLibsDir64,
+                                        kLinkerMountPoint64, kBionicLibsMountPointDir64, true);
+        !result) {
+        return result;
+    }
+
+    ServiceList::GetInstance().MarkRuntimeAvailable();
+    LOG(INFO) << "setup_runtime_bionic done";
+    setup_runtime_bionic_done = true;
+    return Success();
+}
+
 // Builtin-function-map start
 const BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
     constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
@@ -1125,6 +1216,7 @@
         {"mount_all",               {1,     kMax, {false,  do_mount_all}}},
         {"mount",                   {3,     kMax, {false,  do_mount}}},
         {"parse_apex_configs",      {0,     0,    {false,  do_parse_apex_configs}}},
+        {"prepare_bootstrap_bionic",{0,     0,    {false,  do_prepare_bootstrap_bionic}}},
         {"umount",                  {1,     1,    {false,  do_umount}}},
         {"readahead",               {1,     2,    {true,   do_readahead}}},
         {"restart",                 {1,     1,    {false,  do_restart}}},
@@ -1133,6 +1225,7 @@
         {"rm",                      {1,     1,    {true,   do_rm}}},
         {"rmdir",                   {1,     1,    {true,   do_rmdir}}},
         {"setprop",                 {2,     2,    {true,   do_setprop}}},
+        {"setup_runtime_bionic",    {0,     0,    {false,  do_setup_runtime_bionic}}},
         {"setrlimit",               {3,     3,    {false,  do_setrlimit}}},
         {"start",                   {1,     1,    {false,  do_start}}},
         {"stop",                    {1,     1,    {false,  do_stop}}},
diff --git a/init/first_stage_mount.cpp b/init/first_stage_mount.cpp
index b5ff658..affa39e 100644
--- a/init/first_stage_mount.cpp
+++ b/init/first_stage_mount.cpp
@@ -34,6 +34,7 @@
 #include <fs_mgr.h>
 #include <fs_mgr_dm_linear.h>
 #include <fs_mgr_overlayfs.h>
+#include <libgsi/libgsi.h>
 #include <liblp/liblp.h>
 
 #include "devices.h"
@@ -79,6 +80,7 @@
     bool IsDmLinearEnabled();
     bool GetDmLinearMetadataDevice();
     bool InitDmLinearBackingDevices(const android::fs_mgr::LpMetadata& metadata);
+    void UseGsiIfPresent();
 
     ListenerAction UeventCallback(const Uevent& uevent);
 
@@ -207,6 +209,8 @@
     }
 
     required_devices_partition_names_.emplace(super_partition_name_);
+    // When booting from live GSI images, userdata is the super device.
+    required_devices_partition_names_.emplace("userdata");
     return true;
 }
 
@@ -410,20 +414,46 @@
 // this case, we mount system first then pivot to it.  From that point on,
 // we are effectively identical to a system-as-root device.
 bool FirstStageMount::TrySwitchSystemAsRoot() {
+    auto metadata_partition = std::find_if(fstab_.begin(), fstab_.end(), [](const auto& entry) {
+        return entry.mount_point == "/metadata";
+    });
+    if (metadata_partition != fstab_.end()) {
+        if (MountPartition(&(*metadata_partition))) {
+            fstab_.erase(metadata_partition);
+            UseGsiIfPresent();
+        }
+    }
+
     auto system_partition = std::find_if(fstab_.begin(), fstab_.end(), [](const auto& entry) {
         return entry.mount_point == "/system";
     });
 
-    if (system_partition != fstab_.end()) {
-        if (!MountPartition(&(*system_partition))) {
-            return false;
+    if (system_partition == fstab_.end()) return true;
+
+    bool mounted = false;
+    bool no_fail = false;
+    for (auto it = system_partition; it != fstab_.end();) {
+        if (it->mount_point != "/system") {
+            break;
         }
-
-        SwitchRoot((*system_partition).mount_point);
-
-        fstab_.erase(system_partition);
+        no_fail |= (it->fs_mgr_flags).no_fail;
+        if (MountPartition(&(*it))) {
+            mounted = true;
+            SwitchRoot("/system");
+            break;
+        }
+        it++;
     }
 
+    if (!mounted && !no_fail) {
+        LOG(ERROR) << "Failed to mount /system";
+        return false;
+    }
+
+    auto it = std::remove_if(fstab_.begin(), fstab_.end(),
+                             [](const auto& entry) { return entry.mount_point == "/system"; });
+    fstab_.erase(it, fstab_.end());
+
     return true;
 }
 
@@ -444,14 +474,12 @@
         if (skip_mount_point.empty()) {
             continue;
         }
-        auto removing_entry =
-                std::find_if(fstab_.begin(), fstab_.end(), [&skip_mount_point](const auto& entry) {
-                    return entry.mount_point == skip_mount_point;
-                });
-        if (removing_entry != fstab_.end()) {
-            fstab_.erase(removing_entry);
-            LOG(INFO) << "Skip mounting partition: " << skip_mount_point;
-        }
+        auto it = std::remove_if(fstab_.begin(), fstab_.end(),
+                                 [&skip_mount_point](const auto& entry) {
+                                     return entry.mount_point == skip_mount_point;
+                                 });
+        fstab_.erase(it, fstab_.end());
+        LOG(INFO) << "Skip mounting partition: " << skip_mount_point;
     }
 
     return true;
@@ -462,8 +490,21 @@
 
     if (!TrySkipMountingPartitions()) return false;
 
-    for (auto& fstab_entry : fstab_) {
-        if (!MountPartition(&fstab_entry) && !fstab_entry.fs_mgr_flags.no_fail) {
+    for (auto it = fstab_.begin(); it != fstab_.end();) {
+        bool mounted = false;
+        bool no_fail = false;
+        auto start_mount_point = it->mount_point;
+        do {
+            no_fail |= (it->fs_mgr_flags).no_fail;
+            if (!mounted)
+                mounted = MountPartition(&(*it));
+            else
+                LOG(INFO) << "Skip already-mounted partition: " << start_mount_point;
+            it++;
+        } while (it != fstab_.end() && it->mount_point == start_mount_point);
+
+        if (!mounted && !no_fail) {
+            LOG(ERROR) << start_mount_point << " mounted unsuccessfully but it is required!";
             return false;
         }
     }
@@ -486,6 +527,40 @@
     return true;
 }
 
+void FirstStageMount::UseGsiIfPresent() {
+    std::string metadata_file, error;
+
+    if (!android::gsi::CanBootIntoGsi(&metadata_file, &error)) {
+        LOG(INFO) << "GSI " << error << ", proceeding with normal boot";
+        return;
+    }
+
+    auto metadata = android::fs_mgr::ReadFromImageFile(metadata_file.c_str());
+    if (!metadata) {
+        LOG(ERROR) << "GSI partition layout could not be read";
+        return;
+    }
+
+    if (!android::fs_mgr::CreateLogicalPartitions(*metadata.get(), "/dev/block/by-name/userdata")) {
+        LOG(ERROR) << "GSI partition layout could not be instantiated";
+        return;
+    }
+
+    if (!android::gsi::MarkSystemAsGsi()) {
+        PLOG(ERROR) << "GSI indicator file could not be written";
+        return;
+    }
+
+    // Replace the existing system fstab entry.
+    auto system_partition = std::find_if(fstab_.begin(), fstab_.end(), [](const auto& entry) {
+        return entry.mount_point == "/system";
+    });
+    if (system_partition != fstab_.end()) {
+        fstab_.erase(system_partition);
+    }
+    fstab_.emplace_back(BuildGsiSystemFstabEntry());
+}
+
 bool FirstStageMountVBootV1::GetDmVerityDevices() {
     std::string verity_loc_device;
     need_dm_verity_ = false;
diff --git a/init/init.cpp b/init/init.cpp
index dc46a82..d360fdd 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -451,6 +451,8 @@
     // Also, binder can't be used by recovery.
 #ifndef RECOVERY
     android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
+    android::ProcessState::self()->setCallRestriction(
+            ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY);
 #endif
     return Success();
 }
diff --git a/init/main.cpp b/init/main.cpp
index 868c409..2ce46ef 100644
--- a/init/main.cpp
+++ b/init/main.cpp
@@ -57,27 +57,22 @@
         return ueventd_main(argc, argv);
     }
 
-    if (argc < 2) {
-        return FirstStageMain(argc, argv);
+    if (argc > 1) {
+        if (!strcmp(argv[1], "subcontext")) {
+            android::base::InitLogging(argv, &android::base::KernelLogger);
+            const BuiltinFunctionMap function_map;
+
+            return SubcontextMain(argc, argv, &function_map);
+        }
+
+        if (!strcmp(argv[1], "selinux_setup")) {
+            return SetupSelinux(argv);
+        }
+
+        if (!strcmp(argv[1], "second_stage")) {
+            return SecondStageMain(argc, argv);
+        }
     }
 
-    if (!strcmp(argv[1], "subcontext")) {
-        android::base::InitLogging(argv, &android::base::KernelLogger);
-        const BuiltinFunctionMap function_map;
-
-        return SubcontextMain(argc, argv, &function_map);
-    }
-
-    if (!strcmp(argv[1], "selinux_setup")) {
-        return SetupSelinux(argv);
-    }
-
-    if (!strcmp(argv[1], "second_stage")) {
-        return SecondStageMain(argc, argv);
-    }
-
-    android::base::InitLogging(argv, &android::base::KernelLogger);
-
-    LOG(ERROR) << "Unknown argument passed to init '" << argv[1] << "'";
-    return 1;
+    return FirstStageMain(argc, argv);
 }
diff --git a/init/property_service.cpp b/init/property_service.cpp
index 5328869..3199d45 100644
--- a/init/property_service.cpp
+++ b/init/property_service.cpp
@@ -699,22 +699,6 @@
     }
 }
 
-void property_load_boot_defaults() {
-    if (!load_properties_from_file("/system/etc/prop.default", NULL)) {
-        // Try recovery path
-        if (!load_properties_from_file("/prop.default", NULL)) {
-            // Try legacy path
-            load_properties_from_file("/default.prop", NULL);
-        }
-    }
-    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);
-
-    update_sys_usb_config();
-}
-
 static void load_override_properties() {
     if (ALLOW_LOCAL_PROP_OVERRIDE) {
         load_properties_from_file("/data/local.prop", NULL);
@@ -779,12 +763,29 @@
     close(fd);
 }
 
-void load_system_props() {
+void property_load_boot_defaults() {
+    // TODO(b/117892318): merge prop.default and build.prop files into one
+    // TODO(b/122864654): read the prop files from all partitions and then
+    // resolve the duplication by their origin so that RO and non-RO properties
+    // have a consistent overriding order.
+    if (!load_properties_from_file("/system/etc/prop.default", NULL)) {
+        // Try recovery path
+        if (!load_properties_from_file("/prop.default", NULL)) {
+            // Try legacy path
+            load_properties_from_file("/default.prop", NULL);
+        }
+    }
+    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);
     load_properties_from_file("/system/build.prop", NULL);
     load_properties_from_file("/odm/build.prop", NULL);
     load_properties_from_file("/vendor/build.prop", NULL);
     load_properties_from_file("/factory/factory.prop", "ro.*");
     load_recovery_id_prop();
+
+    update_sys_usb_config();
 }
 
 static int SelinuxAuditCallback(void* data, security_class_t /*cls*/, char* buf, size_t len) {
diff --git a/init/reboot.cpp b/init/reboot.cpp
index f08d855..c11ea10 100644
--- a/init/reboot.cpp
+++ b/init/reboot.cpp
@@ -106,13 +106,17 @@
         int st;
         if (IsF2Fs()) {
             const char* f2fs_argv[] = {
-                "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(),
+                    "/system/bin/fsck.f2fs",
+                    "-a",
+                    mnt_fsname_.c_str(),
             };
             android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
                                     true, nullptr, nullptr, 0);
         } else if (IsExt4()) {
             const char* ext4_argv[] = {
-                "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(),
+                    "/system/bin/e2fsck",
+                    "-y",
+                    mnt_fsname_.c_str(),
             };
             android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
                                     true, nullptr, nullptr, 0);
diff --git a/init/selinux.cpp b/init/selinux.cpp
index 04ca207..e4da52c 100644
--- a/init/selinux.cpp
+++ b/init/selinux.cpp
@@ -432,24 +432,6 @@
     selinux_android_restorecon("/dev/urandom", 0);
     selinux_android_restorecon("/dev/__properties__", 0);
 
-    selinux_android_restorecon("/plat_file_contexts", 0);
-    selinux_android_restorecon("/nonplat_file_contexts", 0);
-    selinux_android_restorecon("/vendor_file_contexts", 0);
-    selinux_android_restorecon("/plat_property_contexts", 0);
-    selinux_android_restorecon("/nonplat_property_contexts", 0);
-    selinux_android_restorecon("/vendor_property_contexts", 0);
-    selinux_android_restorecon("/plat_seapp_contexts", 0);
-    selinux_android_restorecon("/nonplat_seapp_contexts", 0);
-    selinux_android_restorecon("/vendor_seapp_contexts", 0);
-    selinux_android_restorecon("/plat_service_contexts", 0);
-    selinux_android_restorecon("/nonplat_service_contexts", 0);
-    selinux_android_restorecon("/vendor_service_contexts", 0);
-    selinux_android_restorecon("/plat_hwservice_contexts", 0);
-    selinux_android_restorecon("/nonplat_hwservice_contexts", 0);
-    selinux_android_restorecon("/vendor_hwservice_contexts", 0);
-    selinux_android_restorecon("/sepolicy", 0);
-    selinux_android_restorecon("/vndservice_contexts", 0);
-
     selinux_android_restorecon("/dev/block", SELINUX_ANDROID_RESTORECON_RECURSE);
     selinux_android_restorecon("/dev/device-mapper", 0);
 
diff --git a/init/service.cpp b/init/service.cpp
index 272809f..f3eafe4 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -140,6 +140,43 @@
     return Success();
 }
 
+Result<Success> Service::SetUpPreApexdMounts() const {
+    // If a pre-apexd service is 're' launched after the runtime APEX is
+    // available, unmount the linker and bionic libs which are currently
+    // bind mounted to the files in the runtime APEX. This will reveal
+    // the hidden mount points (targetting the bootstrap ones in the
+    // system partition) which were setup before the runtime APEX was
+    // started. Note that these unmounts are done in a separate mount namespace
+    // for the process. It does not affect other processes including the init.
+    if (pre_apexd_ && ServiceList::GetInstance().IsRuntimeAvailable()) {
+        if (access(kLinkerMountPoint, F_OK) == 0) {
+            if (umount(kLinkerMountPoint) == -1) {
+                return ErrnoError() << "Could not umount " << kLinkerMountPoint;
+            }
+            for (const auto& libname : kBionicLibFileNames) {
+                std::string mount_point = kBionicLibsMountPointDir + libname;
+                if (umount(mount_point.c_str()) == -1) {
+                    return ErrnoError() << "Could not umount " << mount_point;
+                }
+            }
+        }
+
+        if (access(kLinkerMountPoint64, F_OK) == 0) {
+            if (umount(kLinkerMountPoint64) == -1) {
+                return ErrnoError() << "Could not umount " << kLinkerMountPoint64;
+            }
+            for (const auto& libname : kBionicLibFileNames) {
+                std::string mount_point = kBionicLibsMountPointDir64 + libname;
+                std::string source = kBootstrapBionicLibsDir64 + libname;
+                if (umount(mount_point.c_str()) == -1) {
+                    return ErrnoError() << "Could not umount " << mount_point;
+                }
+            }
+        }
+    }
+    return Success();
+}
+
 Result<Success> Service::SetUpPidNamespace() const {
     if (prctl(PR_SET_NAME, name_.c_str()) == -1) {
         return ErrnoError() << "Could not set name";
@@ -369,7 +406,7 @@
 
     // If we crash > 4 times in 4 minutes, reboot into bootloader or set crashing property
     boot_clock::time_point now = boot_clock::now();
-    if (((flags_ & SVC_CRITICAL) || classnames_.count("updatable")) && !(flags_ & SVC_RESTART)) {
+    if (((flags_ & SVC_CRITICAL) || !pre_apexd_) && !(flags_ & SVC_RESTART)) {
         if (now < time_crashed_ + 4min) {
             if (++crash_count_ > 4) {
                 if (flags_ & SVC_CRITICAL) {
@@ -929,6 +966,14 @@
         scon = *result;
     }
 
+    if (!ServiceList::GetInstance().IsRuntimeAvailable() && !pre_apexd_) {
+        // If this service is started before the runtime APEX gets available,
+        // mark it as pre-apexd one. Note that this marking is permanent. So
+        // for example, if the service is re-launched (e.g., due to crash),
+        // it is still recognized as pre-apexd... for consistency.
+        pre_apexd_ = true;
+    }
+
     LOG(INFO) << "starting service '" << name_ << "'...";
 
     pid_t pid = -1;
@@ -945,6 +990,37 @@
             LOG(FATAL) << "Service '" << name_ << "' could not enter namespaces: " << result.error();
         }
 
+        // b/122559956: mount namespace is not cloned for the devices that don't support
+        // the update of bionic libraries via APEX. In that case, because the bionic
+        // libraries in the runtime APEX and the bootstrap bionic libraries are
+        // identical, it doesn't matter which libs are used. This is also to avoid the
+        // bug in sdcardfs which is triggered when we have multiple mount namespaces
+        // across vold and the others. BIONIC_UPDATABLE shall be true only for the
+        // devices where kernel has the fix for the sdcardfs bug (see the commit message
+        // for the fix).
+        static bool bionic_updatable =
+                android::base::GetBoolProperty("ro.apex.bionic_updatable", false);
+
+        if (bionic_updatable && pre_apexd_) {
+            // pre-apexd process gets a private copy of the mount namespace.
+            // However, this does not mean that mount/unmount events are not
+            // shared across pre-apexd processes and post-apexd processes.
+            // *Most* of the events are still shared because the propagation
+            // type of / is set to 'shared'. (see `mount rootfs rootfs /shared
+            // rec` in init.rc)
+            //
+            // This unsharing is required to not propagate the mount events
+            // under /system/lib/{libc|libdl|libm}.so and /system/bin/linker(64)
+            // whose propagation type is set to private. With this,
+            // bind-mounting the bionic libs and the dynamic linker from the
+            // runtime APEX to the mount points does not affect pre-apexd
+            // processes which should use the bootstrap ones.
+            if (unshare(CLONE_NEWNS) != 0) {
+                LOG(FATAL) << "Creating a new mount namespace for service"
+                           << " '" << name_ << "' failed: " << strerror(errno);
+            }
+        }
+
         if (namespace_flags_ & CLONE_NEWNS) {
             if (auto result = SetUpMountNamespace(); !result) {
                 LOG(FATAL) << "Service '" << name_
@@ -952,6 +1028,14 @@
             }
         }
 
+        // b/122559956: same as above
+        if (bionic_updatable && pre_apexd_ && ServiceList::GetInstance().IsRuntimeAvailable()) {
+            if (auto result = SetUpPreApexdMounts(); !result) {
+                LOG(FATAL) << "Pre-apexd service '" << name_
+                           << "' could not setup the mount points: " << result.error();
+            }
+        }
+
         if (namespace_flags_ & CLONE_NEWPID) {
             // This will fork again to run an init process inside the PID
             // namespace.
@@ -1324,6 +1408,10 @@
     delayed_service_names_.clear();
 }
 
+void ServiceList::MarkRuntimeAvailable() {
+    runtime_available_ = true;
+}
+
 void ServiceList::DelayService(const Service& service) {
     if (services_update_finished_) {
         LOG(ERROR) << "Cannot delay the start of service '" << service.name()
diff --git a/init/service.h b/init/service.h
index 56e75b0..676111f 100644
--- a/init/service.h
+++ b/init/service.h
@@ -62,6 +62,24 @@
 namespace android {
 namespace init {
 
+static constexpr const char* kLinkerMountPoint = "/system/bin/linker";
+static constexpr const char* kBootstrapLinkerPath = "/system/bin/linker";
+static constexpr const char* kRuntimeLinkerPath = "/apex/com.android.runtime/bin/linker";
+
+static constexpr const char* kBionicLibsMountPointDir = "/system/lib/";
+static constexpr const char* kBootstrapBionicLibsDir = "/system/lib/";
+static constexpr const char* kRuntimeBionicLibsDir = "/apex/com.android.runtime/lib/bionic/";
+
+static constexpr const char* kLinkerMountPoint64 = "/system/bin/linker64";
+static constexpr const char* kBootstrapLinkerPath64 = "/system/bin/linker64";
+static constexpr const char* kRuntimeLinkerPath64 = "/apex/com.android.runtime/bin/linker64";
+
+static constexpr const char* kBionicLibsMountPointDir64 = "/system/lib64/";
+static constexpr const char* kBootstrapBionicLibsDir64 = "/system/lib64/";
+static constexpr const char* kRuntimeBionicLibsDir64 = "/apex/com.android.runtime/lib64/bionic/";
+
+static const std::vector<std::string> kBionicLibFileNames = {"libc.so", "libm.so", "libdl.so"};
+
 class Service {
   public:
     Service(const std::string& name, Subcontext* subcontext_for_restart_commands,
@@ -124,6 +142,7 @@
     std::optional<std::chrono::seconds> timeout_period() const { return timeout_period_; }
     const std::vector<std::string>& args() const { return args_; }
     bool is_updatable() const { return updatable_; }
+    bool is_pre_apexd() const { return pre_apexd_; }
 
   private:
     using OptionParser = Result<Success> (Service::*)(std::vector<std::string>&& args);
@@ -132,6 +151,7 @@
     Result<Success> SetUpMountNamespace() const;
     Result<Success> SetUpPidNamespace() const;
     Result<Success> EnterNamespaces() const;
+    Result<Success> SetUpPreApexdMounts() const;
     void NotifyStateChange(const std::string& new_state) const;
     void StopOrReset(int how);
     void ZapStdio() const;
@@ -242,6 +262,8 @@
     std::vector<std::string> args_;
 
     std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_;
+
+    bool pre_apexd_ = false;
 };
 
 class ServiceList {
@@ -284,13 +306,16 @@
     const std::vector<Service*> services_in_shutdown_order() const;
 
     void MarkServicesUpdate();
+    void MarkRuntimeAvailable();
     bool IsServicesUpdated() const { return services_update_finished_; }
+    bool IsRuntimeAvailable() const { return runtime_available_; }
     void DelayService(const Service& service);
 
   private:
     std::vector<std::unique_ptr<Service>> services_;
 
     bool services_update_finished_ = false;
+    bool runtime_available_ = false;
     std::vector<std::string> delayed_service_names_;
 };
 
diff --git a/janitors/OWNERS b/janitors/OWNERS
new file mode 100644
index 0000000..0610b41
--- /dev/null
+++ b/janitors/OWNERS
@@ -0,0 +1,4 @@
+# OWNERS file for projects that don't really have owners so much as volunteer janitors.
+enh@google.com
+hhb@google.com
+narayan@google.com
diff --git a/libcutils/include/cutils/trace.h b/libcutils/include/cutils/trace.h
index 58b9f09..79b4b35 100644
--- a/libcutils/include/cutils/trace.h
+++ b/libcutils/include/cutils/trace.h
@@ -75,7 +75,8 @@
 #define ATRACE_TAG_VIBRATOR         (1<<23)
 #define ATRACE_TAG_AIDL             (1<<24)
 #define ATRACE_TAG_NNAPI            (1<<25)
-#define ATRACE_TAG_LAST             ATRACE_TAG_NNAPI
+#define ATRACE_TAG_RRO              (1<<26)
+#define ATRACE_TAG_LAST             ATRACE_TAG_RRO
 
 // 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 0723612..03edfb5 100644
--- a/libcutils/include/private/android_filesystem_config.h
+++ b/libcutils/include/private/android_filesystem_config.h
@@ -190,7 +190,8 @@
  */
 #define AID_OVERFLOWUID 65534 /* unmapped user in the user namespace */
 
-#define AID_ISOLATED_START 99000 /* start of uids for fully isolated sandboxed processes */
+/* use the ranges below to determine whether a process is isolated */
+#define AID_ISOLATED_START 90000 /* start of uids for fully isolated sandboxed processes */
 #define AID_ISOLATED_END 99999   /* end of uids for fully isolated sandboxed processes */
 
 #define AID_USER 100000        /* TODO: switch users over to AID_USER_OFFSET */
diff --git a/liblog/.clang-format b/liblog/.clang-format
deleted file mode 100644
index 9db87a8..0000000
--- a/liblog/.clang-format
+++ /dev/null
@@ -1,9 +0,0 @@
-BasedOnStyle: Google
-AllowShortFunctionsOnASingleLine: false
-
-CommentPragmas: NOLINT:.*
-DerivePointerAlignment: false
-PointerAlignment: Left
-PenaltyExcessCharacter: 32
-
-Cpp11BracedListStyle: false
diff --git a/liblog/.clang-format b/liblog/.clang-format
new file mode 120000
index 0000000..fd0645f
--- /dev/null
+++ b/liblog/.clang-format
@@ -0,0 +1 @@
+../.clang-format-2
\ No newline at end of file
diff --git a/liblog/Android.bp b/liblog/Android.bp
index 4a165a0..4fd36f5 100644
--- a/liblog/Android.bp
+++ b/liblog/Android.bp
@@ -15,31 +15,29 @@
 //
 
 liblog_sources = [
-    "config_read.c",
-    "config_write.c",
-    "local_logger.c",
-    "log_event_list.c",
-    "log_event_write.c",
-    "log_ratelimit.cpp",
-    "logger_lock.c",
-    "logger_name.c",
-    "logger_read.c",
-    "logger_write.c",
-    "logprint.c",
-    "stderr_write.c",
+    "config_read.cpp",
+    "config_write.cpp",
+    "log_event_list.cpp",
+    "log_event_write.cpp",
+    "logger_lock.cpp",
+    "logger_name.cpp",
+    "logger_read.cpp",
+    "logger_write.cpp",
+    "logprint.cpp",
+    "stderr_write.cpp",
 ]
 liblog_host_sources = [
-    "fake_log_device.c",
-    "fake_writer.c",
+    "fake_log_device.cpp",
+    "fake_writer.cpp",
 ]
 liblog_target_sources = [
     "event_tag_map.cpp",
     "log_time.cpp",
-    "properties.c",
-    "pmsg_reader.c",
-    "pmsg_writer.c",
-    "logd_reader.c",
-    "logd_writer.c",
+    "properties.cpp",
+    "pmsg_reader.cpp",
+    "pmsg_writer.cpp",
+    "logd_reader.cpp",
+    "logd_writer.cpp",
 ]
 
 cc_library_headers {
@@ -87,7 +85,6 @@
             ldflags: ["-Wl,--hash-style=both"],
         },
         windows: {
-            srcs: ["uio.c"],
             enabled: true,
         },
         not_windows: {
diff --git a/liblog/README b/liblog/README
deleted file mode 100644
index 5a845be..0000000
--- a/liblog/README
+++ /dev/null
@@ -1,209 +0,0 @@
-LIBLOG(3)          Android Internal NDK Programming Manual           LIBLOG(3)
-
-
-
-NAME
-       liblog - Android Internal NDK logger interfaces
-
-SYNOPSIS
-       /*
-        * Please limit to 24 characters for runtime is loggable,
-        * 16 characters for persist is loggable, and logcat pretty
-        * alignment with limit of 7 characters.
-        */
-       #define LOG_TAG "yourtag"
-       #include <log/log.h>
-
-       ALOG(android_priority, tag, format, ...)
-       IF_ALOG(android_priority, tag)
-       LOG_PRI(priority, tag, format, ...)
-       LOG_PRI_VA(priority, tag, format, args)
-       #define LOG_TAG NULL
-       ALOGV(format, ...)
-       SLOGV(format, ...)
-       RLOGV(format, ...)
-       ALOGV_IF(cond, format, ...)
-       SLOGV_IF(cond, format, ...)
-       RLOGV_IF(cond, format, ...)
-       IF_ALOGC()
-       ALOGD(format, ...)
-       SLOGD(format, ...)
-       RLOGD(format, ...)
-       ALOGD_IF(cond, format, ...)
-       SLOGD_IF(cond, format, ...)
-       RLOGD_IF(cond, format, ...)
-       IF_ALOGD()
-       ALOGI(format, ...)
-       SLOGI(format, ...)
-       RLOGI(format, ...)
-       ALOGI_IF(cond, format, ...)
-       SLOGI_IF(cond, format, ...)
-       RLOGI_IF(cond, format, ...)
-       IF_ALOGI()
-       ALOGW(format, ...)
-       SLOGW(format, ...)
-       RLOGW(format, ...)
-       ALOGW_IF(cond, format, ...)
-       SLOGW_IF(cond, format, ...)
-       RLOGW_IF(cond, format, ...)
-       IF_ALOGW()
-       ALOGE(format, ...)
-       SLOGE(format, ...)
-       RLOGE(format, ...)
-       ALOGE_IF(cond, format, ...)
-       SLOGE_IF(cond, format, ...)
-       RLOGE_IF(cond, format, ...)
-       IF_ALOGE()
-       LOG_FATAL(format, ...)
-       LOG_ALWAYS_FATAL(format, ...)
-       LOG_FATAL_IF(cond, format, ...)
-       LOG_ALWAYS_FATAL_IF(cond, format, ...)
-       ALOG_ASSERT(cond, format, ...)
-       LOG_EVENT_INT(tag, value)
-       LOG_EVENT_LONG(tag, value)
-
-       clockid_t android_log_clockid()
-
-       log_id_t android_logger_get_id(struct logger *logger)
-       int android_logger_clear(struct logger *logger)
-       int android_logger_get_log_size(struct logger *logger)
-       int android_logger_get_log_readable_size(struct logger *logger)
-       int android_logger_get_log_version(struct logger *logger)
-
-       struct logger_list *android_logger_list_alloc(int mode,
-                                                     unsigned int tail,
-                                                     pid_t pid)
-       struct logger *android_logger_open(struct logger_list *logger_list,
-                                          log_id_t id)
-       struct logger_list *android_logger_list_open(log_id_t id, int mode,
-                                                    unsigned int tail,
-                                                    pid_t pid)
-       int android_logger_list_read(struct logger_list *logger_list,
-                                    struct log_msg *log_msg)
-       void android_logger_list_free(struct logger_list *logger_list)
-
-       log_id_t android_name_to_log_id(const char *logName)
-       const char *android_log_id_to_name(log_id_t log_id)
-
-       android_log_context create_android_logger(uint32_t tag)
-
-       int android_log_write_list_begin(android_log_context ctx)
-       int android_log_write_list_end(android_log_context ctx)
-
-       int android_log_write_int32(android_log_context ctx, int32_t value)
-       int android_log_write_int64(android_log_context ctx, int64_t value)
-       int android_log_write_string8(android_log_context ctx,
-                                     const char *value)
-       int android_log_write_string8_len(android_log_context ctx,
-                                         const char *value, size_t maxlen)
-       int android_log_write_float32(android_log_context ctx, float value)
-
-       int android_log_write_list(android_log_context ctx,
-                                  log_id_t id = LOG_ID_EVENTS)
-
-       android_log_context create_android_log_parser(const char *msg,
-                                                     size_t len)
-       android_log_list_element android_log_read_next(android_log_context ctx)
-       android_log_list_element android_log_peek_next(android_log_context ctx)
-
-       int android_log_destroy(android_log_context *ctx)
-
-       #include <log/log_transport.h>
-
-       int android_set_log_transport(int transport_flag)
-       int android_get_log_transport()
-
-       Link with -llog
-
-DESCRIPTION
-       liblog  represents  an interface to the volatile Android Logging system
-       for NDK (Native) applications  and  libraries.  Interfaces  for  either
-       writing  or reading logs.  The log buffers are divided up in Main, Sys‐
-       tem, Radio and Events sub-logs.
-
-       The logging interfaces are a series of macros,  all  of  which  can  be
-       overridden individually in order to control the verbosity of the appli‐
-       cation or library.  [ASR]LOG[VDIWE] calls are used  to  log  to  BAsic,
-       System or Radio sub-logs in either the Verbose, Debug, Info, Warning or
-       Error priorities.  [ASR]LOG[VDIWE]_IF calls are used  to  perform  thus
-       based  on a condition being true.  IF_ALOG[VDIWE] calls are true if the
-       current LOG_TAG is enabled at the specified priority.  LOG_ALWAYS_FATAL
-       is  used to ALOG a message, then kill the process.  LOG_FATAL call is a
-       variant of LOG_ALWAYS_FATAL,  only  enabled  in  engineering,  and  not
-       release builds.  ALOG_ASSERT is used to ALOG a message if the condition
-       is  false;   the   condition   is   part   of   the   logged   message.
-       LOG_EVENT_(INT|LONG)  is  used  to  drop binary content into the Events
-       sub-log.
-
-       The log reading interfaces permit opening the  logs  either  singly  or
-       multiply,  retrieving  a  log  entry  at  a  time in time sorted order,
-       optionally limited to a specific pid and tail of the log(s) and finally
-       a  call closing the logs.  A single log can be opened with android_log‐
-       ger_list_open;  or  multiple  logs  can  be  opened  with  android_log‐
-       ger_list_alloc,  calling  in  turn the android_logger_open for each log
-       id.  Each entry can be retrieved  with  android_logger_list_read.   The
-       log(s) can be closed with android_logger_list_free.  The logs should be
-       opened  with an  ANDROID_LOG_RDONLY  mode.   ANDROID_LOG_NONBLOCK  mode
-       will report when the  log reading is done with an  EAGAIN  error return
-       code,  otherwise the  android_logger_list_read  call will block for new
-       entries.
-
-       The  ANDROID_LOG_WRAP  mode flag to the  android_logger_list_alloc_time
-       signals  logd to quiesce  the reader until the buffer is about to prune
-       at the start time then proceed to dumping content.
-
-       The  ANDROID_LOG_PSTORE mode flag to the android_logger_open is used to
-       switch from the active logs to the persistent logs from before the last
-       reboot.
-
-       The value returned by android_logger_open can be used as a parameter to
-       the  android_logger_clear  function to empty the sub-log.  It is recom‐
-       mended to only open log ANDROID_LOG_WRONLY in that case.
-
-       The value returned by android_logger_open can be used as a parameter to
-       the android_logger_get_log_(size|readable_size|version) to retrieve the
-       sub-log maximum size, readable size and log buffer format protocol ver‐
-       sion  respectively.  android_logger_get_id returns the id that was used
-       when  opening  the  sub-log.    It  is  recommended  to  open  the  log
-       ANDROID_LOG_RDONLY in these cases.
-
-       android_set_log_transport()  selects  transport  filters.  Argument  is
-       either LOGGER_DEFAULT, LOGGER_LOGD, LOGGER_NULL or LOGGER_LOCAL. Log to
-       logger daemon for default or logd, drop contents on floor,  or log into
-       local   memory   respectively.       Both   android_set_log_transport()
-       and android_get_log_transport() return the current  transport mask,  or
-       a negative errno for any problems.
-
-ERRORS
-       If messages fail, a negative error code will be returned to the caller.
-
-       The -ENOTCONN return code indicates that the logger daemon is stopped.
-
-       The  -EBADF return code indicates that the log access point can not be
-       opened, or the log buffer id is out of range.
-
-       For the  -EAGAIN  return code,  this means that the logging message was
-       temporarily backed-up either because of Denial Of Service (DOS) logging
-       pressure from some chatty application or service in the Android system,
-       or if too small of a value is set in /proc/sys/net/unix/max_dgram_qlen.
-       To aid in diagnosing the occurence of this,  a binary event from liblog
-       will be sent to the  log  daemon  once a  new  message  can get through
-       indicating how many  messages were  dropped  as a result.   Please take
-       action to resolve the structural problems at the source.
-
-       It is generally not advised for the caller to retry the  -EAGAIN return
-       code as  this  will  only  make the  problem(s)  worse  and  cause your
-       application to temporarily drop to the  logger daemon  priority,  BATCH
-       scheduling policy and background task cgroup. If you require a group of
-       messages to be passed atomically,  merge  them  into  one  message with
-       embedded newlines to the maximum length LOGGER_ENTRY_MAX_PAYLOAD.
-
-       Other return codes  from  writing operation can be returned.  Since the
-       library retries on EINTR, -EINTR should never be returned.
-
-SEE ALSO
-       syslogd(8), klogd, auditd(8)
-
-
-
-                                  08 Feb 2017                        LIBLOG(3)
diff --git a/liblog/README.md b/liblog/README.md
new file mode 100644
index 0000000..98bee9f
--- /dev/null
+++ b/liblog/README.md
@@ -0,0 +1,176 @@
+Android liblog
+--------------
+
+Public Functions and Macros
+---------------------------
+
+    /*
+     * Please limit to 24 characters for runtime is loggable,
+     * 16 characters for persist is loggable, and logcat pretty
+     * alignment with limit of 7 characters.
+    */
+    #define LOG_TAG "yourtag"
+    #include <log/log.h>
+
+    ALOG(android_priority, tag, format, ...)
+    IF_ALOG(android_priority, tag)
+    LOG_PRI(priority, tag, format, ...)
+    LOG_PRI_VA(priority, tag, format, args)
+    #define LOG_TAG NULL
+    ALOGV(format, ...)
+    SLOGV(format, ...)
+    RLOGV(format, ...)
+    ALOGV_IF(cond, format, ...)
+    SLOGV_IF(cond, format, ...)
+    RLOGV_IF(cond, format, ...)
+    IF_ALOGC()
+    ALOGD(format, ...)
+    SLOGD(format, ...)
+    RLOGD(format, ...)
+    ALOGD_IF(cond, format, ...)
+    SLOGD_IF(cond, format, ...)
+    RLOGD_IF(cond, format, ...)
+    IF_ALOGD()
+    ALOGI(format, ...)
+    SLOGI(format, ...)
+    RLOGI(format, ...)
+    ALOGI_IF(cond, format, ...)
+    SLOGI_IF(cond, format, ...)
+    RLOGI_IF(cond, format, ...)
+    IF_ALOGI()
+    ALOGW(format, ...)
+    SLOGW(format, ...)
+    RLOGW(format, ...)
+    ALOGW_IF(cond, format, ...)
+    SLOGW_IF(cond, format, ...)
+    RLOGW_IF(cond, format, ...)
+    IF_ALOGW()
+    ALOGE(format, ...)
+    SLOGE(format, ...)
+    RLOGE(format, ...)
+    ALOGE_IF(cond, format, ...)
+    SLOGE_IF(cond, format, ...)
+    RLOGE_IF(cond, format, ...)
+    IF_ALOGE()
+    LOG_FATAL(format, ...)
+    LOG_ALWAYS_FATAL(format, ...)
+    LOG_FATAL_IF(cond, format, ...)
+    LOG_ALWAYS_FATAL_IF(cond, format, ...)
+    ALOG_ASSERT(cond, format, ...)
+    LOG_EVENT_INT(tag, value)
+    LOG_EVENT_LONG(tag, value)
+
+    clockid_t android_log_clockid()
+
+    log_id_t android_logger_get_id(struct logger *logger)
+    int android_logger_clear(struct logger *logger)
+    int android_logger_get_log_size(struct logger *logger)
+    int android_logger_get_log_readable_size(struct logger *logger)
+    int android_logger_get_log_version(struct logger *logger)
+
+    struct logger_list *android_logger_list_alloc(int mode, unsigned int tail, pid_t pid)
+    struct logger *android_logger_open(struct logger_list *logger_list, log_id_t id)
+    struct logger_list *android_logger_list_open(log_id_t id, int mode, unsigned int tail, pid_t pid)
+    int android_logger_list_read(struct logger_list *logger_list, struct log_msg *log_msg)
+    void android_logger_list_free(struct logger_list *logger_list)
+
+    log_id_t android_name_to_log_id(const char *logName)
+    const char *android_log_id_to_name(log_id_t log_id)
+
+    android_log_context create_android_logger(uint32_t tag)
+
+    int android_log_write_list_begin(android_log_context ctx)
+    int android_log_write_list_end(android_log_context ctx)
+
+    int android_log_write_int32(android_log_context ctx, int32_t value)
+    int android_log_write_int64(android_log_context ctx, int64_t value)
+    int android_log_write_string8(android_log_context ctx, const char *value)
+    int android_log_write_string8_len(android_log_context ctx, const char *value, size_t maxlen)
+    int android_log_write_float32(android_log_context ctx, float value)
+
+    int android_log_write_list(android_log_context ctx, log_id_t id = LOG_ID_EVENTS)
+
+    android_log_context create_android_log_parser(const char *msg, size_t len)
+    android_log_list_element android_log_read_next(android_log_context ctx)
+    android_log_list_element android_log_peek_next(android_log_context ctx)
+
+    int android_log_destroy(android_log_context *ctx)
+
+    #include <log/log_transport.h>
+
+    int android_set_log_transport(int transport_flag)
+    int android_get_log_transport()
+
+Description
+-----------
+
+liblog represents an interface to the volatile Android Logging system for NDK (Native) applications
+and libraries.  Interfaces for either writing or reading logs.  The log buffers are divided up in
+Main, System, Radio and Events sub-logs.
+
+The logging interfaces are a series of macros, all of which can be overridden individually in order
+to control the verbosity of the application or library.  `[ASR]LOG[VDIWE]` calls are used to log to
+BAsic, System or Radio sub-logs in either the Verbose, Debug, Info, Warning or Error priorities.
+`[ASR]LOG[VDIWE]_IF` calls are used to perform thus based on a condition being true.
+`IF_ALOG[VDIWE]` calls are true if the current `LOG_TAG` is enabled at the specified priority.
+`LOG_ALWAYS_FATAL` is used to `ALOG` a message, then kill the process.  `LOG_FATAL` call is a
+variant of `LOG_ALWAYS_FATAL`, only enabled in engineering, and not release builds.  `ALOG_ASSERT`
+is used to `ALOG` a message if the condition is false; the condition is part of the logged message.
+`LOG_EVENT_(INT|LONG)` is used to drop binary content into the Events sub-log.
+
+The log reading interfaces permit opening the logs either singly or multiply, retrieving a log entry
+at a time in time sorted order, optionally limited to a specific pid and tail of the log(s) and
+finally a call closing the logs.  A single log can be opened with `android_logger_list_open()`; or
+multiple logs can be opened with `android_logger_list_alloc()`, calling in turn the
+`android_logger_open()` for each log id.  Each entry can be retrieved with
+`android_logger_list_read()`.  The log(s) can be closed with `android_logger_list_free()`.  The logs
+should be opened with an `ANDROID_LOG_RDONLY` mode.  `ANDROID_LOG_NONBLOCK` mode will report when
+the log reading is done with an `EAGAIN` error return code, otherwise the
+`android_logger_list_read()` call will block for new entries.
+
+The `ANDROID_LOG_WRAP` mode flag to the `android_logger_list_alloc_time()` signals logd to quiesce
+the reader until the buffer is about to prune at the start time then proceed to dumping content.
+
+The `ANDROID_LOG_PSTORE` mode flag to the `android_logger_open()` is used to switch from the active
+logs to the persistent logs from before the last reboot.
+
+The value returned by `android_logger_open()` can be used as a parameter to the
+`android_logger_clear()` function to empty the sub-log.  It is recommended to only open log
+`ANDROID_LOG_WRONLY` in that case.
+
+The value returned by `android_logger_open()` can be used as a parameter to the
+`android_logger_get_log_(size|readable_size|version)` to retrieve the sub-log maximum size, readable
+size and log buffer format protocol version respectively.  `android_logger_get_id()` returns the id
+that was used when opening the sub-log.  It is recommended to open the log `ANDROID_LOG_RDONLY` in
+these cases.
+
+`android_set_log_transport()` selects transport filters.  Argument is either `LOGGER_DEFAULT`,
+`LOGGER_LOGD`, or `LOGGER_NULL`. Log to logger daemon for default or logd, or drop contents on floor
+respectively.  `Both android_set_log_transport()` and `android_get_log_transport()` return the
+current transport mask, or a negative errno for any problems.
+
+Errors
+------
+
+If messages fail, a negative error code will be returned to the caller.
+
+The `-ENOTCONN` return code indicates that the logger daemon is stopped.
+
+The `-EBADF` return code indicates that the log access point can not be opened, or the log buffer id
+is out of range.
+
+For the `-EAGAIN` return code, this means that the logging message was temporarily backed-up either
+because of Denial Of Service (DOS) logging pressure from some chatty application or service in the
+Android system, or if too small of a value is set in /proc/sys/net/unix/max_dgram_qlen.  To aid in
+diagnosing the occurence of this, a binary event from liblog will be sent to the log daemon once a
+new message can get through indicating how many messages were dropped as a result.  Please take
+action to resolve the structural problems at the source.
+
+It is generally not advised for the caller to retry the `-EAGAIN` return code as this will only make
+the problem(s) worse and cause your application to temporarily drop to the logger daemon priority,
+BATCH scheduling policy and background task cgroup. If you require a group of messages to be passed
+atomically, merge them into one message with embedded newlines to the maximum length
+`LOGGER_ENTRY_MAX_PAYLOAD`.
+
+Other return codes from writing operation can be returned.  Since the library retries on `EINTR`,
+`-EINTR` should never be returned.
diff --git a/liblog/config_read.c b/liblog/config_read.cpp
similarity index 68%
rename from liblog/config_read.c
rename to liblog/config_read.cpp
index ca80c80..80177a4 100644
--- a/liblog/config_read.c
+++ b/liblog/config_read.cpp
@@ -19,23 +19,21 @@
 #include "config_read.h"
 #include "logger.h"
 
-LIBLOG_HIDDEN struct listnode __android_log_transport_read = {
-  &__android_log_transport_read, &__android_log_transport_read
-};
-LIBLOG_HIDDEN struct listnode __android_log_persist_read = {
-  &__android_log_persist_read, &__android_log_persist_read
-};
+LIBLOG_HIDDEN struct listnode __android_log_transport_read = {&__android_log_transport_read,
+                                                              &__android_log_transport_read};
+LIBLOG_HIDDEN struct listnode __android_log_persist_read = {&__android_log_persist_read,
+                                                            &__android_log_persist_read};
 
-static void __android_log_add_transport(
-    struct listnode* list, struct android_log_transport_read* transport) {
-  size_t i;
+static void __android_log_add_transport(struct listnode* list,
+                                        struct android_log_transport_read* transport) {
+  uint32_t i;
 
   /* Try to keep one functioning transport for each log buffer id */
   for (i = LOG_ID_MIN; i < LOG_ID_MAX; i++) {
     struct android_log_transport_read* transp;
 
     if (list_empty(list)) {
-      if (!transport->available || ((*transport->available)(i) >= 0)) {
+      if (!transport->available || ((*transport->available)(static_cast<log_id_t>(i)) >= 0)) {
         list_add_tail(list, &transport->node);
         return;
       }
@@ -44,8 +42,8 @@
         if (!transp->available) {
           return;
         }
-        if (((*transp->available)(i) < 0) &&
-            (!transport->available || ((*transport->available)(i) >= 0))) {
+        if (((*transp->available)(static_cast<log_id_t>(i)) < 0) &&
+            (!transport->available || ((*transport->available)(static_cast<log_id_t>(i)) >= 0))) {
           list_add_tail(list, &transport->node);
           return;
         }
@@ -55,15 +53,8 @@
 }
 
 LIBLOG_HIDDEN void __android_log_config_read() {
-  if (__android_log_transport & LOGGER_LOCAL) {
-    extern struct android_log_transport_read localLoggerRead;
-
-    __android_log_add_transport(&__android_log_transport_read, &localLoggerRead);
-  }
-
 #if (FAKE_LOG_DEVICE == 0)
-  if ((__android_log_transport == LOGGER_DEFAULT) ||
-      (__android_log_transport & LOGGER_LOGD)) {
+  if ((__android_log_transport == LOGGER_DEFAULT) || (__android_log_transport & LOGGER_LOGD)) {
     extern struct android_log_transport_read logdLoggerRead;
     extern struct android_log_transport_read pmsgLoggerRead;
 
diff --git a/liblog/config_read.h b/liblog/config_read.h
index 7b29fa4..00ea453 100644
--- a/liblog/config_read.h
+++ b/liblog/config_read.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LIBLOG_CONFIG_READ_H__
-#define _LIBLOG_CONFIG_READ_H__
+#pragma once
 
 #include <cutils/list.h>
 
@@ -51,5 +50,3 @@
 LIBLOG_HIDDEN void __android_log_config_read_close();
 
 __END_DECLS
-
-#endif /* _LIBLOG_CONFIG_READ_H__ */
diff --git a/liblog/config_write.c b/liblog/config_write.cpp
similarity index 66%
rename from liblog/config_write.c
rename to liblog/config_write.cpp
index 0a8b52f..e65c238 100644
--- a/liblog/config_write.c
+++ b/liblog/config_write.cpp
@@ -19,23 +19,21 @@
 #include "config_write.h"
 #include "logger.h"
 
-LIBLOG_HIDDEN struct listnode __android_log_transport_write = {
-  &__android_log_transport_write, &__android_log_transport_write
-};
-LIBLOG_HIDDEN struct listnode __android_log_persist_write = {
-  &__android_log_persist_write, &__android_log_persist_write
-};
+LIBLOG_HIDDEN struct listnode __android_log_transport_write = {&__android_log_transport_write,
+                                                               &__android_log_transport_write};
+LIBLOG_HIDDEN struct listnode __android_log_persist_write = {&__android_log_persist_write,
+                                                             &__android_log_persist_write};
 
-static void __android_log_add_transport(
-    struct listnode* list, struct android_log_transport_write* transport) {
-  size_t i;
+static void __android_log_add_transport(struct listnode* list,
+                                        struct android_log_transport_write* transport) {
+  uint32_t i;
 
   /* Try to keep one functioning transport for each log buffer id */
   for (i = LOG_ID_MIN; i < LOG_ID_MAX; i++) {
     struct android_log_transport_write* transp;
 
     if (list_empty(list)) {
-      if (!transport->available || ((*transport->available)(i) >= 0)) {
+      if (!transport->available || ((*transport->available)(static_cast<log_id_t>(i)) >= 0)) {
         list_add_tail(list, &transport->node);
         return;
       }
@@ -44,8 +42,8 @@
         if (!transp->available) {
           return;
         }
-        if (((*transp->available)(i) < 0) &&
-            (!transport->available || ((*transport->available)(i) >= 0))) {
+        if (((*transp->available)(static_cast<log_id_t>(i)) < 0) &&
+            (!transport->available || ((*transport->available)(static_cast<log_id_t>(i)) >= 0))) {
           list_add_tail(list, &transport->node);
           return;
         }
@@ -55,27 +53,17 @@
 }
 
 LIBLOG_HIDDEN void __android_log_config_write() {
-  if (__android_log_transport & LOGGER_LOCAL) {
-    extern struct android_log_transport_write localLoggerWrite;
-
-    __android_log_add_transport(&__android_log_transport_write,
-                                &localLoggerWrite);
-  }
-
-  if ((__android_log_transport == LOGGER_DEFAULT) ||
-      (__android_log_transport & LOGGER_LOGD)) {
+  if ((__android_log_transport == LOGGER_DEFAULT) || (__android_log_transport & LOGGER_LOGD)) {
 #if (FAKE_LOG_DEVICE == 0)
     extern struct android_log_transport_write logdLoggerWrite;
     extern struct android_log_transport_write pmsgLoggerWrite;
 
-    __android_log_add_transport(&__android_log_transport_write,
-                                &logdLoggerWrite);
+    __android_log_add_transport(&__android_log_transport_write, &logdLoggerWrite);
     __android_log_add_transport(&__android_log_persist_write, &pmsgLoggerWrite);
 #else
     extern struct android_log_transport_write fakeLoggerWrite;
 
-    __android_log_add_transport(&__android_log_transport_write,
-                                &fakeLoggerWrite);
+    __android_log_add_transport(&__android_log_transport_write, &fakeLoggerWrite);
 #endif
   }
 
@@ -88,8 +76,7 @@
      * Remember we can be called here if we are already initialized.
      */
     if (list_empty(&__android_log_transport_write)) {
-      __android_log_add_transport(&__android_log_transport_write,
-                                  &stderrLoggerWrite);
+      __android_log_add_transport(&__android_log_transport_write, &stderrLoggerWrite);
     } else {
       struct android_log_transport_write* transp;
       write_transport_for_each(transp, &__android_log_transport_write) {
@@ -97,8 +84,7 @@
           return;
         }
       }
-      __android_log_add_transport(&__android_log_persist_write,
-                                  &stderrLoggerWrite);
+      __android_log_add_transport(&__android_log_persist_write, &stderrLoggerWrite);
     }
   }
 }
diff --git a/liblog/config_write.h b/liblog/config_write.h
index db1a083..e3be445 100644
--- a/liblog/config_write.h
+++ b/liblog/config_write.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LIBLOG_CONFIG_WRITE_H__
-#define _LIBLOG_CONFIG_WRITE_H__
+#pragma once
 
 #include <cutils/list.h>
 
@@ -51,5 +50,3 @@
 LIBLOG_HIDDEN void __android_log_config_write_close();
 
 __END_DECLS
-
-#endif /* _LIBLOG_CONFIG_WRITE_H__ */
diff --git a/liblog/fake_log_device.c b/liblog/fake_log_device.cpp
similarity index 88%
rename from liblog/fake_log_device.c
rename to liblog/fake_log_device.cpp
index 1483c24..5daae41 100644
--- a/liblog/fake_log_device.c
+++ b/liblog/fake_log_device.cpp
@@ -17,6 +17,9 @@
  * Intercepts log messages intended for the Android log device.
  * Messages are printed to stderr.
  */
+
+#include "fake_log_device.h"
+
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -30,9 +33,7 @@
 #include <time.h>
 
 #include <android/log.h>
-#include <log/uio.h>
 
-#include "fake_log_device.h"
 #include "log_portability.h"
 
 #define kMaxTagLen 16 /* from the long-dead utils/Log.cpp */
@@ -181,8 +182,7 @@
   logState->debugName[sizeof(logState->debugName) - 1] = '\0';
 
   /* identify binary logs */
-  if (!strcmp(pathName + kDevLogLen, "events") ||
-      !strcmp(pathName + kDevLogLen, "security")) {
+  if (!strcmp(pathName + kDevLogLen, "events") || !strcmp(pathName + kDevLogLen, "security")) {
     logState->isBinary = 1;
   }
 
@@ -204,8 +204,7 @@
       while (isspace(*tags)) tags++;
 
       i = 0;
-      while (*tags != '\0' && !isspace(*tags) && *tags != ':' &&
-             i < kMaxTagLen) {
+      while (*tags != '\0' && !isspace(*tags) && *tags != ':' && i < kMaxTagLen) {
         tagName[i++] = *tags++;
       }
       if (i == kMaxTagLen) {
@@ -313,13 +312,11 @@
  */
 static const char* getPriorityString(int priority) {
   /* the first character of each string should be unique */
-  static const char* priorityStrings[] = { "Verbose", "Debug", "Info",
-                                           "Warn",    "Error", "Assert" };
+  static const char* priorityStrings[] = {"Verbose", "Debug", "Info", "Warn", "Error", "Assert"};
   int idx;
 
   idx = (int)priority - (int)ANDROID_LOG_VERBOSE;
-  if (idx < 0 ||
-      idx >= (int)(sizeof(priorityStrings) / sizeof(priorityStrings[0])))
+  if (idx < 0 || idx >= (int)(sizeof(priorityStrings) / sizeof(priorityStrings[0])))
     return "?unknown?";
   return priorityStrings[idx];
 }
@@ -351,8 +348,7 @@
  *
  * Log format parsing taken from the long-dead utils/Log.cpp.
  */
-static void showLog(LogState* state, int logPrio, const char* tag,
-                    const char* msg) {
+static void showLog(LogState* state, int logPrio, const char* tag, const char* msg) {
 #if !defined(_WIN32)
   struct tm tmBuf;
 #endif
@@ -397,19 +393,16 @@
 
   switch (state->outputFormat) {
     case FORMAT_TAG:
-      prefixLen =
-          snprintf(prefixBuf, sizeof(prefixBuf), "%c/%-8s: ", priChar, tag);
+      prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%c/%-8s: ", priChar, tag);
       strcpy(suffixBuf, "\n");
       suffixLen = 1;
       break;
     case FORMAT_PROCESS:
-      prefixLen =
-          snprintf(prefixBuf, sizeof(prefixBuf), "%c(%5d) ", priChar, pid);
+      prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%c(%5d) ", priChar, pid);
       suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "  (%s)\n", tag);
       break;
     case FORMAT_THREAD:
-      prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%c(%5d:%5d) ",
-                           priChar, pid, tid);
+      prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%c(%5d:%5d) ", priChar, pid, tid);
       strcpy(suffixBuf, "\n");
       suffixLen = 1;
       break;
@@ -420,28 +413,24 @@
       suffixLen = 1;
       break;
     case FORMAT_TIME:
-      prefixLen =
-          snprintf(prefixBuf, sizeof(prefixBuf), "%s %-8s\n\t", timeBuf, tag);
+      prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%s %-8s\n\t", timeBuf, tag);
       strcpy(suffixBuf, "\n");
       suffixLen = 1;
       break;
     case FORMAT_THREADTIME:
-      prefixLen =
-          snprintf(prefixBuf, sizeof(prefixBuf), "%s %5d %5d %c %-8s \n\t",
-                   timeBuf, pid, tid, priChar, tag);
+      prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%s %5d %5d %c %-8s \n\t", timeBuf, pid,
+                           tid, priChar, tag);
       strcpy(suffixBuf, "\n");
       suffixLen = 1;
       break;
     case FORMAT_LONG:
-      prefixLen =
-          snprintf(prefixBuf, sizeof(prefixBuf), "[ %s %5d:%5d %c/%-8s ]\n",
-                   timeBuf, pid, tid, priChar, tag);
+      prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "[ %s %5d:%5d %c/%-8s ]\n", timeBuf, pid,
+                           tid, priChar, tag);
       strcpy(suffixBuf, "\n\n");
       suffixLen = 2;
       break;
     default:
-      prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
-                           "%c/%-8s(%5d): ", priChar, tag, pid);
+      prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%c/%-8s(%5d): ", priChar, tag, pid);
       strcpy(suffixBuf, "\n");
       suffixLen = 1;
       break;
@@ -559,8 +548,7 @@
  *  tag (N bytes -- null-terminated ASCII string)
  *  message (N bytes -- null-terminated ASCII string)
  */
-LIBLOG_HIDDEN ssize_t fakeLogWritev(int fd, const struct iovec* vector,
-                                    int count) {
+LIBLOG_HIDDEN ssize_t fakeLogWritev(int fd, const struct iovec* vector, int count) {
   LogState* state;
 
   /* Make sure that no-one frees the LogState while we're using it.
@@ -572,17 +560,24 @@
   state = fdToLogState(fd);
   if (state == NULL) {
     errno = EBADF;
-    goto error;
+    unlock();
+    return -1;
   }
 
   if (state->isBinary) {
     TRACE("%s: ignoring binary log\n", state->debugName);
-    goto bail;
+    unlock();
+    int len = 0;
+    for (int i = 0; i < count; ++i) {
+      len += vector[i].iov_len;
+    }
+    return len;
   }
 
   if (count != 3) {
     TRACE("%s: writevLog with count=%d not expected\n", state->debugName, count);
-    goto error;
+    unlock();
+    return -1;
   }
 
   /* pull out the three fields */
@@ -598,7 +593,6 @@
       break; /* reached end of configured values */
 
     if (strcmp(state->tagSet[i].tag, tag) == 0) {
-      // TRACE("MATCH tag '%s'\n", tag);
       minPrio = state->tagSet[i].minPriority;
       break;
     }
@@ -606,21 +600,14 @@
 
   if (logPrio >= minPrio) {
     showLog(state, logPrio, tag, msg);
-  } else {
-    // TRACE("+++ NOLOG(%d): %s %s", logPrio, tag, msg);
   }
 
-bail:
   unlock();
   int len = 0;
   for (i = 0; i < count; ++i) {
     len += vector[i].iov_len;
   }
   return len;
-
-error:
-  unlock();
-  return -1;
 }
 
 /*
@@ -663,22 +650,16 @@
   return fd;
 }
 
-LIBLOG_HIDDEN ssize_t __send_log_msg(char* buf __unused,
-                                     size_t buf_size __unused) {
+LIBLOG_HIDDEN ssize_t __send_log_msg(char*, size_t) {
   return -ENODEV;
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_is_loggable(int prio,
-                                                const char* tag __unused,
-                                                int def) {
+LIBLOG_ABI_PUBLIC int __android_log_is_loggable(int prio, const char*, int def) {
   int logLevel = def;
   return logLevel >= 0 && prio >= logLevel;
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_is_loggable_len(int prio,
-                                                    const char* tag __unused,
-                                                    size_t len __unused,
-                                                    int def) {
+LIBLOG_ABI_PUBLIC int __android_log_is_loggable_len(int prio, const char*, size_t, int def) {
   int logLevel = def;
   return logLevel >= 0 && prio >= logLevel;
 }
diff --git a/liblog/fake_log_device.h b/liblog/fake_log_device.h
index 7b0e745..ef0beb6 100644
--- a/liblog/fake_log_device.h
+++ b/liblog/fake_log_device.h
@@ -14,18 +14,25 @@
  * limitations under the License.
  */
 
-#ifndef _LIBLOG_FAKE_LOG_DEVICE_H
-#define _LIBLOG_FAKE_LOG_DEVICE_H
+#pragma once
 
 #include <sys/types.h>
 
 #include "log_portability.h"
+#include "uio.h"
 
 struct iovec;
 
+__BEGIN_DECLS
+
 LIBLOG_HIDDEN int fakeLogOpen(const char* pathName);
 LIBLOG_HIDDEN int fakeLogClose(int fd);
 LIBLOG_HIDDEN ssize_t fakeLogWritev(int fd, const struct iovec* vector,
                                     int count);
 
-#endif  // _LIBLOG_FAKE_LOG_DEVICE_H
+LIBLOG_HIDDEN ssize_t __send_log_msg(char*, size_t);
+LIBLOG_ABI_PUBLIC int __android_log_is_loggable(int prio, const char*, int def);
+LIBLOG_ABI_PUBLIC int __android_log_is_loggable_len(int prio, const char*, size_t, int def);
+LIBLOG_ABI_PRIVATE int __android_log_is_debuggable();
+
+__END_DECLS
diff --git a/liblog/fake_writer.c b/liblog/fake_writer.cpp
similarity index 82%
rename from liblog/fake_writer.c
rename to liblog/fake_writer.cpp
index 403dc72..46d171b 100644
--- a/liblog/fake_writer.c
+++ b/liblog/fake_writer.cpp
@@ -27,19 +27,18 @@
 
 static int fakeOpen();
 static void fakeClose();
-static int fakeWrite(log_id_t log_id, struct timespec* ts, struct iovec* vec,
-                     size_t nr);
+static int fakeWrite(log_id_t log_id, struct timespec* ts, struct iovec* vec, size_t nr);
 
-static int logFds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1, -1 };
+static int logFds[(int)LOG_ID_MAX] = {-1, -1, -1, -1, -1, -1};
 
 LIBLOG_HIDDEN struct android_log_transport_write fakeLoggerWrite = {
-  .node = { &fakeLoggerWrite.node, &fakeLoggerWrite.node },
-  .context.priv = &logFds,
-  .name = "fake",
-  .available = NULL,
-  .open = fakeOpen,
-  .close = fakeClose,
-  .write = fakeWrite,
+    .node = {&fakeLoggerWrite.node, &fakeLoggerWrite.node},
+    .context.priv = &logFds,
+    .name = "fake",
+    .available = NULL,
+    .open = fakeOpen,
+    .close = fakeClose,
+    .write = fakeWrite,
 };
 
 static int fakeOpen() {
@@ -54,7 +53,7 @@
     if (logFds[i] >= 0) {
       continue;
     }
-    snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
+    snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(static_cast<log_id_t>(i)));
     logFds[i] = fakeLogOpen(buf);
     if (logFds[i] < 0) {
       fprintf(stderr, "fakeLogOpen(%s) failed\n", buf);
@@ -72,8 +71,7 @@
   }
 }
 
-static int fakeWrite(log_id_t log_id, struct timespec* ts __unused,
-                     struct iovec* vec, size_t nr) {
+static int fakeWrite(log_id_t log_id, struct timespec*, struct iovec* vec, size_t nr) {
   ssize_t ret;
   size_t i;
   int logFd, len;
diff --git a/liblog/include/log/event_tag_map.h b/liblog/include/log/event_tag_map.h
index 8dd9157..2687b3a 100644
--- a/liblog/include/log/event_tag_map.h
+++ b/liblog/include/log/event_tag_map.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LIBS_CUTILS_EVENTTAGMAP_H
-#define _LIBS_CUTILS_EVENTTAGMAP_H
+#pragma once
 
 #ifdef __cplusplus
 extern "C" {
@@ -69,5 +68,3 @@
 #ifdef __cplusplus
 }
 #endif
-
-#endif /*_LIBS_CUTILS_EVENTTAGMAP_H*/
diff --git a/liblog/include/log/log.h b/liblog/include/log/log.h
index 3813e6e..5928649 100644
--- a/liblog/include/log/log.h
+++ b/liblog/include/log/log.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LIBS_LOG_LOG_H
-#define _LIBS_LOG_LOG_H
+#pragma once
 
 /* Too many in the ecosystem assume these are included */
 #if !defined(_WIN32)
@@ -35,7 +34,6 @@
 #include <log/log_safetynet.h>
 #include <log/log_system.h>
 #include <log/log_time.h>
-#include <log/uio.h> /* helper to define iovec for portability */
 
 #ifdef __cplusplus
 extern "C" {
@@ -152,35 +150,12 @@
 
 #ifdef __linux__
 
-#ifndef __ANDROID_USE_LIBLOG_CLOCK_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 1
-#elif __ANDROID_API__ > 22 /* > Lollipop */
-#define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_CLOCK_INTERFACE
 clockid_t android_log_clockid(void);
-#endif
 
 #endif /* __linux__ */
 
 /* --------------------------------------------------------------------- */
 
-#ifndef __ANDROID_USE_LIBLOG_CLOSE_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 1
-#elif __ANDROID_API__ > 18 /* > JellyBean */
-#define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_CLOSE_INTERFACE
 /*
  * Release any logger resources (a new log write will immediately re-acquire)
  *
@@ -188,54 +163,6 @@
  * all O_CLOEXEC so wil self clean on exec().
  */
 void __android_log_close(void);
-#endif
-
-#ifndef __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE 1
-#elif __ANDROID_API__ > 25 /* > OC */
-#define __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE
-
-/*
- * if last is NULL, caller _must_ provide a consistent value for seconds.
- *
- * Return -1 if we can not acquire a lock, which below will permit the logging,
- * error on allowing a log message through.
- */
-int __android_log_ratelimit(time_t seconds, time_t* last);
-
-/*
- * Usage:
- *
- *   // Global default and state
- *   IF_ALOG_RATELIMIT() {
- *      ALOG*(...);
- *   }
- *
- *   // local state, 10 seconds ratelimit
- *   static time_t local_state;
- *   IF_ALOG_RATELIMIT_LOCAL(10, &local_state) {
- *     ALOG*(...);
- *   }
- */
-
-#define IF_ALOG_RATELIMIT() if (__android_log_ratelimit(0, NULL) > 0)
-#define IF_ALOG_RATELIMIT_LOCAL(seconds, state) \
-  if (__android_log_ratelimit(seconds, state) > 0)
-
-#else
-
-/* No ratelimiting as API unsupported */
-#define IF_ALOG_RATELIMIT() if (1)
-#define IF_ALOG_RATELIMIT_LOCAL(...) if (1)
-
-#endif
 
 #if defined(__clang__)
 #pragma clang diagnostic pop
@@ -244,5 +171,3 @@
 #ifdef __cplusplus
 }
 #endif
-
-#endif /* _LIBS_LOG_LOG_H */
diff --git a/liblog/include/log/log_event_list.h b/liblog/include/log/log_event_list.h
index 1b7c377..636d417 100644
--- a/liblog/include/log/log_event_list.h
+++ b/liblog/include/log/log_event_list.h
@@ -14,16 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef _LIBS_LOG_EVENT_LIST_H
-#define _LIBS_LOG_EVENT_LIST_H
+#pragma once
 
 #include <errno.h>
 #include <stdint.h>
 
-#if (defined(__cplusplus) && defined(_USING_LIBCXX))
-extern "C++" {
+#ifdef __cplusplus
 #include <string>
-}
 #endif
 
 #include <log/log.h>
@@ -32,18 +29,6 @@
 extern "C" {
 #endif
 
-#ifndef __ANDROID_USE_LIBLOG_EVENT_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_EVENT_INTERFACE 1
-#elif __ANDROID_API__ > 23 /* > Marshmallow */
-#define __ANDROID_USE_LIBLOG_EVENT_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_EVENT_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_EVENT_INTERFACE
-
 /* For manipulating lists of events. */
 
 #define ANDROID_MAX_LIST_NEST_DEPTH 8
@@ -124,8 +109,6 @@
 /* android_log_list C++ helpers */
 extern "C++" {
 class android_log_event_list {
-  friend class __android_log_event_list;
-
  private:
   android_log_context ctx;
   int ret;
@@ -137,10 +120,6 @@
   explicit android_log_event_list(int tag) : ret(0) {
     ctx = create_android_logger(static_cast<uint32_t>(tag));
   }
-  explicit android_log_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));
-  }
   ~android_log_event_list() {
     android_log_destroy(&ctx);
   }
@@ -208,14 +187,12 @@
     return *this;
   }
 
-#if defined(_USING_LIBCXX)
   android_log_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
 
   android_log_event_list& operator<<(float value) {
     int retval = android_log_write_float32(ctx, value);
@@ -269,7 +246,6 @@
     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());
@@ -283,7 +259,6 @@
     if (retval < 0) ret = retval;
     return ret;
   }
-#endif
 
   bool AppendFloat(float value) {
     int retval = android_log_write_float32(ctx, value);
@@ -302,22 +277,11 @@
     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
 
-#endif /* __ANDROID_USE_LIBLOG_EVENT_INTERFACE */
-
 #ifdef __cplusplus
 }
 #endif
-
-#endif /* _LIBS_LOG_EVENT_LIST_H */
diff --git a/liblog/include/log/log_id.h b/liblog/include/log/log_id.h
index c44f5a2..c052a50 100644
--- a/liblog/include/log/log_id.h
+++ b/liblog/include/log/log_id.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LIBS_LOG_LOG_ID_H
-#define _LIBS_LOG_LOG_ID_H
+#pragma once
 
 #ifdef __cplusplus
 extern "C" {
@@ -62,5 +61,3 @@
 #ifdef __cplusplus
 }
 #endif
-
-#endif /* _LIBS_LOG_LOG_ID_H */
diff --git a/liblog/include/log/log_main.h b/liblog/include/log/log_main.h
index 53653de..64791c2 100644
--- a/liblog/include/log/log_main.h
+++ b/liblog/include/log/log_main.h
@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-#ifndef _LIBS_LOG_LOG_MAIN_H
-#define _LIBS_LOG_LOG_MAIN_H
+#pragma once
 
 #include <stdbool.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
 
 #include <android/log.h>
-#include <sys/cdefs.h>
 
 __BEGIN_DECLS
 
@@ -349,20 +349,6 @@
  *        over Android.
  */
 
-#ifndef __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 2
-#elif __ANDROID_API__ > 24 /* > Nougat */
-#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 2
-#elif __ANDROID_API__ > 22 /* > Lollipop */
-#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE
-
 /*
  * Use the per-tag properties "log.tag.<tagname>" to generate a runtime
  * result of non-zero to expose a log. prio is ANDROID_LOG_VERBOSE to
@@ -370,12 +356,7 @@
  * any other value.
  */
 int __android_log_is_loggable(int prio, const char* tag, int default_prio);
-
-#if __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE > 1
-#include <sys/types.h>
-
-int __android_log_is_loggable_len(int prio, const char* tag, size_t len,
-                                  int default_prio);
+int __android_log_is_loggable_len(int prio, const char* tag, size_t len, int default_prio);
 
 #if LOG_NDEBUG /* Production */
 #define android_testLog(prio, tag)                                           \
@@ -387,28 +368,8 @@
                                  ANDROID_LOG_VERBOSE) != 0)
 #endif
 
-#else
-
-#if LOG_NDEBUG /* Production */
-#define android_testLog(prio, tag) \
-  (__android_log_is_loggable(prio, tag, ANDROID_LOG_DEBUG) != 0)
-#else
-#define android_testLog(prio, tag) \
-  (__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE) != 0)
-#endif
-
-#endif
-
-#else /* __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE */
-
-#define android_testLog(prio, tag) (1)
-
-#endif /* !__ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE */
-
 #if defined(__clang__)
 #pragma clang diagnostic pop
 #endif
 
 __END_DECLS
-
-#endif /* _LIBS_LOG_LOG_MAIN_H */
diff --git a/liblog/include/log/log_properties.h b/liblog/include/log/log_properties.h
index 7d398a6..3a8af6d 100644
--- a/liblog/include/log/log_properties.h
+++ b/liblog/include/log/log_properties.h
@@ -7,29 +7,14 @@
 ** General Public License.
 */
 
-#ifndef _LIBS_LOG_PROPERTIES_H
-#define _LIBS_LOG_PROPERTIES_H
+#pragma once
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#ifndef __ANDROID_USE_LIBLOG_IS_DEBUGGABLE_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_IS_DEBUGGABLE_INTERFACE 1
-#elif __ANDROID_API__ > 24 /* > Nougat */
-#define __ANDROID_USE_LIBLOG_IS_DEBUGGABLE_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_IS_DEBUGGABLE_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_IS_DEBUGGABLE_INTERFACE
 int __android_log_is_debuggable();
-#endif
 
 #ifdef __cplusplus
 }
 #endif
-
-#endif /* _LIBS_LOG_PROPERTIES_H */
diff --git a/liblog/include/log/log_radio.h b/liblog/include/log/log_radio.h
index bd629fe..8b8a362 100644
--- a/liblog/include/log/log_radio.h
+++ b/liblog/include/log/log_radio.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LIBS_LOG_LOG_RADIO_H
-#define _LIBS_LOG_LOG_RADIO_H
+#pragma once
 
 #include <android/log.h>
 #include <log/log_id.h>
@@ -140,5 +139,3 @@
                                         LOG_TAG, __VA_ARGS__))           \
        : (void)0)
 #endif
-
-#endif /* _LIBS_LOG_LOG_RADIO_H */
diff --git a/liblog/include/log/log_read.h b/liblog/include/log/log_read.h
index 93b9d4e..fdef306 100644
--- a/liblog/include/log/log_read.h
+++ b/liblog/include/log/log_read.h
@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
-#ifndef _LIBS_LOG_LOG_READ_H
-#define _LIBS_LOG_LOG_READ_H
+#pragma once
+
+#include <sys/types.h>
 
 /* deal with possible sys/cdefs.h conflict with fcntl.h */
 #ifdef __unused
@@ -47,6 +48,8 @@
  * access to raw information, or parsing is an issue.
  */
 
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wzero-length-array"
 /*
  * The userspace structure for version 1 of the logger_entry ABI.
  */
@@ -59,9 +62,7 @@
   int32_t tid;    /* generating process's tid */
   int32_t sec;    /* seconds since Epoch */
   int32_t nsec;   /* nanoseconds */
-#ifndef __cplusplus
   char msg[0]; /* the entry's payload */
-#endif
 };
 #endif
 
@@ -78,9 +79,7 @@
   int32_t sec;       /* seconds since Epoch */
   int32_t nsec;      /* nanoseconds */
   uint32_t euid;     /* effective UID of logger */
-#ifndef __cplusplus
   char msg[0]; /* the entry's payload */
-#endif
 } __attribute__((__packed__));
 #endif
 
@@ -97,9 +96,7 @@
   int32_t sec;       /* seconds since Epoch */
   int32_t nsec;      /* nanoseconds */
   uint32_t lid;      /* log id of the payload */
-#ifndef __cplusplus
   char msg[0]; /* the entry's payload */
-#endif
 } __attribute__((__packed__));
 #endif
 
@@ -117,11 +114,10 @@
   uint32_t nsec;     /* nanoseconds */
   uint32_t lid;      /* log id of the payload, bottom 4 bits currently */
   uint32_t uid;      /* generating process's uid */
-#ifndef __cplusplus
   char msg[0]; /* the entry's payload */
-#endif
 };
 #endif
+#pragma clang diagnostic pop
 
 /*
  * The maximum size of the log entry payload that can be
@@ -197,22 +193,6 @@
 };
 #endif
 
-#ifndef __ANDROID_USE_LIBLOG_READER_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_READER_INTERFACE 3
-#elif __ANDROID_API__ > 23 /* > Marshmallow */
-#define __ANDROID_USE_LIBLOG_READER_INTERFACE 3
-#elif __ANDROID_API__ > 22 /* > Lollipop */
-#define __ANDROID_USE_LIBLOG_READER_INTERFACE 2
-#elif __ANDROID_API__ > 19 /* > KitKat */
-#define __ANDROID_USE_LIBLOG_READER_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_READER_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_READER_INTERFACE
-
 struct logger;
 
 log_id_t android_logger_get_id(struct logger* logger);
@@ -225,14 +205,12 @@
 
 struct logger_list;
 
-#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 1
 ssize_t android_logger_get_statistics(struct logger_list* logger_list,
                                       char* buf, size_t len);
 ssize_t android_logger_get_prune_list(struct logger_list* logger_list,
                                       char* buf, size_t len);
 int android_logger_set_prune_list(struct logger_list* logger_list, char* buf,
                                   size_t len);
-#endif
 
 #define ANDROID_LOG_RDONLY O_RDONLY
 #define ANDROID_LOG_WRONLY O_WRONLY
@@ -243,13 +221,9 @@
 #else
 #define ANDROID_LOG_NONBLOCK O_NONBLOCK
 #endif
-#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 2
 #define ANDROID_LOG_WRAP 0x40000000 /* Block until buffer about to wrap */
 #define ANDROID_LOG_WRAP_DEFAULT_TIMEOUT 7200 /* 2 hour default */
-#endif
-#if __ANDROID_USE_LIBLOG_READER_INTERFACE > 1
 #define ANDROID_LOG_PSTORE 0x80000000
-#endif
 
 struct logger_list* android_logger_list_alloc(int mode, unsigned int tail,
                                               pid_t pid);
@@ -268,10 +242,6 @@
                                              unsigned int tail, pid_t pid);
 #define android_logger_list_close android_logger_list_free
 
-#endif /* __ANDROID_USE_LIBLOG_READER_INTERFACE */
-
 #ifdef __cplusplus
 }
 #endif
-
-#endif /* _LIBS_LOG_LOG_H */
diff --git a/liblog/include/log/log_safetynet.h b/liblog/include/log/log_safetynet.h
index 07e8c8a..d3e9b19 100644
--- a/liblog/include/log/log_safetynet.h
+++ b/liblog/include/log/log_safetynet.h
@@ -7,8 +7,7 @@
 ** General Public License.
 */
 
-#ifndef _LIBS_LOG_SAFETYNET_H
-#define _LIBS_LOG_SAFETYNET_H
+#pragma once
 
 #include <stdint.h>
 
@@ -16,18 +15,6 @@
 extern "C" {
 #endif
 
-#ifndef _ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE
-#ifndef __ANDROID_API__
-#define __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE 1
-#elif __ANDROID_API__ > 22 /* > Lollipop */
-#define __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE 1
-#else
-#define __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE 0
-#endif
-#endif
-
-#if __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE
-
 #define android_errorWriteLog(tag, subTag) \
   __android_log_error_write(tag, subTag, -1, NULL, 0)
 
@@ -37,10 +24,6 @@
 int __android_log_error_write(int tag, const char* subTag, int32_t uid,
                               const char* data, uint32_t dataLen);
 
-#endif /* __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE */
-
 #ifdef __cplusplus
 }
 #endif
-
-#endif /* _LIBS_LOG_SAFETYNET_H */
diff --git a/liblog/include/log/log_system.h b/liblog/include/log/log_system.h
index 3b5ae22..eaec741 100644
--- a/liblog/include/log/log_system.h
+++ b/liblog/include/log/log_system.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LIBS_LOG_LOG_SYSTEM_H
-#define _LIBS_LOG_LOG_SYSTEM_H
+#pragma once
 
 #include <android/log.h>
 #include <log/log_id.h>
@@ -138,5 +137,3 @@
                                         LOG_TAG, __VA_ARGS__))            \
        : (void)0)
 #endif
-
-#endif /* _LIBS_LOG_LOG_SYSTEM_H */
diff --git a/liblog/include/log/log_time.h b/liblog/include/log/log_time.h
index 309f5d1..09c9910 100644
--- a/liblog/include/log/log_time.h
+++ b/liblog/include/log/log_time.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LIBS_LOG_LOG_TIME_H
-#define _LIBS_LOG_LOG_TIME_H
+#pragma once
 
 #include <stdint.h>
 #include <time.h>
@@ -34,6 +33,8 @@
 
 #ifdef __cplusplus
 
+extern "C" {
+
 /*
  * NB: we did NOT define a copy constructor. This will result in structure
  * no longer being compatible with pass-by-value which is desired
@@ -41,25 +42,19 @@
  */
 struct log_time {
  public:
-  uint32_t tv_sec; /* good to Feb 5 2106 */
-  uint32_t tv_nsec;
+  uint32_t tv_sec = 0; /* good to Feb 5 2106 */
+  uint32_t tv_nsec = 0;
 
   static const uint32_t tv_sec_max = 0xFFFFFFFFUL;
   static const uint32_t tv_nsec_max = 999999999UL;
+  static const timespec EPOCH;
 
-  log_time(const timespec& T)
-      : tv_sec(static_cast<uint32_t>(T.tv_sec)),
-        tv_nsec(static_cast<uint32_t>(T.tv_nsec)) {
-  }
+  log_time() {}
+  explicit log_time(const timespec& T)
+      : tv_sec(static_cast<uint32_t>(T.tv_sec)), tv_nsec(static_cast<uint32_t>(T.tv_nsec)) {}
   explicit log_time(uint32_t sec, uint32_t nsec = 0)
       : tv_sec(sec), tv_nsec(nsec) {
   }
-#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
-#define __struct_log_time_private_defined
-  static const timespec EPOCH;
-#endif
-  log_time() {
-  }
 #ifdef __linux__
   explicit log_time(clockid_t id) {
     timespec T;
@@ -103,7 +98,6 @@
     return !(*this > T);
   }
 
-#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
   log_time operator-=(const timespec& T);
   log_time operator-(const timespec& T) const {
     log_time local(*this);
@@ -114,7 +108,6 @@
     log_time local(*this);
     return local += T;
   }
-#endif
 
   /* log_time */
   bool operator==(const log_time& T) const {
@@ -138,7 +131,6 @@
     return !(*this > T);
   }
 
-#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
   log_time operator-=(const log_time& T);
   log_time operator-(const log_time& T) const {
     log_time local(*this);
@@ -149,7 +141,6 @@
     log_time local(*this);
     return local += T;
   }
-#endif
 
   uint64_t nsec() const {
     return static_cast<uint64_t>(tv_sec) * NS_PER_SEC + tv_nsec;
@@ -163,13 +154,12 @@
            tv_nsec / (NS_PER_SEC / MS_PER_SEC);
   }
 
-#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
   static const char default_format[];
 
   /* Add %#q for the fraction of a second to the standard library functions */
   char* strptime(const char* s, const char* format = default_format);
-#endif
 } __attribute__((__packed__));
+}
 
 #else /* __cplusplus */
 
@@ -181,5 +171,3 @@
 #endif /* __cplusplus */
 
 #endif /* __struct_log_time_defined */
-
-#endif /* _LIBS_LOG_LOG_TIME_H */
diff --git a/liblog/include/log/log_transport.h b/liblog/include/log/log_transport.h
index 80b30db..b48761a 100644
--- a/liblog/include/log/log_transport.h
+++ b/liblog/include/log/log_transport.h
@@ -7,8 +7,7 @@
 ** General Public License.
 */
 
-#ifndef _LIBS_LOG_TRANSPORT_H
-#define _LIBS_LOG_TRANSPORT_H
+#pragma once
 
 #ifdef __cplusplus
 extern "C" {
@@ -22,7 +21,7 @@
 #define LOGGER_LOGD    0x01
 #define LOGGER_KERNEL  0x02 /* Reserved/Deprecated */
 #define LOGGER_NULL    0x04 /* Does not release resources of other selections */
-#define LOGGER_LOCAL   0x08 /* logs sent to local memory */
+#define LOGGER_RESERVED 0x08 /* Reserved, previously for logging to local memory */
 #define LOGGER_STDERR  0x10 /* logs sent to stderr */
 /* clang-format on */
 
@@ -33,5 +32,3 @@
 #ifdef __cplusplus
 }
 #endif
-
-#endif /* _LIBS_LOG_TRANSPORT_H */
diff --git a/liblog/include/log/logd.h b/liblog/include/log/logd.h
deleted file mode 100644
index 77400ca..0000000
--- a/liblog/include/log/logd.h
+++ /dev/null
@@ -1,5 +0,0 @@
-#ifndef _LIBS_LOG_LOGD_H
-#define _LIBS_LOG_LOGD_H
-#include <log/log.h>
-#warning "Deprecated: do not include log/logd.h, use log/log.h instead"
-#endif /*_LIBS_LOG_LOGD_H*/
diff --git a/liblog/include/log/logger.h b/liblog/include/log/logger.h
deleted file mode 100644
index 1bf2d17..0000000
--- a/liblog/include/log/logger.h
+++ /dev/null
@@ -1,5 +0,0 @@
-#ifndef _LIBS_LOG_LOGGER_H
-#define _LIBS_LOG_LOGGER_H
-#include <log/log.h>
-#warning "Deprecated: do not include log/logger.h, use log/log.h instead"
-#endif /*_LIBS_LOG_LOGGER_H*/
diff --git a/liblog/include/log/logprint.h b/liblog/include/log/logprint.h
index ca58bc7..8f4b187 100644
--- a/liblog/include/log/logprint.h
+++ b/liblog/include/log/logprint.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LOGPRINT_H
-#define _LOGPRINT_H
+#pragma once
 
 #include <pthread.h>
 
@@ -158,5 +157,3 @@
 #ifdef __cplusplus
 }
 #endif
-
-#endif /*_LOGPRINT_H*/
diff --git a/liblog/include/private/android_logger.h b/liblog/include/private/android_logger.h
index b927b46..5e04148 100644
--- a/liblog/include/private/android_logger.h
+++ b/liblog/include/private/android_logger.h
@@ -16,8 +16,7 @@
 
 /* This file is used to define the internal protocol for the Android Logger */
 
-#ifndef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
-#define _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+#pragma once
 
 /* Android private interfaces */
 
@@ -25,10 +24,8 @@
 #include <stdint.h>
 #include <sys/types.h>
 
-#if (defined(__cplusplus) && defined(_USING_LIBCXX))
-extern "C++" {
+#ifdef __cplusplus
 #include <string>
-}
 #endif
 
 #include <log/log.h>
@@ -153,41 +150,6 @@
 /* Retrieve the composed event buffer */
 int android_log_write_list_buffer(android_log_context ctx, const char** msg);
 
-#ifdef __cplusplus
-#ifdef __class_android_log_event_list_defined
-#ifndef __class_android_log_event_list_private_defined
-#define __class_android_log_event_list_private_defined
-/* android_log_context C++ helpers */
-extern "C++" {
-class __android_log_event_list : public android_log_event_list {
-  __android_log_event_list(const android_log_event_list&) = delete;
-  void operator=(const __android_log_event_list&) = delete;
-
- public:
-  explicit __android_log_event_list(int tag) : android_log_event_list(tag) {
-  }
-  explicit __android_log_event_list(log_msg& log_msg)
-      : android_log_event_list(log_msg) {
-  }
-
-#if defined(_USING_LIBCXX)
-  operator std::string() {
-    if (ret) return std::string("");
-    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("");
-    return std::string(cp, len);
-  }
-#endif
-};
-}
-#endif
-#endif
-#endif
-
 #if defined(__cplusplus)
 }
 #endif
-
-#endif /* _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_ */
diff --git a/liblog/include_vndk/log/log_event_list.h b/liblog/include_vndk/log/log_event_list.h
index 9f74534..1f3dd37 100644
--- a/liblog/include_vndk/log/log_event_list.h
+++ b/liblog/include_vndk/log/log_event_list.h
@@ -27,8 +27,6 @@
 extern "C" {
 #endif
 
-#define __ANDROID_USE_LIBLOG_EVENT_INTERFACE 1
-
 /*
  * The opaque context used to manipulate lists of events.
  */
diff --git a/liblog/local_logger.c b/liblog/local_logger.c
deleted file mode 100644
index 563cb3f..0000000
--- a/liblog/local_logger.c
+++ /dev/null
@@ -1,550 +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 <errno.h>
-#include <fcntl.h>
-#include <pthread.h>
-#if !defined(__MINGW32__)
-#include <pwd.h>
-#endif
-#include <log/uio.h>
-#include <sched.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-
-#include <cutils/list.h> /* template, no library dependency */
-#include <log/log_transport.h>
-#include <private/android_filesystem_config.h>
-#include <private/android_logger.h>
-#include <system/thread_defs.h>
-
-#include "config_read.h"
-#include "config_write.h"
-#include "log_portability.h"
-#include "logger.h"
-
-static const char baseServiceName[] = "android.logd";
-
-static int writeToLocalInit();
-static int writeToLocalAvailable(log_id_t logId);
-static void writeToLocalReset();
-static int writeToLocalWrite(log_id_t logId, struct timespec* ts,
-                             struct iovec* vec, size_t nr);
-
-LIBLOG_HIDDEN struct android_log_transport_write localLoggerWrite = {
-  .node = { &localLoggerWrite.node, &localLoggerWrite.node },
-  .context.priv = NULL,
-  .name = "local",
-  .available = writeToLocalAvailable,
-  .open = writeToLocalInit,
-  .close = writeToLocalReset,
-  .write = writeToLocalWrite,
-};
-
-static int writeToLocalVersion(struct android_log_logger* logger,
-                               struct android_log_transport_context* transp);
-static int writeToLocalRead(struct android_log_logger_list* logger_list,
-                            struct android_log_transport_context* transp,
-                            struct log_msg* log_msg);
-static int writeToLocalPoll(struct android_log_logger_list* logger_list,
-                            struct android_log_transport_context* transp);
-static void writeToLocalClose(struct android_log_logger_list* logger_list,
-                              struct android_log_transport_context* transp);
-static int writeToLocalClear(struct android_log_logger* logger,
-                             struct android_log_transport_context* transp);
-static ssize_t writeToLocalGetSize(struct android_log_logger* logger,
-                                   struct android_log_transport_context* transp);
-static ssize_t writeToLocalSetSize(
-    struct android_log_logger* logger,
-    struct android_log_transport_context* transp __unused, size_t size);
-static ssize_t writeToLocalGetReadbleSize(
-    struct android_log_logger* logger,
-    struct android_log_transport_context* transp);
-
-struct android_log_transport_read localLoggerRead = {
-  .node = { &localLoggerRead.node, &localLoggerRead.node },
-  .name = "local",
-  .available = writeToLocalAvailable,
-  .version = writeToLocalVersion,
-  .read = writeToLocalRead,
-  .poll = writeToLocalPoll,
-  .close = writeToLocalClose,
-  .clear = writeToLocalClear,
-  .getSize = writeToLocalGetSize,
-  .setSize = writeToLocalSetSize,
-  .getReadableSize = writeToLocalGetReadbleSize,
-  .getPrune = NULL,
-  .setPrune = NULL,
-  .getStats = NULL,
-};
-
-struct LogBufferElement {
-  struct listnode node;
-  log_id_t logId;
-  pid_t tid;
-  log_time timestamp;
-  unsigned short len;
-  char msg[];
-};
-
-static const size_t MAX_SIZE_DEFAULT = 32768;
-
-/*
- * Number of log buffers we support with the following assumption:
- *  . . .
- *   LOG_ID_SECURITY = 5, // security logs go to the system logs only
- *   LOG_ID_KERNEL = 6,   // place last, third-parties can not use it
- *   LOG_ID_MAX
- * } log_id_t;
- *
- * Confirm the following should <log/log_id.h> be adjusted in the future.
- */
-#define NUMBER_OF_LOG_BUFFERS \
-  ((LOG_ID_SECURITY == (LOG_ID_MAX - 2)) ? LOG_ID_SECURITY : LOG_ID_KERNEL)
-#define BLOCK_LOG_BUFFERS(id) \
-  (((id) == LOG_ID_SECURITY) || ((id) == LOG_ID_KERNEL))
-
-static struct LogBuffer {
-  struct listnode head;
-  pthread_rwlock_t listLock;
-  char* serviceName; /* Also indicates ready by having a value */
-  /* Order and proximity important for memset */
-  size_t number[NUMBER_OF_LOG_BUFFERS];         /* clear memset          */
-  size_t size[NUMBER_OF_LOG_BUFFERS];           /* clear memset          */
-  size_t totalSize[NUMBER_OF_LOG_BUFFERS];      /* init memset           */
-  size_t maxSize[NUMBER_OF_LOG_BUFFERS];        /* init MAX_SIZE_DEFAULT */
-  struct listnode* last[NUMBER_OF_LOG_BUFFERS]; /* init &head            */
-} logbuf = {
-  .head = { &logbuf.head, &logbuf.head }, .listLock = PTHREAD_RWLOCK_INITIALIZER,
-};
-
-static void LogBufferInit(struct LogBuffer* log) {
-  size_t i;
-
-  pthread_rwlock_wrlock(&log->listLock);
-  list_init(&log->head);
-  memset(log->number, 0,
-         sizeof(log->number) + sizeof(log->size) + sizeof(log->totalSize));
-  for (i = 0; i < NUMBER_OF_LOG_BUFFERS; ++i) {
-    log->maxSize[i] = MAX_SIZE_DEFAULT;
-    log->last[i] = &log->head;
-  }
-#ifdef __BIONIC__
-  asprintf(&log->serviceName, "%s@%d:%d", baseServiceName, __android_log_uid(),
-           getpid());
-#else
-  char buffer[sizeof(baseServiceName) + 1 + 5 + 1 + 5 + 8];
-  snprintf(buffer, sizeof(buffer), "%s@%d:%d", baseServiceName,
-           __android_log_uid(), getpid());
-  log->serviceName = strdup(buffer);
-#endif
-  pthread_rwlock_unlock(&log->listLock);
-}
-
-static void LogBufferClear(struct LogBuffer* log) {
-  size_t i;
-  struct listnode* node;
-
-  pthread_rwlock_wrlock(&log->listLock);
-  memset(log->number, 0, sizeof(log->number) + sizeof(log->size));
-  for (i = 0; i < NUMBER_OF_LOG_BUFFERS; ++i) {
-    log->last[i] = &log->head;
-  }
-  while ((node = list_head(&log->head)) != &log->head) {
-    struct LogBufferElement* element;
-
-    element = node_to_item(node, struct LogBufferElement, node);
-    list_remove(node);
-    free(element);
-  }
-  pthread_rwlock_unlock(&log->listLock);
-}
-
-static inline void LogBufferFree(struct LogBuffer* log) {
-  pthread_rwlock_wrlock(&log->listLock);
-  free(log->serviceName);
-  log->serviceName = NULL;
-  pthread_rwlock_unlock(&log->listLock);
-  LogBufferClear(log);
-}
-
-static int LogBufferLog(struct LogBuffer* log,
-                        struct LogBufferElement* element) {
-  log_id_t logId = element->logId;
-
-  pthread_rwlock_wrlock(&log->listLock);
-  log->number[logId]++;
-  log->size[logId] += element->len;
-  log->totalSize[logId] += element->len;
-  /* prune entry(s) until enough space is available */
-  if (log->last[logId] == &log->head) {
-    log->last[logId] = list_tail(&log->head);
-  }
-  while (log->size[logId] > log->maxSize[logId]) {
-    struct listnode* node = log->last[logId];
-    struct LogBufferElement* e;
-    struct android_log_logger_list* logger_list;
-
-    e = node_to_item(node, struct LogBufferElement, node);
-    log->number[logId]--;
-    log->size[logId] -= e->len;
-    logger_list_rdlock();
-    logger_list_for_each(logger_list) {
-      struct android_log_transport_context* transp;
-
-      transport_context_for_each(transp, logger_list) {
-        if ((transp->transport == &localLoggerRead) &&
-            (transp->context.node == node)) {
-          if (node == &log->head) {
-            transp->context.node = &log->head;
-          } else {
-            transp->context.node = node->next;
-          }
-        }
-      }
-    }
-    logger_list_unlock();
-    if (node != &log->head) {
-      log->last[logId] = node->prev;
-    }
-    list_remove(node);
-    LOG_ALWAYS_FATAL_IF(node == log->last[logId], "corrupted list");
-    free(e);
-  }
-  /* add entry to list */
-  list_add_head(&log->head, &element->node);
-  /* ToDo: wake up all readers */
-  pthread_rwlock_unlock(&log->listLock);
-
-  return element->len;
-}
-
-/*
- * return zero if permitted to log directly to logd,
- * return 1 if binder server started and
- * return negative error number if failed to start binder server.
- */
-static int writeToLocalInit() {
-  pthread_attr_t attr;
-  struct LogBuffer* log;
-
-  if (writeToLocalAvailable(LOG_ID_MAIN) < 0) {
-    return -EPERM;
-  }
-
-  log = &logbuf;
-  if (!log->serviceName) {
-    LogBufferInit(log);
-  }
-
-  if (!log->serviceName) {
-    LogBufferFree(log);
-    return -ENOMEM;
-  }
-
-  return EPERM; /* successful local-only logging */
-}
-
-static void writeToLocalReset() {
-  LogBufferFree(&logbuf);
-}
-
-static int writeToLocalAvailable(log_id_t logId) {
-#if !defined(__MINGW32__)
-  uid_t uid;
-#endif
-
-  if ((logId >= NUMBER_OF_LOG_BUFFERS) || BLOCK_LOG_BUFFERS(logId)) {
-    return -EINVAL;
-  }
-
-/* Android hard coded permitted, system goes to logd */
-#if !defined(__MINGW32__)
-  if (__android_log_transport == LOGGER_DEFAULT) {
-    uid = __android_log_uid();
-    if ((uid < AID_APP) && (getpwuid(uid) != NULL)) {
-      return -EPERM;
-    }
-  }
-#endif
-
-  /* ToDo: Ask package manager for LOGD permissions */
-  /* Assume we do _not_ have permissions to go to LOGD, so must go local */
-  return 0;
-}
-
-static int writeToLocalWrite(log_id_t logId, struct timespec* ts,
-                             struct iovec* vec, size_t nr) {
-  size_t len, i;
-  struct LogBufferElement* element;
-
-  if ((logId >= NUMBER_OF_LOG_BUFFERS) || BLOCK_LOG_BUFFERS(logId)) {
-    return -EINVAL;
-  }
-
-  len = 0;
-  for (i = 0; i < nr; ++i) {
-    len += vec[i].iov_len;
-  }
-
-  if (len > LOGGER_ENTRY_MAX_PAYLOAD) {
-    len = LOGGER_ENTRY_MAX_PAYLOAD;
-  }
-  element = (struct LogBufferElement*)calloc(
-      1, sizeof(struct LogBufferElement) + len + 1);
-  if (!element) {
-    return errno ? -errno : -ENOMEM;
-  }
-  element->timestamp.tv_sec = ts->tv_sec;
-  element->timestamp.tv_nsec = ts->tv_nsec;
-#ifdef __BIONIC__
-  element->tid = gettid();
-#else
-  element->tid = getpid();
-#endif
-  element->logId = logId;
-  element->len = len;
-
-  char* cp = element->msg;
-  for (i = 0; i < nr; ++i) {
-    size_t iov_len = vec[i].iov_len;
-    if (iov_len > len) {
-      iov_len = len;
-    }
-    memcpy(cp, vec[i].iov_base, iov_len);
-    len -= iov_len;
-    if (len == 0) {
-      break;
-    }
-    cp += iov_len;
-  }
-
-  return LogBufferLog(&logbuf, element);
-}
-
-static int writeToLocalVersion(struct android_log_logger* logger __unused,
-                               struct android_log_transport_context* transp
-                                   __unused) {
-  return 3;
-}
-
-/* within reader lock, serviceName already validated */
-static struct listnode* writeToLocalNode(
-    struct android_log_logger_list* logger_list,
-    struct android_log_transport_context* transp) {
-  struct listnode* node;
-  unsigned logMask;
-  unsigned int tail;
-
-  node = transp->context.node;
-  if (node) {
-    return node;
-  }
-
-  if (!logger_list->tail) {
-    return transp->context.node = &logbuf.head;
-  }
-
-  logMask = transp->logMask;
-  tail = logger_list->tail;
-
-  for (node = list_head(&logbuf.head); node != &logbuf.head; node = node->next) {
-    struct LogBufferElement* element;
-    log_id_t logId;
-
-    element = node_to_item(node, struct LogBufferElement, node);
-    logId = element->logId;
-
-    if ((logMask & (1 << logId)) && !--tail) {
-      node = node->next;
-      break;
-    }
-  }
-  return transp->context.node = node;
-}
-
-static int writeToLocalRead(struct android_log_logger_list* logger_list,
-                            struct android_log_transport_context* transp,
-                            struct log_msg* log_msg) {
-  int ret;
-  struct listnode* node;
-  unsigned logMask;
-
-  pthread_rwlock_rdlock(&logbuf.listLock);
-  if (!logbuf.serviceName) {
-    pthread_rwlock_unlock(&logbuf.listLock);
-    return (logger_list->mode & ANDROID_LOG_NONBLOCK) ? -ENODEV : 0;
-  }
-
-  logMask = transp->logMask;
-
-  node = writeToLocalNode(logger_list, transp);
-
-  ret = 0;
-
-  while (node != list_head(&logbuf.head)) {
-    struct LogBufferElement* element;
-    log_id_t logId;
-
-    node = node->prev;
-    element = node_to_item(node, struct LogBufferElement, node);
-    logId = element->logId;
-
-    if (logMask & (1 << logId)) {
-      ret = log_msg->entry_v3.len = element->len;
-      log_msg->entry_v3.hdr_size = sizeof(log_msg->entry_v3);
-      log_msg->entry_v3.pid = getpid();
-      log_msg->entry_v3.tid = element->tid;
-      log_msg->entry_v3.sec = element->timestamp.tv_sec;
-      log_msg->entry_v3.nsec = element->timestamp.tv_nsec;
-      log_msg->entry_v3.lid = logId;
-      memcpy(log_msg->entry_v3.msg, element->msg, ret);
-      ret += log_msg->entry_v3.hdr_size;
-      break;
-    }
-  }
-
-  transp->context.node = node;
-
-  /* ToDo: if blocking, and no entry, put reader to sleep */
-  pthread_rwlock_unlock(&logbuf.listLock);
-  return ret;
-}
-
-static int writeToLocalPoll(struct android_log_logger_list* logger_list,
-                            struct android_log_transport_context* transp) {
-  int ret = (logger_list->mode & ANDROID_LOG_NONBLOCK) ? -ENODEV : 0;
-
-  pthread_rwlock_rdlock(&logbuf.listLock);
-
-  if (logbuf.serviceName) {
-    unsigned logMask = transp->logMask;
-    struct listnode* node = writeToLocalNode(logger_list, transp);
-
-    ret = (node != list_head(&logbuf.head));
-    if (ret) {
-      do {
-        ret = !!(logMask &
-                 (1 << (node_to_item(node->prev, struct LogBufferElement, node))
-                           ->logId));
-      } while (!ret && ((node = node->prev) != list_head(&logbuf.head)));
-    }
-
-    transp->context.node = node;
-  }
-
-  pthread_rwlock_unlock(&logbuf.listLock);
-
-  return ret;
-}
-
-static void writeToLocalClose(struct android_log_logger_list* logger_list
-                                  __unused,
-                              struct android_log_transport_context* transp) {
-  pthread_rwlock_wrlock(&logbuf.listLock);
-  transp->context.node = list_head(&logbuf.head);
-  pthread_rwlock_unlock(&logbuf.listLock);
-}
-
-static int writeToLocalClear(struct android_log_logger* logger,
-                             struct android_log_transport_context* unused
-                                 __unused) {
-  log_id_t logId = logger->logId;
-  struct listnode *node, *n;
-
-  if ((logId >= NUMBER_OF_LOG_BUFFERS) || BLOCK_LOG_BUFFERS(logId)) {
-    return -EINVAL;
-  }
-
-  pthread_rwlock_wrlock(&logbuf.listLock);
-  logbuf.number[logId] = 0;
-  logbuf.last[logId] = &logbuf.head;
-  list_for_each_safe(node, n, &logbuf.head) {
-    struct LogBufferElement* element;
-    element = node_to_item(node, struct LogBufferElement, node);
-
-    if (logId == element->logId) {
-      struct android_log_logger_list* logger_list;
-
-      logger_list_rdlock();
-      logger_list_for_each(logger_list) {
-        struct android_log_transport_context* transp;
-
-        transport_context_for_each(transp, logger_list) {
-          if ((transp->transport == &localLoggerRead) &&
-              (transp->context.node == node)) {
-            transp->context.node = node->next;
-          }
-        }
-      }
-      logger_list_unlock();
-      list_remove(node);
-      free(element);
-    }
-  }
-
-  pthread_rwlock_unlock(&logbuf.listLock);
-
-  return 0;
-}
-
-static ssize_t writeToLocalGetSize(struct android_log_logger* logger,
-                                   struct android_log_transport_context* transp
-                                       __unused) {
-  ssize_t ret = -EINVAL;
-  log_id_t logId = logger->logId;
-
-  if ((logId < NUMBER_OF_LOG_BUFFERS) && !BLOCK_LOG_BUFFERS(logId)) {
-    pthread_rwlock_rdlock(&logbuf.listLock);
-    ret = logbuf.maxSize[logId];
-    pthread_rwlock_unlock(&logbuf.listLock);
-  }
-
-  return ret;
-}
-
-static ssize_t writeToLocalSetSize(
-    struct android_log_logger* logger,
-    struct android_log_transport_context* transp __unused, size_t size) {
-  ssize_t ret = -EINVAL;
-
-  if ((size > LOGGER_ENTRY_MAX_LEN) || (size < (4 * 1024 * 1024))) {
-    log_id_t logId = logger->logId;
-    if ((logId < NUMBER_OF_LOG_BUFFERS) || !BLOCK_LOG_BUFFERS(logId)) {
-      pthread_rwlock_wrlock(&logbuf.listLock);
-      ret = logbuf.maxSize[logId] = size;
-      pthread_rwlock_unlock(&logbuf.listLock);
-    }
-  }
-
-  return ret;
-}
-
-static ssize_t writeToLocalGetReadbleSize(
-    struct android_log_logger* logger,
-    struct android_log_transport_context* transp __unused) {
-  ssize_t ret = -EINVAL;
-  log_id_t logId = logger->logId;
-
-  if ((logId < NUMBER_OF_LOG_BUFFERS) && !BLOCK_LOG_BUFFERS(logId)) {
-    pthread_rwlock_rdlock(&logbuf.listLock);
-    ret = logbuf.serviceName ? (ssize_t)logbuf.size[logId] : -EBADF;
-    pthread_rwlock_unlock(&logbuf.listLock);
-  }
-
-  return ret;
-}
diff --git a/liblog/log_event_list.c b/liblog/log_event_list.cpp
similarity index 88%
rename from liblog/log_event_list.c
rename to liblog/log_event_list.cpp
index 14002ce..088ea94 100644
--- a/liblog/log_event_list.c
+++ b/liblog/log_event_list.cpp
@@ -29,21 +29,26 @@
 
 #define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(int32_t))
 
-typedef struct {
+enum ReadWriteFlag {
+  kAndroidLoggerRead = 1,
+  kAndroidLoggerWrite = 2,
+};
+
+struct android_log_context_internal {
   uint32_t tag;
-  unsigned pos; /* Read/write position into buffer */
+  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;
+  ReadWriteFlag read_write_flag;
   uint8_t storage[LOGGER_ENTRY_MAX_PAYLOAD];
-} android_log_context_internal;
+};
+
+// TODO(tomcherry): real C++ structs.
+typedef struct android_log_context_internal android_log_context_internal;
 
 static void init_context(android_log_context_internal* context, uint32_t tag) {
   size_t needed;
@@ -60,8 +65,8 @@
   context->pos += needed;
 }
 
-static void init_parser_context(android_log_context_internal* context,
-                                const char* msg, size_t len) {
+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);
@@ -71,7 +76,8 @@
 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));
+  context =
+      static_cast<android_log_context_internal*>(calloc(1, sizeof(android_log_context_internal)));
   if (!context) {
     return NULL;
   }
@@ -80,12 +86,12 @@
   return (android_log_context)context;
 }
 
-LIBLOG_ABI_PUBLIC android_log_context create_android_log_parser(const char* msg,
-                                                                size_t len) {
+LIBLOG_ABI_PUBLIC android_log_context create_android_log_parser(const char* msg, size_t len) {
   android_log_context_internal* context;
   size_t i;
 
-  context = calloc(1, sizeof(android_log_context_internal));
+  context =
+      static_cast<android_log_context_internal*>(calloc(1, sizeof(android_log_context_internal)));
   if (!context) {
     return NULL;
   }
@@ -123,8 +129,8 @@
   return 0;
 }
 
-LIBLOG_ABI_PUBLIC int android_log_parser_reset(android_log_context ctx,
-                                               const char* msg, size_t len) {
+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;
@@ -138,7 +144,6 @@
   return 0;
 }
 
-
 LIBLOG_ABI_PUBLIC int android_log_write_list_begin(android_log_context ctx) {
   size_t needed;
   android_log_context_internal* context;
@@ -180,8 +185,7 @@
   buf[3] = (val >> 24) & 0xFF;
 }
 
-LIBLOG_ABI_PUBLIC int android_log_write_int32(android_log_context ctx,
-                                              int32_t value) {
+LIBLOG_ABI_PUBLIC int android_log_write_int32(android_log_context ctx, int32_t value) {
   size_t needed;
   android_log_context_internal* context;
 
@@ -215,8 +219,7 @@
   buf[7] = (val >> 56) & 0xFF;
 }
 
-LIBLOG_ABI_PUBLIC int android_log_write_int64(android_log_context ctx,
-                                              int64_t value) {
+LIBLOG_ABI_PUBLIC int android_log_write_int64(android_log_context ctx, int64_t value) {
   size_t needed;
   android_log_context_internal* context;
 
@@ -239,8 +242,7 @@
   return 0;
 }
 
-LIBLOG_ABI_PUBLIC int android_log_write_string8_len(android_log_context ctx,
-                                                    const char* value,
+LIBLOG_ABI_PUBLIC int android_log_write_string8_len(android_log_context ctx, const char* value,
                                                     size_t maxlen) {
   size_t needed;
   ssize_t len;
@@ -276,13 +278,11 @@
   return len;
 }
 
-LIBLOG_ABI_PUBLIC int android_log_write_string8(android_log_context ctx,
-                                                const char* value) {
+LIBLOG_ABI_PUBLIC int android_log_write_string8(android_log_context ctx, const char* value) {
   return android_log_write_string8_len(ctx, value, MAX_EVENT_PAYLOAD);
 }
 
-LIBLOG_ABI_PUBLIC int android_log_write_float32(android_log_context ctx,
-                                                float value) {
+LIBLOG_ABI_PUBLIC int android_log_write_float32(android_log_context ctx, float value) {
   size_t needed;
   uint32_t ivalue;
   android_log_context_internal* context;
@@ -337,8 +337,7 @@
 /*
  * Logs the list of elements to the event log.
  */
-LIBLOG_ABI_PUBLIC int android_log_write_list(android_log_context ctx,
-                                             log_id_t id) {
+LIBLOG_ABI_PUBLIC int android_log_write_list(android_log_context ctx, log_id_t id) {
   android_log_context_internal* context;
   const char* msg;
   ssize_t len;
@@ -368,13 +367,11 @@
   }
   return (id == LOG_ID_EVENTS)
              ? __android_log_bwrite(context->tag, msg, len)
-             : ((id == LOG_ID_STATS)
-                    ? __android_log_stats_bwrite(context->tag, msg, len)
-                    : __android_log_security_bwrite(context->tag, msg, len));
+             : ((id == LOG_ID_STATS) ? __android_log_stats_bwrite(context->tag, msg, len)
+                                     : __android_log_security_bwrite(context->tag, msg, len));
 }
 
-LIBLOG_ABI_PRIVATE int android_log_write_list_buffer(android_log_context ctx,
-                                                     const char** buffer) {
+LIBLOG_ABI_PRIVATE int android_log_write_list_buffer(android_log_context ctx, const char** buffer) {
   android_log_context_internal* context;
   const char* msg;
   ssize_t len;
@@ -428,8 +425,7 @@
  * this and continues to call this function, the behavior is undefined
  * (although it won't crash).
  */
-static android_log_list_element android_log_read_next_internal(
-    android_log_context ctx, int peek) {
+static android_log_list_element android_log_read_next_internal(android_log_context ctx, int peek) {
   android_log_list_element elem;
   unsigned pos;
   android_log_context_internal* context;
@@ -444,9 +440,9 @@
       (context->count[context->list_nest_depth] >=
        (MAX_EVENT_PAYLOAD / (sizeof(uint8_t) + sizeof(uint8_t))))) {
     elem.type = EVENT_TYPE_UNKNOWN;
-    if (context && (context->list_stop ||
-                    ((context->list_nest_depth <= ANDROID_MAX_LIST_NEST_DEPTH) &&
-                     !context->count[context->list_nest_depth]))) {
+    if (context &&
+        (context->list_stop || ((context->list_nest_depth <= ANDROID_MAX_LIST_NEST_DEPTH) &&
+                                !context->count[context->list_nest_depth]))) {
       elem.type = EVENT_TYPE_LIST_STOP;
     }
     elem.complete = true;
@@ -460,9 +456,8 @@
   pos = context->pos;
   if (context->list_stop) {
     elem.type = EVENT_TYPE_LIST_STOP;
-    elem.complete = !context->count[0] &&
-                    (!context->list_nest_depth ||
-                     ((context->list_nest_depth == 1) && !context->count[1]));
+    elem.complete = !context->count[0] && (!context->list_nest_depth ||
+                                           ((context->list_nest_depth == 1) && !context->count[1]));
     if (!peek) {
       /* Suck in superfluous stop */
       if (context->storage[pos] == EVENT_TYPE_LIST_STOP) {
@@ -485,7 +480,7 @@
     return elem;
   }
 
-  elem.type = context->storage[pos++];
+  elem.type = static_cast<AndroidEventLogType>(context->storage[pos++]);
   switch ((int)elem.type) {
     case EVENT_TYPE_FLOAT:
     /* Rely on union to translate elem.data.int32 into elem.data.float32 */
@@ -598,12 +593,10 @@
   }
 }
 
-LIBLOG_ABI_PUBLIC android_log_list_element
-android_log_read_next(android_log_context ctx) {
+LIBLOG_ABI_PUBLIC android_log_list_element android_log_read_next(android_log_context ctx) {
   return android_log_read_next_internal(ctx, 0);
 }
 
-LIBLOG_ABI_PUBLIC android_log_list_element
-android_log_peek_next(android_log_context ctx) {
+LIBLOG_ABI_PUBLIC android_log_list_element android_log_peek_next(android_log_context ctx) {
   return android_log_read_next_internal(ctx, 1);
 }
diff --git a/liblog/log_event_write.c b/liblog/log_event_write.cpp
similarity index 89%
rename from liblog/log_event_write.c
rename to liblog/log_event_write.cpp
index 45a6f37..e644a3b 100644
--- a/liblog/log_event_write.c
+++ b/liblog/log_event_write.cpp
@@ -24,9 +24,8 @@
 
 #define MAX_SUBTAG_LEN 32
 
-LIBLOG_ABI_PUBLIC int __android_log_error_write(int tag, const char* subTag,
-                                                int32_t uid, const char* data,
-                                                uint32_t dataLen) {
+LIBLOG_ABI_PUBLIC int __android_log_error_write(int tag, const char* subTag, int32_t uid,
+                                                const char* data, uint32_t dataLen) {
   int ret = -EINVAL;
 
   if (subTag && (data || !dataLen)) {
diff --git a/liblog/log_portability.h b/liblog/log_portability.h
index 88805c7..b9fb1d2 100644
--- a/liblog/log_portability.h
+++ b/liblog/log_portability.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LIBLOG_PORTABILITY_H__
-#define _LIBLOG_PORTABILITY_H__
+#pragma once
 
 #include <sys/cdefs.h>
 #include <unistd.h>
@@ -46,7 +45,7 @@
 #if defined(_WIN32)
 #define LIBLOG_WEAK static /* Accept that it is totally private */
 #else
-#define LIBLOG_WEAK __attribute__((weak, visibility("default")))
+#define LIBLOG_WEAK extern "C" __attribute__((weak, visibility("default")))
 #endif
 
 /* possible missing definitions in sys/cdefs.h */
@@ -62,11 +61,6 @@
 #endif
 #endif
 
-/* Unused argument. For C code only, remove symbol name for C++ */
-#ifndef __unused
-#define __unused __attribute__((__unused__))
-#endif
-
 /* possible missing definitions in unistd.h */
 
 #ifndef TEMP_FAILURE_RETRY
@@ -80,5 +74,3 @@
     _rc;                                   \
   })
 #endif
-
-#endif /* _LIBLOG_PORTABILITY_H__ */
diff --git a/liblog/log_ratelimit.cpp b/liblog/log_ratelimit.cpp
deleted file mode 100644
index 33770dd..0000000
--- a/liblog/log_ratelimit.cpp
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
-** Copyright 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 <pthread.h>
-#include <time.h>
-
-#include <log/log.h>
-
-#include "log_portability.h"
-
-// Global default if 'last' argument in __android_log_ratelimit is NULL
-static time_t g_last_clock;
-// Global above can not deal well with callers playing games with the
-// seconds argument, so we will also hold on to the maximum value
-// ever provided and use that to gain consistency.  If the caller
-// provides their own 'last' argument, then they can play such games
-// of varying the 'seconds' argument to their pleasure.
-static time_t g_last_seconds;
-static const time_t last_seconds_default = 10;
-static const time_t last_seconds_max = 24 * 60 * 60;  // maximum of a day
-static const time_t last_seconds_min = 2;             // granularity
-// Lock to protect last_clock and last_seconds, but also 'last'
-// argument (not NULL) as supplied to __android_log_ratelimit.
-static pthread_mutex_t lock_ratelimit = PTHREAD_MUTEX_INITIALIZER;
-
-// if last is NULL, caller _must_ provide a consistent value for
-// seconds, otherwise we will take the maximum ever issued and hold
-// on to that.  Preserves value of non-zero errno.  Return -1 if we
-// can not acquire a lock, 0 if we are not to log a message, and 1
-// if we are ok to log a message.  Caller should check > 0 for true.
-LIBLOG_ABI_PUBLIC int __android_log_ratelimit(time_t seconds, time_t* last) {
-  int save_errno = errno;
-
-  // Two reasons for trylock failure:
-  //   1. In a signal handler. Must prevent deadlock
-  //   2. Too many threads calling __android_log_ratelimit.
-  //      Bonus to not print if they race here because that
-  //      dovetails the goal of ratelimiting. One may print
-  //      and the others will wait their turn ...
-  if (pthread_mutex_trylock(&lock_ratelimit)) {
-    if (save_errno) errno = save_errno;
-    return -1;
-  }
-
-  if (seconds == 0) {
-    seconds = last_seconds_default;
-  } else if (seconds < last_seconds_min) {
-    seconds = last_seconds_min;
-  } else if (seconds > last_seconds_max) {
-    seconds = last_seconds_max;
-  }
-
-  if (!last) {
-    if (g_last_seconds > seconds) {
-      seconds = g_last_seconds;
-    } else if (g_last_seconds < seconds) {
-      g_last_seconds = seconds;
-    }
-    last = &g_last_clock;
-  }
-
-  time_t now = time(NULL);
-  if ((now == (time_t)-1) || ((*last + seconds) > now)) {
-    pthread_mutex_unlock(&lock_ratelimit);
-    if (save_errno) errno = save_errno;
-    return 0;
-  }
-  *last = now;
-  pthread_mutex_unlock(&lock_ratelimit);
-  if (save_errno) errno = save_errno;
-  return 1;
-}
diff --git a/liblog/log_time.cpp b/liblog/log_time.cpp
index ae376be..77bb94f 100644
--- a/liblog/log_time.cpp
+++ b/liblog/log_time.cpp
@@ -137,7 +137,7 @@
 LIBLOG_ABI_PRIVATE log_time log_time::operator-=(const timespec& T) {
   // No concept of negative time, clamp to EPOCH
   if (*this <= T) {
-    return *this = EPOCH;
+    return *this = log_time(EPOCH);
   }
 
   if (this->tv_nsec < (unsigned long int)T.tv_nsec) {
@@ -165,7 +165,7 @@
 LIBLOG_ABI_PRIVATE log_time log_time::operator-=(const log_time& T) {
   // No concept of negative time, clamp to EPOCH
   if (*this <= T) {
-    return *this = EPOCH;
+    return *this = log_time(EPOCH);
   }
 
   if (this->tv_nsec < T.tv_nsec) {
diff --git a/liblog/logd_reader.c b/liblog/logd_reader.cpp
similarity index 86%
rename from liblog/logd_reader.c
rename to liblog/logd_reader.cpp
index 603ba24..05bbcbc 100644
--- a/liblog/logd_reader.c
+++ b/liblog/logd_reader.cpp
@@ -47,8 +47,7 @@
 static int logdVersion(struct android_log_logger* logger,
                        struct android_log_transport_context* transp);
 static int logdRead(struct android_log_logger_list* logger_list,
-                    struct android_log_transport_context* transp,
-                    struct log_msg* log_msg);
+                    struct android_log_transport_context* transp, struct log_msg* log_msg);
 static int logdPoll(struct android_log_logger_list* logger_list,
                     struct android_log_transport_context* transp);
 static void logdClose(struct android_log_logger_list* logger_list,
@@ -56,37 +55,33 @@
 static int logdClear(struct android_log_logger* logger,
                      struct android_log_transport_context* transp);
 static ssize_t logdSetSize(struct android_log_logger* logger,
-                           struct android_log_transport_context* transp,
-                           size_t size);
+                           struct android_log_transport_context* transp, size_t size);
 static ssize_t logdGetSize(struct android_log_logger* logger,
                            struct android_log_transport_context* transp);
 static ssize_t logdGetReadableSize(struct android_log_logger* logger,
                                    struct android_log_transport_context* transp);
 static ssize_t logdGetPrune(struct android_log_logger_list* logger,
-                            struct android_log_transport_context* transp,
-                            char* buf, size_t len);
+                            struct android_log_transport_context* transp, char* buf, size_t len);
 static ssize_t logdSetPrune(struct android_log_logger_list* logger,
-                            struct android_log_transport_context* transp,
-                            char* buf, size_t len);
+                            struct android_log_transport_context* transp, char* buf, size_t len);
 static ssize_t logdGetStats(struct android_log_logger_list* logger,
-                            struct android_log_transport_context* transp,
-                            char* buf, size_t len);
+                            struct android_log_transport_context* transp, char* buf, size_t len);
 
 LIBLOG_HIDDEN struct android_log_transport_read logdLoggerRead = {
-  .node = { &logdLoggerRead.node, &logdLoggerRead.node },
-  .name = "logd",
-  .available = logdAvailable,
-  .version = logdVersion,
-  .read = logdRead,
-  .poll = logdPoll,
-  .close = logdClose,
-  .clear = logdClear,
-  .getSize = logdGetSize,
-  .setSize = logdSetSize,
-  .getReadableSize = logdGetReadableSize,
-  .getPrune = logdGetPrune,
-  .setPrune = logdSetPrune,
-  .getStats = logdGetStats,
+    .node = {&logdLoggerRead.node, &logdLoggerRead.node},
+    .name = "logd",
+    .available = logdAvailable,
+    .version = logdVersion,
+    .read = logdRead,
+    .poll = logdPoll,
+    .close = logdClose,
+    .clear = logdClear,
+    .getSize = logdGetSize,
+    .setSize = logdSetSize,
+    .getReadableSize = logdGetReadableSize,
+    .getPrune = logdGetPrune,
+    .setPrune = logdSetPrune,
+    .getStats = logdGetStats,
 };
 
 static int logdAvailable(log_id_t logId) {
@@ -109,8 +104,7 @@
 
 #if defined(_WIN32)
 
-LIBLOG_WEAK int socket_local_client(const char* name, int namespaceId,
-                                    int type) {
+LIBLOG_WEAK int socket_local_client(const char* name, int namespaceId, int type) {
   errno = ENOSYS;
   return -ENOSYS;
 }
@@ -131,8 +125,7 @@
 
 /* Documented in header file. */
 LIBLOG_WEAK int socket_make_sockaddr_un(const char* name, int namespaceId,
-                                        struct sockaddr_un* p_addr,
-                                        socklen_t* alen) {
+                                        struct sockaddr_un* p_addr, socklen_t* alen) {
   memset(p_addr, 0, sizeof(*p_addr));
   size_t namelen;
 
@@ -158,8 +151,7 @@
 
       namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
       /* unix_path_max appears to be missing on linux */
-      if (namelen >
-          sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
+      if (namelen > sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
         goto error;
       }
 
@@ -171,8 +163,7 @@
     case ANDROID_SOCKET_NAMESPACE_RESERVED:
       namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
       /* unix_path_max appears to be missing on linux */
-      if (namelen >
-          sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
+      if (namelen > sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
         goto error;
       }
 
@@ -183,8 +174,7 @@
     case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
       namelen = strlen(name);
       /* unix_path_max appears to be missing on linux */
-      if (namelen >
-          sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
+      if (namelen > sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
         goto error;
       }
 
@@ -210,8 +200,8 @@
  *
  * Used by AndroidSocketImpl
  */
-LIBLOG_WEAK int socket_local_client_connect(int fd, const char* name,
-                                            int namespaceId, int type __unused) {
+LIBLOG_WEAK int socket_local_client_connect(int fd, const char* name, int namespaceId,
+                                            int type __unused) {
   struct sockaddr_un addr;
   socklen_t alen;
   int err;
@@ -236,8 +226,7 @@
  * connect to peer named "name"
  * returns fd or -1 on error
  */
-LIBLOG_WEAK int socket_local_client(const char* name, int namespaceId,
-                                    int type) {
+LIBLOG_WEAK int socket_local_client(const char* name, int namespaceId, int type) {
   int s;
 
   s = socket(AF_LOCAL, type, 0);
@@ -255,14 +244,13 @@
 /* End of ../libcutils/socket_local_client.c */
 
 /* worker for sending the command to the logger */
-static ssize_t send_log_msg(struct android_log_logger* logger, const char* msg,
-                            char* buf, size_t buf_size) {
+static ssize_t send_log_msg(struct android_log_logger* logger, const char* msg, char* buf,
+                            size_t buf_size) {
   ssize_t ret;
   size_t len;
   char* cp;
   int errno_save = 0;
-  int sock = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED,
-                                 SOCK_STREAM);
+  int sock = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
   if (sock < 0) {
     return sock;
   }
@@ -342,8 +330,7 @@
                      struct android_log_transport_context* transp __unused) {
   char buf[512];
 
-  return check_log_success(buf,
-                           send_log_msg(logger, "clear %d", buf, sizeof(buf)));
+  return check_log_success(buf, send_log_msg(logger, "clear %d", buf, sizeof(buf)));
 }
 
 /* returns the total size of the log's ring buffer */
@@ -364,8 +351,7 @@
 }
 
 static ssize_t logdSetSize(struct android_log_logger* logger,
-                           struct android_log_transport_context* transp __unused,
-                           size_t size) {
+                           struct android_log_transport_context* transp __unused, size_t size) {
   char buf[512];
 
   snprintf(buf, sizeof(buf), "setLogSize %d %zu", logger->logId, size);
@@ -378,8 +364,7 @@
  * log consumed)
  */
 static ssize_t logdGetReadableSize(struct android_log_logger* logger,
-                                   struct android_log_transport_context* transp
-                                       __unused) {
+                                   struct android_log_transport_context* transp __unused) {
   char buf[512];
 
   ssize_t ret = send_log_msg(logger, "getLogSizeUsed %d", buf, sizeof(buf));
@@ -407,8 +392,8 @@
  * returns statistics
  */
 static ssize_t logdGetStats(struct android_log_logger_list* logger_list,
-                            struct android_log_transport_context* transp __unused,
-                            char* buf, size_t len) {
+                            struct android_log_transport_context* transp __unused, char* buf,
+                            size_t len) {
   struct android_log_logger* logger;
   char* cp = buf;
   size_t remaining = len;
@@ -434,14 +419,14 @@
 }
 
 static ssize_t logdGetPrune(struct android_log_logger_list* logger_list __unused,
-                            struct android_log_transport_context* transp __unused,
-                            char* buf, size_t len) {
+                            struct android_log_transport_context* transp __unused, char* buf,
+                            size_t len) {
   return send_log_msg(NULL, "getPruneList", buf, len);
 }
 
 static ssize_t logdSetPrune(struct android_log_logger_list* logger_list __unused,
-                            struct android_log_transport_context* transp __unused,
-                            char* buf, size_t len) {
+                            struct android_log_transport_context* transp __unused, char* buf,
+                            size_t len) {
   const char cmd[] = "setPruneList ";
   const size_t cmdlen = sizeof(cmd) - 1;
 
@@ -455,8 +440,7 @@
   return check_log_success(buf, send_log_msg(NULL, NULL, buf, len));
 }
 
-static void caught_signal(int signum __unused) {
-}
+static void caught_signal(int signum __unused) {}
 
 static int logdOpen(struct android_log_logger_list* logger_list,
                     struct android_log_transport_context* transp) {
@@ -476,12 +460,10 @@
     return sock;
   }
 
-  sock = socket_local_client("logdr", ANDROID_SOCKET_NAMESPACE_RESERVED,
-                             SOCK_SEQPACKET);
+  sock = socket_local_client("logdr", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET);
   if (sock == 0) {
     /* Guarantee not file descriptor zero */
-    int newsock = socket_local_client(
-        "logdr", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET);
+    int newsock = socket_local_client("logdr", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET);
     close(sock);
     sock = newsock;
   }
@@ -492,8 +474,7 @@
     return sock;
   }
 
-  strcpy(buffer, (logger_list->mode & ANDROID_LOG_NONBLOCK) ? "dumpAndClose"
-                                                            : "stream");
+  strcpy(buffer, (logger_list->mode & ANDROID_LOG_NONBLOCK) ? "dumpAndClose" : "stream");
   cp = buffer + strlen(buffer);
 
   strcpy(cp, " lids");
@@ -518,14 +499,13 @@
   if (logger_list->start.tv_sec || logger_list->start.tv_nsec) {
     if (logger_list->mode & ANDROID_LOG_WRAP) {
       // ToDo: alternate API to allow timeout to be adjusted.
-      ret = snprintf(cp, remaining, " timeout=%u",
-                     ANDROID_LOG_WRAP_DEFAULT_TIMEOUT);
+      ret = snprintf(cp, remaining, " timeout=%u", ANDROID_LOG_WRAP_DEFAULT_TIMEOUT);
       ret = min(ret, remaining);
       remaining -= ret;
       cp += ret;
     }
-    ret = snprintf(cp, remaining, " start=%" PRIu32 ".%09" PRIu32,
-                   logger_list->start.tv_sec, logger_list->start.tv_nsec);
+    ret = snprintf(cp, remaining, " start=%" PRIu32 ".%09" PRIu32, logger_list->start.tv_sec,
+                   logger_list->start.tv_nsec);
     ret = min(ret, remaining);
     remaining -= ret;
     cp += ret;
@@ -576,8 +556,7 @@
 
 /* Read from the selected logs */
 static int logdRead(struct android_log_logger_list* logger_list,
-                    struct android_log_transport_context* transp,
-                    struct log_msg* log_msg) {
+                    struct android_log_transport_context* transp, struct log_msg* log_msg) {
   int ret, e;
   struct sigaction ignore;
   struct sigaction old_sigaction;
diff --git a/liblog/logd_reader.h b/liblog/logd_reader.h
index 8ebb1ae..0bba7cf 100644
--- a/liblog/logd_reader.h
+++ b/liblog/logd_reader.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef _LIBLOG_LOGD_READER_H__
-#define _LIBLOG_LOGD_READER_H__
+#pragma once
 
 #include <unistd.h>
 
@@ -26,5 +25,3 @@
 LIBLOG_HIDDEN ssize_t __send_log_msg(char* buf, size_t buf_size);
 
 __END_DECLS
-
-#endif /* _LIBLOG_LOGD_READER_H__ */
diff --git a/liblog/logd_writer.c b/liblog/logd_writer.cpp
similarity index 85%
rename from liblog/logd_writer.c
rename to liblog/logd_writer.cpp
index e0e3eca..ed906b3 100644
--- a/liblog/logd_writer.c
+++ b/liblog/logd_writer.cpp
@@ -38,6 +38,7 @@
 #include "config_write.h"
 #include "log_portability.h"
 #include "logger.h"
+#include "uio.h"
 
 /* branchless on many architectures. */
 #define min(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
@@ -45,17 +46,16 @@
 static int logdAvailable(log_id_t LogId);
 static int logdOpen();
 static void logdClose();
-static int logdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
-                     size_t nr);
+static int logdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr);
 
 LIBLOG_HIDDEN struct android_log_transport_write logdLoggerWrite = {
-  .node = { &logdLoggerWrite.node, &logdLoggerWrite.node },
-  .context.sock = -EBADF,
-  .name = "logd",
-  .available = logdAvailable,
-  .open = logdOpen,
-  .close = logdClose,
-  .write = logdWrite,
+    .node = {&logdLoggerWrite.node, &logdLoggerWrite.node},
+    .context.sock = -EBADF,
+    .name = "logd",
+    .available = logdAvailable,
+    .open = logdOpen,
+    .close = logdClose,
+    .write = logdWrite,
 };
 
 /* log_init_lock assumed */
@@ -64,8 +64,7 @@
 
   i = atomic_load(&logdLoggerWrite.context.sock);
   if (i < 0) {
-    int sock = TEMP_FAILURE_RETRY(
-        socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
+    int sock = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
     if (sock < 0) {
       ret = -errno;
     } else {
@@ -74,15 +73,15 @@
       un.sun_family = AF_UNIX;
       strcpy(un.sun_path, "/dev/socket/logdw");
 
-      if (TEMP_FAILURE_RETRY(connect(sock, (struct sockaddr*)&un,
-                                     sizeof(struct sockaddr_un))) < 0) {
+      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(&logdLoggerWrite.context.sock, ret);
-          /* FALLTHRU */
+            [[fallthrough]];
           default:
             break;
         }
@@ -124,16 +123,15 @@
   return 1;
 }
 
-static int logdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
-                     size_t nr) {
+static int logdWrite(log_id_t logId, 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_fast32_t dropped;
-  static atomic_int_fast32_t droppedSecurity;
+  static atomic_int dropped;
+  static atomic_int droppedSecurity;
 
   sock = atomic_load(&logdLoggerWrite.context.sock);
   if (sock < 0) switch (sock) {
@@ -181,8 +179,7 @@
   newVec[0].iov_len = sizeof(header);
 
   if (sock >= 0) {
-    int32_t snapshot =
-        atomic_exchange_explicit(&droppedSecurity, 0, memory_order_relaxed);
+    int32_t snapshot = atomic_exchange_explicit(&droppedSecurity, 0, memory_order_relaxed);
     if (snapshot) {
       android_log_event_int_t buffer;
 
@@ -196,14 +193,12 @@
 
       ret = TEMP_FAILURE_RETRY(writev(sock, newVec, 2));
       if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
-        atomic_fetch_add_explicit(&droppedSecurity, snapshot,
-                                  memory_order_relaxed);
+        atomic_fetch_add_explicit(&droppedSecurity, snapshot, memory_order_relaxed);
       }
     }
     snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
-    if (snapshot &&
-        __android_log_is_loggable_len(ANDROID_LOG_INFO, "liblog",
-                                      strlen("liblog"), ANDROID_LOG_VERBOSE)) {
+    if (snapshot && __android_log_is_loggable_len(ANDROID_LOG_INFO, "liblog", strlen("liblog"),
+                                                  ANDROID_LOG_VERBOSE)) {
       android_log_event_int_t buffer;
 
       header.id = LOG_ID_EVENTS;
@@ -267,12 +262,11 @@
         return ret;
       }
 
-      ret = TEMP_FAILURE_RETRY(
-          writev(atomic_load(&logdLoggerWrite.context.sock), newVec, i));
+      ret = TEMP_FAILURE_RETRY(writev(atomic_load(&logdLoggerWrite.context.sock), newVec, i));
       if (ret < 0) {
         ret = -errno;
       }
-    /* FALLTHRU */
+      [[fallthrough]];
     default:
       break;
   }
diff --git a/liblog/logger.h b/liblog/logger.h
index 246b33c..b2479d2 100644
--- a/liblog/logger.h
+++ b/liblog/logger.h
@@ -14,22 +14,21 @@
  * limitations under the License.
  */
 
-#ifndef _LIBLOG_LOGGER_H__
-#define _LIBLOG_LOGGER_H__
+#pragma once
 
 #include <stdatomic.h>
 #include <stdbool.h>
 
 #include <cutils/list.h>
 #include <log/log.h>
-#include <log/uio.h>
 
 #include "log_portability.h"
+#include "uio.h"
 
 __BEGIN_DECLS
 
 /* Union, sock or fd of zero is not allowed unless static initialized */
-union android_log_context {
+union android_log_context_union {
   void* priv;
   atomic_int sock;
   atomic_int fd;
@@ -41,7 +40,7 @@
   struct listnode node;
   const char* name;                  /* human name to describe the transport */
   unsigned logMask;                  /* mask cache of available() success */
-  union android_log_context context; /* Initialized by static allocation */
+  union android_log_context_union context; /* Initialized by static allocation */
 
   int (*available)(log_id_t logId); /* Does not cause resources to be taken */
   int (*open)();   /* can be called multiple times, reusing current resources */
@@ -98,7 +97,6 @@
 };
 
 struct android_log_logger_list {
-  struct listnode node;
   struct listnode logger;
   struct listnode transport;
   int mode;
@@ -116,7 +114,7 @@
 
 struct android_log_transport_context {
   struct listnode node;
-  union android_log_context context; /* zero init per-transport context */
+  union android_log_context_union context; /* zero init per-transport context */
   struct android_log_logger_list* parent;
 
   struct android_log_transport_read* transport;
@@ -144,37 +142,6 @@
        (logp) =                                                     \
            node_to_item((logp)->node.next, struct android_log_logger, node))
 
-/*
- *    Global list of log readers.
- *
- * Usage case: search out transport contexts for all readers
- */
-
-LIBLOG_HIDDEN struct listnode __android_log_readers;
-
-#if defined(_WIN32)
-#define logger_list_rdlock()
-#define logger_list_wrlock()
-#define logger_list_unlock()
-#else
-LIBLOG_HIDDEN pthread_rwlock_t __android_log_readers_lock;
-
-#define logger_list_rdlock() pthread_rwlock_rdlock(&__android_log_readers_lock)
-#define logger_list_wrlock() pthread_rwlock_wrlock(&__android_log_readers_lock)
-#define logger_list_unlock() pthread_rwlock_unlock(&__android_log_readers_lock)
-#endif
-
-/* Must be called with logger_list_rdlock() or logger_list_wrlock() held */
-#define logger_list_for_each(logger_list)                                     \
-  for ((logger_list) = node_to_item(&__android_log_readers,                   \
-                                    struct android_log_logger_list, node);    \
-       (logger_list) != node_to_item(&__android_log_readers,                  \
-                                     struct android_log_logger_list, node) && \
-       (logger_list) != node_to_item((logger_list)->node.next,                \
-                                     struct android_log_logger_list, node);   \
-       (logger_list) = node_to_item((logger_list)->node.next,                 \
-                                    struct android_log_logger_list, node))
-
 /* OS specific dribs and drabs */
 
 #if defined(_WIN32)
@@ -193,8 +160,6 @@
 LIBLOG_HIDDEN int __android_log_trylock();
 LIBLOG_HIDDEN void __android_log_unlock();
 
-LIBLOG_HIDDEN int __android_log_transport;
+extern LIBLOG_HIDDEN int __android_log_transport;
 
 __END_DECLS
-
-#endif /* _LIBLOG_LOGGER_H__ */
diff --git a/liblog/logger_lock.c b/liblog/logger_lock.cpp
similarity index 100%
rename from liblog/logger_lock.c
rename to liblog/logger_lock.cpp
diff --git a/liblog/logger_name.c b/liblog/logger_name.cpp
similarity index 79%
rename from liblog/logger_name.c
rename to liblog/logger_name.cpp
index 479bbfe..c6f3cb7 100644
--- a/liblog/logger_name.c
+++ b/liblog/logger_name.cpp
@@ -15,6 +15,7 @@
 */
 
 #include <string.h>
+#include <type_traits>
 
 #include <log/log.h>
 
@@ -22,7 +23,7 @@
 
 /* In the future, we would like to make this list extensible */
 static const char* LOG_NAME[LOG_ID_MAX] = {
-      /* clang-format off */
+    /* clang-format off */
   [LOG_ID_MAIN] = "main",
   [LOG_ID_RADIO] = "radio",
   [LOG_ID_EVENTS] = "events",
@@ -31,7 +32,7 @@
   [LOG_ID_STATS] = "stats",
   [LOG_ID_SECURITY] = "security",
   [LOG_ID_KERNEL] = "kernel",
-  /* clang-format on */
+    /* clang-format on */
 };
 
 LIBLOG_ABI_PUBLIC const char* android_log_id_to_name(log_id_t log_id) {
@@ -41,12 +42,15 @@
   return LOG_NAME[log_id];
 }
 
+static_assert(std::is_same<std::underlying_type<log_id_t>::type, uint32_t>::value,
+              "log_id_t must be an unsigned int");
+
 LIBLOG_ABI_PUBLIC log_id_t android_name_to_log_id(const char* logName) {
   const char* b;
-  int ret;
+  unsigned int ret;
 
   if (!logName) {
-    return -1; /* NB: log_id_t is unsigned */
+    return static_cast<log_id_t>(0xFFFFFFFF);
   }
   b = strrchr(logName, '/');
   if (!b) {
@@ -58,8 +62,8 @@
   for (ret = LOG_ID_MIN; ret < LOG_ID_MAX; ++ret) {
     const char* l = LOG_NAME[ret];
     if (l && !strcmp(b, l)) {
-      return ret;
+      return static_cast<log_id_t>(ret);
     }
   }
-  return -1; /* should never happen */
+  return static_cast<log_id_t>(0xFFFFFFFF); /* should never happen */
 }
diff --git a/liblog/logger_read.c b/liblog/logger_read.cpp
similarity index 76%
rename from liblog/logger_read.c
rename to liblog/logger_read.cpp
index 0fd6efa..e429c36 100644
--- a/liblog/logger_read.c
+++ b/liblog/logger_read.cpp
@@ -14,6 +14,8 @@
 ** limitations under the License.
 */
 
+#include "log/log_read.h"
+
 #include <errno.h>
 #include <fcntl.h>
 #include <pthread.h>
@@ -34,8 +36,7 @@
 /* android_logger_alloc unimplemented, no use case */
 /* android_logger_free not exported */
 static void android_logger_free(struct logger* logger) {
-  struct android_log_logger* logger_internal =
-      (struct android_log_logger*)logger;
+  struct android_log_logger* logger_internal = (struct android_log_logger*)logger;
 
   if (!logger_internal) {
     return;
@@ -71,15 +72,13 @@
 
   __android_log_lock();
   /* mini __write_to_log_initialize() to populate transports */
-  if (list_empty(&__android_log_transport_read) &&
-      list_empty(&__android_log_persist_read)) {
+  if (list_empty(&__android_log_transport_read) && list_empty(&__android_log_persist_read)) {
     __android_log_config_read();
   }
   __android_log_unlock();
 
-  node = (logger_list->mode & ANDROID_LOG_PSTORE)
-             ? &__android_log_persist_read
-             : &__android_log_transport_read;
+  node = (logger_list->mode & ANDROID_LOG_PSTORE) ? &__android_log_persist_read
+                                                  : &__android_log_transport_read;
 
   read_transport_for_each(transport, node) {
     struct android_log_transport_context* transp;
@@ -92,15 +91,14 @@
       if ((logId == LOG_ID_SECURITY) && (__android_log_uid() != AID_SYSTEM)) {
         continue;
       }
-      if (transport->read &&
-          (!transport->available || (transport->available(logId) >= 0))) {
+      if (transport->read && (!transport->available || (transport->available(logId) >= 0))) {
         logMask |= 1 << logId;
       }
     }
     if (!logMask) {
       continue;
     }
-    transp = calloc(1, sizeof(*transp));
+    transp = static_cast<android_log_transport_context*>(calloc(1, sizeof(*transp)));
     if (!transp) {
       return -ENOMEM;
     }
@@ -116,31 +114,29 @@
   return 0;
 }
 
-#define LOGGER_FUNCTION(logger, def, func, args...)                   \
-  ssize_t ret = -EINVAL;                                              \
-  struct android_log_transport_context* transp;                       \
-  struct android_log_logger* logger_internal =                        \
-      (struct android_log_logger*)(logger);                           \
-                                                                      \
-  if (!logger_internal) {                                             \
-    return ret;                                                       \
-  }                                                                   \
-  ret = init_transport_context(logger_internal->parent);              \
-  if (ret < 0) {                                                      \
-    return ret;                                                       \
-  }                                                                   \
-                                                                      \
-  ret = (def);                                                        \
-  transport_context_for_each(transp, logger_internal->parent) {       \
-    if ((transp->logMask & (1 << logger_internal->logId)) &&          \
-        transp->transport && transp->transport->func) {               \
-      ssize_t retval =                                                \
-          (transp->transport->func)(logger_internal, transp, ##args); \
-      if ((ret >= 0) || (ret == (def))) {                             \
-        ret = retval;                                                 \
-      }                                                               \
-    }                                                                 \
-  }                                                                   \
+#define LOGGER_FUNCTION(logger, def, func, args...)                                  \
+  ssize_t ret = -EINVAL;                                                             \
+  struct android_log_transport_context* transp;                                      \
+  struct android_log_logger* logger_internal = (struct android_log_logger*)(logger); \
+                                                                                     \
+  if (!logger_internal) {                                                            \
+    return ret;                                                                      \
+  }                                                                                  \
+  ret = init_transport_context(logger_internal->parent);                             \
+  if (ret < 0) {                                                                     \
+    return ret;                                                                      \
+  }                                                                                  \
+                                                                                     \
+  ret = (def);                                                                       \
+  transport_context_for_each(transp, logger_internal->parent) {                      \
+    if ((transp->logMask & (1 << logger_internal->logId)) && transp->transport &&    \
+        transp->transport->func) {                                                   \
+      ssize_t retval = (transp->transport->func)(logger_internal, transp, ##args);   \
+      if ((ret >= 0) || (ret == (def))) {                                            \
+        ret = retval;                                                                \
+      }                                                                              \
+    }                                                                                \
+  }                                                                                  \
   return ret
 
 LIBLOG_ABI_PUBLIC int android_logger_clear(struct logger* logger) {
@@ -152,8 +148,7 @@
   LOGGER_FUNCTION(logger, -ENODEV, getSize);
 }
 
-LIBLOG_ABI_PUBLIC int android_logger_set_log_size(struct logger* logger,
-                                                  unsigned long size) {
+LIBLOG_ABI_PUBLIC int android_logger_set_log_size(struct logger* logger, unsigned long size) {
   LOGGER_FUNCTION(logger, -ENODEV, setSize, size);
 }
 
@@ -161,8 +156,7 @@
  * returns the readable size of the log's ring buffer (that is, amount of the
  * log consumed)
  */
-LIBLOG_ABI_PUBLIC long android_logger_get_log_readable_size(
-    struct logger* logger) {
+LIBLOG_ABI_PUBLIC long android_logger_get_log_readable_size(struct logger* logger) {
   LOGGER_FUNCTION(logger, -ENODEV, getReadableSize);
 }
 
@@ -173,58 +167,50 @@
   LOGGER_FUNCTION(logger, 4, version);
 }
 
-#define LOGGER_LIST_FUNCTION(logger_list, def, func, args...)              \
-  struct android_log_transport_context* transp;                            \
-  struct android_log_logger_list* logger_list_internal =                   \
-      (struct android_log_logger_list*)(logger_list);                      \
-                                                                           \
-  ssize_t ret = init_transport_context(logger_list_internal);              \
-  if (ret < 0) {                                                           \
-    return ret;                                                            \
-  }                                                                        \
-                                                                           \
-  ret = (def);                                                             \
-  transport_context_for_each(transp, logger_list_internal) {               \
-    if (transp->transport && (transp->transport->func)) {                  \
-      ssize_t retval =                                                     \
-          (transp->transport->func)(logger_list_internal, transp, ##args); \
-      if ((ret >= 0) || (ret == (def))) {                                  \
-        ret = retval;                                                      \
-      }                                                                    \
-    }                                                                      \
-  }                                                                        \
+#define LOGGER_LIST_FUNCTION(logger_list, def, func, args...)                           \
+  struct android_log_transport_context* transp;                                         \
+  struct android_log_logger_list* logger_list_internal =                                \
+      (struct android_log_logger_list*)(logger_list);                                   \
+                                                                                        \
+  ssize_t ret = init_transport_context(logger_list_internal);                           \
+  if (ret < 0) {                                                                        \
+    return ret;                                                                         \
+  }                                                                                     \
+                                                                                        \
+  ret = (def);                                                                          \
+  transport_context_for_each(transp, logger_list_internal) {                            \
+    if (transp->transport && (transp->transport->func)) {                               \
+      ssize_t retval = (transp->transport->func)(logger_list_internal, transp, ##args); \
+      if ((ret >= 0) || (ret == (def))) {                                               \
+        ret = retval;                                                                   \
+      }                                                                                 \
+    }                                                                                   \
+  }                                                                                     \
   return ret
 
 /*
  * returns statistics
  */
-LIBLOG_ABI_PUBLIC ssize_t android_logger_get_statistics(
-    struct logger_list* logger_list, char* buf, size_t len) {
+LIBLOG_ABI_PUBLIC ssize_t android_logger_get_statistics(struct logger_list* logger_list, char* buf,
+                                                        size_t len) {
   LOGGER_LIST_FUNCTION(logger_list, -ENODEV, getStats, buf, len);
 }
 
-LIBLOG_ABI_PUBLIC ssize_t android_logger_get_prune_list(
-    struct logger_list* logger_list, char* buf, size_t len) {
+LIBLOG_ABI_PUBLIC ssize_t android_logger_get_prune_list(struct logger_list* logger_list, char* buf,
+                                                        size_t len) {
   LOGGER_LIST_FUNCTION(logger_list, -ENODEV, getPrune, buf, len);
 }
 
-LIBLOG_ABI_PUBLIC int android_logger_set_prune_list(
-    struct logger_list* logger_list, char* buf, size_t len) {
+LIBLOG_ABI_PUBLIC int android_logger_set_prune_list(struct logger_list* logger_list, char* buf,
+                                                    size_t len) {
   LOGGER_LIST_FUNCTION(logger_list, -ENODEV, setPrune, buf, len);
 }
 
-LIBLOG_HIDDEN struct listnode __android_log_readers = { &__android_log_readers,
-                                                        &__android_log_readers };
-#if !defined(_WIN32)
-LIBLOG_HIDDEN pthread_rwlock_t __android_log_readers_lock =
-    PTHREAD_RWLOCK_INITIALIZER;
-#endif
-
-LIBLOG_ABI_PUBLIC struct logger_list* android_logger_list_alloc(
-    int mode, unsigned int tail, pid_t pid) {
+LIBLOG_ABI_PUBLIC struct logger_list* android_logger_list_alloc(int mode, unsigned int tail,
+                                                                pid_t pid) {
   struct android_log_logger_list* logger_list;
 
-  logger_list = calloc(1, sizeof(*logger_list));
+  logger_list = static_cast<android_log_logger_list*>(calloc(1, sizeof(*logger_list)));
   if (!logger_list) {
     return NULL;
   }
@@ -235,18 +221,14 @@
   logger_list->tail = tail;
   logger_list->pid = pid;
 
-  logger_list_wrlock();
-  list_add_tail(&__android_log_readers, &logger_list->node);
-  logger_list_unlock();
-
   return (struct logger_list*)logger_list;
 }
 
-LIBLOG_ABI_PUBLIC struct logger_list* android_logger_list_alloc_time(
-    int mode, log_time start, pid_t pid) {
+LIBLOG_ABI_PUBLIC struct logger_list* android_logger_list_alloc_time(int mode, log_time start,
+                                                                     pid_t pid) {
   struct android_log_logger_list* logger_list;
 
-  logger_list = calloc(1, sizeof(*logger_list));
+  logger_list = static_cast<android_log_logger_list*>(calloc(1, sizeof(*logger_list)));
   if (!logger_list) {
     return NULL;
   }
@@ -257,10 +239,6 @@
   logger_list->start = start;
   logger_list->pid = pid;
 
-  logger_list_wrlock();
-  list_add_tail(&__android_log_readers, &logger_list->node);
-  logger_list_unlock();
-
   return (struct logger_list*)logger_list;
 }
 
@@ -268,8 +246,8 @@
 /* android_logger_list_unregister unimplemented, no use case */
 
 /* Open the named log and add it to the logger list */
-LIBLOG_ABI_PUBLIC struct logger* android_logger_open(
-    struct logger_list* logger_list, log_id_t logId) {
+LIBLOG_ABI_PUBLIC struct logger* android_logger_open(struct logger_list* logger_list,
+                                                     log_id_t logId) {
   struct android_log_logger_list* logger_list_internal =
       (struct android_log_logger_list*)logger_list;
   struct android_log_logger* logger;
@@ -284,7 +262,7 @@
     }
   }
 
-  logger = calloc(1, sizeof(*logger));
+  logger = static_cast<android_log_logger*>(calloc(1, sizeof(*logger)));
   if (!logger) {
     goto err;
   }
@@ -311,8 +289,8 @@
 }
 
 /* Open the single named log and make it part of a new logger list */
-LIBLOG_ABI_PUBLIC struct logger_list* android_logger_list_open(
-    log_id_t logId, int mode, unsigned int tail, pid_t pid) {
+LIBLOG_ABI_PUBLIC struct logger_list* android_logger_list_open(log_id_t logId, int mode,
+                                                               unsigned int tail, pid_t pid) {
   struct logger_list* logger_list = android_logger_list_alloc(mode, tail, pid);
 
   if (!logger_list) {
@@ -379,8 +357,8 @@
   }
 
   /* at least one transport */
-  transp = node_to_item(logger_list_internal->transport.next,
-                        struct android_log_transport_context, node);
+  transp = node_to_item(logger_list_internal->transport.next, struct android_log_transport_context,
+                        node);
 
   /* more than one transport? */
   if (transp->node.next != &logger_list_internal->transport) {
@@ -401,11 +379,9 @@
             retval = transp->ret = 0;
           } else if ((logger_list_internal->mode & ANDROID_LOG_NONBLOCK) ||
                      !transp->transport->poll) {
-            retval = android_transport_read(logger_list_internal, transp,
-                                            &transp->logMsg);
+            retval = android_transport_read(logger_list_internal, transp, &transp->logMsg);
           } else {
-            int pollval =
-                (*transp->transport->poll)(logger_list_internal, transp);
+            int pollval = (*transp->transport->poll)(logger_list_internal, transp);
             if (pollval <= 0) {
               sched_yield();
               pollval = (*transp->transport->poll)(logger_list_internal, transp);
@@ -417,8 +393,7 @@
               }
               retval = transp->ret = pollval;
             } else if (pollval > 0) {
-              retval = android_transport_read(logger_list_internal, transp,
-                                              &transp->logMsg);
+              retval = android_transport_read(logger_list_internal, transp, &transp->logMsg);
             }
           }
         }
@@ -431,11 +406,9 @@
               (oldest->logMsg.entry.nsec > transp->logMsg.entry.nsec)))) {
           oldest = transp;
         }
-        transp = node_to_item(transp->node.next,
-                              struct android_log_transport_context, node);
+        transp = node_to_item(transp->node.next, struct android_log_transport_context, node);
       } while (transp != node_to_item(&logger_list_internal->transport,
-                                      struct android_log_transport_context,
-                                      node));
+                                      struct android_log_transport_context, node));
       if (!oldest && (logger_list_internal->mode & ANDROID_LOG_NONBLOCK)) {
         return (ret < 0) ? ret : -EAGAIN;
       }
@@ -449,10 +422,10 @@
     ret = oldest->ret;
     if (ret < oldest->logMsg.entry.hdr_size) {
       // zero truncated header fields.
-      memset(log_msg, 0,
-             (oldest->logMsg.entry.hdr_size > sizeof(oldest->logMsg)
-                  ? sizeof(oldest->logMsg)
-                  : oldest->logMsg.entry.hdr_size));
+      memset(
+          log_msg, 0,
+          (oldest->logMsg.entry.hdr_size > sizeof(oldest->logMsg) ? sizeof(oldest->logMsg)
+                                                                  : oldest->logMsg.entry.hdr_size));
     }
     memcpy(log_msg, &oldest->logMsg, ret);
     oldest->logMsg.entry.len = 0; /* Mark it as copied */
@@ -472,10 +445,6 @@
     return;
   }
 
-  logger_list_wrlock();
-  list_remove(&logger_list_internal->node);
-  logger_list_unlock();
-
   while (!list_empty(&logger_list_internal->transport)) {
     struct listnode* node = list_head(&logger_list_internal->transport);
     struct android_log_transport_context* transp =
@@ -490,8 +459,7 @@
 
   while (!list_empty(&logger_list_internal->logger)) {
     struct listnode* node = list_head(&logger_list_internal->logger);
-    struct android_log_logger* logger =
-        node_to_item(node, struct android_log_logger, node);
+    struct android_log_logger* logger = node_to_item(node, struct android_log_logger, node);
     android_logger_free((struct logger*)logger);
   }
 
diff --git a/liblog/logger_write.c b/liblog/logger_write.cpp
similarity index 87%
rename from liblog/logger_write.c
rename to liblog/logger_write.cpp
index 2754e6e..af8cb2d 100644
--- a/liblog/logger_write.c
+++ b/liblog/logger_write.cpp
@@ -33,23 +33,19 @@
 #include "config_write.h"
 #include "log_portability.h"
 #include "logger.h"
+#include "uio.h"
 
 #define LOG_BUF_SIZE 1024
 
 static int __write_to_log_init(log_id_t, struct iovec* vec, size_t nr);
-static int (*write_to_log)(log_id_t, struct iovec* vec,
-                           size_t nr) = __write_to_log_init;
+static int (*write_to_log)(log_id_t, struct iovec* vec, size_t nr) = __write_to_log_init;
 
 /*
  * This is used by the C++ code to decide if it should write logs through
  * the C code.  Basically, if /dev/socket/logd is available, we're running in
  * the simulator rather than a desktop tool and want to use the device.
  */
-static enum {
-  kLogUninitialized,
-  kLogNotAvailable,
-  kLogAvailable
-} g_log_status = kLogUninitialized;
+static enum { kLogUninitialized, kLogNotAvailable, kLogAvailable } g_log_status = kLogUninitialized;
 
 static int check_log_uid_permissions() {
 #if defined(__ANDROID__)
@@ -70,7 +66,7 @@
           if (num_groups <= 0) {
             return -EPERM;
           }
-          groups = calloc(num_groups, sizeof(gid_t));
+          groups = static_cast<gid_t*>(calloc(num_groups, sizeof(gid_t)));
           if (!groups) {
             return -ENOMEM;
           }
@@ -93,9 +89,8 @@
   return 0;
 }
 
-static void __android_log_cache_available(
-    struct android_log_transport_write* node) {
-  size_t i;
+static void __android_log_cache_available(struct android_log_transport_write* node) {
+  uint32_t i;
 
   if (node->logMask) {
     return;
@@ -104,13 +99,13 @@
   for (i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
     if (node->write && (i != LOG_ID_KERNEL) &&
         ((i != LOG_ID_SECURITY) || (check_log_uid_permissions() == 0)) &&
-        (!node->available || ((*node->available)(i) >= 0))) {
+        (!node->available || ((*node->available)(static_cast<log_id_t>(i)) >= 0))) {
       node->logMask |= 1 << i;
     }
   }
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_dev_available() {
+LIBLOG_ABI_PUBLIC extern "C" int __android_log_dev_available() {
   struct android_log_transport_write* node;
 
   if (list_empty(&__android_log_transport_write)) {
@@ -308,10 +303,9 @@
       }
     }
     if (m && (m != (EventTagMap*)(uintptr_t)-1LL)) {
-      tag = android_lookupEventTag_len(m, &len, get4LE(vec[0].iov_base));
+      tag = android_lookupEventTag_len(m, &len, get4LE(static_cast<uint8_t*>(vec[0].iov_base)));
     }
-    ret = __android_log_is_loggable_len(ANDROID_LOG_INFO, tag, len,
-                                        ANDROID_LOG_VERBOSE);
+    ret = __android_log_is_loggable_len(ANDROID_LOG_INFO, tag, len, ANDROID_LOG_VERBOSE);
     if (f) { /* local copy marked for close */
       android_closeEventTagMap(f);
     }
@@ -322,7 +316,7 @@
   } else {
     /* Validate the incoming tag, tag content can not split across iovec */
     char prio = ANDROID_LOG_VERBOSE;
-    const char* tag = vec[0].iov_base;
+    const char* tag = static_cast<const char*>(vec[0].iov_base);
     size_t len = vec[0].iov_len;
     if (!tag) {
       len = 0;
@@ -408,13 +402,12 @@
   return ret;
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_write(int prio, const char* tag,
-                                          const char* msg) {
+LIBLOG_ABI_PUBLIC int __android_log_write(int prio, const char* tag, const char* msg) {
   return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_buf_write(int bufID, int prio,
-                                              const char* tag, const char* msg) {
+LIBLOG_ABI_PUBLIC int __android_log_buf_write(int bufID, int prio, const char* tag,
+                                              const char* msg) {
   struct iovec vec[3];
   char tmp_tag[32];
 
@@ -457,7 +450,7 @@
         bufID = LOG_ID_RADIO;
         snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
         tag = tmp_tag;
-      /* FALLTHRU */
+        [[fallthrough]];
       default:
         break;
     }
@@ -476,11 +469,10 @@
   vec[2].iov_base = (void*)msg;
   vec[2].iov_len = strlen(msg) + 1;
 
-  return write_to_log(bufID, vec, 3);
+  return write_to_log(static_cast<log_id_t>(bufID), vec, 3);
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_vprint(int prio, const char* tag,
-                                           const char* fmt, va_list ap) {
+LIBLOG_ABI_PUBLIC int __android_log_vprint(int prio, const char* tag, const char* fmt, va_list ap) {
   char buf[LOG_BUF_SIZE];
 
   vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
@@ -488,8 +480,7 @@
   return __android_log_write(prio, tag, buf);
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_print(int prio, const char* tag,
-                                          const char* fmt, ...) {
+LIBLOG_ABI_PUBLIC int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
   va_list ap;
   char buf[LOG_BUF_SIZE];
 
@@ -500,8 +491,7 @@
   return __android_log_write(prio, tag, buf);
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_buf_print(int bufID, int prio,
-                                              const char* tag, const char* fmt,
+LIBLOG_ABI_PUBLIC int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt,
                                               ...) {
   va_list ap;
   char buf[LOG_BUF_SIZE];
@@ -513,8 +503,8 @@
   return __android_log_buf_write(bufID, prio, tag, buf);
 }
 
-LIBLOG_ABI_PUBLIC void __android_log_assert(const char* cond, const char* tag,
-                                            const char* fmt, ...) {
+LIBLOG_ABI_PUBLIC void __android_log_assert(const char* cond, const char* tag, const char* fmt,
+                                            ...) {
   char buf[LOG_BUF_SIZE];
 
   if (fmt) {
@@ -535,18 +525,15 @@
 
   // Log assertion failures to stderr for the benefit of "adb shell" users
   // and gtests (http://b/23675822).
-  struct iovec iov[2] = {
-    { buf, strlen(buf) }, { (char*)"\n", 1 },
-  };
-  TEMP_FAILURE_RETRY(writev(2, iov, 2));
+  TEMP_FAILURE_RETRY(write(2, buf, strlen(buf)));
+  TEMP_FAILURE_RETRY(write(2, "\n", 1));
 
   __android_log_write(ANDROID_LOG_FATAL, tag, buf);
   abort(); /* abort so we have a chance to debug the situation */
            /* NOTREACHED */
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_bwrite(int32_t tag, const void* payload,
-                                           size_t len) {
+LIBLOG_ABI_PUBLIC int __android_log_bwrite(int32_t tag, const void* payload, size_t len) {
   struct iovec vec[2];
 
   vec[0].iov_base = &tag;
@@ -557,9 +544,7 @@
   return write_to_log(LOG_ID_EVENTS, vec, 2);
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_stats_bwrite(int32_t tag,
-                                                 const void* payload,
-                                                 size_t len) {
+LIBLOG_ABI_PUBLIC int __android_log_stats_bwrite(int32_t tag, const void* payload, size_t len) {
   struct iovec vec[2];
 
   vec[0].iov_base = &tag;
@@ -570,9 +555,7 @@
   return write_to_log(LOG_ID_STATS, vec, 2);
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_security_bwrite(int32_t tag,
-                                                    const void* payload,
-                                                    size_t len) {
+LIBLOG_ABI_PUBLIC int __android_log_security_bwrite(int32_t tag, const void* payload, size_t len) {
   struct iovec vec[2];
 
   vec[0].iov_base = &tag;
@@ -588,8 +571,8 @@
  * for the general case where we're generating lists of stuff, but very
  * handy if we just want to dump an integer into the log.
  */
-LIBLOG_ABI_PUBLIC int __android_log_btwrite(int32_t tag, char type,
-                                            const void* payload, size_t len) {
+LIBLOG_ABI_PUBLIC int __android_log_btwrite(int32_t tag, char type, const void* payload,
+                                            size_t len) {
   struct iovec vec[3];
 
   vec[0].iov_base = &tag;
@@ -627,8 +610,7 @@
  * Like __android_log_security_bwrite, but used for writing strings to the
  * security log.
  */
-LIBLOG_ABI_PUBLIC int __android_log_security_bswrite(int32_t tag,
-                                                     const char* payload) {
+LIBLOG_ABI_PUBLIC int __android_log_security_bswrite(int32_t tag, const char* payload) {
   struct iovec vec[4];
   char type = EVENT_TYPE_STRING;
   uint32_t len = strlen(payload);
@@ -684,9 +666,9 @@
     return retval;
   }
 
-  __android_log_transport &= LOGGER_LOCAL | LOGGER_LOGD | LOGGER_STDERR;
+  __android_log_transport &= LOGGER_LOGD | LOGGER_STDERR;
 
-  transport_flag &= LOGGER_LOCAL | LOGGER_LOGD | LOGGER_STDERR;
+  transport_flag &= LOGGER_LOGD | LOGGER_STDERR;
 
   if (__android_log_transport != transport_flag) {
     __android_log_transport = transport_flag;
@@ -695,8 +677,7 @@
 
     write_to_log = __write_to_log_init;
     /* generically we only expect these two values for write_to_log */
-  } else if ((write_to_log != __write_to_log_init) &&
-             (write_to_log != __write_to_log_daemon)) {
+  } else if ((write_to_log != __write_to_log_init) && (write_to_log != __write_to_log_daemon)) {
     write_to_log = __write_to_log_init;
   }
 
@@ -714,10 +695,9 @@
   if (write_to_log == __write_to_log_null) {
     ret = LOGGER_NULL;
   } else {
-    __android_log_transport &= LOGGER_LOCAL | LOGGER_LOGD | LOGGER_STDERR;
+    __android_log_transport &= LOGGER_LOGD | LOGGER_STDERR;
     ret = __android_log_transport;
-    if ((write_to_log != __write_to_log_init) &&
-        (write_to_log != __write_to_log_daemon)) {
+    if ((write_to_log != __write_to_log_init) && (write_to_log != __write_to_log_daemon)) {
       ret = -EINVAL;
     }
   }
diff --git a/liblog/logprint.c b/liblog/logprint.cpp
similarity index 90%
rename from liblog/logprint.c
rename to liblog/logprint.cpp
index 7937cb1..798b089 100644
--- a/liblog/logprint.c
+++ b/liblog/logprint.cpp
@@ -15,7 +15,6 @@
 ** limitations under the License.
 */
 
-#define _GNU_SOURCE /* for asprintf */
 #ifndef __MINGW32__
 #define HAVE_STRSEP
 #endif
@@ -154,7 +153,7 @@
     case ANDROID_LOG_DEFAULT:
     case ANDROID_LOG_UNKNOWN:
     default:                  return '?';
-    /* clang-format on */
+      /* clang-format on */
   }
 }
 
@@ -172,16 +171,14 @@
     case ANDROID_LOG_DEFAULT:
     case ANDROID_LOG_UNKNOWN:
     default:                  return ANDROID_COLOR_DEFAULT;
-    /* clang-format on */
+      /* clang-format on */
   }
 }
 
-static android_LogPriority filterPriForTag(AndroidLogFormat* p_format,
-                                           const char* tag) {
+static android_LogPriority filterPriForTag(AndroidLogFormat* p_format, const char* tag) {
   FilterInfo* p_curFilter;
 
-  for (p_curFilter = p_format->filters; p_curFilter != NULL;
-       p_curFilter = p_curFilter->p_next) {
+  for (p_curFilter = p_format->filters; p_curFilter != NULL; p_curFilter = p_curFilter->p_next) {
     if (0 == strcmp(tag, p_curFilter->mTag)) {
       if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) {
         return p_format->global_pri;
@@ -198,8 +195,7 @@
  * returns 1 if this log line should be printed based on its priority
  * and tag, and 0 if it should not
  */
-LIBLOG_ABI_PUBLIC int android_log_shouldPrintLine(AndroidLogFormat* p_format,
-                                                  const char* tag,
+LIBLOG_ABI_PUBLIC int android_log_shouldPrintLine(AndroidLogFormat* p_format, const char* tag,
                                                   android_LogPriority pri) {
   return pri >= filterPriForTag(p_format, tag);
 }
@@ -207,7 +203,7 @@
 LIBLOG_ABI_PUBLIC AndroidLogFormat* android_log_format_new() {
   AndroidLogFormat* p_ret;
 
-  p_ret = calloc(1, sizeof(AndroidLogFormat));
+  p_ret = static_cast<AndroidLogFormat*>(calloc(1, sizeof(AndroidLogFormat)));
 
   p_ret->global_pri = ANDROID_LOG_VERBOSE;
   p_ret->format = FORMAT_BRIEF;
@@ -302,8 +298,7 @@
 /**
  * Returns FORMAT_OFF on invalid string
  */
-LIBLOG_ABI_PUBLIC AndroidLogPrintFormat
-android_log_formatFromString(const char* formatString) {
+LIBLOG_ABI_PUBLIC AndroidLogPrintFormat android_log_formatFromString(const char* formatString) {
   static AndroidLogPrintFormat format;
 
   /* clang-format off */
@@ -326,7 +321,7 @@
   else if (!strcmp(formatString, "monotonic")) format = FORMAT_MODIFIER_MONOTONIC;
   else if (!strcmp(formatString, "uid")) format = FORMAT_MODIFIER_UID;
   else if (!strcmp(formatString, "descriptive")) format = FORMAT_MODIFIER_DESCRIPT;
-  /* clang-format on */
+    /* clang-format on */
 
 #ifndef __MINGW32__
   else {
@@ -344,9 +339,8 @@
      */
     tzset();
     if (!tzname[0] ||
-        ((!strcmp(tzname[0], utc) || !strcmp(tzname[0], gmt)) /* error? */
-         && strcasecmp(formatString, utc) &&
-         strcasecmp(formatString, gmt))) { /* ok */
+        ((!strcmp(tzname[0], utc) || !strcmp(tzname[0], gmt))                  /* error? */
+         && strcasecmp(formatString, utc) && strcasecmp(formatString, gmt))) { /* ok */
       if (cp) {
         setenv(tz, cp, 1);
       } else {
@@ -581,7 +575,7 @@
     msg[msgEnd] = '\0';
   }
 
-  entry->priority = msg[0];
+  entry->priority = static_cast<android_LogPriority>(msg[0]);
   entry->tag = msg + 1;
   entry->tagLen = msgStart - 1;
   entry->message = msg + msgStart;
@@ -643,9 +637,8 @@
   TYPE_MONOTONIC = 's'
 };
 
-static int android_log_printBinaryEvent(const unsigned char** pEventData,
-                                        size_t* pEventDataLen, char** pOutBuf,
-                                        size_t* pOutBufLen, const char** fmtStr,
+static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t* pEventDataLen,
+                                        char** pOutBuf, size_t* pOutBufLen, const char** fmtStr,
                                         size_t* fmtLen) {
   const unsigned char* eventData = *pEventData;
   size_t eventDataLen = *pEventDataLen;
@@ -729,13 +722,10 @@
     }
 
     if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
-      static const unsigned char typeTable[] = {
-        EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING, EVENT_TYPE_LIST,
-        EVENT_TYPE_FLOAT
-      };
+      static const unsigned char typeTable[] = {EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING,
+                                                EVENT_TYPE_LIST, EVENT_TYPE_FLOAT};
 
-      if ((*cp >= '1') &&
-          (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
+      if ((*cp >= '1') && (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
           (type != typeTable[(size_t)(*cp - '1')]))
         len = 0;
 
@@ -858,8 +848,8 @@
         outBufLen--;
 
         for (i = 0; i < count; i++) {
-          result = android_log_printBinaryEvent(
-              &eventData, &eventDataLen, &outBuf, &outBufLen, fmtStr, fmtLen);
+          result = android_log_printBinaryEvent(&eventData, &eventDataLen, &outBuf, &outBufLen,
+                                                fmtStr, fmtLen);
           if (result != 0) goto bail;
 
           if (i < (count - 1)) {
@@ -889,24 +879,21 @@
         case TYPE_BYTES:
           if ((lval != 0) && ((lval % 1024) == 0)) {
             /* repaint with multiplier */
-            static const char suffixTable[] = { 'K', 'M', 'G', 'T' };
+            static const char suffixTable[] = {'K', 'M', 'G', 'T'};
             size_t idx = 0;
             outBuf -= outCount;
             outBufLen += outCount;
             do {
               lval /= 1024;
               if ((lval % 1024) != 0) break;
-            } while (++idx <
-                     ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
-            outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval,
-                                suffixTable[idx]);
+            } while (++idx < ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
+            outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval, suffixTable[idx]);
           } else {
             outCount = snprintf(outBuf, outBufLen, "B");
           }
           break;
         case TYPE_MILLISECONDS:
-          if (((lval <= -1000) || (1000 <= lval)) &&
-              (outBufLen || (outBuf[-1] == '0'))) {
+          if (((lval <= -1000) || (1000 <= lval)) && (outBufLen || (outBuf[-1] == '0'))) {
             /* repaint as (fractional) seconds, possibly saving space */
             if (outBufLen) outBuf[0] = outBuf[-1];
             outBuf[-1] = outBuf[-2];
@@ -943,22 +930,19 @@
           }
           if (val >= minute) {
             if (val >= hour) {
-              outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":",
-                                  (val / hour) % (day / hour));
+              outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":", (val / hour) % (day / hour));
               if (outCount >= outBufLen) break;
               outBuf += outCount;
               outBufLen -= outCount;
             }
             outCount =
-                snprintf(outBuf, outBufLen,
-                         (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":",
+                snprintf(outBuf, outBufLen, (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":",
                          (val / minute) % (hour / minute));
             if (outCount >= outBufLen) break;
             outBuf += outCount;
             outBufLen -= outCount;
           }
-          outCount = snprintf(outBuf, outBufLen,
-                              (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s",
+          outCount = snprintf(outBuf, outBufLen, (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s",
                               val % minute);
         } break;
         case TYPE_ALLOCATIONS:
@@ -1015,7 +999,7 @@
  */
 LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer(
     struct logger_entry* buf, AndroidLogEntry* entry,
-    const EventTagMap* map __unused, /* only on !__ANDROID__ */
+    [[maybe_unused]] const EventTagMap* map, /* only on !__ANDROID__ */
     char* messageBuf, int messageBufLen) {
   size_t inCount;
   uint32_t tagIndex;
@@ -1100,8 +1084,8 @@
   int result = 0;
 
   if ((inCount > 0) || fmtLen) {
-    result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf,
-                                          &outRemaining, &fmtStr, &fmtLen);
+    result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, &fmtStr,
+                                          &fmtLen);
   }
   if ((result == 1) && fmtStr) {
     /* We overflowed :-(, let's repaint the line w/o format dressings */
@@ -1112,8 +1096,7 @@
     eventData += 4;
     outBuf = messageBuf;
     outRemaining = messageBufLen - 1;
-    result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf,
-                                          &outRemaining, NULL, NULL);
+    result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, NULL, NULL);
   }
   if (result < 0) {
     fprintf(stderr, "Binary log entry conversion failed\n");
@@ -1187,8 +1170,7 @@
   }
 
   for (utf32 = 1, num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
-       num_to_read < 5 && (first_char & mask);
-       num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
+       num_to_read < 5 && (first_char & mask); num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
     if (num_to_read > len) {
       return -1;
     }
@@ -1226,8 +1208,7 @@
     len = utf8_character_length(message, len);
 
     if (len < 0) {
-      snprintf(buf, sizeof(buf),
-               ((messageLen > 1) && isdigit(message[1])) ? "\\%03o" : "\\%o",
+      snprintf(buf, sizeof(buf), ((messageLen > 1) && isdigit(message[1])) ? "\\%03o" : "\\%o",
                *message & 0377);
       len = 1;
     } else {
@@ -1281,8 +1262,7 @@
   return p;
 }
 
-static struct timespec* sumTimespec(struct timespec* left,
-                                    struct timespec* right) {
+static struct timespec* sumTimespec(struct timespec* left, struct timespec* right) {
   left->tv_nsec += right->tv_nsec;
   left->tv_sec += right->tv_sec;
   if (left->tv_nsec >= (long)NS_PER_SEC) {
@@ -1292,8 +1272,7 @@
   return left;
 }
 
-static struct timespec* subTimespec(struct timespec* result,
-                                    struct timespec* left,
+static struct timespec* subTimespec(struct timespec* result, struct timespec* left,
                                     struct timespec* right) {
   result->tv_nsec = left->tv_nsec - right->tv_nsec;
   result->tv_sec = left->tv_sec - right->tv_sec;
@@ -1309,8 +1288,7 @@
 }
 
 #ifdef __ANDROID__
-static void convertMonotonic(struct timespec* result,
-                             const AndroidLogEntry* entry) {
+static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) {
   struct listnode* node;
   struct conversionList {
     struct listnode node; /* first */
@@ -1322,8 +1300,8 @@
   /* If we do not have a conversion list, build one up */
   if (list_empty(&convertHead)) {
     bool suspended_pending = false;
-    struct timespec suspended_monotonic = { 0, 0 };
-    struct timespec suspended_diff = { 0, 0 };
+    struct timespec suspended_monotonic = {0, 0};
+    struct timespec suspended_diff = {0, 0};
 
     /*
      * Read dmesg for _some_ synchronization markers and insert
@@ -1413,15 +1391,14 @@
           } else {
             unsetenv(tz);
           }
-          list = calloc(1, sizeof(struct conversionList));
+          list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
           list_init(&list->node);
           list->time = time;
           subTimespec(&list->convert, &time, &monotonic);
           list_add_tail(&convertHead, &list->node);
         }
         if (suspended_pending && !list_empty(&convertHead)) {
-          list = node_to_item(list_tail(&convertHead), struct conversionList,
-                              node);
+          list = node_to_item(list_tail(&convertHead), struct conversionList, node);
           if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert),
                           &suspended_monotonic)
                   ->tv_sec > 0) {
@@ -1434,13 +1411,13 @@
           time = suspended_monotonic;
           sumTimespec(&time, &convert);
           /* breakpoint just before sleep */
-          list = calloc(1, sizeof(struct conversionList));
+          list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
           list_init(&list->node);
           list->time = time;
           list->convert = convert;
           list_add_tail(&convertHead, &list->node);
           /* breakpoint just after sleep */
-          list = calloc(1, sizeof(struct conversionList));
+          list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
           list_init(&list->node);
           list->time = time;
           sumTimespec(&list->time, &suspended_diff);
@@ -1453,7 +1430,7 @@
       pclose(p);
     }
     /* last entry is our current time conversion */
-    list = calloc(1, sizeof(struct conversionList));
+    list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
     list_init(&list->node);
     clock_gettime(CLOCK_REALTIME, &list->time);
     clock_gettime(CLOCK_MONOTONIC, &convert);
@@ -1469,7 +1446,7 @@
       time = suspended_monotonic;
       sumTimespec(&time, &convert);
       /* breakpoint just after sleep */
-      list = calloc(1, sizeof(struct conversionList));
+      list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
       list_init(&list->node);
       list->time = time;
       sumTimespec(&list->time, &suspended_diff);
@@ -1477,7 +1454,7 @@
       sumTimespec(&list->convert, &suspended_diff);
       list_add_head(&convertHead, &list->node);
       /* breakpoint just before sleep */
-      list = calloc(1, sizeof(struct conversionList));
+      list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
       list_init(&list->node);
       list->time = time;
       list->convert = convert;
@@ -1548,8 +1525,7 @@
  * Returns NULL on malloc error
  */
 
-LIBLOG_ABI_PUBLIC char* android_log_formatLogLine(AndroidLogFormat* p_format,
-                                                  char* defaultBuffer,
+LIBLOG_ABI_PUBLIC char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
                                                   size_t defaultBufferSize,
                                                   const AndroidLogEntry* entry,
                                                   size_t* p_outLength) {
@@ -1600,26 +1576,23 @@
   }
   if (p_format->epoch_output || p_format->monotonic_output) {
     ptm = NULL;
-    snprintf(timeBuf, sizeof(timeBuf),
-             p_format->monotonic_output ? "%6lld" : "%19lld", (long long)now);
+    snprintf(timeBuf, sizeof(timeBuf), p_format->monotonic_output ? "%6lld" : "%19lld",
+             (long long)now);
   } else {
 #if !defined(_WIN32)
     ptm = localtime_r(&now, &tmBuf);
 #else
     ptm = localtime(&now);
 #endif
-    strftime(timeBuf, sizeof(timeBuf),
-             &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
+    strftime(timeBuf, sizeof(timeBuf), &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
   }
   len = strlen(timeBuf);
   if (p_format->nsec_time_output) {
     len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec);
   } else if (p_format->usec_time_output) {
-    len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld",
-                    nsec / US_PER_NSEC);
+    len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld", nsec / US_PER_NSEC);
   } else {
-    len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld",
-                    nsec / MS_PER_NSEC);
+    len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld", nsec / MS_PER_NSEC);
   }
   if (p_format->zone_output && ptm) {
     strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
@@ -1629,8 +1602,8 @@
    * Construct a buffer containing the log header and log message.
    */
   if (p_format->colored_output) {
-    prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm",
-                         colorFromPri(entry->priority));
+    prefixLen =
+        snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", colorFromPri(entry->priority));
     prefixLen = MIN(prefixLen, sizeof(prefixBuf));
 
     const char suffixContents[] = "\x1B[0m";
@@ -1649,8 +1622,7 @@
 #if !defined(__MINGW32__)
 #if (FAKE_LOG_DEVICE == 0)
 #ifndef __BIONIC__
-#warning \
-    "This code assumes that getpwuid is thread safe, only true with Bionic!"
+#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
 #endif
 #endif
       struct passwd* pwd = getpwuid(entry->uid);
@@ -1669,21 +1641,21 @@
 
   switch (p_format->format) {
     case FORMAT_TAG:
-      len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
-                     "%c/%-8.*s: ", priChar, (int)entry->tagLen, entry->tag);
+      len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c/%-8.*s: ", priChar,
+                     (int)entry->tagLen, entry->tag);
       strcpy(suffixBuf + suffixLen, "\n");
       ++suffixLen;
       break;
     case FORMAT_PROCESS:
-      len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen,
-                     "  (%.*s)\n", (int)entry->tagLen, entry->tag);
+      len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, "  (%.*s)\n",
+                     (int)entry->tagLen, entry->tag);
       suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
-      len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
-                     "%c(%s%5d) ", priChar, uid, entry->pid);
+      len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d) ", priChar,
+                     uid, entry->pid);
       break;
     case FORMAT_THREAD:
-      len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
-                     "%c(%s%5d:%5d) ", priChar, uid, entry->pid, entry->tid);
+      len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d:%5d) ",
+                     priChar, uid, entry->pid, entry->tid);
       strcpy(suffixBuf + suffixLen, "\n");
       ++suffixLen;
       break;
@@ -1695,8 +1667,8 @@
       break;
     case FORMAT_TIME:
       len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
-                     "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar,
-                     (int)entry->tagLen, entry->tag, uid, entry->pid);
+                     "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar, (int)entry->tagLen, entry->tag, uid,
+                     entry->pid);
       strcpy(suffixBuf + suffixLen, "\n");
       ++suffixLen;
       break;
@@ -1706,24 +1678,24 @@
         *ret = ' ';
       }
       len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
-                     "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid,
-                     entry->tid, priChar, (int)entry->tagLen, entry->tag);
+                     "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid, entry->tid, priChar,
+                     (int)entry->tagLen, entry->tag);
       strcpy(suffixBuf + suffixLen, "\n");
       ++suffixLen;
       break;
     case FORMAT_LONG:
       len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
-                     "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid,
-                     entry->tid, priChar, (int)entry->tagLen, entry->tag);
+                     "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid, entry->tid, priChar,
+                     (int)entry->tagLen, entry->tag);
       strcpy(suffixBuf + suffixLen, "\n\n");
       suffixLen += 2;
       prefixSuffixIsHeaderFooter = 1;
       break;
     case FORMAT_BRIEF:
     default:
-      len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
-                     "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen,
-                     entry->tag, uid, entry->pid);
+      len =
+          snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
+                   "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag, uid, entry->pid);
       strcpy(suffixBuf + suffixLen, "\n");
       ++suffixLen;
       break;
@@ -1847,16 +1819,15 @@
  * Returns count bytes written
  */
 
-LIBLOG_ABI_PUBLIC int android_log_printLogLine(AndroidLogFormat* p_format,
-                                               int fd,
+LIBLOG_ABI_PUBLIC int android_log_printLogLine(AndroidLogFormat* p_format, int fd,
                                                const AndroidLogEntry* entry) {
   int ret;
   char defaultBuffer[512];
   char* outBuffer = NULL;
   size_t totalLen;
 
-  outBuffer = android_log_formatLogLine(
-      p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
+  outBuffer =
+      android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
 
   if (!outBuffer) return -1;
 
diff --git a/liblog/pmsg_reader.c b/liblog/pmsg_reader.cpp
similarity index 84%
rename from liblog/pmsg_reader.c
rename to liblog/pmsg_reader.cpp
index bf0e4fe..7bc6e4a 100644
--- a/liblog/pmsg_reader.c
+++ b/liblog/pmsg_reader.cpp
@@ -32,28 +32,27 @@
 static int pmsgVersion(struct android_log_logger* logger,
                        struct android_log_transport_context* transp);
 static int pmsgRead(struct android_log_logger_list* logger_list,
-                    struct android_log_transport_context* transp,
-                    struct log_msg* log_msg);
+                    struct android_log_transport_context* transp, struct log_msg* log_msg);
 static void pmsgClose(struct android_log_logger_list* logger_list,
                       struct android_log_transport_context* transp);
 static int pmsgClear(struct android_log_logger* logger,
                      struct android_log_transport_context* transp);
 
 LIBLOG_HIDDEN struct android_log_transport_read pmsgLoggerRead = {
-  .node = { &pmsgLoggerRead.node, &pmsgLoggerRead.node },
-  .name = "pmsg",
-  .available = pmsgAvailable,
-  .version = pmsgVersion,
-  .read = pmsgRead,
-  .poll = NULL,
-  .close = pmsgClose,
-  .clear = pmsgClear,
-  .setSize = NULL,
-  .getSize = NULL,
-  .getReadableSize = NULL,
-  .getPrune = NULL,
-  .setPrune = NULL,
-  .getStats = NULL,
+    .node = {&pmsgLoggerRead.node, &pmsgLoggerRead.node},
+    .name = "pmsg",
+    .available = pmsgAvailable,
+    .version = pmsgVersion,
+    .read = pmsgRead,
+    .poll = NULL,
+    .close = pmsgClose,
+    .clear = pmsgClear,
+    .setSize = NULL,
+    .getSize = NULL,
+    .getReadableSize = NULL,
+    .getPrune = NULL,
+    .setPrune = NULL,
+    .getStats = NULL,
 };
 
 static int pmsgAvailable(log_id_t logId) {
@@ -68,8 +67,7 @@
 
 /* Determine the credentials of the caller */
 static bool uid_has_log_permission(uid_t uid) {
-  return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT) ||
-         (uid == AID_LOGD);
+  return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT) || (uid == AID_LOGD);
 }
 
 static uid_t get_best_effective_uid() {
@@ -130,8 +128,7 @@
 }
 
 static int pmsgRead(struct android_log_logger_list* logger_list,
-                    struct android_log_transport_context* transp,
-                    struct log_msg* log_msg) {
+                    struct android_log_transport_context* transp, struct log_msg* log_msg) {
   ssize_t ret;
   off_t current, next;
   uid_t uid;
@@ -174,8 +171,7 @@
       if (fd <= 0) {
         return -EBADF;
       }
-      ret = TEMP_FAILURE_RETRY(
-          read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
+      ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
       if (ret < 0) {
         return -errno;
       }
@@ -185,11 +181,10 @@
       return preread_count ? -EIO : -EAGAIN;
     }
     if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
-        (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) ||
-        (buf.l.id >= LOG_ID_MAX) || (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
+        (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) ||
+        (buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
         ((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
-         ((buf.prio == ANDROID_LOG_UNKNOWN) ||
-          (buf.prio == ANDROID_LOG_DEFAULT) ||
+         ((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
           (buf.prio >= ANDROID_LOG_SILENT)))) {
       do {
         memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
@@ -213,8 +208,7 @@
         if (fd <= 0) {
           return -EBADF;
         }
-        ret = TEMP_FAILURE_RETRY(
-            read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
+        ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
         if (ret < 0) {
           return -errno;
         }
@@ -250,8 +244,7 @@
     if (fd <= 0) {
       return -EBADF;
     }
-    next = TEMP_FAILURE_RETRY(
-        lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
+    next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
     if (next < 0) {
       return -errno;
     }
@@ -277,9 +270,10 @@
   return result;
 }
 
-LIBLOG_ABI_PRIVATE ssize_t
-__android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
-                             __android_log_pmsg_file_read_fn fn, void* arg) {
+LIBLOG_ABI_PRIVATE ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio,
+                                                        const char* prefix,
+                                                        __android_log_pmsg_file_read_fn fn,
+                                                        void* arg) {
   ssize_t ret;
   struct android_log_logger_list logger_list;
   struct android_log_transport_context transp;
@@ -312,14 +306,12 @@
   memset(&logger_list, 0, sizeof(logger_list));
   memset(&transp, 0, sizeof(transp));
 
-  logger_list.mode =
-      ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
+  logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
   transp.logMask = (unsigned)-1;
   if (logId != LOG_ID_ANY) {
     transp.logMask = (1 << logId);
   }
-  transp.logMask &=
-      ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
+  transp.logMask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
   if (!transp.logMask) {
     return -EINVAL;
   }
@@ -346,15 +338,13 @@
 
   /* Read the file content */
   while (pmsgRead(&logger_list, &transp, &transp.logMsg) > 0) {
-    char* cp;
-    size_t hdr_size = transp.logMsg.entry.hdr_size
-                          ? transp.logMsg.entry.hdr_size
-                          : sizeof(transp.logMsg.entry_v1);
+    const char* cp;
+    size_t hdr_size = transp.logMsg.entry.hdr_size ? transp.logMsg.entry.hdr_size
+                                                   : sizeof(transp.logMsg.entry_v1);
     char* msg = (char*)&transp.logMsg + hdr_size;
-    char* split = NULL;
+    const char* split = NULL;
 
-    if ((hdr_size < sizeof(transp.logMsg.entry_v1)) ||
-        (hdr_size > sizeof(transp.logMsg.entry))) {
+    if ((hdr_size < sizeof(transp.logMsg.entry_v1)) || (hdr_size > sizeof(transp.logMsg.entry))) {
       continue;
     }
     /* Check for invalid sequence number */
@@ -366,8 +356,7 @@
 
     /* Determine if it has <dirbase>:<filebase> format for tag */
     len = transp.logMsg.entry.len - sizeof(prio);
-    for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len;
-         ++cp) {
+    for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
       if (*cp == ':') {
         if (split) {
           break;
@@ -395,13 +384,12 @@
         continue;
       }
       offset = split - prefix;
-      if ((msg[offset + sizeof(prio)] != ':') ||
-          strncmp(msg + sizeof(prio), prefix, offset)) {
+      if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
         continue;
       }
       ++offset;
-      if ((prefix_len > offset) && strncmp(&msg[offset + sizeof(prio)],
-                                           split + 1, prefix_len - offset)) {
+      if ((prefix_len > offset) &&
+          strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
         continue;
       }
     }
@@ -413,8 +401,8 @@
     /* check if there is an existing entry */
     list_for_each(node, &name_list) {
       names = node_to_item(node, struct names, node);
-      if (!strcmp(names->name, msg + sizeof(prio)) &&
-          (names->id == transp.logMsg.entry.lid) && (names->prio == *msg)) {
+      if (!strcmp(names->name, msg + sizeof(prio)) && (names->id == transp.logMsg.entry.lid) &&
+          (names->prio == *msg)) {
         break;
       }
     }
@@ -425,13 +413,13 @@
       unsigned long long nl;
 
       len = strlen(msg + sizeof(prio)) + 1;
-      names = calloc(1, sizeof(*names) + len);
+      names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
       if (!names) {
         ret = -ENOMEM;
         break;
       }
       strcpy(names->name, msg + sizeof(prio));
-      names->id = transp.logMsg.entry.lid;
+      names->id = static_cast<log_id_t>(transp.logMsg.entry.lid);
       names->prio = *msg;
       list_init(&names->content);
       /*
@@ -491,19 +479,17 @@
     }
 
     /* Add content */
-    content =
-        calloc(1, sizeof(content->node) + hdr_size + transp.logMsg.entry.len);
+    content = static_cast<struct content*>(
+        calloc(1, sizeof(content->node) + hdr_size + transp.logMsg.entry.len));
     if (!content) {
       ret = -ENOMEM;
       break;
     }
-    memcpy(&content->entry, &transp.logMsg.entry,
-           hdr_size + transp.logMsg.entry.len);
+    memcpy(&content->entry, &transp.logMsg.entry, hdr_size + transp.logMsg.entry.len);
 
     /* Insert in sequence number sorted order, to ease reconstruction */
     list_for_each_reverse(node, &names->content) {
-      if ((node_to_item(node, struct content, node))->entry.nsec <
-          transp.logMsg.entry.nsec) {
+      if ((node_to_item(node, struct content, node))->entry.nsec < transp.logMsg.entry.nsec) {
         break;
       }
     }
@@ -536,7 +522,7 @@
       }
 
       if (!buf) {
-        buf = malloc(sizeof(char));
+        buf = static_cast<char*>(malloc(sizeof(char)));
         if (!buf) {
           ret = -ENOMEM;
           list_remove(content_node);
@@ -549,7 +535,7 @@
       /* Missing sequence numbers */
       while (sequence < content->entry.nsec) {
         /* plus space for enforced nul */
-        buf = realloc_or_free(buf, len + sizeof(char) + sizeof(char));
+        buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
         if (!buf) {
           break;
         }
@@ -564,16 +550,14 @@
         continue;
       }
       /* plus space for enforced nul */
-      buf = realloc_or_free(buf, len + add_len + sizeof(char));
+      buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
       if (!buf) {
         ret = -ENOMEM;
         list_remove(content_node);
         free(content);
         continue;
       }
-      memcpy(buf + len,
-             (char*)&content->entry + content->entry.hdr_size + tag_len +
-                 sizeof(prio),
+      memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
              add_len);
       len += add_len;
       buf[len] = '\0'; /* enforce trailing hidden nul */
diff --git a/liblog/pmsg_writer.c b/liblog/pmsg_writer.cpp
similarity index 88%
rename from liblog/pmsg_writer.c
rename to liblog/pmsg_writer.cpp
index dc42856..b2fc6d0 100644
--- a/liblog/pmsg_writer.c
+++ b/liblog/pmsg_writer.cpp
@@ -33,21 +33,21 @@
 #include "config_write.h"
 #include "log_portability.h"
 #include "logger.h"
+#include "uio.h"
 
 static int pmsgOpen();
 static void pmsgClose();
 static int pmsgAvailable(log_id_t logId);
-static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
-                     size_t nr);
+static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr);
 
 LIBLOG_HIDDEN struct android_log_transport_write pmsgLoggerWrite = {
-  .node = { &pmsgLoggerWrite.node, &pmsgLoggerWrite.node },
-  .context.fd = -1,
-  .name = "pmsg",
-  .available = pmsgAvailable,
-  .open = pmsgOpen,
-  .close = pmsgClose,
-  .write = pmsgWrite,
+    .node = {&pmsgLoggerWrite.node, &pmsgLoggerWrite.node},
+    .context.fd = -1,
+    .name = "pmsg",
+    .available = pmsgAvailable,
+    .open = pmsgOpen,
+    .close = pmsgClose,
+    .write = pmsgWrite,
 };
 
 static int pmsgOpen() {
@@ -76,8 +76,7 @@
   if (logId > LOG_ID_SECURITY) {
     return -EINVAL;
   }
-  if ((logId != LOG_ID_SECURITY) && (logId != LOG_ID_EVENTS) &&
-      !__android_log_is_debuggable()) {
+  if ((logId != LOG_ID_SECURITY) && (logId != LOG_ID_EVENTS) && !__android_log_is_debuggable()) {
     return -EINVAL;
   }
   if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
@@ -96,8 +95,7 @@
   return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
 }
 
-static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
-                     size_t nr) {
+static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
   static const unsigned headerLength = 2;
   struct iovec newVec[nr + headerLength];
   android_log_header_t header;
@@ -110,7 +108,7 @@
       return -EINVAL;
     }
 
-    if (SNET_EVENT_LOG_TAG != get4LE(vec[0].iov_base)) {
+    if (SNET_EVENT_LOG_TAG != get4LE(static_cast<uint8_t*>(vec[0].iov_base))) {
       return -EPERM;
     }
   }
@@ -169,8 +167,7 @@
   }
   pmsgHeader.len += payloadSize;
 
-  ret = TEMP_FAILURE_RETRY(
-      writev(atomic_load(&pmsgLoggerWrite.context.fd), newVec, i));
+  ret = TEMP_FAILURE_RETRY(writev(atomic_load(&pmsgLoggerWrite.context.fd), newVec, i));
   if (ret < 0) {
     ret = errno ? -errno : -ENOTCONN;
   }
@@ -203,10 +200,8 @@
 }
 
 /* Write a buffer as filename references (tag = <basedir>:<basename>) */
-LIBLOG_ABI_PRIVATE ssize_t __android_log_pmsg_file_write(log_id_t logId,
-                                                         char prio,
-                                                         const char* filename,
-                                                         const char* buf,
+LIBLOG_ABI_PRIVATE ssize_t __android_log_pmsg_file_write(log_id_t logId, char prio,
+                                                         const char* filename, const char* buf,
                                                          size_t len) {
   bool weOpened;
   size_t length, packet_len;
@@ -249,13 +244,11 @@
   vec[1].iov_len = length;
 
   weOpened = false;
-  for (ts.tv_nsec = 0, length = len; length;
-       ts.tv_nsec += ANDROID_LOG_PMSG_FILE_SEQUENCE) {
+  for (ts.tv_nsec = 0, length = len; length; ts.tv_nsec += ANDROID_LOG_PMSG_FILE_SEQUENCE) {
     ssize_t ret;
     size_t transfer;
 
-    if ((ts.tv_nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
-        ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
+    if ((ts.tv_nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >= ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
       len -= length;
       break;
     }
diff --git a/liblog/properties.c b/liblog/properties.cpp
similarity index 86%
rename from liblog/properties.c
rename to liblog/properties.cpp
index 11be827..764877e 100644
--- a/liblog/properties.c
+++ b/liblog/properties.cpp
@@ -14,9 +14,10 @@
 ** limitations under the License.
 */
 
+#include <log/log_properties.h>
+
 #include <ctype.h>
 #include <pthread.h>
-#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
@@ -160,11 +161,11 @@
       }
       if (!last_tag || !last_tag[0]) {
         if (!last_tag) {
-          last_tag = calloc(1, len + 1);
+          last_tag = static_cast<char*>(calloc(1, len + 1));
           last_tag_len = 0;
           if (last_tag) last_tag_len = len + 1;
         } else if (len >= last_tag_len) {
-          last_tag = realloc(last_tag, len + 1);
+          last_tag = static_cast<char*>(realloc(last_tag, len + 1));
           last_tag_len = 0;
           if (last_tag) last_tag_len = len + 1;
         }
@@ -258,22 +259,19 @@
     case 'A': return ANDROID_LOG_FATAL;
     case BOOLEAN_FALSE: /* FALLTHRU */ /* Not Officially supported */
     case 'S': return -1; /* ANDROID_LOG_SUPPRESS */
-    /* clang-format on */
+      /* clang-format on */
   }
   return default_prio;
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_is_loggable_len(int prio, const char* tag,
-                                                    size_t len,
+LIBLOG_ABI_PUBLIC int __android_log_is_loggable_len(int prio, const char* tag, size_t len,
                                                     int default_prio) {
   int logLevel = __android_log_level(tag, len, default_prio);
   return logLevel >= 0 && prio >= logLevel;
 }
 
-LIBLOG_ABI_PUBLIC int __android_log_is_loggable(int prio, const char* tag,
-                                                int default_prio) {
-  int logLevel =
-      __android_log_level(tag, (tag && *tag) ? strlen(tag) : 0, default_prio);
+LIBLOG_ABI_PUBLIC int __android_log_is_loggable(int prio, const char* tag, int default_prio) {
+  int logLevel = __android_log_level(tag, (tag && *tag) ? strlen(tag) : 0, default_prio);
   return logLevel >= 0 && prio >= logLevel;
 }
 
@@ -286,7 +284,7 @@
   if (tag_cache.c) { /* ro property does not change after set */
     ret = tag_cache.c == '1';
   } else if (lock()) {
-    struct cache_char temp_cache = { { NULL, -1 }, '\0' };
+    struct cache_char temp_cache = {{NULL, 0xFFFFFFFF}, '\0'};
     refresh_cache(&temp_cache, key);
     ret = temp_cache.c == '1';
   } else {
@@ -332,8 +330,7 @@
     return self->evaluate(self);
   }
 
-  change_detected = check_cache(&self->cache_persist.cache) ||
-                    check_cache(&self->cache_ro.cache);
+  change_detected = check_cache(&self->cache_persist.cache) || check_cache(&self->cache_ro.cache);
   current_serial = __system_property_area_serial();
   if (current_serial != self->serial) {
     change_detected = 1;
@@ -365,15 +362,12 @@
  * to handle developer requirements.
  */
 LIBLOG_ABI_PUBLIC clockid_t android_log_clockid() {
-  static struct cache2_char clockid = {
-    PTHREAD_MUTEX_INITIALIZER, 0,
-    "persist.logd.timestamp",  { { NULL, -1 }, '\0' },
-    "ro.logd.timestamp",       { { NULL, -1 }, '\0' },
-    evaluate_persist_ro
-  };
+  static struct cache2_char clockid = {PTHREAD_MUTEX_INITIALIZER, 0,
+                                       "persist.logd.timestamp",  {{NULL, 0xFFFFFFFF}, '\0'},
+                                       "ro.logd.timestamp",       {{NULL, 0xFFFFFFFF}, '\0'},
+                                       evaluate_persist_ro};
 
-  return (tolower(do_cache2_char(&clockid)) == 'm') ? CLOCK_MONOTONIC
-                                                    : CLOCK_REALTIME;
+  return (tolower(do_cache2_char(&clockid)) == 'm') ? CLOCK_MONOTONIC : CLOCK_REALTIME;
 }
 
 /*
@@ -388,11 +382,10 @@
 
 LIBLOG_ABI_PUBLIC int __android_log_security() {
   static struct cache2_char security = {
-    PTHREAD_MUTEX_INITIALIZER, 0,
-    "persist.logd.security",   { { NULL, -1 }, BOOLEAN_FALSE },
-    "ro.device_owner",         { { NULL, -1 }, BOOLEAN_FALSE },
-    evaluate_security
-  };
+      PTHREAD_MUTEX_INITIALIZER, 0,
+      "persist.logd.security",   {{NULL, 0xFFFFFFFF}, BOOLEAN_FALSE},
+      "ro.device_owner",         {{NULL, 0xFFFFFFFF}, BOOLEAN_FALSE},
+      evaluate_security};
 
   return do_cache2_char(&security);
 }
@@ -423,8 +416,7 @@
   char property[PROP_VALUE_MAX];
 };
 
-static void refresh_cache_property(struct cache_property* cache,
-                                   const char* key) {
+static void refresh_cache_property(struct cache_property* cache, const char* key) {
   if (!cache->cache.pinfo) {
     cache->cache.pinfo = __system_property_find(key);
     if (!cache->cache.pinfo) {
@@ -436,19 +428,18 @@
 }
 
 /* get boolean with the logger twist that supports eng adjustments */
-LIBLOG_ABI_PRIVATE bool __android_logger_property_get_bool(const char* key,
-                                                           int flag) {
-  struct cache_property property = { { NULL, -1 }, { 0 } };
+LIBLOG_ABI_PRIVATE bool __android_logger_property_get_bool(const char* key, int flag) {
+  struct cache_property property = {{NULL, 0xFFFFFFFF}, {0}};
   if (flag & BOOL_DEFAULT_FLAG_PERSIST) {
     char newkey[strlen("persist.") + strlen(key) + 1];
     snprintf(newkey, sizeof(newkey), "ro.%s", key);
     refresh_cache_property(&property, newkey);
     property.cache.pinfo = NULL;
-    property.cache.serial = -1;
+    property.cache.serial = 0xFFFFFFFF;
     snprintf(newkey, sizeof(newkey), "persist.%s", key);
     refresh_cache_property(&property, newkey);
     property.cache.pinfo = NULL;
-    property.cache.serial = -1;
+    property.cache.serial = 0xFFFFFFFF;
   }
 
   refresh_cache_property(&property, key);
@@ -477,8 +468,7 @@
   }
 
   if ((flag & BOOL_DEFAULT_FLAG_SVELTE) &&
-      __android_logger_property_get_bool("ro.config.low_ram",
-                                         BOOL_DEFAULT_FALSE)) {
+      __android_logger_property_get_bool("ro.config.low_ram", BOOL_DEFAULT_FALSE)) {
     return false;
   }
   if ((flag & BOOL_DEFAULT_FLAG_ENG) && !__android_log_is_debuggable()) {
@@ -531,8 +521,7 @@
   unsigned long (*const evaluate)(const struct cache2_property_size* self);
 };
 
-static inline unsigned long do_cache2_property_size(
-    struct cache2_property_size* self) {
+static inline unsigned long do_cache2_property_size(struct cache2_property_size* self) {
   uint32_t current_serial;
   int change_detected;
   unsigned long v;
@@ -542,8 +531,7 @@
     return self->evaluate(self);
   }
 
-  change_detected = check_cache(&self->cache_persist.cache) ||
-                    check_cache(&self->cache_ro.cache);
+  change_detected = check_cache(&self->cache_persist.cache) || check_cache(&self->cache_ro.cache);
   current_serial = __system_property_area_serial();
   if (current_serial != self->serial) {
     change_detected = 1;
@@ -560,8 +548,7 @@
   return v;
 }
 
-static unsigned long property_get_size_from_cache(
-    const struct cache_property* cache) {
+static unsigned long property_get_size_from_cache(const struct cache_property* cache) {
   char* cp;
   unsigned long value = strtoul(cache->property, &cp, 10);
 
@@ -569,11 +556,11 @@
     case 'm':
     case 'M':
       value *= 1024;
-    /* FALLTHRU */
+      [[fallthrough]];
     case 'k':
     case 'K':
       value *= 1024;
-    /* FALLTHRU */
+      [[fallthrough]];
     case '\0':
       break;
 
@@ -588,8 +575,7 @@
   return value;
 }
 
-static unsigned long evaluate_property_get_size(
-    const struct cache2_property_size* self) {
+static unsigned long evaluate_property_get_size(const struct cache2_property_size* self) {
   unsigned long size = property_get_size_from_cache(&self->cache_persist);
   if (size) {
     return size;
@@ -601,37 +587,35 @@
   static const char global_tunable[] = "persist.logd.size"; /* Settings App */
   static const char global_default[] = "ro.logd.size";      /* BoardConfig.mk */
   static struct cache2_property_size global = {
-    /* clang-format off */
+      /* clang-format off */
     PTHREAD_MUTEX_INITIALIZER, 0,
-    global_tunable, { { NULL, -1 }, {} },
-    global_default, { { NULL, -1 }, {} },
+    global_tunable, { { NULL, 0xFFFFFFFF }, {} },
+    global_default, { { NULL, 0xFFFFFFFF }, {} },
     evaluate_property_get_size
-    /* clang-format on */
+      /* clang-format on */
   };
   char key_persist[strlen(global_tunable) + strlen(".security") + 1];
   char key_ro[strlen(global_default) + strlen(".security") + 1];
   struct cache2_property_size local = {
-    /* clang-format off */
+      /* clang-format off */
     PTHREAD_MUTEX_INITIALIZER, 0,
-    key_persist, { { NULL, -1 }, {} },
-    key_ro,      { { NULL, -1 }, {} },
+    key_persist, { { NULL, 0xFFFFFFFF }, {} },
+    key_ro,      { { NULL, 0xFFFFFFFF }, {} },
     evaluate_property_get_size
-    /* clang-format on */
+      /* clang-format on */
   };
   unsigned long property_size, default_size;
 
   default_size = do_cache2_property_size(&global);
   if (!default_size) {
-    default_size = __android_logger_property_get_bool("ro.config.low_ram",
-                                                      BOOL_DEFAULT_FALSE)
+    default_size = __android_logger_property_get_bool("ro.config.low_ram", BOOL_DEFAULT_FALSE)
                        ? LOG_BUFFER_MIN_SIZE /* 64K  */
                        : LOG_BUFFER_SIZE;    /* 256K */
   }
 
   snprintf(key_persist, sizeof(key_persist), "%s.%s", global_tunable,
            android_log_id_to_name(logId));
-  snprintf(key_ro, sizeof(key_ro), "%s.%s", global_default,
-           android_log_id_to_name(logId));
+  snprintf(key_ro, sizeof(key_ro), "%s.%s", global_default, android_log_id_to_name(logId));
   property_size = do_cache2_property_size(&local);
 
   if (!property_size) {
diff --git a/liblog/stderr_write.c b/liblog/stderr_write.cpp
similarity index 90%
rename from liblog/stderr_write.c
rename to liblog/stderr_write.cpp
index dbe5309..28195aa 100644
--- a/liblog/stderr_write.c
+++ b/liblog/stderr_write.cpp
@@ -37,16 +37,15 @@
 #include <log/event_tag_map.h>
 #include <log/log.h>
 #include <log/logprint.h>
-#include <log/uio.h>
 
 #include "log_portability.h"
 #include "logger.h"
+#include "uio.h"
 
 static int stderrOpen();
 static void stderrClose();
 static int stderrAvailable(log_id_t logId);
-static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
-                       size_t nr);
+static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr);
 
 struct stderrContext {
   AndroidLogFormat* logformat;
@@ -56,13 +55,13 @@
 };
 
 LIBLOG_HIDDEN struct android_log_transport_write stderrLoggerWrite = {
-  .node = { &stderrLoggerWrite.node, &stderrLoggerWrite.node },
-  .context.priv = NULL,
-  .name = "stderr",
-  .available = stderrAvailable,
-  .open = stderrOpen,
-  .close = stderrClose,
-  .write = stderrWrite,
+    .node = {&stderrLoggerWrite.node, &stderrLoggerWrite.node},
+    .context.priv = NULL,
+    .name = "stderr",
+    .available = stderrAvailable,
+    .open = stderrOpen,
+    .close = stderrClose,
+    .write = stderrWrite,
 };
 
 static int stderrOpen() {
@@ -78,7 +77,7 @@
     return fileno(stderr);
   }
 
-  ctx = calloc(1, sizeof(struct stderrContext));
+  ctx = static_cast<stderrContext*>(calloc(1, sizeof(stderrContext)));
   if (!ctx) {
     return -ENOMEM;
   }
@@ -123,7 +122,7 @@
 }
 
 static void stderrClose() {
-  struct stderrContext* ctx = stderrLoggerWrite.context.priv;
+  stderrContext* ctx = static_cast<stderrContext*>(stderrLoggerWrite.context.priv);
 
   if (ctx) {
     stderrLoggerWrite.context.priv = NULL;
@@ -147,14 +146,13 @@
   return 1;
 }
 
-static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
-                       size_t nr) {
+static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
   struct log_msg log_msg;
   AndroidLogEntry entry;
   char binaryMsgBuf[1024];
   int err;
   size_t i;
-  struct stderrContext* ctx = stderrLoggerWrite.context.priv;
+  stderrContext* ctx = static_cast<stderrContext*>(stderrLoggerWrite.context.priv);
 
   if (!ctx) return -EBADF;
   if (!vec || !nr) return -EINVAL;
diff --git a/liblog/tests/Android.bp b/liblog/tests/Android.bp
index e6a9c0c..4ab2acb 100644
--- a/liblog/tests/Android.bp
+++ b/liblog/tests/Android.bp
@@ -29,10 +29,10 @@
         "-fno-builtin",
     ],
     shared_libs: [
-        "liblog",
         "libm",
         "libbase",
     ],
+    static_libs: ["liblog"],
     srcs: ["liblog_benchmark.cpp"],
 }
 
@@ -54,9 +54,7 @@
     srcs: [
         "libc_test.cpp",
         "liblog_test_default.cpp",
-        "liblog_test_local.cpp",
         "liblog_test_stderr.cpp",
-        "liblog_test_stderr_local.cpp",
         "log_id_test.cpp",
         "log_radio_test.cpp",
         "log_read_test.cpp",
@@ -65,10 +63,10 @@
         "log_wrap_test.cpp",
     ],
     shared_libs: [
-        "liblog",
         "libcutils",
         "libbase",
     ],
+    static_libs: ["liblog"],
 }
 
 // Build tests for the device (with .so). Run with:
diff --git a/liblog/tests/liblog_benchmark.cpp b/liblog/tests/liblog_benchmark.cpp
index c2f3f83..c8ac321 100644
--- a/liblog/tests/liblog_benchmark.cpp
+++ b/liblog/tests/liblog_benchmark.cpp
@@ -21,6 +21,7 @@
 #include <sys/socket.h>
 #include <sys/syscall.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <unistd.h>
 
 #include <unordered_set>
diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp
index 383d0e7..83f0fa9 100644
--- a/liblog/tests/liblog_test.cpp
+++ b/liblog/tests/liblog_test.cpp
@@ -52,22 +52,6 @@
 #endif
 #endif
 
-#if (!defined(USING_LOGGER_DEFAULT) || !defined(USING_LOGGER_LOCAL) || \
-     !defined(USING_LOGGER_STDERR))
-#ifdef liblog  // a binary clue that we are overriding the test names
-// Does not support log reading blocking feature yet
-// Does not support LOG_ID_SECURITY (unless we set LOGGER_LOCAL | LOGGER_LOGD)
-// Assume some common aspects are tested by USING_LOGGER_DEFAULT:
-// Does not need to _retest_ pmsg functionality
-// Does not need to _retest_ property handling as it is a higher function
-// Does not need to _retest_ event mapping functionality
-// Does not need to _retest_ ratelimit
-// Does not need to _retest_ logprint
-#define USING_LOGGER_LOCAL
-#else
-#define USING_LOGGER_DEFAULT
-#endif
-#endif
 #ifdef USING_LOGGER_STDERR
 #define SUPPORTS_END_TO_END 0
 #else
@@ -175,7 +159,7 @@
 #endif
 
 TEST(liblog, __android_log_btwrite__android_logger_list_read) {
-#if (defined(__ANDROID__) || defined(USING_LOGGER_LOCAL))
+#ifdef __ANDROID__
 #ifdef TEST_PREFIX
   TEST_PREFIX
 #endif
@@ -269,7 +253,7 @@
 #endif
 }
 
-#if (defined(__ANDROID__) || defined(USING_LOGGER_LOCAL))
+#ifdef __ANDROID__
 static void print_transport(const char* prefix, int logger) {
   static const char orstr[] = " | ";
 
@@ -297,16 +281,11 @@
     fprintf(stderr, "%sLOGGER_NULL", prefix);
     prefix = orstr;
   }
-  if (logger & LOGGER_LOCAL) {
-    fprintf(stderr, "%sLOGGER_LOCAL", prefix);
-    prefix = orstr;
-  }
   if (logger & LOGGER_STDERR) {
     fprintf(stderr, "%sLOGGER_STDERR", prefix);
     prefix = orstr;
   }
-  logger &= ~(LOGGER_LOGD | LOGGER_KERNEL | LOGGER_NULL | LOGGER_LOCAL |
-              LOGGER_STDERR);
+  logger &= ~(LOGGER_LOGD | LOGGER_KERNEL | LOGGER_NULL | LOGGER_STDERR);
   if (logger) {
     fprintf(stderr, "%s0x%x", prefix, logger);
     prefix = orstr;
@@ -321,7 +300,7 @@
 // and behind us, to make us whole.  We could incorporate a prefix and
 // suffix test to make this standalone, but opted to not complicate this.
 TEST(liblog, android_set_log_transport) {
-#if (defined(__ANDROID__) || defined(USING_LOGGER_LOCAL))
+#ifdef __ANDROID__
 #ifdef TEST_PREFIX
   TEST_PREFIX
 #endif
@@ -632,7 +611,7 @@
   buf_write_test("\n Hello World \n");
 }
 
-#ifndef USING_LOGGER_LOCAL  // requires blocking reader functionality
+#ifdef USING_LOGGER_DEFAULT  // requires blocking reader functionality
 #ifdef TEST_PREFIX
 static unsigned signaled;
 static log_time signal_time;
@@ -944,7 +923,7 @@
   GTEST_LOG_(INFO) << "This test does nothing.\n";
 #endif
 }
-#endif  // !USING_LOGGER_LOCAL
+#endif  // USING_LOGGER_DEFAULT
 
 #ifdef TEST_PREFIX
 static const char max_payload_tag[] = "TEST_max_payload_and_longish_tag_XXXX";
@@ -2417,7 +2396,7 @@
 }
 
 // Do not retest logger list handling
-#if (defined(TEST_PREFIX) || !defined(USING_LOGGER_LOCAL))
+#ifdef TEST_PREFIX
 static int is_real_element(int type) {
   return ((type == EVENT_TYPE_INT) || (type == EVENT_TYPE_LONG) ||
           (type == EVENT_TYPE_STRING) || (type == EVENT_TYPE_FLOAT));
@@ -2572,7 +2551,7 @@
 
   return 0;
 }
-#endif  // TEST_PREFIX || !USING_LOGGER_LOCAL
+#endif  // TEST_PREFIX
 
 #ifdef TEST_PREFIX
 static const char* event_test_int32(uint32_t tag, size_t& expected_len) {
@@ -3181,41 +3160,6 @@
 }
 #endif  // USING_LOGGER_DEFAULT
 
-#ifdef USING_LOGGER_DEFAULT  // Do not retest ratelimit
-TEST(liblog, __android_log_ratelimit) {
-  time_t state = 0;
-
-  errno = 42;
-  // Prime
-  __android_log_ratelimit(3, &state);
-  EXPECT_EQ(errno, 42);
-  // Check
-  EXPECT_FALSE(__android_log_ratelimit(3, &state));
-  sleep(1);
-  EXPECT_FALSE(__android_log_ratelimit(3, &state));
-  sleep(4);
-  EXPECT_TRUE(__android_log_ratelimit(3, &state));
-  sleep(5);
-  EXPECT_TRUE(__android_log_ratelimit(3, &state));
-
-  // API checks
-  IF_ALOG_RATELIMIT_LOCAL(3, &state) {
-    EXPECT_FALSE(0 != "IF_ALOG_RATELIMIT_LOCAL(3, &state)");
-  }
-
-  IF_ALOG_RATELIMIT() {
-    ;
-  }
-  else {
-    EXPECT_TRUE(0 == "IF_ALOG_RATELIMIT()");
-  }
-  IF_ALOG_RATELIMIT() {
-    EXPECT_FALSE(0 != "IF_ALOG_RATELIMIT()");
-  }
-  // Do not test default seconds, to allow liblog to tune freely
-}
-#endif  // USING_LOGGER_DEFAULT
-
 #ifdef USING_LOGGER_DEFAULT  // Do not retest event mapping functionality
 TEST(liblog, android_lookupEventTagNum) {
 #ifdef __ANDROID__
diff --git a/liblog/tests/liblog_test_local.cpp b/liblog/tests/liblog_test_local.cpp
deleted file mode 100644
index 451beca..0000000
--- a/liblog/tests/liblog_test_local.cpp
+++ /dev/null
@@ -1,4 +0,0 @@
-#include <log/log_transport.h>
-#define liblog liblog_local
-#define TEST_LOGGER LOGGER_LOCAL
-#include "liblog_test.cpp"
diff --git a/liblog/tests/liblog_test_stderr_local.cpp b/liblog/tests/liblog_test_stderr_local.cpp
deleted file mode 100644
index bb5c7ae..0000000
--- a/liblog/tests/liblog_test_stderr_local.cpp
+++ /dev/null
@@ -1,4 +0,0 @@
-#include <log/log_transport.h>
-#define liblog liblog_stderr_local
-#define TEST_LOGGER (LOGGER_LOCAL | LOGGER_STDERR)
-#include "liblog_test.cpp"
diff --git a/liblog/tests/log_time_test.cpp b/liblog/tests/log_time_test.cpp
index 0ae1d18..47fe594 100644
--- a/liblog/tests/log_time_test.cpp
+++ b/liblog/tests/log_time_test.cpp
@@ -21,12 +21,6 @@
 #include <log/log_time.h>
 
 TEST(liblog, log_time) {
-#ifdef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
-  log_time(CLOCK_MONOTONIC);
-
-  EXPECT_EQ(log_time, log_time::EPOCH);
-#endif
-
   struct timespec ts;
   clock_gettime(CLOCK_MONOTONIC, &ts);
   log_time tl(ts);
diff --git a/liblog/uio.c b/liblog/uio.c
deleted file mode 100644
index e127202..0000000
--- a/liblog/uio.c
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (C) 2007-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.
- */
-
-#if defined(_WIN32)
-
-#include <unistd.h>
-
-#include <log/uio.h>
-
-#include "log_portability.h"
-
-LIBLOG_ABI_PUBLIC int readv(int fd, struct iovec* vecs, int count) {
-  int total = 0;
-
-  for (; count > 0; count--, vecs++) {
-    char* buf = vecs->iov_base;
-    int len = vecs->iov_len;
-
-    while (len > 0) {
-      int ret = read(fd, buf, len);
-      if (ret < 0) {
-        if (total == 0) total = -1;
-        goto Exit;
-      }
-      if (ret == 0) goto Exit;
-
-      total += ret;
-      buf += ret;
-      len -= ret;
-    }
-  }
-Exit:
-  return total;
-}
-
-LIBLOG_ABI_PUBLIC int writev(int fd, const struct iovec* vecs, int count) {
-  int total = 0;
-
-  for (; count > 0; count--, vecs++) {
-    const char* buf = vecs->iov_base;
-    int len = vecs->iov_len;
-
-    while (len > 0) {
-      int ret = write(fd, buf, len);
-      if (ret < 0) {
-        if (total == 0) total = -1;
-        goto Exit;
-      }
-      if (ret == 0) goto Exit;
-
-      total += ret;
-      buf += ret;
-      len -= ret;
-    }
-  }
-Exit:
-  return total;
-}
-
-#endif
diff --git a/liblog/include/log/uio.h b/liblog/uio.h
similarity index 62%
rename from liblog/include/log/uio.h
rename to liblog/uio.h
index a492bae..c85893c 100644
--- a/liblog/include/log/uio.h
+++ b/liblog/uio.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2014 The Android Open Source Project
+ * Copyright (C) 2019 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,37 +14,14 @@
  * limitations under the License.
  */
 
-#ifndef _LIBS_CUTILS_UIO_H
-#define _LIBS_CUTILS_UIO_H
+#pragma once
 
-#if !defined(_WIN32)
-
-#include <sys/uio.h>
-
-#else
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-//
-// Implementation of sys/uio.h for Win32.
-//
-
+#if defined(_WIN32)
 #include <stddef.h>
-
 struct iovec {
   void* iov_base;
   size_t iov_len;
 };
-
-extern int readv(int fd, struct iovec* vecs, int count);
-extern int writev(int fd, const struct iovec* vecs, int count);
-
-#ifdef __cplusplus
-}
+#else
+#include <sys/uio.h>
 #endif
-
-#endif
-
-#endif /* _LIBS_UTILS_UIO_H */
diff --git a/libmeminfo/include/meminfo/meminfo.h b/libmeminfo/include/meminfo/meminfo.h
index 5ee32d4..2fc7867 100644
--- a/libmeminfo/include/meminfo/meminfo.h
+++ b/libmeminfo/include/meminfo/meminfo.h
@@ -72,15 +72,10 @@
         : start(s), end(e), offset(off), flags(f), name(n) {}
     ~Vma() = default;
 
-    void clear() {
-        memset(&usage, 0, sizeof(usage));
-        memset(&wss, 0, sizeof(wss));
-    }
+    void clear() { memset(&usage, 0, sizeof(usage)); }
 
     // Memory usage of this mapping.
     MemUsage usage;
-    // Working set within this mapping.
-    MemUsage wss;
 };
 
 }  // namespace meminfo
diff --git a/libmeminfo/include/meminfo/procmeminfo.h b/libmeminfo/include/meminfo/procmeminfo.h
index 0bfd80f..0b66074 100644
--- a/libmeminfo/include/meminfo/procmeminfo.h
+++ b/libmeminfo/include/meminfo/procmeminfo.h
@@ -64,7 +64,12 @@
     //   private_dirty
     //   SwapPss
     // All other fields of MemUsage are zeroed.
-    bool SmapsOrRollup(bool use_rollup, MemUsage* stats) const;
+    bool SmapsOrRollup(MemUsage* stats) const;
+
+    // Used to parse either of /proc/<pid>/{smaps, smaps_rollup} and record the process's
+    // Pss.
+    // Returns 'true' on success and the value of Pss in the out parameter.
+    bool SmapsOrRollupPss(uint64_t* pss) const;
 
     const std::vector<uint16_t>& SwapOffsets();
 
@@ -82,7 +87,6 @@
     std::vector<Vma> maps_;
 
     MemUsage usage_;
-    MemUsage wss_;
     std::vector<uint16_t> swap_offsets_;
 };
 
@@ -90,10 +94,21 @@
 // same format as /proc/<pid>/smaps. Returns 'false' if the file is malformed.
 bool ForEachVmaFromFile(const std::string& path, const VmaCallback& callback);
 
+// Returns if the kernel supports /proc/<pid>/smaps_rollup. Assumes that the
+// calling process has access to the /proc/<pid>/smaps_rollup.
+// Returns 'false' if the calling process has no permission to read the file if it exists
+// of if the file doesn't exist.
+bool IsSmapsRollupSupported(pid_t pid);
+
 // Same as ProcMemInfo::SmapsOrRollup but reads the statistics directly
 // from a file. The file MUST be in the same format as /proc/<pid>/smaps
 // or /proc/<pid>/smaps_rollup
 bool SmapsOrRollupFromFile(const std::string& path, MemUsage* stats);
 
+// Same as ProcMemInfo::SmapsOrRollupPss but reads the statistics directly
+// from a file and returns total Pss in kB. The file MUST be in the same format
+// as /proc/<pid>/smaps or /proc/<pid>/smaps_rollup
+bool SmapsOrRollupPssFromFile(const std::string& path, uint64_t* pss);
+
 }  // namespace meminfo
 }  // namespace android
diff --git a/libmeminfo/libmeminfo_test.cpp b/libmeminfo/libmeminfo_test.cpp
index 20ed3bf..7d85dd2 100644
--- a/libmeminfo/libmeminfo_test.cpp
+++ b/libmeminfo/libmeminfo_test.cpp
@@ -284,13 +284,13 @@
     EXPECT_EQ(swap_offsets.size(), 0);
 }
 
-TEST(TestProcMemInfo, SmapsOrRollupReturn) {
-    // if /proc/<pid>/smaps_rollup file exists, .SmapsRollup() must return true;
-    // false otherwise
+TEST(TestProcMemInfo, IsSmapsSupportedTest) {
     std::string path = ::android::base::StringPrintf("/proc/%d/smaps_rollup", pid);
-    ProcMemInfo proc_mem(pid);
-    MemUsage stats;
-    EXPECT_EQ(!access(path.c_str(), F_OK), proc_mem.SmapsOrRollup(true, &stats));
+    bool supported = IsSmapsRollupSupported(pid);
+    EXPECT_EQ(!access(path.c_str(), F_OK | R_OK), supported);
+    // Second call must return what the first one returned regardless of the pid parameter.
+    // So, deliberately pass invalid pid.
+    EXPECT_EQ(supported, IsSmapsRollupSupported(-1));
 }
 
 TEST(TestProcMemInfo, SmapsOrRollupTest) {
@@ -365,6 +365,50 @@
     EXPECT_EQ(stats.swap_pss, 70);
 }
 
+TEST(TestProcMemInfo, SmapsOrRollupPssRollupTest) {
+    // This is a made up smaps for the test
+    std::string smaps =
+            R"smaps(12c00000-13440000 rw-p 00000000 00:00 0                                  [anon:dalvik-main space (region space)]
+Name:           [anon:dalvik-main space (region space)]
+Size:               8448 kB
+KernelPageSize:        4 kB
+MMUPageSize:           4 kB
+Rss:                2652 kB
+Pss:                2652 kB
+Shared_Clean:        840 kB
+Shared_Dirty:         40 kB
+Private_Clean:        84 kB
+Private_Dirty:      2652 kB
+Referenced:         2652 kB
+Anonymous:          2652 kB
+AnonHugePages:         0 kB
+ShmemPmdMapped:        0 kB
+Shared_Hugetlb:        0 kB
+Private_Hugetlb:       0 kB
+Swap:                102 kB
+SwapPss:              70 kB
+Locked:             2652 kB
+VmFlags: rd wr mr mw me ac 
+)smaps";
+
+    TemporaryFile tf;
+    ASSERT_TRUE(tf.fd != -1);
+    ASSERT_TRUE(::android::base::WriteStringToFd(smaps, tf.fd));
+
+    uint64_t pss;
+    ASSERT_EQ(SmapsOrRollupPssFromFile(tf.path, &pss), true);
+    EXPECT_EQ(pss, 2652);
+}
+
+TEST(TestProcMemInfo, SmapsOrRollupPssSmapsTest) {
+    std::string exec_dir = ::android::base::GetExecutableDirectory();
+    std::string path = ::android::base::StringPrintf("%s/testdata1/smaps_short", exec_dir.c_str());
+
+    uint64_t pss;
+    ASSERT_EQ(SmapsOrRollupPssFromFile(path, &pss), true);
+    EXPECT_EQ(pss, 19119);
+}
+
 TEST(TestProcMemInfo, ForEachVmaFromFileTest) {
     std::string exec_dir = ::android::base::GetExecutableDirectory();
     std::string path = ::android::base::StringPrintf("%s/testdata1/smaps_short", exec_dir.c_str());
@@ -374,6 +418,9 @@
     auto collect_vmas = [&](const Vma& v) { vmas.push_back(v); };
     ASSERT_TRUE(ForEachVmaFromFile(path, collect_vmas));
 
+    // We should get a total of 6 vmas
+    ASSERT_EQ(vmas.size(), 6);
+
     // Expect values to be equal to what we have in testdata1/smaps_short
     // Check for sizes first
     ASSERT_EQ(vmas[0].usage.vss, 32768);
@@ -468,6 +515,8 @@
     auto vmas = proc_mem.Smaps(path);
 
     ASSERT_FALSE(vmas.empty());
+    // We should get a total of 6 vmas
+    ASSERT_EQ(vmas.size(), 6);
 
     // Expect values to be equal to what we have in testdata1/smaps_short
     // Check for sizes first
diff --git a/libmeminfo/procmeminfo.cpp b/libmeminfo/procmeminfo.cpp
index 1f8db1a..d6332a3 100644
--- a/libmeminfo/procmeminfo.cpp
+++ b/libmeminfo/procmeminfo.cpp
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include <atomic>
 #include <fstream>
 #include <iostream>
 #include <memory>
@@ -157,14 +158,14 @@
     if (!get_wss_) {
         LOG(WARNING) << "Trying to read process working set for " << pid_
                      << " using invalid object";
-        return wss_;
+        return usage_;
     }
 
     if (maps_.empty() && !ReadMaps(get_wss_)) {
         LOG(ERROR) << "Failed to get working set for Process " << pid_;
     }
 
-    return wss_;
+    return usage_;
 }
 
 bool ProcMemInfo::ForEachVma(const VmaCallback& callback) {
@@ -172,11 +173,17 @@
     return ForEachVmaFromFile(path, callback);
 }
 
-bool ProcMemInfo::SmapsOrRollup(bool use_rollup, MemUsage* stats) const {
-    std::string path = ::android::base::StringPrintf("/proc/%d/%s", pid_,
-                                                     use_rollup ? "smaps_rollup" : "smaps");
+bool ProcMemInfo::SmapsOrRollup(MemUsage* stats) const {
+    std::string path = ::android::base::StringPrintf(
+            "/proc/%d/%s", pid_, IsSmapsRollupSupported(pid_) ? "smaps_rollup" : "smaps");
     return SmapsOrRollupFromFile(path, stats);
-};
+}
+
+bool ProcMemInfo::SmapsOrRollupPss(uint64_t* pss) const {
+    std::string path = ::android::base::StringPrintf(
+            "/proc/%d/%s", pid_, IsSmapsRollupSupported(pid_) ? "smaps_rollup" : "smaps");
+    return SmapsOrRollupPssFromFile(path, pss);
+}
 
 const std::vector<uint16_t>& ProcMemInfo::SwapOffsets() {
     if (get_wss_) {
@@ -228,11 +235,7 @@
             maps_.clear();
             return false;
         }
-        if (get_wss) {
-            add_mem_usage(&wss_, vma.wss);
-        } else {
-            add_mem_usage(&usage_, vma.usage);
-        }
+        add_mem_usage(&usage_, vma.usage);
     }
 
     return true;
@@ -300,31 +303,20 @@
             // This effectively makes vss = rss for the working set is requested.
             // The libpagemap implementation returns vss > rss for
             // working set, which doesn't make sense.
-            vma.wss.vss += pagesz;
-            vma.wss.rss += pagesz;
-            vma.wss.uss += is_private ? pagesz : 0;
-            vma.wss.pss += pagesz / pg_counts[i];
-            if (is_private) {
-                vma.wss.private_dirty += is_dirty ? pagesz : 0;
-                vma.wss.private_clean += is_dirty ? 0 : pagesz;
-            } else {
-                vma.wss.shared_dirty += is_dirty ? pagesz : 0;
-                vma.wss.shared_clean += is_dirty ? 0 : pagesz;
-            }
+            vma.usage.vss += pagesz;
+        }
+
+        vma.usage.rss += pagesz;
+        vma.usage.uss += is_private ? pagesz : 0;
+        vma.usage.pss += pagesz / pg_counts[i];
+        if (is_private) {
+            vma.usage.private_dirty += is_dirty ? pagesz : 0;
+            vma.usage.private_clean += is_dirty ? 0 : pagesz;
         } else {
-            vma.usage.rss += pagesz;
-            vma.usage.uss += is_private ? pagesz : 0;
-            vma.usage.pss += pagesz / pg_counts[i];
-            if (is_private) {
-                vma.usage.private_dirty += is_dirty ? pagesz : 0;
-                vma.usage.private_clean += is_dirty ? 0 : pagesz;
-            } else {
-                vma.usage.shared_dirty += is_dirty ? pagesz : 0;
-                vma.usage.shared_clean += is_dirty ? 0 : pagesz;
-            }
+            vma.usage.shared_dirty += is_dirty ? pagesz : 0;
+            vma.usage.shared_clean += is_dirty ? 0 : pagesz;
         }
     }
-
     return true;
 }
 
@@ -338,8 +330,9 @@
     char* line = nullptr;
     bool parsing_vma = false;
     ssize_t line_len;
+    size_t line_alloc = 0;
     Vma vma;
-    while ((line_len = getline(&line, 0, fp.get())) > 0) {
+    while ((line_len = getline(&line, &line_alloc, fp.get())) > 0) {
         // Make sure the line buffer terminates like a C string for ReadMapFile
         line[line_len] = '\0';
 
@@ -382,15 +375,41 @@
     return true;
 }
 
+enum smaps_rollup_support { UNTRIED, SUPPORTED, UNSUPPORTED };
+
+static std::atomic<smaps_rollup_support> g_rollup_support = UNTRIED;
+
+bool IsSmapsRollupSupported(pid_t pid) {
+    // Similar to OpenSmapsOrRollup checks from android_os_Debug.cpp, except
+    // the method only checks if rollup is supported and returns the status
+    // right away.
+    enum smaps_rollup_support rollup_support = g_rollup_support.load(std::memory_order_relaxed);
+    if (rollup_support != UNTRIED) {
+        return rollup_support == SUPPORTED;
+    }
+    std::string rollup_file = ::android::base::StringPrintf("/proc/%d/smaps_rollup", pid);
+    if (access(rollup_file.c_str(), F_OK | R_OK)) {
+        // No check for errno = ENOENT necessary here. The caller MUST fallback to
+        // using /proc/<pid>/smaps instead anyway.
+        g_rollup_support.store(UNSUPPORTED, std::memory_order_relaxed);
+        return false;
+    }
+
+    g_rollup_support.store(SUPPORTED, std::memory_order_relaxed);
+    LOG(INFO) << "Using smaps_rollup for pss collection";
+    return true;
+}
+
 bool SmapsOrRollupFromFile(const std::string& path, MemUsage* stats) {
     auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
     if (fp == nullptr) {
         return false;
     }
 
-    char line[1024];
+    char* line = nullptr;
+    size_t line_alloc = 0;
     stats->clear();
-    while (fgets(line, sizeof(line), fp.get()) != nullptr) {
+    while (getline(&line, &line_alloc, fp.get()) > 0) {
         switch (line[0]) {
             case 'P':
                 if (strncmp(line, "Pss:", 4) == 0) {
@@ -423,6 +442,28 @@
         }
     }
 
+    // free getline() managed buffer
+    free(line);
+    return true;
+}
+
+bool SmapsOrRollupPssFromFile(const std::string& path, uint64_t* pss) {
+    auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
+    if (fp == nullptr) {
+        return false;
+    }
+    *pss = 0;
+    char* line = nullptr;
+    size_t line_alloc = 0;
+    while (getline(&line, &line_alloc, fp.get()) > 0) {
+        uint64_t v;
+        if (sscanf(line, "Pss: %" SCNu64 " kB", &v) == 1) {
+            *pss += v;
+        }
+    }
+
+    // free getline() managed buffer
+    free(line);
     return true;
 }
 
diff --git a/libmeminfo/tools/procmem.cpp b/libmeminfo/tools/procmem.cpp
index b9b174d..47881ed 100644
--- a/libmeminfo/tools/procmem.cpp
+++ b/libmeminfo/tools/procmem.cpp
@@ -98,7 +98,7 @@
     std::stringstream ss;
     print_header(ss);
     for (auto& vma : maps) {
-        const MemUsage& vma_stats = show_wss ? vma.wss : vma.usage;
+        const MemUsage& vma_stats = vma.usage;
         if (hide_zeroes && vma_stats.rss == 0) {
             continue;
         }
@@ -116,14 +116,14 @@
 int main(int argc, char* argv[]) {
     int opt;
     auto pss_sort = [](const Vma& a, const Vma& b) {
-        uint64_t pss_a = show_wss ? a.wss.pss : a.usage.pss;
-        uint64_t pss_b = show_wss ? b.wss.pss : b.usage.pss;
+        uint64_t pss_a = a.usage.pss;
+        uint64_t pss_b = b.usage.pss;
         return pss_a > pss_b;
     };
 
     auto uss_sort = [](const Vma& a, const Vma& b) {
-        uint64_t uss_a = show_wss ? a.wss.uss : a.usage.uss;
-        uint64_t uss_b = show_wss ? b.wss.uss : b.usage.uss;
+        uint64_t uss_a = a.usage.uss;
+        uint64_t uss_b = b.usage.uss;
         return uss_a > uss_b;
     };
 
@@ -182,7 +182,7 @@
     }
 
     ProcMemInfo proc(pid, show_wss);
-    const MemUsage& proc_stats = show_wss ? proc.Wss() : proc.Usage();
+    const MemUsage& proc_stats = proc.Usage();
     std::vector<Vma> maps(proc.Maps());
     if (sort_func != nullptr) {
         std::sort(maps.begin(), maps.end(), sort_func);
diff --git a/libmeminfo/tools/procrank.cpp b/libmeminfo/tools/procrank.cpp
index a751722..21a684c 100644
--- a/libmeminfo/tools/procrank.cpp
+++ b/libmeminfo/tools/procrank.cpp
@@ -465,7 +465,7 @@
         }
 
         // Skip processes with no memory mappings
-        uint64_t vss = show_wss ? proc.Wss().vss : proc.Usage().vss;
+        uint64_t vss = proc.Usage().vss;
         if (vss == 0) return true;
 
         // collect swap_offset counts from all processes in 1st pass
diff --git a/libnativebridge/Android.bp b/libnativebridge/Android.bp
index 92fd2a2..c54570e 100644
--- a/libnativebridge/Android.bp
+++ b/libnativebridge/Android.bp
@@ -17,6 +17,20 @@
         "liblog",
     ],
 
+    target: {
+        android: {
+            version_script: "libnativebridge.map.txt",
+        },
+        linux: {
+            version_script: "libnativebridge.map.txt",
+        },
+    },
+
+    stubs: {
+        symbol_file: "libnativebridge.map.txt",
+        versions: ["1"],
+    },
+
     export_include_dirs: ["include"],
 
     cflags: [
diff --git a/libnativebridge/OWNERS b/libnativebridge/OWNERS
index f2cc942..6f0824b 100644
--- a/libnativebridge/OWNERS
+++ b/libnativebridge/OWNERS
@@ -1 +1,2 @@
 dimitry@google.com
+eaeltsin@google.com
diff --git a/libnativebridge/include/nativebridge/native_bridge.h b/libnativebridge/include/nativebridge/native_bridge.h
index 28f1927..5aea967 100644
--- a/libnativebridge/include/nativebridge/native_bridge.h
+++ b/libnativebridge/include/nativebridge/native_bridge.h
@@ -17,12 +17,17 @@
 #ifndef NATIVE_BRIDGE_H_
 #define NATIVE_BRIDGE_H_
 
-#include "jni.h"
 #include <signal.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <sys/types.h>
 
+#include "jni.h"
+
+#ifdef __cplusplus
 namespace android {
+extern "C" {
+#endif  // __cplusplus
 
 struct NativeBridgeRuntimeCallbacks;
 struct NativeBridgeRuntimeValues;
@@ -32,11 +37,10 @@
 // to the chain.
 typedef bool (*NativeBridgeSignalHandlerFn)(int, siginfo_t*, void*);
 
-
 // Open the native bridge, if any. Should be called by Runtime::Init(). A null library filename
 // signals that we do not want to load a native bridge.
 bool LoadNativeBridge(const char* native_bridge_library_filename,
-                      const NativeBridgeRuntimeCallbacks* runtime_callbacks);
+                      const struct NativeBridgeRuntimeCallbacks* runtime_callbacks);
 
 // Quick check whether a native bridge will be needed. This is based off of the instruction set
 // of the process.
@@ -138,19 +142,17 @@
 //
 // Starting with v3, NativeBridge has two scenarios: with/without namespace.
 // Should not use in non-namespace scenario.
-native_bridge_namespace_t* NativeBridgeCreateNamespace(const char* name,
-                                                       const char* ld_library_path,
-                                                       const char* default_library_path,
-                                                       uint64_t type,
-                                                       const char* permitted_when_isolated_path,
-                                                       native_bridge_namespace_t* parent_ns);
+struct native_bridge_namespace_t* NativeBridgeCreateNamespace(
+    const char* name, const char* ld_library_path, const char* default_library_path, uint64_t type,
+    const char* permitted_when_isolated_path, struct native_bridge_namespace_t* parent_ns);
 
 // Creates a link which shares some libraries from one namespace to another.
 // NativeBridge's peer of android_link_namespaces() of dynamic linker.
 //
 // Starting with v3, NativeBridge has two scenarios: with/without namespace.
 // Should not use in non-namespace scenario.
-bool NativeBridgeLinkNamespaces(native_bridge_namespace_t* from, native_bridge_namespace_t* to,
+bool NativeBridgeLinkNamespaces(struct native_bridge_namespace_t* from,
+                                struct native_bridge_namespace_t* to,
                                 const char* shared_libs_sonames);
 
 // Load a shared library with namespace key that is supported by the native bridge.
@@ -159,10 +161,11 @@
 //
 // Starting with v3, NativeBridge has two scenarios: with/without namespace.
 // Use NativeBridgeLoadLibrary() instead in non-namespace scenario.
-void* NativeBridgeLoadLibraryExt(const char* libpath, int flag, native_bridge_namespace_t* ns);
+void* NativeBridgeLoadLibraryExt(const char* libpath, int flag,
+                                 struct native_bridge_namespace_t* ns);
 
 // Returns vendor namespace if it is enabled for the device and null otherwise
-native_bridge_namespace_t* NativeBridgeGetVendorNamespace();
+struct native_bridge_namespace_t* NativeBridgeGetVendorNamespace();
 
 // Native bridge interfaces to runtime.
 struct NativeBridgeCallbacks {
@@ -177,8 +180,8 @@
   //   runtime_cbs [IN] the pointer to NativeBridgeRuntimeCallbacks.
   // Returns:
   //   true if initialization was successful.
-  bool (*initialize)(const NativeBridgeRuntimeCallbacks* runtime_cbs, const char* private_dir,
-                     const char* instruction_set);
+  bool (*initialize)(const struct NativeBridgeRuntimeCallbacks* runtime_cbs,
+                     const char* private_dir, const char* instruction_set);
 
   // Load a shared library that is supported by the native bridge.
   //
@@ -314,12 +317,12 @@
   //
   // Starting with v3, NativeBridge has two scenarios: with/without namespace.
   // Should not use in non-namespace scenario.
-  native_bridge_namespace_t* (*createNamespace)(const char* name,
-                                                const char* ld_library_path,
-                                                const char* default_library_path,
-                                                uint64_t type,
-                                                const char* permitted_when_isolated_path,
-                                                native_bridge_namespace_t* parent_ns);
+  struct native_bridge_namespace_t* (*createNamespace)(const char* name,
+                                                       const char* ld_library_path,
+                                                       const char* default_library_path,
+                                                       uint64_t type,
+                                                       const char* permitted_when_isolated_path,
+                                                       struct native_bridge_namespace_t* parent_ns);
 
   // Creates a link which shares some libraries from one namespace to another.
   // NativeBridge's peer of android_link_namespaces() of dynamic linker.
@@ -334,8 +337,8 @@
   //
   // Starting with v3, NativeBridge has two scenarios: with/without namespace.
   // Should not use in non-namespace scenario.
-  bool (*linkNamespaces)(native_bridge_namespace_t* from, native_bridge_namespace_t* to,
-                         const char* shared_libs_sonames);
+  bool (*linkNamespaces)(struct native_bridge_namespace_t* from,
+                         struct native_bridge_namespace_t* to, const char* shared_libs_sonames);
 
   // Load a shared library within a namespace.
   // NativeBridge's peer of android_dlopen_ext() of dynamic linker, only supports namespace
@@ -350,7 +353,7 @@
   //
   // Starting with v3, NativeBridge has two scenarios: with/without namespace.
   // Use loadLibrary instead in non-namespace scenario.
-  void* (*loadLibraryExt)(const char* libpath, int flag, native_bridge_namespace_t* ns);
+  void* (*loadLibraryExt)(const char* libpath, int flag, struct native_bridge_namespace_t* ns);
 
   // Get native bridge version of vendor namespace.
   // The vendor namespace is the namespace used to load vendor public libraries.
@@ -359,7 +362,7 @@
   //
   // Returns:
   //   vendor namespace or null if it was not set up for the device
-  native_bridge_namespace_t* (*getVendorNamespace)();
+  struct native_bridge_namespace_t* (*getVendorNamespace)();
 };
 
 // Runtime interfaces to native bridge.
@@ -396,6 +399,9 @@
                                uint32_t method_count);
 };
 
-};  // namespace android
+#ifdef __cplusplus
+}  // extern "C"
+}  // namespace android
+#endif  // __cplusplus
 
 #endif  // NATIVE_BRIDGE_H_
diff --git a/libnativebridge/libnativebridge.map.txt b/libnativebridge/libnativebridge.map.txt
new file mode 100644
index 0000000..a616b85
--- /dev/null
+++ b/libnativebridge/libnativebridge.map.txt
@@ -0,0 +1,45 @@
+#
+# Copyright (C) 2019 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.
+#
+
+# TODO(b/122710865): Most of these uses come from libnativeloader, which should be bundled
+# together with libnativebridge in the APEX. Once this happens, prune this list.
+LIBNATIVEBRIDGE_1 {
+  global:
+    NativeBridgeIsSupported;
+    NativeBridgeLoadLibrary;
+    NativeBridgeUnloadLibrary;
+    NativeBridgeGetError;
+    NativeBridgeIsPathSupported;
+    NativeBridgeCreateNamespace;
+    NativeBridgeGetVendorNamespace;
+    NativeBridgeLinkNamespaces;
+    NativeBridgeLoadLibraryExt;
+    NativeBridgeInitAnonymousNamespace;
+    NativeBridgeInitialized;
+    NativeBridgeGetTrampoline;
+    LoadNativeBridge;
+    PreInitializeNativeBridge;
+    InitializeNativeBridge;
+    NativeBridgeGetVersion;
+    NativeBridgeGetSignalHandler;
+    UnloadNativeBridge;
+    NativeBridgeAvailable;
+    NeedsNativeBridge;
+    NativeBridgeError;
+    NativeBridgeNameAcceptable;
+  local:
+    *;
+};
diff --git a/libnativebridge/native_bridge.cc b/libnativebridge/native_bridge.cc
index e24307a..a2d8d81 100644
--- a/libnativebridge/native_bridge.cc
+++ b/libnativebridge/native_bridge.cc
@@ -33,6 +33,13 @@
 
 namespace android {
 
+#ifdef __APPLE__
+template <typename T>
+void UNUSED(const T&) {}
+#endif
+
+extern "C" {
+
 // Environment values required by the apps running with native bridge.
 struct NativeBridgeRuntimeValues {
     const char* os_arch;
@@ -252,10 +259,6 @@
   return strncmp(instruction_set, ABI_STRING, strlen(ABI_STRING) + 1) != 0;
 }
 
-#ifdef __APPLE__
-template<typename T> void UNUSED(const T&) {}
-#endif
-
 bool PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruction_set) {
   if (state != NativeBridgeState::kOpened) {
     ALOGE("Invalid state: native bridge is expected to be opened.");
@@ -626,4 +629,6 @@
   return nullptr;
 }
 
-};  // namespace android
+}  // extern "C"
+
+}  // namespace android
diff --git a/libnativebridge/tests/Android.bp b/libnativebridge/tests/Android.bp
index 222bc4c..744a4a8 100644
--- a/libnativebridge/tests/Android.bp
+++ b/libnativebridge/tests/Android.bp
@@ -86,3 +86,14 @@
     ],
     header_libs: ["libbase_headers"],
 }
+
+// Build the test for the C API.
+cc_test {
+    name: "libnativebridge-api-tests",
+    host_supported: true,
+    test_per_src: true,
+    srcs: [
+        "NativeBridgeApi.c",
+    ],
+    header_libs: ["libnativebridge-dummy-headers"],
+}
diff --git a/libnativebridge/tests/NativeBridgeApi.c b/libnativebridge/tests/NativeBridgeApi.c
new file mode 100644
index 0000000..7ab71fe
--- /dev/null
+++ b/libnativebridge/tests/NativeBridgeApi.c
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+/* The main purpose of this test is to ensure this C header compiles in C, so
+ * that no C++ features inadvertently leak into the C ABI. */
+#include "nativebridge/native_bridge.h"
+
+int main(int argc, char** argv) {
+  (void)argc;
+  (void)argv;
+  return 0;
+}
diff --git a/libnativeloader/Android.bp b/libnativeloader/Android.bp
index 17983bc..2802d36 100644
--- a/libnativeloader/Android.bp
+++ b/libnativeloader/Android.bp
@@ -24,3 +24,10 @@
         "vndksp.libraries.txt",
     ],
 }
+
+cc_library_headers {
+    name: "libnativeloader-dummy-headers",
+
+    host_supported: true,
+    export_include_dirs: ["include"],
+}
diff --git a/libnativeloader/OWNERS b/libnativeloader/OWNERS
index 5c28a9f..f735653 100644
--- a/libnativeloader/OWNERS
+++ b/libnativeloader/OWNERS
@@ -1,2 +1,6 @@
 dimitry@google.com
 jiyong@google.com
+ngeoffray@google.com
+oth@google.com
+mast@google.com
+rpl@google.com
diff --git a/libnativeloader/include/nativeloader/dlext_namespaces.h b/libnativeloader/include/nativeloader/dlext_namespaces.h
index 43c9329..ca026b3 100644
--- a/libnativeloader/include/nativeloader/dlext_namespaces.h
+++ b/libnativeloader/include/nativeloader/dlext_namespaces.h
@@ -18,6 +18,7 @@
 #define __ANDROID_DLEXT_NAMESPACES_H__
 
 #include <android/dlext.h>
+#include <stdbool.h>
 
 __BEGIN_DECLS
 
@@ -84,12 +85,9 @@
  * If a library or any of its dependencies are outside of the permitted_when_isolated_path
  * and search_path, and it is not part of the public namespace dlopen will fail.
  */
-extern struct android_namespace_t* android_create_namespace(const char* name,
-                                                            const char* ld_library_path,
-                                                            const char* default_library_path,
-                                                            uint64_t type,
-                                                            const char* permitted_when_isolated_path,
-                                                            android_namespace_t* parent);
+extern struct android_namespace_t* android_create_namespace(
+    const char* name, const char* ld_library_path, const char* default_library_path, uint64_t type,
+    const char* permitted_when_isolated_path, struct android_namespace_t* parent);
 
 /*
  * Creates a link between namespaces. Every link has list of sonames of
@@ -107,8 +105,8 @@
  *      step will not go deeper into linked namespaces for this library but
  *      will do so for DT_NEEDED libraries.
  */
-extern bool android_link_namespaces(android_namespace_t* from,
-                                    android_namespace_t* to,
+extern bool android_link_namespaces(struct android_namespace_t* from,
+                                    struct android_namespace_t* to,
                                     const char* shared_libs_sonames);
 
 /*
@@ -124,7 +122,7 @@
  */
 extern void android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size);
 
-extern android_namespace_t* android_get_exported_namespace(const char* name);
+extern struct android_namespace_t* android_get_exported_namespace(const char* name);
 
 __END_DECLS
 
diff --git a/libnativeloader/include/nativeloader/native_loader.h b/libnativeloader/include/nativeloader/native_loader.h
index c1d9d2a..af53dc5 100644
--- a/libnativeloader/include/nativeloader/native_loader.h
+++ b/libnativeloader/include/nativeloader/native_loader.h
@@ -17,14 +17,21 @@
 #ifndef NATIVE_LOADER_H_
 #define NATIVE_LOADER_H_
 
-#include "jni.h"
+#include <stdbool.h>
 #include <stdint.h>
-#include <string>
+#include "jni.h"
 #if defined(__ANDROID__)
 #include <android/dlext.h>
 #endif
 
+#ifdef __cplusplus
 namespace android {
+extern "C" {
+#endif  // __cplusplus
+
+// README: the char** error message parameter being passed
+// to the methods below need to be freed through calling NativeLoaderFreeErrorMessage.
+// It's the caller's responsibility to call that method.
 
 __attribute__((visibility("default")))
 void InitializeNativeLoader();
@@ -38,42 +45,39 @@
                                    jstring library_path,
                                    jstring permitted_path);
 
-__attribute__((visibility("default")))
-void* OpenNativeLibrary(JNIEnv* env,
-                        int32_t target_sdk_version,
-                        const char* path,
-                        jobject class_loader,
-                        jstring library_path,
-                        bool* needs_native_bridge,
-                        std::string* error_msg);
+__attribute__((visibility("default"))) void* OpenNativeLibrary(
+    JNIEnv* env, int32_t target_sdk_version, const char* path, jobject class_loader,
+    jstring library_path, bool* needs_native_bridge, char** error_msg);
 
 __attribute__((visibility("default"))) bool CloseNativeLibrary(void* handle,
                                                                const bool needs_native_bridge,
-                                                               std::string* error_msg);
+                                                               char** error_msg);
+
+__attribute__((visibility("default"))) void NativeLoaderFreeErrorMessage(char* msg);
 
 #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(
+__attribute__((visibility("default"))) struct android_namespace_t* FindNamespaceByClassLoader(
     JNIEnv* env, jobject class_loader);
+// That version works with native bridge namespaces, but requires use of OpenNativeLibrary.
+struct NativeLoaderNamespace;
+__attribute__((visibility("default"))) struct 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);
+__attribute__((visibility("default"))) void* OpenNativeLibraryInNamespace(
+    struct NativeLoaderNamespace* ns, const char* path, bool* needs_native_bridge,
+    char** error_msg);
 #endif
 
 __attribute__((visibility("default")))
 void ResetNativeLoader();
 
-};  // namespace android
+#ifdef __cplusplus
+}  // extern "C"
+}  // namespace android
+#endif  // __cplusplus
 
 #endif  // NATIVE_BRIDGE_H_
diff --git a/libnativeloader/libnativeloader.map.txt b/libnativeloader/libnativeloader.map.txt
new file mode 100644
index 0000000..40c30bd
--- /dev/null
+++ b/libnativeloader/libnativeloader.map.txt
@@ -0,0 +1,31 @@
+#
+# Copyright (C) 2019 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.
+#
+
+# TODO(b/122710865): Prune these uses once the runtime APEX is complete.
+LIBNATIVELOADER_1 {
+  global:
+    OpenNativeLibrary;
+    InitializeNativeLoader;
+    ResetNativeLoader;
+    CloseNativeLibrary;
+    OpenNativeLibraryInNamespace;
+    FindNamespaceByClassLoader;
+    FindNativeLoaderNamespaceByClassLoader;
+    CreateClassLoaderNamespace;
+    NativeLoaderFreeErrorMessage;
+  local:
+    *;
+};
diff --git a/libnativeloader/native_loader.cpp b/libnativeloader/native_loader.cpp
index de7ea08..af7df72 100644
--- a/libnativeloader/native_loader.cpp
+++ b/libnativeloader/native_loader.cpp
@@ -52,7 +52,7 @@
 namespace android {
 
 #if defined(__ANDROID__)
-class NativeLoaderNamespace {
+struct NativeLoaderNamespace {
  public:
   NativeLoaderNamespace()
       : android_ns_(nullptr), native_bridge_ns_(nullptr) { }
@@ -151,14 +151,9 @@
  public:
   LibraryNamespaces() : initialized_(false) { }
 
-  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) {
+  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) {
@@ -628,20 +623,16 @@
   return nullptr;
 }
 
-void* OpenNativeLibrary(JNIEnv* env,
-                        int32_t target_sdk_version,
-                        const char* path,
-                        jobject class_loader,
-                        jstring library_path,
-                        bool* needs_native_bridge,
-                        std::string* error_msg) {
+void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
+                        jobject class_loader, jstring library_path, bool* needs_native_bridge,
+                        char** error_msg) {
 #if defined(__ANDROID__)
   UNUSED(target_sdk_version);
   if (class_loader == nullptr) {
     *needs_native_bridge = false;
     void* handle = dlopen(path, RTLD_NOW);
     if (handle == nullptr) {
-      *error_msg = dlerror();
+      *error_msg = strdup(dlerror());
     }
     return handle;
   }
@@ -652,19 +643,16 @@
   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 ((ns = g_namespaces->Create(env,
-                                   target_sdk_version,
-                                   class_loader,
-                                   false /* is_shared */,
-                                   false /* is_for_vendor */,
-                                   library_path,
-                                   nullptr,
-                                   error_msg)) == nullptr) {
+    std::string create_error_msg;
+    if ((ns = g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */,
+                                   false /* is_for_vendor */, library_path, nullptr,
+                                   &create_error_msg)) == nullptr) {
+      *error_msg = strdup(create_error_msg.c_str());
       return nullptr;
     }
   }
 
-  return OpenNativeLibrary(ns, path, needs_native_bridge, error_msg);
+  return OpenNativeLibraryInNamespace(ns, path, needs_native_bridge, error_msg);
 #else
   UNUSED(env, target_sdk_version, class_loader);
 
@@ -705,35 +693,40 @@
       if (handle != nullptr) {
         return handle;
       }
-      *error_msg = NativeBridgeGetError();
+      *error_msg = strdup(NativeBridgeGetError());
     } else {
-      *error_msg = dlerror();
+      *error_msg = strdup(dlerror());
     }
   }
   return nullptr;
 #endif
 }
 
-bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, std::string* error_msg) {
+bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) {
   bool success;
   if (needs_native_bridge) {
     success = (NativeBridgeUnloadLibrary(handle) == 0);
     if (!success) {
-      *error_msg = NativeBridgeGetError();
+      *error_msg = strdup(NativeBridgeGetError());
     }
   } else {
     success = (dlclose(handle) == 0);
     if (!success) {
-      *error_msg = dlerror();
+      *error_msg = strdup(dlerror());
     }
   }
 
   return success;
 }
 
+void NativeLoaderFreeErrorMessage(char* msg) {
+  // The error messages get allocated through strdup, so we must call free on them.
+  free(msg);
+}
+
 #if defined(__ANDROID__)
-void* OpenNativeLibrary(NativeLoaderNamespace* ns, const char* path, bool* needs_native_bridge,
-                        std::string* error_msg) {
+void* OpenNativeLibraryInNamespace(NativeLoaderNamespace* ns, const char* path,
+                                   bool* needs_native_bridge, char** error_msg) {
   if (ns->is_android_namespace()) {
     android_dlextinfo extinfo;
     extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
@@ -741,14 +734,14 @@
 
     void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo);
     if (handle == nullptr) {
-      *error_msg = dlerror();
+      *error_msg = strdup(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();
+      *error_msg = strdup(NativeBridgeGetError());
     }
     *needs_native_bridge = true;
     return handle;
diff --git a/libnativeloader/test/Android.bp b/libnativeloader/test/Android.bp
index d528f30..1464e39 100644
--- a/libnativeloader/test/Android.bp
+++ b/libnativeloader/test/Android.bp
@@ -69,3 +69,14 @@
         "libbase",
     ],
 }
+
+// Build the test for the C API.
+cc_test {
+    name: "libnativeloader-api-tests",
+    host_supported: true,
+    test_per_src: true,
+    srcs: [
+        "api_test.c",
+    ],
+    header_libs: ["libnativeloader-dummy-headers"],
+}
diff --git a/libnativeloader/test/api_test.c b/libnativeloader/test/api_test.c
new file mode 100644
index 0000000..e7025fd
--- /dev/null
+++ b/libnativeloader/test/api_test.c
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+/* The main purpose of this test is to ensure this C header compiles in C, so
+ * that no C++ features inadvertently leak into the C ABI. */
+#include "nativeloader/native_loader.h"
+
+int main(int argc, char** argv) {
+  (void)argc;
+  (void)argv;
+  return 0;
+}
diff --git a/libpackagelistparser/include/packagelistparser/packagelistparser.h b/libpackagelistparser/include/packagelistparser/packagelistparser.h
index d602c26..8bcc1e2 100644
--- a/libpackagelistparser/include/packagelistparser/packagelistparser.h
+++ b/libpackagelistparser/include/packagelistparser/packagelistparser.h
@@ -53,6 +53,7 @@
     char *seinfo;
     gid_list gids;
     void *private_data;
+    bool profileable_from_shell;
 };
 
 /**
diff --git a/libpackagelistparser/packagelistparser.c b/libpackagelistparser/packagelistparser.c
index 3e1a3d1..4ce2363 100644
--- a/libpackagelistparser/packagelistparser.c
+++ b/libpackagelistparser/packagelistparser.c
@@ -223,6 +223,23 @@
             }
         }
 
+        cur = strsep(&next, " \t\r\n");
+        if (cur) {
+            tmp = strtoul(cur, &endptr, 10);
+            if (*endptr != '\0') {
+                errmsg = "Could not convert field \"profileable_from_shell\" to integer value";
+                goto err;
+            }
+
+            /* should be a valid boolean of 1 or 0 */
+            if (!(tmp == 0 || tmp == 1)) {
+                errmsg = "Field \"profileable_from_shell\" is not 0 or 1 boolean value";
+                goto err;
+            }
+
+            pkg_info->profileable_from_shell = (bool)tmp;
+        }
+
         rc = callback(pkg_info, userdata);
         if (rc == false) {
             /*
diff --git a/libstats/include/stats_event_list.h b/libstats/include/stats_event_list.h
index 162d1cf..b5bc5af 100644
--- a/libstats/include/stats_event_list.h
+++ b/libstats/include/stats_event_list.h
@@ -51,10 +51,6 @@
     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() {
@@ -251,9 +247,6 @@
         }
         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
diff --git a/libstats/statsd_writer.c b/libstats/statsd_writer.c
index f00fc2d..f5be95c 100644
--- a/libstats/statsd_writer.c
+++ b/libstats/statsd_writer.c
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <sys/un.h>
 #include <time.h>
 #include <unistd.h>
diff --git a/libunwindstack/Android.bp b/libunwindstack/Android.bp
index 89d4fc0..73dc921 100644
--- a/libunwindstack/Android.bp
+++ b/libunwindstack/Android.bp
@@ -127,6 +127,10 @@
         },
     },
 
+    whole_static_libs: [
+        "libdemangle"
+    ],
+
     static_libs: [
         "libprocinfo",
     ],
@@ -189,6 +193,7 @@
         "tests/MapInfoCreateMemoryTest.cpp",
         "tests/MapInfoGetElfTest.cpp",
         "tests/MapInfoGetLoadBiasTest.cpp",
+        "tests/MapInfoTest.cpp",
         "tests/MapsTest.cpp",
         "tests/MemoryBufferTest.cpp",
         "tests/MemoryCacheTest.cpp",
@@ -221,7 +226,6 @@
         "liblog",
         "liblzma",
         "libunwindstack",
-        "libdexfile",
         "libdexfile_support",
     ],
 
diff --git a/libunwindstack/DexFile.cpp b/libunwindstack/DexFile.cpp
index 9b0b232..eaf867f 100644
--- a/libunwindstack/DexFile.cpp
+++ b/libunwindstack/DexFile.cpp
@@ -23,7 +23,7 @@
 #include <memory>
 
 #include <android-base/unique_fd.h>
-#include <art_api/ext_dex_file.h>
+#include <art_api/dex_file_support.h>
 
 #include <unwindstack/MapInfo.h>
 #include <unwindstack/Memory.h>
@@ -46,7 +46,7 @@
 
 bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name,
                                    uint64_t* method_offset) {
-  art_api::dex::MethodInfo method_info = GetMethodInfoForOffset(dex_offset);
+  art_api::dex::MethodInfo method_info = GetMethodInfoForOffset(dex_offset, false);
   if (method_info.offset == 0) {
     return false;
   }
diff --git a/libunwindstack/DexFile.h b/libunwindstack/DexFile.h
index 5797dee..ca658e6 100644
--- a/libunwindstack/DexFile.h
+++ b/libunwindstack/DexFile.h
@@ -25,7 +25,7 @@
 #include <utility>
 #include <vector>
 
-#include <art_api/ext_dex_file.h>
+#include <art_api/dex_file_support.h>
 
 namespace unwindstack {
 
diff --git a/libunwindstack/ElfInterface.cpp b/libunwindstack/ElfInterface.cpp
index d0af94a..709e6c9 100644
--- a/libunwindstack/ElfInterface.cpp
+++ b/libunwindstack/ElfInterface.cpp
@@ -177,14 +177,14 @@
 template <typename EhdrType, typename PhdrType>
 uint64_t ElfInterface::GetLoadBias(Memory* memory) {
   EhdrType ehdr;
-  if (!memory->Read(0, &ehdr, sizeof(ehdr))) {
+  if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
     return false;
   }
 
   uint64_t offset = ehdr.e_phoff;
   for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
     PhdrType phdr;
-    if (!memory->Read(offset, &phdr, sizeof(phdr))) {
+    if (!memory->ReadFully(offset, &phdr, sizeof(phdr))) {
       return 0;
     }
     if (phdr.p_type == PT_LOAD && phdr.p_offset == 0) {
@@ -308,7 +308,7 @@
   // Skip the first header, it's always going to be NULL.
   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))) {
+    if (!memory_->ReadFully(offset, &shdr, sizeof(shdr))) {
       return;
     }
 
@@ -320,7 +320,7 @@
         continue;
       }
       uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
-      if (!memory_->Read(str_offset, &str_shdr, sizeof(str_shdr))) {
+      if (!memory_->ReadFully(str_offset, &str_shdr, sizeof(str_shdr))) {
         continue;
       }
       if (str_shdr.sh_type != SHT_STRTAB) {
diff --git a/libunwindstack/MapInfo.cpp b/libunwindstack/MapInfo.cpp
index 39a09cf..b9d8b3d 100644
--- a/libunwindstack/MapInfo.cpp
+++ b/libunwindstack/MapInfo.cpp
@@ -221,6 +221,19 @@
   return elf.get();
 }
 
+bool MapInfo::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
+  {
+    // Make sure no other thread is trying to update this elf object.
+    std::lock_guard<std::mutex> guard(mutex_);
+    if (elf == nullptr) {
+      return false;
+    }
+  }
+  // No longer need the lock, once the elf object is created, it is not deleted
+  // until this object is deleted.
+  return elf->GetFunctionName(addr, name, func_offset);
+}
+
 uint64_t MapInfo::GetLoadBias(const std::shared_ptr<Memory>& process_memory) {
   uint64_t cur_load_bias = load_bias.load();
   if (cur_load_bias != static_cast<uint64_t>(-1)) {
diff --git a/libunwindstack/RegsMips.cpp b/libunwindstack/RegsMips.cpp
index 14a4e31..f330fe0 100644
--- a/libunwindstack/RegsMips.cpp
+++ b/libunwindstack/RegsMips.cpp
@@ -135,7 +135,7 @@
   Memory* elf_memory = elf->memory();
   // Read from elf memory since it is usually more expensive to read from
   // process memory.
-  if (!elf_memory->Read(rel_pc, &data, sizeof(data))) {
+  if (!elf_memory->ReadFully(rel_pc, &data, sizeof(data))) {
     return false;
   }
 
@@ -160,7 +160,7 @@
 
   // read sc_pc and sc_regs[32] from stack
   uint64_t values[MIPS_REG_LAST];
-  if (!process_memory->Read(regs_[MIPS_REG_SP] + offset, values, sizeof(values))) {
+  if (!process_memory->ReadFully(regs_[MIPS_REG_SP] + offset, values, sizeof(values))) {
     return false;
   }
 
diff --git a/libunwindstack/Unwinder.cpp b/libunwindstack/Unwinder.cpp
index 8133639..57207de 100644
--- a/libunwindstack/Unwinder.cpp
+++ b/libunwindstack/Unwinder.cpp
@@ -26,10 +26,13 @@
 
 #include <android-base/stringprintf.h>
 
+#include <demangle.h>
+
 #include <unwindstack/Elf.h>
 #include <unwindstack/JitDebug.h>
 #include <unwindstack/MapInfo.h>
 #include <unwindstack/Maps.h>
+#include <unwindstack/Memory.h>
 #include <unwindstack/Unwinder.h>
 
 #if !defined(NO_LIBDEXFILE_SUPPORT)
@@ -306,7 +309,7 @@
   }
 
   if (!frame.function_name.empty()) {
-    data += " (" + frame.function_name;
+    data += " (" + demangle(frame.function_name.c_str());
     if (frame.function_offset != 0) {
       data += android::base::StringPrintf("+%" PRId64, frame.function_offset);
     }
@@ -327,4 +330,29 @@
 }
 #endif
 
+bool UnwinderFromPid::Init(ArchEnum arch) {
+  if (pid_ == getpid()) {
+    maps_ptr_.reset(new LocalMaps());
+  } else {
+    maps_ptr_.reset(new RemoteMaps(pid_));
+  }
+  if (!maps_ptr_->Parse()) {
+    return false;
+  }
+  maps_ = maps_ptr_.get();
+
+  process_memory_ = Memory::CreateProcessMemoryCached(pid_);
+
+  jit_debug_ptr_.reset(new JitDebug(process_memory_));
+  jit_debug_ = jit_debug_ptr_.get();
+  SetJitDebug(jit_debug_, arch);
+#if !defined(NO_LIBDEXFILE_SUPPORT)
+  dex_files_ptr_.reset(new DexFiles(process_memory_));
+  dex_files_ = dex_files_ptr_.get();
+  SetDexFiles(dex_files_, arch);
+#endif
+
+  return true;
+}
+
 }  // namespace unwindstack
diff --git a/libunwindstack/include/unwindstack/MapInfo.h b/libunwindstack/include/unwindstack/MapInfo.h
index 5e3d6f6..a9febd1 100644
--- a/libunwindstack/include/unwindstack/MapInfo.h
+++ b/libunwindstack/include/unwindstack/MapInfo.h
@@ -75,6 +75,8 @@
 
   Memory* CreateMemory(const std::shared_ptr<Memory>& process_memory);
 
+  bool GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset);
+
  private:
   MapInfo(const MapInfo&) = delete;
   void operator=(const MapInfo&) = delete;
diff --git a/libunwindstack/include/unwindstack/Unwinder.h b/libunwindstack/include/unwindstack/Unwinder.h
index d7bbd9d..ab239c1 100644
--- a/libunwindstack/include/unwindstack/Unwinder.h
+++ b/libunwindstack/include/unwindstack/Unwinder.h
@@ -24,7 +24,9 @@
 #include <string>
 #include <vector>
 
+#include <unwindstack/DexFiles.h>
 #include <unwindstack/Error.h>
+#include <unwindstack/JitDebug.h>
 #include <unwindstack/Maps.h>
 #include <unwindstack/Memory.h>
 #include <unwindstack/Regs.h>
@@ -32,9 +34,7 @@
 namespace unwindstack {
 
 // Forward declarations.
-class DexFiles;
 class Elf;
-class JitDebug;
 enum ArchEnum : uint8_t;
 
 struct FrameData {
@@ -67,6 +67,11 @@
       : max_frames_(max_frames), maps_(maps), regs_(regs), process_memory_(process_memory) {
     frames_.reserve(max_frames);
   }
+  Unwinder(size_t max_frames, Maps* maps, std::shared_ptr<Memory> process_memory)
+      : max_frames_(max_frames), maps_(maps), process_memory_(process_memory) {
+    frames_.reserve(max_frames);
+  }
+
   ~Unwinder() = default;
 
   void Unwind(const std::vector<std::string>* initial_map_names_to_skip = nullptr,
@@ -81,6 +86,10 @@
 
   void SetJitDebug(JitDebug* jit_debug, ArchEnum arch);
 
+  void SetRegs(Regs* regs) { regs_ = regs; }
+  Maps* GetMaps() { return maps_; }
+  std::shared_ptr<Memory>& GetProcessMemory() { return process_memory_; }
+
   // Disabling the resolving of names results in the function name being
   // set to an empty string and the function offset being set to zero.
   void SetResolveNames(bool resolve) { resolve_names_ = resolve; }
@@ -92,7 +101,9 @@
   ErrorCode LastErrorCode() { return last_error_.code; }
   uint64_t LastErrorAddress() { return last_error_.address; }
 
- private:
+ protected:
+  Unwinder(size_t max_frames) : max_frames_(max_frames) { frames_.reserve(max_frames); }
+
   void FillInDexFrame();
   void FillInFrame(MapInfo* map_info, Elf* elf, uint64_t rel_pc, uint64_t func_pc,
                    uint64_t pc_adjustment);
@@ -110,6 +121,22 @@
   ErrorData last_error_;
 };
 
+class UnwinderFromPid : public Unwinder {
+ public:
+  UnwinderFromPid(size_t max_frames, pid_t pid) : Unwinder(max_frames), pid_(pid) {}
+  ~UnwinderFromPid() = default;
+
+  bool Init(ArchEnum arch);
+
+ private:
+  pid_t pid_;
+  std::unique_ptr<Maps> maps_ptr_;
+  std::unique_ptr<JitDebug> jit_debug_ptr_;
+#if !defined(NO_LIBDEXFILE_SUPPORT)
+  std::unique_ptr<DexFiles> dex_files_ptr_;
+#endif
+};
+
 }  // namespace unwindstack
 
 #endif  // _LIBUNWINDSTACK_UNWINDER_H
diff --git a/libunwindstack/tests/DexFileTest.cpp b/libunwindstack/tests/DexFileTest.cpp
index 21ca47b..0149a42 100644
--- a/libunwindstack/tests/DexFileTest.cpp
+++ b/libunwindstack/tests/DexFileTest.cpp
@@ -21,7 +21,6 @@
 #include <unordered_map>
 
 #include <android-base/file.h>
-#include <dex/dex_file.h>
 #include <gtest/gtest.h>
 #include <unwindstack/MapInfo.h>
 #include <unwindstack/Memory.h>
@@ -40,17 +39,15 @@
   TemporaryFile tf;
   ASSERT_TRUE(tf.fd != -1);
 
-  ASSERT_EQ(sizeof(art::DexFile::Header) - 1,
-            static_cast<size_t>(
-                TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(art::DexFile::Header) - 1))));
+  ASSERT_EQ(size_t{10}, static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, 10))));
 
   // Header too small.
   EXPECT_TRUE(DexFileFromFile::Create(0, tf.path) == nullptr);
 
   // Header correct, file too small.
   ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET));
-  ASSERT_EQ(sizeof(art::DexFile::Header), static_cast<size_t>(TEMP_FAILURE_RETRY(write(
-                                              tf.fd, kDexData, sizeof(art::DexFile::Header)))));
+  ASSERT_EQ(sizeof(kDexData) - 1,
+            static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData) - 1))));
   EXPECT_TRUE(DexFileFromFile::Create(0, tf.path) == nullptr);
 }
 
@@ -78,7 +75,7 @@
 TEST(DexFileTest, from_memory_fail_too_small_for_header) {
   MemoryFake memory;
 
-  memory.SetMemory(0x1000, kDexData, sizeof(art::DexFile::Header) - 1);
+  memory.SetMemory(0x1000, kDexData, 10);
 
   EXPECT_TRUE(DexFileFromMemory::Create(0x1000, &memory, "") == nullptr);
 }
@@ -187,15 +184,6 @@
   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) {
@@ -210,12 +198,6 @@
   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/MapInfoTest.cpp b/libunwindstack/tests/MapInfoTest.cpp
new file mode 100644
index 0000000..e2cbb98
--- /dev/null
+++ b/libunwindstack/tests/MapInfoTest.cpp
@@ -0,0 +1,75 @@
+/*
+ * 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/MapInfo.h>
+#include <unwindstack/Maps.h>
+
+#include "ElfFake.h"
+
+namespace unwindstack {
+
+TEST(MapInfoTest, maps_constructor_const_char) {
+  MapInfo prev_map(nullptr, 0, 0, 0, 0, "");
+  MapInfo map_info(&prev_map, 1, 2, 3, 4, "map");
+
+  EXPECT_EQ(&prev_map, map_info.prev_map);
+  EXPECT_EQ(1UL, map_info.start);
+  EXPECT_EQ(2UL, map_info.end);
+  EXPECT_EQ(3UL, map_info.offset);
+  EXPECT_EQ(4UL, map_info.flags);
+  EXPECT_EQ("map", map_info.name);
+  EXPECT_EQ(static_cast<uint64_t>(-1), map_info.load_bias);
+  EXPECT_EQ(0UL, map_info.elf_offset);
+  EXPECT_TRUE(map_info.elf.get() == nullptr);
+}
+
+TEST(MapInfoTest, maps_constructor_string) {
+  std::string name("string_map");
+  MapInfo prev_map(nullptr, 0, 0, 0, 0, "");
+  MapInfo map_info(&prev_map, 1, 2, 3, 4, name);
+
+  EXPECT_EQ(&prev_map, map_info.prev_map);
+  EXPECT_EQ(1UL, map_info.start);
+  EXPECT_EQ(2UL, map_info.end);
+  EXPECT_EQ(3UL, map_info.offset);
+  EXPECT_EQ(4UL, map_info.flags);
+  EXPECT_EQ("string_map", map_info.name);
+  EXPECT_EQ(static_cast<uint64_t>(-1), map_info.load_bias);
+  EXPECT_EQ(0UL, map_info.elf_offset);
+  EXPECT_TRUE(map_info.elf.get() == nullptr);
+}
+
+TEST(MapInfoTest, get_function_name) {
+  ElfFake* elf = new ElfFake(nullptr);
+  ElfInterfaceFake* interface = new ElfInterfaceFake(nullptr);
+  elf->FakeSetInterface(interface);
+  interface->FakePushFunctionData(FunctionData("function", 1000));
+
+  MapInfo map_info(nullptr, 1, 2, 3, 4, "");
+  map_info.elf.reset(elf);
+
+  std::string name;
+  uint64_t offset;
+  ASSERT_TRUE(map_info.GetFunctionName(1000, &name, &offset));
+  EXPECT_EQ("function", name);
+  EXPECT_EQ(1000UL, offset);
+}
+
+}  // namespace unwindstack
diff --git a/libunwindstack/tests/UnwindOfflineTest.cpp b/libunwindstack/tests/UnwindOfflineTest.cpp
index aab9ec2..0588a84 100644
--- a/libunwindstack/tests/UnwindOfflineTest.cpp
+++ b/libunwindstack/tests/UnwindOfflineTest.cpp
@@ -210,8 +210,8 @@
   ASSERT_EQ(4U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
   EXPECT_EQ(
       "  #00 pc 0001a9f8  libc.so (abort+64)\n"
-      "  #01 pc 00006a1b  libbase.so (_ZN7android4base14DefaultAborterEPKc+6)\n"
-      "  #02 pc 00007441  libbase.so (_ZN7android4base10LogMessageD2Ev+748)\n"
+      "  #01 pc 00006a1b  libbase.so (android::base::DefaultAborter(char const*)+6)\n"
+      "  #02 pc 00007441  libbase.so (android::base::LogMessage::~LogMessage()+748)\n"
       "  #03 pc 00015147  /does/not/exist/libhidlbase.so\n",
       frame_info);
   EXPECT_EQ(0xf31ea9f8U, unwinder.frames()[0].pc);
@@ -234,9 +234,10 @@
   ASSERT_EQ(2U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
   EXPECT_EQ(
       "  #00 pc 0006dc49  libandroid_runtime.so "
-      "(_ZN7android14AndroidRuntime15javaThreadShellEPv+80)\n"
+      "(android::AndroidRuntime::javaThreadShell(void*)+80)\n"
       "  #01 pc 0006dce5  libandroid_runtime.so "
-      "(_ZN7android14AndroidRuntime19javaCreateThreadEtcEPFiPvES1_PKcijPS1_)\n",
+      "(android::AndroidRuntime::javaCreateThreadEtc(int (*)(void*), void*, char const*, int, "
+      "unsigned int, void**))\n",
       frame_info);
   EXPECT_EQ(0xf1f6dc49U, unwinder.frames()[0].pc);
   EXPECT_EQ(0xd8fe6930U, unwinder.frames()[0].sp);
@@ -257,10 +258,10 @@
       "  #01 pc 000000000042a078  libunwindstack_test (SignalMiddleFunction+8)\n"
       "  #02 pc 000000000042a08c  libunwindstack_test (SignalOuterFunction+8)\n"
       "  #03 pc 000000000042d8fc  libunwindstack_test "
-      "(_ZN11unwindstackL19RemoteThroughSignalEij+20)\n"
+      "(unwindstack::RemoteThroughSignal(int, unsigned int)+20)\n"
       "  #04 pc 000000000042d8d8  libunwindstack_test "
-      "(_ZN11unwindstack37UnwindTest_remote_through_signal_Test8TestBodyEv+32)\n"
-      "  #05 pc 0000000000455d70  libunwindstack_test (_ZN7testing4Test3RunEv+392)\n",
+      "(unwindstack::UnwindTest_remote_through_signal_Test::TestBody()+32)\n"
+      "  #05 pc 0000000000455d70  libunwindstack_test (testing::Test::Run()+392)\n",
       frame_info);
   EXPECT_EQ(0x64d09d4fd8U, unwinder.frames()[0].pc);
   EXPECT_EQ(0x7fe0d84040U, unwinder.frames()[0].sp);
@@ -296,54 +297,57 @@
   std::string frame_info(DumpFrames(unwinder));
   ASSERT_EQ(69U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
   EXPECT_EQ(
-      "  #00 pc 00068fb8  libarttestd.so (_ZN3artL13CauseSegfaultEv+72)\n"
+      "  #00 pc 00068fb8  libarttestd.so (art::CauseSegfault()+72)\n"
       "  #01 pc 00067f00  libarttestd.so (Java_Main_unwindInProcess+10032)\n"
       "  #02 pc 000021a8  137-cfi.odex (offset 0x2000) (boolean Main.unwindInProcess(boolean, int, "
       "boolean)+136)\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"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+885)\n"
       "  #06 pc 0039cf0d  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+653)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
       "  #07 pc 00392552  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+354)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+354)\n"
       "  #08 pc 0039399a  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+234)\n"
       "  #09 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
       "  #10 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\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"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+885)\n"
       "  #14 pc 0039cf0d  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+653)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
       "  #15 pc 00392552  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+354)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+354)\n"
       "  #16 pc 0039399a  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+234)\n"
       "  #17 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
       "  #18 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\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"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+885)\n"
       "  #22 pc 0039cf0d  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+653)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
       "  #23 pc 00392552  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+354)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+354)\n"
       "  #24 pc 0039399a  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+234)\n"
       "  #25 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
       "  #26 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\n"
       "  #27 pc 0000fbdb  anonymous:ee74c000 (int "
@@ -351,81 +355,86 @@
       "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"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+907)\n"
       "  #30 pc 0039cf0d  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+653)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
       "  #31 pc 00392552  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+354)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+354)\n"
       "  #32 pc 0039399a  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+234)\n"
       "  #33 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
       "  #34 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\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"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+885)\n"
       "  #38 pc 0039cf0d  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+653)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
       "  #39 pc 00392552  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+354)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+354)\n"
       "  #40 pc 0039399a  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+234)\n"
       "  #41 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
       "  #42 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\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"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+885)\n"
       "  #46 pc 0039cf0d  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+653)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
       "  #47 pc 00392552  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+354)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+354)\n"
       "  #48 pc 0039399a  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+234)\n"
       "  #49 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
       "  #50 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\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"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+907)\n"
       "  #54 pc 0039cf0d  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+653)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+653)\n"
       "  #55 pc 00392552  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+354)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+354)\n"
       "  #56 pc 0039399a  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+234)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+234)\n"
       "  #57 pc 00684362  libartd.so (artQuickToInterpreterBridge+1058)\n"
       "  #58 pc 006b35bd  libartd.so (art_quick_to_interpreter_bridge+77)\n"
       "  #59 pc 006ad6a2  libartd.so (art_quick_invoke_static_stub+418)\n"
       "  #60 pc 00146acb  libartd.so "
-      "(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+907)\n"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+907)\n"
       "  #61 pc 005aac95  libartd.so "
-      "(_ZN3artL18InvokeWithArgArrayERKNS_33ScopedObjectAccessAlreadyRunnableEPNS_9ArtMethodEPNS_"
-      "8ArgArrayEPNS_6JValueEPKc+85)\n"
+      "(art::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, "
+      "art::ArgArray*, art::JValue*, char const*)+85)\n"
       "  #62 pc 005aab5a  libartd.so "
-      "(_ZN3art17InvokeWithVarArgsERKNS_33ScopedObjectAccessAlreadyRunnableEP8_jobjectP10_"
-      "jmethodIDPc+362)\n"
+      "(art::InvokeWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, "
+      "_jmethodID*, char*)+362)\n"
       "  #63 pc 0048a3dd  libartd.so "
-      "(_ZN3art3JNI21CallStaticVoidMethodVEP7_JNIEnvP7_jclassP10_jmethodIDPc+125)\n"
+      "(art::JNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, char*)+125)\n"
       "  #64 pc 0018448c  libartd.so "
-      "(_ZN3art8CheckJNI11CallMethodVEPKcP7_JNIEnvP8_jobjectP7_jclassP10_jmethodIDPcNS_"
-      "9Primitive4TypeENS_10InvokeTypeE+1964)\n"
+      "(art::CheckJNI::CallMethodV(char const*, _JNIEnv*, _jobject*, _jclass*, _jmethodID*, char*, "
+      "art::Primitive::Type, art::InvokeType)+1964)\n"
       "  #65 pc 0017cf06  libartd.so "
-      "(_ZN3art8CheckJNI21CallStaticVoidMethodVEP7_JNIEnvP7_jclassP10_jmethodIDPc+70)\n"
+      "(art::CheckJNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, char*)+70)\n"
       "  #66 pc 00001d8c  dalvikvm32 "
-      "(_ZN7_JNIEnv20CallStaticVoidMethodEP7_jclassP10_jmethodIDz+60)\n"
+      "(_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+60)\n"
       "  #67 pc 00001a80  dalvikvm32 (main+1312)\n"
       "  #68 pc 00018275  libc.so\n",
       frame_info);
@@ -597,32 +606,34 @@
       "  #03 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
       "  #04 pc 00467129  libartd.so (art_quick_invoke_stub+228)\n"
       "  #05 pc 000bf7a9  libartd.so "
-      "(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+864)\n"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+864)\n"
       "  #06 pc 00247833  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+382)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
       "  #07 pc 0022e935  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+244)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+244)\n"
       "  #08 pc 0022f71d  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+128)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+128)\n"
       "  #09 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
       "  #10 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
       "  #11 pc 00011c31  anonymous:e2796000 (int Main.compare(Main, Main)+64)\n"
       "  #12 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
       "  #13 pc 00467129  libartd.so (art_quick_invoke_stub+228)\n"
       "  #14 pc 000bf7a9  libartd.so "
-      "(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+864)\n"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+864)\n"
       "  #15 pc 00247833  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+382)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
       "  #16 pc 0022e935  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+244)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+244)\n"
       "  #17 pc 0022f71d  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+128)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+128)\n"
       "  #18 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
       "  #19 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
       "  #20 pc 00011b77  anonymous:e2796000 (int Main.compare(java.lang.Object, "
@@ -630,16 +641,17 @@
       "  #21 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
       "  #22 pc 00467129  libartd.so (art_quick_invoke_stub+228)\n"
       "  #23 pc 000bf7a9  libartd.so "
-      "(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+864)\n"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+864)\n"
       "  #24 pc 00247833  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+382)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
       "  #25 pc 0022e935  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+244)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+244)\n"
       "  #26 pc 0022f71d  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+128)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+128)\n"
       "  #27 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
       "  #28 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
       "  #29 pc 00011a29  anonymous:e2796000 (int "
@@ -648,85 +660,90 @@
       "  #30 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
       "  #31 pc 0046722f  libartd.so (art_quick_invoke_static_stub+226)\n"
       "  #32 pc 000bf7bb  libartd.so "
-      "(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+882)\n"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+882)\n"
       "  #33 pc 00247833  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+382)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
       "  #34 pc 0022e935  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+244)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+244)\n"
       "  #35 pc 0022f71d  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+128)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+128)\n"
       "  #36 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
       "  #37 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
       "  #38 pc 0001139b  anonymous:e2796000 (boolean Main.foo()+178)\n"
       "  #39 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
       "  #40 pc 00467129  libartd.so (art_quick_invoke_stub+228)\n"
       "  #41 pc 000bf7a9  libartd.so "
-      "(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+864)\n"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+864)\n"
       "  #42 pc 00247833  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+382)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
       "  #43 pc 0022e935  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+244)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+244)\n"
       "  #44 pc 0022f71d  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+128)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+128)\n"
       "  #45 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
       "  #46 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
       "  #47 pc 00010aa7  anonymous:e2796000 (void Main.runPrimary()+70)\n"
       "  #48 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
       "  #49 pc 00467129  libartd.so (art_quick_invoke_stub+228)\n"
       "  #50 pc 000bf7a9  libartd.so "
-      "(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+864)\n"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+864)\n"
       "  #51 pc 00247833  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+382)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
       "  #52 pc 0022e935  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+244)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+244)\n"
       "  #53 pc 0022f71d  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+128)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+128)\n"
       "  #54 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
       "  #55 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
       "  #56 pc 0000ba99  anonymous:e2796000 (void Main.main(java.lang.String[])+144)\n"
       "  #57 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
       "  #58 pc 0046722f  libartd.so (art_quick_invoke_static_stub+226)\n"
       "  #59 pc 000bf7bb  libartd.so "
-      "(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+882)\n"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+882)\n"
       "  #60 pc 00247833  libartd.so "
-      "(_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPNS_"
-      "11ShadowFrameEtPNS_6JValueE+382)\n"
+      "(art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, "
+      "art::ShadowFrame*, unsigned short, art::JValue*)+382)\n"
       "  #61 pc 0022e935  libartd.so "
-      "(_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_"
-      "6JValueEb+244)\n"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+244)\n"
       "  #62 pc 0022f71d  libartd.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+128)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+128)\n"
       "  #63 pc 00442865  libartd.so (artQuickToInterpreterBridge+796)\n"
       "  #64 pc 004666ff  libartd.so (art_quick_to_interpreter_bridge+30)\n"
       "  #65 pc 00462175  libartd.so (art_quick_invoke_stub_internal+68)\n"
       "  #66 pc 0046722f  libartd.so (art_quick_invoke_static_stub+226)\n"
       "  #67 pc 000bf7bb  libartd.so "
-      "(_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+882)\n"
+      "(art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char "
+      "const*)+882)\n"
       "  #68 pc 003b292d  libartd.so "
-      "(_ZN3artL18InvokeWithArgArrayERKNS_33ScopedObjectAccessAlreadyRunnableEPNS_9ArtMethodEPNS_"
-      "8ArgArrayEPNS_6JValueEPKc+52)\n"
+      "(art::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, "
+      "art::ArgArray*, art::JValue*, char const*)+52)\n"
       "  #69 pc 003b26c3  libartd.so "
-      "(_ZN3art17InvokeWithVarArgsERKNS_33ScopedObjectAccessAlreadyRunnableEP8_jobjectP10_"
-      "jmethodIDSt9__va_list+210)\n"
+      "(art::InvokeWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, "
+      "_jmethodID*, std::__va_list)+210)\n"
       "  #70 pc 00308411  libartd.so "
-      "(_ZN3art3JNI21CallStaticVoidMethodVEP7_JNIEnvP7_jclassP10_jmethodIDSt9__va_list+76)\n"
+      "(art::JNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, std::__va_list)+76)\n"
       "  #71 pc 000e6a9f  libartd.so "
-      "(_ZN3art8CheckJNI11CallMethodVEPKcP7_JNIEnvP8_jobjectP7_jclassP10_jmethodIDSt9__va_listNS_"
-      "9Primitive4TypeENS_10InvokeTypeE+1486)\n"
+      "(art::CheckJNI::CallMethodV(char const*, _JNIEnv*, _jobject*, _jclass*, _jmethodID*, "
+      "std::__va_list, art::Primitive::Type, art::InvokeType)+1486)\n"
       "  #72 pc 000e19b9  libartd.so "
-      "(_ZN3art8CheckJNI21CallStaticVoidMethodVEP7_JNIEnvP7_jclassP10_jmethodIDSt9__va_list+40)\n"
+      "(art::CheckJNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, std::__va_list)+40)\n"
       "  #73 pc 0000159f  dalvikvm32 "
-      "(_ZN7_JNIEnv20CallStaticVoidMethodEP7_jclassP10_jmethodIDz+30)\n"
+      "(_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+30)\n"
       "  #74 pc 00001349  dalvikvm32 (main+896)\n"
       "  #75 pc 000850c9  libc.so\n",
       frame_info);
@@ -997,41 +1014,44 @@
       "(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"
+      "(art::jit::Jit::MaybeDoOnStackReplacement(art::Thread*, art::ArtMethod*, unsigned int, int, "
+      "art::JValue*)+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"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+374)\n"
       "  #08 pc 001dc593  libart.so "
-      "(_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameEPNS_6JValueE+154)\n"
+      "(art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, "
+      "art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+154)\n"
       "  #09 pc 001f4d01  libart.so "
-      "(_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_"
-      "11InstructionEtPNS_6JValueE+732)\n"
+      "(bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, "
+      "art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+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"
+      "(art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, "
+      "art::ShadowFrame&, art::JValue, bool)+374)\n"
       "  #14 pc 001dc4d5  libart.so "
-      "(_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadERKNS_"
-      "20CodeItemDataAccessorEPNS_11ShadowFrameE+92)\n"
+      "(art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor "
+      "const&, art::ShadowFrame*)+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"
+      "  #19 pc 000b468d  libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned "
+      "int, art::JValue*, char const*)+136)\n"
       "  #20 pc 00362f49  libart.so "
-      "(_ZN3art12_GLOBAL__N_118InvokeWithArgArrayERKNS_33ScopedObjectAccessAlreadyRunnableEPNS_"
-      "9ArtMethodEPNS0_8ArgArrayEPNS_6JValueEPKc+52)\n"
+      "(art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable "
+      "const&, art::ArtMethod*, art::(anonymous namespace)::ArgArray*, art::JValue*, char "
+      "const*)+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"
+      "(art::InvokeVirtualOrInterfaceWithJValues(art::ScopedObjectAccessAlreadyRunnable const&, "
+      "_jobject*, _jmethodID*, jvalue*)+332)\n"
+      "  #22 pc 003851dd  libart.so (art::Thread::CreateCallback(void*)+868)\n"
+      "  #23 pc 00062925  libc.so (__pthread_start(void*)+22)\n"
       "  #24 pc 0001de39  libc.so (__start_thread+24)\n",
       frame_info);
   EXPECT_EQ(0xd025c788U, unwinder.frames()[0].pc);
@@ -1107,8 +1127,8 @@
       "(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"
+      "  #03 pc 003851dd  libart.so (art::Thread::CreateCallback(void*)+868)\n"
+      "  #04 pc 00062925  libc.so (__pthread_start(void*)+22)\n"
       "  #05 pc 0001de39  libc.so (__start_thread+24)\n",
       frame_info);
 
@@ -1139,22 +1159,22 @@
       "  #01 pc 0032bfeb  libunwindstack_test (SignalMiddleFunction+2)\n"
       "  #02 pc 0032bff3  libunwindstack_test (SignalOuterFunction+2)\n"
       "  #03 pc 0032fed3  libunwindstack_test "
-      "(_ZN11unwindstackL19SignalCallerHandlerEiP7siginfoPv+26)\n"
+      "(unwindstack::SignalCallerHandler(int, siginfo*, void*)+26)\n"
       "  #04 pc 00026528  libc.so\n"
       "  #05 pc 00000000  <unknown>\n"
       "  #06 pc 0032c2d9  libunwindstack_test (InnerFunction+736)\n"
       "  #07 pc 0032cc4f  libunwindstack_test (MiddleFunction+42)\n"
       "  #08 pc 0032cc81  libunwindstack_test (OuterFunction+42)\n"
       "  #09 pc 0032e547  libunwindstack_test "
-      "(_ZN11unwindstackL19RemoteThroughSignalEij+270)\n"
+      "(unwindstack::RemoteThroughSignal(int, unsigned int)+270)\n"
       "  #10 pc 0032ed99  libunwindstack_test "
-      "(_ZN11unwindstack55UnwindTest_remote_through_signal_with_invalid_func_Test8TestBodyEv+16)\n"
-      "  #11 pc 00354453  libunwindstack_test (_ZN7testing4Test3RunEv+154)\n"
-      "  #12 pc 00354de7  libunwindstack_test (_ZN7testing8TestInfo3RunEv+194)\n"
-      "  #13 pc 00355105  libunwindstack_test (_ZN7testing8TestCase3RunEv+180)\n"
+      "(unwindstack::UnwindTest_remote_through_signal_with_invalid_func_Test::TestBody()+16)\n"
+      "  #11 pc 00354453  libunwindstack_test (testing::Test::Run()+154)\n"
+      "  #12 pc 00354de7  libunwindstack_test (testing::TestInfo::Run()+194)\n"
+      "  #13 pc 00355105  libunwindstack_test (testing::TestCase::Run()+180)\n"
       "  #14 pc 0035a215  libunwindstack_test "
-      "(_ZN7testing8internal12UnitTestImpl11RunAllTestsEv+664)\n"
-      "  #15 pc 00359f4f  libunwindstack_test (_ZN7testing8UnitTest3RunEv+110)\n"
+      "(testing::internal::UnitTestImpl::RunAllTests()+664)\n"
+      "  #15 pc 00359f4f  libunwindstack_test (testing::UnitTest::Run()+110)\n"
       "  #16 pc 0034d3db  libunwindstack_test (main+38)\n"
       "  #17 pc 00092c0d  libc.so (__libc_init+48)\n"
       "  #18 pc 0004202f  libunwindstack_test (_start_main+38)\n",
@@ -1213,9 +1233,9 @@
   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"
+      "  #02 pc 00039535  libbinder.so (android::IPCThreadState::talkWithDriver(bool)+204)\n"
+      "  #03 pc 00039633  libbinder.so (android::IPCThreadState::getAndExecuteCommand()+10)\n"
+      "  #04 pc 00039b57  libbinder.so (android::IPCThreadState::joinThreadPool(bool)+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",
diff --git a/libunwindstack/tests/UnwindTest.cpp b/libunwindstack/tests/UnwindTest.cpp
index ea992c7..a91bd95 100644
--- a/libunwindstack/tests/UnwindTest.cpp
+++ b/libunwindstack/tests/UnwindTest.cpp
@@ -44,6 +44,13 @@
 
 namespace unwindstack {
 
+enum TestTypeEnum : uint8_t {
+  TEST_TYPE_LOCAL_UNWINDER = 0,
+  TEST_TYPE_LOCAL_UNWINDER_FROM_PID,
+  TEST_TYPE_REMOTE,
+  TEST_TYPE_REMOTE_WITH_INVALID_CALL,
+};
+
 static std::atomic_bool g_ready;
 static volatile bool g_ready_for_remote;
 static volatile bool g_signal_ready_for_remote;
@@ -88,10 +95,10 @@
   SignalOuterFunction();
 }
 
-static std::string ErrorMsg(const std::vector<const char*>& function_names, Unwinder& unwinder) {
+static std::string ErrorMsg(const std::vector<const char*>& function_names, Unwinder* unwinder) {
   std::string unwind;
-  for (size_t i = 0; i < unwinder.NumFrames(); i++) {
-    unwind += unwinder.FormatFrame(i) + '\n';
+  for (size_t i = 0; i < unwinder->NumFrames(); i++) {
+    unwind += unwinder->FormatFrame(i) + '\n';
   }
 
   return std::string(
@@ -100,14 +107,10 @@
          function_names.front() + "\n" + "Unwind data:\n" + unwind;
 }
 
-static void VerifyUnwind(pid_t pid, Maps* maps, Regs* regs,
-                         std::vector<const char*> expected_function_names) {
-  auto process_memory(Memory::CreateProcessMemory(pid));
+static void VerifyUnwind(Unwinder* unwinder, std::vector<const char*> expected_function_names) {
+  unwinder->Unwind();
 
-  Unwinder unwinder(512, maps, regs, process_memory);
-  unwinder.Unwind();
-
-  for (auto& frame : unwinder.frames()) {
+  for (auto& frame : unwinder->frames()) {
     if (frame.function_name == expected_function_names.back()) {
       expected_function_names.pop_back();
       if (expected_function_names.empty()) {
@@ -119,35 +122,55 @@
   ASSERT_TRUE(expected_function_names.empty()) << ErrorMsg(expected_function_names, unwinder);
 }
 
+static void VerifyUnwind(pid_t pid, Maps* maps, Regs* regs,
+                         std::vector<const char*> expected_function_names) {
+  auto process_memory(Memory::CreateProcessMemory(pid));
+
+  Unwinder unwinder(512, maps, regs, process_memory);
+  VerifyUnwind(&unwinder, expected_function_names);
+}
+
 // 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 InnerFunction(bool local, bool trigger_invalid_call) {
-  if (local) {
-    LocalMaps maps;
-    ASSERT_TRUE(maps.Parse());
-    std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
-    RegsGetLocal(regs.get());
-
-    VerifyUnwind(getpid(), &maps, regs.get(), kFunctionOrder);
-  } else {
+extern "C" void InnerFunction(TestTypeEnum test_type) {
+  if (test_type == TEST_TYPE_REMOTE || test_type == TEST_TYPE_REMOTE_WITH_INVALID_CALL) {
     g_ready_for_remote = true;
     g_ready = true;
-    if (trigger_invalid_call) {
+    if (test_type == TEST_TYPE_REMOTE_WITH_INVALID_CALL) {
       void (*crash_func)() = nullptr;
       crash_func();
     }
     while (!g_finish.load()) {
     }
+    return;
   }
+
+  std::unique_ptr<Unwinder> unwinder;
+  std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
+  RegsGetLocal(regs.get());
+  std::unique_ptr<Maps> maps;
+
+  if (test_type == TEST_TYPE_LOCAL_UNWINDER) {
+    maps.reset(new LocalMaps());
+    ASSERT_TRUE(maps->Parse());
+    auto process_memory(Memory::CreateProcessMemory(getpid()));
+    unwinder.reset(new Unwinder(512, maps.get(), regs.get(), process_memory));
+  } else {
+    UnwinderFromPid* unwinder_from_pid = new UnwinderFromPid(512, getpid());
+    ASSERT_TRUE(unwinder_from_pid->Init(regs->Arch()));
+    unwinder_from_pid->SetRegs(regs.get());
+    unwinder.reset(unwinder_from_pid);
+  }
+  VerifyUnwind(unwinder.get(), kFunctionOrder);
 }
 
-extern "C" void MiddleFunction(bool local, bool trigger_invalid_call) {
-  InnerFunction(local, trigger_invalid_call);
+extern "C" void MiddleFunction(TestTypeEnum test_type) {
+  InnerFunction(test_type);
 }
 
-extern "C" void OuterFunction(bool local, bool trigger_invalid_call) {
-  MiddleFunction(local, trigger_invalid_call);
+extern "C" void OuterFunction(TestTypeEnum test_type) {
+  MiddleFunction(test_type);
 }
 
 class UnwindTest : public ::testing::Test {
@@ -156,7 +179,11 @@
 };
 
 TEST_F(UnwindTest, local) {
-  OuterFunction(true, false);
+  OuterFunction(TEST_TYPE_LOCAL_UNWINDER);
+}
+
+TEST_F(UnwindTest, local_use_from_pid) {
+  OuterFunction(TEST_TYPE_LOCAL_UNWINDER_FROM_PID);
 }
 
 void WaitForRemote(pid_t pid, uint64_t addr, bool leave_attached, bool* completed) {
@@ -191,7 +218,7 @@
 TEST_F(UnwindTest, remote) {
   pid_t pid;
   if ((pid = fork()) == 0) {
-    OuterFunction(false, false);
+    OuterFunction(TEST_TYPE_REMOTE);
     exit(0);
   }
   ASSERT_NE(-1, pid);
@@ -212,11 +239,39 @@
       << "ptrace detach failed with unexpected error: " << strerror(errno);
 }
 
+TEST_F(UnwindTest, unwind_from_pid_remote) {
+  pid_t pid;
+  if ((pid = fork()) == 0) {
+    OuterFunction(TEST_TYPE_REMOTE);
+    exit(0);
+  }
+  ASSERT_NE(-1, pid);
+  TestScopedPidReaper reap(pid);
+
+  bool completed;
+  WaitForRemote(pid, reinterpret_cast<uint64_t>(&g_ready_for_remote), true, &completed);
+  ASSERT_TRUE(completed) << "Timed out waiting for remote process to be ready.";
+
+  std::unique_ptr<Regs> regs(Regs::RemoteGet(pid));
+  ASSERT_TRUE(regs.get() != nullptr);
+
+  UnwinderFromPid unwinder(512, pid);
+  ASSERT_TRUE(unwinder.Init(regs->Arch()));
+  unwinder.SetRegs(regs.get());
+
+  VerifyUnwind(&unwinder, kFunctionOrder);
+
+  // Verify that calling the same object works again.
+
+  ASSERT_EQ(0, ptrace(PTRACE_DETACH, pid, 0, 0))
+      << "ptrace detach failed with unexpected error: " << strerror(errno);
+}
+
 TEST_F(UnwindTest, from_context) {
   std::atomic_int tid(0);
   std::thread thread([&]() {
     tid = syscall(__NR_gettid);
-    OuterFunction(false, false);
+    OuterFunction(TEST_TYPE_REMOTE);
   });
 
   struct sigaction act, oldact;
@@ -266,7 +321,7 @@
     act.sa_flags = SA_RESTART | SA_ONSTACK | sa_flags;
     ASSERT_EQ(0, sigaction(signal, &act, &oldact));
 
-    OuterFunction(false, signal == SIGSEGV);
+    OuterFunction(signal != SIGSEGV ? TEST_TYPE_REMOTE : TEST_TYPE_REMOTE_WITH_INVALID_CALL);
     exit(0);
   }
   ASSERT_NE(-1, pid);
diff --git a/libunwindstack/tests/UnwinderTest.cpp b/libunwindstack/tests/UnwinderTest.cpp
index 1fdeee5..49aeeb3 100644
--- a/libunwindstack/tests/UnwinderTest.cpp
+++ b/libunwindstack/tests/UnwinderTest.cpp
@@ -1080,6 +1080,12 @@
             Unwinder::FormatFrame(frame, false));
   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so (function)", Unwinder::FormatFrame(frame, true));
 
+  // Verify the function name is demangled.
+  frame.function_name = "_ZN4funcEv";
+  EXPECT_EQ("  #01 pc 0000000000001000  /fake/libfake.so (func())",
+            Unwinder::FormatFrame(frame, false));
+  EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so (func())", Unwinder::FormatFrame(frame, true));
+
   frame.function_name = "";
   EXPECT_EQ("  #01 pc 0000000000001000  /fake/libfake.so", Unwinder::FormatFrame(frame, false));
   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so", Unwinder::FormatFrame(frame, true));
diff --git a/libunwindstack/tools/unwind.cpp b/libunwindstack/tools/unwind.cpp
index e729453..1812e50 100644
--- a/libunwindstack/tools/unwind.cpp
+++ b/libunwindstack/tools/unwind.cpp
@@ -57,12 +57,6 @@
 }
 
 void DoUnwind(pid_t pid) {
-  unwindstack::RemoteMaps remote_maps(pid);
-  if (!remote_maps.Parse()) {
-    printf("Failed to parse map data.\n");
-    return;
-  }
-
   unwindstack::Regs* regs = unwindstack::Regs::RemoteGet(pid);
   if (regs == nullptr) {
     printf("Unable to get remote reg data\n");
@@ -95,15 +89,13 @@
   }
   printf("\n");
 
-  auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);
-  unwindstack::Unwinder unwinder(128, &remote_maps, regs, process_memory);
+  unwindstack::UnwinderFromPid unwinder(1024, pid);
+  if (!unwinder.Init(regs->Arch())) {
+    printf("Failed to init unwinder object.\n");
+    return;
+  }
 
-  unwindstack::JitDebug jit_debug(process_memory);
-  unwinder.SetJitDebug(&jit_debug, regs->Arch());
-
-  unwindstack::DexFiles dex_files(process_memory);
-  unwinder.SetDexFiles(&dex_files, regs->Arch());
-
+  unwinder.SetRegs(regs);
   unwinder.Unwind();
 
   // Print the frames.
diff --git a/libunwindstack/tools/unwind_for_offline.cpp b/libunwindstack/tools/unwind_for_offline.cpp
index 5ae8874..4f67d67 100644
--- a/libunwindstack/tools/unwind_for_offline.cpp
+++ b/libunwindstack/tools/unwind_for_offline.cpp
@@ -240,12 +240,6 @@
     return 1;
   }
 
-  unwindstack::RemoteMaps maps(pid);
-  if (!maps.Parse()) {
-    printf("Unable to parse maps.\n");
-    return 1;
-  }
-
   // Save the current state of the registers.
   if (!SaveRegs(regs)) {
     return 1;
@@ -253,35 +247,38 @@
 
   // Do an unwind so we know how much of the stack to save, and what
   // elf files are involved.
+  unwindstack::UnwinderFromPid unwinder(1024, pid);
+  if (!unwinder.Init(regs->Arch())) {
+    printf("Unable to init unwinder object.\n");
+    return 1;
+  }
+  unwinder.SetRegs(regs);
   uint64_t sp = regs->sp();
-  auto process_memory = unwindstack::Memory::CreateProcessMemory(pid);
-  unwindstack::JitDebug jit_debug(process_memory);
-  unwindstack::Unwinder unwinder(1024, &maps, regs, process_memory);
-  unwinder.SetJitDebug(&jit_debug, regs->Arch());
   unwinder.Unwind();
 
   std::unordered_map<uint64_t, map_info_t> maps_by_start;
   std::vector<std::pair<uint64_t, uint64_t>> stacks;
+  unwindstack::Maps* maps = unwinder.GetMaps();
   uint64_t sp_map_start = 0;
-  unwindstack::MapInfo* map_info = maps.Find(sp);
+  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 (const auto& frame : unwinder.frames()) {
-    map_info = maps.Find(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) {
-      map_info = maps.Find(frame.map_start);
+      map_info = maps->Find(frame.map_start);
 
       auto info = FillInAndGetMapInfo(maps_by_start, map_info);
       bool file_copied = false;
-      SaveMapInformation(process_memory, info, &file_copied);
+      SaveMapInformation(unwinder.GetProcessMemory(), info, &file_copied);
 
       // If you are using a a linker that creates two maps (one read-only, one
       // read-executable), it's necessary to capture the previous map
@@ -291,7 +288,7 @@
           prev_map->flags == PROT_READ && map_info->name == prev_map->name &&
           maps_by_start.count(prev_map->start) == 0) {
         info = FillInAndGetMapInfo(maps_by_start, prev_map);
-        SaveMapInformation(process_memory, info, &file_copied);
+        SaveMapInformation(unwinder.GetProcessMemory(), info, &file_copied);
       }
     }
   }
diff --git a/llkd/libllkd.cpp b/llkd/libllkd.cpp
index 3c295b5..b26ad4d 100644
--- a/llkd/libllkd.cpp
+++ b/llkd/libllkd.cpp
@@ -525,7 +525,7 @@
         // NOTREACHED
         return;
     }
-    ::sync();
+    // Wish could ::sync() here, if storage is locked up, we will not continue.
     if (dump) {
         // Show all locks that are held
         android::base::WriteStringToFd("d", sysrqTriggerFd);
diff --git a/lmkd/lmkd.c b/lmkd/lmkd.c
index c9c9e8e..ca78c38 100644
--- a/lmkd/lmkd.c
+++ b/lmkd/lmkd.c
@@ -16,6 +16,7 @@
 
 #define LOG_TAG "lowmemorykiller"
 
+#include <dirent.h>
 #include <errno.h>
 #include <inttypes.h>
 #include <pwd.h>
@@ -28,18 +29,22 @@
 #include <sys/epoll.h>
 #include <sys/eventfd.h>
 #include <sys/mman.h>
+#include <sys/resource.h>
 #include <sys/socket.h>
 #include <sys/sysinfo.h>
+#include <sys/time.h>
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
 
 #include <cutils/properties.h>
+#include <cutils/sched_policy.h>
 #include <cutils/sockets.h>
 #include <lmkd.h>
 #include <log/log.h>
 #include <log/log_event_list.h>
 #include <log/log_time.h>
+#include <system/thread_defs.h>
 
 #ifdef LMKD_LOG_STATS
 #include "statslog.h"
@@ -1018,19 +1023,20 @@
 
     // field 10 is pgfault
     // field 12 is pgmajfault
+    // field 22 is starttime
     // field 24 is rss_in_pages
-    int64_t pgfault = 0, pgmajfault = 0, rss_in_pages = 0;
+    int64_t pgfault = 0, pgmajfault = 0, starttime = 0, rss_in_pages = 0;
     if (sscanf(buffer,
                "%*u %*s %*s %*d %*d %*d %*d %*d %*d %" SCNd64 " %*d "
                "%" SCNd64 " %*d %*u %*u %*d %*d %*d %*d %*d %*d "
-               "%*d %*d %" SCNd64 "",
-               &pgfault, &pgmajfault, &rss_in_pages) != 3) {
+               "%" SCNd64 " %*d %" SCNd64 "",
+               &pgfault, &pgmajfault, &starttime, &rss_in_pages) != 4) {
         return -1;
     }
     mem_st->pgfault = pgfault;
     mem_st->pgmajfault = pgmajfault;
     mem_st->rss_in_bytes = (rss_in_pages * PAGE_SIZE);
-
+    mem_st->process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK));
     return 0;
 }
 #endif
@@ -1260,6 +1266,41 @@
     return maxprocp;
 }
 
+static void set_process_group_and_prio(int pid, SchedPolicy sp, int prio) {
+    DIR* d;
+    char proc_path[PATH_MAX];
+    struct dirent* de;
+
+    snprintf(proc_path, sizeof(proc_path), "/proc/%d/task", pid);
+    if (!(d = opendir(proc_path))) {
+        ALOGW("Failed to open %s; errno=%d: process pid(%d) might have died", proc_path, errno,
+              pid);
+        return;
+    }
+
+    while ((de = readdir(d))) {
+        int t_pid;
+
+        if (de->d_name[0] == '.') continue;
+        t_pid = atoi(de->d_name);
+
+        if (!t_pid) {
+            ALOGW("Failed to get t_pid for '%s' of pid(%d)", de->d_name, pid);
+            continue;
+        }
+
+        if (setpriority(PRIO_PROCESS, t_pid, prio) && errno != ESRCH) {
+            ALOGW("Unable to raise priority of killing t_pid (%d): errno=%d", t_pid, errno);
+        }
+
+        if (set_cpuset_policy(t_pid, sp)) {
+            ALOGW("Failed to set_cpuset_policy on pid(%d) t_pid(%d) to %d", pid, t_pid, (int)sp);
+            continue;
+        }
+    }
+    closedir(d);
+}
+
 static int last_killed_pid = -1;
 
 /* Kill one process specified by procp.  Returns the size of the process killed */
@@ -1300,6 +1341,9 @@
 
     /* CAP_KILL required */
     r = kill(pid, SIGKILL);
+
+    set_process_group_and_prio(pid, SP_FOREGROUND, ANDROID_PRIORITY_HIGHEST);
+
     inc_killcnt(procp->oomadj);
     ALOGI("Kill '%s' (%d), uid %d, oom_adj %d to free %ldkB",
         taskname, pid, uid, procp->oomadj, tasksize * page_k);
@@ -1316,10 +1360,10 @@
         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);
+                    mem_st.cache_in_bytes, mem_st.swap_in_bytes, mem_st.process_start_time_ns);
         } else if (enable_stats_log) {
             stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, procp->oomadj,
-                                          -1, -1, tasksize * BYTES_IN_KILOBYTE, -1, -1);
+                                          -1, -1, tasksize * BYTES_IN_KILOBYTE, -1, -1, -1);
         }
 #endif
         result = tasksize;
diff --git a/lmkd/statslog.c b/lmkd/statslog.c
index 66d1164..689e8ae 100644
--- a/lmkd/statslog.c
+++ b/lmkd/statslog.c
@@ -65,7 +65,7 @@
 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) {
+                              int64_t swap_in_bytes, int64_t process_start_time_ns) {
     assert(ctx != NULL);
     int ret = -EINVAL;
     if (!ctx) {
@@ -113,5 +113,9 @@
         return ret;
     }
 
+    if ((ret = android_log_write_int64(ctx, process_start_time_ns)) < 0) {
+        return ret;
+    }
+
     return write_to_logger(ctx, LOG_ID_STATS);
 }
diff --git a/lmkd/statslog.h b/lmkd/statslog.h
index 8458480..f3abe11 100644
--- a/lmkd/statslog.h
+++ b/lmkd/statslog.h
@@ -64,6 +64,7 @@
     int64_t rss_in_bytes;
     int64_t cache_in_bytes;
     int64_t swap_in_bytes;
+    int64_t process_start_time_ns;
 };
 
 #define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%u/memory.stat"
@@ -87,7 +88,7 @@
 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);
+                              int64_t swap_in_bytes, int64_t process_start_time_ns);
 
 __END_DECLS
 
diff --git a/lmkd/tests/Android.bp b/lmkd/tests/Android.bp
index cbf44e9..4e845fd 100644
--- a/lmkd/tests/Android.bp
+++ b/lmkd/tests/Android.bp
@@ -18,6 +18,7 @@
     shared_libs: [
         "libbase",
         "liblog",
+        "libcutils",
     ],
 
     static_libs: [
diff --git a/logd/.clang-format b/logd/.clang-format
deleted file mode 100644
index 393c309..0000000
--- a/logd/.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/logd/.clang-format b/logd/.clang-format
new file mode 120000
index 0000000..1af4f51
--- /dev/null
+++ b/logd/.clang-format
@@ -0,0 +1 @@
+../.clang-format-4
\ No newline at end of file
diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp
index 18d5287..470ffed 100644
--- a/logd/LogAudit.cpp
+++ b/logd/LogAudit.cpp
@@ -300,7 +300,7 @@
         return 0;
     }
 
-    log_time now;
+    log_time now(log_time::EPOCH);
 
     static const char audit_str[] = " audit(";
     char* timeptr = strstr(str, audit_str);
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
index fbdbf79..9cbc7c4 100644
--- a/logd/LogBuffer.cpp
+++ b/logd/LogBuffer.cpp
@@ -399,7 +399,7 @@
                         ((*it)->getLogId() != LOG_ID_KERNEL))) {
         mLogElements.push_back(elem);
     } else {
-        log_time end = log_time::EPOCH;
+        log_time end(log_time::EPOCH);
         bool end_set = false;
         bool end_always = false;
 
diff --git a/logd/LogKlog.cpp b/logd/LogKlog.cpp
index 513c0c3..edd326a 100644
--- a/logd/LogKlog.cpp
+++ b/logd/LogKlog.cpp
@@ -197,10 +197,9 @@
     // NOTREACHED
 }
 
-log_time LogKlog::correction =
-    (log_time(CLOCK_REALTIME) < log_time(CLOCK_MONOTONIC))
-        ? log_time::EPOCH
-        : (log_time(CLOCK_REALTIME) - log_time(CLOCK_MONOTONIC));
+log_time LogKlog::correction = (log_time(CLOCK_REALTIME) < log_time(CLOCK_MONOTONIC))
+                                       ? log_time(log_time::EPOCH)
+                                       : (log_time(CLOCK_REALTIME) - log_time(CLOCK_MONOTONIC));
 
 LogKlog::LogKlog(LogBuffer* buf, LogReader* reader, int fdWrite, int fdRead,
                  bool auditd)
@@ -268,7 +267,7 @@
     static const char real_format[] = "%Y-%m-%d %H:%M:%S.%09q UTC";
     if (len < (ssize_t)(strlen(real_format) + 5)) return;
 
-    log_time real;
+    log_time real(log_time::EPOCH);
     const char* ep = real.strptime(real_string, real_format);
     if (!ep || (ep > &real_string[len]) || (real > log_time(CLOCK_REALTIME))) {
         return;
@@ -282,20 +281,20 @@
     tm.tm_isdst = -1;
     localtime_r(&now, &tm);
     if ((tm.tm_gmtoff < 0) && ((-tm.tm_gmtoff) > (long)real.tv_sec)) {
-        real = log_time::EPOCH;
+        real = log_time(log_time::EPOCH);
     } else {
         real.tv_sec += tm.tm_gmtoff;
     }
     if (monotonic > real) {
-        correction = log_time::EPOCH;
+        correction = log_time(log_time::EPOCH);
     } else {
         correction = real - monotonic;
     }
 }
 
-void LogKlog::sniffTime(log_time& now, const char*& buf, ssize_t len,
-                        bool reverse) {
-    if (len <= 0) return;
+log_time LogKlog::sniffTime(const char*& buf, ssize_t len, bool reverse) {
+    log_time now(log_time::EPOCH);
+    if (len <= 0) return now;
 
     const char* cp = nullptr;
     if ((len > 10) && (*buf == '[')) {
@@ -310,7 +309,7 @@
         }
         buf = cp;
 
-        if (isMonotonic()) return;
+        if (isMonotonic()) return now;
 
         const char* b;
         if (((b = android::strnstr(cp, len, suspendStr))) &&
@@ -329,11 +328,11 @@
             //     trigger a check for ntp-induced or hardware clock drift.
             log_time real(CLOCK_REALTIME);
             log_time mono(CLOCK_MONOTONIC);
-            correction = (real < mono) ? log_time::EPOCH : (real - mono);
+            correction = (real < mono) ? log_time(log_time::EPOCH) : (real - mono);
         } else if (((b = android::strnstr(cp, len, suspendedStr))) &&
                    (((b += strlen(suspendStr)) - cp) < len)) {
             len -= b - cp;
-            log_time real;
+            log_time real(log_time::EPOCH);
             char* endp;
             real.tv_sec = strtol(b, &endp, 10);
             if ((*endp == '.') && ((endp - b) < len)) {
@@ -345,7 +344,7 @@
                 }
                 if (reverse) {
                     if (real > correction) {
-                        correction = log_time::EPOCH;
+                        correction = log_time(log_time::EPOCH);
                     } else {
                         correction -= real;
                     }
@@ -363,6 +362,7 @@
             now = log_time(CLOCK_REALTIME);
         }
     }
+    return now;
 }
 
 pid_t LogKlog::sniffPid(const char*& buf, ssize_t len) {
@@ -451,8 +451,7 @@
     }
     parseKernelPrio(cp, len - (cp - buf));
 
-    log_time now;
-    sniffTime(now, cp, len - (cp - buf), true);
+    log_time now = sniffTime(cp, len - (cp - buf), true);
 
     const char* suspended = android::strnstr(buf, len, suspendedStr);
     if (!suspended || (suspended > cp)) {
@@ -468,7 +467,7 @@
     }
     parseKernelPrio(cp, len - (cp - buf));
 
-    sniffTime(now, cp, len - (cp - buf), true);
+    sniffTime(cp, len - (cp - buf), true);
 }
 
 // Convert kernel log priority number into an Android Logger priority number
@@ -548,8 +547,7 @@
     const char* p = buf;
     int pri = parseKernelPrio(p, len);
 
-    log_time now;
-    sniffTime(now, p, len - (p - buf), false);
+    log_time now = sniffTime(p, len - (p - buf), false);
 
     // sniff for start marker
     const char* start = android::strnstr(p, len - (p - buf), klogdStr);
diff --git a/logd/LogKlog.h b/logd/LogKlog.h
index bb92dd2..6bfd6a8 100644
--- a/logd/LogKlog.h
+++ b/logd/LogKlog.h
@@ -55,11 +55,10 @@
     }
 
    protected:
-    void sniffTime(log_time& now, const char*& buf, ssize_t len, bool reverse);
-    pid_t sniffPid(const char*& buf, ssize_t len);
-    void calculateCorrection(const log_time& monotonic, const char* real_string,
-                             ssize_t len);
-    virtual bool onDataAvailable(SocketClient* cli);
+     log_time sniffTime(const char*& buf, ssize_t len, bool reverse);
+     pid_t sniffPid(const char*& buf, ssize_t len);
+     void calculateCorrection(const log_time& monotonic, const char* real_string, ssize_t len);
+     virtual bool onDataAvailable(SocketClient* cli);
 };
 
 #endif
diff --git a/logd/LogTags.cpp b/logd/LogTags.cpp
index 1ab9dd1..f19e7b0 100644
--- a/logd/LogTags.cpp
+++ b/logd/LogTags.cpp
@@ -30,6 +30,7 @@
 
 #include <android-base/file.h>
 #include <android-base/macros.h>
+#include <android-base/scopeguard.h>
 #include <android-base/stringprintf.h>
 #include <log/log_event_list.h>
 #include <log/log_properties.h>
@@ -38,6 +39,8 @@
 #include "LogTags.h"
 #include "LogUtils.h"
 
+using android::base::make_scope_guard;
+
 static LogTags* logtags;
 
 const char LogTags::system_event_log_tags[] = "/system/etc/event-log-tags";
@@ -316,27 +319,29 @@
         std::string Format;
         android_log_list_element elem;
         {
-            android_log_event_list ctx(log_msg);
-            elem = ctx.read();
+            auto ctx = create_android_log_parser(log_msg.msg() + sizeof(uint32_t),
+                                                 log_msg.entry.len - sizeof(uint32_t));
+            auto guard = make_scope_guard([&ctx]() { android_log_destroy(&ctx); });
+            elem = android_log_read_next(ctx);
             if (elem.type != EVENT_TYPE_LIST) {
                 continue;
             }
-            elem = ctx.read();
+            elem = android_log_read_next(ctx);
             if (elem.type != EVENT_TYPE_INT) {
                 continue;
             }
             Tag = elem.data.int32;
-            elem = ctx.read();
+            elem = android_log_read_next(ctx);
             if (elem.type != EVENT_TYPE_STRING) {
                 continue;
             }
             Name = std::string(elem.data.string, elem.len);
-            elem = ctx.read();
+            elem = android_log_read_next(ctx);
             if (elem.type != EVENT_TYPE_STRING) {
                 continue;
             }
             Format = std::string(elem.data.string, elem.len);
-            elem = ctx.read();
+            elem = android_log_read_next(ctx);
         }
         if ((elem.type != EVENT_TYPE_LIST_STOP) || !elem.complete) continue;
 
@@ -524,10 +529,22 @@
     tag2format_const_iterator iform = tag2format.find(tag);
     std::string Format = (iform != tag2format.end()) ? iform->second : "";
 
-    __android_log_event_list ctx(TAG_DEF_LOG_TAG);
-    ctx << tag << Name << Format;
-    std::string buffer(ctx);
-    if (buffer.length() <= 0) return;  // unlikely
+    auto ctx = create_android_logger(TAG_DEF_LOG_TAG);
+    auto guard = make_scope_guard([&ctx]() { android_log_destroy(&ctx); });
+    if (android_log_write_int32(ctx, static_cast<int32_t>(tag) < 0) ||
+        android_log_write_string8_len(ctx, Name.c_str(), Name.size()) < 0 ||
+        android_log_write_string8_len(ctx, Format.c_str(), Format.size()) < 0) {
+        return;
+    }
+
+    const char* cp = nullptr;
+    ssize_t len = android_log_write_list_buffer(ctx, &cp);
+
+    if (len <= 0 || cp == nullptr) {
+        return;
+    }
+
+    std::string buffer(cp, len);
 
     /*
      *  struct {
diff --git a/rootdir/etc/ld.config.txt b/rootdir/etc/ld.config.txt
index 5601e53..2dda648 100644
--- a/rootdir/etc/ld.config.txt
+++ b/rootdir/etc/ld.config.txt
@@ -109,8 +109,7 @@
 
 # Keep in sync with ld.config.txt in the com.android.runtime APEX.
 namespace.default.links = runtime
-namespace.default.link.runtime.shared_libs  = libc.so:libdl.so:libm.so
-namespace.default.link.runtime.shared_libs += libart.so:libartd.so
+namespace.default.link.runtime.shared_libs  = libart.so:libartd.so
 namespace.default.link.runtime.shared_libs += libnativebridge.so
 namespace.default.link.runtime.shared_libs += libnativehelper.so
 namespace.default.link.runtime.shared_libs += libnativeloader.so
@@ -163,12 +162,8 @@
 
 # Once in this namespace, access to libraries in /system/lib is restricted. Only
 # libs listed here can be used.
-namespace.sphal.links = runtime,default,vndk,rs
+namespace.sphal.links = default,vndk,rs
 
-namespace.sphal.link.runtime.shared_libs = libc.so:libdl.so:libm.so
-
-# LLNDK_LIBRARIES includes the runtime libs above, but the order here ensures
-# that they are loaded from the runtime namespace.
 namespace.sphal.link.default.shared_libs  = %LLNDK_LIBRARIES%
 namespace.sphal.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
 
@@ -215,9 +210,7 @@
 namespace.rs.asan.permitted.paths +=           /vendor/${LIB}
 namespace.rs.asan.permitted.paths += /data
 
-namespace.rs.links = runtime,default,vndk
-
-namespace.rs.link.runtime.shared_libs = libc.so:libdl.so:libm.so
+namespace.rs.links = default,vndk
 
 namespace.rs.link.default.shared_libs  = %LLNDK_LIBRARIES%
 namespace.rs.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
@@ -265,13 +258,10 @@
 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 "runtime" for Bionic libs, "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 = runtime,default,sphal
-
-namespace.vndk.link.runtime.shared_libs = libc.so:libdl.so:libm.so
+# 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
@@ -290,7 +280,7 @@
 # (LL-NDK only) access.
 ###############################################################################
 [vendor]
-additional.namespaces = runtime,system,vndk
+additional.namespaces = system,vndk
 
 ###############################################################################
 # "default" namespace
@@ -321,24 +311,12 @@
 namespace.default.asan.permitted.paths += /data/asan/vendor
 namespace.default.asan.permitted.paths +=           /vendor
 
-namespace.default.links = runtime,system,vndk
-namespace.default.link.runtime.shared_libs = libc.so:libdl.so:libm.so
+namespace.default.links = system,vndk
 namespace.default.link.system.shared_libs = %LLNDK_LIBRARIES%
 namespace.default.link.vndk.shared_libs  = %VNDK_SAMEPROCESS_LIBRARIES%
 namespace.default.link.vndk.shared_libs += %VNDK_CORE_LIBRARIES%
 
 ###############################################################################
-# "runtime" APEX namespace
-#
-# This namespace pulls in externally accessible libs from the Runtime APEX.
-###############################################################################
-namespace.runtime.isolated = true
-namespace.runtime.search.paths = /apex/com.android.runtime/${LIB}
-namespace.runtime.links = system
-# TODO(b/119867084): Restrict to Bionic dlopen dependencies.
-namespace.runtime.link.system.allow_all_shared_libs = true
-
-###############################################################################
 # "vndk" namespace
 #
 # This namespace is where VNDK and VNDK-SP libraries are loaded for
@@ -369,9 +347,7 @@
 # When these NDK libs are required inside this namespace, then it is redirected
 # to the system namespace. This is possible since their ABI is stable across
 # Android releases.
-namespace.vndk.links = runtime,system,default
-
-namespace.vndk.link.runtime.shared_libs = libc.so:libdl.so:libm.so
+namespace.vndk.links = system,default
 
 namespace.vndk.link.system.shared_libs  = %LLNDK_LIBRARIES%
 namespace.vndk.link.system.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
@@ -397,36 +373,16 @@
 namespace.system.asan.search.paths += /data/asan/product_services/${LIB}
 namespace.system.asan.search.paths +=           /%PRODUCT_SERVICES%/${LIB}
 
-namespace.system.links = runtime
-namespace.system.link.runtime.shared_libs = libc.so:libdl.so:libm.so
-
-
 ###############################################################################
 # Namespace config for binaries under /postinstall.
-# Only default and runtime namespaces are defined and default has no directories
+# Only default namespace is defined and default 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 postinstall binaries. There is no
 # reason to allow the binaries to access the paths.
 ###############################################################################
 [postinstall]
-additional.namespaces = runtime
-
 namespace.default.isolated = false
 namespace.default.search.paths  = /system/${LIB}
 namespace.default.search.paths += /%PRODUCT%/${LIB}
 namespace.default.search.paths += /%PRODUCT_SERVICES%/${LIB}
-
-namespace.default.links = runtime
-namespace.default.link.runtime.shared_libs = libc.so:libdl.so:libm.so
-
-###############################################################################
-# "runtime" APEX namespace
-#
-# This namespace pulls in externally accessible libs from the Runtime APEX.
-###############################################################################
-namespace.runtime.isolated = true
-namespace.runtime.search.paths = /apex/com.android.runtime/${LIB}
-namespace.runtime.links = default
-# TODO(b/119867084): Restrict to Bionic dlopen dependencies.
-namespace.runtime.link.default.allow_all_shared_libs = true
diff --git a/rootdir/etc/ld.config.vndk_lite.txt b/rootdir/etc/ld.config.vndk_lite.txt
index 7ca45ff..33b4698 100644
--- a/rootdir/etc/ld.config.vndk_lite.txt
+++ b/rootdir/etc/ld.config.vndk_lite.txt
@@ -57,8 +57,7 @@
 
 # Keep in sync with ld.config.txt in the com.android.runtime APEX.
 namespace.default.links = runtime
-namespace.default.link.runtime.shared_libs  = libc.so:libdl.so:libm.so
-namespace.default.link.runtime.shared_libs += libart.so:libartd.so
+namespace.default.link.runtime.shared_libs  = libart.so:libartd.so
 namespace.default.link.runtime.shared_libs += libnativehelper.so
 namespace.default.link.runtime.shared_libs += libnativeloader.so
 
@@ -110,12 +109,8 @@
 
 # Once in this namespace, access to libraries in /system/lib is restricted. Only
 # libs listed here can be used.
-namespace.sphal.links = runtime,default,vndk,rs
+namespace.sphal.links = default,vndk,rs
 
-namespace.sphal.link.runtime.shared_libs = libc.so:libdl.so:libm.so
-
-# LLNDK_LIBRARIES includes the runtime libs above, but the order here ensures
-# that they are loaded from the runtime namespace.
 namespace.sphal.link.default.shared_libs  = %LLNDK_LIBRARIES%
 namespace.sphal.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
 
@@ -162,9 +157,7 @@
 namespace.rs.asan.permitted.paths +=           /vendor/${LIB}
 namespace.rs.asan.permitted.paths += /data
 
-namespace.rs.links = runtime,default,vndk
-
-namespace.rs.link.runtime.shared_libs = libc.so:libdl.so:libm.so
+namespace.rs.links = default,vndk
 
 namespace.rs.link.default.shared_libs  = %LLNDK_LIBRARIES%
 namespace.rs.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
@@ -215,9 +208,7 @@
 # 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 = runtime,default
-
-namespace.vndk.link.runtime.shared_libs = libc.so:libdl.so:libm.so
+namespace.vndk.links = default
 
 namespace.vndk.link.default.shared_libs  = %LLNDK_LIBRARIES%
 namespace.vndk.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
@@ -230,7 +221,6 @@
 # (LL-NDK only) access.
 ###############################################################################
 [vendor]
-additional.namespaces = runtime
 namespace.default.isolated = false
 
 namespace.default.search.paths  = /odm/${LIB}
@@ -270,47 +260,16 @@
 namespace.default.asan.search.paths += /data/asan/product_services/${LIB}
 namespace.default.asan.search.paths +=           /%PRODUCT_SERVICES%/${LIB}
 
-namespace.default.links = runtime
-namespace.default.link.runtime.shared_libs = libc.so:libdl.so:libm.so
-
-###############################################################################
-# "runtime" APEX namespace
-#
-# This namespace pulls in externally accessible libs from the Runtime APEX.
-###############################################################################
-namespace.runtime.isolated = true
-namespace.runtime.search.paths = /apex/com.android.runtime/${LIB}
-namespace.runtime.links = default
-# TODO(b/119867084): Restrict to Bionic dlopen dependencies.
-namespace.runtime.link.default.allow_all_shared_libs = true
-
-
 ###############################################################################
 # Namespace config for binaries under /postinstall.
-# Only default and runtime namespaces are defined and default has no directories
+# Only default namespace is defined and default 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 postinstall binaries. There is no
 # reason to allow the binaries to access the paths.
 ###############################################################################
 [postinstall]
-additional.namespaces = runtime
-
 namespace.default.isolated = false
 namespace.default.search.paths  = /system/${LIB}
 namespace.default.search.paths += /%PRODUCT%/${LIB}
 namespace.default.search.paths += /%PRODUCT_SERVICES%/${LIB}
-
-namespace.default.links = runtime
-namespace.default.link.runtime.shared_libs = libc.so:libdl.so:libm.so
-
-###############################################################################
-# "runtime" APEX namespace
-#
-# This namespace pulls in externally accessible libs from the Runtime APEX.
-###############################################################################
-namespace.runtime.isolated = true
-namespace.runtime.search.paths = /apex/com.android.runtime/${LIB}
-namespace.runtime.links = default
-# TODO(b/119867084): Restrict to Bionic dlopen dependencies.
-namespace.runtime.link.default.allow_all_shared_libs = true
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 0ec6e17..9b20964 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -12,6 +12,12 @@
 import /init.${ro.zygote}.rc
 
 on early-init
+    # Mount shared so changes propagate into child namespaces
+    # Do this before other processes are started from init. Otherwise,
+    # processes launched while the propagation type of / is 'private'
+    # won't get mount events from others.
+    mount rootfs rootfs / shared rec
+
     # Set init and its forked children's oom_adj.
     write /proc/1/oom_score_adj -1000
 
@@ -40,6 +46,8 @@
     # cgroup for system_server and surfaceflinger
     mkdir /dev/memcg/system 0550 system system
 
+    prepare_bootstrap_bionic
+
     start ueventd
 
 on init
@@ -338,20 +346,12 @@
     trigger boot
 
 on post-fs
-    # Load properties from
-    #     /system/build.prop,
-    #     /odm/build.prop,
-    #     /vendor/build.prop and
-    #     /factory/factory.prop
-    load_system_props
     start vold
     exec - system system -- /system/bin/vdc checkpoint markBootAttempt
 
     # Once everything is setup, no need to modify /.
     # 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
     mount none /mnt/runtime/default /storage bind rec
     mount none none /storage slave rec
@@ -587,6 +587,14 @@
     # Check any timezone data in /data is newer than the copy in the runtime module, delete if not.
     exec - system system -- /system/bin/tzdatacheck /apex/com.android.runtime/etc/tz /data/misc/zoneinfo
 
+    # Wait for apexd to finish activating APEXes before starting more processes.
+    # This certainly reduces the parallelism but is required to make as many processes
+    # as possible to use the bionic libs from the runtime APEX. This takes less than 50ms
+    # so the impact on the booting time is not significant.
+    wait_for_prop apexd.status ready
+    setup_runtime_bionic
+    parse_apex_configs
+
     # If there is no post-fs-data action in the init.<device>.rc file, you
     # must uncomment this line, otherwise encrypted filesystems
     # won't work.
@@ -808,6 +816,3 @@
 service flash_recovery /system/bin/install-recovery.sh
     class main
     oneshot
-
-on property:apexd.status=ready
-    parse_apex_configs
diff --git a/run-as/run-as.cpp b/run-as/run-as.cpp
index f49bdf7..8752eef 100644
--- a/run-as/run-as.cpp
+++ b/run-as/run-as.cpp
@@ -26,6 +26,7 @@
 #include <unistd.h>
 
 #include <string>
+#include <vector>
 
 #include <libminijail.h>
 #include <scoped_minijail.h>
@@ -131,6 +132,25 @@
   return check_directory(data_path, uid);
 }
 
+std::vector<gid_t> get_supplementary_gids(uid_t userAppId) {
+  std::vector<gid_t> gids;
+  int size = getgroups(0, &gids[0]);
+  if (size < 0) {
+    error(1, errno, "getgroups failed");
+  }
+  gids.resize(size);
+  size = getgroups(size, &gids[0]);
+  if (size != static_cast<int>(gids.size())) {
+    error(1, errno, "getgroups failed");
+  }
+  // Profile guide compiled oat files (like /data/app/xxx/oat/arm64/base.odex) are not readable
+  // worldwide (DEXOPT_PUBLIC flag isn't set). To support reading them (needed by simpleperf for
+  // profiling), add shared app gid to supplementary groups.
+  gid_t shared_app_gid = userAppId % AID_USER_OFFSET - AID_APP_START + AID_SHARED_GID_START;
+  gids.push_back(shared_app_gid);
+  return gids;
+}
+
 int main(int argc, char* argv[]) {
   // Check arguments.
   if (argc < 2) {
@@ -210,10 +230,11 @@
   // same time to avoid nasty surprises.
   uid_t uid = userAppId;
   uid_t gid = userAppId;
+  std::vector<gid_t> supplementary_gids = get_supplementary_gids(userAppId);
   ScopedMinijail j(minijail_new());
   minijail_change_uid(j.get(), uid);
   minijail_change_gid(j.get(), gid);
-  minijail_keep_supplementary_gids(j.get());
+  minijail_set_supplementary_gids(j.get(), supplementary_gids.size(), supplementary_gids.data());
   minijail_enter(j.get());
 
   std::string seinfo = std::string(info.seinfo) + ":fromRunAs";
diff --git a/storaged/storaged.cpp b/storaged/storaged.cpp
index 77c6167..6897663 100644
--- a/storaged/storaged.cpp
+++ b/storaged/storaged.cpp
@@ -341,20 +341,14 @@
     if (mConfig.event_time_check_usec &&
         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_ts) < 0) {
         check_time = false;
-        static time_t state_a;
-        IF_ALOG_RATELIMIT_LOCAL(300, &state_a) {
-            PLOG_TO(SYSTEM, ERROR) << "clock_gettime() failed";
-        }
+        PLOG_TO(SYSTEM, ERROR) << "clock_gettime() failed";
     }
 
     event();
 
     if (mConfig.event_time_check_usec && check_time) {
         if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_ts) < 0) {
-            static time_t state_b;
-            IF_ALOG_RATELIMIT_LOCAL(300, &state_b) {
-                PLOG_TO(SYSTEM, ERROR) << "clock_gettime() failed";
-            }
+            PLOG_TO(SYSTEM, ERROR) << "clock_gettime() failed";
             return;
         }
         int64_t cost = (end_ts.tv_sec - start_ts.tv_sec) * SEC_TO_USEC +
diff --git a/toolbox/getevent.c b/toolbox/getevent.c
index 39033ad..e2c77c3 100644
--- a/toolbox/getevent.c
+++ b/toolbox/getevent.c
@@ -530,6 +530,9 @@
     const char *device = NULL;
     const char *device_path = "/dev/input";
 
+    /* disable buffering on stdout */
+    setbuf(stdout, NULL);
+
     opterr = 0;
     do {
         c = getopt(argc, argv, "tns:Sv::dpilqc:rh");