Snap for 8426163 from 8e8f1e9c82c946997959642073317784627919a5 to mainline-tzdata2-release

Change-Id: I2f2c16f96ec73b0e0ed7bb47d5678ab01e9f3eca
diff --git a/Android.bp b/Android.bp
index c728f22..e339a4f 100644
--- a/Android.bp
+++ b/Android.bp
@@ -12,10 +12,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package {
-    default_applicable_licenses: ["Android-Apache-2.0"],
-}
-
 cc_library_static {
     name: "libgtest_isolated",
     host_supported: true,
@@ -31,7 +27,11 @@
         "Test.cpp",
     ],
 
+    // NOTE: libbase is re-exported by including them below.
+    // When Soong supports transitive static dependency includes, this
+    // library can be removed.
     whole_static_libs: [
+        "libbase",
         "libgtest",
     ],
 
diff --git a/Isolate.cpp b/Isolate.cpp
index 8f76f54..889b183 100644
--- a/Isolate.cpp
+++ b/Isolate.cpp
@@ -20,21 +20,21 @@
 #include <poll.h>
 #include <signal.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
 #include <atomic>
-#include <memory>
 #include <string>
 #include <tuple>
 #include <vector>
 
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
 #include <gtest/gtest.h>
 
 #include "Color.h"
 #include "Isolate.h"
-#include "Log.h"
 #include "NanoTime.h"
 #include "Test.h"
 
@@ -50,22 +50,22 @@
 static void RegisterSignalHandler() {
   auto ret = signal(SIGINT, SignalHandler);
   if (ret == SIG_ERR) {
-    FATAL_PLOG("Setting up SIGINT handler failed");
+    PLOG(FATAL) << "Setting up SIGINT handler failed";
   }
   ret = signal(SIGQUIT, SignalHandler);
   if (ret == SIG_ERR) {
-    FATAL_PLOG("Setting up SIGQUIT handler failed");
+    PLOG(FATAL) << "Setting up SIGQUIT handler failed";
   }
 }
 
 static void UnregisterSignalHandler() {
   auto ret = signal(SIGINT, SIG_DFL);
   if (ret == SIG_ERR) {
-    FATAL_PLOG("Disabling SIGINT handler failed");
+    PLOG(FATAL) << "Disabling SIGINT handler failed";
   }
   ret = signal(SIGQUIT, SIG_DFL);
   if (ret == SIG_ERR) {
-    FATAL_PLOG("Disabling SIGQUIT handler failed");
+    PLOG(FATAL) << "Disabling SIGQUIT handler failed";
   }
 }
 
@@ -81,12 +81,6 @@
   return string;
 }
 
-inline static bool StartsWithDisabled(const std::string& str) {
-  static constexpr char kDisabledStr[] = "DISABLED_";
-  static constexpr size_t kDisabledStrLen = sizeof(kDisabledStr) - 1;
-  return str.compare(0, kDisabledStrLen, kDisabledStr) == 0;
-}
-
 void Isolate::EnumerateTests() {
   // Only apply --gtest_filter if present. This is the only option that changes
   // what tests are listed.
@@ -95,14 +89,13 @@
     command += " --gtest_filter=" + options_.filter();
   }
   command += " --gtest_list_tests";
-#if defined(__BIONIC__)
-  // Only bionic is guaranteed to support the 'e' option.
-  FILE* fp = popen(command.c_str(), "re");
-#else
+#if defined(__APPLE__)
   FILE* fp = popen(command.c_str(), "r");
+#else
+  FILE* fp = popen(command.c_str(), "re");
 #endif
   if (fp == nullptr) {
-    FATAL_PLOG("Unexpected failure from popen");
+    PLOG(FATAL) << "Unexpected failure from popen";
   }
 
   size_t total_shards = options_.total_shards();
@@ -129,7 +122,7 @@
         suite_name.resize(suite_name.size() - 1);
       }
 
