Live data from Hacker News

Translating All C to Rust (TRACTOR)

darpa.mil

101–110 of 403 posts

Re: Translating All C to Rust (TRACTOR)

#101
post #79

Earlier quoted context omitted.

Indeed. There have been UB bugs in the standard library caused by unsafe blocks. Those are bugs. They are faults in the code. They need to be fixed. They are not UB-as-a-feature like in C/C++. “Well watch out for those traps every time you use this.” This is like getting mad that a programming language boasts that it produces great binaries and yet the compiler has a test suite to catch bugs in the emitted assembly.…

> Those are bugs. They are faults in the code. They need to be fixed. They are not UB-as-a-feature like in C/C++. Rust has UB-as-a-feature too. They could have eliminated UB from the language entirely, but they chose not to (for very valid reasons in my opinion). UB is a set of contracts that you as the author agree to never violate. In return, you get faster code under the assumption that you never actually encounte…

> It's true that Rust UB can only arise from unsafe blocks, but it is not limited to unsafe blocks.

This is correct, and it's hard to teach, and I agree that a lot of folks get it wrong. (Here's my attempt: https://jacko.io/safety_and_soundness.html.) But I think this comment is understating how big of a difference this makes:

1. Rust has a large, powerful safe subset, which includes lots of real-world programs. Unsafe code is an advanced topic, and beginners don't need to learn about it to start getting their work done. Beginners can contribute to big projects without touching the unsafe parts (as you clarified, that means the module privacy boundaries that include unsafe code, not just the unsafe blocks), and reviewers don't need to be paranoid about every line.

2. A lot of real-world unsafe Rust is easy to audit, because you can grep for `unsafe` in a big codebase and zoom right to the parts you need to look at. Again, as you pointed out, those blocks might not be the whole story, and you do need to read what they're doing to see how much code they "infect". But an experienced Rust programmer can audit a well-written codebase in minutes. It's not always that smooth of course, but it's a totally different world that that's even possible.

Re: Translating All C to Rust (TRACTOR)

#102
post #79

Earlier quoted context omitted.

> Those are bugs. They are faults in the code. They need to be fixed. They are not UB-as-a-feature like in C/C++. Rust has UB-as-a-feature too. They could have eliminated UB from the language entirely, but they chose not to (for very valid reasons in my opinion). UB is a set of contracts that you as the author agree to never violate. In return, you get faster code under the assumption that you never actually encounte…

> There are good reasons to favor Rust's flavor of UB over C++'s, but I keep seeing these same incorrect arguments getting repeated everywhere, which is frustrating. Tell me what I wrote that was incorrect. I called them UB bugs in the standard library. If they were trivial bugs that caused some defined-behavior logic bug while used outside of the standard library then it wouldn’t rise to the level of being called an…

> They are not UB-as-a-feature like in C/C++.

That's the part that's incorrect. That, plus the implication that UB is a bug in Rust, but not in C++. As I said, the existence of UB is a feature in both languages and actually encountering UB is a bug in both languages. You can play with the semantics of the word "feature" but I don't think it's possible to find a definition that captures C++ UB and excludes Rust UB without falling into a double standard. Unfortunately double standards on UB are pretty common in conversations about C++ and Rust.

Re: Translating All C to Rust (TRACTOR)

#103

This isn't some "pie in the sky" thing, Immunant has a working C to Rust transpiler and it's really interesting: https://github.com/immunant/c2rust

I've tried that thing. The Rust that comes out is terrible. It converts C into a set of Rust function calls which explicitly emulate C semantics by manipulating raw pointers. It doesn't even convert C arrays to a Vec. It's a brute-force transliteration, not a translation.

I and someone else ran this on a JPEG 2000 decoder that sometimes crashed with a bad memory reference. The Rust version crashed with the same bad memory reference. It's bug-compatible.

What comes out is totally unreadable and much bigger than the original C code. Manual "refactoring" of that output is hopeless.

Re: Translating All C to Rust (TRACTOR)

#106

The one link for those who think that 'Rewrite it All in Rust' will, well, settle any debates: https://github.com/rust-lang/miri/

The trophy cases in miri are about bugs in unsafe code. Yes, you can write UB with unsafe code. This should not be news.

And miri is a blessing. There even is a known case where someone found a bug in C by translating it to rust and then running it through miri.

Re: Translating All C to Rust (TRACTOR)

#107

That sounds ... hard. Especially as idiomatic Rust as written by skilled programmers looks nothing like C, and most interesting code is written in C++ anyway. Isn't it equivalent to statically determining the lifetimes of all allocations in the C program, including those that are implemented using custom allocators or which cross into proprietary libraries? There's been a lot of research into this sort of thing over…

Write tests for your C code. Run c2rust (mechanical translation), including the tests. Let a LLM/MCTS/verifier loop go to town. Verifier here means it passes compiler checks, tests, santiziers and miri.

Additional training data can be generated by running mrustc or by inlining unsafe code (from std/core/leaf crates) into safe code and running semantics-preserving mechanical refactorings on the code.

This can be closer to AlphaProof than ChatGPT

Re: Translating All C to Rust (TRACTOR)

#108

I'm personally not a fan of "rewrite the world in Rust" mentality, but that being said, if one is planning to port a project to a new language or platform, mechanical translation is a poor means of doing so. Spend the time planning better architecture and designing a better software system, and find a way to replace it piece by piece. Don't build a castle in the sky, because it will never reach the ground. If you've…

This is definitely a pie-in-the-sky DARPA challenge that would be great to have around as we migrate away from legacy systems, however, even taking your functions/methods in one language and giving them to ChatGPT and asking it to translate your method to a different language generally doesn't work. Asking ChatGPT the initial problem you're trying to solve, works more frequently, but still generally doesn't work. You still need to do a lot of tinkering and thinking to get even basic things to work that it outputs.

Re: Translating All C to Rust (TRACTOR)

#109
i like the idea but i struggle to see how one can go about doing 'safe' disk reads, having 'safe' ways to manage global resources in kernel land (page tables, descriptor tables etc) and a lot of other stuff. perhaps if those devices also have rust in their firmware they can reply safely?? genuinely curious because i went back to C from rust in my OS. i could not figure it out (maybe i am not a darpa level engineer but i did work at a similar place doing similar things).

id be excited if this gets solved. rust is a lot more comfy for higher level kernel stuff.

Re: Translating All C to Rust (TRACTOR)

#110
post #105

Whatever happened to Ada?

It languished in government work behind a wall of extremely expensive compilers and contractors. Never heard anyone suggest RiiA - Rewrite it in Ada.

GCC contains `gnat` which is a libre Ada compiler.

I think Ada has a lot of technical merit but it's just not fashionable the way Rust is, for lots of uninteresting reasons.

Post reply on HN