Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit feeb7ad

Browse files
aamcommit-bot@chromium.org
authored andcommitted
[vm/concurrency] Add write-lock assertion to Class::SetFields protecting Class fields.
This is follow-up to https://dart-review.googlesource.com/c/sdk/+/168140 which added write-lock to protect Class functions. Bug: dart-lang/sdk#36097 Change-Id: Idb2070d005c44509fc6701f2eb297b90e7ded581 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/168772 Reviewed-by: Martin Kustermann <kustermann@google.com> Commit-Queue: Alexander Aprelev <aam@google.com>
1 parent 471e8fc commit feeb7ad

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

runtime/vm/compiler/aot/precompiler.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,7 @@ void Precompiler::DropFields() {
18071807
AbstractType& type = AbstractType::Handle(Z);
18081808
Function& initializer_function = Function::Handle(Z);
18091809

1810+
SafepointWriteRwLocker ml(T, T->isolate_group()->program_lock());
18101811
for (intptr_t i = 0; i < libraries_.Length(); i++) {
18111812
lib ^= libraries_.At(i);
18121813
ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate);

runtime/vm/object.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,10 +1115,10 @@ void Object::Init(Isolate* isolate) {
11151115
// in the vm isolate. See special handling in Class::SuperClass().
11161116
cls = type_arguments_class_;
11171117
cls.set_interfaces(Object::empty_array());
1118-
cls.SetFields(Object::empty_array());
11191118
{
11201119
Thread* thread = Thread::Current();
11211120
SafepointWriteRwLocker ml(thread, thread->isolate_group()->program_lock());
1121+
cls.SetFields(Object::empty_array());
11221122
cls.SetFunctions(Object::empty_array());
11231123
}
11241124

@@ -4354,6 +4354,9 @@ ErrorPtr Class::EnsureIsAllocateFinalized(Thread* thread) const {
43544354
void Class::SetFields(const Array& value) const {
43554355
ASSERT(!value.IsNull());
43564356
#if defined(DEBUG)
4357+
Thread* thread = Thread::Current();
4358+
ASSERT(thread->IsMutatorThread());
4359+
ASSERT(thread->isolate_group()->program_lock()->IsCurrentThreadWriter());
43574360
// Verify that all the fields in the array have this class as owner.
43584361
Field& field = Field::Handle();
43594362
intptr_t len = value.Length();
@@ -4368,13 +4371,23 @@ void Class::SetFields(const Array& value) const {
43684371
}
43694372

43704373
void Class::AddField(const Field& field) const {
4374+
#if defined(DEBUG)
4375+
Thread* thread = Thread::Current();
4376+
ASSERT(thread->IsMutatorThread());
4377+
ASSERT(thread->isolate_group()->program_lock()->IsCurrentThreadWriter());
4378+
#endif
43714379
const Array& arr = Array::Handle(fields());
43724380
const Array& new_arr = Array::Handle(Array::Grow(arr, arr.Length() + 1));
43734381
new_arr.SetAt(arr.Length(), field);
43744382
SetFields(new_arr);
43754383
}
43764384

43774385
void Class::AddFields(const GrowableArray<const Field*>& new_fields) const {
4386+
#if defined(DEBUG)
4387+
Thread* thread = Thread::Current();
4388+
ASSERT(thread->IsMutatorThread());
4389+
ASSERT(thread->isolate_group()->program_lock()->IsCurrentThreadWriter());
4390+
#endif
43784391
const intptr_t num_new_fields = new_fields.length();
43794392
if (num_new_fields == 0) return;
43804393
const Array& arr = Array::Handle(fields());

runtime/vm/object_test.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ ISOLATE_UNIT_TEST_CASE(SixtyThousandDartClasses) {
207207
SafepointWriteRwLocker ml(thread,
208208
thread->isolate_group()->program_lock());
209209
cls.SetFunctions(Array::empty_array());
210+
cls.SetFields(fields);
210211
}
211-
cls.SetFields(fields);
212212
cls.Finalize();
213213

214214
instance = Instance::New(cls);
@@ -332,7 +332,10 @@ ISOLATE_UNIT_TEST_CASE(InstanceClass) {
332332
Object::dynamic_type(), TokenPosition::kMinSource,
333333
TokenPosition::kMinSource));
334334
one_fields.SetAt(0, field);
335-
one_field_class.SetFields(one_fields);
335+
{
336+
SafepointWriteRwLocker ml(thread, thread->isolate_group()->program_lock());
337+
one_field_class.SetFields(one_fields);
338+
}
336339
one_field_class.Finalize();
337340
intptr_t header_size = sizeof(ObjectLayout);
338341
EXPECT_EQ(Utils::RoundUp((header_size + (1 * kWordSize)), kObjectAlignment),

0 commit comments

Comments
 (0)