Merge "Dex header cleanup and windows simplification"
diff --git a/libdexfile/dex/dex_file.cc b/libdexfile/dex/dex_file.cc
index 18eb903..6a704c1 100644
--- a/libdexfile/dex/dex_file.cc
+++ b/libdexfile/dex/dex_file.cc
@@ -45,6 +45,21 @@
 static_assert(sizeof(dex::TypeIndex) == sizeof(uint16_t), "TypeIndex size is wrong");
 static_assert(std::is_trivially_copyable<dex::TypeIndex>::value, "TypeIndex not trivial");
 
+void DexFile::UnHideAccessFlags(ClassDataItemIterator& class_it) {
+  uint8_t* data = const_cast<uint8_t*>(class_it.DataPointer());
+  uint32_t new_flag = class_it.GetMemberAccessFlags();
+  bool is_method = class_it.IsAtMethod();
+  // Go back 1 uleb to start.
+  data = ReverseSearchUnsignedLeb128(data);
+  if (is_method) {
+    // Methods have another uleb field before the access flags
+    data = ReverseSearchUnsignedLeb128(data);
+  }
+  DCHECK_EQ(HiddenApiAccessFlags::RemoveFromDex(DecodeUnsignedLeb128WithoutMovingCursor(data)),
+            new_flag);
+  UpdateUnsignedLeb128(data, new_flag);
+}
+
 uint32_t DexFile::CalculateChecksum() const {
   return CalculateChecksum(Begin(), Size());
 }
diff --git a/libdexfile/dex/dex_file.h b/libdexfile/dex/dex_file.h
index a38e76c..a62ab62 100644
--- a/libdexfile/dex/dex_file.h
+++ b/libdexfile/dex/dex_file.h
@@ -35,6 +35,7 @@
 
 namespace art {
 
+class ClassDataItemIterator;
 class CompactDexFile;
 enum InvokeType : uint32_t;
 class MemMap;
@@ -1000,6 +1001,9 @@
     return container_.get();
   }
 
+  // Changes the dex file pointed to by class_it to not have any hiddenapi flags.
+  static void UnHideAccessFlags(ClassDataItemIterator& class_it);
+
  protected:
   // First Dex format version supporting default methods.
   static const uint32_t kDefaultMethodsVersion = 37;
diff --git a/openjdkjvmti/fixed_up_dex_file.cc b/openjdkjvmti/fixed_up_dex_file.cc
index 427d87e..90c6449 100644
--- a/openjdkjvmti/fixed_up_dex_file.cc
+++ b/openjdkjvmti/fixed_up_dex_file.cc
@@ -40,6 +40,7 @@
 #include "dex/compact_dex_level.h"
 #include "dex_to_dex_decompiler.h"
 #include "dexlayout.h"
+#include "leb128.h"
 #include "oat_file.h"
 #include "vdex_file.h"
 
@@ -50,21 +51,41 @@
       dex_file->CalculateChecksum();
 }
 