-      if (!options_.allow_disabled_tests() && StartsWithDisabled(suite_name)) {
+      if (!options_.allow_disabled_tests() && android::base::StartsWith(suite_name, "DISABLED_")) {
         // This whole set of tests have been disabled, skip them all.
         skip_until_next_suite = true;
       } else {
@@ -146,7 +139,7 @@
         if (test_name.back() == '\n') {
           test_name.resize(test_name.size() - 1);
         }
-        if (options_.allow_disabled_tests() || !StartsWithDisabled(test_name)) {
+        if (options_.allow_disabled_tests() || !android::base::StartsWith(test_name, "DISABLED_")) {
           if (!sharded || --test_count == 0) {
             tests_.push_back(std::make_tuple(suite_name, test_name));
             total_tests_++;
@@ -174,7 +167,7 @@
   }
   free(buffer);
   if (pclose(fp) == -1) {
-    FATAL_PLOG("Unexpected failure from pclose");
+    PLOG(FATAL) << "Unexpected failure from pclose";
   }
 }
 
@@ -185,7 +178,7 @@
   // Add the filter argument.
   std::vector<char*> args(child_args_);
   std::string filter("--gtest_filter=" + GetTestName(test));
-  args.push_back(filter.data());
+  args.push_back(strdup(filter.c_str()));
 
   int argc = args.size();
   // Add the null terminator.
@@ -194,45 +187,22 @@
   return RUN_ALL_TESTS();
 }
 
-static bool Pipe(int* read_fd, int* write_fd) {
-  int pipefd[2];
-
-#if defined(__linux__)
-  if (pipe2(pipefd, O_CLOEXEC) != 0) {
-    return false;
-  }
-#else  // defined(__APPLE__)
-  if (pipe(pipefd) != 0) {
-    return false;
-  }
-  if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) != 0 || fcntl(pipefd[1], F_SETFD, FD_CLOEXEC)) {
-    close(pipefd[0]);
-    close(pipefd[1]);
-    return false;
-  }
-#endif
-
-  *read_fd = pipefd[0];
-  *write_fd = pipefd[1];
-  return true;
-}
-
 void Isolate::LaunchTests() {
   while (!running_indices_.empty() && cur_test_index_ < tests_.size()) {
-    int read_fd, write_fd;
+    android::base::unique_fd read_fd, write_fd;
     if (!Pipe(&read_fd, &write_fd)) {
-      FATAL_PLOG("Unexpected failure from pipe");
+      PLOG(FATAL) << "Unexpected failure from pipe";
     }
-    if (fcntl(read_fd, F_SETFL, O_NONBLOCK) == -1) {
-      FATAL_PLOG("Unexpected failure from fcntl");
+    if (fcntl(read_fd.get(), F_SETFL, O_NONBLOCK) == -1) {
+      PLOG(FATAL) << "Unexpected failure from fcntl";
     }
 
     pid_t pid = fork();
     if (pid == -1) {
-      FATAL_PLOG("Unexpected failure from fork");
+      PLOG(FATAL) << "Unexpected failure from fork";
     }
     if (pid == 0) {
-      close(read_fd);
+      read_fd.reset();
       close(STDOUT_FILENO);
       close(STDERR_FILENO);
       if (dup2(write_fd, STDOUT_FILENO) == -1) {
@@ -241,14 +211,13 @@
       if (dup2(write_fd, STDERR_FILENO) == -1) {
         exit(1);
       }
-      close(write_fd);
       UnregisterSignalHandler();
       exit(ChildProcessFn(tests_[cur_test_index_]));
     }
 
     size_t run_index = running_indices_.back();
     running_indices_.pop_back();
-    Test* test = new Test(tests_[cur_test_index_], cur_test_index_, run_index, read_fd);
+    Test* test = new Test(tests_[cur_test_index_], cur_test_index_, run_index, read_fd.release());
     running_by_pid_.emplace(pid, test);
     running_[run_index] = test;
     running_by_test_index_[cur_test_index_] = test;
@@ -257,7 +226,6 @@
     pollfd->fd = test->fd();
     pollfd->events = POLLIN;
     cur_test_index_++;
-    close(write_fd);
   }
 }
 
@@ -286,12 +254,9 @@
   int status;
   pid_t pid;
   while ((pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG))) > 0) {
-    if (pid == -1) {
-      FATAL_PLOG("Unexpected failure from waitpid");
-    }
     auto entry = running_by_pid_.find(pid);
     if (entry == running_by_pid_.end()) {
-      FATAL_LOG("Found process not spawned by the isolation framework");
+      LOG(FATAL) << "Pid " << pid << " was not spawned by the isolation framework.";
     }
 
     std::unique_ptr<Test>& test_ptr = entry->second;
@@ -361,7 +326,7 @@
         total_skipped_tests_++;
         break;
       case TEST_NONE:
-        FATAL_LOG("Test result is TEST_NONE, this should not be possible");
+        LOG(FATAL) << "Test result is TEST_NONE, this should not be possible.";
     }
     finished_tests++;
     size_t test_index = test->test_index();
@@ -383,7 +348,7 @@
   // The only valid error case is if ECHILD is returned because there are
   // no more processes left running.
   if (pid == -1 && errno != ECHILD) {
-    FATAL_PLOG("Unexpected failure from waitpid");
+    PLOG(FATAL) << "Unexpected failure from waitpid";
   }
   return finished_tests;
 }
