|
| 1 | +use std::{ |
| 2 | + ffi::{c_char, CStr}, |
| 3 | + marker::PhantomData, |
| 4 | + ops::Deref, |
| 5 | + ptr::NonNull, |
| 6 | +}; |
| 7 | + |
| 8 | +use rustc_data_structures::small_c_str::SmallCStr; |
| 9 | + |
| 10 | +use crate::{errors::LlvmError, llvm}; |
| 11 | + |
| 12 | +/// Responsible for safely creating and disposing llvm::TargetMachine via ffi functions. |
| 13 | +/// Not cloneable as there is no clone function for llvm::TargetMachine. |
| 14 | +#[repr(transparent)] |
| 15 | +pub struct OwnedTargetMachine { |
| 16 | + tm_unique: NonNull<llvm::TargetMachine>, |
| 17 | + phantom: PhantomData<llvm::TargetMachine>, |
| 18 | +} |
| 19 | + |
| 20 | +impl OwnedTargetMachine { |
| 21 | + pub fn new( |
| 22 | + triple: &CStr, |
| 23 | + cpu: &CStr, |
| 24 | + features: &CStr, |
| 25 | + abi: &CStr, |
| 26 | + model: llvm::CodeModel, |
| 27 | + reloc: llvm::RelocModel, |
| 28 | + level: llvm::CodeGenOptLevel, |
| 29 | + use_soft_fp: bool, |
| 30 | + function_sections: bool, |
| 31 | + data_sections: bool, |
| 32 | + unique_section_names: bool, |
| 33 | + trap_unreachable: bool, |
| 34 | + singletree: bool, |
| 35 | + asm_comments: bool, |
| 36 | + emit_stack_size_section: bool, |
| 37 | + relax_elf_relocations: bool, |
| 38 | + use_init_array: bool, |
| 39 | + split_dwarf_file: &CStr, |
| 40 | + debug_info_compression: &CStr, |
| 41 | + force_emulated_tls: bool, |
| 42 | + args_cstr_buff: &[u8], |
| 43 | + ) -> Result<Self, LlvmError<'static>> { |
| 44 | + assert!(args_cstr_buff.len() > 0); |
| 45 | + assert!( |
| 46 | + *args_cstr_buff.last().unwrap() == 0, |
| 47 | + "The last character must be a null terminator." |
| 48 | + ); |
| 49 | + |
| 50 | + // SAFETY: llvm::LLVMRustCreateTargetMachine copies pointed to data |
| 51 | + let tm_ptr = unsafe { |
| 52 | + llvm::LLVMRustCreateTargetMachine( |
| 53 | + triple.as_ptr(), |
| 54 | + cpu.as_ptr(), |
| 55 | + features.as_ptr(), |
| 56 | + abi.as_ptr(), |
| 57 | + model, |
| 58 | + reloc, |
| 59 | + level, |
| 60 | + use_soft_fp, |
| 61 | + function_sections, |
| 62 | + data_sections, |
| 63 | + unique_section_names, |
| 64 | + trap_unreachable, |
| 65 | + singletree, |
| 66 | + asm_comments, |
| 67 | + emit_stack_size_section, |
| 68 | + relax_elf_relocations, |
| 69 | + use_init_array, |
| 70 | + split_dwarf_file.as_ptr(), |
| 71 | + debug_info_compression.as_ptr(), |
| 72 | + force_emulated_tls, |
| 73 | + args_cstr_buff.as_ptr() as *const c_char, |
| 74 | + args_cstr_buff.len(), |
| 75 | + ) |
| 76 | + }; |
| 77 | + |
| 78 | + NonNull::new(tm_ptr) |
| 79 | + .map(|tm_unique| Self { tm_unique, phantom: PhantomData }) |
| 80 | + .ok_or_else(|| LlvmError::CreateTargetMachine { triple: SmallCStr::from(triple) }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl Deref for OwnedTargetMachine { |
| 85 | + type Target = llvm::TargetMachine; |
| 86 | + |
| 87 | + fn deref(&self) -> &Self::Target { |
| 88 | + // SAFETY: constructing ensures we have a valid pointer created by llvm::LLVMRustCreateTargetMachine |
| 89 | + unsafe { self.tm_unique.as_ref() } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl Drop for OwnedTargetMachine { |
| 94 | + fn drop(&mut self) { |
| 95 | + // SAFETY: constructing ensures we have a valid pointer created by llvm::LLVMRustCreateTargetMachine |
| 96 | + // OwnedTargetMachine is not copyable so there is no double free or use after free |
| 97 | + unsafe { |
| 98 | + llvm::LLVMRustDisposeTargetMachine(self.tm_unique.as_mut()); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments