From 1caa593cb6bd9a33eb625ad313010d2b141dea5c Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Sat, 14 Jul 2018 20:34:32 +0200 Subject: Expose a self-referential object --- src/librustc_codegen_llvm/back/write.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/librustc_codegen_llvm') diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs index 467782518f6..0ffb83ff337 100644 --- a/src/librustc_codegen_llvm/back/write.rs +++ b/src/librustc_codegen_llvm/back/write.rs @@ -399,6 +399,9 @@ impl CodegenContext { } struct DiagnosticHandlers<'a> { + #[allow(dead_code)] + // This value is not actually dead, llcx has pointers to it and needs these pointers to be alive + // until Drop is executed on this object inner: Box<(&'a CodegenContext, &'a Handler)>, llcx: ContextRef, } -- cgit 1.4.1-3-g733a5 From ecab96fd7ce9991218145a9e0063ef3c25c7a977 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Sun, 15 Jul 2018 01:52:45 +0200 Subject: Ubsan this newly discovered dead code --- src/librustc_codegen_llvm/back/write.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'src/librustc_codegen_llvm') diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs index 0ffb83ff337..d36142af56c 100644 --- a/src/librustc_codegen_llvm/back/write.rs +++ b/src/librustc_codegen_llvm/back/write.rs @@ -399,10 +399,7 @@ impl CodegenContext { } struct DiagnosticHandlers<'a> { - #[allow(dead_code)] - // This value is not actually dead, llcx has pointers to it and needs these pointers to be alive - // until Drop is executed on this object - inner: Box<(&'a CodegenContext, &'a Handler)>, + data: *mut (&'a CodegenContext, &'a Handler), llcx: ContextRef, } @@ -410,24 +407,22 @@ impl<'a> DiagnosticHandlers<'a> { fn new(cgcx: &'a CodegenContext, handler: &'a Handler, llcx: ContextRef) -> DiagnosticHandlers<'a> { - let data = Box::new((cgcx, handler)); + let data = Box::into_raw(Box::new((cgcx, handler))); unsafe { - let arg = &*data as &(_, _) as *const _ as *mut _; - llvm::LLVMRustSetInlineAsmDiagnosticHandler(llcx, inline_asm_handler, arg); - llvm::LLVMContextSetDiagnosticHandler(llcx, diagnostic_handler, arg); - } - DiagnosticHandlers { - inner: data, - llcx: llcx, + llvm::LLVMRustSetInlineAsmDiagnosticHandler(llcx, inline_asm_handler, data as *mut _); + llvm::LLVMContextSetDiagnosticHandler(llcx, diagnostic_handler, data as *mut _); } + DiagnosticHandlers { data, llcx } } } impl<'a> Drop for DiagnosticHandlers<'a> { fn drop(&mut self) { + use std::ptr::null_mut; unsafe { - llvm::LLVMRustSetInlineAsmDiagnosticHandler(self.llcx, inline_asm_handler, 0 as *mut _); - llvm::LLVMContextSetDiagnosticHandler(self.llcx, diagnostic_handler, 0 as *mut _); + llvm::LLVMRustSetInlineAsmDiagnosticHandler(self.llcx, inline_asm_handler, null_mut()); + llvm::LLVMContextSetDiagnosticHandler(self.llcx, diagnostic_handler, null_mut()); + drop(Box::from_raw(self.data)); } } } -- cgit 1.4.1-3-g733a5