Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move diagnostic handler to a separate struct #224

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub struct Linker {
context: LLVMContextRef,
module: LLVMModuleRef,
target_machine: LLVMTargetMachineRef,
has_errors: bool,
diagnostic_handler: DiagnosticHandler,
}

impl Linker {
Expand All @@ -240,7 +240,7 @@ impl Linker {
context: ptr::null_mut(),
module: ptr::null_mut(),
target_machine: ptr::null_mut(),
has_errors: false,
diagnostic_handler: DiagnosticHandler::new(),
}
}

Expand Down Expand Up @@ -270,7 +270,7 @@ impl Linker {
}

pub fn has_errors(&self) -> bool {
self.has_errors
self.diagnostic_handler.has_errors
}

fn link_modules(&mut self) -> Result<(), LinkerError> {
Expand Down Expand Up @@ -537,8 +537,8 @@ impl Linker {
self.context = LLVMContextCreate();
LLVMContextSetDiagnosticHandler(
self.context,
Some(llvm::diagnostic_handler::<Self>),
self as *mut _ as _,
Some(llvm::diagnostic_handler::<DiagnosticHandler>),
&mut self.diagnostic_handler as *mut _ as _,
);
LLVMInstallFatalErrorHandler(Some(llvm::fatal_error));
LLVMEnablePrettyStackTrace();
Expand All @@ -551,7 +551,33 @@ impl Linker {
}
}

impl llvm::LLVMDiagnosticHandler for Linker {
impl Drop for Linker {
fn drop(&mut self) {
unsafe {
if !self.target_machine.is_null() {
LLVMDisposeTargetMachine(self.target_machine);
}
if !self.module.is_null() {
LLVMDisposeModule(self.module);
}
if !self.context.is_null() {
LLVMContextDispose(self.context);
}
}
}
}

pub struct DiagnosticHandler {
pub(crate) has_errors: bool,
}

impl DiagnosticHandler {
pub fn new() -> Self {
Self { has_errors: false }
}
}

impl llvm::LLVMDiagnosticHandler for DiagnosticHandler {
fn handle_diagnostic(&mut self, severity: llvm_sys::LLVMDiagnosticSeverity, message: &str) {
// TODO(https://reviews.llvm.org/D155894): Remove this when LLVM no longer emits these
// errors.
Expand Down Expand Up @@ -582,22 +608,6 @@ impl llvm::LLVMDiagnosticHandler for Linker {
}
}

impl Drop for Linker {
fn drop(&mut self) {
unsafe {
if !self.target_machine.is_null() {
LLVMDisposeTargetMachine(self.target_machine);
}
if !self.module.is_null() {
LLVMDisposeModule(self.module);
}
if !self.context.is_null() {
LLVMContextDispose(self.context);
}
}
}
}

fn detect_input_type(data: &[u8]) -> Option<InputType> {
if data.len() < 8 {
return None;
Expand Down
27 changes: 12 additions & 15 deletions src/llvm/di.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ use gimli::{DW_TAG_pointer_type, DW_TAG_structure_type, DW_TAG_variant_part};
use llvm_sys::{core::*, debuginfo::*, prelude::*};
use tracing::{span, trace, warn, Level};

use super::types::{
di::DIType,
ir::{Function, MDNode, Metadata, Value},
use crate::llvm::{
iter::*,
types::{
di::{DISubprogram, DIType},
ir::{Function, MDNode, Metadata, Value},
LLVMTypeWrapper,
},
};
use crate::llvm::{iter::*, types::di::DISubprogram};

// KSYM_NAME_LEN from linux kernel intentionally set
// to lower value found accross kernel versions to ensure
Expand All @@ -26,7 +29,6 @@ pub struct DISanitizer {
module: LLVMModuleRef,
builder: LLVMDIBuilderRef,
visited_nodes: HashSet<u64>,
item_stack: Vec<Item>,
replace_operands: HashMap<u64, LLVMMetadataRef>,
skipped_types: Vec<String>,
}
Expand Down Expand Up @@ -66,7 +68,6 @@ impl DISanitizer {
module,
builder: unsafe { LLVMCreateDIBuilder(module) },
visited_nodes: HashSet::new(),
item_stack: Vec::new(),
replace_operands: HashMap::new(),
skipped_types: Vec::new(),
}
Expand Down Expand Up @@ -229,7 +230,7 @@ impl DISanitizer {
// An operand with no value is valid and means that the operand is
// not set
(v, Item::Operand { .. }) if v.is_null() => return,
(v, _) if !v.is_null() => Value::new(v),
(v, _) if !v.is_null() => unsafe { Value::from_ptr(v) },
// All other items should have values
(_, item) => panic!("{item:?} has no value"),
};
Expand All @@ -249,8 +250,6 @@ impl DISanitizer {
return;
}

self.item_stack.push(item.clone());

if let Value::MDNode(mdnode) = value.clone() {
self.visit_mdnode(mdnode)
}
Expand Down Expand Up @@ -285,8 +284,6 @@ impl DISanitizer {
}
}
}

let _ = self.item_stack.pop().unwrap();
}

pub fn run(mut self, exported_symbols: &HashSet<Cow<'static, str>>) {
Expand Down Expand Up @@ -337,7 +334,7 @@ impl DISanitizer {
for mut function in self
.module
.functions_iter()
.map(|value| unsafe { Function::from_value_ref(value) })
.map(|value| unsafe { Function::from_ptr(value) })
{
if export_symbols.contains(function.name()) {
continue;
Expand Down Expand Up @@ -376,7 +373,7 @@ impl DISanitizer {
// replace retained nodes manually below.
LLVMDIBuilderFinalizeSubprogram(self.builder, new_program);

DISubprogram::from_value_ref(LLVMMetadataAsValue(self.context, new_program))
DISubprogram::from_ptr(LLVMMetadataAsValue(self.context, new_program))
};

// Point the function to the new subprogram.
Expand All @@ -402,8 +399,8 @@ impl DISanitizer {
unsafe { LLVMMDNodeInContext2(self.context, core::ptr::null_mut(), 0) };
subprogram.set_retained_nodes(empty_node);

let ret = replace.insert(subprogram.value_ref as u64, unsafe {
LLVMValueAsMetadata(new_program.value_ref)
let ret = replace.insert(subprogram.as_ptr() as u64, unsafe {
LLVMValueAsMetadata(new_program.as_ptr())
});
assert!(ret.is_none());
}
Expand Down
Loading
Loading