Live data from Hacker News

Rust to C compiler – 95.9% test pass rate, odd platforms

fractalfir.github.io

51–60 of 264 posts

Re: Rust to C compiler – 95.9% test pass rate, odd platforms

#51
post #27
post #25

Earlier quoted context omitted.

How does the rust compiler assure that when compiling to machine code? Machine code is less safe than C after all.

Machine code is generally much safer than C - e.g. it usually lacks undefined behaviour. If you're unsure about how a given piece of machine code behaves, it's usually sufficient to test it empirically.

No.

Re: Rust to C compiler – 95.9% test pass rate, odd platforms

#52
post #48

Earlier quoted context omitted.

Why wouldn't it?

It could fail if the generated C code triggered Undefined Behavior. For example, signed overflow is UB in C, but defined in Rust. Generated code can't simply use the + operator. C has type-based alias analysis that makes some type casts illegal. Rust handles alias analysis through borrowing, so it's more forgiving about type casts. Rust has an UnsafeCell wrapper type for hacks that break the safe memory model and wou…

I have workarounds for all "simple" cases of UB in C(this is partially what the talk is about). The test code is running with `-fsantize=undefined`, and triggers no UB checks.

There are also escape hatches for strict aliasing in the C standard - mainly using memcpy for all memory operations.

Re: Rust to C compiler – 95.9% test pass rate, odd platforms

#54
post #19
post #17

Earlier quoted context omitted.

this assumes the rusty guarantees are transitive. There's no reason to believe it isn't, but it'd be nice to see some sort of proof, or at least an argument for it.

Should be. The rust borrow checker has no runtime component. It checks the code as-is before (or during) compilation. Arguably it’s not the compiled binary that’s “safe”. It’s the code.

Borrow checker, and more generally any type checkers, are essentially terminating programs ran during compilation process, thus safety guarantees given by them are ensured before the code is transformed into some target language.

Re: Rust to C compiler – 95.9% test pass rate, odd platforms

#55
post #4

I'm not convinced that it’s worth spending any time supporting most proprietary systems. Maybe not even Windows, but especially the really expensive ones.

Funny, because the average person is convinced it's not worth spending any time supporting Linux!

Re: Rust to C compiler – 95.9% test pass rate, odd platforms

#56
post #48

Earlier quoted context omitted.

Why wouldn't it?

It could fail if the generated C code triggered Undefined Behavior. For example, signed overflow is UB in C, but defined in Rust. Generated code can't simply use the + operator. C has type-based alias analysis that makes some type casts illegal. Rust handles alias analysis through borrowing, so it's more forgiving about type casts. Rust has an UnsafeCell wrapper type for hacks that break the safe memory model and wou…

Wait until you find out how unsafe software written in the machine language that Rust usually transpiles to is.

Re: Rust to C compiler – 95.9% test pass rate, odd platforms

#57

If I see something like "At least on Linux, long and long long are both 64 bits in size." my skin starts to crawl. Not only that, but GCC defines __builtin_popcount() with unsigned int / long / long long, respective, i.e. even in the text it should be mentioned correctly (unless a different compiler uses signed types there ... ugh). The call is done with unsigned, using uint64_t as a type-cast, but using a fixed __bu…

Correct me if I am wrong C, unsigned overflow is well-defined - at least the GCC manual says so, but I'll have to check the standard. https://www.gnu.org/software/c-intro-and-ref/manual/html_nod... Since signed multiplication is bitwise-equivalent to unsigned multiplication, I use unsigned multiplication to emulate UB-free signed multiplication. The signed variant of this overflow check is a bit harder to read becaus…

Yes, the C and C++ unsigned types are analogous to Rust's Wrapping Wrapping Wrapping and so on, except that their size isn't nailed down by the ISO document.

Re: Rust to C compiler – 95.9% test pass rate, odd platforms

#58

If I see something like "At least on Linux, long and long long are both 64 bits in size." my skin starts to crawl. Not only that, but GCC defines __builtin_popcount() with unsigned int / long / long long, respective, i.e. even in the text it should be mentioned correctly (unless a different compiler uses signed types there ... ugh). The call is done with unsigned, using uint64_t as a type-cast, but using a fixed __bu…

[deleted]

Re: Rust to C compiler – 95.9% test pass rate, odd platforms

#60

How is this not dangerous? How can one be assured that all of the compile-time safety features of the Rust compiler are still in effect? Handwaving does not help.

How can one be assured that all of the compile-time safety features of Java are is still in effect in bytecode?

The JVM class loader verifies the bytecode:

https://stackoverflow.com/questions/755005/how-does-bytecode...

Post reply on HN