@@ -358,10 +358,14 @@ pub struct Evaluator<'mir, 'tcx> {
358358 pub ( crate ) report_progress : Option < u32 > ,
359359 /// The number of blocks that passed since the last progress report.
360360 pub ( crate ) since_progress_report : u32 ,
361+
362+ /// Handle of the optional shared object file for external functions.
363+ pub external_so_lib : Option < ( libloading:: Library , std:: path:: PathBuf ) > ,
361364}
362365
363366impl < ' mir , ' tcx > Evaluator < ' mir , ' tcx > {
364367 pub ( crate ) fn new ( config : & MiriConfig , layout_cx : LayoutCx < ' tcx , TyCtxt < ' tcx > > ) -> Self {
368+ let target_triple = & layout_cx. tcx . sess . opts . target_triple . to_string ( ) ;
365369 let local_crates = helpers:: get_local_crates ( layout_cx. tcx ) ;
366370 let layouts =
367371 PrimitiveLayouts :: new ( layout_cx) . expect ( "Couldn't get layouts of primitive types" ) ;
@@ -412,6 +416,24 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
412416 preemption_rate : config. preemption_rate ,
413417 report_progress : config. report_progress ,
414418 since_progress_report : 0 ,
419+ external_so_lib : config. external_so_file . as_ref ( ) . map ( |lib_file_path| {
420+ // Check if host target == the session target.
421+ if option_env ! ( "TARGET" ) == Some ( target_triple) {
422+ panic ! (
423+ "calling external C functions in linked .so file requires target and host to be the same"
424+ ) ;
425+ }
426+ // Note: it is the user's responsibility to provide a correct SO file.
427+ // WATCH OUT: If an invalid/incorrect SO file is specified, this can cause
428+ // undefined behaviour in Miri itself!
429+ (
430+ unsafe {
431+ libloading:: Library :: new ( lib_file_path)
432+ . expect ( "Failed to read specified shared object file" )
433+ } ,
434+ lib_file_path. clone ( ) ,
435+ )
436+ } ) ,
415437 }
416438 }
417439
0 commit comments