Skip to content

Commit

Permalink
Potentially fix race condition #2463 #2814
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Sep 26, 2023
1 parent 9711dd1 commit d358ebd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/check_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ gb_internal void check_struct_type(CheckerContext *ctx, Type *struct_type, Ast *

scope_reserve(ctx->scope, min_field_count);

rw_mutex_lock(&struct_type->Struct.fields_mutex);
defer (rw_mutex_unlock(&struct_type->Struct.fields_mutex));

if (st->is_raw_union && min_field_count > 1) {
struct_type->Struct.is_raw_union = true;
context = str_lit("struct #raw_union");
Expand Down
30 changes: 23 additions & 7 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ struct TypeStruct {
Type * soa_elem;
i32 soa_count;
StructSoaKind soa_kind;
BlockingMutex mutex; // for settings offsets
RwMutex fields_mutex;
BlockingMutex offset_mutex; // for settings offsets

bool is_polymorphic;
bool are_offsets_set : 1;
Expand Down Expand Up @@ -2951,7 +2952,11 @@ gb_internal Selection lookup_field_from_index(Type *type, i64 index) {
gbAllocator a = permanent_allocator();
isize max_count = 0;
switch (type->kind) {
case Type_Struct: max_count = type->Struct.fields.count; break;
case Type_Struct:
rw_mutex_shared_lock(&type->Struct.fields_mutex);
max_count = type->Struct.fields.count;
rw_mutex_shared_unlock(&type->Struct.fields_mutex);
break;
case Type_Tuple: max_count = type->Tuple.variables.count; break;
}

Expand All @@ -2960,7 +2965,9 @@ gb_internal Selection lookup_field_from_index(Type *type, i64 index) {
}

switch (type->kind) {
case Type_Struct:
case Type_Struct: {
rw_mutex_shared_lock(&type->Struct.fields_mutex);
defer (rw_mutex_shared_unlock(&type->Struct.fields_mutex));
for (isize i = 0; i < max_count; i++) {
Entity *f = type->Struct.fields[i];
if (f->kind == Entity_Variable) {
Expand All @@ -2971,7 +2978,8 @@ gb_internal Selection lookup_field_from_index(Type *type, i64 index) {
}
}
}
break;
} break;

case Type_Tuple:
for (isize i = 0; i < max_count; i++) {
Entity *f = type->Tuple.variables[i];
Expand Down Expand Up @@ -3024,7 +3032,10 @@ gb_internal Selection lookup_field_with_selection(Type *type_, String field_name
}
}
if (type->kind == Type_Struct) {
for_array(i, type->Struct.fields) {
rw_mutex_shared_lock(&type->Struct.fields_mutex);
isize field_count = type->Struct.fields.count;
rw_mutex_shared_unlock(&type->Struct.fields_mutex);
if (field_count != 0) for_array(i, type->Struct.fields) {
Entity *f = type->Struct.fields[i];
if (f->flags&EntityFlag_Using) {
sel = lookup_field_with_selection(f->type, field_name, is_type, sel, allow_blank_ident);
Expand Down Expand Up @@ -3052,7 +3063,9 @@ gb_internal Selection lookup_field_with_selection(Type *type_, String field_name
}

if (type->kind == Type_Struct) {
rw_mutex_shared_lock(&type->Struct.fields_mutex);
Scope *s = type->Struct.scope;
rw_mutex_shared_unlock(&type->Struct.fields_mutex);
if (s != nullptr) {
Entity *found = scope_lookup_current(s, field_name);
if (found != nullptr && found->kind != Entity_Variable) {
Expand Down Expand Up @@ -3100,7 +3113,10 @@ gb_internal Selection lookup_field_with_selection(Type *type_, String field_name
}
}

for_array(i, type->Struct.fields) {
rw_mutex_shared_lock(&type->Struct.fields_mutex);
isize field_count = type->Struct.fields.count;
rw_mutex_shared_unlock(&type->Struct.fields_mutex);
if (field_count != 0) for_array(i, type->Struct.fields) {
Entity *f = type->Struct.fields[i];
if (f->kind != Entity_Variable || (f->flags & EntityFlag_Field) == 0) {
continue;
Expand Down Expand Up @@ -3680,7 +3696,7 @@ gb_internal i64 *type_set_offsets_of(Slice<Entity *> const &fields, bool is_pack
gb_internal bool type_set_offsets(Type *t) {
t = base_type(t);
if (t->kind == Type_Struct) {
MUTEX_GUARD(&t->Struct.mutex);
MUTEX_GUARD(&t->Struct.offset_mutex);
if (!t->Struct.are_offsets_set) {
t->Struct.are_offsets_being_processed = true;
t->Struct.offsets = type_set_offsets_of(t->Struct.fields, t->Struct.is_packed, t->Struct.is_raw_union);
Expand Down

0 comments on commit d358ebd

Please sign in to comment.