From b33bf3f7042464ac4f6e187fc487ee42668fcef7 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 26 Aug 2021 22:17:51 +0100 Subject: [PATCH] Correct race condition and incorrect usage of `condition_signal` outside of a mutex lock --- src/check_stmt.cpp | 1 + src/exact_value.cpp | 5 +++++ src/llvm_backend_proc.cpp | 2 ++ src/main.cpp | 1 + src/thread_pool.cpp | 2 +- 5 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 7b039e001..6fc098e6a 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -704,6 +704,7 @@ void add_constant_switch_case(CheckerContext *ctx, Map *seen, Oper if (operand.value.kind == ExactValue_Invalid) { return; } + HashKey key = hash_exact_value(operand.value); TypeAndToken *found = map_get(seen, key); if (found != nullptr) { diff --git a/src/exact_value.cpp b/src/exact_value.cpp index 17b593995..d42f5359e 100644 --- a/src/exact_value.cpp +++ b/src/exact_value.cpp @@ -3,6 +3,8 @@ // TODO(bill): Big numbers // IMPORTANT TODO(bill): This needs to be completely fixed!!!!!!!! +gb_global BlockingMutex hash_exact_value_mutex; + struct Ast; struct HashKey; struct Type; @@ -62,6 +64,9 @@ struct ExactValue { gb_global ExactValue const empty_exact_value = {}; HashKey hash_exact_value(ExactValue v) { + mutex_lock(&hash_exact_value_mutex); + defer (mutex_unlock(&hash_exact_value_mutex)); + HashKey empty = {}; switch (v.kind) { case ExactValue_Invalid: diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 58f03f4e8..cca1ff196 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -676,6 +676,8 @@ lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr, for (unsigned i = 0; i < param_count; i++) { LLVMTypeRef param_type = param_types[i]; LLVMTypeRef arg_type = LLVMTypeOf(args[i]); + // LLVMTypeKind param_kind = LLVMGetTypeKind(param_type); + // LLVMTypeKind arg_kind = LLVMGetTypeKind(arg_type); GB_ASSERT_MSG( arg_type == param_type, "Parameter types do not match: %s != %s, argument: %s", diff --git a/src/main.cpp b/src/main.cpp index f94b6bd4f..505451141 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2018,6 +2018,7 @@ int main(int arg_count, char const **arg_ptr) { virtual_memory_init(); mutex_init(&fullpath_mutex); + mutex_init(&hash_exact_value_mutex); init_string_buffer_memory(); init_string_interner(); diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index a8bc327e5..ed3a065e0 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -93,8 +93,8 @@ bool thread_pool_add_task(ThreadPool *pool, WorkerTaskProc *proc, void *data) { thread_pool_queue_push(pool, task); GB_ASSERT(pool->ready >= 0); pool->ready++; - mutex_unlock(&pool->mutex); condition_signal(&pool->task_cond); + mutex_unlock(&pool->mutex); return true; }