15 ms·
This article is wonderful and I hope as a setup for second article, "How to Rewrite it in Rust"! > However at best, the temptation to RiiR is unproductive > A
by sitkack 7y ago
This article is wonderful and I hope as a setup for second article, "How to Rewrite it in Rust"!
> However at best, the temptation to RiiR is unproductive
> A much better alternative is to reuse the original library and just publish a safe interface to it.
Just as models are a lower dimensional representation of a more complex problem, [1] I have to re-iterate that there are no truth(ism)s in software and as in all engineering, there are trade offs. RiiR can and often is a valid choice. The author talks about "introducing bugs", under an engineering approach to a rewrite, more like a language-port, rewrites can _find_ a lot of bugs. One such way is as follows.
1. keep the same interface in the new system, client code should work against either system.
2. have a body of integration tests, capture these from the field OR write a small collection of orthogonal tests, somewhere between unit and integration.
3. use the tooling, bindgen, etc and generate as much of the interface programmatically as possible.
4. iterate on the port, doing differential testing against both systems.
Doing a language port is comparable in work to long term maintenance and refactoring of an existing codebase. As the tooling gets better, RiiR will be more of a smooth oxidization.
If you own the C/C++ that could possibly get rewritten, I'd think one needs to rationalize NOT RiiR. Better tooling (IDE, build), perf tooling is improving, low bug count, increased team velocity, build is improved, etc.
But the biggest reason to RiiR is safety. Integrating a body of C/C++ code into your rust codebase introduces a huge amount of unsafe code, much worse than surrounding your entire rust program with unsafe { }.
At least do what is outlined in the article but ALSO compile the native code into Wasm and run it from within a sandbox.
edit, something like a library for reading (parsing) a file format, should absolutely be RiiR or run from a sandbox. Data parsing and memory corruption vulnerabilities are excellent dance partners.
[1] see PCA https://en.wikipedia.org/wiki/Principal_component_analysis https://en.wikipedia.org/wiki/Principal_component_analysis
- minraws 7y agoI would like to add my 2 cents, TBH I don't see a future where RIIR every piece of software would make sense, and I think you sort of put it in there somewhere in your comments. But yes I do see that for many it would and for those, RIIR would IMHO be an incremental process of oxidizing your project to the point that there's nothing left but Rust. Writing something from scratch and waiting for it to finish is the biggest problem we face, cause eventually the will to continue with the effort just dies. But creating a meaningful ground not just some "safer" bindings does help in inviting others to help share the effort as well. So hopefully people will be smart and identify what they should do for their projects, should it be a rewrite or should it be a new feature that you write in Rust. :) And no I don't think bindings are the solution to anything, they serve no purpose in Rust community as a long term measure, you shouldn't have to sacrifice safety and/or performance incase of high level language.
- nicoburns 7y ago> TBH I don't see a future where RIIR every piece of software would make sense It doesn't necessarily have to be Rust, and it's a process that may take decades. But I really do think that absolutely everything should be rewritten in a memory-safe language (i.e. not C/C++). The vast majority of security issues are either stupid misconfigurations or memory-safety issues. And currently most security initiatives are undermined by the fact that they're resting on insecure foundations. Imagine if our computing platforms were truly secure. It would be a revolution, and IMO it's a revolution that's coming.
- pjmlp 7y agoYep, on my domain it has been Java and .NET.
- minraws 7y agoJava and .NET are Safe..... O.o Edit: For the uninitiated, I am baffled by the fact that people think Java and .NET are memory safe. Also if you meant that Java and .NET should be replaced by something memory safe.. I am absolutely for it. :)
- pjmlp 7y agoMore than Rust in multithreaded scenarios, actually. Not only they have automatic memory management, they have an industry standard memory model for hardware access, adopted by C and C++ standards, used as inspiration for std:atomic<> on CUDA. While you keep being baffled, where is Rust's memory model specification?
- minraws 7y agoNot sure where I said, Rust is memory safe.... Though. Just scroll up, I explicitly mentioned how it isn't. I just want to say, Java and C# isn't as safe as most people think. Define industry standard, cause nothing points me towards industry standards actually being better than not industry standards. Has everyone forgotten these http://cs.oswego.edu/pipermail/concurrency-interest/2017-December/016272.html http://cs.oswego.edu/pipermail/concurrency-interest/2017-Dec... Nevertheless who knows I might not know anything, and I love being proved wrong when it makes most of the world a safer place (quite literally)... :P
- deleted 7y ago[deleted]
- pjmlp 7y agoAt least in what concerns C++ there are several ways to try its safety instead of bearing the cost of a full rewrite. Using standard library types, actually integrating sanitizers into CI, enable bounds checking (even on release builds) and above all avoid C style coding. Naturally this doesn't work out for third party libraries that one doesn't have control over. So from a business point of view it boils down how much one is willing to spend re-writing the world vs improving parts of it.
- The_rationalist 7y agoBTW there are garbage collectors for c++
- pjmlp 7y agoYep. C++/CLI, C++/CX, GC pluggable API introduced in C++11, C++ Builder VCL in ARC mode, Unreal C++ managed classes.
- The_rationalist 7y agoDo you know how much c++/cli is automatically compatible with c# code? And how much does it retain compatibility with standard c++?
- pjmlp 7y agoEverything that obeys to CLS. https://docs.microsoft.com/en-us/dotnet/standard/language-independence-and-language-independent-components#Rules https://docs.microsoft.com/en-us/dotnet/standard/language-in... https://docs.microsoft.com/en-us/cpp/dotnet/managed-types-cpp-cli?view=vs-2019 https://docs.microsoft.com/en-us/cpp/dotnet/managed-types-cp... Then you can also use regular low level stuff, but in that case you will get mixed mode Assemblies (other forms are now deprecated). https://docs.microsoft.com/en-us/cpp/dotnet/mixed-native-and-managed-assemblies?view=vs-2019 https://docs.microsoft.com/en-us/cpp/dotnet/mixed-native-and... C++/CLI is basically a set of language extensions, just like clang and gcc have theirs. Right now it supports up to C++14, if I am not mistaken. Many don't seem to realise that CLR started as the next evolution of COM, with the required machinery to support VB, C#, J# and C++, alongside any other language that could fit the same kind of semantics.
- otabdeveloper4 7y agoThere's no such thing as a "C/C++".
- dmit 7y agoAlternatively, you could have C/C++ as a third, discrete category somewhere in the middle of the C <---> C++ spectrum. For people who like namespaces, or overloading arithmetic operators for mathy vector/matrix/etc stuff in order to make the code more readable.
- pjmlp 7y agoApparently there is when reading ISO, Microsoft, Apple, Google, IBM, Microsoft,... documentation. Guess what, we even used to have a famous magazine called "The C/C++ Users Journal".
- makapuf 7y agoAgreed. There Are differences between c and "c in c++" but no more than python 2 and 3 or c++11 and c++17
- keldaris 7y agoWhile you can essentially write C in almost any language, C++ is particularly well suited to it.
- cliffcrosland 7y ago> But the biggest reason to RiiR is safety. 100% agree. We had a C++ service that made heavy use of libcurl. A particular release of libcurl introduced some memory safety problems that caused frequent segfaults for us. These memory safety bugs were eventually fixed in another release, but it scared us enough that we investigated rewriting the service in Rust. After successful experimentation with some prototypes, we eventually rewrote the service in Rust, auditing the unsafe code in our dependencies (of which there was very little). No segfaults ever since. Side benefit: Since Rust networking libraries tend to have strong async support, and since some of our C++ libraries performed synchronous networking operations, we saw a big improvement in performance. The number of threads needed dropped by 5x.
- pron 7y ago> But the biggest reason to rewrite it in Rust is safety. This is not a good reason if there are cheaper ways to get safety (at least for existing codebases). Indeed, there are sound static analysis tools (like TrustInSoft) that guarantee no undefined behaviour in C code. Using them is not completely free, and it may even require adding annotations to the code or even changing the code, but it does seem significantly cheaper than a rewrite (in any language). Such sound static analysis tools are already being used for safety-critical systems in C, and I believe they are more popular than a rewrite in Rust in that domain (where there are reasons not to rewrite in Rust other than just cost, though).
- kragen 7y agoUndefined behavior is not the only kind of bug in C programs, and it's far from clear that fixing all the bugs, or even all the undefined behavior, in an existing C library will be less effort than rewriting it. Consider that one of the worst security bugs in history was a result of Kurt Roeckx eliminating an undefined-behavior bug from OpenSSL.
- pron 7y agoIt's pretty much the only kind of bugs that Rust can prevent, too. > and it's far from clear that fixing all the bugs, or even all the undefined behavior, in an existing C library will be less effort than rewriting it It's pretty clear to me. > Consider that one of the worst security bugs in history I'm not sure what bug you're referring to, but if it's Heartbleed, than that was an undefined behavior bug. Of course, a functional bug can be introduced at any time, including during a rewrite in another language.
- kragen 7y agoNo, I'm talking about the Debian OpenSSL bug Luciano Bello discovered. It was a lot worse than Heartbleed. Kurt Roeckx didn't introduce Heartbleed, and Heartbleed wasn't introduced by removing undefined behavior, so there is no plausible reason for you to infer that I was talking about Heartbleed. As for the cost of rewrites, there's a lot of evidence from software project metrics that the cost of modifying software can easily exceed the cost of rewriting it; see Glass's Facts and Fallacies of Software Engineering for details and references. Also, though, it should be intuitively apparent (though perhaps nonobvious) that this is a consequence of the undecidability of the Halting Problem and Rice's Theorem — it's impossible to tell what a given piece of software will do, which means that the cost of reproducing its existing behavior in well-understood code is unbounded.
- cellularmitosis 7y agoI agree, and I see a lot of analogies between this situation and the ObjC -> Swift transition which many code bases have gone through. When there are significant semantic benefits in the new language, a slow war of attrition against the old language is a good move.