Skip to content

Commit

Permalink
deny(unused_results)
Browse files Browse the repository at this point in the history
- Add LLVM error check.
- Add read length checks.
  • Loading branch information
tamird committed Jul 28, 2023
1 parent 5fe6f8d commit c287cd5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![deny(clippy::all)]
#![deny(unused_results)]

mod linker;
mod llvm;
Expand Down
13 changes: 8 additions & 5 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub enum LinkerError {
#[error("failure linking module {1} from {0}")]
LinkArchiveModuleError(PathBuf, PathBuf),

/// Optimizing the BPF code failed.
#[error("LLVMRunPasses failed: {0}")]
OptimizeError(String),

/// Generating the BPF code failed.
#[error("LLVMTargetMachineEmitToFile failed: {0}")]
EmitCodeError(String),
Expand Down Expand Up @@ -255,7 +259,7 @@ impl Linker {

// determine whether the input is bitcode, ELF with embedded bitcode, an archive file
// or an invalid file
file.read(&mut buf)
file.read_exact(&mut buf)
.map_err(|e| LinkerError::IoError(path.clone(), e))?;
file.rewind()
.map_err(|e| LinkerError::IoError(path.clone(), e))?;
Expand Down Expand Up @@ -321,7 +325,7 @@ impl Linker {
in_type: Option<InputType>,
) -> Result<(), LinkerError> {
let mut data = Vec::new();
reader
let _: usize = reader
.read_to_end(&mut data)
.map_err(|e| LinkerError::IoError(path.to_owned(), e))?;
// in_type is unknown when we're linking an item from an archive file
Expand Down Expand Up @@ -437,9 +441,8 @@ impl Linker {
self.options.ignore_inline_never,
&self.options.export_symbols,
)
};

Ok(())
}
.map_err(LinkerError::OptimizeError)
}

fn codegen(&mut self) -> Result<(), LinkerError> {
Expand Down
23 changes: 17 additions & 6 deletions src/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use libc::c_char as libc_char;
use llvm_sys::bit_reader::*;
use llvm_sys::core::*;
use llvm_sys::debuginfo::LLVMStripModuleDebugInfo;
use llvm_sys::error::*;
use llvm_sys::linker::LLVMLinkModules2;
use llvm_sys::object::*;
use llvm_sys::prelude::*;
Expand Down Expand Up @@ -170,7 +171,7 @@ pub unsafe fn optimize(
opt_level: OptLevel,
ignore_inline_never: bool,
export_symbols: &HashSet<Cow<'static, str>>,
) {
) -> Result<(), String> {
if module_asm_is_probestack(module) {
LLVMSetModuleInlineAsm2(module, ptr::null_mut(), 0);
}
Expand All @@ -192,8 +193,6 @@ pub unsafe fn optimize(
}
}

let options = LLVMCreatePassBuilderOptions();

let passes = [
// NB: "default<_>" must be the first pass in the list, otherwise it will be ignored.
match opt_level {
Expand All @@ -214,13 +213,25 @@ pub unsafe fn optimize(
let passes = passes.join(",");
debug!("running passes: {passes}");
let passes = CString::new(passes).unwrap();
LLVMRunPasses(module, passes.as_ptr(), tm, options);

let options = LLVMCreatePassBuilderOptions();
let error = LLVMRunPasses(module, passes.as_ptr(), tm, options);
LLVMDisposePassBuilderOptions(options);
// Handle the error and print it to stderr.
if !error.is_null() {
let error_type_id = LLVMGetErrorTypeId(error);
// This is the only error type that exists currently, but there might be more in the future.
assert_eq!(error_type_id, LLVMGetStringErrorTypeId());
let error_message = LLVMGetErrorMessage(error);
let error_string = CStr::from_ptr(error_message).to_str().unwrap().to_owned();
LLVMDisposeErrorMessage(error_message);
return Err(error_string);
}

// Some debug info generated by rustc seems to trigger a segfault in the
// BTF code in llvm, so strip it until that is fixed
LLVMStripModuleDebugInfo(module);
let _modified = LLVMStripModuleDebugInfo(module) != 0;

Ok(())
}

unsafe fn module_asm_is_probestack(module: LLVMModuleRef) -> bool {
Expand Down

0 comments on commit c287cd5

Please sign in to comment.