Merge "Remove two more unused utf32 functions."
diff --git a/adb/Android.bp b/adb/Android.bp
index 6558b1b..06cfcbf 100644
--- a/adb/Android.bp
+++ b/adb/Android.bp
@@ -27,6 +27,7 @@
"-DADB_HOST=1", // overridden by adbd_defaults
"-DALLOW_ADBD_ROOT=0", // overridden by adbd_defaults
"-DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION=1",
+ "-DENABLE_FASTDEPLOY=1", // enable fast deploy
],
cpp_std: "experimental",
@@ -221,6 +222,7 @@
target: {
windows: {
enabled: true,
+ ldflags: ["-municode"],
shared_libs: ["AdbWinApi"],
},
},
@@ -269,22 +271,33 @@
"client/console.cpp",
"client/adb_install.cpp",
"client/line_printer.cpp",
+ "client/fastdeploy.cpp",
+ "client/fastdeploycallbacks.cpp",
"shell_service_protocol.cpp",
],
+ generated_headers: [
+ "bin2c_fastdeployagent",
+ "bin2c_fastdeployagentscript"
+ ],
+
static_libs: [
"libadb_host",
+ "libandroidfw",
"libbase",
"libcutils",
"libcrypto_utils",
"libcrypto",
+ "libfastdeploy_host",
"libdiagnose_usb",
"liblog",
"libmdnssd",
+ "libprotobuf-cpp-lite",
"libusb",
"libutils",
"liblog",
- "libcutils",
+ "libziparchive",
+ "libz",
],
stl: "libc++_static",
@@ -294,10 +307,6 @@
// will violate ODR
shared_libs: [],
- required: [
- "deploypatchgenerator",
- ],
-
// Archive adb, adb.exe.
dist: {
targets: [
@@ -657,3 +666,90 @@
},
},
}
+
+// Note: using pipe for xxd to control the variable name generated
+// the default name used by xxd is the path to the input file.
+java_genrule {
+ name: "bin2c_fastdeployagent",
+ out: ["deployagent.inc"],
+ srcs: [":deployagent"],
+ cmd: "(echo 'unsigned char kDeployAgent[] = {' && xxd -i <$(in) && echo '};') > $(out)",
+}
+
+genrule {
+ name: "bin2c_fastdeployagentscript",
+ out: ["deployagentscript.inc"],
+ srcs: ["fastdeploy/deployagent/deployagent.sh"],
+ cmd: "(echo 'unsigned char kDeployAgentScript[] = {' && xxd -i <$(in) && echo '};') > $(out)",
+}
+
+cc_library_host_static {
+ name: "libfastdeploy_host",
+ defaults: ["adb_defaults"],
+ srcs: [
+ "fastdeploy/deploypatchgenerator/deploy_patch_generator.cpp",
+ "fastdeploy/deploypatchgenerator/patch_utils.cpp",
+ "fastdeploy/proto/ApkEntry.proto",
+ ],
+ static_libs: [
+ "libadb_host",
+ "libandroidfw",
+ "libbase",
+ "libcutils",
+ "libcrypto_utils",
+ "libcrypto",
+ "libdiagnose_usb",
+ "liblog",
+ "libmdnssd",
+ "libusb",
+ "libutils",
+ "libziparchive",
+ "libz",
+ ],
+ stl: "libc++_static",
+ proto: {
+ type: "lite",
+ export_proto_headers: true,
+ },
+ target: {
+ windows: {
+ enabled: true,
+ shared_libs: ["AdbWinApi"],
+ },
+ },
+}
+
+cc_test_host {
+ name: "fastdeploy_test",
+ defaults: ["adb_defaults"],
+ srcs: [
+ "fastdeploy/deploypatchgenerator/deploy_patch_generator_test.cpp",
+ "fastdeploy/deploypatchgenerator/patch_utils_test.cpp",
+ ],
+ static_libs: [
+ "libadb_host",
+ "libandroidfw",
+ "libbase",
+ "libcutils",
+ "libcrypto_utils",
+ "libcrypto",
+ "libdiagnose_usb",
+ "libfastdeploy_host",
+ "liblog",
+ "libmdnssd",
+ "libprotobuf-cpp-lite",
+ "libusb",
+ "libutils",
+ "libziparchive",
+ "libz",
+ ],
+ target: {
+ windows: {
+ enabled: true,
+ shared_libs: ["AdbWinApi"],
+ },
+ },
+ data: [
+ "fastdeploy/testdata/rotating_cube-release.apk",
+ ],
+}
diff --git a/adb/adb_utils.cpp b/adb/adb_utils.cpp
index cf5fbc8..d1910f1 100644
--- a/adb/adb_utils.cpp
+++ b/adb/adb_utils.cpp
@@ -320,6 +320,10 @@
}
std::string GetLogFilePath() {
+ // https://issuetracker.google.com/112588493
+ const char* path = getenv("ANDROID_ADB_LOG_PATH");
+ if (path) return path;
+
#if defined(_WIN32)
const char log_name[] = "adb.log";
WCHAR temp_path[MAX_PATH];
diff --git a/adb/client/commandline.cpp b/adb/client/commandline.cpp
index 11a3dfd..3c03eb2 100644
--- a/adb/client/commandline.cpp
+++ b/adb/client/commandline.cpp
@@ -96,9 +96,8 @@
" version show version num\n"
"\n"
"networking:\n"
- " connect HOST[:PORT] connect to a device via TCP/IP [default port=5555]\n"
- " disconnect [HOST[:PORT]]\n"
- " disconnect from given TCP/IP device [default port=5555], or all\n"
+ " connect HOST[:PORT] connect to a device via TCP/IP\n"
+ " disconnect [[HOST]:PORT] disconnect from given TCP/IP device, or all\n"
" forward --list list all forward socket connections\n"
" forward [--no-rebind] LOCAL REMOTE\n"
" forward socket connection using:\n"
@@ -130,7 +129,7 @@
" -a: preserve file timestamp and mode\n"
" sync [all|data|odm|oem|product|system|system_ext|vendor]\n"
" sync a local build from $ANDROID_PRODUCT_OUT to the device (default all)\n"
- " -l: list but don't copy\n"
+ " -l: list files that would be copied, but don't copy them\n"
"\n"
"shell:\n"
" shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]\n"
@@ -1615,13 +1614,13 @@
return adb_query_command(query);
}
else if (!strcmp(argv[0], "connect")) {
- if (argc != 2) error_exit("usage: adb connect <host>[:<port>]");
+ if (argc != 2) error_exit("usage: adb connect HOST[:PORT>]");
std::string query = android::base::StringPrintf("host:connect:%s", argv[1]);
return adb_query_command(query);
}
else if (!strcmp(argv[0], "disconnect")) {
- if (argc > 2) error_exit("usage: adb disconnect [<host>[:<port>]]");
+ if (argc > 2) error_exit("usage: adb disconnect [HOST[:PORT]]");
std::string query = android::base::StringPrintf("host:disconnect:%s",
(argc == 2) ? argv[1] : "");
@@ -1891,7 +1890,10 @@
} else if (!strcmp(argv[0], "track-jdwp")) {
return adb_connect_command("track-jdwp");
} else if (!strcmp(argv[0], "track-devices")) {
- return adb_connect_command("host:track-devices");
+ if (argc > 2 || (argc == 2 && strcmp(argv[1], "-l"))) {
+ error_exit("usage: adb track-devices [-l]");
+ }
+ return adb_connect_command(argc == 2 ? "host:track-devices-l" : "host:track-devices");
} else if (!strcmp(argv[0], "raw")) {
if (argc != 2) {
error_exit("usage: adb raw SERVICE");
diff --git a/adb/client/fastdeploy.cpp b/adb/client/fastdeploy.cpp
index f4e8664..fbae219 100644
--- a/adb/client/fastdeploy.cpp
+++ b/adb/client/fastdeploy.cpp
@@ -27,6 +27,9 @@
#include "androidfw/ZipFileRO.h"
#include "client/file_sync_client.h"
#include "commandline.h"
+#include "deployagent.inc" // Generated include via build rule.
+#include "deployagentscript.inc" // Generated include via build rule.
+#include "fastdeploy/deploypatchgenerator/deploy_patch_generator.h"
#include "fastdeploycallbacks.h"
#include "sysdeps.h"
@@ -35,6 +38,8 @@
static constexpr long kRequiredAgentVersion = 0x00000002;
static constexpr const char* kDeviceAgentPath = "/data/local/tmp/";
+static constexpr const char* kDeviceAgentFile = "/data/local/tmp/deployagent.jar";
+static constexpr const char* kDeviceAgentScript = "/data/local/tmp/deployagent";
static bool g_use_localagent = false;
@@ -71,46 +76,32 @@
g_use_localagent = use_localagent;
}
-// local_path - must start with a '/' and be relative to $ANDROID_PRODUCT_OUT
-static std::string get_agent_component_host_path(const char* local_path, const char* sdk_path) {
- std::string adb_dir = android::base::GetExecutableDirectory();
- if (adb_dir.empty()) {
- error_exit("Could not determine location of adb!");
- }
-
- if (g_use_localagent) {
- const char* product_out = getenv("ANDROID_PRODUCT_OUT");
- if (product_out == nullptr) {
- error_exit("Could not locate %s because $ANDROID_PRODUCT_OUT is not defined",
- local_path);
- }
- return android::base::StringPrintf("%s%s", product_out, local_path);
- } else {
- return adb_dir + sdk_path;
- }
-}
-
static bool deploy_agent(bool checkTimeStamps) {
std::vector<const char*> srcs;
- std::string jar_path =
- get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar");
- std::string script_path =
- get_agent_component_host_path("/system/bin/deployagent", "/deployagent");
- srcs.push_back(jar_path.c_str());
- srcs.push_back(script_path.c_str());
-
- if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) {
- // on windows the shell script might have lost execute permission
- // so need to set this explicitly
- const char* kChmodCommandPattern = "chmod 777 %sdeployagent";
- std::string chmodCommand =
- android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath);
- int ret = send_shell_command(chmodCommand);
- if (ret != 0) {
- error_exit("Error executing %s returncode: %d", chmodCommand.c_str(), ret);
- }
- } else {
- error_exit("Error pushing agent files to device");
+ // TODO: Deploy agent from bin2c directly instead of writing to disk first.
+ TemporaryFile tempAgent;
+ android::base::WriteFully(tempAgent.fd, kDeployAgent, sizeof(kDeployAgent));
+ srcs.push_back(tempAgent.path);
+ if (!do_sync_push(srcs, kDeviceAgentFile, checkTimeStamps)) {
+ error_exit("Failed to push fastdeploy agent to device.");
+ }
+ srcs.clear();
+ // TODO: Deploy agent from bin2c directly instead of writing to disk first.
+ TemporaryFile tempAgentScript;
+ android::base::WriteFully(tempAgentScript.fd, kDeployAgentScript, sizeof(kDeployAgentScript));
+ srcs.push_back(tempAgentScript.path);
+ if (!do_sync_push(srcs, kDeviceAgentScript, checkTimeStamps)) {
+ error_exit("Failed to push fastdeploy agent script to device.");
+ }
+ srcs.clear();
+ // on windows the shell script might have lost execute permission
+ // so need to set this explicitly
+ const char* kChmodCommandPattern = "chmod 777 %s";
+ std::string chmodCommand =
+ android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentScript);
+ int ret = send_shell_command(chmodCommand);
+ if (ret != 0) {
+ error_exit("Error executing %s returncode: %d", chmodCommand.c_str(), ret);
}
return true;
@@ -238,34 +229,15 @@
}
}
-static std::string get_patch_generator_command() {
- if (g_use_localagent) {
- // This should never happen on a Windows machine
- const char* host_out = getenv("ANDROID_HOST_OUT");
- if (host_out == nullptr) {
- error_exit(
- "Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT "
- "is not defined");
- }
- return android::base::StringPrintf("java -jar %s/framework/deploypatchgenerator.jar",
- host_out);
- }
-
- std::string adb_dir = android::base::GetExecutableDirectory();
- if (adb_dir.empty()) {
- error_exit("Could not locate deploypatchgenerator.jar");
- }
- return android::base::StringPrintf(R"(java -jar "%s/deploypatchgenerator.jar")",
- adb_dir.c_str());
-}
-
void create_patch(const char* apkPath, const char* metadataPath, const char* patchPath) {
- std::string generatePatchCommand = android::base::StringPrintf(
- R"(%s "%s" "%s" > "%s")", get_patch_generator_command().c_str(), apkPath, metadataPath,
- patchPath);
- int returnCode = system(generatePatchCommand.c_str());
- if (returnCode != 0) {
- error_exit("Executing %s returned %d", generatePatchCommand.c_str(), returnCode);
+ DeployPatchGenerator generator(false);
+ unique_fd patchFd(adb_open(patchPath, O_WRONLY | O_CREAT | O_CLOEXEC));
+ if (patchFd < 0) {
+ perror_exit("adb: failed to create %s", patchPath);
+ }
+ bool success = generator.CreatePatch(apkPath, metadataPath, patchFd);
+ if (!success) {
+ error_exit("Failed to create patch for %s", apkPath);
}
}
diff --git a/adb/client/usb_linux.cpp b/adb/client/usb_linux.cpp
index 81b8306..17b4db1 100644
--- a/adb/client/usb_linux.cpp
+++ b/adb/client/usb_linux.cpp
@@ -324,7 +324,7 @@
h->urb_out_busy = true;
while (true) {
- auto now = std::chrono::system_clock::now();
+ auto now = std::chrono::steady_clock::now();
if (h->cv.wait_until(lock, now + 5s) == std::cv_status::timeout || h->dead) {
// TODO: call USBDEVFS_DISCARDURB?
errno = ETIMEDOUT;
diff --git a/adb/daemon/shell_service.cpp b/adb/daemon/shell_service.cpp
index de97068..0fb14c4 100644
--- a/adb/daemon/shell_service.cpp
+++ b/adb/daemon/shell_service.cpp
@@ -222,7 +222,7 @@
bool Subprocess::ForkAndExec(std::string* error) {
unique_fd child_stdinout_sfd, child_stderr_sfd;
unique_fd parent_error_sfd, child_error_sfd;
- char pts_name[PATH_MAX];
+ const char* pts_name = nullptr;
if (command_.empty()) {
__android_log_security_bswrite(SEC_TAG_ADB_SHELL_INTERACTIVE, "");
@@ -283,10 +283,22 @@
cenv.push_back(nullptr);
if (type_ == SubprocessType::kPty) {
- int fd;
- pid_ = forkpty(&fd, pts_name, nullptr, nullptr);
+ unique_fd pty_master(posix_openpt(O_RDWR | O_NOCTTY | O_CLOEXEC));
+ if (pty_master == -1) {
+ *error =
+ android::base::StringPrintf("failed to create pty master: %s", strerror(errno));
+ return false;
+ }
+ if (unlockpt(pty_master.get()) != 0) {
+ *error = android::base::StringPrintf("failed to unlockpt pty master: %s",
+ strerror(errno));
+ return false;
+ }
+
+ pid_ = fork();
+ pts_name = ptsname(pty_master.get());
if (pid_ > 0) {
- stdinout_sfd_.reset(fd);
+ stdinout_sfd_ = std::move(pty_master);
}
} else {
if (!CreateSocketpair(&stdinout_sfd_, &child_stdinout_sfd)) {
diff --git a/adb/fastdeploy/Android.bp b/adb/fastdeploy/Android.bp
index 1ba0de0..95e1a28 100644
--- a/adb/fastdeploy/Android.bp
+++ b/adb/fastdeploy/Android.bp
@@ -13,27 +13,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
-
java_binary {
name: "deployagent",
sdk_version: "24",
srcs: ["deployagent/src/**/*.java", "deploylib/src/**/*.java", "proto/**/*.proto"],
static_libs: ["apkzlib_zip"],
- wrapper: "deployagent/deployagent.sh",
proto: {
type: "lite",
},
dex_preopt: {
enabled: false,
}
-}
-
-java_binary_host {
- name: "deploypatchgenerator",
- srcs: ["deploypatchgenerator/src/**/*.java", "deploylib/src/**/*.java", "proto/**/*.proto"],
- static_libs: ["apkzlib"],
- manifest: "deploypatchgenerator/manifest.txt",
- proto: {
- type: "full",
- }
-}
+}
\ No newline at end of file
diff --git a/adb/fastdeploy/deployagent/src/com/android/fastdeploy/DeployAgent.java b/adb/fastdeploy/deployagent/src/com/android/fastdeploy/DeployAgent.java
index 2d3b135..a8103c4 100644
--- a/adb/fastdeploy/deployagent/src/com/android/fastdeploy/DeployAgent.java
+++ b/adb/fastdeploy/deployagent/src/com/android/fastdeploy/DeployAgent.java
@@ -181,7 +181,7 @@
private static void extractMetaData(String packageName) throws IOException {
File apkFile = getFileFromPackageName(packageName);
APKMetaData apkMetaData = PatchUtils.getAPKMetaData(apkFile);
- apkMetaData.writeDelimitedTo(System.out);
+ apkMetaData.writeTo(System.out);
}
private static int createInstallSession(String[] args) throws IOException {
@@ -223,7 +223,6 @@
}
int writeExitCode = writePatchedDataToSession(new RandomAccessFile(deviceFile, "r"), deltaStream, sessionId);
-
if (writeExitCode == 0) {
return commitInstallSession(sessionId);
} else {
@@ -285,7 +284,6 @@
if (oldDataLen > 0) {
PatchUtils.pipe(oldData, outputStream, buffer, (int) oldDataLen);
}
-
newDataBytesWritten += copyLen + oldDataLen;
}
diff --git a/adb/fastdeploy/deploylib/src/com/android/fastdeploy/PatchUtils.java b/adb/fastdeploy/deploylib/src/com/android/fastdeploy/PatchUtils.java
index f0f00e1..c60f9a6 100644
--- a/adb/fastdeploy/deploylib/src/com/android/fastdeploy/PatchUtils.java
+++ b/adb/fastdeploy/deploylib/src/com/android/fastdeploy/PatchUtils.java
@@ -39,7 +39,7 @@
class PatchUtils {
private static final long NEGATIVE_MASK = 1L << 63;
private static final long NEGATIVE_LONG_SIGN_MASK = 1L << 63;
- public static final String SIGNATURE = "HAMADI/IHD";
+ public static final String SIGNATURE = "FASTDEPLOY";
private static long getOffsetFromEntry(StoredEntry entry) {
return entry.getCentralDirectoryHeader().getOffset() + entry.getLocalHeaderSize();
diff --git a/adb/fastdeploy/deploypatchgenerator/deploy_patch_generator.cpp b/adb/fastdeploy/deploypatchgenerator/deploy_patch_generator.cpp
new file mode 100644
index 0000000..22c9243
--- /dev/null
+++ b/adb/fastdeploy/deploypatchgenerator/deploy_patch_generator.cpp
@@ -0,0 +1,167 @@
+/*
+ * 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 "deploy_patch_generator.h"
+
+#include <inttypes.h>
+#include <stdio.h>
+
+#include <algorithm>
+#include <fstream>
+#include <functional>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include "adb_unique_fd.h"
+#include "android-base/file.h"
+#include "patch_utils.h"
+#include "sysdeps.h"
+
+using namespace com::android::fastdeploy;
+
+void DeployPatchGenerator::Log(const char* fmt, ...) {
+ if (!is_verbose_) {
+ return;
+ }
+ va_list ap;
+ va_start(ap, fmt);
+ vprintf(fmt, ap);
+ printf("\n");
+ va_end(ap);
+}
+
+void DeployPatchGenerator::APKEntryToLog(const APKEntry& entry) {
+ Log("Filename: %s", entry.filename().c_str());
+ Log("CRC32: 0x%08llX", entry.crc32());
+ Log("Data Offset: %lld", entry.dataoffset());
+ Log("Compressed Size: %lld", entry.compressedsize());
+ Log("Uncompressed Size: %lld", entry.uncompressedsize());
+}
+
+void DeployPatchGenerator::APKMetaDataToLog(const char* file, const APKMetaData& metadata) {
+ if (!is_verbose_) {
+ return;
+ }
+ Log("APK Metadata: %s", file);
+ for (int i = 0; i < metadata.entries_size(); i++) {
+ const APKEntry& entry = metadata.entries(i);
+ APKEntryToLog(entry);
+ }
+}
+
+void DeployPatchGenerator::ReportSavings(const std::vector<SimpleEntry>& identicalEntries,
+ uint64_t totalSize) {
+ long totalEqualBytes = 0;
+ int totalEqualFiles = 0;
+ for (size_t i = 0; i < identicalEntries.size(); i++) {
+ if (identicalEntries[i].deviceEntry != nullptr) {
+ totalEqualBytes += identicalEntries[i].localEntry->compressedsize();
+ totalEqualFiles++;
+ }
+ }
+ float savingPercent = (totalEqualBytes * 100.0f) / totalSize;
+ fprintf(stderr, "Detected %d equal APK entries\n", totalEqualFiles);
+ fprintf(stderr, "%ld bytes are equal out of %" PRIu64 " (%.2f%%)\n", totalEqualBytes, totalSize,
+ savingPercent);
+}
+
+void DeployPatchGenerator::GeneratePatch(const std::vector<SimpleEntry>& entriesToUseOnDevice,
+ const char* localApkPath, borrowed_fd output) {
+ unique_fd input(adb_open(localApkPath, O_RDONLY | O_CLOEXEC));
+ size_t newApkSize = adb_lseek(input, 0L, SEEK_END);
+ adb_lseek(input, 0L, SEEK_SET);
+
+ PatchUtils::WriteSignature(output);
+ PatchUtils::WriteLong(newApkSize, output);
+ size_t currentSizeOut = 0;
+ // Write data from the host upto the first entry we have that matches a device entry. Then write
+ // the metadata about the device entry and repeat for all entries that match on device. Finally
+ // write out any data left. If the device and host APKs are exactly the same this ends up
+ // writing out zip metadata from the local APK followed by offsets to the data to use from the
+ // device APK.
+ for (auto&& entry : entriesToUseOnDevice) {
+ int64_t deviceDataOffset = entry.deviceEntry->dataoffset();
+ int64_t hostDataOffset = entry.localEntry->dataoffset();
+ int64_t deviceDataLength = entry.deviceEntry->compressedsize();
+ int64_t deltaFromDeviceDataStart = hostDataOffset - currentSizeOut;
+ PatchUtils::WriteLong(deltaFromDeviceDataStart, output);
+ if (deltaFromDeviceDataStart > 0) {
+ PatchUtils::Pipe(input, output, deltaFromDeviceDataStart);
+ }
+ PatchUtils::WriteLong(deviceDataOffset, output);
+ PatchUtils::WriteLong(deviceDataLength, output);
+ adb_lseek(input, deviceDataLength, SEEK_CUR);
+ currentSizeOut += deltaFromDeviceDataStart + deviceDataLength;
+ }
+ if (currentSizeOut != newApkSize) {
+ PatchUtils::WriteLong(newApkSize - currentSizeOut, output);
+ PatchUtils::Pipe(input, output, newApkSize - currentSizeOut);
+ PatchUtils::WriteLong(0, output);
+ PatchUtils::WriteLong(0, output);
+ }
+}
+
+bool DeployPatchGenerator::CreatePatch(const char* localApkPath, const char* deviceApkMetadataPath,
+ borrowed_fd output) {
+ std::string content;
+ APKMetaData deviceApkMetadata;
+ if (android::base::ReadFileToString(deviceApkMetadataPath, &content)) {
+ deviceApkMetadata.ParsePartialFromString(content);
+ } else {
+ // TODO: What do we want to do if we don't find any metadata.
+ // The current fallback behavior is to build a patch with the contents of |localApkPath|.
+ }
+
+ APKMetaData localApkMetadata = PatchUtils::GetAPKMetaData(localApkPath);
+ // Log gathered metadata info.
+ APKMetaDataToLog(deviceApkMetadataPath, deviceApkMetadata);
+ APKMetaDataToLog(localApkPath, localApkMetadata);
+
+ std::vector<SimpleEntry> identicalEntries;
+ uint64_t totalSize =
+ BuildIdenticalEntries(identicalEntries, localApkMetadata, deviceApkMetadata);
+ ReportSavings(identicalEntries, totalSize);
+ GeneratePatch(identicalEntries, localApkPath, output);
+ return true;
+}
+
+uint64_t DeployPatchGenerator::BuildIdenticalEntries(std::vector<SimpleEntry>& outIdenticalEntries,
+ const APKMetaData& localApkMetadata,
+ const APKMetaData& deviceApkMetadata) {
+ uint64_t totalSize = 0;
+ for (int i = 0; i < localApkMetadata.entries_size(); i++) {
+ const APKEntry& localEntry = localApkMetadata.entries(i);
+ totalSize += localEntry.compressedsize();
+ for (int j = 0; j < deviceApkMetadata.entries_size(); j++) {
+ const APKEntry& deviceEntry = deviceApkMetadata.entries(j);
+ if (deviceEntry.crc32() == localEntry.crc32() &&
+ deviceEntry.filename().compare(localEntry.filename()) == 0) {
+ SimpleEntry simpleEntry;
+ simpleEntry.localEntry = const_cast<APKEntry*>(&localEntry);
+ simpleEntry.deviceEntry = const_cast<APKEntry*>(&deviceEntry);
+ APKEntryToLog(localEntry);
+ outIdenticalEntries.push_back(simpleEntry);
+ break;
+ }
+ }
+ }
+ std::sort(outIdenticalEntries.begin(), outIdenticalEntries.end(),
+ [](const SimpleEntry& lhs, const SimpleEntry& rhs) {
+ return lhs.localEntry->dataoffset() < rhs.localEntry->dataoffset();
+ });
+ return totalSize;
+}
\ No newline at end of file
diff --git a/adb/fastdeploy/deploypatchgenerator/deploy_patch_generator.h b/adb/fastdeploy/deploypatchgenerator/deploy_patch_generator.h
new file mode 100644
index 0000000..30e41a5
--- /dev/null
+++ b/adb/fastdeploy/deploypatchgenerator/deploy_patch_generator.h
@@ -0,0 +1,102 @@
+/*
+ * 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 <vector>
+
+#include "adb_unique_fd.h"
+#include "fastdeploy/proto/ApkEntry.pb.h"
+
+/**
+ * This class is responsible for creating a patch that can be accepted by the deployagent. The
+ * patch format is documented in GeneratePatch.
+ */
+class DeployPatchGenerator {
+ public:
+ /**
+ * Simple struct to hold mapping between local metadata and device metadata.
+ */
+ struct SimpleEntry {
+ com::android::fastdeploy::APKEntry* localEntry;
+ com::android::fastdeploy::APKEntry* deviceEntry;
+ };
+
+ /**
+ * If |is_verbose| is true ApkEntries that are similar between device and host are written to
+ * the console.
+ */
+ explicit DeployPatchGenerator(bool is_verbose) : is_verbose_(is_verbose) {}
+ /**
+ * Given a |localApkPath|, and the |deviceApkMetadataPath| from an installed APK this function
+ * writes a patch to the given |output|.
+ */
+ bool CreatePatch(const char* localApkPath, const char* deviceApkMetadataPath,
+ android::base::borrowed_fd output);
+
+ private:
+ bool is_verbose_;
+
+ /**
+ * Log function only logs data to stdout when |is_verbose_| is true.
+ */
+ void Log(const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3)));
+
+ /**
+ * Helper function to log the APKMetaData structure. If |is_verbose_| is false this function
+ * early outs. |file| is the path to the file represented by |metadata|. This function is used
+ * for debugging / information.
+ */
+ void APKMetaDataToLog(const char* file, const com::android::fastdeploy::APKMetaData& metadata);
+ /**
+ * Helper function to log APKEntry.
+ */
+ void APKEntryToLog(const com::android::fastdeploy::APKEntry& entry);
+
+ /**
+ * Helper function to report savings by fastdeploy. This function prints out savings even with
+ * |is_verbose_| set to false. |totalSize| is used to show a percentage of savings. Note:
+ * |totalSize| is the size of the ZipEntries. Not the size of the entire file. The metadata of
+ * the zip data needs to be sent across with every iteration.
+ * [Patch format]
+ * |Fixed String| Signature
+ * |long| New Size of Apk
+ * |Packets[]| Array of Packets
+ *
+ * [Packet Format]
+ * |long| Size of data to use from patch
+ * |byte[]| Patch data
+ * |long| Offset of data to use already on device
+ * |long| Length of data to read from device APK
+ * TODO(b/138306784): Move the patch format to a proto.
+ */
+ void ReportSavings(const std::vector<SimpleEntry>& identicalEntries, uint64_t totalSize);
+
+ /**
+ * This enumerates each entry in |entriesToUseOnDevice| and builds a patch file copying data
+ * from |localApkPath| where we are unable to use entries already on the device. The new patch
+ * is written to |output|. The entries are expected to be sorted by data offset from lowest to
+ * highest.
+ */
+ void GeneratePatch(const std::vector<SimpleEntry>& entriesToUseOnDevice,
+ const char* localApkPath, android::base::borrowed_fd output);
+
+ protected:
+ uint64_t BuildIdenticalEntries(
+ std::vector<SimpleEntry>& outIdenticalEntries,
+ const com::android::fastdeploy::APKMetaData& localApkMetadata,
+ const com::android::fastdeploy::APKMetaData& deviceApkMetadataPath);
+};
\ No newline at end of file
diff --git a/adb/fastdeploy/deploypatchgenerator/deploy_patch_generator_test.cpp b/adb/fastdeploy/deploypatchgenerator/deploy_patch_generator_test.cpp
new file mode 100644
index 0000000..9cdc44e
--- /dev/null
+++ b/adb/fastdeploy/deploypatchgenerator/deploy_patch_generator_test.cpp
@@ -0,0 +1,72 @@
+/*
+ * 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 "deploy_patch_generator.h"
+#include "patch_utils.h"
+
+#include <android-base/file.h>
+#include <gtest/gtest.h>
+#include <stdlib.h>
+#include <string.h>
+#include <string>
+
+#include "sysdeps.h"
+
+using namespace com::android::fastdeploy;
+
+static std::string GetTestFile(const std::string& name) {
+ return "fastdeploy/testdata/" + name;
+}
+
+class TestPatchGenerator : DeployPatchGenerator {
+ public:
+ TestPatchGenerator() : DeployPatchGenerator(false) {}
+ void GatherIdenticalEntries(std::vector<DeployPatchGenerator::SimpleEntry>& outIdenticalEntries,
+ const APKMetaData& metadataA, const APKMetaData& metadataB) {
+ BuildIdenticalEntries(outIdenticalEntries, metadataA, metadataB);
+ }
+};
+
+TEST(DeployPatchGeneratorTest, IdenticalFileEntries) {
+ std::string apkPath = GetTestFile("rotating_cube-release.apk");
+ APKMetaData metadataA = PatchUtils::GetAPKMetaData(apkPath.c_str());
+ TestPatchGenerator generator;
+ std::vector<DeployPatchGenerator::SimpleEntry> entries;
+ generator.GatherIdenticalEntries(entries, metadataA, metadataA);
+ // Expect the entry count to match the number of entries in the metadata.
+ const uint32_t identicalCount = entries.size();
+ const uint32_t entriesCount = metadataA.entries_size();
+ EXPECT_EQ(identicalCount, entriesCount);
+}
+
+TEST(DeployPatchGeneratorTest, NoDeviceMetadata) {
+ std::string apkPath = GetTestFile("rotating_cube-release.apk");
+ // Get size of our test apk.
+ long apkSize = 0;
+ {
+ unique_fd apkFile(adb_open(apkPath.c_str(), O_RDWR));
+ apkSize = adb_lseek(apkFile, 0L, SEEK_END);
+ }
+
+ // Create a patch that is 100% different.
+ TemporaryFile output;
+ DeployPatchGenerator generator(true);
+ generator.CreatePatch(apkPath.c_str(), "", output.fd);
+
+ // Expect a patch file that has a size at least the size of our initial APK.
+ long patchSize = adb_lseek(output.fd, 0L, SEEK_END);
+ EXPECT_GT(patchSize, apkSize);
+}
\ No newline at end of file
diff --git a/adb/fastdeploy/deploypatchgenerator/manifest.txt b/adb/fastdeploy/deploypatchgenerator/manifest.txt
deleted file mode 100644
index 5c00505..0000000
--- a/adb/fastdeploy/deploypatchgenerator/manifest.txt
+++ /dev/null
@@ -1 +0,0 @@
-Main-Class: com.android.fastdeploy.DeployPatchGenerator
diff --git a/adb/fastdeploy/deploypatchgenerator/patch_utils.cpp b/adb/fastdeploy/deploypatchgenerator/patch_utils.cpp
new file mode 100644
index 0000000..f11ddd1
--- /dev/null
+++ b/adb/fastdeploy/deploypatchgenerator/patch_utils.cpp
@@ -0,0 +1,87 @@
+/*
+ * 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 "patch_utils.h"
+
+#include <androidfw/ZipFileRO.h>
+#include <stdio.h>
+
+#include "adb_io.h"
+#include "android-base/endian.h"
+#include "sysdeps.h"
+
+using namespace com::android;
+using namespace com::android::fastdeploy;
+using namespace android::base;
+
+static constexpr char kSignature[] = "FASTDEPLOY";
+
+APKMetaData PatchUtils::GetAPKMetaData(const char* apkPath) {
+ APKMetaData apkMetaData;
+#undef open
+ std::unique_ptr<android::ZipFileRO> zipFile(android::ZipFileRO::open(apkPath));
+#define open ___xxx_unix_open
+ if (zipFile == nullptr) {
+ printf("Could not open %s", apkPath);
+ exit(1);
+ }
+ void* cookie;
+ if (zipFile->startIteration(&cookie)) {
+ android::ZipEntryRO entry;
+ while ((entry = zipFile->nextEntry(cookie)) != NULL) {
+ char fileName[256];
+ // Make sure we have a file name.
+ // TODO: Handle filenames longer than 256.
+ if (zipFile->getEntryFileName(entry, fileName, sizeof(fileName))) {
+ continue;
+ }
+
+ uint32_t uncompressedSize, compressedSize, crc32;
+ int64_t dataOffset;
+ zipFile->getEntryInfo(entry, nullptr, &uncompressedSize, &compressedSize, &dataOffset,
+ nullptr, &crc32);
+ APKEntry* apkEntry = apkMetaData.add_entries();
+ apkEntry->set_crc32(crc32);
+ apkEntry->set_filename(fileName);
+ apkEntry->set_compressedsize(compressedSize);
+ apkEntry->set_uncompressedsize(uncompressedSize);
+ apkEntry->set_dataoffset(dataOffset);
+ }
+ }
+ return apkMetaData;
+}
+
+void PatchUtils::WriteSignature(borrowed_fd output) {
+ WriteFdExactly(output, kSignature, sizeof(kSignature) - 1);
+}
+
+void PatchUtils::WriteLong(int64_t value, borrowed_fd output) {
+ int64_t toLittleEndian = htole64(value);
+ WriteFdExactly(output, &toLittleEndian, sizeof(int64_t));
+}
+
+void PatchUtils::Pipe(borrowed_fd input, borrowed_fd output, size_t amount) {
+ constexpr static int BUFFER_SIZE = 128 * 1024;
+ char buffer[BUFFER_SIZE];
+ size_t transferAmount = 0;
+ while (transferAmount != amount) {
+ long chunkAmount =
+ amount - transferAmount > BUFFER_SIZE ? BUFFER_SIZE : amount - transferAmount;
+ long readAmount = adb_read(input, buffer, chunkAmount);
+ WriteFdExactly(output, buffer, readAmount);
+ transferAmount += readAmount;
+ }
+}
\ No newline at end of file
diff --git a/adb/fastdeploy/deploypatchgenerator/patch_utils.h b/adb/fastdeploy/deploypatchgenerator/patch_utils.h
new file mode 100644
index 0000000..0ebfe8f
--- /dev/null
+++ b/adb/fastdeploy/deploypatchgenerator/patch_utils.h
@@ -0,0 +1,46 @@
+/*
+ * 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 "adb_unique_fd.h"
+#include "fastdeploy/proto/ApkEntry.pb.h"
+
+/**
+ * Helper class that mirrors the PatchUtils from deploy agent.
+ */
+class PatchUtils {
+ public:
+ /**
+ * This function takes a local APK file and builds the APKMetaData required by the patching
+ * algorithm. The if this function has an error a string is printed to the terminal and exit(1)
+ * is called.
+ */
+ static com::android::fastdeploy::APKMetaData GetAPKMetaData(const char* file);
+ /**
+ * Writes a fixed signature string to the header of the patch.
+ */
+ static void WriteSignature(android::base::borrowed_fd output);
+ /**
+ * Writes an int64 to the |output| reversing the bytes.
+ */
+ static void WriteLong(int64_t value, android::base::borrowed_fd output);
+ /**
+ * Copy |amount| of data from |input| to |output|.
+ */
+ static void Pipe(android::base::borrowed_fd input, android::base::borrowed_fd output,
+ size_t amount);
+};
\ No newline at end of file
diff --git a/adb/fastdeploy/deploypatchgenerator/patch_utils_test.cpp b/adb/fastdeploy/deploypatchgenerator/patch_utils_test.cpp
new file mode 100644
index 0000000..a7eeebf
--- /dev/null
+++ b/adb/fastdeploy/deploypatchgenerator/patch_utils_test.cpp
@@ -0,0 +1,96 @@
+/*
+ * 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 "patch_utils.h"
+
+#include <android-base/file.h>
+#include <gtest/gtest.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sstream>
+#include <string>
+
+#include "adb_io.h"
+#include "sysdeps.h"
+
+using namespace com::android::fastdeploy;
+
+static std::string GetTestFile(const std::string& name) {
+ return "fastdeploy/testdata/" + name;
+}
+
+bool FileMatchesContent(android::base::borrowed_fd input, const char* contents,
+ ssize_t contentsSize) {
+ adb_lseek(input, 0, SEEK_SET);
+ // Use a temp buffer larger than any test contents.
+ constexpr int BUFFER_SIZE = 2048;
+ char buffer[BUFFER_SIZE];
+ bool result = true;
+ // Validate size of files is equal.
+ ssize_t readAmount = adb_read(input, buffer, BUFFER_SIZE);
+ EXPECT_EQ(readAmount, contentsSize);
+ result = memcmp(buffer, contents, readAmount) == 0;
+ for (int i = 0; i < readAmount; i++) {
+ printf("%x", buffer[i]);
+ }
+ printf(" == ");
+ for (int i = 0; i < contentsSize; i++) {
+ printf("%x", contents[i]);
+ }
+ printf("\n");
+
+ return result;
+}
+
+TEST(PatchUtilsTest, SwapLongWrites) {
+ TemporaryFile output;
+ PatchUtils::WriteLong(0x0011223344556677, output.fd);
+ adb_lseek(output.fd, 0, SEEK_SET);
+ const char expected[] = {0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00};
+ EXPECT_TRUE(FileMatchesContent(output.fd, expected, 8));
+}
+
+TEST(PatchUtilsTest, PipeWritesAmountToOutput) {
+ std::string expected("Some Data");
+ TemporaryFile input;
+ TemporaryFile output;
+ // Populate input file.
+ WriteFdExactly(input.fd, expected);
+ adb_lseek(input.fd, 0, SEEK_SET);
+ // Open input file for read, and output file for write.
+ PatchUtils::Pipe(input.fd, output.fd, expected.size());
+ // Validate pipe worked
+ EXPECT_TRUE(FileMatchesContent(output.fd, expected.c_str(), expected.size()));
+}
+
+TEST(PatchUtilsTest, SignatureConstMatches) {
+ std::string apkFile = GetTestFile("rotating_cube-release.apk");
+ TemporaryFile output;
+ PatchUtils::WriteSignature(output.fd);
+ std::string contents("FASTDEPLOY");
+ EXPECT_TRUE(FileMatchesContent(output.fd, contents.c_str(), contents.size()));
+}
+
+TEST(PatchUtilsTest, GatherMetadata) {
+ std::string apkFile = GetTestFile("rotating_cube-release.apk");
+ APKMetaData metadata = PatchUtils::GetAPKMetaData(apkFile.c_str());
+ std::string expectedMetadata;
+ android::base::ReadFileToString(GetTestFile("rotating_cube-metadata-release.data"),
+ &expectedMetadata);
+ std::string actualMetadata;
+ metadata.SerializeToString(&actualMetadata);
+ EXPECT_EQ(expectedMetadata, actualMetadata);
+}
\ No newline at end of file
diff --git a/adb/fastdeploy/deploypatchgenerator/src/com/android/fastdeploy/DeployPatchGenerator.java b/adb/fastdeploy/deploypatchgenerator/src/com/android/fastdeploy/DeployPatchGenerator.java
deleted file mode 100644
index 24b2eab..0000000
--- a/adb/fastdeploy/deploypatchgenerator/src/com/android/fastdeploy/DeployPatchGenerator.java
+++ /dev/null
@@ -1,208 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.fastdeploy;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.lang.StringBuilder;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
-import java.util.ArrayList;
-
-import java.nio.charset.StandardCharsets;
-import static java.nio.charset.StandardCharsets.UTF_8;
-
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.List;
-import java.util.AbstractMap.SimpleEntry;
-
-import com.android.fastdeploy.APKMetaData;
-import com.android.fastdeploy.APKEntry;
-
-public final class DeployPatchGenerator {
- private static final int BUFFER_SIZE = 128 * 1024;
-
- public static void main(String[] args) {
- try {
- if (args.length < 2) {
- showUsage(0);
- }
-
- boolean verbose = false;
- if (args.length > 2) {
- String verboseFlag = args[2];
- if (verboseFlag.compareTo("--verbose") == 0) {
- verbose = true;
- }
- }
-
- StringBuilder sb = null;
- String apkPath = args[0];
- String deviceMetadataPath = args[1];
- File hostFile = new File(apkPath);
-
- List<APKEntry> deviceZipEntries = getMetadataFromFile(deviceMetadataPath);
- System.err.println("Device Entries (" + deviceZipEntries.size() + ")");
- if (verbose) {
- sb = new StringBuilder();
- for (APKEntry entry : deviceZipEntries) {
- APKEntryToString(entry, sb);
- }
- System.err.println(sb.toString());
- }
-
- List<APKEntry> hostFileEntries = PatchUtils.getAPKMetaData(hostFile).getEntriesList();
- System.err.println("Host Entries (" + hostFileEntries.size() + ")");
- if (verbose) {
- sb = new StringBuilder();
- for (APKEntry entry : hostFileEntries) {
- APKEntryToString(entry, sb);
- }
- System.err.println(sb.toString());
- }
-
- List<SimpleEntry<APKEntry, APKEntry>> identicalContentsEntrySet =
- getIdenticalContents(deviceZipEntries, hostFileEntries);
- reportIdenticalContents(identicalContentsEntrySet, hostFile);
-
- if (verbose) {
- sb = new StringBuilder();
- for (SimpleEntry<APKEntry, APKEntry> identicalEntry : identicalContentsEntrySet) {
- APKEntry entry = identicalEntry.getValue();
- APKEntryToString(entry, sb);
- }
- System.err.println("Identical Entries (" + identicalContentsEntrySet.size() + ")");
- System.err.println(sb.toString());
- }
-
- createPatch(identicalContentsEntrySet, hostFile, System.out);
- } catch (Exception e) {
- System.err.println("Error: " + e);
- e.printStackTrace();
- System.exit(2);
- }
- System.exit(0);
- }
-
- private static void showUsage(int exitCode) {
- System.err.println("usage: deploypatchgenerator <apkpath> <deviceapkmetadata> [--verbose]");
- System.err.println("");
- System.exit(exitCode);
- }
-
- private static void APKEntryToString(APKEntry entry, StringBuilder outputString) {
- outputString.append(String.format("Filename: %s\n", entry.getFileName()));
- outputString.append(String.format("CRC32: 0x%08X\n", entry.getCrc32()));
- outputString.append(String.format("Data Offset: %d\n", entry.getDataOffset()));
- outputString.append(String.format("Compressed Size: %d\n", entry.getCompressedSize()));
- outputString.append(String.format("Uncompressed Size: %d\n", entry.getUncompressedSize()));
- }
-
- private static List<APKEntry> getMetadataFromFile(String deviceMetadataPath) throws IOException {
- InputStream is = new FileInputStream(new File(deviceMetadataPath));
- APKMetaData apkMetaData = APKMetaData.parseDelimitedFrom(is);
- return apkMetaData.getEntriesList();
- }
-
- private static List<SimpleEntry<APKEntry, APKEntry>> getIdenticalContents(
- List<APKEntry> deviceZipEntries, List<APKEntry> hostZipEntries) throws IOException {
- List<SimpleEntry<APKEntry, APKEntry>> identicalContents =
- new ArrayList<SimpleEntry<APKEntry, APKEntry>>();
-
- for (APKEntry deviceZipEntry : deviceZipEntries) {
- for (APKEntry hostZipEntry : hostZipEntries) {
- if (deviceZipEntry.getCrc32() == hostZipEntry.getCrc32() &&
- deviceZipEntry.getFileName().equals(hostZipEntry.getFileName())) {
- identicalContents.add(new SimpleEntry(deviceZipEntry, hostZipEntry));
- }
- }
- }
-
- Collections.sort(identicalContents, new Comparator<SimpleEntry<APKEntry, APKEntry>>() {
- @Override
- public int compare(
- SimpleEntry<APKEntry, APKEntry> p1, SimpleEntry<APKEntry, APKEntry> p2) {
- return Long.compare(p1.getValue().getDataOffset(), p2.getValue().getDataOffset());
- }
- });
-
- return identicalContents;
- }
-
- private static void reportIdenticalContents(
- List<SimpleEntry<APKEntry, APKEntry>> identicalContentsEntrySet, File hostFile)
- throws IOException {
- long totalEqualBytes = 0;
- int totalEqualFiles = 0;
- for (SimpleEntry<APKEntry, APKEntry> entries : identicalContentsEntrySet) {
- APKEntry hostAPKEntry = entries.getValue();
- totalEqualBytes += hostAPKEntry.getCompressedSize();
- totalEqualFiles++;
- }
-
- float savingPercent = (float) (totalEqualBytes * 100) / hostFile.length();
-
- System.err.println("Detected " + totalEqualFiles + " equal APK entries");
- System.err.println(totalEqualBytes + " bytes are equal out of " + hostFile.length() + " ("
- + savingPercent + "%)");
- }
-
- static void createPatch(List<SimpleEntry<APKEntry, APKEntry>> zipEntrySimpleEntrys,
- File hostFile, OutputStream patchStream) throws IOException, PatchFormatException {
- FileInputStream hostFileInputStream = new FileInputStream(hostFile);
-
- patchStream.write(PatchUtils.SIGNATURE.getBytes(StandardCharsets.US_ASCII));
- PatchUtils.writeFormattedLong(hostFile.length(), patchStream);
-
- byte[] buffer = new byte[BUFFER_SIZE];
- long totalBytesWritten = 0;
- Iterator<SimpleEntry<APKEntry, APKEntry>> entrySimpleEntryIterator =
- zipEntrySimpleEntrys.iterator();
- while (entrySimpleEntryIterator.hasNext()) {
- SimpleEntry<APKEntry, APKEntry> entrySimpleEntry = entrySimpleEntryIterator.next();
- APKEntry deviceAPKEntry = entrySimpleEntry.getKey();
- APKEntry hostAPKEntry = entrySimpleEntry.getValue();
-
- long newDataLen = hostAPKEntry.getDataOffset() - totalBytesWritten;
- long oldDataOffset = deviceAPKEntry.getDataOffset();
- long oldDataLen = deviceAPKEntry.getCompressedSize();
-
- PatchUtils.writeFormattedLong(newDataLen, patchStream);
- PatchUtils.pipe(hostFileInputStream, patchStream, buffer, newDataLen);
- PatchUtils.writeFormattedLong(oldDataOffset, patchStream);
- PatchUtils.writeFormattedLong(oldDataLen, patchStream);
-
- long skip = hostFileInputStream.skip(oldDataLen);
- if (skip != oldDataLen) {
- throw new PatchFormatException("skip error: attempted to skip " + oldDataLen
- + " bytes but return code was " + skip);
- }
- totalBytesWritten += oldDataLen + newDataLen;
- }
- long remainderLen = hostFile.length() - totalBytesWritten;
- PatchUtils.writeFormattedLong(remainderLen, patchStream);
- PatchUtils.pipe(hostFileInputStream, patchStream, buffer, remainderLen);
- PatchUtils.writeFormattedLong(0, patchStream);
- PatchUtils.writeFormattedLong(0, patchStream);
- patchStream.flush();
- }
-}
diff --git a/adb/fastdeploy/testdata/rotating_cube-metadata-release.data b/adb/fastdeploy/testdata/rotating_cube-metadata-release.data
new file mode 100644
index 0000000..0671bf3
--- /dev/null
+++ b/adb/fastdeploy/testdata/rotating_cube-metadata-release.data
@@ -0,0 +1,6 @@
+
+#ÇÏ«META-INF/MANIFEST.MFÇ Q(W
+#AndroidManifest.xml1 ä(è
+6¦µ>#lib/armeabi-v7a/libvulkan_sample.so ÀÒQ(²ì
+ ãresources.arscôàQ ´(´
+ÂÉclasses.dexÁ ÿ(ô
diff --git a/adb/fastdeploy/testdata/rotating_cube-release.apk b/adb/fastdeploy/testdata/rotating_cube-release.apk
new file mode 100644
index 0000000..d47e0ea
--- /dev/null
+++ b/adb/fastdeploy/testdata/rotating_cube-release.apk
Binary files differ
diff --git a/adb/socket_spec.cpp b/adb/socket_spec.cpp
index 1333724..98468b5 100644
--- a/adb/socket_spec.cpp
+++ b/adb/socket_spec.cpp
@@ -93,7 +93,7 @@
}
} else {
std::string addr(spec.substr(4));
- port_value = -1;
+ port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
// FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
// on an address that isn't 'localhost' is unsupported.
diff --git a/adb/socket_spec_test.cpp b/adb/socket_spec_test.cpp
index f5ec0f1..3a2f60c 100644
--- a/adb/socket_spec_test.cpp
+++ b/adb/socket_spec_test.cpp
@@ -20,38 +20,71 @@
#include <gtest/gtest.h>
-TEST(socket_spec, parse_tcp_socket_spec) {
+TEST(socket_spec, parse_tcp_socket_spec_just_port) {
std::string hostname, error, serial;
int port;
EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &serial, &error));
EXPECT_EQ("", hostname);
EXPECT_EQ(5037, port);
EXPECT_EQ("", serial);
+}
- // Bad ports:
+TEST(socket_spec, parse_tcp_socket_spec_bad_ports) {
+ std::string hostname, error, serial;
+ int port;
EXPECT_FALSE(parse_tcp_socket_spec("tcp:", &hostname, &port, &serial, &error));
EXPECT_FALSE(parse_tcp_socket_spec("tcp:-1", &hostname, &port, &serial, &error));
EXPECT_FALSE(parse_tcp_socket_spec("tcp:65536", &hostname, &port, &serial, &error));
+}
+TEST(socket_spec, parse_tcp_socket_spec_host_and_port) {
+ std::string hostname, error, serial;
+ int port;
EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &serial, &error));
EXPECT_EQ("localhost", hostname);
EXPECT_EQ(1234, port);
EXPECT_EQ("localhost:1234", serial);
+}
- EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost", &hostname, &port, &serial, &error));
+TEST(socket_spec, parse_tcp_socket_spec_host_no_port) {
+ std::string hostname, error, serial;
+ int port;
+ EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost", &hostname, &port, &serial, &error));
+ EXPECT_EQ("localhost", hostname);
+ EXPECT_EQ(5555, port);
+ EXPECT_EQ("localhost:5555", serial);
+}
+
+TEST(socket_spec, parse_tcp_socket_spec_host_bad_ports) {
+ std::string hostname, error, serial;
+ int port;
EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:", &hostname, &port, &serial, &error));
EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:-1", &hostname, &port, &serial, &error));
EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:65536", &hostname, &port, &serial, &error));
+}
- // IPv6:
+TEST(socket_spec, parse_tcp_socket_spec_ipv6_and_port) {
+ std::string hostname, error, serial;
+ int port;
EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &serial, &error));
EXPECT_EQ("::1", hostname);
EXPECT_EQ(1234, port);
EXPECT_EQ("[::1]:1234", serial);
+}
+TEST(socket_spec, parse_tcp_socket_spec_ipv6_no_port) {
+ std::string hostname, error, serial;
+ int port;
+ EXPECT_TRUE(parse_tcp_socket_spec("tcp:::1", &hostname, &port, &serial, &error));
+ EXPECT_EQ("::1", hostname);
+ EXPECT_EQ(5555, port);
+ EXPECT_EQ("[::1]:5555", serial);
+}
+
+TEST(socket_spec, parse_tcp_socket_spec_ipv6_bad_ports) {
+ std::string hostname, error, serial;
+ int port;
EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]", &hostname, &port, &serial, &error));
EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:", &hostname, &port, &serial, &error));
EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:-1", &hostname, &port, &serial, &error));
- EXPECT_FALSE(parse_tcp_socket_spec("tcp:::1", &hostname, &port, &serial, &error));
- EXPECT_FALSE(parse_tcp_socket_spec("tcp:::1:1234", &hostname, &port, &serial, &error));
}
diff --git a/adb/sysdeps_test.cpp b/adb/sysdeps_test.cpp
index 79cebe6..0f4b39c 100644
--- a/adb/sysdeps_test.cpp
+++ b/adb/sysdeps_test.cpp
@@ -25,6 +25,21 @@
#include "sysdeps.h"
#include "sysdeps/chrono.h"
+#if defined(_WIN32)
+#include <windows.h>
+static bool IsWine() {
+ HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
+ if (!ntdll) {
+ return false;
+ }
+ return GetProcAddress(ntdll, "wine_get_version") != nullptr;
+}
+#else
+static bool IsWine() {
+ return false;
+}
+#endif
+
TEST(sysdeps_socketpair, smoke) {
int fds[2];
ASSERT_EQ(0, adb_socketpair(fds)) << strerror(errno);
@@ -182,8 +197,10 @@
EXPECT_EQ(1, adb_poll(&pfd, 1, 100));
- // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP.
- EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP);
+ if (!IsWine()) {
+ // Linux returns POLLIN | POLLHUP, Windows returns just POLLHUP.
+ EXPECT_EQ(POLLHUP, pfd.revents & POLLHUP);
+ }
}
TEST_F(sysdeps_poll, fd_count) {
diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp
index f86cd03..6372b3d 100644
--- a/adb/sysdeps_win32.cpp
+++ b/adb/sysdeps_win32.cpp
@@ -610,15 +610,6 @@
static int _fh_socket_close(FH f) {
if (f->fh_socket != INVALID_SOCKET) {
- /* gently tell any peer that we're closing the socket */
- if (shutdown(f->fh_socket, SD_BOTH) == SOCKET_ERROR) {
- // If the socket is not connected, this returns an error. We want to
- // minimize logging spam, so don't log these errors for now.
-#if 0
- D("socket shutdown failed: %s",
- android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
-#endif
- }
if (closesocket(f->fh_socket) == SOCKET_ERROR) {
// Don't set errno here, since adb_close will ignore it.
const DWORD err = WSAGetLastError();
diff --git a/adb/transport.cpp b/adb/transport.cpp
index 8bc925f..3d1d620 100644
--- a/adb/transport.cpp
+++ b/adb/transport.cpp
@@ -542,9 +542,7 @@
// for the first time, even if no update occurred.
if (tracker->update_needed) {
tracker->update_needed = false;
-
- std::string transports = list_transports(tracker->long_output);
- device_tracker_send(tracker, transports);
+ device_tracker_send(tracker, list_transports(tracker->long_output));
}
}
@@ -587,13 +585,11 @@
update_transport_status();
// Notify `adb track-devices` clients.
- std::string transports = list_transports(false);
-
device_tracker* tracker = device_tracker_list;
while (tracker != nullptr) {
device_tracker* next = tracker->next;
// This may destroy the tracker if the connection is closed.
- device_tracker_send(tracker, transports);
+ device_tracker_send(tracker, list_transports(tracker->long_output));
tracker = next;
}
}
diff --git a/base/include/android-base/unique_fd.h b/base/include/android-base/unique_fd.h
index a39245b..6e11b4e 100644
--- a/base/include/android-base/unique_fd.h
+++ b/base/include/android-base/unique_fd.h
@@ -92,6 +92,8 @@
explicit unique_fd_impl(int fd) { reset(fd); }
~unique_fd_impl() { reset(); }
+ unique_fd_impl(const unique_fd_impl&) = delete;
+ void operator=(const unique_fd_impl&) = delete;
unique_fd_impl(unique_fd_impl&& other) noexcept { reset(other.release()); }
unique_fd_impl& operator=(unique_fd_impl&& s) noexcept {
int fd = s.fd_;
@@ -118,6 +120,8 @@
// Catch bogus error checks (i.e.: "!fd" instead of "fd != -1").
bool operator!() const = delete;
+ bool ok() const { return get() != -1; }
+
int release() __attribute__((warn_unused_result)) {
tag(fd_, this, nullptr);
int ret = fd_;
@@ -167,9 +171,6 @@
static auto close(int fd, void*) -> decltype(T::Close(fd), void()) {
T::Close(fd);
}
-
- unique_fd_impl(const unique_fd_impl&);
- void operator=(const unique_fd_impl&);
};
using unique_fd = unique_fd_impl<DefaultCloser>;
diff --git a/debuggerd/client/debuggerd_client.cpp b/debuggerd/client/debuggerd_client.cpp
index 60eb241..1a5b435 100644
--- a/debuggerd/client/debuggerd_client.cpp
+++ b/debuggerd/client/debuggerd_client.cpp
@@ -22,9 +22,11 @@
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <time.h>
#include <unistd.h>
#include <chrono>
+#include <iomanip>
#include <android-base/cmsg.h>
#include <android-base/file.h>
@@ -42,8 +44,10 @@
using namespace std::chrono_literals;
+using android::base::ReadFileToString;
using android::base::SendFileDescriptors;
using android::base::unique_fd;
+using android::base::WriteStringToFd;
static bool send_signal(pid_t pid, const DebuggerdDumpType dump_type) {
const int signal = (dump_type == kDebuggerdJavaBacktrace) ? SIGQUIT : DEBUGGER_SIGNAL;
@@ -65,6 +69,76 @@
tv->tv_usec = static_cast<long>(microseconds.count());
}
+static void get_wchan_header(pid_t pid, std::stringstream& buffer) {
+ struct tm now;
+ time_t t = time(nullptr);
+ localtime_r(&t, &now);
+ char timestamp[32];
+ strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", &now);
+ std::string time_now(timestamp);
+
+ std::string path = "/proc/" + std::to_string(pid) + "/cmdline";
+
+ char proc_name_buf[1024];
+ const char* proc_name = nullptr;
+ std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(path.c_str(), "r"), &fclose);
+
+ if (fp) {
+ proc_name = fgets(proc_name_buf, sizeof(proc_name_buf), fp.get());
+ }
+
+ if (!proc_name) {
+ proc_name = "<unknown>";
+ }
+
+ buffer << "\n----- Waiting Channels: pid " << pid << " at " << time_now << " -----\n"
+ << "Cmd line: " << proc_name << "\n";
+}
+
+static void get_wchan_footer(pid_t pid, std::stringstream& buffer) {
+ buffer << "----- end " << std::to_string(pid) << " -----\n";
+}
+
+/**
+ * Returns the wchan data for each thread in the process,
+ * or empty string if unable to obtain any data.
+ */
+static std::string get_wchan_data(pid_t pid) {
+ std::stringstream buffer;
+ std::vector<pid_t> tids;
+
+ if (!android::procinfo::GetProcessTids(pid, &tids)) {
+ LOG(WARNING) << "libdebuggerd_client: Failed to get process tids";
+ return buffer.str();
+ }
+
+ std::stringstream data;
+ for (int tid : tids) {
+ std::string path = "/proc/" + std::to_string(pid) + "/task/" + std::to_string(tid) + "/wchan";
+ std::string wchan_str;
+ if (!ReadFileToString(path, &wchan_str, true)) {
+ PLOG(WARNING) << "libdebuggerd_client: Failed to read \"" << path << "\"";
+ continue;
+ }
+ data << "sysTid=" << std::left << std::setw(10) << tid << wchan_str << "\n";
+ }
+
+ if (std::string str = data.str(); !str.empty()) {
+ get_wchan_header(pid, buffer);
+ buffer << "\n" << str << "\n";
+ get_wchan_footer(pid, buffer);
+ buffer << "\n";
+ }
+
+ return buffer.str();
+}
+
+static void dump_wchan_data(const std::string& data, int fd, pid_t pid) {
+ if (!WriteStringToFd(data, fd)) {
+ LOG(WARNING) << "libdebuggerd_client: Failed to dump wchan data for pid: " << pid;
+ }
+}
+
bool debuggerd_trigger_dump(pid_t tid, DebuggerdDumpType dump_type, unsigned int timeout_ms,
unique_fd output_fd) {
pid_t pid = tid;
@@ -261,6 +335,17 @@
if (copy == -1) {
return -1;
}
+
+ // debuggerd_trigger_dump results in every thread in the process being interrupted
+ // by a signal, so we need to fetch the wchan data before calling that.
+ std::string wchan_data = get_wchan_data(tid);
+
int timeout_ms = timeout_secs > 0 ? timeout_secs * 1000 : 0;
- return debuggerd_trigger_dump(tid, dump_type, timeout_ms, std::move(copy)) ? 0 : -1;
+ int ret = debuggerd_trigger_dump(tid, dump_type, timeout_ms, std::move(copy)) ? 0 : -1;
+
+ // Dump wchan data, since only privileged processes (CAP_SYS_ADMIN) can read
+ // kernel stack traces (/proc/*/stack).
+ dump_wchan_data(wchan_data, fd, tid);
+
+ return ret;
}
diff --git a/debuggerd/debuggerd_test.cpp b/debuggerd/debuggerd_test.cpp
index 1f0e420..fbc8b97 100644
--- a/debuggerd/debuggerd_test.cpp
+++ b/debuggerd/debuggerd_test.cpp
@@ -1099,3 +1099,30 @@
// This should be good enough, though...
ASSERT_LT(diff, 10) << "too many new tombstones; is something crashing in the background?";
}
+
+static __attribute__((__noinline__)) void overflow_stack(void* p) {
+ void* buf[1];
+ buf[0] = p;
+ static volatile void* global = buf;
+ if (global) {
+ global = buf;
+ overflow_stack(&buf);
+ }
+}
+
+TEST_F(CrasherTest, stack_overflow) {
+ int intercept_result;
+ unique_fd output_fd;
+ StartProcess([]() { overflow_stack(nullptr); });
+
+ StartIntercept(&output_fd);
+ FinishCrasher();
+ AssertDeath(SIGSEGV);
+ FinishIntercept(&intercept_result);
+
+ ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
+
+ std::string result;
+ ConsumeFd(std::move(output_fd), &result);
+ ASSERT_MATCH(result, R"(Cause: stack pointer[^\n]*stack overflow.\n)");
+}
diff --git a/debuggerd/libdebuggerd/tombstone.cpp b/debuggerd/libdebuggerd/tombstone.cpp
index d246722..da2ba58 100644
--- a/debuggerd/libdebuggerd/tombstone.cpp
+++ b/debuggerd/libdebuggerd/tombstone.cpp
@@ -86,7 +86,39 @@
_LOG(log, logtype::HEADER, "Timestamp: %s\n", buf);
}
-static void dump_probable_cause(log_t* log, const siginfo_t* si, unwindstack::Maps* maps) {
+static std::string get_stack_overflow_cause(uint64_t fault_addr, uint64_t sp,
+ unwindstack::Maps* maps) {
+ static constexpr uint64_t kMaxDifferenceBytes = 256;
+ uint64_t difference;
+ if (sp >= fault_addr) {
+ difference = sp - fault_addr;
+ } else {
+ difference = fault_addr - sp;
+ }
+ if (difference <= kMaxDifferenceBytes) {
+ // The faulting address is close to the current sp, check if the sp
+ // indicates a stack overflow.
+ // On arm, the sp does not get updated when the instruction faults.
+ // In this case, the sp will still be in a valid map, which is the
+ // last case below.
+ // On aarch64, the sp does get updated when the instruction faults.
+ // In this case, the sp will be in either an invalid map if triggered
+ // on the main thread, or in a guard map if in another thread, which
+ // will be the first case or second case from below.
+ unwindstack::MapInfo* map_info = maps->Find(sp);
+ if (map_info == nullptr) {
+ return "stack pointer is in a non-existent map; likely due to stack overflow.";
+ } else if ((map_info->flags & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) {
+ return "stack pointer is not in a rw map; likely due to stack overflow.";
+ } else if ((sp - map_info->start) <= kMaxDifferenceBytes) {
+ return "stack pointer is close to top of stack; likely stack overflow.";
+ }
+ }
+ return "";
+}
+
+static void dump_probable_cause(log_t* log, const siginfo_t* si, unwindstack::Maps* maps,
+ unwindstack::Regs* regs) {
std::string cause;
if (si->si_signo == SIGSEGV && si->si_code == SEGV_MAPERR) {
if (si->si_addr < reinterpret_cast<void*>(4096)) {
@@ -101,11 +133,16 @@
cause = "call to kuser_memory_barrier";
} else if (si->si_addr == reinterpret_cast<void*>(0xffff0f60)) {
cause = "call to kuser_cmpxchg64";
+ } else {
+ cause = get_stack_overflow_cause(reinterpret_cast<uint64_t>(si->si_addr), regs->sp(), maps);
}
} else if (si->si_signo == SIGSEGV && si->si_code == SEGV_ACCERR) {
- unwindstack::MapInfo* map_info = maps->Find(reinterpret_cast<uint64_t>(si->si_addr));
+ uint64_t fault_addr = reinterpret_cast<uint64_t>(si->si_addr);
+ unwindstack::MapInfo* map_info = maps->Find(fault_addr);
if (map_info != nullptr && map_info->flags == PROT_EXEC) {
cause = "execute-only (no-read) memory access error; likely due to data in .text.";
+ } else {
+ cause = get_stack_overflow_cause(fault_addr, regs->sp(), maps);
}
} else if (si->si_signo == SIGSYS && si->si_code == SYS_SECCOMP) {
cause = StringPrintf("seccomp prevented call to disallowed %s system call %d", ABI_STRING,
@@ -447,7 +484,7 @@
if (thread_info.siginfo) {
dump_signal_info(log, thread_info, unwinder->GetProcessMemory().get());
- dump_probable_cause(log, thread_info.siginfo, unwinder->GetMaps());
+ dump_probable_cause(log, thread_info.siginfo, unwinder->GetMaps(), thread_info.registers.get());
}
if (primary_thread) {
diff --git a/demangle/.clang-format b/demangle/.clang-format
deleted file mode 120000
index fd0645f..0000000
--- a/demangle/.clang-format
+++ /dev/null
@@ -1 +0,0 @@
-../.clang-format-2
\ No newline at end of file
diff --git a/demangle/Android.bp b/demangle/Android.bp
deleted file mode 100644
index fd79cf8..0000000
--- a/demangle/Android.bp
+++ /dev/null
@@ -1,87 +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.
-//
-
-cc_defaults {
- name: "libdemangle_defaults",
-
- host_supported: true,
-
- cflags: [
- "-Wall",
- "-Werror",
- "-Wextra",
- ],
-
- target: {
- linux_bionic: {
- enabled: true,
- },
- },
-}
-
-cc_library {
- name: "libdemangle",
- defaults: ["libdemangle_defaults"],
- vendor_available: true,
- recovery_available: true,
-
- srcs: [
- "Demangler.cpp",
- ],
-
- local_include_dirs: [
- "include",
- ],
-
- export_include_dirs: [
- "include",
- ],
-}
-
-cc_binary {
- name: "demangle",
- defaults: ["libdemangle_defaults"],
- srcs: ["demangle.cpp"],
- host_supported: true,
-
- shared_libs: ["libdemangle"],
-}
-
-//-------------------------------------------------------------------------
-// Unit Tests
-//-------------------------------------------------------------------------
-cc_test {
- name: "libdemangle_test",
- defaults: ["libdemangle_defaults"],
-
- srcs: [
- "DemangleTest.cpp",
- ],
-
- cflags: [
- "-O0",
- "-g",
- ],
-
- shared_libs: [
- "libdemangle",
- ],
-
- test_suites: ["device-tests"],
- required: [
- "libdemangle",
- ],
-}
diff --git a/demangle/Android.mk b/demangle/Android.mk
deleted file mode 100644
index d8082a9..0000000
--- a/demangle/Android.mk
+++ /dev/null
@@ -1,31 +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.
-#
-
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := demangle_fuzzer
-LOCAL_SRC_FILES := \
- Demangler.cpp \
- demangle_fuzzer.cpp \
-
-LOCAL_CFLAGS := \
- -Wall \
- -Werror \
- -Wextra \
-
-include $(BUILD_FUZZ_TEST)
diff --git a/demangle/DemangleTest.cpp b/demangle/DemangleTest.cpp
deleted file mode 100644
index 1787031..0000000
--- a/demangle/DemangleTest.cpp
+++ /dev/null
@@ -1,555 +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 <stdlib.h>
-
-#include <gtest/gtest.h>
-
-#include <demangle.h>
-
-#include "Demangler.h"
-
-TEST(DemangleTest, IllegalArgumentModifiers) {
- Demangler demangler;
-
- ASSERT_EQ("_Zpp4FUNKK", demangler.Parse("_Zpp4FUNKK"));
- ASSERT_EQ("_Zpp4FUNVV", demangler.Parse("_Zpp4FUNVV"));
-}
-
-TEST(DemangleTest, VoidArgument) {
- Demangler demangler;
-
- ASSERT_EQ("func()", demangler.Parse("_ZN4funcEv"));
- ASSERT_EQ("func(void&)", demangler.Parse("_ZN4funcERv"));
- ASSERT_EQ("func(void, void)", demangler.Parse("_ZN4funcEvv"));
- ASSERT_EQ("func(void*)", demangler.Parse("_ZN4funcEPv"));
- ASSERT_EQ("func(void const)", demangler.Parse("_ZN4funcEKv"));
- ASSERT_EQ("func(void volatile)", demangler.Parse("_ZN4funcEVv"));
-}
-
-TEST(DemangleTest, ArgumentModifiers) {
- Demangler demangler;
-
- ASSERT_EQ("func(char)", demangler.Parse("_ZN4funcEc"));
- ASSERT_EQ("func(char*)", demangler.Parse("_ZN4funcEPc"));
- ASSERT_EQ("func(char**)", demangler.Parse("_ZN4funcEPPc"));
- ASSERT_EQ("func(char***)", demangler.Parse("_ZN4funcEPPPc"));
- ASSERT_EQ("func(char&)", demangler.Parse("_ZN4funcERc"));
- ASSERT_EQ("func(char*&)", demangler.Parse("_ZN4funcERPc"));
- ASSERT_EQ("func(char&)", demangler.Parse("_ZN4funcERRc"));
- ASSERT_EQ("func(char*&*)", demangler.Parse("_ZN4funcEPRPc"));
- ASSERT_EQ("func(char**&)", demangler.Parse("_ZN4funcERRPPc"));
- ASSERT_EQ("func(char const)", demangler.Parse("_ZN4funcEKc"));
- ASSERT_EQ("func(char volatile)", demangler.Parse("_ZN4funcEVc"));
- ASSERT_EQ("func(char volatile const)", demangler.Parse("_ZN4funcEKVc"));
- ASSERT_EQ("func(char const volatile)", demangler.Parse("_ZN4funcEVKc"));
- ASSERT_EQ("func(char const* volatile&)", demangler.Parse("_ZN4funcERVPKc"));
- ASSERT_EQ("func(void, char, short)", demangler.Parse("_ZN4funcEvcs"));
- ASSERT_EQ("func(void*, char&, short&*)", demangler.Parse("_ZN4funcEPvRcPRs"));
-}
-
-TEST(DemangleTest, FunctionModifiers) {
- Demangler demangler;
-
- ASSERT_EQ("func() const", demangler.Parse("_ZNK4funcEv"));
- ASSERT_EQ("func() volatile", demangler.Parse("_ZNV4funcEv"));
- ASSERT_EQ("func() volatile const", demangler.Parse("_ZNKV4funcEv"));
- ASSERT_EQ("func() const volatile", demangler.Parse("_ZNVK4funcEv"));
-}
-
-TEST(DemangleTest, MultiplePartsInName) {
- Demangler demangler;
-
- ASSERT_EQ("one::two()", demangler.Parse("_ZN3one3twoEv"));
- ASSERT_EQ("one::two::three()", demangler.Parse("_ZN3one3two5threeEv"));
- ASSERT_EQ("one::two::three::four()", demangler.Parse("_ZN3one3two5three4fourEv"));
- ASSERT_EQ("one::two::three::four::five()", demangler.Parse("_ZN3one3two5three4four4fiveEv"));
- ASSERT_EQ("one(two::three::four::five)", demangler.Parse("_ZN3oneEN3two5three4four4fiveE"));
-}
-
-TEST(DemangleTest, AnonymousNamespace) {
- Demangler demangler;
-
- ASSERT_EQ("(anonymous namespace)::two()", demangler.Parse("_ZN12_GLOBAL__N_13twoEv"));
- ASSERT_EQ("one::two((anonymous namespace))", demangler.Parse("_ZN3one3twoE12_GLOBAL__N_1"));
-}
-
-TEST(DemangleTest, DestructorValues) {
- Demangler demangler;
-
- ASSERT_EQ("one::two::~two()", demangler.Parse("_ZN3one3twoD0Ev"));
- ASSERT_EQ("one::two::~two()", demangler.Parse("_ZN3one3twoD1Ev"));
- ASSERT_EQ("one::two::~two()", demangler.Parse("_ZN3one3twoD2Ev"));
- ASSERT_EQ("one::two::~two()", demangler.Parse("_ZN3one3twoD5Ev"));
- ASSERT_EQ("one::two::three::~three()", demangler.Parse("_ZN3one3two5threeD0Ev"));
-
- ASSERT_EQ("_ZN3one3twoD3Ev", demangler.Parse("_ZN3one3twoD3Ev"));
- ASSERT_EQ("_ZN3one3twoD4Ev", demangler.Parse("_ZN3one3twoD4Ev"));
- ASSERT_EQ("_ZN3one3twoD6Ev", demangler.Parse("_ZN3one3twoD6Ev"));
- ASSERT_EQ("_ZN3one3twoD7Ev", demangler.Parse("_ZN3one3twoD7Ev"));
- ASSERT_EQ("_ZN3one3twoD8Ev", demangler.Parse("_ZN3one3twoD8Ev"));
- ASSERT_EQ("_ZN3one3twoD9Ev", demangler.Parse("_ZN3one3twoD9Ev"));
-
- ASSERT_EQ("one::two<three::four>::~two()", demangler.Parse("_ZN3one3twoIN5three4fourEED2Ev"));
-}
-
-TEST(DemangleTest, ConstructorValues) {
- Demangler demangler;
-
- ASSERT_EQ("one::two::two()", demangler.Parse("_ZN3one3twoC1Ev"));
- ASSERT_EQ("one::two::two()", demangler.Parse("_ZN3one3twoC2Ev"));
- ASSERT_EQ("one::two::two()", demangler.Parse("_ZN3one3twoC3Ev"));
- ASSERT_EQ("one::two::two()", demangler.Parse("_ZN3one3twoC5Ev"));
- ASSERT_EQ("one::two::three::three()", demangler.Parse("_ZN3one3two5threeC1Ev"));
-
- ASSERT_EQ("_ZN3one3twoC0Ev", demangler.Parse("_ZN3one3twoC0Ev"));
- ASSERT_EQ("_ZN3one3twoC4Ev", demangler.Parse("_ZN3one3twoC4Ev"));
- ASSERT_EQ("_ZN3one3twoC6Ev", demangler.Parse("_ZN3one3twoC6Ev"));
- ASSERT_EQ("_ZN3one3twoC7Ev", demangler.Parse("_ZN3one3twoC7Ev"));
- ASSERT_EQ("_ZN3one3twoC8Ev", demangler.Parse("_ZN3one3twoC8Ev"));
- ASSERT_EQ("_ZN3one3twoC9Ev", demangler.Parse("_ZN3one3twoC9Ev"));
-
- ASSERT_EQ("one::two<three::four>::two()", demangler.Parse("_ZN3one3twoIN5three4fourEEC1Ev"));
-}
-
-TEST(DemangleTest, OperatorValues) {
- Demangler demangler;
-
- ASSERT_EQ("operator&&()", demangler.Parse("_Zaav"));
- ASSERT_EQ("operator&()", demangler.Parse("_Zadv"));
- ASSERT_EQ("operator&()", demangler.Parse("_Zanv"));
- ASSERT_EQ("operator&=()", demangler.Parse("_ZaNv"));
- ASSERT_EQ("operator=()", demangler.Parse("_ZaSv"));
- ASSERT_EQ("operator()()", demangler.Parse("_Zclv"));
- ASSERT_EQ("operator,()", demangler.Parse("_Zcmv"));
- ASSERT_EQ("operator~()", demangler.Parse("_Zcov"));
- ASSERT_EQ("operator delete[]()", demangler.Parse("_Zdav"));
- ASSERT_EQ("operator*()", demangler.Parse("_Zdev"));
- ASSERT_EQ("operator delete()", demangler.Parse("_Zdlv"));
- ASSERT_EQ("operator/()", demangler.Parse("_Zdvv"));
- ASSERT_EQ("operator/=()", demangler.Parse("_ZdVv"));
- ASSERT_EQ("operator^()", demangler.Parse("_Zeov"));
- ASSERT_EQ("operator^=()", demangler.Parse("_ZeOv"));
- ASSERT_EQ("operator==()", demangler.Parse("_Zeqv"));
- ASSERT_EQ("operator>=()", demangler.Parse("_Zgev"));
- ASSERT_EQ("operator>()", demangler.Parse("_Zgtv"));
- ASSERT_EQ("operator[]()", demangler.Parse("_Zixv"));
- ASSERT_EQ("operator<=()", demangler.Parse("_Zlev"));
- ASSERT_EQ("operator<<()", demangler.Parse("_Zlsv"));
- ASSERT_EQ("operator<<=()", demangler.Parse("_ZlSv"));
- ASSERT_EQ("operator<()", demangler.Parse("_Zltv"));
- ASSERT_EQ("operator-()", demangler.Parse("_Zmiv"));
- ASSERT_EQ("operator-=()", demangler.Parse("_ZmIv"));
- ASSERT_EQ("operator*()", demangler.Parse("_Zmlv"));
- ASSERT_EQ("operator*=()", demangler.Parse("_ZmLv"));
- ASSERT_EQ("operator--()", demangler.Parse("_Zmmv"));
- ASSERT_EQ("operator new[]()", demangler.Parse("_Znav"));
- ASSERT_EQ("operator!=()", demangler.Parse("_Znev"));
- ASSERT_EQ("operator-()", demangler.Parse("_Zngv"));
- ASSERT_EQ("operator!()", demangler.Parse("_Zntv"));
- ASSERT_EQ("operator new()", demangler.Parse("_Znwv"));
- ASSERT_EQ("operator||()", demangler.Parse("_Zoov"));
- ASSERT_EQ("operator|()", demangler.Parse("_Zorv"));
- ASSERT_EQ("operator|=()", demangler.Parse("_ZoRv"));
- ASSERT_EQ("operator->*()", demangler.Parse("_Zpmv"));
- ASSERT_EQ("operator+()", demangler.Parse("_Zplv"));
- ASSERT_EQ("operator+=()", demangler.Parse("_ZpLv"));
- ASSERT_EQ("operator++()", demangler.Parse("_Zppv"));
- ASSERT_EQ("operator+()", demangler.Parse("_Zpsv"));
- ASSERT_EQ("operator->()", demangler.Parse("_Zptv"));
- ASSERT_EQ("operator?()", demangler.Parse("_Zquv"));
- ASSERT_EQ("operator%()", demangler.Parse("_Zrmv"));
- ASSERT_EQ("operator%=()", demangler.Parse("_ZrMv"));
- ASSERT_EQ("operator>>()", demangler.Parse("_Zrsv"));
- ASSERT_EQ("operator>>=()", demangler.Parse("_ZrSv"));
-
- // Spot check using an operator as part of function name.
- ASSERT_EQ("operator&&()", demangler.Parse("_ZNaaEv"));
- ASSERT_EQ("operator++()", demangler.Parse("_ZNppEv"));
- ASSERT_EQ("one::operator++()", demangler.Parse("_ZN3oneppEv"));
-
- // Spot check using an operator in an argument name.
- ASSERT_EQ("operator+(operator|=)", demangler.Parse("_ZNpsENoRE"));
- ASSERT_EQ("operator==()", demangler.Parse("_Zeqv"));
- ASSERT_EQ("one(arg1::operator|=, arg2::operator==)",
- demangler.Parse("_ZN3oneEN4arg1oREN4arg2eqE"));
-}
-
-TEST(DemangleTest, FunctionStartsWithNumber) {
- Demangler demangler;
-
- ASSERT_EQ("value(char, int)", demangler.Parse("_Z5valueci"));
- ASSERT_EQ("abcdefjklmn(signed char)", demangler.Parse("_Z11abcdefjklmna"));
- ASSERT_EQ("value(one, signed char)", demangler.Parse("_Z5value3onea"));
-}
-
-TEST(DemangleTest, FunctionStartsWithLPlusNumber) {
- Demangler demangler;
-
- ASSERT_EQ("value(char, int)", demangler.Parse("_ZL5valueci"));
- ASSERT_EQ("abcdefjklmn(signed char)", demangler.Parse("_ZL11abcdefjklmna"));
- ASSERT_EQ("value(one, signed char)", demangler.Parse("_ZL5value3onea"));
-}
-
-TEST(DemangleTest, StdTypes) {
- Demangler demangler;
-
- ASSERT_EQ("std::one", demangler.Parse("_ZNSt3oneE"));
- ASSERT_EQ("std::one(std::two)", demangler.Parse("_ZNSt3oneESt3two"));
- ASSERT_EQ("std::std::one(std::two)", demangler.Parse("_ZNStSt3oneESt3two"));
- ASSERT_EQ("std()", demangler.Parse("_ZNStEv"));
- ASSERT_EQ("one::std::std::two::~two(one::std::std::two)",
- demangler.Parse("_ZN3oneStSt3twoD0ES0_"));
-
- ASSERT_EQ("std::allocator", demangler.Parse("_ZNSaE"));
- ASSERT_EQ("std::basic_string", demangler.Parse("_ZNSbE"));
- ASSERT_EQ("_ZNScE", demangler.Parse("_ZNScE"));
- ASSERT_EQ("std::iostream", demangler.Parse("_ZNSdE"));
- ASSERT_EQ("_ZNSeE", demangler.Parse("_ZNSeE"));
- ASSERT_EQ("_ZNSfE", demangler.Parse("_ZNSfE"));
- ASSERT_EQ("_ZNSgE", demangler.Parse("_ZNSgE"));
- ASSERT_EQ("_ZNShE", demangler.Parse("_ZNShE"));
- ASSERT_EQ("std::istream", demangler.Parse("_ZNSiE"));
- ASSERT_EQ("_ZNSjE", demangler.Parse("_ZNSjE"));
- ASSERT_EQ("_ZNSkE", demangler.Parse("_ZNSkE"));
- ASSERT_EQ("_ZNSlE", demangler.Parse("_ZNSlE"));
- ASSERT_EQ("_ZNSmE", demangler.Parse("_ZNSmE"));
- ASSERT_EQ("_ZNSnE", demangler.Parse("_ZNSnE"));
- ASSERT_EQ("std::ostream", demangler.Parse("_ZNSoE"));
- ASSERT_EQ("_ZNSpE", demangler.Parse("_ZNSpE"));
- ASSERT_EQ("_ZNSqE", demangler.Parse("_ZNSqE"));
- ASSERT_EQ("_ZNSrE", demangler.Parse("_ZNSrE"));
- ASSERT_EQ("std::string", demangler.Parse("_ZNSsE"));
- ASSERT_EQ("_ZNSuE", demangler.Parse("_ZNSuE"));
- ASSERT_EQ("_ZNSvE", demangler.Parse("_ZNSvE"));
- ASSERT_EQ("_ZNSwE", demangler.Parse("_ZNSwE"));
- ASSERT_EQ("_ZNSxE", demangler.Parse("_ZNSxE"));
- ASSERT_EQ("_ZNSyE", demangler.Parse("_ZNSyE"));
- ASSERT_EQ("_ZNSzE", demangler.Parse("_ZNSzE"));
-}
-
-TEST(DemangleTest, SingleLetterArguments) {
- Demangler demangler;
-
- ASSERT_EQ("func(signed char)", demangler.Parse("_ZN4funcEa"));
- ASSERT_EQ("func(bool)", demangler.Parse("_ZN4funcEb"));
- ASSERT_EQ("func(char)", demangler.Parse("_ZN4funcEc"));
- ASSERT_EQ("func(double)", demangler.Parse("_ZN4funcEd"));
- ASSERT_EQ("func(long double)", demangler.Parse("_ZN4funcEe"));
- ASSERT_EQ("func(float)", demangler.Parse("_ZN4funcEf"));
- ASSERT_EQ("func(__float128)", demangler.Parse("_ZN4funcEg"));
- ASSERT_EQ("func(unsigned char)", demangler.Parse("_ZN4funcEh"));
- ASSERT_EQ("func(int)", demangler.Parse("_ZN4funcEi"));
- ASSERT_EQ("func(unsigned int)", demangler.Parse("_ZN4funcEj"));
- ASSERT_EQ("_ZN4funcEk", demangler.Parse("_ZN4funcEk"));
- ASSERT_EQ("func(long)", demangler.Parse("_ZN4funcEl"));
- ASSERT_EQ("func(unsigned long)", demangler.Parse("_ZN4funcEm"));
- ASSERT_EQ("func(__int128)", demangler.Parse("_ZN4funcEn"));
- ASSERT_EQ("func(unsigned __int128)", demangler.Parse("_ZN4funcEo"));
- ASSERT_EQ("_ZN4funcEp", demangler.Parse("_ZN4funcEp"));
- ASSERT_EQ("_ZN4funcEq", demangler.Parse("_ZN4funcEq"));
- ASSERT_EQ("_ZN4funcEr", demangler.Parse("_ZN4funcEr"));
- ASSERT_EQ("func(short)", demangler.Parse("_ZN4funcEs"));
- ASSERT_EQ("func(unsigned short)", demangler.Parse("_ZN4funcEt"));
- ASSERT_EQ("_ZN4funcEu", demangler.Parse("_ZN4funcEu"));
- ASSERT_EQ("func()", demangler.Parse("_ZN4funcEv"));
- ASSERT_EQ("func(wchar_t)", demangler.Parse("_ZN4funcEw"));
- ASSERT_EQ("func(long long)", demangler.Parse("_ZN4funcEx"));
- ASSERT_EQ("func(unsigned long long)", demangler.Parse("_ZN4funcEy"));
- ASSERT_EQ("func(...)", demangler.Parse("_ZN4funcEz"));
-}
-
-TEST(DemangleTest, DArguments) {
- Demangler demangler;
-
- ASSERT_EQ("func(auto)", demangler.Parse("_ZN4funcEDa"));
- ASSERT_EQ("_ZN4funcEDb", demangler.Parse("_ZN4funcEDb"));
- ASSERT_EQ("_ZN4funcEDc", demangler.Parse("_ZN4funcEDc"));
- ASSERT_EQ("func(decimal64)", demangler.Parse("_ZN4funcEDd"));
- ASSERT_EQ("func(decimal128)", demangler.Parse("_ZN4funcEDe"));
- ASSERT_EQ("func(decimal32)", demangler.Parse("_ZN4funcEDf"));
- ASSERT_EQ("_ZN4funcEDg", demangler.Parse("_ZN4funcEDg"));
- ASSERT_EQ("func(half)", demangler.Parse("_ZN4funcEDh"));
- ASSERT_EQ("func(char32_t)", demangler.Parse("_ZN4funcEDi"));
- ASSERT_EQ("_ZN4funcEDj", demangler.Parse("_ZN4funcEDj"));
- ASSERT_EQ("_ZN4funcEDk", demangler.Parse("_ZN4funcEDk"));
- ASSERT_EQ("_ZN4funcEDl", demangler.Parse("_ZN4funcEDl"));
- ASSERT_EQ("_ZN4funcEDm", demangler.Parse("_ZN4funcEDm"));
- ASSERT_EQ("func(decltype(nullptr))", demangler.Parse("_ZN4funcEDn"));
- ASSERT_EQ("_ZN4funcEDo", demangler.Parse("_ZN4funcEDo"));
- ASSERT_EQ("_ZN4funcEDp", demangler.Parse("_ZN4funcEDp"));
- ASSERT_EQ("_ZN4funcEDq", demangler.Parse("_ZN4funcEDq"));
- ASSERT_EQ("_ZN4funcEDr", demangler.Parse("_ZN4funcEDr"));
- ASSERT_EQ("func(char16_t)", demangler.Parse("_ZN4funcEDs"));
- ASSERT_EQ("_ZN4funcEDt", demangler.Parse("_ZN4funcEDt"));
- ASSERT_EQ("_ZN4funcEDu", demangler.Parse("_ZN4funcEDu"));
- ASSERT_EQ("_ZN4funcEDv", demangler.Parse("_ZN4funcEDv"));
- ASSERT_EQ("_ZN4funcEDw", demangler.Parse("_ZN4funcEDw"));
- ASSERT_EQ("_ZN4funcEDx", demangler.Parse("_ZN4funcEDx"));
- ASSERT_EQ("_ZN4funcEDy", demangler.Parse("_ZN4funcEDy"));
- ASSERT_EQ("_ZN4funcEDz", demangler.Parse("_ZN4funcEDz"));
-}
-
-TEST(DemangleTest, FunctionArguments) {
- Demangler demangler;
-
- ASSERT_EQ("func(char ())", demangler.Parse("_ZN4funcEFcvE"));
- ASSERT_EQ("func(char (*)())", demangler.Parse("_ZN4funcEPFcvE"));
- ASSERT_EQ("func(char (&)())", demangler.Parse("_ZN4funcERFcvE"));
- ASSERT_EQ("func(char (&)())", demangler.Parse("_ZN4funcERFcvE"));
- ASSERT_EQ("func(char (*&)())", demangler.Parse("_ZN4funcERPFcvE"));
- ASSERT_EQ("func(char (*)(int) const)", demangler.Parse("_ZN4funcEPKFciE"));
- ASSERT_EQ("func(char (&)() const)", demangler.Parse("_ZN4funcERKFcvE"));
- ASSERT_EQ("func(char (&)() volatile)", demangler.Parse("_ZN4funcERVFcvE"));
- ASSERT_EQ("func(char (&)() volatile const)", demangler.Parse("_ZN4funcERKVFcvE"));
- ASSERT_EQ("func(char (&)() const volatile)", demangler.Parse("_ZN4funcERVKFcvE"));
- ASSERT_EQ("func(char (&)(int, signed char) const)", demangler.Parse("_ZN4funcERKFciaE"));
- ASSERT_EQ("fake(char (&* volatile const)(void, void, signed char), signed char)",
- demangler.Parse("_ZN4fakeEKVPRFcvvaEa"));
-}
-
-TEST(DemangleTest, TemplateFunction) {
- Demangler demangler;
-
- ASSERT_EQ("one<char>", demangler.Parse("_ZN3oneIcEE"));
- ASSERT_EQ("one<void>", demangler.Parse("_ZN3oneIvEE"));
- ASSERT_EQ("one<void*>", demangler.Parse("_ZN3oneIPvEE"));
- ASSERT_EQ("one<void const>", demangler.Parse("_ZN3oneIKvEE"));
- ASSERT_EQ("one<char, int, bool>", demangler.Parse("_ZN3oneIcibEE"));
- ASSERT_EQ("one::two<three>", demangler.Parse("_ZN3one3twoIN5threeEEE"));
- ASSERT_EQ("one<char, int, two::three>", demangler.Parse("_ZN3oneIciN3two5threeEEE"));
- // Template within templates.
- ASSERT_EQ("one::two<three<char, int>>", demangler.Parse("_ZN3one3twoIN5threeIciEEEE"));
- ASSERT_EQ("one::two<three<char, four<int>>>", demangler.Parse("_ZN3one3twoIN5threeIcN4fourIiEEEEEE"));
-
- ASSERT_EQ("one<char>", demangler.Parse("_Z3oneIcE"));
- ASSERT_EQ("one<void>", demangler.Parse("_Z3oneIvE"));
- ASSERT_EQ("one<void*>", demangler.Parse("_Z3oneIPvE"));
- ASSERT_EQ("one<void const>", demangler.Parse("_Z3oneIKvE"));
- ASSERT_EQ("one<char, int, bool>", demangler.Parse("_Z3oneIcibE"));
- ASSERT_EQ("one(two<three>)", demangler.Parse("_Z3one3twoIN5threeEE"));
- ASSERT_EQ("one<char, int, two::three>", demangler.Parse("_Z3oneIciN3two5threeEE"));
- // Template within templates.
- ASSERT_EQ("one(two<three<char, int>>)", demangler.Parse("_Z3one3twoIN5threeIciEEE"));
- ASSERT_EQ("one(two<three<char, four<int>>>)",
- demangler.Parse("_Z3one3twoIN5threeIcN4fourIiEEEEE"));
-}
-
-TEST(DemangleTest, TemplateFunctionWithReturnType) {
- Demangler demangler;
-
- ASSERT_EQ("char one<int>(char)", demangler.Parse("_Z3oneIiEcc"));
- ASSERT_EQ("void one<int>()", demangler.Parse("_Z3oneIiEvv"));
- ASSERT_EQ("char one<int>()", demangler.Parse("_Z3oneIiEcv"));
- ASSERT_EQ("char one<int>(void, void)", demangler.Parse("_Z3oneIiEcvv"));
- ASSERT_EQ("char one<int>()", demangler.Parse("_ZN3oneIiEEcv"));
- ASSERT_EQ("char one<int>(void, void)", demangler.Parse("_ZN3oneIiEEcvv"));
-}
-
-TEST(DemangleTest, TemplateArguments) {
- Demangler demangler;
-
- ASSERT_EQ("one(two<char>)", demangler.Parse("_ZN3oneE3twoIcE"));
- ASSERT_EQ("one(two<char, void>)", demangler.Parse("_ZN3oneE3twoIcvE"));
- ASSERT_EQ("one(two<char, void, three<four, int>>)",
- demangler.Parse("_ZN3oneE3twoIcv5threeI4fouriEE"));
-}
-
-TEST(DemangleTest, SubstitutionUnderscore) {
- Demangler demangler;
-
- ASSERT_EQ("a::a", demangler.Parse("_ZN1aS_E"));
- ASSERT_EQ("one::one", demangler.Parse("_ZN3oneS_E"));
- ASSERT_EQ("one::two::one", demangler.Parse("_ZN3one3twoS_E"));
- ASSERT_EQ("one::two::three::one", demangler.Parse("_ZN3one3two5threeS_E"));
- ASSERT_EQ("one::two(one)", demangler.Parse("_ZN3one3twoES_"));
- ASSERT_EQ("one::two(three::one)", demangler.Parse("_ZN3one3twoEN5threeS_E"));
-
- // Special case that St is part of the saved value used in the substitution.
- ASSERT_EQ("std::one::std::one", demangler.Parse("_ZNSt3oneS_E"));
-
- // Multiple substitutions in the string.
- ASSERT_EQ("one::one(one, one)", demangler.Parse("_ZN3oneS_ES_S_"));
- ASSERT_EQ("std::one::two::std::one(std::one)", demangler.Parse("_ZNSt3one3twoS_ES_"));
-}
-
-TEST(DemangleTest, SubstitutionByNumber) {
- Demangler demangler;
-
- // Basic substitution.
- ASSERT_EQ("a::b::c(a::b)", demangler.Parse("_ZN1a1b1cES0_"));
- ASSERT_EQ("_ZN1a1b1cES1_", demangler.Parse("_ZN1a1b1cES1_"));
- ASSERT_EQ("a::b::c::d(a::b::c)", demangler.Parse("_ZN1a1b1c1dES1_"));
- ASSERT_EQ("a::b::c::d::e::f::g::h::i::j::k::l::m::n::o::p::q(a::b::c::d::e::f::g::h::i::j::k::l)",
- demangler.Parse("_ZN1a1b1c1d1e1f1g1h1i1j1k1l1m1n1o1p1qESA_"));
- ASSERT_EQ("a::b::c::d::e::f::g::h::i::j::k::l::m::n::o::p::q(a::b::c::d::e::f::g::h::i::j::k::l::m)",
- demangler.Parse("_ZN1a1b1c1d1e1f1g1h1i1j1k1l1m1n1o1p1qESB_"));
-
- // Verify argument modifiers are included in substitution list.
- ASSERT_EQ("one::two(char&* volatile const, char&)", demangler.Parse("_ZN3one3twoEKVPRcS0_"));
- ASSERT_EQ("one::two(char&* volatile const, char&*)", demangler.Parse("_ZN3one3twoEKVPRcS1_"));
- ASSERT_EQ("one::two(char&* volatile const, char&* volatile const)",
- demangler.Parse("_ZN3one3twoEKVPRcS2_"));
- ASSERT_EQ("one::two(int&* volatile* const, int&)", demangler.Parse("_ZN3one3twoEKPVPRiS0_"));
- ASSERT_EQ("one::two(int&* volatile const, int&*)", demangler.Parse("_ZN3one3twoEKVPRiS1_"));
- ASSERT_EQ("one::two(int&* volatile const, int&* volatile const)",
- demangler.Parse("_ZN3one3twoEKVPRiS2_"));
-
- // Verify Constructor/Destructor does properly save from function name.
- ASSERT_EQ("_ZN1a1bES0_", demangler.Parse("_ZN1a1bES0_"));
- ASSERT_EQ("a::b::b(a::b)", demangler.Parse("_ZN1a1bC1ES0_"));
- ASSERT_EQ("a::b::~b(a::b)", demangler.Parse("_ZN1a1bD0ES0_"));
-
- // Make sure substitution values are not saved.
- ASSERT_EQ("a::b::b(a::b, char*, char*)", demangler.Parse("_ZN1a1bC1ES0_PcS1_"));
-}
-
-TEST(DemangleTest, ComplexSubstitution) {
- Demangler demangler;
-
- ASSERT_EQ("one::two<one::three>::two()", demangler.Parse("_ZN3one3twoINS_5threeEEC1Ev"));
- ASSERT_EQ("one::two::two(one::two const&, bool, one::three*)",
- demangler.Parse("_ZN3one3twoC2ERKS0_bPNS_5threeE"));
- ASSERT_EQ("one::two::three::four<one::five>::~four(one::two*)",
- demangler.Parse("_ZN3one3two5three4fourINS_4fiveEED2EPS0_"));
- ASSERT_EQ("one::two::three::four<one::five>::~four(one::two::three*)",
- demangler.Parse("_ZN3one3two5three4fourINS_4fiveEED2EPS1_"));
- ASSERT_EQ("one::two::three::four<one::five>::~four(one::two::three::four*)",
- demangler.Parse("_ZN3one3two5three4fourINS_4fiveEED2EPS2_"));
- ASSERT_EQ("one::two::three::four<one::five>::~four(one::five*)",
- demangler.Parse("_ZN3one3two5three4fourINS_4fiveEED2EPS3_"));
-}
-
-TEST(DemangleTest, TemplateSubstitution) {
- Demangler demangler;
-
- ASSERT_EQ("void one<int, double>(int)", demangler.Parse("_ZN3oneIidEEvT_"));
- ASSERT_EQ("void one<int, double>(double)", demangler.Parse("_ZN3oneIidEEvT0_"));
- ASSERT_EQ("void one<int, double, char, void>(char)", demangler.Parse("_ZN3oneIidcvEEvT1_"));
-
- ASSERT_EQ("void one<int, double>(int)", demangler.Parse("_Z3oneIidEvT_"));
- ASSERT_EQ("void one<int, double>(double)", demangler.Parse("_Z3oneIidEvT0_"));
- ASSERT_EQ("void one<int, double, char, void>(char)", demangler.Parse("_Z3oneIidcvEvT1_"));
-
- ASSERT_EQ("void one<a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r>(l)",
- demangler.Parse("_ZN3oneI1a1b1c1d1e1f1g1h1i1j1k1l1m1n1o1p1q1rEEvT10_"));
- ASSERT_EQ("void one<a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r>(m)",
- demangler.Parse("_ZN3oneI1a1b1c1d1e1f1g1h1i1j1k1l1m1n1o1p1q1rEEvT11_"));
-
- ASSERT_EQ("void one<a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r>(l)",
- demangler.Parse("_Z3oneI1a1b1c1d1e1f1g1h1i1j1k1l1m1n1o1p1q1rEvT10_"));
- ASSERT_EQ("void one<a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r>(m)",
- demangler.Parse("_Z3oneI1a1b1c1d1e1f1g1h1i1j1k1l1m1n1o1p1q1rEvT11_"));
-}
-
-TEST(DemangleTest, StringTooLong) {
- Demangler demangler;
-
- ASSERT_EQ("_ZN3one3twoC2ERKS0_bPNS_5threeE",
- demangler.Parse("_ZN3one3twoC2ERKS0_bPNS_5threeE", 10));
- ASSERT_EQ("_ZN3one3twoC2ERKS0_bPNS_5threeE",
- demangler.Parse("_ZN3one3twoC2ERKS0_bPNS_5threeE", 30));
- ASSERT_EQ("one::two::two(one::two const&, bool, one::three*)",
- demangler.Parse("_ZN3one3twoC2ERKS0_bPNS_5threeE", 31));
-
- // Check the length check only occurs after the two letter value
- // has been processed.
- ASSERT_EQ("one::two(auto)", demangler.Parse("_ZN3one3twoEDa", 15));
- ASSERT_EQ("one::two(auto)", demangler.Parse("_ZN3one3twoEDa", 14));
- ASSERT_EQ("one::two(auto)", demangler.Parse("_ZN3one3twoEDa", 13));
- ASSERT_EQ("_ZN3one3twoEDa", demangler.Parse("_ZN3one3twoEDa", 12));
-}
-
-TEST(DemangleTest, BooleanLiterals) {
- Demangler demangler;
-
- ASSERT_EQ("one<true>", demangler.Parse("_ZN3oneILb1EEE"));
- ASSERT_EQ("one<false>", demangler.Parse("_ZN3oneILb0EEE"));
- ASSERT_EQ("one<false, true>", demangler.Parse("_ZN3oneILb0ELb1EEE"));
-
- ASSERT_EQ("one<true>", demangler.Parse("_Z3oneILb1EE"));
- ASSERT_EQ("one<false>", demangler.Parse("_Z3oneILb0EE"));
- ASSERT_EQ("one<false, true>", demangler.Parse("_Z3oneILb0ELb1EE"));
-
- ASSERT_EQ("one(two<three<four>, false, true>)",
- demangler.Parse("_ZN3oneE3twoI5threeI4fourELb0ELb1EE"));
-}
-
-TEST(DemangleTest, non_virtual_thunk) {
- Demangler demangler;
-
- ASSERT_EQ("non-virtual thunk to one", demangler.Parse("_ZThn0_N3oneE"));
- ASSERT_EQ("non-virtual thunk to two", demangler.Parse("_ZThn0_3two"));
- ASSERT_EQ("non-virtual thunk to three", demangler.Parse("_ZTh0_5three"));
- ASSERT_EQ("non-virtual thunk to four", demangler.Parse("_ZTh_4four"));
- ASSERT_EQ("non-virtual thunk to five", demangler.Parse("_ZTh0123456789_4five"));
- ASSERT_EQ("non-virtual thunk to six", demangler.Parse("_ZThn0123456789_3six"));
-
- ASSERT_EQ("_ZThn0N3oneE", demangler.Parse("_ZThn0N3oneE"));
- ASSERT_EQ("_ZThn03two", demangler.Parse("_ZThn03two"));
- ASSERT_EQ("_ZTh05three", demangler.Parse("_ZTh05three"));
- ASSERT_EQ("_ZTh4four", demangler.Parse("_ZTh4four"));
- ASSERT_EQ("_ZTh01234567894five", demangler.Parse("_ZTh01234567894five"));
- ASSERT_EQ("_ZThn01234567893six", demangler.Parse("_ZThn01234567893six"));
- ASSERT_EQ("_ZT_N3oneE", demangler.Parse("_ZT_N3oneE"));
- ASSERT_EQ("_ZT0_N3oneE", demangler.Parse("_ZT0_N3oneE"));
- ASSERT_EQ("_ZTH_N3oneE", demangler.Parse("_ZTH_N3oneE"));
-}
-
-TEST(DemangleTest, r_value_reference) {
- Demangler demangler;
- ASSERT_EQ(
- "android::SurfaceComposerClient::Transaction::merge(android::SurfaceComposerClient::"
- "Transaction&&)",
- demangler.Parse("_ZN7android21SurfaceComposerClient11Transaction5mergeEOS1_"));
-}
-
-TEST(DemangleTest, initial_St) {
- Demangler demangler;
- EXPECT_EQ("std::state", demangler.Parse("_ZSt5state"));
- EXPECT_EQ("std::_In::ward", demangler.Parse("_ZNSt3_In4wardE"));
- EXPECT_EQ("std::__terminate(void (*)())", demangler.Parse("_ZSt11__terminatePFvvE"));
-}
-
-TEST(DemangleTest, cfi) {
- Demangler demangler;
- EXPECT_EQ("nfa_sys_ptim_timer_update(tPTIM_CB*)",
- demangler.Parse("_Z25nfa_sys_ptim_timer_updateP8tPTIM_CB"));
- EXPECT_EQ("nfa_sys_ptim_timer_update(tPTIM_CB*) [clone .cfi]",
- demangler.Parse("_Z25nfa_sys_ptim_timer_updateP8tPTIM_CB.cfi"));
-}
-
-TEST(DemangleTest, demangle) {
- std::string str;
-
- str = demangle("_ZN1a1b1cES0_");
- ASSERT_EQ("a::b::c(a::b)", str);
-
- str = demangle("_");
- ASSERT_EQ("_", str);
-
- str = demangle("_Z");
- ASSERT_EQ("_Z", str);
-
- str = demangle("_Za");
- ASSERT_EQ("_Za", str);
-
- str = demangle("_Zaa");
- ASSERT_EQ("operator&&", str);
-
- str = demangle("Xa");
- ASSERT_EQ("Xa", str);
-}
diff --git a/demangle/Demangler.cpp b/demangle/Demangler.cpp
deleted file mode 100644
index 7bae356..0000000
--- a/demangle/Demangler.cpp
+++ /dev/null
@@ -1,925 +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 <assert.h>
-#include <string.h>
-
-#include <cctype>
-#include <stack>
-#include <string>
-#include <vector>
-
-#include "Demangler.h"
-
-constexpr const char* Demangler::kTypes[];
-constexpr const char* Demangler::kDTypes[];
-constexpr const char* Demangler::kSTypes[];
-
-void Demangler::Save(const std::string& str, bool is_name) {
- saves_.push_back(str);
- last_save_name_ = is_name;
-}
-
-std::string Demangler::GetArgumentsString() {
- size_t num_args = cur_state_.args.size();
- std::string arg_str;
- if (num_args > 0) {
- arg_str = cur_state_.args[0];
- for (size_t i = 1; i < num_args; i++) {
- arg_str += ", " + cur_state_.args[i];
- }
- }
- return arg_str;
-}
-
-const char* Demangler::AppendOperatorString(const char* name) {
- const char* oper = nullptr;
- switch (*name) {
- case 'a':
- name++;
- switch (*name) {
- case 'a':
- oper = "operator&&";
- break;
- case 'd':
- case 'n':
- oper = "operator&";
- break;
- case 'N':
- oper = "operator&=";
- break;
- case 'S':
- oper = "operator=";
- break;
- }
- break;
- case 'c':
- name++;
- switch (*name) {
- case 'l':
- oper = "operator()";
- break;
- case 'm':
- oper = "operator,";
- break;
- case 'o':
- oper = "operator~";
- break;
- }
- break;
- case 'd':
- name++;
- switch (*name) {
- case 'a':
- oper = "operator delete[]";
- break;
- case 'e':
- oper = "operator*";
- break;
- case 'l':
- oper = "operator delete";
- break;
- case 'v':
- oper = "operator/";
- break;
- case 'V':
- oper = "operator/=";
- break;
- }
- break;
- case 'e':
- name++;
- switch (*name) {
- case 'o':
- oper = "operator^";
- break;
- case 'O':
- oper = "operator^=";
- break;
- case 'q':
- oper = "operator==";
- break;
- }
- break;
- case 'g':
- name++;
- switch (*name) {
- case 'e':
- oper = "operator>=";
- break;
- case 't':
- oper = "operator>";
- break;
- }
- break;
- case 'i':
- name++;
- switch (*name) {
- case 'x':
- oper = "operator[]";
- break;
- }
- break;
- case 'l':
- name++;
- switch (*name) {
- case 'e':
- oper = "operator<=";
- break;
- case 's':
- oper = "operator<<";
- break;
- case 'S':
- oper = "operator<<=";
- break;
- case 't':
- oper = "operator<";
- break;
- }
- break;
- case 'm':
- name++;
- switch (*name) {
- case 'i':
- oper = "operator-";
- break;
- case 'I':
- oper = "operator-=";
- break;
- case 'l':
- oper = "operator*";
- break;
- case 'L':
- oper = "operator*=";
- break;
- case 'm':
- oper = "operator--";
- break;
- }
- break;
- case 'n':
- name++;
- switch (*name) {
- case 'a':
- oper = "operator new[]";
- break;
- case 'e':
- oper = "operator!=";
- break;
- case 'g':
- oper = "operator-";
- break;
- case 't':
- oper = "operator!";
- break;
- case 'w':
- oper = "operator new";
- break;
- }
- break;
- case 'o':
- name++;
- switch (*name) {
- case 'o':
- oper = "operator||";
- break;
- case 'r':
- oper = "operator|";
- break;
- case 'R':
- oper = "operator|=";
- break;
- }
- break;
- case 'p':
- name++;
- switch (*name) {
- case 'm':
- oper = "operator->*";
- break;
- case 'l':
- oper = "operator+";
- break;
- case 'L':
- oper = "operator+=";
- break;
- case 'p':
- oper = "operator++";
- break;
- case 's':
- oper = "operator+";
- break;
- case 't':
- oper = "operator->";
- break;
- }
- break;
- case 'q':
- name++;
- switch (*name) {
- case 'u':
- oper = "operator?";
- break;
- }
- break;
- case 'r':
- name++;
- switch (*name) {
- case 'm':
- oper = "operator%";
- break;
- case 'M':
- oper = "operator%=";
- break;
- case 's':
- oper = "operator>>";
- break;
- case 'S':
- oper = "operator>>=";
- break;
- }
- break;
- }
- if (oper == nullptr) {
- return nullptr;
- }
- AppendCurrent(oper);
- cur_state_.last_save = oper;
- return name + 1;
-}
-
-const char* Demangler::GetStringFromLength(const char* name, std::string* str) {
- assert(std::isdigit(*name));
-
- size_t length = *name - '0';
- name++;
- while (*name != '\0' && std::isdigit(*name)) {
- length = length * 10 + *name - '0';
- name++;
- }
-
- std::string read_str;
- while (*name != '\0' && length != 0) {
- read_str += *name;
- name++;
- length--;
- }
- if (length != 0) {
- return nullptr;
- }
- // Special replacement of _GLOBAL__N_1 to (anonymous namespace).
- if (read_str == "_GLOBAL__N_1") {
- *str += "(anonymous namespace)";
- } else {
- *str += read_str;
- }
- return name;
-}
-
-void Demangler::AppendCurrent(const std::string& str) {
- if (!cur_state_.str.empty()) {
- cur_state_.str += "::";
- }
- cur_state_.str += str;
-}
-
-void Demangler::AppendCurrent(const char* str) {
- if (!cur_state_.str.empty()) {
- cur_state_.str += "::";
- }
- cur_state_.str += str;
-}
-
-const char* Demangler::ParseS(const char* name) {
- if (std::islower(*name)) {
- const char* type = kSTypes[*name - 'a'];
- if (type == nullptr) {
- return nullptr;
- }
- AppendCurrent(type);
- return name + 1;
- }
-
- if (saves_.empty()) {
- return nullptr;
- }
-
- if (*name == '_') {
- last_save_name_ = false;
- AppendCurrent(saves_[0]);
- return name + 1;
- }
-
- bool isdigit = std::isdigit(*name);
- if (!isdigit && !std::isupper(*name)) {
- return nullptr;
- }
-
- size_t index;
- if (isdigit) {
- index = *name - '0' + 1;
- } else {
- index = *name - 'A' + 11;
- }
- name++;
- if (*name != '_') {
- return nullptr;
- }
-
- if (index >= saves_.size()) {
- return nullptr;
- }
-
- last_save_name_ = false;
- AppendCurrent(saves_[index]);
- return name + 1;
-}
-
-const char* Demangler::ParseT(const char* name) {
- if (template_saves_.empty()) {
- return nullptr;
- }
-
- if (*name == '_') {
- last_save_name_ = false;
- AppendCurrent(template_saves_[0]);
- return name + 1;
- }
-
- // Need to get the total number.
- char* end;
- unsigned long int index = strtoul(name, &end, 10) + 1;
- if (name == end || *end != '_') {
- return nullptr;
- }
-
- if (index >= template_saves_.size()) {
- return nullptr;
- }
-
- last_save_name_ = false;
- AppendCurrent(template_saves_[index]);
- return end + 1;
-}
-
-const char* Demangler::ParseFunctionName(const char* name) {
- if (*name == 'E') {
- if (parse_funcs_.empty()) {
- return nullptr;
- }
- parse_func_ = parse_funcs_.back();
- parse_funcs_.pop_back();
-
- // Remove the last saved part so that the full function name is not saved.
- // But only if the last save was not something like a substitution.
- if (!saves_.empty() && last_save_name_) {
- saves_.pop_back();
- }
-
- function_name_ += cur_state_.str;
- while (!cur_state_.suffixes.empty()) {
- function_suffix_ += cur_state_.suffixes.back();
- cur_state_.suffixes.pop_back();
- }
- cur_state_.Clear();
-
- return name + 1;
- }
-
- if (*name == 'I') {
- state_stack_.push(cur_state_);
- cur_state_.Clear();
-
- parse_funcs_.push_back(parse_func_);
- parse_func_ = &Demangler::ParseFunctionNameTemplate;
- return name + 1;
- }
-
- return ParseComplexString(name);
-}
-
-const char* Demangler::ParseFunctionNameTemplate(const char* name) {
- if (*name == 'E' && name[1] == 'E') {
- // Only consider this a template with saves if it is right before
- // the end of the name.
- template_found_ = true;
- template_saves_ = cur_state_.args;
- }
- return ParseTemplateArgumentsComplex(name);
-}
-
-const char* Demangler::ParseComplexArgument(const char* name) {
- if (*name == 'E') {
- if (parse_funcs_.empty()) {
- return nullptr;
- }
- parse_func_ = parse_funcs_.back();
- parse_funcs_.pop_back();
-
- AppendArgument(cur_state_.str);
- cur_state_.str.clear();
-
- return name + 1;
- }
-
- return ParseComplexString(name);
-}
-
-void Demangler::FinalizeTemplate() {
- std::string arg_str(GetArgumentsString());
- cur_state_ = state_stack_.top();
- state_stack_.pop();
- cur_state_.str += '<' + arg_str + '>';
-}
-
-const char* Demangler::ParseComplexString(const char* name) {
- if (*name == 'S') {
- name++;
- if (*name == 't') {
- AppendCurrent("std");
- return name + 1;
- }
- return ParseS(name);
- }
- if (*name == 'L') {
- name++;
- if (!std::isdigit(*name)) {
- return nullptr;
- }
- }
- if (std::isdigit(*name)) {
- std::string str;
- name = GetStringFromLength(name, &str);
- if (name == nullptr) {
- return name;
- }
- AppendCurrent(str);
- Save(cur_state_.str, true);
- cur_state_.last_save = std::move(str);
- return name;
- }
- if (*name == 'D') {
- name++;
- if (saves_.empty() || (*name != '0' && *name != '1' && *name != '2'
- && *name != '5')) {
- return nullptr;
- }
- last_save_name_ = false;
- AppendCurrent("~" + cur_state_.last_save);
- return name + 1;
- }
- if (*name == 'C') {
- name++;
- if (saves_.empty() || (*name != '1' && *name != '2' && *name != '3'
- && *name != '5')) {
- return nullptr;
- }
- last_save_name_ = false;
- AppendCurrent(cur_state_.last_save);
- return name + 1;
- }
- if (*name == 'K') {
- cur_state_.suffixes.push_back(" const");
- return name + 1;
- }
- if (*name == 'V') {
- cur_state_.suffixes.push_back(" volatile");
- return name + 1;
- }
- if (*name == 'I') {
- // Save the current argument state.
- state_stack_.push(cur_state_);
- cur_state_.Clear();
-
- parse_funcs_.push_back(parse_func_);
- parse_func_ = &Demangler::ParseTemplateArgumentsComplex;
- return name + 1;
- }
- name = AppendOperatorString(name);
- if (name != nullptr) {
- Save(cur_state_.str, true);
- }
- return name;
-}
-
-void Demangler::AppendArgument(const std::string& str) {
- std::string arg(str);
- while (!cur_state_.suffixes.empty()) {
- arg += cur_state_.suffixes.back();
- cur_state_.suffixes.pop_back();
- Save(arg, false);
- }
- cur_state_.args.push_back(arg);
-}
-
-const char* Demangler::ParseFunctionArgument(const char* name) {
- if (*name == 'E') {
- // The first argument is the function modifier.
- // The second argument is the function type.
- // The third argument is the return type of the function.
- // The rest of the arguments are the function arguments.
- size_t num_args = cur_state_.args.size();
- if (num_args < 4) {
- return nullptr;
- }
- std::string function_modifier = cur_state_.args[0];
- std::string function_type = cur_state_.args[1];
-
- std::string str = cur_state_.args[2] + ' ';
- if (!cur_state_.args[1].empty()) {
- str += '(' + cur_state_.args[1] + ')';
- }
-
- if (num_args == 4 && cur_state_.args[3] == "void") {
- str += "()";
- } else {
- str += '(' + cur_state_.args[3];
- for (size_t i = 4; i < num_args; i++) {
- str += ", " + cur_state_.args[i];
- }
- str += ')';
- }
- str += cur_state_.args[0];
-
- cur_state_ = state_stack_.top();
- state_stack_.pop();
- cur_state_.args.emplace_back(std::move(str));
-
- parse_func_ = parse_funcs_.back();
- parse_funcs_.pop_back();
- return name + 1;
- }
- return ParseArguments(name);
-}
-
-const char* Demangler::ParseArguments(const char* name) {
- switch (*name) {
- case 'P':
- cur_state_.suffixes.push_back("*");
- return name + 1;
-
- case 'R':
- // This should always be okay because the string is guaranteed to have
- // at least two characters before this. A mangled string always starts
- // with _Z.
- if (name[-1] != 'R') {
- // Multiple 'R's in a row only add a single &.
- cur_state_.suffixes.push_back("&");
- }
- return name + 1;
-
- case 'O':
- cur_state_.suffixes.push_back("&&");
- return name + 1;
-
- case 'K':
- case 'V': {
- const char* suffix;
- if (*name == 'K') {
- suffix = " const";
- } else {
- suffix = " volatile";
- }
- if (!cur_state_.suffixes.empty() && (name[-1] == 'K' || name[-1] == 'V')) {
- // Special case, const/volatile apply as a single entity.
- size_t index = cur_state_.suffixes.size();
- cur_state_.suffixes[index-1].insert(0, suffix);
- } else {
- cur_state_.suffixes.push_back(suffix);
- }
- return name + 1;
- }
-
- case 'F': {
- std::string function_modifier;
- std::string function_type;
- if (!cur_state_.suffixes.empty()) {
- // If the first element starts with a ' ', then this modifies the
- // function itself.
- if (cur_state_.suffixes.back()[0] == ' ') {
- function_modifier = cur_state_.suffixes.back();
- cur_state_.suffixes.pop_back();
- }
- while (!cur_state_.suffixes.empty()) {
- function_type += cur_state_.suffixes.back();
- cur_state_.suffixes.pop_back();
- }
- }
-
- state_stack_.push(cur_state_);
-
- cur_state_.Clear();
-
- // The function parameter has this format:
- // First argument is the function modifier.
- // Second argument is the function type.
- // Third argument will be the return function type but has not
- // been parsed yet.
- // Any other parameters are the arguments to the function. There
- // must be at least one or this isn't valid.
- cur_state_.args.push_back(function_modifier);
- cur_state_.args.push_back(function_type);
-
- parse_funcs_.push_back(parse_func_);
- parse_func_ = &Demangler::ParseFunctionArgument;
- return name + 1;
- }
-
- case 'N':
- parse_funcs_.push_back(parse_func_);
- parse_func_ = &Demangler::ParseComplexArgument;
- return name + 1;
-
- case 'S':
- name++;
- if (*name == 't') {
- cur_state_.str = "std::";
- return name + 1;
- }
- name = ParseS(name);
- if (name == nullptr) {
- return nullptr;
- }
- AppendArgument(cur_state_.str);
- cur_state_.str.clear();
- return name;
-
- case 'D':
- name++;
- if (*name >= 'a' && *name <= 'z') {
- const char* arg = Demangler::kDTypes[*name - 'a'];
- if (arg == nullptr) {
- return nullptr;
- }
- AppendArgument(arg);
- return name + 1;
- }
- return nullptr;
-
- case 'I':
- // Save the current argument state.
- state_stack_.push(cur_state_);
- cur_state_.Clear();
-
- parse_funcs_.push_back(parse_func_);
- parse_func_ = &Demangler::ParseTemplateArguments;
- return name + 1;
-
- case 'v':
- AppendArgument("void");
- return name + 1;
-
- default:
- if (*name >= 'a' && *name <= 'z') {
- const char* arg = Demangler::kTypes[*name - 'a'];
- if (arg == nullptr) {
- return nullptr;
- }
- AppendArgument(arg);
- return name + 1;
- } else if (std::isdigit(*name)) {
- std::string arg = cur_state_.str;
- name = GetStringFromLength(name, &arg);
- if (name == nullptr) {
- return nullptr;
- }
- Save(arg, true);
- if (*name == 'I') {
- // There is one case where this argument is not complete, and that's
- // where this is a template argument.
- cur_state_.str = arg;
- } else {
- AppendArgument(arg);
- cur_state_.str.clear();
- }
- return name;
- } else if (strcmp(name, ".cfi") == 0) {
- function_suffix_ += " [clone .cfi]";
- return name + 4;
- }
- }
- return nullptr;
-}
-
-const char* Demangler::ParseTemplateLiteral(const char* name) {
- if (*name == 'E') {
- parse_func_ = parse_funcs_.back();
- parse_funcs_.pop_back();
- return name + 1;
- }
- // Only understand boolean values with 0 or 1.
- if (*name == 'b') {
- name++;
- if (*name == '0') {
- AppendArgument("false");
- cur_state_.str.clear();
- } else if (*name == '1') {
- AppendArgument("true");
- cur_state_.str.clear();
- } else {
- return nullptr;
- }
- return name + 1;
- }
- return nullptr;
-}
-
-const char* Demangler::ParseTemplateArgumentsComplex(const char* name) {
- if (*name == 'E') {
- if (parse_funcs_.empty()) {
- return nullptr;
- }
- parse_func_ = parse_funcs_.back();
- parse_funcs_.pop_back();
-
- FinalizeTemplate();
- Save(cur_state_.str, false);
- return name + 1;
- } else if (*name == 'L') {
- // Literal value for a template.
- parse_funcs_.push_back(parse_func_);
- parse_func_ = &Demangler::ParseTemplateLiteral;
- return name + 1;
- }
-
- return ParseArguments(name);
-}
-
-const char* Demangler::ParseTemplateArguments(const char* name) {
- if (*name == 'E') {
- if (parse_funcs_.empty()) {
- return nullptr;
- }
- parse_func_ = parse_funcs_.back();
- parse_funcs_.pop_back();
- FinalizeTemplate();
- AppendArgument(cur_state_.str);
- cur_state_.str.clear();
- return name + 1;
- } else if (*name == 'L') {
- // Literal value for a template.
- parse_funcs_.push_back(parse_func_);
- parse_func_ = &Demangler::ParseTemplateLiteral;
- return name + 1;
- }
-
- return ParseArguments(name);
-}
-
-const char* Demangler::ParseFunctionTemplateArguments(const char* name) {
- if (*name == 'E') {
- parse_func_ = parse_funcs_.back();
- parse_funcs_.pop_back();
-
- function_name_ += '<' + GetArgumentsString() + '>';
- template_found_ = true;
- template_saves_ = cur_state_.args;
- cur_state_.Clear();
- return name + 1;
- }
- return ParseTemplateArgumentsComplex(name);
-}
-
-const char* Demangler::FindFunctionName(const char* name) {
- if (*name == 'T') {
- // non-virtual thunk, verify that it matches one of these patterns:
- // Thn[0-9]+_
- // Th[0-9]+_
- // Thn_
- // Th_
- name++;
- if (*name != 'h') {
- return nullptr;
- }
- name++;
- if (*name == 'n') {
- name++;
- }
- while (std::isdigit(*name)) {
- name++;
- }
- if (*name != '_') {
- return nullptr;
- }
- function_name_ = "non-virtual thunk to ";
- return name + 1;
- }
-
- if (*name == 'N') {
- parse_funcs_.push_back(&Demangler::ParseArgumentsAtTopLevel);
- parse_func_ = &Demangler::ParseFunctionName;
- return name + 1;
- }
-
- if (*name == 'S') {
- name++;
- if (*name == 't') {
- function_name_ = "std::";
- name++;
- } else {
- return nullptr;
- }
- }
-
- if (std::isdigit(*name)) {
- name = GetStringFromLength(name, &function_name_);
- } else if (*name == 'L' && std::isdigit(name[1])) {
- name = GetStringFromLength(name + 1, &function_name_);
- } else {
- name = AppendOperatorString(name);
- function_name_ = cur_state_.str;
- }
- cur_state_.Clear();
-
- // Check for a template argument, which will still be part of the function
- // name.
- if (name != nullptr && *name == 'I') {
- parse_funcs_.push_back(&Demangler::ParseArgumentsAtTopLevel);
- parse_func_ = &Demangler::ParseFunctionTemplateArguments;
- return name + 1;
- }
- parse_func_ = &Demangler::ParseArgumentsAtTopLevel;
- return name;
-}
-
-const char* Demangler::ParseArgumentsAtTopLevel(const char* name) {
- // At the top level is the only place where T is allowed.
- if (*name == 'T') {
- name++;
- name = ParseT(name);
- if (name == nullptr) {
- return nullptr;
- }
- AppendArgument(cur_state_.str);
- cur_state_.str.clear();
- return name;
- }
-
- return Demangler::ParseArguments(name);
-}
-
-std::string Demangler::Parse(const char* name, size_t max_length) {
- if (name[0] == '\0' || name[0] != '_' || name[1] == '\0' || name[1] != 'Z') {
- // Name is not mangled.
- return name;
- }
-
- Clear();
-
- parse_func_ = &Demangler::FindFunctionName;
- parse_funcs_.push_back(&Demangler::Fail);
- const char* cur_name = name + 2;
- while (cur_name != nullptr && *cur_name != '\0'
- && static_cast<size_t>(cur_name - name) < max_length) {
- cur_name = (this->*parse_func_)(cur_name);
- }
- if (cur_name == nullptr || *cur_name != '\0' || function_name_.empty() ||
- !cur_state_.suffixes.empty()) {
- return name;
- }
-
- std::string return_type;
- if (template_found_) {
- // Only a single argument with a template is not allowed.
- if (cur_state_.args.size() == 1) {
- return name;
- }
-
- // If there are at least two arguments, this template has a return type.
- if (cur_state_.args.size() > 1) {
- // The first argument will be the return value.
- return_type = cur_state_.args[0] + ' ';
- cur_state_.args.erase(cur_state_.args.begin());
- }
- }
-
- std::string arg_str;
- if (cur_state_.args.size() == 1 && cur_state_.args[0] == "void") {
- // If the only argument is void, then don't print any args.
- arg_str = "()";
- } else {
- arg_str = GetArgumentsString();
- if (!arg_str.empty()) {
- arg_str = '(' + arg_str + ')';
- }
- }
- return return_type + function_name_ + arg_str + function_suffix_;
-}
-
-std::string demangle(const char* name) {
- Demangler demangler;
- return demangler.Parse(name);
-}
diff --git a/demangle/Demangler.h b/demangle/Demangler.h
deleted file mode 100644
index 3b7d44e..0000000
--- a/demangle/Demangler.h
+++ /dev/null
@@ -1,200 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __LIB_DEMANGLE_DEMANGLER_H
-#define __LIB_DEMANGLE_DEMANGLER_H
-
-#include <assert.h>
-
-#include <stack>
-#include <string>
-#include <vector>
-
-class Demangler {
- public:
- Demangler() = default;
-
- // NOTE: The max_length is not guaranteed to be the absolute max length
- // of a string that will be rejected. Under certain circumstances the
- // length check will not occur until after the second letter of a pair
- // is checked.
- std::string Parse(const char* name, size_t max_length = kMaxDefaultLength);
-
- void AppendCurrent(const std::string& str);
- void AppendCurrent(const char* str);
- void AppendArgument(const std::string& str);
- std::string GetArgumentsString();
- void FinalizeTemplate();
- const char* ParseS(const char* name);
- const char* ParseT(const char* name);
- const char* AppendOperatorString(const char* name);
- void Save(const std::string& str, bool is_name);
-
- private:
- void Clear() {
- parse_funcs_.clear();
- function_name_.clear();
- function_suffix_.clear();
- first_save_.clear();
- cur_state_.Clear();
- saves_.clear();
- template_saves_.clear();
- while (!state_stack_.empty()) {
- state_stack_.pop();
- }
- last_save_name_ = false;
- template_found_ = false;
- }
-
- using parse_func_type = const char* (Demangler::*)(const char*);
- parse_func_type parse_func_;
- std::vector<parse_func_type> parse_funcs_;
- std::vector<std::string> saves_;
- std::vector<std::string> template_saves_;
- bool last_save_name_;
- bool template_found_;
-
- std::string function_name_;
- std::string function_suffix_;
-
- struct StateData {
- void Clear() {
- str.clear();
- args.clear();
- prefix.clear();
- suffixes.clear();
- last_save.clear();
- }
-
- std::string str;
- std::vector<std::string> args;
- std::string prefix;
- std::vector<std::string> suffixes;
- std::string last_save;
- };
- std::stack<StateData> state_stack_;
- std::string first_save_;
- StateData cur_state_;
-
- static const char* GetStringFromLength(const char* name, std::string* str);
-
- // Parsing functions.
- const char* ParseComplexString(const char* name);
- const char* ParseComplexArgument(const char* name);
- const char* ParseArgumentsAtTopLevel(const char* name);
- const char* ParseArguments(const char* name);
- const char* ParseTemplateArguments(const char* name);
- const char* ParseTemplateArgumentsComplex(const char* name);
- const char* ParseTemplateLiteral(const char* name);
- const char* ParseFunctionArgument(const char* name);
- const char* ParseFunctionName(const char* name);
- const char* ParseFunctionNameTemplate(const char* name);
- const char* ParseFunctionTemplateArguments(const char* name);
- const char* FindFunctionName(const char* name);
- const char* Fail(const char*) { return nullptr; }
-
- // The default maximum string length string to process.
- static constexpr size_t kMaxDefaultLength = 2048;
-
- static constexpr const char* kTypes[] = {
- "signed char", // a
- "bool", // b
- "char", // c
- "double", // d
- "long double", // e
- "float", // f
- "__float128", // g
- "unsigned char", // h
- "int", // i
- "unsigned int", // j
- nullptr, // k
- "long", // l
- "unsigned long", // m
- "__int128", // n
- "unsigned __int128", // o
- nullptr, // p
- nullptr, // q
- nullptr, // r
- "short", // s
- "unsigned short", // t
- nullptr, // u
- "void", // v
- "wchar_t", // w
- "long long", // x
- "unsigned long long", // y
- "...", // z
- };
-
- static constexpr const char* kDTypes[] = {
- "auto", // a
- nullptr, // b
- nullptr, // c
- "decimal64", // d
- "decimal128", // e
- "decimal32", // f
- nullptr, // g
- "half", // h
- "char32_t", // i
- nullptr, // j
- nullptr, // k
- nullptr, // l
- nullptr, // m
- "decltype(nullptr)", // n
- nullptr, // o
- nullptr, // p
- nullptr, // q
- nullptr, // r
- "char16_t", // s
- nullptr, // t
- nullptr, // u
- nullptr, // v
- nullptr, // w
- nullptr, // x
- nullptr, // y
- nullptr, // z
- };
-
- static constexpr const char* kSTypes[] = {
- "std::allocator", // a
- "std::basic_string", // b
- nullptr, // c
- "std::iostream", // d
- nullptr, // e
- nullptr, // f
- nullptr, // g
- nullptr, // h
- "std::istream", // i
- nullptr, // j
- nullptr, // k
- nullptr, // l
- nullptr, // m
- nullptr, // n
- "std::ostream", // o
- nullptr, // p
- nullptr, // q
- nullptr, // r
- "std::string", // s
- nullptr, // t
- nullptr, // u
- nullptr, // v
- nullptr, // w
- nullptr, // x
- nullptr, // y
- nullptr, // z
- };
-};
-
-#endif // __LIB_DEMANGLE_DEMANGLER_H
diff --git a/demangle/OWNERS b/demangle/OWNERS
deleted file mode 100644
index 6f7e4a3..0000000
--- a/demangle/OWNERS
+++ /dev/null
@@ -1 +0,0 @@
-cferris@google.com
diff --git a/demangle/demangle.cpp b/demangle/demangle.cpp
deleted file mode 100644
index 66e5e58..0000000
--- a/demangle/demangle.cpp
+++ /dev/null
@@ -1,133 +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 <getopt.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <cctype>
-#include <string>
-
-#include <demangle.h>
-
-extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
-
-static void Usage(const char* prog_name) {
- printf("usage: %s [-c] [NAME_TO_DEMANGLE...]\n", prog_name);
- printf("\n");
- printf("Demangles C++ mangled names if supplied on the command-line, or found\n");
- printf("reading from stdin otherwise.\n");
- printf("\n");
- printf("-c\tCompare against __cxa_demangle\n");
- printf("\n");
-}
-
-static std::string DemangleWithCxa(const char* name) {
- const char* cxa_demangle = __cxa_demangle(name, nullptr, nullptr, nullptr);
- if (cxa_demangle == nullptr) {
- return name;
- }
-
- // The format of our demangler is slightly different from the cxa demangler
- // so modify the cxa demangler output. Specifically, for templates, remove
- // the spaces between '>' and '>'.
- std::string demangled_str;
- for (size_t i = 0; i < strlen(cxa_demangle); i++) {
- if (i > 2 && cxa_demangle[i] == '>' && std::isspace(cxa_demangle[i - 1]) &&
- cxa_demangle[i - 2] == '>') {
- demangled_str.resize(demangled_str.size() - 1);
- }
- demangled_str += cxa_demangle[i];
- }
- return demangled_str;
-}
-
-static void Compare(const char* name, const std::string& demangled_name) {
- std::string cxa_demangled_name(DemangleWithCxa(name));
- if (cxa_demangled_name != demangled_name) {
- printf("\nMismatch!\n");
- printf("\tmangled name: %s\n", name);
- printf("\tour demangle: %s\n", demangled_name.c_str());
- printf("\tcxa demangle: %s\n", cxa_demangled_name.c_str());
- exit(1);
- }
-}
-
-static int Filter(bool compare) {
- char* line = nullptr;
- size_t line_length = 0;
-
- while ((getline(&line, &line_length, stdin)) != -1) {
- char* p = line;
- char* name;
- while ((name = strstr(p, "_Z")) != nullptr) {
- // Output anything before the identifier.
- *name = 0;
- printf("%s", p);
- *name = '_';
-
- // Extract the identifier.
- p = name;
- while (*p && (std::isalnum(*p) || *p == '_' || *p == '.' || *p == '$')) ++p;
-
- // Demangle and output.
- std::string identifier(name, p);
- std::string demangled_name = demangle(identifier.c_str());
- printf("%s", demangled_name.c_str());
-
- if (compare) Compare(identifier.c_str(), demangled_name);
- }
- // Output anything after the last identifier.
- printf("%s", p);
- }
-
- free(line);
- return 0;
-}
-
-int main(int argc, char** argv) {
-#ifdef __BIONIC__
- const char* prog_name = getprogname();
-#else
- const char* prog_name = argv[0];
-#endif
-
- bool compare = false;
- int opt_char;
- while ((opt_char = getopt(argc, argv, "c")) != -1) {
- if (opt_char == 'c') {
- compare = true;
- } else {
- Usage(prog_name);
- return 1;
- }
- }
-
- // With no arguments, act as a filter.
- if (optind == argc) return Filter(compare);
-
- // Otherwise demangle each argument.
- while (optind < argc) {
- const char* name = argv[optind++];
- std::string demangled_name = demangle(name);
- printf("%s\n", demangled_name.c_str());
-
- if (compare) Compare(name, demangled_name);
- }
- return 0;
-}
diff --git a/demangle/demangle_fuzzer.cpp b/demangle/demangle_fuzzer.cpp
deleted file mode 100644
index 83fafc2..0000000
--- a/demangle/demangle_fuzzer.cpp
+++ /dev/null
@@ -1,36 +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 <stddef.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <string>
-
-#include "Demangler.h"
-
-extern "C" void LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
- std::vector<char> data_str(size + 1);
- memcpy(data_str.data(), data, size);
- data_str[size] = '\0';
-
- Demangler demangler;
- std::string demangled_name = demangler.Parse(data_str.data());
- if (size != 0 && data_str[0] != '\0' && demangled_name.empty()) {
- abort();
- }
-}
diff --git a/demangle/include/demangle.h b/demangle/include/demangle.h
deleted file mode 100644
index 01f1b80..0000000
--- a/demangle/include/demangle.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __LIB_DEMANGLE_H_
-#define __LIB_DEMANGLE_H_
-
-#include <string>
-
-// If the name cannot be demangled, the original name will be returned as
-// a std::string. If the name can be demangled, then the demangled name
-// will be returned as a std::string.
-std::string demangle(const char* name);
-
-#endif // __LIB_DEMANGLE_H_
diff --git a/fs_mgr/README.overlayfs.md b/fs_mgr/README.overlayfs.md
index d204bfd..fe2e052 100644
--- a/fs_mgr/README.overlayfs.md
+++ b/fs_mgr/README.overlayfs.md
@@ -95,8 +95,8 @@
- Kernel must have CONFIG_OVERLAY_FS=y and will need to be patched
with "*overlayfs: override_creds=off option bypass creator_cred*"
if kernel is 4.4 or higher.
- The patch is available on the upstream mailing list and the latest as of
- Feb 8 2019 is https://lore.kernel.org/patchwork/patch/1009299/.
+ The patch series is available on the upstream mailing list and the latest as
+ of Jul 24 2019 is https://lore.kernel.org/patchwork/patch/1104577/.
This patch adds an override_creds _mount_ option to overlayfs that
permits legacy behavior for systems that do not have overlapping
sepolicy rules, principals of least privilege, which is how Android behaves.
diff --git a/fs_mgr/libdm/Android.bp b/fs_mgr/libdm/Android.bp
index e429d9f..4cdea71 100644
--- a/fs_mgr/libdm/Android.bp
+++ b/fs_mgr/libdm/Android.bp
@@ -27,6 +27,7 @@
"dm_target.cpp",
"dm.cpp",
"loop_control.cpp",
+ "utility.cpp",
],
static_libs: [
diff --git a/fs_mgr/libdm/dm.cpp b/fs_mgr/libdm/dm.cpp
index d56a4b1..a4e0d76 100644
--- a/fs_mgr/libdm/dm.cpp
+++ b/fs_mgr/libdm/dm.cpp
@@ -29,6 +29,8 @@
#include <android-base/strings.h>
#include <uuid/uuid.h>
+#include "utility.h"
+
namespace android {
namespace dm {
@@ -94,20 +96,6 @@
return true;
}
-bool WaitForCondition(const std::function<bool()>& condition,
- const std::chrono::milliseconds& timeout_ms) {
- auto start_time = std::chrono::steady_clock::now();
- while (true) {
- if (condition()) return true;
-
- std::this_thread::sleep_for(20ms);
-
- auto now = std::chrono::steady_clock::now();
- auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
- if (time_elapsed > timeout_ms) return false;
- }
-}
-
static std::string GenerateUuid() {
uuid_t uuid_bytes;
uuid_generate(uuid_bytes);
@@ -138,16 +126,7 @@
if (timeout_ms <= std::chrono::milliseconds::zero()) {
return true;
}
-
- auto condition = [&]() -> bool {
- // If the file exists but returns EPERM or something, we consider the
- // condition met.
- if (access(unique_path.c_str(), F_OK) != 0) {
- if (errno == ENOENT) return false;
- }
- return true;
- };
- if (!WaitForCondition(condition, timeout_ms)) {
+ if (!WaitForFile(unique_path, timeout_ms)) {
LOG(ERROR) << "Timed out waiting for device path: " << unique_path;
DeleteDevice(name);
return false;
@@ -171,6 +150,15 @@
return true;
}
+std::optional<DeviceMapper::Info> DeviceMapper::GetDetailedInfo(const std::string& name) const {
+ struct dm_ioctl io;
+ InitIo(&io, name);
+ if (ioctl(fd_, DM_DEV_STATUS, &io) < 0) {
+ return std::nullopt;
+ }
+ return Info(io.flags);
+}
+
DmDeviceState DeviceMapper::GetState(const std::string& name) const {
struct dm_ioctl io;
InitIo(&io, name);
@@ -183,6 +171,24 @@
return DmDeviceState::SUSPENDED;
}
+bool DeviceMapper::ChangeState(const std::string& name, DmDeviceState state) {
+ if (state != DmDeviceState::SUSPENDED && state != DmDeviceState::ACTIVE) {
+ return false;
+ }
+
+ struct dm_ioctl io;
+ InitIo(&io, name);
+
+ if (state == DmDeviceState::SUSPENDED) io.flags = DM_SUSPEND_FLAG;
+
+ if (ioctl(fd_, DM_DEV_SUSPEND, &io) < 0) {
+ PLOG(ERROR) << "DM_DEV_SUSPEND "
+ << (state == DmDeviceState::SUSPENDED ? "suspend" : "resume") << " failed";
+ return false;
+ }
+ return true;
+}
+
bool DeviceMapper::CreateDevice(const std::string& name, const DmTable& table) {
std::string ignore_path;
if (!CreateDevice(name, table, &ignore_path, 0ms)) {
diff --git a/fs_mgr/libdm/dm_target.cpp b/fs_mgr/libdm/dm_target.cpp
index da1013e..9152677 100644
--- a/fs_mgr/libdm/dm_target.cpp
+++ b/fs_mgr/libdm/dm_target.cpp
@@ -16,6 +16,7 @@
#include "libdm/dm_target.h"
+#include <inttypes.h>
#include <stdio.h>
#include <sys/types.h>
@@ -149,6 +150,25 @@
return base_device_ + " " + cow_device_ + " " + mode + " " + std::to_string(chunk_size_);
}
+// Computes the percentage of complition for snapshot status.
+// @sectors_initial is the number of sectors_allocated stored right before
+// starting the merge.
+double DmTargetSnapshot::MergePercent(const DmTargetSnapshot::Status& status,
+ uint64_t sectors_initial) {
+ uint64_t s = status.sectors_allocated;
+ uint64_t t = status.total_sectors;
+ uint64_t m = status.metadata_sectors;
+ uint64_t i = sectors_initial == 0 ? t : sectors_initial;
+
+ if (t <= s || i <= s) {
+ return 0.0;
+ }
+ if (s == 0 || t == 0 || s <= m) {
+ return 100.0;
+ }
+ return 100.0 / (i - m) * (i - s);
+}
+
bool DmTargetSnapshot::ReportsOverflow(const std::string& target_type) {
DeviceMapper& dm = DeviceMapper::Instance();
DmTargetTypeInfo info;
@@ -165,35 +185,29 @@
}
bool DmTargetSnapshot::ParseStatusText(const std::string& text, Status* status) {
+ // Try to parse the line as it should be
+ int args = sscanf(text.c_str(), "%" PRIu64 "/%" PRIu64 " %" PRIu64, &status->sectors_allocated,
+ &status->total_sectors, &status->metadata_sectors);
+ if (args == 3) {
+ return true;
+ }
auto sections = android::base::Split(text, " ");
+ if (sections.size() == 0) {
+ LOG(ERROR) << "could not parse empty status";
+ return false;
+ }
+ // Error codes are: "Invalid", "Overflow" and "Merge failed"
if (sections.size() == 1) {
- // This is probably an error code, "Invalid" is possible as is "Overflow"
- // on 4.4+.
+ if (text == "Invalid" || text == "Overflow") {
+ status->error = text;
+ return true;
+ }
+ } else if (sections.size() == 2 && text == "Merge failed") {
status->error = text;
return true;
}
- if (sections.size() != 2) {
- LOG(ERROR) << "snapshot status should have two components";
- return false;
- }
- auto sector_info = android::base::Split(sections[0], "/");
- if (sector_info.size() != 2) {
- LOG(ERROR) << "snapshot sector info should have two components";
- return false;
- }
- if (!android::base::ParseUint(sections[1], &status->metadata_sectors)) {
- LOG(ERROR) << "could not parse metadata sectors";
- return false;
- }
- if (!android::base::ParseUint(sector_info[0], &status->sectors_allocated)) {
- LOG(ERROR) << "could not parse sectors allocated";
- return false;
- }
- if (!android::base::ParseUint(sector_info[1], &status->total_sectors)) {
- LOG(ERROR) << "could not parse total sectors";
- return false;
- }
- return true;
+ LOG(ERROR) << "could not parse snapshot status: wrong format";
+ return false;
}
std::string DmTargetCrypt::GetParameterString() const {
diff --git a/fs_mgr/libdm/dm_test.cpp b/fs_mgr/libdm/dm_test.cpp
index 7a834e2..eed21dc 100644
--- a/fs_mgr/libdm/dm_test.cpp
+++ b/fs_mgr/libdm/dm_test.cpp
@@ -103,9 +103,9 @@
ASSERT_TRUE(android::base::WriteFully(tmp1, message1, sizeof(message1)));
ASSERT_TRUE(android::base::WriteFully(tmp2, message2, sizeof(message2)));
- LoopDevice loop_a(tmp1);
+ LoopDevice loop_a(tmp1, 10s);
ASSERT_TRUE(loop_a.valid());
- LoopDevice loop_b(tmp2);
+ LoopDevice loop_b(tmp2, 10s);
ASSERT_TRUE(loop_b.valid());
// Define a 2-sector device, with each sector mapping to the first sector
@@ -166,6 +166,34 @@
ASSERT_TRUE(dev.Destroy());
}
+TEST(libdm, DmSuspendResume) {
+ unique_fd tmp1(CreateTempFile("file_suspend_resume", 512));
+ ASSERT_GE(tmp1, 0);
+
+ LoopDevice loop_a(tmp1, 10s);
+ ASSERT_TRUE(loop_a.valid());
+
+ DmTable table;
+ ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop_a.device(), 0));
+ ASSERT_TRUE(table.valid());
+
+ TempDevice dev("libdm-test-dm-suspend-resume", table);
+ ASSERT_TRUE(dev.valid());
+ ASSERT_FALSE(dev.path().empty());
+
+ auto& dm = DeviceMapper::Instance();
+
+ // Test Set and Get status of device.
+ vector<DeviceMapper::TargetInfo> targets;
+ ASSERT_EQ(dm.GetState(dev.name()), DmDeviceState::ACTIVE);
+
+ ASSERT_TRUE(dm.ChangeState(dev.name(), DmDeviceState::SUSPENDED));
+ ASSERT_EQ(dm.GetState(dev.name()), DmDeviceState::SUSPENDED);
+
+ ASSERT_TRUE(dm.ChangeState(dev.name(), DmDeviceState::ACTIVE));
+ ASSERT_EQ(dm.GetState(dev.name()), DmDeviceState::ACTIVE);
+}
+
TEST(libdm, DmVerityArgsAvb2) {
std::string device = "/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a";
std::string algorithm = "sha1";
@@ -255,9 +283,9 @@
cow_fd_ = CreateTempFile("cow_device", kCowDeviceSize);
ASSERT_GE(cow_fd_, 0);
- base_loop_ = std::make_unique<LoopDevice>(base_fd_);
+ base_loop_ = std::make_unique<LoopDevice>(base_fd_, 10s);
ASSERT_TRUE(base_loop_->valid());
- cow_loop_ = std::make_unique<LoopDevice>(cow_fd_);
+ cow_loop_ = std::make_unique<LoopDevice>(cow_fd_, 10s);
ASSERT_TRUE(cow_loop_->valid());
DmTable origin_table;
@@ -428,6 +456,87 @@
}
}
+TEST(libdm, ParseStatusText) {
+ DmTargetSnapshot::Status status;
+
+ // Bad inputs
+ EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("", &status));
+ EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("X", &status));
+ EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123", &status));
+ EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123/456", &status));
+ EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456", &status));
+ EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456", &status));
+ EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456 789", &status));
+ EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456/789", &status));
+ EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123/456/789", &status));
+ EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 / 456 789", &status));
+
+ // Good input
+ EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("123/456 789", &status));
+ EXPECT_EQ(status.sectors_allocated, 123);
+ EXPECT_EQ(status.total_sectors, 456);
+ EXPECT_EQ(status.metadata_sectors, 789);
+
+ // Known error codes
+ EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Invalid", &status));
+ EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Merge failed", &status));
+ EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Overflow", &status));
+}
+
+TEST(libdm, DmSnapshotMergePercent) {
+ DmTargetSnapshot::Status status;
+
+ // Correct input
+ status.sectors_allocated = 1000;
+ status.total_sectors = 1000;
+ status.metadata_sectors = 0;
+ EXPECT_LE(DmTargetSnapshot::MergePercent(status), 1.0);
+
+ status.sectors_allocated = 500;
+ status.total_sectors = 1000;
+ status.metadata_sectors = 0;
+ EXPECT_GE(DmTargetSnapshot::MergePercent(status), 49.0);
+ EXPECT_LE(DmTargetSnapshot::MergePercent(status), 51.0);
+
+ status.sectors_allocated = 0;
+ status.total_sectors = 1000;
+ status.metadata_sectors = 0;
+ EXPECT_GE(DmTargetSnapshot::MergePercent(status), 99.0);
+
+ status.sectors_allocated = 500;
+ status.total_sectors = 1000;
+ status.metadata_sectors = 500;
+ EXPECT_GE(DmTargetSnapshot::MergePercent(status), 99.0);
+
+ status.sectors_allocated = 500;
+ status.total_sectors = 1000;
+ status.metadata_sectors = 0;
+ EXPECT_LE(DmTargetSnapshot::MergePercent(status, 500), 1.0);
+ EXPECT_LE(DmTargetSnapshot::MergePercent(status, 1000), 51.0);
+ EXPECT_GE(DmTargetSnapshot::MergePercent(status, 1000), 49.0);
+
+ // Robustness
+ status.sectors_allocated = 2000;
+ status.total_sectors = 1000;
+ status.metadata_sectors = 0;
+ EXPECT_LE(DmTargetSnapshot::MergePercent(status), 0.0);
+
+ status.sectors_allocated = 2000;
+ status.total_sectors = 1000;
+ status.metadata_sectors = 2000;
+ EXPECT_LE(DmTargetSnapshot::MergePercent(status), 0.0);
+
+ status.sectors_allocated = 2000;
+ status.total_sectors = 0;
+ status.metadata_sectors = 2000;
+ EXPECT_LE(DmTargetSnapshot::MergePercent(status), 0.0);
+
+ status.sectors_allocated = 1000;
+ status.total_sectors = 0;
+ status.metadata_sectors = 1000;
+ EXPECT_LE(DmTargetSnapshot::MergePercent(status, 0), 0.0);
+}
+
TEST(libdm, CryptArgs) {
DmTargetCrypt target1(0, 512, "sha1", "abcdefgh", 50, "/dev/loop0", 100);
ASSERT_EQ(target1.name(), "crypt");
diff --git a/fs_mgr/libdm/include/libdm/dm.h b/fs_mgr/libdm/include/libdm/dm.h
index 9c0c2f3..c6b37cf 100644
--- a/fs_mgr/libdm/include/libdm/dm.h
+++ b/fs_mgr/libdm/include/libdm/dm.h
@@ -27,6 +27,7 @@
#include <chrono>
#include <memory>
+#include <optional>
#include <string>
#include <utility>
#include <vector>
@@ -70,15 +71,37 @@
uint64_t dev_;
};
+ class Info {
+ uint32_t flags_;
+
+ public:
+ explicit Info(uint32_t flags) : flags_(flags) {}
+
+ bool IsActiveTablePresent() const { return flags_ & DM_ACTIVE_PRESENT_FLAG; }
+ bool IsBufferFull() const { return flags_ & DM_BUFFER_FULL_FLAG; }
+ bool IsInactiveTablePresent() const { return flags_ & DM_INACTIVE_PRESENT_FLAG; }
+ bool IsReadOnly() const { return flags_ & DM_READONLY_FLAG; }
+ bool IsSuspended() const { return flags_ & DM_SUSPEND_FLAG; }
+ };
+
// Removes a device mapper device with the given name.
// Returns 'true' on success, false otherwise.
bool DeleteDevice(const std::string& name);
+ // Fetches and returns the complete state of the underlying device mapper
+ // device with given name.
+ std::optional<Info> GetDetailedInfo(const std::string& name) const;
+
// Returns the current state of the underlying device mapper device
// with given name.
// One of INVALID, SUSPENDED or ACTIVE.
DmDeviceState GetState(const std::string& name) const;
+ // Puts the given device to the specified status, which must be either:
+ // - SUSPENDED: suspend the device, or
+ // - ACTIVE: resumes the device.
+ bool ChangeState(const std::string& name, DmDeviceState state);
+
// Creates a device, loads the given table, and activates it. If the device
// is not able to be activated, it is destroyed, and false is returned.
// After creation, |path| contains the result of calling
diff --git a/fs_mgr/libdm/include/libdm/dm_target.h b/fs_mgr/libdm/include/libdm/dm_target.h
index 722922d..ab7c2db 100644
--- a/fs_mgr/libdm/include/libdm/dm_target.h
+++ b/fs_mgr/libdm/include/libdm/dm_target.h
@@ -216,6 +216,7 @@
std::string error;
};
+ static double MergePercent(const Status& status, uint64_t sectors_initial = 0);
static bool ParseStatusText(const std::string& text, Status* status);
static bool ReportsOverflow(const std::string& target_type);
diff --git a/fs_mgr/libdm/include/libdm/loop_control.h b/fs_mgr/libdm/include/libdm/loop_control.h
index 6b4c2d8..eeed6b5 100644
--- a/fs_mgr/libdm/include/libdm/loop_control.h
+++ b/fs_mgr/libdm/include/libdm/loop_control.h
@@ -17,6 +17,7 @@
#ifndef _LIBDM_LOOP_CONTROL_H_
#define _LIBDM_LOOP_CONTROL_H_
+#include <chrono>
#include <string>
#include <android-base/unique_fd.h>
@@ -29,8 +30,15 @@
LoopControl();
// Attaches the file specified by 'file_fd' to the loop device specified
- // by 'loopdev'
- bool Attach(int file_fd, std::string* loopdev) const;
+ // by 'loopdev'. It is possible that in between allocating and attaching
+ // a loop device, another process attaches to the chosen loop device. If
+ // this happens, Attach() will retry for up to |timeout_ms|. The timeout
+ // should not be zero.
+ //
+ // The caller does not have to call WaitForFile(); it is implicitly called.
+ // The given |timeout_ms| covers both potential sources of timeout.
+ bool Attach(int file_fd, const std::chrono::milliseconds& timeout_ms,
+ std::string* loopdev) const;
// Detach the loop device given by 'loopdev' from the attached backing file.
bool Detach(const std::string& loopdev) const;
@@ -56,13 +64,13 @@
public:
// Create a loop device for the given file descriptor. It is closed when
// LoopDevice is destroyed only if auto_close is true.
- LoopDevice(int fd, bool auto_close = false);
+ LoopDevice(int fd, const std::chrono::milliseconds& timeout_ms, bool auto_close = false);
// Create a loop device for the given file path. It will be opened for
// reading and writing and closed when the loop device is detached.
- explicit LoopDevice(const std::string& path);
+ LoopDevice(const std::string& path, const std::chrono::milliseconds& timeout_ms);
~LoopDevice();
- bool valid() const { return fd_ != -1 && !device_.empty(); }
+ bool valid() const { return valid_; }
const std::string& device() const { return device_; }
LoopDevice(const LoopDevice&) = delete;
@@ -71,12 +79,13 @@
LoopDevice(LoopDevice&&) = default;
private:
- void Init();
+ void Init(const std::chrono::milliseconds& timeout_ms);
android::base::unique_fd fd_;
bool owns_fd_;
std::string device_;
LoopControl control_;
+ bool valid_ = false;
};
} // namespace dm
diff --git a/fs_mgr/libdm/loop_control.cpp b/fs_mgr/libdm/loop_control.cpp
index 16bf4b0..edc9a45 100644
--- a/fs_mgr/libdm/loop_control.cpp
+++ b/fs_mgr/libdm/loop_control.cpp
@@ -27,6 +27,8 @@
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>
+#include "utility.h"
+
namespace android {
namespace dm {
@@ -37,21 +39,40 @@
}
}
-bool LoopControl::Attach(int file_fd, std::string* loopdev) const {
- if (!FindFreeLoopDevice(loopdev)) {
- LOG(ERROR) << "Failed to attach, no free loop devices";
- return false;
- }
+bool LoopControl::Attach(int file_fd, const std::chrono::milliseconds& timeout_ms,
+ std::string* loopdev) const {
+ auto start_time = std::chrono::steady_clock::now();
+ auto condition = [&]() -> WaitResult {
+ if (!FindFreeLoopDevice(loopdev)) {
+ LOG(ERROR) << "Failed to attach, no free loop devices";
+ return WaitResult::Fail;
+ }
- android::base::unique_fd loop_fd(TEMP_FAILURE_RETRY(open(loopdev->c_str(), O_RDWR | O_CLOEXEC)));
- if (loop_fd < 0) {
- PLOG(ERROR) << "Failed to open: " << *loopdev;
- return false;
- }
+ auto now = std::chrono::steady_clock::now();
+ auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
+ if (!WaitForFile(*loopdev, timeout_ms - time_elapsed)) {
+ LOG(ERROR) << "Timed out waiting for path: " << *loopdev;
+ return WaitResult::Fail;
+ }
- int rc = ioctl(loop_fd, LOOP_SET_FD, file_fd);
- if (rc < 0) {
- PLOG(ERROR) << "Failed LOOP_SET_FD";
+ android::base::unique_fd loop_fd(
+ TEMP_FAILURE_RETRY(open(loopdev->c_str(), O_RDWR | O_CLOEXEC)));
+ if (loop_fd < 0) {
+ PLOG(ERROR) << "Failed to open: " << *loopdev;
+ return WaitResult::Fail;
+ }
+
+ if (int rc = ioctl(loop_fd, LOOP_SET_FD, file_fd); rc == 0) {
+ return WaitResult::Done;
+ }
+ if (errno != EBUSY) {
+ PLOG(ERROR) << "Failed LOOP_SET_FD";
+ return WaitResult::Fail;
+ }
+ return WaitResult::Wait;
+ };
+ if (!WaitForCondition(condition, timeout_ms)) {
+ LOG(ERROR) << "Timed out trying to acquire a loop device";
return false;
}
return true;
@@ -112,17 +133,19 @@
return true;
}
-LoopDevice::LoopDevice(int fd, bool auto_close) : fd_(fd), owns_fd_(auto_close) {
- Init();
+LoopDevice::LoopDevice(int fd, const std::chrono::milliseconds& timeout_ms, bool auto_close)
+ : fd_(fd), owns_fd_(auto_close) {
+ Init(timeout_ms);
}
-LoopDevice::LoopDevice(const std::string& path) : fd_(-1), owns_fd_(true) {
+LoopDevice::LoopDevice(const std::string& path, const std::chrono::milliseconds& timeout_ms)
+ : fd_(-1), owns_fd_(true) {
fd_.reset(open(path.c_str(), O_RDWR | O_CLOEXEC));
if (fd_ < -1) {
PLOG(ERROR) << "open failed for " << path;
return;
}
- Init();
+ Init(timeout_ms);
}
LoopDevice::~LoopDevice() {
@@ -134,8 +157,8 @@
}
}
-void LoopDevice::Init() {
- control_.Attach(fd_, &device_);
+void LoopDevice::Init(const std::chrono::milliseconds& timeout_ms) {
+ valid_ = control_.Attach(fd_, timeout_ms, &device_);
}
} // namespace dm
diff --git a/fs_mgr/libdm/loop_control_test.cpp b/fs_mgr/libdm/loop_control_test.cpp
index 08bdc00..0749f26 100644
--- a/fs_mgr/libdm/loop_control_test.cpp
+++ b/fs_mgr/libdm/loop_control_test.cpp
@@ -53,7 +53,7 @@
unique_fd fd = TempFile();
ASSERT_GE(fd, 0);
- LoopDevice loop(fd);
+ LoopDevice loop(fd, 10s);
ASSERT_TRUE(loop.valid());
char buffer[6];
diff --git a/fs_mgr/libdm/utility.cpp b/fs_mgr/libdm/utility.cpp
new file mode 100644
index 0000000..eccf2fb
--- /dev/null
+++ b/fs_mgr/libdm/utility.cpp
@@ -0,0 +1,56 @@
+// 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 "utility.h"
+
+#include <errno.h>
+#include <unistd.h>
+
+#include <thread>
+
+using namespace std::literals;
+
+namespace android {
+namespace dm {
+
+bool WaitForCondition(const std::function<WaitResult()>& condition,
+ const std::chrono::milliseconds& timeout_ms) {
+ auto start_time = std::chrono::steady_clock::now();
+ while (true) {
+ auto result = condition();
+ if (result == WaitResult::Done) return true;
+ if (result == WaitResult::Fail) return false;
+
+ std::this_thread::sleep_for(20ms);
+
+ auto now = std::chrono::steady_clock::now();
+ auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
+ if (time_elapsed > timeout_ms) return false;
+ }
+}
+
+bool WaitForFile(const std::string& path, const std::chrono::milliseconds& timeout_ms) {
+ auto condition = [&]() -> WaitResult {
+ // If the file exists but returns EPERM or something, we consider the
+ // condition met.
+ if (access(path.c_str(), F_OK) != 0) {
+ if (errno == ENOENT) return WaitResult::Wait;
+ }
+ return WaitResult::Done;
+ };
+ return WaitForCondition(condition, timeout_ms);
+}
+
+} // namespace dm
+} // namespace android
diff --git a/fs_mgr/libdm/utility.h b/fs_mgr/libdm/utility.h
new file mode 100644
index 0000000..f1dce9e
--- /dev/null
+++ b/fs_mgr/libdm/utility.h
@@ -0,0 +1,30 @@
+// 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 <functional>
+
+namespace android {
+namespace dm {
+
+enum class WaitResult { Wait, Done, Fail };
+
+bool WaitForFile(const std::string& path, const std::chrono::milliseconds& timeout_ms);
+bool WaitForCondition(const std::function<WaitResult()>& condition,
+ const std::chrono::milliseconds& timeout_ms);
+
+} // namespace dm
+} // namespace android
diff --git a/fs_mgr/libfiemap_writer/.clang-format b/fs_mgr/libfiemap_writer/.clang-format
deleted file mode 120000
index 8b770a1..0000000
--- a/fs_mgr/libfiemap_writer/.clang-format
+++ /dev/null
@@ -1 +0,0 @@
-../../.clang-format-4
\ No newline at end of file
diff --git a/fs_mgr/libfiemap_writer/Android.bp b/fs_mgr/libfiemap_writer/Android.bp
deleted file mode 100644
index ed209aa..0000000
--- a/fs_mgr/libfiemap_writer/Android.bp
+++ /dev/null
@@ -1,64 +0,0 @@
-//
-// Copyright (C) 2018 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-cc_library_static {
- name: "libfiemap_writer",
- defaults: ["fs_mgr_defaults"],
- recovery_available: true,
- export_include_dirs: ["include"],
- cflags: [
- "-D_FILE_OFFSET_BITS=64",
- ],
-
- srcs: [
- "fiemap_writer.cpp",
- "split_fiemap_writer.cpp",
- "utility.cpp",
- ],
-
- static_libs: [
- "libdm",
- "libext4_utils",
- ],
-
- header_libs: [
- "libbase_headers",
- "liblog_headers",
- ],
-}
-
-cc_test {
- name: "fiemap_writer_test",
- cflags: [
- "-D_FILE_OFFSET_BITS=64",
- ],
- static_libs: [
- "libbase",
- "libdm",
- "libfiemap_writer",
- "liblog",
- ],
-
- data: [
- "testdata/unaligned_file",
- "testdata/file_4k",
- "testdata/file_32k",
- ],
-
- srcs: [
- "fiemap_writer_test.cpp",
- ],
-}
diff --git a/fs_mgr/libfiemap_writer/Android.mk b/fs_mgr/libfiemap_writer/Android.mk
deleted file mode 100644
index 3c07b8e..0000000
--- a/fs_mgr/libfiemap_writer/Android.mk
+++ /dev/null
@@ -1,22 +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.
-#
-
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := VtsFiemapWriterTest
--include test/vts/tools/build/Android.host_config.mk
diff --git a/fs_mgr/libfiemap_writer/AndroidTest.xml b/fs_mgr/libfiemap_writer/AndroidTest.xml
deleted file mode 100644
index 08cff0e..0000000
--- a/fs_mgr/libfiemap_writer/AndroidTest.xml
+++ /dev/null
@@ -1,29 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- 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.
--->
-<configuration description="Config for VTS VtsFiemapWriterTest">
- <option name="config-descriptor:metadata" key="plan" value="vts-kernel" />
- <target_preparer class="com.android.compatibility.common.tradefed.targetprep.VtsFilePusher">
- <option name="abort-on-push-failure" value="false"/>
- <option name="push-group" value="HostDrivenTest.push"/>
- </target_preparer>
- <test class="com.android.tradefed.testtype.VtsMultiDeviceTest">
- <option name="test-module-name" value="VtsFiemapWriterTest"/>
- <option name="binary-test-source" value="_32bit::DATA/nativetest/fiemap_writer_test/fiemap_writer_test" />
- <option name="binary-test-source" value="_64bit::DATA/nativetest64/fiemap_writer_test/fiemap_writer_test" />
- <option name="binary-test-type" value="gtest"/>
- <option name="test-timeout" value="1m"/>
- </test>
-</configuration>
diff --git a/fs_mgr/libfiemap_writer/fiemap_writer.cpp b/fs_mgr/libfiemap_writer/fiemap_writer.cpp
deleted file mode 100644
index 0a3ba6c..0000000
--- a/fs_mgr/libfiemap_writer/fiemap_writer.cpp
+++ /dev/null
@@ -1,745 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <libfiemap_writer/fiemap_writer.h>
-
-#include <dirent.h>
-#include <fcntl.h>
-#include <linux/fs.h>
-#include <stdio.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/sysmacros.h>
-#include <sys/types.h>
-#include <sys/vfs.h>
-#include <unistd.h>
-
-#include <limits>
-#include <string>
-#include <utility>
-#include <vector>
-
-#include <android-base/file.h>
-#include <android-base/logging.h>
-#include <android-base/stringprintf.h>
-#include <android-base/strings.h>
-#include <android-base/unique_fd.h>
-#include <libdm/dm.h>
-
-namespace android {
-namespace fiemap_writer {
-
-using namespace android::dm;
-
-// We are expecting no more than 512 extents in a fiemap of the file we create.
-// If we find more, then it is treated as error for now.
-static constexpr const uint32_t kMaxExtents = 512;
-
-// TODO: Fallback to using fibmap if FIEMAP_EXTENT_MERGED is set.
-static constexpr const uint32_t kUnsupportedExtentFlags =
- FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_UNWRITTEN | FIEMAP_EXTENT_DELALLOC |
- FIEMAP_EXTENT_NOT_ALIGNED | FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_DATA_TAIL |
- FIEMAP_EXTENT_UNWRITTEN | FIEMAP_EXTENT_SHARED | FIEMAP_EXTENT_MERGED;
-
-// Large file support must be enabled.
-static_assert(sizeof(off_t) == sizeof(uint64_t));
-
-static inline void cleanup(const std::string& file_path, bool created) {
- if (created) {
- unlink(file_path.c_str());
- }
-}
-
-static bool BlockDeviceToName(uint32_t major, uint32_t minor, std::string* bdev_name) {
- // The symlinks in /sys/dev/block point to the block device node under /sys/device/..
- // The directory name in the target corresponds to the name of the block device. We use
- // that to extract the block device name.
- // e.g for block device name 'ram0', there exists a symlink named '1:0' in /sys/dev/block as
- // follows.
- // 1:0 -> ../../devices/virtual/block/ram0
- std::string sysfs_path = ::android::base::StringPrintf("/sys/dev/block/%u:%u", major, minor);
- std::string sysfs_bdev;
-
- if (!::android::base::Readlink(sysfs_path, &sysfs_bdev)) {
- PLOG(ERROR) << "Failed to read link at: " << sysfs_path;
- return false;
- }
-
- *bdev_name = ::android::base::Basename(sysfs_bdev);
- // Paranoid sanity check to make sure we just didn't get the
- // input in return as-is.
- if (sysfs_bdev == *bdev_name) {
- LOG(ERROR) << "Malformed symlink for block device: " << sysfs_bdev;
- return false;
- }
-
- return true;
-}
-
-static bool ValidateDmTarget(const DeviceMapper::TargetInfo& target) {
- const auto& entry = target.spec;
- if (entry.sector_start != 0) {
- LOG(INFO) << "Stopping at target with non-zero starting sector";
- return false;
- }
-
- auto target_type = DeviceMapper::GetTargetType(entry);
- if (target_type == "bow" || target_type == "default-key" || target_type == "crypt") {
- return true;
- }
- if (target_type == "linear") {
- auto pieces = android::base::Split(target.data, " ");
- if (pieces[1] != "0") {
- LOG(INFO) << "Stopping at complex linear target with non-zero starting sector: "
- << pieces[1];
- return false;
- }
- return true;
- }
-
- LOG(INFO) << "Stopping at complex target type " << target_type;
- return false;
-}
-
-static bool DeviceMapperStackPop(const std::string& bdev, std::string* bdev_raw) {
- *bdev_raw = bdev;
-
- if (!::android::base::StartsWith(bdev, "dm-")) {
- // We are at the bottom of the device mapper stack.
- return true;
- }
-
- // Get the device name.
- auto dm_name_file = "/sys/block/" + bdev + "/dm/name";
- std::string dm_name;
- if (!android::base::ReadFileToString(dm_name_file, &dm_name)) {
- PLOG(ERROR) << "Could not read file: " << dm_name_file;
- return false;
- }
- dm_name = android::base::Trim(dm_name);
-
- auto& dm = DeviceMapper::Instance();
- std::vector<DeviceMapper::TargetInfo> table;
- if (!dm.GetTableInfo(dm_name, &table)) {
- LOG(ERROR) << "Could not read device-mapper table for " << dm_name << " at " << bdev;
- return false;
- }
-
- // The purpose of libfiemap_writer is to provide an extent-based view into
- // a file. This is difficult if devices are not layered in a 1:1 manner;
- // we would have to translate and break up extents based on the actual
- // block mapping. Since this is too complex, we simply stop processing
- // the device-mapper stack if we encounter a complex case.
- //
- // It is up to the caller to decide whether stopping at a virtual block
- // device is allowable. In most cases it is not, because we want either
- // "userdata" or an external volume. It is useful for tests however.
- // Callers can check by comparing the device number to that of userdata,
- // or by checking whether is a device-mapper node.
- if (table.size() > 1) {
- LOG(INFO) << "Stopping at complex table for " << dm_name << " at " << bdev;
- return true;
- }
- if (!ValidateDmTarget(table[0])) {
- return true;
- }
-
- auto dm_leaf_dir = "/sys/block/" + bdev + "/slaves";
- auto d = std::unique_ptr<DIR, decltype(&closedir)>(opendir(dm_leaf_dir.c_str()), closedir);
- if (d == nullptr) {
- PLOG(ERROR) << "Failed to open: " << dm_leaf_dir;
- return false;
- }
-
- struct dirent* de;
- uint32_t num_leaves = 0;
- std::string bdev_next = "";
- while ((de = readdir(d.get())) != nullptr) {
- if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
- continue;
- }
-
- // We set the first name we find here
- if (bdev_next.empty()) {
- bdev_next = de->d_name;
- }
- num_leaves++;
- }
-
- // if we have more than one leaves, we return immediately. We can't continue to create the
- // file since we don't know how to write it out using fiemap, so it will be readable via the
- // underlying block devices later. The reader will also have to construct the same device mapper
- // target in order read the file out.
- if (num_leaves > 1) {
- LOG(ERROR) << "Found " << num_leaves << " leaf block devices under device mapper device "
- << bdev;
- return false;
- }
-
- // recursively call with the block device we found in order to pop the device mapper stack.
- return DeviceMapperStackPop(bdev_next, bdev_raw);
-}
-
-bool FiemapWriter::GetBlockDeviceForFile(const std::string& file_path, std::string* bdev_path,
- bool* uses_dm) {
- struct stat sb;
- if (stat(file_path.c_str(), &sb)) {
- PLOG(ERROR) << "Failed to get stat for: " << file_path;
- return false;
- }
-
- std::string bdev;
- if (!BlockDeviceToName(major(sb.st_dev), minor(sb.st_dev), &bdev)) {
- LOG(ERROR) << "Failed to get block device name for " << major(sb.st_dev) << ":"
- << minor(sb.st_dev);
- return false;
- }
-
- std::string bdev_raw;
- if (!DeviceMapperStackPop(bdev, &bdev_raw)) {
- LOG(ERROR) << "Failed to get the bottom of the device mapper stack for device: " << bdev;
- return false;
- }
-
- if (uses_dm) {
- *uses_dm = (bdev_raw != bdev);
- }
-
- LOG(DEBUG) << "Popped device (" << bdev_raw << ") from device mapper stack starting with ("
- << bdev << ")";
-
- *bdev_path = ::android::base::StringPrintf("/dev/block/%s", bdev_raw.c_str());
-
- // Make sure we are talking to a block device before calling it a success.
- if (stat(bdev_path->c_str(), &sb)) {
- PLOG(ERROR) << "Failed to get stat for block device: " << *bdev_path;
- return false;
- }
-
- if ((sb.st_mode & S_IFMT) != S_IFBLK) {
- PLOG(ERROR) << "File: " << *bdev_path << " is not a block device";
- return false;
- }
-
- return true;
-}
-
-static bool GetBlockDeviceSize(int bdev_fd, const std::string& bdev_path, uint64_t* bdev_size) {
- uint64_t size_in_bytes = 0;
- if (ioctl(bdev_fd, BLKGETSIZE64, &size_in_bytes)) {
- PLOG(ERROR) << "Failed to get total size for: " << bdev_path;
- return false;
- }
-
- *bdev_size = size_in_bytes;
-
- return true;
-}
-
-static uint64_t GetFileSize(const std::string& file_path) {
- struct stat sb;
- if (stat(file_path.c_str(), &sb)) {
- PLOG(ERROR) << "Failed to get size for file: " << file_path;
- return 0;
- }
-
- return sb.st_size;
-}
-
-static bool PerformFileChecks(const std::string& file_path, uint64_t file_size, uint64_t* blocksz,
- uint32_t* fs_type) {
- struct statfs64 sfs;
- if (statfs64(file_path.c_str(), &sfs)) {
- PLOG(ERROR) << "Failed to read file system status at: " << file_path;
- return false;
- }
-
- if (!sfs.f_bsize) {
- LOG(ERROR) << "Unsupported block size: " << sfs.f_bsize;
- return false;
- }
-
- // Check if the filesystem is of supported types.
- // Only ext4, f2fs, and vfat are tested and supported.
- switch (sfs.f_type) {
- case EXT4_SUPER_MAGIC:
- case F2FS_SUPER_MAGIC:
- case MSDOS_SUPER_MAGIC:
- break;
- default:
- LOG(ERROR) << "Unsupported file system type: 0x" << std::hex << sfs.f_type;
- return false;
- }
-
- uint64_t available_bytes = sfs.f_bsize * sfs.f_bavail;
- if (available_bytes <= file_size) {
- LOG(ERROR) << "Not enough free space in file system to create file of size : " << file_size;
- return false;
- }
-
- *blocksz = sfs.f_bsize;
- *fs_type = sfs.f_type;
- return true;
-}
-
-static bool FallocateFallback(int file_fd, uint64_t block_size, uint64_t file_size,
- const std::string& file_path,
- const std::function<bool(uint64_t, uint64_t)>& on_progress) {
- // Even though this is much faster than writing zeroes, it is still slow
- // enough that we need to fire the progress callback periodically. To
- // easily achieve this, we seek in chunks. We use 1000 chunks since
- // normally we only fire the callback on 1/1000th increments.
- uint64_t bytes_per_chunk = std::max(file_size / 1000, block_size);
-
- // Seek just to the end of each chunk and write a single byte, causing
- // the filesystem to allocate blocks.
- off_t cursor = 0;
- off_t end = static_cast<off_t>(file_size);
- while (cursor < end) {
- cursor = std::min(static_cast<off_t>(cursor + bytes_per_chunk), end);
- auto rv = TEMP_FAILURE_RETRY(lseek(file_fd, cursor - 1, SEEK_SET));
- if (rv < 0) {
- PLOG(ERROR) << "Failed to lseek " << file_path;
- return false;
- }
- if (rv != cursor - 1) {
- LOG(ERROR) << "Seek returned wrong offset " << rv << " for file " << file_path;
- return false;
- }
- char buffer[] = {0};
- if (!android::base::WriteFully(file_fd, buffer, 1)) {
- PLOG(ERROR) << "Write failed: " << file_path;
- return false;
- }
- if (on_progress && !on_progress(cursor, file_size)) {
- return false;
- }
- }
- return true;
-}
-
-static bool AllocateFile(int file_fd, const std::string& file_path, uint64_t blocksz,
- uint64_t file_size, unsigned int fs_type,
- std::function<bool(uint64_t, uint64_t)> on_progress) {
- // Reserve space for the file on the file system and write it out to make sure the extents
- // don't come back unwritten. Return from this function with the kernel file offset set to 0.
- // If the filesystem is f2fs, then we also PIN the file on disk to make sure the blocks
- // aren't moved around.
- switch (fs_type) {
- case EXT4_SUPER_MAGIC:
- case F2FS_SUPER_MAGIC:
- if (fallocate(file_fd, FALLOC_FL_ZERO_RANGE, 0, file_size)) {
- PLOG(ERROR) << "Failed to allocate space for file: " << file_path
- << " size: " << file_size;
- return false;
- }
- break;
- case MSDOS_SUPER_MAGIC:
- // fallocate() is not supported, and not needed, since VFAT does not support holes.
- // Instead we can perform a much faster allocation.
- return FallocateFallback(file_fd, blocksz, file_size, file_path, on_progress);
- default:
- LOG(ERROR) << "Missing fallocate() support for file system " << fs_type;
- return false;
- }
-
- // write zeroes in 'blocksz' byte increments until we reach file_size to make sure the data
- // blocks are actually written to by the file system and thus getting rid of the holes in the
- // file.
- auto buffer = std::unique_ptr<void, decltype(&free)>(calloc(1, blocksz), free);
- if (buffer == nullptr) {
- LOG(ERROR) << "failed to allocate memory for writing file";
- return false;
- }
-
- off64_t offset = lseek64(file_fd, 0, SEEK_SET);
- if (offset < 0) {
- PLOG(ERROR) << "Failed to seek at the beginning of : " << file_path;
- return false;
- }
-
- int permille = -1;
- while (offset < file_size) {
- if (!::android::base::WriteFully(file_fd, buffer.get(), blocksz)) {
- PLOG(ERROR) << "Failed to write" << blocksz << " bytes at offset" << offset
- << " in file " << file_path;
- return false;
- }
-
- offset += blocksz;
-
- // Don't invoke the callback every iteration - wait until a significant
- // chunk (here, 1/1000th) of the data has been processed.
- int new_permille = (static_cast<uint64_t>(offset) * 1000) / file_size;
- if (new_permille != permille && static_cast<uint64_t>(offset) != file_size) {
- if (on_progress && !on_progress(offset, file_size)) {
- return false;
- }
- permille = new_permille;
- }
- }
-
- if (lseek64(file_fd, 0, SEEK_SET) < 0) {
- PLOG(ERROR) << "Failed to reset offset at the beginning of : " << file_path;
- return false;
- }
-
- // flush all writes here ..
- if (fsync(file_fd)) {
- PLOG(ERROR) << "Failed to synchronize written file:" << file_path;
- return false;
- }
-
- // Send one last progress notification.
- if (on_progress && !on_progress(file_size, file_size)) {
- return false;
- }
- return true;
-}
-
-static bool PinFile(int file_fd, const std::string& file_path, uint32_t fs_type) {
- if (fs_type != F2FS_SUPER_MAGIC) {
- // No pinning necessary for ext4/msdos. The blocks, once allocated, are
- // expected to be fixed.
- return true;
- }
-
-// F2FS-specific ioctl
-// It requires the below kernel commit merged in v4.16-rc1.
-// 1ad71a27124c ("f2fs: add an ioctl to disable GC for specific file")
-// In android-4.4,
-// 56ee1e817908 ("f2fs: updates on v4.16-rc1")
-// In android-4.9,
-// 2f17e34672a8 ("f2fs: updates on v4.16-rc1")
-// In android-4.14,
-// ce767d9a55bc ("f2fs: updates on v4.16-rc1")
-#ifndef F2FS_IOC_SET_PIN_FILE
-#ifndef F2FS_IOCTL_MAGIC
-#define F2FS_IOCTL_MAGIC 0xf5
-#endif
-#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
-#endif
-
- uint32_t pin_status = 1;
- int error = ioctl(file_fd, F2FS_IOC_SET_PIN_FILE, &pin_status);
- if (error < 0) {
- if ((errno == ENOTTY) || (errno == ENOTSUP)) {
- PLOG(ERROR) << "Failed to pin file, not supported by kernel: " << file_path;
- } else {
- PLOG(ERROR) << "Failed to pin file: " << file_path;
- }
- return false;
- }
-
- return true;
-}
-
-static bool IsFilePinned(int file_fd, const std::string& file_path, uint32_t fs_type) {
- if (fs_type != F2FS_SUPER_MAGIC) {
- // No pinning necessary for ext4 or vfat. The blocks, once allocated,
- // are expected to be fixed.
- return true;
- }
-
-// F2FS-specific ioctl
-// It requires the below kernel commit merged in v4.16-rc1.
-// 1ad71a27124c ("f2fs: add an ioctl to disable GC for specific file")
-// In android-4.4,
-// 56ee1e817908 ("f2fs: updates on v4.16-rc1")
-// In android-4.9,
-// 2f17e34672a8 ("f2fs: updates on v4.16-rc1")
-// In android-4.14,
-// ce767d9a55bc ("f2fs: updates on v4.16-rc1")
-#ifndef F2FS_IOC_GET_PIN_FILE
-#ifndef F2FS_IOCTL_MAGIC
-#define F2FS_IOCTL_MAGIC 0xf5
-#endif
-#define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
-#endif
-
- // f2fs: export FS_NOCOW_FL flag to user
- uint32_t flags;
- int error = ioctl(file_fd, FS_IOC_GETFLAGS, &flags);
- if (error < 0) {
- if ((errno == ENOTTY) || (errno == ENOTSUP)) {
- PLOG(ERROR) << "Failed to get flags, not supported by kernel: " << file_path;
- } else {
- PLOG(ERROR) << "Failed to get flags: " << file_path;
- }
- return false;
- }
- if (!(flags & FS_NOCOW_FL)) {
- LOG(ERROR) << "It is not pinned: " << file_path;
- return false;
- }
-
- // F2FS_IOC_GET_PIN_FILE returns the number of blocks moved.
- uint32_t moved_blocks_nr;
- error = ioctl(file_fd, F2FS_IOC_GET_PIN_FILE, &moved_blocks_nr);
- if (error < 0) {
- if ((errno == ENOTTY) || (errno == ENOTSUP)) {
- PLOG(ERROR) << "Failed to get file pin status, not supported by kernel: " << file_path;
- } else {
- PLOG(ERROR) << "Failed to get file pin status: " << file_path;
- }
- return false;
- }
-
- if (moved_blocks_nr) {
- LOG(ERROR) << moved_blocks_nr << " blocks moved in file " << file_path;
- }
- return moved_blocks_nr == 0;
-}
-
-bool FiemapWriter::HasPinnedExtents(const std::string& file_path) {
- android::base::unique_fd fd(open(file_path.c_str(), O_NOFOLLOW | O_CLOEXEC | O_RDONLY));
- if (fd < 0) {
- PLOG(ERROR) << "open: " << file_path;
- return false;
- }
-
- struct statfs64 sfs;
- if (fstatfs64(fd, &sfs)) {
- PLOG(ERROR) << "fstatfs64: " << file_path;
- return false;
- }
- return IsFilePinned(fd, file_path, sfs.f_type);
-}
-
-static bool ReadFiemap(int file_fd, const std::string& file_path,
- std::vector<struct fiemap_extent>* extents) {
- uint64_t fiemap_size =
- sizeof(struct fiemap_extent) + kMaxExtents * sizeof(struct fiemap_extent);
- auto buffer = std::unique_ptr<void, decltype(&free)>(calloc(1, fiemap_size), free);
- if (buffer == nullptr) {
- LOG(ERROR) << "Failed to allocate memory for fiemap";
- return false;
- }
-
- struct fiemap* fiemap = reinterpret_cast<struct fiemap*>(buffer.get());
- fiemap->fm_start = 0;
- fiemap->fm_length = UINT64_MAX;
- // make sure file is synced to disk before we read the fiemap
- fiemap->fm_flags = FIEMAP_FLAG_SYNC;
- fiemap->fm_extent_count = kMaxExtents;
-
- if (ioctl(file_fd, FS_IOC_FIEMAP, fiemap)) {
- PLOG(ERROR) << "Failed to get FIEMAP from the kernel for file: " << file_path;
- return false;
- }
-
- if (fiemap->fm_mapped_extents == 0) {
- LOG(ERROR) << "File " << file_path << " has zero extents";
- return false;
- }
-
- // Iterate through each extent read and make sure its valid before adding it to the vector
- bool last_extent_seen = false;
- struct fiemap_extent* extent = &fiemap->fm_extents[0];
- for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++, extent++) {
- // LogExtent(i + 1, *extent);
- if (extent->fe_flags & kUnsupportedExtentFlags) {
- LOG(ERROR) << "Extent " << i + 1 << " of file " << file_path
- << " has unsupported flags";
- extents->clear();
- return false;
- }
-
- if (extent->fe_flags & FIEMAP_EXTENT_LAST) {
- last_extent_seen = true;
- if (i != (fiemap->fm_mapped_extents - 1)) {
- LOG(WARNING) << "Extents are being received out-of-order";
- }
- }
- extents->emplace_back(std::move(*extent));
- }
-
- if (!last_extent_seen) {
- // The file is possibly too fragmented.
- if (fiemap->fm_mapped_extents == kMaxExtents) {
- LOG(ERROR) << "File is too fragmented, needs more than " << kMaxExtents << " extents.";
- }
- extents->clear();
- }
-
- return last_extent_seen;
-}
-
-static bool ReadFibmap(int file_fd, const std::string& file_path,
- std::vector<struct fiemap_extent>* extents) {
- struct stat s;
- if (fstat(file_fd, &s)) {
- PLOG(ERROR) << "Failed to stat " << file_path;
- return false;
- }
-
- unsigned int blksize;
- if (ioctl(file_fd, FIGETBSZ, &blksize) < 0) {
- PLOG(ERROR) << "Failed to get FIGETBSZ for " << file_path;
- return false;
- }
- if (!blksize) {
- LOG(ERROR) << "Invalid filesystem block size: " << blksize;
- return false;
- }
-
- uint64_t num_blocks = (s.st_size + blksize - 1) / blksize;
- if (num_blocks > std::numeric_limits<uint32_t>::max()) {
- LOG(ERROR) << "Too many blocks for FIBMAP (" << num_blocks << ")";
- return false;
- }
-
- for (uint32_t last_block, block_number = 0; block_number < num_blocks; block_number++) {
- uint32_t block = block_number;
- if (ioctl(file_fd, FIBMAP, &block)) {
- PLOG(ERROR) << "Failed to get FIBMAP for file " << file_path;
- return false;
- }
- if (!block) {
- LOG(ERROR) << "Logical block " << block_number << " is a hole, which is not supported";
- return false;
- }
-
- if (!extents->empty() && block == last_block + 1) {
- extents->back().fe_length += blksize;
- } else {
- extents->push_back(fiemap_extent{.fe_logical = block_number,
- .fe_physical = static_cast<uint64_t>(block) * blksize,
- .fe_length = static_cast<uint64_t>(blksize),
- .fe_flags = 0});
- }
- last_block = block;
- }
- return true;
-}
-
-FiemapUniquePtr FiemapWriter::Open(const std::string& file_path, uint64_t file_size, bool create,
- std::function<bool(uint64_t, uint64_t)> progress) {
- // if 'create' is false, open an existing file and do not truncate.
- int open_flags = O_RDWR | O_CLOEXEC;
- if (create) {
- if (access(file_path.c_str(), F_OK) == 0) {
- LOG(WARNING) << "File " << file_path << " already exists, truncating";
- }
- open_flags |= O_CREAT | O_TRUNC;
- }
- ::android::base::unique_fd file_fd(
- TEMP_FAILURE_RETRY(open(file_path.c_str(), open_flags, S_IRUSR | S_IWUSR)));
- if (file_fd < 0) {
- PLOG(ERROR) << "Failed to create file at: " << file_path;
- return nullptr;
- }
-
- std::string abs_path;
- if (!::android::base::Realpath(file_path, &abs_path)) {
- PLOG(ERROR) << "Invalid file path: " << file_path;
- cleanup(file_path, create);
- return nullptr;
- }
-
- std::string bdev_path;
- if (!GetBlockDeviceForFile(abs_path, &bdev_path)) {
- LOG(ERROR) << "Failed to get block dev path for file: " << file_path;
- cleanup(abs_path, create);
- return nullptr;
- }
-
- ::android::base::unique_fd bdev_fd(
- TEMP_FAILURE_RETRY(open(bdev_path.c_str(), O_RDONLY | O_CLOEXEC)));
- if (bdev_fd < 0) {
- PLOG(ERROR) << "Failed to open block device: " << bdev_path;
- cleanup(file_path, create);
- return nullptr;
- }
-
- uint64_t bdevsz;
- if (!GetBlockDeviceSize(bdev_fd, bdev_path, &bdevsz)) {
- LOG(ERROR) << "Failed to get block device size for : " << bdev_path;
- cleanup(file_path, create);
- return nullptr;
- }
-
- if (!create) {
- file_size = GetFileSize(abs_path);
- if (file_size == 0) {
- LOG(ERROR) << "Invalid file size of zero bytes for file: " << abs_path;
- return nullptr;
- }
- }
-
- uint64_t blocksz;
- uint32_t fs_type;
- if (!PerformFileChecks(abs_path, file_size, &blocksz, &fs_type)) {
- LOG(ERROR) << "Failed to validate file or file system for file:" << abs_path;
- cleanup(abs_path, create);
- return nullptr;
- }
-
- // Align up to the nearest block size.
- if (file_size % blocksz) {
- file_size += blocksz - (file_size % blocksz);
- }
-
- if (create) {
- if (!AllocateFile(file_fd, abs_path, blocksz, file_size, fs_type, std::move(progress))) {
- LOG(ERROR) << "Failed to allocate file: " << abs_path << " of size: " << file_size
- << " bytes";
- cleanup(abs_path, create);
- return nullptr;
- }
- }
-
- // f2fs may move the file blocks around.
- if (!PinFile(file_fd, abs_path, fs_type)) {
- cleanup(abs_path, create);
- LOG(ERROR) << "Failed to pin the file in storage";
- return nullptr;
- }
-
- // now allocate the FiemapWriter and start setting it up
- FiemapUniquePtr fmap(new FiemapWriter());
- switch (fs_type) {
- case EXT4_SUPER_MAGIC:
- case F2FS_SUPER_MAGIC:
- if (!ReadFiemap(file_fd, abs_path, &fmap->extents_)) {
- LOG(ERROR) << "Failed to read fiemap of file: " << abs_path;
- cleanup(abs_path, create);
- return nullptr;
- }
- break;
- case MSDOS_SUPER_MAGIC:
- if (!ReadFibmap(file_fd, abs_path, &fmap->extents_)) {
- LOG(ERROR) << "Failed to read fibmap of file: " << abs_path;
- cleanup(abs_path, create);
- return nullptr;
- }
- break;
- }
-
- fmap->file_path_ = abs_path;
- fmap->bdev_path_ = bdev_path;
- fmap->file_size_ = file_size;
- fmap->bdev_size_ = bdevsz;
- fmap->fs_type_ = fs_type;
- fmap->block_size_ = blocksz;
-
- LOG(VERBOSE) << "Successfully created FiemapWriter for file " << abs_path << " on block device "
- << bdev_path;
- return fmap;
-}
-
-} // namespace fiemap_writer
-} // namespace android
diff --git a/fs_mgr/libfiemap_writer/fiemap_writer_test.cpp b/fs_mgr/libfiemap_writer/fiemap_writer_test.cpp
deleted file mode 100644
index dda7dfd..0000000
--- a/fs_mgr/libfiemap_writer/fiemap_writer_test.cpp
+++ /dev/null
@@ -1,541 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <fcntl.h>
-#include <inttypes.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/vfs.h>
-#include <unistd.h>
-
-#include <string>
-
-#include <android-base/file.h>
-#include <android-base/logging.h>
-#include <android-base/stringprintf.h>
-#include <android-base/unique_fd.h>
-#include <gtest/gtest.h>
-#include <libdm/loop_control.h>
-#include <libfiemap_writer/fiemap_writer.h>
-#include <libfiemap_writer/split_fiemap_writer.h>
-
-#include "utility.h"
-
-namespace android {
-namespace fiemap_writer {
-
-using namespace std;
-using namespace std::string_literals;
-using namespace android::fiemap_writer;
-using unique_fd = android::base::unique_fd;
-using LoopDevice = android::dm::LoopDevice;
-
-std::string gTestDir;
-uint64_t testfile_size = 536870912; // default of 512MiB
-size_t gBlockSize = 0;
-
-class FiemapWriterTest : public ::testing::Test {
- protected:
- void SetUp() override {
- const ::testing::TestInfo* tinfo = ::testing::UnitTest::GetInstance()->current_test_info();
- testfile = gTestDir + "/"s + tinfo->name();
- }
-
- void TearDown() override { unlink(testfile.c_str()); }
-
- // name of the file we use for testing
- std::string testfile;
-};
-
-class SplitFiemapTest : public ::testing::Test {
- protected:
- void SetUp() override {
- const ::testing::TestInfo* tinfo = ::testing::UnitTest::GetInstance()->current_test_info();
- testfile = gTestDir + "/"s + tinfo->name();
- }
-
- void TearDown() override {
- std::string message;
- if (!SplitFiemap::RemoveSplitFiles(testfile, &message)) {
- cerr << "Could not remove all split files: " << message;
- }
- }
-
- // name of the file we use for testing
- std::string testfile;
-};
-
-TEST_F(FiemapWriterTest, CreateImpossiblyLargeFile) {
- // Try creating a file of size ~100TB but aligned to
- // 512 byte to make sure block alignment tests don't
- // fail.
- FiemapUniquePtr fptr = FiemapWriter::Open(testfile, 1099511627997184);
- EXPECT_EQ(fptr, nullptr);
- EXPECT_EQ(access(testfile.c_str(), F_OK), -1);
- EXPECT_EQ(errno, ENOENT);
-}
-
-TEST_F(FiemapWriterTest, CreateUnalignedFile) {
- // Try creating a file of size 4097 bytes which is guaranteed
- // to be unaligned to all known block sizes.
- FiemapUniquePtr fptr = FiemapWriter::Open(testfile, gBlockSize + 1);
- ASSERT_NE(fptr, nullptr);
- ASSERT_EQ(fptr->size(), gBlockSize * 2);
-}
-
-TEST_F(FiemapWriterTest, CheckFilePath) {
- FiemapUniquePtr fptr = FiemapWriter::Open(testfile, gBlockSize);
- ASSERT_NE(fptr, nullptr);
- EXPECT_EQ(fptr->size(), gBlockSize);
- EXPECT_EQ(fptr->file_path(), testfile);
- EXPECT_EQ(access(testfile.c_str(), F_OK), 0);
-}
-
-TEST_F(FiemapWriterTest, CheckFileSize) {
- // Create a large-ish file and test that the expected size matches.
- FiemapUniquePtr fptr = FiemapWriter::Open(testfile, 1024 * 1024 * 16);
- ASSERT_NE(fptr, nullptr);
-
- struct stat s;
- ASSERT_EQ(stat(testfile.c_str(), &s), 0);
- EXPECT_EQ(static_cast<uint64_t>(s.st_size), fptr->size());
-}
-
-TEST_F(FiemapWriterTest, CheckProgress) {
- std::vector<uint64_t> expected;
- size_t invocations = 0;
- auto callback = [&](uint64_t done, uint64_t total) -> bool {
- if (invocations >= expected.size()) {
- return false;
- }
- EXPECT_EQ(done, expected[invocations]);
- EXPECT_EQ(total, gBlockSize);
- invocations++;
- return true;
- };
-
- expected.push_back(gBlockSize);
-
- auto ptr = FiemapWriter::Open(testfile, gBlockSize, true, std::move(callback));
- EXPECT_NE(ptr, nullptr);
- EXPECT_EQ(invocations, expected.size());
-}
-
-TEST_F(FiemapWriterTest, CheckPinning) {
- auto ptr = FiemapWriter::Open(testfile, 4096);
- ASSERT_NE(ptr, nullptr);
- EXPECT_TRUE(FiemapWriter::HasPinnedExtents(testfile));
-}
-
-TEST_F(FiemapWriterTest, CheckBlockDevicePath) {
- FiemapUniquePtr fptr = FiemapWriter::Open(testfile, gBlockSize);
- EXPECT_EQ(fptr->size(), gBlockSize);
- EXPECT_EQ(fptr->bdev_path().find("/dev/block/"), size_t(0));
- EXPECT_EQ(fptr->bdev_path().find("/dev/block/dm-"), string::npos);
-}
-
-TEST_F(FiemapWriterTest, CheckFileCreated) {
- FiemapUniquePtr fptr = FiemapWriter::Open(testfile, 32768);
- ASSERT_NE(fptr, nullptr);
- unique_fd fd(open(testfile.c_str(), O_RDONLY));
- EXPECT_GT(fd, -1);
-}
-
-TEST_F(FiemapWriterTest, CheckFileSizeActual) {
- FiemapUniquePtr fptr = FiemapWriter::Open(testfile, testfile_size);
- ASSERT_NE(fptr, nullptr);
-
- struct stat sb;
- ASSERT_EQ(stat(testfile.c_str(), &sb), 0);
- EXPECT_GE(sb.st_size, testfile_size);
-}
-
-TEST_F(FiemapWriterTest, CheckFileExtents) {
- FiemapUniquePtr fptr = FiemapWriter::Open(testfile, testfile_size);
- ASSERT_NE(fptr, nullptr);
- EXPECT_GT(fptr->extents().size(), 0);
-}
-
-TEST_F(FiemapWriterTest, ExistingFile) {
- // Create the file.
- { ASSERT_NE(FiemapWriter::Open(testfile, gBlockSize), nullptr); }
- // Test that we can still open it.
- {
- auto ptr = FiemapWriter::Open(testfile, 0, false);
- ASSERT_NE(ptr, nullptr);
- EXPECT_GT(ptr->extents().size(), 0);
- }
-}
-
-TEST_F(FiemapWriterTest, FileDeletedOnError) {
- auto callback = [](uint64_t, uint64_t) -> bool { return false; };
- auto ptr = FiemapWriter::Open(testfile, gBlockSize, true, std::move(callback));
- EXPECT_EQ(ptr, nullptr);
- EXPECT_EQ(access(testfile.c_str(), F_OK), -1);
- EXPECT_EQ(errno, ENOENT);
-}
-
-TEST_F(FiemapWriterTest, MaxBlockSize) {
- ASSERT_GT(DetermineMaximumFileSize(testfile), 0);
-}
-
-TEST_F(FiemapWriterTest, FibmapBlockAddressing) {
- FiemapUniquePtr fptr = FiemapWriter::Open(testfile, gBlockSize);
- ASSERT_NE(fptr, nullptr);
-
- switch (fptr->fs_type()) {
- case F2FS_SUPER_MAGIC:
- case EXT4_SUPER_MAGIC:
- // Skip the test for FIEMAP supported filesystems. This is really
- // because f2fs/ext4 have caches that seem to defeat reading back
- // directly from the block device, and writing directly is too
- // dangerous.
- std::cout << "Skipping test, filesystem does not use FIBMAP\n";
- return;
- }
-
- bool uses_dm;
- std::string bdev_path;
- ASSERT_TRUE(FiemapWriter::GetBlockDeviceForFile(testfile, &bdev_path, &uses_dm));
-
- if (uses_dm) {
- // We could use a device-mapper wrapper here to bypass encryption, but
- // really this test is for FIBMAP correctness on VFAT (where encryption
- // is never used), so we don't bother.
- std::cout << "Skipping test, block device is metadata encrypted\n";
- return;
- }
-
- std::string data(fptr->size(), '\0');
- for (size_t i = 0; i < data.size(); i++) {
- data[i] = 'A' + static_cast<char>(data.size() % 26);
- }
-
- {
- unique_fd fd(open(testfile.c_str(), O_WRONLY | O_CLOEXEC));
- ASSERT_GE(fd, 0);
- ASSERT_TRUE(android::base::WriteFully(fd, data.data(), data.size()));
- ASSERT_EQ(fsync(fd), 0);
- }
-
- ASSERT_FALSE(fptr->extents().empty());
- const auto& first_extent = fptr->extents()[0];
-
- unique_fd bdev(open(fptr->bdev_path().c_str(), O_RDONLY | O_CLOEXEC));
- ASSERT_GE(bdev, 0);
-
- off_t where = first_extent.fe_physical;
- ASSERT_EQ(lseek(bdev, where, SEEK_SET), where);
-
- // Note: this will fail on encrypted folders.
- std::string actual(data.size(), '\0');
- ASSERT_GE(first_extent.fe_length, data.size());
- ASSERT_TRUE(android::base::ReadFully(bdev, actual.data(), actual.size()));
- EXPECT_EQ(memcmp(actual.data(), data.data(), data.size()), 0);
-}
-
-TEST_F(SplitFiemapTest, Create) {
- auto ptr = SplitFiemap::Create(testfile, 1024 * 768, 1024 * 32);
- ASSERT_NE(ptr, nullptr);
-
- auto extents = ptr->extents();
-
- // Destroy the fiemap, closing file handles. This should not delete them.
- ptr = nullptr;
-
- std::vector<std::string> files;
- ASSERT_TRUE(SplitFiemap::GetSplitFileList(testfile, &files));
- for (const auto& path : files) {
- EXPECT_EQ(access(path.c_str(), F_OK), 0);
- }
-
- ASSERT_GE(extents.size(), files.size());
-}
-
-TEST_F(SplitFiemapTest, Open) {
- {
- auto ptr = SplitFiemap::Create(testfile, 1024 * 768, 1024 * 32);
- ASSERT_NE(ptr, nullptr);
- }
-
- auto ptr = SplitFiemap::Open(testfile);
- ASSERT_NE(ptr, nullptr);
-
- auto extents = ptr->extents();
- ASSERT_GE(extents.size(), 24);
-}
-
-TEST_F(SplitFiemapTest, DeleteOnFail) {
- auto ptr = SplitFiemap::Create(testfile, 1024 * 1024 * 100, 1);
- ASSERT_EQ(ptr, nullptr);
-
- std::string first_file = testfile + ".0001";
- ASSERT_NE(access(first_file.c_str(), F_OK), 0);
- ASSERT_EQ(errno, ENOENT);
- ASSERT_NE(access(testfile.c_str(), F_OK), 0);
- ASSERT_EQ(errno, ENOENT);
-}
-
-static string ReadSplitFiles(const std::string& base_path, size_t num_files) {
- std::string result;
- for (int i = 0; i < num_files; i++) {
- std::string path = base_path + android::base::StringPrintf(".%04d", i);
- std::string data;
- if (!android::base::ReadFileToString(path, &data)) {
- return {};
- }
- result += data;
- }
- return result;
-}
-
-TEST_F(SplitFiemapTest, WriteWholeFile) {
- static constexpr size_t kChunkSize = 32768;
- static constexpr size_t kSize = kChunkSize * 3;
- auto ptr = SplitFiemap::Create(testfile, kSize, kChunkSize);
- ASSERT_NE(ptr, nullptr);
-
- auto buffer = std::make_unique<int[]>(kSize / sizeof(int));
- for (size_t i = 0; i < kSize / sizeof(int); i++) {
- buffer[i] = i;
- }
- ASSERT_TRUE(ptr->Write(buffer.get(), kSize));
-
- std::string expected(reinterpret_cast<char*>(buffer.get()), kSize);
- auto actual = ReadSplitFiles(testfile, 3);
- ASSERT_EQ(expected.size(), actual.size());
- EXPECT_EQ(memcmp(expected.data(), actual.data(), actual.size()), 0);
-}
-
-TEST_F(SplitFiemapTest, WriteFileInChunks1) {
- static constexpr size_t kChunkSize = 32768;
- static constexpr size_t kSize = kChunkSize * 3;
- auto ptr = SplitFiemap::Create(testfile, kSize, kChunkSize);
- ASSERT_NE(ptr, nullptr);
-
- auto buffer = std::make_unique<int[]>(kSize / sizeof(int));
- for (size_t i = 0; i < kSize / sizeof(int); i++) {
- buffer[i] = i;
- }
-
- // Write in chunks of 1000 (so some writes straddle the boundary of two
- // files).
- size_t bytes_written = 0;
- while (bytes_written < kSize) {
- size_t to_write = std::min(kSize - bytes_written, (size_t)1000);
- char* data = reinterpret_cast<char*>(buffer.get()) + bytes_written;
- ASSERT_TRUE(ptr->Write(data, to_write));
- bytes_written += to_write;
- }
-
- std::string expected(reinterpret_cast<char*>(buffer.get()), kSize);
- auto actual = ReadSplitFiles(testfile, 3);
- ASSERT_EQ(expected.size(), actual.size());
- EXPECT_EQ(memcmp(expected.data(), actual.data(), actual.size()), 0);
-}
-
-TEST_F(SplitFiemapTest, WriteFileInChunks2) {
- static constexpr size_t kChunkSize = 32768;
- static constexpr size_t kSize = kChunkSize * 3;
- auto ptr = SplitFiemap::Create(testfile, kSize, kChunkSize);
- ASSERT_NE(ptr, nullptr);
-
- auto buffer = std::make_unique<int[]>(kSize / sizeof(int));
- for (size_t i = 0; i < kSize / sizeof(int); i++) {
- buffer[i] = i;
- }
-
- // Write in chunks of 32KiB so every write is exactly at the end of the
- // current file.
- size_t bytes_written = 0;
- while (bytes_written < kSize) {
- size_t to_write = std::min(kSize - bytes_written, kChunkSize);
- char* data = reinterpret_cast<char*>(buffer.get()) + bytes_written;
- ASSERT_TRUE(ptr->Write(data, to_write));
- bytes_written += to_write;
- }
-
- std::string expected(reinterpret_cast<char*>(buffer.get()), kSize);
- auto actual = ReadSplitFiles(testfile, 3);
- ASSERT_EQ(expected.size(), actual.size());
- EXPECT_EQ(memcmp(expected.data(), actual.data(), actual.size()), 0);
-}
-
-TEST_F(SplitFiemapTest, WritePastEnd) {
- static constexpr size_t kChunkSize = 32768;
- static constexpr size_t kSize = kChunkSize * 3;
- auto ptr = SplitFiemap::Create(testfile, kSize, kChunkSize);
- ASSERT_NE(ptr, nullptr);
-
- auto buffer = std::make_unique<int[]>(kSize / sizeof(int));
- for (size_t i = 0; i < kSize / sizeof(int); i++) {
- buffer[i] = i;
- }
- ASSERT_TRUE(ptr->Write(buffer.get(), kSize));
- ASSERT_FALSE(ptr->Write(buffer.get(), kSize));
-}
-
-class VerifyBlockWritesExt4 : public ::testing::Test {
- // 2GB Filesystem and 4k block size by default
- static constexpr uint64_t block_size = 4096;
- static constexpr uint64_t fs_size = 2147483648;
-
- protected:
- void SetUp() override {
- fs_path = std::string(getenv("TMPDIR")) + "/ext4_2G.img";
- uint64_t count = fs_size / block_size;
- std::string dd_cmd =
- ::android::base::StringPrintf("/system/bin/dd if=/dev/zero of=%s bs=%" PRIu64
- " count=%" PRIu64 " > /dev/null 2>&1",
- fs_path.c_str(), block_size, count);
- std::string mkfs_cmd =
- ::android::base::StringPrintf("/system/bin/mkfs.ext4 -q %s", fs_path.c_str());
- // create mount point
- mntpoint = std::string(getenv("TMPDIR")) + "/fiemap_mnt";
- ASSERT_EQ(mkdir(mntpoint.c_str(), S_IRWXU), 0);
- // create file for the file system
- int ret = system(dd_cmd.c_str());
- ASSERT_EQ(ret, 0);
- // Get and attach a loop device to the filesystem we created
- LoopDevice loop_dev(fs_path);
- ASSERT_TRUE(loop_dev.valid());
- // create file system
- ret = system(mkfs_cmd.c_str());
- ASSERT_EQ(ret, 0);
-
- // mount the file system
- ASSERT_EQ(mount(loop_dev.device().c_str(), mntpoint.c_str(), "ext4", 0, nullptr), 0);
- }
-
- void TearDown() override {
- umount(mntpoint.c_str());
- rmdir(mntpoint.c_str());
- unlink(fs_path.c_str());
- }
-
- std::string mntpoint;
- std::string fs_path;
-};
-
-class VerifyBlockWritesF2fs : public ::testing::Test {
- // 2GB Filesystem and 4k block size by default
- static constexpr uint64_t block_size = 4096;
- static constexpr uint64_t fs_size = 2147483648;
-
- protected:
- void SetUp() override {
- fs_path = std::string(getenv("TMPDIR")) + "/f2fs_2G.img";
- uint64_t count = fs_size / block_size;
- std::string dd_cmd =
- ::android::base::StringPrintf("/system/bin/dd if=/dev/zero of=%s bs=%" PRIu64
- " count=%" PRIu64 " > /dev/null 2>&1",
- fs_path.c_str(), block_size, count);
- std::string mkfs_cmd =
- ::android::base::StringPrintf("/system/bin/make_f2fs -q %s", fs_path.c_str());
- // create mount point
- mntpoint = std::string(getenv("TMPDIR")) + "/fiemap_mnt";
- ASSERT_EQ(mkdir(mntpoint.c_str(), S_IRWXU), 0);
- // create file for the file system
- int ret = system(dd_cmd.c_str());
- ASSERT_EQ(ret, 0);
- // Get and attach a loop device to the filesystem we created
- LoopDevice loop_dev(fs_path);
- ASSERT_TRUE(loop_dev.valid());
- // create file system
- ret = system(mkfs_cmd.c_str());
- ASSERT_EQ(ret, 0);
-
- // mount the file system
- ASSERT_EQ(mount(loop_dev.device().c_str(), mntpoint.c_str(), "f2fs", 0, nullptr), 0);
- }
-
- void TearDown() override {
- umount(mntpoint.c_str());
- rmdir(mntpoint.c_str());
- unlink(fs_path.c_str());
- }
-
- std::string mntpoint;
- std::string fs_path;
-};
-
-bool DetermineBlockSize() {
- struct statfs s;
- if (statfs(gTestDir.c_str(), &s)) {
- std::cerr << "Could not call statfs: " << strerror(errno) << "\n";
- return false;
- }
- if (!s.f_bsize) {
- std::cerr << "Invalid block size: " << s.f_bsize << "\n";
- return false;
- }
-
- gBlockSize = s.f_bsize;
- return true;
-}
-
-} // namespace fiemap_writer
-} // namespace android
-
-using namespace android::fiemap_writer;
-
-int main(int argc, char** argv) {
- ::testing::InitGoogleTest(&argc, argv);
- if (argc > 1 && argv[1] == "-h"s) {
- cerr << "Usage: [test_dir] [file_size]\n";
- cerr << "\n";
- cerr << "Note: test_dir must be a writable, unencrypted directory.\n";
- exit(EXIT_FAILURE);
- }
- ::android::base::InitLogging(argv, ::android::base::StderrLogger);
-
- std::string root_dir = "/data/local/unencrypted";
- if (access(root_dir.c_str(), F_OK)) {
- root_dir = "/data";
- }
-
- std::string tempdir = root_dir + "/XXXXXX"s;
- if (!mkdtemp(tempdir.data())) {
- cerr << "unable to create tempdir on " << root_dir << "\n";
- exit(EXIT_FAILURE);
- }
- if (!android::base::Realpath(tempdir, &gTestDir)) {
- cerr << "unable to find realpath for " << tempdir;
- exit(EXIT_FAILURE);
- }
-
- if (argc > 2) {
- testfile_size = strtoull(argv[2], NULL, 0);
- if (testfile_size == ULLONG_MAX) {
- testfile_size = 512 * 1024 * 1024;
- }
- }
-
- if (!DetermineBlockSize()) {
- exit(EXIT_FAILURE);
- }
-
- auto result = RUN_ALL_TESTS();
-
- std::string cmd = "rm -rf " + gTestDir;
- system(cmd.c_str());
-
- return result;
-}
diff --git a/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h b/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h
deleted file mode 100644
index ee79262..0000000
--- a/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include <linux/fiemap.h>
-#include <stdint.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <functional>
-#include <string>
-#include <vector>
-
-#include <android-base/unique_fd.h>
-
-namespace android {
-namespace fiemap_writer {
-
-class FiemapWriter;
-using FiemapUniquePtr = std::unique_ptr<FiemapWriter>;
-
-class FiemapWriter final {
- public:
- // Factory method for FiemapWriter.
- // The method returns FiemapUniquePtr that contains all the data necessary to be able to write
- // to the given file directly using raw block i/o. The optional progress callback will be
- // invoked, if create is true, while the file is being initialized. It receives the bytes
- // written and the number of total bytes. If the callback returns false, the operation will
- // fail.
- //
- // Note: when create is true, the file size will be aligned up to the nearest file system
- // block.
- static FiemapUniquePtr Open(const std::string& file_path, uint64_t file_size,
- bool create = true,
- std::function<bool(uint64_t, uint64_t)> progress = {});
-
- // Check that a file still has the same extents since it was last opened with FiemapWriter,
- // assuming the file was not resized outside of FiemapWriter. Returns false either on error
- // or if the file was not pinned.
- //
- // This will always return true on Ext4. On F2FS, it will return true if either of the
- // following cases are true:
- // - The file was never pinned.
- // - The file is pinned and has not been moved by the GC.
- // Thus, this method should only be called for pinned files (such as those returned by
- // FiemapWriter::Open).
- static bool HasPinnedExtents(const std::string& file_path);
-
- // Returns the underlying block device of a file. This will look past device-mapper layers
- // as long as each layer would not change block mappings (i.e., dm-crypt, dm-bow, and dm-
- // default-key tables are okay; dm-linear is not). If a mapping such as dm-linear is found,
- // it will be returned in place of any physical block device.
- //
- // It is the caller's responsibility to check whether the returned block device is acceptable.
- // Gsid, for example, will only accept /dev/block/by-name/userdata as the bottom device.
- // Callers can check the device name (dm- or loop prefix), inspect sysfs, or compare the major
- // number against a boot device.
- //
- // If device-mapper nodes were encountered, then |uses_dm| will be set to true.
- static bool GetBlockDeviceForFile(const std::string& file_path, std::string* bdev_path,
- bool* uses_dm = nullptr);
-
- ~FiemapWriter() = default;
-
- const std::string& file_path() const { return file_path_; };
- uint64_t size() const { return file_size_; };
- const std::string& bdev_path() const { return bdev_path_; };
- uint64_t block_size() const { return block_size_; };
- const std::vector<struct fiemap_extent>& extents() { return extents_; };
- uint32_t fs_type() const { return fs_type_; }
-
- // Non-copyable & Non-movable
- FiemapWriter(const FiemapWriter&) = delete;
- FiemapWriter& operator=(const FiemapWriter&) = delete;
- FiemapWriter& operator=(FiemapWriter&&) = delete;
- FiemapWriter(FiemapWriter&&) = delete;
-
- private:
- // Name of the file managed by this class.
- std::string file_path_;
- // Block device on which we have created the file.
- std::string bdev_path_;
-
- // Size in bytes of the file this class is writing
- uint64_t file_size_;
-
- // total size in bytes of the block device
- uint64_t bdev_size_;
-
- // Filesystem type where the file is being created.
- // See: <uapi/linux/magic.h> for filesystem magic numbers
- uint32_t fs_type_;
-
- // block size as reported by the kernel of the underlying block device;
- uint64_t block_size_;
-
- // This file's fiemap
- std::vector<struct fiemap_extent> extents_;
-
- FiemapWriter() = default;
-};
-
-} // namespace fiemap_writer
-} // namespace android
diff --git a/fs_mgr/libfiemap_writer/include/libfiemap_writer/split_fiemap_writer.h b/fs_mgr/libfiemap_writer/include/libfiemap_writer/split_fiemap_writer.h
deleted file mode 100644
index 7b977e1..0000000
--- a/fs_mgr/libfiemap_writer/include/libfiemap_writer/split_fiemap_writer.h
+++ /dev/null
@@ -1,97 +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.
- */
-
-#pragma once
-
-#include <stdint.h>
-
-#include <functional>
-#include <memory>
-#include <string>
-#include <vector>
-
-#include <android-base/unique_fd.h>
-
-#include "fiemap_writer.h"
-
-namespace android {
-namespace fiemap_writer {
-
-// Wrapper around FiemapWriter that is able to split images across files if
-// necessary.
-class SplitFiemap final {
- public:
- using ProgressCallback = std::function<bool(uint64_t, uint64_t)>;
-
- // Create a new split fiemap file. If |max_piece_size| is 0, the number of
- // pieces will be determined automatically by detecting the filesystem.
- // Otherwise, the file will be split evenly (with the remainder in the
- // final file).
- static std::unique_ptr<SplitFiemap> Create(const std::string& file_path, uint64_t file_size,
- uint64_t max_piece_size,
- ProgressCallback progress = {});
-
- // Open an existing split fiemap file.
- static std::unique_ptr<SplitFiemap> Open(const std::string& file_path);
-
- ~SplitFiemap();
-
- // Return a list of all files created for a split file.
- static bool GetSplitFileList(const std::string& file_path, std::vector<std::string>* list);
-
- // Destroy all components of a split file. If the root file does not exist,
- // this returns true and does not report an error.
- static bool RemoveSplitFiles(const std::string& file_path, std::string* message = nullptr);
-
- // Return whether all components of a split file still have pinned extents.
- bool HasPinnedExtents() const;
-
- // Helper method for writing data that spans files. Note there is no seek
- // method (yet); this starts at 0 and increments the position by |bytes|.
- bool Write(const void* data, uint64_t bytes);
-
- // Flush all writes to all split files.
- bool Flush();
-
- const std::vector<struct fiemap_extent>& extents();
- uint32_t block_size() const;
- uint64_t size() const { return total_size_; }
- const std::string& bdev_path() const;
-
- // Non-copyable & Non-movable
- SplitFiemap(const SplitFiemap&) = delete;
- SplitFiemap& operator=(const SplitFiemap&) = delete;
- SplitFiemap& operator=(SplitFiemap&&) = delete;
- SplitFiemap(SplitFiemap&&) = delete;
-
- private:
- SplitFiemap() = default;
- void AddFile(FiemapUniquePtr&& file);
-
- bool creating_ = false;
- std::string list_file_;
- std::vector<FiemapUniquePtr> files_;
- std::vector<struct fiemap_extent> extents_;
- uint64_t total_size_ = 0;
-
- // Most recently open file and position for Write().
- size_t cursor_index_ = 0;
- uint64_t cursor_file_pos_ = 0;
- android::base::unique_fd cursor_fd_;
-};
-
-} // namespace fiemap_writer
-} // namespace android
diff --git a/fs_mgr/libfiemap_writer/split_fiemap_writer.cpp b/fs_mgr/libfiemap_writer/split_fiemap_writer.cpp
deleted file mode 100644
index 16a82d2..0000000
--- a/fs_mgr/libfiemap_writer/split_fiemap_writer.cpp
+++ /dev/null
@@ -1,298 +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 <libfiemap_writer/split_fiemap_writer.h>
-
-#include <fcntl.h>
-#include <stdint.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <memory>
-#include <string>
-#include <vector>
-
-#include <android-base/file.h>
-#include <android-base/logging.h>
-#include <android-base/stringprintf.h>
-#include <android-base/strings.h>
-#include <android-base/unique_fd.h>
-
-#include "utility.h"
-
-namespace android {
-namespace fiemap_writer {
-
-using android::base::unique_fd;
-
-// We use a four-digit suffix at the end of filenames.
-static const size_t kMaxFilePieces = 500;
-
-std::unique_ptr<SplitFiemap> SplitFiemap::Create(const std::string& file_path, uint64_t file_size,
- uint64_t max_piece_size,
- ProgressCallback progress) {
- if (!file_size) {
- LOG(ERROR) << "Cannot create a fiemap for a 0-length file: " << file_path;
- return nullptr;
- }
-
- if (!max_piece_size) {
- max_piece_size = DetermineMaximumFileSize(file_path);
- if (!max_piece_size) {
- LOG(ERROR) << "Could not determine maximum file size for " << file_path;
- return nullptr;
- }
- }
-
- // Call |progress| only when the total percentage would significantly change.
- int permille = -1;
- uint64_t total_bytes_written = 0;
- auto on_progress = [&](uint64_t written, uint64_t) -> bool {
- uint64_t actual_written = total_bytes_written + written;
- int new_permille = (actual_written * 1000) / file_size;
- if (new_permille != permille && actual_written < file_size) {
- if (progress && !progress(actual_written, file_size)) {
- return false;
- }
- permille = new_permille;
- }
- return true;
- };
-
- std::unique_ptr<SplitFiemap> out(new SplitFiemap());
- out->creating_ = true;
- out->list_file_ = file_path;
-
- // Create the split files.
- uint64_t remaining_bytes = file_size;
- while (remaining_bytes) {
- if (out->files_.size() >= kMaxFilePieces) {
- LOG(ERROR) << "Requested size " << file_size << " created too many split files";
- return nullptr;
- }
- std::string chunk_path =
- android::base::StringPrintf("%s.%04d", file_path.c_str(), (int)out->files_.size());
- uint64_t chunk_size = std::min(max_piece_size, remaining_bytes);
- auto writer = FiemapWriter::Open(chunk_path, chunk_size, true, on_progress);
- if (!writer) {
- return nullptr;
- }
-
- // To make sure the alignment doesn't create too much inconsistency, we
- // account the *actual* size, not the requested size.
- total_bytes_written += writer->size();
- // writer->size() is block size aligned and could be bigger than remaining_bytes
- // If remaining_bytes is bigger, set remaining_bytes to 0 to avoid underflow error.
- remaining_bytes = remaining_bytes > writer->size() ? (remaining_bytes - writer->size()) : 0;
- out->AddFile(std::move(writer));
- }
-
- // Create the split file list.
- unique_fd fd(open(out->list_file_.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0660));
- if (fd < 0) {
- PLOG(ERROR) << "Failed to open " << file_path;
- return nullptr;
- }
-
- for (const auto& writer : out->files_) {
- std::string line = android::base::Basename(writer->file_path()) + "\n";
- if (!android::base::WriteFully(fd, line.data(), line.size())) {
- PLOG(ERROR) << "Write failed " << file_path;
- return nullptr;
- }
- }
-
- // Unset this bit, so we don't unlink on destruction.
- out->creating_ = false;
- return out;
-}
-
-std::unique_ptr<SplitFiemap> SplitFiemap::Open(const std::string& file_path) {
- std::vector<std::string> files;
- if (!GetSplitFileList(file_path, &files)) {
- return nullptr;
- }
-
- std::unique_ptr<SplitFiemap> out(new SplitFiemap());
- out->list_file_ = file_path;
-
- for (const auto& file : files) {
- auto writer = FiemapWriter::Open(file, 0, false);
- if (!writer) {
- // Error was logged in Open().
- return nullptr;
- }
- out->AddFile(std::move(writer));
- }
- return out;
-}
-
-bool SplitFiemap::GetSplitFileList(const std::string& file_path, std::vector<std::string>* list) {
- // This is not the most efficient thing, but it is simple and recovering
- // the fiemap/fibmap is much more expensive.
- std::string contents;
- if (!android::base::ReadFileToString(file_path, &contents, true)) {
- PLOG(ERROR) << "Error reading file: " << file_path;
- return false;
- }
-
- std::vector<std::string> names = android::base::Split(contents, "\n");
- std::string dir = android::base::Dirname(file_path);
- for (const auto& name : names) {
- if (!name.empty()) {
- list->emplace_back(dir + "/" + name);
- }
- }
- return true;
-}
-
-bool SplitFiemap::RemoveSplitFiles(const std::string& file_path, std::string* message) {
- // Early exit if this does not exist, and do not report an error.
- if (access(file_path.c_str(), F_OK) && errno == ENOENT) {
- return true;
- }
-
- bool ok = true;
- std::vector<std::string> files;
- if (GetSplitFileList(file_path, &files)) {
- for (const auto& file : files) {
- ok &= android::base::RemoveFileIfExists(file, message);
- }
- }
- ok &= android::base::RemoveFileIfExists(file_path, message);
- return ok;
-}
-
-bool SplitFiemap::HasPinnedExtents() const {
- for (const auto& file : files_) {
- if (!FiemapWriter::HasPinnedExtents(file->file_path())) {
- return false;
- }
- }
- return true;
-}
-
-const std::vector<struct fiemap_extent>& SplitFiemap::extents() {
- if (extents_.empty()) {
- for (const auto& file : files_) {
- const auto& extents = file->extents();
- extents_.insert(extents_.end(), extents.begin(), extents.end());
- }
- }
- return extents_;
-}
-
-bool SplitFiemap::Write(const void* data, uint64_t bytes) {
- // Open the current file.
- FiemapWriter* file = files_[cursor_index_].get();
-
- const uint8_t* data_ptr = reinterpret_cast<const uint8_t*>(data);
- uint64_t bytes_remaining = bytes;
- while (bytes_remaining) {
- // How many bytes can we write into the current file?
- uint64_t file_bytes_left = file->size() - cursor_file_pos_;
- if (!file_bytes_left) {
- if (cursor_index_ == files_.size() - 1) {
- LOG(ERROR) << "write past end of file requested";
- return false;
- }
-
- // No space left in the current file, but we have more files to
- // use, so prep the next one.
- cursor_fd_ = {};
- cursor_file_pos_ = 0;
- file = files_[++cursor_index_].get();
- file_bytes_left = file->size();
- }
-
- // Open the current file if it's not open.
- if (cursor_fd_ < 0) {
- cursor_fd_.reset(open(file->file_path().c_str(), O_CLOEXEC | O_WRONLY));
- if (cursor_fd_ < 0) {
- PLOG(ERROR) << "open failed: " << file->file_path();
- return false;
- }
- CHECK(cursor_file_pos_ == 0);
- }
-
- if (!FiemapWriter::HasPinnedExtents(file->file_path())) {
- LOG(ERROR) << "file is no longer pinned: " << file->file_path();
- return false;
- }
-
- uint64_t bytes_to_write = std::min(file_bytes_left, bytes_remaining);
- if (!android::base::WriteFully(cursor_fd_, data_ptr, bytes_to_write)) {
- PLOG(ERROR) << "write failed: " << file->file_path();
- return false;
- }
- data_ptr += bytes_to_write;
- bytes_remaining -= bytes_to_write;
- cursor_file_pos_ += bytes_to_write;
- }
-
- // If we've reached the end of the current file, close it for sanity.
- if (cursor_file_pos_ == file->size()) {
- cursor_fd_ = {};
- }
- return true;
-}
-
-bool SplitFiemap::Flush() {
- for (const auto& file : files_) {
- unique_fd fd(open(file->file_path().c_str(), O_RDONLY | O_CLOEXEC));
- if (fd < 0) {
- PLOG(ERROR) << "open failed: " << file->file_path();
- return false;
- }
- if (fsync(fd)) {
- PLOG(ERROR) << "fsync failed: " << file->file_path();
- return false;
- }
- }
- return true;
-}
-
-SplitFiemap::~SplitFiemap() {
- if (!creating_) {
- return;
- }
-
- // We failed to finish creating, so unlink everything.
- unlink(list_file_.c_str());
- for (auto&& file : files_) {
- std::string path = file->file_path();
- file = nullptr;
-
- unlink(path.c_str());
- }
-}
-
-void SplitFiemap::AddFile(FiemapUniquePtr&& file) {
- total_size_ += file->size();
- files_.emplace_back(std::move(file));
-}
-
-uint32_t SplitFiemap::block_size() const {
- return files_[0]->block_size();
-}
-
-const std::string& SplitFiemap::bdev_path() const {
- return files_[0]->bdev_path();
-}
-
-} // namespace fiemap_writer
-} // namespace android
diff --git a/fs_mgr/libfiemap_writer/testdata/file_32k b/fs_mgr/libfiemap_writer/testdata/file_32k
deleted file mode 100644
index 12f3be4..0000000
--- a/fs_mgr/libfiemap_writer/testdata/file_32k
+++ /dev/null
Binary files differ
diff --git a/fs_mgr/libfiemap_writer/testdata/file_4k b/fs_mgr/libfiemap_writer/testdata/file_4k
deleted file mode 100644
index 08e7df1..0000000
--- a/fs_mgr/libfiemap_writer/testdata/file_4k
+++ /dev/null
Binary files differ
diff --git a/fs_mgr/libfiemap_writer/testdata/unaligned_file b/fs_mgr/libfiemap_writer/testdata/unaligned_file
deleted file mode 100644
index c107c26..0000000
--- a/fs_mgr/libfiemap_writer/testdata/unaligned_file
+++ /dev/null
Binary files differ
diff --git a/fs_mgr/libfiemap_writer/utility.cpp b/fs_mgr/libfiemap_writer/utility.cpp
deleted file mode 100644
index 192ec16..0000000
--- a/fs_mgr/libfiemap_writer/utility.cpp
+++ /dev/null
@@ -1,66 +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 "utility.h"
-
-#include <stdint.h>
-#include <sys/vfs.h>
-#include <unistd.h>
-
-#include <android-base/logging.h>
-#include <libfiemap_writer/fiemap_writer.h>
-
-namespace android {
-namespace fiemap_writer {
-
-uint64_t DetermineMaximumFileSize(const std::string& file_path) {
- // Create the smallest file possible (one block).
- auto writer = FiemapWriter::Open(file_path, 1);
- if (!writer) {
- return 0;
- }
-
- uint64_t result = 0;
- switch (writer->fs_type()) {
- case EXT4_SUPER_MAGIC:
- // The minimum is 16GiB, so just report that. If we wanted we could parse the
- // superblock and figure out if 64-bit support is enabled.
- result = 17179869184ULL;
- break;
- case F2FS_SUPER_MAGIC:
- // Formula is from https://www.kernel.org/doc/Documentation/filesystems/f2fs.txt
- // 4KB * (923 + 2 * 1018 + 2 * 1018 * 1018 + 1018 * 1018 * 1018) := 3.94TB.
- result = 4329690886144ULL;
- break;
- case MSDOS_SUPER_MAGIC:
- // 4GB-1, which we want aligned to the block size.
- result = 4294967295;
- result -= (result % writer->block_size());
- break;
- default:
- LOG(ERROR) << "Unknown file system type: " << writer->fs_type();
- break;
- }
-
- // Close and delete the temporary file.
- writer = nullptr;
- unlink(file_path.c_str());
-
- return result;
-}
-
-} // namespace fiemap_writer
-} // namespace android
diff --git a/fs_mgr/liblp/builder.cpp b/fs_mgr/liblp/builder.cpp
index 25a042f..8797ea9 100644
--- a/fs_mgr/liblp/builder.cpp
+++ b/fs_mgr/liblp/builder.cpp
@@ -30,8 +30,8 @@
namespace android {
namespace fs_mgr {
-bool MetadataBuilder::sABOverrideSet;
-bool MetadataBuilder::sABOverrideValue;
+std::optional<bool> MetadataBuilder::sABOverride;
+std::optional<bool> MetadataBuilder::sRetrofitDap;
static const std::string kDefaultGroup = "default";
@@ -169,7 +169,8 @@
// needed. On the other hand, for retrofit devices, we'll need to
// translate block device and group names to update their slot suffixes.
auto super_device = GetMetadataSuperBlockDevice(*metadata.get());
- if (GetBlockDevicePartitionName(*super_device) == "super") {
+ if (GetBlockDevicePartitionName(*super_device) == "super" ||
+ !IsRetrofitDynamicPartitionsDevice()) {
return New(*metadata.get(), &opener);
}
@@ -210,8 +211,11 @@
}
void MetadataBuilder::OverrideABForTesting(bool ab_device) {
- sABOverrideSet = true;
- sABOverrideValue = ab_device;
+ sABOverride = ab_device;
+}
+
+void MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(bool retrofit) {
+ sRetrofitDap = retrofit;
}
MetadataBuilder::MetadataBuilder() : auto_slot_suffixing_(false) {
@@ -580,7 +584,8 @@
CHECK_NE(sectors_per_block, 0);
CHECK(sectors_needed % sectors_per_block == 0);
- if (IsABDevice() && !IsRetrofitDevice() && GetPartitionSlotSuffix(partition->name()) == "_b") {
+ if (IsABDevice() && !IsRetrofitMetadata() &&
+ GetPartitionSlotSuffix(partition->name()) == "_b") {
// Allocate "a" partitions top-down and "b" partitions bottom-up, to
// minimize fragmentation during OTA.
free_regions = PrioritizeSecondHalfOfSuper(free_regions);
@@ -1044,14 +1049,21 @@
auto_slot_suffixing_ = true;
}
-bool MetadataBuilder::IsABDevice() const {
- if (sABOverrideSet) {
- return sABOverrideValue;
+bool MetadataBuilder::IsABDevice() {
+ if (sABOverride.has_value()) {
+ return *sABOverride;
}
return !android::base::GetProperty("ro.boot.slot_suffix", "").empty();
}
-bool MetadataBuilder::IsRetrofitDevice() const {
+bool MetadataBuilder::IsRetrofitDynamicPartitionsDevice() {
+ if (sRetrofitDap.has_value()) {
+ return *sRetrofitDap;
+ }
+ return android::base::GetBoolProperty("ro.boot.dynamic_partitions_retrofit", false);
+}
+
+bool MetadataBuilder::IsRetrofitMetadata() const {
return GetBlockDevicePartitionName(block_devices_[0]) != LP_METADATA_DEFAULT_PARTITION_NAME;
}
diff --git a/fs_mgr/liblp/builder_test.cpp b/fs_mgr/liblp/builder_test.cpp
index 34c68d4..377ec68 100644
--- a/fs_mgr/liblp/builder_test.cpp
+++ b/fs_mgr/liblp/builder_test.cpp
@@ -27,20 +27,28 @@
class Environment : public ::testing::Environment {
public:
- void SetUp() override { MetadataBuilder::OverrideABForTesting(false); }
+ void SetUp() override {
+ MetadataBuilder::OverrideABForTesting(false);
+ MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(false);
+ }
};
int main(int argc, char** argv) {
- std::unique_ptr<Environment> env(new Environment);
- ::testing::AddGlobalTestEnvironment(env.get());
+ ::testing::AddGlobalTestEnvironment(new Environment);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
class BuilderTest : public ::testing::Test {
public:
- void SetUp() override { MetadataBuilder::OverrideABForTesting(false); }
- void TearDown() override { MetadataBuilder::OverrideABForTesting(false); }
+ void SetUp() override {
+ MetadataBuilder::OverrideABForTesting(false);
+ MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(false);
+ }
+ void TearDown() override {
+ MetadataBuilder::OverrideABForTesting(false);
+ MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(false);
+ }
};
TEST_F(BuilderTest, BuildBasic) {
diff --git a/fs_mgr/liblp/include/liblp/builder.h b/fs_mgr/liblp/include/liblp/builder.h
index e70c552..a2221ef 100644
--- a/fs_mgr/liblp/include/liblp/builder.h
+++ b/fs_mgr/liblp/include/liblp/builder.h
@@ -199,6 +199,9 @@
// Used by the test harness to override whether the device is "A/B".
static void OverrideABForTesting(bool ab_device);
+ // Used by the test harness to override whether the device is "retrofitting dynamic partitions".
+ static void OverrideRetrofitDynamicParititonsForTesting(bool retrofit);
+
// Define a new partition group. By default there is one group called
// "default", with an unrestricted size. A non-zero size will restrict the
// total space used by all partitions in the group.
@@ -306,8 +309,16 @@
void ImportExtents(Partition* dest, const LpMetadata& metadata,
const LpMetadataPartition& source);
bool ImportPartition(const LpMetadata& metadata, const LpMetadataPartition& source);
- bool IsABDevice() const;
- bool IsRetrofitDevice() const;
+
+ // Return true if the device is an AB device.
+ static bool IsABDevice();
+
+ // Return true if the device is retrofitting dynamic partitions.
+ static bool IsRetrofitDynamicPartitionsDevice();
+
+ // Return true if "this" metadata represents a metadata on a retrofit device.
+ bool IsRetrofitMetadata() const;
+
bool ValidatePartitionGroups() const;
struct Interval {
@@ -336,8 +347,8 @@
const std::vector<Interval>& free_list,
uint64_t sectors_needed) const;
- static bool sABOverrideValue;
- static bool sABOverrideSet;
+ static std::optional<bool> sABOverride;
+ static std::optional<bool> sRetrofitDap;
LpMetadataGeometry geometry_;
LpMetadataHeader header_;
diff --git a/fs_mgr/liblp/io_test.cpp b/fs_mgr/liblp/io_test.cpp
index fcef1f0..70dd85f 100644
--- a/fs_mgr/liblp/io_test.cpp
+++ b/fs_mgr/liblp/io_test.cpp
@@ -664,6 +664,8 @@
}
TEST(liblp, UpdateRetrofit) {
+ MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(true);
+
unique_ptr<MetadataBuilder> builder = CreateDefaultBuilder();
ASSERT_NE(builder, nullptr);
ASSERT_TRUE(AddDefaultPartitions(builder.get()));
@@ -693,6 +695,8 @@
}
TEST(liblp, UpdateNonRetrofit) {
+ MetadataBuilder::OverrideRetrofitDynamicParititonsForTesting(false);
+
unique_fd fd = CreateFlashedDisk();
ASSERT_GE(fd, 0);
diff --git a/fs_mgr/libsnapshot/Android.bp b/fs_mgr/libsnapshot/Android.bp
new file mode 100644
index 0000000..3a08049
--- /dev/null
+++ b/fs_mgr/libsnapshot/Android.bp
@@ -0,0 +1,36 @@
+//
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+cc_library {
+ name: "libsnapshot",
+ recovery_available: true,
+ defaults: ["fs_mgr_defaults"],
+ cppflags: [
+ "-D_FILE_OFFSET_BITS=64",
+ ],
+ srcs: [
+ "snapshot.cpp",
+ ],
+ shared_libs: [
+ "libbase",
+ "liblog",
+ ],
+ static_libs: [
+ "libdm",
+ "libext2_uuid",
+ ],
+ export_include_dirs: ["include"],
+}
diff --git a/fs_mgr/libsnapshot/OWNERS b/fs_mgr/libsnapshot/OWNERS
new file mode 100644
index 0000000..0cfa7e4
--- /dev/null
+++ b/fs_mgr/libsnapshot/OWNERS
@@ -0,0 +1,2 @@
+dvander@google.com
+elsk@google.com
diff --git a/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h b/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h
new file mode 100644
index 0000000..5cfd7fa
--- /dev/null
+++ b/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h
@@ -0,0 +1,88 @@
+// 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 <stdint.h>
+
+#include <chrono>
+#include <memory>
+#include <string>
+
+namespace android {
+namespace snapshot {
+
+enum class UpdateStatus {
+ // No update or merge is in progress.
+ None,
+
+ // An update is pending, but has not been successfully booted yet.
+ Unverified,
+
+ // The kernel is merging in the background.
+ Merging,
+
+ // Merging is complete, and needs to be acknowledged.
+ MergeCompleted
+};
+
+class SnapshotManager final {
+ public:
+ // Return a new SnapshotManager instance, or null on error.
+ static std::unique_ptr<SnapshotManager> New();
+
+ // Create a new snapshot device with the given name, base device, and COW device
+ // size. The new device path will be returned in |dev_path|. If timeout_ms is
+ // greater than zero, this function will wait the given amount of time for
+ // |dev_path| to become available, and fail otherwise. If timeout_ms is 0, then
+ // no wait will occur and |dev_path| may not yet exist on return.
+ bool CreateSnapshot(const std::string& name, const std::string& base_device, uint64_t cow_size,
+ std::string* dev_path, const std::chrono::milliseconds& timeout_ms);
+
+ // Map a snapshot device that was previously created with CreateSnapshot.
+ // If a merge was previously initiated, the device-mapper table will have a
+ // snapshot-merge target instead of a snapshot target. The timeout parameter
+ // is the same as in CreateSnapshotDevice.
+ bool MapSnapshotDevice(const std::string& name, const std::string& base_device,
+ const std::chrono::milliseconds& timeout_ms, std::string* dev_path);
+
+ // Unmap a snapshot device previously mapped with MapSnapshotDevice().
+ bool UnmapSnapshotDevice(const std::string& name);
+
+ // Remove the backing copy-on-write image for the named snapshot. If the
+ // device is still mapped, this will attempt an Unmap, and fail if the
+ // unmap fails.
+ bool DeleteSnapshot(const std::string& name);
+
+ // Initiate a merge on all snapshot devices. This should only be used after an
+ // update has been marked successful after booting.
+ bool InitiateMerge();
+
+ // Wait for the current merge to finish, then perform cleanup when it
+ // completes. It is necessary to call this after InitiateMerge(), or when
+ // a merge is detected for the first time after boot.
+ bool WaitForMerge();
+
+ // Find the status of the current update, if any.
+ //
+ // |progress| depends on the returned status:
+ // None: 0
+ // Unverified: 0
+ // Merging: Value in the range [0, 100)
+ // MergeCompleted: 100
+ UpdateStatus GetUpdateStatus(double* progress);
+};
+
+} // namespace snapshot
+} // namespace android
diff --git a/fs_mgr/libsnapshot/snapshot.cpp b/fs_mgr/libsnapshot/snapshot.cpp
new file mode 100644
index 0000000..3e80239
--- /dev/null
+++ b/fs_mgr/libsnapshot/snapshot.cpp
@@ -0,0 +1,72 @@
+// 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 <libsnapshot/snapshot.h>
+
+namespace android {
+namespace snapshot {
+
+std::unique_ptr<SnapshotManager> SnapshotManager::New() {
+ return std::make_unique<SnapshotManager>();
+}
+
+bool SnapshotManager::CreateSnapshot(const std::string& name, const std::string& base_device,
+ uint64_t cow_size, std::string* dev_path,
+ const std::chrono::milliseconds& timeout_ms) {
+ // (1) Create COW device using libgsi_image.
+ // (2) Create snapshot device using libdm + DmTargetSnapshot.
+ // (3) Record partition in /metadata/ota.
+ (void)name;
+ (void)base_device;
+ (void)cow_size;
+ (void)dev_path;
+ (void)timeout_ms;
+ return false;
+}
+
+bool SnapshotManager::MapSnapshotDevice(const std::string& name, const std::string& base_device,
+ const std::chrono::milliseconds& timeout_ms,
+ std::string* dev_path) {
+ (void)name;
+ (void)base_device;
+ (void)dev_path;
+ (void)timeout_ms;
+ return false;
+}
+
+bool SnapshotManager::UnmapSnapshotDevice(const std::string& name) {
+ (void)name;
+ return false;
+}
+
+bool SnapshotManager::DeleteSnapshot(const std::string& name) {
+ (void)name;
+ return false;
+}
+
+bool SnapshotManager::InitiateMerge() {
+ return false;
+}
+
+bool SnapshotManager::WaitForMerge() {
+ return false;
+}
+
+UpdateStatus SnapshotManager::GetUpdateStatus(double* progress) {
+ *progress = 0.0f;
+ return UpdateStatus::None;
+}
+
+} // namespace snapshot
+} // namespace android
diff --git a/fs_mgr/tests/adb-remount-test.sh b/fs_mgr/tests/adb-remount-test.sh
index c2a0f33..642f2c1 100755
--- a/fs_mgr/tests/adb-remount-test.sh
+++ b/fs_mgr/tests/adb-remount-test.sh
@@ -15,10 +15,13 @@
adb remount tests
---help This help
---serial Specify device (must if multiple are present)
---color Dress output with highlighting colors
---print-time Report the test duration
+--color Dress output with highlighting colors
+--help This help
+--no-wait-screen Do not wait for display screen to settle
+--print-time Report the test duration
+--serial Specify device (must if multiple are present)
+--wait-adb <duration> adb wait timeout
+--wait-fastboot <duration> fastboot wait timeout
Conditions:
- Must be a userdebug build.
@@ -53,6 +56,7 @@
ADB_WAIT=4m
FASTBOOT_WAIT=2m
+screen_wait=true
##
## Helper Functions
@@ -185,7 +189,7 @@
[ "USAGE: adb_cat <file> >stdout
Returns: content of file to stdout with carriage returns skipped,
- true of the file exists" ]
+ true if the file exists" ]
adb_cat() {
local OUTPUT="`adb_sh cat ${1} </dev/null 2>&1`"
local ret=${?}
@@ -193,6 +197,17 @@
return ${ret}
}
+[ "USAGE: adb_ls <dirfile> >stdout
+
+Returns: filename or directoru content to stdout with carriage returns skipped,
+ true if the ls had no errors" ]
+adb_ls() {
+ local OUTPUT="`adb_sh ls ${1} </dev/null 2>/dev/null`"
+ local ret=${?}
+ echo "${OUTPUT}" | tr -d '\r'
+ return ${ret}
+}
+
[ "USAGE: adb_reboot
Returns: true if the reboot command succeeded" ]
@@ -436,6 +451,10 @@
-n - echo newline at exit
TIMEOUT - default `format_duration ${wait_for_screen_timeout}`" ]
wait_for_screen() {
+ if ! ${screen_wait}; then
+ adb_wait
+ return
+ fi
exit_function=true
if [ X"-n" = X"${1}" ]; then
exit_function=echo
@@ -743,6 +762,9 @@
OPTIONS=`getopt --alternative --unquoted \
--longoptions help,serial:,colour,color,no-colour,no-color \
+ --longoptions wait-adb:,wait-fastboot: \
+ --longoptions wait-screen,wait-display \
+ --longoptions no-wait-screen,no-wait-display \
--longoptions gtest_print_time,print-time \
-- "?hs:" ${*}` ||
( echo "${USAGE}" >&2 ; false ) ||
@@ -766,9 +788,23 @@
--no-color | --no-colour)
color=false
;;
+ --no-wait-display | --no-wait-screen)
+ screen_wait=false
+ ;;
+ --wait-display | --wait-screen)
+ screen_wait=true
+ ;;
--print-time | --gtest_print_time)
print_time=true
;;
+ --wait-adb)
+ ADB_WAIT=${2}
+ shift
+ ;;
+ --wait-fastboot)
+ FASTBOOT_WAIT=${2}
+ shift
+ ;;
--)
shift
break
@@ -1145,10 +1181,14 @@
A="Hello World! $(date)"
echo "${A}" | adb_sh cat - ">/system/hello"
+echo "${A}" | adb_sh cat - ">/system/priv-app/hello"
echo "${A}" | adb_sh cat - ">/vendor/hello"
B="`adb_cat /system/hello`" ||
- die "sytem hello"
+ die "system hello"
check_eq "${A}" "${B}" /system before reboot
+B="`adb_cat /system/priv-app/hello`" ||
+ die "system priv-app hello"
+check_eq "${A}" "${B}" /system/priv-app before reboot
B="`adb_cat /vendor/hello`" ||
die "vendor hello"
check_eq "${A}" "${B}" /vendor before reboot
@@ -1230,6 +1270,13 @@
fi
B="`adb_cat /system/hello`"
check_eq "${A}" "${B}" /system after reboot
+# If overlayfs has a nested security problem, this will fail.
+B="`adb_ls /system/`" ||
+ dir "adb ls /system"
+[ X"${B}" != X"${B#*priv-app}" ] ||
+ dir "adb ls /system/priv-app"
+B="`adb_cat /system/priv-app/hello`"
+check_eq "${A}" "${B}" /system/priv-app after reboot
echo "${GREEN}[ OK ]${NORMAL} /system content remains after reboot" >&2
# Only root can read vendor if sepolicy permissions are as expected.
adb_root ||
@@ -1351,6 +1398,12 @@
fi
B="`adb_cat /system/hello`"
check_eq "${A}" "${B}" system after flash vendor
+ B="`adb_ls /system/`" ||
+ dir "adb ls /system"
+ [ X"${B}" != X"${B#*priv-app}" ] ||
+ dir "adb ls /system/priv-app"
+ B="`adb_cat /system/priv-app/hello`"
+ check_eq "${A}" "${B}" system/priv-app after flash vendor
adb_root ||
die "adb root"
B="`adb_cat /vendor/hello`"
@@ -1392,15 +1445,17 @@
echo "${H}"
[ ${err} = 0 ] &&
( adb_sh rm /vendor/hello </dev/null 2>/dev/null || true ) &&
- adb_sh rm /system/hello </dev/null ||
+ adb_sh rm /system/hello /system/priv-app/hello </dev/null ||
( [ -n "${L}" ] && echo "${L}" && false ) ||
die -t ${T} "cleanup hello"
B="`adb_cat /system/hello`"
check_eq "cat: /system/hello: No such file or directory" "${B}" after rm
+B="`adb_cat /system/priv-app/hello`"
+check_eq "cat: /system/priv-app/hello: No such file or directory" "${B}" after rm
B="`adb_cat /vendor/hello`"
check_eq "cat: /vendor/hello: No such file or directory" "${B}" after rm
-if [ -n "${scratch_partition}" ]; then
+if ${is_bootloader_fastboot} && [ -n "${scratch_partition}" ]; then
echo "${GREEN}[ RUN ]${NORMAL} test fastboot flash to ${scratch_partition} recovery" >&2
@@ -1542,7 +1597,9 @@
adb_wait ${ADB_WAIT} ||
die "adb remount -R"
if [ "orange" != "`get_property ro.boot.verifiedbootstate`" -o \
- "2" = "`get_property partition.system.verified`" ]; then
+ "2" = "`get_property partition.system.verified`" ] &&
+ [ -n "`get_property ro.boot.verifiedbootstate`" -o \
+ -n "`get_property partition.system.verified`" ]; then
die "remount -R command failed to disable verity"
fi
diff --git a/fs_mgr/tools/dmctl.cpp b/fs_mgr/tools/dmctl.cpp
index 7e6ad5b..2738457 100644
--- a/fs_mgr/tools/dmctl.cpp
+++ b/fs_mgr/tools/dmctl.cpp
@@ -49,7 +49,10 @@
std::cerr << " delete <dm-name>" << std::endl;
std::cerr << " list <devices | targets> [-v]" << std::endl;
std::cerr << " getpath <dm-name>" << std::endl;
+ std::cerr << " info <dm-name>" << std::endl;
std::cerr << " status <dm-name>" << std::endl;
+ std::cerr << " resume <dm-name>" << std::endl;
+ std::cerr << " suspend <dm-name>" << std::endl;
std::cerr << " table <dm-name>" << std::endl;
std::cerr << " help" << std::endl;
std::cerr << std::endl;
@@ -194,19 +197,12 @@
char** argv_;
};
-static int DmCreateCmdHandler(int argc, char** argv) {
- if (argc < 1) {
- std::cerr << "Usage: dmctl create <dm-name> [-ro] <targets...>" << std::endl;
- return -EINVAL;
- }
- std::string name = argv[0];
-
+static bool parse_table_args(DmTable* table, int argc, char** argv) {
// Parse extended options first.
- DmTable table;
int arg_index = 1;
while (arg_index < argc && argv[arg_index][0] == '-') {
if (strcmp(argv[arg_index], "-ro") == 0) {
- table.set_readonly(true);
+ table->set_readonly(true);
arg_index++;
} else {
std::cerr << "Unrecognized option: " << argv[arg_index] << std::endl;
@@ -218,15 +214,30 @@
TargetParser parser(argc - arg_index, argv + arg_index);
while (parser.More()) {
std::unique_ptr<DmTarget> target = parser.Next();
- if (!target || !table.AddTarget(std::move(target))) {
+ if (!target || !table->AddTarget(std::move(target))) {
return -EINVAL;
}
}
- if (table.num_targets() == 0) {
+ if (table->num_targets() == 0) {
std::cerr << "Must define at least one target." << std::endl;
return -EINVAL;
}
+ return 0;
+}
+
+static int DmCreateCmdHandler(int argc, char** argv) {
+ if (argc < 1) {
+ std::cerr << "Usage: dmctl create <dm-name> [-ro] <targets...>" << std::endl;
+ return -EINVAL;
+ }
+ std::string name = argv[0];
+
+ DmTable table;
+ int ret = parse_table_args(&table, argc, argv);
+ if (ret) {
+ return ret;
+ }
DeviceMapper& dm = DeviceMapper::Instance();
if (!dm.CreateDevice(name, table)) {
@@ -252,6 +263,27 @@
return 0;
}
+static int DmReplaceCmdHandler(int argc, char** argv) {
+ if (argc < 1) {
+ std::cerr << "Usage: dmctl replace <dm-name> <targets...>" << std::endl;
+ return -EINVAL;
+ }
+ std::string name = argv[0];
+
+ DmTable table;
+ int ret = parse_table_args(&table, argc, argv);
+ if (ret) {
+ return ret;
+ }
+
+ DeviceMapper& dm = DeviceMapper::Instance();
+ if (!dm.LoadTableAndActivate(name, table)) {
+ std::cerr << "Failed to replace device-mapper table to: " << name << std::endl;
+ return -EIO;
+ }
+ return 0;
+}
+
static int DmListTargets(DeviceMapper& dm, [[maybe_unused]] int argc,
[[maybe_unused]] char** argv) {
std::vector<DmTargetTypeInfo> targets;
@@ -357,6 +389,41 @@
return 0;
}
+static int InfoCmdHandler(int argc, char** argv) {
+ if (argc != 1) {
+ std::cerr << "Invalid arguments, see \'dmctl help\'" << std::endl;
+ return -EINVAL;
+ }
+
+ DeviceMapper& dm = DeviceMapper::Instance();
+ auto info = dm.GetDetailedInfo(argv[0]);
+ if (!info) {
+ std::cerr << "Invalid device \"" << argv[0] << "\"." << std::endl;
+ return -EINVAL;
+ }
+
+ constexpr int spacing = 14;
+ std::cout << std::left << std::setw(spacing) << "device"
+ << ": " << argv[0] << std::endl;
+ std::cout << std::left << std::setw(spacing) << "active"
+ << ": " << std::boolalpha << !info->IsSuspended() << std::endl;
+ std::cout << std::left << std::setw(spacing) << "access"
+ << ": ";
+ if (info->IsReadOnly()) {
+ std::cout << "ro ";
+ } else {
+ std::cout << "rw ";
+ }
+ std::cout << std::endl;
+ std::cout << std::left << std::setw(spacing) << "activeTable"
+ << ": " << std::boolalpha << info->IsActiveTablePresent() << std::endl;
+ std::cout << std::left << std::setw(spacing) << "inactiveTable"
+ << ": " << std::boolalpha << info->IsInactiveTablePresent() << std::endl;
+ std::cout << std::left << std::setw(spacing) << "bufferFull"
+ << ": " << std::boolalpha << info->IsBufferFull() << std::endl;
+ return 0;
+}
+
static int DumpTable(const std::string& mode, int argc, char** argv) {
if (argc != 1) {
std::cerr << "Invalid arguments, see \'dmctl help\'" << std::endl;
@@ -399,15 +466,47 @@
return DumpTable("status", argc, argv);
}
+static int ResumeCmdHandler(int argc, char** argv) {
+ if (argc != 1) {
+ std::cerr << "Invalid arguments, see \'dmctl help\'" << std::endl;
+ return -EINVAL;
+ }
+
+ DeviceMapper& dm = DeviceMapper::Instance();
+ if (!dm.ChangeState(argv[0], DmDeviceState::ACTIVE)) {
+ std::cerr << "Could not resume device \"" << argv[0] << "\"." << std::endl;
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static int SuspendCmdHandler(int argc, char** argv) {
+ if (argc != 1) {
+ std::cerr << "Invalid arguments, see \'dmctl help\'" << std::endl;
+ return -EINVAL;
+ }
+
+ DeviceMapper& dm = DeviceMapper::Instance();
+ if (!dm.ChangeState(argv[0], DmDeviceState::SUSPENDED)) {
+ std::cerr << "Could not suspend device \"" << argv[0] << "\"." << std::endl;
+ return -EINVAL;
+ }
+ return 0;
+}
+
static std::map<std::string, std::function<int(int, char**)>> cmdmap = {
// clang-format off
{"create", DmCreateCmdHandler},
{"delete", DmDeleteCmdHandler},
+ {"replace", DmReplaceCmdHandler},
{"list", DmListCmdHandler},
{"help", HelpCmdHandler},
{"getpath", GetPathCmdHandler},
+ {"info", InfoCmdHandler},
{"table", TableCmdHandler},
{"status", StatusCmdHandler},
+ {"resume", ResumeCmdHandler},
+ {"suspend", SuspendCmdHandler},
// clang-format on
};
diff --git a/healthd/Android.bp b/healthd/Android.bp
index 2cf6be9..53be526 100644
--- a/healthd/Android.bp
+++ b/healthd/Android.bp
@@ -110,3 +110,55 @@
"libutils",
],
}
+
+sysprop_library {
+ name: "charger_sysprop",
+ srcs: ["charger.sysprop"],
+ property_owner: "Platform",
+ api_packages: ["android.sysprop"],
+}
+
+cc_library_static {
+ name: "libhealthd_draw",
+ export_include_dirs: ["."],
+ static_libs: [
+ "libcharger_sysprop",
+ "libminui",
+ ],
+ shared_libs: [
+ "libbase",
+ ],
+ header_libs: ["libbatteryservice_headers"],
+
+ srcs: ["healthd_draw.cpp"],
+}
+
+cc_library_static {
+ name: "libhealthd_charger",
+ local_include_dirs: ["include"],
+ export_include_dirs: [".", "include"],
+
+ static_libs: [
+ "android.hardware.health@2.0-impl",
+ "android.hardware.health@1.0-convert",
+ "libcharger_sysprop",
+ "libhealthstoragedefault",
+ "libhealthd_draw",
+ "libminui",
+ ],
+
+ shared_libs: [
+ "android.hardware.health@2.0",
+ "libbase",
+ "libcutils",
+ "liblog",
+ "libpng",
+ "libsuspend",
+ "libutils",
+ ],
+
+ srcs: [
+ "healthd_mode_charger.cpp",
+ "AnimationParser.cpp",
+ ],
+}
diff --git a/healthd/Android.mk b/healthd/Android.mk
index d18f15a..b87f3c7 100644
--- a/healthd/Android.mk
+++ b/healthd/Android.mk
@@ -2,74 +2,6 @@
LOCAL_PATH := $(call my-dir)
-### libhealthd_draw ###
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libhealthd_draw
-
-LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)
-LOCAL_STATIC_LIBRARIES := libminui
-LOCAL_SHARED_LIBRARIES := libbase
-LOCAL_SRC_FILES := healthd_draw.cpp
-
-ifneq ($(TARGET_HEALTHD_DRAW_SPLIT_SCREEN),)
-LOCAL_CFLAGS += -DHEALTHD_DRAW_SPLIT_SCREEN=$(TARGET_HEALTHD_DRAW_SPLIT_SCREEN)
-else
-LOCAL_CFLAGS += -DHEALTHD_DRAW_SPLIT_SCREEN=0
-endif
-
-ifneq ($(TARGET_HEALTHD_DRAW_SPLIT_OFFSET),)
-LOCAL_CFLAGS += -DHEALTHD_DRAW_SPLIT_OFFSET=$(TARGET_HEALTHD_DRAW_SPLIT_OFFSET)
-else
-LOCAL_CFLAGS += -DHEALTHD_DRAW_SPLIT_OFFSET=0
-endif
-
-LOCAL_HEADER_LIBRARIES := libbatteryservice_headers
-
-include $(BUILD_STATIC_LIBRARY)
-
-### libhealthd_charger ###
-include $(CLEAR_VARS)
-
-LOCAL_CFLAGS := -Werror
-ifeq ($(strip $(BOARD_CHARGER_DISABLE_INIT_BLANK)),true)
-LOCAL_CFLAGS += -DCHARGER_DISABLE_INIT_BLANK
-endif
-ifeq ($(strip $(BOARD_CHARGER_ENABLE_SUSPEND)),true)
-LOCAL_CFLAGS += -DCHARGER_ENABLE_SUSPEND
-endif
-
-LOCAL_SRC_FILES := \
- healthd_mode_charger.cpp \
- AnimationParser.cpp
-
-LOCAL_MODULE := libhealthd_charger
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
-LOCAL_EXPORT_C_INCLUDE_DIRS := \
- $(LOCAL_PATH) \
- $(LOCAL_PATH)/include
-
-LOCAL_STATIC_LIBRARIES := \
- android.hardware.health@2.0-impl \
- android.hardware.health@1.0-convert \
- libhealthstoragedefault \
- libhealthd_draw \
- libminui \
-
-LOCAL_SHARED_LIBRARIES := \
- android.hardware.health@2.0 \
- libbase \
- libcutils \
- liblog \
- libpng \
- libutils \
-
-ifeq ($(strip $(BOARD_CHARGER_ENABLE_SUSPEND)),true)
-LOCAL_SHARED_LIBRARIES += libsuspend
-endif
-
-include $(BUILD_STATIC_LIBRARY)
-
### charger ###
include $(CLEAR_VARS)
ifeq ($(strip $(BOARD_CHARGER_NO_UI)),true)
@@ -83,18 +15,17 @@
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_CFLAGS := -Werror
-ifeq ($(strip $(LOCAL_CHARGER_NO_UI)),true)
-LOCAL_CFLAGS += -DCHARGER_NO_UI
-endif
CHARGER_STATIC_LIBRARIES := \
android.hardware.health@2.0-impl \
android.hardware.health@1.0-convert \
libbinderthreadstate \
+ libcharger_sysprop \
libhidltransport \
libhidlbase \
libhwbinder_noltopgo \
libhealthstoragedefault \
+ libminui \
libvndksupport \
libhealthd_charger \
libhealthd_charger_nops \
@@ -106,18 +37,12 @@
libbase \
libcutils \
libjsoncpp \
+ libpng \
libprocessgroup \
liblog \
libutils \
-ifneq ($(strip $(LOCAL_CHARGER_NO_UI)),true)
-CHARGER_STATIC_LIBRARIES += libminui
-CHARGER_SHARED_LIBRARIES += libpng
-endif
-
-ifeq ($(strip $(BOARD_CHARGER_ENABLE_SUSPEND)),true)
CHARGER_SHARED_LIBRARIES += libsuspend
-endif
LOCAL_STATIC_LIBRARIES := $(CHARGER_STATIC_LIBRARIES)
LOCAL_SHARED_LIBRARIES := $(CHARGER_SHARED_LIBRARIES)
@@ -141,8 +66,7 @@
LOCAL_MODULE_STEM := charger
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
-LOCAL_CFLAGS := -Wall -Werror
-LOCAL_CFLAGS += -DCHARGER_NO_UI
+LOCAL_CFLAGS := -Wall -Werror -DCHARGER_FORCE_NO_UI=1
# charger.recovery doesn't link against libhealthd_{charger,draw} or libminui, since it doesn't need
# any UI support.
@@ -150,6 +74,7 @@
android.hardware.health@2.0-impl \
android.hardware.health@1.0-convert \
libbinderthreadstate \
+ libcharger_sysprop \
libhidltransport \
libhidlbase \
libhwbinder_noltopgo \
@@ -176,7 +101,7 @@
include $(CLEAR_VARS)
LOCAL_MODULE := charger_test
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
-LOCAL_CFLAGS := -Wall -Werror -DCHARGER_NO_UI
+LOCAL_CFLAGS := -Wall -Werror
LOCAL_STATIC_LIBRARIES := $(CHARGER_STATIC_LIBRARIES)
LOCAL_SHARED_LIBRARIES := $(CHARGER_SHARED_LIBRARIES)
LOCAL_SRC_FILES := \
diff --git a/healthd/AnimationParser.cpp b/healthd/AnimationParser.cpp
index 864038b..fde3b95 100644
--- a/healthd/AnimationParser.cpp
+++ b/healthd/AnimationParser.cpp
@@ -84,7 +84,6 @@
static constexpr const char* fail_prefix = "fail: ";
static constexpr const char* clock_prefix = "clock_display: ";
static constexpr const char* percent_prefix = "percent_display: ";
- static constexpr const char* frame_prefix = "frame: ";
std::vector<animation::frame> frames;
diff --git a/healthd/api/current.txt b/healthd/api/current.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/healthd/api/current.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/healthd/api/removed.txt b/healthd/api/removed.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/healthd/api/removed.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/healthd/api/system-current.txt b/healthd/api/system-current.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/healthd/api/system-current.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/healthd/api/system-removed.txt b/healthd/api/system-removed.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/healthd/api/system-removed.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/healthd/api/test-current.txt b/healthd/api/test-current.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/healthd/api/test-current.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/healthd/api/test-removed.txt b/healthd/api/test-removed.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/healthd/api/test-removed.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/healthd/charger.cpp b/healthd/charger.cpp
index 085cceb..58ed416 100644
--- a/healthd/charger.cpp
+++ b/healthd/charger.cpp
@@ -14,13 +14,18 @@
* limitations under the License.
*/
+#include "charger.sysprop.h"
#include "healthd_mode_charger.h"
#include "healthd_mode_charger_nops.h"
-int main(int argc, char** argv) {
-#ifdef CHARGER_NO_UI
- return healthd_charger_nops(argc, argv);
-#else
- return healthd_charger_main(argc, argv);
+#ifndef CHARGER_FORCE_NO_UI
+#define CHARGER_FORCE_NO_UI 0
#endif
+
+int main(int argc, char** argv) {
+ if (CHARGER_FORCE_NO_UI || android::sysprop::ChargerProperties::no_ui().value_or(false)) {
+ return healthd_charger_nops(argc, argv);
+ } else {
+ return healthd_charger_main(argc, argv);
+ }
}
diff --git a/healthd/charger.sysprop b/healthd/charger.sysprop
new file mode 100644
index 0000000..b3f47a1
--- /dev/null
+++ b/healthd/charger.sysprop
@@ -0,0 +1,38 @@
+owner: Platform
+module: "android.sysprop.ChargerProperties"
+
+prop {
+ api_name: "draw_split_screen"
+ type: Boolean
+ prop_name: "ro.charger.draw_split_screen"
+ scope: Internal
+ access: Readonly
+}
+prop {
+ api_name: "draw_split_offset"
+ type: Long
+ prop_name: "ro.charger.draw_split_offset"
+ scope: Internal
+ access: Readonly
+}
+prop {
+ api_name: "disable_init_blank"
+ type: Boolean
+ prop_name: "ro.charger.disable_init_blank"
+ scope: Internal
+ access: Readonly
+}
+prop {
+ api_name: "enable_suspend"
+ type: Boolean
+ prop_name: "ro.charger.enable_suspend"
+ scope: Internal
+ access: Readonly
+}
+prop {
+ api_name: "no_ui"
+ type: Boolean
+ prop_name: "ro.charger.no_ui"
+ scope: Internal
+ access: Readonly
+}
diff --git a/healthd/healthd_draw.cpp b/healthd/healthd_draw.cpp
index 3da8bda..50eee19 100644
--- a/healthd/healthd_draw.cpp
+++ b/healthd/healthd_draw.cpp
@@ -18,15 +18,34 @@
#include <batteryservice/BatteryService.h>
#include <cutils/klog.h>
+#include "charger.sysprop.h"
#include "healthd_draw.h"
#define LOGE(x...) KLOG_ERROR("charger", x);
#define LOGW(x...) KLOG_WARNING("charger", x);
#define LOGV(x...) KLOG_DEBUG("charger", x);
+static bool get_split_screen() {
+ return android::sysprop::ChargerProperties::draw_split_screen().value_or(false);
+}
+
+static int get_split_offset() {
+ int64_t value = android::sysprop::ChargerProperties::draw_split_offset().value_or(0);
+ if (value < static_cast<int64_t>(std::numeric_limits<int>::min())) {
+ LOGW("draw_split_offset = %" PRId64 " overflow for an int; resetting to %d.\n", value,
+ std::numeric_limits<int>::min());
+ value = std::numeric_limits<int>::min();
+ }
+ if (value > static_cast<int64_t>(std::numeric_limits<int>::max())) {
+ LOGW("draw_split_offset = %" PRId64 " overflow for an int; resetting to %d.\n", value,
+ std::numeric_limits<int>::max());
+ value = std::numeric_limits<int>::max();
+ }
+ return static_cast<int>(value);
+}
+
HealthdDraw::HealthdDraw(animation* anim)
- : kSplitScreen(HEALTHD_DRAW_SPLIT_SCREEN),
- kSplitOffset(HEALTHD_DRAW_SPLIT_OFFSET) {
+ : kSplitScreen(get_split_screen()), kSplitOffset(get_split_offset()) {
int ret = gr_init();
if (ret < 0) {
diff --git a/healthd/healthd_mode_charger.cpp b/healthd/healthd_mode_charger.cpp
index edf34f7..d676083 100644
--- a/healthd/healthd_mode_charger.cpp
+++ b/healthd/healthd_mode_charger.cpp
@@ -43,11 +43,10 @@
#include <cutils/uevent.h>
#include <sys/reboot.h>
-#ifdef CHARGER_ENABLE_SUSPEND
#include <suspend/autosuspend.h>
-#endif
#include "AnimationParser.h"
+#include "charger.sysprop.h"
#include "healthd_draw.h"
#include <health2/Health.h>
@@ -264,18 +263,16 @@
LOGW("\n");
}
-#ifdef CHARGER_ENABLE_SUSPEND
static int request_suspend(bool enable) {
+ if (!android::sysprop::ChargerProperties::enable_suspend().value_or(false)) {
+ return 0;
+ }
+
if (enable)
return autosuspend_enable();
else
return autosuspend_disable();
}
-#else
-static int request_suspend(bool /*enable*/) {
- return 0;
-}
-#endif
static void kick_animation(animation* anim) {
anim->run = true;
@@ -321,10 +318,10 @@
healthd_draw.reset(new HealthdDraw(batt_anim));
-#ifndef CHARGER_DISABLE_INIT_BLANK
- healthd_draw->blank_screen(true);
- charger->screen_blanked = true;
-#endif
+ if (android::sysprop::ChargerProperties::disable_init_blank().value_or(false)) {
+ healthd_draw->blank_screen(true);
+ charger->screen_blanked = true;
+ }
}
/* animation is over, blank screen and leave */
diff --git a/init/Android.bp b/init/Android.bp
index ba60085..31e4173 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -227,9 +227,10 @@
genrule {
name: "generated_stub_builtin_function_map",
+ tool_files: ["host_builtin_map.py"],
out: ["generated_stub_builtin_function_map.h"],
- srcs: ["builtins.cpp"],
- cmd: "sed -n '/Builtin-function-map start/{:a;n;/Builtin-function-map end/q;p;ba}' $(in) | sed -e 's/do_[^}]*/do_stub/g' > $(out)",
+ srcs: ["builtins.cpp", "check_builtins.cpp"],
+ cmd: "$(location host_builtin_map.py) --builtins $(location builtins.cpp) --check_builtins $(location check_builtins.cpp) > $(out)",
}
cc_binary {
@@ -260,6 +261,7 @@
"action_manager.cpp",
"action_parser.cpp",
"capabilities.cpp",
+ "check_builtins.cpp",
"epoll.cpp",
"keychords.cpp",
"import_parser.cpp",
diff --git a/init/action.cpp b/init/action.cpp
index 8cf7645..65ba25d 100644
--- a/init/action.cpp
+++ b/init/action.cpp
@@ -35,9 +35,11 @@
builtin_arguments.args.resize(args.size());
builtin_arguments.args[0] = args[0];
for (std::size_t i = 1; i < args.size(); ++i) {
- if (!expand_props(args[i], &builtin_arguments.args[i])) {
- return Error() << "cannot expand '" << args[i] << "'";
+ auto expanded_arg = ExpandProps(args[i]);
+ if (!expanded_arg) {
+ return expanded_arg.error();
}
+ builtin_arguments.args[i] = std::move(*expanded_arg);
}
return function(builtin_arguments);
@@ -66,6 +68,30 @@
return RunBuiltinFunction(func_, args_, kInitContext);
}
+Result<void> Command::CheckCommand() const {
+ auto builtin_arguments = BuiltinArguments("host_init_verifier");
+
+ builtin_arguments.args.resize(args_.size());
+ builtin_arguments.args[0] = args_[0];
+ for (size_t i = 1; i < args_.size(); ++i) {
+ auto expanded_arg = ExpandProps(args_[i]);
+ if (!expanded_arg) {
+ if (expanded_arg.error().message().find("doesn't exist while expanding") !=
+ std::string::npos) {
+ // If we failed because we won't have a property, use an empty string, which is
+ // never returned from the parser, to indicate that this field cannot be checked.
+ builtin_arguments.args[i] = "";
+ } else {
+ return expanded_arg.error();
+ }
+ } else {
+ builtin_arguments.args[i] = std::move(*expanded_arg);
+ }
+ }
+
+ return func_(builtin_arguments);
+}
+
std::string Command::BuildCommandString() const {
return Join(args_, ' ');
}
@@ -80,17 +106,20 @@
filename_(filename),
line_(line) {}
-const KeywordFunctionMap* Action::function_map_ = nullptr;
+const BuiltinFunctionMap* Action::function_map_ = nullptr;
Result<void> Action::AddCommand(std::vector<std::string>&& args, int line) {
if (!function_map_) {
return Error() << "no function map available";
}
- auto function = function_map_->FindFunction(args);
- if (!function) return Error() << function.error();
+ auto map_result = function_map_->Find(args);
+ if (!map_result) {
+ return Error() << map_result.error();
+ }
- commands_.emplace_back(function->second, function->first, std::move(args), line);
+ commands_.emplace_back(map_result->function, map_result->run_in_subcontext, std::move(args),
+ line);
return {};
}
@@ -102,6 +131,18 @@
return commands_.size();
}
+size_t Action::CheckAllCommands() const {
+ size_t failures = 0;
+ for (const auto& command : commands_) {
+ if (auto result = command.CheckCommand(); !result) {
+ LOG(ERROR) << "Command '" << command.BuildCommandString() << "' (" << filename_ << ":"
+ << command.line() << ") failed: " << result.error();
+ ++failures;
+ }
+ }
+ return failures;
+}
+
void Action::ExecuteOneCommand(std::size_t command) const {
// We need a copy here since some Command execution may result in
// changing commands_ vector by importing .rc files through parser
diff --git a/init/action.h b/init/action.h
index 13b250a..1534bf9 100644
--- a/init/action.h
+++ b/init/action.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef _INIT_ACTION_H
-#define _INIT_ACTION_H
+#pragma once
#include <map>
#include <queue>
@@ -41,6 +40,7 @@
Result<void> InvokeFunc(Subcontext* subcontext) const;
std::string BuildCommandString() const;
+ Result<void> CheckCommand() const;
int line() const { return line_; }
@@ -63,7 +63,7 @@
Result<void> AddCommand(std::vector<std::string>&& args, int line);
void AddCommand(BuiltinFunction f, std::vector<std::string>&& args, int line);
- std::size_t NumCommands() const;
+ size_t NumCommands() const;
void ExecuteOneCommand(std::size_t command) const;
void ExecuteAllCommands() const;
bool CheckEvent(const EventTrigger& event_trigger) const;
@@ -71,11 +71,12 @@
bool CheckEvent(const BuiltinAction& builtin_action) const;
std::string BuildTriggersString() const;
void DumpState() const;
+ size_t CheckAllCommands() const;
bool oneshot() const { return oneshot_; }
const std::string& filename() const { return filename_; }
int line() const { return line_; }
- static void set_function_map(const KeywordFunctionMap* function_map) {
+ static void set_function_map(const BuiltinFunctionMap* function_map) {
function_map_ = function_map;
}
@@ -91,10 +92,8 @@
Subcontext* subcontext_;
std::string filename_;
int line_;
- static const KeywordFunctionMap* function_map_;
+ static const BuiltinFunctionMap* function_map_;
};
} // namespace init
} // namespace android
-
-#endif
diff --git a/init/action_manager.cpp b/init/action_manager.cpp
index 541c8f2..ebca762 100644
--- a/init/action_manager.cpp
+++ b/init/action_manager.cpp
@@ -23,6 +23,14 @@
ActionManager::ActionManager() : current_command_(0) {}
+size_t ActionManager::CheckAllCommands() {
+ size_t failures = 0;
+ for (const auto& action : actions_) {
+ failures += action->CheckAllCommands();
+ }
+ return failures;
+}
+
ActionManager& ActionManager::GetInstance() {
static ActionManager instance;
return instance;
diff --git a/init/action_manager.h b/init/action_manager.h
index 5f47a6d..a2b95ac 100644
--- a/init/action_manager.h
+++ b/init/action_manager.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef _INIT_ACTION_MANAGER_H
-#define _INIT_ACTION_MANAGER_H
+#pragma once
#include <string>
#include <vector>
@@ -32,6 +31,7 @@
// Exposed for testing
ActionManager();
+ size_t CheckAllCommands();
void AddAction(std::unique_ptr<Action> action);
void QueueEventTrigger(const std::string& trigger);
@@ -55,5 +55,3 @@
} // namespace init
} // namespace android
-
-#endif
diff --git a/init/builtins.cpp b/init/builtins.cpp
index ba2c7ac..e75f5cb 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -42,6 +42,7 @@
#include <sys/wait.h>
#include <unistd.h>
+#include <ApexProperties.sysprop.h>
#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/logging.h>
@@ -130,6 +131,13 @@
if (args.context != kInitContext) {
return Error() << "command 'class_start_post_data' only available in init context";
}
+ static bool is_apex_updatable = android::sysprop::ApexProperties::updatable().value_or(false);
+
+ if (!is_apex_updatable) {
+ // No need to start these on devices that don't support APEX, since they're not
+ // stopped either.
+ return {};
+ }
for (const auto& service : ServiceList::GetInstance()) {
if (service->classnames().count(args[1])) {
if (auto result = service->StartIfPostData(); !result) {
@@ -155,6 +163,11 @@
if (args.context != kInitContext) {
return Error() << "command 'class_reset_post_data' only available in init context";
}
+ static bool is_apex_updatable = android::sysprop::ApexProperties::updatable().value_or(false);
+ if (!is_apex_updatable) {
+ // No need to stop these on devices that don't support APEX.
+ return {};
+ }
ForEachServiceInClass(args[1], &Service::ResetIfPostData);
return {};
}
@@ -188,26 +201,26 @@
static Result<void> do_exec(const BuiltinArguments& args) {
auto service = Service::MakeTemporaryOneshotService(args.args);
if (!service) {
- return Error() << "Could not create exec service";
+ return Error() << "Could not create exec service: " << service.error();
}
- if (auto result = service->ExecStart(); !result) {
+ if (auto result = (*service)->ExecStart(); !result) {
return Error() << "Could not start exec service: " << result.error();
}
- ServiceList::GetInstance().AddService(std::move(service));
+ ServiceList::GetInstance().AddService(std::move(*service));
return {};
}
static Result<void> do_exec_background(const BuiltinArguments& args) {
auto service = Service::MakeTemporaryOneshotService(args.args);
if (!service) {
- return Error() << "Could not create exec background service";
+ return Error() << "Could not create exec background service: " << service.error();
}
- if (auto result = service->Start(); !result) {
+ if (auto result = (*service)->Start(); !result) {
return Error() << "Could not start exec background service: " << result.error();
}
- ServiceList::GetInstance().AddService(std::move(service));
+ ServiceList::GetInstance().AddService(std::move(*service));
return {};
}
@@ -331,7 +344,7 @@
if (args.size() == 5) {
gid = DecodeUid(args[4]);
if (!gid) {
- return Error() << "Unable to decode GID for '" << args[3] << "': " << gid.error();
+ return Error() << "Unable to decode GID for '" << args[4] << "': " << gid.error();
}
}
@@ -923,40 +936,17 @@
}
static Result<void> do_restorecon(const BuiltinArguments& args) {
+ auto restorecon_info = ParseRestorecon(args.args);
+ if (!restorecon_info) {
+ return restorecon_info.error();
+ }
+
+ const auto& [flag, paths] = *restorecon_info;
+
int ret = 0;
-
- struct flag_type {const char* name; int value;};
- static const flag_type flags[] = {
- {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
- {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
- {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
- {0, 0}
- };
-
- int flag = 0;
-
- bool in_flags = true;
- for (size_t i = 1; i < args.size(); ++i) {
- if (android::base::StartsWith(args[i], "--")) {
- if (!in_flags) {
- return Error() << "flags must precede paths";
- }
- bool found = false;
- for (size_t j = 0; flags[j].name; ++j) {
- if (args[i] == flags[j].name) {
- flag |= flags[j].value;
- found = true;
- break;
- }
- }
- if (!found) {
- return Error() << "bad flag " << args[i];
- }
- } else {
- in_flags = false;
- if (selinux_android_restorecon(args[i].c_str(), flag) < 0) {
- ret = errno;
- }
+ for (const auto& path : paths) {
+ if (selinux_android_restorecon(path.c_str(), flag) < 0) {
+ ret = errno;
}
}
@@ -1043,9 +1033,9 @@
const BuiltinArguments& args) {
auto service = Service::MakeTemporaryOneshotService(args.args);
if (!service) {
- return Error() << "Could not create exec service";
+ return Error() << "Could not create exec service: " << service.error();
}
- service->AddReapCallback([reboot_reason](const siginfo_t& siginfo) {
+ (*service)->AddReapCallback([reboot_reason](const siginfo_t& siginfo) {
if (siginfo.si_code != CLD_EXITED || siginfo.si_status != 0) {
// TODO (b/122850122): support this in gsi
if (fscrypt_is_native() && !android::gsi::IsGsiRunning()) {
@@ -1060,10 +1050,10 @@
}
}
});
- if (auto result = service->ExecStart(); !result) {
+ if (auto result = (*service)->ExecStart(); !result) {
return Error() << "Could not start exec service: " << result.error();
}
- ServiceList::GetInstance().AddService(std::move(service));
+ ServiceList::GetInstance().AddService(std::move(*service));
return {};
}
@@ -1139,10 +1129,10 @@
}
// Builtin-function-map start
-const BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
+const BuiltinFunctionMap& GetBuiltinFunctionMap() {
constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
// clang-format off
- static const Map builtin_functions = {
+ static const BuiltinFunctionMap builtin_functions = {
{"bootchart", {1, 1, {false, do_bootchart}}},
{"chmod", {2, 2, {true, do_chmod}}},
{"chown", {2, 3, {true, do_chown}}},
diff --git a/init/builtins.h b/init/builtins.h
index 7bbf6aa..f0ff1eb 100644
--- a/init/builtins.h
+++ b/init/builtins.h
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-#ifndef _INIT_BUILTINS_H
-#define _INIT_BUILTINS_H
+#pragma once
#include <functional>
#include <map>
@@ -31,18 +30,16 @@
using BuiltinFunction = std::function<Result<void>(const BuiltinArguments&)>;
-using KeywordFunctionMap = KeywordMap<std::pair<bool, BuiltinFunction>>;
-class BuiltinFunctionMap : public KeywordFunctionMap {
- public:
- BuiltinFunctionMap() {}
-
- private:
- const Map& map() const override;
+struct BuiltinFunctionMapValue {
+ bool run_in_subcontext;
+ BuiltinFunction function;
};
+using BuiltinFunctionMap = KeywordMap<BuiltinFunctionMapValue>;
+
+const BuiltinFunctionMap& GetBuiltinFunctionMap();
+
extern std::vector<std::string> late_import_paths;
} // namespace init
} // namespace android
-
-#endif
diff --git a/init/check_builtins.cpp b/init/check_builtins.cpp
new file mode 100644
index 0000000..3bd4774
--- /dev/null
+++ b/init/check_builtins.cpp
@@ -0,0 +1,197 @@
+/*
+ * 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.
+ */
+
+// Note that these check functions cannot check expanded arguments from properties, since they will
+// not know what those properties would be at runtime. They will be passed an empty string in the
+// situation that the input line had a property expansion without a default value, since an empty
+// string is otherwise an impossible value. They should therefore disregard checking empty
+// arguments.
+
+#include "check_builtins.h"
+
+#include <sys/time.h>
+
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
+#include <android-base/strings.h>
+
+#include "builtin_arguments.h"
+#include "rlimit_parser.h"
+#include "service.h"
+#include "util.h"
+
+using android::base::ParseInt;
+using android::base::StartsWith;
+
+#define ReturnIfAnyArgsEmpty() \
+ for (const auto& arg : args) { \
+ if (arg.empty()) { \
+ return {}; \
+ } \
+ }
+
+namespace android {
+namespace init {
+
+Result<void> check_chown(const BuiltinArguments& args) {
+ if (!args[1].empty()) {
+ auto uid = DecodeUid(args[1]);
+ if (!uid) {
+ return Error() << "Unable to decode UID for '" << args[1] << "': " << uid.error();
+ }
+ }
+
+ // GID is optional and pushes the index of path out by one if specified.
+ if (args.size() == 4 && !args[2].empty()) {
+ auto gid = DecodeUid(args[2]);
+ if (!gid) {
+ return Error() << "Unable to decode GID for '" << args[2] << "': " << gid.error();
+ }
+ }
+
+ return {};
+}
+
+Result<void> check_exec(const BuiltinArguments& args) {
+ ReturnIfAnyArgsEmpty();
+
+ auto result = Service::MakeTemporaryOneshotService(args.args);
+ if (!result) {
+ return result.error();
+ }
+
+ return {};
+}
+
+Result<void> check_exec_background(const BuiltinArguments& args) {
+ return check_exec(std::move(args));
+}
+
+Result<void> check_load_system_props(const BuiltinArguments& args) {
+ return Error() << "'load_system_props' is deprecated";
+}
+
+Result<void> check_loglevel(const BuiltinArguments& args) {
+ ReturnIfAnyArgsEmpty();
+
+ int log_level = -1;
+ ParseInt(args[1], &log_level);
+ if (log_level < 0 || log_level > 7) {
+ return Error() << "loglevel must be in the range of 0-7";
+ }
+ return {};
+}
+
+Result<void> check_mkdir(const BuiltinArguments& args) {
+ if (args.size() >= 4) {
+ if (!args[3].empty()) {
+ auto uid = DecodeUid(args[3]);
+ if (!uid) {
+ return Error() << "Unable to decode UID for '" << args[3] << "': " << uid.error();
+ }
+ }
+
+ if (args.size() == 5 && !args[4].empty()) {
+ auto gid = DecodeUid(args[4]);
+ if (!gid) {
+ return Error() << "Unable to decode GID for '" << args[4] << "': " << gid.error();
+ }
+ }
+ }
+
+ return {};
+}
+
+Result<void> check_restorecon(const BuiltinArguments& args) {
+ ReturnIfAnyArgsEmpty();
+
+ auto restorecon_info = ParseRestorecon(args.args);
+ if (!restorecon_info) {
+ return restorecon_info.error();
+ }
+
+ return {};
+}
+
+Result<void> check_restorecon_recursive(const BuiltinArguments& args) {
+ return check_restorecon(std::move(args));
+}
+
+Result<void> check_setprop(const BuiltinArguments& args) {
+ const std::string& name = args[1];
+ if (name.empty()) {
+ return {};
+ }
+ const std::string& value = args[2];
+
+ if (!IsLegalPropertyName(name)) {
+ return Error() << "'" << name << "' is not a legal property name";
+ }
+
+ if (!value.empty()) {
+ if (auto result = IsLegalPropertyValue(name, value); !result) {
+ return result.error();
+ }
+ }
+
+ if (StartsWith(name, "ctl.")) {
+ return Error()
+ << "Do not set ctl. properties from init; call the Service functions directly";
+ }
+
+ static constexpr const char kRestoreconProperty[] = "selinux.restorecon_recursive";
+ if (name == kRestoreconProperty) {
+ return Error() << "Do not set '" << kRestoreconProperty
+ << "' from init; use the restorecon builtin directly";
+ }
+
+ return {};
+}
+
+Result<void> check_setrlimit(const BuiltinArguments& args) {
+ ReturnIfAnyArgsEmpty();
+
+ auto rlimit = ParseRlimit(args.args);
+ if (!rlimit) return rlimit.error();
+ return {};
+}
+
+Result<void> check_sysclktz(const BuiltinArguments& args) {
+ ReturnIfAnyArgsEmpty();
+
+ struct timezone tz = {};
+ if (!android::base::ParseInt(args[1], &tz.tz_minuteswest)) {
+ return Error() << "Unable to parse mins_west_of_gmt";
+ }
+ return {};
+}
+
+Result<void> check_wait(const BuiltinArguments& args) {
+ if (args.size() == 3 && !args[2].empty()) {
+ int timeout_int;
+ if (!android::base::ParseInt(args[2], &timeout_int)) {
+ return Error() << "failed to parse timeout";
+ }
+ }
+ return {};
+}
+
+Result<void> check_wait_for_prop(const BuiltinArguments& args) {
+ return check_setprop(std::move(args));
+}
+
+} // namespace init
+} // namespace android
diff --git a/init/check_builtins.h b/init/check_builtins.h
new file mode 100644
index 0000000..c974e88
--- /dev/null
+++ b/init/check_builtins.h
@@ -0,0 +1,40 @@
+/*
+ * 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 "builtin_arguments.h"
+#include "result.h"
+
+namespace android {
+namespace init {
+
+Result<void> check_chown(const BuiltinArguments& args);
+Result<void> check_exec(const BuiltinArguments& args);
+Result<void> check_exec_background(const BuiltinArguments& args);
+Result<void> check_load_system_props(const BuiltinArguments& args);
+Result<void> check_loglevel(const BuiltinArguments& args);
+Result<void> check_mkdir(const BuiltinArguments& args);
+Result<void> check_restorecon(const BuiltinArguments& args);
+Result<void> check_restorecon_recursive(const BuiltinArguments& args);
+Result<void> check_setprop(const BuiltinArguments& args);
+Result<void> check_setrlimit(const BuiltinArguments& args);
+Result<void> check_sysclktz(const BuiltinArguments& args);
+Result<void> check_wait(const BuiltinArguments& args);
+Result<void> check_wait_for_prop(const BuiltinArguments& args);
+
+} // namespace init
+} // namespace android
diff --git a/init/host_builtin_map.py b/init/host_builtin_map.py
new file mode 100755
index 0000000..6afcb17
--- /dev/null
+++ b/init/host_builtin_map.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+"""Generates the builtins map to be used by host_init_verifier.
+
+It copies the builtin function map from builtins.cpp, then replaces do_xxx() functions with the
+equivalent check_xxx() if found in check_builtins.cpp.
+
+"""
+
+import re
+import argparse
+
+parser = argparse.ArgumentParser('host_builtin_map.py')
+parser.add_argument('--builtins', required=True, help='Path to builtins.cpp')
+parser.add_argument('--check_builtins', required=True, help='Path to check_builtins.cpp')
+args = parser.parse_args()
+
+CHECK_REGEX = re.compile(r'.+check_(\S+)\(.+')
+check_functions = []
+with open(args.check_builtins) as check_file:
+ for line in check_file:
+ match = CHECK_REGEX.match(line)
+ if match:
+ check_functions.append(match.group(1))
+
+function_map = []
+with open(args.builtins) as builtins_file:
+ in_function_map = False
+ for line in builtins_file:
+ if '// Builtin-function-map start' in line:
+ in_function_map = True
+ elif '// Builtin-function-map end' in line:
+ in_function_map = False
+ elif in_function_map:
+ function_map.append(line)
+
+DO_REGEX = re.compile(r'.+do_([^\}]+).+')
+FUNCTION_REGEX = re.compile(r'(do_[^\}]+)')
+for line in function_map:
+ match = DO_REGEX.match(line)
+ if match:
+ if match.group(1) in check_functions:
+ print line.replace('do_', 'check_'),
+ else:
+ print FUNCTION_REGEX.sub('check_stub', line),
+ else:
+ print line,
diff --git a/init/host_init_stubs.h b/init/host_init_stubs.h
index 7c0544a..f9a08a5 100644
--- a/init/host_init_stubs.h
+++ b/init/host_init_stubs.h
@@ -26,6 +26,7 @@
// android/api-level.h
#define __ANDROID_API_P__ 28
+#define __ANDROID_API_R__ 30
// sys/system_properties.h
#define PROP_VALUE_MAX 92
diff --git a/init/host_init_verifier.cpp b/init/host_init_verifier.cpp
index 92c2aa5..a5a5b1b 100644
--- a/init/host_init_verifier.cpp
+++ b/init/host_init_verifier.cpp
@@ -35,6 +35,7 @@
#include "action.h"
#include "action_manager.h"
#include "action_parser.h"
+#include "check_builtins.h"
#include "host_import_parser.h"
#include "host_init_stubs.h"
#include "parser.h"
@@ -163,7 +164,7 @@
namespace android {
namespace init {
-static Result<void> do_stub(const BuiltinArguments& args) {
+static Result<void> check_stub(const BuiltinArguments& args) {
return {};
}
@@ -221,7 +222,7 @@
return EXIT_FAILURE;
}
- const BuiltinFunctionMap function_map;
+ const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
Action::set_function_map(&function_map);
ActionManager& am = ActionManager::GetInstance();
ServiceList& sl = ServiceList::GetInstance();
@@ -238,9 +239,10 @@
LOG(ERROR) << "Failed to open init rc script '" << *argv << "'";
return EXIT_FAILURE;
}
- if (parser.parse_error_count() > 0) {
- LOG(ERROR) << "Failed to parse init script '" << *argv << "' with "
- << parser.parse_error_count() << " errors";
+ size_t failures = parser.parse_error_count() + am.CheckAllCommands();
+ if (failures > 0) {
+ LOG(ERROR) << "Failed to parse init script '" << *argv << "' with " << failures
+ << " errors";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
diff --git a/init/import_parser.cpp b/init/import_parser.cpp
index c72b7d6..1a43508 100644
--- a/init/import_parser.cpp
+++ b/init/import_parser.cpp
@@ -29,15 +29,14 @@
return Error() << "single argument needed for import\n";
}
- std::string conf_file;
- bool ret = expand_props(args[1], &conf_file);
- if (!ret) {
- return Error() << "error while expanding import";
+ auto conf_file = ExpandProps(args[1]);
+ if (!conf_file) {
+ return Error() << "Could not expand import: " << conf_file.error();
}
- LOG(INFO) << "Added '" << conf_file << "' to import list";
+ LOG(INFO) << "Added '" << *conf_file << "' to import list";
if (filename_.empty()) filename_ = filename;
- imports_.emplace_back(std::move(conf_file), line);
+ imports_.emplace_back(std::move(*conf_file), line);
return {};
}
diff --git a/init/init.cpp b/init/init.cpp
index 5dba54d..18fb0c3 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -688,7 +688,7 @@
MountHandler mount_handler(&epoll);
set_usb_controller();
- const BuiltinFunctionMap function_map;
+ const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
Action::set_function_map(&function_map);
if (!SetupMountNamespaces()) {
diff --git a/init/init_test.cpp b/init/init_test.cpp
index a09db18..0411214 100644
--- a/init/init_test.cpp
+++ b/init/init_test.cpp
@@ -22,6 +22,7 @@
#include "action.h"
#include "action_manager.h"
#include "action_parser.h"
+#include "builtin_arguments.h"
#include "builtins.h"
#include "import_parser.h"
#include "keyword_map.h"
@@ -29,7 +30,6 @@
#include "service.h"
#include "service_list.h"
#include "service_parser.h"
-#include "test_function_map.h"
#include "util.h"
namespace android {
@@ -37,7 +37,7 @@
using ActionManagerCommand = std::function<void(ActionManager&)>;
-void TestInit(const std::string& init_script_file, const TestFunctionMap& test_function_map,
+void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
ActionManager am;
@@ -60,7 +60,7 @@
}
}
-void TestInitText(const std::string& init_script, const TestFunctionMap& test_function_map,
+void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
@@ -76,8 +76,13 @@
pass_test
)init";
- TestFunctionMap test_function_map;
- test_function_map.Add("pass_test", [&expect_true]() { expect_true = true; });
+ auto do_pass_test = [&expect_true](const BuiltinArguments&) {
+ expect_true = true;
+ return Result<void>{};
+ };
+ BuiltinFunctionMap test_function_map = {
+ {"pass_test", {0, 0, {false, do_pass_test}}},
+ };
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
std::vector<ActionManagerCommand> commands{trigger_boot};
@@ -103,10 +108,24 @@
)init";
int num_executed = 0;
- TestFunctionMap test_function_map;
- test_function_map.Add("execute_first", [&num_executed]() { EXPECT_EQ(0, num_executed++); });
- test_function_map.Add("execute_second", [&num_executed]() { EXPECT_EQ(1, num_executed++); });
- test_function_map.Add("execute_third", [&num_executed]() { EXPECT_EQ(2, num_executed++); });
+ auto do_execute_first = [&num_executed](const BuiltinArguments&) {
+ EXPECT_EQ(0, num_executed++);
+ return Result<void>{};
+ };
+ auto do_execute_second = [&num_executed](const BuiltinArguments&) {
+ EXPECT_EQ(1, num_executed++);
+ return Result<void>{};
+ };
+ auto do_execute_third = [&num_executed](const BuiltinArguments&) {
+ EXPECT_EQ(2, num_executed++);
+ return Result<void>{};
+ };
+
+ BuiltinFunctionMap test_function_map = {
+ {"execute_first", {0, 0, {false, do_execute_first}}},
+ {"execute_second", {0, 0, {false, do_execute_second}}},
+ {"execute_third", {0, 0, {false, do_execute_third}}},
+ };
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
std::vector<ActionManagerCommand> commands{trigger_boot};
@@ -127,7 +146,7 @@
)init";
ServiceList service_list;
- TestInitText(init_script, TestFunctionMap(), {}, &service_list);
+ TestInitText(init_script, BuiltinFunctionMap(), {}, &service_list);
ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
auto service = service_list.begin()->get();
@@ -186,8 +205,9 @@
return Result<void>{};
};
- TestFunctionMap test_function_map;
- test_function_map.Add("execute", 1, 1, false, execute_command);
+ BuiltinFunctionMap test_function_map = {
+ {"execute", {1, 1, {false, execute_command}}},
+ };
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
std::vector<ActionManagerCommand> commands{trigger_boot};
diff --git a/init/keyword_map.h b/init/keyword_map.h
index 7837bb3..d92678f 100644
--- a/init/keyword_map.h
+++ b/init/keyword_map.h
@@ -18,36 +18,49 @@
#include <map>
#include <string>
+#include <vector>
#include "result.h"
namespace android {
namespace init {
-template <typename Function>
+// Every init builtin, init service option, and ueventd option has a minimum and maximum number of
+// arguments. These must be checked both at run time for safety and also at build time for
+// correctness in host_init_verifier. Instead of copying and pasting the boiler plate code that
+// does this check into each function, it is abstracted in KeywordMap<>. This class maps keywords
+// to functions and checks that the number of arguments provided falls in the correct range or
+// returns an error otherwise.
+
+// Value is the return value of Find(), which is typically either a single function or a struct with
+// additional information.
+template <typename Value>
class KeywordMap {
public:
- using FunctionInfo = std::tuple<std::size_t, std::size_t, Function>;
- using Map = std::map<std::string, FunctionInfo>;
+ struct MapValue {
+ size_t min_args;
+ size_t max_args;
+ Value value;
+ };
- virtual ~KeywordMap() {
- }
+ KeywordMap() {}
+ KeywordMap(std::initializer_list<std::pair<const std::string, MapValue>> init) : map_(init) {}
- const Result<Function> FindFunction(const std::vector<std::string>& args) const {
+ Result<Value> Find(const std::vector<std::string>& args) const {
if (args.empty()) return Error() << "Keyword needed, but not provided";
auto& keyword = args[0];
auto num_args = args.size() - 1;
- auto function_info_it = map().find(keyword);
- if (function_info_it == map().end()) {
+ auto result_it = map_.find(keyword);
+ if (result_it == map_.end()) {
return Errorf("Invalid keyword '{}'", keyword);
}
- auto function_info = function_info_it->second;
+ auto result = result_it->second;
- auto min_args = std::get<0>(function_info);
- auto max_args = std::get<1>(function_info);
+ auto min_args = result.min_args;
+ auto max_args = result.max_args;
if (min_args == max_args && num_args != min_args) {
return Errorf("{} requires {} argument{}", keyword, min_args,
(min_args > 1 || min_args == 0) ? "s" : "");
@@ -63,13 +76,11 @@
}
}
- return std::get<Function>(function_info);
+ return result.value;
}
private:
- // Map of keyword ->
- // (minimum number of arguments, maximum number of arguments, function pointer)
- virtual const Map& map() const = 0;
+ std::map<std::string, MapValue> map_;
};
} // namespace init
diff --git a/init/main.cpp b/init/main.cpp
index 2ce46ef..38bc74b 100644
--- a/init/main.cpp
+++ b/init/main.cpp
@@ -60,7 +60,7 @@
if (argc > 1) {
if (!strcmp(argv[1], "subcontext")) {
android::base::InitLogging(argv, &android::base::KernelLogger);
- const BuiltinFunctionMap function_map;
+ const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
return SubcontextMain(argc, argv, &function_map);
}
diff --git a/init/property_service.cpp b/init/property_service.cpp
index 3761750..17622a3 100644
--- a/init/property_service.cpp
+++ b/init/property_service.cpp
@@ -174,13 +174,8 @@
return PROP_ERROR_INVALID_NAME;
}
- if (valuelen >= PROP_VALUE_MAX && !StartsWith(name, "ro.")) {
- *error = "Property value too long";
- return PROP_ERROR_INVALID_VALUE;
- }
-
- if (mbstowcs(nullptr, value.data(), 0) == static_cast<std::size_t>(-1)) {
- *error = "Value is not a UTF8 encoded string";
+ if (auto result = IsLegalPropertyValue(name, value); !result) {
+ *error = result.error().message();
return PROP_ERROR_INVALID_VALUE;
}
@@ -648,13 +643,14 @@
}
std::string raw_filename(fn);
- std::string expanded_filename;
- if (!expand_props(raw_filename, &expanded_filename)) {
- LOG(ERROR) << "Could not expand filename '" << raw_filename << "'";
+ auto expanded_filename = ExpandProps(raw_filename);
+
+ if (!expanded_filename) {
+ LOG(ERROR) << "Could not expand filename ': " << expanded_filename.error();
continue;
}
- load_properties_from_file(expanded_filename.c_str(), key, properties);
+ load_properties_from_file(expanded_filename->c_str(), key, properties);
} else {
value = strchr(key, '=');
if (!value) continue;
diff --git a/init/reboot.cpp b/init/reboot.cpp
index cb54d34..b0b5b54 100644
--- a/init/reboot.cpp
+++ b/init/reboot.cpp
@@ -670,11 +670,18 @@
<< err;
}
} else if (reboot_target == "recovery") {
- const std::vector<std::string> options = {};
- std::string err;
- if (!write_bootloader_message(options, &err)) {
- LOG(ERROR) << "Failed to set bootloader message: " << err;
- return false;
+ bootloader_message boot = {};
+ if (std::string err; !read_bootloader_message(&boot, &err)) {
+ LOG(ERROR) << "Failed to read bootloader message: " << err;
+ }
+ // Update the boot command field if it's empty, and preserve
+ // the other arguments in the bootloader message.
+ if (boot.command[0] == '\0') {
+ strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
+ if (std::string err; !write_bootloader_message(boot, &err)) {
+ LOG(ERROR) << "Failed to set bootloader message: " << err;
+ return false;
+ }
}
} else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" ||
reboot_target == "fastboot") {
diff --git a/init/selinux.cpp b/init/selinux.cpp
index 54be086..143cdfd 100644
--- a/init/selinux.cpp
+++ b/init/selinux.cpp
@@ -497,24 +497,28 @@
// This function returns the Android version with which the vendor SEPolicy was compiled.
// It is used for version checks such as whether or not vendor_init should be used
int SelinuxGetVendorAndroidVersion() {
- if (!IsSplitPolicyDevice()) {
- // If this device does not split sepolicy files, it's not a Treble device and therefore,
- // we assume it's always on the latest platform.
- return __ANDROID_API_FUTURE__;
- }
+ static int vendor_android_version = [] {
+ if (!IsSplitPolicyDevice()) {
+ // If this device does not split sepolicy files, it's not a Treble device and therefore,
+ // we assume it's always on the latest platform.
+ return __ANDROID_API_FUTURE__;
+ }
- std::string version;
- if (!GetVendorMappingVersion(&version)) {
- LOG(FATAL) << "Could not read vendor SELinux version";
- }
+ std::string version;
+ if (!GetVendorMappingVersion(&version)) {
+ LOG(FATAL) << "Could not read vendor SELinux version";
+ }
- int major_version;
- std::string major_version_str(version, 0, version.find('.'));
- if (!ParseInt(major_version_str, &major_version)) {
- PLOG(FATAL) << "Failed to parse the vendor sepolicy major version " << major_version_str;
- }
+ int major_version;
+ std::string major_version_str(version, 0, version.find('.'));
+ if (!ParseInt(major_version_str, &major_version)) {
+ PLOG(FATAL) << "Failed to parse the vendor sepolicy major version "
+ << major_version_str;
+ }
- return major_version;
+ return major_version;
+ }();
+ return vendor_android_version;
}
// This function initializes SELinux then execs init to run in the init SELinux context.
diff --git a/init/service.cpp b/init/service.cpp
index 47f4db9..e60c20d 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -100,9 +100,11 @@
expanded_args.resize(args.size());
c_strings.push_back(const_cast<char*>(args[0].data()));
for (std::size_t i = 1; i < args.size(); ++i) {
- if (!expand_props(args[i], &expanded_args[i])) {
- LOG(FATAL) << args[0] << ": cannot expand '" << args[i] << "'";
+ auto expanded_arg = ExpandProps(args[i]);
+ if (!expanded_arg) {
+ LOG(FATAL) << args[0] << ": cannot expand arguments': " << expanded_arg.error();
}
+ expanded_args[i] = *expanded_arg;
c_strings.push_back(expanded_args[i].data());
}
c_strings.push_back(nullptr);
@@ -633,7 +635,8 @@
}
}
-std::unique_ptr<Service> Service::MakeTemporaryOneshotService(const std::vector<std::string>& args) {
+Result<std::unique_ptr<Service>> Service::MakeTemporaryOneshotService(
+ const std::vector<std::string>& args) {
// Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS...
// SECLABEL can be a - to denote default
std::size_t command_arg = 1;
@@ -644,13 +647,11 @@
}
}
if (command_arg > 4 + NR_SVC_SUPP_GIDS) {
- LOG(ERROR) << "exec called with too many supplementary group ids";
- return nullptr;
+ return Error() << "exec called with too many supplementary group ids";
}
if (command_arg >= args.size()) {
- LOG(ERROR) << "exec called without command";
- return nullptr;
+ return Error() << "exec called without command";
}
std::vector<std::string> str_args(args.begin() + command_arg, args.end());
@@ -669,8 +670,7 @@
if (command_arg > 3) {
uid = DecodeUid(args[2]);
if (!uid) {
- LOG(ERROR) << "Unable to decode UID for '" << args[2] << "': " << uid.error();
- return nullptr;
+ return Error() << "Unable to decode UID for '" << args[2] << "': " << uid.error();
}
}
Result<gid_t> gid = 0;
@@ -678,16 +678,14 @@
if (command_arg > 4) {
gid = DecodeUid(args[3]);
if (!gid) {
- LOG(ERROR) << "Unable to decode GID for '" << args[3] << "': " << gid.error();
- return nullptr;
+ return Error() << "Unable to decode GID for '" << args[3] << "': " << gid.error();
}
std::size_t nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */;
for (size_t i = 0; i < nr_supp_gids; ++i) {
auto supp_gid = DecodeUid(args[4 + i]);
if (!supp_gid) {
- LOG(ERROR) << "Unable to decode GID for '" << args[4 + i]
- << "': " << supp_gid.error();
- return nullptr;
+ return Error() << "Unable to decode GID for '" << args[4 + i]
+ << "': " << supp_gid.error();
}
supp_gids.push_back(*supp_gid);
}
diff --git a/init/service.h b/init/service.h
index cdf31bb..6f79faa 100644
--- a/init/service.h
+++ b/init/service.h
@@ -71,7 +71,8 @@
const std::vector<gid_t>& supp_gids, int namespace_flags, const std::string& seclabel,
Subcontext* subcontext_for_restart_commands, const std::vector<std::string>& args);
- static std::unique_ptr<Service> MakeTemporaryOneshotService(const std::vector<std::string>& args);
+ static Result<std::unique_ptr<Service>> MakeTemporaryOneshotService(
+ const std::vector<std::string>& args);
bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
Result<void> ExecStart();
diff --git a/init/service_parser.cpp b/init/service_parser.cpp
index 0fbbeb8..e45e804 100644
--- a/init/service_parser.cpp
+++ b/init/service_parser.cpp
@@ -193,9 +193,9 @@
Result<void> ServiceParser::ParseKeycodes(std::vector<std::string>&& args) {
auto it = args.begin() + 1;
if (args.size() == 2 && StartsWith(args[1], "$")) {
- std::string expanded;
- if (!expand_props(args[1], &expanded)) {
- return Error() << "Could not expand property '" << args[1] << "'";
+ auto expanded = ExpandProps(args[1]);
+ if (!expanded) {
+ return expanded.error();
}
// If the property is not set, it defaults to none, in which case there are no keycodes
@@ -204,7 +204,7 @@
return {};
}
- args = Split(expanded, ",");
+ args = Split(*expanded, ",");
it = args.begin();
}
@@ -422,9 +422,11 @@
FileDescriptor file;
file.type = args[2];
- if (!expand_props(args[1], &file.name)) {
- return Error() << "Could not expand property in file path '" << args[1] << "'";
+ auto file_name = ExpandProps(args[1]);
+ if (!file_name) {
+ return Error() << "Could not expand file path ': " << file_name.error();
}
+ file.name = *file_name;
if (file.name[0] != '/' || file.name.find("../") != std::string::npos) {
return Error() << "file name must not be relative";
}
@@ -461,18 +463,10 @@
return {};
}
-class ServiceParser::OptionParserMap : public KeywordMap<OptionParser> {
- public:
- OptionParserMap() {}
-
- private:
- const Map& map() const override;
-};
-
-const ServiceParser::OptionParserMap::Map& ServiceParser::OptionParserMap::map() const {
+const KeywordMap<ServiceParser::OptionParser>& ServiceParser::GetParserMap() const {
constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
// clang-format off
- static const Map option_parsers = {
+ static const KeywordMap<ServiceParser::OptionParser> parser_map = {
{"capabilities",
{0, kMax, &ServiceParser::ParseCapabilities}},
{"class", {1, kMax, &ServiceParser::ParseClass}},
@@ -518,7 +512,7 @@
{"writepid", {1, kMax, &ServiceParser::ParseWritepid}},
};
// clang-format on
- return option_parsers;
+ return parser_map;
}
Result<void> ServiceParser::ParseSection(std::vector<std::string>&& args,
@@ -561,8 +555,7 @@
return {};
}
- static const OptionParserMap parser_map;
- auto parser = parser_map.FindFunction(args);
+ auto parser = GetParserMap().Find(args);
if (!parser) return parser.error();
diff --git a/init/service_parser.h b/init/service_parser.h
index bca0739..98ab15a 100644
--- a/init/service_parser.h
+++ b/init/service_parser.h
@@ -45,7 +45,7 @@
private:
using OptionParser = Result<void> (ServiceParser::*)(std::vector<std::string>&& args);
- class OptionParserMap;
+ const KeywordMap<ServiceParser::OptionParser>& GetParserMap() const;
Result<void> ParseCapabilities(std::vector<std::string>&& args);
Result<void> ParseClass(std::vector<std::string>&& args);
diff --git a/init/service_test.cpp b/init/service_test.cpp
index 6a34acc..c9cc7bd 100644
--- a/init/service_test.cpp
+++ b/init/service_test.cpp
@@ -75,15 +75,15 @@
TEST(service, make_temporary_oneshot_service_invalid_syntax) {
std::vector<std::string> args;
// Nothing.
- ASSERT_EQ(nullptr, Service::MakeTemporaryOneshotService(args));
+ ASSERT_FALSE(Service::MakeTemporaryOneshotService(args));
// No arguments to 'exec'.
args.push_back("exec");
- ASSERT_EQ(nullptr, Service::MakeTemporaryOneshotService(args));
+ ASSERT_FALSE(Service::MakeTemporaryOneshotService(args));
// No command in "exec --".
args.push_back("--");
- ASSERT_EQ(nullptr, Service::MakeTemporaryOneshotService(args));
+ ASSERT_FALSE(Service::MakeTemporaryOneshotService(args));
}
TEST(service, make_temporary_oneshot_service_too_many_supplementary_gids) {
@@ -97,7 +97,7 @@
}
args.push_back("--");
args.push_back("/system/bin/id");
- ASSERT_EQ(nullptr, Service::MakeTemporaryOneshotService(args));
+ ASSERT_FALSE(Service::MakeTemporaryOneshotService(args));
}
static void Test_make_temporary_oneshot_service(bool dash_dash, bool seclabel, bool uid, bool gid,
@@ -122,8 +122,9 @@
}
args.push_back("/system/bin/toybox");
args.push_back("id");
- auto svc = Service::MakeTemporaryOneshotService(args);
- ASSERT_NE(nullptr, svc);
+ auto service_ret = Service::MakeTemporaryOneshotService(args);
+ ASSERT_TRUE(service_ret);
+ auto svc = std::move(*service_ret);
if (seclabel) {
ASSERT_EQ("u:r:su:s0", svc->seclabel());
diff --git a/init/sigchld_handler.cpp b/init/sigchld_handler.cpp
index c9a09cd..984235d 100644
--- a/init/sigchld_handler.cpp
+++ b/init/sigchld_handler.cpp
@@ -73,6 +73,13 @@
auto exec_duration_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration).count();
wait_string = StringPrintf(" waiting took %f seconds", exec_duration_ms / 1000.0f);
+ } else if (service->flags() & SVC_ONESHOT) {
+ auto exec_duration = boot_clock::now() - service->time_started();
+ auto exec_duration_ms =
+ std::chrono::duration_cast<std::chrono::milliseconds>(exec_duration)
+ .count();
+ wait_string = StringPrintf(" oneshot service took %f seconds in background",
+ exec_duration_ms / 1000.0f);
}
} else {
name = StringPrintf("Untracked pid %d", pid);
diff --git a/init/subcontext.cpp b/init/subcontext.cpp
index 2f9541b..00f91d8 100644
--- a/init/subcontext.cpp
+++ b/init/subcontext.cpp
@@ -27,6 +27,7 @@
#include <selinux/android.h>
#include "action.h"
+#include "builtins.h"
#include "util.h"
#if defined(__ANDROID__)
@@ -99,7 +100,7 @@
class SubcontextProcess {
public:
- SubcontextProcess(const KeywordFunctionMap* function_map, std::string context, int init_fd)
+ SubcontextProcess(const BuiltinFunctionMap* function_map, std::string context, int init_fd)
: function_map_(function_map), context_(std::move(context)), init_fd_(init_fd){};
void MainLoop();
@@ -109,7 +110,7 @@
void ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command,
SubcontextReply* reply) const;
- const KeywordFunctionMap* function_map_;
+ const BuiltinFunctionMap* function_map_;
const std::string context_;
const int init_fd_;
};
@@ -122,12 +123,12 @@
args.emplace_back(string);
}
- auto map_result = function_map_->FindFunction(args);
+ auto map_result = function_map_->Find(args);
Result<void> result;
if (!map_result) {
result = Error() << "Cannot find command: " << map_result.error();
} else {
- result = RunBuiltinFunction(map_result->second, args, context_);
+ result = RunBuiltinFunction(map_result->function, args, context_);
}
for (const auto& [name, value] : properties_to_set) {
@@ -150,15 +151,15 @@
void SubcontextProcess::ExpandArgs(const SubcontextCommand::ExpandArgsCommand& expand_args_command,
SubcontextReply* reply) const {
for (const auto& arg : expand_args_command.args()) {
- auto expanded_prop = std::string{};
- if (!expand_props(arg, &expanded_prop)) {
+ auto expanded_arg = ExpandProps(arg);
+ if (!expanded_arg) {
auto* failure = reply->mutable_failure();
- failure->set_error_string("Failed to expand '" + arg + "'");
+ failure->set_error_string(expanded_arg.error().message());
failure->set_error_errno(0);
return;
} else {
auto* expand_args_reply = reply->mutable_expand_args_reply();
- expand_args_reply->add_expanded_args(expanded_prop);
+ expand_args_reply->add_expanded_args(*expanded_arg);
}
}
}
@@ -215,7 +216,7 @@
} // namespace
-int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map) {
+int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map) {
if (argc < 4) LOG(FATAL) << "Fewer than 4 args specified to subcontext (" << argc << ")";
auto context = std::string(argv[2]);
diff --git a/init/subcontext.h b/init/subcontext.h
index 16bd870..591521f 100644
--- a/init/subcontext.h
+++ b/init/subcontext.h
@@ -60,7 +60,7 @@
android::base::unique_fd socket_;
};
-int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map);
+int SubcontextMain(int argc, char** argv, const BuiltinFunctionMap* function_map);
std::vector<Subcontext>* InitializeSubcontexts();
bool SubcontextChildReap(pid_t pid);
void SubcontextTerminate();
diff --git a/init/subcontext_benchmark.cpp b/init/subcontext_benchmark.cpp
index fdbbc41..f6fee8a 100644
--- a/init/subcontext_benchmark.cpp
+++ b/init/subcontext_benchmark.cpp
@@ -19,8 +19,6 @@
#include <benchmark/benchmark.h>
#include <selinux/selinux.h>
-#include "test_function_map.h"
-
namespace android {
namespace init {
@@ -50,11 +48,11 @@
BENCHMARK(BenchmarkSuccess);
-TestFunctionMap BuildTestFunctionMap() {
- TestFunctionMap test_function_map;
- test_function_map.Add("return_success", 0, 0, true,
- [](const BuiltinArguments& args) { return Result<void>{}; });
-
+BuiltinFunctionMap BuildTestFunctionMap() {
+ auto function = [](const BuiltinArguments& args) { return Result<void>{}; };
+ BuiltinFunctionMap test_function_map = {
+ {"return_success", {0, 0, {true, function}}},
+ };
return test_function_map;
}
diff --git a/init/subcontext_test.cpp b/init/subcontext_test.cpp
index 55912d6..ae89c38 100644
--- a/init/subcontext_test.cpp
+++ b/init/subcontext_test.cpp
@@ -26,7 +26,6 @@
#include <selinux/selinux.h>
#include "builtin_arguments.h"
-#include "test_function_map.h"
using namespace std::literals;
@@ -167,50 +166,58 @@
};
auto result = subcontext.ExpandArgs(args);
ASSERT_FALSE(result);
- EXPECT_EQ("Failed to expand '" + args[1] + "'", result.error().message());
+ EXPECT_EQ("unexpected end of string in '" + args[1] + "', looking for }",
+ result.error().message());
});
}
-TestFunctionMap BuildTestFunctionMap() {
- TestFunctionMap test_function_map;
+BuiltinFunctionMap BuildTestFunctionMap() {
// For CheckDifferentPid
- test_function_map.Add("return_pids_as_error", 0, 0, true,
- [](const BuiltinArguments& args) -> Result<void> {
- return Error() << getpid() << " " << getppid();
- });
+ auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> {
+ return Error() << getpid() << " " << getppid();
+ };
// For SetProp
- test_function_map.Add("setprop", 2, 2, true, [](const BuiltinArguments& args) {
+ auto do_setprop = [](const BuiltinArguments& args) {
android::base::SetProperty(args[1], args[2]);
return Result<void>{};
- });
+ };
// For MultipleCommands
// Using a shared_ptr to extend lifetime of words to both lambdas
auto words = std::make_shared<std::vector<std::string>>();
- test_function_map.Add("add_word", 1, 1, true, [words](const BuiltinArguments& args) {
+ auto do_add_word = [words](const BuiltinArguments& args) {
words->emplace_back(args[1]);
return Result<void>{};
- });
- test_function_map.Add("return_words_as_error", 0, 0, true,
- [words](const BuiltinArguments& args) -> Result<void> {
- return Error() << Join(*words, " ");
- });
+ };
+ auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> {
+ return Error() << Join(*words, " ");
+ };
// For RecoverAfterAbort
- test_function_map.Add("cause_log_fatal", 0, 0, true,
- [](const BuiltinArguments& args) -> Result<void> {
- return Error() << std::string(4097, 'f');
- });
- test_function_map.Add(
- "generate_sane_error", 0, 0, true,
- [](const BuiltinArguments& args) -> Result<void> { return Error() << "Sane error!"; });
+ auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> {
+ return Error() << std::string(4097, 'f');
+ };
+ auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> {
+ return Error() << "Sane error!";
+ };
// For ContextString
- test_function_map.Add(
- "return_context_as_error", 0, 0, true,
- [](const BuiltinArguments& args) -> Result<void> { return Error() << args.context; });
+ auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> {
+ return Error() << args.context;
+ };
+ // clang-format off
+ BuiltinFunctionMap test_function_map = {
+ {"return_pids_as_error", {0, 0, {true, do_return_pids_as_error}}},
+ {"setprop", {2, 2, {true, do_setprop}}},
+ {"add_word", {1, 1, {true, do_add_word}}},
+ {"return_words_as_error", {0, 0, {true, do_return_words_as_error}}},
+ {"cause_log_fatal", {0, 0, {true, do_cause_log_fatal}}},
+ {"generate_sane_error", {0, 0, {true, do_generate_sane_error}}},
+ {"return_context_as_error", {0, 0, {true, do_return_context_as_error}}},
+ };
+ // clang-format on
return test_function_map;
}
diff --git a/init/test_function_map.h b/init/test_function_map.h
deleted file mode 100644
index 466836c..0000000
--- a/init/test_function_map.h
+++ /dev/null
@@ -1,53 +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.
- */
-
-#pragma once
-
-#include <string>
-#include <vector>
-
-#include "builtin_arguments.h"
-#include "builtins.h"
-#include "keyword_map.h"
-
-namespace android {
-namespace init {
-
-class TestFunctionMap : public KeywordFunctionMap {
- public:
- // Helper for argument-less functions
- using BuiltinFunctionNoArgs = std::function<void(void)>;
- void Add(const std::string& name, BuiltinFunctionNoArgs function) {
- Add(name, 0, 0, false, [f = std::move(function)](const BuiltinArguments&) {
- f();
- return Result<void>{};
- });
- }
-
- void Add(const std::string& name, std::size_t min_parameters, std::size_t max_parameters,
- bool run_in_subcontext, BuiltinFunction function) {
- builtin_functions_[name] = make_tuple(min_parameters, max_parameters,
- make_pair(run_in_subcontext, std::move(function)));
- }
-
- private:
- Map builtin_functions_ = {};
-
- const Map& map() const override { return builtin_functions_; }
-};
-
-} // namespace init
-} // namespace android
diff --git a/init/ueventd_parser.cpp b/init/ueventd_parser.cpp
index 25bab93..8ee0cce 100644
--- a/init/ueventd_parser.cpp
+++ b/init/ueventd_parser.cpp
@@ -176,21 +176,14 @@
Result<void> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
using OptionParser = Result<void> (SubsystemParser::*)(std::vector<std::string> && args);
+ // clang-format off
+ static const KeywordMap<OptionParser> parser_map = {
+ {"devname", {1, 1, &SubsystemParser::ParseDevName}},
+ {"dirname", {1, 1, &SubsystemParser::ParseDirName}},
+ };
+ // clang-format on
- static class OptionParserMap : public KeywordMap<OptionParser> {
- private:
- const Map& map() const override {
- // clang-format off
- static const Map option_parsers = {
- {"devname", {1, 1, &SubsystemParser::ParseDevName}},
- {"dirname", {1, 1, &SubsystemParser::ParseDirName}},
- };
- // clang-format on
- return option_parsers;
- }
- } parser_map;
-
- auto parser = parser_map.FindFunction(args);
+ auto parser = parser_map.Find(args);
if (!parser) return Error() << parser.error();
diff --git a/init/util.cpp b/init/util.cpp
index 8bfb755..0532375 100644
--- a/init/util.cpp
+++ b/init/util.cpp
@@ -41,13 +41,18 @@
#include <selinux/android.h>
#if defined(__ANDROID__)
+#include <android/api-level.h>
+#include <sys/system_properties.h>
+
#include "reboot_utils.h"
#include "selabel.h"
+#include "selinux.h"
#else
#include "host_init_stubs.h"
#endif
using android::base::boot_clock;
+using android::base::StartsWith;
using namespace std::literals::string_literals;
namespace android {
@@ -267,12 +272,10 @@
return S_ISDIR(info.st_mode);
}
-bool expand_props(const std::string& src, std::string* dst) {
+Result<std::string> ExpandProps(const std::string& src) {
const char* src_ptr = src.c_str();
- if (!dst) {
- return false;
- }
+ std::string dst;
/* - variables can either be $x.y or ${x.y}, in case they are only part
* of the string.
@@ -286,19 +289,19 @@
c = strchr(src_ptr, '$');
if (!c) {
- dst->append(src_ptr);
- return true;
+ dst.append(src_ptr);
+ return dst;
}
- dst->append(src_ptr, c);
+ dst.append(src_ptr, c);
c++;
if (*c == '$') {
- dst->push_back(*(c++));
+ dst.push_back(*(c++));
src_ptr = c;
continue;
} else if (*c == '\0') {
- return true;
+ return dst;
}
std::string prop_name;
@@ -308,8 +311,7 @@
const char* end = strchr(c, '}');
if (!end) {
// failed to find closing brace, abort.
- LOG(ERROR) << "unexpected end of string in '" << src << "', looking for }";
- return false;
+ return Error() << "unexpected end of string in '" << src << "', looking for }";
}
prop_name = std::string(c, end);
c = end + 1;
@@ -320,29 +322,34 @@
}
} else {
prop_name = c;
- LOG(ERROR) << "using deprecated syntax for specifying property '" << c << "', use ${name} instead";
+ if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_R__) {
+ return Error() << "using deprecated syntax for specifying property '" << c
+ << "', use ${name} instead";
+ } else {
+ LOG(ERROR) << "using deprecated syntax for specifying property '" << c
+ << "', use ${name} instead";
+ }
c += prop_name.size();
}
if (prop_name.empty()) {
- LOG(ERROR) << "invalid zero-length property name in '" << src << "'";
- return false;
+ return Error() << "invalid zero-length property name in '" << src << "'";
}
std::string prop_val = android::base::GetProperty(prop_name, "");
if (prop_val.empty()) {
if (def_val.empty()) {
- LOG(ERROR) << "property '" << prop_name << "' doesn't exist while expanding '" << src << "'";
- return false;
+ return Error() << "property '" << prop_name << "' doesn't exist while expanding '"
+ << src << "'";
}
prop_val = def_val;
}
- dst->append(prop_val);
+ dst.append(prop_val);
src_ptr = c;
}
- return true;
+ return dst;
}
static std::string init_android_dt_dir() {
@@ -414,6 +421,58 @@
return true;
}
+Result<void> IsLegalPropertyValue(const std::string& name, const std::string& value) {
+ if (value.size() >= PROP_VALUE_MAX && !StartsWith(name, "ro.")) {
+ return Error() << "Property value too long";
+ }
+
+ if (mbstowcs(nullptr, value.data(), 0) == static_cast<std::size_t>(-1)) {
+ return Error() << "Value is not a UTF8 encoded string";
+ }
+
+ return {};
+}
+
+Result<std::pair<int, std::vector<std::string>>> ParseRestorecon(
+ const std::vector<std::string>& args) {
+ struct flag_type {
+ const char* name;
+ int value;
+ };
+ static const flag_type flags[] = {
+ {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
+ {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
+ {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
+ {0, 0}};
+
+ int flag = 0;
+ std::vector<std::string> paths;
+
+ bool in_flags = true;
+ for (size_t i = 1; i < args.size(); ++i) {
+ if (android::base::StartsWith(args[i], "--")) {
+ if (!in_flags) {
+ return Error() << "flags must precede paths";
+ }
+ bool found = false;
+ for (size_t j = 0; flags[j].name; ++j) {
+ if (args[i] == flags[j].name) {
+ flag |= flags[j].value;
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ return Error() << "bad flag " << args[i];
+ }
+ } else {
+ in_flags = false;
+ paths.emplace_back(args[i]);
+ }
+ }
+ return std::pair(flag, paths);
+}
+
static void InitAborter(const char* abort_message) {
// When init forks, it continues to use this aborter for LOG(FATAL), but we want children to
// simply abort instead of trying to reboot the system.
diff --git a/init/util.h b/init/util.h
index 6a12fb6..4cccefe 100644
--- a/init/util.h
+++ b/init/util.h
@@ -14,24 +14,20 @@
* limitations under the License.
*/
-#ifndef _INIT_UTIL_H_
-#define _INIT_UTIL_H_
+#pragma once
#include <sys/stat.h>
#include <sys/types.h>
#include <chrono>
#include <functional>
-#include <ostream>
#include <string>
#include <android-base/chrono_utils.h>
-#include <selinux/label.h>
#include "result.h"
using android::base::boot_clock;
-using namespace std::chrono_literals;
namespace android {
namespace init {
@@ -52,7 +48,7 @@
const std::function<void(const std::string&, const std::string&, bool)>&);
bool make_dir(const std::string& path, mode_t mode);
bool is_dir(const char* pathname);
-bool expand_props(const std::string& src, std::string* dst);
+Result<std::string> ExpandProps(const std::string& src);
// Returns the platform's Android DT directory as specified in the kernel cmdline.
// If the platform does not configure a custom DT path, returns the standard one (based in procfs).
@@ -62,11 +58,13 @@
bool is_android_dt_value_expected(const std::string& sub_path, const std::string& expected_content);
bool IsLegalPropertyName(const std::string& name);
+Result<void> IsLegalPropertyValue(const std::string& name, const std::string& value);
+
+Result<std::pair<int, std::vector<std::string>>> ParseRestorecon(
+ const std::vector<std::string>& args);
void SetStdioToDevNull(char** argv);
void InitKernelLogging(char** argv);
bool IsRecoveryMode();
} // namespace init
} // namespace android
-
-#endif
diff --git a/libbacktrace/Android.bp b/libbacktrace/Android.bp
index 9ece847..565f2c3 100644
--- a/libbacktrace/Android.bp
+++ b/libbacktrace/Android.bp
@@ -97,7 +97,6 @@
cflags: ["-DNO_LIBDEXFILE_SUPPORT"],
},
},
- whole_static_libs: ["libdemangle"],
}
cc_test_library {
diff --git a/libbacktrace/Backtrace.cpp b/libbacktrace/Backtrace.cpp
index 71980d7..3e050ab 100644
--- a/libbacktrace/Backtrace.cpp
+++ b/libbacktrace/Backtrace.cpp
@@ -28,13 +28,13 @@
#include <backtrace/Backtrace.h>
#include <backtrace/BacktraceMap.h>
-#include <demangle.h>
-
#include "BacktraceLog.h"
#include "UnwindStack.h"
using android::base::StringPrintf;
+extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
+
//-------------------------------------------------------------------------
// Backtrace functions.
//-------------------------------------------------------------------------
@@ -63,7 +63,14 @@
if (map->start == 0 || (map->flags & PROT_DEVICE_MAP)) {
return "";
}
- return demangle(GetFunctionNameRaw(pc, offset).c_str());
+ std::string name(GetFunctionNameRaw(pc, offset));
+ char* demangled_name = __cxa_demangle(name.c_str(), nullptr, nullptr, nullptr);
+ if (demangled_name != nullptr) {
+ name = demangled_name;
+ free(demangled_name);
+ return name;
+ }
+ return name;
}
bool Backtrace::VerifyReadWordArgs(uint64_t ptr, word_t* out_value) {
diff --git a/libbacktrace/UnwindStack.cpp b/libbacktrace/UnwindStack.cpp
index a128623..624711f 100644
--- a/libbacktrace/UnwindStack.cpp
+++ b/libbacktrace/UnwindStack.cpp
@@ -24,7 +24,6 @@
#include <string>
#include <backtrace/Backtrace.h>
-#include <demangle.h>
#include <unwindstack/Elf.h>
#include <unwindstack/MapInfo.h>
#include <unwindstack/Maps.h>
@@ -41,6 +40,8 @@
#include "UnwindStack.h"
#include "UnwindStackMap.h"
+extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
+
bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
std::vector<std::string>* skip_names, BacktraceUnwindError* error) {
@@ -115,7 +116,13 @@
back_frame->pc = frame->pc;
back_frame->sp = frame->sp;
- back_frame->func_name = demangle(frame->function_name.c_str());
+ char* demangled_name = __cxa_demangle(frame->function_name.c_str(), nullptr, nullptr, nullptr);
+ if (demangled_name != nullptr) {
+ back_frame->func_name = demangled_name;
+ free(demangled_name);
+ } else {
+ back_frame->func_name = frame->function_name;
+ }
back_frame->func_offset = frame->function_offset;
back_frame->map.name = frame->map_name;
diff --git a/libion/OWNERS b/libion/OWNERS
new file mode 100644
index 0000000..143ad2d
--- /dev/null
+++ b/libion/OWNERS
@@ -0,0 +1,2 @@
+sspatil@google.com
+hridya@google.com
diff --git a/libion/ion.c b/libion/ion.c
index b8de5a4..1ecfc78 100644
--- a/libion/ion.c
+++ b/libion/ion.c
@@ -152,6 +152,8 @@
ion_user_handle_t handle;
int ret;
+ if (!handle_fd) return -EINVAL;
+
if (!ion_is_legacy(fd)) {
struct ion_new_allocation_data data = {
.len = len,
@@ -201,6 +203,7 @@
int ret;
struct ion_heap_query query;
+ if (!cnt) return -EINVAL;
memset(&query, 0, sizeof(query));
ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
diff --git a/libion/tests/Android.bp b/libion/tests/Android.bp
index b3fcb3b..d3b4688 100644
--- a/libion/tests/Android.bp
+++ b/libion/tests/Android.bp
@@ -18,18 +18,15 @@
name: "ion-unit-tests",
cflags: [
"-g",
- "-Wall",
- "-Werror",
"-Wno-missing-field-initializers",
],
shared_libs: ["libion"],
srcs: [
- "ion_test_fixture.cpp",
"allocate_test.cpp",
- "formerly_valid_handle_test.cpp",
- "invalid_values_test.cpp",
- "map_test.cpp",
- "device_test.cpp",
"exit_test.cpp",
+ "heap_query.cpp",
+ "invalid_values_test.cpp",
+ "ion_test_fixture.cpp",
+ "map_test.cpp",
],
}
diff --git a/libion/tests/allocate_test.cpp b/libion/tests/allocate_test.cpp
index 3c4524e..5ed01bb 100644
--- a/libion/tests/allocate_test.cpp
+++ b/libion/tests/allocate_test.cpp
@@ -14,95 +14,106 @@
* limitations under the License.
*/
-#include <memory>
#include <sys/mman.h>
+#include <memory>
#include <gtest/gtest.h>
#include <ion/ion.h>
#include "ion_test_fixture.h"
-class Allocate : public IonAllHeapsTest {
-};
+class Allocate : public IonTest {};
-TEST_F(Allocate, Allocate)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Allocate, Allocate) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
- ion_user_handle_t handle = 0;
- ASSERT_EQ(0, ion_alloc(m_ionFd, size, 0, heapMask, 0, &handle));
- ASSERT_TRUE(handle != 0);
- ASSERT_EQ(0, ion_free(m_ionFd, handle));
+ int fd;
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &fd));
+ ASSERT_TRUE(fd != 0);
+ ASSERT_EQ(close(fd), 0); // free the buffer
}
}
}
-TEST_F(Allocate, AllocateCached)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Allocate, AllocateCached) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
- ion_user_handle_t handle = 0;
- ASSERT_EQ(0, ion_alloc(m_ionFd, size, 0, heapMask, ION_FLAG_CACHED, &handle));
- ASSERT_TRUE(handle != 0);
- ASSERT_EQ(0, ion_free(m_ionFd, handle));
+ int fd;
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), ION_FLAG_CACHED, &fd));
+ ASSERT_TRUE(fd != 0);
+ ASSERT_EQ(close(fd), 0); // free the buffer
}
}
}
-TEST_F(Allocate, AllocateCachedNeedsSync)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Allocate, AllocateCachedNeedsSync) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
- ion_user_handle_t handle = 0;
- ASSERT_EQ(0, ion_alloc(m_ionFd, size, 0, heapMask, ION_FLAG_CACHED_NEEDS_SYNC, &handle));
- ASSERT_TRUE(handle != 0);
- ASSERT_EQ(0, ion_free(m_ionFd, handle));
+ int fd;
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id),
+ ION_FLAG_CACHED_NEEDS_SYNC, &fd));
+ ASSERT_TRUE(fd != 0);
+ ASSERT_EQ(close(fd), 0); // free the buffer
}
}
}
-TEST_F(Allocate, RepeatedAllocate)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Allocate, RepeatedAllocate) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
- ion_user_handle_t handle = 0;
+ int fd;
for (unsigned int i = 0; i < 1024; i++) {
SCOPED_TRACE(::testing::Message() << "iteration " << i);
- ASSERT_EQ(0, ion_alloc(m_ionFd, size, 0, heapMask, 0, &handle));
- ASSERT_TRUE(handle != 0);
- ASSERT_EQ(0, ion_free(m_ionFd, handle));
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &fd));
+ ASSERT_TRUE(fd != 0);
+ ASSERT_EQ(close(fd), 0); // free the buffer
}
}
}
}
-TEST_F(Allocate, Zeroed)
-{
+TEST_F(Allocate, Large) {
+ for (const auto& heap : ion_heaps) {
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
+ int fd;
+ ASSERT_EQ(-ENOMEM,
+ ion_alloc_fd(ionfd, 3UL * 1024 * 1024 * 1024, 0, (1 << heap.heap_id), 0, &fd));
+ }
+}
+
+// Make sure all heaps always return zeroed pages
+TEST_F(Allocate, Zeroed) {
auto zeroes_ptr = std::make_unique<char[]>(4096);
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ for (const auto& heap : ion_heaps) {
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
int fds[16];
for (unsigned int i = 0; i < 16; i++) {
int map_fd = -1;
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, 0, &map_fd));
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, 4096, 0, (1 << heap.heap_id), 0, &map_fd));
ASSERT_GE(map_fd, 0);
- void *ptr = NULL;
+ void* ptr = NULL;
ptr = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED, map_fd, 0);
ASSERT_TRUE(ptr != NULL);
@@ -116,13 +127,13 @@
ASSERT_EQ(0, close(fds[i]));
}
- int newIonFd = ion_open();
+ int new_ionfd = ion_open();
int map_fd = -1;
- ASSERT_EQ(0, ion_alloc_fd(newIonFd, 4096, 0, heapMask, 0, &map_fd));
+ ASSERT_EQ(0, ion_alloc_fd(new_ionfd, 4096, 0, (1 << heap.heap_id), 0, &map_fd));
ASSERT_GE(map_fd, 0);
- void *ptr = NULL;
+ void* ptr = NULL;
ptr = mmap(NULL, 4096, PROT_READ, MAP_SHARED, map_fd, 0);
ASSERT_TRUE(ptr != NULL);
@@ -130,14 +141,6 @@
ASSERT_EQ(0, munmap(ptr, 4096));
ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Allocate, Large)
-{
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- ion_user_handle_t handle = 0;
- ASSERT_EQ(-ENOMEM, ion_alloc(m_ionFd, 3UL*1024*1024*1024, 0, heapMask, 0, &handle));
+ ASSERT_EQ(0, ion_close(new_ionfd));
}
}
diff --git a/libion/tests/device_test.cpp b/libion/tests/device_test.cpp
deleted file mode 100644
index eb3f7b6..0000000
--- a/libion/tests/device_test.cpp
+++ /dev/null
@@ -1,546 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <fcntl.h>
-#include <memory>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include <linux/ion_test.h>
-
-#include <gtest/gtest.h>
-
-#include <ion/ion.h>
-
-#include "ion_test_fixture.h"
-
-#define ALIGN(x,y) (((x) + ((y) - 1)) & ~((y) - 1))
-
-class Device : public IonAllHeapsTest {
- public:
- virtual void SetUp();
- virtual void TearDown();
- int m_deviceFd;
- void readDMA(int fd, void *buf, size_t size);
- void writeDMA(int fd, void *buf, size_t size);
- void readKernel(int fd, void *buf, size_t size);
- void writeKernel(int fd, void *buf, size_t size);
- void blowCache();
- void dirtyCache(void *ptr, size_t size);
-};
-
-void Device::SetUp()
-{
- IonAllHeapsTest::SetUp();
- m_deviceFd = open("/dev/ion-test", O_RDONLY);
- ASSERT_GE(m_deviceFd, 0);
-}
-
-void Device::TearDown()
-{
- ASSERT_EQ(0, close(m_deviceFd));
- IonAllHeapsTest::TearDown();
-}
-
-void Device::readDMA(int fd, void *buf, size_t size)
-{
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_SET_FD, fd));
- struct ion_test_rw_data ion_test_rw_data = {
- .ptr = (uint64_t)buf,
- .offset = 0,
- .size = size,
- .write = 0,
- };
-
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_DMA_MAPPING, &ion_test_rw_data));
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_SET_FD, -1));
-}
-
-void Device::writeDMA(int fd, void *buf, size_t size)
-{
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_SET_FD, fd));
- struct ion_test_rw_data ion_test_rw_data = {
- .ptr = (uint64_t)buf,
- .offset = 0,
- .size = size,
- .write = 1,
- };
-
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_DMA_MAPPING, &ion_test_rw_data));
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_SET_FD, -1));
-}
-
-void Device::readKernel(int fd, void *buf, size_t size)
-{
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_SET_FD, fd));
- struct ion_test_rw_data ion_test_rw_data = {
- .ptr = (uint64_t)buf,
- .offset = 0,
- .size = size,
- .write = 0,
- };
-
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_KERNEL_MAPPING, &ion_test_rw_data));
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_SET_FD, -1));
-}
-
-void Device::writeKernel(int fd, void *buf, size_t size)
-{
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_SET_FD, fd));
- struct ion_test_rw_data ion_test_rw_data = {
- .ptr = (uint64_t)buf,
- .offset = 0,
- .size = size,
- .write = 1,
- };
-
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_KERNEL_MAPPING, &ion_test_rw_data));
- ASSERT_EQ(0, ioctl(m_deviceFd, ION_IOC_TEST_SET_FD, -1));
-}
-
-void Device::blowCache()
-{
- const size_t bigger_than_cache = 8*1024*1024;
- void *buf1 = malloc(bigger_than_cache);
- void *buf2 = malloc(bigger_than_cache);
- memset(buf1, 0xaa, bigger_than_cache);
- memcpy(buf2, buf1, bigger_than_cache);
- free(buf1);
- free(buf2);
-}
-
-void Device::dirtyCache(void *ptr, size_t size)
-{
- /* try to dirty cache lines */
- for (size_t i = size-1; i > 0; i--) {
- ((volatile char *)ptr)[i];
- ((char *)ptr)[i] = i;
- }
-}
-
-TEST_F(Device, KernelReadCached)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = ION_FLAG_CACHED;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- for (int i = 0; i < 4096; i++)
- ((char *)ptr)[i] = i;
-
- ((char*)buf)[4096] = 0x12;
- readKernel(map_fd, buf, 4096);
- ASSERT_EQ(((char*)buf)[4096], 0x12);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)buf)[i]);
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, KernelWriteCached)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (int i = 0; i < 4096; i++)
- ((char *)buf)[i] = i;
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = ION_FLAG_CACHED;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- dirtyCache(ptr, 4096);
-
- writeKernel(map_fd, buf, 4096);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)ptr)[i]) << i;
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, DMAReadCached)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = ION_FLAG_CACHED;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- for (int i = 0; i < 4096; i++)
- ((char *)ptr)[i] = i;
-
- readDMA(map_fd, buf, 4096);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)buf)[i]);
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, DMAWriteCached)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (int i = 0; i < 4096; i++)
- ((char *)buf)[i] = i;
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = ION_FLAG_CACHED;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- dirtyCache(ptr, 4096);
-
- writeDMA(map_fd, buf, 4096);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)ptr)[i]) << i;
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, KernelReadCachedNeedsSync)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- for (int i = 0; i < 4096; i++)
- ((char *)ptr)[i] = i;
-
- ((char*)buf)[4096] = 0x12;
- readKernel(map_fd, buf, 4096);
- ASSERT_EQ(((char*)buf)[4096], 0x12);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)buf)[i]);
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, KernelWriteCachedNeedsSync)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (int i = 0; i < 4096; i++)
- ((char *)buf)[i] = i;
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- dirtyCache(ptr, 4096);
-
- writeKernel(map_fd, buf, 4096);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)ptr)[i]) << i;
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, DMAReadCachedNeedsSync)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- for (int i = 0; i < 4096; i++)
- ((char *)ptr)[i] = i;
-
- ion_sync_fd(m_ionFd, map_fd);
-
- readDMA(map_fd, buf, 4096);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)buf)[i]);
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, DMAWriteCachedNeedsSync)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (int i = 0; i < 4096; i++)
- ((char *)buf)[i] = i;
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- dirtyCache(ptr, 4096);
-
- writeDMA(map_fd, buf, 4096);
-
- ion_sync_fd(m_ionFd, map_fd);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)ptr)[i]) << i;
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-TEST_F(Device, KernelRead)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = 0;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- for (int i = 0; i < 4096; i++)
- ((char *)ptr)[i] = i;
-
- ((char*)buf)[4096] = 0x12;
- readKernel(map_fd, buf, 4096);
- ASSERT_EQ(((char*)buf)[4096], 0x12);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)buf)[i]);
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, KernelWrite)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (int i = 0; i < 4096; i++)
- ((char *)buf)[i] = i;
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = 0;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- dirtyCache(ptr, 4096);
-
- writeKernel(map_fd, buf, 4096);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)ptr)[i]) << i;
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, DMARead)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = 0;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- for (int i = 0; i < 4096; i++)
- ((char *)ptr)[i] = i;
-
- readDMA(map_fd, buf, 4096);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)buf)[i]);
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, DMAWrite)
-{
- auto alloc_ptr = std::make_unique<char[]>(8192 + 1024);
- void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024);
-
- for (int i = 0; i < 4096; i++)
- ((char *)buf)[i] = i;
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = 0;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- dirtyCache(ptr, 4096);
-
- writeDMA(map_fd, buf, 4096);
-
- for (int i = 0; i < 4096; i++)
- ASSERT_EQ((char)i, ((char *)ptr)[i]) << i;
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
-
-TEST_F(Device, IsCached)
-{
- auto buf_ptr = std::make_unique<char[]>(4096);
- void *buf = buf_ptr.get();
-
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- int map_fd = -1;
- unsigned int flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, flags, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- dirtyCache(ptr, 4096);
-
- readDMA(map_fd, buf, 4096);
-
- bool same = true;
- for (int i = 4096-16; i >= 0; i -= 16)
- if (((char *)buf)[i] != i)
- same = false;
- ASSERT_FALSE(same);
-
- ASSERT_EQ(0, munmap(ptr, 4096));
- ASSERT_EQ(0, close(map_fd));
- }
-}
diff --git a/libion/tests/exit_test.cpp b/libion/tests/exit_test.cpp
index cdd3e27..f312389 100644
--- a/libion/tests/exit_test.cpp
+++ b/libion/tests/exit_test.cpp
@@ -22,206 +22,206 @@
#include "ion_test_fixture.h"
-class Exit : public IonAllHeapsTest {
-};
+class Exit : public IonTest {};
-TEST_F(Exit, WithAlloc)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Exit, WithAllocFd) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
- EXPECT_EXIT({
- ion_user_handle_t handle = 0;
+ EXPECT_EXIT(
+ {
+ int handle_fd = -1;
- ASSERT_EQ(0, ion_alloc(m_ionFd, size, 0, heapMask, 0, &handle));
- ASSERT_TRUE(handle != 0);
- exit(0);
- }, ::testing::ExitedWithCode(0), "");
+ ASSERT_EQ(0,
+ ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &handle_fd));
+ ASSERT_NE(-1, handle_fd);
+ exit(0);
+ },
+ ::testing::ExitedWithCode(0), "");
}
}
}
-TEST_F(Exit, WithAllocFd)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
- for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- SCOPED_TRACE(::testing::Message() << "size " << size);
- EXPECT_EXIT({
- int handle_fd = -1;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, 0, &handle_fd));
- ASSERT_NE(-1, handle_fd);
- exit(0);
- }, ::testing::ExitedWithCode(0), "");
- }
- }
-}
-
-TEST_F(Exit, WithRepeatedAllocFd)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Exit, WithRepeatedAllocFd) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
for (unsigned int i = 0; i < 1024; i++) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
- ASSERT_EXIT({
- int handle_fd = -1;
+ ASSERT_EXIT(
+ {
+ int handle_fd = -1;
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, 0, &handle_fd));
- ASSERT_NE(-1, handle_fd);
- exit(0);
- }, ::testing::ExitedWithCode(0), "")
- << "failed on heap " << heapMask
- << " and size " << size
- << " on iteration " << i;
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0,
+ &handle_fd));
+ ASSERT_NE(-1, handle_fd);
+ exit(0);
+ },
+ ::testing::ExitedWithCode(0), "")
+ << "failed on heap " << heap.name << ":" << heap.type << ":" << heap.heap_id
+ << " and size " << size << " on iteration " << i;
}
}
}
}
-
-TEST_F(Exit, WithMapping)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Exit, WithMapping) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
- EXPECT_EXIT({
- int map_fd = -1;
+ EXPECT_EXIT(
+ {
+ int map_fd = -1;
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, 0, &map_fd));
- ASSERT_GE(map_fd, 0);
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &map_fd));
+ ASSERT_GE(map_fd, 0);
- void *ptr;
- ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
- exit(0);
- }, ::testing::ExitedWithCode(0), "");
- }
- }
-
-}
-
-TEST_F(Exit, WithPartialMapping)
-{
- static const size_t allocationSizes[] = {64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
- for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- SCOPED_TRACE(::testing::Message() << "size " << size);
- EXPECT_EXIT({
- int map_fd = -1;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, 0, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- ASSERT_EQ(0, munmap(ptr, size / 2));
- exit(0);
- }, ::testing::ExitedWithCode(0), "");
+ void* ptr;
+ ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != NULL);
+ exit(0);
+ },
+ ::testing::ExitedWithCode(0), "");
}
}
}
-TEST_F(Exit, WithMappingCached)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Exit, WithPartialMapping) {
+ static const size_t allocationSizes[] = {64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
- EXPECT_EXIT({
- int map_fd = -1;
+ EXPECT_EXIT(
+ {
+ int map_fd = -1;
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, ION_FLAG_CACHED, &map_fd));
- ASSERT_GE(map_fd, 0);
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &map_fd));
+ ASSERT_GE(map_fd, 0);
- void *ptr;
- ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
- exit(0);
- }, ::testing::ExitedWithCode(0), "");
- }
- }
+ void* ptr;
+ ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != NULL);
-}
-
-TEST_F(Exit, WithPartialMappingCached)
-{
- static const size_t allocationSizes[] = {64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
- for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- SCOPED_TRACE(::testing::Message() << "size " << size);
- EXPECT_EXIT({
- int map_fd = -1;
-
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, ION_FLAG_CACHED, &map_fd));
- ASSERT_GE(map_fd, 0);
-
- void *ptr;
- ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
-
- ASSERT_EQ(0, munmap(ptr, size / 2));
- exit(0);
- }, ::testing::ExitedWithCode(0), "");
+ ASSERT_EQ(0, munmap(ptr, size / 2));
+ exit(0);
+ },
+ ::testing::ExitedWithCode(0), "");
}
}
}
-TEST_F(Exit, WithMappingNeedsSync)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Exit, WithMappingCached) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
- EXPECT_EXIT({
- int map_fd = -1;
+ EXPECT_EXIT(
+ {
+ int map_fd = -1;
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC, &map_fd));
- ASSERT_GE(map_fd, 0);
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id),
+ ION_FLAG_CACHED, &map_fd));
+ ASSERT_GE(map_fd, 0);
- void *ptr;
- ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
- exit(0);
- }, ::testing::ExitedWithCode(0), "");
+ void* ptr;
+ ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != NULL);
+ exit(0);
+ },
+ ::testing::ExitedWithCode(0), "");
}
}
-
}
-TEST_F(Exit, WithPartialMappingNeedsSync)
-{
- static const size_t allocationSizes[] = {64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Exit, WithPartialMappingCached) {
+ static const size_t allocationSizes[] = {64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
- EXPECT_EXIT({
- int map_fd = -1;
+ EXPECT_EXIT(
+ {
+ int map_fd = -1;
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC, &map_fd));
- ASSERT_GE(map_fd, 0);
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id),
+ ION_FLAG_CACHED, &map_fd));
+ ASSERT_GE(map_fd, 0);
- void *ptr;
- ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
+ void* ptr;
+ ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != NULL);
- ASSERT_EQ(0, munmap(ptr, size / 2));
- exit(0);
- }, ::testing::ExitedWithCode(0), "");
+ ASSERT_EQ(0, munmap(ptr, size / 2));
+ exit(0);
+ },
+ ::testing::ExitedWithCode(0), "");
+ }
+ }
+}
+
+TEST_F(Exit, WithMappingNeedsSync) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
+ for (size_t size : allocationSizes) {
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
+ SCOPED_TRACE(::testing::Message() << "size " << size);
+ EXPECT_EXIT(
+ {
+ int map_fd = -1;
+
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id),
+ ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC,
+ &map_fd));
+ ASSERT_GE(map_fd, 0);
+
+ void* ptr;
+ ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != NULL);
+ exit(0);
+ },
+ ::testing::ExitedWithCode(0), "");
+ }
+ }
+}
+
+TEST_F(Exit, WithPartialMappingNeedsSync) {
+ static const size_t allocationSizes[] = {64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
+ for (size_t size : allocationSizes) {
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
+ SCOPED_TRACE(::testing::Message() << "size " << size);
+ EXPECT_EXIT(
+ {
+ int map_fd = -1;
+
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id),
+ ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC,
+ &map_fd));
+ ASSERT_GE(map_fd, 0);
+
+ void* ptr;
+ ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != NULL);
+
+ ASSERT_EQ(0, munmap(ptr, size / 2));
+ exit(0);
+ },
+ ::testing::ExitedWithCode(0), "");
}
}
}
diff --git a/libion/tests/formerly_valid_handle_test.cpp b/libion/tests/formerly_valid_handle_test.cpp
deleted file mode 100644
index 01ab8f3..0000000
--- a/libion/tests/formerly_valid_handle_test.cpp
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <sys/mman.h>
-
-#include <gtest/gtest.h>
-
-#include <ion/ion.h>
-
-#include "ion_test_fixture.h"
-
-class FormerlyValidHandle : public IonTest {
- public:
- virtual void SetUp();
- virtual void TearDown();
- ion_user_handle_t m_handle;
-};
-
-void FormerlyValidHandle::SetUp()
-{
- IonTest::SetUp();
- ASSERT_EQ(0, ion_alloc(m_ionFd, 4096, 0, 1/* ion_env->m_firstHeap */, 0, &m_handle));
- ASSERT_TRUE(m_handle != 0);
- ASSERT_EQ(0, ion_free(m_ionFd, m_handle));
-}
-
-void FormerlyValidHandle::TearDown()
-{
- m_handle = 0;
-}
-
-TEST_F(FormerlyValidHandle, free)
-{
- ASSERT_EQ(-EINVAL, ion_free(m_ionFd, m_handle));
-}
-
-TEST_F(FormerlyValidHandle, map)
-{
- int map_fd;
- unsigned char *ptr;
-
- ASSERT_EQ(-EINVAL, ion_map(m_ionFd, m_handle, 4096, PROT_READ, 0, 0, &ptr, &map_fd));
-}
-
-TEST_F(FormerlyValidHandle, share)
-{
- int share_fd;
-
- ASSERT_EQ(-EINVAL, ion_share(m_ionFd, m_handle, &share_fd));
-}
diff --git a/fs_mgr/libfiemap_writer/utility.h b/libion/tests/heap_query.cpp
similarity index 61%
rename from fs_mgr/libfiemap_writer/utility.h
rename to libion/tests/heap_query.cpp
index 2d418da..bad3bbf 100644
--- a/fs_mgr/libfiemap_writer/utility.h
+++ b/libion/tests/heap_query.cpp
@@ -14,17 +14,14 @@
* limitations under the License.
*/
-#pragma once
+#include <gtest/gtest.h>
+#include "ion_test_fixture.h"
-#include <string>
+class HeapQuery : public IonTest {};
-namespace android {
-namespace fiemap_writer {
+TEST_F(HeapQuery, AtleastOneHeap) {
+ ASSERT_GT(ion_heaps.size(), 0);
+}
-// Given a file that will be created, determine the maximum size its containing
-// filesystem allows. Note this is a theoretical maximum size; free space is
-// ignored entirely.
-uint64_t DetermineMaximumFileSize(const std::string& file_path);
-
-} // namespace fiemap_writer
-} // namespace android
+// TODO: Check if we expect some of the default
+// heap types to be present on all devices.
diff --git a/libion/tests/invalid_values_test.cpp b/libion/tests/invalid_values_test.cpp
index 77fea17..48fcd72 100644
--- a/libion/tests/invalid_values_test.cpp
+++ b/libion/tests/invalid_values_test.cpp
@@ -16,171 +16,71 @@
#include <sys/mman.h>
+#include <memory>
+#include <vector>
+
#include <gtest/gtest.h>
#include <ion/ion.h>
-
#include "ion_test_fixture.h"
-class InvalidValues : public IonAllHeapsTest {
- public:
- virtual void SetUp();
- virtual void TearDown();
- ion_user_handle_t m_validHandle;
- int m_validShareFd;
- ion_user_handle_t const m_badHandle = -1;
-};
+class InvalidValues : public IonTest {};
-void InvalidValues::SetUp()
-{
- IonAllHeapsTest::SetUp();
- ASSERT_EQ(0, ion_alloc(m_ionFd, 4096, 0, m_firstHeap, 0, &m_validHandle))
- << m_ionFd << " " << m_firstHeap;
- ASSERT_TRUE(m_validHandle != 0);
- ASSERT_EQ(0, ion_share(m_ionFd, m_validHandle, &m_validShareFd));
-}
-
-void InvalidValues::TearDown()
-{
- ASSERT_EQ(0, ion_free(m_ionFd, m_validHandle));
- ASSERT_EQ(0, close(m_validShareFd));
- m_validHandle = 0;
- IonAllHeapsTest::TearDown();
-}
-
-TEST_F(InvalidValues, ion_close)
-{
+TEST_F(InvalidValues, ion_close) {
EXPECT_EQ(-EBADF, ion_close(-1));
}
-TEST_F(InvalidValues, ion_alloc)
-{
- ion_user_handle_t handle;
- /* invalid ion_fd */
- int ret = ion_alloc(0, 4096, 0, m_firstHeap, 0, &handle);
- EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
- /* invalid ion_fd */
- EXPECT_EQ(-EBADF, ion_alloc(-1, 4096, 0, m_firstHeap, 0, &handle));
- /* no heaps */
- EXPECT_EQ(-ENODEV, ion_alloc(m_ionFd, 4096, 0, 0, 0, &handle));
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- /* zero size */
- EXPECT_EQ(-EINVAL, ion_alloc(m_ionFd, 0, 0, heapMask, 0, &handle));
- /* too large size */
- EXPECT_EQ(-EINVAL, ion_alloc(m_ionFd, -1, 0, heapMask, 0, &handle));
- /* bad alignment */
- EXPECT_EQ(-EINVAL, ion_alloc(m_ionFd, 4096, -1, heapMask, 0, &handle));
- /* NULL handle */
- EXPECT_EQ(-EINVAL, ion_alloc(m_ionFd, 4096, 0, heapMask, 0, NULL));
- }
-}
-
-TEST_F(InvalidValues, ion_alloc_fd)
-{
+TEST_F(InvalidValues, ion_alloc_fd) {
int fd;
- /* invalid ion_fd */
- int ret = ion_alloc_fd(0, 4096, 0, m_firstHeap, 0, &fd);
- EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
- /* invalid ion_fd */
- EXPECT_EQ(-EBADF, ion_alloc_fd(-1, 4096, 0, m_firstHeap, 0, &fd));
- /* no heaps */
- EXPECT_EQ(-ENODEV, ion_alloc_fd(m_ionFd, 4096, 0, 0, 0, &fd));
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- /* zero size */
- EXPECT_EQ(-EINVAL, ion_alloc_fd(m_ionFd, 0, 0, heapMask, 0, &fd));
- /* too large size */
- EXPECT_EQ(-EINVAL, ion_alloc_fd(m_ionFd, -1, 0, heapMask, 0, &fd));
- /* bad alignment */
- EXPECT_EQ(-EINVAL, ion_alloc_fd(m_ionFd, 4096, -1, heapMask, 0, &fd));
- /* NULL handle */
- EXPECT_EQ(-EINVAL, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, 0, NULL));
+ // no heaps
+ EXPECT_EQ(-ENODEV, ion_alloc_fd(ionfd, 4096, 0, 0, 0, &fd));
+ for (const auto& heap : ion_heaps) {
+ // invalid ion_fd
+ int ret = ion_alloc_fd(0, 4096, 0, (1 << heap.heap_id), 0, &fd);
+ EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
+ // invalid ion_fd
+ EXPECT_EQ(-EBADF, ion_alloc_fd(-1, 4096, 0, (1 << heap.heap_id), 0, &fd));
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
+ // zero size
+ EXPECT_EQ(-EINVAL, ion_alloc_fd(ionfd, 0, 0, (1 << heap.heap_id), 0, &fd));
+ // too large size
+ EXPECT_EQ(-EINVAL, ion_alloc_fd(ionfd, -1, 0, (1 << heap.heap_id), 0, &fd));
+ // bad alignment
+ // TODO: Current userspace and kernel code completely ignores alignment. So this
+ // test is going to fail. We need to completely remove alignment from the API.
+ // All memory by default is always page aligned. OR actually pass the alignment
+ // down into the kernel and make kernel respect the alignment.
+ // EXPECT_EQ(-EINVAL, ion_alloc_fd(ionfd, 4096, -1, (1 << heap.heap_id), 0, &fd));
+
+ // NULL fd
+ EXPECT_EQ(-EINVAL, ion_alloc_fd(ionfd, 4096, 0, (1 << heap.heap_id), 0, nullptr));
}
}
-TEST_F(InvalidValues, ion_free)
-{
- /* invalid ion fd */
- int ret = ion_free(0, m_validHandle);
- EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
- /* invalid ion fd */
- EXPECT_EQ(-EBADF, ion_free(-1, m_validHandle));
- /* zero handle */
- EXPECT_EQ(-EINVAL, ion_free(m_ionFd, 0));
- /* bad handle */
- EXPECT_EQ(-EINVAL, ion_free(m_ionFd, m_badHandle));
+TEST_F(InvalidValues, ion_query_heap_cnt) {
+ // NULL count
+ EXPECT_EQ(-EINVAL, ion_query_heap_cnt(ionfd, nullptr));
+
+ int heap_count;
+ // bad fd
+ EXPECT_EQ(-EBADF, ion_query_heap_cnt(-1, &heap_count));
}
-TEST_F(InvalidValues, ion_map)
-{
- int map_fd;
- unsigned char *ptr;
+TEST_F(InvalidValues, ion_query_get_heaps) {
+ int heap_count;
+ ASSERT_EQ(0, ion_query_heap_cnt(ionfd, &heap_count));
+ ASSERT_GT(heap_count, 0);
- /* invalid ion fd */
- int ret = ion_map(0, m_validHandle, 4096, PROT_READ, 0, 0, &ptr, &map_fd);
- EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
- /* invalid ion fd */
- EXPECT_EQ(-EBADF, ion_map(-1, m_validHandle, 4096, PROT_READ, 0, 0, &ptr, &map_fd));
- /* zero handle */
- EXPECT_EQ(-EINVAL, ion_map(m_ionFd, 0, 4096, PROT_READ, 0, 0, &ptr, &map_fd));
- /* bad handle */
- EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_badHandle, 4096, PROT_READ, 0, 0, &ptr, &map_fd));
- /* zero length */
- EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_validHandle, 0, PROT_READ, 0, 0, &ptr, &map_fd));
- /* bad prot */
- EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_validHandle, 4096, -1, 0, 0, &ptr, &map_fd));
- /* bad offset */
- EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_validHandle, 4096, PROT_READ, 0, -1, &ptr, &map_fd));
- /* NULL ptr */
- EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_validHandle, 4096, PROT_READ, 0, 0, NULL, &map_fd));
- /* NULL map_fd */
- EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_validHandle, 4096, PROT_READ, 0, 0, &ptr, NULL));
-}
+ // nullptr buffers, still returns success but without
+ // the ion_heap_data.
+ EXPECT_EQ(0, ion_query_get_heaps(ionfd, heap_count, nullptr));
-TEST_F(InvalidValues, ion_share)
-{
- int share_fd;
+ std::unique_ptr<struct ion_heap_data[]> heaps =
+ std::make_unique<struct ion_heap_data[]>(heap_count);
+ // bad fd
+ EXPECT_EQ(-EBADF, ion_query_get_heaps(-1, heap_count, heaps.get()));
- /* invalid ion fd */
- int ret = ion_share(0, m_validHandle, &share_fd);
- EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
- /* invalid ion fd */
- EXPECT_EQ(-EBADF, ion_share(-1, m_validHandle, &share_fd));
- /* zero handle */
- EXPECT_EQ(-EINVAL, ion_share(m_ionFd, 0, &share_fd));
- /* bad handle */
- EXPECT_EQ(-EINVAL, ion_share(m_ionFd, m_badHandle, &share_fd));
- /* NULL share_fd */
- EXPECT_EQ(-EINVAL, ion_share(m_ionFd, m_validHandle, NULL));
-}
-
-TEST_F(InvalidValues, ion_import)
-{
- ion_user_handle_t handle;
-
- /* invalid ion fd */
- int ret = ion_import(0, m_validShareFd, &handle);
- EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
- /* invalid ion fd */
- EXPECT_EQ(-EBADF, ion_import(-1, m_validShareFd, &handle));
- /* bad share_fd */
- EXPECT_EQ(-EINVAL, ion_import(m_ionFd, 0, &handle));
- /* invalid share_fd */
- EXPECT_EQ(-EBADF, ion_import(m_ionFd, -1, &handle));
- /* NULL handle */
- EXPECT_EQ(-EINVAL, ion_import(m_ionFd, m_validShareFd, NULL));
-}
-
-TEST_F(InvalidValues, ion_sync_fd)
-{
- /* invalid ion fd */
- int ret = ion_sync_fd(0, m_validShareFd);
- EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
- /* invalid ion fd */
- EXPECT_EQ(-EBADF, ion_sync_fd(-1, m_validShareFd));
- /* bad share_fd */
- EXPECT_EQ(-EINVAL, ion_sync_fd(m_ionFd, 0));
- /* invalid share_fd */
- EXPECT_EQ(-EBADF, ion_sync_fd(m_ionFd, -1));
+ // invalid heap data pointer
+ EXPECT_EQ(-EFAULT, ion_query_get_heaps(ionfd, heap_count, reinterpret_cast<void*>(0xdeadf00d)));
}
diff --git a/libion/tests/ion_4.12.h b/libion/tests/ion_4.12.h
new file mode 100644
index 0000000..614510c
--- /dev/null
+++ b/libion/tests/ion_4.12.h
@@ -0,0 +1,50 @@
+/****************************************************************************
+ ****************************************************************************
+ ***
+ *** This header was automatically generated from a Linux kernel header
+ *** of the same name, to make information necessary for userspace to
+ *** call into the kernel available to libc. It contains only constants,
+ *** structures, and macros generated from the original header, and thus,
+ *** contains no copyrightable information.
+ ***
+ *** To edit the content of this header, modify the corresponding
+ *** source file (e.g. under external/kernel-headers/original/) then
+ *** run bionic/libc/kernel/tools/update_all.py
+ ***
+ *** Any manual change here will be lost the next time this script will
+ *** be run. You've been warned!
+ ***
+ ****************************************************************************
+ ****************************************************************************/
+#ifndef _UAPI_LINUX_ION_NEW_H
+#define _UAPI_LINUX_ION_NEW_H
+#include <linux/ioctl.h>
+#include <linux/types.h>
+#define ION_NUM_HEAP_IDS (sizeof(unsigned int) * 8)
+struct ion_new_allocation_data {
+ __u64 len;
+ __u32 heap_id_mask;
+ __u32 flags;
+ __u32 fd;
+ __u32 unused;
+};
+#define MAX_HEAP_NAME 32
+struct ion_heap_data {
+ char name[MAX_HEAP_NAME];
+ __u32 type;
+ __u32 heap_id;
+ __u32 reserved0;
+ __u32 reserved1;
+ __u32 reserved2;
+};
+struct ion_heap_query {
+ __u32 cnt;
+ __u32 reserved0;
+ __u64 heaps;
+ __u32 reserved1;
+ __u32 reserved2;
+};
+#define ION_IOC_MAGIC 'I'
+#define ION_IOC_NEW_ALLOC _IOWR(ION_IOC_MAGIC, 0, struct ion_new_allocation_data)
+#define ION_IOC_HEAP_QUERY _IOWR(ION_IOC_MAGIC, 8, struct ion_heap_query)
+#endif
diff --git a/libion/tests/ion_test_fixture.cpp b/libion/tests/ion_test_fixture.cpp
index e20c730..935fe5c 100644
--- a/libion/tests/ion_test_fixture.cpp
+++ b/libion/tests/ion_test_fixture.cpp
@@ -15,59 +15,26 @@
*/
#include <gtest/gtest.h>
-
#include <ion/ion.h>
#include "ion_test_fixture.h"
-IonTest::IonTest() : m_ionFd(-1)
-{
-}
+IonTest::IonTest() : ionfd(-1), ion_heaps() {}
void IonTest::SetUp() {
- m_ionFd = ion_open();
- ASSERT_GE(m_ionFd, 0);
+ ionfd = ion_open();
+ ASSERT_GE(ionfd, 0);
+
+ int heap_count;
+ int ret = ion_query_heap_cnt(ionfd, &heap_count);
+ ASSERT_EQ(ret, 0);
+ ASSERT_GT(heap_count, 0);
+
+ ion_heaps.resize(heap_count, {});
+ ret = ion_query_get_heaps(ionfd, heap_count, ion_heaps.data());
+ ASSERT_EQ(ret, 0);
}
void IonTest::TearDown() {
- ion_close(m_ionFd);
-}
-
-IonAllHeapsTest::IonAllHeapsTest() :
- m_firstHeap(0),
- m_lastHeap(0),
- m_allHeaps()
-{
-}
-
-void IonAllHeapsTest::SetUp() {
- int fd = ion_open();
- ASSERT_GE(fd, 0);
-
- for (int i = 1; i != 0; i <<= 1) {
- ion_user_handle_t handle = 0;
- int ret;
- ret = ion_alloc(fd, 4096, 0, i, 0, &handle);
- if (ret == 0 && handle != 0) {
- ion_free(fd, handle);
- if (!m_firstHeap) {
- m_firstHeap = i;
- }
- m_lastHeap = i;
- m_allHeaps.push_back(i);
- } else {
- ASSERT_EQ(-ENODEV, ret);
- }
- }
- ion_close(fd);
-
- EXPECT_NE(0U, m_firstHeap);
- EXPECT_NE(0U, m_lastHeap);
-
- RecordProperty("Heaps", m_allHeaps.size());
- IonTest::SetUp();
-}
-
-void IonAllHeapsTest::TearDown() {
- IonTest::TearDown();
+ ion_close(ionfd);
}
diff --git a/libion/tests/ion_test_fixture.h b/libion/tests/ion_test_fixture.h
index 4098214..4f254b8 100644
--- a/libion/tests/ion_test_fixture.h
+++ b/libion/tests/ion_test_fixture.h
@@ -18,29 +18,19 @@
#define ION_TEST_FIXTURE_H_
#include <gtest/gtest.h>
+#include <vector>
+#include "ion_4.12.h"
using ::testing::Test;
class IonTest : public virtual Test {
- public:
+ public:
IonTest();
- virtual ~IonTest() {};
- virtual void SetUp();
- virtual void TearDown();
- int m_ionFd;
-};
-
-class IonAllHeapsTest : public IonTest {
- public:
- IonAllHeapsTest();
- virtual ~IonAllHeapsTest() {};
+ virtual ~IonTest(){};
virtual void SetUp();
virtual void TearDown();
-
- unsigned int m_firstHeap;
- unsigned int m_lastHeap;
-
- std::vector<unsigned int> m_allHeaps;
+ int ionfd;
+ std::vector<struct ion_heap_data> ion_heaps;
};
#endif /* ION_TEST_FIXTURE_H_ */
diff --git a/libion/tests/map_test.cpp b/libion/tests/map_test.cpp
index c006dc8..f1b47b7 100644
--- a/libion/tests/map_test.cpp
+++ b/libion/tests/map_test.cpp
@@ -15,61 +15,30 @@
*/
#include <sys/mman.h>
+#include <unistd.h>
#include <gtest/gtest.h>
#include <ion/ion.h>
-
#include "ion_test_fixture.h"
-class Map : public IonAllHeapsTest {
-};
+class Map : public IonTest {};
-TEST_F(Map, MapHandle)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Map, MapFd) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
- SCOPED_TRACE(::testing::Message() << "size " << size);
- ion_user_handle_t handle = 0;
-
- ASSERT_EQ(0, ion_alloc(m_ionFd, size, 0, heapMask, 0, &handle));
- ASSERT_TRUE(handle != 0);
-
- int map_fd = -1;
- unsigned char *ptr = NULL;
- ASSERT_EQ(0, ion_map(m_ionFd, handle, size, PROT_READ | PROT_WRITE, MAP_SHARED, 0, &ptr, &map_fd));
- ASSERT_TRUE(ptr != NULL);
- ASSERT_GE(map_fd, 0);
-
- ASSERT_EQ(0, close(map_fd));
-
- ASSERT_EQ(0, ion_free(m_ionFd, handle));
-
- memset(ptr, 0xaa, size);
-
- ASSERT_EQ(0, munmap(ptr, size));
- }
- }
-}
-
-TEST_F(Map, MapFd)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
- for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
int map_fd = -1;
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, 0, &map_fd));
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &map_fd));
ASSERT_GE(map_fd, 0);
- void *ptr;
+ void* ptr;
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
ASSERT_TRUE(ptr != NULL);
-
ASSERT_EQ(0, close(map_fd));
memset(ptr, 0xaa, size);
@@ -79,53 +48,51 @@
}
}
-TEST_F(Map, MapOffset)
-{
- for (unsigned int heapMask : m_allHeaps) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+TEST_F(Map, MapOffset) {
+ for (const auto& heap : ion_heaps) {
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
int map_fd = -1;
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, PAGE_SIZE * 2, 0, heapMask, 0, &map_fd));
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, getpagesize() * 2, 0, (1 << heap.heap_id), 0, &map_fd));
ASSERT_GE(map_fd, 0);
- unsigned char *ptr;
- ptr = (unsigned char *)mmap(NULL, PAGE_SIZE * 2, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ unsigned char* ptr;
+ ptr = (unsigned char*)mmap(NULL, getpagesize() * 2, PROT_READ | PROT_WRITE, MAP_SHARED,
+ map_fd, 0);
ASSERT_TRUE(ptr != NULL);
- memset(ptr, 0, PAGE_SIZE);
- memset(ptr + PAGE_SIZE, 0xaa, PAGE_SIZE);
+ memset(ptr, 0, getpagesize());
+ memset(ptr + getpagesize(), 0xaa, getpagesize());
- ASSERT_EQ(0, munmap(ptr, PAGE_SIZE * 2));
+ ASSERT_EQ(0, munmap(ptr, getpagesize() * 2));
- ptr = (unsigned char *)mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, PAGE_SIZE);
+ ptr = (unsigned char*)mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, map_fd,
+ getpagesize());
ASSERT_TRUE(ptr != NULL);
-
ASSERT_EQ(ptr[0], 0xaa);
- ASSERT_EQ(ptr[PAGE_SIZE - 1], 0xaa);
-
- ASSERT_EQ(0, munmap(ptr, PAGE_SIZE));
-
+ ASSERT_EQ(ptr[getpagesize() - 1], 0xaa);
+ ASSERT_EQ(0, munmap(ptr, getpagesize()));
ASSERT_EQ(0, close(map_fd));
}
}
-TEST_F(Map, MapCached)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Map, MapCached) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
int map_fd = -1;
unsigned int flags = ION_FLAG_CACHED;
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, flags, &map_fd));
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), flags, &map_fd));
ASSERT_GE(map_fd, 0);
- void *ptr;
+ void* ptr;
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
ASSERT_TRUE(ptr != NULL);
-
ASSERT_EQ(0, close(map_fd));
memset(ptr, 0xaa, size);
@@ -135,23 +102,22 @@
}
}
-TEST_F(Map, MapCachedNeedsSync)
-{
- static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
- for (unsigned int heapMask : m_allHeaps) {
+TEST_F(Map, MapCachedNeedsSync) {
+ static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
+ for (const auto& heap : ion_heaps) {
for (size_t size : allocationSizes) {
- SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
+ SCOPED_TRACE(::testing::Message()
+ << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
SCOPED_TRACE(::testing::Message() << "size " << size);
int map_fd = -1;
unsigned int flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
- ASSERT_EQ(0, ion_alloc_fd(m_ionFd, size, 0, heapMask, flags, &map_fd));
+ ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), flags, &map_fd));
ASSERT_GE(map_fd, 0);
- void *ptr;
+ void* ptr;
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
ASSERT_TRUE(ptr != NULL);
-
ASSERT_EQ(0, close(map_fd));
memset(ptr, 0xaa, size);
diff --git a/libmeminfo/Android.bp b/libmeminfo/Android.bp
index fc022bd..8dcc77b 100644
--- a/libmeminfo/Android.bp
+++ b/libmeminfo/Android.bp
@@ -26,10 +26,17 @@
"liblog",
"libprocinfo",
],
+ target: {
+ darwin: {
+ enabled: false,
+ },
+
+ },
}
cc_library {
name: "libmeminfo",
+ host_supported: true,
defaults: ["libmeminfo_defaults"],
export_include_dirs: ["include"],
export_shared_lib_headers: ["libbase"],
diff --git a/libmeminfo/tools/Android.bp b/libmeminfo/tools/Android.bp
index 2e89c41..3968c09 100644
--- a/libmeminfo/tools/Android.bp
+++ b/libmeminfo/tools/Android.bp
@@ -56,6 +56,7 @@
cc_binary {
name: "showmap",
+ host_supported: true,
cflags: [
"-Wall",
"-Werror",
@@ -66,6 +67,12 @@
"libbase",
"libmeminfo",
],
+
+ target: {
+ darwin: {
+ enabled: false,
+ },
+ },
}
cc_binary {
diff --git a/libmeminfo/tools/showmap.cpp b/libmeminfo/tools/showmap.cpp
index a80fa76..8ea2108 100644
--- a/libmeminfo/tools/showmap.cpp
+++ b/libmeminfo/tools/showmap.cpp
@@ -18,6 +18,7 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
+#include <sys/signal.h>
#include <sys/types.h>
#include <unistd.h>
@@ -56,7 +57,7 @@
static VmaInfo g_total;
static std::vector<VmaInfo> g_vmas;
-[[noreturn]] static void usage(int exit_status) {
+[[noreturn]] static void usage(const char* progname, int exit_status) {
fprintf(stderr,
"%s [-aqtv] [-f FILE] PID\n"
"-a\taddresses (show virtual memory map)\n"
@@ -64,7 +65,7 @@
"-t\tterse (show only items with private pages)\n"
"-v\tverbose (don't coalesce maps with the same name)\n"
"-f\tFILE (read from input from FILE instead of PID)\n",
- getprogname());
+ progname);
exit(exit_status);
}
@@ -239,22 +240,22 @@
g_filename = optarg;
break;
case 'h':
- usage(EXIT_SUCCESS);
+ usage(argv[0], EXIT_SUCCESS);
default:
- usage(EXIT_FAILURE);
+ usage(argv[0], EXIT_FAILURE);
}
}
if (g_filename.empty()) {
if ((argc - 1) < optind) {
fprintf(stderr, "Invalid arguments: Must provide <pid> at the end\n");
- usage(EXIT_FAILURE);
+ usage(argv[0], EXIT_FAILURE);
}
g_pid = atoi(argv[optind]);
if (g_pid <= 0) {
fprintf(stderr, "Invalid process id %s\n", argv[optind]);
- usage(EXIT_FAILURE);
+ usage(argv[0], EXIT_FAILURE);
}
g_filename = ::android::base::StringPrintf("/proc/%d/smaps", g_pid);
diff --git a/libmeminfo/vts/AndroidTest.xml b/libmeminfo/vts/AndroidTest.xml
index 530d16e..9614025 100644
--- a/libmeminfo/vts/AndroidTest.xml
+++ b/libmeminfo/vts/AndroidTest.xml
@@ -24,6 +24,7 @@
<option name="binary-test-source" value="_32bit::DATA/nativetest/vts_meminfo_test/vts_meminfo_test" />
<option name="binary-test-source" value="_64bit::DATA/nativetest64/vts_meminfo_test/vts_meminfo_test" />
<option name="binary-test-type" value="gtest"/>
+ <option name="precondition-first-api-level" value="29" />
<option name="test-timeout" value="10m"/>
</test>
</configuration>
diff --git a/libnativeloader/Android.bp b/libnativeloader/Android.bp
index debc43f..b860db9 100644
--- a/libnativeloader/Android.bp
+++ b/libnativeloader/Android.bp
@@ -70,3 +70,27 @@
host_supported: true,
export_include_dirs: ["include"],
}
+
+cc_test {
+ name: "libnativeloader_test",
+ srcs: [
+ "native_loader_test.cpp",
+ "native_loader.cpp",
+ "library_namespaces.cpp",
+ "native_loader_namespace.cpp",
+ "public_libraries.cpp",
+ ],
+ cflags: ["-DANDROID"],
+ static_libs: [
+ "libbase",
+ "liblog",
+ "libnativehelper",
+ "libgmock",
+ ],
+ header_libs: [
+ "libnativebridge-headers",
+ "libnativeloader-headers",
+ ],
+ system_shared_libs: ["libc", "libm"],
+ test_suites: ["device-tests"],
+}
diff --git a/libnativeloader/TEST_MAPPING b/libnativeloader/TEST_MAPPING
new file mode 100644
index 0000000..7becb77
--- /dev/null
+++ b/libnativeloader/TEST_MAPPING
@@ -0,0 +1,12 @@
+{
+ "presubmit": [
+ {
+ "name": "libnativeloader_test"
+ }
+ ],
+ "imports": [
+ {
+ "path": "cts/tests/tests/jni"
+ }
+ ]
+}
diff --git a/libnativeloader/library_namespaces.cpp b/libnativeloader/library_namespaces.cpp
index f7f972f..a9eea8c 100644
--- a/libnativeloader/library_namespaces.cpp
+++ b/libnativeloader/library_namespaces.cpp
@@ -33,6 +33,8 @@
#include "public_libraries.h"
#include "utils.h"
+using android::base::Error;
+
namespace android::nativeloader {
namespace {
@@ -43,6 +45,7 @@
constexpr const char* kVendorNamespaceName = "sphal";
constexpr const char* kVndkNamespaceName = "vndk";
constexpr const char* kRuntimeNamespaceName = "runtime";
+constexpr const char* kNeuralNetworksNamespaceName = "neuralnetworks";
// classloader-namespace is a linker namespace that is created for the loaded
// app. To be specific, it is created for the app classloader. When
@@ -127,11 +130,11 @@
}
}
-NativeLoaderNamespace* LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
- jobject class_loader, bool is_shared,
- jstring dex_path, jstring java_library_path,
- jstring java_permitted_path,
- std::string* error_msg) {
+Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
+ jobject class_loader, bool is_shared,
+ jstring dex_path,
+ jstring java_library_path,
+ jstring java_permitted_path) {
std::string library_path; // empty string by default.
if (java_library_path != nullptr) {
@@ -157,14 +160,14 @@
}
// Initialize the anonymous namespace with the first non-empty library path.
+ Result<void> ret;
if (!library_path.empty() && !initialized_ &&
- !InitPublicNamespace(library_path.c_str(), error_msg)) {
- return nullptr;
+ !(ret = InitPublicNamespace(library_path.c_str()))) {
+ return ret.error();
}
- bool found = FindNamespaceByClassLoader(env, class_loader);
-
- LOG_ALWAYS_FATAL_IF(found, "There is already a namespace associated with this classloader");
+ LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
+ "There is already a namespace associated with this classloader");
std::string system_exposed_libraries = default_public_libraries();
const char* namespace_name = kClassloaderNamespaceName;
@@ -215,50 +218,66 @@
auto app_ns =
NativeLoaderNamespace::Create(namespace_name, library_path, permitted_path, parent_ns,
is_shared, target_sdk_version < 24 /* is_greylist_enabled */);
- if (app_ns.IsNil()) {
- *error_msg = app_ns.GetError();
- return nullptr;
+ if (!app_ns) {
+ return app_ns.error();
}
// ... and link to other namespaces to allow access to some public libraries
- bool is_bridged = app_ns.IsBridged();
+ bool is_bridged = app_ns->IsBridged();
auto platform_ns = NativeLoaderNamespace::GetPlatformNamespace(is_bridged);
- if (!app_ns.Link(platform_ns, system_exposed_libraries)) {
- *error_msg = app_ns.GetError();
- return nullptr;
+ if (!platform_ns) {
+ return platform_ns.error();
+ }
+
+ auto linked = app_ns->Link(*platform_ns, system_exposed_libraries);
+ if (!linked) {
+ return linked.error();
}
auto runtime_ns = NativeLoaderNamespace::GetExportedNamespace(kRuntimeNamespaceName, is_bridged);
// Runtime apex does not exist in host, and under certain build conditions.
- if (!runtime_ns.IsNil()) {
- if (!app_ns.Link(runtime_ns, runtime_public_libraries())) {
- *error_msg = app_ns.GetError();
- return nullptr;
+ if (runtime_ns) {
+ linked = app_ns->Link(*runtime_ns, runtime_public_libraries());
+ if (!linked) {
+ return linked.error();
+ }
+ }
+
+ // Give access to NNAPI libraries (apex-updated LLNDK library).
+ auto nnapi_ns =
+ NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged);
+ if (nnapi_ns) {
+ linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries());
+ if (!linked) {
+ return linked.error();
}
}
// Give access to VNDK-SP libraries from the 'vndk' namespace.
if (unbundled_vendor_or_product_app && !vndksp_libraries().empty()) {
auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
- if (!vndk_ns.IsNil() && !app_ns.Link(vndk_ns, vndksp_libraries())) {
- *error_msg = app_ns.GetError();
- return nullptr;
+ if (vndk_ns) {
+ linked = app_ns->Link(*vndk_ns, vndksp_libraries());
+ if (!linked) {
+ return linked.error();
+ }
}
}
- // Note that when vendor_ns is not configured, vendor_ns.IsNil() will be true
- // and it will result in linking to the default namespace which is expected
- // behavior in this case.
if (!vendor_public_libraries().empty()) {
auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
- if (!app_ns.Link(vendor_ns, vendor_public_libraries())) {
- *error_msg = dlerror();
- return nullptr;
+ // when vendor_ns is not configured, link to the platform namespace
+ auto target_ns = vendor_ns ? vendor_ns : platform_ns;
+ if (target_ns) {
+ linked = app_ns->Link(*target_ns, vendor_public_libraries());
+ if (!linked) {
+ return linked.error();
+ }
}
}
- namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), app_ns));
+ namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
return &(namespaces_.back().second);
}
@@ -276,7 +295,7 @@
return nullptr;
}
-bool LibraryNamespaces::InitPublicNamespace(const char* library_path, std::string* error_msg) {
+Result<void> LibraryNamespaces::InitPublicNamespace(const char* library_path) {
// Ask native bride if this apps library path should be handled by it
bool is_native_bridge = NativeBridgeIsPathSupported(library_path);
@@ -287,8 +306,7 @@
initialized_ = android_init_anonymous_namespace(default_public_libraries().c_str(),
is_native_bridge ? nullptr : library_path);
if (!initialized_) {
- *error_msg = dlerror();
- return false;
+ return Error() << dlerror();
}
// and now initialize native bridge namespaces if necessary.
@@ -296,11 +314,11 @@
initialized_ = NativeBridgeInitAnonymousNamespace(default_public_libraries().c_str(),
is_native_bridge ? library_path : nullptr);
if (!initialized_) {
- *error_msg = NativeBridgeGetError();
+ return Error() << NativeBridgeGetError();
}
}
- return initialized_;
+ return {};
}
NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
diff --git a/libnativeloader/library_namespaces.h b/libnativeloader/library_namespaces.h
index fd46cdc..e54bc0a 100644
--- a/libnativeloader/library_namespaces.h
+++ b/libnativeloader/library_namespaces.h
@@ -25,10 +25,13 @@
#include <list>
#include <string>
+#include <android-base/result.h>
#include <jni.h>
namespace android::nativeloader {
+using android::base::Result;
+
// LibraryNamespaces is a singleton object that manages NativeLoaderNamespace
// objects for an app process. Its main job is to create (and configure) a new
// NativeLoaderNamespace object for a Java ClassLoader, and to find an existing
@@ -42,14 +45,17 @@
LibraryNamespaces& operator=(const LibraryNamespaces&) = delete;
void Initialize();
- void Reset() { namespaces_.clear(); }
- NativeLoaderNamespace* Create(JNIEnv* env, uint32_t target_sdk_version, jobject class_loader,
- bool is_shared, jstring dex_path, jstring java_library_path,
- jstring java_permitted_path, std::string* error_msg);
+ void Reset() {
+ namespaces_.clear();
+ initialized_ = false;
+ }
+ Result<NativeLoaderNamespace*> Create(JNIEnv* env, uint32_t target_sdk_version,
+ jobject class_loader, bool is_shared, jstring dex_path,
+ jstring java_library_path, jstring java_permitted_path);
NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader);
private:
- bool InitPublicNamespace(const char* library_path, std::string* error_msg);
+ Result<void> InitPublicNamespace(const char* library_path);
NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader);
bool initialized_;
diff --git a/libnativeloader/native_loader.cpp b/libnativeloader/native_loader.cpp
index 0c29324..6d3c057 100644
--- a/libnativeloader/native_loader.cpp
+++ b/libnativeloader/native_loader.cpp
@@ -92,12 +92,10 @@
jstring permitted_path) {
#if defined(__ANDROID__)
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
-
- std::string error_msg;
- bool success = g_namespaces->Create(env, target_sdk_version, class_loader, is_shared, dex_path,
- library_path, permitted_path, &error_msg) != nullptr;
- if (!success) {
- return env->NewStringUTF(error_msg.c_str());
+ auto ns = g_namespaces->Create(env, target_sdk_version, class_loader, is_shared, dex_path,
+ library_path, permitted_path);
+ if (!ns) {
+ return env->NewStringUTF(ns.error().message().c_str());
}
#else
UNUSED(env, target_sdk_version, class_loader, is_shared, dex_path, library_path, permitted_path);
@@ -139,11 +137,14 @@
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.
- std::string create_error_msg;
- if ((ns = g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */,
- nullptr, library_path, nullptr, &create_error_msg)) == nullptr) {
- *error_msg = strdup(create_error_msg.c_str());
+ Result<NativeLoaderNamespace*> isolated_ns =
+ g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */, nullptr,
+ library_path, nullptr);
+ if (!isolated_ns) {
+ *error_msg = strdup(isolated_ns.error().message().c_str());
return nullptr;
+ } else {
+ ns = *isolated_ns;
}
}
@@ -222,12 +223,14 @@
#if defined(__ANDROID__)
void* OpenNativeLibraryInNamespace(NativeLoaderNamespace* ns, const char* path,
bool* needs_native_bridge, char** error_msg) {
- void* handle = ns->Load(path);
- if (handle == nullptr) {
- *error_msg = ns->GetError();
+ auto handle = ns->Load(path);
+ if (!handle && error_msg != nullptr) {
+ *error_msg = strdup(handle.error().message().c_str());
}
- *needs_native_bridge = ns->IsBridged();
- return handle;
+ if (needs_native_bridge != nullptr) {
+ *needs_native_bridge = ns->IsBridged();
+ }
+ return handle ? *handle : nullptr;
}
// native_bridge_namespaces are not supported for callers of this function.
diff --git a/libnativeloader/native_loader_namespace.cpp b/libnativeloader/native_loader_namespace.cpp
index 58ac686..4add6e6 100644
--- a/libnativeloader/native_loader_namespace.cpp
+++ b/libnativeloader/native_loader_namespace.cpp
@@ -28,6 +28,9 @@
#include "nativeloader/dlext_namespaces.h"
+using android::base::Error;
+using android::base::Errorf;
+
namespace android {
namespace {
@@ -35,41 +38,54 @@
constexpr const char* kDefaultNamespaceName = "default";
constexpr const char* kPlatformNamespaceName = "platform";
-} // namespace
-
-NativeLoaderNamespace NativeLoaderNamespace::GetExportedNamespace(const std::string& name,
- bool is_bridged) {
- if (!is_bridged) {
- return NativeLoaderNamespace(name, android_get_exported_namespace(name.c_str()));
- } else {
- return NativeLoaderNamespace(name, NativeBridgeGetExportedNamespace(name.c_str()));
+std::string GetLinkerError(bool is_bridged) {
+ const char* msg = is_bridged ? NativeBridgeGetError() : dlerror();
+ if (msg == nullptr) {
+ return "no error";
}
+ return std::string(msg);
}
-char* NativeLoaderNamespace::GetError() const {
- if (!IsBridged()) {
- return strdup(dlerror());
+} // namespace
+
+Result<NativeLoaderNamespace> NativeLoaderNamespace::GetExportedNamespace(const std::string& name,
+ bool is_bridged) {
+ if (!is_bridged) {
+ auto raw = android_get_exported_namespace(name.c_str());
+ if (raw != nullptr) {
+ return NativeLoaderNamespace(name, raw);
+ }
} else {
- return strdup(NativeBridgeGetError());
+ auto raw = NativeBridgeGetExportedNamespace(name.c_str());
+ if (raw != nullptr) {
+ return NativeLoaderNamespace(name, raw);
+ }
}
+ return Errorf("namespace {} does not exist or exported", name);
}
// The platform namespace is called "default" for binaries in /system and
// "platform" for those in the Runtime APEX. Try "platform" first since
// "default" always exists.
-NativeLoaderNamespace NativeLoaderNamespace::GetPlatformNamespace(bool is_bridged) {
- NativeLoaderNamespace ns = GetExportedNamespace(kPlatformNamespaceName, is_bridged);
- if (ns.IsNil()) {
- ns = GetExportedNamespace(kDefaultNamespaceName, is_bridged);
+Result<NativeLoaderNamespace> NativeLoaderNamespace::GetPlatformNamespace(bool is_bridged) {
+ auto ns = GetExportedNamespace(kPlatformNamespaceName, is_bridged);
+ if (ns) return ns;
+ ns = GetExportedNamespace(kDefaultNamespaceName, is_bridged);
+ if (ns) return ns;
+
+ // If nothing is found, return NativeLoaderNamespace constructed from nullptr.
+ // nullptr also means default namespace to the linker.
+ if (!is_bridged) {
+ return NativeLoaderNamespace(kDefaultNamespaceName, static_cast<android_namespace_t*>(nullptr));
+ } else {
+ return NativeLoaderNamespace(kDefaultNamespaceName,
+ static_cast<native_bridge_namespace_t*>(nullptr));
}
- return ns;
}
-NativeLoaderNamespace NativeLoaderNamespace::Create(const std::string& name,
- const std::string& search_paths,
- const std::string& permitted_paths,
- const NativeLoaderNamespace* parent,
- bool is_shared, bool is_greylist_enabled) {
+Result<NativeLoaderNamespace> NativeLoaderNamespace::Create(
+ const std::string& name, const std::string& search_paths, const std::string& permitted_paths,
+ const NativeLoaderNamespace* parent, bool is_shared, bool is_greylist_enabled) {
bool is_bridged = false;
if (parent != nullptr) {
is_bridged = parent->IsBridged();
@@ -78,8 +94,11 @@
}
// Fall back to the platform namespace if no parent is set.
- const NativeLoaderNamespace& effective_parent =
- parent != nullptr ? *parent : GetPlatformNamespace(is_bridged);
+ auto platform_ns = GetPlatformNamespace(is_bridged);
+ if (!platform_ns) {
+ return platform_ns.error();
+ }
+ const NativeLoaderNamespace& effective_parent = parent != nullptr ? *parent : *platform_ns;
uint64_t type = ANDROID_NAMESPACE_TYPE_ISOLATED;
if (is_shared) {
@@ -93,37 +112,56 @@
android_namespace_t* raw =
android_create_namespace(name.c_str(), nullptr, search_paths.c_str(), type,
permitted_paths.c_str(), effective_parent.ToRawAndroidNamespace());
- return NativeLoaderNamespace(name, raw);
+ if (raw != nullptr) {
+ return NativeLoaderNamespace(name, raw);
+ }
} else {
native_bridge_namespace_t* raw = NativeBridgeCreateNamespace(
name.c_str(), nullptr, search_paths.c_str(), type, permitted_paths.c_str(),
effective_parent.ToRawNativeBridgeNamespace());
- return NativeLoaderNamespace(name, raw);
+ if (raw != nullptr) {
+ return NativeLoaderNamespace(name, raw);
+ }
}
+ return Errorf("failed to create {} namespace name:{}, search_paths:{}, permitted_paths:{}",
+ is_bridged ? "bridged" : "native", name, search_paths, permitted_paths);
}
-bool NativeLoaderNamespace::Link(const NativeLoaderNamespace& target,
- const std::string& shared_libs) const {
+Result<void> NativeLoaderNamespace::Link(const NativeLoaderNamespace& target,
+ const std::string& shared_libs) const {
LOG_ALWAYS_FATAL_IF(shared_libs.empty(), "empty share lib when linking %s to %s",
this->name().c_str(), target.name().c_str());
if (!IsBridged()) {
- return android_link_namespaces(this->ToRawAndroidNamespace(), target.ToRawAndroidNamespace(),
- shared_libs.c_str());
+ if (android_link_namespaces(this->ToRawAndroidNamespace(), target.ToRawAndroidNamespace(),
+ shared_libs.c_str())) {
+ return {};
+ }
} else {
- return NativeBridgeLinkNamespaces(this->ToRawNativeBridgeNamespace(),
- target.ToRawNativeBridgeNamespace(), shared_libs.c_str());
+ if (NativeBridgeLinkNamespaces(this->ToRawNativeBridgeNamespace(),
+ target.ToRawNativeBridgeNamespace(), shared_libs.c_str())) {
+ return {};
+ }
}
+ return Error() << GetLinkerError(IsBridged());
}
-void* NativeLoaderNamespace::Load(const char* lib_name) const {
+Result<void*> NativeLoaderNamespace::Load(const char* lib_name) const {
if (!IsBridged()) {
android_dlextinfo extinfo;
extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
extinfo.library_namespace = this->ToRawAndroidNamespace();
- return android_dlopen_ext(lib_name, RTLD_NOW, &extinfo);
+ void* handle = android_dlopen_ext(lib_name, RTLD_NOW, &extinfo);
+ if (handle != nullptr) {
+ return handle;
+ }
} else {
- return NativeBridgeLoadLibraryExt(lib_name, RTLD_NOW, this->ToRawNativeBridgeNamespace());
+ void* handle =
+ NativeBridgeLoadLibraryExt(lib_name, RTLD_NOW, this->ToRawNativeBridgeNamespace());
+ if (handle != nullptr) {
+ return handle;
+ }
}
+ return Error() << GetLinkerError(IsBridged());
}
} // namespace android
diff --git a/libnativeloader/native_loader_namespace.h b/libnativeloader/native_loader_namespace.h
index 71e4247..29b759c 100644
--- a/libnativeloader/native_loader_namespace.h
+++ b/libnativeloader/native_loader_namespace.h
@@ -21,22 +21,25 @@
#include <vector>
#include <android-base/logging.h>
+#include <android-base/result.h>
#include <android/dlext.h>
#include <log/log.h>
#include <nativebridge/native_bridge.h>
namespace android {
+using android::base::Result;
+
// NativeLoaderNamespace abstracts a linker namespace for the native
// architecture (ex: arm on arm) or the translated architecture (ex: arm on
// x86). Instances of this class are managed by LibraryNamespaces object.
struct NativeLoaderNamespace {
public:
- // TODO(return with errors)
- static NativeLoaderNamespace Create(const std::string& name, const std::string& search_paths,
- const std::string& permitted_paths,
- const NativeLoaderNamespace* parent, bool is_shared,
- bool is_greylist_enabled);
+ static Result<NativeLoaderNamespace> Create(const std::string& name,
+ const std::string& search_paths,
+ const std::string& permitted_paths,
+ const NativeLoaderNamespace* parent, bool is_shared,
+ bool is_greylist_enabled);
NativeLoaderNamespace(NativeLoaderNamespace&&) = default;
NativeLoaderNamespace(const NativeLoaderNamespace&) = default;
@@ -47,16 +50,13 @@
std::string name() const { return name_; }
bool IsBridged() const { return raw_.index() == 1; }
- bool IsNil() const {
- return IsBridged() ? std::get<1>(raw_) == nullptr : std::get<0>(raw_) == nullptr;
- }
- bool Link(const NativeLoaderNamespace& target, const std::string& shared_libs) const;
- void* Load(const char* lib_name) const;
- char* GetError() const;
+ Result<void> Link(const NativeLoaderNamespace& target, const std::string& shared_libs) const;
+ Result<void*> Load(const char* lib_name) const;
- static NativeLoaderNamespace GetExportedNamespace(const std::string& name, bool is_bridged);
- static NativeLoaderNamespace GetPlatformNamespace(bool is_bridged);
+ static Result<NativeLoaderNamespace> GetExportedNamespace(const std::string& name,
+ bool is_bridged);
+ static Result<NativeLoaderNamespace> GetPlatformNamespace(bool is_bridged);
private:
explicit NativeLoaderNamespace(const std::string& name, android_namespace_t* ns)
diff --git a/libnativeloader/native_loader_test.cpp b/libnativeloader/native_loader_test.cpp
new file mode 100644
index 0000000..b939eee
--- /dev/null
+++ b/libnativeloader/native_loader_test.cpp
@@ -0,0 +1,574 @@
+/*
+ * 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 <dlfcn.h>
+#include <memory>
+#include <unordered_map>
+
+#include <android-base/strings.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <jni.h>
+
+#include "native_loader_namespace.h"
+#include "nativeloader/dlext_namespaces.h"
+#include "nativeloader/native_loader.h"
+#include "public_libraries.h"
+
+using namespace ::testing;
+
+namespace android {
+namespace nativeloader {
+
+// gmock interface that represents interested platform APIs on libdl and libnativebridge
+class Platform {
+ public:
+ virtual ~Platform() {}
+
+ // libdl APIs
+ virtual void* dlopen(const char* filename, int flags) = 0;
+ virtual int dlclose(void* handle) = 0;
+ virtual char* dlerror(void) = 0;
+
+ // These mock_* are the APIs semantically the same across libdl and libnativebridge.
+ // Instead of having two set of mock APIs for the two, define only one set with an additional
+ // argument 'bool bridged' to identify the context (i.e., called for libdl or libnativebridge).
+ typedef char* mock_namespace_handle;
+ virtual bool mock_init_anonymous_namespace(bool bridged, const char* sonames,
+ const char* search_paths) = 0;
+ virtual mock_namespace_handle mock_create_namespace(
+ bool bridged, const char* name, const char* ld_library_path, const char* default_library_path,
+ uint64_t type, const char* permitted_when_isolated_path, mock_namespace_handle parent) = 0;
+ virtual bool mock_link_namespaces(bool bridged, mock_namespace_handle from,
+ mock_namespace_handle to, const char* sonames) = 0;
+ virtual mock_namespace_handle mock_get_exported_namespace(bool bridged, const char* name) = 0;
+ virtual void* mock_dlopen_ext(bool bridged, const char* filename, int flags,
+ mock_namespace_handle ns) = 0;
+
+ // libnativebridge APIs for which libdl has no corresponding APIs
+ virtual bool NativeBridgeInitialized() = 0;
+ virtual const char* NativeBridgeGetError() = 0;
+ virtual bool NativeBridgeIsPathSupported(const char*) = 0;
+ virtual bool NativeBridgeIsSupported(const char*) = 0;
+
+ // To mock "ClassLoader Object.getParent()"
+ virtual const char* JniObject_getParent(const char*) = 0;
+};
+
+// The mock does not actually create a namespace object. But simply casts the pointer to the
+// string for the namespace name as the handle to the namespace object.
+#define TO_ANDROID_NAMESPACE(str) \
+ reinterpret_cast<struct android_namespace_t*>(const_cast<char*>(str))
+
+#define TO_BRIDGED_NAMESPACE(str) \
+ reinterpret_cast<struct native_bridge_namespace_t*>(const_cast<char*>(str))
+
+#define TO_MOCK_NAMESPACE(ns) reinterpret_cast<Platform::mock_namespace_handle>(ns)
+
+// These represents built-in namespaces created by the linker according to ld.config.txt
+static std::unordered_map<std::string, Platform::mock_namespace_handle> namespaces = {
+ {"platform", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("platform"))},
+ {"default", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("default"))},
+ {"runtime", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("runtime"))},
+ {"sphal", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("sphal"))},
+ {"vndk", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("vndk"))},
+ {"neuralnetworks", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("neuralnetworks"))},
+};
+
+// The actual gmock object
+class MockPlatform : public Platform {
+ public:
+ MockPlatform(bool is_bridged) : is_bridged_(is_bridged) {
+ ON_CALL(*this, NativeBridgeIsSupported(_)).WillByDefault(Return(is_bridged_));
+ ON_CALL(*this, NativeBridgeIsPathSupported(_)).WillByDefault(Return(is_bridged_));
+ ON_CALL(*this, mock_get_exported_namespace(_, _))
+ .WillByDefault(Invoke([](bool, const char* name) -> mock_namespace_handle {
+ if (namespaces.find(name) != namespaces.end()) {
+ return namespaces[name];
+ }
+ return TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("(namespace not found"));
+ }));
+ }
+
+ // Mocking libdl APIs
+ MOCK_METHOD2(dlopen, void*(const char*, int));
+ MOCK_METHOD1(dlclose, int(void*));
+ MOCK_METHOD0(dlerror, char*());
+
+ // Mocking the common APIs
+ MOCK_METHOD3(mock_init_anonymous_namespace, bool(bool, const char*, const char*));
+ MOCK_METHOD7(mock_create_namespace,
+ mock_namespace_handle(bool, const char*, const char*, const char*, uint64_t,
+ const char*, mock_namespace_handle));
+ MOCK_METHOD4(mock_link_namespaces,
+ bool(bool, mock_namespace_handle, mock_namespace_handle, const char*));
+ MOCK_METHOD2(mock_get_exported_namespace, mock_namespace_handle(bool, const char*));
+ MOCK_METHOD4(mock_dlopen_ext, void*(bool, const char*, int, mock_namespace_handle));
+
+ // Mocking libnativebridge APIs
+ MOCK_METHOD0(NativeBridgeInitialized, bool());
+ MOCK_METHOD0(NativeBridgeGetError, const char*());
+ MOCK_METHOD1(NativeBridgeIsPathSupported, bool(const char*));
+ MOCK_METHOD1(NativeBridgeIsSupported, bool(const char*));
+
+ // Mocking "ClassLoader Object.getParent()"
+ MOCK_METHOD1(JniObject_getParent, const char*(const char*));
+
+ private:
+ bool is_bridged_;
+};
+
+static std::unique_ptr<MockPlatform> mock;
+
+// Provide C wrappers for the mock object.
+extern "C" {
+void* dlopen(const char* file, int flag) {
+ return mock->dlopen(file, flag);
+}
+
+int dlclose(void* handle) {
+ return mock->dlclose(handle);
+}
+
+char* dlerror(void) {
+ return mock->dlerror();
+}
+
+bool android_init_anonymous_namespace(const char* sonames, const char* search_path) {
+ return mock->mock_init_anonymous_namespace(false, sonames, search_path);
+}
+
+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) {
+ return TO_ANDROID_NAMESPACE(
+ mock->mock_create_namespace(false, name, ld_library_path, default_library_path, type,
+ permitted_when_isolated_path, TO_MOCK_NAMESPACE(parent)));
+}
+
+bool android_link_namespaces(struct android_namespace_t* from, struct android_namespace_t* to,
+ const char* sonames) {
+ return mock->mock_link_namespaces(false, TO_MOCK_NAMESPACE(from), TO_MOCK_NAMESPACE(to), sonames);
+}
+
+struct android_namespace_t* android_get_exported_namespace(const char* name) {
+ return TO_ANDROID_NAMESPACE(mock->mock_get_exported_namespace(false, name));
+}
+
+void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* info) {
+ return mock->mock_dlopen_ext(false, filename, flags, TO_MOCK_NAMESPACE(info->library_namespace));
+}
+
+// libnativebridge APIs
+bool NativeBridgeIsSupported(const char* libpath) {
+ return mock->NativeBridgeIsSupported(libpath);
+}
+
+struct native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name) {
+ return TO_BRIDGED_NAMESPACE(mock->mock_get_exported_namespace(true, name));
+}
+
+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) {
+ return TO_BRIDGED_NAMESPACE(
+ mock->mock_create_namespace(true, name, ld_library_path, default_library_path, type,
+ permitted_when_isolated_path, TO_MOCK_NAMESPACE(parent)));
+}
+
+bool NativeBridgeLinkNamespaces(struct native_bridge_namespace_t* from,
+ struct native_bridge_namespace_t* to, const char* sonames) {
+ return mock->mock_link_namespaces(true, TO_MOCK_NAMESPACE(from), TO_MOCK_NAMESPACE(to), sonames);
+}
+
+void* NativeBridgeLoadLibraryExt(const char* libpath, int flag,
+ struct native_bridge_namespace_t* ns) {
+ return mock->mock_dlopen_ext(true, libpath, flag, TO_MOCK_NAMESPACE(ns));
+}
+
+bool NativeBridgeInitialized() {
+ return mock->NativeBridgeInitialized();
+}
+
+bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames,
+ const char* anon_ns_library_path) {
+ return mock->mock_init_anonymous_namespace(true, public_ns_sonames, anon_ns_library_path);
+}
+
+const char* NativeBridgeGetError() {
+ return mock->NativeBridgeGetError();
+}
+
+bool NativeBridgeIsPathSupported(const char* path) {
+ return mock->NativeBridgeIsPathSupported(path);
+}
+
+} // extern "C"
+
+// A very simple JNI mock.
+// jstring is a pointer to utf8 char array. We don't need utf16 char here.
+// jobject, jclass, and jmethodID are also a pointer to utf8 char array
+// Only a few JNI methods that are actually used in libnativeloader are mocked.
+JNINativeInterface* CreateJNINativeInterface() {
+ JNINativeInterface* inf = new JNINativeInterface();
+ memset(inf, 0, sizeof(JNINativeInterface));
+
+ inf->GetStringUTFChars = [](JNIEnv*, jstring s, jboolean*) -> const char* {
+ return reinterpret_cast<const char*>(s);
+ };
+
+ inf->ReleaseStringUTFChars = [](JNIEnv*, jstring, const char*) -> void { return; };
+
+ inf->NewStringUTF = [](JNIEnv*, const char* bytes) -> jstring {
+ return reinterpret_cast<jstring>(const_cast<char*>(bytes));
+ };
+
+ inf->FindClass = [](JNIEnv*, const char* name) -> jclass {
+ return reinterpret_cast<jclass>(const_cast<char*>(name));
+ };
+
+ inf->CallObjectMethodV = [](JNIEnv*, jobject obj, jmethodID mid, va_list) -> jobject {
+ if (strcmp("getParent", reinterpret_cast<const char*>(mid)) == 0) {
+ // JniObject_getParent can be a valid jobject or nullptr if there is
+ // no parent classloader.
+ const char* ret = mock->JniObject_getParent(reinterpret_cast<const char*>(obj));
+ return reinterpret_cast<jobject>(const_cast<char*>(ret));
+ }
+ return nullptr;
+ };
+
+ inf->GetMethodID = [](JNIEnv*, jclass, const char* name, const char*) -> jmethodID {
+ return reinterpret_cast<jmethodID>(const_cast<char*>(name));
+ };
+
+ inf->NewWeakGlobalRef = [](JNIEnv*, jobject obj) -> jobject { return obj; };
+
+ inf->IsSameObject = [](JNIEnv*, jobject a, jobject b) -> jboolean {
+ return strcmp(reinterpret_cast<const char*>(a), reinterpret_cast<const char*>(b)) == 0;
+ };
+
+ return inf;
+}
+
+static void* const any_nonnull = reinterpret_cast<void*>(0x12345678);
+
+// Custom matcher for comparing namespace handles
+MATCHER_P(NsEq, other, "") {
+ *result_listener << "comparing " << reinterpret_cast<const char*>(arg) << " and " << other;
+ return strcmp(reinterpret_cast<const char*>(arg), reinterpret_cast<const char*>(other)) == 0;
+}
+
+/////////////////////////////////////////////////////////////////
+
+// Test fixture
+class NativeLoaderTest : public ::testing::TestWithParam<bool> {
+ protected:
+ bool IsBridged() { return GetParam(); }
+
+ void SetUp() override {
+ mock = std::make_unique<NiceMock<MockPlatform>>(IsBridged());
+
+ env = std::make_unique<JNIEnv>();
+ env->functions = CreateJNINativeInterface();
+ }
+
+ void SetExpectations() {
+ std::vector<std::string> default_public_libs =
+ android::base::Split(default_public_libraries(), ":");
+ for (auto l : default_public_libs) {
+ EXPECT_CALL(*mock, dlopen(StrEq(l.c_str()), RTLD_NOW | RTLD_NODELETE))
+ .WillOnce(Return(any_nonnull));
+ }
+ }
+
+ void RunTest() { InitializeNativeLoader(); }
+
+ void TearDown() override {
+ ResetNativeLoader();
+ delete env->functions;
+ mock.reset();
+ }
+
+ std::unique_ptr<JNIEnv> env;
+};
+
+/////////////////////////////////////////////////////////////////
+
+TEST_P(NativeLoaderTest, InitializeLoadsDefaultPublicLibraries) {
+ SetExpectations();
+ RunTest();
+}
+
+INSTANTIATE_TEST_SUITE_P(NativeLoaderTests, NativeLoaderTest, testing::Bool());
+
+/////////////////////////////////////////////////////////////////
+
+class NativeLoaderTest_Create : public NativeLoaderTest {
+ protected:
+ // Test inputs (initialized to the default values). Overriding these
+ // must be done before calling SetExpectations() and RunTest().
+ uint32_t target_sdk_version = 29;
+ std::string class_loader = "my_classloader";
+ bool is_shared = false;
+ std::string dex_path = "/data/app/foo/classes.dex";
+ std::string library_path = "/data/app/foo/lib/arm";
+ std::string permitted_path = "/data/app/foo/lib";
+
+ // expected output (.. for the default test inputs)
+ std::string expected_namespace_name = "classloader-namespace";
+ uint64_t expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
+ std::string expected_library_path = library_path;
+ std::string expected_permitted_path = std::string("/data:/mnt/expand:") + permitted_path;
+ std::string expected_parent_namespace = "platform";
+ bool expected_link_with_platform_ns = true;
+ bool expected_link_with_runtime_ns = true;
+ bool expected_link_with_sphal_ns = !vendor_public_libraries().empty();
+ bool expected_link_with_vndk_ns = false;
+ bool expected_link_with_default_ns = false;
+ bool expected_link_with_neuralnetworks_ns = true;
+ std::string expected_shared_libs_to_platform_ns = default_public_libraries();
+ std::string expected_shared_libs_to_runtime_ns = runtime_public_libraries();
+ std::string expected_shared_libs_to_sphal_ns = vendor_public_libraries();
+ std::string expected_shared_libs_to_vndk_ns = vndksp_libraries();
+ std::string expected_shared_libs_to_default_ns = default_public_libraries();
+ std::string expected_shared_libs_to_neuralnetworks_ns = neuralnetworks_public_libraries();
+
+ void SetExpectations() {
+ NativeLoaderTest::SetExpectations();
+
+ ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr));
+
+ EXPECT_CALL(*mock, NativeBridgeIsPathSupported(_)).Times(AnyNumber());
+ EXPECT_CALL(*mock, NativeBridgeInitialized()).Times(AnyNumber());
+
+ if (IsBridged()) {
+ EXPECT_CALL(*mock,
+ mock_init_anonymous_namespace(false, StrEq(default_public_libraries()), nullptr))
+ .WillOnce(Return(true));
+
+ EXPECT_CALL(*mock, NativeBridgeInitialized()).WillOnce(Return(true));
+ }
+
+ EXPECT_CALL(*mock, mock_init_anonymous_namespace(
+ Eq(IsBridged()), StrEq(default_public_libraries()), StrEq(library_path)))
+ .WillOnce(Return(true));
+ EXPECT_CALL(*mock, mock_create_namespace(
+ Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
+ StrEq(expected_library_path), expected_namespace_flags,
+ StrEq(expected_permitted_path), NsEq(expected_parent_namespace.c_str())))
+ .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(dex_path.c_str()))));
+ if (expected_link_with_platform_ns) {
+ EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("platform"),
+ StrEq(expected_shared_libs_to_platform_ns)))
+ .WillOnce(Return(true));
+ }
+ if (expected_link_with_runtime_ns) {
+ EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("runtime"),
+ StrEq(expected_shared_libs_to_runtime_ns)))
+ .WillOnce(Return(true));
+ }
+ if (expected_link_with_sphal_ns) {
+ EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("sphal"),
+ StrEq(expected_shared_libs_to_sphal_ns)))
+ .WillOnce(Return(true));
+ }
+ if (expected_link_with_vndk_ns) {
+ EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk"),
+ StrEq(expected_shared_libs_to_vndk_ns)))
+ .WillOnce(Return(true));
+ }
+ if (expected_link_with_default_ns) {
+ EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("default"),
+ StrEq(expected_shared_libs_to_default_ns)))
+ .WillOnce(Return(true));
+ }
+ if (expected_link_with_neuralnetworks_ns) {
+ EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("neuralnetworks"),
+ StrEq(expected_shared_libs_to_neuralnetworks_ns)))
+ .WillOnce(Return(true));
+ }
+ }
+
+ void RunTest() {
+ NativeLoaderTest::RunTest();
+
+ jstring err = CreateClassLoaderNamespace(
+ env(), target_sdk_version, env()->NewStringUTF(class_loader.c_str()), is_shared,
+ env()->NewStringUTF(dex_path.c_str()), env()->NewStringUTF(library_path.c_str()),
+ env()->NewStringUTF(permitted_path.c_str()));
+
+ // no error
+ EXPECT_EQ(err, nullptr);
+
+ if (!IsBridged()) {
+ struct android_namespace_t* ns =
+ FindNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
+
+ // The created namespace is for this apk
+ EXPECT_EQ(dex_path.c_str(), reinterpret_cast<const char*>(ns));
+ } else {
+ struct NativeLoaderNamespace* ns =
+ FindNativeLoaderNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
+
+ // The created namespace is for the this apk
+ EXPECT_STREQ(dex_path.c_str(),
+ reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
+ }
+ }
+
+ JNIEnv* env() { return NativeLoaderTest::env.get(); }
+};
+
+TEST_P(NativeLoaderTest_Create, DownloadedApp) {
+ SetExpectations();
+ RunTest();
+}
+
+TEST_P(NativeLoaderTest_Create, BundledSystemApp) {
+ dex_path = "/system/app/foo/foo.apk";
+ is_shared = true;
+
+ expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED;
+ SetExpectations();
+ RunTest();
+}
+
+TEST_P(NativeLoaderTest_Create, BundledVendorApp) {
+ dex_path = "/vendor/app/foo/foo.apk";
+ is_shared = true;
+
+ expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED;
+ SetExpectations();
+ RunTest();
+}
+
+TEST_P(NativeLoaderTest_Create, UnbundledVendorApp) {
+ dex_path = "/vendor/app/foo/foo.apk";
+ is_shared = false;
+
+ expected_namespace_name = "vendor-classloader-namespace";
+ expected_library_path = expected_library_path + ":/vendor/lib";
+ expected_permitted_path = expected_permitted_path + ":/vendor/lib";
+ expected_shared_libs_to_platform_ns =
+ expected_shared_libs_to_platform_ns + ":" + llndk_libraries();
+ expected_link_with_vndk_ns = true;
+ SetExpectations();
+ RunTest();
+}
+
+TEST_P(NativeLoaderTest_Create, BundledProductApp_pre30) {
+ dex_path = "/product/app/foo/foo.apk";
+ is_shared = true;
+
+ expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED;
+ SetExpectations();
+ RunTest();
+}
+
+TEST_P(NativeLoaderTest_Create, BundledProductApp_post30) {
+ dex_path = "/product/app/foo/foo.apk";
+ is_shared = true;
+ target_sdk_version = 30;
+
+ expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED;
+ SetExpectations();
+ RunTest();
+}
+
+TEST_P(NativeLoaderTest_Create, UnbundledProductApp_pre30) {
+ dex_path = "/product/app/foo/foo.apk";
+ is_shared = false;
+ SetExpectations();
+ RunTest();
+}
+
+TEST_P(NativeLoaderTest_Create, UnbundledProductApp_post30) {
+ dex_path = "/product/app/foo/foo.apk";
+ is_shared = false;
+ target_sdk_version = 30;
+
+ expected_namespace_name = "vendor-classloader-namespace";
+ expected_library_path = expected_library_path + ":/product/lib:/system/product/lib";
+ expected_permitted_path = expected_permitted_path + ":/product/lib:/system/product/lib";
+ expected_shared_libs_to_platform_ns =
+ expected_shared_libs_to_platform_ns + ":" + llndk_libraries();
+ expected_link_with_vndk_ns = true;
+ SetExpectations();
+ RunTest();
+}
+
+TEST_P(NativeLoaderTest_Create, TwoApks) {
+ SetExpectations();
+ const uint32_t second_app_target_sdk_version = 29;
+ const std::string second_app_class_loader = "second_app_classloader";
+ const bool second_app_is_shared = false;
+ const std::string second_app_dex_path = "/data/app/bar/classes.dex";
+ const std::string second_app_library_path = "/data/app/bar/lib/arm";
+ const std::string second_app_permitted_path = "/data/app/bar/lib";
+ const std::string expected_second_app_permitted_path =
+ std::string("/data:/mnt/expand:") + second_app_permitted_path;
+ const std::string expected_second_app_parent_namespace = "classloader-namespace";
+
+ // The scenario is that second app is loaded by the first app.
+ // So the first app's classloader (`classloader`) is parent of the second
+ // app's classloader.
+ ON_CALL(*mock, JniObject_getParent(StrEq(second_app_class_loader)))
+ .WillByDefault(Return(class_loader.c_str()));
+
+ // namespace for the second app is created. Its parent is set to the namespace
+ // of the first app.
+ EXPECT_CALL(*mock, mock_create_namespace(Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
+ StrEq(second_app_library_path), expected_namespace_flags,
+ StrEq(expected_second_app_permitted_path),
+ NsEq(dex_path.c_str())))
+ .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(second_app_dex_path.c_str()))));
+ EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), NsEq(second_app_dex_path.c_str()), _, _))
+ .WillRepeatedly(Return(true));
+
+ RunTest();
+ jstring err = CreateClassLoaderNamespace(
+ env(), second_app_target_sdk_version, env()->NewStringUTF(second_app_class_loader.c_str()),
+ second_app_is_shared, env()->NewStringUTF(second_app_dex_path.c_str()),
+ env()->NewStringUTF(second_app_library_path.c_str()),
+ env()->NewStringUTF(second_app_permitted_path.c_str()));
+
+ // success
+ EXPECT_EQ(err, nullptr);
+
+ if (!IsBridged()) {
+ struct android_namespace_t* ns =
+ FindNamespaceByClassLoader(env(), env()->NewStringUTF(second_app_class_loader.c_str()));
+
+ // The created namespace is for the second apk
+ EXPECT_EQ(second_app_dex_path.c_str(), reinterpret_cast<const char*>(ns));
+ } else {
+ struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader(
+ env(), env()->NewStringUTF(second_app_class_loader.c_str()));
+
+ // The created namespace is for the second apk
+ EXPECT_STREQ(second_app_dex_path.c_str(),
+ reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
+ }
+}
+
+INSTANTIATE_TEST_SUITE_P(NativeLoaderTests_Create, NativeLoaderTest_Create, testing::Bool());
+
+// TODO(b/130388701#comment22) add a test for anonymous namespace
+
+} // namespace nativeloader
+} // namespace android
diff --git a/libnativeloader/public_libraries.cpp b/libnativeloader/public_libraries.cpp
index c205eb1..6cee668 100644
--- a/libnativeloader/public_libraries.cpp
+++ b/libnativeloader/public_libraries.cpp
@@ -26,6 +26,7 @@
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
+#include <android-base/result.h>
#include <android-base/strings.h>
#include <log/log.h>
@@ -34,6 +35,9 @@
namespace android::nativeloader {
using namespace std::string_literals;
+using android::base::ErrnoError;
+using android::base::Errorf;
+using android::base::Result;
namespace {
@@ -51,6 +55,8 @@
constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/" LIB;
+constexpr const char* kNeuralNetworksApexPublicLibrary = "libneuralnetworks.so";
+
// TODO(b/130388701): do we need this?
std::string root_dir() {
static const char* android_root_env = getenv("ANDROID_ROOT");
@@ -89,22 +95,21 @@
file_name->insert(insert_pos, vndk_version_str());
}
-const std::function<bool(const std::string&, std::string*)> always_true =
- [](const std::string&, std::string*) { return true; };
+const std::function<Result<void>(const std::string&)> always_true =
+ [](const std::string&) -> Result<void> { return {}; };
-bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
- const std::function<bool(const std::string& /* soname */,
- std::string* /* error_msg */)>& check_soname,
- std::string* error_msg = nullptr) {
+Result<std::vector<std::string>> ReadConfig(
+ const std::string& configFile,
+ const std::function<Result<void>(const std::string& /* soname */)>& check_soname) {
// Read list of public native libraries from the config file.
std::string file_content;
if (!base::ReadFileToString(configFile, &file_content)) {
- if (error_msg) *error_msg = strerror(errno);
- return false;
+ return ErrnoError();
}
std::vector<std::string> lines = base::Split(file_content, "\n");
+ std::vector<std::string> sonames;
for (auto& line : lines) {
auto trimmed_line = base::Trim(line);
if (trimmed_line[0] == '#' || trimmed_line.empty()) {
@@ -114,8 +119,7 @@
if (space_pos != std::string::npos) {
std::string type = trimmed_line.substr(space_pos + 1);
if (type != "32" && type != "64") {
- if (error_msg) *error_msg = "Malformed line: " + line;
- return false;
+ return Errorf("Malformed line: {}", line);
}
#if defined(__LP64__)
// Skip 32 bit public library.
@@ -131,13 +135,13 @@
trimmed_line.resize(space_pos);
}
- if (check_soname(trimmed_line, error_msg)) {
- sonames->push_back(trimmed_line);
- } else {
- return false;
+ auto ret = check_soname(trimmed_line);
+ if (!ret) {
+ return ret.error();
}
+ sonames.push_back(trimmed_line);
}
- return true;
+ return sonames;
}
void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
@@ -160,24 +164,22 @@
"Error extracting company name from public native library list file path \"%s\"",
config_file_path.c_str());
- std::string error_msg;
-
- LOG_ALWAYS_FATAL_IF(
- !ReadConfig(config_file_path, sonames,
- [&company_name](const std::string& soname, std::string* error_msg) {
- if (android::base::StartsWith(soname, "lib") &&
- android::base::EndsWith(soname, "." + company_name + ".so")) {
- return true;
- } else {
- *error_msg = "Library name \"" + soname +
- "\" does not end with the company name: " + company_name +
- ".";
- return false;
- }
- },
- &error_msg),
- "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
- error_msg.c_str());
+ auto ret = ReadConfig(
+ config_file_path, [&company_name](const std::string& soname) -> Result<void> {
+ if (android::base::StartsWith(soname, "lib") &&
+ android::base::EndsWith(soname, "." + company_name + ".so")) {
+ return {};
+ } else {
+ return Errorf("Library name \"{}\" does not end with the company name {}.", soname,
+ company_name);
+ }
+ });
+ if (ret) {
+ sonames->insert(sonames->end(), ret->begin(), ret->end());
+ } else {
+ LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
+ config_file_path.c_str(), ret.error().message().c_str());
+ }
}
}
}
@@ -185,16 +187,17 @@
static std::string InitDefaultPublicLibraries() {
std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
- std::vector<std::string> sonames;
- std::string error_msg;
- LOG_ALWAYS_FATAL_IF(!ReadConfig(config_file, &sonames, always_true, &error_msg),
- "Error reading public native library list from \"%s\": %s",
- config_file.c_str(), error_msg.c_str());
+ auto sonames = ReadConfig(config_file, always_true);
+ if (!sonames) {
+ LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
+ config_file.c_str(), sonames.error().message().c_str());
+ return "";
+ }
std::string additional_libs = additional_public_libraries();
if (!additional_libs.empty()) {
auto vec = base::Split(additional_libs, ":");
- std::copy(vec.begin(), vec.end(), std::back_inserter(sonames));
+ std::copy(vec.begin(), vec.end(), std::back_inserter(*sonames));
}
// Remove the public libs in the runtime namespace.
@@ -214,12 +217,18 @@
continue;
}
- auto it = std::find(sonames.begin(), sonames.end(), lib_name);
- if (it != sonames.end()) {
- sonames.erase(it);
+ auto it = std::find(sonames->begin(), sonames->end(), lib_name);
+ if (it != sonames->end()) {
+ sonames->erase(it);
}
}
- return android::base::Join(sonames, ':');
+
+ // Remove the public libs in the nnapi namespace.
+ auto it = std::find(sonames->begin(), sonames->end(), kNeuralNetworksApexPublicLibrary);
+ if (it != sonames->end()) {
+ sonames->erase(it);
+ }
+ return android::base::Join(*sonames, ':');
}
static std::string InitRuntimePublicLibraries() {
@@ -235,9 +244,11 @@
static std::string InitVendorPublicLibraries() {
// This file is optional, quietly ignore if the file does not exist.
- std::vector<std::string> sonames;
- ReadConfig(kVendorPublicLibrariesFile, &sonames, always_true, nullptr);
- return android::base::Join(sonames, ':');
+ auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true);
+ if (!sonames) {
+ return "";
+ }
+ return android::base::Join(*sonames, ':');
}
// read /system/etc/public.libraries-<companyname>.txt and
@@ -254,17 +265,27 @@
static std::string InitLlndkLibraries() {
std::string config_file = kLlndkLibrariesFile;
InsertVndkVersionStr(&config_file);
- std::vector<std::string> sonames;
- ReadConfig(config_file, &sonames, always_true, nullptr);
- return android::base::Join(sonames, ':');
+ auto sonames = ReadConfig(config_file, always_true);
+ if (!sonames) {
+ LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
+ return "";
+ }
+ return android::base::Join(*sonames, ':');
}
static std::string InitVndkspLibraries() {
std::string config_file = kVndkLibrariesFile;
InsertVndkVersionStr(&config_file);
- std::vector<std::string> sonames;
- ReadConfig(config_file, &sonames, always_true, nullptr);
- return android::base::Join(sonames, ':');
+ auto sonames = ReadConfig(config_file, always_true);
+ if (!sonames) {
+ LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
+ return "";
+ }
+ return android::base::Join(*sonames, ':');
+}
+
+static std::string InitNeuralNetworksPublicLibraries() {
+ return kNeuralNetworksApexPublicLibrary;
}
} // namespace
@@ -289,6 +310,11 @@
return list;
}
+const std::string& neuralnetworks_public_libraries() {
+ static std::string list = InitNeuralNetworksPublicLibraries();
+ return list;
+}
+
const std::string& llndk_libraries() {
static std::string list = InitLlndkLibraries();
return list;
diff --git a/libnativeloader/public_libraries.h b/libnativeloader/public_libraries.h
index 9b6dea8..9bb3366 100644
--- a/libnativeloader/public_libraries.h
+++ b/libnativeloader/public_libraries.h
@@ -26,6 +26,7 @@
const std::string& runtime_public_libraries();
const std::string& vendor_public_libraries();
const std::string& extended_public_libraries();
+const std::string& neuralnetworks_public_libraries();
const std::string& llndk_libraries();
const std::string& vndksp_libraries();
diff --git a/libunwindstack/Android.bp b/libunwindstack/Android.bp
index a0a6b4f..73237e6 100644
--- a/libunwindstack/Android.bp
+++ b/libunwindstack/Android.bp
@@ -125,10 +125,6 @@
},
},
- whole_static_libs: [
- "libdemangle"
- ],
-
static_libs: [
"libprocinfo",
],
@@ -162,6 +158,7 @@
cc_test {
name: "libunwindstack_test",
defaults: ["libunwindstack_flags"],
+ isolated: true,
srcs: [
"tests/ArmExidxDecodeTest.cpp",
@@ -184,6 +181,7 @@
"tests/ElfInterfaceTest.cpp",
"tests/ElfTest.cpp",
"tests/ElfTestUtils.cpp",
+ "tests/IsolatedSettings.cpp",
"tests/JitDebugTest.cpp",
"tests/LocalUnwinderTest.cpp",
"tests/LogFake.cpp",
diff --git a/libunwindstack/Unwinder.cpp b/libunwindstack/Unwinder.cpp
index c95f852..7556482 100644
--- a/libunwindstack/Unwinder.cpp
+++ b/libunwindstack/Unwinder.cpp
@@ -27,8 +27,6 @@
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
-#include <demangle.h>
-
#include <unwindstack/Elf.h>
#include <unwindstack/JitDebug.h>
#include <unwindstack/MapInfo.h>
@@ -40,6 +38,9 @@
#include <unwindstack/DexFiles.h>
#endif
+// Use the demangler from libc++.
+extern "C" char* __cxa_demangle(const char*, char*, size_t*, int* status);
+
namespace unwindstack {
// Inject extra 'virtual' frame that represents the dex pc data.
@@ -330,7 +331,14 @@
}
if (!frame.function_name.empty()) {
- data += " (" + demangle(frame.function_name.c_str());
+ char* demangled_name = __cxa_demangle(frame.function_name.c_str(), nullptr, nullptr, nullptr);
+ if (demangled_name == nullptr) {
+ data += " (" + frame.function_name;
+ } else {
+ data += " (";
+ data += demangled_name;
+ free(demangled_name);
+ }
if (frame.function_offset != 0) {
data += android::base::StringPrintf("+%" PRId64, frame.function_offset);
}
diff --git a/libunwindstack/tests/IsolatedSettings.cpp b/libunwindstack/tests/IsolatedSettings.cpp
new file mode 100644
index 0000000..dbd8bd6
--- /dev/null
+++ b/libunwindstack/tests/IsolatedSettings.cpp
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+
+extern "C" bool GetInitialArgs(const char*** args, size_t* num_args) {
+ static const char* initial_args[2] = {"--slow_threshold_ms=90000",
+ "--deadline_threshold_ms=120000"};
+ *args = initial_args;
+ *num_args = 2;
+ return true;
+}
diff --git a/libunwindstack/tests/MapInfoCreateMemoryTest.cpp b/libunwindstack/tests/MapInfoCreateMemoryTest.cpp
index 5b4ca7c..6c1cfa2 100644
--- a/libunwindstack/tests/MapInfoCreateMemoryTest.cpp
+++ b/libunwindstack/tests/MapInfoCreateMemoryTest.cpp
@@ -58,7 +58,7 @@
ASSERT_TRUE(android::base::WriteFully(fd, buffer.data(), buffer.size()));
}
- static void SetUpTestSuite() {
+ void SetUp() override {
std::vector<uint8_t> buffer(12288, 0);
memcpy(buffer.data(), ELFMAG, SELFMAG);
buffer[EI_CLASS] = ELFCLASS32;
@@ -72,9 +72,7 @@
InitElf<Elf32_Ehdr, Elf32_Shdr>(elf32_at_map_.fd, 0x1000, 0x2000, ELFCLASS32);
InitElf<Elf64_Ehdr, Elf64_Shdr>(elf64_at_map_.fd, 0x2000, 0x3000, ELFCLASS64);
- }
- void SetUp() override {
memory_ = new MemoryFake;
process_memory_.reset(memory_);
}
@@ -82,17 +80,13 @@
MemoryFake* memory_;
std::shared_ptr<Memory> process_memory_;
- static TemporaryFile elf_;
+ TemporaryFile elf_;
- static TemporaryFile elf_at_1000_;
+ TemporaryFile elf_at_1000_;
- static TemporaryFile elf32_at_map_;
- static TemporaryFile elf64_at_map_;
+ TemporaryFile elf32_at_map_;
+ TemporaryFile elf64_at_map_;
};
-TemporaryFile MapInfoCreateMemoryTest::elf_;
-TemporaryFile MapInfoCreateMemoryTest::elf_at_1000_;
-TemporaryFile MapInfoCreateMemoryTest::elf32_at_map_;
-TemporaryFile MapInfoCreateMemoryTest::elf64_at_map_;
TEST_F(MapInfoCreateMemoryTest, end_le_start) {
MapInfo info(nullptr, 0x100, 0x100, 0, 0, elf_.path);
diff --git a/libunwindstack/tests/UnwindOfflineTest.cpp b/libunwindstack/tests/UnwindOfflineTest.cpp
index e6158a2..bded57a 100644
--- a/libunwindstack/tests/UnwindOfflineTest.cpp
+++ b/libunwindstack/tests/UnwindOfflineTest.cpp
@@ -1482,11 +1482,15 @@
" #09 pc 0000000000ed5e25 perfetto_unittests "
"(testing::internal::UnitTestImpl::RunAllTests()+581)\n"
" #10 pc 0000000000ef63f3 perfetto_unittests "
- "(_ZN7testing8internal38HandleSehExceptionsInMethodIfSupportedINS0_12UnitTestImplEbEET0_PT_"
- "MS4_FS3_vEPKc+131)\n"
+ "(bool "
+ "testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, "
+ "bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char "
+ "const*)+131)\n"
" #11 pc 0000000000ee2a21 perfetto_unittests "
- "(_ZN7testing8internal35HandleExceptionsInMethodIfSupportedINS0_12UnitTestImplEbEET0_PT_MS4_"
- "FS3_vEPKc+113)\n"
+ "(bool "
+ "testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, "
+ "bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char "
+ "const*)+113)\n"
" #12 pc 0000000000ed5bb9 perfetto_unittests (testing::UnitTest::Run()+185)\n"
" #13 pc 0000000000e900f0 perfetto_unittests (RUN_ALL_TESTS()+16)\n"
" #14 pc 0000000000e900d8 perfetto_unittests (main+56)\n"
diff --git a/libutils/include/utils/LightRefBase.h b/libutils/include/utils/LightRefBase.h
index e488e60..b04e5c1 100644
--- a/libutils/include/utils/LightRefBase.h
+++ b/libutils/include/utils/LightRefBase.h
@@ -47,8 +47,6 @@
return mCount.load(std::memory_order_relaxed);
}
- typedef LightRefBase<T> basetype;
-
protected:
inline ~LightRefBase() { }
diff --git a/libutils/include/utils/RefBase.h b/libutils/include/utils/RefBase.h
index 3a02a8a..42c6efb 100644
--- a/libutils/include/utils/RefBase.h
+++ b/libutils/include/utils/RefBase.h
@@ -296,8 +296,6 @@
getWeakRefs()->trackMe(enable, retain);
}
- typedef RefBase basetype;
-
protected:
RefBase();
virtual ~RefBase();
diff --git a/libziparchive/Android.bp b/libziparchive/Android.bp
index 3843252..0253f2f 100644
--- a/libziparchive/Android.bp
+++ b/libziparchive/Android.bp
@@ -76,6 +76,10 @@
"liblog",
],
+ // for FRIEND_TEST
+ static_libs: ["libgtest_prod"],
+ export_static_lib_headers: ["libgtest_prod"],
+
export_include_dirs: ["include"],
}
diff --git a/libziparchive/include/ziparchive/zip_writer.h b/libziparchive/include/ziparchive/zip_writer.h
index a2a0dbf..d68683d 100644
--- a/libziparchive/include/ziparchive/zip_writer.h
+++ b/libziparchive/include/ziparchive/zip_writer.h
@@ -19,6 +19,7 @@
#include <cstdio>
#include <ctime>
+#include <gtest/gtest_prod.h>
#include <memory>
#include <string>
#include <string_view>
@@ -165,6 +166,7 @@
int32_t StoreBytes(FileEntry* file, const void* data, uint32_t len);
int32_t CompressBytes(FileEntry* file, const void* data, uint32_t len);
int32_t FlushCompressedBytes(FileEntry* file);
+ bool ShouldUseDataDescriptor() const;
enum class State {
kWritingZip,
@@ -182,4 +184,6 @@
std::unique_ptr<z_stream, void (*)(z_stream*)> z_stream_;
std::vector<uint8_t> buffer_;
+
+ FRIEND_TEST(zipwriter, WriteToUnseekableFile);
};
diff --git a/libziparchive/zip_writer.cc b/libziparchive/zip_writer.cc
index 198154b..67279a6 100644
--- a/libziparchive/zip_writer.cc
+++ b/libziparchive/zip_writer.cc
@@ -455,6 +455,11 @@
return kNoError;
}
+bool ZipWriter::ShouldUseDataDescriptor() const {
+ // Only use a trailing "data descriptor" if the output isn't seekable.
+ return !seekable_;
+}
+
int32_t ZipWriter::FinishEntry() {
if (state_ != State::kWritingEntry) {
return kInvalidState;
@@ -467,7 +472,7 @@
}
}
- if ((current_file_entry_.compression_method & kCompressDeflated) || !seekable_) {
+ if (ShouldUseDataDescriptor()) {
// Some versions of ZIP don't allow STORED data to have a trailing DataDescriptor.
// If this file is not seekable, or if the data is compressed, write a DataDescriptor.
const uint32_t sig = DataDescriptor::kOptSignature;
@@ -515,7 +520,7 @@
for (FileEntry& file : files_) {
CentralDirectoryRecord cdr = {};
cdr.record_signature = CentralDirectoryRecord::kSignature;
- if ((file.compression_method & kCompressDeflated) || !seekable_) {
+ if (ShouldUseDataDescriptor()) {
cdr.gpb_flags |= kGPBDDFlagMask;
}
cdr.compression_method = file.compression_method;
diff --git a/libziparchive/zip_writer_test.cc b/libziparchive/zip_writer_test.cc
index c3da23c..d324d4b 100644
--- a/libziparchive/zip_writer_test.cc
+++ b/libziparchive/zip_writer_test.cc
@@ -243,6 +243,7 @@
ZipEntry data;
ASSERT_EQ(0, FindEntry(handle, "file.txt", &data));
EXPECT_EQ(kCompressDeflated, data.method);
+ EXPECT_EQ(0u, data.has_data_descriptor);
ASSERT_EQ(4u, data.uncompressed_length);
ASSERT_TRUE(AssertFileEntryContentsEq("helo", handle, &data));
@@ -351,6 +352,29 @@
CloseArchive(handle);
}
+TEST_F(zipwriter, WriteToUnseekableFile) {
+ const char* expected = "hello";
+ ZipWriter writer(file_);
+ writer.seekable_ = false;
+
+ ASSERT_EQ(0, writer.StartEntry("file.txt", 0));
+ ASSERT_EQ(0, writer.WriteBytes(expected, strlen(expected)));
+ ASSERT_EQ(0, writer.FinishEntry());
+ ASSERT_EQ(0, writer.Finish());
+ ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
+
+ ZipArchiveHandle handle;
+ ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
+ ZipEntry data;
+ ASSERT_EQ(0, FindEntry(handle, "file.txt", &data));
+ EXPECT_EQ(kCompressStored, data.method);
+ EXPECT_EQ(1u, data.has_data_descriptor);
+ EXPECT_EQ(strlen(expected), data.compressed_length);
+ ASSERT_EQ(strlen(expected), data.uncompressed_length);
+ ASSERT_TRUE(AssertFileEntryContentsEq(expected, handle, &data));
+ CloseArchive(handle);
+}
+
TEST_F(zipwriter, TruncateFileAfterBackup) {
ZipWriter writer(file_);
diff --git a/logcat/logcatd.rc b/logcat/logcatd.rc
index 25104eb..26c9de3 100644
--- a/logcat/logcatd.rc
+++ b/logcat/logcatd.rc
@@ -13,28 +13,13 @@
# expect /init to report failure if property empty (default)
setprop persist.logd.logpersistd.size ${persist.logd.logpersistd.count}
-on property:persist.logd.logpersistd.size=256
- setprop persist.logd.logpersistd.size ""
- setprop logd.logpersistd.size ""
-
on property:persist.logd.logpersistd.size=*
- # expect /init to report failure if property empty (default)
setprop logd.logpersistd.size ${persist.logd.logpersistd.size}
-on property:persist.logd.logpersistd.rotate_kbytes=1024
- setprop persist.logd.logpersistd.rotate_kbytes ""
- setprop logd.logpersistd.rotate_kbytes ""
-
on property:persist.logd.logpersistd.rotate_kbytes=*
- # expect /init to report failure if property empty (default)
- setprop logd.logpersistd.rotate_kbytes ${persist.logd.logpersistd.rotate_kbytes}
-
-on property:persist.logd.logpersistd.buffer=all
- setprop persist.logd.logpersistd.buffer ""
- setprop logd.logpersistd.buffer ""
+ setprop logd.logpersistd.rotate_kbytes ${persist.logd.logpersistd.rotate_kbytes}
on property:persist.logd.logpersistd.buffer=*
- # expect /init to report failure if property empty (default)
setprop logd.logpersistd.buffer ${persist.logd.logpersistd.buffer}
on property:persist.logd.logpersistd=logcatd
diff --git a/rootdir/Android.mk b/rootdir/Android.mk
index 246f9ac..76d6f7e 100644
--- a/rootdir/Android.mk
+++ b/rootdir/Android.mk
@@ -378,6 +378,62 @@
echo $(lib).so >> $@;)
#######################################
+# vndkcore.libraries.txt
+include $(CLEAR_VARS)
+LOCAL_MODULE := vndkcore.libraries.txt
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)
+LOCAL_MODULE_STEM := $(call append_vndk_version,$(LOCAL_MODULE))
+include $(BUILD_SYSTEM)/base_rules.mk
+$(LOCAL_BUILT_MODULE): PRIVATE_VNDK_CORE_LIBRARIES := $(VNDK_CORE_LIBRARIES)
+$(LOCAL_BUILT_MODULE):
+ @echo "Generate: $@"
+ @mkdir -p $(dir $@)
+ $(hide) echo -n > $@
+ $(hide) $(foreach lib,$(PRIVATE_VNDK_CORE_LIBRARIES), \
+ echo $(lib).so >> $@;)
+
+#######################################
+# vndkprivate.libraries.txt
+include $(CLEAR_VARS)
+LOCAL_MODULE := vndkprivate.libraries.txt
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)
+LOCAL_MODULE_STEM := $(call append_vndk_version,$(LOCAL_MODULE))
+include $(BUILD_SYSTEM)/base_rules.mk
+$(LOCAL_BUILT_MODULE): PRIVATE_VNDK_PRIVATE_LIBRARIES := $(VNDK_PRIVATE_LIBRARIES)
+$(LOCAL_BUILT_MODULE):
+ @echo "Generate: $@"
+ @mkdir -p $(dir $@)
+ $(hide) echo -n > $@
+ $(hide) $(foreach lib,$(PRIVATE_VNDK_PRIVATE_LIBRARIES), \
+ echo $(lib).so >> $@;)
+
+#######################################
+# sanitizer.libraries.txt
+include $(CLEAR_VARS)
+LOCAL_MODULE := sanitizer.libraries.txt
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)
+LOCAL_MODULE_STEM := $(LOCAL_MODULE)
+include $(BUILD_SYSTEM)/base_rules.mk
+$(LOCAL_BUILT_MODULE): PRIVATE_SANITIZER_RUNTIME_LIBRARIES := $(addsuffix .so,\
+ $(ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
+ $(HWADDRESS_SANITIZER_RUNTIME_LIBRARY) \
+ $(UBSAN_RUNTIME_LIBRARY) \
+ $(TSAN_RUNTIME_LIBRARY) \
+ $(2ND_ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
+ $(2ND_HWADDRESS_SANITIZER_RUNTIME_LIBRARY) \
+ $(2ND_UBSAN_RUNTIME_LIBRARY) \
+ $(2ND_TSAN_RUNTIME_LIBRARY))
+$(LOCAL_BUILT_MODULE):
+ @echo "Generate: $@"
+ @mkdir -p $(dir $@)
+ $(hide) echo -n > $@
+ $(hide) $(foreach lib,$(PRIVATE_SANITIZER_RUNTIME_LIBRARIES), \
+ echo $(lib) >> $@;)
+
+#######################################
# adb_debug.prop in debug ramdisk
include $(CLEAR_VARS)
LOCAL_MODULE := adb_debug.prop
diff --git a/rootdir/etc/ld.config.legacy.txt b/rootdir/etc/ld.config.legacy.txt
index ad14493..f0b1fd2 100644
--- a/rootdir/etc/ld.config.legacy.txt
+++ b/rootdir/etc/ld.config.legacy.txt
@@ -42,7 +42,7 @@
# APEX related namespaces.
###############################################################################
-additional.namespaces = runtime,conscrypt,media,resolv
+additional.namespaces = runtime,conscrypt,media,neuralnetworks,resolv
# Keep in sync with the "platform" namespace in art/build/apex/ld.config.txt.
# If a shared library or an executable requests a shared library that
@@ -51,8 +51,8 @@
# shared library cannot be loaded from the runtime namespace either, the
# dynamic linker tries to load the shared library from the resolv namespace.
# Finally, if all attempts fail, the dynamic linker returns an error.
-namespace.default.links = runtime,resolv
-namespace.default.asan.links = runtime,resolv
+namespace.default.links = runtime,resolv,neuralnetworks
+namespace.default.asan.links = runtime,resolv,neuralnetworks
namespace.default.link.runtime.shared_libs = libandroidicu.so
namespace.default.link.runtime.shared_libs += libdexfile_external.so
namespace.default.link.runtime.shared_libs += libdexfiled_external.so
@@ -71,6 +71,9 @@
# to be loaded in the default namespace.
namespace.default.link.resolv.shared_libs = libnetd_resolv.so
+# LLNDK library moved into apex
+namespace.default.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "runtime" APEX namespace
#
@@ -159,6 +162,27 @@
namespace.resolv.link.default.shared_libs += libvndksupport.so
###############################################################################
+# "neuralnetworks" APEX namespace
+#
+# This namespace is for libraries within the NNAPI APEX.
+###############################################################################
+namespace.neuralnetworks.isolated = true
+namespace.neuralnetworks.visible = true
+
+namespace.neuralnetworks.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.asan.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.links = default
+namespace.neuralnetworks.link.default.shared_libs = libc.so
+namespace.neuralnetworks.link.default.shared_libs += libcgrouprc.so
+namespace.neuralnetworks.link.default.shared_libs += libdl.so
+namespace.neuralnetworks.link.default.shared_libs += liblog.so
+namespace.neuralnetworks.link.default.shared_libs += libm.so
+namespace.neuralnetworks.link.default.shared_libs += libnativewindow.so
+namespace.neuralnetworks.link.default.shared_libs += libsync.so
+namespace.neuralnetworks.link.default.shared_libs += libvndksupport.so
+
+
+###############################################################################
# Namespace config for binaries under /postinstall.
# Only one default namespace is defined and it has no directories other than
# /system/lib and /product/lib in the search paths. This is because linker
diff --git a/rootdir/etc/ld.config.txt b/rootdir/etc/ld.config.txt
index a6ea2ab..3321425 100644
--- a/rootdir/etc/ld.config.txt
+++ b/rootdir/etc/ld.config.txt
@@ -35,7 +35,7 @@
dir.system = /data
[system]
-additional.namespaces = runtime,conscrypt,media,resolv,sphal,vndk,rs
+additional.namespaces = runtime,conscrypt,media,neuralnetworks,resolv,sphal,vndk,rs
###############################################################################
# "default" namespace
@@ -132,7 +132,7 @@
# shared library cannot be loaded from the runtime namespace either, the
# dynamic linker tries to load the shared library from the resolv namespace.
# Finally, if all attempts fail, the dynamic linker returns an error.
-namespace.default.links = runtime,resolv
+namespace.default.links = runtime,resolv,neuralnetworks
namespace.default.link.runtime.shared_libs = libandroidicu.so
namespace.default.link.runtime.shared_libs += libdexfile_external.so
namespace.default.link.runtime.shared_libs += libdexfiled_external.so
@@ -152,6 +152,9 @@
# to be loaded in the default namespace.
namespace.default.link.resolv.shared_libs = libnetd_resolv.so
+# LLNDK library moved into apex
+namespace.default.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "runtime" APEX namespace
#
@@ -185,13 +188,16 @@
namespace.media.permitted.paths = /apex/com.android.media/${LIB}/extractors
-namespace.media.links = default
+namespace.media.links = default,neuralnetworks
namespace.media.link.default.shared_libs = %LLNDK_LIBRARIES%
namespace.media.link.default.shared_libs += libbinder_ndk.so
namespace.media.link.default.shared_libs += libcgrouprc.so
namespace.media.link.default.shared_libs += libmediametrics.so
namespace.media.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
+# LLNDK library moved into apex
+namespace.media.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "conscrypt" APEX namespace
#
@@ -269,7 +275,7 @@
# libs listed here can be used. Order is important here as the namespaces are
# tried in this order. rs should be before vndk because both are capable
# of loading libRS_internal.so
-namespace.sphal.links = rs,default,vndk
+namespace.sphal.links = rs,default,vndk,neuralnetworks
# Renderscript gets separate namespace
namespace.sphal.link.rs.shared_libs = libRS_internal.so
@@ -279,6 +285,10 @@
namespace.sphal.link.vndk.shared_libs = %VNDK_SAMEPROCESS_LIBRARIES%
+# LLNDK library moved into apex
+namespace.sphal.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
+
###############################################################################
# "rs" namespace
#
@@ -318,7 +328,7 @@
namespace.rs.asan.permitted.paths += /vendor/${LIB}
namespace.rs.asan.permitted.paths += /data
-namespace.rs.links = default,vndk
+namespace.rs.links = default,vndk,neuralnetworks
namespace.rs.link.default.shared_libs = %LLNDK_LIBRARIES%
namespace.rs.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
@@ -328,6 +338,10 @@
namespace.rs.link.vndk.shared_libs = %VNDK_SAMEPROCESS_LIBRARIES%
+# LLNDK library moved into apex
+namespace.rs.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
+
###############################################################################
# "vndk" namespace
#
@@ -373,7 +387,7 @@
# 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,runtime
+namespace.vndk.links = default,sphal,runtime,neuralnetworks
# 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
@@ -386,6 +400,28 @@
# Allow VNDK-SP extensions to use vendor libraries
namespace.vndk.link.sphal.allow_all_shared_libs = true
+# LLNDK library moved into apex
+namespace.vndk.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
+###############################################################################
+# "neuralnetworks" APEX namespace
+#
+# This namespace is for libraries within the NNAPI APEX.
+###############################################################################
+namespace.neuralnetworks.isolated = true
+namespace.neuralnetworks.visible = true
+
+namespace.neuralnetworks.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.asan.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.links = default
+namespace.neuralnetworks.link.default.shared_libs = libc.so
+namespace.neuralnetworks.link.default.shared_libs += libcgrouprc.so
+namespace.neuralnetworks.link.default.shared_libs += libdl.so
+namespace.neuralnetworks.link.default.shared_libs += liblog.so
+namespace.neuralnetworks.link.default.shared_libs += libm.so
+namespace.neuralnetworks.link.default.shared_libs += libnativewindow.so
+namespace.neuralnetworks.link.default.shared_libs += libsync.so
+namespace.neuralnetworks.link.default.shared_libs += libvndksupport.so
###############################################################################
# Namespace config for vendor processes. In O, no restriction is enforced for
@@ -394,7 +430,7 @@
# (LL-NDK only) access.
###############################################################################
[vendor]
-additional.namespaces = runtime,system,vndk%VNDK_IN_SYSTEM_NS%
+additional.namespaces = runtime,system,neuralnetworks,vndk%VNDK_IN_SYSTEM_NS%
###############################################################################
# "default" namespace
@@ -435,7 +471,7 @@
namespace.default.asan.permitted.paths += /data/asan/vendor
namespace.default.asan.permitted.paths += /vendor
-namespace.default.links = system,vndk%VNDK_IN_SYSTEM_NS%,runtime
+namespace.default.links = system,vndk%VNDK_IN_SYSTEM_NS%,runtime,neuralnetworks
namespace.default.link.runtime.shared_libs = %SANITIZER_RUNTIME_LIBRARIES%
namespace.default.link.system.shared_libs = %LLNDK_LIBRARIES%
namespace.default.link.system.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
@@ -443,6 +479,9 @@
namespace.default.link.vndk.shared_libs = %VNDK_SAMEPROCESS_LIBRARIES%
namespace.default.link.vndk.shared_libs += %VNDK_CORE_LIBRARIES%
+# LLNDK library moved into apex
+namespace.default.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "runtime" APEX namespace
#
@@ -458,6 +497,7 @@
# classloader-namespace for oat files, and tighten this up.
namespace.runtime.link.system.allow_all_shared_libs = true
+
###############################################################################
# "vndk" namespace
#
@@ -491,7 +531,7 @@
# Android releases. The links here should be identical to that of the
# 'vndk_in_system' namespace, except for the link between 'vndk' and
# 'vndk_in_system'.
-namespace.vndk.links = system,default%VNDK_IN_SYSTEM_NS%,runtime
+namespace.vndk.links = system,default%VNDK_IN_SYSTEM_NS%,runtime,neuralnetworks
namespace.vndk.link.system.shared_libs = %LLNDK_LIBRARIES%
namespace.vndk.link.system.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
@@ -502,6 +542,9 @@
namespace.vndk.link.vndk_in_system.shared_libs = %VNDK_USING_CORE_VARIANT_LIBRARIES%
+# LLNDK library moved into apex
+namespace.vndk.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "system" namespace
#
@@ -571,14 +614,34 @@
# 1. 'vndk_in_system' needs to be freely linked back to 'vndk'.
# 2. 'vndk_in_system' does not need to link to 'default', as any library that
# requires anything vendor would not be a vndk_in_system library.
-namespace.vndk_in_system.links = vndk,system,runtime
+namespace.vndk_in_system.links = vndk,system,runtime,neuralnetworks
namespace.vndk_in_system.link.runtime.shared_libs = %SANITIZER_RUNTIME_LIBRARIES%
namespace.vndk_in_system.link.system.shared_libs = %LLNDK_LIBRARIES%
namespace.vndk_in_system.link.system.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
namespace.vndk_in_system.link.vndk.allow_all_shared_libs = true
+namespace.vndk_in_system.link.neuralnetworks.shared_libs = libneuralnetworks.so
+###############################################################################
+# "neuralnetworks" APEX namespace
+#
+# This namespace is for libraries within the NNAPI APEX.
+###############################################################################
+namespace.neuralnetworks.isolated = true
+namespace.neuralnetworks.visible = true
+
+namespace.neuralnetworks.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.asan.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.links = default
+namespace.neuralnetworks.link.default.shared_libs = libc.so
+namespace.neuralnetworks.link.default.shared_libs += libcgrouprc.so
+namespace.neuralnetworks.link.default.shared_libs += libdl.so
+namespace.neuralnetworks.link.default.shared_libs += liblog.so
+namespace.neuralnetworks.link.default.shared_libs += libm.so
+namespace.neuralnetworks.link.default.shared_libs += libnativewindow.so
+namespace.neuralnetworks.link.default.shared_libs += libsync.so
+namespace.neuralnetworks.link.default.shared_libs += libvndksupport.so
###############################################################################
# Namespace config for native tests that need access to both system and vendor
@@ -587,7 +650,7 @@
# includes the requisite namespace setup for APEXes.
###############################################################################
[unrestricted]
-additional.namespaces = runtime,media,conscrypt,resolv
+additional.namespaces = runtime,media,conscrypt,resolv,neuralnetworks
# Visible to allow links to be created at runtime, e.g. through
# android_link_namespaces in libnativeloader.
@@ -605,7 +668,7 @@
namespace.default.asan.search.paths += /vendor/${LIB}
# Keep in sync with the "platform" namespace in art/build/apex/ld.config.txt.
-namespace.default.links = runtime,resolv
+namespace.default.links = runtime,resolv,neuralnetworks
namespace.default.link.runtime.shared_libs = libandroidicu.so
namespace.default.link.runtime.shared_libs += libdexfile_external.so
namespace.default.link.runtime.shared_libs += libdexfiled_external.so
@@ -621,6 +684,7 @@
namespace.default.link.runtime.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
namespace.default.link.resolv.shared_libs = libnetd_resolv.so
+namespace.default.link.neuralnetworks.shared_libs = libneuralnetworks.so
###############################################################################
# "runtime" APEX namespace
@@ -653,12 +717,16 @@
namespace.media.permitted.paths = /apex/com.android.media/${LIB}/extractors
-namespace.media.links = default
+namespace.media.links = default,neuralnetworks
namespace.media.link.default.shared_libs = %LLNDK_LIBRARIES%
namespace.media.link.default.shared_libs += libbinder_ndk.so
namespace.media.link.default.shared_libs += libmediametrics.so
namespace.media.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
+# LLNDK library moved into apex
+namespace.media.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
+
###############################################################################
# "conscrypt" APEX namespace
#
@@ -694,6 +762,25 @@
namespace.resolv.link.default.shared_libs += libbinder_ndk.so
namespace.resolv.link.default.shared_libs += liblog.so
+###############################################################################
+# "neuralnetworks" APEX namespace
+#
+# This namespace is for libraries within the NNAPI APEX.
+###############################################################################
+namespace.neuralnetworks.isolated = true
+namespace.neuralnetworks.visible = true
+
+namespace.neuralnetworks.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.asan.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.links = default
+namespace.neuralnetworks.link.default.shared_libs = libc.so
+namespace.neuralnetworks.link.default.shared_libs += libcgrouprc.so
+namespace.neuralnetworks.link.default.shared_libs += libdl.so
+namespace.neuralnetworks.link.default.shared_libs += liblog.so
+namespace.neuralnetworks.link.default.shared_libs += libm.so
+namespace.neuralnetworks.link.default.shared_libs += libnativewindow.so
+namespace.neuralnetworks.link.default.shared_libs += libsync.so
+namespace.neuralnetworks.link.default.shared_libs += libvndksupport.so
###############################################################################
# Namespace config for binaries under /postinstall.
diff --git a/rootdir/etc/ld.config.vndk_lite.txt b/rootdir/etc/ld.config.vndk_lite.txt
index 69535a9..0bb60ab 100644
--- a/rootdir/etc/ld.config.vndk_lite.txt
+++ b/rootdir/etc/ld.config.vndk_lite.txt
@@ -35,7 +35,7 @@
dir.system = /data
[system]
-additional.namespaces = runtime,conscrypt,media,resolv,sphal,vndk,rs
+additional.namespaces = runtime,conscrypt,media,neuralnetworks,resolv,sphal,vndk,rs
###############################################################################
# "default" namespace
@@ -72,7 +72,7 @@
# shared library cannot be loaded from the runtime namespace either, the
# dynamic linker tries to load the shared library from the resolv namespace.
# Finally, if all attempts fail, the dynamic linker returns an error.
-namespace.default.links = runtime,resolv
+namespace.default.links = runtime,resolv,neuralnetworks
namespace.default.link.runtime.shared_libs = libandroidicu.so
namespace.default.link.runtime.shared_libs += libdexfile_external.so
namespace.default.link.runtime.shared_libs += libdexfiled_external.so
@@ -91,6 +91,9 @@
# to be loaded in the default namespace.
namespace.default.link.resolv.shared_libs = libnetd_resolv.so
+# LLNDK library moved into apex
+namespace.default.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "runtime" APEX namespace
#
@@ -124,12 +127,15 @@
namespace.media.permitted.paths = /apex/com.android.media/${LIB}/extractors
-namespace.media.links = default
+namespace.media.links = default,neuralnetworks
namespace.media.link.default.shared_libs = %LLNDK_LIBRARIES%
namespace.media.link.default.shared_libs += libbinder_ndk.so
namespace.media.link.default.shared_libs += libmediametrics.so
namespace.media.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
+# LLNDK library moved into apex
+namespace.media.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "conscrypt" APEX namespace
#
@@ -207,7 +213,7 @@
# libs listed here can be used. Order is important here as the namespaces are
# tried in this order. rs should be before vndk because both are capable
# of loading libRS_internal.so
-namespace.sphal.links = rs,default,vndk
+namespace.sphal.links = rs,default,vndk,neuralnetworks
# Renderscript gets separate namespace
namespace.sphal.link.rs.shared_libs = libRS_internal.so
@@ -217,6 +223,9 @@
namespace.sphal.link.vndk.shared_libs = %VNDK_SAMEPROCESS_LIBRARIES%
+# LLNDK library moved into apex
+namespace.sphal.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "rs" namespace
#
@@ -256,7 +265,7 @@
namespace.rs.asan.permitted.paths += /vendor/${LIB}
namespace.rs.asan.permitted.paths += /data
-namespace.rs.links = default,vndk
+namespace.rs.links = default,vndk,neuralnetworks
namespace.rs.link.default.shared_libs = %LLNDK_LIBRARIES%
namespace.rs.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
@@ -266,6 +275,9 @@
namespace.rs.link.vndk.shared_libs = %VNDK_SAMEPROCESS_LIBRARIES%
+# LLNDK library moved into apex
+namespace.rs.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "vndk" namespace
#
@@ -310,10 +322,31 @@
# When these NDK libs are required inside this namespace, then it is redirected
# to the default namespace. This is possible since their ABI is stable across
# Android releases.
-namespace.vndk.links = default
+namespace.vndk.links = default,neuralnetworks
namespace.vndk.link.default.shared_libs = %LLNDK_LIBRARIES%
namespace.vndk.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
+namespace.vndk.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
+###############################################################################
+# "neuralnetworks" APEX namespace
+#
+# This namespace is for libraries within the NNAPI APEX.
+###############################################################################
+namespace.neuralnetworks.isolated = true
+namespace.neuralnetworks.visible = true
+
+namespace.neuralnetworks.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.asan.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.links = default
+namespace.neuralnetworks.link.default.shared_libs = libc.so
+namespace.neuralnetworks.link.default.shared_libs += libcgrouprc.so
+namespace.neuralnetworks.link.default.shared_libs += libdl.so
+namespace.neuralnetworks.link.default.shared_libs += liblog.so
+namespace.neuralnetworks.link.default.shared_libs += libm.so
+namespace.neuralnetworks.link.default.shared_libs += libnativewindow.so
+namespace.neuralnetworks.link.default.shared_libs += libsync.so
+namespace.neuralnetworks.link.default.shared_libs += libvndksupport.so
###############################################################################
@@ -323,7 +356,7 @@
# (LL-NDK only) access.
###############################################################################
[vendor]
-additional.namespaces = runtime
+additional.namespaces = runtime,neuralnetworks
namespace.default.isolated = false
@@ -365,7 +398,7 @@
namespace.default.asan.search.paths += /data/asan/system/${LIB}/vndk%VNDK_VER%
namespace.default.asan.search.paths += /system/${LIB}/vndk%VNDK_VER%
-namespace.default.links = runtime
+namespace.default.links = runtime,neuralnetworks
namespace.default.link.runtime.shared_libs = libdexfile_external.so
namespace.default.link.runtime.shared_libs += libdexfiled_external.so
# TODO(b/120786417 or b/134659294): libicuuc.so and libicui18n.so are kept for app compat.
@@ -377,6 +410,9 @@
# Workaround for b/124772622
namespace.default.link.runtime.shared_libs += libandroidicu.so
+# LLNDK library moved into apex
+namespace.default.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "runtime" APEX namespace
#
@@ -393,13 +429,34 @@
namespace.runtime.link.default.allow_all_shared_libs = true
###############################################################################
+# "neuralnetworks" APEX namespace
+#
+# This namespace is for libraries within the NNAPI APEX.
+###############################################################################
+namespace.neuralnetworks.isolated = true
+namespace.neuralnetworks.visible = true
+
+namespace.neuralnetworks.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.asan.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.links = default
+namespace.neuralnetworks.link.default.shared_libs = libc.so
+namespace.neuralnetworks.link.default.shared_libs += libcgrouprc.so
+namespace.neuralnetworks.link.default.shared_libs += libdl.so
+namespace.neuralnetworks.link.default.shared_libs += liblog.so
+namespace.neuralnetworks.link.default.shared_libs += libm.so
+namespace.neuralnetworks.link.default.shared_libs += libnativewindow.so
+namespace.neuralnetworks.link.default.shared_libs += libsync.so
+namespace.neuralnetworks.link.default.shared_libs += libvndksupport.so
+
+
+###############################################################################
# Namespace config for native tests that need access to both system and vendor
# libraries. This replicates the default linker config (done by
# init_default_namespace_no_config in bionic/linker/linker.cpp), except that it
# includes the requisite namespace setup for APEXes.
###############################################################################
[unrestricted]
-additional.namespaces = runtime,media,conscrypt,resolv
+additional.namespaces = runtime,media,conscrypt,resolv,neuralnetworks
# Visible to allow links to be created at runtime, e.g. through
# android_link_namespaces in libnativeloader.
@@ -417,7 +474,7 @@
namespace.default.asan.search.paths += /vendor/${LIB}
# Keep in sync with the "platform" namespace in art/build/apex/ld.config.txt.
-namespace.default.links = runtime,resolv
+namespace.default.links = runtime,resolv,neuralnetworks
namespace.default.link.runtime.shared_libs = libandroidicu.so
namespace.default.link.runtime.shared_libs += libdexfile_external.so
namespace.default.link.runtime.shared_libs += libdexfiled_external.so
@@ -434,6 +491,9 @@
namespace.default.link.resolv.shared_libs = libnetd_resolv.so
+# LLNDK library moved into apex
+namespace.default.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "runtime" APEX namespace
#
@@ -450,7 +510,6 @@
namespace.runtime.links = default
# TODO(b/130340935): Use a dynamically created linker namespace similar to
# classloader-namespace for oat files, and tighten this up.
-namespace.runtime.link.default.allow_all_shared_libs = true
###############################################################################
# "media" APEX namespace
@@ -465,12 +524,15 @@
namespace.media.permitted.paths = /apex/com.android.media/${LIB}/extractors
-namespace.media.links = default
+namespace.media.links = default,neuralnetworks
namespace.media.link.default.shared_libs = %LLNDK_LIBRARIES%
namespace.media.link.default.shared_libs += libbinder_ndk.so
namespace.media.link.default.shared_libs += libmediametrics.so
namespace.media.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
+# LLNDK library moved into apex
+namespace.media.link.neuralnetworks.shared_libs = libneuralnetworks.so
+
###############################################################################
# "conscrypt" APEX namespace
#
@@ -506,6 +568,27 @@
namespace.resolv.link.default.shared_libs += libbinder_ndk.so
###############################################################################
+# "neuralnetworks" APEX namespace
+#
+# This namespace is for libraries within the NNAPI APEX.
+###############################################################################
+namespace.neuralnetworks.isolated = true
+namespace.neuralnetworks.visible = true
+
+namespace.neuralnetworks.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.asan.search.paths = /apex/com.android.neuralnetworks/${LIB}
+namespace.neuralnetworks.links = default
+namespace.neuralnetworks.link.default.shared_libs = libc.so
+namespace.neuralnetworks.link.default.shared_libs += libcgrouprc.so
+namespace.neuralnetworks.link.default.shared_libs += libdl.so
+namespace.neuralnetworks.link.default.shared_libs += liblog.so
+namespace.neuralnetworks.link.default.shared_libs += libm.so
+namespace.neuralnetworks.link.default.shared_libs += libnativewindow.so
+namespace.neuralnetworks.link.default.shared_libs += libsync.so
+namespace.neuralnetworks.link.default.shared_libs += libvndksupport.so
+
+
+###############################################################################
# Namespace config for binaries under /postinstall.
# Only default namespace is defined and default has no directories
# other than /system/lib in the search paths. This is because linker calls
diff --git a/rootdir/init.rc b/rootdir/init.rc
index a5bfc41..d22e9a7 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -295,6 +295,11 @@
chown system system /sys/power/wakeup_count
chmod 0660 /sys/power/state
+ chown radio wakelock /sys/power/wake_lock
+ chown radio wakelock /sys/power/wake_unlock
+ chmod 0660 /sys/power/wake_lock
+ chmod 0660 /sys/power/wake_unlock
+
# Start logd before any other services run to ensure we capture all of their logs.
start logd
@@ -409,6 +414,7 @@
mkdir /metadata/vold
chmod 0700 /metadata/vold
mkdir /metadata/password_slots 0771 root system
+ mkdir /metadata/ota 0700 root system
mkdir /metadata/apex 0700 root system
mkdir /metadata/apex/sessions 0700 root system
@@ -506,7 +512,6 @@
mkdir /data/misc/ethernet 0770 system system
mkdir /data/misc/dhcp 0770 dhcp dhcp
mkdir /data/misc/user 0771 root root
- mkdir /data/misc/perfprofd 0775 root root
# give system access to wpa_supplicant.conf for backup and restore
chmod 0660 /data/misc/wifi/wpa_supplicant.conf
mkdir /data/local 0751 root root
@@ -689,10 +694,6 @@
chown radio system /sys/android_power/acquire_partial_wake_lock
chown radio system /sys/android_power/release_wake_lock
chown system system /sys/power/autosleep
- chown radio wakelock /sys/power/wake_lock
- chown radio wakelock /sys/power/wake_unlock
- chmod 0660 /sys/power/wake_lock
- chmod 0660 /sys/power/wake_unlock
chown system system /sys/devices/system/cpu/cpufreq/interactive/timer_rate
chmod 0660 /sys/devices/system/cpu/cpufreq/interactive/timer_rate
diff --git a/rootdir/update_and_install_ld_config.mk b/rootdir/update_and_install_ld_config.mk
index c949a4f..dbe60e5 100644
--- a/rootdir/update_and_install_ld_config.mk
+++ b/rootdir/update_and_install_ld_config.mk
@@ -40,6 +40,7 @@
vndksp_libraries_file := $(library_lists_dir)/vndksp.libraries.$(vndk_version).txt
vndkcore_libraries_file := $(library_lists_dir)/vndkcore.libraries.txt
vndkprivate_libraries_file := $(library_lists_dir)/vndkprivate.libraries.txt
+llndk_moved_to_apex_libraries_file := $(library_lists_dir)/llndkinapex.libraries.txt
ifeq ($(my_vndk_use_core_variant),true)
vndk_using_core_variant_libraries_file := $(library_lists_dir)/vndk_using_core_variant.libraries.$(vndk_version).txt
endif
@@ -65,6 +66,10 @@
vndksp_libraries_list := $(VNDK_SAMEPROCESS_LIBRARIES)
endif
+# LLNDK libraries that has been moved to an apex package and no longer are present on
+# /system image.
+llndk_libraries_moved_to_apex_list:=$(LLNDK_MOVED_TO_APEX_LIBRARIES)
+
# $(1): list of libraries
# $(2): output file to write the list of libraries to
define write-libs-to-file
@@ -88,9 +93,17 @@
# $(2): output file with the filtered list of lib names
$(LOCAL_BUILT_MODULE): private-filter-out-private-libs = \
paste -sd ":" $(1) > $(2) && \
- cat $(PRIVATE_VNDK_PRIVATE_LIBRARIES_FILE) | xargs -n 1 -I privatelib bash -c "sed -i.bak 's/privatelib//' $(2)" && \
+ while read -r privatelib; do sed -i.bak "s/$$privatelib//" $(2) ; done < $(PRIVATE_VNDK_PRIVATE_LIBRARIES_FILE) && \
sed -i.bak -e 's/::\+/:/g ; s/^:\+// ; s/:\+$$//' $(2) && \
rm -f $(2).bak
+
+# # Given a file with a list of libs in "a:b:c" format, filter-out the LLNDK libraries migrated into apex file
+# # and write resulting list to a new file in "a:b:c" format
+ $(LOCAL_BUILT_MODULE): private-filter-out-llndk-in-apex-libs = \
+ for lib in $(PRIVATE_LLNDK_LIBRARIES_MOVED_TO_APEX_LIST); do sed -i.bak s/$$lib.so// $(1); done && \
+ sed -i.bak -e 's/::\+/:/g ; s/^:\+// ; s/:\+$$//' $(1) && \
+ rm -f $(1).bak
+
$(LOCAL_BUILT_MODULE): PRIVATE_LLNDK_LIBRARIES_FILE := $(llndk_libraries_file)
$(LOCAL_BUILT_MODULE): PRIVATE_VNDK_SP_LIBRARIES_FILE := $(vndksp_libraries_file)
$(LOCAL_BUILT_MODULE): PRIVATE_VNDK_CORE_LIBRARIES_FILE := $(vndkcore_libraries_file)
@@ -100,6 +113,7 @@
$(LOCAL_BUILT_MODULE): PRIVATE_INTERMEDIATES_DIR := $(intermediates_dir)
$(LOCAL_BUILT_MODULE): PRIVATE_COMP_CHECK_SCRIPT := $(compatibility_check_script)
$(LOCAL_BUILT_MODULE): PRIVATE_VNDK_VERSION_TAG := \#VNDK$(vndk_version)\#
+$(LOCAL_BUILT_MODULE): PRIVATE_LLNDK_LIBRARIES_MOVED_TO_APEX_LIST := $(llndk_libraries_moved_to_apex_list)
deps := $(llndk_libraries_file) $(vndksp_libraries_file) $(vndkcore_libraries_file) \
$(vndkprivate_libraries_file)
ifeq ($(check_backward_compatibility),true)
@@ -118,6 +132,7 @@
endif
@mkdir -p $(dir $@)
$(call private-filter-out-private-libs,$(PRIVATE_LLNDK_LIBRARIES_FILE),$(PRIVATE_INTERMEDIATES_DIR)/llndk_filtered)
+ $(call private-filter-out-llndk-in-apex-libs,$(PRIVATE_INTERMEDIATES_DIR)/llndk_filtered)
$(hide) sed -e "s?%LLNDK_LIBRARIES%?$$(cat $(PRIVATE_INTERMEDIATES_DIR)/llndk_filtered)?g" $< >$@
$(call private-filter-out-private-libs,$(PRIVATE_VNDK_SP_LIBRARIES_FILE),$(PRIVATE_INTERMEDIATES_DIR)/vndksp_filtered)
$(hide) sed -i.bak -e "s?%VNDK_SAMEPROCESS_LIBRARIES%?$$(cat $(PRIVATE_INTERMEDIATES_DIR)/vndksp_filtered)?g" $@
@@ -139,8 +154,9 @@
endif
$(hide) echo -n > $(PRIVATE_INTERMEDIATES_DIR)/private_llndk && \
- cat $(PRIVATE_VNDK_PRIVATE_LIBRARIES_FILE) | \
- xargs -n 1 -I privatelib bash -c "(grep privatelib $(PRIVATE_LLNDK_LIBRARIES_FILE) || true) >> $(PRIVATE_INTERMEDIATES_DIR)/private_llndk" && \
+ while read -r privatelib; \
+ do (grep $$privatelib $(PRIVATE_LLNDK_LIBRARIES_FILE) || true) >> $(PRIVATE_INTERMEDIATES_DIR)/private_llndk ; \
+ done < $(PRIVATE_VNDK_PRIVATE_LIBRARIES_FILE) && \
paste -sd ":" $(PRIVATE_INTERMEDIATES_DIR)/private_llndk | \
sed -i.bak -e "s?%PRIVATE_LLNDK_LIBRARIES%?$$(cat -)?g" $@
@@ -161,6 +177,7 @@
intermediates_dir :=
library_lists_dir :=
llndk_libraries_file :=
+llndk_moved_to_apex_libraries_file :=
vndksp_libraries_file :=
vndkcore_libraries_file :=
vndkprivate_libraries_file :=
diff --git a/shell_and_utilities/Android.bp b/shell_and_utilities/Android.bp
index bac3dc3..694b50e 100644
--- a/shell_and_utilities/Android.bp
+++ b/shell_and_utilities/Android.bp
@@ -13,6 +13,7 @@
"auditctl",
"awk",
"bzip2",
+ "ldd",
"logwrapper",
"mini-keyctl",
"mkshrc",
diff --git a/storaged/storaged.cpp b/storaged/storaged.cpp
index 6897663..1d934a2 100644
--- a/storaged/storaged.cpp
+++ b/storaged/storaged.cpp
@@ -164,8 +164,10 @@
}
void storaged_t::add_user_ce(userid_t user_id) {
- load_proto(user_id);
- proto_loaded[user_id] = true;
+ if (!proto_loaded[user_id]) {
+ load_proto(user_id);
+ proto_loaded[user_id] = true;
+ }
}
void storaged_t::remove_user_ce(userid_t user_id) {