-static void DoDexUnquicken(const art::DexFile& new_dex_file,
-                           const art::DexFile& original_dex_file) {
+static void UnhideApis(const art::DexFile& target_dex_file) {
+  for (uint32_t i = 0; i < target_dex_file.NumClassDefs(); ++i) {
+    const uint8_t* class_data = target_dex_file.GetClassData(target_dex_file.GetClassDef(i));
+    if (class_data != nullptr) {
+      for (art::ClassDataItemIterator class_it(target_dex_file, class_data);
+           class_it.HasNext();
+           class_it.Next()) {
+        art::DexFile::UnHideAccessFlags(class_it);
+      }
+    }
+  }
+}
+
+static const art::VdexFile* GetVdex(const art::DexFile& original_dex_file) {
   const art::OatDexFile* oat_dex = original_dex_file.GetOatDexFile();
   if (oat_dex == nullptr) {
-    return;
+    return nullptr;
   }
   const art::OatFile* oat_file = oat_dex->GetOatFile();
   if (oat_file == nullptr) {
-    return;
+    return nullptr;
   }
-  const art::VdexFile* vdex = oat_file->GetVdexFile();
-  if (vdex == nullptr) {
-    return;
+  return oat_file->GetVdexFile();
+}
+
+static void DoDexUnquicken(const art::DexFile& new_dex_file,
+                           const art::DexFile& original_dex_file) {
+  const art::VdexFile* vdex = GetVdex(original_dex_file);
+  if (vdex != nullptr) {
+    vdex->UnquickenDexFile(new_dex_file, original_dex_file, /* decompile_return_instruction */true);
+  } else {
+    // The dex file isn't quickened since it is being used directly. We might still have hiddenapis
+    // so we need to get rid of those.
+    UnhideApis(new_dex_file);
   }
-  vdex->UnquickenDexFile(new_dex_file, original_dex_file, /* decompile_return_instruction */true);
 }
 
 static void DCheckVerifyDexFile(const art::DexFile& dex) {
diff --git a/profman/profile_assistant_test.cc b/profman/profile_assistant_test.cc
index 188d0b0..1dd1a4a 100644
--- a/profman/profile_assistant_test.cc
+++ b/profman/profile_assistant_test.cc
@@ -75,31 +75,34 @@
                     const ScratchFile& profile,
                     ProfileCompilationInfo* info,
                     uint16_t start_method_index = 0,
-                    bool reverse_dex_write_order = false) {
+                    bool reverse_dex_write_order = false,
+                    uint32_t number_of_methods1 = kMaxMethodIds,
+                    uint32_t number_of_methods2 = kMaxMethodIds) {
     for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) {
       // reverse_dex_write_order controls the order in which the dex files will be added to
       // the profile and thus written to disk.
       ProfileCompilationInfo::OfflineProfileMethodInfo pmi =
           GetOfflineProfileMethodInfo(dex_location1, dex_location_checksum1,
-                                      dex_location2, dex_location_checksum2);
+                                      dex_location2, dex_location_checksum2,
+                                      number_of_methods1, number_of_methods2);
       Hotness::Flag flags = Hotness::kFlagPostStartup;
       if (reverse_dex_write_order) {
         ASSERT_TRUE(info->AddMethod(
-            dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi, flags));
+            dex_location2, dex_location_checksum2, i, number_of_methods2, pmi, flags));
         ASSERT_TRUE(info->AddMethod(
-            dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi, flags));
+            dex_location1, dex_location_checksum1, i, number_of_methods1, pmi, flags));
       } else {
         ASSERT_TRUE(info->AddMethod(
-            dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi, flags));
+            dex_location1, dex_location_checksum1, i, number_of_methods1, pmi, flags));
         ASSERT_TRUE(info->AddMethod(
-            dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi, flags));
+            dex_location2, dex_location_checksum2, i, number_of_methods2, pmi, flags));
       }
     }
     for (uint16_t i = 0; i < number_of_classes; i++) {
       ASSERT_TRUE(info->AddClassIndex(dex_location1,
                                       dex_location_checksum1,
                                       dex::TypeIndex(i),
-                                      kMaxMethodIds));
+                                      number_of_methods1));
     }
 
     ASSERT_TRUE(info->Save(GetFd(profile)));
