Fix race in AllocEntrypointsInstrumented
We were using the quick_alloc_entry_points_instrumentation_counter_,
this counter is updated before the threads are suspended. The
allocator could come out of a suspend point, see that threads are
supposedly still no instrumented, continue the allocation, then
suddenly quick_alloc_entry_points_instrumentation_counter_ becomes
1 and alloc_tracking_enabled_ becomes true resulting in a failing
DCHECK.
The fix is to add a boolean that is updated only when the threads
are suspended.
Bug: 27506909
(cherry picked from commit 77d993107773b7b9bd7f07ce08d0aaac1631bf84)
Change-Id: Id12983ef77c4fddb0394e4439ce0829777f1a70b
diff --git a/runtime/instrumentation.cc b/runtime/instrumentation.cc
index b107b72..a0c6bfb 100644
--- a/runtime/instrumentation.cc
+++ b/runtime/instrumentation.cc
@@ -83,7 +83,8 @@
deoptimized_methods_lock_("deoptimized methods lock"),
deoptimization_enabled_(false),
interpreter_handler_table_(kMainHandlerTable),
- quick_alloc_entry_points_instrumentation_counter_(0) {
+ quick_alloc_entry_points_instrumentation_counter_(0),
+ alloc_entrypoints_instrumented_(false) {
}
void Instrumentation::InstallStubsForClass(mirror::Class* klass) {
@@ -642,10 +643,12 @@
MutexLock mu(self, *Locks::runtime_shutdown_lock_);
SetQuickAllocEntryPointsInstrumented(instrumented);
ResetQuickAllocEntryPoints();
+ alloc_entrypoints_instrumented_ = instrumented;
} else {
MutexLock mu(self, *Locks::runtime_shutdown_lock_);
SetQuickAllocEntryPointsInstrumented(instrumented);
ResetQuickAllocEntryPoints();
+ alloc_entrypoints_instrumented_ = instrumented;
}
}
diff --git a/runtime/instrumentation.h b/runtime/instrumentation.h
index b3cdb41..d07f47b 100644
--- a/runtime/instrumentation.h
+++ b/runtime/instrumentation.h
@@ -422,7 +422,7 @@
// Does not hold lock, used to check if someone changed from not instrumented to instrumented
// during a GC suspend point.
bool AllocEntrypointsInstrumented() const SHARED_REQUIRES(Locks::mutator_lock_) {
- return quick_alloc_entry_points_instrumentation_counter_ > 0;
+ return alloc_entrypoints_instrumented_;
}
private:
@@ -579,6 +579,12 @@
// Greater than 0 if quick alloc entry points instrumented.
size_t quick_alloc_entry_points_instrumentation_counter_;
+
+ // alloc_entrypoints_instrumented_ is only updated with all the threads suspended, this is done
+ // to prevent races with the GC where the GC relies on thread suspension only see
+ // alloc_entrypoints_instrumented_ change during suspend points.
+ bool alloc_entrypoints_instrumented_;
+
friend class InstrumentationTest; // For GetCurrentInstrumentationLevel and ConfigureStubs.
DISALLOW_COPY_AND_ASSIGN(Instrumentation);