@@ -69,20 +69,98 @@ fn main() {
6969 let mut lib_path = custom_lib_dir. join ( std:: env:: var_os ( "TARGET" ) . unwrap ( ) ) ;
7070 lib_path. push ( "release" ) ;
7171
72- #[ cfg( all( feature = "embed-runtime" , target_family = "unix" ) ) ]
73- {
74- // NOTE: lib, .a are added always on unix-like systems as described in:
75- // https://gist.github.com/novafacing/1389cbb2f0a362d7eb103e67b4468e2b
72+ if cfg ! ( target_family = "unix" ) {
73+ use std:: path:: Path ;
74+
75+ lib_path. push ( "libafl_libfuzzer_runtime.a" ) ;
76+ let target_libdir = Command :: new ( "rustc" )
77+ . args ( [ "--print" , "target-libdir" ] )
78+ . output ( )
79+ . expect ( "Couldn't find rustc's target-libdir" ) ;
80+ let target_libdir = String :: from_utf8 ( target_libdir. stdout ) . unwrap ( ) ;
81+ let target_libdir = Path :: new ( target_libdir. trim ( ) ) ;
82+
83+ let rust_lld = target_libdir. join ( "../bin/rust-lld" ) ;
84+ let rust_ar = target_libdir. join ( "../bin/llvm-ar" ) ; // NOTE: depends on llvm-tools
85+ let rust_objcopy = target_libdir. join ( "../bin/llvm-objcopy" ) ; // NOTE: depends on llvm-tools
86+
87+ let objfile_orig = custom_lib_dir. join ( "libFuzzer.o" ) ;
88+ let objfile_dest = custom_lib_dir. join ( "libFuzzer-mimalloc.o" ) ;
89+
90+ let mut command = Command :: new ( rust_lld) ;
91+ command
92+ . args ( [ "-flavor" , "gnu" ] )
93+ . arg ( "-r" )
94+ . arg ( "--whole-archive" )
95+ . arg ( lib_path)
96+ . args ( [ "-o" , objfile_orig. to_str ( ) . expect ( "Invalid path characters present in your current directory prevent us from linking to the runtime" ) ] ) ;
97+
98+ assert ! (
99+ !command. status( ) . map( |s| !s. success( ) ) . unwrap_or( true ) ,
100+ "Couldn't link runtime crate! Do you have the llvm-tools component installed?"
101+ ) ;
102+
103+ let mut command = Command :: new ( rust_objcopy) ;
104+ command
105+ . args ( [ "--redefine-sym" , "__rust_alloc=__rust_alloc_mimalloc" ] )
106+ . args ( [ "--redefine-sym" , "__rust_dealloc=__rust_dealloc_mimalloc" ] )
107+ . args ( [ "--redefine-sym" , "__rust_realloc=__rust_realloc_mimalloc" ] )
108+ . args ( [
109+ "--redefine-sym" ,
110+ "__rust_alloc_zeroed=__rust_alloc_zeroed_mimalloc" ,
111+ ] )
112+ . args ( [
113+ "--redefine-sym" ,
114+ "__rust_alloc_error_handler=__rust_alloc_error_handler_mimalloc" ,
115+ ] )
116+ . args ( [
117+ "--redefine-sym" ,
118+ "__rust_no_alloc_shim_is_unstable=__rust_no_alloc_shim_is_unstable_mimalloc" ,
119+ ] )
120+ . args ( [
121+ "--redefine-sym" ,
122+ "__rust_alloc_error_handler_should_panic=__rust_alloc_error_handler_should_panic_mimalloc" ,
123+ ] )
124+ . args ( [ & objfile_orig, & objfile_dest] ) ;
125+
126+ assert ! (
127+ !command. status( ) . map( |s| !s. success( ) ) . unwrap_or( true ) ,
128+ "Couldn't rename allocators in the runtime crate! Do you have the llvm-tools component installed?"
129+ ) ;
130+
131+ let mut command = Command :: new ( rust_ar) ;
132+ command
133+ . arg ( "cr" )
134+ . arg ( custom_lib_dir. join ( "libFuzzer.a" ) )
135+ . arg ( objfile_dest) ;
136+
137+ assert ! (
138+ !command. status( ) . map( |s| !s. success( ) ) . unwrap_or( true ) ,
139+ "Couldn't create runtime archive!"
140+ ) ;
141+
142+ #[ cfg( feature = "embed-runtime" ) ]
143+ {
144+ // NOTE: lib, .a are added always on unix-like systems as described in:
145+ // https://gist.github.com/novafacing/1389cbb2f0a362d7eb103e67b4468e2b
146+ println ! (
147+ "cargo:rustc-env=LIBAFL_LIBFUZZER_RUNTIME_PATH={}" ,
148+ custom_lib_dir. join( "libFuzzer.a" ) . display( )
149+ ) ;
150+ }
151+
76152 println ! (
77- "cargo:rustc-env=LIBAFL_LIBFUZZER_RUNTIME_PATH ={}" ,
78- lib_path . join ( "libafl_libfuzzer_runtime.a" ) . display ( )
153+ "cargo:rustc-link-search=native ={}" ,
154+ custom_lib_dir . to_str ( ) . unwrap ( )
79155 ) ;
156+ println ! ( "cargo:rustc-link-lib=static=Fuzzer" ) ;
157+ } else {
158+ println ! (
159+ "cargo:rustc-link-search=native={}" ,
160+ lib_path. to_str( ) . unwrap( )
161+ ) ;
162+ println ! ( "cargo:rustc-link-lib=static=afl_fuzzer_runtime" ) ;
80163 }
81164
82- println ! (
83- "cargo:rustc-link-search=native={}" ,
84- lib_path. to_str( ) . unwrap( )
85- ) ;
86- println ! ( "cargo:rustc-link-lib=static=afl_libfuzzer_runtime" ) ;
87165 println ! ( "cargo:rustc-link-lib=stdc++" ) ;
88166}
0 commit comments