Prevent ArtMethod clearing race

There was race condition where the GC would be visiting the methods
of a class while the class linker overwrote the contents with 0xFE
when copying to a new array.

Since the GC is holding the class table lock at this time, we can
use this lock in the class linker to prevent the race.

Bug: 28699001
Change-Id: I91ca2406ef723e7be69cd1c53f4bafa2e5f63657
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index 35c40cd..e9b8643 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -6970,6 +6970,7 @@
     }
     // Put some random garbage in old methods to help find stale pointers.
     if (methods != old_methods && old_methods != nullptr) {
+      WriterMutexLock mu(self, ClassTableForClassLoader(klass->GetClassLoader())->GetLock());
       memset(old_methods, 0xFEu, old_size);
     }
   } else {
diff --git a/runtime/class_linker.h b/runtime/class_linker.h
index ece171c..d1c8172 100644
--- a/runtime/class_linker.h
+++ b/runtime/class_linker.h
@@ -1020,7 +1020,7 @@
 
   // Returns null if not found.
   ClassTable* ClassTableForClassLoader(mirror::ClassLoader* class_loader)
-      SHARED_REQUIRES(Locks::mutator_lock_, Locks::classlinker_classes_lock_);
+      SHARED_REQUIRES(Locks::mutator_lock_);
 
   // Insert a new class table if not found.
   ClassTable* InsertClassTableForClassLoader(mirror::ClassLoader* class_loader)
diff --git a/runtime/class_table.h b/runtime/class_table.h
index eb784b5..686381d 100644
--- a/runtime/class_table.h
+++ b/runtime/class_table.h
@@ -153,6 +153,10 @@
       REQUIRES(!lock_)
       SHARED_REQUIRES(Locks::mutator_lock_);
 
+  ReaderWriterMutex& GetLock() {
+    return lock_;
+  }
+
  private:
   // Lock to guard inserting and removing.
   mutable ReaderWriterMutex lock_;