@@ -143,11 +146,12 @@
 
   ProfileCompilationInfo::OfflineProfileMethodInfo GetOfflineProfileMethodInfo(
         const std::string& dex_location1, uint32_t dex_checksum1,
-        const std::string& dex_location2, uint32_t dex_checksum2) {
+        const std::string& dex_location2, uint32_t dex_checksum2,
+        uint32_t number_of_methods1 = kMaxMethodIds, uint32_t number_of_methods2 = kMaxMethodIds) {
     ProfileCompilationInfo::InlineCacheMap* ic_map = CreateInlineCacheMap();
     ProfileCompilationInfo::OfflineProfileMethodInfo pmi(ic_map);
-    pmi.dex_references.emplace_back(dex_location1, dex_checksum1, kMaxMethodIds);
-    pmi.dex_references.emplace_back(dex_location2, dex_checksum2, kMaxMethodIds);
+    pmi.dex_references.emplace_back(dex_location1, dex_checksum1, number_of_methods1);
+    pmi.dex_references.emplace_back(dex_location2, dex_checksum2, number_of_methods2);
 
     // Monomorphic
     for (uint16_t dex_pc = 0; dex_pc < 11; dex_pc++) {
@@ -1242,4 +1246,61 @@
   ASSERT_TRUE(expected.Equals(result));
 }
 
+TEST_F(ProfileAssistantTest, CopyAndUpdateProfileKey) {
+  ScratchFile profile1;
+  ScratchFile reference_profile;
+
+  // Use a real dex file to generate profile test data. During the copy-and-update the
+  // matching is done based on checksum so we have to match with the real thing.
+  std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("ProfileTestMultiDex");
+  const DexFile& d1 = *dex_files[0];
+  const DexFile& d2 = *dex_files[1];
+
+  ProfileCompilationInfo info1;
+  uint16_t num_methods_to_add = std::min(d1.NumMethodIds(), d2.NumMethodIds());
+  SetupProfile("fake-location1",
+               d1.GetLocationChecksum(),
+               "fake-location2",
+               d2.GetLocationChecksum(),
+               num_methods_to_add,
+               /*num_classes*/ 0,
+               profile1,
+               &info1,
+               /*start_method_index*/ 0,
+               /*reverse_dex_write_order*/ false,
+               /*number_of_methods1*/ d1.NumMethodIds(),
+               /*number_of_methods2*/ d2.NumMethodIds());
+
+  // Run profman and pass the dex file with --apk-fd.
+  android::base::unique_fd apk_fd(
+      open(GetTestDexFileName("ProfileTestMultiDex").c_str(), O_RDONLY));
+  ASSERT_GE(apk_fd.get(), 0);
+
+  std::string profman_cmd = GetProfmanCmd();
+  std::vector<std::string> argv_str;
+  argv_str.push_back(profman_cmd);
+  argv_str.push_back("--profile-file-fd=" + std::to_string(profile1.GetFd()));
+  argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile.GetFd()));
+  argv_str.push_back("--apk-fd=" + std::to_string(apk_fd.get()));
+  argv_str.push_back("--copy-and-update-profile-key");
+  std::string error;
+
+  ASSERT_EQ(ExecAndReturnCode(argv_str, &error), 0) << error;
+
+  // Verify that we can load the result.
+  ProfileCompilationInfo result;
+  ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
+  ASSERT_TRUE(result.Load(reference_profile.GetFd()));
+
+  // Verify that the renaming was done.
+  for (uint16_t i = 0; i < num_methods_to_add; i ++) {
+      std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi;
+      ASSERT_TRUE(result.GetMethod(d1.GetLocation(), d1.GetLocationChecksum(), i) != nullptr) << i;
+      ASSERT_TRUE(result.GetMethod(d2.GetLocation(), d2.GetLocationChecksum(), i) != nullptr) << i;
+
+      ASSERT_TRUE(result.GetMethod("fake-location1", d1.GetLocationChecksum(), i) == nullptr);
+      ASSERT_TRUE(result.GetMethod("fake-location2", d2.GetLocationChecksum(), i) == nullptr);
+  }
+}
+
 }  // namespace art
diff --git a/profman/profman.cc b/profman/profman.cc
index efb7fcf..5551c34 100644
--- a/profman/profman.cc
+++ b/profman/profman.cc
@@ -280,6 +280,8 @@
                         Usage);
       } else if (option.starts_with("--generate-test-profile-seed=")) {
         ParseUintOption(option, "--generate-test-profile-seed", &test_profile_seed_, Usage);
+      } else if (option.starts_with("--copy-and-update-profile-key")) {
+        copy_and_update_profile_key_ = true;
       } else {
         Usage("Unknown argument '%s'", option.data());
       }
@@ -405,9 +407,12 @@
         }
       }
     } else if (!apk_files_.empty()) {
-        if (dex_locations_.size() != apk_files_.size()) {
-            Usage("The number of apk-fds must match the number of dex-locations.");
-        }
+      if (dex_locations_.empty()) {
+        // If no dex locations are specified use the apk names as locations.
+        dex_locations_ = apk_files_;
+      } else if (dex_locations_.size() != apk_files_.size()) {
+          Usage("The number of apk-fds must match the number of dex-locations.");
+      }
     } else {
       // No APKs were specified.
       CHECK(dex_locations_.empty());
@@ -1178,7 +1183,7 @@
     return copy_and_update_profile_key_;
   }
 
