OSDN Git Service

Only fill methods with 0xFE on debug builds
authorMathieu Chartier <mathieuc@google.com>
Thu, 19 May 2016 17:13:04 +0000 (10:13 -0700)
committerMathieu Chartier <mathieuc@google.com>
Fri, 20 May 2016 23:30:50 +0000 (16:30 -0700)
The GC scans classes without holding any locks, we can not fill
the methods if it is running. Added a GC critical section to address
this. Fixes random crash when scanning classes' methods.

Only for debug builds to not hurt performance.

(cherry picked from commit 22bd2a1b5ec2a5038cc3ae1964781f30aef0315f)

Bug: 28699001
Change-Id: If96155eaf3fc0e6df31f57dcf32fbd4063b09345

runtime/class_linker.cc
runtime/gc/collector_type.h
runtime/gc/gc_cause.cc
runtime/gc/gc_cause.h
runtime/gc/scoped_gc_critical_section.cc

index 8fcb6b2..d03b57c 100644 (file)
@@ -52,6 +52,7 @@
 #include "gc/accounting/card_table-inl.h"
 #include "gc/accounting/heap_bitmap-inl.h"
 #include "gc/heap.h"
+#include "gc/scoped_gc_critical_section.h"
 #include "gc/space/image_space.h"
 #include "handle_scope-inl.h"
 #include "image-inl.h"
@@ -6981,8 +6982,13 @@ bool ClassLinker::LinkInterfaceMethods(
       }
     }
     // 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());
+    if (methods != old_methods && old_methods != nullptr && kIsDebugBuild) {
+      // Need to make sure the GC is not running since it could be scanning the methods we are
+      // about to overwrite.
+      ScopedThreadStateChange tsc(self, kSuspended);
+      gc::ScopedGCCriticalSection gcs(self,
+                                      gc::kGcCauseClassLinker,
+                                      gc::kCollectorTypeClassLinker);
       memset(old_methods, 0xFEu, old_size);
     }
   } else {
index 4ffc8af..c602081 100644 (file)
@@ -49,6 +49,8 @@ enum CollectorType {
   // A homogeneous space compaction collector used in background transition
   // when both foreground and background collector are CMS.
   kCollectorTypeHomogeneousSpaceCompact,
+  // Class linker fake collector.
+  kCollectorTypeClassLinker,
 };
 std::ostream& operator<<(std::ostream& os, const CollectorType& collector_type);
 
index 18e5703..ad9bb92 100644 (file)
@@ -36,6 +36,7 @@ const char* PrettyCause(GcCause cause) {
     case kGcCauseInstrumentation: return "Instrumentation";
     case kGcCauseAddRemoveAppImageSpace: return "AddRemoveAppImageSpace";
     case kGcCauseDebugger: return "Debugger";
+    case kGcCauseClassLinker: return "ClassLinker";
     default:
       LOG(FATAL) << "Unreachable";
       UNREACHABLE();
index ad67eb7..797ec34 100644 (file)
@@ -47,6 +47,8 @@ enum GcCause {
   kGcCauseDebugger,
   // GC triggered for background transition when both foreground and background collector are CMS.
   kGcCauseHomogeneousSpaceCompact,
+  // Class linker cause, used to guard filling art methods with special values.
+  kGcCauseClassLinker,
 };
 
 const char* PrettyCause(GcCause cause);
index e7786a1..b5eb979 100644 (file)
@@ -38,4 +38,3 @@ ScopedGCCriticalSection::~ScopedGCCriticalSection() {
 
 }  // namespace gc
 }  // namespace art
-