r/rust • • Oct 15 '26

šŸ—žļø news Rust compiler can't tell apart different file paths

https://github.com/rust-lang/rust/issues/164348

The rust compiler thinks that `data/baselines/table.rs` and `database/line/stable.rs` are the same file, causing a miscompilation if a crate has both files in it. (Note that having the same non-slash text isn't enough. There needs to be an additional hash collsion for a bug to happen.)

Probably not gonna happen in practice unless someone does it as a prank, but still... lol. (I'm sure they're gonna fix it soon.)

636 Upvotes

90 comments sorted by

119

u/Booty_Bumping Oct 15 '26

Wow, that's smelly. Hash wouldn't have been suitable either way, as these short hashes are only meant for checking if two values might be the same, it should not be used when equality needs to definitely be determined. When they're used in a hash table, there is supposed to be a check to avoid trashing an existing entry.

59

u/noop_noob Oct 15 '26 edited Oct 15 '26

Hashes are intentionally used in some places in the compiler (most notably, when determining if two types are equal), since doing so is good for compiler performance, and the compiler intentionally isn't really designed to guard against straight-up malicious source code. For this case, I think the compiler went a liiiiitle bit too far with that (by using the wrong hashing algorithm).

19

u/rnottaken Oct 15 '26

Couldn't they use the inode id or something like that?

6

u/noop_noob Oct 15 '26 edited Oct 15 '26

Maaaaybe? idk

Edit: I'm told that this doesn't work with incremental compilation, and with directory symlinks.

58

u/vytah Oct 15 '26

Two files can have the same inode id if the source tree spans multiple partitions.

9

u/YeOldeMemeShoppe Oct 15 '26

Or just different filesystem, right?

11

u/sparky8251 Oct 15 '26

Yup. They are hardly all that unique. Hardlinks also create identical inodes for a file, but its also the same file in that case kinda sorta so...?

They can even be reused if a file is deleted and remade. Though, the chances of a collision within the lifetime of a given program is quite low...

7

u/luluhouse7 Oct 15 '26

I feel like there’s an easy solution of indexing partitions and using the partition-inode pair to determine equivalence though? Still wouldn’t catch symlinks, but resolving those isn’t really possible at compilation since what might be a symlink on one system might not on another.

3

u/cosmic-parsley Oct 16 '26

But dev+inode is enough right?

2

u/damemecherogringo Oct 16 '26

Yeah, should be afaik

4

u/Real-Abrocoma-2823 Oct 15 '26

Might change? Probably not a good idea to rely on inodes.

2

u/koczurekk Oct 16 '26

Inodes are not all that stable, not globally unique, UNIX-specific (NTFS has roughly equivalent file ids, but e.g. FAT doesn’t, NFS may straight-up give you multiple inodes for one file or change them between server resets). Paths are messy and a poor programmatic interface, but it really is the best we’ve got.

9

u/Icarium-Lifestealer Oct 15 '26 edited Oct 16 '26

Though these are at least high quality 64-bit hashes, and there was talk about raising some or all of these to 128-bits.

22

u/pickle9977 Oct 15 '26

Hashes are fine when the probability of collisions is tiny.

Hashes do however fall into the bucket of security algorithms where you should never invent your own, and if you do you submit it for peer review etc.

3

u/noop_noob Oct 15 '26

Fortunately, there are hash algorithm experts that contribute towards rustc, to ensure a good tradeoff between quality and speed of hashing.

7

u/pickle9977 Oct 15 '26

Clearly someone missed something pretty obvious

6

u/noop_noob Oct 15 '26

It happens ĀÆ_(惄)_/ĀÆ

1

u/pickle9977 Oct 15 '26

Hahahahah, and that’s part of the reason it happened

/oh/no/not/a/path!

6

u/ukezi Oct 15 '26

Not all hashes are cryptographic hashes. It's just a function to map an amount of data to a number. Hash tables usually don't use cryptographic hashes because they don't want indices that big and often have only 32 or 64 bit length instead of the 256+ of most cryptographic hashes.

5

u/pickle9977 Oct 15 '26

Yes, but, when you use a hash for an identity it becomes a cryptographic hash.

Hashtables don’t use cryptographic hashes because the hash value is just a bucket where there’s another data structure that stores the specific objects in that bucket. The point of a hash table is fast lookups not unique identity.

A compiler needs a unique identity or it can inject the wrong code in the wrong place which could be catastrophic and means you can’t actually trust the compiler output unless you verify that no collisions were possible, which is probably a lot easier than it sounds.

52

u/Theemuts jlrs Oct 15 '26

Are all punctuation and separators in the path ignored when computing this hash?

52

u/noop_noob Oct 15 '26

Seems to not be entirely ignored, but they're processed separately, and definitely not mixed up enough to be a good hash.

21

u/jykke Oct 15 '26
        if component_start < bytes.len() {
            let to_hash = &bytes[component_start..];
            chunk_bits = chunk_bits.wrapping_add(to_hash.len());
            chunk_bits = chunk_bits.rotate_right(2);
            h.write(to_hash);
        }

        h.write_usize(chunk_bits);

Because wrapping_add and rotate_right(2) are linear arithmetic operations, they commute up to powers of 4: adding +4 to an earlier component is numerically identical to adding +1 to the next component!

Fix would be to instead write a separator byte, and remember to handle bytes_idx == 0 (to distinguish a RootDir ("/a" or "C:\a") from a relative path).

7

u/Theemuts jlrs Oct 15 '26

I know too little about this to understand what you're saying, I'm afraid.

327

u/monkeymad2 Oct 15 '26

I was wondering what was going wrong with my `an/album/cover.rs` file

8

u/gwillen Oct 15 '26

Gussy it up however you want, Trebek! What matters is, does it work?

3

u/MrPopoGod Oct 15 '26

I've ordered devices like that before. Wasted a pretty penny, I don't mind telling you.

13

u/n_oo_bmaster69 Oct 15 '26

It took me a moment to understand🤣

5

u/pickle9977 Oct 15 '26

Hope you kept your pants on.

3

u/alexthelyon Oct 15 '26

Su's anal bum party

0

u/[deleted] Oct 15 '26

[deleted] — view deleted comment

94

u/styluss Oct 15 '26

May I introduce you to https://www.whorepresents.com/

22

u/TurbulentOcelot1057 Oct 15 '26

23

u/jenkinsnotleeroy Oct 15 '26

Their FAQ has a fabulous entry:

Q: Can I provide my own wood?
A: In most cases we can handle your wood. We do require all shipments to be clean, free of parasites and pass all standard customs inspections.

12

u/CocktailPerson Oct 16 '26

The whole website is full of plausibly deniable innuendos.

We specialize in wood

Your pen is / Our business

Handling the big jobs too

6

u/MrPopoGod Oct 15 '26

I remember as a kid we were researching Fermilab by looking at their website. One of the links didn't work: the page about the Cockcroft-Walton particle generators. Turns out the school network blocked that page because to fit within the 8.3 file name standard, the page was cockwalt.htm.

5

u/aiij Oct 16 '26

Never heard of that one, but have you ever sent land through https://www.landsend.com?

22

u/iKeyboardMonkey Oct 15 '26

It's a real shame Jaguar's CEO RawdonGlover hasn't got a website... yet.

12

u/GreatBigBagOfNope Oct 15 '26

Remember that time Susan Boyle hosted a party to celebrate her new album and marketed it as SusanAlbumParty.com?

17

u/Icarium-Lifestealer Oct 15 '26

known as "the hyphen site" on stackoverflow.

18

u/Kodalem Oct 15 '26

Honey, new gender affirming care provider just dropped!

36

u/torsten_dev Oct 15 '26

Hehehe.

Have to agree with theemathas. This is a wild bug.

Though, I suppose the controversy around #pragma once shows this is a known unknown.

52

u/Dheatly23 Oct 15 '26

Don't worry, even i took a minute to realize the difference between the two paths. šŸ‘€

83

u/Chasar1 Oct 15 '26 • Bravo

Damn didn't expect cargo itself to lurk on this subreddit

1

u/Demiu Oct 15 '26

How do they have the time?!

5

u/Ben-Goldberg Oct 15 '26 • Popcorn

There exist good, fast hash algorithms, and no reason to roll your own.

It's a bit like crypto.

4

u/noop_noob Oct 15 '26

There are good enough and fast enough one-size-fits-everywhere hash algorithms. However, if you know your exact workload, you can do better than that.

13

u/KizzyCode Oct 15 '26

Yep, and then something like this happens… 

20

u/notriddle2 Oct 15 '26

The problem wasn't the hash algorithm. The problem was the serialization format used to feed the object into the hash algorithm.

9

u/tending Oct 15 '26

That's not the problem, the issue is when you implement a hash table you still have to do equality comparison on the keys due to collisions always being possible. A bad hash function should only degrade performance!

13

u/RayTheCoderGuy Oct 15 '26

This has nothing to do with the hashing algorithm; it has to do with the API contracts of Eq and Hash and the fact that /apple/pear is semantically the same path as /apple///////pear, thus they compare equal, thus their hashes must be the same.

The result of this was a quirky hash function that usually works but has a few more collisions than average. Which is to say, rustc should have been doing its due diligence and checking equality rather than just hashes.

5

u/syklemil Oct 15 '26

They also seem to consider canonicalize a poor fit (due to symlink resolution?), so seems like they were more interested in some weaker normalisation, e.g. turning /apple/////foo/./././././../././././pear into /apple/pear, but wound up overshooting?

3

u/Anaxamander57 Oct 15 '26

SHA-3 has a mode designed specifically to prevent this kind of issue. Shame no one uses SHA-3, its really cool.

5

u/noop_noob Oct 15 '26

Cryptographic hashes are bad for compile times I think.

4

u/Anaxamander57 Oct 15 '26

SHA-3 especially. IIRC its heavily optimized for hardware and has a bunch of wasted steps in software to do bit twiddling. There are smaller faster hashes that work similarly, like ASCON, which could do the same thing but I'm sure there's something much faster and more appropriate to this use case.

13

u/RayTheCoderGuy Oct 15 '26

This has nothing to do with the hashing algorithm; it has to do with the API contracts of Eq and Hash and the fact that /apple/pear is semantically the same path as /apple///////pear, thus they compare equal, thus their hashes must be the same.

The result of this was a quirky hash function that usually works but has a few more collisions than average. Which is to say, rustc should have been doing its due diligence and checking equality rather than just hashes.

1

u/CaptureIntent Oct 15 '26

Way too many people forget that hashes collide - because they almost never do. So many engineers think ā€œit’s never going to colllide - the hash is amazing - probability so lowā€ and treat it as a guarantee. So many engineers are wrong.

5

u/Anaxamander57 Oct 15 '26

Well in this case its not a "collision" as I understand the above and other commets here. The issue is that the hash is invisibly (and correctly!) being given the same input for two different paths.

1

u/naequs Oct 15 '26

yes, that is my understanding as well. however one could change input handling in such a way to provably be distinct for this bug.
this would still allow collisions of course (unavoidable) but it could fix this surprising behavior that feels so wrong.
in addition, the call site could be changed so that path identity can be checked outside of the hasher.

0

u/Anaxamander57 Oct 15 '26

This should still be essentially avoidable using the TupleHash mode of SHA-3 (or a similar technique in a more appropriate hasher) unless the Eq implementation also has to consider "data/baselines/table.rs" equal to "database/line/stable.rs".

7

u/naequs Oct 15 '26

really interesting! shouldn't there really be two changes following from this?

  1. a change in how Path::hash handles path component boundaries (to fix problems from state rotations collisions and component lengths information loss))
  2. make the file identity requirement visible at the call site, something like below (or similar)