-  bool CopyAndUpdateProfileKey() {
+  int32_t CopyAndUpdateProfileKey() {
     // Validate that at least one profile file was passed, as well as a reference profile.
     if (!(profile_files_.size() == 1 ^ profile_files_fd_.size() == 1)) {
       Usage("Only one profile file should be specified.");
@@ -1191,22 +1196,30 @@
       Usage("No apk files specified");
     }
 
+    static constexpr int32_t kErrorFailedToUpdateProfile = -1;
+    static constexpr int32_t kErrorFailedToSaveProfile = -2;
+    static constexpr int32_t kErrorFailedToLoadProfile = -3;
+
     bool use_fds = profile_files_fd_.size() == 1;
 
     ProfileCompilationInfo profile;
     // Do not clear if invalid. The input might be an archive.
-    if (profile.Load(profile_files_[0], /*clear_if_invalid*/ false)) {
+    bool load_ok = use_fds
+        ? profile.Load(profile_files_fd_[0])
+        : profile.Load(profile_files_[0], /*clear_if_invalid*/ false);
+    if (load_ok) {
       // Open the dex files to look up classes and methods.
       std::vector<std::unique_ptr<const DexFile>> dex_files;
       OpenApkFilesFromLocations(&dex_files);
       if (!profile.UpdateProfileKeys(dex_files)) {
-        return false;
+        return kErrorFailedToUpdateProfile;
       }
-      return use_fds
-        ? profile.Save(reference_profile_file_fd_)
-        : profile.Save(reference_profile_file_, /*bytes_written*/ nullptr);
+      bool result = use_fds
+          ? profile.Save(reference_profile_file_fd_)
+          : profile.Save(reference_profile_file_, /*bytes_written*/ nullptr);
+      return result ? 0 : kErrorFailedToSaveProfile;
     } else {
-      return false;
+      return kErrorFailedToLoadProfile;
     }
   }
 
@@ -1285,6 +1298,11 @@
   if (profman.ShouldCreateBootProfile()) {
     return profman.CreateBootProfile();
   }
+
+  if (profman.ShouldCopyAndUpdateProfileKey()) {
+    return profman.CopyAndUpdateProfileKey();
+  }
+
   // Process profile information and assess if we need to do a profile guided compilation.
   // This operation involves I/O.
   return profman.ProcessProfiles();
diff --git a/runtime/vdex_file.cc b/runtime/vdex_file.cc
index 0829c54..443c35f 100644
--- a/runtime/vdex_file.cc
+++ b/runtime/vdex_file.cc
@@ -263,18 +263,6 @@
   UnquickenDexFile(target_dex_file, source_dex_file.Begin(), decompile_return_instruction);
 }
 
-static void UpdateAccessFlags(uint8_t* data, uint32_t new_flag, bool is_method) {
-  // Go back 1 uleb to start.
-  data = ReverseSearchUnsignedLeb128(data);
-  if (is_method) {
-    // Methods have another uleb field before the access flags
-    data = ReverseSearchUnsignedLeb128(data);
-  }
-  DCHECK_EQ(HiddenApiAccessFlags::RemoveFromDex(DecodeUnsignedLeb128WithoutMovingCursor(data)),
-            new_flag);
-  UpdateUnsignedLeb128(data, new_flag);
-}
-
 void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
                                 const uint8_t* source_dex_begin,
                                 bool decompile_return_instruction) const {
@@ -312,14 +300,8 @@
                 quicken_data,
                 decompile_return_instruction);
           }
-          UpdateAccessFlags(const_cast<uint8_t*>(class_it.DataPointer()),
-                            class_it.GetMemberAccessFlags(),
-                            /*is_method*/ true);
-        } else {
-          UpdateAccessFlags(const_cast<uint8_t*>(class_it.DataPointer()),
-                            class_it.GetMemberAccessFlags(),
-                            /*is_method*/ false);
         }
+        DexFile::UnHideAccessFlags(class_it);
       }
     }
   }
diff --git a/test/912-classes/src-art/art/Test912.java b/test/912-classes/src-art/art/Test912.java
index ddfadf3..1a60185 100644
--- a/test/912-classes/src-art/art/Test912.java
+++ b/test/912-classes/src-art/art/Test912.java
@@ -398,6 +398,7 @@
     public static double dummy = Math.random();  // So it can't be compile-time initialized.
   }
 
+  @SuppressWarnings("RandomCast")
   private static class TestForInitFail {
     public static int dummy = ((int)Math.random())/0;  // So it throws when initializing.
   }
diff --git a/test/knownfailures.json b/test/knownfailures.json
index b483b93..ddf9098 100644
--- a/test/knownfailures.json
+++ b/test/knownfailures.json
@@ -377,12 +377,6 @@
         "variant": "jvmti-stress & jit | redefine-stress & jit"
     },
     {
-        "test_patterns": ["674-hiddenapi"],
-        "description": ["hiddenapi test is failing with redefine stress cdex"],
-        "bug": "http://b/72610009",
-        "variant": "redefine-stress & cdex-fast"
-    },
-    {
         "test_patterns": ["616-cha"],
         "description": ["The test assumes a boot image exists."],
         "bug": "http://b/34193647",
@@ -423,21 +417,13 @@
         "variant": "redefine-stress & speed-profile | jvmti-stress & speed-profile"
     },
     {
-        "tests": [
-            "714-invoke-custom-lambda-metafactory",
-            "950-redefine-intrinsic",
-            "951-threaded-obsolete",
-            "952-invoke-custom",
-            "952-invoke-custom-kinds",
-            "953-invoke-polymorphic-compiler",
-            "954-invoke-polymorphic-verifier",
-            "955-methodhandles-smali",
-            "956-methodhandles",
-            "957-methodhandle-transforms",
-            "958-methodhandle-stackframe",
-            "959-invoke-polymorphic-accessors",
-            "979-const-method-handle",
-            "990-method-handle-and-mr"
+        "test_patterns": [
+            ".*invoke-custom.*",
+            ".*invoke-polymorphic.*",
+            ".*methodhandle.*",
+            ".*method-handle.*",
+            ".*varhandle.*",
+            ".*var-handle.*"
         ],
         "description": [
             "Tests that use invoke-polymorphic/invoke-custom which is not yet supported by",
@@ -460,9 +446,22 @@
     },
     {
         "tests": [
+            "132-daemon-locks-shutdown",
+            "607-daemon-stress",
+            "602-deoptimizeable",
+            "121-simple-suspend-check",
+            "083-compiler-regressions"
+        ],
+        "description": ["Tests that have failed on redefine stress for unknown reasons"],
+        "bug": "b/73177368",
+        "variant": "redefine-stress"
+    },
+    {
+        "tests": [
             "097-duplicate-method",
             "138-duplicate-classes-check2",
             "159-app-image-fields",
+            "674-hiddenapi",
             "649-vdex-duplicate-method",
             "804-class-extends-itself",
             "921-hello-failure"
@@ -481,6 +480,7 @@
             "626-const-class-linking",
             "629-vdex-speed",
             "647-jni-get-field-id",
+            "674-hiddenapi",
             "944-transform-classloaders"
         ],
         "description": [
@@ -494,7 +494,7 @@
             "004-ThreadStress"
         ],
         "description": "The thread stress test just takes too long with field-stress",
-        "variant": "jvmti-stress | field-stress | step-stress"
+        "variant": "jvmti-stress | field-stress | step-stress | redefine-stress"
     },
     {
         "tests": [
@@ -653,11 +653,6 @@
         "description": ["Test is designed to only check --compiler-filter=speed"]
     },
     {
-        "test_patterns": [".*"],
-        "description": ["Tests are timing out for weeks now, disable to fix."],
-        "variant": "cdex-fast & redefine-stress"
-    },
-    {
         "tests": "674-HelloWorld-Dm",
         "variant": "target",
         "description": ["Requires zip, which isn't available on device"]
diff --git a/test/testrunner/testrunner.py b/test/testrunner/testrunner.py
index 3d173f5..4329ad4 100755
--- a/test/testrunner/testrunner.py
+++ b/test/testrunner/testrunner.py
@@ -110,6 +110,7 @@
 total_test_count = 0
 verbose = False
 dry_run = False
+ignore_skips = False
 build = False
 gdb = False
 gdb_arg = ''
@@ -710,6 +711,8 @@
     return True
   if test in env.EXTRA_DISABLED_TESTS:
     return True
+  if ignore_skips:
+    return False
   variants_list = DISABLED_TEST_CONTAINER.get(test, {})
   for variants in variants_list:
     variants_present = True
@@ -878,6 +881,7 @@
 def parse_option():
   global verbose
   global dry_run
+  global ignore_skips
   global n_thread
   global build
   global gdb
@@ -897,6 +901,8 @@
   parser.add_argument('--dry-run', action='store_true', dest='dry_run')
   parser.add_argument("--skip", action="append", dest="skips", default=[],
                       help="Skip the given test in all circumstances.")
+  parser.add_argument("--no-skips", dest="ignore_skips", action="store_true", default=False,
+                      help="Don't skip any run-test configurations listed in knownfailures.json.")
   parser.add_argument('--no-build-dependencies',
                       action='store_false', dest='build',
                       help="Don't build dependencies under any circumstances. This is the " +
@@ -935,6 +941,7 @@
     verbose = True
   if options['n_thread']:
     n_thread = max(1, options['n_thread'])
+  ignore_skips = options['ignore_skips']
   if options['dry_run']:
     dry_run = True
     verbose = True