From 96b3a3bf5c65e514de0cf230e07da84258edd486 Mon Sep 17 00:00:00 2001 From: z78078 Date: Mon, 4 Jul 2022 18:06:36 +0800 Subject: [PATCH] work around random segmentation fault (#275) Reviewed-on: https://git.m-labs.hk/M-Labs/nac3/pulls/302 Co-authored-by: z78078 Co-committed-by: z78078 --- Cargo.lock | 1 + nac3core/Cargo.toml | 1 + nac3core/src/codegen/mod.rs | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f53b5bfd..97aba67e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -521,6 +521,7 @@ dependencies = [ "inkwell", "insta", "itertools", + "lazy_static", "nac3parser", "parking_lot 0.11.2", "rayon", diff --git a/nac3core/Cargo.toml b/nac3core/Cargo.toml index ec4cd3c1..12a72484 100644 --- a/nac3core/Cargo.toml +++ b/nac3core/Cargo.toml @@ -10,6 +10,7 @@ crossbeam = "0.8.1" parking_lot = "0.11.1" rayon = "1.5.1" nac3parser = { path = "../nac3parser" } +lazy_static = "1.4.0" [dependencies.inkwell] git = "https://github.com/TheDan64/inkwell.git" diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 69596516..a7be388b 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -31,6 +31,7 @@ use std::sync::{ Arc, }; use std::thread; +use lazy_static::lazy_static; pub mod concrete_type; pub mod expr; @@ -52,6 +53,12 @@ pub struct StaticValueStore { pub type VarValue<'ctx> = (PointerValue<'ctx>, Option>, i64); +lazy_static!( + // HACK: The Mutex is a work-around for issue + // https://git.m-labs.hk/M-Labs/nac3/issues/275 + static ref PASSES_INIT_LOCK: Mutex = Mutex::new(AtomicBool::new(true)); +); + pub struct CodeGenContext<'ctx, 'a> { pub ctx: &'ctx Context, pub builder: Builder<'ctx>, @@ -218,10 +225,16 @@ impl WorkerRegistry { context.i32_type().const_int(4, false), ); - let pass_builder = PassManagerBuilder::create(); - pass_builder.set_optimization_level(OptimizationLevel::Default); let passes = PassManager::create(&module); - pass_builder.populate_function_pass_manager(&passes); + + // HACK: This critical section is a work-around for issue + // https://git.m-labs.hk/M-Labs/nac3/issues/275 + { + let _data = PASSES_INIT_LOCK.lock(); + let pass_builder = PassManagerBuilder::create(); + pass_builder.set_optimization_level(OptimizationLevel::Default); + pass_builder.populate_function_pass_manager(&passes); + } let mut errors = HashSet::new(); while let Some(task) = self.receiver.recv().unwrap() {