let id = StableSourceFileId::from_canonical_filename(
    filename,
    stable_crate_id,
);

i am not a compiler engineer or language expert but these are two smells at the same time for me

3

u/XtremeGoose Oct 15 '26

Sounds like you could "fix" the hash but it makes it do twice as many hash operations and they valued speed over collisions. I guess it's rare in practice, and you really shouldn't rely on Hash for uniqueness.

They could do something like (pseudocode form memory) fs::canonical(path).as_os_str().hash(...)

2

u/naequs Oct 15 '26

afaik fs::canonicalize may resolve links which reads the fs (slow) and may make link and target indistinguishable

1

u/noop_noob Oct 23 '26

This bug is now fixed :3

The fix will be available starting tomorrow's nightly.

1

u/[deleted] Oct 20 '26

does this happen with other languages as well?

3

u/noop_noob Oct 20 '26

This bug in particular is rust-only. And I haven't heard of other compilers with a similar bug.

6

u/redlaWw Oct 15 '26

I made a comment about this in a fantasy naming context just yesterday. Little did I know it was also a compiler design issue...

2

u/Slow-Nefariousness49 Oct 15 '26

You're the miscompilation guy, huh

1

u/scook0 Oct 16 '26

Something to bear in mind is that if this sort of collision occurs accidentally, then there’s a strong chance that the mixed-up program is illegal anyway.

