Re:mrustc: In-progress alternative Rust compiler (to C)
by lmkg on Sunday February 28, 2021 @04:06PM
(#26297059)
The interesting thing to me is that this does not fully valid Rust code, e.g. not checking for borrow safety. Which would normally make no damn sense for a Rust compiler, but actually makes sense for what this particular project is trying to achieve.
This is not a compiler for developing Rust programs. This is a compiler to let platforms unsupported by Rust/LLVM build rust programs from source. So it takes a Rust program which already exists, assumes it can be compiled by rustc, and translates it to C so it can be handed off to your local compiler. Skipping the burrow checker probably lets this execute in environments which are more constrained than what rustc can fit into.
Of course there's a question of how much of Rust's safety survive translation into C. Certainly some. E.g. the emitted program would include bounds checks where not specifically elided in Rust, and references are statically guaranteed to be non-null before being lowered into pointers. But there could be gaps, especially in cases where Rust semantics mismatch with C in ways that LLVM hasn't exposed yet.
Re:mrustc: In-progress alternative Rust compiler (to C)
by rmdashrfstar on Sunday February 28, 2021 @04:24PM
(#26297198)
> But there could be gaps, especially in cases where Rust semantics mismatch with C in ways that LLVM hasn't exposed yet.
Could you elaborate on what potential mismatches there currently are between the semantics of Rust and C? Where particularly do you think there might be issues/dragons hiding in the semantics of a translation?
Re:mrustc: In-progress alternative Rust compiler (to C)
by gpm on Sunday February 28, 2021 @04:48PM
(#26297414)
Integer overflows (signed overflows defined to wrap or panic in rust, are undefined behavior in C).
Float integer conversions (overflows are defined to saturate in rust, are undefined behavior in C).
Maybe things like how pointer casts are treated in correct (but unsafe) rust vs C. Or similarly in transmute. Generally it wouldn't surprise me if rust was standardizing a memory model that was subtly different from C.
I'm sure the list goes on, but "that sort of thing".
Re:mrustc: In-progress alternative Rust compiler (to C)
by pornel on Sunday February 28, 2021 @06:27PM
(#26298115)
float to integer saturates in Rust. `99999.0 as u8` gives 255. `(char)99999.0` is UB in C and LLVM. Rust had to add explicit clamping code to float casts, because LLVM compiled nasal demons otherwise: https://github.com/rust-lang/rust/issues/10184
Rust also has different aliasing rules. Stricter than C for references, but more relaxed than C for raw pointers.
Re:mrustc: In-progress alternative Rust compiler (to C)
by gpm on Sunday February 28, 2021 @09:54PM
(#26299234)
I'm not sure if I edited at all, if I did edit it was within a minute or two of my original post.
Edit: Now that I am coming back and reading this though I am going to make an edit, and like normal for substantive edits I'm going to mark it clearly...
Edit2: Or I can't edit, so let's put it here:
A better example than any of the above might be infinite loops, `while (true) {}` (or any similar loop without side effects) is undefined behavior in C, it's defined behavior in rust.
Re:mrustc: In-progress alternative Rust compiler (to C)
by Blikkentrekker on Sunday February 28, 2021 @04:28PM
(#26297238)
> This is not a compiler for developing Rust programs. This is a compiler to let platforms unsupported by Rust/LLVM build rust programs from source. So it takes a Rust program which already exists, assumes it can be compiled by rustc, and translates it to C so it can be handed off to your local compiler. Skipping the burrow checker probably lets this execute in environments which are more constrained than what rustc can fit into.
I would assume that borrow checking uses far less memory than the actual compilation after the compiler has proven the program to be in concordance with the borrow rules.
It more so seems that it is something they did not bother to put too much effort into, as it wasn't necessary, than a legitimate way to reduce a compiler's memory footprint
Re:mrustc: In-progress alternative Rust compiler (to C)
by ris on Sunday February 28, 2021 @05:35PM
(#26297752)
To me the question it raises is - does the rust borrow checker need to be part of the compiler or could it be separated into a standalone component? Is the borrow checker architecture-agnostic?
Re:mrustc: In-progress alternative Rust compiler (to C)
by twic on Sunday February 28, 2021 @05:48PM
(#26297845)
It is architecture-agnostic.
As currently implemented in rustc, it operates on an intermediate representation that has already had name resolution and some simplification done. As such, i think it would be rather hard to separate from the rest of the compiler.
Re:mrustc: In-progress alternative Rust compiler (to C)
by tylerhou on Sunday February 28, 2021 @08:09PM
(#26298730)
The borrow checker is actually fairly simple to implement, if you have the types. To deduce types, you need to parse the grammar, understand the control flow, and resolve any builtins. But you don’t need to emit any further representation.
Re:mrustc: In-progress alternative Rust compiler (to C)
by MR4D on Sunday February 28, 2021 @06:05PM
(#26297955)
It would be a nice addition in use similar to the auto-complete function - when you write a line of code that uses a value the breaks the tiles, it highlights the code right there.
That alone would make rust a much easier language for newbies.
Re:mrustc: In-progress alternative Rust compiler (to C)
by coolreader18 on Sunday February 28, 2021 @03:50PM
(#26296942)
Oh, nice! I didn't realize that development was still happening for that, I thought it had stopped around Rust 1.20. Glad to see that it's still being developed, stuff like this will definitely be important for bootstrapping/"trusting trust".
Re:mrustc: In-progress alternative Rust compiler (to C)
by monocasa on Sunday February 28, 2021 @03:59PM
(#26297004)
When I say "systems", I should have been a little more specific.
There's a bunch of corporate/.gov envs that don't allow binaries to enter their system. All new code has to be compiled from source, on the target system (or at least on that side of the process firewall, it might be an airgapped cluster that can share code). They have C and C++ compilers that they've been compiling from source since the dawn of time, but they don't have a rust compiler. This gives them a mechanism to begin using rust within their process.
Re:mrustc: In-progress alternative Rust compiler (to C)
by Blikkentrekker on Sunday February 28, 2021 @04:32PM
(#26297268)
How did they get the original C compiler there then without allowing binaries? Did they originally write one in assembler?
I remember reading an interesting thought experiment that traced history that investigated what would happen if Dennis Ritchie had put malicious code in the first C compiler that was designed to detect whether the compiler compiled a compiler, and then copied the malicious code into it.
It concluded that tracing the history, that if this were to have happened, then GCC and Clang and many other programming languages would have said malicious code in their compilers that do not show up in the source code with none the wiser.
Re:mrustc: In-progress alternative Rust compiler (to C)
by twic on Sunday February 28, 2021 @06:02PM
(#26297936)
If understand Dennis Ritchie's explanation [1] correctly, he started with a compiler for the B language, itself written in B, and modified it to accept a somewhat different language, which he called C.
Before the B compiler written in B, there was a B compiler written in something else. I couldn't figure out what from that document.
Re:mrustc: In-progress alternative Rust compiler (to C)
by monocasa on Sunday February 28, 2021 @05:34PM
(#26297746)
Epoch creation is covered by a totally different set of process rules, for the reasons you've stated. These rules are mainly intended to make it much harder for someone to gain access to the system if they weren't there when the system initially was blocked off from the world.
Re:mrustc: In-progress alternative Rust compiler (to C)
by josephg on Sunday February 28, 2021 @04:53PM
(#26297440)
Couldn’t they run mrustc on the rust source code on another machine to generate C code for rustc, then copy the C code to their internal systems and compile that into a working rust compiler?
Or would that not count as “compiling from source”?
Re:mrustc: In-progress alternative Rust compiler (to C)
by dataflow on Sunday February 28, 2021 @04:05PM
(#26297048)
Wow I see. Could they not use the initial Rust compilers (and however those were bootstrapped? I have no idea how) to get a Rust compiler going? I'm guessing it's just too painful?
Re:mrustc: In-progress alternative Rust compiler (to C)
by spijdar on Sunday February 28, 2021 @04:09PM
(#26297084)
Bootstrapping rust is really, really painful as is. The rust compiler itself tends to require a recent version of the rust compiler to work. So to bootstrap from the original versions of rustc (which were written in OCaml) you'd have to incrementally step up really slowly.
Being able to target a pretty recent version of Rust with a compiler written in C would be so, so useful for these purposes.
Re:mrustc: In-progress alternative Rust compiler (to C)
by Blikkentrekker on Sunday February 28, 2021 @04:34PM
(#26297287)
Certainly that could be automated quite easily.
I would assume that on such compilation farms that these systems generally use, this could be done very quickly.
Perhaps there would be an interest for the Rustc team to provide a recursive automated setup that is capable of compiling the latest Rustc from OCaml, and finally from C as well since one must follow a similar process with OCaml.
Re:mrustc: In-progress alternative Rust compiler (to C)
by LegionMammal978 on Sunday February 28, 2021 @04:44PM
(#26297374)
The automation for this is actually trickier than it would first appear: especially with the earlier snapshots, a number of patches are needed to compile the LLVM interfaces written in C++. Also, due to linking issues, some snapshots require a stage2 compiler, while others can only use a stage1 compiler. I agree that it would be trivial once you have the chain in place, but that chain can only be built through trial and error.
Re:mrustc: In-progress alternative Rust compiler (to C)
by sanxiyn on Sunday February 28, 2021 @05:32PM
(#26297736)
We are discussing mrustc here. The reason it hasn't been done yet is that nearly everyone interested in this agrees mrustc is a better bootstrapping path than starting from the last OCaml version.
Re:mrustc: In-progress alternative Rust compiler (to C)
by LegionMammal978 on Sunday February 28, 2021 @04:38PM
(#26297326)
I've actually been trying this for real. Right now, I'm at the 82-compiler mark, including the ~6 LLVM versions needed. For a sense of scale, the most recent compiler in this chain is the 2011-12-07 snapshot, which is still older than Rust 0.1 (2012-01-20). In most cases, additional compilers are needed only to resolve syntax errors, but there are occasional LLVM-related segfaults that create most of my headaches.
Re:mrustc: In-progress alternative Rust compiler (to C)
by maccam94 on Sunday February 28, 2021 @04:10PM
(#26297099)
The earliest versions of the rust compiler were written in Ocaml, which is also unlikely to have been built in a trusted way by these organizations. mrustc exists to be a way to bootstrap trusted builds of rustc.
Re:mrustc: In-progress alternative Rust compiler (to C)
by shakow on Sunday February 28, 2021 @04:10PM
(#26297096)
> I'm guessing it's just too painful?
Indeed. Although it is being worked on, the whole bootstrap process of rustc is currently a major hassle, requiring to start from the oldest CaML versions of the compiler up to the most recent ones.
Re:mrustc: In-progress alternative Rust compiler (to C)
by monocasa on Sunday February 28, 2021 @04:13PM
(#26297114)
Without mrustc you'd need to trust the rustc binary, or build around a hundred versions of rust to go back to a rustc that itself didn't depend on rust.
Re:mrustc: In-progress alternative Rust compiler (to C)
by dan-robertson on Sunday February 28, 2021 @04:00PM
(#26297008)
One way to trust your computer system more is to have very few binary dependencies. Ie the goal is to have some source files and hopefully if you start with (eg) a trusted C compiler and linker you can work your way up to the whole system with deterministic results. This is supposed to be a way to prove that the compiler wasn’t doing anything funky like injecting some vulnerability into the code it compiled and injecting that vulnerability injection into any compiler it compiled.
Re:mrustc: In-progress alternative Rust compiler (to C)
by faho on Sunday February 28, 2021 @03:57PM
(#26296993)
Unfortunately rustc uses rust features almost immediately (I think even features introduced in the immediately preceding version?), so you get a bootstrap chain, and if you start with the compiler written in OCaml you now need to build every single rustc version ever released.
This takes very long and is error-prone, so having a compiler that can build even an intermediate version is already a big help.
Re:mrustc: In-progress alternative Rust compiler (to C)
by kiwidrew on Sunday February 28, 2021 @05:16PM
(#26297627)
It's unfortunate that Rust has adopted development policies that are so hostile towards bootstrapping and cross-compiling rustc. The fact that most of the people cheerleading for Rust the ecosystem don't even see this as a problem is enough to convince me to steer clear of anything involving Rust the language.
You don't have a real language until there is a stable specification and multiple independent implementations. Until then it's just an experimental toy.
Re:mrustc: In-progress alternative Rust compiler (to C)
by coldtea on Monday March 01, 2021 @02:45AM
(#26300525)
>is enough to convince me to steer clear of anything involving Rust the language.
This has a "I will cancel my subscription" letters to the editor feel, as if anyone cares :-)
I mean, there are pros and cons to any language and ecosystem. "I found some fault in bootstrapping approach and will condemn the whole language/effort for it" is a hardly a reasonable argument...
As if regular devs often have a need to bootstrap from scratch themselves...
Re:mrustc: In-progress alternative Rust compiler (to C)
by monocasa on Sunday February 28, 2021 @05:32PM
(#26297734)
You can't build GCC on MSVC or vis versa.
And people do see the pain here as a problem, that's why the project in this very thread exists. I think most in the Rust community see mrustc and the pressure that an independent implementation brings process wise as a very good thing for the language.
Re:mrustc: In-progress alternative Rust compiler (to C)
by kiwidrew on Sunday February 28, 2021 @07:23PM
(#26298430)
CPython is the only viable Python interpreter, and the Python language specification is "whatever CPython does". In my humble opinion this disqualifies Python from consideration as a language.
That doesn't mean Python isn't useful! It is in fact a wildly successful tool that has spawned a large ecosystem of packages and a diverse group of users. But it unfortunately does not qualify as a true language, for it lacks the necessary ingredients: a formal specification of the language and more than one implementation of said specification.
This is not a compiler for developing Rust programs. This is a compiler to let platforms unsupported by Rust/LLVM build rust programs from source. So it takes a Rust program which already exists, assumes it can be compiled by rustc, and translates it to C so it can be handed off to your local compiler. Skipping the burrow checker probably lets this execute in environments which are more constrained than what rustc can fit into.
Of course there's a question of how much of Rust's safety survive translation into C. Certainly some. E.g. the emitted program would include bounds checks where not specifically elided in Rust, and references are statically guaranteed to be non-null before being lowered into pointers. But there could be gaps, especially in cases where Rust semantics mismatch with C in ways that LLVM hasn't exposed yet.
Could you elaborate on what potential mismatches there currently are between the semantics of Rust and C? Where particularly do you think there might be issues/dragons hiding in the semantics of a translation?
Float integer conversions (overflows are defined to saturate in rust, are undefined behavior in C).
Maybe things like how pointer casts are treated in correct (but unsafe) rust vs C. Or similarly in transmute. Generally it wouldn't surprise me if rust was standardizing a memory model that was subtly different from C.
I'm sure the list goes on, but "that sort of thing".
Rust also has different aliasing rules. Stricter than C for references, but more relaxed than C for raw pointers.
Edit: Now that I am coming back and reading this though I am going to make an edit, and like normal for substantive edits I'm going to mark it clearly...
Edit2: Or I can't edit, so let's put it here:
A better example than any of the above might be infinite loops, `while (true) {}` (or any similar loop without side effects) is undefined behavior in C, it's defined behavior in rust.
I would assume that borrow checking uses far less memory than the actual compilation after the compiler has proven the program to be in concordance with the borrow rules.
It more so seems that it is something they did not bother to put too much effort into, as it wasn't necessary, than a legitimate way to reduce a compiler's memory footprint
As currently implemented in rustc, it operates on an intermediate representation that has already had name resolution and some simplification done. As such, i think it would be rather hard to separate from the rest of the compiler.
That alone would make rust a much easier language for newbies.
I wonder how these two projects compare.
There's a bunch of corporate/.gov envs that don't allow binaries to enter their system. All new code has to be compiled from source, on the target system (or at least on that side of the process firewall, it might be an airgapped cluster that can share code). They have C and C++ compilers that they've been compiling from source since the dawn of time, but they don't have a rust compiler. This gives them a mechanism to begin using rust within their process.
I remember reading an interesting thought experiment that traced history that investigated what would happen if Dennis Ritchie had put malicious code in the first C compiler that was designed to detect whether the compiler compiled a compiler, and then copied the malicious code into it.
It concluded that tracing the history, that if this were to have happened, then GCC and Clang and many other programming languages would have said malicious code in their compilers that do not show up in the source code with none the wiser.
Before the B compiler written in B, there was a B compiler written in something else. I couldn't figure out what from that document.
[1] https://www.bell-labs.com/usr/dmr/www/chist.html
Or would that not count as “compiling from source”?
Being able to target a pretty recent version of Rust with a compiler written in C would be so, so useful for these purposes.
I would assume that on such compilation farms that these systems generally use, this could be done very quickly.
Perhaps there would be an interest for the Rustc team to provide a recursive automated setup that is capable of compiling the latest Rustc from OCaml, and finally from C as well since one must follow a similar process with OCaml.
Methinks that building this chain by trial and error is rather trivial compared to the actual work in building a compiler.
Since them they've bootstrapped this all the way to 1.50: https://git.savannah.gnu.org/cgit/guix.git/tree/gnu/packages...
The work done here is non trivial, this chain of versions are required for the final binary to be reproducible.
Typically, they are inversely correlated.
Indeed. Although it is being worked on, the whole bootstrap process of rustc is currently a major hassle, requiring to start from the oldest CaML versions of the compiler up to the most recent ones.
I guess you didn't want to advertise the real reason of the project.
I hope there's some automated assistance for scanning inbound source code too. Imagine reviewing everything, line by line, millions of them.
This takes very long and is error-prone, so having a compiler that can build even an intermediate version is already a big help.
See e.g. https://guix.gnu.org/blog/2018/bootstrapping-rust/
You don't have a real language until there is a stable specification and multiple independent implementations. Until then it's just an experimental toy.
This has a "I will cancel my subscription" letters to the editor feel, as if anyone cares :-)
I mean, there are pros and cons to any language and ecosystem. "I found some fault in bootstrapping approach and will condemn the whole language/effort for it" is a hardly a reasonable argument...
As if regular devs often have a need to bootstrap from scratch themselves...
And people do see the pain here as a problem, that's why the project in this very thread exists. I think most in the Rust community see mrustc and the pressure that an independent implementation brings process wise as a very good thing for the language.
It should probably be still functional, or easily fixed if not.
That doesn't mean Python isn't useful! It is in fact a wildly successful tool that has spawned a large ecosystem of packages and a diverse group of users. But it unfortunately does not qualify as a true language, for it lacks the necessary ingredients: a formal specification of the language and more than one implementation of said specification.