@@ -653,10 +618,7 @@
   }
 
   if (result.type() == ::testing::TestPartResult::kSkip) {
-    printf("%s:(%d) Skipped\n", result.file_name(), result.line_number());
-    if (*result.message()) {
-      printf("%s\n", result.message());
-    }
+    printf("%s:(%d) Skipped%s\n", result.file_name(), result.line_number(), result.message());
   } else {
     // Print failure message from the assertion (e.g. expected this and got that).
     printf("%s:(%d) Failure in test %s.%s\n%s\n", result.file_name(), result.line_number(),
@@ -678,7 +640,7 @@
 
   const tm* time_struct = localtime(&start_time);
   if (time_struct == nullptr) {
-    FATAL_PLOG("Unexpected failure from localtime");
+    PLOG(FATAL) << "Unexpected failure from localtime";
   }
   char timestamp[40];
   snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d",
@@ -777,7 +739,7 @@
   EnumerateTests();
 
   // Stop default result printer to avoid environment setup/teardown information for each test.
-  delete ::testing::UnitTest::GetInstance()->listeners().Release(
+  ::testing::UnitTest::GetInstance()->listeners().Release(
       ::testing::UnitTest::GetInstance()->listeners().default_result_printer());
   ::testing::UnitTest::GetInstance()->listeners().Append(new TestResultPrinter);
   RegisterSignalHandler();
diff --git a/IsolateMain.cpp b/IsolateMain.cpp
index 5b34ef5..30b871b 100644
--- a/IsolateMain.cpp
+++ b/IsolateMain.cpp
@@ -15,16 +15,14 @@
  */
 
 #include <errno.h>
-#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
-#include <cstring>
-#include <string_view>
 #include <vector>
 
+#include <android-base/file.h>
 #include <gtest/gtest.h>
 #include <gtest_extras/IsolateMain.h>
 
@@ -46,15 +44,7 @@
       "      Use isolation mode, Run each test in a separate process.\n"
       "      If JOB_COUNT is not given, it is set to the count of available processors.\n");
   ColoredPrintf(COLOR_GREEN, "  --no_isolate\n");
-  printf(
-      "      Don't use isolation mode, run all tests in a single process.\n"
-      "      If the test seems to be running in a debugger (based on the parent's name) this will\n"
-      "      be automatically set. If this behavior is not desired use the '--force_isolate' flag\n"
-      "      below.\n");
-  ColoredPrintf(COLOR_GREEN, "  --force_isolate\n");
-  printf(
-      "      Force the use of isolation mode, even if it looks like we are running in a\n"
-      "      debugger.\n");
+  printf("      Don't use isolation mode, run all tests in a single process.\n");
   ColoredPrintf(COLOR_GREEN, "  --deadline_threshold_ms=");
   ColoredPrintf(COLOR_YELLOW, "[TIME_IN_MS]\n");
   printf("      Run each test in no longer than ");
@@ -69,8 +59,10 @@
   printf(
       " will be called slow.\n"
       "      Only valid in isolation mode. Default slow threshold is 2000 ms.\n");
+  ColoredPrintf(COLOR_GREEN, "-j");
   printf(
-      "\nIn isolation mode, you can send SIGQUIT to the parent process to show the\n"
+      ".\n"
+      "In isolation mode, you can send SIGQUIT to the parent process to show the\n"
       "current running tests, or send SIGINT to the parent process to stop all\n"
       "running tests.\n"
       "\n");
@@ -85,37 +77,13 @@
 
 static bool RunInIsolationMode(std::vector<const char*>& args) {
   // Parse arguments that can't be used in isolation mode.
-  bool isolation_forced = false;
   for (size_t i = 1; i < args.size(); ++i) {
     if (strcmp(args[i], "--no_isolate") == 0) {
       return false;
-    } else if (strcmp(args[i], "--force_isolate") == 0) {
-      // We want to make sure we prioritize --no_isolate and --gtest_list_tests.
-      isolation_forced = true;
     } else if (strcmp(args[i], "--gtest_list_tests") == 0) {
       return false;
     }
   }
-  if (!isolation_forced) {
-    // Check if we are running gdb/gdbserver/lldb/lldb-server. No need to be sneaky we are assuming
-    // no one is hiding.
-    pid_t ppid = getppid();
-    std::string exe_path = std::string("/proc/") + std::to_string(ppid) + "/exe";
-    char buf[PATH_MAX + 1];
-    size_t len;
-    // NB We can't use things like android::base::* or std::filesystem::* due to linking
-    // issues.
-    // Since PATH_MAX is the longest a symlink can be in posix we don't need to
-    // deal with truncation. Anyway this isn't critical for correctness and is
-    // just a QOL thing so it's fine if we are wrong.
-    if ((len = TEMP_FAILURE_RETRY(readlink(exe_path.c_str(), buf, sizeof(buf) - 1))) > 0) {
-      buf[len] = '\0';
-      std::string_view file(basename(buf));
-      return file != "gdb" && file != "gdbserver" && file != "gdbserver64" &&
-             file != "gdbserver32" && file != "lldb" && file != "lldb-server";
-    }
-    // If we can't figure out what our parent was just assume we are fine to isolate.
-  }
   return true;
 }
 
@@ -123,7 +91,9 @@
 }  // namespace android
 
 // Tests that override this weak function can add default arguments.
-extern "C" bool __attribute__((weak)) GetInitialArgs(const char***, size_t*);
+extern "C" bool __attribute__((weak)) GetInitialArgs(const char***, size_t*) {
+  return false;
+}
 
 int IsolateMain(int argc, char** argv, char**) {
   std::vector<const char*> args{argv[0]};
@@ -156,7 +126,7 @@
 
   const char** start_args;
   size_t num_args;
-  if (GetInitialArgs != nullptr && GetInitialArgs(&start_args, &num_args)) {
+  if (GetInitialArgs(&start_args, &num_args)) {
     std::vector<const char*> initial_args;
     for (size_t i = 0; i < num_args; i++) {
       initial_args.push_back(start_args[i]);
@@ -179,9 +149,5 @@
   ::testing::GTEST_FLAG(print_time) = options.print_time();
 
   android::gtest_extras::Isolate isolate(options, child_args);
-  int return_val = isolate.Run();
-  for (auto child_arg : child_args) {
-    free(child_arg);
-  }
-  return return_val;
+  return isolate.Run();
 }
diff --git a/Log.h b/Log.h
deleted file mode 100644
index d424c51..0000000
--- a/Log.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2020 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 <errno.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <android/log.h>
-
-#define GTEST_EXTRAS_TAG "gtest_extras"
-
-#define FATAL_PLOG(msg)                                                                    \
-  __android_log_print(ANDROID_LOG_FATAL, GTEST_EXTRAS_TAG, "%s:%d] " msg ": %s", __FILE__, \
-                      __LINE__, strerror(errno));                                          \
-  abort();
-
-#define FATAL_LOG(msg)                                                                         \
-  __android_log_print(ANDROID_LOG_FATAL, GTEST_EXTRAS_TAG, "%s:%d] " msg, __FILE__, __LINE__); \
-  abort();
diff --git a/Options.cpp b/Options.cpp
index 96842a1..bcc4b06 100644
--- a/Options.cpp
+++ b/Options.cpp
@@ -15,22 +15,20 @@
  */
 
 #include <errno.h>
-#include <fcntl.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
 #include <unistd.h>
 
 #include <algorithm>
 #include <cctype>
-#include <charconv>
-#include <regex>
 #include <string>
 #include <unordered_map>
 #include <vector>
 
+#include <android-base/file.h>
+#include <android-base/parseint.h>
+#include <android-base/strings.h>
 #include <gtest/gtest.h>
 
 #include "Options.h"
@@ -92,16 +90,20 @@
 }
 
 template <typename IntType>
-static bool GetNumeric(const std::string& arg, const std::string& value, IntType* numeric_value,
-                       bool from_env) {
-  auto result = std::from_chars(value.c_str(), value.c_str() + value.size(), *numeric_value, 10);
-  if (result.ec == std::errc::result_out_of_range) {
-    PrintError(arg, std::string("value overflows (") + value + ")", from_env);
-    return false;
-  } else if (result.ec == std::errc::invalid_argument || result.ptr == nullptr ||
-             *result.ptr != '\0') {
-    PrintError(arg, std::string("value is not formatted as a numeric value (") + value + ")",
-               from_env);
+static bool GetNumeric(const char* arg, const char* value, IntType* numeric_value, bool from_env) {
+  bool result = false;
+  if constexpr (std::is_unsigned<IntType>::value) {
+    result = android::base::ParseUint<IntType>(value, numeric_value);
+  } else {
+    result = android::base::ParseInt<IntType>(value, numeric_value);
+  }
+  if (!result) {
+    if (errno == ERANGE) {
+      PrintError(arg, std::string("value overflows (") + value + ")", from_env);
+    } else {
+      PrintError(arg, std::string("value is not formatted as a numeric value (") + value + ")",
+                 from_env);
+    }
     return false;
   }
   return true;
@@ -116,7 +118,7 @@
 
 bool Options::SetNumeric(const std::string& arg, const std::string& value, bool from_env) {
   uint64_t* numeric = &numerics_.find(arg)->second;
-  if (!GetNumeric<uint64_t>(arg, value, numeric, from_env)) {
+  if (!GetNumeric<uint64_t>(arg.c_str(), value.c_str(), numeric, from_env)) {
     return false;
   }
   if (*numeric == 0) {
@@ -132,7 +134,7 @@
     return false;
   }
   uint64_t* numeric = &numerics_.find(arg)->second;
-  if (!GetNumeric<uint64_t>(arg, value, numeric, from_env)) {
+  if (!GetNumeric<uint64_t>(arg.c_str(), value.c_str(), numeric, from_env)) {
     return false;
   }
   return true;
@@ -144,7 +146,7 @@
 }
 
 bool Options::SetIterations(const std::string& arg, const std::string& value, bool from_env) {
-  if (!GetNumeric<int>(arg, value, &num_iterations_, from_env)) {
+  if (!GetNumeric<int>(arg.c_str(), value.c_str(), &num_iterations_, from_env)) {
     return false;
   }
   return true;
@@ -212,29 +214,13 @@
   return true;
 }
 
-static bool ReadFileToString(const std::string& file, std::string* contents) {
-  int fd = TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC));
-  if (fd == -1) {
-    return false;
-  }
-  char buf[4096];
-  ssize_t bytes_read;
-  while ((bytes_read = TEMP_FAILURE_RETRY(read(fd, &buf, sizeof(buf)))) > 0) {
-    contents->append(buf, bytes_read);
-  }
-  close(fd);
-  return true;
-}
-
 bool Options::ProcessFlagfile(const std::string& file, std::vector<char*>* child_args) {
   std::string contents;
-  if (!ReadFileToString(file, &contents)) {
+  if (!android::base::ReadFileToString(file, &contents)) {
     printf("Unable to read data from file %s\n", file.c_str());
     return false;
   }
 
-  std::regex flag_regex("^\\s*(\\S.*\\S)\\s*$");
-  std::regex empty_line_regex("^\\s*$");
   size_t idx = 0;
   while (idx < contents.size()) {
     size_t newline_idx = contents.find('\n', idx);
@@ -243,10 +229,8 @@
     }
     std::string line(&contents[idx], newline_idx - idx);
     idx = newline_idx + 1;
-    std::smatch match;
-    if (std::regex_match(line, match, flag_regex)) {
-      line = match[1];
-    } else if (std::regex_match(line, match, empty_line_regex)) {
+    line = android::base::Trim(line);
+    if (line.empty()) {
       // Skip lines with only whitespace.
       continue;
     }
diff --git a/Test.cpp b/Test.cpp
index 23d139c..327facd 100644
--- a/Test.cpp
+++ b/Test.cpp
@@ -23,11 +23,10 @@
 #include <tuple>
 #include <vector>
 
-#include <android/log.h>
+#include <android-base/logging.h>
 #include <gtest/gtest.h>
 
 #include "Color.h"
-#include "Log.h"
 #include "NanoTime.h"
 #include "Test.h"
 
@@ -50,10 +49,7 @@
 }
 
 void Test::CloseFd() {
-  if (fd_ != -1) {
-    close(fd_);
-    fd_ = -1;
-  }
+  fd_.reset();
 }
 
 void Test::Print() {
@@ -89,7 +85,7 @@
       // Reading would block. Since this is not an error keep going.
       return true;
     }
-    FATAL_PLOG("Unexpected failure from read");
+    PLOG(FATAL) << "Unexpected failure from read";
     return false;
   }
 
diff --git a/Test.h b/Test.h
index dd54dc6..b4a1182 100644
--- a/Test.h
+++ b/Test.h
@@ -20,6 +20,8 @@
 #include <string>
 #include <tuple>
 
+#include <android-base/unique_fd.h>
+
 namespace android {
 namespace gtest_extras {
 
@@ -36,7 +38,6 @@
 class Test {
  public:
   Test(std::tuple<std::string, std::string>& test, size_t test_index, size_t run_index, int fd);
-  ~Test() { CloseFd(); }
 
   void Print();
 
@@ -86,7 +87,7 @@
   std::string name_;
   size_t test_index_;  // Index into test list.
   size_t run_index_;   // Index into running list.
-  int fd_ = -1;
+  android::base::unique_fd fd_;
 
   uint64_t start_ns_;
   uint64_t end_ns_ = 0;