1
- use std:: { path:: PathBuf , process:: Command } ;
1
+ use std:: {
2
+ fs:: File ,
3
+ io:: { BufRead , BufReader , BufWriter , Write } ,
4
+ path:: PathBuf ,
5
+ process:: { Command , Stdio } ,
6
+ } ;
2
7
3
8
fn main ( ) {
4
9
if cfg ! ( any( feature = "cargo-clippy" , docsrs) ) {
@@ -83,6 +88,9 @@ fn main() {
83
88
let rust_lld = target_libdir. join ( "../bin/rust-lld" ) ;
84
89
let rust_ar = target_libdir. join ( "../bin/llvm-ar" ) ; // NOTE: depends on llvm-tools
85
90
let rust_objcopy = target_libdir. join ( "../bin/llvm-objcopy" ) ; // NOTE: depends on llvm-tools
91
+ let nm = "nm" ; // NOTE: we use system nm here because llvm-nm doesn't respect the encoding?
92
+
93
+ let redefined_symbols = custom_lib_dir. join ( "redefs.txt" ) ;
86
94
87
95
let objfile_orig = custom_lib_dir. join ( "libFuzzer.o" ) ;
88
96
let objfile_dest = custom_lib_dir. join ( "libFuzzer-mimalloc.o" ) ;
@@ -100,27 +108,67 @@ fn main() {
100
108
"Couldn't link runtime crate! Do you have the llvm-tools component installed?"
101
109
) ;
102
110
111
+ let mut child = Command :: new ( nm)
112
+ . arg ( & objfile_orig)
113
+ . stdout ( Stdio :: piped ( ) )
114
+ . spawn ( )
115
+ . unwrap ( ) ;
116
+
117
+ let mut redefinitions_file = BufWriter :: new ( File :: create ( & redefined_symbols) . unwrap ( ) ) ;
118
+
119
+ // redefine all the rust-mangled symbols we can
120
+ // TODO this will break when v0 mangling is stabilised
121
+ for line in BufReader :: new ( child. stdout . take ( ) . unwrap ( ) ) . lines ( ) {
122
+ let line = line. unwrap ( ) ;
123
+ let ( _, symbol) = line. rsplit_once ( ' ' ) . unwrap ( ) ;
124
+ if symbol. starts_with ( "_ZN" ) {
125
+ writeln ! (
126
+ redefinitions_file,
127
+ "{} {}" ,
128
+ symbol,
129
+ symbol. replacen( "_ZN" , "_ZN26__libafl_libfuzzer_runtime" , 1 )
130
+ )
131
+ . unwrap ( ) ;
132
+ }
133
+ }
134
+ redefinitions_file. flush ( ) . unwrap ( ) ;
135
+ drop ( redefinitions_file) ;
136
+
137
+ assert ! (
138
+ !child. wait( ) . map( |s| !s. success( ) ) . unwrap_or( true ) ,
139
+ "Couldn't link runtime crate! Do you have the llvm-tools component installed?"
140
+ ) ;
141
+
103
142
let mut command = Command :: new ( rust_objcopy) ;
143
+
144
+ for symbol in [
145
+ "__rust_drop_panic" ,
146
+ "__rust_foreign_exception" ,
147
+ "rust_begin_unwind" ,
148
+ "rust_panic" ,
149
+ "rust_eh_personality" ,
150
+ "__rg_oom" ,
151
+ "__rdl_oom" ,
152
+ "__rdl_alloc" ,
153
+ "__rust_alloc" ,
154
+ "__rdl_dealloc" ,
155
+ "__rust_dealloc" ,
156
+ "__rdl_realloc" ,
157
+ "__rust_realloc" ,
158
+ "__rdl_alloc_zeroed" ,
159
+ "__rust_alloc_zeroed" ,
160
+ "__rust_alloc_error_handler" ,
161
+ "__rust_no_alloc_shim_is_unstable" ,
162
+ "__rust_alloc_error_handler_should_panic" ,
163
+ ] {
164
+ command
165
+ . arg ( "--redefine-sym" )
166
+ . arg ( format ! ( "{symbol}={symbol}_libafl_libfuzzer_runtime" ) ) ;
167
+ }
168
+
104
169
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
- ] )
170
+ . arg ( "--redefine-syms" )
171
+ . arg ( redefined_symbols)
124
172
. args ( [ & objfile_orig, & objfile_dest] ) ;
125
173
126
174
assert ! (
0 commit comments