1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
use libc::c_int; use llvm_sys::execution_engine::{LLVMGetExecutionEngineTargetData, LLVMExecutionEngineRef, LLVMRunFunction, LLVMRunFunctionAsMain, LLVMDisposeExecutionEngine, LLVMGetFunctionAddress, LLVMAddModule, LLVMFindFunction, LLVMLinkInMCJIT, LLVMLinkInInterpreter, LLVMRemoveModule, LLVMGenericValueRef, LLVMFreeMachineCodeForFunction, LLVMAddGlobalMapping, LLVMRunStaticConstructors, LLVMRunStaticDestructors}; use crate::context::Context; use crate::module::Module; use crate::support::LLVMString; use crate::targets::TargetData; use crate::values::{AnyValue, AsValueRef, FunctionValue, GenericValue}; use std::error::Error; use std::rc::Rc; use std::ops::Deref; use std::ffi::CString; use std::fmt::{self, Debug, Display, Formatter}; use std::mem::{forget, zeroed, transmute_copy, size_of}; static EE_INNER_PANIC: &str = "ExecutionEngineInner should exist until Drop"; #[derive(Debug, PartialEq, Eq)] pub enum FunctionLookupError { JITNotEnabled, FunctionNotFound, // 404! } impl Error for FunctionLookupError {} impl FunctionLookupError { fn as_str(&self) -> &str { match self { FunctionLookupError::JITNotEnabled => "ExecutionEngine does not have JIT functionality enabled", FunctionLookupError::FunctionNotFound => "Function not found in ExecutionEngine", } } } impl Display for FunctionLookupError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "FunctionLookupError({})", self.as_str()) } } #[derive(Debug, PartialEq, Eq)] pub enum RemoveModuleError { ModuleNotOwned, IncorrectModuleOwner, LLVMError(LLVMString), } impl Error for RemoveModuleError { // This method is deprecated on nighty so it's probably not // something we should worry about fn description(&self) -> &str { self.as_str() } fn cause(&self) -> Option<&dyn Error> { None } } impl RemoveModuleError { fn as_str(&self) -> &str { match self { RemoveModuleError::ModuleNotOwned => "Module is not owned by an Execution Engine", RemoveModuleError::IncorrectModuleOwner => "Module is not owned by this Execution Engine", RemoveModuleError::LLVMError(string) => string.to_str().unwrap_or("LLVMError with invalid unicode"), } } } impl Display for RemoveModuleError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "RemoveModuleError({})", self.as_str()) } } /// A reference-counted wrapper around LLVM's execution engine. /// /// # Note /// /// Cloning this object is essentially just a case of copying a couple pointers /// and incrementing one or two atomics, so this should be quite cheap to create /// copies. The underlying LLVM object will be automatically deallocated when /// there are no more references to it. // non_global_context is required to ensure last remaining Context ref will drop // after EE drop. execution_engine & target_data are an option for drop purposes #[derive(PartialEq, Eq, Debug)] pub struct ExecutionEngine { non_global_context: Option<Context>, execution_engine: Option<ExecEngineInner>, target_data: Option<TargetData>, jit_mode: bool, } impl ExecutionEngine { pub(crate) fn new( execution_engine: Rc<LLVMExecutionEngineRef>, non_global_context: Option<Context>, jit_mode: bool, ) -> ExecutionEngine { assert!(!execution_engine.is_null()); // REVIEW: Will we have to do this for LLVMGetExecutionEngineTargetMachine too? let target_data = unsafe { LLVMGetExecutionEngineTargetData(*execution_engine) }; ExecutionEngine { non_global_context, execution_engine: Some(ExecEngineInner(execution_engine)), target_data: Some(TargetData::new(target_data)), jit_mode: jit_mode, } } pub(crate) fn execution_engine_rc(&self) -> &Rc<LLVMExecutionEngineRef> { &self.execution_engine.as_ref().expect(EE_INNER_PANIC).0 } #[inline] pub(crate) fn execution_engine_inner(&self) -> LLVMExecutionEngineRef { **self.execution_engine_rc() } /// This function probably doesn't need to be called, but is here due to /// linking(?) requirements. Bad things happen if we don't provide it. pub fn link_in_mc_jit() { unsafe { LLVMLinkInMCJIT() } } /// This function probably doesn't need to be called, but is here due to /// linking(?) requirements. Bad things happen if we don't provide it. pub fn link_in_interpreter() { unsafe { LLVMLinkInInterpreter(); } } /// Maps the specified value to an address. /// /// # Example /// ```no_run /// use inkwell::targets::{InitializationConfig, Target}; /// use inkwell::context::Context; /// use inkwell::OptimizationLevel; /// /// Target::initialize_native(&InitializationConfig::default()).unwrap(); /// /// extern fn sumf(a: f64, b: f64) -> f64 { /// a + b /// } /// /// let context = Context::create(); /// let module = context.create_module("test"); /// let builder = context.create_builder(); /// /// let ft = context.f64_type(); /// let fnt = ft.fn_type(&[], false); /// /// let f = module.add_function("test_fn", fnt, None); /// let b = context.append_basic_block(&f, "entry"); /// /// builder.position_at_end(&b); /// /// let extf = module.add_function("sumf", ft.fn_type(&[ft.into(), ft.into()], false), None); /// /// let argf = ft.const_float(64.); /// let call_site_value = builder.build_call(extf, &[argf.into(), argf.into()], "retv"); /// let retv = call_site_value.try_as_basic_value().left().unwrap().into_float_value(); /// /// builder.build_return(Some(&retv)); /// /// let mut ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap(); /// ee.add_global_mapping(&extf, sumf as usize); /// /// let result = unsafe { ee.run_function(&f, &[]) }.as_float(&ft); /// /// assert_eq!(result, 128.); /// ``` pub fn add_global_mapping(&self, value: &dyn AnyValue, addr: usize) { unsafe { LLVMAddGlobalMapping(self.execution_engine_inner(), value.as_value_ref(), addr as *mut _) } } /// Adds a module to an `ExecutionEngine`. /// /// The method will be `Ok(())` if the module does not belong to an `ExecutionEngine` already and `Err(())` otherwise. /// /// ```rust,no_run /// use inkwell::targets::{InitializationConfig, Target}; /// use inkwell::context::Context; /// use inkwell::OptimizationLevel; /// /// Target::initialize_native(&InitializationConfig::default()).unwrap(); /// /// let context = Context::create(); /// let module = context.create_module("test"); /// let mut ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap(); /// /// assert!(ee.add_module(&module).is_err()); /// ``` pub fn add_module(&self, module: &Module) -> Result<(), ()> { unsafe { LLVMAddModule(self.execution_engine_inner(), module.module.get()) } if module.owned_by_ee.borrow().is_some() { return Err(()); } *module.owned_by_ee.borrow_mut() = Some(self.clone()); Ok(()) } pub fn remove_module(&self, module: &Module) -> Result<(), RemoveModuleError> { match *module.owned_by_ee.borrow() { Some(ref ee) if ee.execution_engine_inner() != self.execution_engine_inner() => return Err(RemoveModuleError::IncorrectModuleOwner), None => return Err(RemoveModuleError::ModuleNotOwned), _ => () } let mut new_module = unsafe { zeroed() }; let mut err_string = unsafe { zeroed() }; let code = unsafe { LLVMRemoveModule(self.execution_engine_inner(), module.module.get(), &mut new_module, &mut err_string) }; if code == 1 { return Err(RemoveModuleError::LLVMError(LLVMString::new(err_string))); } module.module.set(new_module); *module.owned_by_ee.borrow_mut() = None; Ok(()) } /// Try to load a function from the execution engine. /// /// If a target hasn't already been initialized, spurious "function not /// found" errors may be encountered. /// /// The [`UnsafeFunctionPointer`] trait is designed so only `unsafe extern /// "C"` functions can be retrieved via the `get_function()` method. If you /// get funny type errors then it's probably because you have specified the /// wrong calling convention or forgotten to specify the retrieved function /// as `unsafe`. /// /// # Examples /// /// /// ```rust,no_run /// # use inkwell::targets::{InitializationConfig, Target}; /// # use inkwell::context::Context; /// # use inkwell::OptimizationLevel; /// # Target::initialize_native(&InitializationConfig::default()).unwrap(); /// let context = Context::create(); /// let module = context.create_module("test"); /// let builder = context.create_builder(); /// /// // Set up the function signature /// let double = context.f64_type(); /// let sig = double.fn_type(&[], false); /// /// // Add the function to our module /// let f = module.add_function("test_fn", sig, None); /// let b = context.append_basic_block(&f, "entry"); /// builder.position_at_end(&b); /// /// // Insert a return statement /// let ret = double.const_float(64.0); /// builder.build_return(Some(&ret)); /// /// // create the JIT engine /// let mut ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap(); /// /// // fetch our JIT'd function and execute it /// unsafe { /// let test_fn = ee.get_function::<unsafe extern "C" fn() -> f64>("test_fn").unwrap(); /// let return_value = test_fn.call(); /// assert_eq!(return_value, 64.0); /// } /// ``` /// /// # Safety /// /// It is the caller's responsibility to ensure they call the function with /// the correct signature and calling convention. /// /// The `JitFunction` wrapper ensures a function won't accidentally outlive the /// execution engine it came from, but adding functions after calling this /// method *may* invalidate the function pointer. /// /// [`UnsafeFunctionPointer`]: trait.UnsafeFunctionPointer.html pub unsafe fn get_function<F>(&self, fn_name: &str) -> Result<JitFunction<F>, FunctionLookupError> where F: UnsafeFunctionPointer, { if !self.jit_mode { return Err(FunctionLookupError::JITNotEnabled); } // LLVMGetFunctionAddress segfaults in llvm 5.0 -> 8.0 when fn_name doesn't exist. This is a workaround // to see if it exists and avoid the segfault when it doesn't #[cfg(any(feature = "llvm5-0", feature = "llvm6-0", feature = "llvm7-0", feature = "llvm8-0"))] self.get_function_value(fn_name)?; let c_string = CString::new(fn_name).expect("Conversion to CString failed unexpectedly"); let address = LLVMGetFunctionAddress(self.execution_engine_inner(), c_string.as_ptr()); // REVIEW: Can also return 0 if no targets are initialized. // One option might be to set a (thread local?) global to true if any at all of the targets have been // initialized (maybe we could figure out which config in particular is the trigger) // and if not return an "NoTargetsInitialized" error, instead of not found. if address == 0 { return Err(FunctionLookupError::FunctionNotFound); } assert_eq!(size_of::<F>(), size_of::<usize>(), "The type `F` must have the same size as a function pointer"); let execution_engine = self.execution_engine.as_ref().expect(EE_INNER_PANIC); Ok(JitFunction { _execution_engine: execution_engine.clone(), inner: transmute_copy(&address), }) } // REVIEW: Not sure if an EE's target data can change.. if so we might want to update the value // when making this call pub fn get_target_data(&self) -> &TargetData { self.target_data.as_ref().expect("TargetData should always exist until Drop") } // REVIEW: Can also find nothing if no targeting is initialized. Maybe best to // do have a global flag for anything initialized. Catch is that it must be initialized // before EE is created pub fn get_function_value(&self, fn_name: &str) -> Result<FunctionValue, FunctionLookupError> { if !self.jit_mode { return Err(FunctionLookupError::JITNotEnabled); } let c_string = CString::new(fn_name).expect("Conversion to CString failed unexpectedly"); let mut function = unsafe { zeroed() }; let code = unsafe { LLVMFindFunction(self.execution_engine_inner(), c_string.as_ptr(), &mut function) }; if code == 0 { return FunctionValue::new(function).ok_or(FunctionLookupError::FunctionNotFound) }; Err(FunctionLookupError::FunctionNotFound) } // TODOC: Marked as unsafe because input function could very well do something unsafe. It's up to the caller // to ensure that doesn't happen by defining their function correctly. pub unsafe fn run_function(&self, function: &FunctionValue, args: &[&GenericValue]) -> GenericValue { let mut args: Vec<LLVMGenericValueRef> = args.iter() .map(|val| val.generic_value) .collect(); let value = LLVMRunFunction(self.execution_engine_inner(), function.as_value_ref(), args.len() as u32, args.as_mut_ptr()); // REVIEW: usize to u32 ok?? GenericValue::new(value) } // TODOC: Marked as unsafe because input function could very well do something unsafe. It's up to the caller // to ensure that doesn't happen by defining their function correctly. // SubType: Only for JIT EEs? pub unsafe fn run_function_as_main(&self, function: &FunctionValue, args: &[&str]) -> c_int { let cstring_args: Vec<CString> = args.iter().map(|&arg| CString::new(arg).expect("Conversion to CString failed unexpectedly")).collect(); let raw_args: Vec<*const _> = cstring_args.iter().map(|arg| arg.as_ptr()).collect(); let environment_variables = vec![]; // TODO: Support envp. Likely needs to be null terminated LLVMRunFunctionAsMain(self.execution_engine_inner(), function.as_value_ref(), raw_args.len() as u32, raw_args.as_ptr(), environment_variables.as_ptr()) // REVIEW: usize to u32 cast ok?? } pub fn free_fn_machine_code(&self, function: &FunctionValue) { unsafe { LLVMFreeMachineCodeForFunction(self.execution_engine_inner(), function.as_value_ref()) } } // REVIEW: Is this actually safe? pub fn run_static_constructors(&self) { unsafe { LLVMRunStaticConstructors(self.execution_engine_inner()) } } // REVIEW: Is this actually safe? Can you double destruct/free? pub fn run_static_destructors(&self) { unsafe { LLVMRunStaticDestructors(self.execution_engine_inner()) } } } // Modules owned by the EE will be discarded by the EE so we don't // want owned modules to drop. impl Drop for ExecutionEngine { fn drop(&mut self) { forget( self.target_data .take() .expect("TargetData should always exist until Drop"), ); // We must ensure the EE gets dropped before its context does, // which is important in the case where the EE has the last // remaining reference to it context drop(self.execution_engine.take().expect(EE_INNER_PANIC)); } } impl Clone for ExecutionEngine { fn clone(&self) -> ExecutionEngine { let context = self.non_global_context.clone(); let execution_engine_rc = self.execution_engine_rc().clone(); ExecutionEngine::new(execution_engine_rc, context, self.jit_mode) } } /// A smart pointer which wraps the `Drop` logic for `LLVMExecutionEngineRef`. #[derive(Debug, Clone, PartialEq, Eq)] struct ExecEngineInner(Rc<LLVMExecutionEngineRef>); impl Drop for ExecEngineInner { fn drop(&mut self) { if Rc::strong_count(&self.0) == 1 { unsafe { LLVMDisposeExecutionEngine(*self.0); } } } } impl Deref for ExecEngineInner { type Target = LLVMExecutionEngineRef; fn deref(&self) -> &Self::Target { &*self.0 } } /// A wrapper around a function pointer which ensures the function being pointed /// to doesn't accidentally outlive its execution engine. #[derive(Clone)] pub struct JitFunction<F> { _execution_engine: ExecEngineInner, inner: F, } impl<F> Debug for JitFunction<F> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.debug_tuple("JitFunction") .field(&"<unnamed>") .finish() } } /// Marker trait representing an unsafe function pointer (`unsafe extern "C" fn(A, B, ...) -> Output`). pub trait UnsafeFunctionPointer: private::SealedUnsafeFunctionPointer {} mod private { /// A sealed trait which ensures nobody outside this crate can implement /// `UnsafeFunctionPointer`. /// /// See https://rust-lang-nursery.github.io/api-guidelines/future-proofing.html pub trait SealedUnsafeFunctionPointer: Copy {} } impl<F: private::SealedUnsafeFunctionPointer> UnsafeFunctionPointer for F {} macro_rules! impl_unsafe_fn { (@recurse $first:ident $( , $rest:ident )*) => { impl_unsafe_fn!($( $rest ),*); }; (@recurse) => {}; ($( $param:ident ),*) => { impl<Output, $( $param ),*> private::SealedUnsafeFunctionPointer for unsafe extern "C" fn($( $param ),*) -> Output {} impl<Output, $( $param ),*> JitFunction<unsafe extern "C" fn($( $param ),*) -> Output> { /// This method allows you to call the underlying function while making /// sure that the backing storage is not dropped too early and /// preserves the `unsafe` marker for any calls. #[allow(non_snake_case)] #[inline(always)] pub unsafe fn call(&self, $( $param: $param ),*) -> Output { (self.inner)($( $param ),*) } } impl_unsafe_fn!(@recurse $( $param ),*); }; } impl_unsafe_fn!(A, B, C, D, E, F, G, H, I, J, K, L, M);