@@ -762,105 +762,78 @@ impl<'a> CrateLoader<'a> {
762762 }
763763
764764 fn inject_sanitizer_runtime ( & mut self ) {
765- if let Some ( ref sanitizer) = self . sess . opts . debugging_opts . sanitizer {
766- // Sanitizers can only be used on some tested platforms with
767- // executables linked to `std`
768- const ASAN_SUPPORTED_TARGETS : & [ & str ] = & [ "x86_64-unknown-linux-gnu" ,
769- "x86_64-apple-darwin" ] ;
770- const TSAN_SUPPORTED_TARGETS : & [ & str ] = & [ "x86_64-unknown-linux-gnu" ,
771- "x86_64-apple-darwin" ] ;
772- const LSAN_SUPPORTED_TARGETS : & [ & str ] = & [ "x86_64-unknown-linux-gnu" ] ;
773- const MSAN_SUPPORTED_TARGETS : & [ & str ] = & [ "x86_64-unknown-linux-gnu" ] ;
774-
775- let supported_targets = match * sanitizer {
776- Sanitizer :: Address => ASAN_SUPPORTED_TARGETS ,
777- Sanitizer :: Thread => TSAN_SUPPORTED_TARGETS ,
778- Sanitizer :: Leak => LSAN_SUPPORTED_TARGETS ,
779- Sanitizer :: Memory => MSAN_SUPPORTED_TARGETS ,
780- } ;
781- if !supported_targets. contains ( & & * self . sess . opts . target_triple . triple ( ) ) {
782- self . sess . err ( & format ! ( "{:?}Sanitizer only works with the `{}` target" ,
783- sanitizer,
784- supported_targets. join( "` or `" )
785- ) ) ;
765+ // Sanitizers can only be used on some tested platforms with
766+ // executables linked to `std`
767+ const ASAN_SUPPORTED_TARGETS : & [ & str ] = & [ "x86_64-unknown-linux-gnu" ,
768+ "x86_64-apple-darwin" ] ;
769+ const TSAN_SUPPORTED_TARGETS : & [ & str ] = & [ "x86_64-unknown-linux-gnu" ,
770+ "x86_64-apple-darwin" ] ;
771+ const LSAN_SUPPORTED_TARGETS : & [ & str ] = & [ "x86_64-unknown-linux-gnu" ,
772+ "x86_64-apple-darwin" ] ;
773+ const MSAN_SUPPORTED_TARGETS : & [ & str ] = & [ "x86_64-unknown-linux-gnu" ] ;
774+
775+ let sanitizer = match self . sess . opts . debugging_opts . sanitizer {
776+ Some ( ref sanitizer) => sanitizer,
777+ None => {
778+ self . sess . injected_sanitizer_runtime . set ( None ) ;
786779 return
787780 }
781+ } ;
788782
789- // firstyear 2017 - during testing I was unable to access an OSX machine
790- // to make this work on different crate types. As a result, today I have
791- // only been able to test and support linux as a target.
792- if self . sess . opts . target_triple . triple ( ) == "x86_64-unknown-linux-gnu" {
793- if !self . sess . crate_types . borrow ( ) . iter ( ) . all ( |ct| {
794- match * ct {
795- // Link the runtime
796- config:: CrateType :: Staticlib |
797- config:: CrateType :: Executable => true ,
798- // This crate will be compiled with the required
799- // instrumentation pass
800- config:: CrateType :: Rlib |
801- config:: CrateType :: Dylib |
802- config:: CrateType :: Cdylib =>
803- false ,
804- _ => {
805- self . sess . err ( & format ! ( "Only executables, staticlibs, \
806- cdylibs, dylibs and rlibs can be compiled with \
807- `-Z sanitizer`") ) ;
808- false
809- }
810- }
811- } ) {
812- return
813- }
814- } else {
815- if !self . sess . crate_types . borrow ( ) . iter ( ) . all ( |ct| {
816- match * ct {
817- // Link the runtime
818- config:: CrateType :: Executable => true ,
819- // This crate will be compiled with the required
820- // instrumentation pass
821- config:: CrateType :: Rlib => false ,
822- _ => {
823- self . sess . err ( & format ! ( "Only executables and rlibs can be \
824- compiled with `-Z sanitizer`") ) ;
825- false
826- }
827- }
828- } ) {
829- return
830- }
783+ let supported_targets = match * sanitizer {
784+ Sanitizer :: Address => ASAN_SUPPORTED_TARGETS ,
785+ Sanitizer :: Thread => TSAN_SUPPORTED_TARGETS ,
786+ Sanitizer :: Leak => LSAN_SUPPORTED_TARGETS ,
787+ Sanitizer :: Memory => MSAN_SUPPORTED_TARGETS ,
788+ } ;
789+ if !supported_targets. contains ( & & * self . sess . opts . target_triple . triple ( ) ) {
790+ self . sess . err ( & format ! ( "{:?}Sanitizer only works with the `{}` target" ,
791+ sanitizer,
792+ supported_targets. join( "` or `" )
793+ ) ) ;
794+ return
795+ }
796+
797+ let any_non_rlib = self . sess . crate_types . borrow ( ) . iter ( ) . any ( |ct| {
798+ * ct != config:: CrateType :: Rlib
799+ } ) ;
800+ if !any_non_rlib {
801+ info ! ( "sanitizer runtime injection skipped, only generating rlib" ) ;
802+ self . sess . injected_sanitizer_runtime . set ( None ) ;
803+ return
804+ }
805+
806+ let mut uses_std = false ;
807+ self . cstore . iter_crate_data ( |_, data| {
808+ if data. name == sym:: std {
809+ uses_std = true ;
831810 }
811+ } ) ;
832812
833- let mut uses_std = false ;
834- self . cstore . iter_crate_data ( |_, data| {
835- if data. name == sym:: std {
836- uses_std = true ;
837- }
838- } ) ;
813+ if uses_std {
814+ let name = match * sanitizer {
815+ Sanitizer :: Address => "rustc_asan" ,
816+ Sanitizer :: Leak => "rustc_lsan" ,
817+ Sanitizer :: Memory => "rustc_msan" ,
818+ Sanitizer :: Thread => "rustc_tsan" ,
819+ } ;
820+ info ! ( "loading sanitizer: {}" , name) ;
839821
840- if uses_std {
841- let name = match * sanitizer {
842- Sanitizer :: Address => "rustc_asan" ,
843- Sanitizer :: Leak => "rustc_lsan" ,
844- Sanitizer :: Memory => "rustc_msan" ,
845- Sanitizer :: Thread => "rustc_tsan" ,
846- } ;
847- info ! ( "loading sanitizer: {}" , name) ;
848-
849- let symbol = Symbol :: intern ( name) ;
850- let dep_kind = DepKind :: Explicit ;
851- let ( _, data) =
852- self . resolve_crate ( & None , symbol, symbol, None , None , DUMMY_SP ,
853- PathKind :: Crate , dep_kind)
854- . unwrap_or_else ( |err| err. report ( ) ) ;
855-
856- // Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
857- if !data. root . sanitizer_runtime {
858- self . sess . err ( & format ! ( "the crate `{}` is not a sanitizer runtime" ,
859- name) ) ;
860- }
861- } else {
862- self . sess . err ( "Must link std to be compiled with `-Z sanitizer`" ) ;
822+ let symbol = Symbol :: intern ( name) ;
823+ let dep_kind = DepKind :: Implicit ;
824+ let ( cnum, data) =
825+ self . resolve_crate ( & None , symbol, symbol, None , None , DUMMY_SP ,
826+ PathKind :: Crate , dep_kind)
827+ . unwrap_or_else ( |err| err. report ( ) ) ;
828+
829+ // Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
830+ if !data. root . sanitizer_runtime {
831+ self . sess . err ( & format ! ( "the crate `{}` is not a sanitizer runtime" ,
832+ name) ) ;
863833 }
834+ self . sess . injected_sanitizer_runtime . set ( Some ( cnum) ) ;
835+ } else {
836+ self . sess . err ( "Must link std to be compiled with `-Z sanitizer`" ) ;
864837 }
865838 }
866839
0 commit comments