16
16
17
17
use cranelift_codegen_meta as meta;
18
18
19
- use sha2:: { Digest , Sha512 } ;
20
19
use std:: env;
21
20
use std:: io:: Read ;
22
21
use std:: process;
@@ -163,7 +162,28 @@ impl IsleCompilation {
163
162
/// `<generated_filename>.manifest` and use it to verify that a
164
163
/// rebuild was done if necessary.
165
164
fn compute_manifest ( & self ) -> Result < String , Box < dyn std:: error:: Error + ' static > > {
165
+ // We use the deprecated SipHasher from std::hash in order to verify
166
+ // that ISLE sources haven't changed since the generated source was
167
+ // last regenerated.
168
+ //
169
+ // We use this instead of a stronger and more usual content hash, like
170
+ // SHA-{160,256,512}, because it's built into the standard library and
171
+ // we don't want to pull in a separate crate. We try to keep Cranelift
172
+ // crate dependencies as intentionally small as possible. In fact, we
173
+ // used to use the `sha2` crate for SHA-512 and this turns out to be
174
+ // undesirable for downstream consumers (see #3609).
175
+ //
176
+ // Why not the recommended replacement
177
+ // `std::collections::hash_map::DefaultHasher`? Because we need the
178
+ // hash to be deterministic, both between runs (i.e., not seeded with
179
+ // random state) and across Rust versions.
180
+ //
181
+ // If `SipHasher` is ever actually removed from `std`, we'll need to
182
+ // find a new option, either a very small crate or something else
183
+ // that's built-in.
184
+ #![ allow( deprecated) ]
166
185
use std:: fmt:: Write ;
186
+ use std:: hash:: { Hasher , SipHasher } ;
167
187
168
188
let mut manifest = String :: new ( ) ;
169
189
@@ -176,11 +196,11 @@ impl IsleCompilation {
176
196
// to `\r\n`; canonicalize the source that we hash to
177
197
// Unix-style (`\n`) so hashes will match.
178
198
let content = content. replace ( "\r \n " , "\n " ) ;
179
- // One line in the manifest: <filename> <sha-512 hash >.
180
- let mut hasher = Sha512 :: default ( ) ;
181
- hasher. update ( content. as_bytes ( ) ) ;
199
+ // One line in the manifest: <filename> <siphash >.
200
+ let mut hasher = SipHasher :: new_with_keys ( 0 , 0 ) ; // fixed keys for determinism
201
+ hasher. write ( content. as_bytes ( ) ) ;
182
202
let filename = format ! ( "{}" , filename. display( ) ) . replace ( "\\ " , "/" ) ;
183
- writeln ! ( & mut manifest, "{} {:x}" , filename, hasher. finalize ( ) ) ?;
203
+ writeln ! ( & mut manifest, "{} {:x}" , filename, hasher. finish ( ) ) ?;
184
204
}
185
205
186
206
Ok ( manifest)
0 commit comments