Skip to content

Commit

Permalink
Prevent Clippy library from segfaulting
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed May 6, 2023
1 parent 1e1c3b0 commit 3db9dda
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions driver/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/trailofbits/dylint"

[dependencies]
anyhow = "1.0"
libc = "0.2"
libloading = "0.8"
log = "0.4"
rustversion = "1.0"
Expand Down
22 changes: 21 additions & 1 deletion driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,33 @@ struct Callbacks {
loaded_libs: Vec<LoadedLibrary>,
}

// smoelius: Use of thread local storage was added to Clippy by:
// https://github.com/rust-lang/rust-clippy/commit/3c06e0b1ce003912f8fe0536d3a7fe22558e38cf
// This results in a segfault after the Clippy library has been unloaded; see the following issue
// for an explanation as to why: https://github.com/nagisa/rust_libloading/issues/5
// The workaround I've chosen is:
// https://github.com/nagisa/rust_libloading/issues/5#issuecomment-244195096
impl Callbacks {
// smoelius: Load the libraries when `Callbacks` is created and not later (e.g., in `config`)
// to ensure that the libraries live long enough.
fn new(paths: Vec<PathBuf>) -> Self {
let mut loaded_libs = Vec::new();
for path in paths {
unsafe {
let lib = libloading::Library::new(&path).unwrap_or_else(|err| {
// smoelius: `libloading` does not define `RTLD_NODELETE`.
#[cfg(unix)]
let result = libloading::os::unix::Library::open(
Some(&path),
libloading::os::unix::RTLD_LAZY
| libloading::os::unix::RTLD_LOCAL
| libc::RTLD_NODELETE,
)
.map(Into::into);

#[cfg(not(unix))]
let result = libloading::Library::new(&path);

let lib = result.unwrap_or_else(|err| {
rustc_session::early_error(
rustc_session::config::ErrorOutputType::default(),
&format!(
Expand All @@ -107,6 +126,7 @@ impl Callbacks {
),
);
});

loaded_libs.push(LoadedLibrary { path, lib });
}
}
Expand Down

0 comments on commit 3db9dda

Please sign in to comment.