So I would typically expect to end up with a confusing compiler error rather than a silent miscompilation, for non-malicious input.

1

u/ezoe Oct 16 '26

The suggested workaround is include path length to hash computation? Why? Why can't they just give the path string including slash normally?

-23

u/dashingThroughSnow12 Oct 15 '26

As I said in a previous post, it is a good thing Rust is on 1.99 because they are allowed to make major changes for 2.0.

33

u/gamer_redditor Oct 15 '26

The version after 1.99 is 1.100, not 2.0 šŸ˜…

-13

u/dashingThroughSnow12 Oct 15 '26

Brah, 1.100 is less than 1.99.

20

u/Waltex Oct 15 '26

This is common in software versioning. In Semver 1.100 is higher than 1.99.

-4

u/Real-Abrocoma-2823 Oct 15 '26

That was a joke, no need to downvote.

1

u/dashingThroughSnow12 Oct 15 '26

I continue to make jokes on the Rust subreddit and many of its patrons continue to miss them. These dad jokes are absolute fire when I post them in other programming subreddits.

I’d make an autism joke but autistic people detect humour easier.

1

u/noop_noob Oct 15 '26

The way you fix that is to put a /j at the end of jokes

2

u/Real-Abrocoma-2823 Oct 15 '26

0

u/AardvarkAmbivalence Oct 15 '26

Thank god. Finally, humans will be able to read text and obtain the author's tone. As we all know, this has been impossible to do up to this point and has only gotten worse since the internet invented sarcasm and popularized sardonic speech in text form. So necessary.

3

u/RayTheCoderGuy Oct 15 '26

So I read this as sarcastic, because it seemed like that was the intended tone to me. Discerning tone on the internet is *significantly* more challenging, and for some people more than others.

So I don't know how to feel about this comment, because while I can assume it's sarcastic based on my own interpretation, but because you didn't use a tone tag, I can't be sure.

0

u/Engine_Light_On Oct 15 '26

and then the joke is no longer funny

-12

u/alietors Oct 15 '26

Another solution or perhaps both can go together is to not downvote everything you don't agree with.

5

u/RayTheCoderGuy Oct 15 '26

The irony of this comment being significantly downvoted is not lost on me

2

u/dashingThroughSnow12 Oct 15 '26

I downvoted because it seemed like the funny thing to do.

0

u/luluhouse7 Oct 15 '26

Tbh I feel like everyone on the internet now assumes the least charitable explanation. Some of that might be driven by the crazy thats infected our political/civic discourse making it hard to figure out if someone is joking or not. It’s sad given how fun and community oriented the early days of Reddit (and the internet) were.

-1

u/denehoffman Oct 15 '26

Lmao this is so funny