Skip to content

Commit 38c7c59

Browse files
authored
Merge pull request #4697 from michaliskok/genmc-llvm-dep
genmc: Build without LLVM; adjust to API changes
2 parents e110b81 + 89a97b9 commit 38c7c59

File tree

8 files changed

+53
-80
lines changed

8 files changed

+53
-80
lines changed

genmc-sys/build.rs

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod downloading {
2828
/// The GenMC repository the we get our commit from.
2929
pub(crate) const GENMC_GITHUB_URL: &str = "https://github.com/MPI-SWS/genmc.git";
3030
/// The GenMC commit we depend on. It must be available on the specified GenMC repository.
31-
pub(crate) const GENMC_COMMIT: &str = "d9527280bb99f1cef64326b1803ffd952e3880df";
31+
pub(crate) const GENMC_COMMIT: &str = "aa10ed65117c3291524efc19253b5d443a4602ac";
3232

3333
/// Ensure that a local GenMC repo is present and set to the correct commit.
3434
/// Return the path of the GenMC repo and whether the checked out commit was changed.
@@ -140,51 +140,6 @@ mod downloading {
140140
}
141141
}
142142

143-
// FIXME(genmc,llvm): Remove once the LLVM dependency of the GenMC model checker is removed.
144-
/// The linked LLVM version is in the generated `config.h`` file, which we parse and use to link to LLVM.
145-
/// Returns c++ compiler definitions required for building with/including LLVM, and the include path for LLVM headers.
146-
fn link_to_llvm(config_file: &Path) -> (String, String) {
147-
/// Search a string for a line matching `//@VARIABLE_NAME: VARIABLE CONTENT`
148-
fn extract_value<'a>(input: &'a str, name: &str) -> Option<&'a str> {
149-
input
150-
.lines()
151-
.find_map(|line| line.strip_prefix("//@")?.strip_prefix(name)?.strip_prefix(": "))
152-
}
153-
154-
let file_content = std::fs::read_to_string(&config_file).unwrap_or_else(|err| {
155-
panic!("GenMC config file ({}) should exist, but got errror {err:?}", config_file.display())
156-
});
157-
158-
let llvm_definitions = extract_value(&file_content, "LLVM_DEFINITIONS")
159-
.expect("Config file should contain LLVM_DEFINITIONS");
160-
let llvm_include_dirs = extract_value(&file_content, "LLVM_INCLUDE_DIRS")
161-
.expect("Config file should contain LLVM_INCLUDE_DIRS");
162-
let llvm_library_dir = extract_value(&file_content, "LLVM_LIBRARY_DIR")
163-
.expect("Config file should contain LLVM_LIBRARY_DIR");
164-
let llvm_config_path = extract_value(&file_content, "LLVM_CONFIG_PATH")
165-
.expect("Config file should contain LLVM_CONFIG_PATH");
166-
167-
// Add linker search path.
168-
let lib_dir = PathBuf::from_str(llvm_library_dir).unwrap();
169-
println!("cargo::rustc-link-search=native={}", lib_dir.display());
170-
171-
// Add libraries to link.
172-
let output = std::process::Command::new(llvm_config_path)
173-
.arg("--libs") // Print the libraries to link to (space-separated list)
174-
.output()
175-
.expect("failed to execute llvm-config");
176-
let llvm_link_libs =
177-
String::try_from(output.stdout).expect("llvm-config output should be a valid string");
178-
179-
for link_lib in llvm_link_libs.trim().split(" ") {
180-
let link_lib =
181-
link_lib.strip_prefix("-l").expect("Linker parameter should start with \"-l\"");
182-
println!("cargo::rustc-link-lib=dylib={link_lib}");
183-
}
184-
185-
(llvm_definitions.to_string(), llvm_include_dirs.to_string())
186-
}
187-
188143
/// Build the GenMC model checker library and the Rust-C++ interop library with cxx.rs
189144
fn compile_cpp_dependencies(genmc_path: &Path, always_configure: bool) {
190145
// Give each step a separate build directory to prevent interference.
@@ -204,6 +159,7 @@ fn compile_cpp_dependencies(genmc_path: &Path, always_configure: bool) {
204159
.always_configure(always_configure) // We force running the configure step when the GenMC commit changed.
205160
.out_dir(genmc_build_dir)
206161
.profile(GENMC_CMAKE_PROFILE)
162+
.define("BUILD_LLI", "OFF")
207163
.define("GENMC_DEBUG", if enable_genmc_debug { "ON" } else { "OFF" });
208164

209165
// The actual compilation happens here:
@@ -214,19 +170,11 @@ fn compile_cpp_dependencies(genmc_path: &Path, always_configure: bool) {
214170
println!("cargo::rustc-link-search=native={}", cmake_lib_dir.display());
215171
println!("cargo::rustc-link-lib=static={GENMC_MODEL_CHECKER}");
216172

217-
// FIXME(genmc,llvm): Remove once the LLVM dependency of the GenMC model checker is removed.
218-
let config_file = genmc_install_dir.join("include").join("genmc").join("config.h");
219-
let (llvm_definitions, llvm_include_dirs) = link_to_llvm(&config_file);
220-
221173
// Part 2:
222174
// Compile the cxx_bridge (the link between the Rust and C++ code).
223175

224176
let genmc_include_dir = genmc_install_dir.join("include").join("genmc");
225177

226-
// FIXME(genmc,llvm): remove once LLVM dependency is removed.
227-
// These definitions are parsed into a cmake list and then printed to the config.h file, so they are ';' separated.
228-
let definitions = llvm_definitions.split(";");
229-
230178
// These are all the C++ files we need to compile, which needs to be updated if more C++ files are added to Miri.
231179
// We use absolute paths since relative paths can confuse IDEs when attempting to go-to-source on a path in a compiler error.
232180
let cpp_files_base_path = Path::new("cpp/src/");
@@ -244,16 +192,12 @@ fn compile_cpp_dependencies(genmc_path: &Path, always_configure: bool) {
244192
if enable_genmc_debug {
245193
bridge.define("ENABLE_GENMC_DEBUG", None);
246194
}
247-
for definition in definitions {
248-
bridge.flag(definition);
249-
}
250195
bridge
251196
.opt_level(2)
252197
.debug(true) // Same settings that GenMC uses (default for cmake `RelWithDebInfo`)
253198
.warnings(false) // NOTE: enabling this produces a lot of warnings.
254199
.std("c++23")
255200
.include(genmc_include_dir)
256-
.include(llvm_include_dirs)
257201
.include("./cpp/include")
258202
.files(&cpp_files)
259203
.out_dir(interface_build_dir)

genmc-sys/cpp/include/MiriInterface.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ struct MiriGenmcShim : private GenMCDriver {
220220
auto handle_load_reset_if_none(ThreadId tid, std::optional<SVal> old_val, Ts&&... params)
221221
-> HandleResult<SVal> {
222222
const auto pos = inc_pos(tid);
223-
const auto ret = GenMCDriver::handleLoad<k>(pos, old_val, std::forward<Ts>(params)...);
223+
const auto ret =
224+
GenMCDriver::handleLoad<k>(nullptr, pos, old_val, std::forward<Ts>(params)...);
224225
// If we didn't get a value, we have to reset the index of the current thread.
225226
if (!std::holds_alternative<SVal>(ret)) {
226227
dec_pos(tid);

genmc-sys/cpp/include/ResultHandling.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
// GenMC headers:
88
#include "Verification/VerificationError.hpp"
99

10+
#include <format>
11+
#include <memory>
12+
#include <sstream>
1013
#include <string>
1114

1215
/** Information about an error, formatted as a string to avoid having to share an error enum and
1316
* printing functionality with the Rust side. */
1417
static auto format_error(VerificationError err) -> std::unique_ptr<std::string> {
15-
auto buf = std::string();
16-
auto s = llvm::raw_string_ostream(buf);
17-
s << err;
18+
std::stringstream s;
19+
s << std::format("{}", err);
1820
return std::make_unique<std::string>(s.str());
1921
}
2022

genmc-sys/cpp/src/MiriInterface/EventHandling.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
void MiriGenmcShim::handle_assume_block(ThreadId thread_id, AssumeType assume_type) {
3636
BUG_ON(getExec().getGraph().isThreadBlocked(thread_id));
37-
GenMCDriver::handleAssume(inc_pos(thread_id), assume_type);
37+
GenMCDriver::handleAssume(nullptr, inc_pos(thread_id), assume_type);
3838
}
3939

4040
/**** Memory access handling ****/
@@ -76,6 +76,7 @@ void MiriGenmcShim::handle_assume_block(ThreadId thread_id, AssumeType assume_ty
7676
) -> StoreResult {
7777
const auto pos = inc_pos(thread_id);
7878
const auto ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::Write>(
79+
nullptr,
7980
pos,
8081
GenmcScalarExt::try_to_sval(old_val),
8182
ord,
@@ -100,7 +101,7 @@ void MiriGenmcShim::handle_assume_block(ThreadId thread_id, AssumeType assume_ty
100101

101102
void MiriGenmcShim::handle_fence(ThreadId thread_id, MemOrdering ord) {
102103
const auto pos = inc_pos(thread_id);
103-
GenMCDriver::handleFence(pos, ord, EventDeps());
104+
GenMCDriver::handleFence(nullptr, pos, ord, EventDeps());
104105
}
105106

106107
[[nodiscard]] auto MiriGenmcShim::handle_read_modify_write(
@@ -143,6 +144,7 @@ void MiriGenmcShim::handle_fence(ThreadId thread_id, MemOrdering ord) {
143144

144145
const auto storePos = inc_pos(thread_id);
145146
const auto store_ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::FaiWrite>(
147+
nullptr,
146148
storePos,
147149
GenmcScalarExt::try_to_sval(old_val),
148150
ordering,
@@ -210,6 +212,7 @@ void MiriGenmcShim::handle_fence(ThreadId thread_id, MemOrdering ord) {
210212

211213
const auto storePos = inc_pos(thread_id);
212214
const auto store_ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::CasWrite>(
215+
nullptr,
213216
storePos,
214217
GenmcScalarExt::try_to_sval(old_val),
215218
success_ordering,
@@ -242,6 +245,7 @@ auto MiriGenmcShim::handle_malloc(ThreadId thread_id, uint64_t size, uint64_t al
242245
const auto address_space = AddressSpace::AS_User;
243246

244247
const SVal ret_val = GenMCDriver::handleMalloc(
248+
nullptr,
245249
pos,
246250
size,
247251
alignment,
@@ -255,7 +259,7 @@ auto MiriGenmcShim::handle_malloc(ThreadId thread_id, uint64_t size, uint64_t al
255259

256260
auto MiriGenmcShim::handle_free(ThreadId thread_id, uint64_t address) -> bool {
257261
const auto pos = inc_pos(thread_id);
258-
GenMCDriver::handleFree(pos, SAddr(address), EventDeps());
262+
GenMCDriver::handleFree(nullptr, pos, SAddr(address), EventDeps());
259263
// FIXME(genmc): use returned error from `handleFree` once implemented in GenMC.
260264
return getResult().status.has_value();
261265
}

genmc-sys/cpp/src/MiriInterface/Exploration.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@ auto MiriGenmcShim::schedule_next(
2222
// a scheduling decision.
2323
threads_action_[curr_thread_id].kind = curr_thread_next_instr_kind;
2424

25-
if (const auto result = GenMCDriver::scheduleNext(threads_action_))
26-
return SchedulingResult { ExecutionState::Ok, static_cast<int32_t>(result.value()) };
27-
if (getExec().getGraph().isBlocked())
28-
return SchedulingResult { ExecutionState::Blocked, 0 };
29-
if (getResult().status.has_value()) // the "value" here is a `VerificationError`
30-
return SchedulingResult { ExecutionState::Error, 0 };
31-
return SchedulingResult { ExecutionState::Finished, 0 };
25+
auto result = GenMCDriver::scheduleNext(threads_action_);
26+
return std::visit(
27+
[](auto&& arg) {
28+
using T = std::decay_t<decltype(arg)>;
29+
if constexpr (std::is_same_v<T, int>)
30+
return SchedulingResult { ExecutionState::Ok, static_cast<int32_t>(arg) };
31+
else if constexpr (std::is_same_v<T, Blocked>)
32+
return SchedulingResult { ExecutionState::Blocked, 0 };
33+
else if constexpr (std::is_same_v<T, Error>)
34+
return SchedulingResult { ExecutionState::Error, 0 };
35+
else if constexpr (std::is_same_v<T, Finished>)
36+
return SchedulingResult { ExecutionState::Finished, 0 };
37+
else
38+
static_assert(false, "non-exhaustive visitor!");
39+
},
40+
result
41+
);
3242
}
3343

3444
/**** Execution start/end handling ****/

genmc-sys/cpp/src/MiriInterface/Mutex.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ auto MiriGenmcShim::handle_mutex_lock(ThreadId thread_id, uint64_t address, uint
6060
const bool is_lock_acquired = *ret_val == MUTEX_UNLOCKED;
6161
if (is_lock_acquired) {
6262
const auto store_ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::LockCasWrite>(
63+
nullptr,
6364
inc_pos(thread_id),
6465
old_val,
6566
address,
@@ -93,6 +94,7 @@ auto MiriGenmcShim::handle_mutex_try_lock(ThreadId thread_id, uint64_t address,
9394
// a mutex to be "unlocked".
9495
const auto old_val = MUTEX_UNLOCKED;
9596
const auto load_ret = GenMCDriver::handleLoad<EventLabel::EventLabelKind::TrylockCasRead>(
97+
nullptr,
9698
++currPos,
9799
old_val,
98100
SAddr(address),
@@ -115,6 +117,7 @@ auto MiriGenmcShim::handle_mutex_try_lock(ThreadId thread_id, uint64_t address,
115117
}
116118

117119
const auto store_ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::TrylockCasWrite>(
120+
nullptr,
118121
++currPos,
119122
old_val,
120123
SAddr(address),
@@ -136,6 +139,7 @@ auto MiriGenmcShim::handle_mutex_unlock(ThreadId thread_id, uint64_t address, ui
136139
-> StoreResult {
137140
const auto pos = inc_pos(thread_id);
138141
const auto ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::UnlockWrite>(
142+
nullptr,
139143
pos,
140144
// As usual, we need to tell GenMC which value was stored at this location before this
141145
// atomic access, if there previously was a non-atomic initializing access. We set the

genmc-sys/cpp/src/MiriInterface/Setup.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,16 @@ static auto to_genmc_verbosity_level(const LogLevel log_level) -> VerbosityLevel
147147
// that is allowed to leak and memory that is not.
148148
conf->warnUnfreedMemory = false;
149149

150-
// FIXME(genmc,error handling): This function currently exits on error, but will return an
151-
// error value in the future. The return value should be checked once this change is made.
152-
checkConfig(*conf);
150+
// Validate the config and exit if there are any errors
151+
std::vector<std::string> warnings;
152+
auto config_valid = conf->validate(warnings);
153+
for (const auto& w : warnings)
154+
WARN("{}", w);
155+
if (auto* errors = std::get_if<ConfigErrorList>(&config_valid); errors) {
156+
for (const auto& e : *errors)
157+
LOG(VerbosityLevel::Error, "{}", e);
158+
exit(EUSER);
159+
}
153160

154161
// Create the actual driver and Miri-GenMC communication shim.
155162
auto driver = std::make_unique<MiriGenmcShim>(std::move(conf), mode);

genmc-sys/cpp/src/MiriInterface/ThreadManagement.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ void MiriGenmcShim::handle_thread_create(ThreadId thread_id, ThreadId parent_id)
1919
// FIXME(genmc): for supporting symmetry reduction, these will need to be properly set:
2020
const unsigned fun_id = 0;
2121
const SVal arg = SVal(0);
22-
const ThreadInfo child_info = ThreadInfo { thread_id, parent_id, fun_id, arg };
22+
const ThreadInfo child_info =
23+
ThreadInfo { thread_id, parent_id, fun_id, arg, "unknown thread" };
2324

2425
// NOTE: Default memory ordering (`Release`) used here.
25-
const auto child_tid = GenMCDriver::handleThreadCreate(pos, child_info, EventDeps());
26+
const auto child_tid = GenMCDriver::handleThreadCreate(nullptr, pos, child_info, EventDeps());
2627
// Sanity check the thread id, which is the index in the `threads_action_` array.
2728
BUG_ON(child_tid != thread_id || child_tid <= 0 || child_tid != threads_action_.size());
2829
threads_action_.push_back(Action(ActionKind::Load, Event(child_tid, 0)));
@@ -33,7 +34,7 @@ void MiriGenmcShim::handle_thread_join(ThreadId thread_id, ThreadId child_id) {
3334
const auto pos = inc_pos(thread_id);
3435

3536
// NOTE: Default memory ordering (`Acquire`) used here.
36-
const auto ret = GenMCDriver::handleThreadJoin(pos, child_id, EventDeps());
37+
const auto ret = GenMCDriver::handleThreadJoin(nullptr, pos, child_id, EventDeps());
3738
// If the join failed, decrease the event index again:
3839
if (!std::holds_alternative<SVal>(ret)) {
3940
dec_pos(thread_id);
@@ -46,10 +47,10 @@ void MiriGenmcShim::handle_thread_join(ThreadId thread_id, ThreadId child_id) {
4647
void MiriGenmcShim::handle_thread_finish(ThreadId thread_id, uint64_t ret_val) {
4748
const auto pos = inc_pos(thread_id);
4849
// NOTE: Default memory ordering (`Release`) used here.
49-
GenMCDriver::handleThreadFinish(pos, SVal(ret_val));
50+
GenMCDriver::handleThreadFinish(nullptr, pos, SVal(ret_val));
5051
}
5152

5253
void MiriGenmcShim::handle_thread_kill(ThreadId thread_id) {
5354
const auto pos = inc_pos(thread_id);
54-
GenMCDriver::handleThreadKill(pos);
55+
GenMCDriver::handleThreadKill(nullptr, pos);
5556
}

0 commit comments

Comments
 (0)